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

Last change on this file since 668 was 668, checked in by Sam Hocevar, 12 years ago

imlib: make JCFont use vec2i classes.

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