-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFsb.c
3106 lines (2653 loc) · 107 KB
/
Fsb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2021 Olaf Wintermann
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "Fsb.h"
#include "FsbP.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include <limits.h>
#include <dirent.h>
#include <fnmatch.h>
#include <Xm/XmAll.h>
#include <Xm/DropDown.h>
#ifdef FSB_ENABLE_DETAIL
#include <XmL/Grid.h>
#endif
#define WIDGET_SPACING 5
#define WINDOW_SPACING 8
#define BUTTON_EXTRA_SPACE 4
#define DATE_FORMAT_SAME_YEAR "%b %d %H:%M"
#define DATE_FORMAT_OTHER_YEAR "%b %d %Y"
#define KB_SUFFIX "KiB"
#define MB_SUFFIX "MiB"
#define GB_SUFFIX "GiB"
#define TB_SUFFIX "TiB"
#define FSB_ERROR_TITLE "Error"
#define FSB_ERROR_CHAR "Character '/' is not allowed in file names"
#define FSB_ERROR_RENAME "Cannot rename file: %s"
#define FSB_ERROR_DELETE "Cannot delete file: %s"
#define FSB_ERROR_CREATE_FOLDER "Cannot create folder: %s"
#define FSB_ERROR_OPEN_DIR "Cannot open directory: %s"
#define FSB_DETAIL_HEADINGS "Name|Size|Last Modified"
static void fsb_class_init(void);
static void fsb_class_part_init (WidgetClass wc);
static void fsb_init(Widget request, Widget neww, ArgList args, Cardinal *num_args);
static void fsb_resize(Widget widget);
static void fsb_realize(Widget widget, XtValueMask *mask, XSetWindowAttributes *attributes);
static void fsb_destroy(Widget widget);
static Boolean fsb_set_values(Widget old, Widget request, Widget neww, ArgList args, Cardinal *num_args);
static Boolean fsb_acceptfocus(Widget widget, Time *time);
static void fsb_insert_child(Widget child);
static void fsb_mapcb(Widget widget, XtPointer u, XtPointer cb);
static int FSBGlobFilter(const char *a, const char *b);
static void FSBUpdateTitle(Widget w);
static void FocusInAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
static void ErrDialog(XnFileSelectionBox w, const char *title, const char *errmsg);
static void FSBRename(XnFileSelectionBox fsb, const char *path);
static void FSBDelete(XnFileSelectionBox fsb, const char *path);
static void FSBSelectItem(XnFileSelectionBox fsb, const char *item);
static char* set_selected_path(XnFileSelectionBox data, XmString item);
static void FileContextMenuCB(Widget item, XtPointer index, XtPointer cd);
static Widget CreateContextMenu(XnFileSelectionBox fsb, Widget parent, XtCallbackProc callback);
static void FileListUpdate(Widget fsb, Widget view, FileElm *dirlist, int dircount, FileElm *files, int filecount, const char *filter, int maxnamelen, void *userData);
static void FileListSelect(Widget fsb, Widget view, const char *item);
static void FileListCleanup(Widget fsb, Widget view, void *userData);
static void FileListDestroy(Widget fsb, Widget view, void *userData);
static void FileListActivateCB(Widget w, XnFileSelectionBox data, XmListCallbackStruct *cb);
static void FileListSelectCB(Widget w, XnFileSelectionBox data, XmListCallbackStruct *cb);
static void FileListWidgetAdd(XnFileSelectionBox fsb, Widget w, int showHidden, const char *filter, FileElm *ls, int count);
#ifdef FSB_ENABLE_DETAIL
static void FileListDetailUpdate(Widget fsb, Widget view, FileElm *dirlist, int dircount, FileElm *files, int filecount, const char *filter, int maxnamelen, void *userData);
static void FileListDetailSelect(Widget fsb, Widget view, const char *item);
static void FileListDetailCleanup(Widget fsb, Widget view, void *userData);
static void FileListDetailDestroy(Widget fsb, Widget view, void *userData);
static void FileListDetailAdjustColWidth(Widget grid);
static void FileListDetailAdd(XnFileSelectionBox fsb, Widget grid, int showHidden, const char *filter, FileElm *ls, int count, int maxWidth);
#endif
static void FSBNewFolder(Widget w, XnFileSelectionBox data, XtPointer u);
static void FSBHome(Widget w, XnFileSelectionBox data, XtPointer u);
static void FileSelectionCallback(XnFileSelectionBox fsb, XtCallbackList cb, int reason, const char *value);
static void CreateUI(XnFileSelectionBox w);
static FSBViewWidgets CreateView(XnFileSelectionBox w, FSBViewCreateProc createProc, void *userData, Boolean useDirList);
static void AddViewMenuItem(XnFileSelectionBox w, const char *name, int viewIndex);
static void SelectView(XnFileSelectionBox f, int view);
static char* FSBDialogTitle(Widget w);
static FSBViewWidgets CreateListView(Widget fsb, ArgList args, int n, void *userData);
static FSBViewWidgets CreateDetailView(Widget fsb, ArgList args, int n, void *userData);
static const char* GetHomeDir(void);
static char* ConcatPath(const char *parent, const char *name);
static char* FileName(char *path);
static char* ParentPath(const char *path);
//static int CheckFileName(const char *name);
static int filedialog_update_dir(XnFileSelectionBox data, const char *path);
static void filedialog_cleanup_filedata(XnFileSelectionBox data);
static void pathbar_resize(Widget w, PathBar *p, XtPointer d);
static XtResource resources[] = {
{XmNokCallback, XmCCallback, XmRCallback, sizeof(XtCallbackList), XtOffset(XnFileSelectionBox, fsb.okCallback), XmRCallback, NULL},
{XmNcancelCallback, XmCCallback, XmRCallback, sizeof(XtCallbackList), XtOffset(XnFileSelectionBox, fsb.cancelCallback), XmRCallback, NULL},
{XnNwidgetSpacing, XmCSpacing, XmRDimension, sizeof(Dimension), XtOffset(XnFileSelectionBox, fsb.widgetSpacing), XmRImmediate, (XtPointer)WIDGET_SPACING},
{XnNwindowSpacing, XmCSpacing, XmRDimension, sizeof(Dimension), XtOffset(XnFileSelectionBox, fsb.windowSpacing), XmRImmediate, (XtPointer)WINDOW_SPACING},
{XnNfsbType, XnCfsbType, XmRInt, sizeof(int), XtOffset(XnFileSelectionBox, fsb.type), XmRImmediate, (XtPointer)FILEDIALOG_OPEN},
{XnNshowHidden, XnCshowHidden, XmRBoolean, sizeof(Boolean), XtOffset(XnFileSelectionBox, fsb.showHidden), XmRImmediate, (XtPointer)False},
{XnNshowHiddenButton, XnCshowHiddenButton, XmRBoolean, sizeof(Boolean), XtOffset(XnFileSelectionBox, fsb.showHiddenButton), XmRImmediate, (XtPointer)True},
{XnNshowViewMenu, XnCshowViewMenu, XmRBoolean, sizeof(Boolean), XtOffset(XnFileSelectionBox, fsb.showViewMenu), XmRImmediate, (XtPointer)False},
{XnNselectedView, XnCselectedView, XmRInt, sizeof(int), XtOffset(XnFileSelectionBox, fsb.selectedview), XmRImmediate, (XtPointer)0},
{XnNdirectory, XnCdirectory, XmRString, sizeof(XmString), XtOffset(XnFileSelectionBox, fsb.currentPath), XmRString, NULL},
{XnNselectedPath, XnCselectedPath, XmRString, sizeof(XmString), XtOffset(XnFileSelectionBox, fsb.selectedPath), XmRString, NULL},
{XnNhomePath, XnChomePath, XmRString, sizeof(XmString), XtOffset(XnFileSelectionBox, fsb.homePath), XmRString, NULL},
{XnNfilter,XnCfilter,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.filterStr), XmRString, "*"},
{XnNfilterFunc,XnCfilterFunc,XmRFunction,sizeof(FSBFilterFunc),XtOffset(XnFileSelectionBox, fsb.filterFunc), XmRFunction, NULL},
{XnNlabelListView,XnClabelListView,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelListView), XmRString, "List"},
{XnNlabelDetailView,XnClabelDetailView,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelDetailView), XmRString, "Detail"},
{XnNlabelOpenFileTitle,XnClabelOpenFileTitle,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelOpenFileTitle), XmRString, "Open File"},
{XnNlabelSaveFileTitle,XnClabelSaveFileTitle,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelSaveFileTitle), XmRString, "Save File"},
{XnNlabelDirUp,XnClabelDirUp,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelDirUp), XmRString, "Dir Up"},
{XnNlabelHome,XnClabelHome,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelHome), XmRString, "Home"},
{XnNlabelNewFolder,XnClabelNewFolder,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelNewFolder), XmRString, "New Folder"},
{XnNlabelFilterButton,XnClabelFilterButton,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelFilterButton), XmRString, "Filter"},
{XnNlabelShowHiddenFiles,XnClabelShowHiddenFiles,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelShowHiddenFiles), XmRString, "Show hiden files"},
{XnNlabelDirectories,XnClabelDirectories,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelDirectories), XmRString, "Directories"},
{XnNlabelFiles,XnClabelFiles,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelFiles), XmRString, "Files"},
{XnNlabelRename,XnClabelRename,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelRename), XmRString, "Rename"},
{XnNlabelDelete,XnClabelDelete,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelDelete), XmRString, "Delete"},
{XnNlabelOpen,XnClabelOpen,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelOpen), XmRString, "Open"},
{XnNlabelSave,XnClabelSave,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelSave), XmRString, "Save"},
{XnNlabelOk,XnClabelOk,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelOk), XmRString, "OK"},
{XnNlabelCancel,XnClabelCancel,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelCancel), XmRString, "Cancel"},
{XnNlabelHelp,XnClabelHelp,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelHelp), XmRString, "Help"},
{XnNlabelFileName,XnClabelFileName,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelFileName), XmRString, "New File Name"},
{XnNlabelDirectoryName,XnClabelDirectoryName,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelDirectoryName), XmRString, "Directory name:"},
{XnNlabelNewFileName,XnClabelNewFileName,XmRXmString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelNewFileName), XmRString, "New file name:"},
{XnNlabelDeleteFile,XnClabelDeleteFile,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.labelDeleteFile), XmRString, "Delete file '%s'?"},
{XnNdetailHeadings,XnCdetailHeadings,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.detailHeadings), XmRString,FSB_DETAIL_HEADINGS},
{XnNdateFormatSameYear,XnCdateFormatSameYear,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.dateFormatSameYear), XmRString,DATE_FORMAT_SAME_YEAR},
{XnNdateFormatOtherYear,XnNdateFormatOtherYear,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.dateFormatOtherYear), XmRString,DATE_FORMAT_OTHER_YEAR},
{XnNsuffixBytes,XnCsuffixBytes,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.suffixBytes), XmRString,"bytes"},
{XnNsuffixKB,XnCsuffixKB,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.suffixKB), XmRString,KB_SUFFIX},
{XnNsuffixMB,XnCsuffixKB,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.suffixMB), XmRString,MB_SUFFIX},
{XnNsuffixGB,XnCsuffixKB,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.suffixGB), XmRString,GB_SUFFIX},
{XnNsuffixTB,XnCsuffixKB,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.suffixTB), XmRString,TB_SUFFIX},
{XnNerrorTitle,XnCerrorTitle,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.errorTitle), XmRString,FSB_ERROR_TITLE},
{XnNerrorIllegalChar,XnCerrorIllegalChar,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.errorIllegalChar), XmRString,FSB_ERROR_CHAR},
{XnNerrorRename,XnCerrorRename,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.errorRename), XmRString,FSB_ERROR_RENAME},
{XnNerrorCreateFolder,XnCerrorCreateFolder,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.errorFolder), XmRString,FSB_ERROR_CREATE_FOLDER},
{XnNerrorDelete,XnCerrorDelete,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.errorDelete), XmRString,FSB_ERROR_DELETE},
{XnNerrorOpenDir,XnCerrorOpenDir,XmRString,sizeof(XmString),XtOffset(XnFileSelectionBox, fsb.errorOpenDir), XmRString,FSB_ERROR_OPEN_DIR}
};
static XtActionsRec actionslist[] = {
{"focusIn", FocusInAP},
{"NULL", NULL}
};
static char defaultTranslations[] = "<FocusIn>: focusIn()";
static XtResource constraints[] = {};
FSBClassRec fsbWidgetClassRec = {
// Core Class
{
(WidgetClass)&xmFormClassRec,
"XnFSB", // class_name
sizeof(FSBRec), // widget_size
fsb_class_init, // class_initialize
fsb_class_part_init, // class_part_initialize
FALSE, // class_inited
fsb_init, // initialize
NULL, // initialize_hook
fsb_realize, // realize
actionslist, // actions
XtNumber(actionslist), // num_actions
resources, // resources
XtNumber(resources), // num_resources
NULLQUARK, // xrm_class
True, // compress_motion
True, // compress_exposure
True, // compress_enterleave
False, // visible_interest
fsb_destroy, // destroy
fsb_resize, // resize
XtInheritExpose, // expose
fsb_set_values, // set_values
NULL, // set_values_hook
XtInheritSetValuesAlmost, // set_values_almost
NULL, // get_values_hook
fsb_acceptfocus, // accept_focus
XtVersion, // version
NULL, // callback_offsets
defaultTranslations, // tm_table
XtInheritQueryGeometry, // query_geometry
XtInheritDisplayAccelerator, // display_accelerator
NULL, // extension
},
// Composite Class
{
XtInheritGeometryManager, // geometry_manager
XtInheritChangeManaged, // change_managed
fsb_insert_child, // insert_child
XtInheritDeleteChild, // delete_child
NULL, // extension
},
// Constraint Class
{
constraints, // resources
XtNumber(constraints), // num_resources
sizeof(XmFormConstraintRec), // constraint_size
NULL, // initialize
NULL, // destroy
NULL, // set_value
NULL, // extension
},
// XmManager Class
{
XtInheritTranslations, // translations
NULL, // syn_resources
0, // num_syn_resources
NULL, // syn_constraint_resources
0, // num_syn_constraint_resources
XmInheritParentProcess, // parent_process
NULL // extension
},
// XmBulletinBoard
{
FALSE,
NULL,
XmInheritFocusMovedProc,
NULL
},
// XmForm Class
{
NULL
},
// FSB Class
{
0
}
};
WidgetClass xnFsbWidgetClass = (WidgetClass)&fsbWidgetClassRec;
static void fsb_class_init(void) {
}
static void fsb_class_part_init (WidgetClass wc) {
FSBClassRec *fsbClass = (FSBClassRec*)wc;
XmFormClassRec *formClass = (XmFormClassRec*)xmFormWidgetClass;
fsbClass->constraint_class.initialize = formClass->constraint_class.initialize;
fsbClass->constraint_class.set_values = formClass->constraint_class.set_values;
}
#define STRDUP_RES(a) if(a) a = strdup(a)
#define XMS_STRDUP_RES(a) if(a) a = XmStringCopy(a)
static void fsb_init(Widget request, Widget neww, ArgList args, Cardinal *num_args) {
XnFileSelectionBox fsb = (XnFileSelectionBox)neww;
(xmFormClassRec.core_class.initialize)(request, neww, args, num_args);
fsb->fsb.disable_set_values = 0;
STRDUP_RES(fsb->fsb.homePath);
STRDUP_RES(fsb->fsb.selectedPath);
STRDUP_RES(fsb->fsb.currentPath);
STRDUP_RES(fsb->fsb.filterStr);
STRDUP_RES(fsb->fsb.labelListView);
STRDUP_RES(fsb->fsb.labelDetailView);
STRDUP_RES(fsb->fsb.labelOpenFileTitle);
STRDUP_RES(fsb->fsb.labelSaveFileTitle);
XMS_STRDUP_RES(fsb->fsb.labelDirUp);
XMS_STRDUP_RES(fsb->fsb.labelHome);
XMS_STRDUP_RES(fsb->fsb.labelNewFolder);
XMS_STRDUP_RES(fsb->fsb.labelFilterButton);
XMS_STRDUP_RES(fsb->fsb.labelShowHiddenFiles);
XMS_STRDUP_RES(fsb->fsb.labelDirectories);
XMS_STRDUP_RES(fsb->fsb.labelFiles);
XMS_STRDUP_RES(fsb->fsb.labelRename);
XMS_STRDUP_RES(fsb->fsb.labelDelete);
XMS_STRDUP_RES(fsb->fsb.labelOpen);
XMS_STRDUP_RES(fsb->fsb.labelSave);
XMS_STRDUP_RES(fsb->fsb.labelCancel);
XMS_STRDUP_RES(fsb->fsb.labelHelp);
XMS_STRDUP_RES(fsb->fsb.labelFileName);
XMS_STRDUP_RES(fsb->fsb.labelDirectoryName);
XMS_STRDUP_RES(fsb->fsb.labelNewFileName);
STRDUP_RES(fsb->fsb.labelDeleteFile);
STRDUP_RES(fsb->fsb.detailHeadings);
STRDUP_RES(fsb->fsb.dateFormatSameYear);
STRDUP_RES(fsb->fsb.dateFormatOtherYear);
STRDUP_RES(fsb->fsb.suffixBytes);
STRDUP_RES(fsb->fsb.suffixKB);
STRDUP_RES(fsb->fsb.suffixMB);
STRDUP_RES(fsb->fsb.suffixGB);
STRDUP_RES(fsb->fsb.suffixTB);
STRDUP_RES(fsb->fsb.errorTitle);
STRDUP_RES(fsb->fsb.errorIllegalChar);
STRDUP_RES(fsb->fsb.errorRename);
STRDUP_RES(fsb->fsb.errorFolder);
STRDUP_RES(fsb->fsb.errorDelete);
STRDUP_RES(fsb->fsb.errorOpenDir);
CreateUI((XnFileSelectionBox)fsb);
XtAddCallback(neww, XmNmapCallback, fsb_mapcb, NULL);
}
#define STR_FREE(a) if(a) free(a)
#define XMSTR_FREE(a) if(a) XmStringFree(a)
static void fsb_destroy(Widget widget) {
XnFileSelectionBox w = (XnFileSelectionBox)widget;
// destroy all views
for(int i=0;i<w->fsb.numviews;i++) {
FSBView v = w->fsb.view[i];
v.destroy(widget, v.widget, v.userData);
}
STR_FREE(w->fsb.homePath);
// free filelists
filedialog_cleanup_filedata(w);
STR_FREE(w->fsb.currentPath);
STR_FREE(w->fsb.selectedPath);
STR_FREE(w->fsb.filterStr);
PathBarDestroy(w->fsb.pathBar);
// free strings
STR_FREE(w->fsb.labelListView);
STR_FREE(w->fsb.labelDetailView);
STR_FREE(w->fsb.labelOpenFileTitle);
STR_FREE(w->fsb.labelSaveFileTitle);
XMSTR_FREE(w->fsb.labelDirUp);
XMSTR_FREE(w->fsb.labelHome);
XMSTR_FREE(w->fsb.labelNewFolder);
XMSTR_FREE(w->fsb.labelFilterButton);
XMSTR_FREE(w->fsb.labelShowHiddenFiles);
XMSTR_FREE(w->fsb.labelDirectories);
XMSTR_FREE(w->fsb.labelFiles);
XMSTR_FREE(w->fsb.labelRename);
XMSTR_FREE(w->fsb.labelDelete);
XMSTR_FREE(w->fsb.labelOpen);
XMSTR_FREE(w->fsb.labelSave);
XMSTR_FREE(w->fsb.labelCancel);
XMSTR_FREE(w->fsb.labelHelp);
XMSTR_FREE(w->fsb.labelFileName);
XMSTR_FREE(w->fsb.labelDirectoryName);
XMSTR_FREE(w->fsb.labelNewFileName);
STR_FREE(w->fsb.labelDeleteFile);
STR_FREE(w->fsb.detailHeadings);
STR_FREE(w->fsb.dateFormatSameYear);
STR_FREE(w->fsb.dateFormatOtherYear);
STR_FREE(w->fsb.suffixBytes);
STR_FREE(w->fsb.suffixKB);
STR_FREE(w->fsb.suffixMB);
STR_FREE(w->fsb.suffixGB);
STR_FREE(w->fsb.suffixTB);
STR_FREE(w->fsb.errorTitle);
STR_FREE(w->fsb.errorIllegalChar);
STR_FREE(w->fsb.errorRename);
STR_FREE(w->fsb.errorFolder);
STR_FREE(w->fsb.errorDelete);
STR_FREE(w->fsb.errorOpenDir);
}
static void fsb_resize(Widget widget) {
XnFileSelectionBox w = (XnFileSelectionBox)widget;
(xmFormClassRec.core_class.resize)(widget);
#ifdef FSB_ENABLE_DETAIL
if(w->fsb.view[w->fsb.selectedview].update == FileListDetailUpdate) {
FileListDetailAdjustColWidth(w->fsb.grid);
}
#endif
}
static void fsb_realize(Widget widget, XtValueMask *mask, XSetWindowAttributes *attributes) {
XnFileSelectionBox w = (XnFileSelectionBox)widget;
(xmFormClassRec.core_class.realize)(widget, mask, attributes);
FSBView view = w->fsb.view[w->fsb.selectedview];
XmProcessTraversal(view.focus, XmTRAVERSE_CURRENT);
#ifdef FSB_ENABLE_DETAIL
if(w->fsb.view[w->fsb.selectedview].update == FileListDetailUpdate) {
FileListDetailAdjustColWidth(w->fsb.grid);
}
#endif
}
static void FSBUpdateTitle(Widget w) {
if(XtParent(w)->core.widget_class == xmDialogShellWidgetClass) {
char *title = FSBDialogTitle(w);
XtVaSetValues(XtParent(w), XmNtitle, title, NULL);
}
}
static Boolean fsb_set_values(Widget old, Widget request, Widget neww, ArgList args, Cardinal *num_args) {
Boolean r = False;
XnFileSelectionBox o = (XnFileSelectionBox)old;
XnFileSelectionBox n = (XnFileSelectionBox)neww;
int setOkBtnLabel = 0;
int ismanaged = XtIsManaged(neww);
Dimension width, height;
if(!ismanaged) {
width = n->core.width;
height = n->core.height;
if(n->fsb.pathBar) {
n->fsb.pathBar->disableResize = True;
}
}
if(o->fsb.selectedview != n->fsb.selectedview) {
int selectedview = n->fsb.selectedview;
n->fsb.selectedview = o->fsb.selectedview;
SelectView(n, selectedview);
}
char *updateDir = NULL;
int selectItem = 0;
if(o->fsb.selectedPath != n->fsb.selectedPath) {
STR_FREE(o->fsb.selectedPath);
STRDUP_RES(n->fsb.selectedPath);
XmTextFieldSetString(n->fsb.name, FileName(n->fsb.selectedPath));
// also update current directory
updateDir = ParentPath(n->fsb.selectedPath);
selectItem = 1;
}
if(o->fsb.currentPath != n->fsb.currentPath) {
STR_FREE(o->fsb.currentPath);
updateDir = strdup(n->fsb.currentPath);
n->fsb.currentPath = NULL;
}
if(o->fsb.filterStr != n->fsb.filterStr) {
STR_FREE(o->fsb.filterStr);
STRDUP_RES(n->fsb.filterStr);
XmTextFieldSetString(XmDropDownGetText(n->fsb.filter), n->fsb.filterStr);
if(!updateDir) {
filedialog_update_dir(n, NULL);
}
}
if(updateDir) {
filedialog_update_dir(n, updateDir);
PathBarSetPath(n->fsb.pathBar, updateDir);
free(updateDir);
}
if(o->fsb.type != n->fsb.type) {
if(n->fsb.type == FILEDIALOG_OPEN) {
XtVaSetValues(n->fsb.workarea, XmNbottomWidget, n->fsb.separator, NULL);
XtUnmanageChild(n->fsb.name);
XtUnmanageChild(n->fsb.nameLabel);
} else {
XtManageChild(n->fsb.name);
XtManageChild(n->fsb.nameLabel);
XtVaSetValues(n->fsb.workarea, XmNbottomWidget, n->fsb.nameLabel, NULL);
}
FSBUpdateTitle(neww);
setOkBtnLabel = 1;
}
// label strings
int updateTitle = 0;
if(o->fsb.labelListView != n->fsb.labelListView) {
STR_FREE(o->fsb.labelListView);
STRDUP_RES(n->fsb.labelListView);
XmString label = XmStringCreateLocalized(n->fsb.labelListView);
XtVaSetValues(n->fsb.viewSelectorList, XmNlabelString, label, NULL);
XmStringFree(label);
}
if(o->fsb.labelDetailView != n->fsb.labelDetailView) {
STR_FREE(o->fsb.labelDetailView);
STRDUP_RES(n->fsb.labelDetailView);
XmString label = XmStringCreateLocalized(n->fsb.labelDetailView);
XtVaSetValues(n->fsb.viewSelectorDetail, XmNlabelString, label, NULL);
if(n->fsb.detailToggleButton) {
XtVaSetValues(n->fsb.detailToggleButton, XmNlabelString, label, NULL);
}
XmStringFree(label);
}
if(o->fsb.labelOpenFileTitle != n->fsb.labelOpenFileTitle) {
STR_FREE(o->fsb.labelOpenFileTitle);
STRDUP_RES(n->fsb.labelOpenFileTitle);
updateTitle = 1;
}
if(o->fsb.labelSaveFileTitle != n->fsb.labelSaveFileTitle) {
STR_FREE(o->fsb.labelSaveFileTitle);
STRDUP_RES(n->fsb.labelSaveFileTitle);
updateTitle = 1;
}
if(o->fsb.labelDirUp != n->fsb.labelDirUp) {
XMSTR_FREE(o->fsb.labelDirUp);
XMS_STRDUP_RES(n->fsb.labelDirUp);
XtVaSetValues(n->fsb.dirUp, XmNlabelString, n->fsb.labelDirUp, NULL);
}
if(o->fsb.labelHome != n->fsb.labelHome) {
XMSTR_FREE(o->fsb.labelHome);
XMS_STRDUP_RES(n->fsb.labelHome);
XtVaSetValues(n->fsb.dirUp, XmNlabelString, n->fsb.labelHome, NULL);
}
if(o->fsb.labelNewFolder != n->fsb.labelNewFolder) {
XMSTR_FREE(o->fsb.labelNewFolder);
XMS_STRDUP_RES(n->fsb.labelNewFolder);
XtVaSetValues(n->fsb.newFolder, XmNlabelString, n->fsb.labelNewFolder, NULL);
}
if(o->fsb.labelFilterButton != n->fsb.labelFilterButton) {
XMSTR_FREE(o->fsb.labelFilterButton);
XMS_STRDUP_RES(n->fsb.labelFilterButton);
XtVaSetValues(n->fsb.filterButton, XmNlabelString, n->fsb.labelFilterButton, NULL);
}
if(o->fsb.labelShowHiddenFiles != n->fsb.labelShowHiddenFiles) {
XMSTR_FREE(o->fsb.labelShowHiddenFiles);
XMS_STRDUP_RES(n->fsb.labelShowHiddenFiles);
XtVaSetValues(n->fsb.showHiddenButtonW, XmNlabelString, n->fsb.labelShowHiddenFiles, NULL);
}
if(o->fsb.labelDirectories != n->fsb.labelDirectories) {
XMSTR_FREE(o->fsb.labelDirectories);
XMS_STRDUP_RES(n->fsb.labelDirectories);
XtVaSetValues(n->fsb.lsDirLabel, XmNlabelString, n->fsb.labelDirectories, NULL);
}
if(o->fsb.labelFiles != n->fsb.labelFiles) {
XMSTR_FREE(o->fsb.labelFiles);
XMS_STRDUP_RES(n->fsb.labelFiles);
XtVaSetValues(n->fsb.lsFileLabel, XmNlabelString, n->fsb.labelFiles, NULL);
}
int recreateContextMenu = 0;
if(o->fsb.labelRename != n->fsb.labelRename) {
XMSTR_FREE(o->fsb.labelRename);
XMS_STRDUP_RES(n->fsb.labelRename);
recreateContextMenu = 1;
}
if(o->fsb.labelDelete != n->fsb.labelDelete) {
XMSTR_FREE(o->fsb.labelDelete);
XMS_STRDUP_RES(n->fsb.labelDelete);
recreateContextMenu = 1;
}
if(o->fsb.labelOpen != n->fsb.labelOpen) {
XMSTR_FREE(o->fsb.labelOpen);
XMS_STRDUP_RES(n->fsb.labelOpen);
setOkBtnLabel = 1;
}
if(o->fsb.labelSave != n->fsb.labelSave) {
XMSTR_FREE(o->fsb.labelSave);
XMS_STRDUP_RES(n->fsb.labelSave);
setOkBtnLabel = 1;
}
if(o->fsb.labelCancel != n->fsb.labelCancel) {
XMSTR_FREE(o->fsb.labelCancel);
XMS_STRDUP_RES(n->fsb.labelCancel);
XtVaSetValues(n->fsb.cancelBtn, XmNlabelString, n->fsb.labelCancel, NULL);
}
if(o->fsb.labelHelp != n->fsb.labelHelp) {
XMSTR_FREE(o->fsb.labelHelp);
XMS_STRDUP_RES(n->fsb.labelHelp);
XtVaSetValues(n->fsb.helpBtn, XmNlabelString, n->fsb.labelHelp, NULL);
}
if(o->fsb.labelFileName != n->fsb.labelFileName) {
XMSTR_FREE(o->fsb.labelFileName);
XMS_STRDUP_RES(n->fsb.labelFileName);
XtVaSetValues(n->fsb.nameLabel, XmNlabelString, n->fsb.labelFileName, NULL);
}
if(o->fsb.labelDirectoryName != n->fsb.labelDirectoryName) {
XMSTR_FREE(o->fsb.labelDirectoryName);
XMS_STRDUP_RES(n->fsb.labelDirectoryName);
}
if(o->fsb.labelNewFileName != n->fsb.labelNewFileName) {
XMSTR_FREE(o->fsb.labelNewFileName);
XMS_STRDUP_RES(n->fsb.labelNewFileName);
}
if(o->fsb.labelDeleteFile != n->fsb.labelDeleteFile) {
STR_FREE(o->fsb.labelDeleteFile);
STRDUP_RES(n->fsb.labelDeleteFile);
}
#ifdef FSB_ENABLE_DETAIL
if(o->fsb.detailHeadings != n->fsb.detailHeadings) {
STR_FREE(o->fsb.detailHeadings);
STRDUP_RES(n->fsb.detailHeadings);
XtVaSetValues(n->fsb.grid, XmNsimpleHeadings, n->fsb.detailHeadings, NULL);
}
#endif
if(o->fsb.dateFormatSameYear != n->fsb.dateFormatSameYear) {
STR_FREE(o->fsb.dateFormatSameYear);
STRDUP_RES(n->fsb.dateFormatSameYear);
}
if(o->fsb.dateFormatOtherYear != n->fsb.dateFormatOtherYear) {
STR_FREE(o->fsb.dateFormatOtherYear);
STRDUP_RES(n->fsb.dateFormatOtherYear);
}
if(o->fsb.suffixBytes != n->fsb.suffixBytes) {
STR_FREE(o->fsb.suffixBytes);
STRDUP_RES(n->fsb.suffixBytes);
}
if(o->fsb.suffixMB != n->fsb.suffixMB) {
STR_FREE(o->fsb.suffixMB);
STRDUP_RES(n->fsb.suffixMB);
}
if(o->fsb.suffixGB != n->fsb.suffixGB) {
STR_FREE(o->fsb.suffixGB);
STRDUP_RES(n->fsb.suffixGB);
}
if(o->fsb.suffixTB != n->fsb.suffixTB) {
STR_FREE(o->fsb.suffixTB);
STRDUP_RES(n->fsb.suffixTB);
}
if(o->fsb.errorTitle != n->fsb.errorTitle) {
STR_FREE(o->fsb.errorTitle);
STRDUP_RES(n->fsb.errorTitle);
}
if(o->fsb.errorIllegalChar != n->fsb.errorIllegalChar) {
STR_FREE(o->fsb.errorIllegalChar);
STRDUP_RES(n->fsb.errorIllegalChar);
}
if(o->fsb.errorRename != n->fsb.errorRename) {
STR_FREE(o->fsb.errorRename);
STRDUP_RES(n->fsb.errorRename);
}
if(o->fsb.errorFolder != n->fsb.errorFolder) {
STR_FREE(o->fsb.errorFolder);
STRDUP_RES(n->fsb.errorFolder);
}
if(o->fsb.errorDelete != n->fsb.errorDelete) {
STR_FREE(o->fsb.errorDelete);
STRDUP_RES(n->fsb.errorDelete);
}
if(o->fsb.errorOpenDir != n->fsb.errorOpenDir) {
STR_FREE(o->fsb.errorOpenDir);
STRDUP_RES(n->fsb.errorOpenDir);
}
if(updateTitle) {
FSBUpdateTitle(neww);
}
if(recreateContextMenu) {
XtDestroyWidget(n->fsb.listContextMenu);
XtDestroyWidget(n->fsb.gridContextMenu);
n->fsb.listContextMenu = CreateContextMenu(n, n->fsb.filelist, FileContextMenuCB);
n->fsb.gridContextMenu = CreateContextMenu(n, n->fsb.grid, FileContextMenuCB);
}
if(setOkBtnLabel) {
XtVaSetValues(n->fsb.okBtn, XmNlabelString, n->fsb.type == FILEDIALOG_OPEN ? n->fsb.labelOpen : n->fsb.labelSave, NULL);
}
if(!ismanaged && !n->fsb.disable_set_values) {
n->fsb.disable_set_values = 1;
XtVaSetValues(neww, XmNwidth, width, XmNheight, height, NULL);
n->fsb.disable_set_values = 0;
if(n->fsb.pathBar)
n->fsb.pathBar->disableResize = False;
}
if(selectItem) {
if(ismanaged) {
FSBSelectItem(n, FileName(n->fsb.selectedPath));
}
}
Boolean fr = (xmFormClassRec.core_class.set_values)(old, request, neww, args, num_args);
return fr ? fr : r;
}
static void fsb_insert_child(Widget child) {
XnFileSelectionBox p = (XnFileSelectionBox)XtParent(child);
(xmFormClassRec.composite_class.insert_child)(child);
if(!p->fsb.gui_created) {
return;
}
// custom child widget insert
XtVaSetValues(child,
XmNbottomAttachment, XmATTACH_WIDGET,
XmNbottomWidget, p->fsb.bottom_widget,
XmNbottomOffset, p->fsb.widgetSpacing,
XmNleftAttachment, XmATTACH_FORM,
XmNleftOffset, p->fsb.windowSpacing,
XmNrightAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNrightOffset, p->fsb.windowSpacing,
NULL);
XtVaSetValues(p->fsb.listform,
XmNbottomWidget, child,
XmNbottomOffset, 0,
NULL);
p->fsb.workarea = child;
}
Boolean fsb_acceptfocus(Widget widget, Time *time) {
return 0;
}
static void fsb_mapcb(Widget widget, XtPointer u, XtPointer cb) {
XnFileSelectionBox w = (XnFileSelectionBox)widget;
pathbar_resize(w->fsb.pathBar->widget, w->fsb.pathBar, NULL);
if(w->fsb.type == FILEDIALOG_OPEN) {
FSBView view = w->fsb.view[w->fsb.selectedview];
XmProcessTraversal(view.focus, XmTRAVERSE_CURRENT);
} else {
XmProcessTraversal(w->fsb.name, XmTRAVERSE_CURRENT);
}
if(w->fsb.selectedPath) {
FSBSelectItem(w, FileName(w->fsb.selectedPath));
}
}
static void FocusInAP(Widget w, XEvent *event, String *args, Cardinal *nArgs) {
}
static int apply_filter(XnFileSelectionBox w, const char *pattern, const char *string) {
if(!pattern) return 0;
FSBFilterFunc func = w->fsb.filterFunc ? w->fsb.filterFunc : FSBGlobFilter;
return func(pattern, string);
}
static int FSBGlobFilter(const char *a, const char *b) {
return fnmatch(a, b, 0);
}
static void errCB(Widget w, XtPointer d, XtPointer cbs) {
XtDestroyWidget(w);
}
static void ErrDialog(XnFileSelectionBox w, const char *title, const char *errmsg) {
Arg args[16];
int n = 0;
XmString titleStr = XmStringCreateLocalized((char*)title);
XmString msg = XmStringCreateLocalized((char*)errmsg);
XtSetArg(args[n], XmNdialogTitle, titleStr); n++;
XtSetArg(args[n], XmNselectionLabelString, msg); n++;
XtSetArg(args[n], XmNokLabelString, w->fsb.labelOk); n++;
XtSetArg(args[n], XmNcancelLabelString, w->fsb.labelCancel); n++;
Widget dialog = XmCreatePromptDialog ((Widget)w, "NewFolderPrompt", args, n);
Widget help = XmSelectionBoxGetChild(dialog, XmDIALOG_HELP_BUTTON);
XtUnmanageChild(help);
Widget cancel = XmSelectionBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON);
XtUnmanageChild(cancel);
Widget text = XmSelectionBoxGetChild(dialog, XmDIALOG_TEXT);
XtUnmanageChild(text);
XtAddCallback(dialog, XmNokCallback, errCB, NULL);
XtManageChild(dialog);
XmStringFree(titleStr);
XmStringFree(msg);
}
static void rename_file_cb(Widget w, const char *path, XmSelectionBoxCallbackStruct *cb) {
XnFileSelectionBox fsb = NULL;
XtVaGetValues(w, XmNuserData, &fsb, NULL);
char *fileName = NULL;
XmStringGetLtoR(cb->value, XmSTRING_DEFAULT_CHARSET, &fileName);
// make sure the new file name doesn't contain a path separator
if(strchr(fileName, '/')) {
ErrDialog(fsb, fsb->fsb.errorTitle, fsb->fsb.errorIllegalChar);
XtFree(fileName);
return;
}
char *parentPath = ParentPath(path);
char *newPath = ConcatPath(parentPath, fileName);
if(rename(path, newPath)) {
char errmsg[256];
snprintf(errmsg, 256, fsb->fsb.errorRename, strerror(errno));
ErrDialog(fsb, fsb->fsb.errorTitle, errmsg);
} else {
filedialog_update_dir(fsb, parentPath);
}
free(parentPath);
free(newPath);
XtFree(fileName);
XtDestroyWidget(XtParent(w));
}
static void selectionbox_cancel(Widget w, XtPointer data, XtPointer d) {
XtDestroyWidget(XtParent(w));
}
static void FSBRename(XnFileSelectionBox fsb, const char *path) {
Arg args[16];
int n = 0;
Widget w = (Widget)fsb;
char *name = FileName((char*)path);
XmString filename = XmStringCreateLocalized(name);
XtSetArg(args[n], XmNselectionLabelString,fsb->fsb.labelNewFileName); n++;
XtSetArg(args[n], XmNtextString, filename); n++;
XtSetArg(args[n], XmNuserData, fsb); n++;
XtSetArg(args[n], XmNdialogTitle, fsb->fsb.labelRename); n++;
XtSetArg(args[n], XmNokLabelString, fsb->fsb.labelOk); n++;
XtSetArg(args[n], XmNcancelLabelString, fsb->fsb.labelCancel); n++;
Widget dialog = XmCreatePromptDialog (w, "RenameFilePrompt", args, n);
Widget help = XmSelectionBoxGetChild(dialog, XmDIALOG_HELP_BUTTON);
XtUnmanageChild(help);
XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)rename_file_cb, (char*)path);
XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)selectionbox_cancel, NULL);
XmStringFree(filename);
XtManageChild(dialog);
}
static void delete_file_cb(Widget w, const char *path, XmSelectionBoxCallbackStruct *cb) {
XnFileSelectionBox fsb = NULL;
XtVaGetValues(w, XmNuserData, &fsb, NULL);
if(unlink(path)) {
char errmsg[256];
snprintf(errmsg, 256, fsb->fsb.errorDelete, strerror(errno));
ErrDialog(fsb, fsb->fsb.errorTitle, errmsg);
} else {
char *parentPath = ParentPath(path);
filedialog_update_dir(fsb, parentPath);
free(parentPath);
}
XtDestroyWidget(XtParent(w));
}
static void FSBDelete(XnFileSelectionBox fsb, const char *path) {
Arg args[16];
int n = 0;
Widget w = (Widget)fsb;
char *name = FileName((char*)path);
size_t len = strlen(name);
size_t msglen = len + strlen(fsb->fsb.labelDeleteFile) + 4;
char *msg = malloc(msglen);
snprintf(msg, msglen, fsb->fsb.labelDeleteFile, name);
XmString prompt = XmStringCreateLocalized(msg);
XtSetArg(args[n], XmNselectionLabelString, prompt); n++;
XtSetArg(args[n], XmNuserData, fsb); n++;
XtSetArg(args[n], XmNdialogTitle, fsb->fsb.labelDelete); n++;
XtSetArg(args[n], XmNokLabelString, fsb->fsb.labelOk); n++;
XtSetArg(args[n], XmNcancelLabelString, fsb->fsb.labelCancel); n++;
Widget dialog = XmCreatePromptDialog (w, "DeleteFilePrompt", args, n);
Widget help = XmSelectionBoxGetChild(dialog, XmDIALOG_HELP_BUTTON);
XtUnmanageChild(help);
Widget text = XmSelectionBoxGetChild(dialog, XmDIALOG_TEXT);
XtUnmanageChild(text);
XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)delete_file_cb, (char*)path);
XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)selectionbox_cancel, NULL);
free(msg);
XmStringFree(prompt);
XtManageChild(dialog);
}
static void FSBSelectItem(XnFileSelectionBox fsb, const char *item) {
FSBView view = fsb->fsb.view[fsb->fsb.selectedview];
if(view.select) {
view.select((Widget)fsb, view.widget, item);
}
}
static char* set_selected_path(XnFileSelectionBox data, XmString item)
{
char *name = NULL;
XmStringGetLtoR(item, XmFONTLIST_DEFAULT_TAG, &name);
if(!name) {
return NULL;
}
char *path = ConcatPath(data->fsb.currentPath, name);
XtFree(name);
if(data->fsb.selectedPath) {
free(data->fsb.selectedPath);
}
data->fsb.selectedPath = path;
return path;
}
// item0: rename
// item1: delete
static void FileContextMenuCB(Widget item, XtPointer index, XtPointer cd) {
intptr_t i = (intptr_t)index;
Widget parent = XtParent(item);
XnFileSelectionBox fsb = NULL;
XtVaGetValues(parent, XmNuserData, &fsb, NULL);
const char *path = fsb->fsb.selectedPath;
if(path) {
if(i == 0) {
FSBRename(fsb, path);
} else if(i == 1) {
FSBDelete(fsb, path);
}
}
}
static Widget CreateContextMenu(XnFileSelectionBox fsb, Widget parent, XtCallbackProc callback) {
return XmVaCreateSimplePopupMenu(
parent, "popup", callback, XmNpopupEnabled, XmPOPUP_AUTOMATIC,
XmNuserData, fsb,
XmVaPUSHBUTTON, fsb->fsb.labelRename, 'R', NULL, NULL,
XmVaPUSHBUTTON, fsb->fsb.labelDelete, 'D', NULL, NULL,
NULL);
}
static void FileListUpdate(Widget fsb, Widget view, FileElm *dirs, int dircount, FileElm *files, int filecount, const char *filter, int maxnamelen, void *userData) {
XnFileSelectionBox data = userData;
FileListWidgetAdd(data, data->fsb.filelist, data->fsb.showHidden, filter, files, filecount);
}
static void FileListSelect(Widget fsb, Widget view, const char *item) {
XnFileSelectionBox w = (XnFileSelectionBox)fsb;
int numItems = 0;