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
;
145 menu
->frame
->rbutton_image
= screen
->b_pixmaps
[WBUT_CLOSE
];
149 menu
->alloced_entries
= 0;
150 menu
->selected_entry
= -1;
151 menu
->entries
= NULL
;
153 menu
->frame_x
= screen
->app_menu_x
;
154 menu
->frame_y
= screen
->app_menu_y
;
156 menu
->frame
->child
= menu
;
158 menu
->flags
.lowered
= 0;
162 /* setup object descriptors */
163 menu
->frame
->on_mousedown_titlebar
= menuTitleMouseDown
;
164 menu
->frame
->on_dblclick_titlebar
= menuTitleDoubleClick
;
167 menu
->frame
->on_click_right
= menuCloseClick
;
170 menu
->menu
= wCoreCreate(menu
->frame
->core
, 0, menu
->frame
->top_width
,
171 menu
->frame
->core
->width
, 10);
173 menu
->menu
->descriptor
.parent
= menu
;
174 menu
->menu
->descriptor
.parent_type
= WCLASS_MENU
;
175 menu
->menu
->descriptor
.handle_expose
= menuExpose
;
176 menu
->menu
->descriptor
.handle_mousedown
= menuMouseDown
;
178 menu
->menu_texture_data
= None
;
180 XMapWindow(dpy
, menu
->menu
->window
);
186 menu
->brother
= wMenuCreate(screen
, title
, main_menu
);
188 menu
->brother
->flags
.brother
= 1;
189 menu
->brother
->brother
= menu
;
200 wMenuCreateForApp(WScreen
*screen
, char *title
, int main_menu
)
204 menu
= wMenuCreate(screen
, title
, main_menu
);
207 menu
->flags
.app_menu
= 1;
208 menu
->brother
->flags
.app_menu
= 1;
216 insertEntry(WMenu
*menu
, WMenuEntry
*entry
, int index
)
220 for (i
= menu
->entry_no
-1; i
>= index
; i
--) {
221 menu
->entries
[i
]->order
++;
222 menu
->entries
[i
+1] = menu
->entries
[i
];
224 menu
->entries
[index
] = entry
;
229 wMenuRefresh(WMenu
*menu
, int flags
)
233 if (flags
& MR_TEXT_BACK
) {
234 menu
->frame
->flags
.need_texture_remake
= 1;
237 if (flags
& (MR_RESIZED
|MR_TITLE_TEXT
)) {
238 menu
->flags
.realized
= 0;
243 if (menu
->flags
.titled
)
244 wFrameWindowPaint(menu
->frame
);
246 if (!menu
->flags
.brother
) {
248 wMenuRefresh(menu
->brother
, flags
);
250 for (i
=0; i
< menu
->cascade_no
; i
++) {
251 if (!menu
->cascades
[i
]->flags
.brother
)
252 wMenuRefresh(menu
->cascades
[i
], flags
);
254 wMenuRefresh(menu
->cascades
[i
]->brother
, flags
);
262 wMenuInsertCallback(WMenu
*menu
, int index
, char *text
,
263 void (*callback
)(WMenu
*menu
, WMenuEntry
*entry
),
270 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
275 assert(menu
->flags
.brother
==0);
276 menu
->flags
.realized
= 0;
277 menu
->brother
->flags
.realized
= 0;
279 /* reallocate array if it's too small */
280 if (menu
->entry_no
>= menu
->alloced_entries
) {
283 puts("doing wrealloc()");
285 tmp
= wrealloc(menu
->entries
,
286 sizeof(WMenuEntry
)*(menu
->alloced_entries
+5));
288 wwarning(_("wrealloc() failed while trying to add menu item"));
293 menu
->alloced_entries
+= 5;
295 menu
->brother
->entries
= tmp
;
296 menu
->brother
->alloced_entries
= menu
->alloced_entries
;
298 entry
= wmalloc(sizeof(WMenuEntry
));
299 memset(entry
, 0, sizeof(WMenuEntry
));
300 entry
->flags
.enabled
= 1;
301 entry
->text
= wstrdup(text
);
303 entry
->clientdata
= clientdata
;
304 entry
->callback
= callback
;
305 if (index
<0 || index
>=menu
->entry_no
) {
306 entry
->order
= menu
->entry_no
;
307 menu
->entries
[menu
->entry_no
] = entry
;
309 entry
->order
= index
;
310 insertEntry(menu
, entry
, index
);
314 menu
->brother
->entry_no
= menu
->entry_no
;
322 wMenuEntrySetCascade(WMenu
*menu
, WMenuEntry
*entry
, WMenu
*cascade
)
324 WMenu
*brother
= menu
->brother
;
327 assert(menu
->flags
.brother
==0);
329 if (entry
->cascade
>=0) {
330 menu
->flags
.realized
= 0;
331 brother
->flags
.realized
= 0;
334 cascade
->parent
= menu
;
336 cascade
->brother
->parent
= brother
;
339 for (i
=0; i
<menu
->cascade_no
; i
++) {
340 if (menu
->cascades
[i
]==NULL
) {
341 menu
->cascades
[i
] = cascade
;
342 brother
->cascades
[i
] = cascade
->brother
;
349 entry
->cascade
= menu
->cascade_no
;
351 menu
->cascades
= wrealloc(menu
->cascades
,
352 sizeof(WMenu
)*(menu
->cascade_no
+1));
353 menu
->cascades
[menu
->cascade_no
++] = cascade
;
356 brother
->cascades
= wrealloc(brother
->cascades
,
357 sizeof(WMenu
)*(brother
->cascade_no
+1));
358 brother
->cascades
[brother
->cascade_no
++] = cascade
->brother
;
362 if (menu
->flags
.lowered
) {
364 cascade
->flags
.lowered
= 1;
365 ChangeStackingLevel(cascade
->frame
->core
, WMNormalLevel
);
367 cascade
->brother
->flags
.lowered
= 1;
368 ChangeStackingLevel(cascade
->brother
->frame
->core
, WMNormalLevel
);
371 if (!menu
->flags
.realized
)
377 wMenuEntryRemoveCascade(WMenu
*menu
, WMenuEntry
*entry
)
379 assert(menu
->flags
.brother
==0);
381 /* destroy cascade menu */
382 if (entry
->cascade
>=0 && menu
->cascades
383 && menu
->cascades
[entry
->cascade
]!=NULL
) {
385 wMenuDestroy(menu
->cascades
[entry
->cascade
], True
);
387 menu
->cascades
[entry
->cascade
] = NULL
;
388 menu
->brother
->cascades
[entry
->cascade
] = NULL
;
396 wMenuRemoveItem(WMenu
*menu
, int index
)
400 if (menu
->flags
.brother
) {
401 wMenuRemoveItem(menu
->brother
, index
);
405 if (index
>=menu
->entry_no
) return;
407 /* destroy cascade menu */
408 wMenuEntryRemoveCascade(menu
, menu
->entries
[index
]);
410 /* destroy unshared data */
412 if (menu
->entries
[index
]->text
)
413 free(menu
->entries
[index
]->text
);
415 if (menu
->entries
[index
]->rtext
)
416 free(menu
->entries
[index
]->rtext
);
418 if (menu
->entries
[index
]->free_cdata
&& menu
->entries
[index
]->clientdata
)
419 (*menu
->entries
[index
]->free_cdata
)(menu
->entries
[index
]->clientdata
);
421 free(menu
->entries
[index
]);
423 for (i
=index
; i
<menu
->entry_no
-1; i
++) {
424 menu
->entries
[i
+1]->order
--;
425 menu
->entries
[i
]=menu
->entries
[i
+1];
428 menu
->brother
->entry_no
--;
434 wMenuRealize(WMenu
*menu
)
437 int width
, rwidth
, mrwidth
, mwidth
;
438 int theight
, twidth
, eheight
;
439 WScreen
*scr
= menu
->frame
->screen_ptr
;
440 static int brother_done
=0;
445 wMenuRealize(menu
->brother
);
449 flags
= WFF_SINGLE_STATE
;
450 if (menu
->flags
.titled
)
451 flags
|= WFF_TITLEBAR
|WFF_RIGHT_BUTTON
;
453 wFrameWindowUpdateBorders(menu
->frame
, flags
);
455 if (menu
->flags
.titled
) {
456 twidth
= wTextWidth(scr
->menu_title_font
->font
, menu
->frame
->title
,
457 strlen(menu
->frame
->title
));
458 theight
= menu
->frame
->top_width
;
459 twidth
+= theight
+ (wPreferences
.new_style
? 16 : 8);
464 eheight
= scr
->menu_entry_font
->height
+ 6;
465 menu
->entry_height
= eheight
;
468 for (i
=0; i
<menu
->entry_no
; i
++) {
471 /* search widest text */
472 text
= menu
->entries
[i
]->text
;
473 width
= wTextWidth(scr
->menu_entry_font
->font
, text
, strlen(text
))+10;
475 if (menu
->entries
[i
]->flags
.indicator
) {
476 width
+= MENU_INDICATOR_SPACE
;
482 /* search widest text on right */
483 text
= menu
->entries
[i
]->rtext
;
485 rwidth
= wTextWidth(scr
->menu_entry_font
->font
, text
,
487 else if (menu
->entries
[i
]->cascade
>=0)
492 if (rwidth
> mrwidth
)
501 wCoreConfigure(menu
->menu
, 0, theight
, mwidth
, menu
->entry_no
*eheight
-1);
503 wFrameWindowResize(menu
->frame
, mwidth
, menu
->entry_no
*eheight
-1
504 + menu
->frame
->top_width
+ menu
->frame
->bottom_width
);
507 /* setup background texture */
508 if (scr
->menu_item_texture
->any
.type
!= WTEX_SOLID
) {
509 if (!menu
->flags
.brother
) {
510 FREE_PIXMAP(menu
->menu_texture_data
);
512 wTextureRender(scr
, scr
->menu_item_texture
,
513 &menu
->menu_texture_data
, menu
->menu
->width
,
514 menu
->entry_height
, WREL_MENUENTRY
);
516 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
,
517 menu
->menu_texture_data
);
518 XClearWindow(dpy
, menu
->menu
->window
);
520 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
,
521 menu
->menu_texture_data
);
522 XClearWindow(dpy
, menu
->brother
->menu
->window
);
525 XSetWindowBackground(dpy
, menu
->menu
->window
,
526 scr
->menu_item_texture
->any
.color
.pixel
);
527 XClearWindow(dpy
, menu
->menu
->window
);
530 menu
->flags
.realized
= 1;
532 if (menu
->flags
.mapped
)
534 if (menu
->brother
->flags
.mapped
)
535 wMenuPaint(menu
->brother
);
540 wMenuDestroy(WMenu
*menu
, int recurse
)
544 /* remove any pending timers */
546 WMDeleteTimerHandler(menu
->timer
);
549 /* call destroy handler */
550 if (menu
->on_destroy
)
551 (*menu
->on_destroy
)(menu
);
553 /* Destroy items if this menu own them. If this is the "brother" menu,
554 * leave them alone as it is shared by them.
556 if (!menu
->flags
.brother
) {
557 for (i
=0; i
<menu
->entry_no
; i
++) {
559 free(menu
->entries
[i
]->text
);
561 if (menu
->entries
[i
]->rtext
)
562 free(menu
->entries
[i
]->rtext
);
564 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
565 (*menu
->entries
[i
]->free_cdata
)(menu
->entries
[i
]->clientdata
);
567 free(menu
->entries
[i
]);
571 for (i
=0; i
<menu
->cascade_no
; i
++) {
572 if (menu
->cascades
[i
]) {
573 if (menu
->cascades
[i
]->flags
.brother
)
574 wMenuDestroy(menu
->cascades
[i
]->brother
, recurse
);
576 wMenuDestroy(menu
->cascades
[i
], recurse
);
586 FREE_PIXMAP(menu
->menu_texture_data
);
589 free(menu
->cascades
);
591 wCoreDestroy(menu
->menu
);
592 wFrameWindowDestroy(menu
->frame
);
594 /* destroy copy of this menu */
595 if (!menu
->flags
.brother
&& menu
->brother
)
596 wMenuDestroy(menu
->brother
, False
);
603 drawFrame(WScreen
*scr
, Window win
, int y
, int w
, int h
)
614 segs
[1].y2
= y
+ h
- 2;
615 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, 2);
617 XDrawLine(dpy
, win
, scr
->menu_item_auxtexture
->dark_gc
, 0, y
+h
-1,
621 segs
[0].y1
= segs
[0].y2
= y
+ h
-2;
623 segs
[1].x1
= segs
[1].x2
= w
-1;
625 segs
[1].y2
= y
+ h
-2;
626 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, 2);
631 paintEntry(WMenu
*menu
, int index
, int selected
)
634 GC light
, dim
, dark
, textGC
;
635 WScreen
*scr
=menu
->frame
->screen_ptr
;
636 Window win
= menu
->menu
->window
;
637 WMenuEntry
*entry
=menu
->entries
[index
];
639 if (!menu
->flags
.realized
) return;
640 h
= menu
->entry_height
;
641 w
= menu
->menu
->width
;
644 light
= scr
->menu_item_auxtexture
->light_gc
;
645 dim
= scr
->menu_item_auxtexture
->dim_gc
;
646 dark
= scr
->menu_item_auxtexture
->dark_gc
;
648 /* paint background */
650 XSetForeground(dpy
, scr
->select_menu_gc
, scr
->select_pixel
);
651 XFillRectangle(dpy
, win
, scr
->select_menu_gc
, 1, y
+1, w
-2, h
-3);
652 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
)
653 drawFrame(scr
, win
, y
, w
, h
);
655 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
656 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
658 drawFrame(scr
, win
, y
, w
, h
);
660 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
665 textGC
= scr
->select_menu_gc
;
666 if (entry
->flags
.enabled
)
667 XSetForeground(dpy
, textGC
, scr
->select_text_pixel
);
669 XSetForeground(dpy
, textGC
, scr
->dtext_pixel
);
670 } else if (!entry
->flags
.enabled
) {
671 textGC
= scr
->disabled_menu_entry_gc
;
673 textGC
= scr
->menu_entry_gc
;
677 if (entry
->flags
.indicator
)
678 x
+= MENU_INDICATOR_SPACE
+ 2;
680 wDrawString(win
, scr
->menu_entry_font
,
681 textGC
, x
, 3+y
+scr
->menu_entry_font
->y
, entry
->text
,
682 strlen(entry
->text
));
684 if (entry
->cascade
>=0) {
685 /* draw the cascade indicator */
686 XDrawLine(dpy
,win
,dim
, w
-11, y
+6, w
-6, y
+h
/2-1);
687 XDrawLine(dpy
,win
,light
, w
-11, y
+h
-8, w
-6, y
+h
/2-1);
688 XDrawLine(dpy
,win
,dark
, w
-12, y
+6, w
-12, y
+h
-8);
692 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
697 switch (entry
->flags
.indicator_type
) {
699 indicator
= scr
->menu_check_indicator
;
702 indicator
= scr
->menu_mini_indicator
;
705 indicator
= scr
->menu_hide_indicator
;
708 indicator
= scr
->menu_shade_indicator
;
712 indicator
= scr
->menu_radio_indicator
;
716 iw
= indicator
->width
;
717 ih
= indicator
->height
;
718 XSetClipMask(dpy
, scr
->copy_gc
, indicator
->mask
);
719 XSetClipOrigin(dpy
, scr
->copy_gc
, 5, y
+(h
-ih
)/2);
721 XSetForeground(dpy
, scr
->copy_gc
, scr
->black_pixel
);
723 XSetForeground(dpy
, scr
->copy_gc
, scr
->mtext_pixel
);
724 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+(h
-ih
)/2, iw
, ih
);
726 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
727 iw, ih, 5, y+(h-ih)/2);
729 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
732 /* draw right text */
734 if (entry
->rtext
&& entry
->cascade
<0) {
735 tw
= wTextWidth(scr
->menu_entry_font
->font
, entry
->rtext
,
736 strlen(entry
->rtext
));
738 wDrawString(win
, scr
->menu_entry_font
, textGC
, w
-6-tw
,
739 3+y
+scr
->menu_entry_font
->y
, entry
->rtext
,
740 strlen(entry
->rtext
));
745 move_menus(WMenu
*menu
, int x
, int y
)
747 while (menu
->parent
) {
750 if (!wPreferences
.align_menus
&& menu
->selected_entry
>=0) {
751 y
-= menu
->selected_entry
*menu
->entry_height
;
754 wMenuMove(menu
, x
, y
, True
);
758 makeVisible(WMenu
*menu
)
760 WScreen
*scr
= menu
->frame
->screen_ptr
;
761 int x1
, y1
, x2
, y2
, new_x
, new_y
, move
;
763 if (menu
->entry_no
<0) return;
766 y1
= menu
->frame_y
+menu
->frame
->top_width
767 + menu
->selected_entry
*menu
->entry_height
;
768 x2
= x1
+ MENUW(menu
);
769 y2
= y1
+ menu
->entry_height
;
778 } else if (x2
>= scr
->scr_width
) {
779 new_x
= scr
->scr_width
- MENUW(menu
) - 1;
786 } else if (y2
>= scr
->scr_height
) {
787 new_y
= scr
->scr_height
- menu
->entry_height
- 1;
791 new_y
= new_y
- menu
->frame
->top_width
792 - menu
->selected_entry
*menu
->entry_height
;
793 move_menus(menu
, new_x
, new_y
);
798 check_key(WMenu
*menu
, XKeyEvent
*event
)
803 if (XLookupString(event
, buffer
, 32, NULL
, NULL
)<1)
806 ch
= toupper(buffer
[0]);
808 s
= (menu
->selected_entry
>=0 ? menu
->selected_entry
+1 : 0);
811 for (i
=s
; i
<menu
->entry_no
; i
++) {
812 if (ch
==toupper(menu
->entries
[i
]->text
[0])) {
816 /* no match. Retry from start, if previous started from a selected entry */
826 keyboardMenu(WMenu
*menu
)
829 KeySym ksym
=NoSymbol
;
833 int old_pos_x
= menu
->frame_x
;
834 int old_pos_y
= menu
->frame_y
;
835 int new_x
= old_pos_x
, new_y
= old_pos_y
;
836 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
837 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
839 if (menu
->flags
.editing
)
843 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
,
844 GrabModeAsync
, CurrentTime
);
846 if (menu
->frame_y
+menu
->frame
->top_width
>= scr_height
)
847 new_y
= scr_height
- menu
->frame
->top_width
;
849 if (menu
->frame_x
+MENUW(menu
) >= scr_width
)
850 new_x
= scr_width
-MENUW(menu
)-1;
852 move_menus(menu
, new_x
, new_y
);
854 while (!done
&& menu
->flags
.mapped
) {
855 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
856 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonPressMask
857 |ButtonReleaseMask
|KeyPressMask
|KeyReleaseMask
858 |SubstructureNotifyMask
, &event
);
860 switch (event
.type
) {
862 ksym
= XLookupKeysym(&event
.xkey
, 0);
870 selectEntry(menu
, 0);
876 selectEntry(menu
, menu
->entry_no
-1);
885 if (menu
->selected_entry
<= 0)
886 selectEntry(menu
, menu
->entry_no
-1);
888 selectEntry(menu
, menu
->selected_entry
-1);
897 if (menu
->selected_entry
<0)
898 selectEntry(menu
, 0);
899 else if (menu
->selected_entry
== menu
->entry_no
-1)
900 selectEntry(menu
, 0);
901 else if (menu
->selected_entry
< menu
->entry_no
-1)
902 selectEntry(menu
, menu
->selected_entry
+1);
911 if (menu
->selected_entry
>=0) {
913 entry
= menu
->entries
[menu
->selected_entry
];
915 if (entry
->cascade
>= 0 && menu
->cascades
916 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
918 XUngrabKeyboard(dpy
, CurrentTime
);
920 selectEntry(menu
->cascades
[entry
->cascade
], 0);
921 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
924 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
925 GrabModeAsync
, GrabModeAsync
,
936 if (menu
->parent
!=NULL
&& menu
->parent
->selected_entry
>=0) {
937 selectEntry(menu
, -1);
938 move_menus(menu
, old_pos_x
, old_pos_y
);
948 index
= check_key(menu
, &event
.xkey
);
950 selectEntry(menu
, index
);
956 if (event
.type
==ButtonPress
)
959 WMHandleEvent(&event
);
963 XUngrabKeyboard(dpy
, CurrentTime
);
965 if (done
==2 && menu
->selected_entry
>=0) {
966 entry
= menu
->entries
[menu
->selected_entry
];
971 if (entry
&& entry
->callback
!=NULL
&& entry
->flags
.enabled
972 && entry
->cascade
< 0) {
973 #if (MENU_BLINK_COUNT > 0)
974 int sel
= menu
->selected_entry
;
977 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
978 paintEntry(menu
, sel
, False
);
980 wusleep(MENU_BLINK_DELAY
);
981 paintEntry(menu
, sel
, True
);
983 wusleep(MENU_BLINK_DELAY
);
986 selectEntry(menu
, -1);
988 if (!menu
->flags
.buttoned
) {
990 move_menus(menu
, old_pos_x
, old_pos_y
);
994 (*entry
->callback
)(menu
, entry
);
996 if (!menu
->flags
.buttoned
) {
998 move_menus(menu
, old_pos_x
, old_pos_y
);
1000 selectEntry(menu
, -1);
1004 /* returns True if returning from a submenu to a parent menu,
1005 * False if exiting from menu */
1011 wMenuMapAt(WMenu
*menu
, int x
, int y
, int keyboard
)
1013 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
1014 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
1016 if (!menu
->flags
.realized
) {
1017 menu
->flags
.realized
=1;
1020 if (!menu
->flags
.mapped
) {
1021 if (wPreferences
.wrap_menus
) {
1024 if (x
+MENUW(menu
) > scr_width
)
1025 x
= scr_width
- MENUW(menu
);
1026 if (y
+MENUH(menu
) > scr_height
)
1027 y
= scr_height
- MENUH(menu
);
1030 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
1033 XMapWindow(dpy
, menu
->frame
->core
->window
);
1034 wRaiseFrame(menu
->frame
->core
);
1035 menu
->flags
.mapped
= 1;
1037 selectEntry(menu
, 0);
1046 wMenuMap(WMenu
*menu
)
1048 if (!menu
->flags
.realized
) {
1049 menu
->flags
.realized
=1;
1052 if (menu
->flags
.app_menu
&& menu
->parent
==NULL
) {
1053 menu
->frame_x
= menu
->frame
->screen_ptr
->app_menu_x
;
1054 menu
->frame_y
= menu
->frame
->screen_ptr
->app_menu_y
;
1055 XMoveWindow(dpy
, menu
->frame
->core
->window
, menu
->frame_x
, menu
->frame_y
);
1057 XMapWindow(dpy
, menu
->frame
->core
->window
);
1058 wRaiseFrame(menu
->frame
->core
);
1059 menu
->flags
.mapped
= 1;
1064 wMenuUnmap(WMenu
*menu
)
1068 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1069 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1070 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1072 menu
->flags
.buttoned
= 0;
1073 menu
->flags
.mapped
= 0;
1074 menu
->flags
.open_to_left
= 0;
1076 for (i
=0; i
<menu
->cascade_no
; i
++) {
1077 if (menu
->cascades
[i
]!=NULL
1078 && menu
->cascades
[i
]->flags
.mapped
1079 && !menu
->cascades
[i
]->flags
.buttoned
) {
1081 wMenuUnmap(menu
->cascades
[i
]);
1084 menu
->selected_entry
= -1;
1090 wMenuPaint(WMenu
*menu
)
1094 if (!menu
->flags
.mapped
) {
1099 for (i
=0; i
<menu
->entry_no
; i
++) {
1100 paintEntry(menu
, i
, i
==menu
->selected_entry
);
1106 wMenuSetEnabled(WMenu
*menu
, int index
, int enable
)
1108 if (index
>=menu
->entry_no
) return;
1109 menu
->entries
[index
]->flags
.enabled
=enable
;
1110 paintEntry(menu
, index
, index
==menu
->selected_entry
);
1111 paintEntry(menu
->brother
, index
, index
==menu
->selected_entry
);
1115 /* ====================================================================== */
1119 editEntry(WMenu
*menu
, WMenuEntry
*entry
)
1123 WObjDescriptor
*desc
;
1129 menu
->flags
.editing
= 1;
1131 text
= wTextCreate(menu
->menu
, 1, menu
->entry_height
* entry
->order
,
1132 menu
->menu
->width
- 2, menu
->entry_height
- 1);
1134 wTextPutText(text
, entry
->text
);
1135 XGetInputFocus(dpy
, &old_focus
, &old_revert
);
1136 XSetInputFocus(dpy
, text
->core
->window
, RevertToNone
, CurrentTime
);
1138 if (XGrabKeyboard(dpy
, text
->core
->window
, True
, GrabModeAsync
,
1139 GrabModeAsync
, CurrentTime
)!=GrabSuccess
) {
1140 wwarning("could not grab keyboard");
1143 wSetFocusTo(menu
->frame
->screen_ptr
,
1144 menu
->frame
->screen_ptr
->focused_window
);
1149 while (!done
&& !text
->done
) {
1151 XAllowEvents(dpy
, AsyncKeyboard
|AsyncPointer
, CurrentTime
);
1153 WMNextEvent(dpy
, &event
);
1155 if (XFindContext(dpy
, event
.xany
.window
, wWinContext
,
1156 (XPointer
*)&desc
)==XCNOENT
)
1159 if ((desc
!= NULL
) && (desc
->handle_anything
!= NULL
)) {
1161 (*desc
->handle_anything
)(desc
, &event
);
1164 switch (event
.type
) {
1166 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
1170 WMHandleEvent(&event
);
1176 XSetInputFocus(dpy
, old_focus
, old_revert
, CurrentTime
);
1178 wSetFocusTo(menu
->frame
->screen_ptr
,
1179 menu
->frame
->screen_ptr
->focused_window
);
1182 t
= wTextGetText(text
);
1183 /* if !t, the user has canceled editing */
1187 entry
->text
= wstrdup(t
);
1189 menu
->flags
.realized
= 0;
1193 XUngrabKeyboard(dpy
, CurrentTime
);
1195 if (t
&& menu
->on_edit
)
1196 (*menu
->on_edit
)(menu
, entry
);
1198 menu
->flags
.editing
= 0;
1200 if (!menu
->flags
.realized
)
1206 selectEntry(WMenu
*menu
, int entry_no
)
1212 if (menu
->entries
==NULL
)
1215 if (entry_no
>= menu
->entry_no
)
1218 old_entry
= menu
->selected_entry
;
1219 menu
->selected_entry
= entry_no
;
1221 if (old_entry
!=entry_no
) {
1223 /* unselect previous entry */
1225 paintEntry(menu
, old_entry
, False
);
1226 entry
= menu
->entries
[old_entry
];
1229 if (entry
->cascade
>=0 && menu
->cascades
) {
1230 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1231 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1237 menu
->selected_entry
= -1;
1240 entry
= menu
->entries
[entry_no
];
1242 if (entry
->cascade
>=0 && menu
->cascades
&& entry
->flags
.enabled
) {
1243 /* Callback for when the submenu is opened.
1245 submenu
= menu
->cascades
[entry
->cascade
];
1246 if (submenu
&& submenu
->flags
.brother
)
1247 submenu
= submenu
->brother
;
1249 if (entry
->callback
) {
1250 /* Only call the callback if the submenu is not yet mapped.
1252 if (menu
->flags
.brother
) {
1253 if (!submenu
|| !submenu
->flags
.mapped
)
1254 (*entry
->callback
)(menu
->brother
, entry
);
1256 if (!submenu
|| !submenu
->flags
.buttoned
)
1257 (*entry
->callback
)(menu
, entry
);
1261 /* the submenu menu might have changed */
1262 submenu
= menu
->cascades
[entry
->cascade
];
1265 if (!submenu
->flags
.mapped
) {
1268 if (!submenu
->flags
.realized
)
1269 wMenuRealize(submenu
);
1270 if (wPreferences
.wrap_menus
) {
1271 if (menu
->flags
.open_to_left
)
1272 submenu
->flags
.open_to_left
= 1;
1274 if (submenu
->flags
.open_to_left
) {
1275 x
= menu
->frame_x
- MENUW(submenu
);
1278 submenu
->flags
.open_to_left
= 0;
1281 x
= menu
->frame_x
+ MENUW(menu
);
1283 if (x
+ MENUW(submenu
)
1284 >= menu
->frame
->screen_ptr
->scr_width
) {
1286 x
= menu
->frame_x
- MENUW(submenu
);
1287 submenu
->flags
.open_to_left
= 1;
1291 x
= menu
->frame_x
+ MENUW(menu
);
1294 if (wPreferences
.align_menus
) {
1297 y
= menu
->frame_y
+ menu
->entry_height
*entry_no
;
1298 if (menu
->flags
.titled
)
1299 y
+= menu
->frame
->top_width
;
1300 if (menu
->cascades
[entry
->cascade
]->flags
.titled
)
1301 y
-= menu
->cascades
[entry
->cascade
]->frame
->top_width
;
1304 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1305 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1310 paintEntry(menu
, entry_no
, True
);
1316 findMenu(WScreen
*scr
, int *x_ret
, int *y_ret
)
1319 WObjDescriptor
*desc
;
1320 Window root_ret
, win
, junk_win
;
1324 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
,
1327 if (win
==None
) return NULL
;
1329 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1332 if (desc
->parent_type
== WCLASS_MENU
) {
1333 menu
= (WMenu
*)desc
->parent
;
1334 XTranslateCoordinates(dpy
, root_ret
, menu
->menu
->window
, wx
, wy
,
1335 x_ret
, y_ret
, &junk_win
);
1345 closeCascade(WMenu
*menu
)
1347 WMenu
*parent
=menu
->parent
;
1349 if (menu
->flags
.brother
1350 || (!menu
->flags
.buttoned
1351 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
))) {
1353 selectEntry(menu
, -1);
1355 #if (MENU_BLINK_DELAY > 2)
1356 wusleep(MENU_BLINK_DELAY
/2);
1360 && (parent
->parent
!=NULL
|| !parent
->flags
.app_menu
1361 || parent
->flags
.brother
)
1362 && !parent
->flags
.buttoned
) {
1363 selectEntry(parent
, -1);
1365 parent
= parent
->parent
;
1368 selectEntry(parent
, -1);
1374 closeBrotherCascadesOf(WMenu
*menu
)
1379 for (i
=0; i
<menu
->cascade_no
; i
++) {
1380 if (menu
->cascades
[i
]->flags
.brother
) {
1381 tmp
= menu
->cascades
[i
];
1383 tmp
= menu
->cascades
[i
]->brother
;
1385 if (tmp
->flags
.mapped
) {
1386 selectEntry(tmp
->parent
, -1);
1387 closeBrotherCascadesOf(tmp
);
1394 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1398 parentMenu(WMenu
*menu
)
1403 if (menu
->flags
.buttoned
)
1406 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1407 parent
= menu
->parent
;
1408 if (parent
->selected_entry
< 0)
1410 entry
= parent
->entries
[parent
->selected_entry
];
1411 if (!entry
->flags
.enabled
|| entry
->cascade
<0 || !parent
->cascades
||
1412 parent
->cascades
[entry
->cascade
] != menu
)
1415 if (menu
->flags
.buttoned
)
1425 * Will raise the passed menu, if submenu = 0
1426 * If submenu > 0 will also raise all mapped submenus
1427 * until the first buttoned one
1428 * If submenu < 0 will also raise all mapped parent menus
1429 * until the first buttoned one
1433 raiseMenus(WMenu
*menu
, int submenus
)
1440 wRaiseFrame(menu
->frame
->core
);
1442 if (submenus
>0 && menu
->selected_entry
>=0) {
1443 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
1444 if (i
>=0 && menu
->cascades
) {
1445 submenu
= menu
->cascades
[i
];
1446 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
1447 raiseMenus(submenu
, submenus
);
1450 if (submenus
<0 && !menu
->flags
.buttoned
&&
1451 menu
->parent
&& menu
->parent
->flags
.mapped
)
1452 raiseMenus(menu
->parent
, submenus
);
1457 wMenuUnderPointer(WScreen
*screen
)
1459 WObjDescriptor
*desc
;
1460 Window root_ret
, win
;
1464 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
,
1465 &dummy
, &dummy
, &mask
);
1467 if (win
==None
) return NULL
;
1469 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1472 if (desc
->parent_type
== WCLASS_MENU
)
1473 return (WMenu
*)desc
->parent
;
1479 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1483 getPointerPosition(WScreen
*scr
, int *x
, int *y
)
1485 Window root_ret
, win
;
1489 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1494 getScrollAmount(WMenu
*menu
, int *hamount
, int *vamount
)
1496 WScreen
*scr
= menu
->menu
->screen_ptr
;
1497 int menuX1
= menu
->frame_x
;
1498 int menuY1
= menu
->frame_y
;
1499 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1500 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1501 int screenW
= scr
->scr_width
;
1502 int screenH
= scr
->scr_height
;
1508 getPointerPosition(scr
, &xroot
, &yroot
);
1511 if (xroot
<= 1 && menuX1
< 0) {
1512 /* scroll to the right */
1513 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX1
));
1515 } else if (xroot
>= screenW
-2 && menuX2
> screenW
-1) {
1516 /* scroll to the left */
1517 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX2
-screenW
-1));
1522 *hamount
= -*hamount
;
1525 if (yroot
<= 1 && menuY1
< 0) {
1527 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY1
));
1529 } else if (yroot
>= screenH
-2 && menuY2
> screenH
-1) {
1531 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY2
-screenH
-2));
1533 *vamount
= -*vamount
;
1539 dragScrollMenuCallback(void *data
)
1541 WMenu
*menu
= (WMenu
*)data
;
1542 WScreen
*scr
= menu
->menu
->screen_ptr
;
1543 WMenu
*parent
= parentMenu(menu
);
1544 int hamount
, vamount
;
1546 int newSelectedEntry
;
1548 getScrollAmount(menu
, &hamount
, &vamount
);
1551 if (hamount
!= 0 || vamount
!= 0) {
1552 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1553 parent
->frame_y
+ vamount
, True
);
1554 if (findMenu(scr
, &x
, &y
)) {
1555 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1556 selectEntry(menu
, newSelectedEntry
);
1558 /* Pointer fell outside of menu. If the selected entry is
1559 * not a submenu, unselect it */
1560 if (menu
->selected_entry
>= 0
1561 && menu
->entries
[menu
->selected_entry
]->cascade
<0)
1562 selectEntry(menu
, -1);
1563 newSelectedEntry
= 0;
1566 /* paranoid check */
1567 if (newSelectedEntry
>= 0) {
1568 /* keep scrolling */
1569 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1570 dragScrollMenuCallback
, menu
);
1575 /* don't need to scroll anymore */
1577 if (findMenu(scr
, &x
, &y
)) {
1578 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1579 selectEntry(menu
, newSelectedEntry
);
1586 scrollMenuCallback(void *data
)
1588 WMenu
*menu
= (WMenu
*)data
;
1589 WMenu
*parent
= parentMenu(menu
);
1590 int hamount
= 0; /* amount to scroll */
1593 getScrollAmount(menu
, &hamount
, &vamount
);
1595 if (hamount
!= 0 || vamount
!= 0) {
1596 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1597 parent
->frame_y
+ vamount
, True
);
1599 /* keep scrolling */
1600 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1601 scrollMenuCallback
, menu
);
1603 /* don't need to scroll anymore */
1610 #define MENU_SCROLL_BORDER 5
1613 isPointNearBoder(WMenu
*menu
, int x
, int y
)
1615 int menuX1
= menu
->frame_x
;
1616 int menuY1
= menu
->frame_y
;
1617 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1618 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1619 int scrXe
= menu
->menu
->screen_ptr
->scr_width
-1;
1620 int scrYe
= menu
->menu
->screen_ptr
->scr_height
-1;
1623 if (x
>= menuX1
&& x
<= menuX2
&& (y
< MENU_SCROLL_BORDER
1624 || y
> scrYe
-MENU_SCROLL_BORDER
))
1626 else if (y
>= menuY1
&& y
<= menuY2
&& (x
< MENU_SCROLL_BORDER
1627 || x
> scrXe
-MENU_SCROLL_BORDER
))
1635 wMenuScroll(WMenu
*menu
, XEvent
*event
)
1638 WMenu
*omenu
= parentMenu(menu
);
1639 WScreen
*scr
= menu
->frame
->screen_ptr
;
1642 int old_frame_x
= omenu
->frame_x
;
1643 int old_frame_y
= omenu
->frame_y
;
1647 puts("Entering menu Scroll");
1650 if ((/*omenu->flags.buttoned &&*/ !wPreferences
.wrap_menus
)
1651 || omenu
->flags
.app_menu
) {
1655 if (!wPreferences
.wrap_menus
)
1656 raiseMenus(omenu
, True
);
1658 raiseMenus(menu
, False
);
1661 scrollMenuCallback(menu
);
1664 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1666 WMNextEvent(dpy
, &ev
);
1671 x
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1672 y
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1674 /* on_border is != 0 if the pointer is between the menu
1675 * and the screen border and is close enough to the border */
1676 on_border
= isPointNearBoder(menu
, x
, y
);
1678 smenu
= wMenuUnderPointer(scr
);
1680 if ((smenu
==NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
)!=omenu
)) {
1685 on_x_edge
= x
<= 1 || x
>= scr
->scr_width
- 2;
1686 on_y_edge
= y
<= 1 || y
>= scr
->scr_height
- 2;
1687 on_border
= on_x_edge
|| on_y_edge
;
1689 if (!on_border
&& !jump_back
) {
1694 if (menu
->timer
&& (smenu
!=menu
|| (!on_y_edge
&& !on_x_edge
))) {
1695 WMDeleteTimerHandler(menu
->timer
);
1703 scrollMenuCallback(menu
);
1706 /* True if we push on title, or drag the omenu to other position */
1707 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1708 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1709 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1710 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1712 smenu
= wMenuUnderPointer(scr
);
1713 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1715 else if (smenu
==omenu
&& on_title
) {
1729 WMDeleteTimerHandler(menu
->timer
);
1734 wMenuMove(omenu
, old_frame_x
, old_frame_y
, True
);
1737 puts("Leaving menu Scroll");
1744 menuExpose(WObjDescriptor
*desc
, XEvent
*event
)
1746 wMenuPaint(desc
->parent
);
1750 int *delayed_select
;
1757 delaySelection(void *data
)
1759 delay_data
*d
= (delay_data
*)data
;
1765 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1766 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1767 entry_no
= getEntryAt(menu
, x
, y
);
1768 selectEntry(menu
, entry_no
);
1770 if (d
->delayed_select
)
1771 *(d
->delayed_select
) = 0;
1776 menuMouseDown(WObjDescriptor
*desc
, XEvent
*event
)
1778 XButtonEvent
*bev
= &event
->xbutton
;
1779 WMenu
*menu
= desc
->parent
;
1781 WScreen
*scr
=menu
->frame
->screen_ptr
;
1782 WMenuEntry
*entry
=NULL
;
1784 int close_on_exit
=0;
1786 int delayed_select
= 0;
1790 int old_frame_x
= 0;
1791 int old_frame_y
= 0;
1792 delay_data d_data
= {NULL
, NULL
, NULL
};
1794 if (!wPreferences
.wrap_menus
) {
1795 smenu
= parentMenu(menu
);
1796 old_frame_x
= smenu
->frame_x
;
1797 old_frame_y
= smenu
->frame_y
;
1799 else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1800 /* This is true if the menu was launched with right click on root window */
1802 d_data
.delayed_select
= &delayed_select
;
1804 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
,
1805 delaySelection
, &d_data
);
1808 wRaiseFrame(menu
->frame
->core
);
1810 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1812 smenu
= findMenu(scr
, &x
, &y
);
1820 if (menu
->flags
.editing
) {
1823 entry_no
= getEntryAt(menu
, x
, y
);
1825 entry
= menu
->entries
[entry_no
];
1827 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
1828 && entry
->flags
.editable
) {
1829 editEntry(smenu
, entry
);
1831 } else if (bev
->state
& ControlMask
) {
1835 if (entry
->flags
.enabled
&& entry
->cascade
>=0 && menu
->cascades
) {
1836 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1838 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&&
1839 menu
->selected_entry
!=entry_no
) {
1840 wMenuUnmap(submenu
);
1842 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1843 selectEntry(menu
, entry_no
);
1844 } else if (!submenu
->flags
.buttoned
) {
1845 selectEntry(menu
, -1);
1848 } else if (!delayed_select
) {
1849 selectEntry(menu
, entry_no
);
1852 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1854 dragScrollMenuCallback(menu
);
1858 prevx
= bev
->x_root
;
1859 prevy
= bev
->y_root
;
1863 XAllowEvents(dpy
, SyncPointer
, CurrentTime
);
1865 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonReleaseMask
1866 |ButtonPressMask
, &ev
);
1869 smenu
= findMenu(scr
, &x
, &y
);
1871 if (smenu
== NULL
) {
1872 /* moved mouse out of menu */
1874 if (!delayed_select
&& d_data
.magic
) {
1875 WMDeleteTimerHandler(d_data
.magic
);
1876 d_data
.magic
= NULL
;
1879 || (menu
->selected_entry
>=0
1880 && menu
->entries
[menu
->selected_entry
]->cascade
>=0)) {
1881 prevx
= ev
.xmotion
.x_root
;
1882 prevy
= ev
.xmotion
.y_root
;
1886 selectEntry(menu
, -1);
1888 prevx
= ev
.xmotion
.x_root
;
1889 prevy
= ev
.xmotion
.y_root
;
1891 } else if (menu
&& menu
!=smenu
1892 && (menu
->selected_entry
<0
1893 || menu
->entries
[menu
->selected_entry
]->cascade
<0)) {
1894 selectEntry(menu
, -1);
1896 if (!delayed_select
&& d_data
.magic
) {
1897 WMDeleteTimerHandler(d_data
.magic
);
1898 d_data
.magic
= NULL
;
1902 /* hysteresis for item selection */
1904 /* check if the motion was to the side, indicating that
1905 * the user may want to cross to a submenu */
1906 if (!delayed_select
&& menu
) {
1908 Bool moved_to_submenu
;/* moved to direction of submenu */
1910 dx
= abs(prevx
- ev
.xmotion
.x_root
);
1912 moved_to_submenu
= False
;
1913 if (dx
> 0 /* if moved enough to the side */
1914 /* maybe a open submenu */
1915 && menu
->selected_entry
>=0
1916 /* moving to the right direction */
1917 && (wPreferences
.align_menus
1918 || ev
.xmotion
.y_root
>= prevy
)) {
1921 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
1923 if (menu
->cascades
[index
]->frame_x
>menu
->frame_x
) {
1924 if (prevx
< ev
.xmotion
.x_root
)
1925 moved_to_submenu
= True
;
1927 if (prevx
> ev
.xmotion
.x_root
)
1928 moved_to_submenu
= True
;
1934 if (menu
!= smenu
) {
1936 WMDeleteTimerHandler(d_data
.magic
);
1938 d_data
.magic
= NULL
;
1939 } else if (moved_to_submenu
) {
1940 /* while we are moving, postpone the selection */
1942 WMDeleteTimerHandler(d_data
.magic
);
1944 d_data
.delayed_select
= NULL
;
1946 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
1949 prevx
= ev
.xmotion
.x_root
;
1950 prevy
= ev
.xmotion
.y_root
;
1954 WMDeleteTimerHandler(d_data
.magic
);
1955 d_data
.magic
= NULL
;
1959 prevx
= ev
.xmotion
.x_root
;
1960 prevy
= ev
.xmotion
.y_root
;
1962 /* pointer crossed menus */
1963 if (menu
&& menu
->timer
) {
1964 WMDeleteTimerHandler(menu
->timer
);
1968 dragScrollMenuCallback(smenu
);
1972 dragScrollMenuCallback(menu
);
1974 if (!delayed_select
) {
1975 entry_no
= getEntryAt(menu
, x
, y
);
1977 entry
= menu
->entries
[entry_no
];
1978 if (entry
->flags
.enabled
&& entry
->cascade
>=0 &&
1980 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1981 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
1982 && menu
->selected_entry
!=entry_no
) {
1983 wMenuUnmap(submenu
);
1987 selectEntry(menu
, entry_no
);
1995 if (ev
.xbutton
.button
== event
->xbutton
.button
)
2005 if (menu
&& menu
->timer
) {
2006 WMDeleteTimerHandler(menu
->timer
);
2009 if (d_data
.magic
!=NULL
)
2010 WMDeleteTimerHandler(d_data
.magic
);
2012 if (menu
&& menu
->selected_entry
>=0) {
2013 entry
= menu
->entries
[menu
->selected_entry
];
2014 if (entry
->callback
!=NULL
&& entry
->flags
.enabled
2015 && entry
->cascade
< 0) {
2016 /* blink and erase menu selection */
2017 #if (MENU_BLINK_DELAY > 0)
2018 int sel
= menu
->selected_entry
;
2021 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
2022 paintEntry(menu
, sel
, False
);
2024 wusleep(MENU_BLINK_DELAY
);
2025 paintEntry(menu
, sel
, True
);
2027 wusleep(MENU_BLINK_DELAY
);
2030 /* unmap the menu, it's parents and call the callback */
2031 if (!menu
->flags
.buttoned
&&
2032 (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2035 selectEntry(menu
, -1);
2037 (*entry
->callback
)(menu
, entry
);
2039 /* If the user double clicks an entry, the entry will
2040 * be executed twice, which is not good for things like
2041 * the root menu. So, ignore any clicks that were generated
2042 * while the entry was being executed */
2043 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
,
2045 } else if (entry
->callback
!=NULL
&& entry
->cascade
<0) {
2046 selectEntry(menu
, -1);
2048 if (entry
->cascade
>=0 && menu
->cascades
2049 && menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2050 selectEntry(menu
, -1);
2055 if (((WMenu
*)desc
->parent
)->flags
.brother
|| close_on_exit
2057 closeCascade(desc
->parent
);
2059 /* close the cascade windows that should not remain opened */
2060 closeBrotherCascadesOf(desc
->parent
);
2062 if (!wPreferences
.wrap_menus
)
2063 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2068 wMenuMove(WMenu
*menu
, int x
, int y
, int submenus
)
2077 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2079 if (submenus
>0 && menu
->selected_entry
>=0) {
2080 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2082 if (i
>=0 && menu
->cascades
) {
2083 submenu
= menu
->cascades
[i
];
2084 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
2085 if (wPreferences
.align_menus
) {
2086 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2088 wMenuMove(submenu
, x
+ MENUW(menu
),
2089 y
+ submenu
->entry_height
*menu
->selected_entry
,
2094 if (submenus
<0 && menu
->parent
!=NULL
&& menu
->parent
->flags
.mapped
&&
2095 !menu
->parent
->flags
.buttoned
) {
2096 if (wPreferences
.align_menus
) {
2097 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2099 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2100 - menu
->parent
->entry_height
*menu
->parent
->selected_entry
,
2108 changeMenuLevels(WMenu
*menu
, int lower
)
2113 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
2115 wRaiseFrame(menu
->frame
->core
);
2116 menu
->flags
.lowered
= 0;
2118 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2119 wLowerFrame(menu
->frame
->core
);
2120 menu
->flags
.lowered
= 1;
2122 for (i
=0; i
<menu
->cascade_no
; i
++) {
2123 if (menu
->cascades
[i
]
2124 && !menu
->cascades
[i
]->flags
.buttoned
2125 && menu
->cascades
[i
]->flags
.lowered
!=lower
) {
2126 changeMenuLevels(menu
->cascades
[i
], lower
);
2134 menuTitleDoubleClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2139 if (event
->xbutton
.state
& MOD_MASK
) {
2140 if (menu
->flags
.lowered
) {
2145 changeMenuLevels(menu
, lower
);
2151 menuTitleMouseDown(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2156 int x
=menu
->frame_x
, y
=menu
->frame_y
;
2157 int dx
=event
->xbutton
.x_root
, dy
=event
->xbutton
.y_root
;
2162 printf("Moving menu\n");
2165 /* can't touch the menu copy */
2166 if (menu
->flags
.brother
)
2169 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2172 if (event
->xbutton
.state
& MOD_MASK
) {
2173 wLowerFrame(menu
->frame
->core
);
2176 wRaiseFrame(menu
->frame
->core
);
2181 /* lower/raise all submenus */
2183 if (tmp
->selected_entry
>=0 && tmp
->cascades
2184 && tmp
->entries
[tmp
->selected_entry
]->cascade
>=0) {
2185 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2186 if (!tmp
|| !tmp
->flags
.mapped
)
2189 wLowerFrame(tmp
->frame
->core
);
2191 wRaiseFrame(tmp
->frame
->core
);
2197 /* tear off the menu if it's a root menu or a cascade
2199 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
2200 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2201 menu
->flags
.buttoned
=1;
2202 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2204 /* turn off selected menu entry in parent menu */
2205 selectEntry(menu
->parent
, -1);
2207 /* make parent map the copy in place of the original */
2208 for (i
=0; i
<menu
->parent
->cascade_no
; i
++) {
2209 if (menu
->parent
->cascades
[i
] == menu
) {
2210 menu
->parent
->cascades
[i
] = menu
->brother
;
2219 WMMaskEvent(dpy
, ButtonMotionMask
|ButtonReleaseMask
|ButtonPressMask
2220 |ExposureMask
, &ev
);
2224 x
+= ev
.xmotion
.x_root
- dx
;
2225 y
+= ev
.xmotion
.y_root
- dy
;
2226 dx
= ev
.xmotion
.x_root
;
2227 dy
= ev
.xmotion
.y_root
;
2228 wMenuMove(menu
, x
, y
, True
);
2230 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2231 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2233 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2234 ButtonMotionMask
|ButtonReleaseMask
2236 GrabModeAsync
, GrabModeAsync
, None
,
2237 wCursor
[WCUR_MOVE
], CurrentTime
);
2246 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2249 printf("End menu move\n");
2251 XUngrabPointer(dpy
, CurrentTime
);
2262 *----------------------------------------------------------------------
2264 * Handles mouse click on the close button of menus. The menu is
2265 * closed when the button is clicked.
2268 * The closed menu is reinserted at it's parent menus
2270 *----------------------------------------------------------------------
2273 menuCloseClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2275 WMenu
*menu
= (WMenu
*)data
;
2276 WMenu
*parent
= menu
->parent
;
2280 for (i
=0; i
<parent
->cascade_no
; i
++) {
2281 /* find the entry that points to the copy */
2282 if (parent
->cascades
[i
] == menu
->brother
) {
2283 /* make it point to the original */
2284 parent
->cascades
[i
] = menu
;
2285 menu
->parent
= parent
;
2295 wMenuSaveState(WScreen
*scr
)
2297 proplist_t menus
, key
, value
;
2301 menus
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
2304 if (scr
->root_menu
&& scr
->root_menu
->flags
.buttoned
) {
2305 sprintf(buffer
, "%i,%i", scr
->root_menu
->frame_x
,
2306 scr
->root_menu
->frame_y
);
2307 key
= PLMakeString("RootMenu");
2308 value
= PLMakeString(buffer
);
2309 PLInsertDictionaryEntry(menus
, key
, value
);
2315 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2316 sprintf(buffer
, "%i,%i", scr
->switch_menu
->frame_x
,
2317 scr
->switch_menu
->frame_y
);
2318 key
= PLMakeString("SwitchMenu");
2319 value
= PLMakeString(buffer
);
2320 PLInsertDictionaryEntry(menus
, key
, value
);
2326 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2327 sprintf(buffer
, "%i,%i", scr
->workspace_menu
->frame_x
,
2328 scr
->workspace_menu
->frame_y
);
2329 key
= PLMakeString("WorkspaceMenu");
2330 value
= PLMakeString(buffer
);
2331 PLInsertDictionaryEntry(menus
, key
, value
);
2338 key
= PLMakeString("Menus");
2339 PLInsertDictionaryEntry(scr
->session_state
, key
, menus
);
2346 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2350 restoreMenu(WScreen
*scr
, proplist_t menu
, int which
)
2353 WMenu
*pmenu
= NULL
;
2358 if (!PLIsString(menu
)) {
2359 COMPLAIN("Position");
2363 if (sscanf(PLGetString(menu
), "%i,%i", &x
, &y
)!=2)
2364 COMPLAIN("Position");
2367 if (which
& WSS_ROOTMENU
) {
2368 OpenRootMenu(scr
, x
, y
, False
);
2369 pmenu
= scr
->root_menu
;
2370 } else if (which
& WSS_SWITCHMENU
) {
2371 OpenSwitchMenu(scr
, x
, y
, False
);
2372 pmenu
= scr
->switch_menu
;
2375 if (which
& WSS_WSMENU
) {
2376 OpenWorkspaceMenu(scr
, x
, y
);
2377 pmenu
= scr
->workspace_menu
;
2378 if (pmenu
->parent
) {
2379 /* make parent map the copy in place of the original */
2380 for (i
=0; i
<pmenu
->parent
->cascade_no
; i
++) {
2381 if (pmenu
->parent
->cascades
[i
] == pmenu
) {
2382 pmenu
->parent
->cascades
[i
] = pmenu
->brother
;
2390 int width
= MENUW(pmenu
);
2391 int height
= MENUH(pmenu
);
2393 x
= (x
< -width
) ? 0 : x
;
2394 x
= (x
> scr
->scr_width
) ? scr
->scr_width
- width
: x
;
2395 y
= (y
< 0) ? 0 : y
;
2396 y
= (y
> scr
->scr_height
) ? scr
->scr_height
- height
: y
;
2397 wMenuMove(pmenu
, x
, y
, True
);
2398 pmenu
->flags
.buttoned
= 1;
2399 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2407 wMenuRestoreState(WScreen
*scr
)
2409 proplist_t menus
, menu
, key
, rkey
, skey
, wkey
;
2411 key
= PLMakeString("Menus");
2412 menus
= PLGetDictionaryEntry(scr
->session_state
, key
);
2420 rkey
= PLMakeString("RootMenu");
2421 skey
= PLMakeString("SwitchMenu");
2422 wkey
= PLMakeString("WorkspaceMenu");
2423 menu
= PLGetDictionaryEntry(menus
, rkey
);
2424 restoreMenu(scr
, menu
, WSS_ROOTMENU
);
2425 menu
= PLGetDictionaryEntry(menus
, skey
);
2426 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2427 menu
= PLGetDictionaryEntry(menus
, wkey
);
2428 restoreMenu(scr
, menu
, WSS_WSMENU
);
2437 OpenWorkspaceMenu(WScreen
*scr
, int x
, int y
)
2439 WMenu
*menu
, *parent
;
2443 if (!scr
->root_menu
) {
2444 OpenRootMenu(scr
, scr
->scr_width
*2, 0, False
);
2445 wMenuUnmap(scr
->root_menu
);
2448 menu
= scr
->workspace_menu
;
2450 if (menu
->flags
.mapped
) {
2451 if (!menu
->flags
.buttoned
) {
2453 parent
= menu
->parent
;
2454 if (parent
&& parent
->selected_entry
>= 0) {
2455 entry
= parent
->entries
[parent
->selected_entry
];
2456 if (parent
->cascades
[entry
->cascade
] == menu
) {
2457 selectEntry(parent
, -1);
2458 wMenuMapAt(menu
, x
, y
, False
);
2462 wRaiseFrame(menu
->frame
->core
);
2463 wMenuMapCopyAt(menu
, x
, y
);
2467 wMenuMapAt(menu
, x
, y
, False
);