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

Last change on this file since 534 was 524, checked in by Sam Hocevar, 12 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
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
[524]13#include <unistd.h>
14
[512]15#include "common.h"
16
[481]17#include "filesel.h"
18#include "input.h"
19#include "scroller.h"
20#include "jdir.h"
[2]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; }
[106]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);
[120]33  virtual void note_selection(image *screen, InputManager *inm, int x);
[2]34  void free_up();
35  ~file_picker() { free_up(); }
[124]36} ;
[2]37
38void file_picker::free_up()
39{
40  int i=0;
[494]41  for (; i<tf; i++)
[129]42    free(f[i]);
[494]43  for (i=0; i<td; i++)
[129]44    free(d[i]);
45  if (tf) free(f);
46  if (td) free(d);
[2]47}
48
[120]49void file_picker::note_selection(image *screen, InputManager *inm, int x)
[2]50{
51  if (x<td)
52  {
53    if (strcmp(d[x],"."))
54    {
55      int x1,y1,x2,y2;
[106]56      area(x1,y1,x2,y2);
[2]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;
[494]70      for (; i<tf; i++)
[4]71      if ((int)strlen(f[i])>wid) wid=strlen(f[i]);
[494]72      for (i=0; i<td; i++)
[4]73      if ((int)strlen(d[i])+2>wid) wid=strlen(d[i])+2;
[2]74      sx=0;
75
76
[124]77
78      reconfigure();
[106]79      draw_first(screen);
[2]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);
[106]86    link->change_data(nm,strlen(nm),1,screen);
[2]87  }
88
89}
90
[106]91void file_picker::draw_item(image *screen, int x, int y, int num, int active)
[2]92{
93  if (active)
[106]94    screen->bar(x,y,x+item_width()-1,y+item_height()-1,wm->black());
[2]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{
[124]108
[2]109  sid=ID;
110
111  strcpy(cd,".");
[124]112
[2]113  get_directory(cd,f,tf,d,td);
114  wid=0;
115  int i=0;
[494]116  for (; i<tf; i++)
[4]117    if ((int)strlen(f[i])>wid) wid=strlen(f[i]);
[494]118  for (i=0; i<td; i++)
[4]119    if ((int)strlen(d[i])+2>wid) wid=strlen(d[i])+2;
[124]120  reconfigure();
[2]121}
122
[120]123Jwindow *file_dialog(char const *prompt, char const *def,
[124]124             int ok_id, char const *ok_name, int cancel_id,
[39]125                     char const *cancel_name, char const *FILENAME_str,
126                     int filename_id)
[2]127{
[111]128  int wh2 = 5 + wm->font()->height() + 5;
129  int wh3 = wh2 + wm->font()->height() + 12;
[120]130  Jwindow *j=wm->new_window(0,0,-1,-1,
[124]131                new info_field(5, 5, 0, prompt,
[111]132                            new text_field(0, wh2, filename_id,
[124]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))))),
[2]138
[124]139                FILENAME_str);
[2]140  return j;
141}
142
143
144
145
146
Note: See TracBrowser for help on using the repository browser.