source: abuse/branches/lol/src/imlib/filesel.cpp @ 732

Last change on this file since 732 was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

File size: 3.7 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2013 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, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined HAVE_UNISTD_H
16#   include <unistd.h>
17#endif
18
19#include "common.h"
20
21#include "imlib/filesel.h"
22#include "imlib/input.h"
23#include "imlib/scroller.h"
24
25#include "jdir.h"
26
27class AFilePicker : public AScrollPicker
28{
29public:
30    AFilePicker(ivec2 pos, int id, int rows);
31    ~AFilePicker() { free_up(); }
32
33    virtual int total() { return tf+td; }
34    virtual int item_width() { return wm->font()->Size().x * wid; }
35    virtual int item_height() { return wm->font()->Size().y + 1; }
36    virtual void DrawItem(AImage *screen, ivec2 pos, int num, int active);
37    virtual void note_selection(AImage *screen, InputManager *inm, int x);
38    void free_up();
39
40private:
41    char **f,**d;
42    int tf,td,wid,sid;
43    char cd[300];
44};
45
46void AFilePicker::free_up()
47{
48  int i=0;
49  for (; i<tf; i++)
50    free(f[i]);
51  for (i=0; i<td; i++)
52    free(d[i]);
53  if (tf) free(f);
54  if (td) free(d);
55}
56
57void AFilePicker::note_selection(AImage *screen, InputManager *inm, int x)
58{
59  if (x<td)
60  {
61#if !defined __CELLOS_LV2__
62    if (strcmp(d[x],"."))
63    {
64      ibox2 area = GetArea();
65      screen->Bar(area.A, area.B, wm->medium_color());
66
67      char st[200],curdir[200];
68      sprintf(st,"%s/%s",cd,d[x]);
69      getcwd(curdir,200);
70      chdir(st);
71      getcwd(cd,200);
72      chdir(curdir);
73
74      free_up();
75      get_directory(cd,f,tf,d,td);
76      wid=0;
77      int i=0;
78      for (; i<tf; i++)
79        if ((int)strlen(f[i])>wid)
80          wid=strlen(f[i]);
81      for (i=0; i<td; i++)
82        if ((int)strlen(d[i])+2>wid)
83          wid=strlen(d[i])+2;
84      sx=0;
85
86      reconfigure();
87      draw_first(screen);
88    }
89#endif
90  } else
91  {
92    char nm[200];
93    sprintf(nm,"%s/%s",cd,f[x-td]);
94    ATextField *link=(ATextField *)inm->get(sid);
95    link->change_data(nm,strlen(nm),1,screen);
96  }
97
98}
99
100void AFilePicker::DrawItem(AImage *screen, ivec2 pos, int num, int active)
101{
102    if (active)
103        screen->Bar(pos,
104                    pos + ivec2(item_width() - 1, item_height() - 1),
105                    wm->black());
106
107    char st[100], *dest;
108    if (num >= td)
109        dest = f[num - td];
110    else
111        sprintf(dest = st, "<%s>", d[num]);
112
113    wm->font()->PutString(screen, pos, dest, wm->bright_color());
114}
115
116AFilePicker::AFilePicker(ivec2 pos, int id, int Rows)
117  : AScrollPicker(pos, 0, Rows, 1, 1, 0)
118{
119    sid = id;
120    strcpy(cd, ".");
121
122    get_directory(cd,f,tf,d,td);
123    wid=0;
124    int i=0;
125    for (; i<tf; i++)
126        if ((int)strlen(f[i])>wid) wid=strlen(f[i]);
127    for (i=0; i<td; i++)
128        if ((int)strlen(d[i])+2>wid) wid=strlen(d[i])+2;
129    reconfigure();
130}
131
132AWindow *file_dialog(char const *prompt, char const *def,
133                     int ok_id, char const *ok_name, int cancel_id,
134                     char const *cancel_name, char const *FILENAME_str,
135                     int filename_id)
136{
137    int wh2 = 5 + wm->font()->Size().y + 5;
138    int wh3 = wh2 + wm->font()->Size().y + 12;
139
140    AWidgetList widgets;
141    widgets << new AInfoField(ivec2(5, 5), 0, prompt);
142    widgets << new ATextField(ivec2(0, wh2), filename_id, ">", "****************************************", def);
143    widgets << new AButton(ivec2(50, wh3), ok_id, ok_name);
144    widgets << new AButton(ivec2(100, wh3), cancel_id, cancel_name);
145    widgets << new AFilePicker(ivec2(15, wh3 + wm->font()->Size().y + 10), filename_id, 8);
146    return wm->CreateWindow(ivec2::zero, ivec2(-1), FILENAME_str, widgets);
147}
148
149
150
151
152
Note: See TracBrowser for help on using the repository browser.