Debian Codegraph @

source/filewin.cc

raw
1 // ----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2003-2013 Fons Adriaensen <[email protected]>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // ----------------------------------------------------------------------------
19
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <dirent.h>
28 #include <X11/keysym.h>
29 #include "filewin.h"
30 #include "styles.h"
31
32
33 Filewin::Filewin (X_window *parw, X_resman *xres) :
34 X_window (parw, XPOS, YPOS, XMIN, YMIN, XftColors.main_bg->pixel),
35 _xs (XMIN),
36 _ys (YMIN),
37 _home (0),
38 _sess (0),
39 _callb (0),
40 _cbarg (0)
41 {
42 X_hints H;
43
44 _xatom = XInternAtom (dpy (), "WM_DELETE_WINDOW", True);
45 XSetWMProtocols (dpy (), win (), &_xatom, 1);
46 _xatom = XInternAtom (dpy (), "WM_PROTOCOLS", True);
47
48 H.size (XMIN, YMIN);
49 H.minsize (XMIN, YMIN);
50 H.maxsize (XMAX, YMAX);
51 H.position (XPOS, YPOS);
52 x_apply (&H);
53 x_set_title ("Aliki");
54 x_add_events (ExposureMask | StructureNotifyMask);
55
56 _tfpath = new X_textip (this, 0, &Tst0, 0, 0, XMAX, 18, 1023);
57 _mclist = new X_mclist (this, this, &Lst0, LM, 19, _xs - LM - RM, _ys - 94, 1000, 50000);
58 _scroll = new X_scroll (this, this, &Bst1, LM, _ys - 74, _xs - LM - RM, 13, 0);
59 _scroll->x_set_win_gravity (SouthWestGravity);
60
61 _wbottom = new X_window (this, 0, _ys - 60, _xs, 60, XftColors.main_bg->pixel);
62 _wbottom->x_set_win_gravity (SouthWestGravity);
63 _wbottom->x_map ();
64
65 Bst0.size.x = 60;
66 Bst0.size.y = 18;
67 _tfname = new X_textip (_wbottom, this, &Tst1, 15, 10, 350, 18, 63);
68 _baction = new X_tbutton (_wbottom, this, &Bst0, 380, 10, "", 0, B_ACTION);
69 _bcancel = new X_tbutton (_wbottom, this, &Bst0, 380, 35, "Cancel", 0, B_CANCEL);
70 _bnewdir = new X_tbutton (_wbottom, this, &Bst0, 15, 35, "New dir", 0, B_NEWDIR);
71 _bdelete = new X_tbutton (_wbottom, this, &Bst0, 75, 35, "Delete", 0, B_DELETE);
72 _bparent = new X_tbutton (_wbottom, this, &Bst0, 185, 35, "Up dir", 0, B_PARENT);
73 _bgohome = new X_tbutton (_wbottom, this, &Bst0, 245, 35, "Home", 0, B_GOHOME);
74 _bgosess = new X_tbutton (_wbottom, this, &Bst0, 305, 35, "Session", 0, B_GOSESS);
75 _bcancel->set_stat (1);
76 _baction->set_stat (2);
77
78 _tfpath->x_map ();
79 _tfname->x_map ();
80 _mclist->x_map ();
81 _scroll->x_map ();
82 _baction->x_map ();
83 _bcancel->x_map ();
84 _bnewdir->x_map ();
85 _bdelete->x_map ();
86 _bparent->x_map ();
87 _bgohome->x_map ();
88 _bgosess->x_map ();
89 }
90
91
92 Filewin::~Filewin (void)
93 {
94 delete _mclist;
95 }
96
97
98 void Filewin::handle_callb (int type, X_window *W, _XEvent *E)
99 {
100 X_textip *T;
101 X_button *B;
102
103 switch (type)
104 {
105 case TEXTIP | X_textip::BUT:
106 T = (X_textip *) W;
107 T->enable ();
108 break;
109 case TEXTIP | X_textip::KEY:
110 T = (X_textip *) W;
111 if (T->key () == XK_Return) action ();
112 if (T->key () == XK_Escape) hide (CB_FILE_CANC);
113 break;
114 case MCLIST | X_mclist::SEL:
115 select ((XButtonEvent *) E);
116 break;
117 case BUTTON | X_button::RELSE:
118 B = (X_button *) W;
119 switch (B->cbid ())
120 {
121 case B_CANCEL:
122 hide (CB_FILE_CANC);
123 break;
124 case B_ACTION:
125 action ();
126 break;
127 case B_NEWDIR:
128 newdir ();
129 break;
130 case B_DELETE:
131 remove ();
132 break;
133 case B_PARENT:
134 scandir ("..", _fext);
135 break;
136 case B_GOHOME:
137 if (_home) scandir (_home, _fext);
138 break;
139 case B_GOSESS:
140 if (_sess) scandir (_sess, _fext);
141 break;
142 }
143 break;
144 case SCROLL | X_scroll::MOVE:
145 _mclist->move ((int)(_scroll->offs () * (_mclist->span () - _xs) + 0.5f));
146 break;
147 case SCROLL | X_scroll::C1LU:
148 case SCROLL | X_scroll::C3RD:
149 _scroll->move (-1.0f);
150 _mclist->move ((int)(_scroll->offs () * (_mclist->span () - _xs) + 0.5f));
151 break;
152 case SCROLL | X_scroll::C1RD:
153 case SCROLL | X_scroll::C3LU:
154 _scroll->move (1.0f);
155 _mclist->move ((int)(_scroll->offs () * (_mclist->span () - _xs) + 0.5f));
156 break;
157 }
158 }
159
160
161 void Filewin::handle_event (XEvent *E)
162 {
163 switch (E->type)
164 {
165 case Expose:
166 expose ((XExposeEvent *) E);
167 break;
168 case ConfigureNotify:
169 resize ((XConfigureEvent *) E);
170 break;
171 case ClientMessage:
172 xcmesg ((XClientMessageEvent *) E);
173 break;
174 }
175 }
176
177
178 void Filewin::expose (XExposeEvent *E)
179 {
180 if (E->count == 0) redraw ();
181 }
182
183
184 void Filewin::resize (XConfigureEvent *E)
185 {
186 while (XCheckTypedWindowEvent (dpy (), win (), ConfigureNotify, (XEvent *) E) == True);
187 if ((_xs != E->width) || (_ys != E->height))
188 {
189 _xs = E->width;
190 _ys = E->height;
191 _mclist->resize (_xs - LM - RM, _ys - 94);
192 _scroll->geom (0.0f, (_mclist->span () > _xs - LM - RM) ? (float) _xs / _mclist->span () : 1.0f, _xs - LM - RM);
193 }
194 }
195
196
197 void Filewin::xcmesg (XClientMessageEvent *E)
198 {
199 if (E->message_type == _xatom) hide (CB_FILE_CANC);
200 }
201
202
203 void Filewin::redraw (void)
204 {
205 X_draw D (dpy (), win (), dgc (), 0);
206 D.clearwin ();
207 D.setfunc (GXcopy);
208 D.setcolor (Colors.main_ds);
209 D.move (LM, 18);
210 D.rdraw (_xs - LM - RM, 0);
211 D.move (0, _ys - 62);
212 D.rdraw (_xs, 0);
213 D.setcolor (Colors.main_ls);
214 D.move (0, _ys - 61);
215 D.rdraw (_xs, 0);
216 }
217
218
219 void Filewin::open (X_callback *callb, int cbarg, const char *path, const char *fext, const char *file)
220 {
221 if (_callb) return;
222 _callb = callb;
223 _cbarg = cbarg;
224 switch (cbarg)
225 {
226 case CB_FILE_SESS:
227 _baction->set_text ("Select", 0);
228 break;
229 case CB_FILE_LOAD:
230 _baction->set_text ("Load", 0);
231 break;
232 case CB_FILE_SAVE:
233 _baction->set_text ("Save", 0);
234 break;
235 default:
236 _baction->set_text ("???", 0);
237 }
238 _baction->redraw ();
239 _tfname->set_text (file ? file : "");
240 show (path, fext);
241 }
242
243
244 void Filewin::show (const char *path, const char *fext)
245 {
246 strncpy (_fext, fext, 64);
247 _fext [63] = 0;
248 getcwd (_cdir, 1024);
249 x_mapraised ();
250 scandir (path, fext);
251 }
252
253
254 void Filewin::hide (int cbarg)
255 {
256 chdir (_cdir);
257 x_unmap ();
258 _callb->handle_callb (cbarg, 0, 0);
259 _callb = 0;
260 }
261
262
263 void Filewin::scandir (const char *path, const char *fext)
264 {
265 DIR *D;
266 struct dirent *E;
267 struct stat S;
268 int k, ke, kn;
269
270 if (fext) ke = strlen (fext);
271 else ke = 0;
272 if (chdir (path))
273 {
274 perror ("chdir");
275 return;
276 }
277 if (getcwd (_wdir, 1024) == 0)
278 {
279 perror ("getcwd");
280 return;
281 }
282 x_clear ();
283 redraw ();
284 if ((D = opendir (".")) == 0)
285 {
286 perror ("opendir");
287 return;
288 }
289 _mclist->reset ();
290 while (1)
291 {
292 if ((E = readdir (D)) == 0) break;
293 kn = strlen (E->d_name);
294 if (stat (E->d_name, &S)) k = 3;
295 else if (S_ISREG (S.st_mode))
296 {
297 if (ke && ((kn < ke) || strcmp (E->d_name + kn - ke, fext))) continue;
298 k = 0;
299 }
300 else if (S_ISDIR (S.st_mode)) k = 1;
301 else k = 2;
302 _mclist->item (E->d_name, k, kn);
303 }
304 closedir (D);
305 _mclist->sort ();
306 _mclist->show ();
307 _tfpath->set_text (_wdir);
308 k = _mclist->span ();
309 _scroll->geom (0.0f, (k > _xs) ? (float) _xs / k : 1.0f);
310 }
311
312
313 void Filewin::select (XButtonEvent *E)
314 {
315 if ((_mclist->col () != 1) || (E->state & ShiftMask))
316 {
317 _tfname->set_text (_mclist->ptr ());
318 _tfname->enable ();
319 }
320 else
321 {
322 scandir (_mclist->ptr (), _fext);
323 }
324 }
325
326
327 void Filewin::action ()
328 {
329 char s [64];
330 struct stat S;
331
332 switch (_cbarg)
333 {
334 case CB_FILE_SESS:
335 _tfname->set_text (s);
336 hide (_cbarg);
337 break;
338
339 default:
340 *s = 0;
341 sscanf (_tfname->text (), "%s", s);
342 if (*s == 0) hide (CB_FILE_CANC);
343 else if (stat (s, &S) || S_ISREG (S.st_mode))
344 {
345 _tfname->set_text (s);
346 hide (_cbarg);
347 }
348 else if (S_ISDIR (S.st_mode))
349 {
350 _tfname->set_text ("");
351 scandir (s, _fext);
352 }
353 }
354 }
355
356
357 void Filewin::newdir (void)
358 {
359 char s [64];
360
361 *s = 0;
362 sscanf (_tfname->text (), "%s", s);
363 mkdir (s, 0777);
364 _tfname->set_text ("");
365 scandir (s, _fext);
366 }
367
368
369 void Filewin::remove (void)
370 {
371 char s [64];
372 struct stat S;
373
374 *s = 0;
375 sscanf (_tfname->text (), "%s", s);
376 if (stat (s, &S)) return;
377 if (S_ISDIR (S.st_mode)) rmdir (s);
378 else unlink (s);
379 _tfname->set_text ("");
380 scandir (".", _fext);
381 }
382
383