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
;
270 wMenuInsertCallback(WMenu
*menu
, int index
, char *text
,
271 void (*callback
)(WMenu
*menu
, WMenuEntry
*entry
),
278 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
283 assert(menu
->flags
.brother
==0);
284 menu
->flags
.realized
= 0;
285 menu
->brother
->flags
.realized
= 0;
287 /* reallocate array if it's too small */
288 if (menu
->entry_no
>= menu
->alloced_entries
) {
291 puts("doing wrealloc()");
293 tmp
= wrealloc(menu
->entries
,
294 sizeof(WMenuEntry
)*(menu
->alloced_entries
+5));
296 wwarning(_("wrealloc() failed while trying to add menu item"));
301 menu
->alloced_entries
+= 5;
303 menu
->brother
->entries
= tmp
;
304 menu
->brother
->alloced_entries
= menu
->alloced_entries
;
306 entry
= wmalloc(sizeof(WMenuEntry
));
307 memset(entry
, 0, sizeof(WMenuEntry
));
308 entry
->flags
.enabled
= 1;
309 entry
->text
= wstrdup(text
);
311 entry
->clientdata
= clientdata
;
312 entry
->callback
= callback
;
313 if (index
<0 || index
>=menu
->entry_no
) {
314 entry
->order
= menu
->entry_no
;
315 menu
->entries
[menu
->entry_no
] = entry
;
317 entry
->order
= index
;
318 insertEntry(menu
, entry
, index
);
322 menu
->brother
->entry_no
= menu
->entry_no
;
330 wMenuEntrySetCascade(WMenu
*menu
, WMenuEntry
*entry
, WMenu
*cascade
)
332 WMenu
*brother
= menu
->brother
;
335 assert(menu
->flags
.brother
==0);
337 if (entry
->cascade
>=0) {
338 menu
->flags
.realized
= 0;
339 brother
->flags
.realized
= 0;
342 cascade
->parent
= menu
;
344 cascade
->brother
->parent
= brother
;
347 for (i
=0; i
<menu
->cascade_no
; i
++) {
348 if (menu
->cascades
[i
]==NULL
) {
349 menu
->cascades
[i
] = cascade
;
350 brother
->cascades
[i
] = cascade
->brother
;
357 entry
->cascade
= menu
->cascade_no
;
359 menu
->cascades
= wrealloc(menu
->cascades
,
360 sizeof(WMenu
)*(menu
->cascade_no
+1));
361 menu
->cascades
[menu
->cascade_no
++] = cascade
;
364 brother
->cascades
= wrealloc(brother
->cascades
,
365 sizeof(WMenu
)*(brother
->cascade_no
+1));
366 brother
->cascades
[brother
->cascade_no
++] = cascade
->brother
;
370 if (menu
->flags
.lowered
) {
372 cascade
->flags
.lowered
= 1;
373 ChangeStackingLevel(cascade
->frame
->core
, WMNormalLevel
);
375 cascade
->brother
->flags
.lowered
= 1;
376 ChangeStackingLevel(cascade
->brother
->frame
->core
, WMNormalLevel
);
379 if (!menu
->flags
.realized
)
385 wMenuEntryRemoveCascade(WMenu
*menu
, WMenuEntry
*entry
)
387 assert(menu
->flags
.brother
==0);
389 /* destroy cascade menu */
390 if (entry
->cascade
>=0 && menu
->cascades
391 && menu
->cascades
[entry
->cascade
]!=NULL
) {
393 wMenuDestroy(menu
->cascades
[entry
->cascade
], True
);
395 menu
->cascades
[entry
->cascade
] = NULL
;
396 menu
->brother
->cascades
[entry
->cascade
] = NULL
;
404 wMenuRemoveItem(WMenu
*menu
, int index
)
408 if (menu
->flags
.brother
) {
409 wMenuRemoveItem(menu
->brother
, index
);
413 if (index
>=menu
->entry_no
) return;
415 /* destroy cascade menu */
416 wMenuEntryRemoveCascade(menu
, menu
->entries
[index
]);
418 /* destroy unshared data */
420 if (menu
->entries
[index
]->text
)
421 free(menu
->entries
[index
]->text
);
423 if (menu
->entries
[index
]->rtext
)
424 free(menu
->entries
[index
]->rtext
);
426 if (menu
->entries
[index
]->free_cdata
&& menu
->entries
[index
]->clientdata
)
427 (*menu
->entries
[index
]->free_cdata
)(menu
->entries
[index
]->clientdata
);
429 free(menu
->entries
[index
]);
431 for (i
=index
; i
<menu
->entry_no
-1; i
++) {
432 menu
->entries
[i
+1]->order
--;
433 menu
->entries
[i
]=menu
->entries
[i
+1];
436 menu
->brother
->entry_no
--;
441 updateTexture(WMenu
*menu
)
443 WScreen
*scr
= menu
->menu
->screen_ptr
;
445 /* setup background texture */
446 if (scr
->menu_item_texture
->any
.type
!= WTEX_SOLID
) {
447 if (!menu
->flags
.brother
) {
448 FREE_PIXMAP(menu
->menu_texture_data
);
450 if (wPreferences
.alt_menu_style
) {
451 wTextureRender(scr
, scr
->menu_item_texture
,
452 &menu
->menu_texture_data
, menu
->menu
->width
,
453 menu
->menu
->height
, WREL_MENUENTRY
);
455 wTextureRender(scr
, scr
->menu_item_texture
,
456 &menu
->menu_texture_data
, menu
->menu
->width
,
457 menu
->entry_height
, WREL_MENUENTRY
);
460 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
,
461 menu
->menu_texture_data
);
462 XClearWindow(dpy
, menu
->menu
->window
);
464 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
,
465 menu
->menu_texture_data
);
466 XClearWindow(dpy
, menu
->brother
->menu
->window
);
469 XSetWindowBackground(dpy
, menu
->menu
->window
,
470 scr
->menu_item_texture
->any
.color
.pixel
);
471 XClearWindow(dpy
, menu
->menu
->window
);
477 wMenuRealize(WMenu
*menu
)
480 int width
, rwidth
, mrwidth
, mwidth
;
481 int theight
, twidth
, eheight
;
482 WScreen
*scr
= menu
->frame
->screen_ptr
;
483 static int brother_done
=0;
488 wMenuRealize(menu
->brother
);
492 flags
= WFF_SINGLE_STATE
;
493 if (menu
->flags
.titled
)
494 flags
|= WFF_TITLEBAR
|WFF_RIGHT_BUTTON
;
496 wFrameWindowUpdateBorders(menu
->frame
, flags
);
498 if (menu
->flags
.titled
) {
499 twidth
= wTextWidth(scr
->menu_title_font
->font
, menu
->frame
->title
,
500 strlen(menu
->frame
->title
));
501 theight
= menu
->frame
->top_width
;
502 twidth
+= theight
+ (wPreferences
.new_style
? 16 : 8);
507 eheight
= scr
->menu_entry_font
->height
+ 6;
508 menu
->entry_height
= eheight
;
511 for (i
=0; i
<menu
->entry_no
; i
++) {
514 /* search widest text */
515 text
= menu
->entries
[i
]->text
;
516 width
= wTextWidth(scr
->menu_entry_font
->font
, text
, strlen(text
))+10;
518 if (menu
->entries
[i
]->flags
.indicator
) {
519 width
+= MENU_INDICATOR_SPACE
;
525 /* search widest text on right */
526 text
= menu
->entries
[i
]->rtext
;
528 rwidth
= wTextWidth(scr
->menu_entry_font
->font
, text
,
530 else if (menu
->entries
[i
]->cascade
>=0)
535 if (rwidth
> mrwidth
)
544 wCoreConfigure(menu
->menu
, 0, theight
, mwidth
, menu
->entry_no
*eheight
-1);
546 wFrameWindowResize(menu
->frame
, mwidth
, menu
->entry_no
*eheight
-1
547 + menu
->frame
->top_width
+ menu
->frame
->bottom_width
);
552 menu
->flags
.realized
= 1;
554 if (menu
->flags
.mapped
)
556 if (menu
->brother
->flags
.mapped
)
557 wMenuPaint(menu
->brother
);
562 wMenuDestroy(WMenu
*menu
, int recurse
)
566 WMRemoveNotificationObserver(menu
);
568 /* remove any pending timers */
570 WMDeleteTimerHandler(menu
->timer
);
573 /* call destroy handler */
574 if (menu
->on_destroy
)
575 (*menu
->on_destroy
)(menu
);
577 /* Destroy items if this menu own them. If this is the "brother" menu,
578 * leave them alone as it is shared by them.
580 if (!menu
->flags
.brother
) {
581 for (i
=0; i
<menu
->entry_no
; i
++) {
583 free(menu
->entries
[i
]->text
);
585 if (menu
->entries
[i
]->rtext
)
586 free(menu
->entries
[i
]->rtext
);
589 if (menu
->entries
[i
]->instances
){
590 PLRelease(menu
->entries
[i
]->instances
);
592 #endif /* USER_MENU */
594 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
595 (*menu
->entries
[i
]->free_cdata
)(menu
->entries
[i
]->clientdata
);
597 free(menu
->entries
[i
]);
601 for (i
=0; i
<menu
->cascade_no
; i
++) {
602 if (menu
->cascades
[i
]) {
603 if (menu
->cascades
[i
]->flags
.brother
)
604 wMenuDestroy(menu
->cascades
[i
]->brother
, recurse
);
606 wMenuDestroy(menu
->cascades
[i
], recurse
);
616 FREE_PIXMAP(menu
->menu_texture_data
);
619 free(menu
->cascades
);
621 wCoreDestroy(menu
->menu
);
622 wFrameWindowDestroy(menu
->frame
);
624 /* destroy copy of this menu */
625 if (!menu
->flags
.brother
&& menu
->brother
)
626 wMenuDestroy(menu
->brother
, False
);
633 drawFrame(WScreen
*scr
, Window win
, int y
, int w
, int h
)
644 segs
[1].y2
= y
+ h
- 2;
645 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, 2);
647 XDrawLine(dpy
, win
, scr
->menu_item_auxtexture
->dark_gc
, 0, y
+h
-1,
651 segs
[0].y1
= segs
[0].y2
= y
+ h
-2;
653 segs
[1].x1
= segs
[1].x2
= w
-1;
655 segs
[1].y2
= y
+ h
-2;
656 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, 2);
661 paintEntry(WMenu
*menu
, int index
, int selected
)
664 GC light
, dim
, dark
, textGC
;
665 WScreen
*scr
=menu
->frame
->screen_ptr
;
666 Window win
= menu
->menu
->window
;
667 WMenuEntry
*entry
=menu
->entries
[index
];
669 if (!menu
->flags
.realized
) return;
670 h
= menu
->entry_height
;
671 w
= menu
->menu
->width
;
674 light
= scr
->menu_item_auxtexture
->light_gc
;
675 dim
= scr
->menu_item_auxtexture
->dim_gc
;
676 dark
= scr
->menu_item_auxtexture
->dark_gc
;
678 /* paint background */
680 XSetForeground(dpy
, scr
->select_menu_gc
, scr
->select_pixel
);
681 XFillRectangle(dpy
, win
, scr
->select_menu_gc
, 1, y
+1, w
-2, h
-3);
682 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
)
683 drawFrame(scr
, win
, y
, w
, h
);
685 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
686 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
688 drawFrame(scr
, win
, y
, w
, h
);
690 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
695 textGC
= scr
->select_menu_gc
;
696 if (entry
->flags
.enabled
)
697 XSetForeground(dpy
, textGC
, scr
->select_text_pixel
);
699 XSetForeground(dpy
, textGC
, scr
->dtext_pixel
);
700 } else if (!entry
->flags
.enabled
) {
701 textGC
= scr
->disabled_menu_entry_gc
;
703 textGC
= scr
->menu_entry_gc
;
707 if (entry
->flags
.indicator
)
708 x
+= MENU_INDICATOR_SPACE
+ 2;
710 wDrawString(win
, scr
->menu_entry_font
,
711 textGC
, x
, 3+y
+scr
->menu_entry_font
->y
, entry
->text
,
712 strlen(entry
->text
));
714 if (entry
->cascade
>=0) {
715 /* draw the cascade indicator */
716 XDrawLine(dpy
,win
,dim
, w
-11, y
+6, w
-6, y
+h
/2-1);
717 XDrawLine(dpy
,win
,light
, w
-11, y
+h
-8, w
-6, y
+h
/2-1);
718 XDrawLine(dpy
,win
,dark
, w
-12, y
+6, w
-12, y
+h
-8);
722 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
727 switch (entry
->flags
.indicator_type
) {
729 indicator
= scr
->menu_check_indicator
;
732 indicator
= scr
->menu_mini_indicator
;
735 indicator
= scr
->menu_hide_indicator
;
738 indicator
= scr
->menu_shade_indicator
;
742 indicator
= scr
->menu_radio_indicator
;
746 iw
= indicator
->width
;
747 ih
= indicator
->height
;
748 XSetClipMask(dpy
, scr
->copy_gc
, indicator
->mask
);
749 XSetClipOrigin(dpy
, scr
->copy_gc
, 5, y
+(h
-ih
)/2);
751 XSetForeground(dpy
, scr
->copy_gc
, scr
->black_pixel
);
753 XSetForeground(dpy
, scr
->copy_gc
, scr
->mtext_pixel
);
754 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+(h
-ih
)/2, iw
, ih
);
756 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
757 iw, ih, 5, y+(h-ih)/2);
759 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
762 /* draw right text */
764 if (entry
->rtext
&& entry
->cascade
<0) {
765 tw
= wTextWidth(scr
->menu_entry_font
->font
, entry
->rtext
,
766 strlen(entry
->rtext
));
768 wDrawString(win
, scr
->menu_entry_font
, textGC
, w
-6-tw
,
769 3+y
+scr
->menu_entry_font
->y
, entry
->rtext
,
770 strlen(entry
->rtext
));
776 move_menus(WMenu
*menu
, int x
, int y
)
778 while (menu
->parent
) {
781 if (!wPreferences
.align_menus
&& menu
->selected_entry
>=0) {
782 y
-= menu
->selected_entry
*menu
->entry_height
;
785 wMenuMove(menu
, x
, y
, True
);
789 makeVisible(WMenu
*menu
)
791 WScreen
*scr
= menu
->frame
->screen_ptr
;
792 int x1
, y1
, x2
, y2
, new_x
, new_y
, move
;
794 if (menu
->entry_no
<0) return;
797 y1
= menu
->frame_y
+menu
->frame
->top_width
798 + menu
->selected_entry
*menu
->entry_height
;
799 x2
= x1
+ MENUW(menu
);
800 y2
= y1
+ menu
->entry_height
;
809 } else if (x2
>= scr
->scr_width
) {
810 new_x
= scr
->scr_width
- MENUW(menu
) - 1;
817 } else if (y2
>= scr
->scr_height
) {
818 new_y
= scr
->scr_height
- menu
->entry_height
- 1;
822 new_y
= new_y
- menu
->frame
->top_width
823 - menu
->selected_entry
*menu
->entry_height
;
824 move_menus(menu
, new_x
, new_y
);
829 check_key(WMenu
*menu
, XKeyEvent
*event
)
834 if (XLookupString(event
, buffer
, 32, NULL
, NULL
)<1)
837 ch
= toupper(buffer
[0]);
839 s
= (menu
->selected_entry
>=0 ? menu
->selected_entry
+1 : 0);
842 for (i
=s
; i
<menu
->entry_no
; i
++) {
843 if (ch
==toupper(menu
->entries
[i
]->text
[0])) {
847 /* no match. Retry from start, if previous started from a selected entry */
857 keyboardMenu(WMenu
*menu
)
860 KeySym ksym
=NoSymbol
;
864 int old_pos_x
= menu
->frame_x
;
865 int old_pos_y
= menu
->frame_y
;
866 int new_x
= old_pos_x
, new_y
= old_pos_y
;
867 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
868 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
870 if (menu
->flags
.editing
)
874 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
,
875 GrabModeAsync
, CurrentTime
);
877 if (menu
->frame_y
+menu
->frame
->top_width
>= scr_height
)
878 new_y
= scr_height
- menu
->frame
->top_width
;
880 if (menu
->frame_x
+MENUW(menu
) >= scr_width
)
881 new_x
= scr_width
-MENUW(menu
)-1;
883 move_menus(menu
, new_x
, new_y
);
885 while (!done
&& menu
->flags
.mapped
) {
886 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
887 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonPressMask
888 |ButtonReleaseMask
|KeyPressMask
|KeyReleaseMask
889 |SubstructureNotifyMask
, &event
);
891 switch (event
.type
) {
893 ksym
= XLookupKeysym(&event
.xkey
, 0);
901 selectEntry(menu
, 0);
907 selectEntry(menu
, menu
->entry_no
-1);
916 if (menu
->selected_entry
<= 0)
917 selectEntry(menu
, menu
->entry_no
-1);
919 selectEntry(menu
, menu
->selected_entry
-1);
928 if (menu
->selected_entry
<0)
929 selectEntry(menu
, 0);
930 else if (menu
->selected_entry
== menu
->entry_no
-1)
931 selectEntry(menu
, 0);
932 else if (menu
->selected_entry
< menu
->entry_no
-1)
933 selectEntry(menu
, menu
->selected_entry
+1);
942 if (menu
->selected_entry
>=0) {
944 entry
= menu
->entries
[menu
->selected_entry
];
946 if (entry
->cascade
>= 0 && menu
->cascades
947 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
949 XUngrabKeyboard(dpy
, CurrentTime
);
951 selectEntry(menu
->cascades
[entry
->cascade
], 0);
952 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
955 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
956 GrabModeAsync
, GrabModeAsync
,
967 if (menu
->parent
!=NULL
&& menu
->parent
->selected_entry
>=0) {
968 selectEntry(menu
, -1);
969 move_menus(menu
, old_pos_x
, old_pos_y
);
979 index
= check_key(menu
, &event
.xkey
);
981 selectEntry(menu
, index
);
987 if (event
.type
==ButtonPress
)
990 WMHandleEvent(&event
);
994 XUngrabKeyboard(dpy
, CurrentTime
);
996 if (done
==2 && menu
->selected_entry
>=0) {
997 entry
= menu
->entries
[menu
->selected_entry
];
1002 if (entry
&& entry
->callback
!=NULL
&& entry
->flags
.enabled
1003 && entry
->cascade
< 0) {
1004 #if (MENU_BLINK_COUNT > 0)
1005 int sel
= menu
->selected_entry
;
1008 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
1009 paintEntry(menu
, sel
, False
);
1011 wusleep(MENU_BLINK_DELAY
);
1012 paintEntry(menu
, sel
, True
);
1014 wusleep(MENU_BLINK_DELAY
);
1017 selectEntry(menu
, -1);
1019 if (!menu
->flags
.buttoned
) {
1021 move_menus(menu
, old_pos_x
, old_pos_y
);
1025 (*entry
->callback
)(menu
, entry
);
1027 if (!menu
->flags
.buttoned
) {
1029 move_menus(menu
, old_pos_x
, old_pos_y
);
1031 selectEntry(menu
, -1);
1035 /* returns True if returning from a submenu to a parent menu,
1036 * False if exiting from menu */
1042 wMenuMapAt(WMenu
*menu
, int x
, int y
, int keyboard
)
1044 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
1045 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
1047 if (!menu
->flags
.realized
) {
1048 menu
->flags
.realized
=1;
1051 if (!menu
->flags
.mapped
) {
1052 if (wPreferences
.wrap_menus
) {
1055 if (x
+MENUW(menu
) > scr_width
)
1056 x
= scr_width
- MENUW(menu
);
1057 if (y
+MENUH(menu
) > scr_height
)
1058 y
= scr_height
- MENUH(menu
);
1061 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
1064 XMapWindow(dpy
, menu
->frame
->core
->window
);
1065 wRaiseFrame(menu
->frame
->core
);
1066 menu
->flags
.mapped
= 1;
1068 selectEntry(menu
, 0);
1077 wMenuMap(WMenu
*menu
)
1079 if (!menu
->flags
.realized
) {
1080 menu
->flags
.realized
=1;
1083 if (menu
->flags
.app_menu
&& menu
->parent
==NULL
) {
1084 menu
->frame_x
= menu
->frame
->screen_ptr
->app_menu_x
;
1085 menu
->frame_y
= menu
->frame
->screen_ptr
->app_menu_y
;
1086 XMoveWindow(dpy
, menu
->frame
->core
->window
, menu
->frame_x
, menu
->frame_y
);
1088 XMapWindow(dpy
, menu
->frame
->core
->window
);
1089 wRaiseFrame(menu
->frame
->core
);
1090 menu
->flags
.mapped
= 1;
1095 wMenuUnmap(WMenu
*menu
)
1099 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1100 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1101 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1103 menu
->flags
.buttoned
= 0;
1104 menu
->flags
.mapped
= 0;
1105 menu
->flags
.open_to_left
= 0;
1107 for (i
=0; i
<menu
->cascade_no
; i
++) {
1108 if (menu
->cascades
[i
]!=NULL
1109 && menu
->cascades
[i
]->flags
.mapped
1110 && !menu
->cascades
[i
]->flags
.buttoned
) {
1112 wMenuUnmap(menu
->cascades
[i
]);
1115 menu
->selected_entry
= -1;
1121 wMenuPaint(WMenu
*menu
)
1125 if (!menu
->flags
.mapped
) {
1130 for (i
=0; i
<menu
->entry_no
; i
++) {
1131 paintEntry(menu
, i
, i
==menu
->selected_entry
);
1137 wMenuSetEnabled(WMenu
*menu
, int index
, int enable
)
1139 if (index
>=menu
->entry_no
) return;
1140 menu
->entries
[index
]->flags
.enabled
=enable
;
1141 paintEntry(menu
, index
, index
==menu
->selected_entry
);
1142 paintEntry(menu
->brother
, index
, index
==menu
->selected_entry
);
1146 /* ====================================================================== */
1150 editEntry(WMenu
*menu
, WMenuEntry
*entry
)
1154 WObjDescriptor
*desc
;
1160 menu
->flags
.editing
= 1;
1162 text
= wTextCreate(menu
->menu
, 1, menu
->entry_height
* entry
->order
,
1163 menu
->menu
->width
- 2, menu
->entry_height
- 1);
1165 wTextPutText(text
, entry
->text
);
1166 XGetInputFocus(dpy
, &old_focus
, &old_revert
);
1167 XSetInputFocus(dpy
, text
->core
->window
, RevertToNone
, CurrentTime
);
1169 if (XGrabKeyboard(dpy
, text
->core
->window
, True
, GrabModeAsync
,
1170 GrabModeAsync
, CurrentTime
)!=GrabSuccess
) {
1171 wwarning("could not grab keyboard");
1174 wSetFocusTo(menu
->frame
->screen_ptr
,
1175 menu
->frame
->screen_ptr
->focused_window
);
1180 while (!done
&& !text
->done
) {
1182 XAllowEvents(dpy
, AsyncKeyboard
|AsyncPointer
, CurrentTime
);
1184 WMNextEvent(dpy
, &event
);
1186 if (XFindContext(dpy
, event
.xany
.window
, wWinContext
,
1187 (XPointer
*)&desc
)==XCNOENT
)
1190 if ((desc
!= NULL
) && (desc
->handle_anything
!= NULL
)) {
1192 (*desc
->handle_anything
)(desc
, &event
);
1195 switch (event
.type
) {
1197 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
1201 WMHandleEvent(&event
);
1207 XSetInputFocus(dpy
, old_focus
, old_revert
, CurrentTime
);
1209 wSetFocusTo(menu
->frame
->screen_ptr
,
1210 menu
->frame
->screen_ptr
->focused_window
);
1213 t
= wTextGetText(text
);
1214 /* if !t, the user has canceled editing */
1218 entry
->text
= wstrdup(t
);
1220 menu
->flags
.realized
= 0;
1224 XUngrabKeyboard(dpy
, CurrentTime
);
1226 if (t
&& menu
->on_edit
)
1227 (*menu
->on_edit
)(menu
, entry
);
1229 menu
->flags
.editing
= 0;
1231 if (!menu
->flags
.realized
)
1237 selectEntry(WMenu
*menu
, int entry_no
)
1243 if (menu
->entries
==NULL
)
1246 if (entry_no
>= menu
->entry_no
)
1249 old_entry
= menu
->selected_entry
;
1250 menu
->selected_entry
= entry_no
;
1252 if (old_entry
!=entry_no
) {
1254 /* unselect previous entry */
1256 paintEntry(menu
, old_entry
, False
);
1257 entry
= menu
->entries
[old_entry
];
1260 if (entry
->cascade
>=0 && menu
->cascades
) {
1261 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1262 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1268 menu
->selected_entry
= -1;
1271 entry
= menu
->entries
[entry_no
];
1273 if (entry
->cascade
>=0 && menu
->cascades
&& entry
->flags
.enabled
) {
1274 /* Callback for when the submenu is opened.
1276 submenu
= menu
->cascades
[entry
->cascade
];
1277 if (submenu
&& submenu
->flags
.brother
)
1278 submenu
= submenu
->brother
;
1280 if (entry
->callback
) {
1281 /* Only call the callback if the submenu is not yet mapped.
1283 if (menu
->flags
.brother
) {
1284 if (!submenu
|| !submenu
->flags
.mapped
)
1285 (*entry
->callback
)(menu
->brother
, entry
);
1287 if (!submenu
|| !submenu
->flags
.buttoned
)
1288 (*entry
->callback
)(menu
, entry
);
1292 /* the submenu menu might have changed */
1293 submenu
= menu
->cascades
[entry
->cascade
];
1296 if (!submenu
->flags
.mapped
) {
1299 if (!submenu
->flags
.realized
)
1300 wMenuRealize(submenu
);
1301 if (wPreferences
.wrap_menus
) {
1302 if (menu
->flags
.open_to_left
)
1303 submenu
->flags
.open_to_left
= 1;
1305 if (submenu
->flags
.open_to_left
) {
1306 x
= menu
->frame_x
- MENUW(submenu
);
1309 submenu
->flags
.open_to_left
= 0;
1312 x
= menu
->frame_x
+ MENUW(menu
);
1314 if (x
+ MENUW(submenu
)
1315 >= menu
->frame
->screen_ptr
->scr_width
) {
1317 x
= menu
->frame_x
- MENUW(submenu
);
1318 submenu
->flags
.open_to_left
= 1;
1322 x
= menu
->frame_x
+ MENUW(menu
);
1325 if (wPreferences
.align_menus
) {
1328 y
= menu
->frame_y
+ menu
->entry_height
*entry_no
;
1329 if (menu
->flags
.titled
)
1330 y
+= menu
->frame
->top_width
;
1331 if (menu
->cascades
[entry
->cascade
]->flags
.titled
)
1332 y
-= menu
->cascades
[entry
->cascade
]->frame
->top_width
;
1335 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1336 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1341 paintEntry(menu
, entry_no
, True
);
1347 findMenu(WScreen
*scr
, int *x_ret
, int *y_ret
)
1350 WObjDescriptor
*desc
;
1351 Window root_ret
, win
, junk_win
;
1355 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
,
1358 if (win
==None
) return NULL
;
1360 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1363 if (desc
->parent_type
== WCLASS_MENU
) {
1364 menu
= (WMenu
*)desc
->parent
;
1365 XTranslateCoordinates(dpy
, root_ret
, menu
->menu
->window
, wx
, wy
,
1366 x_ret
, y_ret
, &junk_win
);
1376 closeCascade(WMenu
*menu
)
1378 WMenu
*parent
=menu
->parent
;
1380 if (menu
->flags
.brother
1381 || (!menu
->flags
.buttoned
1382 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
))) {
1384 selectEntry(menu
, -1);
1386 #if (MENU_BLINK_DELAY > 2)
1387 wusleep(MENU_BLINK_DELAY
/2);
1391 && (parent
->parent
!=NULL
|| !parent
->flags
.app_menu
1392 || parent
->flags
.brother
)
1393 && !parent
->flags
.buttoned
) {
1394 selectEntry(parent
, -1);
1396 parent
= parent
->parent
;
1399 selectEntry(parent
, -1);
1405 closeBrotherCascadesOf(WMenu
*menu
)
1410 for (i
=0; i
<menu
->cascade_no
; i
++) {
1411 if (menu
->cascades
[i
]->flags
.brother
) {
1412 tmp
= menu
->cascades
[i
];
1414 tmp
= menu
->cascades
[i
]->brother
;
1416 if (tmp
->flags
.mapped
) {
1417 selectEntry(tmp
->parent
, -1);
1418 closeBrotherCascadesOf(tmp
);
1425 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1429 parentMenu(WMenu
*menu
)
1434 if (menu
->flags
.buttoned
)
1437 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1438 parent
= menu
->parent
;
1439 if (parent
->selected_entry
< 0)
1441 entry
= parent
->entries
[parent
->selected_entry
];
1442 if (!entry
->flags
.enabled
|| entry
->cascade
<0 || !parent
->cascades
||
1443 parent
->cascades
[entry
->cascade
] != menu
)
1446 if (menu
->flags
.buttoned
)
1456 * Will raise the passed menu, if submenu = 0
1457 * If submenu > 0 will also raise all mapped submenus
1458 * until the first buttoned one
1459 * If submenu < 0 will also raise all mapped parent menus
1460 * until the first buttoned one
1464 raiseMenus(WMenu
*menu
, int submenus
)
1471 wRaiseFrame(menu
->frame
->core
);
1473 if (submenus
>0 && menu
->selected_entry
>=0) {
1474 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
1475 if (i
>=0 && menu
->cascades
) {
1476 submenu
= menu
->cascades
[i
];
1477 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
1478 raiseMenus(submenu
, submenus
);
1481 if (submenus
<0 && !menu
->flags
.buttoned
&&
1482 menu
->parent
&& menu
->parent
->flags
.mapped
)
1483 raiseMenus(menu
->parent
, submenus
);
1488 wMenuUnderPointer(WScreen
*screen
)
1490 WObjDescriptor
*desc
;
1491 Window root_ret
, win
;
1495 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
,
1496 &dummy
, &dummy
, &mask
);
1498 if (win
==None
) return NULL
;
1500 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1503 if (desc
->parent_type
== WCLASS_MENU
)
1504 return (WMenu
*)desc
->parent
;
1510 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1514 getPointerPosition(WScreen
*scr
, int *x
, int *y
)
1516 Window root_ret
, win
;
1520 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1525 getScrollAmount(WMenu
*menu
, int *hamount
, int *vamount
)
1527 WScreen
*scr
= menu
->menu
->screen_ptr
;
1528 int menuX1
= menu
->frame_x
;
1529 int menuY1
= menu
->frame_y
;
1530 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1531 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1532 int screenW
= scr
->scr_width
;
1533 int screenH
= scr
->scr_height
;
1539 getPointerPosition(scr
, &xroot
, &yroot
);
1542 if (xroot
<= 1 && menuX1
< 0) {
1543 /* scroll to the right */
1544 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX1
));
1546 } else if (xroot
>= screenW
-2 && menuX2
> screenW
-1) {
1547 /* scroll to the left */
1548 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX2
-screenW
-1));
1553 *hamount
= -*hamount
;
1556 if (yroot
<= 1 && menuY1
< 0) {
1558 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY1
));
1560 } else if (yroot
>= screenH
-2 && menuY2
> screenH
-1) {
1562 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY2
-screenH
-2));
1564 *vamount
= -*vamount
;
1570 dragScrollMenuCallback(void *data
)
1572 WMenu
*menu
= (WMenu
*)data
;
1573 WScreen
*scr
= menu
->menu
->screen_ptr
;
1574 WMenu
*parent
= parentMenu(menu
);
1575 int hamount
, vamount
;
1577 int newSelectedEntry
;
1579 getScrollAmount(menu
, &hamount
, &vamount
);
1582 if (hamount
!= 0 || vamount
!= 0) {
1583 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1584 parent
->frame_y
+ vamount
, True
);
1585 if (findMenu(scr
, &x
, &y
)) {
1586 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1587 selectEntry(menu
, newSelectedEntry
);
1589 /* Pointer fell outside of menu. If the selected entry is
1590 * not a submenu, unselect it */
1591 if (menu
->selected_entry
>= 0
1592 && menu
->entries
[menu
->selected_entry
]->cascade
<0)
1593 selectEntry(menu
, -1);
1594 newSelectedEntry
= 0;
1597 /* paranoid check */
1598 if (newSelectedEntry
>= 0) {
1599 /* keep scrolling */
1600 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1601 dragScrollMenuCallback
, menu
);
1606 /* don't need to scroll anymore */
1608 if (findMenu(scr
, &x
, &y
)) {
1609 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1610 selectEntry(menu
, newSelectedEntry
);
1617 scrollMenuCallback(void *data
)
1619 WMenu
*menu
= (WMenu
*)data
;
1620 WMenu
*parent
= parentMenu(menu
);
1621 int hamount
= 0; /* amount to scroll */
1624 getScrollAmount(menu
, &hamount
, &vamount
);
1626 if (hamount
!= 0 || vamount
!= 0) {
1627 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1628 parent
->frame_y
+ vamount
, True
);
1630 /* keep scrolling */
1631 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1632 scrollMenuCallback
, menu
);
1634 /* don't need to scroll anymore */
1641 #define MENU_SCROLL_BORDER 5
1644 isPointNearBoder(WMenu
*menu
, int x
, int y
)
1646 int menuX1
= menu
->frame_x
;
1647 int menuY1
= menu
->frame_y
;
1648 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1649 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1650 int scrXe
= menu
->menu
->screen_ptr
->scr_width
-1;
1651 int scrYe
= menu
->menu
->screen_ptr
->scr_height
-1;
1654 if (x
>= menuX1
&& x
<= menuX2
&& (y
< MENU_SCROLL_BORDER
1655 || y
> scrYe
-MENU_SCROLL_BORDER
))
1657 else if (y
>= menuY1
&& y
<= menuY2
&& (x
< MENU_SCROLL_BORDER
1658 || x
> scrXe
-MENU_SCROLL_BORDER
))
1666 wMenuScroll(WMenu
*menu
, XEvent
*event
)
1669 WMenu
*omenu
= parentMenu(menu
);
1670 WScreen
*scr
= menu
->frame
->screen_ptr
;
1673 int old_frame_x
= omenu
->frame_x
;
1674 int old_frame_y
= omenu
->frame_y
;
1678 puts("Entering menu Scroll");
1681 if ((/*omenu->flags.buttoned &&*/ !wPreferences
.wrap_menus
)
1682 || omenu
->flags
.app_menu
) {
1686 if (!wPreferences
.wrap_menus
)
1687 raiseMenus(omenu
, True
);
1689 raiseMenus(menu
, False
);
1692 scrollMenuCallback(menu
);
1695 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1697 WMNextEvent(dpy
, &ev
);
1702 x
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1703 y
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1705 /* on_border is != 0 if the pointer is between the menu
1706 * and the screen border and is close enough to the border */
1707 on_border
= isPointNearBoder(menu
, x
, y
);
1709 smenu
= wMenuUnderPointer(scr
);
1711 if ((smenu
==NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
)!=omenu
)) {
1716 on_x_edge
= x
<= 1 || x
>= scr
->scr_width
- 2;
1717 on_y_edge
= y
<= 1 || y
>= scr
->scr_height
- 2;
1718 on_border
= on_x_edge
|| on_y_edge
;
1720 if (!on_border
&& !jump_back
) {
1725 if (menu
->timer
&& (smenu
!=menu
|| (!on_y_edge
&& !on_x_edge
))) {
1726 WMDeleteTimerHandler(menu
->timer
);
1734 scrollMenuCallback(menu
);
1737 /* True if we push on title, or drag the omenu to other position */
1738 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1739 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1740 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1741 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1743 smenu
= wMenuUnderPointer(scr
);
1744 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1746 else if (smenu
==omenu
&& on_title
) {
1760 WMDeleteTimerHandler(menu
->timer
);
1765 wMenuMove(omenu
, old_frame_x
, old_frame_y
, True
);
1768 puts("Leaving menu Scroll");
1775 menuExpose(WObjDescriptor
*desc
, XEvent
*event
)
1777 wMenuPaint(desc
->parent
);
1781 int *delayed_select
;
1788 delaySelection(void *data
)
1790 delay_data
*d
= (delay_data
*)data
;
1796 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1797 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1798 entry_no
= getEntryAt(menu
, x
, y
);
1799 selectEntry(menu
, entry_no
);
1801 if (d
->delayed_select
)
1802 *(d
->delayed_select
) = 0;
1807 menuMouseDown(WObjDescriptor
*desc
, XEvent
*event
)
1809 XButtonEvent
*bev
= &event
->xbutton
;
1810 WMenu
*menu
= desc
->parent
;
1812 WScreen
*scr
=menu
->frame
->screen_ptr
;
1813 WMenuEntry
*entry
=NULL
;
1815 int close_on_exit
=0;
1817 int delayed_select
= 0;
1821 int old_frame_x
= 0;
1822 int old_frame_y
= 0;
1823 delay_data d_data
= {NULL
, NULL
, NULL
};
1825 if (!wPreferences
.wrap_menus
) {
1826 smenu
= parentMenu(menu
);
1827 old_frame_x
= smenu
->frame_x
;
1828 old_frame_y
= smenu
->frame_y
;
1830 else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1831 /* This is true if the menu was launched with right click on root window */
1833 d_data
.delayed_select
= &delayed_select
;
1835 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
,
1836 delaySelection
, &d_data
);
1839 wRaiseFrame(menu
->frame
->core
);
1841 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1843 smenu
= findMenu(scr
, &x
, &y
);
1851 if (menu
->flags
.editing
) {
1854 entry_no
= getEntryAt(menu
, x
, y
);
1856 entry
= menu
->entries
[entry_no
];
1858 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
1859 && entry
->flags
.editable
) {
1860 editEntry(smenu
, entry
);
1862 } else if (bev
->state
& ControlMask
) {
1866 if (entry
->flags
.enabled
&& entry
->cascade
>=0 && menu
->cascades
) {
1867 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1869 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&&
1870 menu
->selected_entry
!=entry_no
) {
1871 wMenuUnmap(submenu
);
1873 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1874 selectEntry(menu
, entry_no
);
1875 } else if (!submenu
->flags
.buttoned
) {
1876 selectEntry(menu
, -1);
1879 } else if (!delayed_select
) {
1880 selectEntry(menu
, entry_no
);
1883 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1885 dragScrollMenuCallback(menu
);
1889 prevx
= bev
->x_root
;
1890 prevy
= bev
->y_root
;
1894 XAllowEvents(dpy
, SyncPointer
, CurrentTime
);
1896 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonReleaseMask
1897 |ButtonPressMask
, &ev
);
1900 smenu
= findMenu(scr
, &x
, &y
);
1902 if (smenu
== NULL
) {
1903 /* moved mouse out of menu */
1905 if (!delayed_select
&& d_data
.magic
) {
1906 WMDeleteTimerHandler(d_data
.magic
);
1907 d_data
.magic
= NULL
;
1910 || (menu
->selected_entry
>=0
1911 && menu
->entries
[menu
->selected_entry
]->cascade
>=0)) {
1912 prevx
= ev
.xmotion
.x_root
;
1913 prevy
= ev
.xmotion
.y_root
;
1917 selectEntry(menu
, -1);
1919 prevx
= ev
.xmotion
.x_root
;
1920 prevy
= ev
.xmotion
.y_root
;
1922 } else if (menu
&& menu
!=smenu
1923 && (menu
->selected_entry
<0
1924 || menu
->entries
[menu
->selected_entry
]->cascade
<0)) {
1925 selectEntry(menu
, -1);
1927 if (!delayed_select
&& d_data
.magic
) {
1928 WMDeleteTimerHandler(d_data
.magic
);
1929 d_data
.magic
= NULL
;
1933 /* hysteresis for item selection */
1935 /* check if the motion was to the side, indicating that
1936 * the user may want to cross to a submenu */
1937 if (!delayed_select
&& menu
) {
1939 Bool moved_to_submenu
;/* moved to direction of submenu */
1941 dx
= abs(prevx
- ev
.xmotion
.x_root
);
1943 moved_to_submenu
= False
;
1944 if (dx
> 0 /* if moved enough to the side */
1945 /* maybe a open submenu */
1946 && menu
->selected_entry
>=0
1947 /* moving to the right direction */
1948 && (wPreferences
.align_menus
1949 || ev
.xmotion
.y_root
>= prevy
)) {
1952 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
1954 if (menu
->cascades
[index
]->frame_x
>menu
->frame_x
) {
1955 if (prevx
< ev
.xmotion
.x_root
)
1956 moved_to_submenu
= True
;
1958 if (prevx
> ev
.xmotion
.x_root
)
1959 moved_to_submenu
= True
;
1965 if (menu
!= smenu
) {
1967 WMDeleteTimerHandler(d_data
.magic
);
1969 d_data
.magic
= NULL
;
1970 } else if (moved_to_submenu
) {
1971 /* while we are moving, postpone the selection */
1973 WMDeleteTimerHandler(d_data
.magic
);
1975 d_data
.delayed_select
= NULL
;
1977 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
1980 prevx
= ev
.xmotion
.x_root
;
1981 prevy
= ev
.xmotion
.y_root
;
1985 WMDeleteTimerHandler(d_data
.magic
);
1986 d_data
.magic
= NULL
;
1990 prevx
= ev
.xmotion
.x_root
;
1991 prevy
= ev
.xmotion
.y_root
;
1993 /* pointer crossed menus */
1994 if (menu
&& menu
->timer
) {
1995 WMDeleteTimerHandler(menu
->timer
);
1999 dragScrollMenuCallback(smenu
);
2003 dragScrollMenuCallback(menu
);
2005 if (!delayed_select
) {
2006 entry_no
= getEntryAt(menu
, x
, y
);
2008 entry
= menu
->entries
[entry_no
];
2009 if (entry
->flags
.enabled
&& entry
->cascade
>=0 &&
2011 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
2012 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
2013 && menu
->selected_entry
!=entry_no
) {
2014 wMenuUnmap(submenu
);
2018 selectEntry(menu
, entry_no
);
2026 if (ev
.xbutton
.button
== event
->xbutton
.button
)
2036 if (menu
&& menu
->timer
) {
2037 WMDeleteTimerHandler(menu
->timer
);
2040 if (d_data
.magic
!=NULL
)
2041 WMDeleteTimerHandler(d_data
.magic
);
2043 if (menu
&& menu
->selected_entry
>=0) {
2044 entry
= menu
->entries
[menu
->selected_entry
];
2045 if (entry
->callback
!=NULL
&& entry
->flags
.enabled
2046 && entry
->cascade
< 0) {
2047 /* blink and erase menu selection */
2048 #if (MENU_BLINK_DELAY > 0)
2049 int sel
= menu
->selected_entry
;
2052 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
2053 paintEntry(menu
, sel
, False
);
2055 wusleep(MENU_BLINK_DELAY
);
2056 paintEntry(menu
, sel
, True
);
2058 wusleep(MENU_BLINK_DELAY
);
2061 /* unmap the menu, it's parents and call the callback */
2062 if (!menu
->flags
.buttoned
&&
2063 (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2066 selectEntry(menu
, -1);
2068 (*entry
->callback
)(menu
, entry
);
2070 /* If the user double clicks an entry, the entry will
2071 * be executed twice, which is not good for things like
2072 * the root menu. So, ignore any clicks that were generated
2073 * while the entry was being executed */
2074 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
,
2076 } else if (entry
->callback
!=NULL
&& entry
->cascade
<0) {
2077 selectEntry(menu
, -1);
2079 if (entry
->cascade
>=0 && menu
->cascades
2080 && menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2081 selectEntry(menu
, -1);
2086 if (((WMenu
*)desc
->parent
)->flags
.brother
|| close_on_exit
2088 closeCascade(desc
->parent
);
2090 /* close the cascade windows that should not remain opened */
2091 closeBrotherCascadesOf(desc
->parent
);
2093 if (!wPreferences
.wrap_menus
)
2094 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2099 wMenuMove(WMenu
*menu
, int x
, int y
, int submenus
)
2108 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2110 if (submenus
>0 && menu
->selected_entry
>=0) {
2111 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2113 if (i
>=0 && menu
->cascades
) {
2114 submenu
= menu
->cascades
[i
];
2115 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
2116 if (wPreferences
.align_menus
) {
2117 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2119 wMenuMove(submenu
, x
+ MENUW(menu
),
2120 y
+ submenu
->entry_height
*menu
->selected_entry
,
2125 if (submenus
<0 && menu
->parent
!=NULL
&& menu
->parent
->flags
.mapped
&&
2126 !menu
->parent
->flags
.buttoned
) {
2127 if (wPreferences
.align_menus
) {
2128 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2130 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2131 - menu
->parent
->entry_height
*menu
->parent
->selected_entry
,
2139 changeMenuLevels(WMenu
*menu
, int lower
)
2144 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
2146 wRaiseFrame(menu
->frame
->core
);
2147 menu
->flags
.lowered
= 0;
2149 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2150 wLowerFrame(menu
->frame
->core
);
2151 menu
->flags
.lowered
= 1;
2153 for (i
=0; i
<menu
->cascade_no
; i
++) {
2154 if (menu
->cascades
[i
]
2155 && !menu
->cascades
[i
]->flags
.buttoned
2156 && menu
->cascades
[i
]->flags
.lowered
!=lower
) {
2157 changeMenuLevels(menu
->cascades
[i
], lower
);
2165 menuTitleDoubleClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2170 if (event
->xbutton
.state
& MOD_MASK
) {
2171 if (menu
->flags
.lowered
) {
2176 changeMenuLevels(menu
, lower
);
2182 menuTitleMouseDown(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2187 int x
=menu
->frame_x
, y
=menu
->frame_y
;
2188 int dx
=event
->xbutton
.x_root
, dy
=event
->xbutton
.y_root
;
2193 printf("Moving menu\n");
2196 /* can't touch the menu copy */
2197 if (menu
->flags
.brother
)
2200 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2203 if (event
->xbutton
.state
& MOD_MASK
) {
2204 wLowerFrame(menu
->frame
->core
);
2207 wRaiseFrame(menu
->frame
->core
);
2212 /* lower/raise all submenus */
2214 if (tmp
->selected_entry
>=0 && tmp
->cascades
2215 && tmp
->entries
[tmp
->selected_entry
]->cascade
>=0) {
2216 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2217 if (!tmp
|| !tmp
->flags
.mapped
)
2220 wLowerFrame(tmp
->frame
->core
);
2222 wRaiseFrame(tmp
->frame
->core
);
2228 /* tear off the menu if it's a root menu or a cascade
2230 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
2231 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2232 menu
->flags
.buttoned
=1;
2233 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2235 /* turn off selected menu entry in parent menu */
2236 selectEntry(menu
->parent
, -1);
2238 /* make parent map the copy in place of the original */
2239 for (i
=0; i
<menu
->parent
->cascade_no
; i
++) {
2240 if (menu
->parent
->cascades
[i
] == menu
) {
2241 menu
->parent
->cascades
[i
] = menu
->brother
;
2250 WMMaskEvent(dpy
, ButtonMotionMask
|ButtonReleaseMask
|ButtonPressMask
2251 |ExposureMask
, &ev
);
2255 x
+= ev
.xmotion
.x_root
- dx
;
2256 y
+= ev
.xmotion
.y_root
- dy
;
2257 dx
= ev
.xmotion
.x_root
;
2258 dy
= ev
.xmotion
.y_root
;
2259 wMenuMove(menu
, x
, y
, True
);
2261 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2262 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2264 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2265 ButtonMotionMask
|ButtonReleaseMask
2267 GrabModeAsync
, GrabModeAsync
, None
,
2268 wCursor
[WCUR_MOVE
], CurrentTime
);
2277 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2280 printf("End menu move\n");
2282 XUngrabPointer(dpy
, CurrentTime
);
2293 *----------------------------------------------------------------------
2295 * Handles mouse click on the close button of menus. The menu is
2296 * closed when the button is clicked.
2299 * The closed menu is reinserted at it's parent menus
2301 *----------------------------------------------------------------------
2304 menuCloseClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2306 WMenu
*menu
= (WMenu
*)data
;
2307 WMenu
*parent
= menu
->parent
;
2311 for (i
=0; i
<parent
->cascade_no
; i
++) {
2312 /* find the entry that points to the copy */
2313 if (parent
->cascades
[i
] == menu
->brother
) {
2314 /* make it point to the original */
2315 parent
->cascades
[i
] = menu
;
2316 menu
->parent
= parent
;
2326 wMenuSaveState(WScreen
*scr
)
2328 proplist_t menus
, key
, value
;
2332 menus
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
2335 if (scr
->root_menu
&& scr
->root_menu
->flags
.buttoned
) {
2336 sprintf(buffer
, "%i,%i", scr
->root_menu
->frame_x
,
2337 scr
->root_menu
->frame_y
);
2338 key
= PLMakeString("RootMenu");
2339 value
= PLMakeString(buffer
);
2340 PLInsertDictionaryEntry(menus
, key
, value
);
2346 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2347 sprintf(buffer
, "%i,%i", scr
->switch_menu
->frame_x
,
2348 scr
->switch_menu
->frame_y
);
2349 key
= PLMakeString("SwitchMenu");
2350 value
= PLMakeString(buffer
);
2351 PLInsertDictionaryEntry(menus
, key
, value
);
2357 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2358 sprintf(buffer
, "%i,%i", scr
->workspace_menu
->frame_x
,
2359 scr
->workspace_menu
->frame_y
);
2360 key
= PLMakeString("WorkspaceMenu");
2361 value
= PLMakeString(buffer
);
2362 PLInsertDictionaryEntry(menus
, key
, value
);
2369 key
= PLMakeString("Menus");
2370 PLInsertDictionaryEntry(scr
->session_state
, key
, menus
);
2377 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2381 restoreMenu(WScreen
*scr
, proplist_t menu
, int which
)
2384 WMenu
*pmenu
= NULL
;
2389 if (!PLIsString(menu
)) {
2390 COMPLAIN("Position");
2394 if (sscanf(PLGetString(menu
), "%i,%i", &x
, &y
)!=2)
2395 COMPLAIN("Position");
2398 if (which
& WSS_ROOTMENU
) {
2399 OpenRootMenu(scr
, x
, y
, False
);
2400 pmenu
= scr
->root_menu
;
2401 } else if (which
& WSS_SWITCHMENU
) {
2402 OpenSwitchMenu(scr
, x
, y
, False
);
2403 pmenu
= scr
->switch_menu
;
2406 if (which
& WSS_WSMENU
) {
2407 OpenWorkspaceMenu(scr
, x
, y
);
2408 pmenu
= scr
->workspace_menu
;
2409 if (pmenu
->parent
) {
2410 /* make parent map the copy in place of the original */
2411 for (i
=0; i
<pmenu
->parent
->cascade_no
; i
++) {
2412 if (pmenu
->parent
->cascades
[i
] == pmenu
) {
2413 pmenu
->parent
->cascades
[i
] = pmenu
->brother
;
2421 int width
= MENUW(pmenu
);
2422 int height
= MENUH(pmenu
);
2424 x
= (x
< -width
) ? 0 : x
;
2425 x
= (x
> scr
->scr_width
) ? scr
->scr_width
- width
: x
;
2426 y
= (y
< 0) ? 0 : y
;
2427 y
= (y
> scr
->scr_height
) ? scr
->scr_height
- height
: y
;
2428 wMenuMove(pmenu
, x
, y
, True
);
2429 pmenu
->flags
.buttoned
= 1;
2430 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2438 wMenuRestoreState(WScreen
*scr
)
2440 proplist_t menus
, menu
, key
, rkey
, skey
, wkey
;
2442 key
= PLMakeString("Menus");
2443 menus
= PLGetDictionaryEntry(scr
->session_state
, key
);
2451 rkey
= PLMakeString("RootMenu");
2452 skey
= PLMakeString("SwitchMenu");
2453 wkey
= PLMakeString("WorkspaceMenu");
2454 menu
= PLGetDictionaryEntry(menus
, rkey
);
2455 restoreMenu(scr
, menu
, WSS_ROOTMENU
);
2456 menu
= PLGetDictionaryEntry(menus
, skey
);
2457 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2458 menu
= PLGetDictionaryEntry(menus
, wkey
);
2459 restoreMenu(scr
, menu
, WSS_WSMENU
);
2468 OpenWorkspaceMenu(WScreen
*scr
, int x
, int y
)
2470 WMenu
*menu
, *parent
;
2474 if (!scr
->root_menu
) {
2475 OpenRootMenu(scr
, scr
->scr_width
*2, 0, False
);
2476 wMenuUnmap(scr
->root_menu
);
2479 menu
= scr
->workspace_menu
;
2481 if (menu
->flags
.mapped
) {
2482 if (!menu
->flags
.buttoned
) {
2484 parent
= menu
->parent
;
2485 if (parent
&& parent
->selected_entry
>= 0) {
2486 entry
= parent
->entries
[parent
->selected_entry
];
2487 if (parent
->cascades
[entry
->cascade
] == menu
) {
2488 selectEntry(parent
, -1);
2489 wMenuMapAt(menu
, x
, y
, False
);
2493 wRaiseFrame(menu
->frame
->core
);
2494 wMenuMapCopyAt(menu
, x
, y
);
2498 wMenuMapAt(menu
, x
, y
, False
);