-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenus.c
6826 lines (5761 loc) · 157 KB
/
menus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************/
/** Copyright 1988 by Evans & Sutherland Computer Corporation, **/
/** Salt Lake City, Utah **/
/** Portions Copyright 1989 by the Massachusetts Institute of Technology **/
/** Cambridge, Massachusetts **/
/** **/
/** All Rights Reserved **/
/** **/
/** Permission to use, copy, modify, and distribute this software and **/
/** its documentation for any purpose and without fee is hereby **/
/** granted, provided that the above copyright notice appear in all **/
/** copies and that both that copyright notice and this permis- **/
/** sion notice appear in supporting documentation, and that the **/
/** names of Evans & Sutherland and M.I.T. not be used in advertising **/
/** in publicity pertaining to distribution of the software without **/
/** specific, written prior permission. **/
/** **/
/** EVANS & SUTHERLAND AND M.I.T. DISCLAIM ALL WARRANTIES WITH REGARD **/
/** TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- **/
/** ABILITY AND FITNESS, IN NO EVENT SHALL EVANS & SUTHERLAND OR **/
/** M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAM- **/
/** AGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA **/
/** OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER **/
/** TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE **/
/** OR PERFORMANCE OF THIS SOFTWARE. **/
/*****************************************************************************/
/***********************************************************************
*
* $XConsortium: menus.c,v 1.186 91/07/17 13:58:00 dave Exp $
*
* twm menu code
*
* 17-Nov-87 Thomas E. LaStrange File created
*
***********************************************************************/
#include <X11/Xmu/CharSet.h>
#include <X11/Xos.h>
#ifndef NO_REGEX_SUPPORT
#include <sys/types.h>
#include <regex.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#ifdef NEED_PROCESS_H
#include <process.h>
#endif
#include <ctype.h>
#include <sys/wait.h>
#include "twm.h"
#include "gc.h"
#include "menus.h"
#include "resize.h"
#include "events.h"
#include "image_formats.h"
#include "util.h"
#include "parse.h"
#include "gram.h"
#include "screen.h"
#include "doors.h"
#include "desktop.h"
#include "add_window.h"
#ifdef SOUND_SUPPORT
#include "sound.h"
#endif
#include "version.h"
#include "prototypes.h"
extern char *Action, *Action2, *Action3, *Action4;
extern int Context;
extern int ConstrainedMoveTime;
extern TwmWindow *ButtonWindow, *Tmp_win;
extern XEvent Event, ButtonEvent;
extern char *InitFile;
int RootFunction = F_NOFUNCTION;
MenuRoot *ActiveMenu = NULL; /* the active menu */
MenuItem *ActiveItem = NULL; /* the active menu item */
int MoveFunction = F_NOFUNCTION; /* or F_MOVE or F_FORCEMOVE */
int WindowMoved = FALSE;
int menuFromFrameOrWindowOrTitlebar = FALSE;
#ifdef SOUND_SUPPORT
int createSoundFromFunction = FALSE;
int destroySoundFromFunction = FALSE;
#endif
static void BumpWindowColormap(TwmWindow * tmp, int inc);
static void DestroyMenu(MenuRoot * menu);
static void HideIconManager(TwmWindow * tmp_win);
static void MakeMenu(MenuRoot * mr);
static void SendDeleteWindowMessage(TwmWindow * tmp, Time timestamp);
static void SendSaveYourselfMessage(TwmWindow * tmp, Time timestamp);
static void WarpClass(int next, TwmWindow * t, char *class);
static void WarpToScreen(ScreenInfo * scr, int n, int inc);
static void WarpScreenToWindow(TwmWindow * t);
static int MatchWinName(char *action, TwmWindow * t);
int ConstMove = FALSE; /* constrained move variables */
/* The location of the most recent poped-up menu.
* Used by UpdateMenu to see if the mouse has moved more than MoveDelta
* since the menu was mapped. */
static int MenuOrigX, MenuOrigY;
/* Globals used to keep track of whether the mouse has moved during
a resize function. */
int ResizeOrigX;
int ResizeOrigY;
extern int origx, origy, origWidth, origHeight;
int MenuDepth = 0; /* number of menus up */
static struct
{
int x;
int y;
} MenuOrigins[MAXMENUDEPTH];
static Cursor LastCursor;
static char *actionHack = "", *actionHack2 = "", *actionHack3 = "", *actionHack4 = "";
/*
* context bitmaps for TwmWindows menu, f.showdesktop and f.showiconmgr
*/
static int have_twmwindows = -1;
static int have_showdesktop = -1;
static int have_showlist = -1;
static void WarpAlongRing(XButtonEvent * ev, int forward);
static void Paint3DEntry(MenuRoot * mr, MenuItem * mi, int exposure);
static void Identify(TwmWindow * t);
static void PaintNormalEntry(MenuRoot * mr, MenuItem * mi, int exposure);
static TwmWindow *next_by_class(int next, TwmWindow * t, char *class);
static int warp_if_warpunmapped(TwmWindow * w, int func);
static void setup_restart(Time time);
static int FindMenuOrFuncInBindings(int contexts, MenuRoot * mr, int func);
static int FindMenuOrFuncInWindows(TwmWindow * tmp_win, int contexts, MenuRoot * mr, int func);
static int FindMenuInMenus(MenuRoot * start, MenuRoot * sought);
static int FindFuncInMenus(MenuRoot * mr, int func);
static void HideIconMgr(IconMgr * ip);
static void ShowIconMgr(IconMgr * ip);
static int do_squeezetitle(int context, int func, TwmWindow * tmp_win, SqueezeInfo * squeeze);
#undef MAX
#define MAX(x,y) ((x)>(y)?(x):(y))
#define SHADOWWIDTH 5 /* in pixels */
#ifdef TWM_USE_SPACING
#define EDGE_OFFSET (Scr->MenuFont.ascent/2)
#else
#define EDGE_OFFSET 5
#endif
#define PULLDOWNMENU_OFFSET ((Scr->RightHandSidePulldownMenus)?\
(JunkWidth - EDGE_OFFSET * 2 - Scr->pullW):\
(JunkWidth >> 1))
/***********************************************************************
*
* Procedure:
* InitMenus - initialize menu roots
*
***********************************************************************
*/
void
InitMenus(void)
{
int i, j, k;
FuncKey *key, *tmp;
for (i = 0; i < NumButtons + 1; i++)
for (j = 0; j < NUM_CONTEXTS; j++)
for (k = 0; k < MOD_SIZE; k++)
{
Scr->Mouse[MOUSELOC(i, j, k)].func = F_NOFUNCTION;
Scr->Mouse[MOUSELOC(i, j, k)].item = NULL;
}
Scr->DefaultFunction.func = F_NOFUNCTION;
Scr->WindowFunction.func = F_NOFUNCTION;
if (FirstScreen)
{
for (key = Scr->FuncKeyRoot.next; key != NULL;)
{
free(key->name);
tmp = key;
key = key->next;
free((char *)tmp);
}
Scr->FuncKeyRoot.next = NULL;
}
}
/***********************************************************************
*
* Procedure:
* AddFuncKey - add a function key to the list
*
* Inputs:
* name - the name of the key
* cont - the context to look for the key press in
* mods - modifier keys that need to be pressed
* func - the function to perform
* win_name- the window name (if any)
* action - the action string associated with the function (if any)
*
***********************************************************************
*/
Bool
AddFuncKey(char *name, int cont, int mods, int func, char *win_name, char *action, char *action2, char *action3, char *action4)
{
FuncKey *tmp;
KeySym keysym;
KeyCode keycode;
/*
* Don't let a 0 keycode go through, since that means AnyKey to the
* XGrabKey call in GrabKeys().
*/
if ((keysym = XStringToKeysym(name)) == NoSymbol || (keycode = XKeysymToKeycode(dpy, keysym)) == 0)
{
return False;
}
/* see if there already is a key defined for this context */
for (tmp = Scr->FuncKeyRoot.next; tmp != NULL; tmp = tmp->next)
{
if (tmp->keysym == keysym && tmp->cont == cont && tmp->mods == mods)
break;
}
if (tmp == NULL)
{
tmp = (FuncKey *) malloc(sizeof(FuncKey));
tmp->next = Scr->FuncKeyRoot.next;
Scr->FuncKeyRoot.next = tmp;
}
tmp->name = name;
tmp->keysym = keysym;
tmp->keycode = keycode;
tmp->cont = cont;
tmp->mods = mods;
tmp->func = func;
tmp->win_name = win_name;
tmp->action = action;
tmp->action2 = action2;
tmp->action3 = action3;
tmp->action4 = action4;
return True;
}
int
CreateTitleButton(char *name, int func, char *action, char *action2, char *action3, char *action4, MenuRoot * menuroot, Bool rightside, Bool append)
{
TitleButton *tb = (TitleButton *) malloc(sizeof(TitleButton));
if (!tb)
{
fprintf(stderr, "%s: unable to allocate %lu bytes for title button\n", ProgramName, (unsigned long int) sizeof(TitleButton));
return 0;
}
tb->next = NULL;
tb->name = name; /* note that we are not copying */
tb->width = 0; /* see InitTitlebarButtons */
tb->height = 0; /* ditto */
tb->func = func;
tb->action = action;
tb->action2 = action2;
tb->action3 = action3;
tb->action4 = action4;
tb->menuroot = menuroot;
tb->rightside = rightside;
if (rightside)
{
Scr->TBInfo.nright++;
}
else
{
Scr->TBInfo.nleft++;
}
/*
* Cases for list:
*
* 1. empty list, prepend left put at head of list
* 2. append left, prepend right put in between left and right
* 3. append right put at tail of list
*
* Do not refer to widths and heights yet since buttons not created
* (since fonts not loaded and heights not known).
*/
if ((!Scr->TBInfo.head) || ((!append) && (!rightside)))
{ /* 1 */
tb->next = Scr->TBInfo.head;
Scr->TBInfo.head = tb;
}
else if (append && rightside)
{ /* 3 */
register TitleButton *t;
for (t = Scr->TBInfo.head; t->next; t = t->next)
; /* SUPPRESS 530 */
t->next = tb;
tb->next = NULL;
}
else
{ /* 2 */
register TitleButton *t, *prev = NULL;
for (t = Scr->TBInfo.head; t && !t->rightside; t = t->next)
prev = t;
if (prev)
{
tb->next = prev->next;
prev->next = tb;
}
else
{
tb->next = Scr->TBInfo.head;
Scr->TBInfo.head = tb;
}
}
return 1;
}
/*
* InitTitlebarButtons - Do all the necessary stuff to load in a titlebar
* button. If we can't find the button, then put in a question; if we can't
* find the question mark, something is wrong and we are probably going to be
* in trouble later on.
*/
int
InitTitlebarButtons(void)
{
Image *image;
TitleButton *tb;
int h, height;
/*
* initialize dimensions
*/
Scr->TBInfo.width = (Scr->TitleHeight - 2 * (Scr->FramePadding + Scr->ButtonIndent));
Scr->TBInfo.pad = Scr->TitlePadding;
h = Scr->TBInfo.width - 2 * Scr->TBInfo.border;
if (!(h & 1))
h--;
height = h;
/*
* add in some useful buttons and bindings so that novices can still
* use the system. -- modified by DSE
*/
if (!Scr->NoDefaultTitleButtons)
{
/* insert extra buttons */
if (Scr->TitleBevelWidth > 0)
{
if (!CreateTitleButton(TBPM_3DDOT, F_ICONIFY, "", 0, 0, 0, (MenuRoot *) NULL, False, False))
fprintf(stderr, "%s: unable to add iconify button\n", ProgramName);
if (!CreateTitleButton(TBPM_3DRESIZE, F_RESIZE, "", 0, 0, 0, (MenuRoot *) NULL, True, True))
fprintf(stderr, "%s: unable to add resize button\n", ProgramName);
}
else
{
if (!CreateTitleButton(TBPM_ICONIFY, F_ICONIFY, "", 0, 0, 0, (MenuRoot *) NULL, False, False))
fprintf(stderr, "%s: unable to add iconify button\n", ProgramName);
if (!CreateTitleButton(TBPM_RESIZE, F_RESIZE, "", 0, 0, 0, (MenuRoot *) NULL, True, True))
fprintf(stderr, "%s: unable to add resize button\n", ProgramName);
}
}
if (!Scr->NoDefaultMouseOrKeyboardBindings)
{
AddDefaultBindings();
}
ComputeCommonTitleOffsets();
/*
* load in images and do appropriate centering
*/
for (tb = Scr->TBInfo.head; tb; tb = tb->next)
{
image = GetImage(tb->name, h, h, Scr->ButtonBevelWidth * 2, (Scr->ButtonColorIsFrame) ? Scr->BorderColorC : Scr->TitleC);
tb->width = image->width;
height = tb->height = image->height;
tb->dstx = (h - tb->width + 1) / 2;
if (tb->dstx < 0)
{ /* clip to minimize copying */
tb->srcx = -(tb->dstx);
tb->width = h;
tb->dstx = 0;
}
else
{
tb->srcx = 0;
}
tb->dsty = (h - tb->height + 1) / 2;
if (tb->dsty < 0)
{
tb->srcy = -(tb->dsty);
tb->height = h;
tb->dsty = 0;
}
else
{
tb->srcy = 0;
}
} /* for(...) */
return (height > h) ? height : h;
/* ...end of moved */
}
void
SetMenuIconPixmap(char *filename)
{
Scr->menuIconName = filename;
}
void
PaintEntry(MenuRoot * mr, MenuItem * mi, int exposure)
{
if (Scr->MenuBevelWidth > 0)
Paint3DEntry(mr, mi, exposure);
else
PaintNormalEntry(mr, mi, exposure);
}
void
Paint3DEntry(MenuRoot * mr, MenuItem * mi, int exposure)
{
int y_offset;
int text_y;
GC gc;
y_offset = (mi->item_num - mr->top) * Scr->EntryHeight + Scr->MenuBevelWidth;
#ifdef TWM_USE_SPACING
if (mi->func == F_TITLE)
text_y = y_offset + (((Scr->EntryHeight - Scr->MenuTitleFont.height) / 2) + Scr->MenuTitleFont.y);
else
#endif
text_y = y_offset + (((Scr->EntryHeight - Scr->MenuFont.height) / 2) + Scr->MenuFont.y);
if (mi->func != F_TITLE)
{
int x, y;
if (mi->state)
{
Draw3DBorder(mr->w.win, Scr->MenuBevelWidth, y_offset + 1, mr->width - 2 * Scr->MenuBevelWidth, Scr->EntryHeight - 1, 1,
mi->highlight, off, True, False);
MyFont_DrawString(dpy, &mr->w, &Scr->MenuFont, &mi->highlight, mi->x + Scr->MenuBevelWidth, text_y, mi->item, mi->strlen);
#ifdef TWM_USE_XFT
/*
* initialise NormalGC with color for XCopyPlane() later
* ("non-TWM_USE_XFT" does it in MyFont_DrawString() already):
*/
if (Scr->use_xft > 0)
FB(Scr, mi->highlight.fore, mi->highlight.back);
#endif
gc = Scr->NormalGC;
}
else
{
if (mi->user_colors || !exposure)
{
XSetForeground(dpy, Scr->NormalGC, mi->normal.back);
XFillRectangle(dpy, mr->w.win, Scr->NormalGC, Scr->MenuBevelWidth, y_offset + 1,
mr->width - 2 * Scr->MenuBevelWidth, Scr->EntryHeight - 1);
#ifdef TWM_USE_XFT
if (Scr->use_xft > 0)
FB(Scr, mi->normal.fore, mi->normal.back);
#endif
gc = Scr->NormalGC;
}
else
{
gc = Scr->MenuGC;
}
/* MyFont_DrawString() sets NormalGC: */
MyFont_DrawString(dpy, &mr->w, &Scr->MenuFont, &mi->normal, mi->x + Scr->MenuBevelWidth, text_y, mi->item, mi->strlen);
if (mi->separated)
{
if (!Scr->BeNiceToColormap)
{
FB(Scr, Scr->MenuC.shadd, Scr->MenuC.shadc);
XDrawLine(dpy, mr->w.win, Scr->NormalGC, Scr->MenuBevelWidth + 1, y_offset + Scr->EntryHeight - 1,
mr->width - Scr->MenuBevelWidth - 3, y_offset + Scr->EntryHeight - 1);
}
FB(Scr, Scr->MenuC.shadc, Scr->MenuC.shadd);
XDrawLine(dpy, mr->w.win, Scr->NormalGC, Scr->MenuBevelWidth + 2, y_offset + Scr->EntryHeight,
mr->width - Scr->MenuBevelWidth - 2, y_offset + Scr->EntryHeight);
}
}
if (mi->func == F_MENU)
{
Image *image;
Pixel back;
back = Scr->MenuC.back;
if (mi->state)
Scr->MenuC.back = mi->highlight.back;
else
Scr->MenuC.back = mi->normal.back;
Scr->pullW = Scr->pullH = Scr->MenuFont.height;
image = GetImage(Scr->menuIconName, Scr->pullW, Scr->pullH, 0, Scr->MenuC);
Scr->MenuC.back = back;
x = mr->width - Scr->pullW - Scr->MenuBevelWidth - EDGE_OFFSET;
y = y_offset + ((Scr->EntryHeight - Scr->pullH) / 2) + 1;
XCopyArea(dpy, image->pixmap, mr->w.win, gc, 0, 0, Scr->pullW, Scr->pullH, x, y);
}
}
else
{
Draw3DBorder(mr->w.win, Scr->MenuBevelWidth, y_offset, mr->width - 2 * Scr->MenuBevelWidth, Scr->EntryHeight + 1, 1,
mi->normal, off, True, False);
MyFont_DrawString(dpy, &mr->w, &Scr->MenuTitleFont, &mi->normal, mi->x, text_y, mi->item, mi->strlen);
}
}
void
PaintNormalEntry(MenuRoot * mr, MenuItem * mi, int exposure)
{
int y_offset;
int text_y;
GC gc;
y_offset = (mi->item_num - mr->top) * Scr->EntryHeight;
#ifdef TWM_USE_SPACING
if (mi->func == F_TITLE)
text_y = y_offset + (((Scr->EntryHeight - Scr->MenuTitleFont.height) / 2) + Scr->MenuTitleFont.y);
else
#endif
text_y = y_offset + (((Scr->EntryHeight - Scr->MenuFont.height) / 2) + Scr->MenuFont.y);
if (mi->func != F_TITLE)
{
int x, y;
if (mi->state)
{
XSetForeground(dpy, Scr->NormalGC, mi->highlight.back);
XFillRectangle(dpy, mr->w.win, Scr->NormalGC, 0, y_offset, mr->width, Scr->EntryHeight);
MyFont_DrawString(dpy, &mr->w, &Scr->MenuFont, &mi->highlight, mi->x, text_y, mi->item, mi->strlen);
#ifdef TWM_USE_XFT
/*
* initialise NormalGC with color for XCopyPlane() later
* ("non-TWM_USE_XFT" does it in MyFont_DrawString() already):
*/
if (Scr->use_xft > 0)
FB(Scr, mi->highlight.fore, mi->highlight.back);
#endif
gc = Scr->NormalGC;
}
else
{
if (mi->user_colors || !exposure)
{
XSetForeground(dpy, Scr->NormalGC, mi->normal.back);
XFillRectangle(dpy, mr->w.win, Scr->NormalGC, 0, y_offset, mr->width, Scr->EntryHeight);
#ifdef TWM_USE_XFT
if (Scr->use_xft > 0)
FB(Scr, mi->normal.fore, mi->normal.back);
#endif
gc = Scr->NormalGC;
}
else
{
gc = Scr->MenuGC;
}
/* MyFont_DrawString() sets NormalGC: */
MyFont_DrawString(dpy, &mr->w, &Scr->MenuFont, &mi->normal, mi->x, text_y, mi->item, mi->strlen);
if (mi->separated)
XDrawLine(dpy, mr->w.win, gc, 0, y_offset + Scr->EntryHeight - 1, mr->width, y_offset + Scr->EntryHeight - 1);
}
if (mi->func == F_MENU)
{
Image *image;
ColorPair cp;
cp.fore = cp.back = Scr->MenuC.back;
if (strncmp(Scr->menuIconName, ":xpm:", 5) != 0)
{
cp.fore = Scr->MenuC.fore;
Scr->MenuC.fore = (mi->state) ? mi->highlight.fore : mi->normal.fore;
Scr->MenuC.back = (mi->state) ? mi->highlight.back : mi->normal.back;
}
else
Scr->MenuC.back = (mi->state) ? mi->highlight.back : mi->normal.back;
Scr->pullW = Scr->pullH = Scr->MenuFont.height;
image = GetImage(Scr->menuIconName, Scr->pullW, Scr->pullH, 0, Scr->MenuC);
Scr->MenuC.back = cp.back;
if (strncmp(Scr->menuIconName, ":xpm:", 5) != 0)
Scr->MenuC.fore = cp.fore;
x = mr->width - Scr->pullW - EDGE_OFFSET;
y = y_offset + ((Scr->EntryHeight - Scr->pullH) / 2);
XCopyArea(dpy, image->pixmap, mr->w.win, gc, 0, 0, Scr->pullW, Scr->pullH, x, y);
}
}
else
{
int y;
XSetForeground(dpy, Scr->NormalGC, mi->normal.back);
/* fill the rectangle with the title background color */
XFillRectangle(dpy, mr->w.win, Scr->NormalGC, 0, y_offset, mr->width, Scr->EntryHeight);
/* Draw separators only if menu has borders: */
if (Scr->BorderWidth > 0 || Scr->MenuBevelWidth > 0)
{
XSetForeground(dpy, Scr->NormalGC, mi->normal.fore);
/* now draw the dividing lines */
if (y_offset)
XDrawLine(dpy, mr->w.win, Scr->NormalGC, 0, y_offset, mr->width, y_offset);
y = ((mi->item_num + 1) * Scr->EntryHeight) - 1;
XDrawLine(dpy, mr->w.win, Scr->NormalGC, 0, y, mr->width, y);
}
/* finally render the title */
MyFont_DrawString(dpy, &mr->w, &Scr->MenuTitleFont, &mi->normal, mi->x, text_y, mi->item, mi->strlen);
}
}
void
PaintMenu(MenuRoot * mr, XEvent * e)
{
MenuItem *mi;
int y_offset;
if (Scr->MenuBevelWidth > 0)
{
Draw3DBorder(mr->w.win, 0, 0, mr->width, mr->height, Scr->MenuBevelWidth, Scr->MenuC, off, False, False);
}
for (mi = mr->first; mi != NULL; mi = mi->next)
{
if (mi->item_num < mr->top)
continue;
y_offset = (mi->item_num - mr->top) * Scr->EntryHeight;
if (y_offset + Scr->EntryHeight > mr->height)
break;
/* some servers want the previous entry redrawn - djhjr - 10/24/00 */
if (Scr->MenuBevelWidth > 0)
y_offset += Scr->EntryHeight;
/*
* Be smart about handling the expose, redraw only the entries
* that we need to.
*/
/* those servers want the next entry redrawn, too - djhjr - 10/24/00 */
if (e->xexpose.y < (y_offset + Scr->EntryHeight) &&
(e->xexpose.y + e->xexpose.height) > y_offset - ((mr->shadow) ? Scr->EntryHeight : 0))
{
#ifdef TWM_USE_XFT
if (Scr->use_xft > 0)
PaintEntry(mr, mi, False); /* enforce clear area first */
else
#endif
PaintEntry(mr, mi, True);
}
}
XSync(dpy, False);
}
static Bool fromMenu;
extern int GlobalFirstTime;
/*
* Process events inside a menu and its submenus until the menu is exited.
*/
void
UpdateMenu(void)
{
MenuItem *mi;
int i, x, y, x_root, y_root, entry;
int done;
MenuItem *badItem = NULL;
static int firstTime = True;
fromMenu = TRUE;
while (TRUE)
{ /* block until there is an event */
XNextEvent(dpy, &Event);
if (!DispatchEvent())
continue;
if (Event.type == ButtonRelease)
{
if (Scr->StayUpMenus)
{
if (firstTime == True)
{ /* it was the first release of the button */
firstTime = False;
}
else
{ /* thats the second we need to return now */
firstTime = True;
menuFromFrameOrWindowOrTitlebar = FALSE;
fromMenu = FALSE;
return;
}
}
else
{ /* not stay-up */
menuFromFrameOrWindowOrTitlebar = FALSE;
fromMenu = FALSE;
return;
}
}
if (Cancel)
return;
if (Event.type != MotionNotify)
continue;
/* If we haven't received the enter event yet, wait for it. */
if (!ActiveMenu || !ActiveMenu->entered)
continue;
/* done will be set true if all required item highlighting/unhighlighting
* has been completed. */
done = FALSE;
/* Find where the mouse is. */
XQueryPointer(dpy, ActiveMenu->w.win, &JunkRoot, &JunkChild, &x_root, &y_root, &x, &y, &JunkMask);
if (!ActiveItem)
/* If the mouse hasn't moved MoveDelta since the menu was popped-up,
* ignore the motion. */
if (abs(x_root - MenuOrigX) < Scr->MoveDelta && abs(y_root - MenuOrigY) < Scr->MoveDelta)
continue;
/* Set Scr as an implicit argument for operations. */
XFindContext(dpy, ActiveMenu->w.win, ScreenContext, (caddr_t *) & Scr);
/* Remove the effect of MenuBevel{Width,Height}. */
JunkWidth = ActiveMenu->width;
JunkHeight = ActiveMenu->height;
if (Scr->MenuBevelWidth > 0)
{
x -= Scr->MenuBevelWidth;
y -= Scr->MenuBevelWidth;
JunkWidth -= 2 * Scr->MenuBevelWidth;
JunkHeight -= Scr->MenuBevelWidth;
}
if ((x < 0 || y < 0 || x >= JunkWidth || y >= JunkHeight) ||
(ActiveMenu->too_tall && (y < Scr->MenuScrollBorderWidth || y > JunkHeight - Scr->MenuScrollBorderWidth)))
{
/* Mouse is outside the menu itself, or is in the scrolling border.
* Check for scrolling actions. */
/* Exit the active item, if there is one. */
if (ActiveItem && ActiveItem->func != F_TITLE)
{
ActiveItem->state = 0;
PaintEntry(ActiveMenu, ActiveItem, False);
}
ActiveItem = NULL;
/* menu scrolling - djhjr - 5/22/00 */
if (ActiveMenu->too_tall && x >= 0 && x < JunkWidth)
{
short j = ActiveMenu->top;
if (y < Scr->MenuScrollBorderWidth)
{
if (ActiveMenu->top - Scr->MenuScrollJump < 0)
continue;
else
j -= Scr->MenuScrollJump;
}
else if (y > JunkHeight - Scr->MenuScrollBorderWidth)
{
int k = JunkHeight / Scr->EntryHeight;
if (ActiveMenu->top + k >= ActiveMenu->items)
continue;
else
j += Scr->MenuScrollJump;
}
if (ActiveMenu->top != j)
{
ActiveMenu->top = j;
XClearArea(dpy, ActiveMenu->w.win, 0, 0, 0, 0, True);
}
}
continue;
}
/* Look for the entry that the mouse is in. */
entry = (y / Scr->EntryHeight) + ActiveMenu->top;
for (i = 0, mi = ActiveMenu->first; mi != NULL; i++, mi = mi->next)
{
if (i == entry)
break;
}
/* if there is an active item, we might have to turn it off */
if (ActiveItem)
{
/* is the active item the one we are on ? */
if (ActiveItem->item_num == entry && ActiveItem->state)
done = TRUE;
/* if we weren't on the active entry, let's turn the old
* active one off
*/
if (!done && ActiveItem->func != F_TITLE)
{
ActiveItem->state = 0;
PaintEntry(ActiveMenu, ActiveItem, False);
}
}
if (ActiveMenu->too_tall && y + Scr->EntryHeight > JunkHeight)
continue;
/* if we weren't on the active item, change the active item and turn
* it on
*/
if (!done)
{
ActiveItem = mi;
if (ActiveItem && ActiveItem->func != F_TITLE && !ActiveItem->state)
{
ActiveItem->state = 1;
PaintEntry(ActiveMenu, ActiveItem, False);
if (Scr->StayUpOptionalMenus)
GlobalFirstTime = firstTime = False;
}
}
/* now check to see if we were over the arrow of a pull right entry */
if (ActiveItem && ActiveItem->func == F_MENU && (x > PULLDOWNMENU_OFFSET))
{
/* Pop up the submenu. */
MenuRoot *save = ActiveMenu;
int savex = MenuOrigins[MenuDepth - 1].x;
int savey = MenuOrigins[MenuDepth - 1].y;
if (MenuDepth < MAXMENUDEPTH)
{
PopUpMenu(ActiveItem->sub, (savex + PULLDOWNMENU_OFFSET), (savey + ActiveItem->item_num * Scr->EntryHeight), False);
}
else if (!badItem)
{
DoAudible();
badItem = ActiveItem;
}
/* if the menu did get popped up, unhighlight the active item */
if (save != ActiveMenu && ActiveItem->state)
{
ActiveItem->state = 0;
PaintEntry(save, ActiveItem, False);
ActiveItem = NULL;
}
}
if (badItem != ActiveItem)
badItem = NULL;
XFlush(dpy);
}
}
/***********************************************************************
*
* Procedure:
* NewMenuRoot - create a new menu root
*
* Returned Value:
* (MenuRoot *)
*
* Inputs:
* name - the name of the menu root
*
***********************************************************************
*/
MenuRoot *
NewMenuRoot(char *name)
{
MenuRoot *tmp;
#define UNUSED_PIXEL ((unsigned long) (~0)) /* more than 24 bits */
tmp = (MenuRoot *) malloc(sizeof(MenuRoot));
tmp->highlight.fore = UNUSED_PIXEL;
tmp->highlight.back = UNUSED_PIXEL;
tmp->name = name;
tmp->prev = NULL;
tmp->first = NULL;
tmp->last = NULL;
tmp->items = 0;
tmp->width = 0;
tmp->mapped = NEVER_MAPPED;
tmp->pull = FALSE;
tmp->w.win = None;
tmp->shadow = None;
tmp->real_menu = FALSE;
tmp->too_tall = 0;
tmp->top = 0;
if (Scr->MenuList == NULL)
{
Scr->MenuList = tmp;
Scr->MenuList->next = NULL;
}