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
);
84 static void updateTexture(WMenu
*menu
);
87 static void selectEntry(WMenu
*menu
, int entry_no
);
88 static void closeCascade(WMenu
*menu
);
91 /****** Notification Observers ******/
94 appearanceObserver(void *self
, WMNotification
*notif
)
96 WMenu
*menu
= (WMenu
*)self
;
97 int flags
= (int)WMGetNotificationClientData(notif
);
99 if (!menu
->flags
.realized
)
102 if (WMGetNotificationName(notif
) == WNMenuAppearanceSettingsChanged
) {
103 if (flags
& WFontSettings
) {
104 menu
->flags
.realized
= 0;
107 if (flags
& WTextureSettings
) {
110 if (flags
& (WTextureSettings
|WColorSettings
)) {
113 } else if (menu
->flags
.titled
) {
115 if (flags
& WFontSettings
) {
116 menu
->flags
.realized
= 0;
119 if (flags
& WTextureSettings
) {
120 menu
->frame
->flags
.need_texture_remake
= 1;
122 if (flags
& (WColorSettings
|WTextureSettings
))
123 wFrameWindowPaint(menu
->frame
);
127 /************************************/
131 *----------------------------------------------------------------------
133 * Creates a new empty menu with the specified title. If main_menu
134 * is True, the created menu will be a main menu, which has some special
135 * properties such as being placed over other normal menus.
136 * If title is NULL, the menu will have no titlebar.
140 *----------------------------------------------------------------------
143 wMenuCreate(WScreen
*screen
, char *title
, int main_menu
)
146 static int brother
=0;
149 menu
= wmalloc(sizeof(WMenu
));
151 memset(menu
, 0, sizeof(WMenu
));
153 #ifdef SINGLE_MENULEVEL
154 tmp
= WMSubmenuLevel
;
156 tmp
= (main_menu
? WMMainMenuLevel
: WMSubmenuLevel
);
159 flags
= WFF_SINGLE_STATE
;
161 flags
|= WFF_TITLEBAR
|WFF_RIGHT_BUTTON
;
162 menu
->flags
.titled
= 1;
165 wFrameWindowCreate(screen
, tmp
, 8, 2, 1, 1, flags
,
166 screen
->menu_title_texture
, NULL
,
167 screen
->menu_title_pixel
, &screen
->menu_title_gc
,
168 &screen
->menu_title_font
);
170 menu
->frame
->core
->descriptor
.parent
= menu
;
171 menu
->frame
->core
->descriptor
.parent_type
= WCLASS_MENU
;
172 menu
->frame
->core
->descriptor
.handle_mousedown
= menuMouseDown
;
174 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
177 menu
->frame
->title
= wstrdup(title
);
180 menu
->frame
->flags
.justification
= WTJ_LEFT
;
182 menu
->frame
->rbutton_image
= screen
->b_pixmaps
[WBUT_CLOSE
];
185 menu
->alloced_entries
= 0;
186 menu
->selected_entry
= -1;
187 menu
->entries
= NULL
;
189 menu
->frame_x
= screen
->app_menu_x
;
190 menu
->frame_y
= screen
->app_menu_y
;
192 menu
->frame
->child
= menu
;
194 menu
->flags
.lowered
= 0;
198 /* setup object descriptors */
199 menu
->frame
->on_mousedown_titlebar
= menuTitleMouseDown
;
200 menu
->frame
->on_dblclick_titlebar
= menuTitleDoubleClick
;
203 menu
->frame
->on_click_right
= menuCloseClick
;
206 menu
->menu
= wCoreCreate(menu
->frame
->core
, 0, menu
->frame
->top_width
,
207 menu
->frame
->core
->width
, 10);
209 menu
->menu
->descriptor
.parent
= menu
;
210 menu
->menu
->descriptor
.parent_type
= WCLASS_MENU
;
211 menu
->menu
->descriptor
.handle_expose
= menuExpose
;
212 menu
->menu
->descriptor
.handle_mousedown
= menuMouseDown
;
214 menu
->menu_texture_data
= None
;
216 XMapWindow(dpy
, menu
->menu
->window
);
222 menu
->brother
= wMenuCreate(screen
, title
, main_menu
);
224 menu
->brother
->flags
.brother
= 1;
225 menu
->brother
->brother
= menu
;
227 WMAddNotificationObserver(appearanceObserver
, menu
,
228 WNMenuTitleAppearanceSettingsChanged
, menu
);
230 WMAddNotificationObserver(appearanceObserver
, menu
,
231 WNMenuAppearanceSettingsChanged
, menu
);
241 wMenuCreateForApp(WScreen
*screen
, char *title
, int main_menu
)
245 menu
= wMenuCreate(screen
, title
, main_menu
);
248 menu
->flags
.app_menu
= 1;
249 menu
->brother
->flags
.app_menu
= 1;
257 insertEntry(WMenu
*menu
, WMenuEntry
*entry
, int index
)
261 for (i
= menu
->entry_no
-1; i
>= index
; i
--) {
262 menu
->entries
[i
]->order
++;
263 menu
->entries
[i
+1] = menu
->entries
[i
];
265 menu
->entries
[index
] = entry
;
271 wMenuInsertCallback(WMenu
*menu
, int index
, char *text
,
272 void (*callback
)(WMenu
*menu
, WMenuEntry
*entry
),
279 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
284 assert(menu
->flags
.brother
==0);
285 menu
->flags
.realized
= 0;
286 menu
->brother
->flags
.realized
= 0;
288 /* reallocate array if it's too small */
289 if (menu
->entry_no
>= menu
->alloced_entries
) {
292 puts("doing wrealloc()");
294 tmp
= wrealloc(menu
->entries
,
295 sizeof(WMenuEntry
)*(menu
->alloced_entries
+5));
297 wwarning(_("wrealloc() failed while trying to add menu item"));
302 menu
->alloced_entries
+= 5;
304 menu
->brother
->entries
= tmp
;
305 menu
->brother
->alloced_entries
= menu
->alloced_entries
;
307 entry
= wmalloc(sizeof(WMenuEntry
));
308 memset(entry
, 0, sizeof(WMenuEntry
));
309 entry
->flags
.enabled
= 1;
310 entry
->text
= wstrdup(text
);
312 entry
->clientdata
= clientdata
;
313 entry
->callback
= callback
;
314 if (index
<0 || index
>=menu
->entry_no
) {
315 entry
->order
= menu
->entry_no
;
316 menu
->entries
[menu
->entry_no
] = entry
;
318 entry
->order
= index
;
319 insertEntry(menu
, entry
, index
);
323 menu
->brother
->entry_no
= menu
->entry_no
;
331 wMenuEntrySetCascade(WMenu
*menu
, WMenuEntry
*entry
, WMenu
*cascade
)
333 WMenu
*brother
= menu
->brother
;
336 assert(menu
->flags
.brother
==0);
338 if (entry
->cascade
>=0) {
339 menu
->flags
.realized
= 0;
340 brother
->flags
.realized
= 0;
343 cascade
->parent
= menu
;
345 cascade
->brother
->parent
= brother
;
348 for (i
=0; i
<menu
->cascade_no
; i
++) {
349 if (menu
->cascades
[i
]==NULL
) {
350 menu
->cascades
[i
] = cascade
;
351 brother
->cascades
[i
] = cascade
->brother
;
358 entry
->cascade
= menu
->cascade_no
;
360 menu
->cascades
= wrealloc(menu
->cascades
,
361 sizeof(WMenu
)*(menu
->cascade_no
+1));
362 menu
->cascades
[menu
->cascade_no
++] = cascade
;
365 brother
->cascades
= wrealloc(brother
->cascades
,
366 sizeof(WMenu
)*(brother
->cascade_no
+1));
367 brother
->cascades
[brother
->cascade_no
++] = cascade
->brother
;
371 if (menu
->flags
.lowered
) {
373 cascade
->flags
.lowered
= 1;
374 ChangeStackingLevel(cascade
->frame
->core
, WMNormalLevel
);
376 cascade
->brother
->flags
.lowered
= 1;
377 ChangeStackingLevel(cascade
->brother
->frame
->core
, WMNormalLevel
);
380 if (!menu
->flags
.realized
)
386 wMenuEntryRemoveCascade(WMenu
*menu
, WMenuEntry
*entry
)
388 assert(menu
->flags
.brother
==0);
390 /* destroy cascade menu */
391 if (entry
->cascade
>=0 && menu
->cascades
392 && menu
->cascades
[entry
->cascade
]!=NULL
) {
394 wMenuDestroy(menu
->cascades
[entry
->cascade
], True
);
396 menu
->cascades
[entry
->cascade
] = NULL
;
397 menu
->brother
->cascades
[entry
->cascade
] = NULL
;
405 wMenuRemoveItem(WMenu
*menu
, int index
)
409 if (menu
->flags
.brother
) {
410 wMenuRemoveItem(menu
->brother
, index
);
414 if (index
>=menu
->entry_no
) return;
416 /* destroy cascade menu */
417 wMenuEntryRemoveCascade(menu
, menu
->entries
[index
]);
419 /* destroy unshared data */
421 if (menu
->entries
[index
]->text
)
422 free(menu
->entries
[index
]->text
);
424 if (menu
->entries
[index
]->rtext
)
425 free(menu
->entries
[index
]->rtext
);
427 if (menu
->entries
[index
]->free_cdata
&& menu
->entries
[index
]->clientdata
)
428 (*menu
->entries
[index
]->free_cdata
)(menu
->entries
[index
]->clientdata
);
430 free(menu
->entries
[index
]);
432 for (i
=index
; i
<menu
->entry_no
-1; i
++) {
433 menu
->entries
[i
+1]->order
--;
434 menu
->entries
[i
]=menu
->entries
[i
+1];
437 menu
->brother
->entry_no
--;
442 updateTexture(WMenu
*menu
)
444 WScreen
*scr
= menu
->menu
->screen_ptr
;
446 /* setup background texture */
447 if (scr
->menu_item_texture
->any
.type
!= WTEX_SOLID
) {
448 if (!menu
->flags
.brother
) {
449 FREE_PIXMAP(menu
->menu_texture_data
);
451 if (wPreferences
.alt_menu_style
) {
452 wTextureRender(scr
, scr
->menu_item_texture
,
453 &menu
->menu_texture_data
, menu
->menu
->width
,
454 menu
->menu
->height
, WREL_MENUENTRY
);
456 wTextureRender(scr
, scr
->menu_item_texture
,
457 &menu
->menu_texture_data
, menu
->menu
->width
,
458 menu
->entry_height
, WREL_MENUENTRY
);
461 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
,
462 menu
->menu_texture_data
);
463 XClearWindow(dpy
, menu
->menu
->window
);
465 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
,
466 menu
->menu_texture_data
);
467 XClearWindow(dpy
, menu
->brother
->menu
->window
);
470 XSetWindowBackground(dpy
, menu
->menu
->window
,
471 scr
->menu_item_texture
->any
.color
.pixel
);
472 XClearWindow(dpy
, menu
->menu
->window
);
478 wMenuRealize(WMenu
*menu
)
481 int width
, rwidth
, mrwidth
, mwidth
;
482 int theight
, twidth
, eheight
;
483 WScreen
*scr
= menu
->frame
->screen_ptr
;
484 static int brother_done
=0;
489 wMenuRealize(menu
->brother
);
493 flags
= WFF_SINGLE_STATE
;
494 if (menu
->flags
.titled
)
495 flags
|= WFF_TITLEBAR
|WFF_RIGHT_BUTTON
;
497 wFrameWindowUpdateBorders(menu
->frame
, flags
);
499 if (menu
->flags
.titled
) {
500 twidth
= wTextWidth(scr
->menu_title_font
->font
, menu
->frame
->title
,
501 strlen(menu
->frame
->title
));
502 theight
= menu
->frame
->top_width
;
503 twidth
+= theight
+ (wPreferences
.new_style
? 16 : 8);
508 eheight
= scr
->menu_entry_font
->height
+ 6;
509 menu
->entry_height
= eheight
;
512 for (i
=0; i
<menu
->entry_no
; i
++) {
515 /* search widest text */
516 text
= menu
->entries
[i
]->text
;
517 width
= wTextWidth(scr
->menu_entry_font
->font
, text
, strlen(text
))+10;
519 if (menu
->entries
[i
]->flags
.indicator
) {
520 width
+= MENU_INDICATOR_SPACE
;
526 /* search widest text on right */
527 text
= menu
->entries
[i
]->rtext
;
529 rwidth
= wTextWidth(scr
->menu_entry_font
->font
, text
,
531 else if (menu
->entries
[i
]->cascade
>=0)
536 if (rwidth
> mrwidth
)
545 wCoreConfigure(menu
->menu
, 0, theight
, mwidth
, menu
->entry_no
*eheight
-1);
547 wFrameWindowResize(menu
->frame
, mwidth
, menu
->entry_no
*eheight
-1
548 + menu
->frame
->top_width
+ menu
->frame
->bottom_width
);
553 menu
->flags
.realized
= 1;
555 if (menu
->flags
.mapped
)
557 if (menu
->brother
->flags
.mapped
)
558 wMenuPaint(menu
->brother
);
563 wMenuDestroy(WMenu
*menu
, int recurse
)
567 WMRemoveNotificationObserver(menu
);
569 /* remove any pending timers */
571 WMDeleteTimerHandler(menu
->timer
);
574 /* call destroy handler */
575 if (menu
->on_destroy
)
576 (*menu
->on_destroy
)(menu
);
578 /* Destroy items if this menu own them. If this is the "brother" menu,
579 * leave them alone as it is shared by them.
581 if (!menu
->flags
.brother
) {
582 for (i
=0; i
<menu
->entry_no
; i
++) {
584 free(menu
->entries
[i
]->text
);
586 if (menu
->entries
[i
]->rtext
)
587 free(menu
->entries
[i
]->rtext
);
589 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
590 (*menu
->entries
[i
]->free_cdata
)(menu
->entries
[i
]->clientdata
);
592 free(menu
->entries
[i
]);
596 for (i
=0; i
<menu
->cascade_no
; i
++) {
597 if (menu
->cascades
[i
]) {
598 if (menu
->cascades
[i
]->flags
.brother
)
599 wMenuDestroy(menu
->cascades
[i
]->brother
, recurse
);
601 wMenuDestroy(menu
->cascades
[i
], recurse
);
611 FREE_PIXMAP(menu
->menu_texture_data
);
614 free(menu
->cascades
);
616 wCoreDestroy(menu
->menu
);
617 wFrameWindowDestroy(menu
->frame
);
619 /* destroy copy of this menu */
620 if (!menu
->flags
.brother
&& menu
->brother
)
621 wMenuDestroy(menu
->brother
, False
);
628 drawFrame(WScreen
*scr
, Window win
, int y
, int w
, int h
)
639 segs
[1].y2
= y
+ h
- 2;
640 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, 2);
642 XDrawLine(dpy
, win
, scr
->menu_item_auxtexture
->dark_gc
, 0, y
+h
-1,
646 segs
[0].y1
= segs
[0].y2
= y
+ h
-2;
648 segs
[1].x1
= segs
[1].x2
= w
-1;
650 segs
[1].y2
= y
+ h
-2;
651 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, 2);
656 paintEntry(WMenu
*menu
, int index
, int selected
)
659 GC light
, dim
, dark
, textGC
;
660 WScreen
*scr
=menu
->frame
->screen_ptr
;
661 Window win
= menu
->menu
->window
;
662 WMenuEntry
*entry
=menu
->entries
[index
];
664 if (!menu
->flags
.realized
) return;
665 h
= menu
->entry_height
;
666 w
= menu
->menu
->width
;
669 light
= scr
->menu_item_auxtexture
->light_gc
;
670 dim
= scr
->menu_item_auxtexture
->dim_gc
;
671 dark
= scr
->menu_item_auxtexture
->dark_gc
;
673 /* paint background */
675 XSetForeground(dpy
, scr
->select_menu_gc
, scr
->select_pixel
);
676 XFillRectangle(dpy
, win
, scr
->select_menu_gc
, 1, y
+1, w
-2, h
-3);
677 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
)
678 drawFrame(scr
, win
, y
, w
, h
);
680 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
681 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
683 drawFrame(scr
, win
, y
, w
, h
);
685 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
690 textGC
= scr
->select_menu_gc
;
691 if (entry
->flags
.enabled
)
692 XSetForeground(dpy
, textGC
, scr
->select_text_pixel
);
694 XSetForeground(dpy
, textGC
, scr
->dtext_pixel
);
695 } else if (!entry
->flags
.enabled
) {
696 textGC
= scr
->disabled_menu_entry_gc
;
698 textGC
= scr
->menu_entry_gc
;
702 if (entry
->flags
.indicator
)
703 x
+= MENU_INDICATOR_SPACE
+ 2;
705 wDrawString(win
, scr
->menu_entry_font
,
706 textGC
, x
, 3+y
+scr
->menu_entry_font
->y
, entry
->text
,
707 strlen(entry
->text
));
709 if (entry
->cascade
>=0) {
710 /* draw the cascade indicator */
711 XDrawLine(dpy
,win
,dim
, w
-11, y
+6, w
-6, y
+h
/2-1);
712 XDrawLine(dpy
,win
,light
, w
-11, y
+h
-8, w
-6, y
+h
/2-1);
713 XDrawLine(dpy
,win
,dark
, w
-12, y
+6, w
-12, y
+h
-8);
717 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
722 switch (entry
->flags
.indicator_type
) {
724 indicator
= scr
->menu_check_indicator
;
727 indicator
= scr
->menu_mini_indicator
;
730 indicator
= scr
->menu_hide_indicator
;
733 indicator
= scr
->menu_shade_indicator
;
737 indicator
= scr
->menu_radio_indicator
;
741 iw
= indicator
->width
;
742 ih
= indicator
->height
;
743 XSetClipMask(dpy
, scr
->copy_gc
, indicator
->mask
);
744 XSetClipOrigin(dpy
, scr
->copy_gc
, 5, y
+(h
-ih
)/2);
746 XSetForeground(dpy
, scr
->copy_gc
, scr
->black_pixel
);
748 XSetForeground(dpy
, scr
->copy_gc
, scr
->mtext_pixel
);
749 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+(h
-ih
)/2, iw
, ih
);
751 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
752 iw, ih, 5, y+(h-ih)/2);
754 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
757 /* draw right text */
759 if (entry
->rtext
&& entry
->cascade
<0) {
760 tw
= wTextWidth(scr
->menu_entry_font
->font
, entry
->rtext
,
761 strlen(entry
->rtext
));
763 wDrawString(win
, scr
->menu_entry_font
, textGC
, w
-6-tw
,
764 3+y
+scr
->menu_entry_font
->y
, entry
->rtext
,
765 strlen(entry
->rtext
));
771 move_menus(WMenu
*menu
, int x
, int y
)
773 while (menu
->parent
) {
776 if (!wPreferences
.align_menus
&& menu
->selected_entry
>=0) {
777 y
-= menu
->selected_entry
*menu
->entry_height
;
780 wMenuMove(menu
, x
, y
, True
);
784 makeVisible(WMenu
*menu
)
786 WScreen
*scr
= menu
->frame
->screen_ptr
;
787 int x1
, y1
, x2
, y2
, new_x
, new_y
, move
;
789 if (menu
->entry_no
<0) return;
792 y1
= menu
->frame_y
+menu
->frame
->top_width
793 + menu
->selected_entry
*menu
->entry_height
;
794 x2
= x1
+ MENUW(menu
);
795 y2
= y1
+ menu
->entry_height
;
804 } else if (x2
>= scr
->scr_width
) {
805 new_x
= scr
->scr_width
- MENUW(menu
) - 1;
812 } else if (y2
>= scr
->scr_height
) {
813 new_y
= scr
->scr_height
- menu
->entry_height
- 1;
817 new_y
= new_y
- menu
->frame
->top_width
818 - menu
->selected_entry
*menu
->entry_height
;
819 move_menus(menu
, new_x
, new_y
);
824 check_key(WMenu
*menu
, XKeyEvent
*event
)
829 if (XLookupString(event
, buffer
, 32, NULL
, NULL
)<1)
832 ch
= toupper(buffer
[0]);
834 s
= (menu
->selected_entry
>=0 ? menu
->selected_entry
+1 : 0);
837 for (i
=s
; i
<menu
->entry_no
; i
++) {
838 if (ch
==toupper(menu
->entries
[i
]->text
[0])) {
842 /* no match. Retry from start, if previous started from a selected entry */
852 keyboardMenu(WMenu
*menu
)
855 KeySym ksym
=NoSymbol
;
859 int old_pos_x
= menu
->frame_x
;
860 int old_pos_y
= menu
->frame_y
;
861 int new_x
= old_pos_x
, new_y
= old_pos_y
;
862 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
863 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
865 if (menu
->flags
.editing
)
869 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
,
870 GrabModeAsync
, CurrentTime
);
872 if (menu
->frame_y
+menu
->frame
->top_width
>= scr_height
)
873 new_y
= scr_height
- menu
->frame
->top_width
;
875 if (menu
->frame_x
+MENUW(menu
) >= scr_width
)
876 new_x
= scr_width
-MENUW(menu
)-1;
878 move_menus(menu
, new_x
, new_y
);
880 while (!done
&& menu
->flags
.mapped
) {
881 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
882 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonPressMask
883 |ButtonReleaseMask
|KeyPressMask
|KeyReleaseMask
884 |SubstructureNotifyMask
, &event
);
886 switch (event
.type
) {
888 ksym
= XLookupKeysym(&event
.xkey
, 0);
896 selectEntry(menu
, 0);
902 selectEntry(menu
, menu
->entry_no
-1);
911 if (menu
->selected_entry
<= 0)
912 selectEntry(menu
, menu
->entry_no
-1);
914 selectEntry(menu
, menu
->selected_entry
-1);
923 if (menu
->selected_entry
<0)
924 selectEntry(menu
, 0);
925 else if (menu
->selected_entry
== menu
->entry_no
-1)
926 selectEntry(menu
, 0);
927 else if (menu
->selected_entry
< menu
->entry_no
-1)
928 selectEntry(menu
, menu
->selected_entry
+1);
937 if (menu
->selected_entry
>=0) {
939 entry
= menu
->entries
[menu
->selected_entry
];
941 if (entry
->cascade
>= 0 && menu
->cascades
942 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
944 XUngrabKeyboard(dpy
, CurrentTime
);
946 selectEntry(menu
->cascades
[entry
->cascade
], 0);
947 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
950 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
951 GrabModeAsync
, GrabModeAsync
,
962 if (menu
->parent
!=NULL
&& menu
->parent
->selected_entry
>=0) {
963 selectEntry(menu
, -1);
964 move_menus(menu
, old_pos_x
, old_pos_y
);
974 index
= check_key(menu
, &event
.xkey
);
976 selectEntry(menu
, index
);
982 if (event
.type
==ButtonPress
)
985 WMHandleEvent(&event
);
989 XUngrabKeyboard(dpy
, CurrentTime
);
991 if (done
==2 && menu
->selected_entry
>=0) {
992 entry
= menu
->entries
[menu
->selected_entry
];
997 if (entry
&& entry
->callback
!=NULL
&& entry
->flags
.enabled
998 && entry
->cascade
< 0) {
999 #if (MENU_BLINK_COUNT > 0)
1000 int sel
= menu
->selected_entry
;
1003 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
1004 paintEntry(menu
, sel
, False
);
1006 wusleep(MENU_BLINK_DELAY
);
1007 paintEntry(menu
, sel
, True
);
1009 wusleep(MENU_BLINK_DELAY
);
1012 selectEntry(menu
, -1);
1014 if (!menu
->flags
.buttoned
) {
1016 move_menus(menu
, old_pos_x
, old_pos_y
);
1020 (*entry
->callback
)(menu
, entry
);
1022 if (!menu
->flags
.buttoned
) {
1024 move_menus(menu
, old_pos_x
, old_pos_y
);
1026 selectEntry(menu
, -1);
1030 /* returns True if returning from a submenu to a parent menu,
1031 * False if exiting from menu */
1037 wMenuMapAt(WMenu
*menu
, int x
, int y
, int keyboard
)
1039 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
1040 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
1042 if (!menu
->flags
.realized
) {
1043 menu
->flags
.realized
=1;
1046 if (!menu
->flags
.mapped
) {
1047 if (wPreferences
.wrap_menus
) {
1050 if (x
+MENUW(menu
) > scr_width
)
1051 x
= scr_width
- MENUW(menu
);
1052 if (y
+MENUH(menu
) > scr_height
)
1053 y
= scr_height
- MENUH(menu
);
1056 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
1059 XMapWindow(dpy
, menu
->frame
->core
->window
);
1060 wRaiseFrame(menu
->frame
->core
);
1061 menu
->flags
.mapped
= 1;
1063 selectEntry(menu
, 0);
1072 wMenuMap(WMenu
*menu
)
1074 if (!menu
->flags
.realized
) {
1075 menu
->flags
.realized
=1;
1078 if (menu
->flags
.app_menu
&& menu
->parent
==NULL
) {
1079 menu
->frame_x
= menu
->frame
->screen_ptr
->app_menu_x
;
1080 menu
->frame_y
= menu
->frame
->screen_ptr
->app_menu_y
;
1081 XMoveWindow(dpy
, menu
->frame
->core
->window
, menu
->frame_x
, menu
->frame_y
);
1083 XMapWindow(dpy
, menu
->frame
->core
->window
);
1084 wRaiseFrame(menu
->frame
->core
);
1085 menu
->flags
.mapped
= 1;
1090 wMenuUnmap(WMenu
*menu
)
1094 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1095 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1096 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1098 menu
->flags
.buttoned
= 0;
1099 menu
->flags
.mapped
= 0;
1100 menu
->flags
.open_to_left
= 0;
1102 for (i
=0; i
<menu
->cascade_no
; i
++) {
1103 if (menu
->cascades
[i
]!=NULL
1104 && menu
->cascades
[i
]->flags
.mapped
1105 && !menu
->cascades
[i
]->flags
.buttoned
) {
1107 wMenuUnmap(menu
->cascades
[i
]);
1110 menu
->selected_entry
= -1;
1116 wMenuPaint(WMenu
*menu
)
1120 if (!menu
->flags
.mapped
) {
1125 for (i
=0; i
<menu
->entry_no
; i
++) {
1126 paintEntry(menu
, i
, i
==menu
->selected_entry
);
1132 wMenuSetEnabled(WMenu
*menu
, int index
, int enable
)
1134 if (index
>=menu
->entry_no
) return;
1135 menu
->entries
[index
]->flags
.enabled
=enable
;
1136 paintEntry(menu
, index
, index
==menu
->selected_entry
);
1137 paintEntry(menu
->brother
, index
, index
==menu
->selected_entry
);
1141 /* ====================================================================== */
1145 editEntry(WMenu
*menu
, WMenuEntry
*entry
)
1149 WObjDescriptor
*desc
;
1155 menu
->flags
.editing
= 1;
1157 text
= wTextCreate(menu
->menu
, 1, menu
->entry_height
* entry
->order
,
1158 menu
->menu
->width
- 2, menu
->entry_height
- 1);
1160 wTextPutText(text
, entry
->text
);
1161 XGetInputFocus(dpy
, &old_focus
, &old_revert
);
1162 XSetInputFocus(dpy
, text
->core
->window
, RevertToNone
, CurrentTime
);
1164 if (XGrabKeyboard(dpy
, text
->core
->window
, True
, GrabModeAsync
,
1165 GrabModeAsync
, CurrentTime
)!=GrabSuccess
) {
1166 wwarning("could not grab keyboard");
1169 wSetFocusTo(menu
->frame
->screen_ptr
,
1170 menu
->frame
->screen_ptr
->focused_window
);
1175 while (!done
&& !text
->done
) {
1177 XAllowEvents(dpy
, AsyncKeyboard
|AsyncPointer
, CurrentTime
);
1179 WMNextEvent(dpy
, &event
);
1181 if (XFindContext(dpy
, event
.xany
.window
, wWinContext
,
1182 (XPointer
*)&desc
)==XCNOENT
)
1185 if ((desc
!= NULL
) && (desc
->handle_anything
!= NULL
)) {
1187 (*desc
->handle_anything
)(desc
, &event
);
1190 switch (event
.type
) {
1192 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
1196 WMHandleEvent(&event
);
1202 XSetInputFocus(dpy
, old_focus
, old_revert
, CurrentTime
);
1204 wSetFocusTo(menu
->frame
->screen_ptr
,
1205 menu
->frame
->screen_ptr
->focused_window
);
1208 t
= wTextGetText(text
);
1209 /* if !t, the user has canceled editing */
1213 entry
->text
= wstrdup(t
);
1215 menu
->flags
.realized
= 0;
1219 XUngrabKeyboard(dpy
, CurrentTime
);
1221 if (t
&& menu
->on_edit
)
1222 (*menu
->on_edit
)(menu
, entry
);
1224 menu
->flags
.editing
= 0;
1226 if (!menu
->flags
.realized
)
1232 selectEntry(WMenu
*menu
, int entry_no
)
1238 if (menu
->entries
==NULL
)
1241 if (entry_no
>= menu
->entry_no
)
1244 old_entry
= menu
->selected_entry
;
1245 menu
->selected_entry
= entry_no
;
1247 if (old_entry
!=entry_no
) {
1249 /* unselect previous entry */
1251 paintEntry(menu
, old_entry
, False
);
1252 entry
= menu
->entries
[old_entry
];
1255 if (entry
->cascade
>=0 && menu
->cascades
) {
1256 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1257 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1263 menu
->selected_entry
= -1;
1266 entry
= menu
->entries
[entry_no
];
1268 if (entry
->cascade
>=0 && menu
->cascades
&& entry
->flags
.enabled
) {
1269 /* Callback for when the submenu is opened.
1271 submenu
= menu
->cascades
[entry
->cascade
];
1272 if (submenu
&& submenu
->flags
.brother
)
1273 submenu
= submenu
->brother
;
1275 if (entry
->callback
) {
1276 /* Only call the callback if the submenu is not yet mapped.
1278 if (menu
->flags
.brother
) {
1279 if (!submenu
|| !submenu
->flags
.mapped
)
1280 (*entry
->callback
)(menu
->brother
, entry
);
1282 if (!submenu
|| !submenu
->flags
.buttoned
)
1283 (*entry
->callback
)(menu
, entry
);
1287 /* the submenu menu might have changed */
1288 submenu
= menu
->cascades
[entry
->cascade
];
1291 if (!submenu
->flags
.mapped
) {
1294 if (!submenu
->flags
.realized
)
1295 wMenuRealize(submenu
);
1296 if (wPreferences
.wrap_menus
) {
1297 if (menu
->flags
.open_to_left
)
1298 submenu
->flags
.open_to_left
= 1;
1300 if (submenu
->flags
.open_to_left
) {
1301 x
= menu
->frame_x
- MENUW(submenu
);
1304 submenu
->flags
.open_to_left
= 0;
1307 x
= menu
->frame_x
+ MENUW(menu
);
1309 if (x
+ MENUW(submenu
)
1310 >= menu
->frame
->screen_ptr
->scr_width
) {
1312 x
= menu
->frame_x
- MENUW(submenu
);
1313 submenu
->flags
.open_to_left
= 1;
1317 x
= menu
->frame_x
+ MENUW(menu
);
1320 if (wPreferences
.align_menus
) {
1323 y
= menu
->frame_y
+ menu
->entry_height
*entry_no
;
1324 if (menu
->flags
.titled
)
1325 y
+= menu
->frame
->top_width
;
1326 if (menu
->cascades
[entry
->cascade
]->flags
.titled
)
1327 y
-= menu
->cascades
[entry
->cascade
]->frame
->top_width
;
1330 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1331 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1336 paintEntry(menu
, entry_no
, True
);
1342 findMenu(WScreen
*scr
, int *x_ret
, int *y_ret
)
1345 WObjDescriptor
*desc
;
1346 Window root_ret
, win
, junk_win
;
1350 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
,
1353 if (win
==None
) return NULL
;
1355 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1358 if (desc
->parent_type
== WCLASS_MENU
) {
1359 menu
= (WMenu
*)desc
->parent
;
1360 XTranslateCoordinates(dpy
, root_ret
, menu
->menu
->window
, wx
, wy
,
1361 x_ret
, y_ret
, &junk_win
);
1371 closeCascade(WMenu
*menu
)
1373 WMenu
*parent
=menu
->parent
;
1375 if (menu
->flags
.brother
1376 || (!menu
->flags
.buttoned
1377 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
))) {
1379 selectEntry(menu
, -1);
1381 #if (MENU_BLINK_DELAY > 2)
1382 wusleep(MENU_BLINK_DELAY
/2);
1386 && (parent
->parent
!=NULL
|| !parent
->flags
.app_menu
1387 || parent
->flags
.brother
)
1388 && !parent
->flags
.buttoned
) {
1389 selectEntry(parent
, -1);
1391 parent
= parent
->parent
;
1394 selectEntry(parent
, -1);
1400 closeBrotherCascadesOf(WMenu
*menu
)
1405 for (i
=0; i
<menu
->cascade_no
; i
++) {
1406 if (menu
->cascades
[i
]->flags
.brother
) {
1407 tmp
= menu
->cascades
[i
];
1409 tmp
= menu
->cascades
[i
]->brother
;
1411 if (tmp
->flags
.mapped
) {
1412 selectEntry(tmp
->parent
, -1);
1413 closeBrotherCascadesOf(tmp
);
1420 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1424 parentMenu(WMenu
*menu
)
1429 if (menu
->flags
.buttoned
)
1432 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1433 parent
= menu
->parent
;
1434 if (parent
->selected_entry
< 0)
1436 entry
= parent
->entries
[parent
->selected_entry
];
1437 if (!entry
->flags
.enabled
|| entry
->cascade
<0 || !parent
->cascades
||
1438 parent
->cascades
[entry
->cascade
] != menu
)
1441 if (menu
->flags
.buttoned
)
1451 * Will raise the passed menu, if submenu = 0
1452 * If submenu > 0 will also raise all mapped submenus
1453 * until the first buttoned one
1454 * If submenu < 0 will also raise all mapped parent menus
1455 * until the first buttoned one
1459 raiseMenus(WMenu
*menu
, int submenus
)
1466 wRaiseFrame(menu
->frame
->core
);
1468 if (submenus
>0 && menu
->selected_entry
>=0) {
1469 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
1470 if (i
>=0 && menu
->cascades
) {
1471 submenu
= menu
->cascades
[i
];
1472 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
1473 raiseMenus(submenu
, submenus
);
1476 if (submenus
<0 && !menu
->flags
.buttoned
&&
1477 menu
->parent
&& menu
->parent
->flags
.mapped
)
1478 raiseMenus(menu
->parent
, submenus
);
1483 wMenuUnderPointer(WScreen
*screen
)
1485 WObjDescriptor
*desc
;
1486 Window root_ret
, win
;
1490 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
,
1491 &dummy
, &dummy
, &mask
);
1493 if (win
==None
) return NULL
;
1495 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1498 if (desc
->parent_type
== WCLASS_MENU
)
1499 return (WMenu
*)desc
->parent
;
1505 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1509 getPointerPosition(WScreen
*scr
, int *x
, int *y
)
1511 Window root_ret
, win
;
1515 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1520 getScrollAmount(WMenu
*menu
, int *hamount
, int *vamount
)
1522 WScreen
*scr
= menu
->menu
->screen_ptr
;
1523 int menuX1
= menu
->frame_x
;
1524 int menuY1
= menu
->frame_y
;
1525 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1526 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1527 int screenW
= scr
->scr_width
;
1528 int screenH
= scr
->scr_height
;
1534 getPointerPosition(scr
, &xroot
, &yroot
);
1537 if (xroot
<= 1 && menuX1
< 0) {
1538 /* scroll to the right */
1539 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX1
));
1541 } else if (xroot
>= screenW
-2 && menuX2
> screenW
-1) {
1542 /* scroll to the left */
1543 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX2
-screenW
-1));
1548 *hamount
= -*hamount
;
1551 if (yroot
<= 1 && menuY1
< 0) {
1553 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY1
));
1555 } else if (yroot
>= screenH
-2 && menuY2
> screenH
-1) {
1557 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY2
-screenH
-2));
1559 *vamount
= -*vamount
;
1565 dragScrollMenuCallback(void *data
)
1567 WMenu
*menu
= (WMenu
*)data
;
1568 WScreen
*scr
= menu
->menu
->screen_ptr
;
1569 WMenu
*parent
= parentMenu(menu
);
1570 int hamount
, vamount
;
1572 int newSelectedEntry
;
1574 getScrollAmount(menu
, &hamount
, &vamount
);
1577 if (hamount
!= 0 || vamount
!= 0) {
1578 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1579 parent
->frame_y
+ vamount
, True
);
1580 if (findMenu(scr
, &x
, &y
)) {
1581 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1582 selectEntry(menu
, newSelectedEntry
);
1584 /* Pointer fell outside of menu. If the selected entry is
1585 * not a submenu, unselect it */
1586 if (menu
->selected_entry
>= 0
1587 && menu
->entries
[menu
->selected_entry
]->cascade
<0)
1588 selectEntry(menu
, -1);
1589 newSelectedEntry
= 0;
1592 /* paranoid check */
1593 if (newSelectedEntry
>= 0) {
1594 /* keep scrolling */
1595 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1596 dragScrollMenuCallback
, menu
);
1601 /* don't need to scroll anymore */
1603 if (findMenu(scr
, &x
, &y
)) {
1604 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1605 selectEntry(menu
, newSelectedEntry
);
1612 scrollMenuCallback(void *data
)
1614 WMenu
*menu
= (WMenu
*)data
;
1615 WMenu
*parent
= parentMenu(menu
);
1616 int hamount
= 0; /* amount to scroll */
1619 getScrollAmount(menu
, &hamount
, &vamount
);
1621 if (hamount
!= 0 || vamount
!= 0) {
1622 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1623 parent
->frame_y
+ vamount
, True
);
1625 /* keep scrolling */
1626 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1627 scrollMenuCallback
, menu
);
1629 /* don't need to scroll anymore */
1636 #define MENU_SCROLL_BORDER 5
1639 isPointNearBoder(WMenu
*menu
, int x
, int y
)
1641 int menuX1
= menu
->frame_x
;
1642 int menuY1
= menu
->frame_y
;
1643 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1644 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1645 int scrXe
= menu
->menu
->screen_ptr
->scr_width
-1;
1646 int scrYe
= menu
->menu
->screen_ptr
->scr_height
-1;
1649 if (x
>= menuX1
&& x
<= menuX2
&& (y
< MENU_SCROLL_BORDER
1650 || y
> scrYe
-MENU_SCROLL_BORDER
))
1652 else if (y
>= menuY1
&& y
<= menuY2
&& (x
< MENU_SCROLL_BORDER
1653 || x
> scrXe
-MENU_SCROLL_BORDER
))
1661 wMenuScroll(WMenu
*menu
, XEvent
*event
)
1664 WMenu
*omenu
= parentMenu(menu
);
1665 WScreen
*scr
= menu
->frame
->screen_ptr
;
1668 int old_frame_x
= omenu
->frame_x
;
1669 int old_frame_y
= omenu
->frame_y
;
1673 puts("Entering menu Scroll");
1676 if ((/*omenu->flags.buttoned &&*/ !wPreferences
.wrap_menus
)
1677 || omenu
->flags
.app_menu
) {
1681 if (!wPreferences
.wrap_menus
)
1682 raiseMenus(omenu
, True
);
1684 raiseMenus(menu
, False
);
1687 scrollMenuCallback(menu
);
1690 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1692 WMNextEvent(dpy
, &ev
);
1697 x
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1698 y
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1700 /* on_border is != 0 if the pointer is between the menu
1701 * and the screen border and is close enough to the border */
1702 on_border
= isPointNearBoder(menu
, x
, y
);
1704 smenu
= wMenuUnderPointer(scr
);
1706 if ((smenu
==NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
)!=omenu
)) {
1711 on_x_edge
= x
<= 1 || x
>= scr
->scr_width
- 2;
1712 on_y_edge
= y
<= 1 || y
>= scr
->scr_height
- 2;
1713 on_border
= on_x_edge
|| on_y_edge
;
1715 if (!on_border
&& !jump_back
) {
1720 if (menu
->timer
&& (smenu
!=menu
|| (!on_y_edge
&& !on_x_edge
))) {
1721 WMDeleteTimerHandler(menu
->timer
);
1729 scrollMenuCallback(menu
);
1732 /* True if we push on title, or drag the omenu to other position */
1733 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1734 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1735 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1736 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1738 smenu
= wMenuUnderPointer(scr
);
1739 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1741 else if (smenu
==omenu
&& on_title
) {
1755 WMDeleteTimerHandler(menu
->timer
);
1760 wMenuMove(omenu
, old_frame_x
, old_frame_y
, True
);
1763 puts("Leaving menu Scroll");
1770 menuExpose(WObjDescriptor
*desc
, XEvent
*event
)
1772 wMenuPaint(desc
->parent
);
1776 int *delayed_select
;
1783 delaySelection(void *data
)
1785 delay_data
*d
= (delay_data
*)data
;
1791 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1792 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1793 entry_no
= getEntryAt(menu
, x
, y
);
1794 selectEntry(menu
, entry_no
);
1796 if (d
->delayed_select
)
1797 *(d
->delayed_select
) = 0;
1802 menuMouseDown(WObjDescriptor
*desc
, XEvent
*event
)
1804 XButtonEvent
*bev
= &event
->xbutton
;
1805 WMenu
*menu
= desc
->parent
;
1807 WScreen
*scr
=menu
->frame
->screen_ptr
;
1808 WMenuEntry
*entry
=NULL
;
1810 int close_on_exit
=0;
1812 int delayed_select
= 0;
1816 int old_frame_x
= 0;
1817 int old_frame_y
= 0;
1818 delay_data d_data
= {NULL
, NULL
, NULL
};
1820 if (!wPreferences
.wrap_menus
) {
1821 smenu
= parentMenu(menu
);
1822 old_frame_x
= smenu
->frame_x
;
1823 old_frame_y
= smenu
->frame_y
;
1825 else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1826 /* This is true if the menu was launched with right click on root window */
1828 d_data
.delayed_select
= &delayed_select
;
1830 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
,
1831 delaySelection
, &d_data
);
1834 wRaiseFrame(menu
->frame
->core
);
1836 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1838 smenu
= findMenu(scr
, &x
, &y
);
1846 if (menu
->flags
.editing
) {
1849 entry_no
= getEntryAt(menu
, x
, y
);
1851 entry
= menu
->entries
[entry_no
];
1853 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
1854 && entry
->flags
.editable
) {
1855 editEntry(smenu
, entry
);
1857 } else if (bev
->state
& ControlMask
) {
1861 if (entry
->flags
.enabled
&& entry
->cascade
>=0 && menu
->cascades
) {
1862 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1864 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&&
1865 menu
->selected_entry
!=entry_no
) {
1866 wMenuUnmap(submenu
);
1868 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1869 selectEntry(menu
, entry_no
);
1870 } else if (!submenu
->flags
.buttoned
) {
1871 selectEntry(menu
, -1);
1874 } else if (!delayed_select
) {
1875 selectEntry(menu
, entry_no
);
1878 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1880 dragScrollMenuCallback(menu
);
1884 prevx
= bev
->x_root
;
1885 prevy
= bev
->y_root
;
1889 XAllowEvents(dpy
, SyncPointer
, CurrentTime
);
1891 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonReleaseMask
1892 |ButtonPressMask
, &ev
);
1895 smenu
= findMenu(scr
, &x
, &y
);
1897 if (smenu
== NULL
) {
1898 /* moved mouse out of menu */
1900 if (!delayed_select
&& d_data
.magic
) {
1901 WMDeleteTimerHandler(d_data
.magic
);
1902 d_data
.magic
= NULL
;
1905 || (menu
->selected_entry
>=0
1906 && menu
->entries
[menu
->selected_entry
]->cascade
>=0)) {
1907 prevx
= ev
.xmotion
.x_root
;
1908 prevy
= ev
.xmotion
.y_root
;
1912 selectEntry(menu
, -1);
1914 prevx
= ev
.xmotion
.x_root
;
1915 prevy
= ev
.xmotion
.y_root
;
1917 } else if (menu
&& menu
!=smenu
1918 && (menu
->selected_entry
<0
1919 || menu
->entries
[menu
->selected_entry
]->cascade
<0)) {
1920 selectEntry(menu
, -1);
1922 if (!delayed_select
&& d_data
.magic
) {
1923 WMDeleteTimerHandler(d_data
.magic
);
1924 d_data
.magic
= NULL
;
1928 /* hysteresis for item selection */
1930 /* check if the motion was to the side, indicating that
1931 * the user may want to cross to a submenu */
1932 if (!delayed_select
&& menu
) {
1934 Bool moved_to_submenu
;/* moved to direction of submenu */
1936 dx
= abs(prevx
- ev
.xmotion
.x_root
);
1938 moved_to_submenu
= False
;
1939 if (dx
> 0 /* if moved enough to the side */
1940 /* maybe a open submenu */
1941 && menu
->selected_entry
>=0
1942 /* moving to the right direction */
1943 && (wPreferences
.align_menus
1944 || ev
.xmotion
.y_root
>= prevy
)) {
1947 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
1949 if (menu
->cascades
[index
]->frame_x
>menu
->frame_x
) {
1950 if (prevx
< ev
.xmotion
.x_root
)
1951 moved_to_submenu
= True
;
1953 if (prevx
> ev
.xmotion
.x_root
)
1954 moved_to_submenu
= True
;
1960 if (menu
!= smenu
) {
1962 WMDeleteTimerHandler(d_data
.magic
);
1964 d_data
.magic
= NULL
;
1965 } else if (moved_to_submenu
) {
1966 /* while we are moving, postpone the selection */
1968 WMDeleteTimerHandler(d_data
.magic
);
1970 d_data
.delayed_select
= NULL
;
1972 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
1975 prevx
= ev
.xmotion
.x_root
;
1976 prevy
= ev
.xmotion
.y_root
;
1980 WMDeleteTimerHandler(d_data
.magic
);
1981 d_data
.magic
= NULL
;
1985 prevx
= ev
.xmotion
.x_root
;
1986 prevy
= ev
.xmotion
.y_root
;
1988 /* pointer crossed menus */
1989 if (menu
&& menu
->timer
) {
1990 WMDeleteTimerHandler(menu
->timer
);
1994 dragScrollMenuCallback(smenu
);
1998 dragScrollMenuCallback(menu
);
2000 if (!delayed_select
) {
2001 entry_no
= getEntryAt(menu
, x
, y
);
2003 entry
= menu
->entries
[entry_no
];
2004 if (entry
->flags
.enabled
&& entry
->cascade
>=0 &&
2006 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
2007 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
2008 && menu
->selected_entry
!=entry_no
) {
2009 wMenuUnmap(submenu
);
2013 selectEntry(menu
, entry_no
);
2021 if (ev
.xbutton
.button
== event
->xbutton
.button
)
2031 if (menu
&& menu
->timer
) {
2032 WMDeleteTimerHandler(menu
->timer
);
2035 if (d_data
.magic
!=NULL
)
2036 WMDeleteTimerHandler(d_data
.magic
);
2038 if (menu
&& menu
->selected_entry
>=0) {
2039 entry
= menu
->entries
[menu
->selected_entry
];
2040 if (entry
->callback
!=NULL
&& entry
->flags
.enabled
2041 && entry
->cascade
< 0) {
2042 /* blink and erase menu selection */
2043 #if (MENU_BLINK_DELAY > 0)
2044 int sel
= menu
->selected_entry
;
2047 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
2048 paintEntry(menu
, sel
, False
);
2050 wusleep(MENU_BLINK_DELAY
);
2051 paintEntry(menu
, sel
, True
);
2053 wusleep(MENU_BLINK_DELAY
);
2056 /* unmap the menu, it's parents and call the callback */
2057 if (!menu
->flags
.buttoned
&&
2058 (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2061 selectEntry(menu
, -1);
2063 (*entry
->callback
)(menu
, entry
);
2065 /* If the user double clicks an entry, the entry will
2066 * be executed twice, which is not good for things like
2067 * the root menu. So, ignore any clicks that were generated
2068 * while the entry was being executed */
2069 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
,
2071 } else if (entry
->callback
!=NULL
&& entry
->cascade
<0) {
2072 selectEntry(menu
, -1);
2074 if (entry
->cascade
>=0 && menu
->cascades
2075 && menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2076 selectEntry(menu
, -1);
2081 if (((WMenu
*)desc
->parent
)->flags
.brother
|| close_on_exit
2083 closeCascade(desc
->parent
);
2085 /* close the cascade windows that should not remain opened */
2086 closeBrotherCascadesOf(desc
->parent
);
2088 if (!wPreferences
.wrap_menus
)
2089 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2094 wMenuMove(WMenu
*menu
, int x
, int y
, int submenus
)
2103 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2105 if (submenus
>0 && menu
->selected_entry
>=0) {
2106 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2108 if (i
>=0 && menu
->cascades
) {
2109 submenu
= menu
->cascades
[i
];
2110 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
2111 if (wPreferences
.align_menus
) {
2112 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2114 wMenuMove(submenu
, x
+ MENUW(menu
),
2115 y
+ submenu
->entry_height
*menu
->selected_entry
,
2120 if (submenus
<0 && menu
->parent
!=NULL
&& menu
->parent
->flags
.mapped
&&
2121 !menu
->parent
->flags
.buttoned
) {
2122 if (wPreferences
.align_menus
) {
2123 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2125 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2126 - menu
->parent
->entry_height
*menu
->parent
->selected_entry
,
2134 changeMenuLevels(WMenu
*menu
, int lower
)
2139 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
2141 wRaiseFrame(menu
->frame
->core
);
2142 menu
->flags
.lowered
= 0;
2144 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2145 wLowerFrame(menu
->frame
->core
);
2146 menu
->flags
.lowered
= 1;
2148 for (i
=0; i
<menu
->cascade_no
; i
++) {
2149 if (menu
->cascades
[i
]
2150 && !menu
->cascades
[i
]->flags
.buttoned
2151 && menu
->cascades
[i
]->flags
.lowered
!=lower
) {
2152 changeMenuLevels(menu
->cascades
[i
], lower
);
2160 menuTitleDoubleClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2165 if (event
->xbutton
.state
& MOD_MASK
) {
2166 if (menu
->flags
.lowered
) {
2171 changeMenuLevels(menu
, lower
);
2177 menuTitleMouseDown(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2182 int x
=menu
->frame_x
, y
=menu
->frame_y
;
2183 int dx
=event
->xbutton
.x_root
, dy
=event
->xbutton
.y_root
;
2188 printf("Moving menu\n");
2191 /* can't touch the menu copy */
2192 if (menu
->flags
.brother
)
2195 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2198 if (event
->xbutton
.state
& MOD_MASK
) {
2199 wLowerFrame(menu
->frame
->core
);
2202 wRaiseFrame(menu
->frame
->core
);
2207 /* lower/raise all submenus */
2209 if (tmp
->selected_entry
>=0 && tmp
->cascades
2210 && tmp
->entries
[tmp
->selected_entry
]->cascade
>=0) {
2211 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2212 if (!tmp
|| !tmp
->flags
.mapped
)
2215 wLowerFrame(tmp
->frame
->core
);
2217 wRaiseFrame(tmp
->frame
->core
);
2223 /* tear off the menu if it's a root menu or a cascade
2225 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
2226 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2227 menu
->flags
.buttoned
=1;
2228 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2230 /* turn off selected menu entry in parent menu */
2231 selectEntry(menu
->parent
, -1);
2233 /* make parent map the copy in place of the original */
2234 for (i
=0; i
<menu
->parent
->cascade_no
; i
++) {
2235 if (menu
->parent
->cascades
[i
] == menu
) {
2236 menu
->parent
->cascades
[i
] = menu
->brother
;
2245 WMMaskEvent(dpy
, ButtonMotionMask
|ButtonReleaseMask
|ButtonPressMask
2246 |ExposureMask
, &ev
);
2250 x
+= ev
.xmotion
.x_root
- dx
;
2251 y
+= ev
.xmotion
.y_root
- dy
;
2252 dx
= ev
.xmotion
.x_root
;
2253 dy
= ev
.xmotion
.y_root
;
2254 wMenuMove(menu
, x
, y
, True
);
2256 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2257 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2259 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2260 ButtonMotionMask
|ButtonReleaseMask
2262 GrabModeAsync
, GrabModeAsync
, None
,
2263 wCursor
[WCUR_MOVE
], CurrentTime
);
2272 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2275 printf("End menu move\n");
2277 XUngrabPointer(dpy
, CurrentTime
);
2288 *----------------------------------------------------------------------
2290 * Handles mouse click on the close button of menus. The menu is
2291 * closed when the button is clicked.
2294 * The closed menu is reinserted at it's parent menus
2296 *----------------------------------------------------------------------
2299 menuCloseClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2301 WMenu
*menu
= (WMenu
*)data
;
2302 WMenu
*parent
= menu
->parent
;
2306 for (i
=0; i
<parent
->cascade_no
; i
++) {
2307 /* find the entry that points to the copy */
2308 if (parent
->cascades
[i
] == menu
->brother
) {
2309 /* make it point to the original */
2310 parent
->cascades
[i
] = menu
;
2311 menu
->parent
= parent
;
2321 wMenuSaveState(WScreen
*scr
)
2323 proplist_t menus
, key
, value
;
2327 menus
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
2330 if (scr
->root_menu
&& scr
->root_menu
->flags
.buttoned
) {
2331 sprintf(buffer
, "%i,%i", scr
->root_menu
->frame_x
,
2332 scr
->root_menu
->frame_y
);
2333 key
= PLMakeString("RootMenu");
2334 value
= PLMakeString(buffer
);
2335 PLInsertDictionaryEntry(menus
, key
, value
);
2341 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2342 sprintf(buffer
, "%i,%i", scr
->switch_menu
->frame_x
,
2343 scr
->switch_menu
->frame_y
);
2344 key
= PLMakeString("SwitchMenu");
2345 value
= PLMakeString(buffer
);
2346 PLInsertDictionaryEntry(menus
, key
, value
);
2352 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2353 sprintf(buffer
, "%i,%i", scr
->workspace_menu
->frame_x
,
2354 scr
->workspace_menu
->frame_y
);
2355 key
= PLMakeString("WorkspaceMenu");
2356 value
= PLMakeString(buffer
);
2357 PLInsertDictionaryEntry(menus
, key
, value
);
2364 key
= PLMakeString("Menus");
2365 PLInsertDictionaryEntry(scr
->session_state
, key
, menus
);
2372 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2376 restoreMenu(WScreen
*scr
, proplist_t menu
, int which
)
2379 WMenu
*pmenu
= NULL
;
2384 if (!PLIsString(menu
)) {
2385 COMPLAIN("Position");
2389 if (sscanf(PLGetString(menu
), "%i,%i", &x
, &y
)!=2)
2390 COMPLAIN("Position");
2393 if (which
& WSS_ROOTMENU
) {
2394 OpenRootMenu(scr
, x
, y
, False
);
2395 pmenu
= scr
->root_menu
;
2396 } else if (which
& WSS_SWITCHMENU
) {
2397 OpenSwitchMenu(scr
, x
, y
, False
);
2398 pmenu
= scr
->switch_menu
;
2401 if (which
& WSS_WSMENU
) {
2402 OpenWorkspaceMenu(scr
, x
, y
);
2403 pmenu
= scr
->workspace_menu
;
2404 if (pmenu
->parent
) {
2405 /* make parent map the copy in place of the original */
2406 for (i
=0; i
<pmenu
->parent
->cascade_no
; i
++) {
2407 if (pmenu
->parent
->cascades
[i
] == pmenu
) {
2408 pmenu
->parent
->cascades
[i
] = pmenu
->brother
;
2416 int width
= MENUW(pmenu
);
2417 int height
= MENUH(pmenu
);
2419 x
= (x
< -width
) ? 0 : x
;
2420 x
= (x
> scr
->scr_width
) ? scr
->scr_width
- width
: x
;
2421 y
= (y
< 0) ? 0 : y
;
2422 y
= (y
> scr
->scr_height
) ? scr
->scr_height
- height
: y
;
2423 wMenuMove(pmenu
, x
, y
, True
);
2424 pmenu
->flags
.buttoned
= 1;
2425 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2433 wMenuRestoreState(WScreen
*scr
)
2435 proplist_t menus
, menu
, key
, rkey
, skey
, wkey
;
2437 key
= PLMakeString("Menus");
2438 menus
= PLGetDictionaryEntry(scr
->session_state
, key
);
2446 rkey
= PLMakeString("RootMenu");
2447 skey
= PLMakeString("SwitchMenu");
2448 wkey
= PLMakeString("WorkspaceMenu");
2449 menu
= PLGetDictionaryEntry(menus
, rkey
);
2450 restoreMenu(scr
, menu
, WSS_ROOTMENU
);
2451 menu
= PLGetDictionaryEntry(menus
, skey
);
2452 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2453 menu
= PLGetDictionaryEntry(menus
, wkey
);
2454 restoreMenu(scr
, menu
, WSS_WSMENU
);
2463 OpenWorkspaceMenu(WScreen
*scr
, int x
, int y
)
2465 WMenu
*menu
, *parent
;
2469 if (!scr
->root_menu
) {
2470 OpenRootMenu(scr
, scr
->scr_width
*2, 0, False
);
2471 wMenuUnmap(scr
->root_menu
);
2474 menu
= scr
->workspace_menu
;
2476 if (menu
->flags
.mapped
) {
2477 if (!menu
->flags
.buttoned
) {
2479 parent
= menu
->parent
;
2480 if (parent
&& parent
->selected_entry
>= 0) {
2481 entry
= parent
->entries
[parent
->selected_entry
];
2482 if (parent
->cascades
[entry
->cascade
] == menu
) {
2483 selectEntry(parent
, -1);
2484 wMenuMapAt(menu
, x
, y
, False
);
2488 wRaiseFrame(menu
->frame
->core
);
2489 wMenuMapCopyAt(menu
, x
, y
);
2493 wMenuMapAt(menu
, x
, y
, False
);