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
-1; 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 wFrameWindowResize(menu
->frame
, mwidth
, menu
->entry_no
*eheight
-1
500 + menu
->frame
->top_width
+ menu
->frame
->bottom_width
);
503 /* setup background texture */
504 if (scr
->menu_item_texture
->any
.type
!= WTEX_SOLID
) {
505 if (!menu
->flags
.brother
) {
506 FREE_PIXMAP(menu
->menu_texture_data
);
508 wTextureRender(scr
, scr
->menu_item_texture
,
509 &menu
->menu_texture_data
, menu
->menu
->width
,
510 menu
->entry_height
, WREL_MENUENTRY
);
512 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
,
513 menu
->menu_texture_data
);
514 XClearWindow(dpy
, menu
->menu
->window
);
516 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
,
517 menu
->menu_texture_data
);
518 XClearWindow(dpy
, menu
->brother
->menu
->window
);
521 XSetWindowBackground(dpy
, menu
->menu
->window
,
522 scr
->menu_item_texture
->any
.color
.pixel
);
523 XClearWindow(dpy
, menu
->menu
->window
);
526 menu
->flags
.realized
= 1;
528 if (menu
->flags
.mapped
)
530 if (menu
->brother
->flags
.mapped
)
531 wMenuPaint(menu
->brother
);
536 wMenuDestroy(WMenu
*menu
, int recurse
)
540 /* remove any pending timers */
542 WMDeleteTimerHandler(menu
->timer
);
545 /* call destroy handler */
546 if (menu
->on_destroy
)
547 (*menu
->on_destroy
)(menu
);
549 /* Destroy items if this menu own them. If this is the "brother" menu,
550 * leave them alone as it is shared by them.
552 if (!menu
->flags
.brother
) {
553 for (i
=0; i
<menu
->entry_no
; i
++) {
555 free(menu
->entries
[i
]->text
);
557 if (menu
->entries
[i
]->rtext
)
558 free(menu
->entries
[i
]->rtext
);
560 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
561 (*menu
->entries
[i
]->free_cdata
)(menu
->entries
[i
]->clientdata
);
563 free(menu
->entries
[i
]);
567 for (i
=0; i
<menu
->cascade_no
; i
++) {
568 if (menu
->cascades
[i
]) {
569 if (menu
->cascades
[i
]->flags
.brother
)
570 wMenuDestroy(menu
->cascades
[i
]->brother
, recurse
);
572 wMenuDestroy(menu
->cascades
[i
], recurse
);
582 FREE_PIXMAP(menu
->menu_texture_data
);
585 free(menu
->cascades
);
587 wCoreDestroy(menu
->menu
);
588 wFrameWindowDestroy(menu
->frame
);
590 /* destroy copy of this menu */
591 if (!menu
->flags
.brother
&& menu
->brother
)
592 wMenuDestroy(menu
->brother
, False
);
599 drawFrame(WScreen
*scr
, Window win
, int y
, int w
, int h
)
610 segs
[1].y2
= y
+ h
- 2;
611 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, 2);
613 XDrawLine(dpy
, win
, scr
->menu_item_auxtexture
->dark_gc
, 0, y
+h
-1,
617 segs
[0].y1
= segs
[0].y2
= y
+ h
-2;
619 segs
[1].x1
= segs
[1].x2
= w
-1;
621 segs
[1].y2
= y
+ h
-2;
622 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, 2);
627 paintEntry(WMenu
*menu
, int index
, int selected
)
630 GC light
, dim
, dark
, textGC
;
631 WScreen
*scr
=menu
->frame
->screen_ptr
;
632 Window win
= menu
->menu
->window
;
633 WMenuEntry
*entry
=menu
->entries
[index
];
635 if (!menu
->flags
.realized
) return;
636 h
= menu
->entry_height
;
637 w
= menu
->menu
->width
;
640 light
= scr
->menu_item_auxtexture
->light_gc
;
641 dim
= scr
->menu_item_auxtexture
->dim_gc
;
642 dark
= scr
->menu_item_auxtexture
->dark_gc
;
644 /* paint background */
646 XSetForeground(dpy
, scr
->select_menu_gc
, scr
->select_pixel
);
647 XFillRectangle(dpy
, win
, scr
->select_menu_gc
, 1, y
+1, w
-2, h
-3);
648 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
)
649 drawFrame(scr
, win
, y
, w
, h
);
651 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
652 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
654 drawFrame(scr
, win
, y
, w
, h
);
656 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
661 textGC
= scr
->select_menu_gc
;
662 if (entry
->flags
.enabled
)
663 XSetForeground(dpy
, textGC
, scr
->select_text_pixel
);
665 XSetForeground(dpy
, textGC
, scr
->dtext_pixel
);
666 } else if (!entry
->flags
.enabled
) {
667 textGC
= scr
->disabled_menu_entry_gc
;
669 textGC
= scr
->menu_entry_gc
;
673 if (entry
->flags
.indicator
)
674 x
+= MENU_INDICATOR_SPACE
+ 2;
676 wDrawString(win
, scr
->menu_entry_font
,
677 textGC
, x
, 3+y
+scr
->menu_entry_font
->y
, entry
->text
,
678 strlen(entry
->text
));
680 if (entry
->cascade
>=0) {
681 /* draw the cascade indicator */
682 XDrawLine(dpy
,win
,dim
, w
-11, y
+6, w
-6, y
+h
/2-1);
683 XDrawLine(dpy
,win
,light
, w
-11, y
+h
-8, w
-6, y
+h
/2-1);
684 XDrawLine(dpy
,win
,dark
, w
-12, y
+6, w
-12, y
+h
-8);
688 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
693 switch (entry
->flags
.indicator_type
) {
695 indicator
= scr
->menu_check_indicator
;
698 indicator
= scr
->menu_mini_indicator
;
701 indicator
= scr
->menu_hide_indicator
;
704 indicator
= scr
->menu_shade_indicator
;
708 indicator
= scr
->menu_radio_indicator
;
712 iw
= indicator
->width
;
713 ih
= indicator
->height
;
714 XSetClipMask(dpy
, scr
->copy_gc
, indicator
->mask
);
715 XSetClipOrigin(dpy
, scr
->copy_gc
, 5, y
+(h
-ih
)/2);
717 XSetForeground(dpy
, scr
->copy_gc
, scr
->black_pixel
);
719 XSetForeground(dpy
, scr
->copy_gc
, scr
->mtext_pixel
);
720 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+(h
-ih
)/2, iw
, ih
);
722 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
723 iw, ih, 5, y+(h-ih)/2);
725 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
728 /* draw right text */
730 if (entry
->rtext
&& entry
->cascade
<0) {
731 tw
= wTextWidth(scr
->menu_entry_font
->font
, entry
->rtext
,
732 strlen(entry
->rtext
));
734 wDrawString(win
, scr
->menu_entry_font
, textGC
, w
-6-tw
,
735 3+y
+scr
->menu_entry_font
->y
, entry
->rtext
,
736 strlen(entry
->rtext
));
741 move_menus(WMenu
*menu
, int x
, int y
)
743 while (menu
->parent
) {
746 if (!wPreferences
.align_menus
&& menu
->selected_entry
>=0) {
747 y
-= menu
->selected_entry
*menu
->entry_height
;
750 wMenuMove(menu
, x
, y
, True
);
754 makeVisible(WMenu
*menu
)
756 WScreen
*scr
= menu
->frame
->screen_ptr
;
757 int x1
, y1
, x2
, y2
, new_x
, new_y
, move
;
759 if (menu
->entry_no
<0) return;
762 y1
= menu
->frame_y
+menu
->frame
->top_width
763 + menu
->selected_entry
*menu
->entry_height
;
764 x2
= x1
+ MENUW(menu
);
765 y2
= y1
+ menu
->entry_height
;
774 } else if (x2
>= scr
->scr_width
) {
775 new_x
= scr
->scr_width
- MENUW(menu
) - 1;
782 } else if (y2
>= scr
->scr_height
) {
783 new_y
= scr
->scr_height
- menu
->entry_height
- 1;
787 new_y
= new_y
- menu
->frame
->top_width
788 - menu
->selected_entry
*menu
->entry_height
;
789 move_menus(menu
, new_x
, new_y
);
794 check_key(WMenu
*menu
, XKeyEvent
*event
)
799 if (XLookupString(event
, buffer
, 32, NULL
, NULL
)<1)
802 ch
= toupper(buffer
[0]);
804 s
= (menu
->selected_entry
>=0 ? menu
->selected_entry
+1 : 0);
807 for (i
=s
; i
<menu
->entry_no
; i
++) {
808 if (ch
==toupper(menu
->entries
[i
]->text
[0])) {
812 /* no match. Retry from start, if previous started from a selected entry */
822 keyboardMenu(WMenu
*menu
)
825 KeySym ksym
=NoSymbol
;
829 int old_pos_x
= menu
->frame_x
;
830 int old_pos_y
= menu
->frame_y
;
831 int new_x
= old_pos_x
, new_y
= old_pos_y
;
832 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
833 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
835 if (menu
->flags
.editing
)
839 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
,
840 GrabModeAsync
, CurrentTime
);
842 if (menu
->frame_y
+menu
->frame
->top_width
>= scr_height
)
843 new_y
= scr_height
- menu
->frame
->top_width
;
845 if (menu
->frame_x
+MENUW(menu
) >= scr_width
)
846 new_x
= scr_width
-MENUW(menu
)-1;
848 move_menus(menu
, new_x
, new_y
);
850 while (!done
&& menu
->flags
.mapped
) {
851 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
852 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonPressMask
853 |ButtonReleaseMask
|KeyPressMask
|KeyReleaseMask
854 |SubstructureNotifyMask
, &event
);
856 switch (event
.type
) {
858 ksym
= XLookupKeysym(&event
.xkey
, 0);
866 selectEntry(menu
, 0);
872 selectEntry(menu
, menu
->entry_no
-1);
881 if (menu
->selected_entry
<= 0)
882 selectEntry(menu
, menu
->entry_no
-1);
884 selectEntry(menu
, menu
->selected_entry
-1);
893 if (menu
->selected_entry
<0)
894 selectEntry(menu
, 0);
895 else if (menu
->selected_entry
== menu
->entry_no
-1)
896 selectEntry(menu
, 0);
897 else if (menu
->selected_entry
< menu
->entry_no
-1)
898 selectEntry(menu
, menu
->selected_entry
+1);
907 if (menu
->selected_entry
>=0) {
909 entry
= menu
->entries
[menu
->selected_entry
];
911 if (entry
->cascade
>= 0 && menu
->cascades
912 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
914 XUngrabKeyboard(dpy
, CurrentTime
);
916 selectEntry(menu
->cascades
[entry
->cascade
], 0);
917 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
920 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
921 GrabModeAsync
, GrabModeAsync
,
932 if (menu
->parent
!=NULL
&& menu
->parent
->selected_entry
>=0) {
933 selectEntry(menu
, -1);
934 move_menus(menu
, old_pos_x
, old_pos_y
);
944 index
= check_key(menu
, &event
.xkey
);
946 selectEntry(menu
, index
);
952 if (event
.type
==ButtonPress
)
955 WMHandleEvent(&event
);
959 XUngrabKeyboard(dpy
, CurrentTime
);
961 if (done
==2 && menu
->selected_entry
>=0) {
962 entry
= menu
->entries
[menu
->selected_entry
];
967 if (entry
&& entry
->callback
!=NULL
&& entry
->flags
.enabled
968 && entry
->cascade
< 0) {
969 #if (MENU_BLINK_COUNT > 0)
970 int sel
= menu
->selected_entry
;
973 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
974 paintEntry(menu
, sel
, False
);
976 wusleep(MENU_BLINK_DELAY
);
977 paintEntry(menu
, sel
, True
);
979 wusleep(MENU_BLINK_DELAY
);
982 selectEntry(menu
, -1);
984 if (!menu
->flags
.buttoned
) {
986 move_menus(menu
, old_pos_x
, old_pos_y
);
990 (*entry
->callback
)(menu
, entry
);
992 if (!menu
->flags
.buttoned
) {
994 move_menus(menu
, old_pos_x
, old_pos_y
);
996 selectEntry(menu
, -1);
1000 /* returns True if returning from a submenu to a parent menu,
1001 * False if exiting from menu */
1007 wMenuMapAt(WMenu
*menu
, int x
, int y
, int keyboard
)
1009 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
1010 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
1012 if (!menu
->flags
.realized
) {
1013 menu
->flags
.realized
=1;
1016 if (!menu
->flags
.mapped
) {
1017 if (wPreferences
.wrap_menus
) {
1020 if (x
+MENUW(menu
) > scr_width
)
1021 x
= scr_width
- MENUW(menu
);
1022 if (y
+MENUH(menu
) > scr_height
)
1023 y
= scr_height
- MENUH(menu
);
1026 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
1029 XMapWindow(dpy
, menu
->frame
->core
->window
);
1030 wRaiseFrame(menu
->frame
->core
);
1031 menu
->flags
.mapped
= 1;
1033 selectEntry(menu
, 0);
1042 wMenuMap(WMenu
*menu
)
1044 if (!menu
->flags
.realized
) {
1045 menu
->flags
.realized
=1;
1048 if (menu
->flags
.app_menu
&& menu
->parent
==NULL
) {
1049 menu
->frame_x
= menu
->frame
->screen_ptr
->app_menu_x
;
1050 menu
->frame_y
= menu
->frame
->screen_ptr
->app_menu_y
;
1051 XMoveWindow(dpy
, menu
->frame
->core
->window
, menu
->frame_x
, menu
->frame_y
);
1053 XMapWindow(dpy
, menu
->frame
->core
->window
);
1054 wRaiseFrame(menu
->frame
->core
);
1055 menu
->flags
.mapped
= 1;
1060 wMenuUnmap(WMenu
*menu
)
1064 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1065 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1066 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1068 menu
->flags
.buttoned
= 0;
1069 menu
->flags
.mapped
= 0;
1070 menu
->flags
.open_to_left
= 0;
1072 for (i
=0; i
<menu
->cascade_no
; i
++) {
1073 if (menu
->cascades
[i
]!=NULL
1074 && menu
->cascades
[i
]->flags
.mapped
1075 && !menu
->cascades
[i
]->flags
.buttoned
) {
1077 wMenuUnmap(menu
->cascades
[i
]);
1080 menu
->selected_entry
= -1;
1086 wMenuPaint(WMenu
*menu
)
1090 if (!menu
->flags
.mapped
) {
1095 for (i
=0; i
<menu
->entry_no
; i
++) {
1096 paintEntry(menu
, i
, i
==menu
->selected_entry
);
1102 wMenuSetEnabled(WMenu
*menu
, int index
, int enable
)
1104 if (index
>=menu
->entry_no
) return;
1105 menu
->entries
[index
]->flags
.enabled
=enable
;
1106 paintEntry(menu
, index
, index
==menu
->selected_entry
);
1107 paintEntry(menu
->brother
, index
, index
==menu
->selected_entry
);
1111 /* ====================================================================== */
1115 editEntry(WMenu
*menu
, WMenuEntry
*entry
)
1119 WObjDescriptor
*desc
;
1125 menu
->flags
.editing
= 1;
1127 text
= wTextCreate(menu
->menu
, 1, menu
->entry_height
* entry
->order
,
1128 menu
->menu
->width
- 2, menu
->entry_height
- 1);
1130 wTextPutText(text
, entry
->text
);
1131 XGetInputFocus(dpy
, &old_focus
, &old_revert
);
1132 XSetInputFocus(dpy
, text
->core
->window
, RevertToNone
, CurrentTime
);
1134 if (XGrabKeyboard(dpy
, text
->core
->window
, True
, GrabModeAsync
,
1135 GrabModeAsync
, CurrentTime
)!=GrabSuccess
) {
1136 wwarning("could not grab keyboard");
1139 wSetFocusTo(menu
->frame
->screen_ptr
,
1140 menu
->frame
->screen_ptr
->focused_window
);
1145 while (!done
&& !text
->done
) {
1147 XAllowEvents(dpy
, AsyncKeyboard
|AsyncPointer
, CurrentTime
);
1149 WMNextEvent(dpy
, &event
);
1151 if (XFindContext(dpy
, event
.xany
.window
, wWinContext
,
1152 (XPointer
*)&desc
)==XCNOENT
)
1155 if ((desc
!= NULL
) && (desc
->handle_anything
!= NULL
)) {
1157 (*desc
->handle_anything
)(desc
, &event
);
1160 switch (event
.type
) {
1162 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
1166 WMHandleEvent(&event
);
1172 XSetInputFocus(dpy
, old_focus
, old_revert
, CurrentTime
);
1174 wSetFocusTo(menu
->frame
->screen_ptr
,
1175 menu
->frame
->screen_ptr
->focused_window
);
1178 t
= wTextGetText(text
);
1179 /* if !t, the user has canceled editing */
1183 entry
->text
= wstrdup(t
);
1185 menu
->flags
.realized
= 0;
1189 XUngrabKeyboard(dpy
, CurrentTime
);
1191 if (t
&& menu
->on_edit
)
1192 (*menu
->on_edit
)(menu
, entry
);
1194 menu
->flags
.editing
= 0;
1196 if (!menu
->flags
.realized
)
1202 selectEntry(WMenu
*menu
, int entry_no
)
1208 if (menu
->entries
==NULL
)
1211 if (entry_no
>= menu
->entry_no
)
1214 old_entry
= menu
->selected_entry
;
1215 menu
->selected_entry
= entry_no
;
1217 if (old_entry
!=entry_no
) {
1219 /* unselect previous entry */
1221 paintEntry(menu
, old_entry
, False
);
1222 entry
= menu
->entries
[old_entry
];
1225 if (entry
->cascade
>=0 && menu
->cascades
) {
1226 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1227 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1233 menu
->selected_entry
= -1;
1236 entry
= menu
->entries
[entry_no
];
1238 if (entry
->cascade
>=0 && menu
->cascades
&& entry
->flags
.enabled
) {
1239 /* Callback for when the submenu is opened.
1241 submenu
= menu
->cascades
[entry
->cascade
];
1242 if (submenu
&& submenu
->flags
.brother
)
1243 submenu
= submenu
->brother
;
1245 if (entry
->callback
) {
1246 /* Only call the callback if the submenu is not yet mapped.
1248 if (menu
->flags
.brother
) {
1249 if (!submenu
|| !submenu
->flags
.mapped
)
1250 (*entry
->callback
)(menu
->brother
, entry
);
1252 if (!submenu
|| !submenu
->flags
.buttoned
)
1253 (*entry
->callback
)(menu
, entry
);
1257 /* the submenu menu might have changed */
1258 submenu
= menu
->cascades
[entry
->cascade
];
1261 if (!submenu
->flags
.mapped
) {
1264 if (!submenu
->flags
.realized
)
1265 wMenuRealize(submenu
);
1266 if (wPreferences
.wrap_menus
) {
1267 if (menu
->flags
.open_to_left
)
1268 submenu
->flags
.open_to_left
= 1;
1270 if (submenu
->flags
.open_to_left
) {
1271 x
= menu
->frame_x
- MENUW(submenu
);
1274 submenu
->flags
.open_to_left
= 0;
1277 x
= menu
->frame_x
+ MENUW(menu
);
1279 if (x
+ MENUW(submenu
)
1280 >= menu
->frame
->screen_ptr
->scr_width
) {
1282 x
= menu
->frame_x
- MENUW(submenu
);
1283 submenu
->flags
.open_to_left
= 1;
1287 x
= menu
->frame_x
+ MENUW(menu
);
1290 if (wPreferences
.align_menus
) {
1293 y
= menu
->frame_y
+ menu
->entry_height
*entry_no
;
1294 if (menu
->flags
.titled
)
1295 y
+= menu
->frame
->top_width
;
1296 if (menu
->cascades
[entry
->cascade
]->flags
.titled
)
1297 y
-= menu
->cascades
[entry
->cascade
]->frame
->top_width
;
1300 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1301 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1306 paintEntry(menu
, entry_no
, True
);
1312 findMenu(WScreen
*scr
, int *x_ret
, int *y_ret
)
1315 WObjDescriptor
*desc
;
1316 Window root_ret
, win
, junk_win
;
1320 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
,
1323 if (win
==None
) return NULL
;
1325 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1328 if (desc
->parent_type
== WCLASS_MENU
) {
1329 menu
= (WMenu
*)desc
->parent
;
1330 XTranslateCoordinates(dpy
, root_ret
, menu
->menu
->window
, wx
, wy
,
1331 x_ret
, y_ret
, &junk_win
);
1341 closeCascade(WMenu
*menu
)
1343 WMenu
*parent
=menu
->parent
;
1345 if (menu
->flags
.brother
1346 || (!menu
->flags
.buttoned
1347 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
))) {
1349 selectEntry(menu
, -1);
1351 #if (MENU_BLINK_DELAY > 2)
1352 wusleep(MENU_BLINK_DELAY
/2);
1356 && (parent
->parent
!=NULL
|| !parent
->flags
.app_menu
1357 || parent
->flags
.brother
)
1358 && !parent
->flags
.buttoned
) {
1359 selectEntry(parent
, -1);
1361 parent
= parent
->parent
;
1364 selectEntry(parent
, -1);
1370 closeBrotherCascadesOf(WMenu
*menu
)
1375 for (i
=0; i
<menu
->cascade_no
; i
++) {
1376 if (menu
->cascades
[i
]->flags
.brother
) {
1377 tmp
= menu
->cascades
[i
];
1379 tmp
= menu
->cascades
[i
]->brother
;
1381 if (tmp
->flags
.mapped
) {
1382 selectEntry(tmp
->parent
, -1);
1383 closeBrotherCascadesOf(tmp
);
1390 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1394 parentMenu(WMenu
*menu
)
1399 if (menu
->flags
.buttoned
)
1402 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1403 parent
= menu
->parent
;
1404 if (parent
->selected_entry
< 0)
1406 entry
= parent
->entries
[parent
->selected_entry
];
1407 if (!entry
->flags
.enabled
|| entry
->cascade
<0 || !parent
->cascades
||
1408 parent
->cascades
[entry
->cascade
] != menu
)
1411 if (menu
->flags
.buttoned
)
1421 * Will raise the passed menu, if submenu = 0
1422 * If submenu > 0 will also raise all mapped submenus
1423 * until the first buttoned one
1424 * If submenu < 0 will also raise all mapped parent menus
1425 * until the first buttoned one
1429 raiseMenus(WMenu
*menu
, int submenus
)
1436 wRaiseFrame(menu
->frame
->core
);
1438 if (submenus
>0 && menu
->selected_entry
>=0) {
1439 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
1440 if (i
>=0 && menu
->cascades
) {
1441 submenu
= menu
->cascades
[i
];
1442 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
1443 raiseMenus(submenu
, submenus
);
1446 if (submenus
<0 && !menu
->flags
.buttoned
&&
1447 menu
->parent
&& menu
->parent
->flags
.mapped
)
1448 raiseMenus(menu
->parent
, submenus
);
1453 wMenuUnderPointer(WScreen
*screen
)
1455 WObjDescriptor
*desc
;
1456 Window root_ret
, win
;
1460 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
,
1461 &dummy
, &dummy
, &mask
);
1463 if (win
==None
) return NULL
;
1465 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1468 if (desc
->parent_type
== WCLASS_MENU
)
1469 return (WMenu
*)desc
->parent
;
1475 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1479 getPointerPosition(WScreen
*scr
, int *x
, int *y
)
1481 Window root_ret
, win
;
1485 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1490 getScrollAmount(WMenu
*menu
, int *hamount
, int *vamount
)
1492 WScreen
*scr
= menu
->menu
->screen_ptr
;
1493 int menuX1
= menu
->frame_x
;
1494 int menuY1
= menu
->frame_y
;
1495 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1496 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1497 int screenW
= scr
->scr_width
;
1498 int screenH
= scr
->scr_height
;
1504 getPointerPosition(scr
, &xroot
, &yroot
);
1507 if (xroot
<= 1 && menuX1
< 0) {
1508 /* scroll to the right */
1509 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX1
));
1511 } else if (xroot
>= screenW
-2 && menuX2
> screenW
-1) {
1512 /* scroll to the left */
1513 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX2
-screenW
-1));
1518 *hamount
= -*hamount
;
1521 if (yroot
<= 1 && menuY1
< 0) {
1523 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY1
));
1525 } else if (yroot
>= screenH
-2 && menuY2
> screenH
-1) {
1527 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY2
-screenH
-2));
1529 *vamount
= -*vamount
;
1535 dragScrollMenuCallback(void *data
)
1537 WMenu
*menu
= (WMenu
*)data
;
1538 WScreen
*scr
= menu
->menu
->screen_ptr
;
1539 WMenu
*parent
= parentMenu(menu
);
1540 int hamount
, vamount
;
1542 int newSelectedEntry
;
1544 getScrollAmount(menu
, &hamount
, &vamount
);
1547 if (hamount
!= 0 || vamount
!= 0) {
1548 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1549 parent
->frame_y
+ vamount
, True
);
1550 if (findMenu(scr
, &x
, &y
)) {
1551 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1552 selectEntry(menu
, newSelectedEntry
);
1554 /* Pointer fell outside of menu. If the selected entry is
1555 * not a submenu, unselect it */
1556 if (menu
->selected_entry
>= 0
1557 && menu
->entries
[menu
->selected_entry
]->cascade
<0)
1558 selectEntry(menu
, -1);
1559 newSelectedEntry
= 0;
1562 /* paranoid check */
1563 if (newSelectedEntry
>= 0) {
1564 /* keep scrolling */
1565 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1566 dragScrollMenuCallback
, menu
);
1571 /* don't need to scroll anymore */
1573 if (findMenu(scr
, &x
, &y
)) {
1574 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1575 selectEntry(menu
, newSelectedEntry
);
1582 scrollMenuCallback(void *data
)
1584 WMenu
*menu
= (WMenu
*)data
;
1585 WMenu
*parent
= parentMenu(menu
);
1586 int hamount
= 0; /* amount to scroll */
1589 getScrollAmount(menu
, &hamount
, &vamount
);
1591 if (hamount
!= 0 || vamount
!= 0) {
1592 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1593 parent
->frame_y
+ vamount
, True
);
1595 /* keep scrolling */
1596 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1597 scrollMenuCallback
, menu
);
1599 /* don't need to scroll anymore */
1606 #define MENU_SCROLL_BORDER 5
1609 isPointNearBoder(WMenu
*menu
, int x
, int y
)
1611 int menuX1
= menu
->frame_x
;
1612 int menuY1
= menu
->frame_y
;
1613 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1614 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1615 int scrXe
= menu
->menu
->screen_ptr
->scr_width
-1;
1616 int scrYe
= menu
->menu
->screen_ptr
->scr_height
-1;
1619 if (x
>= menuX1
&& x
<= menuX2
&& (y
< MENU_SCROLL_BORDER
1620 || y
> scrYe
-MENU_SCROLL_BORDER
))
1622 else if (y
>= menuY1
&& y
<= menuY2
&& (x
< MENU_SCROLL_BORDER
1623 || x
> scrXe
-MENU_SCROLL_BORDER
))
1631 wMenuScroll(WMenu
*menu
, XEvent
*event
)
1634 WMenu
*omenu
= parentMenu(menu
);
1635 WScreen
*scr
= menu
->frame
->screen_ptr
;
1638 int old_frame_x
= omenu
->frame_x
;
1639 int old_frame_y
= omenu
->frame_y
;
1643 puts("Entering menu Scroll");
1646 if ((/*omenu->flags.buttoned &&*/ !wPreferences
.wrap_menus
)
1647 || omenu
->flags
.app_menu
) {
1651 if (!wPreferences
.wrap_menus
)
1652 raiseMenus(omenu
, True
);
1654 raiseMenus(menu
, False
);
1657 scrollMenuCallback(menu
);
1660 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1662 WMNextEvent(dpy
, &ev
);
1667 x
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1668 y
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1670 /* on_border is != 0 if the pointer is between the menu
1671 * and the screen border and is close enough to the border */
1672 on_border
= isPointNearBoder(menu
, x
, y
);
1674 smenu
= wMenuUnderPointer(scr
);
1676 if ((smenu
==NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
)!=omenu
)) {
1681 on_x_edge
= x
<= 1 || x
>= scr
->scr_width
- 2;
1682 on_y_edge
= y
<= 1 || y
>= scr
->scr_height
- 2;
1683 on_border
= on_x_edge
|| on_y_edge
;
1685 if (!on_border
&& !jump_back
) {
1690 if (menu
->timer
&& (smenu
!=menu
|| (!on_y_edge
&& !on_x_edge
))) {
1691 WMDeleteTimerHandler(menu
->timer
);
1699 scrollMenuCallback(menu
);
1702 /* True if we push on title, or drag the omenu to other position */
1703 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1704 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1705 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1706 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1708 smenu
= wMenuUnderPointer(scr
);
1709 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1711 else if (smenu
==omenu
&& on_title
) {
1725 WMDeleteTimerHandler(menu
->timer
);
1730 wMenuMove(omenu
, old_frame_x
, old_frame_y
, True
);
1733 puts("Leaving menu Scroll");
1740 menuExpose(WObjDescriptor
*desc
, XEvent
*event
)
1742 wMenuPaint(desc
->parent
);
1746 int *delayed_select
;
1753 delaySelection(void *data
)
1755 delay_data
*d
= (delay_data
*)data
;
1761 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1762 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1763 entry_no
= getEntryAt(menu
, x
, y
);
1764 selectEntry(menu
, entry_no
);
1766 if (d
->delayed_select
)
1767 *(d
->delayed_select
) = 0;
1772 menuMouseDown(WObjDescriptor
*desc
, XEvent
*event
)
1774 XButtonEvent
*bev
= &event
->xbutton
;
1775 WMenu
*menu
= desc
->parent
;
1777 WScreen
*scr
=menu
->frame
->screen_ptr
;
1778 WMenuEntry
*entry
=NULL
;
1780 int close_on_exit
=0;
1782 int delayed_select
= 0;
1786 int old_frame_x
= 0;
1787 int old_frame_y
= 0;
1788 delay_data d_data
= {NULL
, NULL
, NULL
};
1790 if (!wPreferences
.wrap_menus
) {
1791 smenu
= parentMenu(menu
);
1792 old_frame_x
= smenu
->frame_x
;
1793 old_frame_y
= smenu
->frame_y
;
1795 else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1796 /* This is true if the menu was launched with right click on root window */
1798 d_data
.delayed_select
= &delayed_select
;
1800 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
,
1801 delaySelection
, &d_data
);
1804 wRaiseFrame(menu
->frame
->core
);
1806 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1808 smenu
= findMenu(scr
, &x
, &y
);
1816 if (menu
->flags
.editing
) {
1819 entry_no
= getEntryAt(menu
, x
, y
);
1821 entry
= menu
->entries
[entry_no
];
1823 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
1824 && entry
->flags
.editable
) {
1825 editEntry(smenu
, entry
);
1827 } else if (bev
->state
& ControlMask
) {
1831 if (entry
->flags
.enabled
&& entry
->cascade
>=0 && menu
->cascades
) {
1832 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1834 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&&
1835 menu
->selected_entry
!=entry_no
) {
1836 wMenuUnmap(submenu
);
1838 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1839 selectEntry(menu
, entry_no
);
1840 } else if (!submenu
->flags
.buttoned
) {
1841 selectEntry(menu
, -1);
1844 } else if (!delayed_select
) {
1845 selectEntry(menu
, entry_no
);
1848 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1850 dragScrollMenuCallback(menu
);
1854 prevx
= bev
->x_root
;
1855 prevy
= bev
->y_root
;
1859 XAllowEvents(dpy
, SyncPointer
, CurrentTime
);
1861 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonReleaseMask
1862 |ButtonPressMask
, &ev
);
1865 smenu
= findMenu(scr
, &x
, &y
);
1867 if (smenu
== NULL
) {
1868 /* moved mouse out of menu */
1870 if (!delayed_select
&& d_data
.magic
) {
1871 WMDeleteTimerHandler(d_data
.magic
);
1872 d_data
.magic
= NULL
;
1875 || (menu
->selected_entry
>=0
1876 && menu
->entries
[menu
->selected_entry
]->cascade
>=0)) {
1877 prevx
= ev
.xmotion
.x_root
;
1878 prevy
= ev
.xmotion
.y_root
;
1882 selectEntry(menu
, -1);
1884 prevx
= ev
.xmotion
.x_root
;
1885 prevy
= ev
.xmotion
.y_root
;
1887 } else if (menu
&& menu
!=smenu
1888 && (menu
->selected_entry
<0
1889 || menu
->entries
[menu
->selected_entry
]->cascade
<0)) {
1890 selectEntry(menu
, -1);
1892 if (!delayed_select
&& d_data
.magic
) {
1893 WMDeleteTimerHandler(d_data
.magic
);
1894 d_data
.magic
= NULL
;
1898 /* hysteresis for item selection */
1900 /* check if the motion was to the side, indicating that
1901 * the user may want to cross to a submenu */
1902 if (!delayed_select
&& menu
) {
1904 Bool moved_to_submenu
;/* moved to direction of submenu */
1906 dx
= abs(prevx
- ev
.xmotion
.x_root
);
1908 moved_to_submenu
= False
;
1909 if (dx
> 0 /* if moved enough to the side */
1910 /* maybe a open submenu */
1911 && menu
->selected_entry
>=0
1912 /* moving to the right direction */
1913 && (wPreferences
.align_menus
1914 || ev
.xmotion
.y_root
>= prevy
)) {
1917 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
1919 if (menu
->cascades
[index
]->frame_x
>menu
->frame_x
) {
1920 if (prevx
< ev
.xmotion
.x_root
)
1921 moved_to_submenu
= True
;
1923 if (prevx
> ev
.xmotion
.x_root
)
1924 moved_to_submenu
= True
;
1930 if (menu
!= smenu
) {
1932 WMDeleteTimerHandler(d_data
.magic
);
1934 d_data
.magic
= NULL
;
1935 } else if (moved_to_submenu
) {
1936 /* while we are moving, postpone the selection */
1938 WMDeleteTimerHandler(d_data
.magic
);
1940 d_data
.delayed_select
= NULL
;
1942 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
1945 prevx
= ev
.xmotion
.x_root
;
1946 prevy
= ev
.xmotion
.y_root
;
1950 WMDeleteTimerHandler(d_data
.magic
);
1951 d_data
.magic
= NULL
;
1955 prevx
= ev
.xmotion
.x_root
;
1956 prevy
= ev
.xmotion
.y_root
;
1958 /* pointer crossed menus */
1959 if (menu
&& menu
->timer
) {
1960 WMDeleteTimerHandler(menu
->timer
);
1964 dragScrollMenuCallback(smenu
);
1968 dragScrollMenuCallback(menu
);
1970 if (!delayed_select
) {
1971 entry_no
= getEntryAt(menu
, x
, y
);
1973 entry
= menu
->entries
[entry_no
];
1974 if (entry
->flags
.enabled
&& entry
->cascade
>=0 &&
1976 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1977 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
1978 && menu
->selected_entry
!=entry_no
) {
1979 wMenuUnmap(submenu
);
1983 selectEntry(menu
, entry_no
);
1991 if (ev
.xbutton
.button
== event
->xbutton
.button
)
2001 if (menu
&& menu
->timer
) {
2002 WMDeleteTimerHandler(menu
->timer
);
2005 if (d_data
.magic
!=NULL
)
2006 WMDeleteTimerHandler(d_data
.magic
);
2008 if (menu
&& menu
->selected_entry
>=0) {
2009 entry
= menu
->entries
[menu
->selected_entry
];
2010 if (entry
->callback
!=NULL
&& entry
->flags
.enabled
2011 && entry
->cascade
< 0) {
2012 /* blink and erase menu selection */
2013 #if (MENU_BLINK_DELAY > 0)
2014 int sel
= menu
->selected_entry
;
2017 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
2018 paintEntry(menu
, sel
, False
);
2020 wusleep(MENU_BLINK_DELAY
);
2021 paintEntry(menu
, sel
, True
);
2023 wusleep(MENU_BLINK_DELAY
);
2026 /* unmap the menu, it's parents and call the callback */
2027 if (!menu
->flags
.buttoned
&&
2028 (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2031 selectEntry(menu
, -1);
2033 (*entry
->callback
)(menu
, entry
);
2035 /* If the user double clicks an entry, the entry will
2036 * be executed twice, which is not good for things like
2037 * the root menu. So, ignore any clicks that were generated
2038 * while the entry was being executed */
2039 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
,
2041 } else if (entry
->callback
!=NULL
&& entry
->cascade
<0) {
2042 selectEntry(menu
, -1);
2044 if (entry
->cascade
>=0 && menu
->cascades
2045 && menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2046 selectEntry(menu
, -1);
2051 if (((WMenu
*)desc
->parent
)->flags
.brother
|| close_on_exit
2053 closeCascade(desc
->parent
);
2055 /* close the cascade windows that should not remain opened */
2056 closeBrotherCascadesOf(desc
->parent
);
2058 if (!wPreferences
.wrap_menus
)
2059 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2064 wMenuMove(WMenu
*menu
, int x
, int y
, int submenus
)
2073 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2075 if (submenus
>0 && menu
->selected_entry
>=0) {
2076 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2078 if (i
>=0 && menu
->cascades
) {
2079 submenu
= menu
->cascades
[i
];
2080 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
2081 if (wPreferences
.align_menus
) {
2082 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2084 wMenuMove(submenu
, x
+ MENUW(menu
),
2085 y
+ submenu
->entry_height
*menu
->selected_entry
,
2090 if (submenus
<0 && menu
->parent
!=NULL
&& menu
->parent
->flags
.mapped
&&
2091 !menu
->parent
->flags
.buttoned
) {
2092 if (wPreferences
.align_menus
) {
2093 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2095 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2096 - menu
->parent
->entry_height
*menu
->parent
->selected_entry
,
2104 changeMenuLevels(WMenu
*menu
, int lower
)
2109 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
2111 wRaiseFrame(menu
->frame
->core
);
2112 menu
->flags
.lowered
= 0;
2114 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2115 wLowerFrame(menu
->frame
->core
);
2116 menu
->flags
.lowered
= 1;
2118 for (i
=0; i
<menu
->cascade_no
; i
++) {
2119 if (menu
->cascades
[i
]
2120 && !menu
->cascades
[i
]->flags
.buttoned
2121 && menu
->cascades
[i
]->flags
.lowered
!=lower
) {
2122 changeMenuLevels(menu
->cascades
[i
], lower
);
2130 menuTitleDoubleClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2135 if (event
->xbutton
.state
& MOD_MASK
) {
2136 if (menu
->flags
.lowered
) {
2141 changeMenuLevels(menu
, lower
);
2147 menuTitleMouseDown(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2152 int x
=menu
->frame_x
, y
=menu
->frame_y
;
2153 int dx
=event
->xbutton
.x_root
, dy
=event
->xbutton
.y_root
;
2158 printf("Moving menu\n");
2161 /* can't touch the menu copy */
2162 if (menu
->flags
.brother
)
2165 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2168 if (event
->xbutton
.state
& MOD_MASK
) {
2169 wLowerFrame(menu
->frame
->core
);
2172 wRaiseFrame(menu
->frame
->core
);
2177 /* lower/raise all submenus */
2179 if (tmp
->selected_entry
>=0 && tmp
->cascades
2180 && tmp
->entries
[tmp
->selected_entry
]->cascade
>=0) {
2181 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2182 if (!tmp
|| !tmp
->flags
.mapped
)
2185 wLowerFrame(tmp
->frame
->core
);
2187 wRaiseFrame(tmp
->frame
->core
);
2193 /* tear off the menu if it's a root menu or a cascade
2195 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
2196 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2197 menu
->flags
.buttoned
=1;
2198 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2200 /* turn off selected menu entry in parent menu */
2201 selectEntry(menu
->parent
, -1);
2203 /* make parent map the copy in place of the original */
2204 for (i
=0; i
<menu
->parent
->cascade_no
; i
++) {
2205 if (menu
->parent
->cascades
[i
] == menu
) {
2206 menu
->parent
->cascades
[i
] = menu
->brother
;
2215 WMMaskEvent(dpy
, ButtonMotionMask
|ButtonReleaseMask
|ButtonPressMask
2216 |ExposureMask
, &ev
);
2220 x
+= ev
.xmotion
.x_root
- dx
;
2221 y
+= ev
.xmotion
.y_root
- dy
;
2222 dx
= ev
.xmotion
.x_root
;
2223 dy
= ev
.xmotion
.y_root
;
2224 wMenuMove(menu
, x
, y
, True
);
2226 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2227 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2229 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2230 ButtonMotionMask
|ButtonReleaseMask
2232 GrabModeAsync
, GrabModeAsync
, None
,
2233 wCursor
[WCUR_MOVE
], CurrentTime
);
2242 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2245 printf("End menu move\n");
2247 XUngrabPointer(dpy
, CurrentTime
);
2258 *----------------------------------------------------------------------
2260 * Handles mouse click on the close button of menus. The menu is
2261 * closed when the button is clicked.
2264 * The closed menu is reinserted at it's parent menus
2266 *----------------------------------------------------------------------
2269 menuCloseClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2271 WMenu
*menu
= (WMenu
*)data
;
2272 WMenu
*parent
= menu
->parent
;
2276 for (i
=0; i
<parent
->cascade_no
; i
++) {
2277 /* find the entry that points to the copy */
2278 if (parent
->cascades
[i
] == menu
->brother
) {
2279 /* make it point to the original */
2280 parent
->cascades
[i
] = menu
;
2281 menu
->parent
= parent
;
2291 wMenuSaveState(WScreen
*scr
)
2293 proplist_t menus
, key
, value
;
2297 menus
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
2300 if (scr
->root_menu
&& scr
->root_menu
->flags
.buttoned
) {
2301 sprintf(buffer
, "%i,%i", scr
->root_menu
->frame_x
,
2302 scr
->root_menu
->frame_y
);
2303 key
= PLMakeString("RootMenu");
2304 value
= PLMakeString(buffer
);
2305 PLInsertDictionaryEntry(menus
, key
, value
);
2311 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2312 sprintf(buffer
, "%i,%i", scr
->switch_menu
->frame_x
,
2313 scr
->switch_menu
->frame_y
);
2314 key
= PLMakeString("SwitchMenu");
2315 value
= PLMakeString(buffer
);
2316 PLInsertDictionaryEntry(menus
, key
, value
);
2322 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2323 sprintf(buffer
, "%i,%i", scr
->workspace_menu
->frame_x
,
2324 scr
->workspace_menu
->frame_y
);
2325 key
= PLMakeString("WorkspaceMenu");
2326 value
= PLMakeString(buffer
);
2327 PLInsertDictionaryEntry(menus
, key
, value
);
2334 key
= PLMakeString("Menus");
2335 PLInsertDictionaryEntry(scr
->session_state
, key
, menus
);
2342 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2346 restoreMenu(WScreen
*scr
, proplist_t menu
, int which
)
2349 WMenu
*pmenu
= NULL
;
2354 if (!PLIsString(menu
)) {
2355 COMPLAIN("Position");
2359 if (sscanf(PLGetString(menu
), "%i,%i", &x
, &y
)!=2)
2360 COMPLAIN("Position");
2363 if (which
& WSS_ROOTMENU
) {
2364 OpenRootMenu(scr
, x
, y
, False
);
2365 pmenu
= scr
->root_menu
;
2366 } else if (which
& WSS_SWITCHMENU
) {
2367 OpenSwitchMenu(scr
, x
, y
, False
);
2368 pmenu
= scr
->switch_menu
;
2371 if (which
& WSS_WSMENU
) {
2372 OpenWorkspaceMenu(scr
, x
, y
);
2373 pmenu
= scr
->workspace_menu
;
2374 if (pmenu
->parent
) {
2375 /* make parent map the copy in place of the original */
2376 for (i
=0; i
<pmenu
->parent
->cascade_no
; i
++) {
2377 if (pmenu
->parent
->cascades
[i
] == pmenu
) {
2378 pmenu
->parent
->cascades
[i
] = pmenu
->brother
;
2386 int width
= MENUW(pmenu
);
2387 int height
= MENUH(pmenu
);
2389 x
= (x
< -width
) ? 0 : x
;
2390 x
= (x
> scr
->scr_width
) ? scr
->scr_width
- width
: x
;
2391 y
= (y
< 0) ? 0 : y
;
2392 y
= (y
> scr
->scr_height
) ? scr
->scr_height
- height
: y
;
2393 wMenuMove(pmenu
, x
, y
, True
);
2394 pmenu
->flags
.buttoned
= 1;
2395 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2403 wMenuRestoreState(WScreen
*scr
)
2405 proplist_t menus
, menu
, key
, rkey
, skey
, wkey
;
2407 key
= PLMakeString("Menus");
2408 menus
= PLGetDictionaryEntry(scr
->session_state
, key
);
2416 rkey
= PLMakeString("RootMenu");
2417 skey
= PLMakeString("SwitchMenu");
2418 wkey
= PLMakeString("WorkspaceMenu");
2419 menu
= PLGetDictionaryEntry(menus
, rkey
);
2420 restoreMenu(scr
, menu
, WSS_ROOTMENU
);
2421 menu
= PLGetDictionaryEntry(menus
, skey
);
2422 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2423 menu
= PLGetDictionaryEntry(menus
, wkey
);
2424 restoreMenu(scr
, menu
, WSS_WSMENU
);
2433 OpenWorkspaceMenu(WScreen
*scr
, int x
, int y
)
2435 WMenu
*menu
, *parent
;
2439 if (!scr
->root_menu
) {
2440 OpenRootMenu(scr
, scr
->scr_width
*2, 0, False
);
2441 wMenuUnmap(scr
->root_menu
);
2444 menu
= scr
->workspace_menu
;
2446 if (menu
->flags
.mapped
) {
2447 if (!menu
->flags
.buttoned
) {
2449 parent
= menu
->parent
;
2450 if (parent
&& parent
->selected_entry
>= 0) {
2451 entry
= parent
->entries
[parent
->selected_entry
];
2452 if (parent
->cascades
[entry
->cascade
] == menu
) {
2453 selectEntry(parent
, -1);
2454 wMenuMapAt(menu
, x
, y
, False
);
2458 wRaiseFrame(menu
->frame
->core
);
2459 wMenuMapCopyAt(menu
, x
, y
);
2463 wMenuMapAt(menu
, x
, y
, False
);