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
))
1744 typedef struct _delay
{
1752 _leaving(_delay
*dl
)
1754 wMenuMove(dl
->menu
, dl
->ox
, dl
->oy
, True
);
1755 dl
->menu
->jump_back
=NULL
;
1756 dl
->menu
->menu
->screen_ptr
->flags
.jump_back_pending
= 0;
1762 wMenuScroll(WMenu
*menu
, XEvent
*event
)
1765 WMenu
*omenu
= parentMenu(menu
);
1766 WScreen
*scr
= menu
->frame
->screen_ptr
;
1769 int old_frame_x
= omenu
->frame_x
;
1770 int old_frame_y
= omenu
->frame_y
;
1774 puts("Entering menu Scroll");
1777 if (omenu
->jump_back
)
1778 WMDeleteTimerWithClientData(omenu
->jump_back
);
1781 if ((/*omenu->flags.buttoned &&*/ !wPreferences
.wrap_menus
)
1782 || omenu
->flags
.app_menu
) {
1786 if (!wPreferences
.wrap_menus
)
1787 raiseMenus(omenu
, True
);
1789 raiseMenus(menu
, False
);
1792 scrollMenuCallback(menu
);
1795 int x
, y
, on_border
, on_x_edge
, on_y_edge
, on_title
;
1797 WMNextEvent(dpy
, &ev
);
1802 x
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.x_root
: ev
.xcrossing
.x_root
;
1803 y
= (ev
.type
==MotionNotify
) ? ev
.xmotion
.y_root
: ev
.xcrossing
.y_root
;
1805 /* on_border is != 0 if the pointer is between the menu
1806 * and the screen border and is close enough to the border */
1807 on_border
= isPointNearBoder(menu
, x
, y
);
1809 smenu
= wMenuUnderPointer(scr
);
1811 if ((smenu
==NULL
&& !on_border
) || (smenu
&& parentMenu(smenu
)!=omenu
)) {
1816 on_x_edge
= x
<= 1 || x
>= scr
->scr_width
- 2;
1817 on_y_edge
= y
<= 1 || y
>= scr
->scr_height
- 2;
1818 on_border
= on_x_edge
|| on_y_edge
;
1820 if (!on_border
&& !jump_back
) {
1825 if (menu
->timer
&& (smenu
!=menu
|| (!on_y_edge
&& !on_x_edge
))) {
1826 WMDeleteTimerHandler(menu
->timer
);
1834 scrollMenuCallback(menu
);
1837 /* True if we push on title, or drag the omenu to other position */
1838 on_title
= ev
.xbutton
.x_root
>= omenu
->frame_x
&&
1839 ev
.xbutton
.x_root
<= omenu
->frame_x
+ MENUW(omenu
) &&
1840 ev
.xbutton
.y_root
>= omenu
->frame_y
&&
1841 ev
.xbutton
.y_root
<= omenu
->frame_y
+ omenu
->frame
->top_width
;
1843 smenu
= wMenuUnderPointer(scr
);
1844 if (smenu
== NULL
|| (smenu
&& smenu
->flags
.buttoned
&& smenu
!= omenu
))
1846 else if (smenu
==omenu
&& on_title
) {
1860 WMDeleteTimerHandler(menu
->timer
);
1866 if (!omenu
->jump_back
) {
1867 delayer
=wmalloc(sizeof(_delay
));
1868 delayer
->menu
=omenu
;
1869 delayer
->ox
=old_frame_x
;
1870 delayer
->oy
=old_frame_y
;
1871 omenu
->jump_back
=delayer
;
1872 scr
->flags
.jump_back_pending
= 1;
1874 else delayer
= omenu
->jump_back
;
1875 WMAddTimerHandler(MENU_JUMP_BACK_DELAY
,(WMCallback
*)_leaving
, delayer
);
1880 puts("Leaving menu Scroll");
1887 menuExpose(WObjDescriptor
*desc
, XEvent
*event
)
1889 wMenuPaint(desc
->parent
);
1893 int *delayed_select
;
1900 delaySelection(void *data
)
1902 delay_data
*d
= (delay_data
*)data
;
1908 menu
= findMenu(d
->menu
->menu
->screen_ptr
, &x
, &y
);
1909 if (menu
&& (d
->menu
== menu
|| d
->delayed_select
)) {
1910 entry_no
= getEntryAt(menu
, x
, y
);
1911 selectEntry(menu
, entry_no
);
1913 if (d
->delayed_select
)
1914 *(d
->delayed_select
) = 0;
1919 menuMouseDown(WObjDescriptor
*desc
, XEvent
*event
)
1921 XButtonEvent
*bev
= &event
->xbutton
;
1922 WMenu
*menu
= desc
->parent
;
1924 WScreen
*scr
=menu
->frame
->screen_ptr
;
1925 WMenuEntry
*entry
=NULL
;
1927 int close_on_exit
=0;
1929 int delayed_select
= 0;
1933 int old_frame_x
= 0;
1934 int old_frame_y
= 0;
1935 delay_data d_data
= {NULL
, NULL
, NULL
};
1937 if (!wPreferences
.wrap_menus
) {
1938 smenu
= parentMenu(menu
);
1939 old_frame_x
= smenu
->frame_x
;
1940 old_frame_y
= smenu
->frame_y
;
1941 } else if (event
->xbutton
.window
== menu
->frame
->core
->window
) {
1942 /* This is true if the menu was launched with right click on root window */
1944 d_data
.delayed_select
= &delayed_select
;
1946 d_data
.magic
= WMAddTimerHandler(wPreferences
.dblclick_time
,
1947 delaySelection
, &d_data
);
1950 wRaiseFrame(menu
->frame
->core
);
1952 close_on_exit
= (bev
->send_event
|| menu
->flags
.brother
);
1954 smenu
= findMenu(scr
, &x
, &y
);
1962 if (menu
->flags
.editing
) {
1965 entry_no
= getEntryAt(menu
, x
, y
);
1967 entry
= menu
->entries
[entry_no
];
1969 if (!close_on_exit
&& (bev
->state
& ControlMask
) && smenu
1970 && entry
->flags
.editable
) {
1971 editEntry(smenu
, entry
);
1973 } else if (bev
->state
& ControlMask
) {
1977 if (entry
->flags
.enabled
&& entry
->cascade
>=0 && menu
->cascades
) {
1978 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
1980 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
&&
1981 menu
->selected_entry
!=entry_no
) {
1982 wMenuUnmap(submenu
);
1984 if (!submenu
->flags
.mapped
&& !delayed_select
) {
1985 selectEntry(menu
, entry_no
);
1986 } else if (!submenu
->flags
.buttoned
) {
1987 selectEntry(menu
, -1);
1990 } else if (!delayed_select
) {
1991 selectEntry(menu
, entry_no
);
1994 if (!wPreferences
.wrap_menus
&& !wPreferences
.scrollable_menus
) {
1996 dragScrollMenuCallback(menu
);
2000 prevx
= bev
->x_root
;
2001 prevy
= bev
->y_root
;
2005 XAllowEvents(dpy
, SyncPointer
, CurrentTime
);
2007 WMMaskEvent(dpy
, ExposureMask
|ButtonMotionMask
|ButtonReleaseMask
2008 |ButtonPressMask
, &ev
);
2011 smenu
= findMenu(scr
, &x
, &y
);
2013 if (smenu
== NULL
) {
2014 /* moved mouse out of menu */
2016 if (!delayed_select
&& d_data
.magic
) {
2017 WMDeleteTimerHandler(d_data
.magic
);
2018 d_data
.magic
= NULL
;
2021 || (menu
->selected_entry
>=0
2022 && menu
->entries
[menu
->selected_entry
]->cascade
>=0)) {
2023 prevx
= ev
.xmotion
.x_root
;
2024 prevy
= ev
.xmotion
.y_root
;
2028 selectEntry(menu
, -1);
2030 prevx
= ev
.xmotion
.x_root
;
2031 prevy
= ev
.xmotion
.y_root
;
2033 } else if (menu
&& menu
!=smenu
2034 && (menu
->selected_entry
<0
2035 || menu
->entries
[menu
->selected_entry
]->cascade
<0)) {
2036 selectEntry(menu
, -1);
2038 if (!delayed_select
&& d_data
.magic
) {
2039 WMDeleteTimerHandler(d_data
.magic
);
2040 d_data
.magic
= NULL
;
2044 /* hysteresis for item selection */
2046 /* check if the motion was to the side, indicating that
2047 * the user may want to cross to a submenu */
2048 if (!delayed_select
&& menu
) {
2050 Bool moved_to_submenu
;/* moved to direction of submenu */
2052 dx
= abs(prevx
- ev
.xmotion
.x_root
);
2054 moved_to_submenu
= False
;
2055 if (dx
> 0 /* if moved enough to the side */
2056 /* maybe a open submenu */
2057 && menu
->selected_entry
>=0
2058 /* moving to the right direction */
2059 && (wPreferences
.align_menus
2060 || ev
.xmotion
.y_root
>= prevy
)) {
2063 index
= menu
->entries
[menu
->selected_entry
]->cascade
;
2065 if (menu
->cascades
[index
]->frame_x
>menu
->frame_x
) {
2066 if (prevx
< ev
.xmotion
.x_root
)
2067 moved_to_submenu
= True
;
2069 if (prevx
> ev
.xmotion
.x_root
)
2070 moved_to_submenu
= True
;
2076 if (menu
!= smenu
) {
2078 WMDeleteTimerHandler(d_data
.magic
);
2080 d_data
.magic
= NULL
;
2081 } else if (moved_to_submenu
) {
2082 /* while we are moving, postpone the selection */
2084 WMDeleteTimerHandler(d_data
.magic
);
2086 d_data
.delayed_select
= NULL
;
2088 d_data
.magic
= WMAddTimerHandler(MENU_SELECT_DELAY
,
2091 prevx
= ev
.xmotion
.x_root
;
2092 prevy
= ev
.xmotion
.y_root
;
2096 WMDeleteTimerHandler(d_data
.magic
);
2097 d_data
.magic
= NULL
;
2101 prevx
= ev
.xmotion
.x_root
;
2102 prevy
= ev
.xmotion
.y_root
;
2104 /* pointer crossed menus */
2105 if (menu
&& menu
->timer
) {
2106 WMDeleteTimerHandler(menu
->timer
);
2110 dragScrollMenuCallback(smenu
);
2114 dragScrollMenuCallback(menu
);
2116 if (!delayed_select
) {
2117 entry_no
= getEntryAt(menu
, x
, y
);
2119 entry
= menu
->entries
[entry_no
];
2120 if (entry
->flags
.enabled
&& entry
->cascade
>=0 &&
2122 WMenu
*submenu
= menu
->cascades
[entry
->cascade
];
2123 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
2124 && menu
->selected_entry
!=entry_no
) {
2125 wMenuUnmap(submenu
);
2129 selectEntry(menu
, entry_no
);
2137 if (ev
.xbutton
.button
== event
->xbutton
.button
)
2147 if (menu
&& menu
->timer
) {
2148 WMDeleteTimerHandler(menu
->timer
);
2151 if (d_data
.magic
!=NULL
)
2152 WMDeleteTimerHandler(d_data
.magic
);
2154 if (menu
&& menu
->selected_entry
>=0) {
2155 entry
= menu
->entries
[menu
->selected_entry
];
2156 if (entry
->callback
!=NULL
&& entry
->flags
.enabled
2157 && entry
->cascade
< 0) {
2158 /* blink and erase menu selection */
2159 #if (MENU_BLINK_DELAY > 0)
2160 int sel
= menu
->selected_entry
;
2163 for (i
=0; i
<MENU_BLINK_COUNT
; i
++) {
2164 paintEntry(menu
, sel
, False
);
2166 wusleep(MENU_BLINK_DELAY
);
2167 paintEntry(menu
, sel
, True
);
2169 wusleep(MENU_BLINK_DELAY
);
2172 /* unmap the menu, it's parents and call the callback */
2173 if (!menu
->flags
.buttoned
&&
2174 (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2177 selectEntry(menu
, -1);
2179 (*entry
->callback
)(menu
, entry
);
2181 /* If the user double clicks an entry, the entry will
2182 * be executed twice, which is not good for things like
2183 * the root menu. So, ignore any clicks that were generated
2184 * while the entry was being executed */
2185 while (XCheckTypedWindowEvent(dpy
, menu
->menu
->window
,
2187 } else if (entry
->callback
!=NULL
&& entry
->cascade
<0) {
2188 selectEntry(menu
, -1);
2190 if (entry
->cascade
>=0 && menu
->cascades
2191 && menu
->cascades
[entry
->cascade
]->flags
.brother
) {
2192 selectEntry(menu
, -1);
2197 if (((WMenu
*)desc
->parent
)->flags
.brother
|| close_on_exit
|| !smenu
)
2198 closeCascade(desc
->parent
);
2200 /* close the cascade windows that should not remain opened */
2201 closeBrotherCascadesOf(desc
->parent
);
2203 if (!wPreferences
.wrap_menus
)
2204 wMenuMove(parentMenu(desc
->parent
), old_frame_x
, old_frame_y
, True
);
2209 wMenuMove(WMenu
*menu
, int x
, int y
, int submenus
)
2218 XMoveWindow(dpy
, menu
->frame
->core
->window
, x
, y
);
2220 if (submenus
>0 && menu
->selected_entry
>=0) {
2221 i
= menu
->entries
[menu
->selected_entry
]->cascade
;
2223 if (i
>=0 && menu
->cascades
) {
2224 submenu
= menu
->cascades
[i
];
2225 if (submenu
->flags
.mapped
&& !submenu
->flags
.buttoned
)
2226 if (wPreferences
.align_menus
) {
2227 wMenuMove(submenu
, x
+ MENUW(menu
), y
, submenus
);
2229 wMenuMove(submenu
, x
+ MENUW(menu
),
2230 y
+ submenu
->entry_height
*menu
->selected_entry
,
2235 if (submenus
<0 && menu
->parent
!=NULL
&& menu
->parent
->flags
.mapped
&&
2236 !menu
->parent
->flags
.buttoned
) {
2237 if (wPreferences
.align_menus
) {
2238 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), y
, submenus
);
2240 wMenuMove(menu
->parent
, x
- MENUW(menu
->parent
), menu
->frame_y
2241 - menu
->parent
->entry_height
*menu
->parent
->selected_entry
,
2249 changeMenuLevels(WMenu
*menu
, int lower
)
2254 ChangeStackingLevel(menu
->frame
->core
, (!menu
->parent
? WMMainMenuLevel
2256 wRaiseFrame(menu
->frame
->core
);
2257 menu
->flags
.lowered
= 0;
2259 ChangeStackingLevel(menu
->frame
->core
, WMNormalLevel
);
2260 wLowerFrame(menu
->frame
->core
);
2261 menu
->flags
.lowered
= 1;
2263 for (i
=0; i
<menu
->cascade_no
; i
++) {
2264 if (menu
->cascades
[i
]
2265 && !menu
->cascades
[i
]->flags
.buttoned
2266 && menu
->cascades
[i
]->flags
.lowered
!=lower
) {
2267 changeMenuLevels(menu
->cascades
[i
], lower
);
2275 menuTitleDoubleClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2280 if (event
->xbutton
.state
& MOD_MASK
) {
2281 if (menu
->flags
.lowered
) {
2286 changeMenuLevels(menu
, lower
);
2292 menuTitleMouseDown(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2297 int x
=menu
->frame_x
, y
=menu
->frame_y
;
2298 int dx
=event
->xbutton
.x_root
, dy
=event
->xbutton
.y_root
;
2303 printf("Moving menu\n");
2306 /* can't touch the menu copy */
2307 if (menu
->flags
.brother
)
2310 if (event
->xbutton
.button
!= Button1
&& event
->xbutton
.button
!= Button2
)
2313 if (event
->xbutton
.state
& MOD_MASK
) {
2314 wLowerFrame(menu
->frame
->core
);
2317 wRaiseFrame(menu
->frame
->core
);
2322 /* lower/raise all submenus */
2324 if (tmp
->selected_entry
>=0 && tmp
->cascades
2325 && tmp
->entries
[tmp
->selected_entry
]->cascade
>=0) {
2326 tmp
= tmp
->cascades
[tmp
->entries
[tmp
->selected_entry
]->cascade
];
2327 if (!tmp
|| !tmp
->flags
.mapped
)
2330 wLowerFrame(tmp
->frame
->core
);
2332 wRaiseFrame(tmp
->frame
->core
);
2338 /* tear off the menu if it's a root menu or a cascade
2340 if (!menu
->flags
.buttoned
&& !menu
->flags
.brother
2341 && (!menu
->flags
.app_menu
||menu
->parent
!=NULL
)) {
2342 menu
->flags
.buttoned
=1;
2343 wFrameWindowShowButton(menu
->frame
, WFF_RIGHT_BUTTON
);
2345 /* turn off selected menu entry in parent menu */
2346 selectEntry(menu
->parent
, -1);
2348 /* make parent map the copy in place of the original */
2349 for (i
=0; i
<menu
->parent
->cascade_no
; i
++) {
2350 if (menu
->parent
->cascades
[i
] == menu
) {
2351 menu
->parent
->cascades
[i
] = menu
->brother
;
2360 WMMaskEvent(dpy
, ButtonMotionMask
|ButtonReleaseMask
|ButtonPressMask
2361 |ExposureMask
, &ev
);
2365 x
+= ev
.xmotion
.x_root
- dx
;
2366 y
+= ev
.xmotion
.y_root
- dy
;
2367 dx
= ev
.xmotion
.x_root
;
2368 dy
= ev
.xmotion
.y_root
;
2369 wMenuMove(menu
, x
, y
, True
);
2371 if (abs(ev
.xmotion
.x_root
- dx
) > MOVE_THRESHOLD
2372 || abs(ev
.xmotion
.y_root
- dy
) > MOVE_THRESHOLD
) {
2374 XGrabPointer(dpy
, menu
->frame
->titlebar
->window
, False
,
2375 ButtonMotionMask
|ButtonReleaseMask
2377 GrabModeAsync
, GrabModeAsync
, None
,
2378 wCursor
[WCUR_MOVE
], CurrentTime
);
2387 if (ev
.xbutton
.button
!= event
->xbutton
.button
)
2390 printf("End menu move\n");
2392 XUngrabPointer(dpy
, CurrentTime
);
2403 *----------------------------------------------------------------------
2405 * Handles mouse click on the close button of menus. The menu is
2406 * closed when the button is clicked.
2409 * The closed menu is reinserted at it's parent menus
2411 *----------------------------------------------------------------------
2414 menuCloseClick(WCoreWindow
*sender
, void *data
, XEvent
*event
)
2416 WMenu
*menu
= (WMenu
*)data
;
2417 WMenu
*parent
= menu
->parent
;
2421 for (i
=0; i
<parent
->cascade_no
; i
++) {
2422 /* find the entry that points to the copy */
2423 if (parent
->cascades
[i
] == menu
->brother
) {
2424 /* make it point to the original */
2425 parent
->cascades
[i
] = menu
;
2426 menu
->parent
= parent
;
2436 wMenuSaveState(WScreen
*scr
)
2438 proplist_t menus
, key
, value
;
2442 menus
= PLMakeDictionaryFromEntries(NULL
, NULL
, NULL
);
2445 if (scr
->root_menu
&& scr
->root_menu
->flags
.buttoned
) {
2446 sprintf(buffer
, "%i,%i", scr
->root_menu
->frame_x
,
2447 scr
->root_menu
->frame_y
);
2448 key
= PLMakeString("RootMenu");
2449 value
= PLMakeString(buffer
);
2450 PLInsertDictionaryEntry(menus
, key
, value
);
2456 if (scr
->switch_menu
&& scr
->switch_menu
->flags
.buttoned
) {
2457 sprintf(buffer
, "%i,%i", scr
->switch_menu
->frame_x
,
2458 scr
->switch_menu
->frame_y
);
2459 key
= PLMakeString("SwitchMenu");
2460 value
= PLMakeString(buffer
);
2461 PLInsertDictionaryEntry(menus
, key
, value
);
2467 if (scr
->workspace_menu
&& scr
->workspace_menu
->flags
.buttoned
) {
2468 sprintf(buffer
, "%i,%i", scr
->workspace_menu
->frame_x
,
2469 scr
->workspace_menu
->frame_y
);
2470 key
= PLMakeString("WorkspaceMenu");
2471 value
= PLMakeString(buffer
);
2472 PLInsertDictionaryEntry(menus
, key
, value
);
2479 key
= PLMakeString("Menus");
2480 PLInsertDictionaryEntry(scr
->session_state
, key
, menus
);
2487 #define COMPLAIN(key) wwarning(_("bad value in menus state info:%s"), key)
2491 restoreMenu(WScreen
*scr
, proplist_t menu
, int which
)
2494 WMenu
*pmenu
= NULL
;
2499 if (!PLIsString(menu
)) {
2500 COMPLAIN("Position");
2504 if (sscanf(PLGetString(menu
), "%i,%i", &x
, &y
)!=2)
2505 COMPLAIN("Position");
2508 if (which
& WSS_ROOTMENU
) {
2509 OpenRootMenu(scr
, x
, y
, False
);
2510 pmenu
= scr
->root_menu
;
2511 } else if (which
& WSS_SWITCHMENU
) {
2512 OpenSwitchMenu(scr
, x
, y
, False
);
2513 pmenu
= scr
->switch_menu
;
2516 if (which
& WSS_WSMENU
) {
2517 OpenWorkspaceMenu(scr
, x
, y
);
2518 pmenu
= scr
->workspace_menu
;
2519 if (pmenu
->parent
) {
2520 /* make parent map the copy in place of the original */
2521 for (i
=0; i
<pmenu
->parent
->cascade_no
; i
++) {
2522 if (pmenu
->parent
->cascades
[i
] == pmenu
) {
2523 pmenu
->parent
->cascades
[i
] = pmenu
->brother
;
2531 int width
= MENUW(pmenu
);
2532 int height
= MENUH(pmenu
);
2534 x
= (x
< -width
) ? 0 : x
;
2535 x
= (x
> scr
->scr_width
) ? scr
->scr_width
- width
: x
;
2536 y
= (y
< 0) ? 0 : y
;
2537 y
= (y
> scr
->scr_height
) ? scr
->scr_height
- height
: y
;
2538 wMenuMove(pmenu
, x
, y
, True
);
2539 pmenu
->flags
.buttoned
= 1;
2540 wFrameWindowShowButton(pmenu
->frame
, WFF_RIGHT_BUTTON
);
2548 wMenuRestoreState(WScreen
*scr
)
2550 proplist_t menus
, menu
, key
, rkey
, skey
, wkey
;
2552 key
= PLMakeString("Menus");
2553 menus
= PLGetDictionaryEntry(scr
->session_state
, key
);
2561 rkey
= PLMakeString("RootMenu");
2562 skey
= PLMakeString("SwitchMenu");
2563 wkey
= PLMakeString("WorkspaceMenu");
2564 menu
= PLGetDictionaryEntry(menus
, rkey
);
2565 restoreMenu(scr
, menu
, WSS_ROOTMENU
);
2566 menu
= PLGetDictionaryEntry(menus
, skey
);
2567 restoreMenu(scr
, menu
, WSS_SWITCHMENU
);
2568 menu
= PLGetDictionaryEntry(menus
, wkey
);
2569 restoreMenu(scr
, menu
, WSS_WSMENU
);
2578 OpenWorkspaceMenu(WScreen
*scr
, int x
, int y
)
2580 WMenu
*menu
, *parent
;
2584 if (!scr
->root_menu
) {
2585 OpenRootMenu(scr
, scr
->scr_width
*2, 0, False
);
2586 wMenuUnmap(scr
->root_menu
);
2589 menu
= scr
->workspace_menu
;
2591 if (menu
->flags
.mapped
) {
2592 if (!menu
->flags
.buttoned
) {
2594 parent
= menu
->parent
;
2595 if (parent
&& parent
->selected_entry
>= 0) {
2596 entry
= parent
->entries
[parent
->selected_entry
];
2597 if (parent
->cascades
[entry
->cascade
] == menu
) {
2598 selectEntry(parent
, -1);
2599 wMenuMapAt(menu
, x
, y
, False
);
2603 wRaiseFrame(menu
->frame
->core
);
2604 wMenuMapCopyAt(menu
, x
, y
);
2608 wMenuMapAt(menu
, x
, y
, False
);