1 /* menu.c- generic menu, used for root menu, application menus etc.
3 * Window Maker window manager
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
26 #include <X11/Xutil.h>
27 #include <X11/keysym.h>
34 #include "WindowMaker.h"
44 /****** Global Variables ******/
46 extern Cursor wCursor
[WCUR_LAST
];
48 extern XContext wWinContext
;
50 extern WPreferences wPreferences
;
52 #define MOD_MASK wPreferences.modifier_mask
54 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
55 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
59 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
60 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
63 /***** Local Stuff ******/
68 } menuScrollParameters
[5] = {
69 {MENU_SCROLL_STEPS_UF
, MENU_SCROLL_DELAY_UF
},
70 {MENU_SCROLL_STEPS_F
, MENU_SCROLL_DELAY_F
},
71 {MENU_SCROLL_STEPS_M
, MENU_SCROLL_DELAY_M
},
72 {MENU_SCROLL_STEPS_S
, MENU_SCROLL_DELAY_S
},
73 {MENU_SCROLL_STEPS_U
, MENU_SCROLL_DELAY_U
}};
76 static void menuMouseDown(WObjDescriptor
*desc
, XEvent
*event
);
77 static void menuExpose(WObjDescriptor
*desc
, XEvent
*event
);
79 static void menuTitleDoubleClick(WCoreWindow
*sender
, void *data
, XEvent
*event
);
80 static void menuTitleMouseDown(WCoreWindow
*sender
, void *data
, XEvent
*event
);
82 static void menuCloseClick(WCoreWindow
*sender
, void *data
, XEvent
*event
);
86 static void selectEntry(WMenu
*menu
, int entry_no
);
87 static void closeCascade(WMenu
*menu
);
92 *----------------------------------------------------------------------
94 * Creates a new empty menu with the specified title. If main_menu
95 * is True, the created menu will be a main menu, which has some special
96 * properties such as being placed over other normal menus.
97 * If title is NULL, the menu will have no titlebar.
101 *----------------------------------------------------------------------
104 wMenuCreate(WScreen
*screen
, char *title
, int main_menu
)
107 static int brother
=0;
110 menu
= wmalloc(sizeof(WMenu
));
112 memset(menu
, 0, sizeof(WMenu
));
114 #ifdef SINGLE_MENULEVEL
115 tmp
= WMSubmenuLevel
;
117 tmp
= (main_menu
? WMMainMenuLevel
: WMSubmenuLevel
);
120 flags
= WFF_SINGLE_STATE
;
122 flags
|= WFF_TITLEBAR
|WFF_RIGHT_BUTTON
;
123 menu
->flags
.titled
= 1;
126 wFrameWindowCreate(screen
, tmp
, 8, 2, 1, 1, flags
,
127 screen
->menu_title_texture
, NULL
,
128 screen
->menu_title_pixel
, &screen
->menu_title_gc
,
129 &screen
->menu_title_font
);
131 menu
->frame
->core
->descriptor
.parent
= menu
;
132 menu
->frame
->core
->descriptor
.parent_type
= WCLASS_MENU
;
133 menu
->frame
->core
->descriptor
.handle_mousedown
= menuMouseDown
;
135 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
138 menu
->frame
->title
= wstrdup(title
);
141 menu
->frame
->flags
.justification
= WTJ_LEFT
;
143 menu
->frame
->rbutton_image
= screen
->b_pixmaps
[WBUT_CLOSE
];
146 menu
->alloced_entries
= 0;
147 menu
->selected_entry
= -1;
148 menu
->entries
= NULL
;
150 menu
->frame_x
= screen
->app_menu_x
;
151 menu
->frame_y
= screen
->app_menu_y
;
153 menu
->frame
->child
= menu
;
155 menu
->flags
.lowered
= 0;
159 /* setup object descriptors */
160 menu
->frame
->on_mousedown_titlebar
= menuTitleMouseDown
;
161 menu
->frame
->on_dblclick_titlebar
= menuTitleDoubleClick
;
164 menu
->frame
->on_click_right
= menuCloseClick
;
167 menu
->menu
= wCoreCreate(menu
->frame
->core
, 0, menu
->frame
->top_width
,
168 menu
->frame
->core
->width
, 10);
170 menu
->menu
->descriptor
.parent
= menu
;
171 menu
->menu
->descriptor
.parent_type
= WCLASS_MENU
;
172 menu
->menu
->descriptor
.handle_expose
= menuExpose
;
173 menu
->menu
->descriptor
.handle_mousedown
= menuMouseDown
;
175 menu
->menu_texture_data
= None
;
177 XMapWindow(dpy
, menu
->menu
->window
);
183 menu
->brother
= wMenuCreate(screen
, title
, main_menu
);
185 menu
->brother
->flags
.brother
= 1;
186 menu
->brother
->brother
= menu
;
197 wMenuCreateForApp(WScreen
*screen
, char *title
, int main_menu
)
201 menu
= wMenuCreate(screen
, title
, main_menu
);
204 menu
->flags
.app_menu
= 1;
205 menu
->brother
->flags
.app_menu
= 1;
213 insertEntry(WMenu
*menu
, WMenuEntry
*entry
, int index
)
217 for (i
=menu
->entry_no
; i
>index
; i
--) {
218 menu
->entries
[i
]->order
++;
219 menu
->entries
[i
+1]=menu
->entries
[i
];
221 menu
->entries
[index
] = entry
;
226 wMenuRefresh(WMenu
*menu
, int flags
)
230 if (flags
& MR_TEXT_BACK
) {
231 menu
->frame
->flags
.need_texture_remake
= 1;
234 if (flags
& (MR_RESIZED
|MR_TITLE_TEXT
)) {
235 menu
->flags
.realized
= 0;
240 if (menu
->flags
.titled
)
241 wFrameWindowPaint(menu
->frame
);
243 if (!menu
->flags
.brother
) {
245 wMenuRefresh(menu
->brother
, flags
);
247 for (i
=0; i
< menu
->cascade_no
; i
++) {
248 if (!menu
->cascades
[i
]->flags
.brother
)
249 wMenuRefresh(menu
->cascades
[i
], flags
);
251 wMenuRefresh(menu
->cascades
[i
]->brother
, flags
);
259 wMenuInsertCallback(WMenu
*menu
, int index
, char *text
,
260 void (*callback
)(WMenu
*menu
, WMenuEntry
*entry
),
267 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
272 assert(menu
->flags
.brother
==0);
273 menu
->flags
.realized
= 0;
274 menu
->brother
->flags
.realized
= 0;
276 /* reallocate array if it's too small */
277 if (menu
->entry_no
>= menu
->alloced_entries
) {
280 puts("doing wrealloc()");
282 tmp
= wrealloc(menu
->entries
,
283 sizeof(WMenuEntry
)*(menu
->alloced_entries
+5));
285 wwarning(_("wrealloc() failed while trying to add menu item"));
290 menu
->alloced_entries
+= 5;
292 menu
->brother
->entries
= tmp
;
293 menu
->brother
->alloced_entries
= menu
->alloced_entries
;
295 entry
= wmalloc(sizeof(WMenuEntry
));
296 memset(entry
, 0, sizeof(WMenuEntry
));
297 entry
->flags
.enabled
= 1;
298 entry
->text
= wstrdup(text
);
300 entry
->clientdata
= clientdata
;
301 entry
->callback
= callback
;
302 if (index
<0 || index
>=menu
->entry_no
) {
303 entry
->order
= menu
->entry_no
;
304 menu
->entries
[menu
->entry_no
] = entry
;
306 entry
->order
= index
;
307 insertEntry(menu
, entry
, index
);
311 menu
->brother
->entry_no
= menu
->entry_no
;
319 wMenuEntrySetCascade(WMenu
*menu
, WMenuEntry
*entry
, WMenu
*cascade
)
321 WMenu
*brother
= menu
->brother
;
324 assert(menu
->flags
.brother
==0);
326 if (entry
->cascade
>=0) {
327 menu
->flags
.realized
= 0;
328 brother
->flags
.realized
= 0;
331 cascade
->parent
= menu
;
333 cascade
->brother
->parent
= brother
;
336 for (i
=0; i
<menu
->cascade_no
; i
++) {
337 if (menu
->cascades
[i
]==NULL
) {
338 menu
->cascades
[i
] = cascade
;
339 brother
->cascades
[i
] = cascade
->brother
;
346 entry
->cascade
= menu
->cascade_no
;
348 menu
->cascades
= wrealloc(menu
->cascades
,
349 sizeof(WMenu
)*(menu
->cascade_no
+1));
350 menu
->cascades
[menu
->cascade_no
++] = cascade
;
353 brother
->cascades
= wrealloc(brother
->cascades
,
354 sizeof(WMenu
)*(brother
->cascade_no
+1));
355 brother
->cascades
[brother
->cascade_no
++] = cascade
->brother
;
359 if (menu
->flags
.lowered
) {
361 cascade
->flags
.lowered
= 1;
362 ChangeStackingLevel(cascade
->frame
->core
, WMNormalLevel
);
364 cascade
->brother
->flags
.lowered
= 1;
365 ChangeStackingLevel(cascade
->brother
->frame
->core
, WMNormalLevel
);
368 if (!menu
->flags
.realized
)
374 wMenuEntryRemoveCascade(WMenu
*menu
, WMenuEntry
*entry
)
376 assert(menu
->flags
.brother
==0);
378 /* destroy cascade menu */
379 if (entry
->cascade
>=0 && menu
->cascades
380 && menu
->cascades
[entry
->cascade
]!=NULL
) {
382 wMenuDestroy(menu
->cascades
[entry
->cascade
], True
);
384 menu
->cascades
[entry
->cascade
] = NULL
;
385 menu
->brother
->cascades
[entry
->cascade
] = NULL
;
393 wMenuRemoveItem(WMenu
*menu
, int index
)
397 if (menu
->flags
.brother
) {
398 wMenuRemoveItem(menu
->brother
, index
);
402 if (index
>=menu
->entry_no
) return;
404 /* destroy cascade menu */
405 wMenuEntryRemoveCascade(menu
, menu
->entries
[index
]);
407 /* destroy unshared data */
409 if (menu
->entries
[index
]->text
)
410 free(menu
->entries
[index
]->text
);
412 if (menu
->entries
[index
]->rtext
)
413 free(menu
->entries
[index
]->rtext
);
415 if (menu
->entries
[index
]->free_cdata
&& menu
->entries
[index
]->clientdata
)
416 (*menu
->entries
[index
]->free_cdata
)(menu
->entries
[index
]->clientdata
);
418 free(menu
->entries
[index
]);
420 for (i
=index
; i
<menu
->entry_no
-1; i
++) {
421 menu
->entries
[i
+1]->order
--;
422 menu
->entries
[i
]=menu
->entries
[i
+1];
425 menu
->brother
->entry_no
--;
431 wMenuRealize(WMenu
*menu
)
434 int width
, rwidth
, mrwidth
, mwidth
;
435 int theight
, twidth
, eheight
;
436 WScreen
*scr
= menu
->frame
->screen_ptr
;
437 static int brother_done
=0;
442 wMenuRealize(menu
->brother
);
446 flags
= WFF_SINGLE_STATE
;
447 if (menu
->flags
.titled
)
448 flags
|= WFF_TITLEBAR
|WFF_RIGHT_BUTTON
;
450 wFrameWindowUpdateBorders(menu
->frame
, flags
);
452 if (menu
->flags
.titled
) {
453 twidth
= wTextWidth(scr
->menu_title_font
->font
, menu
->frame
->title
,
454 strlen(menu
->frame
->title
));
455 theight
= menu
->frame
->top_width
;
456 twidth
+= theight
+ theight
/2 + (wPreferences
.new_style
? 8 : 0);
461 eheight
= scr
->menu_entry_font
->height
+ 6;
462 menu
->entry_height
= eheight
;
465 for (i
=0; i
<menu
->entry_no
; i
++) {
468 /* search widest text */
469 text
= menu
->entries
[i
]->text
;
470 width
= wTextWidth(scr
->menu_entry_font
->font
, text
, strlen(text
))+12;
472 if (menu
->entries
[i
]->flags
.indicator
) {
473 width
+= MENU_INDICATOR_SPACE
;
479 /* search widest text on right */
480 text
= menu
->entries
[i
]->rtext
;
482 rwidth
= wTextWidth(scr
->menu_entry_font
->font
, text
,
484 else if (menu
->entries
[i
]->cascade
>=0)
489 if (rwidth
> mrwidth
)
497 wCoreConfigure(menu
->menu
, 0, theight
, mwidth
, menu
->entry_no
*eheight
-1);
499 wFrameWindowResizeInternal(menu
->frame
, mwidth
, menu
->entry_no
*eheight
-1);
502 /* setup background texture */
503 switch (scr
->menu_item_texture
->any
.type
) {
507 case WTEX_MHGRADIENT
:
508 case WTEX_MVGRADIENT
:
509 case WTEX_MDGRADIENT
:
511 if (!menu
->flags
.brother
) {
512 FREE_PIXMAP(menu
->menu_texture_data
);
514 wTextureRender(scr
, scr
->menu_item_texture
,
515 &menu
->menu_texture_data
, menu
->menu
->width
,
516 menu
->entry_height
, WREL_MENUENTRY
);
517 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
,
518 menu
->menu_texture_data
);
519 XClearWindow(dpy
, menu
->menu
->window
);
521 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
,
522 menu
->menu_texture_data
);
523 XClearWindow(dpy
, menu
->brother
->menu
->window
);
528 XSetWindowBackground(dpy
, menu
->menu
->window
,
529 scr
->menu_item_texture
->any
.color
.pixel
);
530 XClearWindow(dpy
, menu
->menu
->window
);
533 menu
->flags
.realized
= 1;
535 if (menu
->flags
.mapped
)
537 if (menu
->brother
->flags
.mapped
)
538 wMenuPaint(menu
->brother
);
543 wMenuDestroy(WMenu
*menu
, int recurse
)
547 /* remove any pending timers */
549 WMDeleteTimerHandler(menu
->timer
);
552 /* call destroy handler */
553 if (menu
->on_destroy
)
554 (*menu
->on_destroy
)(menu
);
556 /* Destroy items if this menu own them. If this is the "brother" menu,
557 * leave them alone as it is shared by them.
559 if (!menu
->flags
.brother
) {
560 for (i
=0; i
<menu
->entry_no
; i
++) {
562 free(menu
->entries
[i
]->text
);
564 if (menu
->entries
[i
]->rtext
)
565 free(menu
->entries
[i
]->rtext
);
567 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
568 (*menu
->entries
[i
]->free_cdata
)(menu
->entries
[i
]->clientdata
);
570 free(menu
->entries
[i
]);
574 for (i
=0; i
<menu
->cascade_no
; i
++) {
575 if (menu
->cascades
[i
]) {
576 if (menu
->cascades
[i
]->flags
.brother
)
577 wMenuDestroy(menu
->cascades
[i
]->brother
, recurse
);
579 wMenuDestroy(menu
->cascades
[i
], recurse
);
589 FREE_PIXMAP(menu
->menu_texture_data
);
592 free(menu
->cascades
);
594 wCoreDestroy(menu
->menu
);
595 wFrameWindowDestroy(menu
->frame
);
597 /* destroy copy of this menu */
598 if (!menu
->flags
.brother
&& menu
->brother
)
599 wMenuDestroy(menu
->brother
, False
);
606 drawFrame(WScreen
*scr
, Window win
, int y
, int w
, int h
)
617 segs
[1].y2
= y
+ h
- 2;
618 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, 2);
620 XDrawLine(dpy
, win
, scr
->menu_item_auxtexture
->dark_gc
, 0, y
+h
-1,
624 segs
[0].y1
= segs
[0].y2
= y
+ h
-2;
626 segs
[1].x1
= segs
[1].x2
= w
-1;
628 segs
[1].y2
= y
+ h
-2;
629 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, 2);
634 paintEntry(WMenu
*menu
, int index
, int selected
)
637 GC light
, dim
, dark
, textGC
;
638 WScreen
*scr
=menu
->frame
->screen_ptr
;
639 Window win
= menu
->menu
->window
;
640 WMenuEntry
*entry
=menu
->entries
[index
];
642 if (!menu
->flags
.realized
) return;
643 h
= menu
->entry_height
;
644 w
= menu
->menu
->width
;
647 light
= scr
->menu_item_auxtexture
->light_gc
;
648 dim
= scr
->menu_item_auxtexture
->dim_gc
;
649 dark
= scr
->menu_item_auxtexture
->dark_gc
;
651 /* paint background */
653 XSetForeground(dpy
, scr
->select_menu_gc
, scr
->select_pixel
);
654 XFillRectangle(dpy
, win
, scr
->select_menu_gc
, 1, y
+1, w
-2, h
-3);
655 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
)
656 drawFrame(scr
, win
, y
, w
, h
);
658 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
659 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
661 drawFrame(scr
, win
, y
, w
, h
);
663 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
668 textGC
= scr
->select_menu_gc
;
669 if (entry
->flags
.enabled
)
670 XSetForeground(dpy
, textGC
, scr
->select_text_pixel
);
672 XSetForeground(dpy
, textGC
, scr
->dtext_pixel
);
673 } else if (!entry
->flags
.enabled
) {
674 textGC
= scr
->disabled_menu_entry_gc
;
676 textGC
= scr
->menu_entry_gc
;
680 if (entry
->flags
.indicator
)
681 x
+= MENU_INDICATOR_SPACE
+ 2;
683 wDrawString(win
, scr
->menu_entry_font
,
684 textGC
, x
, 3+y
+scr
->menu_entry_font
->y
, entry
->text
,
685 strlen(entry
->text
));
687 if (entry
->cascade
>=0) {
688 /* draw the cascade indicator */
689 XDrawLine(dpy
,win
,dim
, w
-11, y
+6, w
-6, y
+h
/2-1);
690 XDrawLine(dpy
,win
,light
, w
-11, y
+h
-8, w
-6, y
+h
/2-1);
691 XDrawLine(dpy
,win
,dark
, w
-12, y
+6, w
-12, y
+h
-8);
695 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
700 switch (entry
->flags
.indicator_type
) {
702 indicator
= scr
->menu_check_indicator
;
705 indicator
= scr
->menu_mini_indicator
;
708 indicator
= scr
->menu_hide_indicator
;
711 indicator
= scr
->menu_shade_indicator
;
715 indicator
= scr
->menu_radio_indicator
;
719 iw
= indicator
->width
;
720 ih
= indicator
->height
;
721 XSetClipMask(dpy
, scr
->copy_gc
, indicator
->mask
);
722 XSetClipOrigin(dpy
, scr
->copy_gc
, 5, y
+(h
-ih
)/2);
724 XSetForeground(dpy
, scr
->copy_gc
, scr
->black_pixel
);
726 XSetForeground(dpy
, scr
->copy_gc
, scr
->mtext_pixel
);
727 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+(h
-ih
)/2, iw
, ih
);
729 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
730 iw, ih, 5, y+(h-ih)/2);
732 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
735 /* draw right text */
737 if (entry
->rtext
&& entry
->cascade
<0) {
738 tw
= wTextWidth(scr
->menu_entry_font
->font
, entry
->rtext
,
739 strlen(entry
->rtext
));
741 wDrawString(win
, scr
->menu_entry_font
, textGC
, w
-6-tw
,
742 3+y
+scr
->menu_entry_font
->y
, entry
->rtext
,
743 strlen(entry
->rtext
));
748 move_menus(WMenu
*menu
, int x
, int y
)
750 while (menu
->parent
) {
753 if (!wPreferences
.align_menus
&& menu
->selected_entry
>=0) {
754 y
-= menu
->selected_entry
*menu
->entry_height
;
757 wMenuMove(menu
, x
, y
, True
);
761 makeVisible(WMenu
*menu
)
763 WScreen
*scr
= menu
->frame
->screen_ptr
;
764 int x1
, y1
, x2
, y2
, new_x
, new_y
, move
;
766 if (menu
->entry_no
<0) return;
769 y1
= menu
->frame_y
+menu
->frame
->top_width
770 + menu
->selected_entry
*menu
->entry_height
;
771 x2
= x1
+ MENUW(menu
);
772 y2
= y1
+ menu
->entry_height
;
781 } else if (x2
>= scr
->scr_width
) {
782 new_x
= scr
->scr_width
- MENUW(menu
) - 1;
789 } else if (y2
>= scr
->scr_height
) {
790 new_y
= scr
->scr_height
- menu
->entry_height
- 1;
794 new_y
= new_y
- menu
->frame
->top_width
795 - menu
->selected_entry
*menu
->entry_height
;
796 move_menus(menu
, new_x
, new_y
);
802 check_key(WMenu
*menu
, XKeyEvent
*event
)
807 if (XLookupString(event
, buffer
, 32, NULL
, NULL
)<1)
810 ch
= toupper(buffer
[0]);
812 s
= (menu
->selected_entry
>=0 ? menu
->selected_entry
+1 : 0);
815 for (i
=s
; i
<menu
->entry_no
; i
++) {
816 if (ch
==toupper(menu
->entries
[i
]->text
[0])) {
820 /* no match. Retry from start, if previous started from a selected entry */
830 keyboardMenu(WMenu
*menu
)
833 KeySym ksym
=NoSymbol
;
837 int old_pos_x
= menu
->frame_x
;
838 int old_pos_y
= menu
->frame_y
;
839 int new_x
= old_pos_x
, new_y
= old_pos_y
;
840 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
841 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
843 if (menu
->flags
.editing
)
847 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
,
848 GrabModeAsync
, CurrentTime
);
850 if (menu
->frame_y
+menu
->frame
->top_width
>= scr_height
)
851 new_y
= scr_height
- menu
->frame
->top_width
;
853 if (menu
->frame_x
+MENUW(menu
) >= scr_width
)
854 new_x
= scr_width
-MENUW(menu
)-1;
856 move_menus(menu
, new_x
, new_y
);
858 while (!done
&& menu
->flags
.mapped
) {
859 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
860 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonPressMask
861 |ButtonReleaseMask
|KeyPressMask
|KeyReleaseMask
862 |SubstructureNotifyMask
, &event
);
864 switch (event
.type
) {
866 ksym
= XLookupKeysym(&event
.xkey
, 0);
874 selectEntry(menu
, 0);
880 selectEntry(menu
, menu
->entry_no
-1);
889 if (menu
->selected_entry
<= 0)
890 selectEntry(menu
, menu
->entry_no
-1);
892 selectEntry(menu
, menu
->selected_entry
-1);
901 if (menu
->selected_entry
<0)
902 selectEntry(menu
, 0);
903 else if (menu
->selected_entry
== menu
->entry_no
-1)
904 selectEntry(menu
, 0);
905 else if (menu
->selected_entry
< menu
->entry_no
-1)
906 selectEntry(menu
, menu
->selected_entry
+1);
915 if (menu
->selected_entry
>=0) {
917 entry
= menu
->entries
[menu
->selected_entry
];
919 if (entry
->cascade
>= 0 && menu
->cascades
920 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
922 XUngrabKeyboard(dpy
, CurrentTime
);
924 selectEntry(menu
->cascades
[entry
->cascade
], 0);
925 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
928 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
929 GrabModeAsync
, GrabModeAsync
,
940 if (menu
->parent
!=NULL
&& menu
->parent
->selected_entry
>=0) {
941 selectEntry(menu
, -1);
942 move_menus(menu
, old_pos_x
, old_pos_y
);
952 index
= check_key(menu
, &event
.xkey
);
954 selectEntry(menu
, index
);
960 if (event
.type
==ButtonPress
)
963 WMHandleEvent(&event
);
967 XUngrabKeyboard(dpy
, CurrentTime
);
969 if (done
==2 && menu
->selected_entry
>=0) {
970 entry
= menu
->entries
[menu
->selected_entry
];
975 if (entry
&& entry
->callback
!=NULL
&& entry
->flags
.enabled
976 && entry
->cascade
< 0) {
977 #if (MENU_BLINK_COUNT > 0)
978 int sel
= menu
->selected_entry
;
981 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
982 paintEntry(menu
, sel
, False
);
984 wusleep(MENU_BLINK_DELAY
);
985 paintEntry(menu
, sel
, True
);
987 wusleep(MENU_BLINK_DELAY
);
990 selectEntry(menu
, -1);
992 if (!menu
->flags
.buttoned
) {
994 move_menus(menu
, old_pos_x
, old_pos_y
);
998 (*entry
->callback
)(menu
, entry
);
1000 if (!menu
->flags
.buttoned
) {
1002 move_menus(menu
, old_pos_x
, old_pos_y
);
1004 selectEntry(menu
, -1);
1008 /* returns True if returning from a submenu to a parent menu,
1009 * False if exiting from menu */
1015 wMenuMapAt(WMenu
*menu
, int x
, int y
, int keyboard
)
1017 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
1018 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
1020 if (!menu
->flags
.realized
) {
1021 menu
->flags
.realized
=1;
1024 if (!menu
->flags
.mapped
) {
1025 if (wPreferences
.wrap_menus
) {
1028 if (x
+MENUW(menu
) > scr_width
)
1029 x
= scr_width
- MENUW(menu
);
1030 if (y
+MENUH(menu
) > scr_height
)
1031 y
= scr_height
- MENUH(menu
);
1034 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
1037 XMapWindow(dpy
, menu
->frame
->core
->window
);
1038 wRaiseFrame(menu
->frame
->core
);
1039 menu
->flags
.mapped
= 1;
1041 selectEntry(menu
, 0);
1050 wMenuMap(WMenu
*menu
)
1052 if (!menu
->flags
.realized
) {
1053 menu
->flags
.realized
=1;
1056 if (menu
->flags
.app_menu
&& menu
->parent
==NULL
) {
1057 menu
->frame_x
= menu
->frame
->screen_ptr
->app_menu_x
;
1058 menu
->frame_y
= menu
->frame
->screen_ptr
->app_menu_y
;
1059 XMoveWindow(dpy
, menu
->frame
->core
->window
, menu
->frame_x
, menu
->frame_y
);
1061 XMapWindow(dpy
, menu
->frame
->core
->window
);
1062 wRaiseFrame(menu
->frame
->core
);
1063 menu
->flags
.mapped
= 1;
1068 wMenuUnmap(WMenu
*menu
)
1072 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1073 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1074 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1076 menu
->flags
.buttoned
= 0;
1077 menu
->flags
.mapped
= 0;
1078 menu
->flags
.open_to_left
= 0;
1080 for (i
=0; i
<menu
->cascade_no
; i
++) {
1081 if (menu
->cascades
[i
]!=NULL
1082 && menu
->cascades
[i
]->flags
.mapped
1083 && !menu
->cascades
[i
]->flags
.buttoned
) {
1085 wMenuUnmap(menu
->cascades
[i
]);
1088 menu
->selected_entry
= -1;
1094 wMenuPaint(WMenu
*menu
)
1098 if (!menu
->flags
.mapped
) {
1103 for (i
=0; i
<menu
->entry_no
; i
++) {
1104 paintEntry(menu
, i
, i
==menu
->selected_entry
);
1110 wMenuSetEnabled(WMenu
*menu
, int index
, int enable
)
1112 if (index
>=menu
->entry_no
) return;
1113 menu
->entries
[index
]->flags
.enabled
=enable
;
1114 paintEntry(menu
, index
, index
==menu
->selected_entry
);
1115 paintEntry(menu
->brother
, index
, index
==menu
->selected_entry
);
1119 /* ====================================================================== */
1123 editEntry(WMenu
*menu
, WMenuEntry
*entry
)
1127 WObjDescriptor
*desc
;
1133 menu
->flags
.editing
= 1;
1135 text
= wTextCreate(menu
->menu
, 1, menu
->entry_height
* entry
->order
,
1136 menu
->menu
->width
- 2, menu
->entry_height
- 1);
1138 wTextPutText(text
, entry
->text
);
1139 XGetInputFocus(dpy
, &old_focus
, &old_revert
);
1140 XSetInputFocus(dpy
, text
->core
->window
, RevertToNone
, CurrentTime
);
1142 if (XGrabKeyboard(dpy
, text
->core
->window
, True
, GrabModeAsync
,
1143 GrabModeAsync
, CurrentTime
)!=GrabSuccess
) {
1144 wwarning("could not grab keyboard");
1147 wSetFocusTo(menu
->frame
->screen_ptr
,
1148 menu
->frame
->screen_ptr
->focused_window
);
1153 while (!done
&& !text
->done
) {
1155 XAllowEvents(dpy
, AsyncKeyboard
|AsyncPointer
, CurrentTime
);
1157 WMNextEvent(dpy
, &event
);
1159 if (XFindContext(dpy
, event
.xany
.window
, wWinContext
,
1160 (XPointer
*)&desc
)==XCNOENT
)
1163 if ((desc
!= NULL
) && (desc
->handle_anything
!= NULL
)) {
1165 (*desc
->handle_anything
)(desc
, &event
);
1168 switch (event
.type
) {
1170 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
1174 WMHandleEvent(&event
);
1179 XSetInputFocus(dpy
, old_focus
, old_revert
, CurrentTime
);
1181 wSetFocusTo(menu
->frame
->screen_ptr
,
1182 menu
->frame
->screen_ptr
->focused_window
);
1185 t
= wTextGetText(text
);
1186 /* if !t, the user has canceled editing */
1190 entry
->text
= wstrdup(t
);
1192 menu
->flags
.realized
= 0;
1196 XUngrabKeyboard(dpy
, CurrentTime
);
1198 if (t
&& menu
->on_edit
)
1199 (*menu
->on_edit
)(menu
, entry
);
1201 menu
->flags
.editing
= 0;
1203 if (!menu
->flags
.realized
)
1209 selectEntry(WMenu
*menu
, int entry_no
)
1215 if (menu
->entries
==NULL
)
1218 if (entry_no
>= menu
->entry_no
)
1221 old_entry
= menu
->selected_entry
;
1222 menu
->selected_entry
= entry_no
;
1224 if (old_entry
!=entry_no
) {
1226 /* unselect previous entry */
1228 paintEntry(menu
, old_entry
, False
);
1229 entry
= menu
->entries
[old_entry
];
1232 if (entry
->cascade
>=0 && menu
->cascades
) {
1233 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1234 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1240 menu
->selected_entry
= -1;
1243 entry
= menu
->entries
[entry_no
];
1245 if (entry
->cascade
>=0 && menu
->cascades
&& entry
->flags
.enabled
) {
1246 /* Callback for when the submenu is opened.
1248 submenu
= menu
->cascades
[entry
->cascade
];
1249 if (submenu
&& submenu
->flags
.brother
)
1250 submenu
= submenu
->brother
;
1252 if (entry
->callback
) {
1253 /* Only call the callback if the submenu is not yet mapped.
1255 if (menu
->flags
.brother
) {
1256 if (!submenu
|| !submenu
->flags
.mapped
)
1257 (*entry
->callback
)(menu
->brother
, entry
);
1259 if (!submenu
|| !submenu
->flags
.buttoned
)
1260 (*entry
->callback
)(menu
, entry
);
1264 /* the submenu menu might have changed */
1265 submenu
= menu
->cascades
[entry
->cascade
];
1268 if (!submenu
->flags
.mapped
) {
1271 if (!submenu
->flags
.realized
)
1272 wMenuRealize(submenu
);
1273 if (wPreferences
.wrap_menus
) {
1274 if (menu
->flags
.open_to_left
)
1275 submenu
->flags
.open_to_left
= 1;
1277 if (submenu
->flags
.open_to_left
) {
1278 x
= menu
->frame_x
- MENUW(submenu
);
1281 submenu
->flags
.open_to_left
= 0;
1284 x
= menu
->frame_x
+ MENUW(menu
);
1286 if (x
+ MENUW(submenu
)
1287 >= menu
->frame
->screen_ptr
->scr_width
) {
1289 x
= menu
->frame_x
- MENUW(submenu
);
1290 submenu
->flags
.open_to_left
= 1;
1294 x
= menu
->frame_x
+ MENUW(menu
);
1297 if (wPreferences
.align_menus
) {
1300 y
= menu
->frame_y
+ menu
->entry_height
*entry_no
;
1301 if (menu
->flags
.titled
)
1302 y
+= menu
->frame
->top_width
;
1303 if (menu
->cascades
[entry
->cascade
]->flags
.titled
)
1304 y
-= menu
->cascades
[entry
->cascade
]->frame
->top_width
;
1307 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1308 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1313 paintEntry(menu
, entry_no
, True
);
1319 findMenu(WScreen
*scr
, int *x_ret
, int *y_ret
)
1322 WObjDescriptor
*desc
;
1323 Window root_ret
, win
, junk_win
;
1327 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
,
1330 if (win
==None
) return NULL
;
1332 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1335 if (desc
->parent_type
== WCLASS_MENU
) {
1336 menu
= (WMenu
*)desc
->parent
;
1337 XTranslateCoordinates(dpy
, root_ret
, menu
->menu
->window
, wx
, wy
,
1338 x_ret
, y_ret
, &junk_win
);
1348 closeCascade(WMenu
*menu
)
1350 WMenu
*parent
=menu
->parent
;
1352 if (menu
->flags
.brother
1353 || (!menu
->flags
.buttoned
1354 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
))) {
1356 selectEntry(menu
, -1);
1358 #if (MENU_BLINK_DELAY > 2)
1359 wusleep(MENU_BLINK_DELAY
/2);
1363 && (parent
->parent
!=NULL
|| !parent
->flags
.app_menu
1364 || parent
->flags
.brother
)
1365 && !parent
->flags
.buttoned
) {
1366 selectEntry(parent
, -1);
1368 parent
= parent
->parent
;
1371 selectEntry(parent
, -1);
1377 closeBrotherCascadesOf(WMenu
*menu
)
1382 for (i
=0; i
<menu
->cascade_no
; i
++) {
1383 if (menu
->cascades
[i
]->flags
.brother
) {
1384 tmp
= menu
->cascades
[i
];
1386 tmp
= menu
->cascades
[i
]->brother
;
1388 if (tmp
->flags
.mapped
) {
1389 selectEntry(tmp
->parent
, -1);
1390 closeBrotherCascadesOf(tmp
);
1397 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1401 parentMenu(WMenu
*menu
)
1406 if (menu
->flags
.buttoned
)
1409 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1410 parent
= menu
->parent
;
1411 if (parent
->selected_entry
< 0)
1413 entry
= parent
->entries
[parent
->selected_entry
];
1414 if (!entry
->flags
.enabled
|| entry
->cascade
<0 || !parent
->cascades
||
1415 parent
->cascades
[entry
->cascade
] != menu
)
1418 if (menu
->flags
.buttoned
)
1428 * Will raise the passed menu, if submenu = 0
1429 * If submenu > 0 will also raise all mapped submenus
1430 * until the first buttoned one
1431 * If submenu < 0 will also raise all mapped parent menus
1432 * until the first buttoned one
1436 raiseMenus(WMenu
*menu
, int submenus
)
1443 wRaiseFrame(menu
->frame
->core
);
1445 if (submenus
>0 && menu
->selected_entry
>=0) {
1446 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
1447 if (i
>=0 && menu
->cascades
) {
1448 submenu
= menu
->cascades
[i
];
1449 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
1450 raiseMenus(submenu
, submenus
);
1453 if (submenus
<0 && !menu
->flags
.buttoned
&&
1454 menu
->parent
&& menu
->parent
->flags
.mapped
)
1455 raiseMenus(menu
->parent
, submenus
);
1460 wMenuUnderPointer(WScreen
*screen
)
1462 WObjDescriptor
*desc
;
1463 Window root_ret
, win
;
1467 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
,
1468 &dummy
, &dummy
, &mask
);
1470 if (win
==None
) return NULL
;
1472 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1475 if (desc
->parent_type
== WCLASS_MENU
)
1476 return (WMenu
*)desc
->parent
;
1482 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1486 getPointerPosition(WScreen
*scr
, int *x
, int *y
)
1488 Window root_ret
, win
;
1492 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1497 getScrollAmount(WMenu
*menu
, int *hamount
, int *vamount
)
1499 WScreen
*scr
= menu
->menu
->screen_ptr
;
1500 int menuX1
= menu
->frame_x
;
1501 int menuY1
= menu
->frame_y
;
1502 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1503 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1504 int screenW
= scr
->scr_width
;
1505 int screenH
= scr
->scr_height
;
1511 getPointerPosition(scr
, &xroot
, &yroot
);
1514 if (xroot
<= 1 && menuX1
< 0) {
1515 /* scroll to the right */
1516 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX1
));
1518 } else if (xroot
>= screenW
-2 && menuX2
> screenW
-1) {
1519 /* scroll to the left */
1520 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX2
-screenW
-1));
1525 *hamount
= -*hamount
;
1528 if (yroot
<= 1 && menuY1
< 0) {
1530 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY1
));
1532 } else if (yroot
>= screenH
-2 && menuY2
> screenH
-1) {
1534 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY2
-screenH
-1));
1536 *vamount
= -*vamount
;
1542 dragScrollMenuCallback(void *data
)
1544 WMenu
*menu
= (WMenu
*)data
;
1545 WScreen
*scr
= menu
->menu
->screen_ptr
;
1546 WMenu
*parent
= parentMenu(menu
);
1547 int hamount
, vamount
;
1549 int newSelectedEntry
;
1551 getScrollAmount(menu
, &hamount
, &vamount
);
1554 if (hamount
!= 0 || vamount
!= 0) {
1555 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1556 parent
->frame_y
+ vamount
, True
);
1557 if (findMenu(scr
, &x
, &y
)) {
1558 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1559 selectEntry(menu
, newSelectedEntry
);
1561 /* Pointer fell outside of menu. If the selected entry is
1562 * not a submenu, unselect it */
1563 if (menu
->selected_entry
>= 0
1564 && menu
->entries
[menu
->selected_entry
]->cascade
<0)
1565 selectEntry(menu
, -1);
1566 newSelectedEntry
= 0;
1569 /* paranoid check */
1570 if (newSelectedEntry
>= 0) {
1571 /* keep scrolling */
1572 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1573 dragScrollMenuCallback
, menu
);
1578 /* don't need to scroll anymore */
1580 if (findMenu(scr
, &x
, &y
)) {
1581 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1582 selectEntry(menu
, newSelectedEntry
);
1589 scrollMenuCallback(void *data
)
1591 WMenu
*menu
= (WMenu
*)data
;
1592 WMenu
*parent
= parentMenu(menu
);
1593 int hamount
= 0; /* amount to scroll */
1596 getScrollAmount(menu
, &hamount
, &vamount
);
1598 if (hamount
!= 0 || vamount
!= 0) {
1599 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1600 parent
->frame_y
+ vamount
, True
);
1602 /* keep scrolling */
1603 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1604 scrollMenuCallback
, menu
);
1606 /* don't need to scroll anymore */
1613 #define MENU_SCROLL_BORDER 5
1616 isPointNearBoder(WMenu
*menu
, int x
, int y
)
1618 int menuX1
= menu
->frame_x
;
1619 int menuY1
= menu
->frame_y
;
1620 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1621 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1622 int scrXe
= menu
->menu
->screen_ptr
->scr_width
-1;
1623 int scrYe
= menu
->menu
->screen_ptr
->scr_height
-1;
1626 if (x
>= menuX1
&& x
<= menuX2
&& (y
< MENU_SCROLL_BORDER
1627 || y
> scrYe
-MENU_SCROLL_BORDER
))
1629 else if (y
>= menuY1
&& y
<= menuY2
&& (x
< MENU_SCROLL_BORDER
1630 || x
> scrXe
-MENU_SCROLL_BORDER
))
1638 wMenuScroll(WMenu
*menu
, XEvent
*event
)
1641 WMenu
*omenu
= parentMenu(menu
);
1642 WScreen
*scr
= menu
->frame
->screen_ptr
;
1645 int old_frame_x
= omenu
->frame_x
;
1646 int old_frame_y
= omenu
->frame_y
;
1650 puts("Entering menu Scroll");
1653 if ((/*omenu->flags.buttoned &&*/ !wPreferences
.wrap_menus
)
1654 || omenu
->flags
.app_menu
) {
1658 if (!wPreferences
.wrap_menus
)
1659 raiseMenus(omenu
, True
);
1661 raiseMenus(menu
, False
);
1664 scrollMenuCallback(menu
);
1667 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1669 WMNextEvent(dpy
, &ev
);
1674 x
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1675 y
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1677 /* on_border is != 0 if the pointer is between the menu
1678 * and the screen border and is close enough to the border */
1679 on_border
= isPointNearBoder(menu
, x
, y
);
1681 smenu
= wMenuUnderPointer(scr
);
1683 if ((smenu
==NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
)!=omenu
)) {
1688 on_x_edge
= x
<= 1 || x
>= scr
->scr_width
- 2;
1689 on_y_edge
= y
<= 1 || y
>= scr
->scr_height
- 2;
1690 on_border
= on_x_edge
|| on_y_edge
;
1692 if (!on_border
&& !jump_back
) {
1697 if (menu
->timer
&& (smenu
!=menu
|| (!on_y_edge
&& !on_x_edge
))) {
1698 WMDeleteTimerHandler(menu
->timer
);
1706 scrollMenuCallback(menu
);
1709 /* True if we push on title, or drag the omenu to other position */
1710 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1711 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1712 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1713 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1715 smenu
= wMenuUnderPointer(scr
);
1716 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1718 else if (smenu
==omenu
&& on_title
) {
1732 WMDeleteTimerHandler(menu
->timer
);
1737 wMenuMove(omenu
, old_frame_x
, old_frame_y
, True
);
1740 puts("Leaving menu Scroll");
1747 menuExpose(WObjDescriptor
*desc
, XEvent
*event
)
1749 wMenuPaint(desc
->parent
);
1753 int *delayed_select
;
1760 delaySelection(void *data
)
1762 delay_data
*d
= (delay_data
*)data
;
1768 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1769 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1770 entry_no
= getEntryAt(menu
, x
, y
);
1771 selectEntry(menu
, entry_no
);
1773 if (d
->delayed_select
)
1774 *(d
->delayed_select
) = 0;
1779 menuMouseDown(WObjDescriptor
*desc
, XEvent
*event
)
1781 XButtonEvent
*bev
= &event
->xbutton
;
1782 WMenu
*menu
= desc
->parent
;
1784 WScreen
*scr
=menu
->frame
->screen_ptr
;
1785 WMenuEntry
*entry
=NULL
;
1787 int close_on_exit
=0;
1789 int delayed_select
= 0;
1793 int old_frame_x
= 0;
1794 int old_frame_y
= 0;
1795 delay_data d_data
= {NULL
, NULL
, NULL
};
1797 if (!wPreferences
.wrap_menus
) {
1798 smenu
= parentMenu(menu
);
1799 old_frame_x
= smenu
->frame_x
;
1800 old_frame_y
= smenu
->frame_y
;
1802 else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1803 /* This is true if the menu was launched with right click on root window */
1805 d_data
.delayed_select
= &delayed_select
;
1807 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
,
1808 delaySelection
, &d_data
);
1811 wRaiseFrame(menu
->frame
->core
);
1813 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1815 smenu
= findMenu(scr
, &x
, &y
);
1823 if (menu
->flags
.editing
) {
1826 entry_no
= getEntryAt(menu
, x
, y
);
1828 entry
= menu
->entries
[entry_no
];
1830 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
1831 && entry
->flags
.editable
) {
1832 editEntry(smenu
, entry
);
1834 } else if (bev
->state
& ControlMask
) {
1838 if (entry
->flags
.enabled
&& entry
->cascade
>=0 && menu
->cascades
) {
1839 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1841 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&&
1842 menu
->selected_entry
!=entry_no
) {
1843 wMenuUnmap(submenu
);
1845 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1846 selectEntry(menu
, entry_no
);
1847 } else if (!submenu
->flags
.buttoned
) {
1848 selectEntry(menu
, -1);
1851 } else if (!delayed_select
) {
1852 selectEntry(menu
, entry_no
);
1855 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1857 dragScrollMenuCallback(menu
);
1861 prevx
= bev
->x_root
;
1862 prevy
= bev
->y_root
;
1865 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonReleaseMask
1866 |ButtonPressMask
, &ev
);
1869 smenu
= findMenu(scr
, &x
, &y
);
1871 if (smenu
== NULL
) {
1872 /* moved mouse out of menu */
1874 if (!delayed_select
&& d_data
.magic
) {
1875 WMDeleteTimerHandler(d_data
.magic
);
1876 d_data
.magic
= NULL
;
1879 || (menu
->selected_entry
>=0
1880 && menu
->entries
[menu
->selected_entry
]->cascade
>=0)) {
1881 prevx
= ev
.xmotion
.x_root
;
1882 prevy
= ev
.xmotion
.y_root
;
1886 selectEntry(menu
, -1);
1888 prevx
= ev
.xmotion
.x_root
;
1889 prevy
= ev
.xmotion
.y_root
;
1891 } else if (menu
&& menu
!=smenu
1892 && (menu
->selected_entry
<0
1893 || menu
->entries
[menu
->selected_entry
]->cascade
<0)) {
1894 selectEntry(menu
, -1);
1896 if (!delayed_select
&& d_data
.magic
) {
1897 WMDeleteTimerHandler(d_data
.magic
);
1898 d_data
.magic
= NULL
;
1902 /* hysteresis for item selection */
1904 /* check if the motion was to the side, indicating that
1905 * the user may want to cross to a submenu */
1906 if (!delayed_select
&& menu
) {
1908 Bool moved_to_submenu
;/* moved to direction of submenu */
1910 dx
= abs(prevx
- ev
.xmotion
.x_root
);
1912 moved_to_submenu
= False
;
1913 if (dx
> 0 /* if moved enough to the side */
1914 /* maybe a open submenu */
1915 && menu
->selected_entry
>=0
1916 /* moving to the right direction */
1917 && (wPreferences
.align_menus
1918 || ev
.xmotion
.y_root
>= prevy
)) {
1921 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
1923 if (menu
->cascades
[index
]->frame_x
>menu
->frame_x
) {
1924 if (prevx
< ev
.xmotion
.x_root
)
1925 moved_to_submenu
= True
;
1927 if (prevx
> ev
.xmotion
.x_root
)
1928 moved_to_submenu
= True
;
1934 if (menu
!= smenu
) {
1936 WMDeleteTimerHandler(d_data
.magic
);
1938 d_data
.magic
= NULL
;
1939 } else if (moved_to_submenu
) {
1940 /* while we are moving, postpone the selection */
1942 WMDeleteTimerHandler(d_data
.magic
);
1944 d_data
.delayed_select
= NULL
;
1946 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
1949 prevx
= ev
.xmotion
.x_root
;
1950 prevy
= ev
.xmotion
.y_root
;
1954 WMDeleteTimerHandler(d_data
.magic
);
1955 d_data
.magic
= NULL
;
1959 prevx
= ev
.xmotion
.x_root
;
1960 prevy
= ev
.xmotion
.y_root
;
1962 /* pointer crossed menus */
1963 if (menu
&& menu
->timer
) {
1964 WMDeleteTimerHandler(menu
->timer
);
1968 dragScrollMenuCallback(smenu
);
1972 dragScrollMenuCallback(menu
);
1974 if (!delayed_select
) {
1975 entry_no
= getEntryAt(menu
, x
, y
);
1977 entry
= menu
->entries
[entry_no
];
1978 if (entry
->flags
.enabled
&& entry
->cascade
>=0 &&
1980 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1981 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
1982 && menu
->selected_entry
!=entry_no
) {
1983 wMenuUnmap(submenu
);
1987 selectEntry(menu
, entry_no
);
1995 if (ev
.xbutton
.button
== event
->xbutton
.button
)
2005 if (menu
&& menu
->timer
) {
2006 WMDeleteTimerHandler(menu
->timer
);
2009 if (d_data
.magic
!=NULL
)
2010 WMDeleteTimerHandler(d_data
.magic
);
2012 if (menu
&& menu
->selected_entry
>=0) {
2013 entry
= menu
->entries
[menu
->selected_entry
];
2014 if (entry
->callback
!=NULL
&& entry
->flags
.enabled
2015 && entry
->cascade
< 0) {
2016 /* blink and erase menu selection */
2017 #if (MENU_BLINK_DELAY > 0)
2018 int sel
= menu
->selected_entry
;
2021 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
2022 paintEntry(menu
, sel
, False
);
2024 wusleep(MENU_BLINK_DELAY
);
2025 paintEntry(menu
, sel
, True
);
2027 wusleep(MENU_BLINK_DELAY
);
2030 /* unmap the menu, it's parents and call the callback */
2031 if (!menu
->flags
.buttoned
&&
2032 (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2035 selectEntry(menu
, -1);
2037 (*entry
->callback
)(menu
, entry
);
2039 /* If the user double clicks an entry, the entry will
2040 * be executed twice, which is not good for things like
2041 * the root menu. So, ignore any clicks that were generated
2042 * while the entry was being executed */
2043 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
,
2045 } else if (entry
->callback
!=NULL
&& entry
->cascade
<0) {
2046 selectEntry(menu
, -1);
2048 if (entry
->cascade
>=0 && menu
->cascades
2049 && menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2050 selectEntry(menu
, -1);
2055 if (((WMenu
*)desc
->parent
)->flags
.brother
|| close_on_exit
2057 closeCascade(desc
->parent
);
2059 /* close the cascade windows that should not remain opened */
2060 closeBrotherCascadesOf(desc
->parent
);
2062 if (!wPreferences
.wrap_menus
)
2063 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2068 wMenuMove(WMenu
*menu
, int x
, int y
, int submenus
)
2077 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2079 if (submenus
>0 && menu
->selected_entry
>=0) {
2080 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2082 if (i
>=0 && menu
->cascades
) {
2083 submenu
= menu
->cascades
[i
];
2084 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
2085 if (wPreferences
.align_menus
) {
2086 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2088 wMenuMove(submenu
, x
+ MENUW(menu
),
2089 y
+ submenu
->entry_height
*menu
->selected_entry
,
2094 if (submenus
<0 && menu
->parent
!=NULL
&& menu
->parent
->flags
.mapped
&&
2095 !menu
->parent
->flags
.buttoned
) {
2096 if (wPreferences
.align_menus
) {
2097 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2099 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2100 - menu
->parent
->entry_height
*menu
->parent
->selected_entry
,
2108 changeMenuLevels(WMenu
*menu
, int lower
)
2113 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
2115 wRaiseFrame(menu
->frame
->core
);
2116 menu
->flags
.lowered
= 0;
2118 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2119 wLowerFrame(menu
->frame
->core
);
2120 menu
->flags
.lowered
= 1;
2122 for (i
=0; i
<menu
->cascade_no
; i
++) {
2123 if (menu
->cascades
[i
]
2124 && !menu
->cascades
[i
]->flags
.buttoned
2125 && menu
->cascades
[i
]->flags
.lowered
!=lower
) {
2126 changeMenuLevels(menu
->cascades
[i
], lower
);
2134 menuTitleDoubleClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2139 if (event
->xbutton
.state
& MOD_MASK
) {
2140 if (menu
->flags
.lowered
) {
2145 changeMenuLevels(menu
, lower
);
2151 menuTitleMouseDown(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2156 int x
=menu
->frame_x
, y
=menu
->frame_y
;
2157 int dx
=event
->xbutton
.x_root
, dy
=event
->xbutton
.y_root
;
2161 printf("Moving menu\n");
2164 /* can't touch the menu copy */
2165 if (menu
->flags
.brother
)
2168 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2170 if (XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2171 ButtonMotionMask
|ButtonReleaseMask
|ButtonPressMask
,
2172 GrabModeAsync
, GrabModeAsync
, None
,
2173 wCursor
[WCUR_MOVE
], CurrentTime
)!=GrabSuccess
) {
2175 wwarning("pointer grab failed for menu move\n");
2179 if (event
->xbutton
.state
& MOD_MASK
) {
2180 wLowerFrame(menu
->frame
->core
);
2183 wRaiseFrame(menu
->frame
->core
);
2188 /* lower/raise all submenus */
2190 if (tmp
->selected_entry
>=0 && tmp
->cascades
) {
2191 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2192 if (!tmp
|| !tmp
->flags
.mapped
)
2195 wLowerFrame(tmp
->frame
->core
);
2197 wRaiseFrame(tmp
->frame
->core
);
2202 /* tear off the menu if it's a root menu or a cascade
2204 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
2205 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2206 menu
->flags
.buttoned
=1;
2207 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2209 /* turn off selected menu entry in parent menu */
2210 selectEntry(menu
->parent
, -1);
2212 /* make parent map the copy in place of the original */
2213 for (i
=0; i
<menu
->parent
->cascade_no
; i
++) {
2214 if (menu
->parent
->cascades
[i
] == menu
) {
2215 menu
->parent
->cascades
[i
] = menu
->brother
;
2223 WMMaskEvent(dpy
, ButtonMotionMask
|ButtonReleaseMask
|ButtonPressMask
2224 |ExposureMask
, &ev
);
2227 x
+= ev
.xmotion
.x_root
- dx
;
2228 y
+= ev
.xmotion
.y_root
- dy
;
2229 dx
= ev
.xmotion
.x_root
;
2230 dy
= ev
.xmotion
.y_root
;
2231 wMenuMove(menu
, x
, y
, True
);
2238 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2241 printf("End menu move\n");
2243 XUngrabPointer(dpy
, CurrentTime
);
2253 *----------------------------------------------------------------------
2255 * Handles mouse click on the close button of menus. The menu is
2256 * closed when the button is clicked.
2259 * The closed menu is reinserted at it's parent menus
2261 *----------------------------------------------------------------------
2264 menuCloseClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2266 WMenu
*menu
= (WMenu
*)data
;
2267 WMenu
*parent
= menu
->parent
;
2271 for (i
=0; i
<parent
->cascade_no
; i
++) {
2272 /* find the entry that points to the copy */
2273 if (parent
->cascades
[i
] == menu
->brother
) {
2274 /* make it point to the original */
2275 parent
->cascades
[i
] = menu
;
2276 menu
->parent
= parent
;
2286 wMenuSaveState(WScreen
*scr
)
2288 proplist_t menus
, key
, value
;
2292 menus
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
2294 if (scr
->root_menu
&& scr
->root_menu
->flags
.buttoned
) {
2295 sprintf(buffer
, "%i,%i", scr
->root_menu
->frame_x
,
2296 scr
->root_menu
->frame_y
);
2297 key
= PLMakeString("RootMenu");
2298 value
= PLMakeString(buffer
);
2299 PLInsertDictionaryEntry(menus
, key
, value
);
2305 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2306 sprintf(buffer
, "%i,%i", scr
->switch_menu
->frame_x
,
2307 scr
->switch_menu
->frame_y
);
2308 key
= PLMakeString("SwitchMenu");
2309 value
= PLMakeString(buffer
);
2310 PLInsertDictionaryEntry(menus
, key
, value
);
2316 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2317 sprintf(buffer
, "%i,%i", scr
->workspace_menu
->frame_x
,
2318 scr
->workspace_menu
->frame_y
);
2319 key
= PLMakeString("WorkspaceMenu");
2320 value
= PLMakeString(buffer
);
2321 PLInsertDictionaryEntry(menus
, key
, value
);
2328 key
= PLMakeString("Menus");
2329 PLInsertDictionaryEntry(scr
->session_state
, key
, menus
);
2336 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2340 restoreMenu(WScreen
*scr
, proplist_t menu
, int which
)
2343 WMenu
*pmenu
= NULL
;
2348 if (!PLIsString(menu
)) {
2349 COMPLAIN("Position");
2353 if (sscanf(PLGetString(menu
), "%i,%i", &x
, &y
)!=2)
2354 COMPLAIN("Position");
2356 if (which
& WSS_ROOTMENU
) {
2357 OpenRootMenu(scr
, x
, y
, False
);
2358 pmenu
= scr
->root_menu
;
2360 else if (which
& WSS_SWITCHMENU
) {
2361 OpenSwitchMenu(scr
, x
, y
, False
);
2362 pmenu
= scr
->switch_menu
;
2364 else if (which
& WSS_WSMENU
) {
2365 OpenWorkspaceMenu(scr
, x
, y
);
2366 pmenu
= scr
->workspace_menu
;
2367 if (pmenu
->parent
) {
2368 /* make parent map the copy in place of the original */
2369 for (i
=0; i
<pmenu
->parent
->cascade_no
; i
++) {
2370 if (pmenu
->parent
->cascades
[i
] == pmenu
) {
2371 pmenu
->parent
->cascades
[i
] = pmenu
->brother
;
2379 int width
= MENUW(pmenu
);
2380 int height
= MENUH(pmenu
);
2382 x
= (x
< -width
) ? 0 : x
;
2383 x
= (x
> scr
->scr_width
) ? scr
->scr_width
- width
: x
;
2384 y
= (y
< 0) ? 0 : y
;
2385 y
= (y
> scr
->scr_height
) ? scr
->scr_height
- height
: y
;
2386 wMenuMove(pmenu
, x
, y
, True
);
2387 pmenu
->flags
.buttoned
= 1;
2388 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2396 wMenuRestoreState(WScreen
*scr
)
2398 proplist_t menus
, menu
, key
, rkey
, skey
, wkey
;
2400 key
= PLMakeString("Menus");
2401 menus
= PLGetDictionaryEntry(scr
->session_state
, key
);
2409 rkey
= PLMakeString("RootMenu");
2410 skey
= PLMakeString("SwitchMenu");
2411 wkey
= PLMakeString("WorkspaceMenu");
2412 menu
= PLGetDictionaryEntry(menus
, rkey
);
2413 restoreMenu(scr
, menu
, WSS_ROOTMENU
);
2414 menu
= PLGetDictionaryEntry(menus
, skey
);
2415 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2416 menu
= PLGetDictionaryEntry(menus
, wkey
);
2417 restoreMenu(scr
, menu
, WSS_WSMENU
);
2426 OpenWorkspaceMenu(WScreen
*scr
, int x
, int y
)
2428 WMenu
*menu
, *parent
;
2431 if (!scr
->root_menu
) {
2432 OpenRootMenu(scr
, scr
->scr_width
*2, 0, False
);
2433 wMenuUnmap(scr
->root_menu
);
2436 menu
= scr
->workspace_menu
;
2438 if (menu
->flags
.mapped
) {
2439 if (!menu
->flags
.buttoned
) {
2441 parent
= menu
->parent
;
2442 if (parent
&& parent
->selected_entry
>= 0) {
2443 entry
= parent
->entries
[parent
->selected_entry
];
2444 if (parent
->cascades
[entry
->cascade
] == menu
) {
2445 selectEntry(parent
, -1);
2446 wMenuMapAt(menu
, x
, y
, False
);
2450 wRaiseFrame(menu
->frame
->core
);
2451 wMenuMapCopyAt(menu
, x
, y
);
2455 wMenuMapAt(menu
, x
, y
, False
);