-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathframe-draw.c
678 lines (527 loc) · 15.4 KB
/
frame-draw.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
/*
* ion/ioncore/frame-draw.c
*
* Copyright (c) Tuomo Valkonen 1999-2007.
*
* See the included file LICENSE for details.
*/
#include <string.h>
#include <libtu/objp.h>
#include <libtu/minmax.h>
#include <libtu/map.h>
#include "common.h"
#include "frame.h"
#include "framep.h"
#include "frame-draw.h"
#include "strings.h"
#include "activity.h"
#include "names.h"
#include "gr.h"
#include "gr-util.h"
#define BAR_INSIDE_BORDER(FRAME) \
((FRAME)->barmode==FRAME_BAR_INSIDE || (FRAME)->barmode==FRAME_BAR_NONE)
#define BAR_EXISTS(FRAME) ((FRAME)->barmode!=FRAME_BAR_NONE)
#define BAR_H(FRAME) (FRAME)->bar_h
/*{{{ Attributes */
GR_DEFATTR(active);
GR_DEFATTR(inactive);
GR_DEFATTR(selected);
GR_DEFATTR(unselected);
GR_DEFATTR(tagged);
GR_DEFATTR(not_tagged);
GR_DEFATTR(dragged);
GR_DEFATTR(not_dragged);
GR_DEFATTR(activity);
GR_DEFATTR(no_activity);
GR_DEFATTR(quasiactive);
GR_DEFATTR(not_quasiactive);
static void ensure_create_attrs()
{
GR_ALLOCATTR_BEGIN;
GR_ALLOCATTR(active);
GR_ALLOCATTR(inactive);
GR_ALLOCATTR(selected);
GR_ALLOCATTR(unselected);
GR_ALLOCATTR(tagged);
GR_ALLOCATTR(not_tagged);
GR_ALLOCATTR(dragged);
GR_ALLOCATTR(not_dragged);
GR_ALLOCATTR(no_activity);
GR_ALLOCATTR(activity);
GR_ALLOCATTR(quasiactive);
GR_ALLOCATTR(not_quasiactive);
GR_ALLOCATTR_END;
}
void frame_update_attr(WFrame *frame, int i, WRegion *reg)
{
GrStyleSpec *spec;
bool selected, tagged, dragged, activity;
if(i>=frame->titles_n){
/* Might happen when deinitialising */
return;
}
ensure_create_attrs();
spec=&frame->titles[i].attr;
selected=(reg==FRAME_CURRENT(frame));
tagged=(reg!=NULL && reg->flags®ION_TAGGED);
dragged=(i==frame->tab_dragged_idx);
activity=(reg!=NULL && region_is_activity_r(reg));
gr_stylespec_unalloc(spec);
gr_stylespec_set(spec, selected ? GR_ATTR(selected) : GR_ATTR(unselected));
gr_stylespec_set(spec, tagged ? GR_ATTR(tagged) : GR_ATTR(not_tagged));
gr_stylespec_set(spec, dragged ? GR_ATTR(dragged) : GR_ATTR(not_dragged));
gr_stylespec_set(spec, activity ? GR_ATTR(activity) : GR_ATTR(no_activity));
}
/*}}}*/
/*{{{ (WFrame) dynfun default implementations */
static uint get_spacing(const WFrame *frame)
{
GrBorderWidths bdw;
if(frame->brush==NULL)
return 0;
grbrush_get_border_widths(frame->brush, &bdw);
return bdw.spacing;
}
void frame_border_geom(const WFrame *frame, WRectangle *geom)
{
geom->x=0;
geom->y=0;
geom->w=REGION_GEOM(frame).w;
geom->h=REGION_GEOM(frame).h;
if(!BAR_INSIDE_BORDER(frame) && frame->brush!=NULL){
geom->y+=frame->bar_h;
geom->h=MAXOF(0, geom->h-frame->bar_h);
}
}
void frame_border_inner_geom(const WFrame *frame, WRectangle *geom)
{
GrBorderWidths bdw;
frame_border_geom(frame, geom);
if(frame->brush!=NULL){
grbrush_get_border_widths(frame->brush, &bdw);
geom->x+=bdw.left;
geom->y+=bdw.top;
geom->w=MAXOF((unsigned int)0, geom->w-(bdw.left+bdw.right));
geom->h=MAXOF((unsigned int)0, geom->h-(bdw.top+bdw.bottom));
}
}
void frame_bar_geom(const WFrame *frame, WRectangle *geom)
{
uint off;
if(BAR_INSIDE_BORDER(frame)){
off=0; /*get_spacing(frame);*/
frame_border_inner_geom(frame, geom);
}else{
off=0;
geom->x=0;
geom->y=0;
geom->w=(frame->barmode==FRAME_BAR_SHAPED
? frame->bar_w
: REGION_GEOM(frame).w);
}
geom->x+=off;
geom->y+=off;
geom->w=MAXOF((unsigned int)0, geom->w-2*off);
geom->h=BAR_H(frame);
}
void frame_managed_geom(const WFrame *frame, WRectangle *geom)
{
uint spacing=get_spacing(frame);
frame_border_inner_geom(frame, geom);
/*
geom->x+=spacing;
geom->y+=spacing;
geom->w-=2*spacing;
geom->h-=2*spacing;
*/
if(BAR_INSIDE_BORDER(frame) && BAR_EXISTS(frame)){
geom->y+=frame->bar_h+spacing;
geom->h-=frame->bar_h+spacing;
}
geom->w=MAXOF(geom->w, 0);
geom->h=MAXOF(geom->h, 0);
}
void frame_set_shape(WFrame *frame)
{
WRectangle gs[2];
int n=0;
if(frame->brush!=NULL){
if(BAR_EXISTS(frame)){
frame_bar_geom(frame, gs+n);
n++;
}
frame_border_geom(frame, gs+n);
n++;
grbrush_set_window_shape(frame->brush, TRUE, n, gs);
}
}
void frame_clear_shape(WFrame *frame)
{
grbrush_set_window_shape(frame->brush, TRUE, 0, NULL);
}
#define CF_TAB_MAX_TEXT_X_OFF 10
void frame_get_max_width_and_elastic(WFrame * frame,int bar_w,int *maxw,int *elastic, int *minw);//FIXME
static void free_title(WFrame *frame, int i)
{
if(frame->titles[i].text!=NULL){
free(frame->titles[i].text);
frame->titles[i].text=NULL;
}
}
/* Proportional tabs algorithm:
* Sort tabs by text sizes.
* From smallest to largest do:
* Compare current tab width with remaining width (incl. current)
divided by no. of remaining tabs (incl. current).
* If larger or equal, then set maximum width to the number computed
above and set elastic space to zero (resp. to remain after division).
(Return.)
* If smaller, subctract current width from remaining width and
subtract one from no. of remaining tabs. If this is the last
tab, then set additional padding to remaining width divided
by total number of tabs.
Do not use w for shaped frames.
*/
void frame_get_max_width_and_elastic(WFrame * frame,int bar_w,int *maxw,int *elastic, int *minw){
/* Dummy implementation O(n^2), instead of O(n*log(n)) */
int textw=0,tmp,tminw=frame->tabs_params.propor_tab_min_w;
WLListIterTmp itmp;
WRegion *sub;
const char *displayname;
GrBorderWidths bdw;
uint bdtotal,curw,nextw;
int m, n;
m=FRAME_MCOUNT(frame);
*minw=0;
/*
if(frame->bar_brush==NULL)
*elastic=0;
*maxw=-1;
fprintf(stderr,"ZERR\n");
return;
*/
if(m>0){
grbrush_get_border_widths(frame->bar_brush, &bdw);
bdtotal=((m-1)*(bdw.tb_ileft+bdw.tb_iright+bdw.spacing)
+bdw.right+bdw.left);
tmp = bar_w - bdtotal;
#ifdef DEBUG
fprintf(stderr,"TMPST:%i barw:%i bdt:%i\n",tmp,bar_w,bdtotal);
#endif
curw=0;
n=m;
while (n>0) {
nextw=(uint)-1;/*FIXME: MAXINT*/
if ((signed)curw*n >= tmp) {
/*Remainig tabs are too large => equal width.*/
*maxw=tmp/n;
*elastic=tmp-(*maxw)*n;
#ifdef DEBUG
fprintf(stderr,"TRUNC maxw:%i elastic:%i tmp:%i n:%i\n",*maxw,*elastic,tmp,n);
#endif
return;
}
FRAME_MX_FOR_ALL(sub, frame, itmp){
displayname=region_displayname(sub);
if(displayname==NULL)
textw=0;
else
textw=grbrush_get_text_width(frame->bar_brush,
displayname, strlen(displayname));
if (textw<tminw)
textw=tminw;
if((unsigned)textw == curw){
#ifdef DEBUG
fprintf(stderr,"TW:%i (%s)\n",textw,displayname);
#endif
tmp-=textw;
n--;
} else if((unsigned)textw>curw){
if ((unsigned)textw<nextw)
nextw=textw;
}
}
curw = nextw;
}
#ifdef DEBUG
fprintf(stderr,"TMP elastic:%i\n",tmp);
#endif
n=0;
curw=0;
/*We have some padding left. Try to enlarge small tabs*/
while (n<m) {
nextw=(uint)-1;/*FIXME: MAXINT*/
FRAME_MX_FOR_ALL(sub, frame, itmp){
displayname=region_displayname(sub);
if(displayname==NULL)
continue;
textw=grbrush_get_text_width(frame->bar_brush,
displayname, strlen(displayname));
if (textw<tminw)
textw=tminw;
if((unsigned)textw == curw){
n++;
} else if((unsigned)textw>curw){
if ((unsigned)textw<nextw)
nextw=textw;
}
}
if (nextw>(unsigned)frame->tabs_params.tab_min_w)
nextw=frame->tabs_params.tab_min_w;
#ifdef DEBUG
fprintf(stderr,"TMP --- tmp:%i n:%i curw:%i nextw:%i min:%i\n",tmp,n,curw,nextw,*minw);
#endif
if (n*(nextw-curw)<(unsigned)tmp) {
/*we can extend small tabs to 'nextw'*/
*minw=nextw;
tmp-=n*(nextw-curw);
#ifdef DEBUG
fprintf(stderr,"TMPSUBGO tmp:%i n:%i curw:%i nextw:%i min:%i\n",tmp,n,curw,nextw,*minw);
#endif
} else {
/*we can extend small tabs only to 'curw+tmp/n'*/
*minw+=tmp/n;
tmp-=(*minw-curw)*n;
#ifdef DEBUG
fprintf(stderr,"TMPSUBBRK tmp:%i n:%i curw:%i nextw:%i min:%i\n",tmp,n,curw,nextw,*minw);
#endif
break;
}
if (nextw==(unsigned)frame->tabs_params.tab_min_w)
break;
curw=nextw;
}
#ifdef DEBUG
fprintf(stderr,"TMP elastic:%i min:%i\n",tmp,*minw);
#endif
*elastic=tmp;
} else {
*elastic=0;
}
*maxw=-1;
}
void frame_recalc_bar(WFrame *frame)
{
int textw, i;
WLListIterTmp tmp;
WRegion *sub;
char *title;
bool set_shape;
bool complete = TRUE;
if(frame->bar_brush==NULL || frame->titles==NULL)
return;
set_shape=frame->tabs_params.alg(frame, complete);
if(set_shape) {
if(frame->barmode==FRAME_BAR_SHAPED)
frame_set_shape(frame);
else
frame_clear_shape(frame);
}
i=0;
if(FRAME_MCOUNT(frame)==0){
free_title(frame, i);
textw=frame->titles[i].iw;
if(textw>0){
title=grbrush_make_label(frame->bar_brush, TR("<empty frame>"),
textw);
frame->titles[i].text=title;
}
return;
}
FRAME_MX_FOR_ALL(sub, frame, tmp){
free_title(frame, i);
textw=frame->titles[i].iw;
if(textw>0){
if(frame->flags&FRAME_SHOW_NUMBERS){
char *s=NULL;
const char *name=region_displayname(sub);
libtu_asprintf(&s, "[%d] %s", i+1, name);
if(s!=NULL){
title=grbrush_make_label(frame->bar_brush, s, textw);
free(s);
}else{
title=NULL;
}
}else{
title=region_make_label(sub, textw, frame->bar_brush);
}
frame->titles[i].text=title;
}
i++;
}
}
void frame_draw_bar(const WFrame *frame, bool complete)
{
WRectangle geom;
if(frame->bar_brush==NULL
|| !BAR_EXISTS(frame)
|| frame->titles==NULL){
return;
}
frame_bar_geom(frame, &geom);
grbrush_begin(frame->bar_brush, &geom, GRBRUSH_AMEND);
grbrush_init_attr(frame->bar_brush, &frame->baseattr);
grbrush_draw_textboxes(frame->bar_brush, &geom, frame->titles_n,
frame->titles, complete);
grbrush_end(frame->bar_brush);
}
void frame_draw(const WFrame *frame, bool complete)
{
WRectangle geom;
if(frame->brush==NULL)
return;
frame_border_geom(frame, &geom);
grbrush_begin(frame->brush, &geom, (complete ? 0 : GRBRUSH_NO_CLEAR_OK));
grbrush_init_attr(frame->brush, &frame->baseattr);
grbrush_draw_border(frame->brush, &geom);
frame_draw_bar(frame, TRUE);
grbrush_end(frame->brush);
}
void frame_brushes_updated(WFrame *frame)
{
WFrameBarMode barmode=FRAME_BAR_INSIDE;
char *s;
if(frame->brush==NULL)
return;
if(frame->mode==FRAME_MODE_FLOATING)
barmode=FRAME_BAR_SHAPED;
else if(frame->mode==FRAME_MODE_TRANSIENT)
barmode=FRAME_BAR_NONE;
else if(frame->mode==FRAME_MODE_TILED_ALT)
barmode=FRAME_BAR_NONE;
if(grbrush_get_extra(frame->brush, "bar", 's', &s)){
if(strcmp(s, "inside")==0)
barmode=FRAME_BAR_INSIDE;
else if(strcmp(s, "outside")==0)
barmode=FRAME_BAR_OUTSIDE;
else if(strcmp(s, "shaped")==0)
barmode=FRAME_BAR_SHAPED;
else if(strcmp(s, "none")==0)
barmode=FRAME_BAR_NONE;
free(s);
}
frame->barmode=barmode;
if(barmode==FRAME_BAR_NONE || frame->bar_brush==NULL){
frame->bar_h=0;
}else{
GrBorderWidths bdw;
GrFontExtents fnte;
grbrush_get_border_widths(frame->bar_brush, &bdw);
grbrush_get_font_extents(frame->bar_brush, &fnte);
frame->bar_h=bdw.top+bdw.bottom+fnte.max_height;
}
/* tabs and bar width calculation stuff */
frame_tabs_calc_brushes_updated(frame);
}
/*}}}*/
/*{{{ Misc. */
void frame_updategr(WFrame *frame)
{
frame_release_brushes(frame);
frame_initialise_gr(frame);
/* Update children */
region_updategr_default((WRegion*)frame);
mplex_fit_managed(&frame->mplex);
frame_recalc_bar(frame);
frame_set_background(frame, TRUE);
}
StringIntMap frame_tab_styles[]={
{"tab-frame-tiled", FRAME_MODE_TILED},
{"tab-frame-tiled-alt", FRAME_MODE_TILED_ALT},
{"tab-frame-floating", FRAME_MODE_FLOATING},
{"tab-frame-transient", FRAME_MODE_TRANSIENT},
END_STRINGINTMAP
};
const char *framemode_get_tab_style(WFrameMode mode)
{
return stringintmap_key(frame_tab_styles, mode, "tab-frame");
}
const char *framemode_get_style(WFrameMode mode)
{
const char *p=framemode_get_tab_style(mode);
assert(p!=NULL);
return (p+4);
}
void frame_initialise_gr(WFrame *frame)
{
Window win=frame->mplex.win.win;
WRootWin *rw=region_rootwin_of((WRegion*)frame);
const char *style=framemode_get_style(frame->mode);
const char *tab_style=framemode_get_tab_style(frame->mode);
frame->brush=gr_get_brush(win, rw, style);
if(frame->brush==NULL)
return;
frame->bar_brush=grbrush_get_slave(frame->brush, rw, tab_style);
if(frame->bar_brush==NULL)
return;
frame_brushes_updated(frame);
}
void frame_release_brushes(WFrame *frame)
{
if(frame->bar_brush!=NULL){
grbrush_release(frame->bar_brush);
frame->bar_brush=NULL;
}
if(frame->brush!=NULL){
grbrush_release(frame->brush);
frame->brush=NULL;
}
}
bool frame_set_background(WFrame *frame, bool set_always)
{
GrTransparency mode=GR_TRANSPARENCY_DEFAULT;
if(FRAME_CURRENT(frame)!=NULL){
if(OBJ_IS(FRAME_CURRENT(frame), WClientWin)){
WClientWin *cwin=(WClientWin*)FRAME_CURRENT(frame);
mode=(cwin->flags&CLIENTWIN_PROP_TRANSPARENT
? GR_TRANSPARENCY_YES : GR_TRANSPARENCY_NO);
}else if(!OBJ_IS(FRAME_CURRENT(frame), WGroup)){
mode=GR_TRANSPARENCY_NO;
}
}
if(mode!=frame->tr_mode || set_always){
frame->tr_mode=mode;
if(frame->brush!=NULL){
grbrush_enable_transparency(frame->brush, mode);
window_draw((WWindow*)frame, TRUE);
}
return TRUE;
}
return FALSE;
}
void frame_setup_dragwin_style(WFrame *frame, GrStyleSpec *spec, int tab)
{
gr_stylespec_append(spec, &frame->baseattr);
gr_stylespec_append(spec, &frame->titles[tab].attr);
}
/*}}}*/
/*{{{ Activated/inactivated */
void frame_inactivated(WFrame *frame)
{
ensure_create_attrs();
gr_stylespec_set(&frame->baseattr, GR_ATTR(inactive));
gr_stylespec_unset(&frame->baseattr, GR_ATTR(active));
window_draw((WWindow*)frame, FALSE);
}
void frame_activated(WFrame *frame)
{
ensure_create_attrs();
gr_stylespec_set(&frame->baseattr, GR_ATTR(active));
gr_stylespec_unset(&frame->baseattr, GR_ATTR(inactive));
window_draw((WWindow*)frame, FALSE);
}
void frame_quasiactivity_change(WFrame *frame)
{
bool is=(frame->quasiactive_count>0);
ensure_create_attrs();
if(is){
gr_stylespec_set(&frame->baseattr, GR_ATTR(quasiactive));
gr_stylespec_unset(&frame->baseattr, GR_ATTR(not_quasiactive));
}else{
gr_stylespec_set(&frame->baseattr, GR_ATTR(not_quasiactive));
gr_stylespec_unset(&frame->baseattr, GR_ATTR(quasiactive));
}
if(!REGION_IS_ACTIVE(frame))
window_draw((WWindow*)frame, FALSE);
}
/*}}}*/