source: abuse/trunk/src/imlib/filesel.cpp

Last change on this file was 682, checked in by Sam Hocevar, 8 years ago

core: rename vec2i to ivec2 and update matrix.h from Lol Engine.

File size: 3.7 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <unistd.h>
16
17#include "common.h"
18
19#include "filesel.h"
20#include "input.h"
21#include "scroller.h"
22#include "jdir.h"
23
24class file_picker : public spicker
25{
26  char **f,**d;
27  int tf,td,wid,sid;
28  char cd[300];
29  public:
30  file_picker(int X, int Y, int ID, int Rows, ifield *Next);
31  virtual int total() { return tf+td; }
32  virtual int item_width() { return wm->font()->Size().x * wid; }
33  virtual int item_height() { return wm->font()->Size().y + 1; }
34  virtual void draw_item(image *screen, int x, int y, int num, int active);
35  virtual void note_selection(image *screen, InputManager *inm, int x);
36  void free_up();
37  ~file_picker() { free_up(); }
38} ;
39
40void file_picker::free_up()
41{
42  int i=0;
43  for (; i<tf; i++)
44    free(f[i]);
45  for (i=0; i<td; i++)
46    free(d[i]);
47  if (tf) free(f);
48  if (td) free(d);
49}
50
51void file_picker::note_selection(image *screen, InputManager *inm, int x)
52{
53  if (x<td)
54  {
55#if !defined __CELLOS_LV2__
56    if (strcmp(d[x],"."))
57    {
58      int x1,y1,x2,y2;
59      area(x1,y1,x2,y2);
60      screen->Bar(ivec2(x1, y1), ivec2(x2, y2), wm->medium_color());
61
62      char st[200],curdir[200];
63      sprintf(st,"%s/%s",cd,d[x]);
64      getcwd(curdir,200);
65      chdir(st);
66      getcwd(cd,200);
67      chdir(curdir);
68
69      free_up();
70      get_directory(cd,f,tf,d,td);
71      wid=0;
72      int i=0;
73      for (; i<tf; i++)
74      if ((int)strlen(f[i])>wid) wid=strlen(f[i]);
75      for (i=0; i<td; i++)
76      if ((int)strlen(d[i])+2>wid) wid=strlen(d[i])+2;
77      sx=0;
78
79
80
81      reconfigure();
82      draw_first(screen);
83    }
84#endif
85  } else
86  {
87    char nm[200];
88    sprintf(nm,"%s/%s",cd,f[x-td]);
89    text_field *link=(text_field *)inm->get(sid);
90    link->change_data(nm,strlen(nm),1,screen);
91  }
92
93}
94
95void file_picker::draw_item(image *screen, int x, int y, int num, int active)
96{
97    if (active)
98        screen->Bar(ivec2(x, y),
99                    ivec2(x + item_width() - 1, y + item_height() - 1),
100                    wm->black());
101
102    char st[100], *dest;
103    if (num >= td)
104        dest = f[num - td];
105    else
106        sprintf(dest = st, "<%s>", d[num]);
107
108    wm->font()->PutString(screen, ivec2(x, y), dest, wm->bright_color());
109}
110
111file_picker::file_picker(int X, int Y, int ID, int Rows, ifield *Next)
112  : spicker(X,Y,0,Rows,1,1,0,Next)
113{
114
115  sid=ID;
116
117  strcpy(cd,".");
118
119  get_directory(cd,f,tf,d,td);
120  wid=0;
121  int i=0;
122  for (; i<tf; i++)
123    if ((int)strlen(f[i])>wid) wid=strlen(f[i]);
124  for (i=0; i<td; i++)
125    if ((int)strlen(d[i])+2>wid) wid=strlen(d[i])+2;
126  reconfigure();
127}
128
129Jwindow *file_dialog(char const *prompt, char const *def,
130             int ok_id, char const *ok_name, int cancel_id,
131                     char const *cancel_name, char const *FILENAME_str,
132                     int filename_id)
133{
134  int wh2 = 5 + wm->font()->Size().y + 5;
135  int wh3 = wh2 + wm->font()->Size().y + 12;
136  Jwindow *j=wm->CreateWindow(ivec2(0), ivec2(-1),
137                new info_field(5, 5, 0, prompt,
138                            new text_field(0, wh2, filename_id,
139                       ">","****************************************",def,
140                new button(50, wh3, ok_id, ok_name,
141                new button(100, wh3, cancel_id, cancel_name,
142                new file_picker(15, wh3 + wm->font()->Size().y + 10, filename_id, 8,
143                      NULL))))),
144
145                FILENAME_str);
146  return j;
147}
148
149
150
151
152
Note: See TracBrowser for help on using the repository browser.