forked from x42/sofd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibsofd.c
2412 lines (2177 loc) · 65.1 KB
/
libsofd.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
/* libSOFD - Simple Open File Dialog [for X11 without toolkit]
*
* Copyright (C) 2014 Robin Gareus <[email protected]>
*
* 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.
*/
/* Test and example:
* gcc -Wall -D SOFD_TEST -g -o sofd libsofd.c -lX11
*
* public API documentation and example code at the bottom of this file
*
* This small lib may one day include openGL rendering and
* wayland window support, but not today. Today we celebrate
* 30 years of X11.
*/
#ifdef SOFD_TEST
#define HAVE_X11
#include "libsofd.h"
#endif
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
// shared 'recently used' implementation
// sadly, xbel does not qualify as simple.
// hence we use a simple format alike the
// gtk-bookmark list (one file per line)
#define MAX_RECENT_ENTRIES 24
#define MAX_RECENT_AGE (15552000) // 180 days (in sec)
typedef struct {
char path[1024];
time_t atime;
} FibRecentFile;
static FibRecentFile *_recentlist = NULL;
static unsigned int _recentcnt = 0;
static uint8_t _recentlock = 0;
static int fib_isxdigit (const char x) {
if (
(x >= '0' && x <= '9')
||
(x >= 'a' && x <= 'f')
||
(x >= 'A' && x <= 'F')
) return 1;
return 0;
}
static void decode_3986 (char *str) {
int len = strlen (str);
int idx = 0;
while (idx + 2 < len) {
char *in = &str[idx];
if (('%' == *in) && fib_isxdigit (in[1]) && fib_isxdigit (in[2])) {
char hexstr[3];
hexstr[0] = in[1];
hexstr[1] = in[2];
hexstr[2] = 0;
long hex = strtol (hexstr, NULL, 16);
*in = hex;
memmove (&str[idx+1], &str[idx + 3], len - idx - 2);
len -= 2;
}
++idx;
}
}
static char *encode_3986 (const char *str) {
size_t alloc, newlen;
char *ns = NULL;
unsigned char in;
size_t i = 0;
size_t length;
if (!str) return strdup ("");
alloc = strlen (str) + 1;
newlen = alloc;
ns = (char*) malloc (alloc);
length = alloc;
while (--length) {
in = *str;
switch (in) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
case '_': case '~': case '.': case '-':
case '/': case ',': // XXX not in RFC3986
ns[i++] = in;
break;
default:
newlen += 2; /* this'll become a %XX */
if (newlen > alloc) {
alloc *= 2;
ns = (char*) realloc (ns, alloc);
}
snprintf (&ns[i], 4, "%%%02X", in);
i += 3;
break;
}
++str;
}
ns[i] = 0;
return ns;
}
void x_fib_free_recent () {
free (_recentlist);
_recentlist = NULL;
_recentcnt = 0;
}
static int cmp_recent (const void *p1, const void *p2) {
FibRecentFile *a = (FibRecentFile*) p1;
FibRecentFile *b = (FibRecentFile*) p2;
if (a->atime == b->atime) return 0;
return a->atime < b->atime;
}
int x_fib_add_recent (const char *path, time_t atime) {
unsigned int i;
struct stat fs;
if (_recentlock) { return -1; }
if (access (path, R_OK)) {
return -1;
}
if (stat (path, &fs)) {
return -1;
}
if (!S_ISREG (fs.st_mode)) {
return -1;
}
if (atime == 0) atime = time (NULL);
if (MAX_RECENT_AGE > 0 && atime + MAX_RECENT_AGE < time (NULL)) {
return -1;
}
for (i = 0; i < _recentcnt; ++i) {
if (!strcmp (_recentlist[i].path, path)) {
if (_recentlist[i].atime < atime) {
_recentlist[i].atime = atime;
}
qsort (_recentlist, _recentcnt, sizeof(FibRecentFile), cmp_recent);
return _recentcnt;
}
}
_recentlist = (FibRecentFile*)realloc (_recentlist, (_recentcnt + 1) * sizeof(FibRecentFile));
_recentlist[_recentcnt].atime = atime;
strcpy (_recentlist[_recentcnt].path, path);
qsort (_recentlist, _recentcnt + 1, sizeof(FibRecentFile), cmp_recent);
if (_recentcnt >= MAX_RECENT_ENTRIES) {
return (_recentcnt);
}
return (++_recentcnt);
}
#ifdef PATHSEP
#undef PATHSEP
#endif
#ifdef PLATFORM_WINDOWS
#define DIRSEP '\\'
#else
#define DIRSEP '/'
#endif
static void mkpath(const char *dir) {
char tmp[1024];
char *p;
size_t len;
snprintf (tmp, sizeof(tmp), "%s", dir);
len = strlen(tmp);
if (tmp[len - 1] == '/')
tmp[len - 1] = 0;
for (p = tmp + 1; *p; ++p)
if(*p == DIRSEP) {
*p = 0;
#ifdef PLATFORM_WINDOWS
mkdir(tmp);
#else
mkdir(tmp, 0755);
#endif
*p = DIRSEP;
}
#ifdef PLATFORM_WINDOWS
mkdir(tmp);
#else
mkdir(tmp, 0755);
#endif
}
int x_fib_save_recent (const char *fn) {
if (_recentlock) { return -1; }
if (!fn) { return -1; }
if (_recentcnt < 1 || !_recentlist) { return -1; }
unsigned int i;
char *dn = strdup (fn);
mkpath (dirname (dn));
free (dn);
FILE *rf = fopen (fn, "w");
if (!rf) return -1;
qsort (_recentlist, _recentcnt, sizeof(FibRecentFile), cmp_recent);
for (i = 0; i < _recentcnt; ++i) {
char *n = encode_3986 (_recentlist[i].path);
fprintf (rf, "%s %lu\n", n, _recentlist[i].atime);
free (n);
}
fclose (rf);
return 0;
}
int x_fib_load_recent (const char *fn) {
char tmp[1024];
if (_recentlock) { return -1; }
if (!fn) { return -1; }
x_fib_free_recent ();
if (access (fn, R_OK)) {
return -1;
}
FILE *rf = fopen (fn, "r");
if (!rf) return -1;
while (fgets (tmp, sizeof(tmp), rf)
&& strlen (tmp) > 1
&& strlen (tmp) < sizeof(tmp))
{
char *s;
tmp[strlen (tmp) - 1] = '\0'; // strip newline
if (!(s = strchr (tmp, ' '))) { // find name <> atime sep
continue;
}
*s = '\0';
time_t t = atol (++s);
decode_3986 (tmp);
x_fib_add_recent (tmp, t);
}
fclose (rf);
return 0;
}
unsigned int x_fib_recent_count () {
return _recentcnt;
}
const char *x_fib_recent_at (unsigned int i) {
if (i >= _recentcnt)
return NULL;
return _recentlist[i].path;
}
#ifdef PLATFORM_WINDOWS
#define PATHSEP "\\"
#else
#define PATHSEP "/"
#endif
const char *x_fib_recent_file(const char *appname) {
static char recent_file[1024];
assert(!strchr(appname, '/'));
const char *xdg = getenv("XDG_DATA_HOME");
if (xdg && (strlen(xdg) + strlen(appname) + 10) < sizeof(recent_file)) {
sprintf(recent_file, "%s" PATHSEP "%s" PATHSEP "recent", xdg, appname);
return recent_file;
}
#ifdef PLATFORM_WINDOWS
const char * homedrive = getenv("HOMEDRIVE");
const char * homepath = getenv("HOMEPATH");
if (homedrive && homepath && (strlen(homedrive) + strlen(homepath) + strlen(appname) + 29) < PATH_MAX) {
sprintf(recent_file, "%s%s" PATHSEP "Application Data" PATHSEP "%s" PATHSEP "recent.txt", homedrive, homepath, appname);
return recent_file;
}
#elif defined PLATFORM_OSX
const char *home = getenv("HOME");
if (home && (strlen(home) + strlen(appname) + 29) < sizeof(recent_file)) {
sprintf(recent_file, "%s/Library/Preferences/%s/recent", home, appname);
return recent_file;
}
#else
const char *home = getenv("HOME");
if (home && (strlen(home) + strlen(appname) + 22) < sizeof(recent_file)) {
sprintf(recent_file, "%s/.local/share/%s/recent", home, appname);
return recent_file;
}
#endif
return NULL;
}
#ifdef HAVE_X11
#include <mntent.h>
#include <dirent.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/Xos.h>
#ifndef MIN
#define MIN(A,B) ( (A) < (B) ? (A) : (B) )
#endif
#ifndef MAX
#define MAX(A,B) ( (A) < (B) ? (B) : (A) )
#endif
static Window _fib_win = 0;
static GC _fib_gc = 0;
static XColor _c_gray0, _c_gray1, _c_gray2, _c_gray3, _c_gray4, _c_gray5, _c_gray6;
static Font _fibfont = 0;
static Pixmap _pixbuffer = None;
static int _fib_width = 100;
static int _fib_height = 100;
static int _btn_w = 0;
static int _btn_span = 0;
static int _fib_font_height = 0;
static int _fib_dir_indent = 0;
static int _fib_spc_norm = 0;
static int _fib_font_ascent = 0;
static int _fib_font_vsep = 0;
static int _fib_font_size_width = 0;
static int _fib_font_time_width = 0;
static int _fib_place_width = 0;
static int _scrl_f = 0;
static int _scrl_y0 = -1;
static int _scrl_y1 = -1;
static int _scrl_my = -1;
static int _scrl_mf = -1;
static int _view_p = -1;
static int _fsel = -1;
static int _hov_b = -1;
static int _hov_f = -1;
static int _hov_p = -1;
static int _hov_h = -1;
static int _hov_l = -1;
static int _hov_s = -1;
static int _sort = 0;
static int _columns = 0;
static int _fib_filter_fn = 1;
static int _fib_hidden_fn = 0;
static int _fib_show_places = 0;
static uint8_t _fib_mapped = 0;
static uint8_t _fib_resized = 0;
static unsigned long _dblclk = 0;
static int _status = -2;
static char _rv_open[1024] = "";
static char _fib_cfg_custom_places[1024] = "";
static char _fib_cfg_custom_font[256] = "";
static char _fib_cfg_title[128] = "xjadeo - Open Video File";
typedef struct {
char name[256];
int x0;
int xw;
} FibPathButton;
typedef struct {
char name[256];
char strtime[32];
char strsize[32];
int ssizew;
off_t size;
time_t mtime;
uint8_t flags; // 2: selected, 4: isdir 8: recent-entry
FibRecentFile *rfp;
} FibFileEntry;
typedef struct {
char text[24];
uint8_t flags; // 2: selected, 4: toggle, 8 disable
int x0;
int tw;
int xw;
void (*callback)(Display*);
} FibButton;
typedef struct {
char name[256];
char path[1024];
uint8_t flags; // 1: hover, 2: selected, 4:add sep
} FibPlace;
static char _cur_path[1024] = "";
static FibFileEntry *_dirlist = NULL;
static FibPathButton *_pathbtn = NULL;
static FibPlace *_placelist = NULL;
static int _dircount = 0;
static int _pathparts = 0;
static int _placecnt = 0;
static FibButton _btn_ok;
static FibButton _btn_cancel;
static FibButton _btn_filter;
static FibButton _btn_places;
static FibButton _btn_hidden;
static FibButton *_btns[] = {&_btn_places, &_btn_filter, &_btn_hidden, &_btn_cancel, &_btn_ok};
static int (*_fib_filter_function)(const char *filename);
/* hardcoded layout */
#define DSEP 6 // px; horiz space beween elements, also l+r margin for file-list
#define PSEP 4 // px; horiz space beween paths
#define FILECOLUMN (17 * _fib_dir_indent) //px; min width of file-column
#define LISTTOP 2.7 //em; top of the file-browser list
#define LISTBOT 4.75 //em; bottom of the file-browers list
#define BTNBTMMARGIN 0.75 //em; height/margin of the button row
#define BTNPADDING 2 // px - only used for open/cancel buttons
#define SCROLLBARW (3 + (_fib_spc_norm&~1)) //px; - should be SCROLLBARW = (N * 2 + 3)
#define SCROLLBOXH 10 //px; arrow box top+bottom
#define PLACESW _fib_place_width //px;
#define PLACESWMAX (15 *_fib_spc_norm) //px;
#define PATHBTNTOP _fib_font_vsep //px; offset by (_fib_font_ascent);
#define FAREAMRGB 3 //px; base L+R margin
#define FAREAMRGR (FAREAMRGB + 1) //px; right margin of file-area + 1 (line width)
#define FAREAMRGL (_fib_show_places ? PLACESW + FAREAMRGB : FAREAMRGB) //px; left margin of file-area
#define TEXTSEP 4 //px;
#define FAREATEXTL (FAREAMRGL + TEXTSEP) //px; filename text-left FAREAMRGL + TEXTSEP
#define SORTBTNOFF -10 //px;
#ifndef DBLCLKTME
#define DBLCLKTME 200 //msec; double click time
#endif
#define DRAW_OUTLINE
#define DOUBLE_BUFFER
static int query_font_geometry (Display *dpy, GC gc, const char *txt, int *w, int *h, int *a, int *d) {
XCharStruct text_structure;
int font_direction, font_ascent, font_descent;
XFontStruct *fontinfo = XQueryFont (dpy, XGContextFromGC (gc));
if (!fontinfo) { return -1; }
XTextExtents (fontinfo, txt, strlen (txt), &font_direction, &font_ascent, &font_descent, &text_structure);
if (w) *w = XTextWidth (fontinfo, txt, strlen (txt));
if (h) *h = text_structure.ascent + text_structure.descent;
if (a) *a = text_structure.ascent;
if (d) *d = text_structure.descent;
XFreeFontInfo (NULL, fontinfo, 1);
return 0;
}
static void VDrawRectangle (Display *dpy, Drawable d, GC gc, int x, int y, unsigned int w, unsigned int h) {
const unsigned long blackColor = BlackPixel (dpy, DefaultScreen (dpy));
#ifdef DRAW_OUTLINE
XSetForeground (dpy, gc, _c_gray5.pixel);
XDrawLine (dpy, d, gc, x + 1, y + h, x + w, y + h);
XDrawLine (dpy, d, gc, x + w, y + 1, x + w, y + h);
XSetForeground (dpy, gc, blackColor);
XDrawLine (dpy, d, gc, x + 1, y, x + w, y);
XDrawLine (dpy, d, gc, x, y + 1, x, y + h);
#else
XSetForeground (dpy, _fib_gc, blackColor);
XDrawRectangle (dpy, d, gc, x, y, w, h);
#endif
}
static void fib_expose (Display *dpy, Window realwin) {
int i;
XID win;
const unsigned long whiteColor = WhitePixel (dpy, DefaultScreen (dpy));
const unsigned long blackColor = BlackPixel (dpy, DefaultScreen (dpy));
if (!_fib_mapped) return;
if (_fib_resized
#ifdef DOUBLE_BUFFER
|| !_pixbuffer
#endif
)
{
#ifdef DOUBLE_BUFFER
unsigned int w = 0, h = 0;
if (_pixbuffer != None) {
Window ignored_w;
int ignored_i;
unsigned int ignored_u;
XGetGeometry(dpy, _pixbuffer, &ignored_w, &ignored_i, &ignored_i, &w, &h, &ignored_u, &ignored_u);
if (_fib_width != (int)w || _fib_height != (int)h) {
XFreePixmap (dpy, _pixbuffer);
_pixbuffer = None;
}
}
if (_pixbuffer == None) {
XWindowAttributes wa;
XGetWindowAttributes (dpy, realwin, &wa);
_pixbuffer = XCreatePixmap (dpy, realwin, _fib_width, _fib_height, wa.depth);
}
#endif
if (_pixbuffer != None) {
XSetForeground (dpy, _fib_gc, _c_gray1.pixel);
XFillRectangle (dpy, _pixbuffer, _fib_gc, 0, 0, _fib_width, _fib_height);
} else {
XSetForeground (dpy, _fib_gc, _c_gray1.pixel);
XFillRectangle (dpy, realwin, _fib_gc, 0, 0, _fib_width, _fib_height);
}
_fib_resized = 0;
}
if (_pixbuffer == None) {
win = realwin;
} else {
win = _pixbuffer;
}
// Top Row: dirs and up navigation
int ppw = 0;
int ppx = FAREAMRGB;
for (i = _pathparts - 1; i >= 0; --i) {
ppw += _pathbtn[i].xw + PSEP;
if (ppw >= _fib_width - PSEP - _pathbtn[0].xw - FAREAMRGB) break; // XXX, first change is from "/" to "<", NOOP
}
++i;
// border-less "<" parent/up, IFF space is limited
if (i > 0) {
if (0 == _hov_p || (_hov_p > 0 && _hov_p < _pathparts - 1)) {
XSetForeground (dpy, _fib_gc, _c_gray4.pixel);
} else {
XSetForeground (dpy, _fib_gc, blackColor);
}
XDrawString (dpy, win, _fib_gc, ppx, PATHBTNTOP, "<", 1);
ppx += _pathbtn[0].xw + PSEP;
if (i == _pathparts) --i;
}
_view_p = i;
while (i < _pathparts) {
if (i == _hov_p) {
XSetForeground (dpy, _fib_gc, _c_gray0.pixel);
} else {
XSetForeground (dpy, _fib_gc, _c_gray2.pixel);
}
XFillRectangle (dpy, win, _fib_gc,
ppx + 1, PATHBTNTOP - _fib_font_ascent,
_pathbtn[i].xw - 1, _fib_font_height);
VDrawRectangle (dpy, win, _fib_gc,
ppx, PATHBTNTOP - _fib_font_ascent,
_pathbtn[i].xw, _fib_font_height);
XDrawString (dpy, win, _fib_gc, ppx + 1 + BTNPADDING, PATHBTNTOP,
_pathbtn[i].name, strlen (_pathbtn[i].name));
_pathbtn[i].x0 = ppx; // current position
ppx += _pathbtn[i].xw + PSEP;
++i;
}
// middle, scroll list of file names
const int ltop = LISTTOP * _fib_font_vsep;
const int llen = (_fib_height - LISTBOT * _fib_font_vsep) / _fib_font_vsep;
const int fsel_height = 4 + llen * _fib_font_vsep;
const int fsel_width = _fib_width - FAREAMRGL - FAREAMRGR - (llen < _dircount ? SCROLLBARW : 0);
const int t_x = FAREATEXTL;
int t_s = FAREATEXTL + fsel_width;
int t_t = FAREATEXTL + fsel_width;
// check which colums can be visible
// depending on available width of window.
_columns = 0;
if (fsel_width > FILECOLUMN + _fib_font_size_width + _fib_font_time_width) {
_columns |= 2;
t_s = FAREAMRGL + fsel_width - _fib_font_time_width - TEXTSEP;
}
if (fsel_width > FILECOLUMN + _fib_font_size_width) {
_columns |= 1;
t_t = t_s - _fib_font_size_width - TEXTSEP;
}
int fstop = _scrl_f; // first entry in scroll position
const int ttop = ltop - _fib_font_height + _fib_font_ascent;
if (fstop > 0 && fstop + llen > _dircount) {
fstop = MAX (0, _dircount - llen);
_scrl_f = fstop;
}
// list header
XSetForeground (dpy, _fib_gc, _c_gray3.pixel);
XFillRectangle (dpy, win, _fib_gc, FAREAMRGL, ltop - _fib_font_vsep, fsel_width, _fib_font_vsep);
// draw background of file list
XSetForeground (dpy, _fib_gc, _c_gray2.pixel);
XFillRectangle (dpy, win, _fib_gc, FAREAMRGL, ltop, fsel_width, fsel_height);
#ifdef DRAW_OUTLINE
VDrawRectangle (dpy, win, _fib_gc, FAREAMRGL, ltop - _fib_font_vsep -1, _fib_width - FAREAMRGL - FAREAMRGR, fsel_height + _fib_font_vsep + 1);
#endif
switch (_hov_h) {
case 1:
XSetForeground (dpy, _fib_gc, _c_gray0.pixel);
XFillRectangle (dpy, win, _fib_gc, t_x + _fib_dir_indent - TEXTSEP + 1, ltop - _fib_font_vsep, t_t - t_x - _fib_dir_indent - 1, _fib_font_vsep);
break;
case 2:
XSetForeground (dpy, _fib_gc, _c_gray0.pixel);
XFillRectangle (dpy, win, _fib_gc, t_t - TEXTSEP + 1, ltop - _fib_font_vsep, _fib_font_size_width + TEXTSEP - 1, _fib_font_vsep);
break;
case 3:
XSetForeground (dpy, _fib_gc, _c_gray0.pixel);
XFillRectangle (dpy, win, _fib_gc, t_s - TEXTSEP + 1, ltop - _fib_font_vsep, TEXTSEP + TEXTSEP + _fib_font_time_width - 1, _fib_font_vsep);
break;
default:
break;
}
// column headings and sort order
int arp = MAX (2, _fib_font_height / 5); // arrow scale
const int trioff = _fib_font_height - _fib_font_ascent - arp + 1;
XPoint ptri[4] = { {0, ttop - trioff }, {arp, -arp - arp - 1}, {-arp - arp, 0}, {arp, arp + arp + 1}};
if (_sort & 1) {
ptri[0].y = ttop -arp - arp - 1;
ptri[1].y *= -1;
ptri[3].y *= -1;
}
switch (_sort) {
case 0:
case 1:
ptri[0].x = t_t + SORTBTNOFF + 2 - arp;
XSetForeground (dpy, _fib_gc, _c_gray6.pixel);
XFillPolygon (dpy, win, _fib_gc, ptri, 3, Convex, CoordModePrevious);
XDrawLines (dpy, win, _fib_gc, ptri, 4, CoordModePrevious);
break;
case 2:
case 3:
if (_columns & 1) {
ptri[0].x = t_s + SORTBTNOFF + 2 - arp;
XSetForeground (dpy, _fib_gc, _c_gray6.pixel);
XFillPolygon (dpy, win, _fib_gc, ptri, 3, Convex, CoordModePrevious);
XDrawLines (dpy, win, _fib_gc, ptri, 4, CoordModePrevious);
}
break;
case 4:
case 5:
if (_columns & 2) {
ptri[0].x = FAREATEXTL + fsel_width + SORTBTNOFF + 2 - arp;
XSetForeground (dpy, _fib_gc, _c_gray6.pixel);
XFillPolygon (dpy, win, _fib_gc, ptri, 3, Convex, CoordModePrevious);
XDrawLines (dpy, win, _fib_gc, ptri, 4, CoordModePrevious);
}
break;
}
#if 0 // bottom header bottom border
XSetForeground (dpy, _fib_gc, _c_gray5.pixel);
XSetLineAttributes (dpy, _fib_gc, 1, LineOnOffDash, CapButt, JoinMiter);
XDrawLine (dpy, win, _fib_gc,
FAREAMRGL + 1, ltop,
FAREAMRGL + fsel_width, ltop);
XSetLineAttributes (dpy, _fib_gc, 1, LineSolid, CapButt, JoinMiter);
#endif
XSetForeground (dpy, _fib_gc, _c_gray4.pixel);
XDrawLine (dpy, win, _fib_gc,
t_x + _fib_dir_indent - TEXTSEP, ltop - _fib_font_vsep + 3,
t_x + _fib_dir_indent - TEXTSEP, ltop - 3);
XSetForeground (dpy, _fib_gc, blackColor);
XDrawString (dpy, win, _fib_gc, t_x + _fib_dir_indent, ttop, "Name", 4);
if (_columns & 1) {
XSetForeground (dpy, _fib_gc, _c_gray4.pixel);
XDrawLine (dpy, win, _fib_gc,
t_t - TEXTSEP, ltop - _fib_font_vsep + 3,
t_t - TEXTSEP, ltop - 3);
XSetForeground (dpy, _fib_gc, blackColor);
XDrawString (dpy, win, _fib_gc, t_t, ttop, "Size", 4);
}
if (_columns & 2) {
XSetForeground (dpy, _fib_gc, _c_gray4.pixel);
XDrawLine (dpy, win, _fib_gc,
t_s - TEXTSEP, ltop - _fib_font_vsep + 3,
t_s - TEXTSEP, ltop - 3);
XSetForeground (dpy, _fib_gc, blackColor);
if (_pathparts > 0)
XDrawString (dpy, win, _fib_gc, t_s, ttop, "Last Modified", 13);
else
XDrawString (dpy, win, _fib_gc, t_s, ttop, "Last Used", 9);
}
// scrollbar sep
if (llen < _dircount) {
const int sx0 = _fib_width - SCROLLBARW - FAREAMRGR;
XSetForeground (dpy, _fib_gc, _c_gray4.pixel);
XDrawLine (dpy, win, _fib_gc,
sx0 - 1, ltop - _fib_font_vsep,
#ifdef DRAW_OUTLINE
sx0 - 1, ltop + fsel_height
#else
sx0 - 1, ltop - 1
#endif
);
}
// clip area for file-name
XRectangle clp = {FAREAMRGL + 1, ltop, t_t - FAREAMRGL - TEXTSEP - TEXTSEP - 1, fsel_height};
// list files in view
for (i = 0; i < llen; ++i) {
const int j = i + fstop;
if (j >= _dircount) break;
const int t_y = ltop + (i+1) * _fib_font_vsep - 4;
XSetForeground (dpy, _fib_gc, blackColor);
if (_dirlist[j].flags & 2) {
XSetForeground (dpy, _fib_gc, blackColor);
XFillRectangle (dpy, win, _fib_gc,
FAREAMRGL, t_y - _fib_font_ascent, fsel_width, _fib_font_height);
XSetForeground (dpy, _fib_gc, whiteColor);
}
if (_hov_f == j && !(_dirlist[j].flags & 2)) {
XSetForeground (dpy, _fib_gc, _c_gray4.pixel);
}
if (_dirlist[j].flags & 4) {
XDrawString (dpy, win, _fib_gc, t_x, t_y, "D", 1);
}
XSetClipRectangles (dpy, _fib_gc, 0, 0, &clp, 1, Unsorted);
XDrawString (dpy, win, _fib_gc,
t_x + _fib_dir_indent, t_y,
_dirlist[j].name, strlen (_dirlist[j].name));
XSetClipMask (dpy, _fib_gc, None);
if (_columns & 1) // right-aligned 'size'
XDrawString (dpy, win, _fib_gc,
t_s - TEXTSEP - 2 - _dirlist[j].ssizew, t_y,
_dirlist[j].strsize, strlen (_dirlist[j].strsize));
if (_columns & 2)
XDrawString (dpy, win, _fib_gc,
t_s, t_y,
_dirlist[j].strtime, strlen (_dirlist[j].strtime));
}
// scrollbar
if (llen < _dircount) {
float sl = (fsel_height + _fib_font_vsep - (SCROLLBOXH + SCROLLBOXH)) / (float) _dircount;
sl = MAX ((8. / llen), sl); // 8px min height of scroller
const int sy1 = llen * sl;
const float mx = (fsel_height + _fib_font_vsep - (SCROLLBOXH + SCROLLBOXH) - sy1) / (float)(_dircount - llen);
const int sy0 = fstop * mx;
const int sx0 = _fib_width - SCROLLBARW - FAREAMRGR;
const int stop = ltop - _fib_font_vsep;
_scrl_y0 = stop + SCROLLBOXH + sy0;
_scrl_y1 = _scrl_y0 + sy1;
assert (fstop + llen <= _dircount);
// scroll-bar background
XSetForeground (dpy, _fib_gc, _c_gray3.pixel);
XFillRectangle (dpy, win, _fib_gc, sx0, stop, SCROLLBARW, fsel_height + _fib_font_vsep);
// scroller
if (_hov_s == 0) {
XSetForeground (dpy, _fib_gc, _c_gray0.pixel);
} else {
XSetForeground (dpy, _fib_gc, _c_gray1.pixel);
}
XFillRectangle (dpy, win, _fib_gc, sx0 + 1, stop + SCROLLBOXH + sy0, SCROLLBARW - 2, sy1);
int scrw = (SCROLLBARW -3) / 2;
// arrows top and bottom
if (_hov_s == 1) {
XSetForeground (dpy, _fib_gc, _c_gray0.pixel);
} else {
XSetForeground (dpy, _fib_gc, _c_gray1.pixel);
}
XPoint ptst[4] = { {sx0 + 1, stop + 8}, {scrw, -7}, {scrw, 7}, {-2 * scrw, 0}};
XFillPolygon (dpy, win, _fib_gc, ptst, 3, Convex, CoordModePrevious);
XDrawLines (dpy, win, _fib_gc, ptst, 4, CoordModePrevious);
if (_hov_s == 2) {
XSetForeground (dpy, _fib_gc, _c_gray0.pixel);
} else {
XSetForeground (dpy, _fib_gc, _c_gray1.pixel);
}
XPoint ptsb[4] = { {sx0 + 1, ltop + fsel_height - 9}, {2*scrw, 0}, {-scrw, 7}, {-scrw, -7}};
XFillPolygon (dpy, win, _fib_gc, ptsb, 3, Convex, CoordModePrevious);
XDrawLines (dpy, win, _fib_gc, ptsb, 4, CoordModePrevious);
} else {
_scrl_y0 = _scrl_y1 = -1;
}
if (_fib_show_places) {
assert (_placecnt > 0);
// heading
XSetForeground (dpy, _fib_gc, _c_gray3.pixel);
XFillRectangle (dpy, win, _fib_gc, FAREAMRGB, ltop - _fib_font_vsep, PLACESW - TEXTSEP, _fib_font_vsep);
// body
XSetForeground (dpy, _fib_gc, _c_gray2.pixel);
XFillRectangle (dpy, win, _fib_gc, FAREAMRGB, ltop, PLACESW - TEXTSEP, fsel_height);
#ifdef DRAW_OUTLINE
VDrawRectangle (dpy, win, _fib_gc, FAREAMRGB, ltop - _fib_font_vsep -1, PLACESW - TEXTSEP, fsel_height + _fib_font_vsep + 1);
#endif
XSetForeground (dpy, _fib_gc, blackColor);
XDrawString (dpy, win, _fib_gc, FAREAMRGB + TEXTSEP, ttop, "Places", 6);
XRectangle pclip = {FAREAMRGB + 1, ltop, PLACESW - TEXTSEP -1, fsel_height};
XSetClipRectangles (dpy, _fib_gc, 0, 0, &pclip, 1, Unsorted);
const int plx = FAREAMRGB + TEXTSEP;
for (i = 0; i < llen && i < _placecnt; ++i) {
const int ply = ltop + (i+1) * _fib_font_vsep - 4;
if (i == _hov_l) {
XSetForeground (dpy, _fib_gc, _c_gray4.pixel);
} else {
XSetForeground (dpy, _fib_gc, blackColor);
}
XDrawString (dpy, win, _fib_gc,
plx, ply,
_placelist[i].name, strlen (_placelist[i].name));
if (_placelist[i].flags & 4) {
XSetForeground (dpy, _fib_gc, _c_gray3.pixel);
const int plly = ply - _fib_font_ascent + _fib_font_height;
const int pllx0 = FAREAMRGB;
const int pllx1 = FAREAMRGB + (PLACESW - TEXTSEP);
XDrawLine (dpy, win, _fib_gc, pllx0, plly, pllx1, plly);
}
}
XSetClipMask (dpy, _fib_gc, None);
if (_placecnt > llen) {
const int plly = ltop + fsel_height - _fib_font_height + _fib_font_ascent;
const int pllx0 = FAREAMRGB + (PLACESW - TEXTSEP) * .75;
const int pllx1 = FAREAMRGB + (PLACESW - TEXTSEP - TEXTSEP);
XSetForeground (dpy, _fib_gc, blackColor);
XSetLineAttributes (dpy, _fib_gc, 1, LineOnOffDash, CapButt, JoinMiter);
XDrawLine (dpy, win, _fib_gc, pllx0, plly, pllx1, plly);
XSetLineAttributes (dpy, _fib_gc, 1, LineSolid, CapButt, JoinMiter);
}
}
// Bottom Buttons
const int numb = sizeof(_btns) / sizeof(FibButton*);
int xtra = _fib_width - _btn_span;
const int cbox = _fib_font_ascent - 2;
const int bbase = _fib_height - BTNBTMMARGIN * _fib_font_vsep - BTNPADDING;
const int cblw = cbox > 20 ? 5 : ( cbox > 9 ? 3 : 1);
int bx = FAREAMRGB;
for (i = 0; i < numb; ++i) {
if (_btns[i]->flags & 8) { continue; }
if (_btns[i]->flags & 4) {
// checkbutton
const int cby0 = bbase - cbox + 1 + BTNPADDING;
if (i == _hov_b) {
XSetForeground (dpy, _fib_gc, _c_gray4.pixel);
} else {
XSetForeground (dpy, _fib_gc, blackColor);
}
XDrawRectangle (dpy, win, _fib_gc,
bx, cby0 - 1, cbox + 1, cbox + 1);
if (i == _hov_b) {
XSetForeground (dpy, _fib_gc, _c_gray5.pixel);
} else {
XSetForeground (dpy, _fib_gc, blackColor);
}
XDrawString (dpy, win, _fib_gc, BTNPADDING + bx + _fib_font_ascent, 1 + bbase + BTNPADDING,
_btns[i]->text, strlen (_btns[i]->text));
if (i == _hov_b) {
XSetForeground (dpy, _fib_gc, _c_gray0.pixel);
} else {
if (_btns[i]->flags & 2) {
XSetForeground (dpy, _fib_gc, _c_gray1.pixel);
} else {
XSetForeground (dpy, _fib_gc, _c_gray2.pixel);
}
}
XFillRectangle (dpy, win, _fib_gc,
bx+1, cby0, cbox, cbox);
if (_btns[i]->flags & 2) {
XSetLineAttributes (dpy, _fib_gc, cblw, LineSolid, CapRound, JoinMiter);
XSetForeground (dpy, _fib_gc, _c_gray6.pixel);
XDrawLine (dpy, win, _fib_gc,
bx + 2, cby0 + 1,
bx + cbox - 1, cby0 + cbox - 2);
XDrawLine (dpy, win, _fib_gc,
bx + cbox - 1, cby0 + 1,
bx + 2, cby0 + cbox - 2);
XSetLineAttributes (dpy, _fib_gc, 1, LineSolid, CapButt, JoinMiter);
}
} else {
if (xtra > 0) {
bx += xtra;
xtra = 0;
}
// pushbutton
uint8_t can_hover = 1; // special case
if (_btns[i] == &_btn_ok) {
if (_fsel < 0 || _fsel >= _dircount) {
can_hover = 0;
}
}
if (can_hover && i == _hov_b) {
XSetForeground (dpy, _fib_gc, _c_gray0.pixel);
} else {
XSetForeground (dpy, _fib_gc, _c_gray2.pixel);
}
XFillRectangle (dpy, win, _fib_gc,
bx + 1, bbase - _fib_font_ascent,
_btn_w - 1, _fib_font_height + BTNPADDING + BTNPADDING);
VDrawRectangle (dpy, win, _fib_gc,
bx, bbase - _fib_font_ascent,
_btn_w, _fib_font_height + BTNPADDING + BTNPADDING);
XDrawString (dpy, win, _fib_gc, bx + (_btn_w - _btns[i]->tw) * .5, 1 + bbase + BTNPADDING,
_btns[i]->text, strlen (_btns[i]->text));
}
_btns[i]->x0 = bx;
bx += _btns[i]->xw + DSEP;
}
if (_pixbuffer != None) {
XCopyArea(dpy, _pixbuffer, realwin, _fib_gc, 0, 0, _fib_width, _fib_height, 0, 0);
}
XFlush (dpy);
}
static void fib_reset () {
_hov_p = _hov_f = _hov_h = _hov_l = -1;
_scrl_f = 0;
_fib_resized = 1;
}
static int cmp_n_up (const void *p1, const void *p2) {
FibFileEntry *a = (FibFileEntry*) p1;
FibFileEntry *b = (FibFileEntry*) p2;
if ((a->flags & 4) && !(b->flags & 4)) return -1;
if (!(a->flags & 4) && (b->flags & 4)) return 1;
return strcmp (a->name, b->name);
}
static int cmp_n_down (const void *p1, const void *p2) {
FibFileEntry *a = (FibFileEntry*) p1;
FibFileEntry *b = (FibFileEntry*) p2;
if ((a->flags & 4) && !(b->flags & 4)) return -1;
if (!(a->flags & 4) && (b->flags & 4)) return 1;
return strcmp (b->name, a->name);
}
static int cmp_t_up (const void *p1, const void *p2) {
FibFileEntry *a = (FibFileEntry*) p1;
FibFileEntry *b = (FibFileEntry*) p2;