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 renderTexture(WMenu
*menu
)
449 WScreen
*scr
= menu
->menu
->screen_ptr
;
450 WTexture
*texture
= scr
->menu_item_texture
;
452 if (wPreferences
.menu_style
== MS_NORMAL
) {
453 img
= wTextureRenderImage(texture
, menu
->menu
->width
,
454 menu
->entry_height
, WREL_MENUENTRY
);
456 img
= wTextureRenderImage(texture
, menu
->menu
->width
,
457 menu
->menu
->height
+1, WREL_MENUENTRY
);
460 wwarning(_("could not render texture: %s"),
461 RMessageForError(RErrorCode
));
466 if (wPreferences
.menu_style
== MS_SINGLE_TEXTURE
) {
468 light
.red
= light
.green
= light
.blue
= 80;
471 dark
.red
= dark
.green
= dark
.blue
= 0;
474 mid
.red
= mid
.green
= mid
.blue
= 40;
476 for (i
= 1; i
< menu
->entry_no
; i
++) {
477 ROperateLine(img
, RSubtractOperation
, 0, i
*menu
->entry_height
-2,
478 menu
->menu
->width
-1, i
*menu
->entry_height
-2, &mid
);
480 RDrawLine(img
, 0, i
*menu
->entry_height
-1,
481 menu
->menu
->width
-1, i
*menu
->entry_height
-1, &dark
);
483 ROperateLine(img
, RAddOperation
, 0, i
*menu
->entry_height
,
484 menu
->menu
->width
-1, i
*menu
->entry_height
,
488 if (!RConvertImage(scr
->rcontext
, img
, &pix
)) {
489 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode
));
498 updateTexture(WMenu
*menu
)
500 WScreen
*scr
= menu
->menu
->screen_ptr
;
502 /* setup background texture */
503 if (scr
->menu_item_texture
->any
.type
!= WTEX_SOLID
) {
504 if (!menu
->flags
.brother
) {
505 FREE_PIXMAP(menu
->menu_texture_data
);
507 menu
->menu_texture_data
= renderTexture(menu
);
509 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
,
510 menu
->menu_texture_data
);
511 XClearWindow(dpy
, menu
->menu
->window
);
513 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
,
514 menu
->menu_texture_data
);
515 XClearWindow(dpy
, menu
->brother
->menu
->window
);
518 XSetWindowBackground(dpy
, menu
->menu
->window
,
519 scr
->menu_item_texture
->any
.color
.pixel
);
520 XClearWindow(dpy
, menu
->menu
->window
);
526 wMenuRealize(WMenu
*menu
)
529 int width
, rwidth
, mrwidth
, mwidth
;
530 int theight
, twidth
, eheight
;
531 WScreen
*scr
= menu
->frame
->screen_ptr
;
532 static int brother_done
=0;
537 wMenuRealize(menu
->brother
);
541 flags
= WFF_SINGLE_STATE
;
542 if (menu
->flags
.titled
)
543 flags
|= WFF_TITLEBAR
|WFF_RIGHT_BUTTON
;
545 wFrameWindowUpdateBorders(menu
->frame
, flags
);
547 if (menu
->flags
.titled
) {
548 twidth
= wTextWidth(scr
->menu_title_font
->font
, menu
->frame
->title
,
549 strlen(menu
->frame
->title
));
550 theight
= menu
->frame
->top_width
;
551 twidth
+= theight
+ (wPreferences
.new_style
? 16 : 8);
556 eheight
= scr
->menu_entry_font
->height
+ 6;
557 menu
->entry_height
= eheight
;
560 for (i
=0; i
<menu
->entry_no
; i
++) {
563 /* search widest text */
564 text
= menu
->entries
[i
]->text
;
565 width
= wTextWidth(scr
->menu_entry_font
->font
, text
, strlen(text
))+10;
567 if (menu
->entries
[i
]->flags
.indicator
) {
568 width
+= MENU_INDICATOR_SPACE
;
574 /* search widest text on right */
575 text
= menu
->entries
[i
]->rtext
;
577 rwidth
= wTextWidth(scr
->menu_entry_font
->font
, text
,
579 else if (menu
->entries
[i
]->cascade
>=0)
584 if (rwidth
> mrwidth
)
593 wCoreConfigure(menu
->menu
, 0, theight
, mwidth
, menu
->entry_no
*eheight
-1);
595 wFrameWindowResize(menu
->frame
, mwidth
, menu
->entry_no
*eheight
-1
596 + menu
->frame
->top_width
+ menu
->frame
->bottom_width
);
601 menu
->flags
.realized
= 1;
603 if (menu
->flags
.mapped
)
605 if (menu
->brother
->flags
.mapped
)
606 wMenuPaint(menu
->brother
);
611 wMenuDestroy(WMenu
*menu
, int recurse
)
615 WMRemoveNotificationObserver(menu
);
617 /* remove any pending timers */
619 WMDeleteTimerHandler(menu
->timer
);
622 /* call destroy handler */
623 if (menu
->on_destroy
)
624 (*menu
->on_destroy
)(menu
);
626 /* Destroy items if this menu own them. If this is the "brother" menu,
627 * leave them alone as it is shared by them.
629 if (!menu
->flags
.brother
) {
630 for (i
=0; i
<menu
->entry_no
; i
++) {
632 free(menu
->entries
[i
]->text
);
634 if (menu
->entries
[i
]->rtext
)
635 free(menu
->entries
[i
]->rtext
);
638 if (menu
->entries
[i
]->instances
){
639 PLRelease(menu
->entries
[i
]->instances
);
641 #endif /* USER_MENU */
643 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
644 (*menu
->entries
[i
]->free_cdata
)(menu
->entries
[i
]->clientdata
);
646 free(menu
->entries
[i
]);
650 for (i
=0; i
<menu
->cascade_no
; i
++) {
651 if (menu
->cascades
[i
]) {
652 if (menu
->cascades
[i
]->flags
.brother
)
653 wMenuDestroy(menu
->cascades
[i
]->brother
, recurse
);
655 wMenuDestroy(menu
->cascades
[i
], recurse
);
665 FREE_PIXMAP(menu
->menu_texture_data
);
668 free(menu
->cascades
);
670 wCoreDestroy(menu
->menu
);
671 wFrameWindowDestroy(menu
->frame
);
673 /* destroy copy of this menu */
674 if (!menu
->flags
.brother
&& menu
->brother
)
675 wMenuDestroy(menu
->brother
, False
);
687 drawFrame(WScreen
*scr
, Window win
, int y
, int w
, int h
, int type
)
693 segs
[i
].x1
= segs
[i
].x2
= w
-1;
695 segs
[i
].y2
= y
+ h
- 1;
697 if (type
!= F_TOP
&& type
!= F_NONE
) {
699 segs
[i
].y1
= segs
[i
].y2
= y
+ h
-2;
703 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, i
);
709 segs
[i
].y2
= y
+ h
- 1;
711 if (type
!= F_BOTTOM
&& type
!= F_NONE
) {
718 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, i
);
720 if (type
!= F_TOP
&& type
!= F_NONE
)
721 XDrawLine(dpy
, win
, scr
->menu_item_auxtexture
->dark_gc
, 0, y
+h
-1,
727 paintEntry(WMenu
*menu
, int index
, int selected
)
731 GC light
, dim
, dark
, textGC
;
732 WScreen
*scr
=menu
->frame
->screen_ptr
;
733 Window win
= menu
->menu
->window
;
734 WMenuEntry
*entry
=menu
->entries
[index
];
736 if (!menu
->flags
.realized
) return;
737 h
= menu
->entry_height
;
738 w
= menu
->menu
->width
;
741 light
= scr
->menu_item_auxtexture
->light_gc
;
742 dim
= scr
->menu_item_auxtexture
->dim_gc
;
743 dark
= scr
->menu_item_auxtexture
->dark_gc
;
745 if (wPreferences
.menu_style
== MS_FLAT
&& menu
->entry_no
> 1) {
748 else if (index
== menu
->entry_no
- 1)
756 /* paint background */
758 XSetForeground(dpy
, scr
->select_menu_gc
, scr
->select_pixel
);
759 XFillRectangle(dpy
, win
, scr
->select_menu_gc
, 1, y
+1, w
-2, h
-3);
760 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
)
761 drawFrame(scr
, win
, y
, w
, h
, type
);
763 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
764 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
766 drawFrame(scr
, win
, y
, w
, h
, type
);
768 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
773 textGC
= scr
->select_menu_gc
;
774 if (entry
->flags
.enabled
)
775 XSetForeground(dpy
, textGC
, scr
->select_text_pixel
);
777 XSetForeground(dpy
, textGC
, scr
->dtext_pixel
);
778 } else if (!entry
->flags
.enabled
) {
779 textGC
= scr
->disabled_menu_entry_gc
;
781 textGC
= scr
->menu_entry_gc
;
785 if (entry
->flags
.indicator
)
786 x
+= MENU_INDICATOR_SPACE
+ 2;
788 wDrawString(win
, scr
->menu_entry_font
,
789 textGC
, x
, 3+y
+scr
->menu_entry_font
->y
, entry
->text
,
790 strlen(entry
->text
));
792 if (entry
->cascade
>=0) {
793 /* draw the cascade indicator */
794 XDrawLine(dpy
,win
,dim
, w
-11, y
+6, w
-6, y
+h
/2-1);
795 XDrawLine(dpy
,win
,light
, w
-11, y
+h
-8, w
-6, y
+h
/2-1);
796 XDrawLine(dpy
,win
,dark
, w
-12, y
+6, w
-12, y
+h
-8);
800 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
805 switch (entry
->flags
.indicator_type
) {
807 indicator
= scr
->menu_check_indicator
;
810 indicator
= scr
->menu_mini_indicator
;
813 indicator
= scr
->menu_hide_indicator
;
816 indicator
= scr
->menu_shade_indicator
;
820 indicator
= scr
->menu_radio_indicator
;
824 iw
= indicator
->width
;
825 ih
= indicator
->height
;
826 XSetClipMask(dpy
, scr
->copy_gc
, indicator
->mask
);
827 XSetClipOrigin(dpy
, scr
->copy_gc
, 5, y
+(h
-ih
)/2);
829 XSetForeground(dpy
, scr
->copy_gc
, scr
->black_pixel
);
831 XSetForeground(dpy
, scr
->copy_gc
, scr
->mtext_pixel
);
832 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+(h
-ih
)/2, iw
, ih
);
834 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
835 iw, ih, 5, y+(h-ih)/2);
837 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
840 /* draw right text */
842 if (entry
->rtext
&& entry
->cascade
<0) {
843 tw
= wTextWidth(scr
->menu_entry_font
->font
, entry
->rtext
,
844 strlen(entry
->rtext
));
846 wDrawString(win
, scr
->menu_entry_font
, textGC
, w
-6-tw
,
847 3+y
+scr
->menu_entry_font
->y
, entry
->rtext
,
848 strlen(entry
->rtext
));
854 move_menus(WMenu
*menu
, int x
, int y
)
856 while (menu
->parent
) {
859 if (!wPreferences
.align_menus
&& menu
->selected_entry
>=0) {
860 y
-= menu
->selected_entry
*menu
->entry_height
;
863 wMenuMove(menu
, x
, y
, True
);
867 makeVisible(WMenu
*menu
)
869 WScreen
*scr
= menu
->frame
->screen_ptr
;
870 int x1
, y1
, x2
, y2
, new_x
, new_y
, move
;
872 if (menu
->entry_no
<0) return;
875 y1
= menu
->frame_y
+menu
->frame
->top_width
876 + menu
->selected_entry
*menu
->entry_height
;
877 x2
= x1
+ MENUW(menu
);
878 y2
= y1
+ menu
->entry_height
;
887 } else if (x2
>= scr
->scr_width
) {
888 new_x
= scr
->scr_width
- MENUW(menu
) - 1;
895 } else if (y2
>= scr
->scr_height
) {
896 new_y
= scr
->scr_height
- menu
->entry_height
- 1;
900 new_y
= new_y
- menu
->frame
->top_width
901 - menu
->selected_entry
*menu
->entry_height
;
902 move_menus(menu
, new_x
, new_y
);
907 check_key(WMenu
*menu
, XKeyEvent
*event
)
912 if (XLookupString(event
, buffer
, 32, NULL
, NULL
)<1)
915 ch
= toupper(buffer
[0]);
917 s
= (menu
->selected_entry
>=0 ? menu
->selected_entry
+1 : 0);
920 for (i
=s
; i
<menu
->entry_no
; i
++) {
921 if (ch
==toupper(menu
->entries
[i
]->text
[0])) {
925 /* no match. Retry from start, if previous started from a selected entry */
935 keyboardMenu(WMenu
*menu
)
938 KeySym ksym
=NoSymbol
;
942 int old_pos_x
= menu
->frame_x
;
943 int old_pos_y
= menu
->frame_y
;
944 int new_x
= old_pos_x
, new_y
= old_pos_y
;
945 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
946 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
948 if (menu
->flags
.editing
)
952 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
,
953 GrabModeAsync
, CurrentTime
);
955 if (menu
->frame_y
+menu
->frame
->top_width
>= scr_height
)
956 new_y
= scr_height
- menu
->frame
->top_width
;
958 if (menu
->frame_x
+MENUW(menu
) >= scr_width
)
959 new_x
= scr_width
-MENUW(menu
)-1;
961 move_menus(menu
, new_x
, new_y
);
963 while (!done
&& menu
->flags
.mapped
) {
964 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
965 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonPressMask
966 |ButtonReleaseMask
|KeyPressMask
|KeyReleaseMask
967 |SubstructureNotifyMask
, &event
);
969 switch (event
.type
) {
971 ksym
= XLookupKeysym(&event
.xkey
, 0);
979 selectEntry(menu
, 0);
985 selectEntry(menu
, menu
->entry_no
-1);
994 if (menu
->selected_entry
<= 0)
995 selectEntry(menu
, menu
->entry_no
-1);
997 selectEntry(menu
, menu
->selected_entry
-1);
1002 #ifdef ARROWLESS_KBD
1006 if (menu
->selected_entry
<0)
1007 selectEntry(menu
, 0);
1008 else if (menu
->selected_entry
== menu
->entry_no
-1)
1009 selectEntry(menu
, 0);
1010 else if (menu
->selected_entry
< menu
->entry_no
-1)
1011 selectEntry(menu
, menu
->selected_entry
+1);
1016 #ifdef ARROWLESS_KBD
1020 if (menu
->selected_entry
>=0) {
1022 entry
= menu
->entries
[menu
->selected_entry
];
1024 if (entry
->cascade
>= 0 && menu
->cascades
1025 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
1027 XUngrabKeyboard(dpy
, CurrentTime
);
1029 selectEntry(menu
->cascades
[entry
->cascade
], 0);
1030 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
1033 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
1034 GrabModeAsync
, GrabModeAsync
,
1041 #ifdef ARROWLESS_KBD
1045 if (menu
->parent
!=NULL
&& menu
->parent
->selected_entry
>=0) {
1046 selectEntry(menu
, -1);
1047 move_menus(menu
, old_pos_x
, old_pos_y
);
1057 index
= check_key(menu
, &event
.xkey
);
1059 selectEntry(menu
, index
);
1065 if (event
.type
==ButtonPress
)
1068 WMHandleEvent(&event
);
1072 XUngrabKeyboard(dpy
, CurrentTime
);
1074 if (done
==2 && menu
->selected_entry
>=0) {
1075 entry
= menu
->entries
[menu
->selected_entry
];
1080 if (entry
&& entry
->callback
!=NULL
&& entry
->flags
.enabled
1081 && entry
->cascade
< 0) {
1082 #if (MENU_BLINK_COUNT > 0)
1083 int sel
= menu
->selected_entry
;
1086 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
1087 paintEntry(menu
, sel
, False
);
1089 wusleep(MENU_BLINK_DELAY
);
1090 paintEntry(menu
, sel
, True
);
1092 wusleep(MENU_BLINK_DELAY
);
1095 selectEntry(menu
, -1);
1097 if (!menu
->flags
.buttoned
) {
1099 move_menus(menu
, old_pos_x
, old_pos_y
);
1103 (*entry
->callback
)(menu
, entry
);
1105 if (!menu
->flags
.buttoned
) {
1107 move_menus(menu
, old_pos_x
, old_pos_y
);
1109 selectEntry(menu
, -1);
1113 /* returns True if returning from a submenu to a parent menu,
1114 * False if exiting from menu */
1120 wMenuMapAt(WMenu
*menu
, int x
, int y
, int keyboard
)
1122 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
1123 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
1125 if (!menu
->flags
.realized
) {
1126 menu
->flags
.realized
=1;
1129 if (!menu
->flags
.mapped
) {
1130 if (wPreferences
.wrap_menus
) {
1133 if (x
+MENUW(menu
) > scr_width
)
1134 x
= scr_width
- MENUW(menu
);
1135 if (y
+MENUH(menu
) > scr_height
)
1136 y
= scr_height
- MENUH(menu
);
1139 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
1142 XMapWindow(dpy
, menu
->frame
->core
->window
);
1143 wRaiseFrame(menu
->frame
->core
);
1144 menu
->flags
.mapped
= 1;
1146 selectEntry(menu
, 0);
1155 wMenuMap(WMenu
*menu
)
1157 if (!menu
->flags
.realized
) {
1158 menu
->flags
.realized
=1;
1161 if (menu
->flags
.app_menu
&& menu
->parent
==NULL
) {
1162 menu
->frame_x
= menu
->frame
->screen_ptr
->app_menu_x
;
1163 menu
->frame_y
= menu
->frame
->screen_ptr
->app_menu_y
;
1164 XMoveWindow(dpy
, menu
->frame
->core
->window
, menu
->frame_x
, menu
->frame_y
);
1166 XMapWindow(dpy
, menu
->frame
->core
->window
);
1167 wRaiseFrame(menu
->frame
->core
);
1168 menu
->flags
.mapped
= 1;
1173 wMenuUnmap(WMenu
*menu
)
1177 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1178 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1179 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1181 menu
->flags
.buttoned
= 0;
1182 menu
->flags
.mapped
= 0;
1183 menu
->flags
.open_to_left
= 0;
1185 for (i
=0; i
<menu
->cascade_no
; i
++) {
1186 if (menu
->cascades
[i
]!=NULL
1187 && menu
->cascades
[i
]->flags
.mapped
1188 && !menu
->cascades
[i
]->flags
.buttoned
) {
1190 wMenuUnmap(menu
->cascades
[i
]);
1193 menu
->selected_entry
= -1;
1199 wMenuPaint(WMenu
*menu
)
1203 if (!menu
->flags
.mapped
) {
1208 for (i
=0; i
<menu
->entry_no
; i
++) {
1209 paintEntry(menu
, i
, i
==menu
->selected_entry
);
1215 wMenuSetEnabled(WMenu
*menu
, int index
, int enable
)
1217 if (index
>=menu
->entry_no
) return;
1218 menu
->entries
[index
]->flags
.enabled
=enable
;
1219 paintEntry(menu
, index
, index
==menu
->selected_entry
);
1220 paintEntry(menu
->brother
, index
, index
==menu
->selected_entry
);
1224 /* ====================================================================== */
1228 editEntry(WMenu
*menu
, WMenuEntry
*entry
)
1232 WObjDescriptor
*desc
;
1238 menu
->flags
.editing
= 1;
1240 text
= wTextCreate(menu
->menu
, 1, menu
->entry_height
* entry
->order
,
1241 menu
->menu
->width
- 2, menu
->entry_height
- 1);
1243 wTextPutText(text
, entry
->text
);
1244 XGetInputFocus(dpy
, &old_focus
, &old_revert
);
1245 XSetInputFocus(dpy
, text
->core
->window
, RevertToNone
, CurrentTime
);
1247 if (XGrabKeyboard(dpy
, text
->core
->window
, True
, GrabModeAsync
,
1248 GrabModeAsync
, CurrentTime
)!=GrabSuccess
) {
1249 wwarning("could not grab keyboard");
1252 wSetFocusTo(menu
->frame
->screen_ptr
,
1253 menu
->frame
->screen_ptr
->focused_window
);
1258 while (!done
&& !text
->done
) {
1260 XAllowEvents(dpy
, AsyncKeyboard
|AsyncPointer
, CurrentTime
);
1262 WMNextEvent(dpy
, &event
);
1264 if (XFindContext(dpy
, event
.xany
.window
, wWinContext
,
1265 (XPointer
*)&desc
)==XCNOENT
)
1268 if ((desc
!= NULL
) && (desc
->handle_anything
!= NULL
)) {
1270 (*desc
->handle_anything
)(desc
, &event
);
1273 switch (event
.type
) {
1275 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
1279 WMHandleEvent(&event
);
1285 XSetInputFocus(dpy
, old_focus
, old_revert
, CurrentTime
);
1287 wSetFocusTo(menu
->frame
->screen_ptr
,
1288 menu
->frame
->screen_ptr
->focused_window
);
1291 t
= wTextGetText(text
);
1292 /* if !t, the user has canceled editing */
1296 entry
->text
= wstrdup(t
);
1298 menu
->flags
.realized
= 0;
1302 XUngrabKeyboard(dpy
, CurrentTime
);
1304 if (t
&& menu
->on_edit
)
1305 (*menu
->on_edit
)(menu
, entry
);
1307 menu
->flags
.editing
= 0;
1309 if (!menu
->flags
.realized
)
1315 selectEntry(WMenu
*menu
, int entry_no
)
1321 if (menu
->entries
==NULL
)
1324 if (entry_no
>= menu
->entry_no
)
1327 old_entry
= menu
->selected_entry
;
1328 menu
->selected_entry
= entry_no
;
1330 if (old_entry
!=entry_no
) {
1332 /* unselect previous entry */
1334 paintEntry(menu
, old_entry
, False
);
1335 entry
= menu
->entries
[old_entry
];
1338 if (entry
->cascade
>=0 && menu
->cascades
) {
1339 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1340 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1346 menu
->selected_entry
= -1;
1349 entry
= menu
->entries
[entry_no
];
1351 if (entry
->cascade
>=0 && menu
->cascades
&& entry
->flags
.enabled
) {
1352 /* Callback for when the submenu is opened.
1354 submenu
= menu
->cascades
[entry
->cascade
];
1355 if (submenu
&& submenu
->flags
.brother
)
1356 submenu
= submenu
->brother
;
1358 if (entry
->callback
) {
1359 /* Only call the callback if the submenu is not yet mapped.
1361 if (menu
->flags
.brother
) {
1362 if (!submenu
|| !submenu
->flags
.mapped
)
1363 (*entry
->callback
)(menu
->brother
, entry
);
1365 if (!submenu
|| !submenu
->flags
.buttoned
)
1366 (*entry
->callback
)(menu
, entry
);
1370 /* the submenu menu might have changed */
1371 submenu
= menu
->cascades
[entry
->cascade
];
1374 if (!submenu
->flags
.mapped
) {
1377 if (!submenu
->flags
.realized
)
1378 wMenuRealize(submenu
);
1379 if (wPreferences
.wrap_menus
) {
1380 if (menu
->flags
.open_to_left
)
1381 submenu
->flags
.open_to_left
= 1;
1383 if (submenu
->flags
.open_to_left
) {
1384 x
= menu
->frame_x
- MENUW(submenu
);
1387 submenu
->flags
.open_to_left
= 0;
1390 x
= menu
->frame_x
+ MENUW(menu
);
1392 if (x
+ MENUW(submenu
)
1393 >= menu
->frame
->screen_ptr
->scr_width
) {
1395 x
= menu
->frame_x
- MENUW(submenu
);
1396 submenu
->flags
.open_to_left
= 1;
1400 x
= menu
->frame_x
+ MENUW(menu
);
1403 if (wPreferences
.align_menus
) {
1406 y
= menu
->frame_y
+ menu
->entry_height
*entry_no
;
1407 if (menu
->flags
.titled
)
1408 y
+= menu
->frame
->top_width
;
1409 if (menu
->cascades
[entry
->cascade
]->flags
.titled
)
1410 y
-= menu
->cascades
[entry
->cascade
]->frame
->top_width
;
1413 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1414 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1419 paintEntry(menu
, entry_no
, True
);
1425 findMenu(WScreen
*scr
, int *x_ret
, int *y_ret
)
1428 WObjDescriptor
*desc
;
1429 Window root_ret
, win
, junk_win
;
1433 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
,
1436 if (win
==None
) return NULL
;
1438 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1441 if (desc
->parent_type
== WCLASS_MENU
) {
1442 menu
= (WMenu
*)desc
->parent
;
1443 XTranslateCoordinates(dpy
, root_ret
, menu
->menu
->window
, wx
, wy
,
1444 x_ret
, y_ret
, &junk_win
);
1454 closeCascade(WMenu
*menu
)
1456 WMenu
*parent
=menu
->parent
;
1458 if (menu
->flags
.brother
1459 || (!menu
->flags
.buttoned
1460 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
))) {
1462 selectEntry(menu
, -1);
1464 #if (MENU_BLINK_DELAY > 2)
1465 wusleep(MENU_BLINK_DELAY
/2);
1469 && (parent
->parent
!=NULL
|| !parent
->flags
.app_menu
1470 || parent
->flags
.brother
)
1471 && !parent
->flags
.buttoned
) {
1472 selectEntry(parent
, -1);
1474 parent
= parent
->parent
;
1477 selectEntry(parent
, -1);
1483 closeBrotherCascadesOf(WMenu
*menu
)
1488 for (i
=0; i
<menu
->cascade_no
; i
++) {
1489 if (menu
->cascades
[i
]->flags
.brother
) {
1490 tmp
= menu
->cascades
[i
];
1492 tmp
= menu
->cascades
[i
]->brother
;
1494 if (tmp
->flags
.mapped
) {
1495 selectEntry(tmp
->parent
, -1);
1496 closeBrotherCascadesOf(tmp
);
1503 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1507 parentMenu(WMenu
*menu
)
1512 if (menu
->flags
.buttoned
)
1515 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1516 parent
= menu
->parent
;
1517 if (parent
->selected_entry
< 0)
1519 entry
= parent
->entries
[parent
->selected_entry
];
1520 if (!entry
->flags
.enabled
|| entry
->cascade
<0 || !parent
->cascades
||
1521 parent
->cascades
[entry
->cascade
] != menu
)
1524 if (menu
->flags
.buttoned
)
1534 * Will raise the passed menu, if submenu = 0
1535 * If submenu > 0 will also raise all mapped submenus
1536 * until the first buttoned one
1537 * If submenu < 0 will also raise all mapped parent menus
1538 * until the first buttoned one
1542 raiseMenus(WMenu
*menu
, int submenus
)
1549 wRaiseFrame(menu
->frame
->core
);
1551 if (submenus
>0 && menu
->selected_entry
>=0) {
1552 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
1553 if (i
>=0 && menu
->cascades
) {
1554 submenu
= menu
->cascades
[i
];
1555 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
1556 raiseMenus(submenu
, submenus
);
1559 if (submenus
<0 && !menu
->flags
.buttoned
&&
1560 menu
->parent
&& menu
->parent
->flags
.mapped
)
1561 raiseMenus(menu
->parent
, submenus
);
1566 wMenuUnderPointer(WScreen
*screen
)
1568 WObjDescriptor
*desc
;
1569 Window root_ret
, win
;
1573 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
,
1574 &dummy
, &dummy
, &mask
);
1576 if (win
==None
) return NULL
;
1578 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1581 if (desc
->parent_type
== WCLASS_MENU
)
1582 return (WMenu
*)desc
->parent
;
1588 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1592 getPointerPosition(WScreen
*scr
, int *x
, int *y
)
1594 Window root_ret
, win
;
1598 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1603 getScrollAmount(WMenu
*menu
, int *hamount
, int *vamount
)
1605 WScreen
*scr
= menu
->menu
->screen_ptr
;
1606 int menuX1
= menu
->frame_x
;
1607 int menuY1
= menu
->frame_y
;
1608 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1609 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1610 int screenW
= scr
->scr_width
;
1611 int screenH
= scr
->scr_height
;
1617 getPointerPosition(scr
, &xroot
, &yroot
);
1620 if (xroot
<= 1 && menuX1
< 0) {
1621 /* scroll to the right */
1622 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX1
));
1624 } else if (xroot
>= screenW
-2 && menuX2
> screenW
-1) {
1625 /* scroll to the left */
1626 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX2
-screenW
-1));
1631 *hamount
= -*hamount
;
1634 if (yroot
<= 1 && menuY1
< 0) {
1636 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY1
));
1638 } else if (yroot
>= screenH
-2 && menuY2
> screenH
-1) {
1640 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY2
-screenH
-2));
1642 *vamount
= -*vamount
;
1648 dragScrollMenuCallback(void *data
)
1650 WMenu
*menu
= (WMenu
*)data
;
1651 WScreen
*scr
= menu
->menu
->screen_ptr
;
1652 WMenu
*parent
= parentMenu(menu
);
1653 int hamount
, vamount
;
1655 int newSelectedEntry
;
1657 getScrollAmount(menu
, &hamount
, &vamount
);
1660 if (hamount
!= 0 || vamount
!= 0) {
1661 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1662 parent
->frame_y
+ vamount
, True
);
1663 if (findMenu(scr
, &x
, &y
)) {
1664 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1665 selectEntry(menu
, newSelectedEntry
);
1667 /* Pointer fell outside of menu. If the selected entry is
1668 * not a submenu, unselect it */
1669 if (menu
->selected_entry
>= 0
1670 && menu
->entries
[menu
->selected_entry
]->cascade
<0)
1671 selectEntry(menu
, -1);
1672 newSelectedEntry
= 0;
1675 /* paranoid check */
1676 if (newSelectedEntry
>= 0) {
1677 /* keep scrolling */
1678 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1679 dragScrollMenuCallback
, menu
);
1684 /* don't need to scroll anymore */
1686 if (findMenu(scr
, &x
, &y
)) {
1687 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1688 selectEntry(menu
, newSelectedEntry
);
1695 scrollMenuCallback(void *data
)
1697 WMenu
*menu
= (WMenu
*)data
;
1698 WMenu
*parent
= parentMenu(menu
);
1699 int hamount
= 0; /* amount to scroll */
1702 getScrollAmount(menu
, &hamount
, &vamount
);
1704 if (hamount
!= 0 || vamount
!= 0) {
1705 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1706 parent
->frame_y
+ vamount
, True
);
1708 /* keep scrolling */
1709 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1710 scrollMenuCallback
, menu
);
1712 /* don't need to scroll anymore */
1719 #define MENU_SCROLL_BORDER 5
1722 isPointNearBoder(WMenu
*menu
, int x
, int y
)
1724 int menuX1
= menu
->frame_x
;
1725 int menuY1
= menu
->frame_y
;
1726 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1727 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1728 int scrXe
= menu
->menu
->screen_ptr
->scr_width
-1;
1729 int scrYe
= menu
->menu
->screen_ptr
->scr_height
-1;
1732 if (x
>= menuX1
&& x
<= menuX2
&& (y
< MENU_SCROLL_BORDER
1733 || y
> scrYe
-MENU_SCROLL_BORDER
))
1735 else if (y
>= menuY1
&& y
<= menuY2
&& (x
< MENU_SCROLL_BORDER
1736 || x
> scrXe
-MENU_SCROLL_BORDER
))
1744 wMenuScroll(WMenu
*menu
, XEvent
*event
)
1747 WMenu
*omenu
= parentMenu(menu
);
1748 WScreen
*scr
= menu
->frame
->screen_ptr
;
1751 int old_frame_x
= omenu
->frame_x
;
1752 int old_frame_y
= omenu
->frame_y
;
1756 puts("Entering menu Scroll");
1759 if ((/*omenu->flags.buttoned &&*/ !wPreferences
.wrap_menus
)
1760 || omenu
->flags
.app_menu
) {
1764 if (!wPreferences
.wrap_menus
)
1765 raiseMenus(omenu
, True
);
1767 raiseMenus(menu
, False
);
1770 scrollMenuCallback(menu
);
1773 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1775 WMNextEvent(dpy
, &ev
);
1780 x
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1781 y
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1783 /* on_border is != 0 if the pointer is between the menu
1784 * and the screen border and is close enough to the border */
1785 on_border
= isPointNearBoder(menu
, x
, y
);
1787 smenu
= wMenuUnderPointer(scr
);
1789 if ((smenu
==NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
)!=omenu
)) {
1794 on_x_edge
= x
<= 1 || x
>= scr
->scr_width
- 2;
1795 on_y_edge
= y
<= 1 || y
>= scr
->scr_height
- 2;
1796 on_border
= on_x_edge
|| on_y_edge
;
1798 if (!on_border
&& !jump_back
) {
1803 if (menu
->timer
&& (smenu
!=menu
|| (!on_y_edge
&& !on_x_edge
))) {
1804 WMDeleteTimerHandler(menu
->timer
);
1812 scrollMenuCallback(menu
);
1815 /* True if we push on title, or drag the omenu to other position */
1816 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1817 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1818 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1819 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1821 smenu
= wMenuUnderPointer(scr
);
1822 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1824 else if (smenu
==omenu
&& on_title
) {
1838 WMDeleteTimerHandler(menu
->timer
);
1843 wMenuMove(omenu
, old_frame_x
, old_frame_y
, True
);
1846 puts("Leaving menu Scroll");
1853 menuExpose(WObjDescriptor
*desc
, XEvent
*event
)
1855 wMenuPaint(desc
->parent
);
1859 int *delayed_select
;
1866 delaySelection(void *data
)
1868 delay_data
*d
= (delay_data
*)data
;
1874 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1875 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1876 entry_no
= getEntryAt(menu
, x
, y
);
1877 selectEntry(menu
, entry_no
);
1879 if (d
->delayed_select
)
1880 *(d
->delayed_select
) = 0;
1885 menuMouseDown(WObjDescriptor
*desc
, XEvent
*event
)
1887 XButtonEvent
*bev
= &event
->xbutton
;
1888 WMenu
*menu
= desc
->parent
;
1890 WScreen
*scr
=menu
->frame
->screen_ptr
;
1891 WMenuEntry
*entry
=NULL
;
1893 int close_on_exit
=0;
1895 int delayed_select
= 0;
1899 int old_frame_x
= 0;
1900 int old_frame_y
= 0;
1901 delay_data d_data
= {NULL
, NULL
, NULL
};
1903 if (!wPreferences
.wrap_menus
) {
1904 smenu
= parentMenu(menu
);
1905 old_frame_x
= smenu
->frame_x
;
1906 old_frame_y
= smenu
->frame_y
;
1908 else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1909 /* This is true if the menu was launched with right click on root window */
1911 d_data
.delayed_select
= &delayed_select
;
1913 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
,
1914 delaySelection
, &d_data
);
1917 wRaiseFrame(menu
->frame
->core
);
1919 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1921 smenu
= findMenu(scr
, &x
, &y
);
1929 if (menu
->flags
.editing
) {
1932 entry_no
= getEntryAt(menu
, x
, y
);
1934 entry
= menu
->entries
[entry_no
];
1936 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
1937 && entry
->flags
.editable
) {
1938 editEntry(smenu
, entry
);
1940 } else if (bev
->state
& ControlMask
) {
1944 if (entry
->flags
.enabled
&& entry
->cascade
>=0 && menu
->cascades
) {
1945 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1947 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&&
1948 menu
->selected_entry
!=entry_no
) {
1949 wMenuUnmap(submenu
);
1951 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1952 selectEntry(menu
, entry_no
);
1953 } else if (!submenu
->flags
.buttoned
) {
1954 selectEntry(menu
, -1);
1957 } else if (!delayed_select
) {
1958 selectEntry(menu
, entry_no
);
1961 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1963 dragScrollMenuCallback(menu
);
1967 prevx
= bev
->x_root
;
1968 prevy
= bev
->y_root
;
1972 XAllowEvents(dpy
, SyncPointer
, CurrentTime
);
1974 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonReleaseMask
1975 |ButtonPressMask
, &ev
);
1978 smenu
= findMenu(scr
, &x
, &y
);
1980 if (smenu
== NULL
) {
1981 /* moved mouse out of menu */
1983 if (!delayed_select
&& d_data
.magic
) {
1984 WMDeleteTimerHandler(d_data
.magic
);
1985 d_data
.magic
= NULL
;
1988 || (menu
->selected_entry
>=0
1989 && menu
->entries
[menu
->selected_entry
]->cascade
>=0)) {
1990 prevx
= ev
.xmotion
.x_root
;
1991 prevy
= ev
.xmotion
.y_root
;
1995 selectEntry(menu
, -1);
1997 prevx
= ev
.xmotion
.x_root
;
1998 prevy
= ev
.xmotion
.y_root
;
2000 } else if (menu
&& menu
!=smenu
2001 && (menu
->selected_entry
<0
2002 || menu
->entries
[menu
->selected_entry
]->cascade
<0)) {
2003 selectEntry(menu
, -1);
2005 if (!delayed_select
&& d_data
.magic
) {
2006 WMDeleteTimerHandler(d_data
.magic
);
2007 d_data
.magic
= NULL
;
2011 /* hysteresis for item selection */
2013 /* check if the motion was to the side, indicating that
2014 * the user may want to cross to a submenu */
2015 if (!delayed_select
&& menu
) {
2017 Bool moved_to_submenu
;/* moved to direction of submenu */
2019 dx
= abs(prevx
- ev
.xmotion
.x_root
);
2021 moved_to_submenu
= False
;
2022 if (dx
> 0 /* if moved enough to the side */
2023 /* maybe a open submenu */
2024 && menu
->selected_entry
>=0
2025 /* moving to the right direction */
2026 && (wPreferences
.align_menus
2027 || ev
.xmotion
.y_root
>= prevy
)) {
2030 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
2032 if (menu
->cascades
[index
]->frame_x
>menu
->frame_x
) {
2033 if (prevx
< ev
.xmotion
.x_root
)
2034 moved_to_submenu
= True
;
2036 if (prevx
> ev
.xmotion
.x_root
)
2037 moved_to_submenu
= True
;
2043 if (menu
!= smenu
) {
2045 WMDeleteTimerHandler(d_data
.magic
);
2047 d_data
.magic
= NULL
;
2048 } else if (moved_to_submenu
) {
2049 /* while we are moving, postpone the selection */
2051 WMDeleteTimerHandler(d_data
.magic
);
2053 d_data
.delayed_select
= NULL
;
2055 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
2058 prevx
= ev
.xmotion
.x_root
;
2059 prevy
= ev
.xmotion
.y_root
;
2063 WMDeleteTimerHandler(d_data
.magic
);
2064 d_data
.magic
= NULL
;
2068 prevx
= ev
.xmotion
.x_root
;
2069 prevy
= ev
.xmotion
.y_root
;
2071 /* pointer crossed menus */
2072 if (menu
&& menu
->timer
) {
2073 WMDeleteTimerHandler(menu
->timer
);
2077 dragScrollMenuCallback(smenu
);
2081 dragScrollMenuCallback(menu
);
2083 if (!delayed_select
) {
2084 entry_no
= getEntryAt(menu
, x
, y
);
2086 entry
= menu
->entries
[entry_no
];
2087 if (entry
->flags
.enabled
&& entry
->cascade
>=0 &&
2089 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
2090 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
2091 && menu
->selected_entry
!=entry_no
) {
2092 wMenuUnmap(submenu
);
2096 selectEntry(menu
, entry_no
);
2104 if (ev
.xbutton
.button
== event
->xbutton
.button
)
2114 if (menu
&& menu
->timer
) {
2115 WMDeleteTimerHandler(menu
->timer
);
2118 if (d_data
.magic
!=NULL
)
2119 WMDeleteTimerHandler(d_data
.magic
);
2121 if (menu
&& menu
->selected_entry
>=0) {
2122 entry
= menu
->entries
[menu
->selected_entry
];
2123 if (entry
->callback
!=NULL
&& entry
->flags
.enabled
2124 && entry
->cascade
< 0) {
2125 /* blink and erase menu selection */
2126 #if (MENU_BLINK_DELAY > 0)
2127 int sel
= menu
->selected_entry
;
2130 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
2131 paintEntry(menu
, sel
, False
);
2133 wusleep(MENU_BLINK_DELAY
);
2134 paintEntry(menu
, sel
, True
);
2136 wusleep(MENU_BLINK_DELAY
);
2139 /* unmap the menu, it's parents and call the callback */
2140 if (!menu
->flags
.buttoned
&&
2141 (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2144 selectEntry(menu
, -1);
2146 (*entry
->callback
)(menu
, entry
);
2148 /* If the user double clicks an entry, the entry will
2149 * be executed twice, which is not good for things like
2150 * the root menu. So, ignore any clicks that were generated
2151 * while the entry was being executed */
2152 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
,
2154 } else if (entry
->callback
!=NULL
&& entry
->cascade
<0) {
2155 selectEntry(menu
, -1);
2157 if (entry
->cascade
>=0 && menu
->cascades
2158 && menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2159 selectEntry(menu
, -1);
2164 if (((WMenu
*)desc
->parent
)->flags
.brother
|| close_on_exit
2166 closeCascade(desc
->parent
);
2168 /* close the cascade windows that should not remain opened */
2169 closeBrotherCascadesOf(desc
->parent
);
2171 if (!wPreferences
.wrap_menus
)
2172 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2177 wMenuMove(WMenu
*menu
, int x
, int y
, int submenus
)
2186 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2188 if (submenus
>0 && menu
->selected_entry
>=0) {
2189 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2191 if (i
>=0 && menu
->cascades
) {
2192 submenu
= menu
->cascades
[i
];
2193 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
2194 if (wPreferences
.align_menus
) {
2195 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2197 wMenuMove(submenu
, x
+ MENUW(menu
),
2198 y
+ submenu
->entry_height
*menu
->selected_entry
,
2203 if (submenus
<0 && menu
->parent
!=NULL
&& menu
->parent
->flags
.mapped
&&
2204 !menu
->parent
->flags
.buttoned
) {
2205 if (wPreferences
.align_menus
) {
2206 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2208 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2209 - menu
->parent
->entry_height
*menu
->parent
->selected_entry
,
2217 changeMenuLevels(WMenu
*menu
, int lower
)
2222 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
2224 wRaiseFrame(menu
->frame
->core
);
2225 menu
->flags
.lowered
= 0;
2227 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2228 wLowerFrame(menu
->frame
->core
);
2229 menu
->flags
.lowered
= 1;
2231 for (i
=0; i
<menu
->cascade_no
; i
++) {
2232 if (menu
->cascades
[i
]
2233 && !menu
->cascades
[i
]->flags
.buttoned
2234 && menu
->cascades
[i
]->flags
.lowered
!=lower
) {
2235 changeMenuLevels(menu
->cascades
[i
], lower
);
2243 menuTitleDoubleClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2248 if (event
->xbutton
.state
& MOD_MASK
) {
2249 if (menu
->flags
.lowered
) {
2254 changeMenuLevels(menu
, lower
);
2260 menuTitleMouseDown(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2265 int x
=menu
->frame_x
, y
=menu
->frame_y
;
2266 int dx
=event
->xbutton
.x_root
, dy
=event
->xbutton
.y_root
;
2271 printf("Moving menu\n");
2274 /* can't touch the menu copy */
2275 if (menu
->flags
.brother
)
2278 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2281 if (event
->xbutton
.state
& MOD_MASK
) {
2282 wLowerFrame(menu
->frame
->core
);
2285 wRaiseFrame(menu
->frame
->core
);
2290 /* lower/raise all submenus */
2292 if (tmp
->selected_entry
>=0 && tmp
->cascades
2293 && tmp
->entries
[tmp
->selected_entry
]->cascade
>=0) {
2294 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2295 if (!tmp
|| !tmp
->flags
.mapped
)
2298 wLowerFrame(tmp
->frame
->core
);
2300 wRaiseFrame(tmp
->frame
->core
);
2306 /* tear off the menu if it's a root menu or a cascade
2308 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
2309 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2310 menu
->flags
.buttoned
=1;
2311 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2313 /* turn off selected menu entry in parent menu */
2314 selectEntry(menu
->parent
, -1);
2316 /* make parent map the copy in place of the original */
2317 for (i
=0; i
<menu
->parent
->cascade_no
; i
++) {
2318 if (menu
->parent
->cascades
[i
] == menu
) {
2319 menu
->parent
->cascades
[i
] = menu
->brother
;
2328 WMMaskEvent(dpy
, ButtonMotionMask
|ButtonReleaseMask
|ButtonPressMask
2329 |ExposureMask
, &ev
);
2333 x
+= ev
.xmotion
.x_root
- dx
;
2334 y
+= ev
.xmotion
.y_root
- dy
;
2335 dx
= ev
.xmotion
.x_root
;
2336 dy
= ev
.xmotion
.y_root
;
2337 wMenuMove(menu
, x
, y
, True
);
2339 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2340 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2342 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2343 ButtonMotionMask
|ButtonReleaseMask
2345 GrabModeAsync
, GrabModeAsync
, None
,
2346 wCursor
[WCUR_MOVE
], CurrentTime
);
2355 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2358 printf("End menu move\n");
2360 XUngrabPointer(dpy
, CurrentTime
);
2371 *----------------------------------------------------------------------
2373 * Handles mouse click on the close button of menus. The menu is
2374 * closed when the button is clicked.
2377 * The closed menu is reinserted at it's parent menus
2379 *----------------------------------------------------------------------
2382 menuCloseClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2384 WMenu
*menu
= (WMenu
*)data
;
2385 WMenu
*parent
= menu
->parent
;
2389 for (i
=0; i
<parent
->cascade_no
; i
++) {
2390 /* find the entry that points to the copy */
2391 if (parent
->cascades
[i
] == menu
->brother
) {
2392 /* make it point to the original */
2393 parent
->cascades
[i
] = menu
;
2394 menu
->parent
= parent
;
2404 wMenuSaveState(WScreen
*scr
)
2406 proplist_t menus
, key
, value
;
2410 menus
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
2413 if (scr
->root_menu
&& scr
->root_menu
->flags
.buttoned
) {
2414 sprintf(buffer
, "%i,%i", scr
->root_menu
->frame_x
,
2415 scr
->root_menu
->frame_y
);
2416 key
= PLMakeString("RootMenu");
2417 value
= PLMakeString(buffer
);
2418 PLInsertDictionaryEntry(menus
, key
, value
);
2424 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2425 sprintf(buffer
, "%i,%i", scr
->switch_menu
->frame_x
,
2426 scr
->switch_menu
->frame_y
);
2427 key
= PLMakeString("SwitchMenu");
2428 value
= PLMakeString(buffer
);
2429 PLInsertDictionaryEntry(menus
, key
, value
);
2435 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2436 sprintf(buffer
, "%i,%i", scr
->workspace_menu
->frame_x
,
2437 scr
->workspace_menu
->frame_y
);
2438 key
= PLMakeString("WorkspaceMenu");
2439 value
= PLMakeString(buffer
);
2440 PLInsertDictionaryEntry(menus
, key
, value
);
2447 key
= PLMakeString("Menus");
2448 PLInsertDictionaryEntry(scr
->session_state
, key
, menus
);
2455 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2459 restoreMenu(WScreen
*scr
, proplist_t menu
, int which
)
2462 WMenu
*pmenu
= NULL
;
2467 if (!PLIsString(menu
)) {
2468 COMPLAIN("Position");
2472 if (sscanf(PLGetString(menu
), "%i,%i", &x
, &y
)!=2)
2473 COMPLAIN("Position");
2476 if (which
& WSS_ROOTMENU
) {
2477 OpenRootMenu(scr
, x
, y
, False
);
2478 pmenu
= scr
->root_menu
;
2479 } else if (which
& WSS_SWITCHMENU
) {
2480 OpenSwitchMenu(scr
, x
, y
, False
);
2481 pmenu
= scr
->switch_menu
;
2484 if (which
& WSS_WSMENU
) {
2485 OpenWorkspaceMenu(scr
, x
, y
);
2486 pmenu
= scr
->workspace_menu
;
2487 if (pmenu
->parent
) {
2488 /* make parent map the copy in place of the original */
2489 for (i
=0; i
<pmenu
->parent
->cascade_no
; i
++) {
2490 if (pmenu
->parent
->cascades
[i
] == pmenu
) {
2491 pmenu
->parent
->cascades
[i
] = pmenu
->brother
;
2499 int width
= MENUW(pmenu
);
2500 int height
= MENUH(pmenu
);
2502 x
= (x
< -width
) ? 0 : x
;
2503 x
= (x
> scr
->scr_width
) ? scr
->scr_width
- width
: x
;
2504 y
= (y
< 0) ? 0 : y
;
2505 y
= (y
> scr
->scr_height
) ? scr
->scr_height
- height
: y
;
2506 wMenuMove(pmenu
, x
, y
, True
);
2507 pmenu
->flags
.buttoned
= 1;
2508 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2516 wMenuRestoreState(WScreen
*scr
)
2518 proplist_t menus
, menu
, key
, rkey
, skey
, wkey
;
2520 key
= PLMakeString("Menus");
2521 menus
= PLGetDictionaryEntry(scr
->session_state
, key
);
2529 rkey
= PLMakeString("RootMenu");
2530 skey
= PLMakeString("SwitchMenu");
2531 wkey
= PLMakeString("WorkspaceMenu");
2532 menu
= PLGetDictionaryEntry(menus
, rkey
);
2533 restoreMenu(scr
, menu
, WSS_ROOTMENU
);
2534 menu
= PLGetDictionaryEntry(menus
, skey
);
2535 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2536 menu
= PLGetDictionaryEntry(menus
, wkey
);
2537 restoreMenu(scr
, menu
, WSS_WSMENU
);
2546 OpenWorkspaceMenu(WScreen
*scr
, int x
, int y
)
2548 WMenu
*menu
, *parent
;
2552 if (!scr
->root_menu
) {
2553 OpenRootMenu(scr
, scr
->scr_width
*2, 0, False
);
2554 wMenuUnmap(scr
->root_menu
);
2557 menu
= scr
->workspace_menu
;
2559 if (menu
->flags
.mapped
) {
2560 if (!menu
->flags
.buttoned
) {
2562 parent
= menu
->parent
;
2563 if (parent
&& parent
->selected_entry
>= 0) {
2564 entry
= parent
->entries
[parent
->selected_entry
];
2565 if (parent
->cascades
[entry
->cascade
] == menu
) {
2566 selectEntry(parent
, -1);
2567 wMenuMapAt(menu
, x
, y
, False
);
2571 wRaiseFrame(menu
->frame
->core
);
2572 wMenuMapCopyAt(menu
, x
, y
);
2576 wMenuMapAt(menu
, x
, y
, False
);