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

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

imlib: use vec2i for image::size and unroll all necessary changes
everywhere else in the code.

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