/
frame-actions.c
1396 lines (1194 loc) · 48.8 KB
/
frame-actions.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
/**************************************************************************
Lunchbox Window Manager
Copyright (C) 2008 Alysander Stanley
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <X11/extensions/shape.h>
#include <X11/Xlib.h>
#include <X11/Xcursor/Xcursor.h>
#include <X11/Xatom.h>
#include <limits.h>
#include "xcheck.h"
#include "lunchbox.h"
#include "menus.h"
#include "theme.h"
#include "frame.h"
#include "space.h"
#include "frame-actions.h"
#include "util.h"
/**
@file frame-actions.c
@brief High level functions for manipulating frames.
@author Alysander Stanley
**/
static void
change_mode_pulldown_text_pixmap(Display *display, struct Frame *frame, int index, struct Themes *themes);
static Bool
move_frame_into_spaces (struct Frame *frame, struct Rectangle_list free_spaces);
static Bool
fit_frame_into_spaces(struct Frame *frame, struct Rectangle_list free_spaces);
//Attempt to expand a frame in place if that is possible
static Bool
maximize_tiled_frame_in_place(struct Frame *frame, const char direction, struct Rectangle_list free_spaces);
static void
_resize_frame(Display* display, struct Frame* frame, const struct Workarea *workarea, struct Themes *themes, Bool enlarge_background_frames);
/**
@brief This function ensures that the window is within limits. It sets the frames x,y,w,h to achieve this.
@return void
**/
void
check_frame_limits(struct Frame *frame, const struct Workarea *workarea, struct Themes *themes) {
if(frame->state == fullscreen) return;
frame->w -= (frame->w - frame->hspace) % frame->width_inc - frame->w_inc_offset;
frame->h -= (frame->h - frame->vspace) % frame->height_inc - frame->h_inc_offset;
if(frame->w < frame->min_width) frame->w = frame->min_width;
else
if(frame->w > frame->max_width) frame->w = frame->max_width;
if(frame->h < frame->min_height) frame->h = frame->min_height;
else
if(frame->h > frame->max_height) frame->h = frame->max_height;
if(frame->mode == desktop) {
frame->x = 0 - themes->window_type[frame->theme_type][window].x;
frame->y = 0 - themes->window_type[frame->theme_type][window].y;
frame->w = workarea->screen_width + frame->hspace;
frame->h = workarea->screen_height + frame->vspace;
} else {
if(frame->w > workarea->width)
frame->w = workarea->width;
if(frame->h > workarea->height)
frame->h = workarea->height;
if(frame->x < 0 ) frame->x = 0;
if(frame->y < 0 ) frame->y = 0;
if(frame->x + frame->w > workarea->width)
frame->x = workarea->width - frame->w;
if(frame->y + frame->h > workarea->height)
frame->y = workarea->height - frame->h;
}
}
void
reset_frame_titlebar(Display *display, struct Frame *frame) {
change_frame_widget_state(display, frame, frame_parent, normal);
change_frame_widget_state(display, frame, close_button, normal);
change_frame_widget_state(display, frame, title_menu_lhs, normal);
change_frame_widget_state(display, frame, title_menu_text, normal);
change_frame_widget_state(display, frame, title_menu_rhs, normal);
change_frame_widget_state(display, frame, mode_dropdown_lhs, normal);
change_frame_widget_state(display, frame, mode_dropdown_text, normal);
change_frame_widget_state(display, frame, mode_dropdown_rhs, normal);
xcheck_raisewin(display, frame->widgets[mode_dropdown_hotspot].widget);
XFlush(display);
}
/**
@brief This function changes the frames mode to the desired mode.
It shows the appropriate mode menu on the frame and resizes/moves the frame if appropriate.
If the mode is "tiling" it cannot ensure that the windows do not overlap because it doesn't have access to the frame list.
If the mode is set to "unset" then it sets the frame to whatever frame->mode currently is.
In the create frame function, this must be after create_frame_subwindows is called.
It is also after the get_frame_mode function, which sets the frame->mode directly before the create_frame_subwindows.
It should then be called with the mode "unset"
@return void
**/
void
change_frame_mode(Display *display, struct Frame *frame, enum Window_mode mode, const struct Workarea *workarea, struct Themes *themes) {
/**** Set the initial frame mode to whatever frame mode currently. This is done when the frame is created. ***/
if(mode == unset) {
mode = frame->mode;
}
/**** Undo state changes from current frame mode before settings new mode ****/
else {
if(frame->mode == desktop) {
frame->mode = mode;
check_frame_limits(frame, workarea, themes);
resize_frame(display, frame, workarea, themes);
}
}
XFlush(display);
/*** Change the state of the frame to the new mode ***/
if(mode == floating) {
frame->mode = floating;
change_mode_pulldown_text_pixmap(display, frame, mode_dropdown_text_floating, themes);
} else if(mode == tiling) {
frame->mode = tiling;
change_mode_pulldown_text_pixmap(display, frame, mode_dropdown_text_tiling, themes);
//cannot drop frame here because it requires access to the whole frame list
check_frame_limits(frame, workarea, themes);
resize_frame(display, frame, workarea, themes);
} else if(mode == desktop) {
change_mode_pulldown_text_pixmap(display, frame, mode_dropdown_text_desktop, themes);
frame->mode = desktop;
check_frame_limits(frame, workarea, themes);
resize_frame(display, frame, workarea, themes);
}
reset_frame_titlebar(display, frame);
}
/**
@brief This is an internal function used to change the frame mode indicated on the frame. It mimics that used to change the frame name, but luckily the mode pixmaps can be pre-generated
@return void
**/
static void
change_mode_pulldown_text_pixmap(Display *display, struct Frame *frame, int index, struct Themes *themes) {
if(frame->widgets[mode_dropdown_text].widget) {
XUnmapWindow(display, frame->widgets[mode_dropdown_text].widget);
for(int j = 0; j <= inactive; j++) if(themes->window_type[frame->theme_type][mode_dropdown_text].exists)
if(themes->window_type[frame->theme_type][index].state_p[j]){
XSetWindowBackgroundPixmap(display, frame->widgets[mode_dropdown_text].state[j]
, themes->window_type[frame->theme_type][index].state_p[j]);
}
XFlush(display);
XMapWindow(display, frame->widgets[mode_dropdown_text].widget);
XFlush(display);
}
}
/**
@brief This function makes a window fullscreen. It resizes it, but resets the values back to their originals
@return void
**/
void
change_frame_state (Display *display, struct Frame *frame, enum Window_state state
, struct Separators *seps, const struct Workarea *workarea, struct Themes *themes, struct Atoms *atoms) {
if(state == fullscreen) {
int x = frame->x;
int y = frame->y;
int w = frame->w;
int h = frame->h;
//Make the window look fullscreen and then swap the variables back
frame->x = 0 - themes->window_type[frame->type][window].x;
frame->y = 0 - themes->window_type[frame->type][window].y;
frame->w = workarea->screen_width + frame->hspace;
frame->h = workarea->screen_height + frame->vspace;
frame->state = none;
resize_frame(display, frame, workarea, themes);
frame->state = state;
stack_frame(display, frame, seps);
frame->x = x;
frame->y = y;
frame->w = w;
frame->h = h;
XChangeProperty(display, frame->framed_window, atoms->wm_state, XA_ATOM, 32, PropModeReplace, (unsigned char *)&atoms->wm_state_fullscreen, 1);
XFlush(display);
#ifdef SHOW_FRAME_STATE
printf("Making window fullscreen %s\n", frame->window_name);
#endif
}
if(state == none) {
frame->state = none;
//make sure that the property is removed
XDeleteProperty(display, frame->framed_window, atoms->wm_state);
stack_frame(display, frame, seps);
resize_frame(display, frame, workarea, themes);
XFlush(display);
#ifdef SHOW_FRAME_STATE
printf("Making window state none %s\n", frame->window_name);
#endif
}
if(state == minimized) {
frame->state = minimized;
XChangeProperty(display, frame->framed_window, atoms->wm_state, XA_ATOM, 32, PropModeReplace, (unsigned char *)&atoms->wm_state_hidden, 1);
XFlush(display);
#ifdef SHOW_FRAME_STATE
printf("Making window hidden/minimized %s\n", frame->window_name);
#endif
}
return;
}
/**
@brief drop frame is normally called when the mode of the frame is going to change.
In that case, we can determine whether we want to try and tile it next to all tiling windows or just panels
However, in several cases we are just trying to enforce whatever it's exiting tiling policy is.
This function is a wrapper for drop frame that does that.
@return True if the frame fits, False otherwise
**/
Bool
redrop_frame (Display *display, struct Workspace *frames, int clicked_frame, const struct Workarea *workarea, struct Themes *themes) {
Bool result = False;
if(frames->list[clicked_frame]->mode == floating) {
//Floating windows only need to avoid panels
result = drop_frame(frames, clicked_frame, True, workarea);
} else {
result = drop_frame(frames, clicked_frame, False, workarea);
}
// If anything has been moved or resized we need
// to check it's ok and reflect the changes
// in the window
// These *might* make the window smaller
if(result) {
check_frame_limits(frames->list[clicked_frame], workarea, themes);
resize_frame(display, frames->list[clicked_frame], workarea, themes);
XFlush(display);
}
return result;
}
/**
@brief This function moves the dropped window to the nearest available space.
If the window has been enlarged so that it exceeds all available sizes,
a best-fit algorithm is used to determine the closest size.
If all spaces are smaller than the window's minimum size
(which can only happen if the window's mode is being changed) the window
remains unchanged and the function returns false.
@pre the window is not fullscreen or minimized
@return True if it was able to tile the frame, False otherwise.
**/
Bool
drop_frame (struct Workspace *frames, int clicked_frame, Bool only_panels, const struct Workarea *workarea) {
struct Frame *frame = frames->list[clicked_frame];
struct Rectangle_list free_spaces = {0, 8, NULL};
#ifdef SHOW_FRAME_DROP
printf("Attempting to find room for a window\n");
#endif
if(frame->state == fullscreen || frame->mode == desktop) return True;
//try and drop it in if its state is none or minimized
enum Window_mode temp_mode = frame->mode;
frame->mode = floating; //otherwise the window might try to avoid itself!
free_spaces = get_free_screen_spaces (only_panels, frames, workarea);
frame->mode = temp_mode;
if(free_spaces.list == NULL) {
#ifdef SHOW_FRAME_DROP
printf("No free spaces\n");
printf("Couldn't fit window at all: %s\n", frame->window_name);
#endif
return False;
}
//Try to just move the window to the nearest available space
if(!move_frame_into_spaces(frame, free_spaces)) {
//Try to fit the window to the most closely sized space available
if(!fit_frame_into_spaces(frame, free_spaces)) {
#ifdef SHOW_FRAME_DROP
printf("Could not move or the frame anywhere!\n");
#endif
free(free_spaces.list);
return False;
} else {
//It was resized to fit into an existing space.
//Find the nearest space to place it in (there might
// be more than one)
if(!move_frame_into_spaces(frame, free_spaces)) {
#ifdef SHOW_FRAME_DROP
printf("Could not move fitted frame!\n");
#endif
free(free_spaces.list);
return False;
}
}
}
free(free_spaces.list);
return True;
}
// Attempt to expand a frame in place if that is possible. Modified frame width and height.
// direction can be 'x', 'y' or 0
Bool
maximize_tiled_frame_in_place(struct Frame *frame, const char direction, struct Rectangle_list free_spaces) {
double max_size = 0;
//make the frame into a rectangle for calculating displacement function
struct Rectangle window = {frame->x, frame->y, frame->w, frame->h };
struct Rectangle *best_space = NULL;
#ifdef SHOW_FRAME_DROP
printf("Expanding frame\n");
#endif
/* Try and fit the window into a space in already occupies. */
for(unsigned int k = 0; k < free_spaces.used; k++) {
//if x,y is inside a space
//find the max size it can be.
//while including the max width or height
int effective_width;
int effective_height;
// Don't ever reduce any dimension when maximizing
if(frame->h > free_spaces.list[k].h) continue;
if(frame->w > free_spaces.list[k].w) continue;
if(direction == 'x') {
effective_height = frame->h;
effective_width = MIN(frame->max_width, free_spaces.list[k].w);
}
if(direction == 'y') {
effective_height = MIN(frame->max_height, free_spaces.list[k].h);
effective_width = frame->w;
}
//maximize in both directions
if(!direction) {
effective_height = MIN(frame->max_height, free_spaces.list[k].h);
effective_width = MIN(frame->max_width, free_spaces.list[k].w);
}
int effective_area = effective_width * effective_height;
// +-----------+
// | |
// | O----+|
// | | ||
// | | ||
// | +----+|
// | |
// +-----------+
// Before a frame is maximized, it will look like the above diagram
// With the x1,y1 of the window inside the space.
if((window.x >= free_spaces.list[k].x && window.x < free_spaces.list[k].x + free_spaces.list[k].w)
&& (window.y >= free_spaces.list[k].y && window.y < free_spaces.list[k].y + free_spaces.list[k].h)
) {
// There could be more than one overlapping space, so
// find the one that allows the frame to be the biggest.
if(effective_area > max_size) {
max_size = effective_area;
best_space = &free_spaces.list[k];
}
}
}
if(best_space) {
if(direction == 'x') {
frame->w = best_space->w;
}
if(direction == 'y') {
frame->h = best_space->h;
}
//maximize in both directions
if(!direction) {
frame->w = best_space->w;
frame->h = best_space->h;
}
return True;
}
return False;
}
Bool
move_frame_into_spaces (struct Frame *frame, struct Rectangle_list free_spaces) {
double min_displacement = M_DOUBLE_MAX;
int min_dx = 0;
int min_dy = 0;
//make the frame into a rectangle for calculating displacement function
struct Rectangle window = {frame->x, frame->y, frame->w, frame->h };
#ifdef SHOW_FRAME_DROP
printf("Attempting to move frame into a space\n");
#endif
/* Try and fit the window into a space. */
for(unsigned int k = 0; k < free_spaces.used; k++) {
int dx = 0;
int dy = 0;
double displacement = calculate_displacement(window, free_spaces.list[k], &dx, &dy);
#ifdef SHOW_FRAME_DROP
printf("Free space:space %d, x %d, y %d, w %d, h %d, distance %f\n", k
, free_spaces.list[k].x, free_spaces.list[k].y
, free_spaces.list[k].w, free_spaces.list[k].h, displacement < M_DOUBLE_MAX ? displacement : -1.00);
#endif
if(displacement < min_displacement) {
min_displacement = displacement;
min_dx = dx;
min_dy = dy;
}
}
if(min_displacement != M_DOUBLE_MAX) {
#ifdef SHOW_FRAME_DROP
printf("Found min_dx %d, min_dy %d, distance %f\n", min_dx, min_dy, min_displacement);
#endif
frame->x += min_dx;
frame->y += min_dy;
return True;
}
return False;
}
// Change a frame with/heitght to fit into the most closely sized space
// Doesn't move the frame.
Bool
fit_frame_into_spaces(struct Frame *frame, struct Rectangle_list free_spaces) {
#ifdef SHOW_FRAME_DROP
printf("Move failed - finding the nearest size\n");
#endif
double best_fit = -1;
struct Rectangle *best_space = NULL;
for(unsigned int k = 0; k < free_spaces.used; k++) {
// Don't use any spaces that have a width less
// than the minimum
if (free_spaces.list[k].w < frame->min_width
|| free_spaces.list[k].h < frame->min_height) {
continue;
}
int effective_height = MIN(frame->h, free_spaces.list[k].h);
int effective_width = MIN(frame->w, free_spaces.list[k].w);
double effective_area = effective_width * effective_height;
#ifdef SHOW_FRAME_DROP
printf("Closest fit %f for space %d\n", effective_area, k);
#endif
//get the space that reduces the size of the frame the least
if(effective_area > best_fit) {
best_fit = effective_area;
best_space = &free_spaces.list[k];
}
}
if(best_space != NULL) {
int effective_height = MIN(frame->h, best_space->h);
int effective_width = MIN(frame->w, best_space->w);
frame->w = effective_width;
frame->h = effective_height;
#ifdef SHOW_FRAME_DROP
printf("Window w/h changed. w: %d, h: %d. %s\n", frame->w, frame->h, frame->window_name);
#endif
return True;
} else {
#ifdef SHOW_FRAME_DROP
printf("Couldn't fit window at all: %s\n", frame->window_name);
#endif
return False;
}
}
/**
@brief Changes the look of a frame's widget. It considers whether the window is already focussed when doing this.
@return void
**/
void
change_frame_widget_state(Display* display, struct Frame* frame, enum Frame_widget widget, enum Widget_state state) {
if(frame->focussed) {
switch(state) {
case normal: state = normal_focussed; break;
case active: state = active_focussed; break;
case normal_hover: state = normal_focussed_hover; break;
case active_hover: state = active_focussed_hover; break;
break;
default:
break;
}
}
xcheck_raisewin(display, frame->widgets[widget].state[state]);
XFlush(display);
}
/**
@brief Resizes the subwindows of the frame after screen size changes occur so as not to expose black borders.
@return void
**/
void
resize_frame_after_screen_size_change(Display* display, struct Frame* frame, const struct Workarea *workarea, struct Themes *themes) {
_resize_frame(display, frame, workarea, themes, True);
}
/**
@brief Moves and resizes the subwindows of the frame
@return void
**/
resize_frame(Display* display, struct Frame* frame, const struct Workarea *workarea, struct Themes *themes) {
_resize_frame(display, frame, workarea, themes, False);
}
void
_resize_frame(Display* display, struct Frame* frame, const struct Workarea *workarea, struct Themes *themes, Bool enlarge_background_frames) {
/*Do not move or resize fullscreen windows */
if(frame->state == fullscreen) return;
XMoveResizeWindow(display, frame->widgets[frame_parent].widget, frame->x, frame->y, frame->w, frame->h);
XMoveResizeWindow(display, frame->framed_window, 0, 0, frame->w - frame->hspace, frame->h - frame->vspace);
if((frame->w - frame->hspace) % frame->width_inc) {
#ifdef SHOW_FRAME_INC
printf("Width remainder %d of inc %d, offset %d \n", (frame->w - frame->hspace) % frame->width_inc
, frame->width_inc, frame->w_inc_offset);
#endif
}
if((frame->h - frame->vspace) % frame->height_inc) {
#ifdef SHOW_FRAME_INC
printf("Height remainder %d of inc %d, offset %d \n", (frame->h - frame->vspace) % frame->height_inc
, frame->height_inc, frame->h_inc_offset);
#endif
}
/* Bit of a hack to make the title menu use only the minimum space required */
int title_menu_text_diff = 0;
int title_menu_rhs_w = 0;
int mode_menu_text_diff = 0;
if(themes->window_type[frame->theme_type][title_menu_rhs].exists)
title_menu_rhs_w = themes->window_type[frame->theme_type][title_menu_rhs].w;
for(int i = 0; i < frame_parent; i++) {
int x = themes->window_type[frame->theme_type][i].x;
int y = themes->window_type[frame->theme_type][i].y;
int w = themes->window_type[frame->theme_type][i].w;
int h = themes->window_type[frame->theme_type][i].h;
if(!themes->window_type[frame->theme_type][i].exists) continue; //the exists variable is -1 for hotspots
if(x < 0 || y < 0 || w <= 0 || h <= 0) { //only resize those which are dependent on the width
if(x < 0) x += frame->w;
if(y < 0) y += frame->h;
if(w <= 0) w += frame->w;
if(h <= 0) h += frame->h;
/* Bit of a hack to make the title menu use only the minimum space required */
if(i == title_menu_text && (frame->menu.width + title_menu_rhs_w) < w) {
title_menu_text_diff = w - (frame->menu.width + title_menu_rhs_w);
int mode_menu_extra_space = w - (frame->menu.width + title_menu_rhs_w);
//mode_menu_extra_space this is the amount "given up" by reducing the title menu width
w = frame->menu.width;
if(mode_menu_extra_space > themes->mode_pulldown_width) {
mode_menu_text_diff = themes->mode_pulldown_width;
}
else mode_menu_text_diff = mode_menu_extra_space;
if(mode_menu_text_diff < 0) mode_menu_text_diff = 0;
} /* title_menu_text is processed before */
else if(i == title_menu_rhs && title_menu_text_diff) x -= title_menu_text_diff;
else if(i == title_menu_hotspot && title_menu_text_diff) w -= title_menu_text_diff;
if(i == mode_dropdown_lhs) {
x -= mode_menu_text_diff;
}
else if(i == mode_dropdown_text || i == mode_dropdown_hotspot) {
x -= mode_menu_text_diff;
w += mode_menu_text_diff;
}
XMoveResizeWindow(display, frame->widgets[i].widget, x, y, w, h);
// These windows aren't resized normally, but will need to be when the screen resolution has changed
// Othwerwise there will be black borders around a window
if(enlarge_background_frames && i != window) { //don't create state windows for the framed window
if(themes->window_type[frame->theme_type][i].w <= 0) w = workarea->screen_width;
if(themes->window_type[frame->theme_type][i].h <= 0) h = workarea->screen_height;
for(int j = 0; j <= inactive; j++) {
XResizeWindow(display, frame->widgets[i].state[j], w, h);
}
}
}
}
XFlush(display);
}
/**
@brief This function handles responding to the users click and drag on the resize grips of the window.
@return void
**/
void
resize_using_frame_grip (Display *display, struct Workspace *frames, int clicked_frame
, int pointer_start_x, int pointer_start_y, int mouse_root_x, int mouse_root_y
, int r_edge_dx, int b_edge_dy, Window clicked_widget, const struct Workarea *workarea, struct Themes *themes) {
#define W_INC_REM ((new_width - frame->hspace) %frame->width_inc - frame->w_inc_offset)
#define H_INC_REM ((new_height - frame->vspace)%frame->height_inc - frame->h_inc_offset)
/*new_x and new_y increment compensation for shrinking windows */
/*New x increment compensation for enlarging windows */
#define NEW_X_INC if((W_INC_REM) && new_width != frame->w) { new_x += W_INC_REM; }
#define NEW_Y_INC if((H_INC_REM) && new_height != frame->h) { new_y += H_INC_REM; }
struct Frame *frame = frames->list[clicked_frame];
int new_width = frame->w;
int new_height = frame->h;
int new_x = frame->x;
int new_y = frame->y;
//review this
if(W_INC_REM < 0) new_width = frame->hspace;
if(H_INC_REM < 0) new_height = frame->vspace;
/* precalculated potential values for x and y */
/* This is done so that the they can be tested and altered in one place */
int pot_x = mouse_root_x - pointer_start_x;
int pot_y = mouse_root_y - pointer_start_y;
if(frame->mode != desktop) {
if(pot_x < 0) pot_x = 0;
if(pot_y < 0) pot_y = 0;
}
// printf("resize_nontiling_frame %s\n, x %d, y %d\n", frame->window_name, frame->x, frame->y);
/* We need to consider more widgets */
if(clicked_widget == frame->widgets[l_edge].widget) {
new_x = pot_x;
new_width = frame->w + (frame->x - new_x);
NEW_X_INC
}
else if(clicked_widget == frame->widgets[t_edge].widget) {
new_y = pot_y;
new_height = frame->h + (frame->y - new_y);
NEW_Y_INC
}
//this could be created from the above 2
else if(clicked_widget == frame->widgets[tl_corner].widget) {
new_x = pot_x;
new_y = pot_y;
new_height = frame->h + (frame->y - new_y);
new_width = frame->w + (frame->x - new_x);
NEW_X_INC
NEW_Y_INC
}
else if(clicked_widget == frame->widgets[tr_corner].widget) {
new_y = pot_y;
new_height = frame->h + (frame->y - new_y);
new_width = mouse_root_x - frame->x + r_edge_dx;
NEW_Y_INC
}
else if(clicked_widget == frame->widgets[r_edge].widget) {
new_width = mouse_root_x - frame->x + r_edge_dx;
}
else if(clicked_widget == frame->widgets[bl_corner].widget) {
new_x = pot_x;
new_width = frame->w + (frame->x - new_x);
new_height = mouse_root_y - frame->y;
NEW_X_INC
}
else if(clicked_widget == frame->widgets[br_corner].widget) {
new_width = mouse_root_x - frame->x + r_edge_dx;
new_height = mouse_root_y - frame->y + b_edge_dy;
}
else if(clicked_widget == frame->widgets[b_edge].widget) {
new_height = mouse_root_y - frame->y + b_edge_dy;
}
if(frame->mode != desktop) {
if(new_x + new_width > workarea->width) {
new_width = workarea->width - new_x;
NEW_X_INC
}
if(new_y + new_height > workarea->height) {
new_height = workarea->height - new_y;
NEW_Y_INC
}
}
#ifdef SHOW_FRAME_INC
if(W_INC_REM) printf("Subtracted %d from width\n", W_INC_REM);
if(H_INC_REM) printf("Subtracted %d from height\n", H_INC_REM);
#endif
new_width -= W_INC_REM;
new_height-= H_INC_REM;
//check that the frame is not outside its min or max sizes
//if height or width is then restricted, need to reduce the movement if it is moving
if(new_height < frame->min_height) { //decreasing
if(new_y > frame->y) new_y -= frame->min_height - new_height;
new_height = frame->min_height;
}
if(new_width < frame->min_width) { //decreasing
if(new_x > frame->x) new_x -= frame->min_width - new_width;
new_width = frame->min_width;
}
if(new_height > frame->max_height) { //increasing
if(new_y < frame->y) new_y += new_height - frame->max_height;
new_height = frame->max_height;
}
if(new_width > frame->max_width) { //increasing
if(new_x < frame->x) new_x += new_width - frame->max_width;
new_width = frame->max_width;
}
//commit height changes
if(frame->mode == tiling) {
if(new_height != frame->h) resize_tiling_frame(display, frames, clicked_frame, 'y', new_y, new_height, workarea, themes);
if(new_width != frame->w) resize_tiling_frame(display, frames, clicked_frame, 'x', new_x, new_width, workarea, themes);
}
else {
frame->x = new_x;
frame->y = new_y;
frame->w = new_width;
frame->h = new_height;
}
resize_frame(display, frame, workarea, themes);
XFlush(display);
}
/**
@brief This handles moving/resizing the window when the titlebar is dragged.
It resizes windows that are pushed against the edge of the screen,
to sizes between the defined min and max.
@return void
**/
void
move_frame (Display *display, struct Frame *frame
, int *pointer_start_x, int *pointer_start_y, int mouse_root_x, int mouse_root_y
, int *resize_x_direction, int *resize_y_direction, const struct Workarea *workarea, struct Themes *themes) {
//the pointer start variables may be updated as the window is squished against the LHS or top of the screen
//because of the changing relative co-ordinates of the pointer on the window.
//resize_x_direction - 1 means LHS, -1 means RHS , 0 means unset
//resize_y_direction - 1 means top, -1 means bottom, 0 means unset
// the resize_x/y_direction variables are used to identify when a squish resize has occured
// and this is then used to decide when to stretch the window from the edge.
int new_width = 0;
int new_height = 0;
int new_x = mouse_root_x - *pointer_start_x;
int new_y = mouse_root_y - *pointer_start_y;
if(frame->state == fullscreen) return;
//do not attempt to resize if the window is larger than the screen
if(frame->min_width <= workarea->width
&& frame->min_height <= workarea->height
&& frame->mode != desktop) {
if((new_x + frame->w > workarea->width ) //window moving off RHS
|| (*resize_x_direction == -1)) {
*resize_x_direction = -1;
new_width = workarea->width - new_x;
}
if((new_x < 0) //window moving off LHS
|| (*resize_x_direction == 1)) {
*resize_x_direction = 1;
new_width = frame->w + new_x;
new_x = 0;
*pointer_start_x = mouse_root_x;
}
if((new_y + frame->h > workarea->height) //window moving off the bottom
|| (*resize_y_direction == -1)) {
*resize_y_direction = -1;
new_height = workarea->height - new_y;
}
if((new_y < 0) //window moving off the top of the screen
|| (*resize_y_direction == 1)) {
*resize_y_direction = 1;
new_height = frame->h + new_y;
new_y = 0;
*pointer_start_y = mouse_root_y;
}
if(new_width != 0 && new_width < frame->min_width) {
new_width = frame->min_width;
if(*resize_x_direction == -1) new_x = workarea->width - frame->min_width;
//don't move the window off the RHS if it has reached it's minimum size
//LHS not considered because x has already been set to 0
}
if(new_height != 0 && new_height < frame->min_height) {
new_height = frame->min_height;
//don't move the window off the bottom if it has reached it's minimum size
//Top not considered because y has already been set to 0
if(*resize_y_direction == -1) new_y = workarea->height - frame->min_height;
}
//limit resizes to max width
if((new_width != 0 && new_width > frame->max_width)
|| (new_height != 0 && new_height > frame->max_height)) {
//these have been changed from zero to max values..
if(new_width > frame->max_width) new_width = frame->max_width;
if(new_height > frame->max_height) new_height = frame->max_height;
}
//do not attempt to resize windows that cannot be resized
if(frame->min_width == frame->max_width) *resize_x_direction = 0;
if(frame->min_height == frame->max_height) *resize_y_direction = 0;
}
if(new_width != 0 || new_height != 0) { //resize window if required
//allow movement in axis if it hasn't been resized
frame->x = new_x;
frame->y = new_y;
if(new_width != 0) frame->w = new_width;
if(new_height != 0) frame->h = new_height;
//allow movement in axis if it hasn't been resized
resize_frame(display, frame, workarea, themes);
}
else {
//Moves the window to the specified location if there is no resizing going on.
frame->x = new_x;
frame->y = new_y;
XMoveWindow(display, frame->widgets[frame_parent].widget, frame->x, frame->y);
}
XFlush(display);
}
/**
@brief This function is activated from the title menu. It causes the windows to swap their positions and mode provided they are within each others frame limits.
@return 0 if nothing happened, 1 if it was successful.
**/
int
replace_frame(Display *display, struct Frame *target, struct Frame *replacement
, struct Separators *seps, const struct Workarea *workarea, struct Themes *themes) {
XWindowChanges changes;
XWindowChanges changes2;
enum Window_mode mode;
unsigned int mask = CWX | CWY | CWWidth | CWHeight;
#ifdef SHOW_BUTTON_PRESS_EVENT
printf("replace frame %s\n", replacement->window_name);
#endif
if(replacement->framed_window == target->framed_window) return 0; //this can be chosen from the title menu
if(target->w < replacement->min_width
|| target->h < replacement->min_height) {
//TODO give message to the user
#ifdef SHOW_BUTTON_PRESS_EVENT
printf("The requested window doesn't fit on the target window\n");
#endif
return 0;
}
if((target->min_width > replacement->w
|| target->min_height > replacement->h)) {
#ifdef SHOW_BUTTON_PRESS_EVENT
printf("The requested window doesn't fit on the target window\n");
#endif
return 0;
}
changes.x = target->x;
changes.y = target->y;
if(target->w < replacement->max_width) changes.width = target->w; //save info for request
else changes.width = replacement->max_width;
if(target->h < replacement->max_height) changes.height = target->h;
else changes.height = replacement->max_height;
mode = replacement->mode;
//if(replacement->state != minimized) {
changes2.x = replacement->x;
changes2.y = replacement->y;
if(replacement->w < target->max_width) changes2.width = replacement->w; //save info for request
else changes2.width = target->max_width;
if(replacement->h < target->max_height) changes2.height = replacement->h;
else changes2.height = target->max_height;
target->x = changes2.x;
target->y = changes2.y;
target->w = changes2.width;
target->h = changes2.height;
//printf("Target mode %d, x %d, y %d, w %d, h %d\n", target->mode, target->x, target->y, target->w, target->h);
XConfigureWindow(display, target->widgets[frame_parent].widget, mask, &changes2);
//}
replacement->x = changes.x;
replacement->y = changes.y;
replacement->w = changes.width;
replacement->h = changes.height;
//printf("Target mode %d, x %d, y %d, w %d, h %d\n", mode, replacement->x, replacement->y, replacement->w, replacement->h);
XConfigureWindow(display, replacement->widgets[frame_parent].widget, mask, &changes);
resize_frame(display, replacement, workarea, themes);
resize_frame(display, target, workarea, themes);
change_frame_mode(display, replacement, target->mode, workarea, themes);
change_frame_mode(display, target, mode, workarea, themes);
stack_frame(display, target, seps);
stack_frame(display, replacement, seps);
if(replacement->state == minimized) {
target->state = minimized;
XUnmapWindow(display, target->widgets[frame_parent].widget);
XMapWindow(display, replacement->widgets[frame_parent].widget);
replacement->state = none;
}
return 1;
}
/**
@brief Implements stacking policy
@return void
**/
void
stack_frame(Display *display, struct Frame *frame, struct Separators *seps) {
XWindowChanges changes;
unsigned int mask = CWSibling | CWStackMode;
changes.stack_mode = Below;
if(frame->type == panel) {
changes.sibling = seps->panel_separator;
changes.stack_mode = Below;
// changes.sibling = seps->sinking_separator;
// changes.stack_mode = Above;
}
#ifdef SHOW_BUTTON_PRESS_EVENT
printf("stacking window %s\n", frame->window_name);
#endif
if(frame->mode == tiling) {
changes.sibling = seps->tiling_separator;
}
else if(frame->mode == floating) {
changes.sibling = seps->floating_separator;
}
else {