-
Notifications
You must be signed in to change notification settings - Fork 4
/
widget.c
3755 lines (3515 loc) · 88.6 KB
/
widget.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
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <poll.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/cursorfont.h>
#include <X11/xpm.h>
#include <X11/Xcursor/Xcursor.h>
#include <X11/extensions/Xrender.h>
#include <X11/extensions/shape.h>
#include <control/selection.h>
#include <control/dragndrop.h>
#include <control/font.h>
#include "icons.h"
#include "util.h"
#include "widget.h"
#define ATOMS \
X(TEXT, NULL) \
X(TEXT_URI_LIST, "text/uri-list") \
X(UTF8_STRING, NULL) \
X(WM_PROTOCOLS, NULL) \
X(WM_DELETE_WINDOW, NULL) \
X(_NET_WM_ICON, NULL) \
X(_NET_WM_NAME, NULL) \
X(_NET_WM_PID, NULL) \
X(_NET_WM_WINDOW_TYPE, NULL) \
X(_NET_WM_WINDOW_TYPE_DND, NULL) \
X(_NET_WM_WINDOW_TYPE_NORMAL, NULL) \
X(_NET_WM_WINDOW_OPACITY, NULL) \
X(_XEMBED, NULL) \
X(_CONTROL_STATUS, NULL) \
X(_CONTROL_CWD, NULL) \
X(_CONTROL_GOTO, NULL)
#define RESOURCES \
/* CLASS NAME */ \
X(GEOMETRY, "Geometry", "geometry") \
X(FACE_NAME, "FaceName", "faceName") \
X(FACE_SIZE, "FaceSize", "faceSize") \
X(ICONS, "FileIcons", "fileIcons") \
X(NORMAL_BG, "Background", "background") \
X(NORMAL_FG, "Foreground", "foreground") \
X(SELECT_BG, "ActiveBackground", "activeBackground") \
X(SELECT_FG, "ActiveForeground", "activeForeground") \
X(STATUSBAR, "StatusBarEnable", "statusBarEnable") \
X(BARSTATUS, "EnableStatusBar", "enableStatusBar") \
X(OPACITY, "Opacity", "opacity")
#define DEF_COLOR_BG (XRenderColor){ .red = 0x0000, .green = 0x0000, .blue = 0x0000, .alpha = 0xFFFF }
#define DEF_COLOR_FG (XRenderColor){ .red = 0xFFFF, .green = 0xFFFF, .blue = 0xFFFF, .alpha = 0xFFFF }
#define DEF_COLOR_SELBG (XRenderColor){ .red = 0x3400, .green = 0x6500, .blue = 0xA400, .alpha = 0xFFFF }
#define DEF_COLOR_SELFG (XRenderColor){ .red = 0xFFFF, .green = 0xFFFF, .blue = 0xFFFF, .alpha = 0xFFFF }
#define DEF_SIZE (XRectangle){ .x = 0, .y = 0, .width = 600 , .height = 460 }
#define DEF_OPACITY 0xFFFF
#define DEF_STATUSBAR True
#define FREE(x) do{free(x); x = NULL;}while(0)
#define UNKNOWN_STATUS "<\?\?\?>"
/* ellipsis has two dots rather than three; the third comes from the extension */
#define ELLIPSIS ".."
/* opacity of drag-and-drop mini window */
#define DND_OPACITY 0x7FFFFFFF
/* opacity of rectangular selection */
#define SEL_OPACITY 0xC000
#define RECT_OPACITY 0x4000
/* constants to check a .ppm file */
#define PPM_HEADER "P6\n"
#define PPM_COLOR "255\n"
/* how much to scroll on PageUp/PageDown (half the window height) */
#define PAGE_STEP(w) ((w)->h / 2)
/* window capacity (max number of rows that can fit on the window) */
#define WIN_ROWS(w) ((w)->h / (w)->itemh)
/* number of WHOLE rows that the current directory has */
#define WHL_ROWS(w) ((w)->nitems / (w)->ncols)
/* 0 if all rows are entirely filled; 1 if there's an extra, half-filled row */
#define MOD_ROWS(w) (((w)->nitems % (w)->ncols) != 0 ? 1 : 0)
/* actual number of rows (either whole or not) that the current directory has */
#define ALL_ROWS(w) (WHL_ROWS(w) + MOD_ROWS(w))
#define STATUSBAR_HEIGHT(w) ((w)->fonth * 2)
#define STATUSBAR_MARGIN(w) ((w)->fonth / 2)
enum {
XEMBED_EMBEDDED_NOTIFY,
XEMBED_WINDOW_ACTIVATE,
XEMBED_WINDOW_DEACTIVATE,
XEMBED_REQUEST_FOCUS,
XEMBED_FOCUS_IN,
XEMBED_FOCUS_OUT,
XEMBED_FOCUS_NEXT,
XEMBED_FOCUS_PREV,
XEMBED_GRAB_KEY,
XEMBED_UNGRAB_KEY,
XEMBED_MODALITY_ON,
XEMBED_MODALITY_OFF,
XEMBED_REGISTER_ACCELERATOR,
XEMBED_UNREGISTER_ACCELERATOR,
XEMBED_ACTIVATE_ACCELERATOR,
};
enum {
XEMBED_FOCUS_CURRENT,
XEMBED_FOCUS_FIRST,
XEMBED_FOCUS_LAST,
};
enum {
/* size of border of rectangular selection */
RECT_BORDER = 1,
/* distance the cursor must move to be considered a drag */
DRAG_THRESHOLD = 8,
DRAG_SQUARE = (DRAG_THRESHOLD * DRAG_THRESHOLD),
/* buttons not defined by X.h */
BUTTON8 = 8,
BUTTON9 = 9,
/* one byte was 8 bits in size last time I checked */
BYTE = 8,
/* color depths */
CLIP_DEPTH = 1, /* 0/1 */
PPM_DEPTH = 3, /* RGB */
DATA_DEPTH = 4, /* BGRA */
/* sizes of string buffers */
KSYM_BUFSIZE = 64, /* key symbol name buffer size */
/* hardcoded object sizes in pixels */
/* there's no ITEM_HEIGHT for it is computed at runtime from font height */
THUMBSIZE = 64, /* maximum thumbnail size */
ICON_MARGIN = (THUMBSIZE / 2), /* margin around item icon */
ITEM_WIDTH = (THUMBSIZE * 2), /* width of an item (icon + margins) */
MARGIN = 16, /* top margin above first row */
/* constants for parsing .ppm files */
PPM_HEADER_SIZE = (sizeof(PPM_HEADER) - 1),
PPM_COLOR_SIZE = (sizeof(PPM_COLOR) - 1),
PPM_BUFSIZE = 8,
/* draw up to NLINES lines of label; each one up to LABELWIDTH pixels long */
NLINES = 2, /* change it for more lines below icons */
LABELWIDTH = ITEM_WIDTH - 16, /* save 8 pixels each side around label */
/* times in milliseconds */
DOUBLECLICK = 250, /* time of a doubleclick, in milliseconds */
MOTION_TIME = 32, /* update time rate for rectangular selection */
SCROLL_TIME = 128,
/* scrolling */
SCROLL_STEP = 32, /* pixels per scroll */
SCROLLER_SIZE = 32, /* size of the scroller */
SCROLLER_MIN = 16, /* min lines to scroll for the scroller to change */
HANDLE_MAX_SIZE = (SCROLLER_SIZE - 4), /* max size of the scroller handle */
};
enum {
SELECT_NOT,
SELECT_YES,
SELECT_LAST,
};
enum {
TARGET_STRING,
TARGET_UTF8,
TARGET_URI,
TARGET_LAST
};
enum {
COLOR_BG,
COLOR_FG,
COLOR_LAST,
};
enum Layer {
LAYER_CANVAS,
LAYER_ICONS,
LAYER_SELALPHA,
LAYER_RECTALPHA,
LAYER_SCROLLER,
LAYER_STATUSBAR,
LAYER_LAST,
};
enum Atom {
#define X(atom, name) atom,
ATOMS
NATOMS
#undef X
};
enum Resource {
#define X(res, class, name) res,
RESOURCES
NRESOURCES
#undef X
};
struct Icon {
Pixmap pix, mask;
};
struct Thumb {
struct Thumb *next;
int w, h;
XImage *img;
};
struct Selection {
struct Selection *prev, *next;
int index;
};
struct Widget {
Bool start, isset, error;
int redraw;
/* X11 stuff */
Display *display;
Atom atoms[NATOMS];
GC gc;
Cursor busycursor;
Window window, root, child;
struct {
XRenderColor chans;
Pixmap pix;
Picture pict;
} colors[SELECT_LAST][COLOR_LAST];
XRenderPictFormat *format, *alpha_format;
Visual *visual;
Colormap colormap;
int fd;
int screen;
unsigned int depth;
unsigned short opacity;
CtrlFontSet *fontset;
struct {
XrmClass class;
XrmName name;
} application, resources[NRESOURCES];
const char **cliresources;
char *gototext;
char ksymbuf[KSYM_BUFSIZE]; /* buffer where the keysym passed to xfilesctl is held */
struct {
Pixmap pix;
Picture pict;
} layers[LAYER_LAST];
Pixmap namepix; /* temporary pixmap for the labels */
Pixmap namepict;
struct clipboard {
unsigned char *buf;
size_t size;
FILE *stream;
Bool filled;
} plainclip, uriclip; /* streams for X Selection content */
/*
* Lock used for synchronizing the thumbnail and the main threads.
*/
pthread_mutex_t lock;
/*
* Items to be displayed
*/
Item *items;
int nitems; /* number of items */
int *linelen; /* for each item, the lengths of its largest label line */
int *nlines; /* for each item, the number of label lines */
/*
* Items can be selected with the mouse and the Control and Shift modifiers.
*
* We keep track of selections in a list of selections, which is
* essentially a doubly-linked list of indices. It's kept as a
* doubly-linked list for easily adding and removing any
* element.
*
* We also maintain an array of pointers to selections, so we
* can easily access a selection in the list, and remove it for
* example, given the index of an item.
*/
struct Selection *sel; /* list of selections */
struct Selection *rectsel; /* list of selections by rectsel */
struct Selection **issel; /* array of pointers to Selections */
Time seltime;
/*
* We keep track of thumbnails in a list of thumbnails, which is
* essentially a singly-linked list of XImages. It's kept as a
* singly-linked list just so we can traverse them one-by-one at
* the end for freeing them.
*
* We also maintain an array of pointers to thumbnails, so we
* can easily access a thumbnail in the list, given the index
* of an item.
*/
struct Thumb *thumbhead;
struct Thumb **thumbs;
/*
* Geometry of the window and its contents.
*
* WARNING:
* - .nrows is the number of rows in the pixmap, which is
* approximately the number of rows visible in the window
* (that is, those that are not hidden by scrolling) plus 2.
* - .ncols is also the number of rows in the pixmap, which is
* exactly the number of columns visible in the window.
*
* I call "screenful" what is being visible at a given time on
* the window.
*/
int w, h; /* icon area size */
int winw, winh; /* window size */
int pixw, pixh; /* pixmap size */
int itemw, itemh; /* size of a item (margin + icon + label) */
int ydiff; /* how much the pixmap is scrolled up */
int ncols, nrows; /* number of columns and rows visible at a time */
int nscreens; /* maximum number of screenfuls we can scroll */
int row; /* index of first row visible in the current screenful */
int fonth; /* font height */
int x0; /* position of first column after the left margin */
int ellipsisw; /* width of the ellipsis we draw on long labels */
/*
* We use icons for items that do not have a thumbnail.
*/
struct Icon *icons; /* array of icons set by the user */
int nicons;
/* Strings used to build the title bar. */
const char *title;
const char *class;
/*
* Index of highlighted item (usually the last item clicked by
* the user); or -1 if none.
*/
int highlight;
/*
* The scroller how this code calls the widget that replaces the
* scrollbar. It is a little pop-up window that appears after a
* middle-click. It can be either controlled as a scrollbar, by
* dragging its inner manipulable object (called the "handler"),
* or by moving the pointer up and down the scroller itself.
*
* The scroller is based on a Firefox's hidden feature called
* autoScroll.
*
* TIP: To enable this feature in Firefox, set the option
* "general.autoScroll" to True in about:config.
*/
Window scroller; /* the scroller popup window */
int handlew; /* size of scroller handle */
/*
* Statusbar describing highlighted item.
*/
Bool status_enable;
};
struct Options {
const char *class;
const char *name;
int argc;
char **argv;
};
static int
error_handler(Display *display, XErrorEvent *error)
{
char msg[128], req[128], num[8];
if (error->error_code == BadWindow)
return 0;
(void)XGetErrorText(display, error->error_code, msg, sizeof(msg));
(void)snprintf(num, sizeof(num), "%d", error->request_code);
(void)XGetErrorDatabaseText(
display, "XRequest", num,
"unknown request", req, sizeof(req)
);
errx(EXIT_FAILURE, "xlib: %s: %s", req, msg);
return 0; /* unreachable */
}
static char *
getitemstatus(Widget *widget, int index)
{
if (index < 0 || index >= widget->nitems)
return UNKNOWN_STATUS;
if (widget->items[widget->highlight].status == NULL)
return UNKNOWN_STATUS;
return widget->items[widget->highlight].status;
}
static void
resetlayer(Widget *widget, enum Layer layer, int width, int height)
{
Bool isalpha = (layer == LAYER_SELALPHA || layer == LAYER_RECTALPHA);
if (widget->layers[layer].pix != None)
XFreePixmap(widget->display, widget->layers[layer].pix);
if (widget->layers[layer].pict != None)
XRenderFreePicture(widget->display, widget->layers[layer].pict);
widget->layers[layer].pix = XCreatePixmap(
widget->display,
widget->window,
width,
height,
isalpha ? 8 : widget->depth
);
widget->layers[layer].pict = XRenderCreatePicture(
widget->display,
widget->layers[layer].pix,
isalpha ? widget->alpha_format : widget->format,
0,
NULL
);
XRenderFillRectangle(
widget->display,
PictOpClear,
widget->layers[layer].pict,
&(XRenderColor){ 0 },
0, 0, widget->w, widget->h
);
}
static void
setfont(Widget *widget, const char *facename, double fontsize)
{
CtrlFontSet *fontset;
if (facename == NULL)
facename = "xft:";
fontset = ctrlfnt_open(
widget->display,
widget->screen,
widget->visual,
widget->colormap,
facename,
fontsize
);
if (fontset == NULL)
return;
if (widget->fontset != NULL)
ctrlfnt_free(widget->fontset);
widget->fontset = fontset;
widget->fonth = ctrlfnt_height(widget->fontset);
widget->itemh = THUMBSIZE + (NLINES + 1) * widget->fonth;
widget->ellipsisw = ctrlfnt_width(widget->fontset, ELLIPSIS, strlen(ELLIPSIS));
if (widget->namepix != None)
XFreePixmap(widget->display, widget->namepix);
if (widget->namepict != None)
XRenderFreePicture(widget->display, widget->namepict);
widget->namepix = XCreatePixmap(
widget->display,
widget->window,
LABELWIDTH,
widget->fonth,
widget->depth
);
widget->namepict = XRenderCreatePicture(
widget->display,
widget->namepix,
widget->format,
0,
NULL
);
}
static void
setcolor(Widget *widget, int scheme, int colornum, const char *colorname)
{
XColor color;
if (scheme >= SELECT_LAST || colornum >= COLOR_LAST || colorname == NULL)
return;
if (!XParseColor(widget->display, widget->colormap, colorname, &color)) {
warnx("%s: unknown color name", colorname);
return;
}
widget->colors[scheme][colornum].chans = (XRenderColor){
.red = FLAG(color.flags, DoRed) ? color.red : 0x0000,
.green = FLAG(color.flags, DoGreen) ? color.green : 0x0000,
.blue = FLAG(color.flags, DoBlue) ? color.blue : 0x0000,
.alpha = 0xFFFF,
};
XRenderFillRectangle(
widget->display,
PictOpSrc,
widget->colors[scheme][colornum].pict,
&widget->colors[scheme][colornum].chans,
0, 0, 1, 1
);
}
static void
setopacity(Widget *widget, const char *value)
{
char *endp;
double d;
if (value == NULL)
return;
d = strtod(value, &endp);
if (endp == value || *endp != '\0' || d < 0.0 || d > 1.0) {
warnx("%s: invalid opacity value", value);
return;
}
widget->opacity = d * 0xFFFF;
}
static char *
getresource(XrmDatabase xdb, XrmClass appclass, XrmName appname, XrmClass resclass, XrmName resname)
{
XrmQuark name[] = { appname, resname, NULLQUARK };
XrmQuark class[] = { appclass, resclass, NULLQUARK };
XrmRepresentation tmp;
XrmValue xval;
if (XrmQGetResource(xdb, name, class, &tmp, &xval))
return xval.addr;
return NULL;
}
static XrmDatabase
loadxdb(Widget *widget, const char *str)
{
XrmDatabase xdb, tmp;
int i;
if ((xdb = XrmGetStringDatabase(str)) == NULL)
return NULL;
for (i = 0; widget->cliresources[i] != NULL; i++) {
tmp = XrmGetStringDatabase(widget->cliresources[i]);
XrmMergeDatabases(tmp, &xdb);
}
return xdb;
}
static void
drawstatusbar(Widget *widget)
{
size_t statuslen, scrolllen;
int countwid, statuswid, scrollwid, rightwid;
char *status;
char countstr[64]; /* enough for writing number of files */
char scrollstr[8]; /* enough for writing the percentage */
int scrollpct;
if (!widget->status_enable)
return;
etlock(&widget->lock);
widget->redraw = True;
/* clear previous content */
XRenderFillRectangle(
widget->display,
PictOpClear,
widget->layers[LAYER_STATUSBAR].pict,
&(XRenderColor){ 0 },
0, 0, widget->winw, STATUSBAR_HEIGHT(widget)
);
/* draw item counter */
(void)snprintf(
countstr,
LEN(countstr),
"[%d/%d]",
widget->highlight > 0 ? widget->highlight : 0,
widget->nitems - 1 /* -1 because the first item ".." is not counted */
);
countwid = ctrlfnt_draw(
widget->fontset,
widget->layers[LAYER_STATUSBAR].pict,
widget->colors[SELECT_NOT][COLOR_FG].pict,
(XRectangle){
.x = STATUSBAR_MARGIN(widget),
.y = STATUSBAR_MARGIN(widget),
.width = widget->w,
.height = widget->fonth,
},
countstr,
strlen(countstr)
);
countwid += STATUSBAR_MARGIN(widget);
/* draw name of highlighted item */
if (widget->highlight > 0) {
ctrlfnt_draw(
widget->fontset,
widget->layers[LAYER_STATUSBAR].pict,
widget->colors[SELECT_NOT][COLOR_FG].pict,
(XRectangle){
.x = STATUSBAR_MARGIN(widget) + countwid,
.y = STATUSBAR_MARGIN(widget),
.width = widget->w,
.height = widget->fonth,
},
widget->items[widget->highlight].name,
strlen(widget->items[widget->highlight].name)
);
}
/* get percentage */
scrollpct = 100 * ((double)(widget->row + 1) / widget->nscreens);
scrollpct = min(scrollpct, 100);
(void)snprintf(scrollstr, LEN(scrollstr), "[%d%%]", scrollpct);
scrolllen = strlen(scrollstr);
scrollwid = ctrlfnt_width(widget->fontset, scrollstr, scrolllen);
rightwid = STATUSBAR_MARGIN(widget) * 2 + scrollwid;
/* get metadata */
if (widget->highlight > 0) {
status = getitemstatus(widget, widget->highlight);
statuslen = strlen(status);
statuswid = ctrlfnt_width(widget->fontset, status, statuslen);
rightwid += STATUSBAR_MARGIN(widget) + statuswid;
}
/* clear content below right side of statusbar */
XRenderFillRectangle(
widget->display,
PictOpClear,
widget->layers[LAYER_STATUSBAR].pict,
&(XRenderColor){ 0 },
widget->winw - rightwid, 0,
rightwid,
STATUSBAR_HEIGHT(widget)
);
/* draw percentage */
ctrlfnt_draw(
widget->fontset,
widget->layers[LAYER_STATUSBAR].pict,
widget->colors[SELECT_NOT][COLOR_FG].pict,
(XRectangle){
.x = widget->winw - scrollwid - STATUSBAR_MARGIN(widget),
.y = STATUSBAR_MARGIN(widget),
.width = scrollwid,
.height = widget->fonth,
},
scrollstr,
scrolllen
);
/* draw metadata for highlighted item */
if (widget->highlight > 0) {
ctrlfnt_draw(
widget->fontset,
widget->layers[LAYER_STATUSBAR].pict,
widget->colors[SELECT_NOT][COLOR_FG].pict,
(XRectangle){
.x = widget->winw - rightwid + STATUSBAR_MARGIN(widget),
.y = STATUSBAR_MARGIN(widget),
.width = statuswid,
.height = widget->fonth,
},
status,
statuslen
);
}
etunlock(&widget->lock);
}
static void
loadresources(Widget *widget, const char *str)
{
XrmDatabase xdb;
char *value;
enum Resource resource;
char *endp;
char *fontname = NULL;
double d;
double fontsize = 0.0;
Bool changefont = False;
if (str == NULL)
return;
if ((xdb = loadxdb(widget, str)) == NULL)
return;
for (resource = 0; resource < NRESOURCES; resource++) {
value = getresource(
xdb,
widget->application.class,
widget->application.name,
widget->resources[resource].class,
widget->resources[resource].name
);
if (value == NULL)
continue;
switch (resource) {
case FACE_NAME:
fontname = value;
changefont = True;
break;
case FACE_SIZE:
d = strtod(value, &endp);
if (value[0] != '\0' && *endp == '\0' && d > 0.0 && d <= 100.0) {
fontsize = d;
changefont = True;
}
break;
case NORMAL_BG:
setcolor(widget, SELECT_NOT, COLOR_BG, value);
break;
case NORMAL_FG:
setcolor(widget, SELECT_NOT, COLOR_FG, value);
break;
case SELECT_BG:
setcolor(widget, SELECT_YES, COLOR_BG, value);
break;
case SELECT_FG:
setcolor(widget, SELECT_YES, COLOR_FG, value);
break;
case OPACITY:
setopacity(widget, value);
break;
case STATUSBAR:
case BARSTATUS:
widget->status_enable = strcasecmp(value, "on") == 0
|| strcasecmp(value, "true") == 0
|| strcmp(value, "1") == 0;
break;
default:
break;
}
}
if (changefont)
setfont(widget, fontname, fontsize);
XrmDestroyDatabase(xdb);
}
static void
embed_resize(Widget *widget)
{
if (widget->child == None)
return;
XMoveResizeWindow(
widget->display,
widget->child,
0, 0,
widget->winw,
widget->winh
);
XSendEvent(
widget->display, widget->child, False, StructureNotifyMask,
&(XEvent){ .xconfigure = {
.type = ConfigureNotify,
.display = widget->display,
.event = widget->child,
.window = widget->child,
.x = 0,
.y = 0,
.width = widget->winw,
.height = widget->winh,
.border_width = 0,
.above = None,
.override_redirect = False,
}}
);
XSync(widget->display, False);
}
static int
calcsize(Widget *widget, int w, int h)
{
int ncols, nrows, ret;
double d;
ret = False;
if (widget->winw == w && widget->winh == h)
return False;
widget->redraw = True;
etlock(&widget->lock);
ncols = widget->ncols;
nrows = widget->nrows;
if (w > 0 && h > 0) {
widget->winw = w;
widget->winh = h;
widget->ydiff = 0;
}
if (widget->status_enable)
widget->h = max(widget->winh - STATUSBAR_HEIGHT(widget), widget->itemh);
else
widget->h = widget->winh;
widget->w = widget->winw;
widget->ncols = max(widget->w / widget->itemw, 1);
widget->nrows = max(WIN_ROWS(widget) + (widget->h % widget->itemh ? 2 : 1), 1);
widget->x0 = max((widget->w - widget->ncols * widget->itemw) / 2, 0);
widget->nscreens = ALL_ROWS(widget) - WIN_ROWS(widget) + 1;
widget->nscreens = max(widget->nscreens, 1);
d = (double)widget->nscreens / SCROLLER_MIN;
d = (d < 1.0 ? 1.0 : d);
widget->handlew = max(SCROLLER_SIZE / d - 2, 1);
widget->handlew = min(widget->handlew, HANDLE_MAX_SIZE);
if (widget->handlew == HANDLE_MAX_SIZE && ALL_ROWS(widget) > WIN_ROWS(widget))
widget->handlew = HANDLE_MAX_SIZE - 1;
if (ncols != widget->ncols || nrows != widget->nrows) {
widget->pixw = widget->ncols * widget->itemw;
widget->pixh = widget->nrows * widget->itemh;
resetlayer(widget, LAYER_ICONS, widget->pixw, widget->pixh);
resetlayer(widget, LAYER_SELALPHA, widget->pixw, widget->pixh);
ret = True;
}
resetlayer(widget, LAYER_RECTALPHA, widget->w, widget->h);
resetlayer(widget, LAYER_STATUSBAR, widget->winw, STATUSBAR_HEIGHT(widget));
resetlayer(widget, LAYER_CANVAS, widget->winw, widget->winh);
embed_resize(widget);
etunlock(&widget->lock);
return ret;
}
static int
isbreakable(char c)
{
return c == '.' || c == '-' || c == '_';
}
static void
drawname(Widget *widget, Picture color, int x, const char *text, int len)
{
XRenderFillRectangle(
widget->display,
PictOpClear,
widget->namepict,
&(XRenderColor){ 0 },
0, 0, LABELWIDTH, widget->fonth
);
ctrlfnt_draw(
widget->fontset,
widget->namepict,
color,
(XRectangle){
.x = x,
.y = 0,
.width = LABELWIDTH,
.height = widget->fonth,
},
text,
len
);
}
static void
setrow(Widget *widget, int row)
{
etlock(&widget->lock);
widget->row = row;
etunlock(&widget->lock);
}
static struct Icon *
geticon(Widget *widget, int index)
{
return &widget->icons[widget->items[index].icon];
}
static void
drawicon(Widget *widget, int index, int x, int y)
{
struct Icon *icon;
Pixmap pix, mask;
int xorigin;
icon = geticon(widget, index);
pix = icon->pix;
mask = icon->mask;
if (widget->thumbs != NULL && widget->thumbs[index] != NULL) {
/* draw thumbnail */
XPutImage(
widget->display,
widget->layers[LAYER_ICONS].pix,
widget->gc,
widget->thumbs[index]->img,
0, 0,
x + (widget->itemw - widget->thumbs[index]->w) / 2,
y + (THUMBSIZE - widget->thumbs[index]->h) / 2,
widget->thumbs[index]->w,
widget->thumbs[index]->h
);
} else {
/* draw icon */
xorigin = x + (widget->itemw - THUMBSIZE) / 2;
XChangeGC(
widget->display,
widget->gc,
GCClipXOrigin | GCClipYOrigin | GCClipMask,
&(XGCValues){
.clip_x_origin = xorigin,
.clip_y_origin = y,
.clip_mask = mask,
}
);
XCopyArea(
widget->display,
pix, widget->layers[LAYER_ICONS].pix,
widget->gc,
0, 0,
THUMBSIZE, THUMBSIZE,
xorigin,
y
);
XChangeGC(
widget->display,
widget->gc,
GCClipMask,
&(XGCValues){ .clip_mask = None }
);
}
}
static void
drawlabel(Widget *widget, int index, int x, int y)
{
Picture color;
int i, sel;
int textx, maxw;
int textw, w, textlen, len;
int extensionw, extensionlen;
char *text, *extension;
if (widget->issel != NULL && widget->issel[index])
sel = SELECT_YES;
else
sel = SELECT_NOT;
color = widget->colors[sel][COLOR_FG].pict;
text = widget->items[index].name;
widget->nlines[index] = 1;
textx = x + widget->itemw / 2 - LABELWIDTH / 2;
extension = NULL;
textw = 0;
maxw = 0;
textlen = 0;
widget->linelen[index] = 0;
for (i = 0; i < widget->nlines[index]; i++) {
while (isspace(text[textlen]))
textlen++;
text += textlen;
textlen = strlen(text);
textw = ctrlfnt_width(widget->fontset, text, textlen);
if (widget->nlines[index] < NLINES && textw >= LABELWIDTH) {
textlen = len = 0;
w = 0;
while (w < LABELWIDTH) {
textlen = len;
textw = w;
while (isspace(text[len]))
len++;
while (isbreakable(text[len]))
len++;
while (text[len] != '\0' && !isspace(text[len]) && !isbreakable(text[len]))
len++;
w = ctrlfnt_width(widget->fontset, text, len);
if (text[len] == '\0') {
break;
}
}
if (textw > 0) {
widget->nlines[index] = min(widget->nlines[index] + 1, NLINES);
} else {
textlen = len;