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
+ (wPreferences
.new_style
? 16 : 8);
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
))+10;
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
)
498 wCoreConfigure(menu
->menu
, 0, theight
, mwidth
, menu
->entry_no
*eheight
-1);
500 wFrameWindowResize(menu
->frame
, mwidth
, menu
->entry_no
*eheight
-1
501 + menu
->frame
->top_width
+ menu
->frame
->bottom_width
);
504 /* setup background texture */
505 if (scr
->menu_item_texture
->any
.type
!= WTEX_SOLID
) {
506 if (!menu
->flags
.brother
) {
507 FREE_PIXMAP(menu
->menu_texture_data
);
509 wTextureRender(scr
, scr
->menu_item_texture
,
510 &menu
->menu_texture_data
, menu
->menu
->width
,
511 menu
->entry_height
, WREL_MENUENTRY
);
513 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
,
514 menu
->menu_texture_data
);
515 XClearWindow(dpy
, menu
->menu
->window
);
517 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
,
518 menu
->menu_texture_data
);
519 XClearWindow(dpy
, menu
->brother
->menu
->window
);
522 XSetWindowBackground(dpy
, menu
->menu
->window
,
523 scr
->menu_item_texture
->any
.color
.pixel
);
524 XClearWindow(dpy
, menu
->menu
->window
);
527 menu
->flags
.realized
= 1;
529 if (menu
->flags
.mapped
)
531 if (menu
->brother
->flags
.mapped
)
532 wMenuPaint(menu
->brother
);
537 wMenuDestroy(WMenu
*menu
, int recurse
)
541 /* remove any pending timers */
543 WMDeleteTimerHandler(menu
->timer
);
546 /* call destroy handler */
547 if (menu
->on_destroy
)
548 (*menu
->on_destroy
)(menu
);
550 /* Destroy items if this menu own them. If this is the "brother" menu,
551 * leave them alone as it is shared by them.
553 if (!menu
->flags
.brother
) {
554 for (i
=0; i
<menu
->entry_no
; i
++) {
556 free(menu
->entries
[i
]->text
);
558 if (menu
->entries
[i
]->rtext
)
559 free(menu
->entries
[i
]->rtext
);
561 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
562 (*menu
->entries
[i
]->free_cdata
)(menu
->entries
[i
]->clientdata
);
564 free(menu
->entries
[i
]);
568 for (i
=0; i
<menu
->cascade_no
; i
++) {
569 if (menu
->cascades
[i
]) {
570 if (menu
->cascades
[i
]->flags
.brother
)
571 wMenuDestroy(menu
->cascades
[i
]->brother
, recurse
);
573 wMenuDestroy(menu
->cascades
[i
], recurse
);
583 FREE_PIXMAP(menu
->menu_texture_data
);
586 free(menu
->cascades
);
588 wCoreDestroy(menu
->menu
);
589 wFrameWindowDestroy(menu
->frame
);
591 /* destroy copy of this menu */
592 if (!menu
->flags
.brother
&& menu
->brother
)
593 wMenuDestroy(menu
->brother
, False
);
600 drawFrame(WScreen
*scr
, Window win
, int y
, int w
, int h
)
611 segs
[1].y2
= y
+ h
- 2;
612 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, 2);
614 XDrawLine(dpy
, win
, scr
->menu_item_auxtexture
->dark_gc
, 0, y
+h
-1,
618 segs
[0].y1
= segs
[0].y2
= y
+ h
-2;
620 segs
[1].x1
= segs
[1].x2
= w
-1;
622 segs
[1].y2
= y
+ h
-2;
623 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, 2);
628 paintEntry(WMenu
*menu
, int index
, int selected
)
631 GC light
, dim
, dark
, textGC
;
632 WScreen
*scr
=menu
->frame
->screen_ptr
;
633 Window win
= menu
->menu
->window
;
634 WMenuEntry
*entry
=menu
->entries
[index
];
636 if (!menu
->flags
.realized
) return;
637 h
= menu
->entry_height
;
638 w
= menu
->menu
->width
;
641 light
= scr
->menu_item_auxtexture
->light_gc
;
642 dim
= scr
->menu_item_auxtexture
->dim_gc
;
643 dark
= scr
->menu_item_auxtexture
->dark_gc
;
645 /* paint background */
647 XSetForeground(dpy
, scr
->select_menu_gc
, scr
->select_pixel
);
648 XFillRectangle(dpy
, win
, scr
->select_menu_gc
, 1, y
+1, w
-2, h
-3);
649 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
)
650 drawFrame(scr
, win
, y
, w
, h
);
652 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
653 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
655 drawFrame(scr
, win
, y
, w
, h
);
657 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
662 textGC
= scr
->select_menu_gc
;
663 if (entry
->flags
.enabled
)
664 XSetForeground(dpy
, textGC
, scr
->select_text_pixel
);
666 XSetForeground(dpy
, textGC
, scr
->dtext_pixel
);
667 } else if (!entry
->flags
.enabled
) {
668 textGC
= scr
->disabled_menu_entry_gc
;
670 textGC
= scr
->menu_entry_gc
;
674 if (entry
->flags
.indicator
)
675 x
+= MENU_INDICATOR_SPACE
+ 2;
677 wDrawString(win
, scr
->menu_entry_font
,
678 textGC
, x
, 3+y
+scr
->menu_entry_font
->y
, entry
->text
,
679 strlen(entry
->text
));
681 if (entry
->cascade
>=0) {
682 /* draw the cascade indicator */
683 XDrawLine(dpy
,win
,dim
, w
-11, y
+6, w
-6, y
+h
/2-1);
684 XDrawLine(dpy
,win
,light
, w
-11, y
+h
-8, w
-6, y
+h
/2-1);
685 XDrawLine(dpy
,win
,dark
, w
-12, y
+6, w
-12, y
+h
-8);
689 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
694 switch (entry
->flags
.indicator_type
) {
696 indicator
= scr
->menu_check_indicator
;
699 indicator
= scr
->menu_mini_indicator
;
702 indicator
= scr
->menu_hide_indicator
;
705 indicator
= scr
->menu_shade_indicator
;
709 indicator
= scr
->menu_radio_indicator
;
713 iw
= indicator
->width
;
714 ih
= indicator
->height
;
715 XSetClipMask(dpy
, scr
->copy_gc
, indicator
->mask
);
716 XSetClipOrigin(dpy
, scr
->copy_gc
, 5, y
+(h
-ih
)/2);
718 XSetForeground(dpy
, scr
->copy_gc
, scr
->black_pixel
);
720 XSetForeground(dpy
, scr
->copy_gc
, scr
->mtext_pixel
);
721 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+(h
-ih
)/2, iw
, ih
);
723 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
724 iw, ih, 5, y+(h-ih)/2);
726 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
729 /* draw right text */
731 if (entry
->rtext
&& entry
->cascade
<0) {
732 tw
= wTextWidth(scr
->menu_entry_font
->font
, entry
->rtext
,
733 strlen(entry
->rtext
));
735 wDrawString(win
, scr
->menu_entry_font
, textGC
, w
-6-tw
,
736 3+y
+scr
->menu_entry_font
->y
, entry
->rtext
,
737 strlen(entry
->rtext
));
742 move_menus(WMenu
*menu
, int x
, int y
)
744 while (menu
->parent
) {
747 if (!wPreferences
.align_menus
&& menu
->selected_entry
>=0) {
748 y
-= menu
->selected_entry
*menu
->entry_height
;
751 wMenuMove(menu
, x
, y
, True
);
755 makeVisible(WMenu
*menu
)
757 WScreen
*scr
= menu
->frame
->screen_ptr
;
758 int x1
, y1
, x2
, y2
, new_x
, new_y
, move
;
760 if (menu
->entry_no
<0) return;
763 y1
= menu
->frame_y
+menu
->frame
->top_width
764 + menu
->selected_entry
*menu
->entry_height
;
765 x2
= x1
+ MENUW(menu
);
766 y2
= y1
+ menu
->entry_height
;
775 } else if (x2
>= scr
->scr_width
) {
776 new_x
= scr
->scr_width
- MENUW(menu
) - 1;
783 } else if (y2
>= scr
->scr_height
) {
784 new_y
= scr
->scr_height
- menu
->entry_height
- 1;
788 new_y
= new_y
- menu
->frame
->top_width
789 - menu
->selected_entry
*menu
->entry_height
;
790 move_menus(menu
, new_x
, new_y
);
795 check_key(WMenu
*menu
, XKeyEvent
*event
)
800 if (XLookupString(event
, buffer
, 32, NULL
, NULL
)<1)
803 ch
= toupper(buffer
[0]);
805 s
= (menu
->selected_entry
>=0 ? menu
->selected_entry
+1 : 0);
808 for (i
=s
; i
<menu
->entry_no
; i
++) {
809 if (ch
==toupper(menu
->entries
[i
]->text
[0])) {
813 /* no match. Retry from start, if previous started from a selected entry */
823 keyboardMenu(WMenu
*menu
)
826 KeySym ksym
=NoSymbol
;
830 int old_pos_x
= menu
->frame_x
;
831 int old_pos_y
= menu
->frame_y
;
832 int new_x
= old_pos_x
, new_y
= old_pos_y
;
833 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
834 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
836 if (menu
->flags
.editing
)
840 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
,
841 GrabModeAsync
, CurrentTime
);
843 if (menu
->frame_y
+menu
->frame
->top_width
>= scr_height
)
844 new_y
= scr_height
- menu
->frame
->top_width
;
846 if (menu
->frame_x
+MENUW(menu
) >= scr_width
)
847 new_x
= scr_width
-MENUW(menu
)-1;
849 move_menus(menu
, new_x
, new_y
);
851 while (!done
&& menu
->flags
.mapped
) {
852 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
853 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonPressMask
854 |ButtonReleaseMask
|KeyPressMask
|KeyReleaseMask
855 |SubstructureNotifyMask
, &event
);
857 switch (event
.type
) {
859 ksym
= XLookupKeysym(&event
.xkey
, 0);
867 selectEntry(menu
, 0);
873 selectEntry(menu
, menu
->entry_no
-1);
882 if (menu
->selected_entry
<= 0)
883 selectEntry(menu
, menu
->entry_no
-1);
885 selectEntry(menu
, menu
->selected_entry
-1);
894 if (menu
->selected_entry
<0)
895 selectEntry(menu
, 0);
896 else if (menu
->selected_entry
== menu
->entry_no
-1)
897 selectEntry(menu
, 0);
898 else if (menu
->selected_entry
< menu
->entry_no
-1)
899 selectEntry(menu
, menu
->selected_entry
+1);
908 if (menu
->selected_entry
>=0) {
910 entry
= menu
->entries
[menu
->selected_entry
];
912 if (entry
->cascade
>= 0 && menu
->cascades
913 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
915 XUngrabKeyboard(dpy
, CurrentTime
);
917 selectEntry(menu
->cascades
[entry
->cascade
], 0);
918 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
921 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
922 GrabModeAsync
, GrabModeAsync
,
933 if (menu
->parent
!=NULL
&& menu
->parent
->selected_entry
>=0) {
934 selectEntry(menu
, -1);
935 move_menus(menu
, old_pos_x
, old_pos_y
);
945 index
= check_key(menu
, &event
.xkey
);
947 selectEntry(menu
, index
);
953 if (event
.type
==ButtonPress
)
956 WMHandleEvent(&event
);
960 XUngrabKeyboard(dpy
, CurrentTime
);
962 if (done
==2 && menu
->selected_entry
>=0) {
963 entry
= menu
->entries
[menu
->selected_entry
];
968 if (entry
&& entry
->callback
!=NULL
&& entry
->flags
.enabled
969 && entry
->cascade
< 0) {
970 #if (MENU_BLINK_COUNT > 0)
971 int sel
= menu
->selected_entry
;
974 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
975 paintEntry(menu
, sel
, False
);
977 wusleep(MENU_BLINK_DELAY
);
978 paintEntry(menu
, sel
, True
);
980 wusleep(MENU_BLINK_DELAY
);
983 selectEntry(menu
, -1);
985 if (!menu
->flags
.buttoned
) {
987 move_menus(menu
, old_pos_x
, old_pos_y
);
991 (*entry
->callback
)(menu
, entry
);
993 if (!menu
->flags
.buttoned
) {
995 move_menus(menu
, old_pos_x
, old_pos_y
);
997 selectEntry(menu
, -1);
1001 /* returns True if returning from a submenu to a parent menu,
1002 * False if exiting from menu */
1008 wMenuMapAt(WMenu
*menu
, int x
, int y
, int keyboard
)
1010 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
1011 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
1013 if (!menu
->flags
.realized
) {
1014 menu
->flags
.realized
=1;
1017 if (!menu
->flags
.mapped
) {
1018 if (wPreferences
.wrap_menus
) {
1021 if (x
+MENUW(menu
) > scr_width
)
1022 x
= scr_width
- MENUW(menu
);
1023 if (y
+MENUH(menu
) > scr_height
)
1024 y
= scr_height
- MENUH(menu
);
1027 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
1030 XMapWindow(dpy
, menu
->frame
->core
->window
);
1031 wRaiseFrame(menu
->frame
->core
);
1032 menu
->flags
.mapped
= 1;
1034 selectEntry(menu
, 0);
1043 wMenuMap(WMenu
*menu
)
1045 if (!menu
->flags
.realized
) {
1046 menu
->flags
.realized
=1;
1049 if (menu
->flags
.app_menu
&& menu
->parent
==NULL
) {
1050 menu
->frame_x
= menu
->frame
->screen_ptr
->app_menu_x
;
1051 menu
->frame_y
= menu
->frame
->screen_ptr
->app_menu_y
;
1052 XMoveWindow(dpy
, menu
->frame
->core
->window
, menu
->frame_x
, menu
->frame_y
);
1054 XMapWindow(dpy
, menu
->frame
->core
->window
);
1055 wRaiseFrame(menu
->frame
->core
);
1056 menu
->flags
.mapped
= 1;
1061 wMenuUnmap(WMenu
*menu
)
1065 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1066 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1067 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1069 menu
->flags
.buttoned
= 0;
1070 menu
->flags
.mapped
= 0;
1071 menu
->flags
.open_to_left
= 0;
1073 for (i
=0; i
<menu
->cascade_no
; i
++) {
1074 if (menu
->cascades
[i
]!=NULL
1075 && menu
->cascades
[i
]->flags
.mapped
1076 && !menu
->cascades
[i
]->flags
.buttoned
) {
1078 wMenuUnmap(menu
->cascades
[i
]);
1081 menu
->selected_entry
= -1;
1087 wMenuPaint(WMenu
*menu
)
1091 if (!menu
->flags
.mapped
) {
1096 for (i
=0; i
<menu
->entry_no
; i
++) {
1097 paintEntry(menu
, i
, i
==menu
->selected_entry
);
1103 wMenuSetEnabled(WMenu
*menu
, int index
, int enable
)
1105 if (index
>=menu
->entry_no
) return;
1106 menu
->entries
[index
]->flags
.enabled
=enable
;
1107 paintEntry(menu
, index
, index
==menu
->selected_entry
);
1108 paintEntry(menu
->brother
, index
, index
==menu
->selected_entry
);
1112 /* ====================================================================== */
1116 editEntry(WMenu
*menu
, WMenuEntry
*entry
)
1120 WObjDescriptor
*desc
;
1126 menu
->flags
.editing
= 1;
1128 text
= wTextCreate(menu
->menu
, 1, menu
->entry_height
* entry
->order
,
1129 menu
->menu
->width
- 2, menu
->entry_height
- 1);
1131 wTextPutText(text
, entry
->text
);
1132 XGetInputFocus(dpy
, &old_focus
, &old_revert
);
1133 XSetInputFocus(dpy
, text
->core
->window
, RevertToNone
, CurrentTime
);
1135 if (XGrabKeyboard(dpy
, text
->core
->window
, True
, GrabModeAsync
,
1136 GrabModeAsync
, CurrentTime
)!=GrabSuccess
) {
1137 wwarning("could not grab keyboard");
1140 wSetFocusTo(menu
->frame
->screen_ptr
,
1141 menu
->frame
->screen_ptr
->focused_window
);
1146 while (!done
&& !text
->done
) {
1148 XAllowEvents(dpy
, AsyncKeyboard
|AsyncPointer
, CurrentTime
);
1150 WMNextEvent(dpy
, &event
);
1152 if (XFindContext(dpy
, event
.xany
.window
, wWinContext
,
1153 (XPointer
*)&desc
)==XCNOENT
)
1156 if ((desc
!= NULL
) && (desc
->handle_anything
!= NULL
)) {
1158 (*desc
->handle_anything
)(desc
, &event
);
1161 switch (event
.type
) {
1163 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
1167 WMHandleEvent(&event
);
1173 XSetInputFocus(dpy
, old_focus
, old_revert
, CurrentTime
);
1175 wSetFocusTo(menu
->frame
->screen_ptr
,
1176 menu
->frame
->screen_ptr
->focused_window
);
1179 t
= wTextGetText(text
);
1180 /* if !t, the user has canceled editing */
1184 entry
->text
= wstrdup(t
);
1186 menu
->flags
.realized
= 0;
1190 XUngrabKeyboard(dpy
, CurrentTime
);
1192 if (t
&& menu
->on_edit
)
1193 (*menu
->on_edit
)(menu
, entry
);
1195 menu
->flags
.editing
= 0;
1197 if (!menu
->flags
.realized
)
1203 selectEntry(WMenu
*menu
, int entry_no
)
1209 if (menu
->entries
==NULL
)
1212 if (entry_no
>= menu
->entry_no
)
1215 old_entry
= menu
->selected_entry
;
1216 menu
->selected_entry
= entry_no
;
1218 if (old_entry
!=entry_no
) {
1220 /* unselect previous entry */
1222 paintEntry(menu
, old_entry
, False
);
1223 entry
= menu
->entries
[old_entry
];
1226 if (entry
->cascade
>=0 && menu
->cascades
) {
1227 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1228 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1234 menu
->selected_entry
= -1;
1237 entry
= menu
->entries
[entry_no
];
1239 if (entry
->cascade
>=0 && menu
->cascades
&& entry
->flags
.enabled
) {
1240 /* Callback for when the submenu is opened.
1242 submenu
= menu
->cascades
[entry
->cascade
];
1243 if (submenu
&& submenu
->flags
.brother
)
1244 submenu
= submenu
->brother
;
1246 if (entry
->callback
) {
1247 /* Only call the callback if the submenu is not yet mapped.
1249 if (menu
->flags
.brother
) {
1250 if (!submenu
|| !submenu
->flags
.mapped
)
1251 (*entry
->callback
)(menu
->brother
, entry
);
1253 if (!submenu
|| !submenu
->flags
.buttoned
)
1254 (*entry
->callback
)(menu
, entry
);
1258 /* the submenu menu might have changed */
1259 submenu
= menu
->cascades
[entry
->cascade
];
1262 if (!submenu
->flags
.mapped
) {
1265 if (!submenu
->flags
.realized
)
1266 wMenuRealize(submenu
);
1267 if (wPreferences
.wrap_menus
) {
1268 if (menu
->flags
.open_to_left
)
1269 submenu
->flags
.open_to_left
= 1;
1271 if (submenu
->flags
.open_to_left
) {
1272 x
= menu
->frame_x
- MENUW(submenu
);
1275 submenu
->flags
.open_to_left
= 0;
1278 x
= menu
->frame_x
+ MENUW(menu
);
1280 if (x
+ MENUW(submenu
)
1281 >= menu
->frame
->screen_ptr
->scr_width
) {
1283 x
= menu
->frame_x
- MENUW(submenu
);
1284 submenu
->flags
.open_to_left
= 1;
1288 x
= menu
->frame_x
+ MENUW(menu
);
1291 if (wPreferences
.align_menus
) {
1294 y
= menu
->frame_y
+ menu
->entry_height
*entry_no
;
1295 if (menu
->flags
.titled
)
1296 y
+= menu
->frame
->top_width
;
1297 if (menu
->cascades
[entry
->cascade
]->flags
.titled
)
1298 y
-= menu
->cascades
[entry
->cascade
]->frame
->top_width
;
1301 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1302 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1307 paintEntry(menu
, entry_no
, True
);
1313 findMenu(WScreen
*scr
, int *x_ret
, int *y_ret
)
1316 WObjDescriptor
*desc
;
1317 Window root_ret
, win
, junk_win
;
1321 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
,
1324 if (win
==None
) return NULL
;
1326 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1329 if (desc
->parent_type
== WCLASS_MENU
) {
1330 menu
= (WMenu
*)desc
->parent
;
1331 XTranslateCoordinates(dpy
, root_ret
, menu
->menu
->window
, wx
, wy
,
1332 x_ret
, y_ret
, &junk_win
);
1342 closeCascade(WMenu
*menu
)
1344 WMenu
*parent
=menu
->parent
;
1346 if (menu
->flags
.brother
1347 || (!menu
->flags
.buttoned
1348 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
))) {
1350 selectEntry(menu
, -1);
1352 #if (MENU_BLINK_DELAY > 2)
1353 wusleep(MENU_BLINK_DELAY
/2);
1357 && (parent
->parent
!=NULL
|| !parent
->flags
.app_menu
1358 || parent
->flags
.brother
)
1359 && !parent
->flags
.buttoned
) {
1360 selectEntry(parent
, -1);
1362 parent
= parent
->parent
;
1365 selectEntry(parent
, -1);
1371 closeBrotherCascadesOf(WMenu
*menu
)
1376 for (i
=0; i
<menu
->cascade_no
; i
++) {
1377 if (menu
->cascades
[i
]->flags
.brother
) {
1378 tmp
= menu
->cascades
[i
];
1380 tmp
= menu
->cascades
[i
]->brother
;
1382 if (tmp
->flags
.mapped
) {
1383 selectEntry(tmp
->parent
, -1);
1384 closeBrotherCascadesOf(tmp
);
1391 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1395 parentMenu(WMenu
*menu
)
1400 if (menu
->flags
.buttoned
)
1403 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1404 parent
= menu
->parent
;
1405 if (parent
->selected_entry
< 0)
1407 entry
= parent
->entries
[parent
->selected_entry
];
1408 if (!entry
->flags
.enabled
|| entry
->cascade
<0 || !parent
->cascades
||
1409 parent
->cascades
[entry
->cascade
] != menu
)
1412 if (menu
->flags
.buttoned
)
1422 * Will raise the passed menu, if submenu = 0
1423 * If submenu > 0 will also raise all mapped submenus
1424 * until the first buttoned one
1425 * If submenu < 0 will also raise all mapped parent menus
1426 * until the first buttoned one
1430 raiseMenus(WMenu
*menu
, int submenus
)
1437 wRaiseFrame(menu
->frame
->core
);
1439 if (submenus
>0 && menu
->selected_entry
>=0) {
1440 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
1441 if (i
>=0 && menu
->cascades
) {
1442 submenu
= menu
->cascades
[i
];
1443 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
1444 raiseMenus(submenu
, submenus
);
1447 if (submenus
<0 && !menu
->flags
.buttoned
&&
1448 menu
->parent
&& menu
->parent
->flags
.mapped
)
1449 raiseMenus(menu
->parent
, submenus
);
1454 wMenuUnderPointer(WScreen
*screen
)
1456 WObjDescriptor
*desc
;
1457 Window root_ret
, win
;
1461 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
,
1462 &dummy
, &dummy
, &mask
);
1464 if (win
==None
) return NULL
;
1466 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1469 if (desc
->parent_type
== WCLASS_MENU
)
1470 return (WMenu
*)desc
->parent
;
1476 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1480 getPointerPosition(WScreen
*scr
, int *x
, int *y
)
1482 Window root_ret
, win
;
1486 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1491 getScrollAmount(WMenu
*menu
, int *hamount
, int *vamount
)
1493 WScreen
*scr
= menu
->menu
->screen_ptr
;
1494 int menuX1
= menu
->frame_x
;
1495 int menuY1
= menu
->frame_y
;
1496 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1497 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1498 int screenW
= scr
->scr_width
;
1499 int screenH
= scr
->scr_height
;
1505 getPointerPosition(scr
, &xroot
, &yroot
);
1508 if (xroot
<= 1 && menuX1
< 0) {
1509 /* scroll to the right */
1510 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX1
));
1512 } else if (xroot
>= screenW
-2 && menuX2
> screenW
-1) {
1513 /* scroll to the left */
1514 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX2
-screenW
-1));
1519 *hamount
= -*hamount
;
1522 if (yroot
<= 1 && menuY1
< 0) {
1524 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY1
));
1526 } else if (yroot
>= screenH
-2 && menuY2
> screenH
-1) {
1528 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY2
-screenH
-2));
1530 *vamount
= -*vamount
;
1536 dragScrollMenuCallback(void *data
)
1538 WMenu
*menu
= (WMenu
*)data
;
1539 WScreen
*scr
= menu
->menu
->screen_ptr
;
1540 WMenu
*parent
= parentMenu(menu
);
1541 int hamount
, vamount
;
1543 int newSelectedEntry
;
1545 getScrollAmount(menu
, &hamount
, &vamount
);
1548 if (hamount
!= 0 || vamount
!= 0) {
1549 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1550 parent
->frame_y
+ vamount
, True
);
1551 if (findMenu(scr
, &x
, &y
)) {
1552 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1553 selectEntry(menu
, newSelectedEntry
);
1555 /* Pointer fell outside of menu. If the selected entry is
1556 * not a submenu, unselect it */
1557 if (menu
->selected_entry
>= 0
1558 && menu
->entries
[menu
->selected_entry
]->cascade
<0)
1559 selectEntry(menu
, -1);
1560 newSelectedEntry
= 0;
1563 /* paranoid check */
1564 if (newSelectedEntry
>= 0) {
1565 /* keep scrolling */
1566 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1567 dragScrollMenuCallback
, menu
);
1572 /* don't need to scroll anymore */
1574 if (findMenu(scr
, &x
, &y
)) {
1575 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1576 selectEntry(menu
, newSelectedEntry
);
1583 scrollMenuCallback(void *data
)
1585 WMenu
*menu
= (WMenu
*)data
;
1586 WMenu
*parent
= parentMenu(menu
);
1587 int hamount
= 0; /* amount to scroll */
1590 getScrollAmount(menu
, &hamount
, &vamount
);
1592 if (hamount
!= 0 || vamount
!= 0) {
1593 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1594 parent
->frame_y
+ vamount
, True
);
1596 /* keep scrolling */
1597 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1598 scrollMenuCallback
, menu
);
1600 /* don't need to scroll anymore */
1607 #define MENU_SCROLL_BORDER 5
1610 isPointNearBoder(WMenu
*menu
, int x
, int y
)
1612 int menuX1
= menu
->frame_x
;
1613 int menuY1
= menu
->frame_y
;
1614 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1615 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1616 int scrXe
= menu
->menu
->screen_ptr
->scr_width
-1;
1617 int scrYe
= menu
->menu
->screen_ptr
->scr_height
-1;
1620 if (x
>= menuX1
&& x
<= menuX2
&& (y
< MENU_SCROLL_BORDER
1621 || y
> scrYe
-MENU_SCROLL_BORDER
))
1623 else if (y
>= menuY1
&& y
<= menuY2
&& (x
< MENU_SCROLL_BORDER
1624 || x
> scrXe
-MENU_SCROLL_BORDER
))
1632 wMenuScroll(WMenu
*menu
, XEvent
*event
)
1635 WMenu
*omenu
= parentMenu(menu
);
1636 WScreen
*scr
= menu
->frame
->screen_ptr
;
1639 int old_frame_x
= omenu
->frame_x
;
1640 int old_frame_y
= omenu
->frame_y
;
1644 puts("Entering menu Scroll");
1647 if ((/*omenu->flags.buttoned &&*/ !wPreferences
.wrap_menus
)
1648 || omenu
->flags
.app_menu
) {
1652 if (!wPreferences
.wrap_menus
)
1653 raiseMenus(omenu
, True
);
1655 raiseMenus(menu
, False
);
1658 scrollMenuCallback(menu
);
1661 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1663 WMNextEvent(dpy
, &ev
);
1668 x
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1669 y
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1671 /* on_border is != 0 if the pointer is between the menu
1672 * and the screen border and is close enough to the border */
1673 on_border
= isPointNearBoder(menu
, x
, y
);
1675 smenu
= wMenuUnderPointer(scr
);
1677 if ((smenu
==NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
)!=omenu
)) {
1682 on_x_edge
= x
<= 1 || x
>= scr
->scr_width
- 2;
1683 on_y_edge
= y
<= 1 || y
>= scr
->scr_height
- 2;
1684 on_border
= on_x_edge
|| on_y_edge
;
1686 if (!on_border
&& !jump_back
) {
1691 if (menu
->timer
&& (smenu
!=menu
|| (!on_y_edge
&& !on_x_edge
))) {
1692 WMDeleteTimerHandler(menu
->timer
);
1700 scrollMenuCallback(menu
);
1703 /* True if we push on title, or drag the omenu to other position */
1704 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1705 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1706 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1707 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1709 smenu
= wMenuUnderPointer(scr
);
1710 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1712 else if (smenu
==omenu
&& on_title
) {
1726 WMDeleteTimerHandler(menu
->timer
);
1731 wMenuMove(omenu
, old_frame_x
, old_frame_y
, True
);
1734 puts("Leaving menu Scroll");
1741 menuExpose(WObjDescriptor
*desc
, XEvent
*event
)
1743 wMenuPaint(desc
->parent
);
1747 int *delayed_select
;
1754 delaySelection(void *data
)
1756 delay_data
*d
= (delay_data
*)data
;
1762 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1763 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1764 entry_no
= getEntryAt(menu
, x
, y
);
1765 selectEntry(menu
, entry_no
);
1767 if (d
->delayed_select
)
1768 *(d
->delayed_select
) = 0;
1773 menuMouseDown(WObjDescriptor
*desc
, XEvent
*event
)
1775 XButtonEvent
*bev
= &event
->xbutton
;
1776 WMenu
*menu
= desc
->parent
;
1778 WScreen
*scr
=menu
->frame
->screen_ptr
;
1779 WMenuEntry
*entry
=NULL
;
1781 int close_on_exit
=0;
1783 int delayed_select
= 0;
1787 int old_frame_x
= 0;
1788 int old_frame_y
= 0;
1789 delay_data d_data
= {NULL
, NULL
, NULL
};
1791 if (!wPreferences
.wrap_menus
) {
1792 smenu
= parentMenu(menu
);
1793 old_frame_x
= smenu
->frame_x
;
1794 old_frame_y
= smenu
->frame_y
;
1796 else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1797 /* This is true if the menu was launched with right click on root window */
1799 d_data
.delayed_select
= &delayed_select
;
1801 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
,
1802 delaySelection
, &d_data
);
1805 wRaiseFrame(menu
->frame
->core
);
1807 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1809 smenu
= findMenu(scr
, &x
, &y
);
1817 if (menu
->flags
.editing
) {
1820 entry_no
= getEntryAt(menu
, x
, y
);
1822 entry
= menu
->entries
[entry_no
];
1824 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
1825 && entry
->flags
.editable
) {
1826 editEntry(smenu
, entry
);
1828 } else if (bev
->state
& ControlMask
) {
1832 if (entry
->flags
.enabled
&& entry
->cascade
>=0 && menu
->cascades
) {
1833 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1835 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&&
1836 menu
->selected_entry
!=entry_no
) {
1837 wMenuUnmap(submenu
);
1839 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1840 selectEntry(menu
, entry_no
);
1841 } else if (!submenu
->flags
.buttoned
) {
1842 selectEntry(menu
, -1);
1845 } else if (!delayed_select
) {
1846 selectEntry(menu
, entry_no
);
1849 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1851 dragScrollMenuCallback(menu
);
1855 prevx
= bev
->x_root
;
1856 prevy
= bev
->y_root
;
1860 XAllowEvents(dpy
, SyncPointer
, CurrentTime
);
1862 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonReleaseMask
1863 |ButtonPressMask
, &ev
);
1866 smenu
= findMenu(scr
, &x
, &y
);
1868 if (smenu
== NULL
) {
1869 /* moved mouse out of menu */
1871 if (!delayed_select
&& d_data
.magic
) {
1872 WMDeleteTimerHandler(d_data
.magic
);
1873 d_data
.magic
= NULL
;
1876 || (menu
->selected_entry
>=0
1877 && menu
->entries
[menu
->selected_entry
]->cascade
>=0)) {
1878 prevx
= ev
.xmotion
.x_root
;
1879 prevy
= ev
.xmotion
.y_root
;
1883 selectEntry(menu
, -1);
1885 prevx
= ev
.xmotion
.x_root
;
1886 prevy
= ev
.xmotion
.y_root
;
1888 } else if (menu
&& menu
!=smenu
1889 && (menu
->selected_entry
<0
1890 || menu
->entries
[menu
->selected_entry
]->cascade
<0)) {
1891 selectEntry(menu
, -1);
1893 if (!delayed_select
&& d_data
.magic
) {
1894 WMDeleteTimerHandler(d_data
.magic
);
1895 d_data
.magic
= NULL
;
1899 /* hysteresis for item selection */
1901 /* check if the motion was to the side, indicating that
1902 * the user may want to cross to a submenu */
1903 if (!delayed_select
&& menu
) {
1905 Bool moved_to_submenu
;/* moved to direction of submenu */
1907 dx
= abs(prevx
- ev
.xmotion
.x_root
);
1909 moved_to_submenu
= False
;
1910 if (dx
> 0 /* if moved enough to the side */
1911 /* maybe a open submenu */
1912 && menu
->selected_entry
>=0
1913 /* moving to the right direction */
1914 && (wPreferences
.align_menus
1915 || ev
.xmotion
.y_root
>= prevy
)) {
1918 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
1920 if (menu
->cascades
[index
]->frame_x
>menu
->frame_x
) {
1921 if (prevx
< ev
.xmotion
.x_root
)
1922 moved_to_submenu
= True
;
1924 if (prevx
> ev
.xmotion
.x_root
)
1925 moved_to_submenu
= True
;
1931 if (menu
!= smenu
) {
1933 WMDeleteTimerHandler(d_data
.magic
);
1935 d_data
.magic
= NULL
;
1936 } else if (moved_to_submenu
) {
1937 /* while we are moving, postpone the selection */
1939 WMDeleteTimerHandler(d_data
.magic
);
1941 d_data
.delayed_select
= NULL
;
1943 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
1946 prevx
= ev
.xmotion
.x_root
;
1947 prevy
= ev
.xmotion
.y_root
;
1951 WMDeleteTimerHandler(d_data
.magic
);
1952 d_data
.magic
= NULL
;
1956 prevx
= ev
.xmotion
.x_root
;
1957 prevy
= ev
.xmotion
.y_root
;
1959 /* pointer crossed menus */
1960 if (menu
&& menu
->timer
) {
1961 WMDeleteTimerHandler(menu
->timer
);
1965 dragScrollMenuCallback(smenu
);
1969 dragScrollMenuCallback(menu
);
1971 if (!delayed_select
) {
1972 entry_no
= getEntryAt(menu
, x
, y
);
1974 entry
= menu
->entries
[entry_no
];
1975 if (entry
->flags
.enabled
&& entry
->cascade
>=0 &&
1977 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1978 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
1979 && menu
->selected_entry
!=entry_no
) {
1980 wMenuUnmap(submenu
);
1984 selectEntry(menu
, entry_no
);
1992 if (ev
.xbutton
.button
== event
->xbutton
.button
)
2002 if (menu
&& menu
->timer
) {
2003 WMDeleteTimerHandler(menu
->timer
);
2006 if (d_data
.magic
!=NULL
)
2007 WMDeleteTimerHandler(d_data
.magic
);
2009 if (menu
&& menu
->selected_entry
>=0) {
2010 entry
= menu
->entries
[menu
->selected_entry
];
2011 if (entry
->callback
!=NULL
&& entry
->flags
.enabled
2012 && entry
->cascade
< 0) {
2013 /* blink and erase menu selection */
2014 #if (MENU_BLINK_DELAY > 0)
2015 int sel
= menu
->selected_entry
;
2018 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
2019 paintEntry(menu
, sel
, False
);
2021 wusleep(MENU_BLINK_DELAY
);
2022 paintEntry(menu
, sel
, True
);
2024 wusleep(MENU_BLINK_DELAY
);
2027 /* unmap the menu, it's parents and call the callback */
2028 if (!menu
->flags
.buttoned
&&
2029 (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2032 selectEntry(menu
, -1);
2034 (*entry
->callback
)(menu
, entry
);
2036 /* If the user double clicks an entry, the entry will
2037 * be executed twice, which is not good for things like
2038 * the root menu. So, ignore any clicks that were generated
2039 * while the entry was being executed */
2040 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
,
2042 } else if (entry
->callback
!=NULL
&& entry
->cascade
<0) {
2043 selectEntry(menu
, -1);
2045 if (entry
->cascade
>=0 && menu
->cascades
2046 && menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2047 selectEntry(menu
, -1);
2052 if (((WMenu
*)desc
->parent
)->flags
.brother
|| close_on_exit
2054 closeCascade(desc
->parent
);
2056 /* close the cascade windows that should not remain opened */
2057 closeBrotherCascadesOf(desc
->parent
);
2059 if (!wPreferences
.wrap_menus
)
2060 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2065 wMenuMove(WMenu
*menu
, int x
, int y
, int submenus
)
2074 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2076 if (submenus
>0 && menu
->selected_entry
>=0) {
2077 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2079 if (i
>=0 && menu
->cascades
) {
2080 submenu
= menu
->cascades
[i
];
2081 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
2082 if (wPreferences
.align_menus
) {
2083 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2085 wMenuMove(submenu
, x
+ MENUW(menu
),
2086 y
+ submenu
->entry_height
*menu
->selected_entry
,
2091 if (submenus
<0 && menu
->parent
!=NULL
&& menu
->parent
->flags
.mapped
&&
2092 !menu
->parent
->flags
.buttoned
) {
2093 if (wPreferences
.align_menus
) {
2094 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2096 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2097 - menu
->parent
->entry_height
*menu
->parent
->selected_entry
,
2105 changeMenuLevels(WMenu
*menu
, int lower
)
2110 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
2112 wRaiseFrame(menu
->frame
->core
);
2113 menu
->flags
.lowered
= 0;
2115 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2116 wLowerFrame(menu
->frame
->core
);
2117 menu
->flags
.lowered
= 1;
2119 for (i
=0; i
<menu
->cascade_no
; i
++) {
2120 if (menu
->cascades
[i
]
2121 && !menu
->cascades
[i
]->flags
.buttoned
2122 && menu
->cascades
[i
]->flags
.lowered
!=lower
) {
2123 changeMenuLevels(menu
->cascades
[i
], lower
);
2131 menuTitleDoubleClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2136 if (event
->xbutton
.state
& MOD_MASK
) {
2137 if (menu
->flags
.lowered
) {
2142 changeMenuLevels(menu
, lower
);
2148 menuTitleMouseDown(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2153 int x
=menu
->frame_x
, y
=menu
->frame_y
;
2154 int dx
=event
->xbutton
.x_root
, dy
=event
->xbutton
.y_root
;
2159 printf("Moving menu\n");
2162 /* can't touch the menu copy */
2163 if (menu
->flags
.brother
)
2166 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2169 if (event
->xbutton
.state
& MOD_MASK
) {
2170 wLowerFrame(menu
->frame
->core
);
2173 wRaiseFrame(menu
->frame
->core
);
2178 /* lower/raise all submenus */
2180 if (tmp
->selected_entry
>=0 && tmp
->cascades
2181 && tmp
->entries
[tmp
->selected_entry
]->cascade
>=0) {
2182 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2183 if (!tmp
|| !tmp
->flags
.mapped
)
2186 wLowerFrame(tmp
->frame
->core
);
2188 wRaiseFrame(tmp
->frame
->core
);
2194 /* tear off the menu if it's a root menu or a cascade
2196 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
2197 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2198 menu
->flags
.buttoned
=1;
2199 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2201 /* turn off selected menu entry in parent menu */
2202 selectEntry(menu
->parent
, -1);
2204 /* make parent map the copy in place of the original */
2205 for (i
=0; i
<menu
->parent
->cascade_no
; i
++) {
2206 if (menu
->parent
->cascades
[i
] == menu
) {
2207 menu
->parent
->cascades
[i
] = menu
->brother
;
2216 WMMaskEvent(dpy
, ButtonMotionMask
|ButtonReleaseMask
|ButtonPressMask
2217 |ExposureMask
, &ev
);
2221 x
+= ev
.xmotion
.x_root
- dx
;
2222 y
+= ev
.xmotion
.y_root
- dy
;
2223 dx
= ev
.xmotion
.x_root
;
2224 dy
= ev
.xmotion
.y_root
;
2225 wMenuMove(menu
, x
, y
, True
);
2227 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2228 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2230 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2231 ButtonMotionMask
|ButtonReleaseMask
2233 GrabModeAsync
, GrabModeAsync
, None
,
2234 wCursor
[WCUR_MOVE
], CurrentTime
);
2243 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2246 printf("End menu move\n");
2248 XUngrabPointer(dpy
, CurrentTime
);
2259 *----------------------------------------------------------------------
2261 * Handles mouse click on the close button of menus. The menu is
2262 * closed when the button is clicked.
2265 * The closed menu is reinserted at it's parent menus
2267 *----------------------------------------------------------------------
2270 menuCloseClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2272 WMenu
*menu
= (WMenu
*)data
;
2273 WMenu
*parent
= menu
->parent
;
2277 for (i
=0; i
<parent
->cascade_no
; i
++) {
2278 /* find the entry that points to the copy */
2279 if (parent
->cascades
[i
] == menu
->brother
) {
2280 /* make it point to the original */
2281 parent
->cascades
[i
] = menu
;
2282 menu
->parent
= parent
;
2292 wMenuSaveState(WScreen
*scr
)
2294 proplist_t menus
, key
, value
;
2298 menus
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
2301 if (scr
->root_menu
&& scr
->root_menu
->flags
.buttoned
) {
2302 sprintf(buffer
, "%i,%i", scr
->root_menu
->frame_x
,
2303 scr
->root_menu
->frame_y
);
2304 key
= PLMakeString("RootMenu");
2305 value
= PLMakeString(buffer
);
2306 PLInsertDictionaryEntry(menus
, key
, value
);
2312 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2313 sprintf(buffer
, "%i,%i", scr
->switch_menu
->frame_x
,
2314 scr
->switch_menu
->frame_y
);
2315 key
= PLMakeString("SwitchMenu");
2316 value
= PLMakeString(buffer
);
2317 PLInsertDictionaryEntry(menus
, key
, value
);
2323 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2324 sprintf(buffer
, "%i,%i", scr
->workspace_menu
->frame_x
,
2325 scr
->workspace_menu
->frame_y
);
2326 key
= PLMakeString("WorkspaceMenu");
2327 value
= PLMakeString(buffer
);
2328 PLInsertDictionaryEntry(menus
, key
, value
);
2335 key
= PLMakeString("Menus");
2336 PLInsertDictionaryEntry(scr
->session_state
, key
, menus
);
2343 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2347 restoreMenu(WScreen
*scr
, proplist_t menu
, int which
)
2350 WMenu
*pmenu
= NULL
;
2355 if (!PLIsString(menu
)) {
2356 COMPLAIN("Position");
2360 if (sscanf(PLGetString(menu
), "%i,%i", &x
, &y
)!=2)
2361 COMPLAIN("Position");
2364 if (which
& WSS_ROOTMENU
) {
2365 OpenRootMenu(scr
, x
, y
, False
);
2366 pmenu
= scr
->root_menu
;
2367 } else if (which
& WSS_SWITCHMENU
) {
2368 OpenSwitchMenu(scr
, x
, y
, False
);
2369 pmenu
= scr
->switch_menu
;
2372 if (which
& WSS_WSMENU
) {
2373 OpenWorkspaceMenu(scr
, x
, y
);
2374 pmenu
= scr
->workspace_menu
;
2375 if (pmenu
->parent
) {
2376 /* make parent map the copy in place of the original */
2377 for (i
=0; i
<pmenu
->parent
->cascade_no
; i
++) {
2378 if (pmenu
->parent
->cascades
[i
] == pmenu
) {
2379 pmenu
->parent
->cascades
[i
] = pmenu
->brother
;
2387 int width
= MENUW(pmenu
);
2388 int height
= MENUH(pmenu
);
2390 x
= (x
< -width
) ? 0 : x
;
2391 x
= (x
> scr
->scr_width
) ? scr
->scr_width
- width
: x
;
2392 y
= (y
< 0) ? 0 : y
;
2393 y
= (y
> scr
->scr_height
) ? scr
->scr_height
- height
: y
;
2394 wMenuMove(pmenu
, x
, y
, True
);
2395 pmenu
->flags
.buttoned
= 1;
2396 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2404 wMenuRestoreState(WScreen
*scr
)
2406 proplist_t menus
, menu
, key
, rkey
, skey
, wkey
;
2408 key
= PLMakeString("Menus");
2409 menus
= PLGetDictionaryEntry(scr
->session_state
, key
);
2417 rkey
= PLMakeString("RootMenu");
2418 skey
= PLMakeString("SwitchMenu");
2419 wkey
= PLMakeString("WorkspaceMenu");
2420 menu
= PLGetDictionaryEntry(menus
, rkey
);
2421 restoreMenu(scr
, menu
, WSS_ROOTMENU
);
2422 menu
= PLGetDictionaryEntry(menus
, skey
);
2423 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2424 menu
= PLGetDictionaryEntry(menus
, wkey
);
2425 restoreMenu(scr
, menu
, WSS_WSMENU
);
2434 OpenWorkspaceMenu(WScreen
*scr
, int x
, int y
)
2436 WMenu
*menu
, *parent
;
2440 if (!scr
->root_menu
) {
2441 OpenRootMenu(scr
, scr
->scr_width
*2, 0, False
);
2442 wMenuUnmap(scr
->root_menu
);
2445 menu
= scr
->workspace_menu
;
2447 if (menu
->flags
.mapped
) {
2448 if (!menu
->flags
.buttoned
) {
2450 parent
= menu
->parent
;
2451 if (parent
&& parent
->selected_entry
>= 0) {
2452 entry
= parent
->entries
[parent
->selected_entry
];
2453 if (parent
->cascades
[entry
->cascade
] == menu
) {
2454 selectEntry(parent
, -1);
2455 wMenuMapAt(menu
, x
, y
, False
);
2459 wRaiseFrame(menu
->frame
->core
);
2460 wMenuMapCopyAt(menu
, x
, y
);
2464 wMenuMapAt(menu
, x
, y
, False
);