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

Last change on this file since 494 was 494, checked in by Sam Hocevar, 11 years ago

style: remove trailing spaces, fix copyright statements.

File size: 3.4 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 or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#include "filesel.h"
14#include "input.h"
15#include "scroller.h"
16#include "jdir.h"
17
18class file_picker : public spicker
19{
20  char **f,**d;
21  int tf,td,wid,sid;
22  char cd[300];
23  public:
24  file_picker(int X, int Y, int ID, int Rows, ifield *Next);
25  virtual int total() { return tf+td; }
26  virtual int item_width() { return wm->font()->width()*wid; }
27  virtual int item_height() { return wm->font()->height()+1; }
28  virtual void draw_item(image *screen, int x, int y, int num, int active);
29  virtual void note_selection(image *screen, InputManager *inm, int x);
30  void free_up();
31  ~file_picker() { free_up(); }
32} ;
33
34void file_picker::free_up()
35{
36  int i=0;
37  for (; i<tf; i++)
38    free(f[i]);
39  for (i=0; i<td; i++)
40    free(d[i]);
41  if (tf) free(f);
42  if (td) free(d);
43}
44
45void file_picker::note_selection(image *screen, InputManager *inm, int x)
46{
47  if (x<td)
48  {
49    if (strcmp(d[x],"."))
50    {
51      int x1,y1,x2,y2;
52      area(x1,y1,x2,y2);
53      screen->bar(x1,y1,x2,y2,wm->medium_color());
54
55      char st[200],curdir[200];
56      sprintf(st,"%s/%s",cd,d[x]);
57      getcwd(curdir,200);
58      chdir(st);
59      getcwd(cd,200);
60      chdir(curdir);
61
62      free_up();
63      get_directory(cd,f,tf,d,td);
64      wid=0;
65      int i=0;
66      for (; i<tf; i++)
67      if ((int)strlen(f[i])>wid) wid=strlen(f[i]);
68      for (i=0; i<td; i++)
69      if ((int)strlen(d[i])+2>wid) wid=strlen(d[i])+2;
70      sx=0;
71
72
73
74      reconfigure();
75      draw_first(screen);
76    }
77  } else
78  {
79    char nm[200];
80    sprintf(nm,"%s/%s",cd,f[x-td]);
81    text_field *link=(text_field *)inm->get(sid);
82    link->change_data(nm,strlen(nm),1,screen);
83  }
84
85}
86
87void file_picker::draw_item(image *screen, int x, int y, int num, int active)
88{
89  if (active)
90    screen->bar(x,y,x+item_width()-1,y+item_height()-1,wm->black());
91
92  if (num<td)
93  {
94    char st[100];
95    sprintf(st,"<%s>",d[num]);
96    wm->font()->put_string(screen,x,y,st,wm->bright_color());
97  } else
98    wm->font()->put_string(screen,x,y,f[num-td],wm->bright_color());
99}
100
101file_picker::file_picker(int X, int Y, int ID, int Rows, ifield *Next)
102  : spicker(X,Y,0,Rows,1,1,0,Next)
103{
104
105  sid=ID;
106
107  strcpy(cd,".");
108
109  get_directory(cd,f,tf,d,td);
110  wid=0;
111  int i=0;
112  for (; i<tf; i++)
113    if ((int)strlen(f[i])>wid) wid=strlen(f[i]);
114  for (i=0; i<td; i++)
115    if ((int)strlen(d[i])+2>wid) wid=strlen(d[i])+2;
116  reconfigure();
117}
118
119Jwindow *file_dialog(char const *prompt, char const *def,
120             int ok_id, char const *ok_name, int cancel_id,
121                     char const *cancel_name, char const *FILENAME_str,
122                     int filename_id)
123{
124  int wh2 = 5 + wm->font()->height() + 5;
125  int wh3 = wh2 + wm->font()->height() + 12;
126  Jwindow *j=wm->new_window(0,0,-1,-1,
127                new info_field(5, 5, 0, prompt,
128                            new text_field(0, wh2, filename_id,
129                       ">","****************************************",def,
130                new button(50, wh3, ok_id, ok_name,
131                new button(100, wh3, cancel_id, cancel_name,
132                new file_picker(15, wh3 + wm->font()->height() + 10, filename_id, 8,
133                      NULL))))),
134
135                FILENAME_str);
136  return j;
137}
138
139
140
141
142
Note: See TracBrowser for help on using the repository browser.