Debian Codegraph @

gdraw/gfilechooser.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include <assert.h>
31 #include <dirent.h>
32 #include <errno.h>
33
34 #include "ffglib.h"
35 #include "gdraw.h"
36 #include "gdrawP.h"
37 #include "gfile.h"
38 #include "ggadgetP.h"
39 #include "gicons.h"
40 #include "gwidgetP.h"
41 #include "ustring.h"
42 #include "utype.h"
43 #ifdef WIN32
44 #include <windows.h>
45
46 // Returns "CDQ" if C:\, D:\ and Q:\ are available.
47 // This could probably be put in fsys.c, but we don't need it anywhere else,
48 // and shouldn't ever.
49 static const int WIN32_DRIVES_MAXLEN = 26;
50 static char* _DriveLetters() {
51 int idx = -1;
52 char* ret = calloc(WIN32_DRIVES_MAXLEN+1,sizeof(char));
53 DWORD drives = GetLogicalDrives();
54 assert(drives > 0);
55 for (int d = 0; d < WIN32_DRIVES_MAXLEN; d++) {
56 if ( (drives&(1 << d)) ) {
57 ret[++idx] = 'A'+d;
58 }
59 }
60 return ret;
61 }
62 #endif
63
64 /* This isn't really a gadget, it's just a collection of gadgets with some glue*/
65 /* to make them work together. Therefore there are no expose routines here, */
66 /* nor mouse, nor key. That's all handled by the individual gadgets themselves*/
67 /* Instead we've got a bunch of routines that make them work as a whole */
68 static GBox gfilechooser_box = GBOX_EMPTY; /* no box */
69 static unichar_t *lastdir;
70 static int showhidden = false;
71 static enum { dirs_mixed, dirs_first, dirs_separate } dir_placement = dirs_mixed;
72 static unichar_t **bookmarks = NULL;
73 static void *prefs_changed_data = NULL;
74 static void (*prefs_changed)(void *) = NULL;
75
76 static unichar_t *SubMatch(unichar_t *pattern, unichar_t *eop, unichar_t *name,int ignorecase) {
77 unichar_t ch, *ppt, *npt, *ept, *eon;
78
79 while ( pattern<eop && ( ch = *pattern)!='\0' ) {
80 if ( ch=='*' ) {
81 if ( pattern[1]=='\0' )
82 return( name+u_strlen(name));
83 for ( npt=name; ; ++npt ) {
84 if ( (eon = SubMatch(pattern+1,eop,npt,ignorecase))!= NULL )
85 return( eon );
86 if ( *npt=='\0' )
87 return( NULL );
88 }
89 } else if ( ch=='?' ) {
90 if ( *name=='\0' )
91 return( NULL );
92 ++name;
93 } else if ( ch=='[' ) {
94 /* [<char>...] matches the chars
95 * [<char>-<char>...] matches any char within the range (inclusive)
96 * the above may be concattenated and the resultant pattern matches
97 * anything thing which matches any of them.
98 * [^<char>...] matches any char which does not match the rest of
99 * the pattern
100 * []...] as a special case a ']' immediately after the '[' matches
101 * itself and does not end the pattern
102 */
103 int found = 0, not=0;
104 ++pattern;
105 if ( pattern[0]=='^' ) { not = 1; ++pattern; }
106 for ( ppt = pattern; (ppt!=pattern || *ppt!=']') && *ppt!='\0' ; ++ppt ) {
107 ch = *ppt;
108 if ( ppt[1]=='-' && ppt[2]!=']' && ppt[2]!='\0' ) {
109 unichar_t ch2 = ppt[2];
110 if ( (*name>=ch && *name<=ch2) ||
111 (ignorecase && islower(ch) && islower(ch2) &&
112 *name>=toupper(ch) && *name<=toupper(ch2)) ||
113 (ignorecase && isupper(ch) && isupper(ch2) &&
114 *name>=tolower(ch) && *name<=tolower(ch2))) {
115 if ( !not ) {
116 found = 1;
117 break;
118 }
119 } else {
120 if ( not ) {
121 found = 1;
122 break;
123 }
124 }
125 ppt += 2;
126 } else if ( ch==*name || (ignorecase && tolower(ch)==tolower(*name)) ) {
127 if ( !not ) {
128 found = 1;
129 break;
130 }
131 } else {
132 if ( not ) {
133 found = 1;
134 break;
135 }
136 }
137 }
138 if ( !found )
139 return( NULL );
140 while ( *ppt!=']' && *ppt!='\0' ) ++ppt;
141 pattern = ppt;
142 ++name;
143 } else if ( ch=='{' ) {
144 /* matches any of a comma separated list of substrings */
145 for ( ppt = pattern+1; *ppt!='\0' ; ppt = ept ) {
146 for ( ept=ppt; *ept!='}' && *ept!=',' && *ept!='\0'; ++ept );
147 npt = SubMatch(ppt,ept,name,ignorecase);
148 if ( npt!=NULL ) {
149 unichar_t *ecurly = ept;
150 while ( *ecurly!='}' && ecurly<eop && *ecurly!='\0' ) ++ecurly;
151 if ( (eon=SubMatch(ecurly+1,eop,npt,ignorecase))!=NULL )
152 return( eon );
153 }
154 if ( *ept=='}' )
155 return( NULL );
156 if ( *ept==',' ) ++ept;
157 }
158 } else if ( ch==*name ) {
159 ++name;
160 } else if ( ignorecase && tolower(ch)==tolower(*name)) {
161 ++name;
162 } else
163 return( NULL );
164 ++pattern;
165 }
166 if (pattern > eop) {
167 return NULL;
168 }
169 return( name );
170 }
171
172 /* Handles *?{}[] wildcards */
173 int GGadgetWildMatch(unichar_t *pattern, unichar_t *name,int ignorecase) {
174 if ( pattern==NULL )
175 return( true );
176
177 unichar_t *eop = pattern + u_strlen(pattern);
178
179 name = SubMatch(pattern,eop,name,ignorecase);
180 if ( name==NULL )
181 return( false );
182 if ( *name=='\0' )
183 return( true );
184
185 return( false );
186 }
187
188 static int GGadgetWildMatch8(unichar_t *pattern, const char *name,int ignorecase) {
189 unichar_t *uname = utf82u_copy(name);
190 int ret = GGadgetWildMatch(pattern, uname, ignorecase);
191 free(uname);
192 return ret;
193 }
194
195 enum fchooserret GFileChooserDefFilter(GGadget *g,const struct gdirentry *ent,const char *dir) {
196 GFileChooser *gfc = (GFileChooser *) g;
197 int i;
198
199 if ( strcmp(ent->name,".")==0 ) /* Don't show the current directory entry */
200 return( fc_hide );
201 if ( gfc->wildcard!=NULL && *gfc->wildcard=='.' )
202 /* If they asked for hidden files, show them */;
203 else if ( !showhidden && ent->name[0]=='.' && strcmp(ent->name,"..")!=0 )
204 return( fc_hide );
205 if ( ent->isdir ) /* Show all other directories */
206 return( fc_show );
207 if ( gfc->wildcard==NULL && gfc->mimetypes==NULL )
208 return( fc_show );
209 /* If we've got a wildcard, and it matches, then show */
210 if ( gfc->wildcard!=NULL && GGadgetWildMatch8(gfc->wildcard,ent->name,true))
211 return( fc_show );
212 /* If no mimetypes then we're done */
213 if ( gfc->mimetypes==NULL )
214 return( fc_hide );
215 /* match the mimetypes */
216 if ( ent->mimetype ) {
217 for ( i=0; gfc->mimetypes[i]!=NULL; ++i )
218 if ( strcasecmp(u_to_c(gfc->mimetypes[i]),ent->mimetype)==0 ) {
219 return( fc_show );
220 }
221 }
222
223 return( fc_hide );
224 }
225
226 static GImage *GFileChooserPickIcon(const struct gdirentry *e) {
227 InitChooserIcons();
228
229 if ( e->isdir ) {
230 if ( !strcmp(e->name,"..") )
231 return( &_GIcon_updir );
232 return( &_GIcon_dir );
233 } else if ( !e->mimetype ) {
234 return( &_GIcon_unknown );
235 }
236 if (strncasecmp("text/", e->mimetype, 5) == 0) {
237 if (strcasecmp("text/html", e->mimetype) == 0)
238 return( &_GIcon_texthtml );
239 if (strcasecmp("text/xml", e->mimetype) == 0)
240 return( &_GIcon_textxml );
241 if (strcasecmp("text/css", e->mimetype) == 0)
242 return( &_GIcon_textcss );
243 if (strcasecmp("text/c", e->mimetype) == 0)
244 return( &_GIcon_textc );
245 if (strcasecmp("text/java", e->mimetype) == 0)
246 return( &_GIcon_textjava );
247 if (strcasecmp("text/x-makefile", e->mimetype) == 0)
248 return( &_GIcon_textmake );
249 if (strcasecmp("application/x-font-type1", e->mimetype) == 0)
250 return( &_GIcon_textfontps );
251 if (strcasecmp("text/font", e->mimetype) == 0)
252 return( &_GIcon_textfontbdf );
253 if (strcasecmp("text/ps", e->mimetype) == 0)
254 return( &_GIcon_textps );
255
256 return( &_GIcon_textplain );
257 }
258
259 if (strncasecmp("image/", e->mimetype, 6) == 0)
260 return( &_GIcon_image );
261 if (strncasecmp("video/", e->mimetype, 6) == 0)
262 return( &_GIcon_video );
263 if (strncasecmp("audio/", e->mimetype, 6) == 0)
264 return( &_GIcon_audio );
265 if (strcasecmp("application/x-navidir", e->mimetype) == 0 ||
266 strcasecmp("inode/directory", e->mimetype) == 0)
267 return( &_GIcon_dir );
268 if (strcasecmp("application/x-object", e->mimetype) == 0)
269 return( &_GIcon_object );
270 if (strcasecmp("application/x-core", e->mimetype) == 0)
271 return( &_GIcon_core );
272 if (strcasecmp("application/x-tar", e->mimetype) == 0)
273 return( &_GIcon_tar );
274 if (strcasecmp("application/x-compressed", e->mimetype) == 0)
275 return( &_GIcon_compressed );
276 if (strcasecmp("application/pdf", e->mimetype) == 0)
277 return( &_GIcon_texthtml );
278 if (strcasecmp("application/vnd.font-fontforge-sfd", e->mimetype) == 0)
279 return( &_GIcon_textfontsfd );
280 if (strcasecmp("application/x-font-type1", e->mimetype) == 0)
281 return( &_GIcon_textfontps );
282 if (strcasecmp("application/x-font-ttf", e->mimetype) == 0 ||
283 strcasecmp("application/x-font-otf", e->mimetype) == 0 ||
284 strcasecmp("font/woff", e->mimetype) == 0 ||
285 strcasecmp("font/woff2", e->mimetype) == 0 ||
286 strcasecmp("font/ttf", e->mimetype) == 0 ||
287 strcasecmp("font/otf", e->mimetype) == 0) {
288 return( &_GIcon_ttf );
289 }
290 if (strcasecmp("application/x-font-cid", e->mimetype) == 0 )
291 return( &_GIcon_cid );
292 if (strcasecmp("application/x-macbinary", e->mimetype) == 0 ||
293 strcasecmp("application/x-mac-binhex40", e->mimetype) == 0 ) {
294 return( &_GIcon_mac );
295 }
296 if (strcasecmp("application/x-mac-dfont", e->mimetype) == 0 ||
297 strcasecmp("application/x-mac-suit", e->mimetype) == 0 ) {
298 return( &_GIcon_macttf );
299 }
300 if (strcasecmp("application/x-font-pcf", e->mimetype) == 0 ||
301 strcasecmp("application/x-font-snf", e->mimetype) == 0) {
302 return( &_GIcon_textfontbdf );
303 }
304
305 return( &_GIcon_unknown );
306 }
307
308 static void GFileChooserFillList(GFileChooser *gfc, const unichar_t *dirname_) {
309 DIR *dir;
310 char *dirname = u2def_copy(dirname_);
311 struct dirent *dent;
312
313 if (!dirname || (dir = opendir(dirname)) == NULL) {
314 GTextInfo *ti[2], _ti[2] = { 0 };
315 static unichar_t nullstr[] = { 0 };
316
317 _ti[0].text = def2u_copy(strerror(errno));
318 _ti[0].fg = _ti[0].bg = COLOR_DEFAULT;
319 ti[0] = _ti; ti[1] = _ti+1;
320 GGadgetSetEnabled(&gfc->files->g, false);
321 GGadgetSetList(&gfc->files->g, ti, true);
322 GGadgetSetEnabled(&gfc->subdirs->g, false);
323 GGadgetSetList(&gfc->subdirs->g, ti, true);
324 free(_ti[0].text);
325 if (gfc->lastname != NULL) {
326 GGadgetSetTitle(&gfc->name->g, gfc->lastname);
327 free(gfc->lastname);
328 gfc->lastname=NULL;
329 } else {
330 GGadgetSetTitle(&gfc->name->g, nullstr);
331 }
332
333 if (gfc->filterb != NULL && gfc->ok != NULL) {
334 _GWidget_MakeDefaultButton(&gfc->ok->g);
335 }
336 free(dirname);
337 return;
338 }
339
340 if (gfc->lastname!=NULL) {
341 GGadgetSetTitle(&gfc->name->g, gfc->lastname);
342 free(gfc->lastname);
343 gfc->lastname=NULL;
344 }
345
346 GPtrArray *ti = g_ptr_array_new();
347 GPtrArray *dti = (dir_placement == dirs_separate) ? g_ptr_array_new() : NULL;
348 while ((dent = readdir(dir)) != NULL) {
349 struct gdirentry ent;
350 if (!strcmp(dent->d_name, ".")) {
351 continue;
352 }
353 ent.fullpath = smprintf("%s/%s", dirname, dent->d_name);
354 ent.name = dent->d_name;
355 ent.mimetype = GFileMimeType(ent.fullpath);
356 ent.isdir = GFileIsDir(ent.fullpath);
357
358 int fcdata = (gfc->filter)(&gfc->g, &ent, dirname);
359 if (fcdata != fc_hide) {
360 GTextInfo *me = calloc(1, sizeof(GTextInfo));
361 me->text = def2u_copy(dent->d_name);
362 me->image = GFileChooserPickIcon(&ent);
363 me->fg = COLOR_DEFAULT;
364 me->bg = COLOR_DEFAULT;
365 me->font = NULL;
366 me->disabled = fcdata == fc_showdisabled;
367 me->image_precedes = true;
368 me->checked = ent.isdir;
369 if (dir_placement == dirs_first && ent.isdir) {
370 ((GTextInfo2 *)me)->sort_me_first_in_list = true;
371 }
372 g_ptr_array_add((dti && ent.isdir) ? dti : ti, me);
373 }
374 free((char*)ent.fullpath);
375 free((char*)ent.mimetype);
376 }
377 closedir(dir);
378
379 GTextInfo **rti = malloc((ti->len + 1) * sizeof(GTextInfo*));
380 memcpy(rti, ti->pdata, ti->len * sizeof(GTextInfo*));
381 rti[ti->len] = calloc(1, sizeof(GTextInfo));
382 GGadgetSetList(&gfc->files->g, rti, false);
383 g_ptr_array_free(ti, false);
384
385 if (dti) {
386 GTextInfo **rdti = malloc((dti->len + 1) * sizeof(GTextInfo*));
387 memcpy(rdti, dti->pdata, dti->len * sizeof(GTextInfo*));
388 rdti[dti->len] = calloc(1, sizeof(GTextInfo));
389 GGadgetSetList(&gfc->subdirs->g, rdti, false);
390 g_ptr_array_free(dti, false);
391 }
392
393 GGadgetScrollListToText(&gfc->files->g,u_GFileNameTail(_GGadgetGetTitle(&gfc->name->g)),true);
394 GGadgetSetEnabled(&gfc->files->g, true);
395 GGadgetSetEnabled(&gfc->subdirs->g, true);
396 free(dirname);
397 }
398
399 static void GFileChooserScanDir(GFileChooser *gfc,unichar_t *dir) {
400 GTextInfo **ti=NULL;
401 int cnt, tot=0;
402 #ifdef _WIN32
403 char* drives = _DriveLetters();
404 int dcnt = strlen(drives);
405 #endif
406 unichar_t *pt, *ept, *freeme;
407
408 dir = u_GFileNormalize(dir);
409 while ( 1 ) {
410 ept = dir;
411 cnt = 0;
412 if ( (pt=uc_strstr(dir,"://"))!=NULL ) {
413 ept = u_strchr(pt+3,'/');
414 if ( ept==NULL )
415 ept = pt+u_strlen(pt);
416 else
417 ++ept;
418 } else if ( *dir=='/' )
419 ept = dir+1;
420 if ( ept!=dir ) {
421 if ( ti!=NULL ) {
422 ti[tot-cnt] = calloc(1,sizeof(GTextInfo));
423 ti[tot-cnt]->text = u_copyn(dir,ept-dir);
424 ti[tot-cnt]->fg = ti[tot-cnt]->bg = COLOR_DEFAULT;
425 }
426 cnt = 1;
427 }
428 for ( pt = ept; *pt!='\0'; pt = ept ) {
429 for ( ept = pt; *ept!='/' && *ept!='\0'; ++ept );
430 if ( ti!=NULL ) {
431 ti[tot-cnt] = calloc(1,sizeof(GTextInfo));
432 ti[tot-cnt]->text = u_copyn(pt,ept-pt);
433 ti[tot-cnt]->fg = ti[tot-cnt]->bg = COLOR_DEFAULT;
434 }
435 ++cnt;
436 if ( *ept=='/' ) ++ept;
437 }
438 if ( ti!=NULL )
439 break;
440 ti = malloc((cnt+1)*sizeof(GTextInfo *));
441 tot = cnt-1;
442 }
443
444 #ifdef _WIN32
445 ti = realloc(ti, (dcnt+cnt+1)*sizeof(GTextInfo *));
446
447 for (char* c = drives; *c != '\0'; c++) {
448 // Don't put the root twice if we're already on it.
449 if ((unichar_t)*c == toupper(dir[0])) continue;
450
451 ti[cnt] = calloc(1,sizeof(GTextInfo));
452 unichar_t* utemp = calloc(3,sizeof(unichar_t));
453 utemp[0] = *c; utemp[1] = ':'; //utemp[2] = '\\'; // utemp[3] = 0;
454 ti[cnt]->text = utemp;
455 ti[cnt]->fg = ti[cnt]->bg = COLOR_DEFAULT;
456 // We don't want to add a new field to GTextInfo as it's used in a lot of places.
457 // This is the next best thing AFAICT.
458 ti[cnt]->userdata = (void*)1;
459 cnt++;
460 }
461 free(drives);
462 #endif
463 ti[cnt] = calloc(1,sizeof(GTextInfo));
464
465 GGadgetSetList(&gfc->directories->g,ti,false);
466 GGadgetSelectOneListItem(&gfc->directories->g,0);
467
468 freeme = NULL;
469 if ( dir[u_strlen(dir)-1]!='/' ) {
470 freeme = malloc((u_strlen(dir)+3)*sizeof(unichar_t));
471 u_strcpy(freeme,dir);
472 uc_strcat(freeme,"/");
473 dir = freeme;
474 }
475 if ( gfc->hpos+1>=gfc->hmax ) {
476 gfc->hmax = gfc->hmax+20;
477 gfc->history = realloc(gfc->history,(gfc->hmax)*sizeof(unichar_t *));
478 }
479 if ( gfc->hcnt==0 ) {
480 gfc->history[gfc->hcnt++] = u_copy(dir);
481 } else if ( u_strcmp(gfc->history[gfc->hpos],dir)==0 )
482 /* Just a refresh */;
483 else {
484 gfc->history[++gfc->hpos] = u_copy(dir);
485 gfc->hcnt = gfc->hpos+1;
486 }
487
488 GFileChooserFillList(gfc, dir);
489 free(freeme);
490 }
491
492 /* Handle events from the text field */
493 static int GFileChooserTextChanged(GGadget *t,GEvent *e) {
494 GFileChooser *gfc;
495 GGadget *g = (GGadget *)GGadgetGetUserData(t);
496
497 const unichar_t *pt, *spt;
498 unichar_t * pt_toFree = 0, *local_toFree = 0;
499 if ( e->type!=et_controlevent || e->u.control.subtype!=et_textchanged )
500 return( true );
501 spt = pt = _GGadgetGetTitle(t);
502 #ifdef _WIN32
503 local_toFree = u_GFileNormalizePath(u_copy(spt));
504 pt = spt = local_toFree;
505 #endif
506
507 if ( pt==NULL )
508 return( true );
509 gfc = (GFileChooser *) GGadgetGetUserData(t);
510
511 if( GFileChooserGetInputFilenameFunc(g)(g, &pt,gfc->inputfilenameprevchar)) {
512 pt_toFree = (unichar_t*)pt;
513 spt = pt;
514 GGadgetSetTitle(g, pt);
515 }
516
517 while ( *pt && *pt!='*' && *pt!='?' && *pt!='[' && *pt!='{' )
518 ++pt;
519 if ( *spt!='\0' && spt[u_strlen(spt)-1]=='/' )
520 pt = spt+u_strlen(spt)-1;
521
522 /* if there are no wildcards and no directories in the filename */
523 /* then as it gets changed the list box should show the closest */
524 /* approximation to it */
525 if ( *pt=='\0' ) {
526 GGadgetScrollListToText(&gfc->files->g,spt,true);
527 if ( gfc->filterb!=NULL && gfc->ok!=NULL )
528 _GWidget_MakeDefaultButton(&gfc->ok->g);
529 } else if ( *pt=='/' && e->u.control.u.tf_changed.from_pulldown!=-1 ) {
530 GEvent e;
531 e.type = et_controlevent;
532 e.u.control.subtype = et_buttonactivate;
533 e.u.control.g = (gfc->ok!=NULL?&gfc->ok->g:&gfc->g);
534 e.u.control.u.button.clicks = 0;
535 e.w = e.u.control.g->base;
536 if ( e.u.control.g->handle_controlevent != NULL )
537 (e.u.control.g->handle_controlevent)(e.u.control.g,&e);
538 else
539 GDrawPostEvent(&e);
540 } else {
541 if ( gfc->filterb!=NULL && gfc->ok!=NULL )
542 _GWidget_MakeDefaultButton(&gfc->filterb->g);
543 }
544 free(gfc->lastname); gfc->lastname = NULL;
545 if(pt_toFree)
546 free(pt_toFree);
547
548 if (local_toFree)
549 free(local_toFree);
550
551 if(gfc->inputfilenameprevchar)
552 free(gfc->inputfilenameprevchar);
553 gfc->inputfilenameprevchar = u_copy(_GGadgetGetTitle(t));
554
555 return( true );
556 }
557
558 static unichar_t **GFileChooserCompletion(GGadget *t,int from_tab) {
559 GFileChooser *gfc;
560 const unichar_t *pt, *spt; unichar_t **ret;
561 GTextInfo **ti;
562 int32_t len;
563 int i, cnt, doit, match_len;
564
565 pt = spt = _GGadgetGetTitle(t);
566 if ( pt==NULL )
567 return( NULL );
568 while ( *pt && *pt!='/' && *pt!='*' && *pt!='?' && *pt!='[' && *pt!='{' )
569 ++pt;
570 if ( *pt!='\0' )
571 return( NULL ); /* Can't complete if not in cur directory or has wildcards */
572
573 match_len = u_strlen(spt);
574 gfc = (GFileChooser *) GGadgetGetUserData(t);
575 ti = GGadgetGetList(&gfc->files->g,&len);
576 ret = NULL;
577 for ( doit=0; doit<2; ++doit ) {
578 cnt=0;
579 for ( i=0; i<len; ++i ) {
580 if ( u_strncmp(ti[i]->text,spt,match_len)==0 ) {
581 if ( doit ) {
582 if ( ti[i]->checked /* isdirectory */ ) {
583 int len = u_strlen(ti[i]->text);
584 ret[cnt] = malloc((len+2)*sizeof(unichar_t));
585 u_strcpy(ret[cnt],ti[i]->text);
586 ret[cnt][len] = '/';
587 ret[cnt][len+1] = '\0';
588 } else
589 ret[cnt] = u_copy(ti[i]->text);
590 }
591 ++cnt;
592 }
593 }
594 if ( doit )
595 ret[cnt] = NULL;
596 else if ( cnt==0 )
597 return( NULL );
598 else
599 ret = malloc((cnt+1)*sizeof(unichar_t *));
600 }
601 return( ret );
602 }
603
604 static unichar_t *GFileChooserGetCurDir(GFileChooser *gfc,int dirindex) {
605 GTextInfo **ti;
606 int32_t len; int j, cnt;
607 unichar_t *dir, *pt;
608
609 ti = GGadgetGetList(&gfc->directories->g,&len);
610 if ( dirindex==-1 )
611 dirindex = 0;
612
613 for ( j=len-1,cnt=0; j>=dirindex; --j ) {
614 if (ti[j]->userdata != NULL)
615 continue;
616 cnt += u_strlen(ti[j]->text)+1;
617 }
618 pt = dir = malloc((cnt+1)*sizeof(unichar_t));
619 for ( j=len-1; j>=dirindex; --j ) {
620 if (ti[j]->userdata != NULL)
621 continue;
622 u_strcpy(pt,ti[j]->text);
623 pt += u_strlen(pt);
624 if ( pt[-1]!='/' )
625 *pt++ = '/';
626 }
627 *pt = '\0';
628 return( dir );
629 }
630
631 static void GFileChooserRefresh(GFileChooser *gfc) {
632 unichar_t *dir;
633
634 dir = GFileChooserGetCurDir(gfc,-1);
635 GFileChooserScanDir(gfc,dir);
636 free(dir);
637 }
638
639 static void GFileChooserSetTitle(GGadget *g,const unichar_t *tit);
640
641 /* Handle events from the directory dropdown list */
642 static int GFileChooserDListChanged(GGadget *pl,GEvent *e) {
643 GFileChooser *gfc;
644 int i;
645 unichar_t *dir;
646
647 if ( e->type!=et_controlevent || e->u.control.subtype!=et_listselected )
648 return( true );
649 i = GGadgetGetFirstListSelectedItem(pl);
650 if ( i==-1 )
651 return(true);
652 gfc = (GFileChooser *) GGadgetGetUserData(pl);
653 #ifdef _WIN32
654 int len;
655 GTextInfo **ti;
656 ti = GGadgetGetList(pl,&len);
657 if (ti[i]->userdata != NULL) {
658 GFileChooserSetTitle((GGadget*)gfc, ti[i]->text);
659 GFileChooserRefresh(gfc);
660 return( true );
661 }
662 #endif
663 if ( i==0 ) /* Nothing changed */
664 return( true );
665 dir = GFileChooserGetCurDir(gfc,i);
666 GFileChooserScanDir(gfc,dir);
667 free(dir);
668 return( true );
669 }
670
671 /* Handle events from the file list list */
672 static int GFileChooserFListSelected(GGadget *gl,GEvent *e) {
673 GFileChooser *gfc;
674 int i;
675 int32_t listlen; int len, cnt, dirpos, apos;
676 unichar_t *dir, *newdir;
677 GTextInfo *ti, **all;
678
679 if ( e->type!=et_controlevent ||
680 ( e->u.control.subtype!=et_listselected &&
681 e->u.control.subtype!=et_listdoubleclick ))
682 return( true );
683 if ( ((GList *) gl)->multiple_sel ) {
684 all = GGadgetGetList(gl,&listlen);
685 len = cnt = 0;
686 dirpos = apos = -1;
687 for ( i=0; i<listlen; ++i ) {
688 if ( !all[i]->selected )
689 /* Not selected? ignore it */;
690 else if ( all[i]->checked ) /* Directory */
691 dirpos = i;
692 else {
693 len += u_strlen( all[i]->text ) + 2;
694 ++cnt;
695 apos = i;
696 }
697 }
698 if ( dirpos!=-1 && cnt>0 ) {
699 /* If a directory was selected, clear everything else */
700 for ( i=0; i<listlen; ++i ) if ( i!=dirpos )
701 all[i]->selected = false;
702 _ggadget_redraw(gl);
703 }
704 if ( dirpos!=-1 ) { cnt = 1; apos = dirpos; }
705 } else {
706 cnt = 1;
707 apos = GGadgetGetFirstListSelectedItem(gl);
708 }
709 if ( apos==-1 )
710 return(true);
711 gfc = (GFileChooser *) GGadgetGetUserData(gl);
712 ti = GGadgetGetListItem(gl,apos);
713 if ( e->u.control.subtype==et_listselected && cnt==1 ) {
714 /* Nope, quite doesn't work. Goal is to remember first filename. But */
715 /* if user types into the list box we'll (probably) get several diff*/
716 /* filenames before we hit the directory. So we just ignore the typ-*/
717 /* ing case for now. */
718 if ( ti->checked /* it's a directory */ && e->u.control.u.list.from_mouse &&
719 gfc->lastname==NULL )
720 gfc->lastname = GGadgetGetTitle(&gfc->name->g);
721 if ( ti->checked ) {
722 unichar_t *val = malloc((u_strlen(ti->text)+2)*sizeof(unichar_t));
723 u_strcpy(val,ti->text);
724 uc_strcat(val,"/");
725 GGadgetSetTitle(&gfc->name->g,val);
726 free(val);
727 if ( gfc->filterb!=NULL && gfc->ok!=NULL )
728 _GWidget_MakeDefaultButton(&gfc->filterb->g);
729 } else {
730 GGadgetSetTitle(&gfc->name->g,ti->text);
731 if ( gfc->filterb!=NULL && gfc->ok!=NULL )
732 _GWidget_MakeDefaultButton(&gfc->ok->g);
733 free(gfc->lastname);
734 gfc->lastname = NULL;
735 }
736 } else if ( e->u.control.subtype==et_listselected ) {
737 unichar_t *val, *upt = malloc((len+1)*sizeof(unichar_t));
738 val = upt;
739 for ( i=0; i<listlen; ++i ) {
740 if ( all[i]->selected ) {
741 u_strcpy(upt,all[i]->text);
742 upt += u_strlen(upt);
743 if ( --cnt>0 ) {
744 uc_strcpy(upt,"; ");
745 upt += 2;
746 }
747 }
748 }
749 GGadgetSetTitle(&gfc->name->g,val);
750 free(val);
751 } else if ( ti->checked /* it's a directory */ ) {
752 dir = GFileChooserGetCurDir(gfc,-1);
753 newdir = u_GFileAppendFile(dir,ti->text,true);
754 GFileChooserScanDir(gfc,newdir);
755 free(dir); free(newdir);
756 } else {
757 /* Post the double click (on a file) to the parent */
758 /* if we know what the ok button is then pretend it got pressed */
759 /* otherwise just send a list double click from ourselves */
760 if ( gfc->ok!=NULL ) {
761 e->u.control.subtype = et_buttonactivate;
762 e->u.control.g = &gfc->ok->g;
763 e->u.control.u.button.clicks = 0;
764 e->w = e->u.control.g->base;
765 } else
766 e->u.control.g = &gfc->g;
767 if ( e->u.control.g->handle_controlevent != NULL )
768 (e->u.control.g->handle_controlevent)(e->u.control.g,e);
769 else
770 GDrawPostEvent(e);
771 }
772 return( true );
773 }
774
775 static void GFCHideToggle(GWindow gw,struct gmenuitem *mi,GEvent *e) {
776 GFileChooser *gfc = (GFileChooser *) (mi->ti.userdata);
777 unichar_t *dir;
778
779 showhidden = !showhidden;
780
781 dir = GFileChooserGetCurDir(gfc,-1);
782 GFileChooserScanDir(gfc,dir);
783 free(dir);
784
785 if ( prefs_changed!=NULL )
786 (prefs_changed)(prefs_changed_data);
787 }
788
789 static void GFCRemetric(GFileChooser *gfc) {
790 GRect size;
791
792 GGadgetGetSize(&gfc->topbox->g,&size);
793 GGadgetResize(&gfc->topbox->g,size.width,size.height);
794 }
795
796 static void GFCDirsAmidToggle(GWindow gw,struct gmenuitem *mi,GEvent *e) {
797 GFileChooser *gfc = (GFileChooser *) (mi->ti.userdata);
798 unichar_t *dir;
799
800 if ( dir_placement==dirs_separate ) {
801 GGadgetSetVisible(&gfc->subdirs->g,false);
802 GFCRemetric(gfc);
803 }
804 dir_placement = dirs_mixed;
805
806 dir = GFileChooserGetCurDir(gfc,-1);
807 GFileChooserScanDir(gfc,dir);
808 free(dir);
809
810 if ( prefs_changed!=NULL )
811 (prefs_changed)(prefs_changed_data);
812 }
813
814 static void GFCDirsFirstToggle(GWindow gw,struct gmenuitem *mi,GEvent *e) {
815 GFileChooser *gfc = (GFileChooser *) (mi->ti.userdata);
816 unichar_t *dir;
817
818 if ( dir_placement==dirs_separate ) {
819 GGadgetSetVisible(&gfc->subdirs->g,false);
820 GFCRemetric(gfc);
821 }
822 dir_placement = dirs_first;
823
824 dir = GFileChooserGetCurDir(gfc,-1);
825 GFileChooserScanDir(gfc,dir);
826 free(dir);
827
828 if ( prefs_changed!=NULL )
829 (prefs_changed)(prefs_changed_data);
830 }
831
832 static void GFCDirsSeparateToggle(GWindow gw,struct gmenuitem *mi,GEvent *e) {
833 GFileChooser *gfc = (GFileChooser *) (mi->ti.userdata);
834 unichar_t *dir;
835
836 if ( dir_placement!=dirs_separate ) {
837 GGadgetSetVisible(&gfc->subdirs->g,true);
838 GFCRemetric(gfc);
839 }
840 dir_placement = dirs_separate;
841
842 dir = GFileChooserGetCurDir(gfc,-1);
843 GFileChooserScanDir(gfc,dir);
844 free(dir);
845
846 if ( prefs_changed!=NULL )
847 (prefs_changed)(prefs_changed_data);
848 }
849
850 static void GFCRefresh(GWindow gw,struct gmenuitem *mi,GEvent *e) {
851 GFileChooser *gfc = (GFileChooser *) (mi->ti.userdata);
852 GFileChooserRefresh(gfc);
853 }
854
855 static int GFileChooserHome(GGadget *g, GEvent *e) {
856 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
857 unichar_t* homedir = u_GFileGetHomeDocumentsDir();
858 if ( homedir==NULL )
859 GGadgetSetEnabled(g,false);
860 else {
861 GFileChooser *gfc = (GFileChooser *) GGadgetGetUserData(g);
862 GFileChooserScanDir(gfc,homedir);
863 free(homedir);
864 }
865 }
866 return( true );
867 }
868
869 static int GFileChooserUpDirButton(GGadget *g, GEvent *e) {
870 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
871 GFileChooser *gfc = (GFileChooser *) GGadgetGetUserData(g);
872 unichar_t *dir, *newdir;
873 static unichar_t dotdot[] = { '.', '.', 0 };
874 dir = GFileChooserGetCurDir(gfc,-1);
875 newdir = u_GFileAppendFile(dir,dotdot,true);
876 GFileChooserScanDir(gfc,newdir);
877 free(dir); free(newdir);
878 }
879 return( true );
880 }
881
882 static GMenuItem gfcpopupmenu[] = {
883 { { (unichar_t *) N_("Show Hidden Files"), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 1, 0, 0, 0, 1, 0, 0, 'H' }, '\0', ksm_control, NULL, NULL, GFCHideToggle, 0 },
884 { { (unichar_t *) N_("Directories Amid Files"), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 1, 0, 0, 0, 1, 0, 0, 'H' }, '\0', ksm_control, NULL, NULL, GFCDirsAmidToggle, 0 },
885 { { (unichar_t *) N_("Directories First"), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 1, 0, 0, 0, 1, 0, 0, 'H' }, '\0', ksm_control, NULL, NULL, GFCDirsFirstToggle, 0 },
886 { { (unichar_t *) N_("Directories Separate"), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 1, 0, 0, 0, 1, 0, 0, 'H' }, '\0', ksm_control, NULL, NULL, GFCDirsSeparateToggle, 0 },
887 { { (unichar_t *) N_("Refresh File List"), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, 'H' }, '\0', ksm_control, NULL, NULL, GFCRefresh, 0 },
888 GMENUITEM_EMPTY
889 };
890 static int gotten=false;
891
892 static void GFCPopupMenu(GGadget *g, GEvent *e) {
893 int i;
894 // The reason this casting works is a bit of a hack.
895 // If this was initiated from a right click, `g` will be the GFC GGadget.
896 // Then its userdata would be the initiator's, and NOT the GFC struct.
897 // Thus we cannot call GGadgetGetUserData.
898 // However, since in the GFC struct, its GGadget comes first, effectively
899 // the pointer to its GGadget will equal the pointer to its GFC struct.
900 // I have ensured that all calls to this function pass the GFC GGadget.
901 GFileChooser *gfc = (GFileChooser *) g;
902
903 for ( i=0; gfcpopupmenu[i].ti.text!=NULL || gfcpopupmenu[i].ti.line; ++i )
904 gfcpopupmenu[i].ti.userdata = gfc;
905 gfcpopupmenu[0].ti.checked = showhidden;
906 gfcpopupmenu[1].ti.checked = dir_placement == dirs_mixed;
907 gfcpopupmenu[2].ti.checked = dir_placement == dirs_first;
908 gfcpopupmenu[3].ti.checked = dir_placement == dirs_separate;
909 if ( !gotten ) {
910 gotten = true;
911 for ( i=0; gfcpopupmenu[i].ti.text!=NULL || gfcpopupmenu[i].ti.line; ++i )
912 gfcpopupmenu[i].ti.text = (unichar_t *) _( (char *) gfcpopupmenu[i].ti.text);
913 }
914 GMenuCreatePopupMenu(g->base,e, gfcpopupmenu);
915 }
916
917 static int GFileChooserConfigure(GGadget *g, GEvent *e) {
918 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonpress ) {
919 GEvent fake;
920 GRect pos;
921 GGadgetGetSize(g,&pos);
922 memset(&fake,0,sizeof(fake));
923 fake.type = et_mousedown;
924 fake.w = g->base;
925 fake.u.mouse.x = pos.x;
926 fake.u.mouse.y = pos.y+pos.height;
927 GFCPopupMenu((GGadget*)GGadgetGetUserData(g),&fake);
928 }
929 return( true );
930 }
931
932 static void GFCBack(GWindow gw,struct gmenuitem *mi,GEvent *e) {
933 GFileChooser *gfc = (GFileChooser *) (mi->ti.userdata);
934
935 if ( gfc->hpos<=0 )
936 return;
937 --gfc->hpos;
938 GFileChooserScanDir(gfc,gfc->history[gfc->hpos]);
939 }
940
941 static void GFCForward(GWindow gw,struct gmenuitem *mi,GEvent *e) {
942 GFileChooser *gfc = (GFileChooser *) (mi->ti.userdata);
943
944 if ( gfc->hpos+1>=gfc->hcnt )
945 return;
946 ++gfc->hpos;
947 GFileChooserScanDir(gfc,gfc->history[gfc->hpos]);
948 }
949
950 static void GFCAddCur(GWindow gw,struct gmenuitem *mi,GEvent *e) {
951 GFileChooser *gfc = (GFileChooser *) (mi->ti.userdata);
952 unichar_t *dir;
953 int bcnt;
954
955 dir = GFileChooserGetCurDir(gfc,-1);
956 bcnt = 0;
957 if ( bookmarks!=NULL )
958 for ( ; bookmarks[bcnt]!=NULL; ++bcnt );
959 bookmarks = realloc(bookmarks,(bcnt+2)*sizeof(unichar_t *));
960 bookmarks[bcnt] = dir;
961 bookmarks[bcnt+1] = NULL;
962
963 if ( prefs_changed!=NULL )
964 (prefs_changed)(prefs_changed_data);
965 }
966
967 static void GFCRemoveBook(GWindow gw,struct gmenuitem *mi,GEvent *e) {
968 int i,bcnt;
969 char **books;
970 char *sel;
971 char *buts[2];
972
973 if ( bookmarks==NULL || bookmarks[0]==NULL )
974 return; /* All gone */
975 for ( bcnt=0; bookmarks[bcnt]!=NULL; ++bcnt );
976 sel = calloc(bcnt,1);
977 books = calloc(bcnt+1,sizeof(char *));
978 for ( bcnt=0; bookmarks[bcnt]!=NULL; ++bcnt )
979 books[bcnt] = u2utf8_copy(bookmarks[bcnt]);
980 books[bcnt] = NULL;
981 buts[0] = _("_Remove");
982 buts[1] = _("_Cancel");
983 if ( GWidgetChoicesBM8( _("Remove bookmarks"),(const char **) books,sel,bcnt,buts,
984 _("Remove selected bookmarks"))==0 ) {
985 for ( i=bcnt=0; bookmarks[bcnt]!=NULL; ++bcnt ) {
986 if ( sel[bcnt] ) {
987 free(bookmarks[bcnt]);
988 } else {
989 bookmarks[i++] = bookmarks[bcnt];
990 }
991 }
992 bookmarks[i] = NULL;
993
994 if ( prefs_changed!=NULL )
995 (prefs_changed)(prefs_changed_data);
996 }
997 for ( i=0; books[i]!=NULL; ++i )
998 free(books[i]);
999 free(books);
1000 free(sel);
1001 }
1002
1003 static void GFCBookmark(GWindow gw,struct gmenuitem *mi,GEvent *e) {
1004 GFileChooser *gfc = (GFileChooser *) (mi->ti.userdata);
1005 char *home;
1006
1007 if ( *bookmarks[mi->mid]=='~' && bookmarks[mi->mid][1]=='/' &&
1008 (home = getenv("HOME"))!=NULL ) {
1009 unichar_t *space;
1010 space = malloc((strlen(home)+u_strlen(bookmarks[mi->mid])+2)*sizeof(unichar_t));
1011 uc_strcpy(space,home);
1012 u_strcat(space,bookmarks[mi->mid]+1);
1013 GFileChooserScanDir(gfc,space);
1014 free(space);
1015 } else
1016 GFileChooserScanDir(gfc,bookmarks[mi->mid]);
1017 }
1018
1019 static void GFCPath(GWindow gw,struct gmenuitem *mi,GEvent *e) {
1020 GFileChooser *gfc = (GFileChooser *) (mi->ti.userdata);
1021 char *home;
1022
1023 if ( *gfc->paths[mi->mid]=='~' && gfc->paths[mi->mid][1]=='/' &&
1024 (home = getenv("HOME"))!=NULL ) {
1025 unichar_t *space;
1026 space = malloc((strlen(home)+u_strlen(bookmarks[mi->mid])+2)*sizeof(unichar_t));
1027 uc_strcpy(space,home);
1028 u_strcat(space,gfc->paths[mi->mid]+1);
1029 GFileChooserScanDir(gfc,space);
1030 free(space);
1031 } else
1032 GFileChooserScanDir(gfc,gfc->paths[mi->mid]);
1033 }
1034
1035 static GMenuItem gfcbookmarkmenu[] = {
1036 { { (unichar_t *) N_("Directory|Back"), &_GIcon_backarrow, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 1, 0, 0, 0, 0, 1, 0, 0, '\0' }, '\0', ksm_control, NULL, NULL, GFCBack, 0 },
1037 { { (unichar_t *) N_("Directory|Forward"), &_GIcon_forwardarrow, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 1, 0, 0, 0, 0, 1, 0, 0, '\0' }, '\0', ksm_control, NULL, NULL, GFCForward, 0 },
1038 { { NULL, NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 1, 0, 0, 0, '\0' }, '\0', 0, NULL, NULL, NULL, 0 }, /* line */
1039 { { (unichar_t *) N_("Bookmark Current Dir"), &_GIcon_bookmark, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 1, 0, 0, 0, 0, 1, 0, 0, '\0' }, '\0', ksm_control, NULL, NULL, GFCAddCur, 0 },
1040 { { (unichar_t *) N_("Remove Bookmark..."), &_GIcon_nobookmark, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 1, 0, 0, 0, 0, 1, 0, 0, '\0' }, '\0', ksm_control, NULL, NULL, GFCRemoveBook, 0 },
1041 { { NULL, NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 1, 0, 0, 0, '\0' }, '\0', 0, NULL, NULL, NULL, 0 }, /* line */
1042 GMENUITEM_EMPTY
1043 };
1044 static int bgotten=false;
1045
1046 static int GFileChooserBookmarks(GGadget *g, GEvent *e) {
1047 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonpress ) {
1048 GFileChooser *gfc = (GFileChooser *) GGadgetGetUserData(g);
1049 GMenuItem *mi;
1050 int i, bcnt, mcnt, pcnt;
1051 GEvent fake;
1052 GRect pos;
1053
1054 if ( !bgotten ) {
1055 bgotten = true;
1056 for ( i=0; gfcbookmarkmenu[i].ti.text!=NULL || gfcbookmarkmenu[i].ti.line; ++i )
1057 if ( gfcbookmarkmenu[i].ti.text!=NULL )
1058 gfcbookmarkmenu[i].ti.text = (unichar_t *) S_( (char *) gfcbookmarkmenu[i].ti.text);
1059 }
1060 for ( mcnt=0; gfcbookmarkmenu[mcnt].ti.text!=NULL || gfcbookmarkmenu[mcnt].ti.line; ++mcnt );
1061 bcnt = 0;
1062 if ( bookmarks!=NULL )
1063 for ( ; bookmarks[bcnt]!=NULL; ++bcnt );
1064 if ( gfc->paths!=NULL ) {
1065 for ( pcnt=0; gfc->paths[pcnt]!=NULL; ++pcnt );
1066 if ( pcnt!=0 && bcnt!=0 ) ++pcnt;
1067 bcnt += pcnt;
1068 }
1069 mi = calloc((mcnt+bcnt+1),sizeof(GMenuItem));
1070 for ( mcnt=0; gfcbookmarkmenu[mcnt].ti.text!=NULL || gfcbookmarkmenu[mcnt].ti.line; ++mcnt ) {
1071 mi[mcnt] = gfcbookmarkmenu[mcnt];
1072 mi[mcnt].ti.text = (unichar_t *) copy( (char *) mi[mcnt].ti.text);
1073 mi[mcnt].ti.userdata = gfc;
1074 }
1075 if ( gfc->hpos==0 )
1076 mi[0].ti.disabled = true; /* can't go further back */
1077 if ( gfc->hpos+1>=gfc->hcnt )
1078 mi[1].ti.disabled = true; /* can't go further forward */
1079 if ( bookmarks==NULL )
1080 mi[4].ti.disabled = true; /* can't remove bookmarks, already none */
1081 else {
1082 if ( gfc->paths!=NULL ) {
1083 for ( bcnt=0; gfc->paths[bcnt]!=NULL; ++bcnt ) {
1084 mi[mcnt+bcnt].ti.text = u_copy(gfc->paths[bcnt]);
1085 mi[mcnt+bcnt].ti.fg = mi[mcnt+bcnt].ti.bg = COLOR_DEFAULT;
1086 mi[mcnt+bcnt].ti.userdata = gfc;
1087 mi[mcnt+bcnt].mid = bcnt;
1088 mi[mcnt+bcnt].invoke = GFCPath;
1089 }
1090 mcnt+=bcnt;
1091 if ( bookmarks!=NULL && bookmarks[0]!=NULL ) {
1092 mi[mcnt].ti.line = true;
1093 mi[mcnt].ti.fg = mi[mcnt].ti.bg = COLOR_DEFAULT;
1094 ++mcnt;
1095 }
1096 }
1097 for ( bcnt=0; bookmarks[bcnt]!=NULL; ++bcnt ) {
1098 mi[mcnt+bcnt].ti.text = u_copy(bookmarks[bcnt]);
1099 mi[mcnt+bcnt].ti.fg = mi[mcnt+bcnt].ti.bg = COLOR_DEFAULT;
1100 mi[mcnt+bcnt].ti.userdata = gfc;
1101 mi[mcnt+bcnt].mid = bcnt;
1102 mi[mcnt+bcnt].invoke = GFCBookmark;
1103 }
1104 }
1105 GGadgetGetSize(g,&pos);
1106 memset(&fake,0,sizeof(fake));
1107 fake.type = et_mousedown;
1108 fake.w = g->base;
1109 fake.u.mouse.x = pos.x;
1110 fake.u.mouse.y = pos.y+pos.height;
1111 GMenuCreatePopupMenu(gfc->g.base,&fake, mi);
1112 GMenuItemArrayFree(mi);
1113 }
1114 return( true );
1115 }
1116
1117 /* Routine to be called as the mouse moves across the dlg */
1118 void GFileChooserPopupCheck(GGadget *g,GEvent *e) {
1119 GFileChooser *gfc = (GFileChooser *) g;
1120 int inside=false;
1121
1122 if ( e->type == et_mousemove && (e->u.mouse.state&ksm_buttons)==0 ) {
1123 GGadgetEndPopup();
1124 for ( g=((GContainerD *) (gfc->g.base->widget_data))->gadgets; g!=NULL; g=g->prev ) {
1125 if ( g!=(GGadget *) gfc && g!=(GGadget *) (gfc->filterb) &&
1126 g!=(GGadget *) (gfc->files) &&
1127 g->takes_input &&
1128 e->u.mouse.x >= g->r.x &&
1129 e->u.mouse.x<g->r.x+g->r.width &&
1130 e->u.mouse.y >= g->r.y &&
1131 e->u.mouse.y<g->r.y+g->r.height ) {
1132 inside = true;
1133 break;
1134 }
1135 }
1136 if ( !inside )
1137 GGadgetPreparePopup(gfc->g.base,gfc->wildcard);
1138 } else if ( e->type == et_mousedown && e->u.mouse.button==3 ) {
1139 GFCPopupMenu(g,e);
1140 }
1141 }
1142
1143 /* Routine to be called by the filter button */
1144 void GFileChooserFilterIt(GGadget *g) {
1145 GFileChooser *gfc = (GFileChooser *) g;
1146 unichar_t *pt, *spt, *slashpt, *dir, *temp;
1147 unichar_t *tofree = NULL;
1148 int wasdir;
1149
1150 wasdir = gfc->lastname!=NULL;
1151
1152 spt = (unichar_t *) _GGadgetGetTitle(&gfc->name->g);
1153 #ifdef _WIN32
1154 spt = tofree = u_GFileNormalizePath(u_copy(spt));
1155 #endif
1156 if ( *spt=='\0' ) { /* Werner tells me that pressing the Filter button with nothing should show the default filter mask */
1157 if ( gfc->wildcard!=NULL )
1158 GGadgetSetTitle(&gfc->name->g,gfc->wildcard);
1159 return;
1160 }
1161
1162 if (( slashpt = u_strrchr(spt,'/'))==NULL )
1163 slashpt = spt;
1164 else
1165 ++slashpt;
1166 pt = slashpt;
1167 while ( *pt && *pt!='*' && *pt!='?' && *pt!='[' && *pt!='{' )
1168 ++pt;
1169 if ( *pt!='\0' ) {
1170 free(gfc->wildcard);
1171 gfc->wildcard = u_copy(slashpt);
1172 } else if ( gfc->lastname==NULL )
1173 gfc->lastname = u_copy(slashpt);
1174 if( u_GFileIsAbsolute(spt) )
1175 dir = u_copyn(spt,slashpt-spt);
1176 else {
1177 unichar_t *curdir = GFileChooserGetCurDir(gfc,-1);
1178 if ( slashpt!=spt ) {
1179 temp = u_copyn(spt,slashpt-spt);
1180 dir = u_GFileAppendFile(curdir,temp,true);
1181 free(temp);
1182 } else if ( wasdir && *pt=='\0' )
1183 dir = u_GFileAppendFile(curdir,spt,true);
1184 else
1185 dir = curdir;
1186 if ( dir!=curdir )
1187 free(curdir);
1188 }
1189 GFileChooserScanDir(gfc,dir);
1190 free(dir);
1191 free(tofree);
1192 }
1193
1194 /* A function that may be connected to a filter button as its handle_controlevent */
1195 int GFileChooserFilterEh(GGadget *g, GEvent *e) {
1196 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate )
1197 GFileChooserFilterIt(GGadgetGetUserData(g));
1198 return( true );
1199 }
1200
1201 void GFileChooserConnectButtons(GGadget *g,GGadget *ok, GGadget *filter) {
1202 GFileChooser *gfc = (GFileChooser *) g;
1203 gfc->ok = (GButton *) ok;
1204 gfc->filterb = (GButton *) filter;
1205 }
1206
1207 void GFileChooserSetFilterText(GGadget *g,const unichar_t *wildcard) {
1208 GFileChooser *gfc = (GFileChooser *) g;
1209 free(gfc->wildcard);
1210 gfc->wildcard = u_copy(wildcard);
1211 }
1212
1213 void GFileChooserSetFilterText8(GGadget *g,const char *wildcard) {
1214 GFileChooser *gfc = (GFileChooser *) g;
1215 free(gfc->wildcard);
1216 gfc->wildcard = utf82u_copy(wildcard);
1217 }
1218
1219 unichar_t *GFileChooserGetFilterText(GGadget *g) {
1220 GFileChooser *gfc = (GFileChooser *) g;
1221 return( gfc->wildcard );
1222 }
1223
1224 char *GFileChooserGetFilterText8(GGadget *g) {
1225 GFileChooser *gfc = (GFileChooser *) g;
1226 return( u2utf8_copy(gfc->wildcard) );
1227 }
1228
1229
1230 void GFileChooserSetFilterFunc(GGadget *g,GFileChooserFilterType filter) {
1231 GFileChooser *gfc = (GFileChooser *) g;
1232 if ( filter==NULL )
1233 filter = GFileChooserDefFilter;
1234 gfc->filter = filter;
1235 }
1236
1237 GFileChooserFilterType GFileChooserGetFilterFunc(GGadget *g) {
1238 GFileChooser *gfc = (GFileChooser *) g;
1239 return( gfc->filter );
1240 }
1241
1242 /**
1243 * no change to the current filename by default
1244 *
1245 * if a change is desired, then currentFilename should point to the
1246 * new string and return 1 to allow the caller to free this new
1247 * string.
1248 */
1249 int GFileChooserDefInputFilenameFunc( GGadget *g,
1250 const unichar_t ** currentFilename,
1251 unichar_t* oldfilename ) {
1252 return 0;
1253 }
1254
1255 void GFileChooserSetInputFilenameFunc(GGadget *g,GFileChooserInputFilenameFuncType func) {
1256 GFileChooser *gfc = (GFileChooser *) g;
1257 if ( func==NULL )
1258 func = GFileChooserDefInputFilenameFunc;
1259 gfc->inputfilenamefunc = func;
1260 }
1261
1262 GFileChooserInputFilenameFuncType GFileChooserGetInputFilenameFunc(GGadget *g) {
1263 GFileChooser *gfc = (GFileChooser *) g;
1264 if ( gfc->inputfilenamefunc==NULL )
1265 return GFileChooserDefInputFilenameFunc;
1266 return( gfc->inputfilenamefunc );
1267 }
1268
1269
1270 void GFileChooserSetFilename(GGadget *g,const unichar_t *defaultfile)
1271 {
1272 GFileChooser *gfc = (GFileChooser *) g;
1273
1274 GGadgetSetTitle(g,defaultfile);
1275
1276 // if this is the first time we are here, we assume
1277 // the current filename is what it was before. Less NULL
1278 // checks in the callback function below.
1279 if(!gfc->inputfilenameprevchar)
1280 gfc->inputfilenameprevchar = u_copy(_GGadgetGetTitle(&gfc->name->g));
1281
1282 }
1283
1284
1285 void GFileChooserSetMimetypes(GGadget *g,unichar_t **mimetypes) {
1286 GFileChooser *gfc = (GFileChooser *) g;
1287 int i;
1288
1289 if ( gfc->mimetypes ) {
1290 for ( i=0; gfc->mimetypes[i]!=NULL; ++i )
1291 free( gfc->mimetypes[i]);
1292 free(gfc->mimetypes);
1293 }
1294 if ( mimetypes ) {
1295 for ( i=0; mimetypes[i]!=NULL; ++i );
1296 gfc->mimetypes = malloc((i+1)*sizeof(unichar_t *));
1297 for ( i=0; mimetypes[i]!=NULL; ++i )
1298 gfc->mimetypes[i] = u_copy(mimetypes[i]);
1299 gfc->mimetypes[i] = NULL;
1300 } else
1301 gfc->mimetypes = NULL;
1302 }
1303
1304 unichar_t **GFileChooserGetMimetypes(GGadget *g) {
1305 GFileChooser *gfc = (GFileChooser *) g;
1306 return( gfc->mimetypes );
1307 }
1308
1309 /* Change the current file, or the current directory/file */
1310 static void GFileChooserSetTitle(GGadget *g,const unichar_t *tit) {
1311 GFileChooser *gfc = (GFileChooser *) g;
1312 unichar_t *pt, *curdir, *temp, *dir, *base;
1313
1314 if ( tit==NULL ) {
1315 curdir = GFileChooserGetCurDir(gfc,-1);
1316 GFileChooserScanDir(gfc,curdir);
1317 free(curdir);
1318 return;
1319 }
1320
1321 pt = u_strrchr(tit,'/');
1322 free(gfc->lastname);
1323 gfc->lastname = NULL;
1324
1325 if ( u_GFileIsAbsolute(tit) ){
1326 base = uc_strstr(tit, "://");
1327 if(!base) base = (unichar_t*) tit;
1328 if(pt > base && pt[1] && (pt[1]!='.' || pt[2]!='\0')){
1329 gfc->lastname = u_copy(pt+1);
1330 dir = u_copyn(tit, pt-tit);
1331 }
1332 else{
1333 dir = u_copy(tit);
1334 }
1335 GFileChooserScanDir(gfc,dir);
1336 free(dir);
1337 } else if ( pt==NULL ) {
1338 GGadgetSetTitle(&gfc->name->g,tit);
1339 curdir = GFileChooserGetCurDir(gfc,-1);
1340 GFileChooserScanDir(gfc,curdir);
1341 free(curdir);
1342 } else {
1343 curdir = GFileChooserGetCurDir(gfc,-1);
1344 temp = u_copyn(tit,pt-tit);
1345 dir = u_GFileAppendFile(curdir,temp,true);
1346 free(temp); free(curdir);
1347 free(gfc->lastname);
1348 if ( pt[1]!='\0' )
1349 gfc->lastname = u_copy(pt+1);
1350 GFileChooserScanDir(gfc,dir);
1351 free(dir);
1352 }
1353 }
1354
1355 void GFileChooserRefreshList(GGadget *g) {
1356 GFileChooser *gfc = (GFileChooser *) g;
1357 unichar_t *curdir;
1358
1359 curdir = GFileChooserGetCurDir(gfc,-1);
1360 GFileChooserScanDir(gfc,curdir);
1361 free(curdir);
1362 }
1363
1364 /* Get the current directory/file */
1365 static unichar_t *GFileChooserGetTitle(GGadget *g) {
1366 GFileChooser *gfc = (GFileChooser *) g;
1367 unichar_t *spt, *curdir, *file;
1368
1369 spt = u_GFileNormalizePath(u_copy((unichar_t *)_GGadgetGetTitle(&gfc->name->g)));
1370 if ( u_GFileIsAbsolute(spt) )
1371 file = spt;
1372 else {
1373 curdir = GFileChooserGetCurDir(gfc,-1);
1374 file = u_GFileAppendFile(curdir,spt,gfc->lastname!=NULL);
1375 free(curdir);
1376 }
1377 if (file != spt) {
1378 free(spt);
1379 }
1380 return( file );
1381 }
1382
1383 static void GFileChooser_destroy(GGadget *g) {
1384 GFileChooser *gfc = (GFileChooser *) g;
1385 int i;
1386
1387 free(lastdir);
1388 lastdir = GFileChooserGetCurDir(gfc,-1);
1389
1390 GGadgetDestroy(&gfc->topbox->g); /* destroys everything */
1391 if ( gfc->paths!=NULL ) {
1392 for ( i=0; gfc->paths[i]!=NULL; ++i )
1393 free(gfc->paths[i]);
1394 free(gfc->paths);
1395 }
1396 free(gfc->wildcard);
1397 free(gfc->lastname);
1398 if ( gfc->mimetypes ) {
1399 for ( i=0; gfc->mimetypes[i]!=NULL; ++i )
1400 free( gfc->mimetypes[i]);
1401 free(gfc->mimetypes);
1402 }
1403 for ( i=0; i<gfc->hcnt; ++i )
1404 free(gfc->history[i]);
1405 free(gfc->history);
1406 _ggadget_destroy(&gfc->g);
1407 }
1408
1409 static int gfilechooser_expose(GWindow pixmap, GGadget *g, GEvent *event) {
1410 return( true );
1411 }
1412
1413 static int gfilechooser_mouse(GGadget *g, GEvent *event) {
1414 GFileChooser *gfc = (GFileChooser *) g;
1415
1416 if (( event->type==et_mouseup || event->type==et_mousedown ) &&
1417 (event->u.mouse.button>=4 && event->u.mouse.button<=7) ) {
1418 if ( gfc->files->vsb!=NULL )
1419 return( GGadgetDispatchEvent(&gfc->files->vsb->g,event));
1420 else
1421 return( true );
1422 }
1423
1424 return( false );
1425 }
1426
1427 static int gfilechooser_noop(GGadget *g, GEvent *event) {
1428 return( false );
1429 }
1430
1431 static int gfilechooser_timer(GGadget *g, GEvent *event) {
1432 return( false );
1433 }
1434
1435 static void gfilechooser_move(GGadget *g, int32_t x, int32_t y ) {
1436 GFileChooser *gfc = (GFileChooser *) g;
1437
1438 GGadgetMove(&gfc->topbox->g,x,y);
1439 _ggadget_move(g,x,y);
1440 }
1441
1442 static void gfilechooser_resize(GGadget *g, int32_t width, int32_t height ) {
1443 GFileChooser *gfc = (GFileChooser *) g;
1444
1445 GGadgetResize(&gfc->topbox->g,width,height);
1446 _ggadget_resize(g,width,height);
1447 }
1448
1449 static void gfilechooser_setvisible(GGadget *g, int visible ) {
1450 GFileChooser *gfc = (GFileChooser *) g;
1451 GGadgetSetVisible(&gfc->files->g,visible);
1452 GGadgetSetVisible(&gfc->directories->g,visible);
1453 GGadgetSetVisible(&gfc->name->g,visible);
1454 GGadgetSetVisible(&gfc->up->g,visible);
1455 GGadgetSetVisible(&gfc->home->g,visible);
1456 GGadgetSetVisible(&gfc->bookmarks->g,visible);
1457 GGadgetSetVisible(&gfc->config->g,visible);
1458 GGadgetSetVisible(&gfc->topbox->g,visible);
1459 _ggadget_setvisible(g,visible);
1460 }
1461
1462 static void gfilechooser_setenabled(GGadget *g, int enabled ) {
1463 GFileChooser *gfc = (GFileChooser *) g;
1464 GGadgetSetEnabled(&gfc->files->g,enabled);
1465 GGadgetSetEnabled(&gfc->subdirs->g,enabled);
1466 GGadgetSetEnabled(&gfc->directories->g,enabled);
1467 GGadgetSetEnabled(&gfc->name->g,enabled);
1468 GGadgetSetEnabled(&gfc->up->g,enabled);
1469 GGadgetSetEnabled(&gfc->home->g,enabled);
1470 GGadgetSetEnabled(&gfc->bookmarks->g,enabled);
1471 GGadgetSetEnabled(&gfc->config->g,enabled);
1472 GGadgetSetEnabled(&gfc->topbox->g,enabled);
1473 _ggadget_setenabled(g,enabled);
1474 }
1475
1476 static void GFileChooserGetDesiredSize(GGadget *g,GRect *outer,GRect *inner) {
1477 if ( inner!=NULL ) {
1478 int bp = GBoxBorderWidth(g->base,g->box);
1479 inner->x = inner->y = 0;
1480 inner->width = g->desired_width - 2*bp;
1481 inner->height = g->desired_height - 2*bp;
1482 }
1483 if ( outer!=NULL ) {
1484 outer->x = outer->y = 0;
1485 outer->width = g->desired_width;
1486 outer->height = g->desired_height;
1487 }
1488 }
1489
1490 static int gfilechooser_FillsWindow(GGadget *g) {
1491 return( g->prev==NULL &&
1492 (_GWidgetGetGadgets(g->base)==g ||
1493 _GWidgetGetGadgets(g->base)==(GGadget *) ((GFileChooser *) g)->up ));
1494 }
1495
1496 struct gfuncs GFileChooser_funcs = {
1497 0,
1498 sizeof(struct gfuncs),
1499
1500 gfilechooser_expose,
1501 gfilechooser_mouse,
1502 gfilechooser_noop,
1503 NULL,
1504 NULL,
1505 gfilechooser_timer,
1506 NULL,
1507
1508 _ggadget_redraw,
1509 gfilechooser_move,
1510 gfilechooser_resize,
1511 gfilechooser_setvisible,
1512 gfilechooser_setenabled,
1513 _ggadget_getsize,
1514 _ggadget_getinnersize,
1515
1516 GFileChooser_destroy,
1517
1518 GFileChooserSetTitle,
1519 NULL,
1520 GFileChooserGetTitle,
1521 NULL,
1522 NULL,
1523 NULL,
1524 NULL,
1525 NULL,
1526
1527 NULL,
1528 NULL,
1529 NULL,
1530 NULL,
1531 NULL,
1532 NULL,
1533 NULL,
1534 NULL,
1535 NULL,
1536 NULL,
1537
1538 GFileChooserGetDesiredSize,
1539 _ggadget_setDesiredSize,
1540 gfilechooser_FillsWindow,
1541 NULL
1542 };
1543
1544 static void GFileChooserCreateChildren(GFileChooser *gfc, int flags) {
1545 GGadgetCreateData gcd[9], boxes[4], *varray[9], *harray[12], *harray2[4];
1546 GTextInfo label[9];
1547 int k=0, l=0, homek, upk, bookk, confk, dirsk, subdirsk, filesk, textk;
1548
1549 memset(&gcd,'\0',sizeof(gcd));
1550 memset(&boxes,'\0',sizeof(boxes));
1551 memset(&label,'\0',sizeof(label));
1552
1553 gcd[k].gd.flags = gg_visible|gg_enabled;
1554 gcd[k].gd.popup_msg = _("Home Folder");
1555 label[k].image = &_GIcon_homefolder;
1556 gcd[k].gd.label = &label[k];
1557 gcd[k].gd.handle_controlevent = GFileChooserHome;
1558 homek = k;
1559 gcd[k++].creator = GButtonCreate;
1560 harray[l++] = &gcd[k-1]; harray[l++] = GCD_Glue;
1561
1562 gcd[k].gd.flags = gg_visible|gg_enabled;
1563 gcd[k].gd.popup_msg = _("Bookmarks");
1564 label[k].image = &_GIcon_bookmark;
1565 gcd[k].gd.label = &label[k];
1566 gcd[k].gd.handle_controlevent = GFileChooserBookmarks;
1567 bookk = k;
1568 gcd[k++].creator = GButtonCreate;
1569 harray[l++] = &gcd[k-1]; harray[l++] = GCD_Glue;
1570
1571 gcd[k].gd.flags = gg_visible|gg_enabled|gg_list_exactlyone;
1572 gcd[k].gd.handle_controlevent = GFileChooserDListChanged;
1573 dirsk = k;
1574 gcd[k++].creator = GListButtonCreate;
1575 harray[l++] = &gcd[k-1]; harray[l++] = GCD_Glue;
1576
1577 gcd[k].gd.flags = gg_visible|gg_enabled;
1578 gcd[k].gd.popup_msg = _("Parent Folder");
1579 label[k].image = &_GIcon_updir;
1580 gcd[k].gd.label = &label[k];
1581 gcd[k].gd.handle_controlevent = GFileChooserUpDirButton;
1582 upk = k;
1583 gcd[k++].creator = GButtonCreate;
1584 harray[l++] = &gcd[k-1]; harray[l++] = GCD_Glue;
1585
1586 gcd[k].gd.flags = gg_visible|gg_enabled;
1587 gcd[k].gd.popup_msg = _("Configure");
1588 label[k].image = &_GIcon_configtool;
1589 gcd[k].gd.label = &label[k];
1590 gcd[k].gd.handle_controlevent = GFileChooserConfigure;
1591 confk = k;
1592 gcd[k++].creator = GButtonCreate;
1593 harray[l++] = &gcd[k-1]; harray[l++] = NULL;
1594
1595 boxes[2].gd.flags = gg_enabled|gg_visible;
1596 boxes[2].gd.u.boxelements = harray;
1597 boxes[2].creator = GHBoxCreate;
1598
1599 l=0;
1600 varray[l++] = &boxes[2];
1601
1602 if ( dir_placement==dirs_separate )
1603 gcd[k].gd.flags = gg_visible|gg_enabled|gg_list_alphabetic|gg_list_exactlyone;
1604 else
1605 gcd[k].gd.flags = gg_enabled|gg_list_alphabetic|gg_list_exactlyone;
1606 gcd[k].gd.handle_controlevent = GFileChooserFListSelected;
1607 subdirsk = k;
1608 gcd[k++].creator = GListCreate;
1609 harray2[0] = &gcd[k-1];
1610
1611 if ( flags & gg_file_multiple )
1612 gcd[k].gd.flags = gg_visible|gg_enabled|gg_list_alphabetic|gg_list_multiplesel;
1613 else
1614 gcd[k].gd.flags = gg_visible|gg_enabled|gg_list_alphabetic|gg_list_exactlyone;
1615 gcd[k].gd.handle_controlevent = GFileChooserFListSelected;
1616 filesk = k;
1617 gcd[k++].creator = GListCreate;
1618 harray2[1] = &gcd[k-1]; harray2[2] = NULL;
1619
1620 boxes[3].gd.flags = gg_enabled|gg_visible;
1621 boxes[3].gd.u.boxelements = harray2;
1622 boxes[3].creator = GHBoxCreate;
1623 varray[l++] = &boxes[3];
1624
1625 gcd[k].gd.flags = gg_visible|gg_enabled;
1626 gcd[k].gd.handle_controlevent = GFileChooserTextChanged;
1627 textk = k;
1628 if ( flags&gg_file_pulldown )
1629 gcd[k++].creator = GListFieldCreate;
1630 else
1631 gcd[k++].creator = GTextCompletionCreate;
1632 varray[l++] = &gcd[k-1]; varray[l] = NULL;
1633
1634 boxes[0].gd.pos.x = gfc->g.r.x;
1635 boxes[0].gd.pos.y = gfc->g.r.y;
1636 boxes[0].gd.pos.width = gfc->g.r.width;
1637 boxes[0].gd.pos.height = gfc->g.r.height;
1638 boxes[0].gd.flags = gg_enabled|gg_visible;
1639 boxes[0].gd.u.boxelements = varray;
1640 boxes[0].creator = GVBoxCreate;
1641
1642 for ( l=0; l<k; ++l )
1643 gcd[l].data = gfc;
1644
1645 GGadgetsCreate(gfc->g.base,boxes);
1646
1647 gfc->topbox = (GHVBox *) boxes[0].ret;
1648 gfc->home = (GButton *) gcd[homek].ret;
1649 gfc->bookmarks = (GButton *) gcd[bookk].ret;
1650 gfc->directories = (GListButton *) gcd[dirsk].ret;
1651 gfc->up = (GButton *) gcd[upk ].ret;
1652 gfc->config = (GButton *) gcd[confk].ret;
1653 gfc->subdirs = (GList *) gcd[subdirsk].ret;
1654 gfc->files = (GList *) gcd[filesk].ret;
1655 gfc->name = (GTextField *) gcd[textk].ret;
1656
1657 gfc->home->g.contained = true;
1658 gfc->bookmarks->g.contained = true;
1659 gfc->directories->g.contained = true;
1660 gfc->up->g.contained = true;
1661 gfc->config->g.contained = true;
1662 gfc->subdirs->g.contained = true;
1663 gfc->files->g.contained = true;
1664 gfc->name->g.contained = true;
1665 gfc->topbox->g.contained = true;
1666
1667 GCompletionFieldSetCompletion(&gfc->name->g,GFileChooserCompletion);
1668 GCompletionFieldSetCompletionMode(&gfc->name->g,true);
1669
1670 GHVBoxSetExpandableRow(boxes[0].ret,1);
1671 GHVBoxSetExpandableCol(boxes[2].ret,4);
1672 if ( boxes[0].gd.pos.width!=0 && boxes[0].gd.pos.height!=0 )
1673 GGadgetResize(boxes[0].ret,boxes[0].gd.pos.width,boxes[0].gd.pos.height);
1674 }
1675
1676 GGadget *GFileChooserCreate(struct gwindow *base, GGadgetData *gd,void *data) {
1677 GFileChooser *gfc = (GFileChooser *) calloc(1,sizeof(GFileChooser));
1678
1679 gfc->g.funcs = &GFileChooser_funcs;
1680 _GGadget_Create(&gfc->g,base,gd,data,&gfilechooser_box);
1681 gfc->g.takes_input = gfc->g.takes_keyboard = false; gfc->g.focusable = false;
1682 if ( gfc->g.r.width == 0 )
1683 gfc->g.r.width = GGadgetScale(GDrawPointsToPixels(base,140));
1684 if ( gfc->g.r.height == 0 )
1685 gfc->g.r.height = GDrawPointsToPixels(base,100);
1686 gfc->g.desired_width = gfc->g.r.width;
1687 gfc->g.desired_height = gfc->g.r.height;
1688 gfc->g.inner = gfc->g.r;
1689 _GGadget_FinalPosition(&gfc->g,base,gd);
1690
1691 GFileChooserCreateChildren(gfc, gd->flags);
1692 gfc->filter = GFileChooserDefFilter;
1693 GFileChooserSetInputFilenameFunc( (GGadget*)gfc, 0 );
1694 if ( gd->flags & gg_group_end )
1695 _GGadgetCloseGroup(&gfc->g);
1696
1697 if ( lastdir==NULL ) {
1698 lastdir = def2u_copy(GFileGetAbsoluteName("./"));
1699 }
1700 if ( gd->label==NULL || gd->label->text==NULL )
1701 GFileChooserSetTitle(&gfc->g,lastdir);
1702 else if ( u_GFileIsAbsolute(gd->label->text) )
1703 GFileChooserSetTitle(&gfc->g,gd->label->text);
1704 else {
1705 unichar_t *temp = u_GFileAppendFile(lastdir,gd->label->text,false);
1706 temp = u_GFileNormalize(temp);
1707 GFileChooserSetTitle(&gfc->g,temp);
1708 free(temp);
1709 }
1710
1711 return( &gfc->g );
1712 }
1713
1714 void GFileChooserSetDir(GGadget *g,unichar_t *dir) {
1715 GFileChooserScanDir((GFileChooser *) g,dir);
1716 }
1717
1718 unichar_t *GFileChooserGetDir(GGadget *g) {
1719 return( GFileChooserGetCurDir((GFileChooser *) g,-1));
1720 }
1721
1722 void GFileChooserGetChildren(GGadget *g,GGadget **pulldown, GGadget **list, GGadget **tf) {
1723 GFileChooser *gfc = (GFileChooser *)g;
1724
1725 if ( pulldown!=NULL ) *pulldown = &gfc->directories->g;
1726 if ( tf!=NULL ) *tf = &gfc->name->g;
1727 if ( list!=NULL ) *list = &gfc->files->g;
1728 }
1729
1730 int GFileChooserPosIsDir(GGadget *g, int pos) {
1731 GFileChooser *gfc = (GFileChooser *)g;
1732 GGadget *list = &gfc->files->g;
1733 GTextInfo *ti;
1734
1735 ti = GGadgetGetListItem(list,pos);
1736 if ( ti==NULL )
1737 return( false );
1738
1739 return( ti->checked );
1740 }
1741
1742 unichar_t *GFileChooserFileNameOfPos(GGadget *g, int pos) {
1743 GFileChooser *gfc = (GFileChooser *)g;
1744 GGadget *list = &gfc->files->g;
1745 GTextInfo *ti;
1746 unichar_t *curdir, *file;
1747
1748 ti = GGadgetGetListItem(list,pos);
1749 if ( ti==NULL )
1750 return( NULL );
1751
1752 curdir = GFileChooserGetCurDir(gfc,-1);
1753 file = u_GFileAppendFile(curdir,ti->text,false);
1754 free(curdir);
1755 return( file );
1756 }
1757
1758 void GFileChooserSetShowHidden(int sh) {
1759 showhidden = sh;
1760 }
1761
1762 int GFileChooserGetShowHidden(void) {
1763 return( showhidden );
1764 }
1765
1766 void GFileChooserSetDirectoryPlacement(int dp) {
1767 dir_placement = dp;
1768 }
1769
1770 int GFileChooserGetDirectoryPlacement(void) {
1771 return( dir_placement );
1772 }
1773
1774 void GFileChooserSetBookmarks(unichar_t **b) {
1775 if ( bookmarks!=NULL && bookmarks!=b ) {
1776 int i;
1777
1778 for ( i=0; bookmarks[i]!=NULL; ++i )
1779 free(bookmarks[i]);
1780 free(bookmarks);
1781 }
1782 bookmarks = b;
1783 }
1784
1785 unichar_t **GFileChooserGetBookmarks(void) {
1786 return( bookmarks );
1787 }
1788
1789 void GFileChooserSetPrefsChangedCallback(void *data, void (*p_c)(void *)) {
1790 prefs_changed = p_c;
1791 prefs_changed_data = data;
1792 }
1793
1794 void GFileChooserSetPaths(GGadget *g, const char* const* path) {
1795 unichar_t **dirs = NULL;
1796 int dcnt;
1797 GFileChooser *gfc = (GFileChooser *) g;
1798
1799 if ( gfc->paths!=NULL ) {
1800 for ( dcnt=0; gfc->paths[dcnt]!=NULL; ++dcnt )
1801 free( gfc->paths[dcnt] );
1802 free(gfc->paths);
1803 gfc->paths = NULL;
1804 }
1805 if ( path==NULL || path[0]==NULL )
1806 return;
1807
1808 for ( dcnt=0; path[dcnt]!=NULL; ++dcnt );
1809 gfc->paths = dirs = malloc((dcnt+1)*sizeof(unichar_t *));
1810 for ( dcnt=0; path[dcnt]!=NULL; ++dcnt )
1811 dirs[dcnt] = utf82u_copy(path[dcnt]);
1812 dirs[dcnt] = NULL;
1813 }
1814
1815 /* Split multipath of form [<dir>/<file_name>; <file_name>; <file_name>; ...;
1816 * <file_name>] */
1817 char** GFileChooserGetMultipleFiles(const char* multipath) {
1818 char *path = NULL, *eod, *full;
1819 const char *spt = multipath, *file = multipath, *fpt;
1820 char** path_list = NULL;
1821 size_t sc_count = 0, i = 0;
1822 if (multipath == NULL) return NULL;
1823
1824 /* Count delimiting semicolons and allocate the NULL-terminated array of
1825 * strings */
1826 for (sc_count = 0; spt[sc_count];
1827 spt[sc_count] == ';' ? sc_count++ : *spt++);
1828 path_list = calloc(sc_count + 2, sizeof(char*));
1829
1830 eod = strrchr(multipath, '/');
1831 if (eod != NULL) {
1832 path = copyn(multipath, eod - multipath);
1833 file = eod + 1;
1834 }
1835
1836 do {
1837 fpt = strstr(file, "; ");
1838
1839 if (fpt == NULL) fpt = file + strlen(file);
1840 full = malloc(c_strlen(path) + 1 + (fpt - file) + 1);
1841 full[0] = '\0';
1842
1843 if (path) {
1844 strcpy(full, path);
1845 strcat(full, "/");
1846 }
1847 strncat(full, file, fpt - file);
1848 path_list[i++] = full;
1849 file = fpt + 2;
1850 } while (*fpt != '\0');
1851
1852 free(path);
1853 return path_list;
1854 }