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
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]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
[481]13#include "filesel.h"
14#include "input.h"
15#include "scroller.h"
16#include "jdir.h"
[2]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; }
[106]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);
[120]29  virtual void note_selection(image *screen, InputManager *inm, int x);
[2]30  void free_up();
31  ~file_picker() { free_up(); }
[124]32} ;
[2]33
34void file_picker::free_up()
35{
36  int i=0;
[494]37  for (; i<tf; i++)
[129]38    free(f[i]);
[494]39  for (i=0; i<td; i++)
[129]40    free(d[i]);
41  if (tf) free(f);
42  if (td) free(d);
[2]43}
44
[120]45void file_picker::note_selection(image *screen, InputManager *inm, int x)
[2]46{
47  if (x<td)
48  {
49    if (strcmp(d[x],"."))
50    {
51      int x1,y1,x2,y2;
[106]52      area(x1,y1,x2,y2);
[2]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;
[494]66      for (; i<tf; i++)
[4]67      if ((int)strlen(f[i])>wid) wid=strlen(f[i]);
[494]68      for (i=0; i<td; i++)
[4]69      if ((int)strlen(d[i])+2>wid) wid=strlen(d[i])+2;
[2]70      sx=0;
71
72
[124]73
74      reconfigure();
[106]75      draw_first(screen);
[2]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);
[106]82    link->change_data(nm,strlen(nm),1,screen);
[2]83  }
84
85}
86
[106]87void file_picker::draw_item(image *screen, int x, int y, int num, int active)
[2]88{
89  if (active)
[106]90    screen->bar(x,y,x+item_width()-1,y+item_height()-1,wm->black());
[2]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{
[124]104
[2]105  sid=ID;
106
107  strcpy(cd,".");
[124]108
[2]109  get_directory(cd,f,tf,d,td);
110  wid=0;
111  int i=0;
[494]112  for (; i<tf; i++)
[4]113    if ((int)strlen(f[i])>wid) wid=strlen(f[i]);
[494]114  for (i=0; i<td; i++)
[4]115    if ((int)strlen(d[i])+2>wid) wid=strlen(d[i])+2;
[124]116  reconfigure();
[2]117}
118
[120]119Jwindow *file_dialog(char const *prompt, char const *def,
[124]120             int ok_id, char const *ok_name, int cancel_id,
[39]121                     char const *cancel_name, char const *FILENAME_str,
122                     int filename_id)
[2]123{
[111]124  int wh2 = 5 + wm->font()->height() + 5;
125  int wh3 = wh2 + wm->font()->height() + 12;
[120]126  Jwindow *j=wm->new_window(0,0,-1,-1,
[124]127                new info_field(5, 5, 0, prompt,
[111]128                            new text_field(0, wh2, filename_id,
[124]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))))),
[2]134
[124]135                FILENAME_str);
[2]136  return j;
137}
138
139
140
141
142
Note: See TracBrowser for help on using the repository browser.