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

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

core: Get rid of mostly useless headers, move endianness handling to
common.h (and rewrite functions so that they do not need the SDL headers)
and move a few functions out of sdlport's video.cpp. These functions
were in the original video.cpp (which reappears) and shouldn't be part
of the SDL port.

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