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
6 * Copyright (c) 1997, 1998 Dan Pascu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 #include <X11/Xutil.h>
28 #include <X11/keysym.h>
35 #include "WindowMaker.h"
45 /****** Global Variables ******/
47 extern Cursor wCursor
[WCUR_LAST
];
49 extern XContext wWinContext
;
51 extern WPreferences wPreferences
;
53 #define MOD_MASK wPreferences.modifier_mask
55 #define MENU_SCROLL_STEP menuScrollParameters[(int)wPreferences.menu_scroll_speed].steps
56 #define MENU_SCROLL_DELAY menuScrollParameters[(int)wPreferences.menu_scroll_speed].delay
60 #define MENUW(m) ((m)->frame->core->width+2*FRAME_BORDER_WIDTH)
61 #define MENUH(m) ((m)->frame->core->height+2*FRAME_BORDER_WIDTH)
64 /***** Local Stuff ******/
69 } menuScrollParameters
[5] = {
70 {MENU_SCROLL_STEPS_UF
, MENU_SCROLL_DELAY_UF
},
71 {MENU_SCROLL_STEPS_F
, MENU_SCROLL_DELAY_F
},
72 {MENU_SCROLL_STEPS_M
, MENU_SCROLL_DELAY_M
},
73 {MENU_SCROLL_STEPS_S
, MENU_SCROLL_DELAY_S
},
74 {MENU_SCROLL_STEPS_U
, MENU_SCROLL_DELAY_U
}};
77 static void menuMouseDown(WObjDescriptor
*desc
, XEvent
*event
);
78 static void menuExpose(WObjDescriptor
*desc
, XEvent
*event
);
80 static void menuTitleDoubleClick(WCoreWindow
*sender
, void *data
, XEvent
*event
);
81 static void menuTitleMouseDown(WCoreWindow
*sender
, void *data
, XEvent
*event
);
83 static void menuCloseClick(WCoreWindow
*sender
, void *data
, XEvent
*event
);
85 static void updateTexture(WMenu
*menu
);
88 static void selectEntry(WMenu
*menu
, int entry_no
);
89 static void closeCascade(WMenu
*menu
);
92 /****** Notification Observers ******/
95 appearanceObserver(void *self
, WMNotification
*notif
)
97 WMenu
*menu
= (WMenu
*)self
;
98 int flags
= (int)WMGetNotificationClientData(notif
);
100 if (!menu
->flags
.realized
)
103 if (WMGetNotificationName(notif
) == WNMenuAppearanceSettingsChanged
) {
104 if (flags
& WFontSettings
) {
105 menu
->flags
.realized
= 0;
108 if (flags
& WTextureSettings
) {
111 if (flags
& (WTextureSettings
|WColorSettings
)) {
114 } else if (menu
->flags
.titled
) {
116 if (flags
& WFontSettings
) {
117 menu
->flags
.realized
= 0;
120 if (flags
& WTextureSettings
) {
121 menu
->frame
->flags
.need_texture_remake
= 1;
123 if (flags
& (WColorSettings
|WTextureSettings
))
124 wFrameWindowPaint(menu
->frame
);
128 /************************************/
132 *----------------------------------------------------------------------
134 * Creates a new empty menu with the specified title. If main_menu
135 * is True, the created menu will be a main menu, which has some special
136 * properties such as being placed over other normal menus.
137 * If title is NULL, the menu will have no titlebar.
141 *----------------------------------------------------------------------
144 wMenuCreate(WScreen
*screen
, char *title
, int main_menu
)
147 static int brother
=0;
150 menu
= wmalloc(sizeof(WMenu
));
152 memset(menu
, 0, sizeof(WMenu
));
154 #ifdef SINGLE_MENULEVEL
155 tmp
= WMSubmenuLevel
;
157 tmp
= (main_menu
? WMMainMenuLevel
: WMSubmenuLevel
);
160 flags
= WFF_SINGLE_STATE
;
162 flags
|= WFF_TITLEBAR
|WFF_RIGHT_BUTTON
;
163 menu
->flags
.titled
= 1;
166 wFrameWindowCreate(screen
, tmp
, 8, 2, 1, 1, flags
,
167 screen
->menu_title_texture
, NULL
,
168 screen
->menu_title_pixel
, &screen
->menu_title_gc
,
169 &screen
->menu_title_font
);
171 menu
->frame
->core
->descriptor
.parent
= menu
;
172 menu
->frame
->core
->descriptor
.parent_type
= WCLASS_MENU
;
173 menu
->frame
->core
->descriptor
.handle_mousedown
= menuMouseDown
;
175 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
178 menu
->frame
->title
= wstrdup(title
);
181 menu
->frame
->flags
.justification
= WTJ_LEFT
;
183 menu
->frame
->rbutton_image
= screen
->b_pixmaps
[WBUT_CLOSE
];
186 menu
->alloced_entries
= 0;
187 menu
->selected_entry
= -1;
188 menu
->entries
= NULL
;
190 menu
->frame_x
= screen
->app_menu_x
;
191 menu
->frame_y
= screen
->app_menu_y
;
193 menu
->frame
->child
= menu
;
195 menu
->flags
.lowered
= 0;
199 /* setup object descriptors */
200 menu
->frame
->on_mousedown_titlebar
= menuTitleMouseDown
;
201 menu
->frame
->on_dblclick_titlebar
= menuTitleDoubleClick
;
204 menu
->frame
->on_click_right
= menuCloseClick
;
207 menu
->menu
= wCoreCreate(menu
->frame
->core
, 0, menu
->frame
->top_width
,
208 menu
->frame
->core
->width
, 10);
210 menu
->menu
->descriptor
.parent
= menu
;
211 menu
->menu
->descriptor
.parent_type
= WCLASS_MENU
;
212 menu
->menu
->descriptor
.handle_expose
= menuExpose
;
213 menu
->menu
->descriptor
.handle_mousedown
= menuMouseDown
;
215 menu
->menu_texture_data
= None
;
217 XMapWindow(dpy
, menu
->menu
->window
);
223 menu
->brother
= wMenuCreate(screen
, title
, main_menu
);
225 menu
->brother
->flags
.brother
= 1;
226 menu
->brother
->brother
= menu
;
228 WMAddNotificationObserver(appearanceObserver
, menu
,
229 WNMenuTitleAppearanceSettingsChanged
, menu
);
231 WMAddNotificationObserver(appearanceObserver
, menu
,
232 WNMenuAppearanceSettingsChanged
, menu
);
242 wMenuCreateForApp(WScreen
*screen
, char *title
, int main_menu
)
246 menu
= wMenuCreate(screen
, title
, main_menu
);
249 menu
->flags
.app_menu
= 1;
250 menu
->brother
->flags
.app_menu
= 1;
258 insertEntry(WMenu
*menu
, WMenuEntry
*entry
, int index
)
262 for (i
= menu
->entry_no
-1; i
>= index
; i
--) {
263 menu
->entries
[i
]->order
++;
264 menu
->entries
[i
+1] = menu
->entries
[i
];
266 menu
->entries
[index
] = entry
;
271 wMenuInsertCallback(WMenu
*menu
, int index
, char *text
,
272 void (*callback
)(WMenu
*menu
, WMenuEntry
*entry
),
279 printf("Passed NULL as menu parameter to wMenuAddCallback() \n");
284 assert(menu
->flags
.brother
==0);
285 menu
->flags
.realized
= 0;
286 menu
->brother
->flags
.realized
= 0;
288 /* reallocate array if it's too small */
289 if (menu
->entry_no
>= menu
->alloced_entries
) {
292 puts("doing wrealloc()");
294 tmp
= wrealloc(menu
->entries
,
295 sizeof(WMenuEntry
)*(menu
->alloced_entries
+5));
297 wwarning(_("wrealloc() failed while trying to add menu item"));
302 menu
->alloced_entries
+= 5;
304 menu
->brother
->entries
= tmp
;
305 menu
->brother
->alloced_entries
= menu
->alloced_entries
;
307 entry
= wmalloc(sizeof(WMenuEntry
));
308 memset(entry
, 0, sizeof(WMenuEntry
));
309 entry
->flags
.enabled
= 1;
310 entry
->text
= wstrdup(text
);
312 entry
->clientdata
= clientdata
;
313 entry
->callback
= callback
;
314 if (index
<0 || index
>=menu
->entry_no
) {
315 entry
->order
= menu
->entry_no
;
316 menu
->entries
[menu
->entry_no
] = entry
;
318 entry
->order
= index
;
319 insertEntry(menu
, entry
, index
);
323 menu
->brother
->entry_no
= menu
->entry_no
;
331 wMenuEntrySetCascade(WMenu
*menu
, WMenuEntry
*entry
, WMenu
*cascade
)
333 WMenu
*brother
= menu
->brother
;
336 assert(menu
->flags
.brother
==0);
338 if (entry
->cascade
>=0) {
339 menu
->flags
.realized
= 0;
340 brother
->flags
.realized
= 0;
343 cascade
->parent
= menu
;
345 cascade
->brother
->parent
= brother
;
348 for (i
=0; i
<menu
->cascade_no
; i
++) {
349 if (menu
->cascades
[i
]==NULL
) {
350 menu
->cascades
[i
] = cascade
;
351 brother
->cascades
[i
] = cascade
->brother
;
358 entry
->cascade
= menu
->cascade_no
;
360 menu
->cascades
= wrealloc(menu
->cascades
,
361 sizeof(WMenu
)*(menu
->cascade_no
+1));
362 menu
->cascades
[menu
->cascade_no
++] = cascade
;
365 brother
->cascades
= wrealloc(brother
->cascades
,
366 sizeof(WMenu
)*(brother
->cascade_no
+1));
367 brother
->cascades
[brother
->cascade_no
++] = cascade
->brother
;
371 if (menu
->flags
.lowered
) {
373 cascade
->flags
.lowered
= 1;
374 ChangeStackingLevel(cascade
->frame
->core
, WMNormalLevel
);
376 cascade
->brother
->flags
.lowered
= 1;
377 ChangeStackingLevel(cascade
->brother
->frame
->core
, WMNormalLevel
);
380 if (!menu
->flags
.realized
)
386 wMenuEntryRemoveCascade(WMenu
*menu
, WMenuEntry
*entry
)
388 assert(menu
->flags
.brother
==0);
390 /* destroy cascade menu */
391 if (entry
->cascade
>=0 && menu
->cascades
392 && menu
->cascades
[entry
->cascade
]!=NULL
) {
394 wMenuDestroy(menu
->cascades
[entry
->cascade
], True
);
396 menu
->cascades
[entry
->cascade
] = NULL
;
397 menu
->brother
->cascades
[entry
->cascade
] = NULL
;
405 wMenuRemoveItem(WMenu
*menu
, int index
)
409 if (menu
->flags
.brother
) {
410 wMenuRemoveItem(menu
->brother
, index
);
414 if (index
>=menu
->entry_no
) return;
416 /* destroy cascade menu */
417 wMenuEntryRemoveCascade(menu
, menu
->entries
[index
]);
419 /* destroy unshared data */
421 if (menu
->entries
[index
]->text
)
422 free(menu
->entries
[index
]->text
);
424 if (menu
->entries
[index
]->rtext
)
425 free(menu
->entries
[index
]->rtext
);
427 if (menu
->entries
[index
]->free_cdata
&& menu
->entries
[index
]->clientdata
)
428 (*menu
->entries
[index
]->free_cdata
)(menu
->entries
[index
]->clientdata
);
430 free(menu
->entries
[index
]);
432 for (i
=index
; i
<menu
->entry_no
-1; i
++) {
433 menu
->entries
[i
+1]->order
--;
434 menu
->entries
[i
]=menu
->entries
[i
+1];
437 menu
->brother
->entry_no
--;
442 renderTexture(WMenu
*menu
)
450 WScreen
*scr
= menu
->menu
->screen_ptr
;
451 WTexture
*texture
= scr
->menu_item_texture
;
453 if (wPreferences
.menu_style
== MS_NORMAL
) {
454 img
= wTextureRenderImage(texture
, menu
->menu
->width
,
455 menu
->entry_height
, WREL_MENUENTRY
);
457 img
= wTextureRenderImage(texture
, menu
->menu
->width
,
458 menu
->menu
->height
+1, WREL_MENUENTRY
);
461 wwarning(_("could not render texture: %s"),
462 RMessageForError(RErrorCode
));
467 if (wPreferences
.menu_style
== MS_SINGLE_TEXTURE
) {
469 light
.red
= light
.green
= light
.blue
= 80;
472 dark
.red
= dark
.green
= dark
.blue
= 0;
475 mid
.red
= mid
.green
= mid
.blue
= 40;
477 for (i
= 1; i
< menu
->entry_no
; i
++) {
478 ROperateLine(img
, RSubtractOperation
, 0, i
*menu
->entry_height
-2,
479 menu
->menu
->width
-1, i
*menu
->entry_height
-2, &mid
);
481 RDrawLine(img
, 0, i
*menu
->entry_height
-1,
482 menu
->menu
->width
-1, i
*menu
->entry_height
-1, &dark
);
484 ROperateLine(img
, RAddOperation
, 0, i
*menu
->entry_height
,
485 menu
->menu
->width
-1, i
*menu
->entry_height
,
489 if (!RConvertImage(scr
->rcontext
, img
, &pix
)) {
490 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode
));
499 updateTexture(WMenu
*menu
)
501 WScreen
*scr
= menu
->menu
->screen_ptr
;
503 /* setup background texture */
504 if (scr
->menu_item_texture
->any
.type
!= WTEX_SOLID
) {
505 if (!menu
->flags
.brother
) {
506 FREE_PIXMAP(menu
->menu_texture_data
);
508 menu
->menu_texture_data
= renderTexture(menu
);
510 XSetWindowBackgroundPixmap(dpy
, menu
->menu
->window
,
511 menu
->menu_texture_data
);
512 XClearWindow(dpy
, menu
->menu
->window
);
514 XSetWindowBackgroundPixmap(dpy
, menu
->brother
->menu
->window
,
515 menu
->menu_texture_data
);
516 XClearWindow(dpy
, menu
->brother
->menu
->window
);
519 XSetWindowBackground(dpy
, menu
->menu
->window
,
520 scr
->menu_item_texture
->any
.color
.pixel
);
521 XClearWindow(dpy
, menu
->menu
->window
);
527 wMenuRealize(WMenu
*menu
)
530 int width
, rwidth
, mrwidth
, mwidth
;
531 int theight
, twidth
, eheight
;
532 WScreen
*scr
= menu
->frame
->screen_ptr
;
533 static int brother_done
=0;
538 wMenuRealize(menu
->brother
);
542 flags
= WFF_SINGLE_STATE
;
543 if (menu
->flags
.titled
)
544 flags
|= WFF_TITLEBAR
|WFF_RIGHT_BUTTON
;
546 wFrameWindowUpdateBorders(menu
->frame
, flags
);
548 if (menu
->flags
.titled
) {
549 twidth
= wTextWidth(scr
->menu_title_font
->font
, menu
->frame
->title
,
550 strlen(menu
->frame
->title
));
551 theight
= menu
->frame
->top_width
;
552 twidth
+= theight
+ (wPreferences
.new_style
? 16 : 8);
557 eheight
= scr
->menu_entry_font
->height
+ 6;
558 menu
->entry_height
= eheight
;
561 for (i
=0; i
<menu
->entry_no
; i
++) {
564 /* search widest text */
565 text
= menu
->entries
[i
]->text
;
566 width
= wTextWidth(scr
->menu_entry_font
->font
, text
, strlen(text
))+10;
568 if (menu
->entries
[i
]->flags
.indicator
) {
569 width
+= MENU_INDICATOR_SPACE
;
575 /* search widest text on right */
576 text
= menu
->entries
[i
]->rtext
;
578 rwidth
= wTextWidth(scr
->menu_entry_font
->font
, text
,
580 else if (menu
->entries
[i
]->cascade
>=0)
585 if (rwidth
> mrwidth
)
594 wCoreConfigure(menu
->menu
, 0, theight
, mwidth
, menu
->entry_no
*eheight
-1);
596 wFrameWindowResize(menu
->frame
, mwidth
, menu
->entry_no
*eheight
-1
597 + menu
->frame
->top_width
+ menu
->frame
->bottom_width
);
602 menu
->flags
.realized
= 1;
604 if (menu
->flags
.mapped
)
606 if (menu
->brother
->flags
.mapped
)
607 wMenuPaint(menu
->brother
);
612 wMenuDestroy(WMenu
*menu
, int recurse
)
616 WMRemoveNotificationObserver(menu
);
618 /* remove any pending timers */
620 WMDeleteTimerHandler(menu
->timer
);
623 /* call destroy handler */
624 if (menu
->on_destroy
)
625 (*menu
->on_destroy
)(menu
);
627 /* Destroy items if this menu own them. If this is the "brother" menu,
628 * leave them alone as it is shared by them.
630 if (!menu
->flags
.brother
) {
631 for (i
=0; i
<menu
->entry_no
; i
++) {
633 free(menu
->entries
[i
]->text
);
635 if (menu
->entries
[i
]->rtext
)
636 free(menu
->entries
[i
]->rtext
);
639 if (menu
->entries
[i
]->instances
){
640 PLRelease(menu
->entries
[i
]->instances
);
642 #endif /* USER_MENU */
644 if (menu
->entries
[i
]->free_cdata
&& menu
->entries
[i
]->clientdata
) {
645 (*menu
->entries
[i
]->free_cdata
)(menu
->entries
[i
]->clientdata
);
647 free(menu
->entries
[i
]);
651 for (i
=0; i
<menu
->cascade_no
; i
++) {
652 if (menu
->cascades
[i
]) {
653 if (menu
->cascades
[i
]->flags
.brother
)
654 wMenuDestroy(menu
->cascades
[i
]->brother
, recurse
);
656 wMenuDestroy(menu
->cascades
[i
], recurse
);
666 FREE_PIXMAP(menu
->menu_texture_data
);
669 free(menu
->cascades
);
671 wCoreDestroy(menu
->menu
);
672 wFrameWindowDestroy(menu
->frame
);
674 /* destroy copy of this menu */
675 if (!menu
->flags
.brother
&& menu
->brother
)
676 wMenuDestroy(menu
->brother
, False
);
688 drawFrame(WScreen
*scr
, Window win
, int y
, int w
, int h
, int type
)
694 segs
[i
].x1
= segs
[i
].x2
= w
-1;
696 segs
[i
].y2
= y
+ h
- 1;
698 if (type
!= F_TOP
&& type
!= F_NONE
) {
700 segs
[i
].y1
= segs
[i
].y2
= y
+ h
-2;
704 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->dim_gc
, segs
, i
);
710 segs
[i
].y2
= y
+ h
- 1;
712 if (type
!= F_BOTTOM
&& type
!= F_NONE
) {
719 XDrawSegments(dpy
, win
, scr
->menu_item_auxtexture
->light_gc
, segs
, i
);
721 if (type
!= F_TOP
&& type
!= F_NONE
)
722 XDrawLine(dpy
, win
, scr
->menu_item_auxtexture
->dark_gc
, 0, y
+h
-1,
728 paintEntry(WMenu
*menu
, int index
, int selected
)
732 GC light
, dim
, dark
, textGC
;
733 WScreen
*scr
=menu
->frame
->screen_ptr
;
734 Window win
= menu
->menu
->window
;
735 WMenuEntry
*entry
=menu
->entries
[index
];
737 if (!menu
->flags
.realized
) return;
738 h
= menu
->entry_height
;
739 w
= menu
->menu
->width
;
742 light
= scr
->menu_item_auxtexture
->light_gc
;
743 dim
= scr
->menu_item_auxtexture
->dim_gc
;
744 dark
= scr
->menu_item_auxtexture
->dark_gc
;
746 if (wPreferences
.menu_style
== MS_FLAT
&& menu
->entry_no
> 1) {
749 else if (index
== menu
->entry_no
- 1)
757 /* paint background */
759 XSetForeground(dpy
, scr
->select_menu_gc
, scr
->select_pixel
);
760 XFillRectangle(dpy
, win
, scr
->select_menu_gc
, 1, y
+1, w
-2, h
-3);
761 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
)
762 drawFrame(scr
, win
, y
, w
, h
, type
);
764 if (scr
->menu_item_texture
->any
.type
== WTEX_SOLID
) {
765 XClearArea(dpy
, win
, 0, y
+ 1, w
- 1, h
- 3, False
);
767 drawFrame(scr
, win
, y
, w
, h
, type
);
769 XClearArea(dpy
, win
, 0, y
, w
, h
, False
);
774 textGC
= scr
->select_menu_gc
;
775 if (entry
->flags
.enabled
)
776 XSetForeground(dpy
, textGC
, scr
->select_text_pixel
);
778 XSetForeground(dpy
, textGC
, scr
->dtext_pixel
);
779 } else if (!entry
->flags
.enabled
) {
780 textGC
= scr
->disabled_menu_entry_gc
;
782 textGC
= scr
->menu_entry_gc
;
786 if (entry
->flags
.indicator
)
787 x
+= MENU_INDICATOR_SPACE
+ 2;
789 wDrawString(win
, scr
->menu_entry_font
,
790 textGC
, x
, 3+y
+scr
->menu_entry_font
->y
, entry
->text
,
791 strlen(entry
->text
));
793 if (entry
->cascade
>=0) {
794 /* draw the cascade indicator */
795 XDrawLine(dpy
,win
,dim
, w
-11, y
+6, w
-6, y
+h
/2-1);
796 XDrawLine(dpy
,win
,light
, w
-11, y
+h
-8, w
-6, y
+h
/2-1);
797 XDrawLine(dpy
,win
,dark
, w
-12, y
+6, w
-12, y
+h
-8);
801 if (entry
->flags
.indicator
&& entry
->flags
.indicator_on
) {
806 switch (entry
->flags
.indicator_type
) {
808 indicator
= scr
->menu_check_indicator
;
811 indicator
= scr
->menu_mini_indicator
;
814 indicator
= scr
->menu_hide_indicator
;
817 indicator
= scr
->menu_shade_indicator
;
821 indicator
= scr
->menu_radio_indicator
;
825 iw
= indicator
->width
;
826 ih
= indicator
->height
;
827 XSetClipMask(dpy
, scr
->copy_gc
, indicator
->mask
);
828 XSetClipOrigin(dpy
, scr
->copy_gc
, 5, y
+(h
-ih
)/2);
830 XSetForeground(dpy
, scr
->copy_gc
, scr
->black_pixel
);
832 XSetForeground(dpy
, scr
->copy_gc
, scr
->mtext_pixel
);
833 XFillRectangle(dpy
, win
, scr
->copy_gc
, 5, y
+(h
-ih
)/2, iw
, ih
);
835 XCopyArea(dpy, indicator->image, win, scr->copy_gc, 0, 0,
836 iw, ih, 5, y+(h-ih)/2);
838 XSetClipOrigin(dpy
, scr
->copy_gc
, 0, 0);
841 /* draw right text */
843 if (entry
->rtext
&& entry
->cascade
<0) {
844 tw
= wTextWidth(scr
->menu_entry_font
->font
, entry
->rtext
,
845 strlen(entry
->rtext
));
847 wDrawString(win
, scr
->menu_entry_font
, textGC
, w
-6-tw
,
848 3+y
+scr
->menu_entry_font
->y
, entry
->rtext
,
849 strlen(entry
->rtext
));
855 move_menus(WMenu
*menu
, int x
, int y
)
857 while (menu
->parent
) {
860 if (!wPreferences
.align_menus
&& menu
->selected_entry
>=0) {
861 y
-= menu
->selected_entry
*menu
->entry_height
;
864 wMenuMove(menu
, x
, y
, True
);
868 makeVisible(WMenu
*menu
)
870 WScreen
*scr
= menu
->frame
->screen_ptr
;
871 int x1
, y1
, x2
, y2
, new_x
, new_y
, move
;
873 if (menu
->entry_no
<0) return;
876 y1
= menu
->frame_y
+menu
->frame
->top_width
877 + menu
->selected_entry
*menu
->entry_height
;
878 x2
= x1
+ MENUW(menu
);
879 y2
= y1
+ menu
->entry_height
;
888 } else if (x2
>= scr
->scr_width
) {
889 new_x
= scr
->scr_width
- MENUW(menu
) - 1;
896 } else if (y2
>= scr
->scr_height
) {
897 new_y
= scr
->scr_height
- menu
->entry_height
- 1;
901 new_y
= new_y
- menu
->frame
->top_width
902 - menu
->selected_entry
*menu
->entry_height
;
903 move_menus(menu
, new_x
, new_y
);
908 check_key(WMenu
*menu
, XKeyEvent
*event
)
913 if (XLookupString(event
, buffer
, 32, NULL
, NULL
)<1)
916 ch
= toupper(buffer
[0]);
918 s
= (menu
->selected_entry
>=0 ? menu
->selected_entry
+1 : 0);
921 for (i
=s
; i
<menu
->entry_no
; i
++) {
922 if (ch
==toupper(menu
->entries
[i
]->text
[0])) {
926 /* no match. Retry from start, if previous started from a selected entry */
936 keyboardMenu(WMenu
*menu
)
939 KeySym ksym
=NoSymbol
;
943 int old_pos_x
= menu
->frame_x
;
944 int old_pos_y
= menu
->frame_y
;
945 int new_x
= old_pos_x
, new_y
= old_pos_y
;
946 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
947 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
949 if (menu
->flags
.editing
)
953 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
, GrabModeAsync
,
954 GrabModeAsync
, CurrentTime
);
956 if (menu
->frame_y
+menu
->frame
->top_width
>= scr_height
)
957 new_y
= scr_height
- menu
->frame
->top_width
;
959 if (menu
->frame_x
+MENUW(menu
) >= scr_width
)
960 new_x
= scr_width
-MENUW(menu
)-1;
962 move_menus(menu
, new_x
, new_y
);
964 while (!done
&& menu
->flags
.mapped
) {
965 XAllowEvents(dpy
, AsyncKeyboard
, CurrentTime
);
966 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonPressMask
967 |ButtonReleaseMask
|KeyPressMask
|KeyReleaseMask
968 |SubstructureNotifyMask
, &event
);
970 switch (event
.type
) {
972 ksym
= XLookupKeysym(&event
.xkey
, 0);
980 selectEntry(menu
, 0);
986 selectEntry(menu
, menu
->entry_no
-1);
995 if (menu
->selected_entry
<= 0)
996 selectEntry(menu
, menu
->entry_no
-1);
998 selectEntry(menu
, menu
->selected_entry
-1);
1003 #ifdef ARROWLESS_KBD
1007 if (menu
->selected_entry
<0)
1008 selectEntry(menu
, 0);
1009 else if (menu
->selected_entry
== menu
->entry_no
-1)
1010 selectEntry(menu
, 0);
1011 else if (menu
->selected_entry
< menu
->entry_no
-1)
1012 selectEntry(menu
, menu
->selected_entry
+1);
1017 #ifdef ARROWLESS_KBD
1021 if (menu
->selected_entry
>=0) {
1023 entry
= menu
->entries
[menu
->selected_entry
];
1025 if (entry
->cascade
>= 0 && menu
->cascades
1026 && menu
->cascades
[entry
->cascade
]->entry_no
> 0) {
1028 XUngrabKeyboard(dpy
, CurrentTime
);
1030 selectEntry(menu
->cascades
[entry
->cascade
], 0);
1031 if (!keyboardMenu(menu
->cascades
[entry
->cascade
]))
1034 XGrabKeyboard(dpy
, menu
->frame
->core
->window
, True
,
1035 GrabModeAsync
, GrabModeAsync
,
1042 #ifdef ARROWLESS_KBD
1046 if (menu
->parent
!=NULL
&& menu
->parent
->selected_entry
>=0) {
1047 selectEntry(menu
, -1);
1048 move_menus(menu
, old_pos_x
, old_pos_y
);
1058 index
= check_key(menu
, &event
.xkey
);
1060 selectEntry(menu
, index
);
1066 if (event
.type
==ButtonPress
)
1069 WMHandleEvent(&event
);
1073 XUngrabKeyboard(dpy
, CurrentTime
);
1075 if (done
==2 && menu
->selected_entry
>=0) {
1076 entry
= menu
->entries
[menu
->selected_entry
];
1081 if (entry
&& entry
->callback
!=NULL
&& entry
->flags
.enabled
1082 && entry
->cascade
< 0) {
1083 #if (MENU_BLINK_COUNT > 0)
1084 int sel
= menu
->selected_entry
;
1087 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
1088 paintEntry(menu
, sel
, False
);
1090 wusleep(MENU_BLINK_DELAY
);
1091 paintEntry(menu
, sel
, True
);
1093 wusleep(MENU_BLINK_DELAY
);
1096 selectEntry(menu
, -1);
1098 if (!menu
->flags
.buttoned
) {
1100 move_menus(menu
, old_pos_x
, old_pos_y
);
1104 (*entry
->callback
)(menu
, entry
);
1106 if (!menu
->flags
.buttoned
) {
1108 move_menus(menu
, old_pos_x
, old_pos_y
);
1110 selectEntry(menu
, -1);
1114 /* returns True if returning from a submenu to a parent menu,
1115 * False if exiting from menu */
1121 wMenuMapAt(WMenu
*menu
, int x
, int y
, int keyboard
)
1123 int scr_width
= menu
->frame
->screen_ptr
->scr_width
;
1124 int scr_height
= menu
->frame
->screen_ptr
->scr_height
;
1126 if (!menu
->flags
.realized
) {
1127 menu
->flags
.realized
=1;
1130 if (!menu
->flags
.mapped
) {
1131 if (wPreferences
.wrap_menus
) {
1134 if (x
+MENUW(menu
) > scr_width
)
1135 x
= scr_width
- MENUW(menu
);
1136 if (y
+MENUH(menu
) > scr_height
)
1137 y
= scr_height
- MENUH(menu
);
1140 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
1143 XMapWindow(dpy
, menu
->frame
->core
->window
);
1144 wRaiseFrame(menu
->frame
->core
);
1145 menu
->flags
.mapped
= 1;
1147 selectEntry(menu
, 0);
1156 wMenuMap(WMenu
*menu
)
1158 if (!menu
->flags
.realized
) {
1159 menu
->flags
.realized
=1;
1162 if (menu
->flags
.app_menu
&& menu
->parent
==NULL
) {
1163 menu
->frame_x
= menu
->frame
->screen_ptr
->app_menu_x
;
1164 menu
->frame_y
= menu
->frame
->screen_ptr
->app_menu_y
;
1165 XMoveWindow(dpy
, menu
->frame
->core
->window
, menu
->frame_x
, menu
->frame_y
);
1167 XMapWindow(dpy
, menu
->frame
->core
->window
);
1168 wRaiseFrame(menu
->frame
->core
);
1169 menu
->flags
.mapped
= 1;
1174 wMenuUnmap(WMenu
*menu
)
1178 XUnmapWindow(dpy
, menu
->frame
->core
->window
);
1179 if (menu
->flags
.titled
&& menu
->flags
.buttoned
) {
1180 wFrameWindowHideButton(menu
->frame
, WFF_RIGHT_BUTTON
);
1182 menu
->flags
.buttoned
= 0;
1183 menu
->flags
.mapped
= 0;
1184 menu
->flags
.open_to_left
= 0;
1186 for (i
=0; i
<menu
->cascade_no
; i
++) {
1187 if (menu
->cascades
[i
]!=NULL
1188 && menu
->cascades
[i
]->flags
.mapped
1189 && !menu
->cascades
[i
]->flags
.buttoned
) {
1191 wMenuUnmap(menu
->cascades
[i
]);
1194 menu
->selected_entry
= -1;
1200 wMenuPaint(WMenu
*menu
)
1204 if (!menu
->flags
.mapped
) {
1209 for (i
=0; i
<menu
->entry_no
; i
++) {
1210 paintEntry(menu
, i
, i
==menu
->selected_entry
);
1216 wMenuSetEnabled(WMenu
*menu
, int index
, int enable
)
1218 if (index
>=menu
->entry_no
) return;
1219 menu
->entries
[index
]->flags
.enabled
=enable
;
1220 paintEntry(menu
, index
, index
==menu
->selected_entry
);
1221 paintEntry(menu
->brother
, index
, index
==menu
->selected_entry
);
1225 /* ====================================================================== */
1229 editEntry(WMenu
*menu
, WMenuEntry
*entry
)
1233 WObjDescriptor
*desc
;
1239 menu
->flags
.editing
= 1;
1241 text
= wTextCreate(menu
->menu
, 1, menu
->entry_height
* entry
->order
,
1242 menu
->menu
->width
- 2, menu
->entry_height
- 1);
1244 wTextPutText(text
, entry
->text
);
1245 XGetInputFocus(dpy
, &old_focus
, &old_revert
);
1246 XSetInputFocus(dpy
, text
->core
->window
, RevertToNone
, CurrentTime
);
1248 if (XGrabKeyboard(dpy
, text
->core
->window
, True
, GrabModeAsync
,
1249 GrabModeAsync
, CurrentTime
)!=GrabSuccess
) {
1250 wwarning("could not grab keyboard");
1253 wSetFocusTo(menu
->frame
->screen_ptr
,
1254 menu
->frame
->screen_ptr
->focused_window
);
1259 while (!done
&& !text
->done
) {
1261 XAllowEvents(dpy
, AsyncKeyboard
|AsyncPointer
, CurrentTime
);
1263 WMNextEvent(dpy
, &event
);
1265 if (XFindContext(dpy
, event
.xany
.window
, wWinContext
,
1266 (XPointer
*)&desc
)==XCNOENT
)
1269 if ((desc
!= NULL
) && (desc
->handle_anything
!= NULL
)) {
1271 (*desc
->handle_anything
)(desc
, &event
);
1274 switch (event
.type
) {
1276 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
1280 WMHandleEvent(&event
);
1286 XSetInputFocus(dpy
, old_focus
, old_revert
, CurrentTime
);
1288 wSetFocusTo(menu
->frame
->screen_ptr
,
1289 menu
->frame
->screen_ptr
->focused_window
);
1292 t
= wTextGetText(text
);
1293 /* if !t, the user has canceled editing */
1297 entry
->text
= wstrdup(t
);
1299 menu
->flags
.realized
= 0;
1303 XUngrabKeyboard(dpy
, CurrentTime
);
1305 if (t
&& menu
->on_edit
)
1306 (*menu
->on_edit
)(menu
, entry
);
1308 menu
->flags
.editing
= 0;
1310 if (!menu
->flags
.realized
)
1316 selectEntry(WMenu
*menu
, int entry_no
)
1322 if (menu
->entries
==NULL
)
1325 if (entry_no
>= menu
->entry_no
)
1328 old_entry
= menu
->selected_entry
;
1329 menu
->selected_entry
= entry_no
;
1331 if (old_entry
!=entry_no
) {
1333 /* unselect previous entry */
1335 paintEntry(menu
, old_entry
, False
);
1336 entry
= menu
->entries
[old_entry
];
1339 if (entry
->cascade
>=0 && menu
->cascades
) {
1340 if (!menu
->cascades
[entry
->cascade
]->flags
.buttoned
) {
1341 wMenuUnmap(menu
->cascades
[entry
->cascade
]);
1347 menu
->selected_entry
= -1;
1350 entry
= menu
->entries
[entry_no
];
1352 if (entry
->cascade
>=0 && menu
->cascades
&& entry
->flags
.enabled
) {
1353 /* Callback for when the submenu is opened.
1355 submenu
= menu
->cascades
[entry
->cascade
];
1356 if (submenu
&& submenu
->flags
.brother
)
1357 submenu
= submenu
->brother
;
1359 if (entry
->callback
) {
1360 /* Only call the callback if the submenu is not yet mapped.
1362 if (menu
->flags
.brother
) {
1363 if (!submenu
|| !submenu
->flags
.mapped
)
1364 (*entry
->callback
)(menu
->brother
, entry
);
1366 if (!submenu
|| !submenu
->flags
.buttoned
)
1367 (*entry
->callback
)(menu
, entry
);
1371 /* the submenu menu might have changed */
1372 submenu
= menu
->cascades
[entry
->cascade
];
1375 if (!submenu
->flags
.mapped
) {
1378 if (!submenu
->flags
.realized
)
1379 wMenuRealize(submenu
);
1380 if (wPreferences
.wrap_menus
) {
1381 if (menu
->flags
.open_to_left
)
1382 submenu
->flags
.open_to_left
= 1;
1384 if (submenu
->flags
.open_to_left
) {
1385 x
= menu
->frame_x
- MENUW(submenu
);
1388 submenu
->flags
.open_to_left
= 0;
1391 x
= menu
->frame_x
+ MENUW(menu
);
1393 if (x
+ MENUW(submenu
)
1394 >= menu
->frame
->screen_ptr
->scr_width
) {
1396 x
= menu
->frame_x
- MENUW(submenu
);
1397 submenu
->flags
.open_to_left
= 1;
1401 x
= menu
->frame_x
+ MENUW(menu
);
1404 if (wPreferences
.align_menus
) {
1407 y
= menu
->frame_y
+ menu
->entry_height
*entry_no
;
1408 if (menu
->flags
.titled
)
1409 y
+= menu
->frame
->top_width
;
1410 if (menu
->cascades
[entry
->cascade
]->flags
.titled
)
1411 y
-= menu
->cascades
[entry
->cascade
]->frame
->top_width
;
1414 wMenuMapAt(menu
->cascades
[entry
->cascade
], x
, y
, False
);
1415 menu
->cascades
[entry
->cascade
]->parent
= menu
;
1420 paintEntry(menu
, entry_no
, True
);
1426 findMenu(WScreen
*scr
, int *x_ret
, int *y_ret
)
1429 WObjDescriptor
*desc
;
1430 Window root_ret
, win
, junk_win
;
1434 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, &x
, &y
, &wx
, &wy
,
1437 if (win
==None
) return NULL
;
1439 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1442 if (desc
->parent_type
== WCLASS_MENU
) {
1443 menu
= (WMenu
*)desc
->parent
;
1444 XTranslateCoordinates(dpy
, root_ret
, menu
->menu
->window
, wx
, wy
,
1445 x_ret
, y_ret
, &junk_win
);
1455 closeCascade(WMenu
*menu
)
1457 WMenu
*parent
=menu
->parent
;
1459 if (menu
->flags
.brother
1460 || (!menu
->flags
.buttoned
1461 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
))) {
1463 selectEntry(menu
, -1);
1465 #if (MENU_BLINK_DELAY > 2)
1466 wusleep(MENU_BLINK_DELAY
/2);
1470 && (parent
->parent
!=NULL
|| !parent
->flags
.app_menu
1471 || parent
->flags
.brother
)
1472 && !parent
->flags
.buttoned
) {
1473 selectEntry(parent
, -1);
1475 parent
= parent
->parent
;
1478 selectEntry(parent
, -1);
1484 closeBrotherCascadesOf(WMenu
*menu
)
1489 for (i
=0; i
<menu
->cascade_no
; i
++) {
1490 if (menu
->cascades
[i
]->flags
.brother
) {
1491 tmp
= menu
->cascades
[i
];
1493 tmp
= menu
->cascades
[i
]->brother
;
1495 if (tmp
->flags
.mapped
) {
1496 selectEntry(tmp
->parent
, -1);
1497 closeBrotherCascadesOf(tmp
);
1504 #define getEntryAt(menu, x, y) ((y)<0 ? -1 : (y)/(menu->entry_height))
1508 parentMenu(WMenu
*menu
)
1513 if (menu
->flags
.buttoned
)
1516 while (menu
->parent
&& menu
->parent
->flags
.mapped
) {
1517 parent
= menu
->parent
;
1518 if (parent
->selected_entry
< 0)
1520 entry
= parent
->entries
[parent
->selected_entry
];
1521 if (!entry
->flags
.enabled
|| entry
->cascade
<0 || !parent
->cascades
||
1522 parent
->cascades
[entry
->cascade
] != menu
)
1525 if (menu
->flags
.buttoned
)
1535 * Will raise the passed menu, if submenu = 0
1536 * If submenu > 0 will also raise all mapped submenus
1537 * until the first buttoned one
1538 * If submenu < 0 will also raise all mapped parent menus
1539 * until the first buttoned one
1543 raiseMenus(WMenu
*menu
, int submenus
)
1550 wRaiseFrame(menu
->frame
->core
);
1552 if (submenus
>0 && menu
->selected_entry
>=0) {
1553 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
1554 if (i
>=0 && menu
->cascades
) {
1555 submenu
= menu
->cascades
[i
];
1556 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
1557 raiseMenus(submenu
, submenus
);
1560 if (submenus
<0 && !menu
->flags
.buttoned
&&
1561 menu
->parent
&& menu
->parent
->flags
.mapped
)
1562 raiseMenus(menu
->parent
, submenus
);
1567 wMenuUnderPointer(WScreen
*screen
)
1569 WObjDescriptor
*desc
;
1570 Window root_ret
, win
;
1574 XQueryPointer(dpy
, screen
->root_win
, &root_ret
, &win
, &dummy
, &dummy
,
1575 &dummy
, &dummy
, &mask
);
1577 if (win
==None
) return NULL
;
1579 if (XFindContext(dpy
, win
, wWinContext
, (XPointer
*)&desc
)==XCNOENT
)
1582 if (desc
->parent_type
== WCLASS_MENU
)
1583 return (WMenu
*)desc
->parent
;
1589 #define MIN(a,b) (((a) > (b)) ? (b) : (a))
1593 getPointerPosition(WScreen
*scr
, int *x
, int *y
)
1595 Window root_ret
, win
;
1599 XQueryPointer(dpy
, scr
->root_win
, &root_ret
, &win
, x
, y
, &wx
, &wy
, &mask
);
1604 getScrollAmount(WMenu
*menu
, int *hamount
, int *vamount
)
1606 WScreen
*scr
= menu
->menu
->screen_ptr
;
1607 int menuX1
= menu
->frame_x
;
1608 int menuY1
= menu
->frame_y
;
1609 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1610 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1611 int screenW
= scr
->scr_width
;
1612 int screenH
= scr
->scr_height
;
1618 getPointerPosition(scr
, &xroot
, &yroot
);
1621 if (xroot
<= 1 && menuX1
< 0) {
1622 /* scroll to the right */
1623 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX1
));
1625 } else if (xroot
>= screenW
-2 && menuX2
> screenW
-1) {
1626 /* scroll to the left */
1627 *hamount
= MIN(MENU_SCROLL_STEP
, abs(menuX2
-screenW
-1));
1632 *hamount
= -*hamount
;
1635 if (yroot
<= 1 && menuY1
< 0) {
1637 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY1
));
1639 } else if (yroot
>= screenH
-2 && menuY2
> screenH
-1) {
1641 *vamount
= MIN(MENU_SCROLL_STEP
, abs(menuY2
-screenH
-2));
1643 *vamount
= -*vamount
;
1649 dragScrollMenuCallback(void *data
)
1651 WMenu
*menu
= (WMenu
*)data
;
1652 WScreen
*scr
= menu
->menu
->screen_ptr
;
1653 WMenu
*parent
= parentMenu(menu
);
1654 int hamount
, vamount
;
1656 int newSelectedEntry
;
1658 getScrollAmount(menu
, &hamount
, &vamount
);
1661 if (hamount
!= 0 || vamount
!= 0) {
1662 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1663 parent
->frame_y
+ vamount
, True
);
1664 if (findMenu(scr
, &x
, &y
)) {
1665 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1666 selectEntry(menu
, newSelectedEntry
);
1668 /* Pointer fell outside of menu. If the selected entry is
1669 * not a submenu, unselect it */
1670 if (menu
->selected_entry
>= 0
1671 && menu
->entries
[menu
->selected_entry
]->cascade
<0)
1672 selectEntry(menu
, -1);
1673 newSelectedEntry
= 0;
1676 /* paranoid check */
1677 if (newSelectedEntry
>= 0) {
1678 /* keep scrolling */
1679 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1680 dragScrollMenuCallback
, menu
);
1685 /* don't need to scroll anymore */
1687 if (findMenu(scr
, &x
, &y
)) {
1688 newSelectedEntry
= getEntryAt(menu
, x
, y
);
1689 selectEntry(menu
, newSelectedEntry
);
1696 scrollMenuCallback(void *data
)
1698 WMenu
*menu
= (WMenu
*)data
;
1699 WMenu
*parent
= parentMenu(menu
);
1700 int hamount
= 0; /* amount to scroll */
1703 getScrollAmount(menu
, &hamount
, &vamount
);
1705 if (hamount
!= 0 || vamount
!= 0) {
1706 wMenuMove(parent
, parent
->frame_x
+ hamount
,
1707 parent
->frame_y
+ vamount
, True
);
1709 /* keep scrolling */
1710 menu
->timer
= WMAddTimerHandler(MENU_SCROLL_DELAY
,
1711 scrollMenuCallback
, menu
);
1713 /* don't need to scroll anymore */
1720 #define MENU_SCROLL_BORDER 5
1723 isPointNearBoder(WMenu
*menu
, int x
, int y
)
1725 int menuX1
= menu
->frame_x
;
1726 int menuY1
= menu
->frame_y
;
1727 int menuX2
= menu
->frame_x
+ MENUW(menu
);
1728 int menuY2
= menu
->frame_y
+ MENUH(menu
);
1729 int scrXe
= menu
->menu
->screen_ptr
->scr_width
-1;
1730 int scrYe
= menu
->menu
->screen_ptr
->scr_height
-1;
1733 if (x
>= menuX1
&& x
<= menuX2
&& (y
< MENU_SCROLL_BORDER
1734 || y
> scrYe
-MENU_SCROLL_BORDER
))
1736 else if (y
>= menuY1
&& y
<= menuY2
&& (x
< MENU_SCROLL_BORDER
1737 || x
> scrXe
-MENU_SCROLL_BORDER
))
1745 wMenuScroll(WMenu
*menu
, XEvent
*event
)
1748 WMenu
*omenu
= parentMenu(menu
);
1749 WScreen
*scr
= menu
->frame
->screen_ptr
;
1752 int old_frame_x
= omenu
->frame_x
;
1753 int old_frame_y
= omenu
->frame_y
;
1757 puts("Entering menu Scroll");
1760 if ((/*omenu->flags.buttoned &&*/ !wPreferences
.wrap_menus
)
1761 || omenu
->flags
.app_menu
) {
1765 if (!wPreferences
.wrap_menus
)
1766 raiseMenus(omenu
, True
);
1768 raiseMenus(menu
, False
);
1771 scrollMenuCallback(menu
);
1774 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1776 WMNextEvent(dpy
, &ev
);
1781 x
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1782 y
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1784 /* on_border is != 0 if the pointer is between the menu
1785 * and the screen border and is close enough to the border */
1786 on_border
= isPointNearBoder(menu
, x
, y
);
1788 smenu
= wMenuUnderPointer(scr
);
1790 if ((smenu
==NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
)!=omenu
)) {
1795 on_x_edge
= x
<= 1 || x
>= scr
->scr_width
- 2;
1796 on_y_edge
= y
<= 1 || y
>= scr
->scr_height
- 2;
1797 on_border
= on_x_edge
|| on_y_edge
;
1799 if (!on_border
&& !jump_back
) {
1804 if (menu
->timer
&& (smenu
!=menu
|| (!on_y_edge
&& !on_x_edge
))) {
1805 WMDeleteTimerHandler(menu
->timer
);
1813 scrollMenuCallback(menu
);
1816 /* True if we push on title, or drag the omenu to other position */
1817 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1818 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1819 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1820 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1822 smenu
= wMenuUnderPointer(scr
);
1823 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1825 else if (smenu
==omenu
&& on_title
) {
1839 WMDeleteTimerHandler(menu
->timer
);
1844 wMenuMove(omenu
, old_frame_x
, old_frame_y
, True
);
1847 puts("Leaving menu Scroll");
1854 menuExpose(WObjDescriptor
*desc
, XEvent
*event
)
1856 wMenuPaint(desc
->parent
);
1860 int *delayed_select
;
1867 delaySelection(void *data
)
1869 delay_data
*d
= (delay_data
*)data
;
1875 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1876 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1877 entry_no
= getEntryAt(menu
, x
, y
);
1878 selectEntry(menu
, entry_no
);
1880 if (d
->delayed_select
)
1881 *(d
->delayed_select
) = 0;
1886 menuMouseDown(WObjDescriptor
*desc
, XEvent
*event
)
1888 XButtonEvent
*bev
= &event
->xbutton
;
1889 WMenu
*menu
= desc
->parent
;
1891 WScreen
*scr
=menu
->frame
->screen_ptr
;
1892 WMenuEntry
*entry
=NULL
;
1894 int close_on_exit
=0;
1896 int delayed_select
= 0;
1900 int old_frame_x
= 0;
1901 int old_frame_y
= 0;
1902 delay_data d_data
= {NULL
, NULL
, NULL
};
1904 if (!wPreferences
.wrap_menus
) {
1905 smenu
= parentMenu(menu
);
1906 old_frame_x
= smenu
->frame_x
;
1907 old_frame_y
= smenu
->frame_y
;
1909 else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1910 /* This is true if the menu was launched with right click on root window */
1912 d_data
.delayed_select
= &delayed_select
;
1914 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
,
1915 delaySelection
, &d_data
);
1918 wRaiseFrame(menu
->frame
->core
);
1920 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1922 smenu
= findMenu(scr
, &x
, &y
);
1930 if (menu
->flags
.editing
) {
1933 entry_no
= getEntryAt(menu
, x
, y
);
1935 entry
= menu
->entries
[entry_no
];
1937 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
1938 && entry
->flags
.editable
) {
1939 editEntry(smenu
, entry
);
1941 } else if (bev
->state
& ControlMask
) {
1945 if (entry
->flags
.enabled
&& entry
->cascade
>=0 && menu
->cascades
) {
1946 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1948 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&&
1949 menu
->selected_entry
!=entry_no
) {
1950 wMenuUnmap(submenu
);
1952 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1953 selectEntry(menu
, entry_no
);
1954 } else if (!submenu
->flags
.buttoned
) {
1955 selectEntry(menu
, -1);
1958 } else if (!delayed_select
) {
1959 selectEntry(menu
, entry_no
);
1962 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1964 dragScrollMenuCallback(menu
);
1968 prevx
= bev
->x_root
;
1969 prevy
= bev
->y_root
;
1973 XAllowEvents(dpy
, SyncPointer
, CurrentTime
);
1975 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonReleaseMask
1976 |ButtonPressMask
, &ev
);
1979 smenu
= findMenu(scr
, &x
, &y
);
1981 if (smenu
== NULL
) {
1982 /* moved mouse out of menu */
1984 if (!delayed_select
&& d_data
.magic
) {
1985 WMDeleteTimerHandler(d_data
.magic
);
1986 d_data
.magic
= NULL
;
1989 || (menu
->selected_entry
>=0
1990 && menu
->entries
[menu
->selected_entry
]->cascade
>=0)) {
1991 prevx
= ev
.xmotion
.x_root
;
1992 prevy
= ev
.xmotion
.y_root
;
1996 selectEntry(menu
, -1);
1998 prevx
= ev
.xmotion
.x_root
;
1999 prevy
= ev
.xmotion
.y_root
;
2001 } else if (menu
&& menu
!=smenu
2002 && (menu
->selected_entry
<0
2003 || menu
->entries
[menu
->selected_entry
]->cascade
<0)) {
2004 selectEntry(menu
, -1);
2006 if (!delayed_select
&& d_data
.magic
) {
2007 WMDeleteTimerHandler(d_data
.magic
);
2008 d_data
.magic
= NULL
;
2012 /* hysteresis for item selection */
2014 /* check if the motion was to the side, indicating that
2015 * the user may want to cross to a submenu */
2016 if (!delayed_select
&& menu
) {
2018 Bool moved_to_submenu
;/* moved to direction of submenu */
2020 dx
= abs(prevx
- ev
.xmotion
.x_root
);
2022 moved_to_submenu
= False
;
2023 if (dx
> 0 /* if moved enough to the side */
2024 /* maybe a open submenu */
2025 && menu
->selected_entry
>=0
2026 /* moving to the right direction */
2027 && (wPreferences
.align_menus
2028 || ev
.xmotion
.y_root
>= prevy
)) {
2031 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
2033 if (menu
->cascades
[index
]->frame_x
>menu
->frame_x
) {
2034 if (prevx
< ev
.xmotion
.x_root
)
2035 moved_to_submenu
= True
;
2037 if (prevx
> ev
.xmotion
.x_root
)
2038 moved_to_submenu
= True
;
2044 if (menu
!= smenu
) {
2046 WMDeleteTimerHandler(d_data
.magic
);
2048 d_data
.magic
= NULL
;
2049 } else if (moved_to_submenu
) {
2050 /* while we are moving, postpone the selection */
2052 WMDeleteTimerHandler(d_data
.magic
);
2054 d_data
.delayed_select
= NULL
;
2056 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
2059 prevx
= ev
.xmotion
.x_root
;
2060 prevy
= ev
.xmotion
.y_root
;
2064 WMDeleteTimerHandler(d_data
.magic
);
2065 d_data
.magic
= NULL
;
2069 prevx
= ev
.xmotion
.x_root
;
2070 prevy
= ev
.xmotion
.y_root
;
2072 /* pointer crossed menus */
2073 if (menu
&& menu
->timer
) {
2074 WMDeleteTimerHandler(menu
->timer
);
2078 dragScrollMenuCallback(smenu
);
2082 dragScrollMenuCallback(menu
);
2084 if (!delayed_select
) {
2085 entry_no
= getEntryAt(menu
, x
, y
);
2087 entry
= menu
->entries
[entry_no
];
2088 if (entry
->flags
.enabled
&& entry
->cascade
>=0 &&
2090 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
2091 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
2092 && menu
->selected_entry
!=entry_no
) {
2093 wMenuUnmap(submenu
);
2097 selectEntry(menu
, entry_no
);
2105 if (ev
.xbutton
.button
== event
->xbutton
.button
)
2115 if (menu
&& menu
->timer
) {
2116 WMDeleteTimerHandler(menu
->timer
);
2119 if (d_data
.magic
!=NULL
)
2120 WMDeleteTimerHandler(d_data
.magic
);
2122 if (menu
&& menu
->selected_entry
>=0) {
2123 entry
= menu
->entries
[menu
->selected_entry
];
2124 if (entry
->callback
!=NULL
&& entry
->flags
.enabled
2125 && entry
->cascade
< 0) {
2126 /* blink and erase menu selection */
2127 #if (MENU_BLINK_DELAY > 0)
2128 int sel
= menu
->selected_entry
;
2131 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
2132 paintEntry(menu
, sel
, False
);
2134 wusleep(MENU_BLINK_DELAY
);
2135 paintEntry(menu
, sel
, True
);
2137 wusleep(MENU_BLINK_DELAY
);
2140 /* unmap the menu, it's parents and call the callback */
2141 if (!menu
->flags
.buttoned
&&
2142 (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2145 selectEntry(menu
, -1);
2147 (*entry
->callback
)(menu
, entry
);
2149 /* If the user double clicks an entry, the entry will
2150 * be executed twice, which is not good for things like
2151 * the root menu. So, ignore any clicks that were generated
2152 * while the entry was being executed */
2153 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
,
2155 } else if (entry
->callback
!=NULL
&& entry
->cascade
<0) {
2156 selectEntry(menu
, -1);
2158 if (entry
->cascade
>=0 && menu
->cascades
2159 && menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2160 selectEntry(menu
, -1);
2165 if (((WMenu
*)desc
->parent
)->flags
.brother
|| close_on_exit
2167 closeCascade(desc
->parent
);
2169 /* close the cascade windows that should not remain opened */
2170 closeBrotherCascadesOf(desc
->parent
);
2172 if (!wPreferences
.wrap_menus
)
2173 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2178 wMenuMove(WMenu
*menu
, int x
, int y
, int submenus
)
2187 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2189 if (submenus
>0 && menu
->selected_entry
>=0) {
2190 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2192 if (i
>=0 && menu
->cascades
) {
2193 submenu
= menu
->cascades
[i
];
2194 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
2195 if (wPreferences
.align_menus
) {
2196 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2198 wMenuMove(submenu
, x
+ MENUW(menu
),
2199 y
+ submenu
->entry_height
*menu
->selected_entry
,
2204 if (submenus
<0 && menu
->parent
!=NULL
&& menu
->parent
->flags
.mapped
&&
2205 !menu
->parent
->flags
.buttoned
) {
2206 if (wPreferences
.align_menus
) {
2207 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2209 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2210 - menu
->parent
->entry_height
*menu
->parent
->selected_entry
,
2218 changeMenuLevels(WMenu
*menu
, int lower
)
2223 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
2225 wRaiseFrame(menu
->frame
->core
);
2226 menu
->flags
.lowered
= 0;
2228 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2229 wLowerFrame(menu
->frame
->core
);
2230 menu
->flags
.lowered
= 1;
2232 for (i
=0; i
<menu
->cascade_no
; i
++) {
2233 if (menu
->cascades
[i
]
2234 && !menu
->cascades
[i
]->flags
.buttoned
2235 && menu
->cascades
[i
]->flags
.lowered
!=lower
) {
2236 changeMenuLevels(menu
->cascades
[i
], lower
);
2244 menuTitleDoubleClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2249 if (event
->xbutton
.state
& MOD_MASK
) {
2250 if (menu
->flags
.lowered
) {
2255 changeMenuLevels(menu
, lower
);
2261 menuTitleMouseDown(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2266 int x
=menu
->frame_x
, y
=menu
->frame_y
;
2267 int dx
=event
->xbutton
.x_root
, dy
=event
->xbutton
.y_root
;
2272 printf("Moving menu\n");
2275 /* can't touch the menu copy */
2276 if (menu
->flags
.brother
)
2279 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2282 if (event
->xbutton
.state
& MOD_MASK
) {
2283 wLowerFrame(menu
->frame
->core
);
2286 wRaiseFrame(menu
->frame
->core
);
2291 /* lower/raise all submenus */
2293 if (tmp
->selected_entry
>=0 && tmp
->cascades
2294 && tmp
->entries
[tmp
->selected_entry
]->cascade
>=0) {
2295 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2296 if (!tmp
|| !tmp
->flags
.mapped
)
2299 wLowerFrame(tmp
->frame
->core
);
2301 wRaiseFrame(tmp
->frame
->core
);
2307 /* tear off the menu if it's a root menu or a cascade
2309 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
2310 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2311 menu
->flags
.buttoned
=1;
2312 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2314 /* turn off selected menu entry in parent menu */
2315 selectEntry(menu
->parent
, -1);
2317 /* make parent map the copy in place of the original */
2318 for (i
=0; i
<menu
->parent
->cascade_no
; i
++) {
2319 if (menu
->parent
->cascades
[i
] == menu
) {
2320 menu
->parent
->cascades
[i
] = menu
->brother
;
2329 WMMaskEvent(dpy
, ButtonMotionMask
|ButtonReleaseMask
|ButtonPressMask
2330 |ExposureMask
, &ev
);
2334 x
+= ev
.xmotion
.x_root
- dx
;
2335 y
+= ev
.xmotion
.y_root
- dy
;
2336 dx
= ev
.xmotion
.x_root
;
2337 dy
= ev
.xmotion
.y_root
;
2338 wMenuMove(menu
, x
, y
, True
);
2340 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2341 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2343 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2344 ButtonMotionMask
|ButtonReleaseMask
2346 GrabModeAsync
, GrabModeAsync
, None
,
2347 wCursor
[WCUR_MOVE
], CurrentTime
);
2356 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2359 printf("End menu move\n");
2361 XUngrabPointer(dpy
, CurrentTime
);
2372 *----------------------------------------------------------------------
2374 * Handles mouse click on the close button of menus. The menu is
2375 * closed when the button is clicked.
2378 * The closed menu is reinserted at it's parent menus
2380 *----------------------------------------------------------------------
2383 menuCloseClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2385 WMenu
*menu
= (WMenu
*)data
;
2386 WMenu
*parent
= menu
->parent
;
2390 for (i
=0; i
<parent
->cascade_no
; i
++) {
2391 /* find the entry that points to the copy */
2392 if (parent
->cascades
[i
] == menu
->brother
) {
2393 /* make it point to the original */
2394 parent
->cascades
[i
] = menu
;
2395 menu
->parent
= parent
;
2405 wMenuSaveState(WScreen
*scr
)
2407 proplist_t menus
, key
, value
;
2411 menus
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
2414 if (scr
->root_menu
&& scr
->root_menu
->flags
.buttoned
) {
2415 sprintf(buffer
, "%i,%i", scr
->root_menu
->frame_x
,
2416 scr
->root_menu
->frame_y
);
2417 key
= PLMakeString("RootMenu");
2418 value
= PLMakeString(buffer
);
2419 PLInsertDictionaryEntry(menus
, key
, value
);
2425 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2426 sprintf(buffer
, "%i,%i", scr
->switch_menu
->frame_x
,
2427 scr
->switch_menu
->frame_y
);
2428 key
= PLMakeString("SwitchMenu");
2429 value
= PLMakeString(buffer
);
2430 PLInsertDictionaryEntry(menus
, key
, value
);
2436 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2437 sprintf(buffer
, "%i,%i", scr
->workspace_menu
->frame_x
,
2438 scr
->workspace_menu
->frame_y
);
2439 key
= PLMakeString("WorkspaceMenu");
2440 value
= PLMakeString(buffer
);
2441 PLInsertDictionaryEntry(menus
, key
, value
);
2448 key
= PLMakeString("Menus");
2449 PLInsertDictionaryEntry(scr
->session_state
, key
, menus
);
2456 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2460 restoreMenu(WScreen
*scr
, proplist_t menu
, int which
)
2463 WMenu
*pmenu
= NULL
;
2468 if (!PLIsString(menu
)) {
2469 COMPLAIN("Position");
2473 if (sscanf(PLGetString(menu
), "%i,%i", &x
, &y
)!=2)
2474 COMPLAIN("Position");
2477 if (which
& WSS_ROOTMENU
) {
2478 OpenRootMenu(scr
, x
, y
, False
);
2479 pmenu
= scr
->root_menu
;
2480 } else if (which
& WSS_SWITCHMENU
) {
2481 OpenSwitchMenu(scr
, x
, y
, False
);
2482 pmenu
= scr
->switch_menu
;
2485 if (which
& WSS_WSMENU
) {
2486 OpenWorkspaceMenu(scr
, x
, y
);
2487 pmenu
= scr
->workspace_menu
;
2488 if (pmenu
->parent
) {
2489 /* make parent map the copy in place of the original */
2490 for (i
=0; i
<pmenu
->parent
->cascade_no
; i
++) {
2491 if (pmenu
->parent
->cascades
[i
] == pmenu
) {
2492 pmenu
->parent
->cascades
[i
] = pmenu
->brother
;
2500 int width
= MENUW(pmenu
);
2501 int height
= MENUH(pmenu
);
2503 x
= (x
< -width
) ? 0 : x
;
2504 x
= (x
> scr
->scr_width
) ? scr
->scr_width
- width
: x
;
2505 y
= (y
< 0) ? 0 : y
;
2506 y
= (y
> scr
->scr_height
) ? scr
->scr_height
- height
: y
;
2507 wMenuMove(pmenu
, x
, y
, True
);
2508 pmenu
->flags
.buttoned
= 1;
2509 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2517 wMenuRestoreState(WScreen
*scr
)
2519 proplist_t menus
, menu
, key
, rkey
, skey
, wkey
;
2521 key
= PLMakeString("Menus");
2522 menus
= PLGetDictionaryEntry(scr
->session_state
, key
);
2530 rkey
= PLMakeString("RootMenu");
2531 skey
= PLMakeString("SwitchMenu");
2532 wkey
= PLMakeString("WorkspaceMenu");
2533 menu
= PLGetDictionaryEntry(menus
, rkey
);
2534 restoreMenu(scr
, menu
, WSS_ROOTMENU
);
2535 menu
= PLGetDictionaryEntry(menus
, skey
);
2536 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2537 menu
= PLGetDictionaryEntry(menus
, wkey
);
2538 restoreMenu(scr
, menu
, WSS_WSMENU
);
2547 OpenWorkspaceMenu(WScreen
*scr
, int x
, int y
)
2549 WMenu
*menu
, *parent
;
2553 if (!scr
->root_menu
) {
2554 OpenRootMenu(scr
, scr
->scr_width
*2, 0, False
);
2555 wMenuUnmap(scr
->root_menu
);
2558 menu
= scr
->workspace_menu
;
2560 if (menu
->flags
.mapped
) {
2561 if (!menu
->flags
.buttoned
) {
2563 parent
= menu
->parent
;
2564 if (parent
&& parent
->selected_entry
>= 0) {
2565 entry
= parent
->entries
[parent
->selected_entry
];
2566 if (parent
->cascades
[entry
->cascade
] == menu
) {
2567 selectEntry(parent
, -1);
2568 wMenuMapAt(menu
, x
, y
, False
);
2572 wRaiseFrame(menu
->frame
->core
);
2573 wMenuMapCopyAt(menu
, x
, y
);
2577 wMenuMapAt(menu
, x
, y
, False
);