4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
15 #include <libtu/minmax.h>
16 #include <libtu/objp.h>
17 #include <libtu/obj.h>
18 #include <libmainloop/defer.h>
19 #include <libmainloop/signal.h>
21 #include <ioncore/common.h>
22 #include <ioncore/window.h>
23 #include <ioncore/global.h>
24 #include <ioncore/regbind.h>
25 #include <ioncore/strings.h>
26 #include <ioncore/pointer.h>
27 #include <ioncore/focus.h>
28 #include <ioncore/event.h>
29 #include <ioncore/xwindow.h>
30 #include <ioncore/names.h>
31 #include <ioncore/gr.h>
32 #include <ioncore/gr-util.h>
37 #define MENU_WIN(MENU) ((MENU)->win.win)
43 static bool extl_table_getis(ExtlTab tab, int i, const char *s, char c,
49 if(!extl_table_geti_t(tab, i, &sub))
51 ret=extl_table_get(sub, 's', c, s, p);
52 extl_unref_table(sub);
60 /*{{{ Drawing routines */
63 static void get_outer_geom(WMenu *menu, WRectangle *geom)
67 geom->w=REGION_GEOM(menu).w;
68 geom->h=REGION_GEOM(menu).h;
72 static void get_inner_geom(WMenu *menu, WRectangle *geom)
76 get_outer_geom(menu, geom);
78 if(menu->brush!=NULL){
79 grbrush_get_border_widths(menu->brush, &bdw);
82 geom->w-=bdw.left+bdw.right;
83 geom->h-=bdw.top+bdw.bottom;
84 geom->w=maxof(0, geom->w);
85 geom->h=maxof(0, geom->h);
93 GR_DEFATTR(unselected);
98 static void init_attr()
101 GR_ALLOCATTR(active);
102 GR_ALLOCATTR(inactive);
103 GR_ALLOCATTR(selected);
104 GR_ALLOCATTR(unselected);
105 GR_ALLOCATTR(normal);
106 GR_ALLOCATTR(submenu);
111 static void menu_draw_entry(WMenu *menu, int i, const WRectangle *igeom,
117 aa=(REGION_IS_ACTIVE(menu) ? GR_ATTR(active) : GR_ATTR(inactive));
118 sa=(menu->selected_entry==i ? GR_ATTR(selected) : GR_ATTR(unselected));
120 if(menu->entry_brush==NULL)
124 geom.h=menu->entry_h;
125 geom.y+=(i-menu->first_entry)*(menu->entry_h+menu->entry_spacing);
127 grbrush_begin(menu->entry_brush, &geom, GRBRUSH_AMEND|GRBRUSH_KEEP_ATTR);
129 grbrush_init_attr(menu->entry_brush, &menu->entries[i].attr);
131 grbrush_set_attr(menu->entry_brush, aa);
132 grbrush_set_attr(menu->entry_brush, sa);
134 grbrush_draw_textbox(menu->entry_brush, &geom, menu->entries[i].title,
137 grbrush_end(menu->entry_brush);
141 void menu_draw_entries(WMenu *menu, bool complete)
146 if(menu->entry_brush==NULL)
149 get_inner_geom(menu, &igeom);
151 mx=menu->first_entry+menu->vis_entries;
152 mx=(mx < menu->n_entries ? mx : menu->n_entries);
154 for(i=menu->first_entry; i<mx; i++)
155 menu_draw_entry(menu, i, &igeom, complete);
159 void menu_draw(WMenu *menu, bool complete)
161 GrAttr aa=(REGION_IS_ACTIVE(menu) ? GR_ATTR(active) : GR_ATTR(inactive));
164 if(menu->brush==NULL)
167 get_outer_geom(menu, &geom);
169 grbrush_begin(menu->brush, &geom,
170 (complete ? 0 : GRBRUSH_NO_CLEAR_OK));
172 grbrush_set_attr(menu->brush, aa);
174 grbrush_draw_border(menu->brush, &geom);
176 menu_draw_entries(menu, FALSE);
178 grbrush_end(menu->brush);
188 static void menu_calc_size(WMenu *menu, bool maxexact,
190 int *w_ret, int *h_ret)
192 GrBorderWidths bdw, e_bdw;
195 int nath, bdh, maxew=menu->max_entry_w;
197 grbrush_get_border_widths(menu->brush, &bdw);
198 grbrush_get_border_widths(menu->entry_brush, &e_bdw);
200 if(maxexact || maxew>maxw-(int)bdw.left-(int)bdw.right){
201 maxew=maxw-bdw.left-bdw.right;
204 *w_ret=maxew+bdw.left+bdw.right;
207 bdh=bdw.top+bdw.bottom;
209 if(menu->n_entries==0){
210 *h_ret=(maxexact ? maxh : bdh);
214 int vis=(maxh-bdh+e_bdw.spacing)/(e_bdw.spacing+menu->entry_h);
215 if(vis>menu->n_entries){
218 }else if(menu->selected_entry>=0){
219 if(menu->selected_entry<menu->first_entry)
220 menu->first_entry=menu->selected_entry;
221 else if(menu->selected_entry>=menu->first_entry+vis)
222 menu->first_entry=menu->selected_entry-vis+1;
226 menu->vis_entries=vis;
230 *h_ret=vis*menu->entry_h+(vis-1)*e_bdw.spacing+bdh;
233 /* Calculate new shortened entry names */
234 maxew-=e_bdw.left+e_bdw.right;
236 for(i=0; i<menu->n_entries; i++){
237 if(menu->entries[i].title){
238 free(menu->entries[i].title);
239 menu->entries[i].title=NULL;
244 if(extl_table_getis(menu->tab, i+1, "name", 's', &str)){
245 menu->entries[i].title=grbrush_make_label(menu->entry_brush,
253 void calc_size(WMenu *menu, int *w, int *h)
255 if(menu->pmenu_mode){
256 menu_calc_size(menu, FALSE, INT_MAX, INT_MAX, w, h);
258 menu_calc_size(menu, !(menu->last_fp.mode®ION_FIT_BOUNDS),
259 menu->last_fp.g.w, menu->last_fp.g.h, w, h);
264 /* Return offset from bottom-left corner of containing mplex or top-right
265 * corner of parent menu for the respective corner of menu.
267 static void get_placement_offs(WMenu *menu, int *xoff, int *yoff)
274 if(menu->brush!=NULL){
275 grbrush_get_border_widths(menu->brush, &bdw);
280 if(menu->entry_brush!=NULL){
281 grbrush_get_border_widths(menu->entry_brush, &bdw);
288 #define MINIMUM_Y_VISIBILITY 20
289 #define POINTER_OFFSET 5
291 static void menu_firstfit(WMenu *menu, bool submenu, const WRectangle *refg)
295 calc_size(menu, &(geom.w), &(geom.h));
297 if(!(menu->last_fp.mode®ION_FIT_BOUNDS)){
298 geom.x=menu->last_fp.g.x;
299 geom.y=menu->last_fp.g.y;
300 }else if(menu->pmenu_mode){
305 const WRectangle *maxg =
306 ®ION_GEOM(REGION_PARENT((WRegion*)menu));
309 geom.y+=POINTER_OFFSET;
311 if(geom.y+MINIMUM_Y_VISIBILITY>maxg->y+maxg->h){
312 geom.y=maxg->y+maxg->h-MINIMUM_Y_VISIBILITY;
313 geom.x=refg->x+POINTER_OFFSET;
314 if(geom.x+geom.w>maxg->x+maxg->w)
315 geom.x=refg->x-geom.w-POINTER_OFFSET;
319 else if(geom.x+geom.w>maxg->x+maxg->w)
320 geom.x=maxg->x+maxg->w-geom.w;
324 const WRectangle *maxg=&(menu->last_fp.g);
326 int l, r, t, b, xoff, yoff;
328 get_placement_offs(menu, &xoff, &yoff);
330 r=refg->x+refg->w+xoff;
332 b=refg->y+refg->h-yoff;
334 geom.x=maxof(l, r-geom.w);
335 if(geom.x+geom.w>maxg->x+maxg->w)
338 geom.y=minof(b-geom.h, t);
343 geom.y=maxg->y+maxg->h-geom.h;
347 window_do_fitrep(&menu->win, NULL, &geom);
351 static void menu_do_refit(WMenu *menu, WWindow *par, const WFitParams *oldfp)
355 calc_size(menu, &(geom.w), &(geom.h));
357 if(!(menu->last_fp.mode®ION_FIT_BOUNDS)){
358 geom.x=menu->last_fp.g.x;
359 geom.y=menu->last_fp.g.y;
360 }else if(menu->pmenu_mode){
361 geom.x=REGION_GEOM(menu).x;
362 geom.y=REGION_GEOM(menu).y;
364 const WRectangle *maxg=&(menu->last_fp.g);
365 int xdiff=REGION_GEOM(menu).x-oldfp->g.x;
366 int ydiff=(REGION_GEOM(menu).y+REGION_GEOM(menu).h
367 -(oldfp->g.y+oldfp->g.h));
368 geom.x=maxof(0, minof(maxg->x+xdiff, maxg->x+maxg->w-geom.w));
369 geom.y=maxof(0, minof(maxg->y+maxg->h+ydiff, maxg->y+maxg->h)-geom.h);
372 window_do_fitrep(&menu->win, par, &geom);
376 bool menu_fitrep(WMenu *menu, WWindow *par, const WFitParams *fp)
380 if(par!=NULL && !region_same_rootwin((WRegion*)par, (WRegion*)menu))
385 menu_do_refit(menu, par, &oldfp);
387 if(menu->submenu!=NULL && !menu->pmenu_mode)
388 region_fitrep((WRegion*)(menu->submenu), par, fp);
397 /*{{{ Brush update */
400 static void calc_entry_dimens(WMenu *menu)
402 int i, n=extl_table_get_n(menu->tab);
409 if(extl_table_gets_s(menu->tab, title, &str)){
410 maxw=grbrush_get_text_width(title_brush, str, strlen(str));
416 if(extl_table_getis(menu->tab, i, "name", 's', &str)){
417 int w=grbrush_get_text_width(menu->entry_brush,
425 grbrush_get_border_widths(menu->entry_brush, &bdw);
426 grbrush_get_font_extents(menu->entry_brush, &fnte);
428 menu->max_entry_w=maxw+bdw.left+bdw.right;
429 menu->entry_h=fnte.max_height+bdw.top+bdw.bottom;
430 menu->entry_spacing=bdw.spacing;
434 static bool menu_init_gr(WMenu *menu, WRootWin *rootwin, Window win)
436 GrBrush *brush, *entry_brush;
438 const char *style=(menu->big_mode
442 : "input-menu-normal"));
444 const char *entry_style=(menu->big_mode
445 ? "tab-menuentry-big"
447 ? "tab-menuentry-pmenu"
448 : "tab-menuentry-normal"));
450 brush=gr_get_brush(win, rootwin, style);
455 entry_brush=grbrush_get_slave(brush, rootwin, entry_style);
457 if(entry_brush==NULL){
458 grbrush_release(brush);
462 if(menu->entry_brush!=NULL)
463 grbrush_release(menu->entry_brush);
464 if(menu->brush!=NULL)
465 grbrush_release(menu->brush);
468 menu->entry_brush=entry_brush;
470 calc_entry_dimens(menu);
476 void menu_updategr(WMenu *menu)
478 if(!menu_init_gr(menu, region_rootwin_of((WRegion*)menu),
483 menu_do_refit(menu, NULL, &(menu->last_fp));
485 region_updategr_default((WRegion*)menu);
487 window_draw((WWindow*)menu, TRUE);
491 static void menu_release_gr(WMenu *menu)
493 if(menu->entry_brush!=NULL){
494 grbrush_release(menu->entry_brush);
495 menu->entry_brush=NULL;
497 if(menu->brush!=NULL){
498 grbrush_release(menu->brush);
510 static WMenuEntry *preprocess_menu(ExtlTab tab, int *n_entries)
516 n=extl_table_get_n(tab);
522 entries=ALLOC_N(WMenuEntry, n);
529 /* Initialise entries and check submenus */
531 WMenuEntry *ent=&entries[i-1];
536 gr_stylespec_init(&ent->attr);
538 if(extl_table_geti_t(tab, i, &entry)){
543 if(extl_table_gets_s(entry, "attr", &attr)){
544 gr_stylespec_load_(&ent->attr, attr, TRUE);
548 if(extl_table_gets_f(entry, "submenu_fn", &fn)){
549 ent->flags|=WMENUENTRY_SUBMENU;
551 }else if(extl_table_gets_t(entry, "submenu", &sub)){
552 ent->flags|=WMENUENTRY_SUBMENU;
553 extl_unref_table(sub);
556 if(ent->flags&WMENUENTRY_SUBMENU)
557 gr_stylespec_set(&ent->attr, GR_ATTR(submenu));
559 extl_unref_table(entry);
567 static void deinit_entries(WMenu *menu);
570 bool menu_init(WMenu *menu, WWindow *par, const WFitParams *fp,
571 const WMenuCreateParams *params)
576 menu->entries=preprocess_menu(params->tab, &(menu->n_entries));
578 if(menu->entries==NULL){
579 warn(TR("Empty menu."));
583 menu->tab=extl_ref_table(params->tab);
584 menu->handler=extl_ref_fn(params->handler);
585 menu->pmenu_mode=params->pmenu_mode;
586 menu->big_mode=params->big_mode;
590 if(params->pmenu_mode){
591 menu->selected_entry=-1;
593 menu->selected_entry=params->initial-1;
594 if(menu->selected_entry<0)
595 menu->selected_entry=0;
596 if(params->initial > menu->n_entries)
597 menu->selected_entry=0;
603 menu->entry_brush=NULL;
604 menu->entry_spacing=0;
605 menu->vis_entries=menu->n_entries;
608 menu->typeahead=NULL;
613 if(!window_init((WWindow*)menu, par, fp))
618 if(!menu_init_gr(menu, region_rootwin_of((WRegion*)par), win))
623 menu_firstfit(menu, params->submenu_mode, &(params->refg));
625 window_select_input(&(menu->win), IONCORE_EVENTMASK_NORMAL);
627 region_add_bindmap((WRegion*)menu, mod_menu_menu_bindmap);
629 region_register((WRegion*)menu);
634 window_deinit((WWindow*)menu);
636 extl_unref_table(menu->tab);
637 extl_unref_fn(menu->handler);
638 deinit_entries(menu);
643 WMenu *create_menu(WWindow *par, const WFitParams *fp,
644 const WMenuCreateParams *params)
646 CREATEOBJ_IMPL(WMenu, menu, (p, par, fp, params));
650 static void deinit_entries(WMenu *menu)
654 for(i=0; i<menu->n_entries; i++){
655 gr_stylespec_unalloc(&menu->entries[i].attr);
656 if(menu->entries[i].title!=NULL)
657 free(menu->entries[i].title);
664 void menu_deinit(WMenu *menu)
666 menu_typeahead_clear(menu);
668 if(menu->submenu!=NULL)
669 destroy_obj((Obj*)menu->submenu);
671 extl_unref_table(menu->tab);
672 extl_unref_fn(menu->handler);
674 deinit_entries(menu);
676 menu_release_gr(menu);
678 window_deinit((WWindow*)menu);
688 static void menu_inactivated(WMenu *menu)
690 window_draw((WWindow*)menu, FALSE);
694 static void menu_activated(WMenu *menu)
696 window_draw((WWindow*)menu, FALSE);
706 static WMenu *menu_head(WMenu *menu)
708 WMenu *m=REGION_MANAGER_CHK(menu, WMenu);
709 return (m==NULL ? menu : menu_head(m));
713 static WMenu *menu_tail(WMenu *menu)
715 return (menu->submenu==NULL ? menu : menu_tail(menu->submenu));
719 static void menu_managed_remove(WMenu *menu, WRegion *sub)
721 bool mcf=region_may_control_focus((WRegion*)menu);
723 if(sub!=(WRegion*)menu->submenu)
728 region_unset_manager(sub, (WRegion*)menu);
731 region_do_set_focus((WRegion*)menu, FALSE);
735 int get_sub_y_off(WMenu *menu, int n)
737 /* top + sum of above entries and spacings - top_of_sub */
738 return (menu->entry_h+menu->entry_spacing)*(n-menu->first_entry);
742 static void show_sub(WMenu *menu, int n)
745 WMenuCreateParams fnp;
749 par=REGION_PARENT(menu);
756 fnp.pmenu_mode=menu->pmenu_mode;
757 fnp.big_mode=menu->big_mode;
758 fnp.submenu_mode=TRUE;
760 if(menu->pmenu_mode){
761 fnp.refg.x=REGION_GEOM(menu).x+REGION_GEOM(menu).w;
762 fnp.refg.y=REGION_GEOM(menu).y+get_sub_y_off(menu, n);
766 fnp.refg=REGION_GEOM(menu);
769 fnp.tab=extl_table_none();
772 if(extl_table_getis(menu->tab, n+1, "submenu_fn", 'f', &fn)){
774 extl_call(fn, NULL, "t", &(fnp.tab));
775 extl_unprotect(NULL);
778 extl_table_getis(menu->tab, n+1, "submenu", 't', &(fnp.tab));
780 if(fnp.tab==extl_table_none())
784 fnp.handler=extl_ref_fn(menu->handler);
789 if(extl_table_getis(menu->tab, n+1, "initial", 'f', &fn)){
791 extl_call(fn, NULL, "i", &(fnp.initial));
792 extl_unprotect(NULL);
795 extl_table_getis(menu->tab, n+1, "initial", 'i', &(fnp.initial));
799 submenu=create_menu(par, &fp, &fnp);
804 menu->submenu=submenu;
805 region_set_manager((WRegion*)submenu, (WRegion*)menu);
807 region_restack((WRegion*)submenu, MENU_WIN(menu), Above);
808 region_map((WRegion*)submenu);
810 if(!menu->pmenu_mode && region_may_control_focus((WRegion*)menu))
811 region_do_set_focus((WRegion*)submenu, FALSE);
815 static void menu_do_set_focus(WMenu *menu, bool warp)
817 if(menu->submenu!=NULL)
818 region_do_set_focus((WRegion*)menu->submenu, warp);
820 window_do_set_focus((WWindow*)menu, warp);
824 void menu_restack(WMenu *menu, Window other, int mode)
826 xwindow_restack(MENU_WIN(menu), other, mode);
827 if(menu->submenu!=NULL)
828 region_restack((WRegion*)(menu->submenu), MENU_WIN(menu), Above);
832 void menu_stacking(WMenu *menu, Window *bottomret, Window *topret)
836 if(menu->submenu!=NULL)
837 region_stacking((WRegion*)(menu->submenu), bottomret, topret);
839 *bottomret=MENU_WIN(menu);
841 *topret=MENU_WIN(menu);
852 static void menu_do_select_nth(WMenu *menu, int n)
854 int oldn=menu->selected_entry;
860 if(menu->submenu!=NULL)
861 destroy_obj((Obj*)menu->submenu);
863 assert(menu->submenu==NULL);
865 menu->selected_entry=n;
868 if(n<menu->first_entry){
871 }else if(n>=menu->first_entry+menu->vis_entries){
872 menu->first_entry=n-menu->vis_entries+1;
876 if(menu->entries[n].flags&WMENUENTRY_SUBMENU &&
883 menu_draw_entries(menu, TRUE);
885 /* redraw new and old selected entry */
887 get_inner_geom(menu, &igeom);
891 menu_draw_entry(menu, oldn, &igeom, TRUE);
893 menu_draw_entry(menu, n, &igeom, TRUE);
899 * Select \var{n}:th entry in menu.
902 void menu_select_nth(WMenu *menu, int n)
906 if(n>=menu->n_entries)
909 menu_typeahead_clear(menu);
910 menu_do_select_nth(menu, n);
915 * Select previous entry in menu.
918 void menu_select_prev(WMenu *menu)
920 menu_select_nth(menu, (menu->selected_entry<=0
922 : menu->selected_entry-1));
927 * Select next entry in menu.
930 void menu_select_next(WMenu *menu)
932 menu_select_nth(menu, (menu->selected_entry+1)%menu->n_entries);
936 static void menu_do_finish(WMenu *menu)
941 WMenu *head=menu_head(menu);
943 handler=menu->handler;
944 menu->handler=extl_fn_none();
946 ok=extl_table_geti_t(menu->tab, menu->selected_entry+1, &tab);
948 if(region_manager_allows_destroying((WRegion*)head))
949 destroy_obj((Obj*)head);
950 else if(head->submenu!=NULL)
951 destroy_obj((Obj*)head->submenu);
954 extl_call(handler, "t", NULL, tab);
956 extl_unref_fn(handler);
957 extl_unref_table(tab);
962 * If selected entry is a submenu, display that.
963 * Otherwise destroy the menu and call handler for selected entry.
966 void menu_finish(WMenu *menu)
968 menu_typeahead_clear(menu);
970 if(!menu->pmenu_mode && menu->selected_entry>=0 &&
971 menu->entries[menu->selected_entry].flags&WMENUENTRY_SUBMENU){
972 show_sub(menu, menu->selected_entry);
976 mainloop_defer_action((Obj*)menu, (WDeferredAction*)menu_do_finish);
982 * Close \var{menu} not calling any possible finish handlers.
985 void menu_cancel(WMenu *menu)
987 if(region_manager_allows_destroying((WRegion*)menu))
988 region_dispose_((WRegion*)menu);
997 static int scroll_time=20;
998 static int scroll_amount=3;
999 static WTimer *scroll_timer=NULL;
1002 static void reset_scroll_timer()
1004 if(scroll_timer!=NULL){
1005 destroy_obj((Obj*)scroll_timer);
1012 * Set module basic settings. The parameter table may contain the
1015 * \begin{tabularx}{\linewidth}{lX}
1016 * \tabhead{Field & Description}
1017 * \var{scroll_amount} & Number of pixels to scroll at a time
1018 * pointer-controlled menus when one extends
1019 * beyond a border of the screen and the pointer
1020 * touches that border. \\
1021 * \var{scroll_delay} & Time between such scrolling events in
1026 void mod_menu_set(ExtlTab tab)
1030 if(extl_table_gets_i(tab, "scroll_amount", &a))
1031 scroll_amount=maxof(0, a);
1032 if(extl_table_gets_i(tab, "scroll_delay", &t))
1033 scroll_time=maxof(0, t);
1038 * Get module basic settings. For details, see \fnref{mod_menu.set}.
1042 ExtlTab mod_menu_get()
1044 ExtlTab tab=extl_create_table();
1045 extl_table_sets_i(tab, "scroll_amount", scroll_amount);
1046 extl_table_sets_i(tab, "scroll_delay", scroll_time);
1059 static int calc_diff(const WRectangle *mg, const WRectangle *pg, int d)
1063 return mg->x+mg->w-pg->w;
1065 return mg->y+mg->h-pg->h;
1075 static int scrolld_subs(WMenu *menu, int d)
1078 WRegion *p=REGION_PARENT_REG(menu);
1079 const WRectangle *pg;
1087 diff=maxof(diff, calc_diff(®ION_GEOM(menu), pg, d));
1091 return minof(maxof(0, diff), scroll_amount);
1095 static void menu_select_entry_at(WMenu *menu, int px, int py);
1098 static void do_scroll(WMenu *menu, int xd, int yd)
1103 xwindow_pointer_pos(region_root_of((WRegion*)menu), &px, &py);
1106 g=REGION_GEOM(menu);
1110 window_do_fitrep((WWindow*)menu, NULL, &g);
1112 menu_select_entry_at(menu, px, py);
1119 static void scroll_left(WTimer *timer, WMenu *menu)
1122 do_scroll(menu, -scrolld_subs(menu, D_LEFT), 0);
1123 if(scrolld_subs(menu, D_LEFT)>0){
1124 timer_set(timer, scroll_time, (WTimerHandler*)scroll_left,
1132 static void scroll_up(WTimer *timer, WMenu *menu)
1135 do_scroll(menu, 0, -scrolld_subs(menu, D_UP));
1136 if(scrolld_subs(menu, D_UP)>0){
1137 timer_set(timer, scroll_time, (WTimerHandler*)scroll_up,
1146 static void scroll_right(WTimer *timer, WMenu *menu)
1149 do_scroll(menu, scrolld_subs(menu, D_RIGHT), 0);
1150 if(scrolld_subs(menu, D_RIGHT)>0){
1151 timer_set(timer, scroll_time, (WTimerHandler*)scroll_right,
1159 static void scroll_down(WTimer *timer, WMenu *menu)
1162 do_scroll(menu, 0, scrolld_subs(menu, D_DOWN));
1163 if(scrolld_subs(menu, D_DOWN)>0){
1164 timer_set(timer, scroll_time, (WTimerHandler*)scroll_down,
1172 static void end_scroll(WMenu *menu)
1174 reset_scroll_timer();
1177 #define SCROLL_OFFSET 10
1179 static void check_scroll(WMenu *menu, int x, int y)
1181 WRegion *parent=REGION_PARENT_REG(menu);
1183 WTimerHandler *fn=NULL;
1185 if(!menu->pmenu_mode)
1193 region_rootpos(parent, &rx, &ry);
1197 if(x<=SCROLL_OFFSET){
1198 fn=(WTimerHandler*)scroll_right;
1199 }else if(y<=SCROLL_OFFSET){
1200 fn=(WTimerHandler*)scroll_down;
1201 }else if(x>=REGION_GEOM(parent).w-SCROLL_OFFSET){
1202 fn=(WTimerHandler*)scroll_left;
1203 }else if(y>=REGION_GEOM(parent).h-SCROLL_OFFSET){
1204 fn=(WTimerHandler*)scroll_up;
1212 if(scroll_timer!=NULL){
1213 if(scroll_timer->handler==(WTimerHandler*)fn &&
1214 timer_is_set(scroll_timer)){
1218 scroll_timer=create_timer();
1219 if(scroll_timer==NULL)
1223 fn(scroll_timer, (Obj*)menu_head(menu));
1230 /*{{{ Pointer handlers */
1233 int menu_entry_at_root(WMenu *menu, int root_x, int root_y)
1235 int rx, ry, x, y, entry;
1237 region_rootpos((WRegion*)menu, &rx, &ry);
1239 get_inner_geom(menu, &ig);
1244 if(x<0 || x>=ig.w || y<0 || y>=ig.h)
1247 entry=y/(menu->entry_h+menu->entry_spacing);
1248 if(entry<0 || entry>=menu->vis_entries)
1250 return entry+menu->first_entry;
1254 int menu_entry_at_root_tree(WMenu *menu, int root_x, int root_y,
1259 menu=menu_tail(menu);
1263 if(!menu->pmenu_mode)
1264 return menu_entry_at_root(menu, root_x, root_y);
1267 entry=menu_entry_at_root(menu, root_x, root_y);
1272 menu=REGION_MANAGER_CHK(menu, WMenu);
1279 static void menu_select_entry_at(WMenu *menu, int px, int py)
1281 int entry=menu_entry_at_root_tree(menu, px, py, &menu);
1283 menu_do_select_nth(menu, entry);
1287 void menu_release(WMenu *menu, XButtonEvent *ev)
1289 int entry=menu_entry_at_root_tree(menu, ev->x_root, ev->y_root, &menu);
1292 menu_select_nth(menu, entry);
1294 }else if(menu->pmenu_mode){
1295 menu_cancel(menu_head(menu));
1300 void menu_motion(WMenu *menu, XMotionEvent *ev, int dx, int dy)
1302 menu_select_entry_at(menu, ev->x_root, ev->y_root);
1303 check_scroll(menu, ev->x_root, ev->y_root);
1307 void menu_button(WMenu *menu, XButtonEvent *ev)
1309 int entry=menu_entry_at_root_tree(menu, ev->x_root, ev->y_root, &menu);
1311 menu_select_nth(menu, entry);
1315 int menu_press(WMenu *menu, XButtonEvent *ev, WRegion **reg_ret)
1317 menu_button(menu, ev);
1318 menu=menu_head(menu);
1319 ioncore_set_drag_handlers((WRegion*)menu,
1321 (WMotionHandler*)menu_motion,
1322 (WButtonHandler*)menu_release,
1332 /*{{{ Typeahead find */
1335 static void menu_insstr(WMenu *menu, const char *buf, size_t n)
1337 size_t oldlen=(menu->typeahead==NULL ? 0 : strlen(menu->typeahead));
1338 char *newta=(char*)malloc(oldlen+n+1);
1346 memcpy(newta, menu->typeahead, oldlen);
1348 memcpy(newta+oldlen, buf, n);
1349 newta[oldlen+n]='\0';
1352 while(*newta!='\0'){
1354 entry=menu->selected_entry;
1356 if(menu->entries[entry].title!=NULL){
1357 size_t l=strlen(menu->entries[entry].title);
1358 if(libtu_strcasestr(menu->entries[entry].title, newta)){
1363 entry=(entry+1)%menu->n_entries;
1364 }while(entry!=menu->selected_entry);
1366 menu_do_select_nth(menu, entry);
1372 if(newta_orig!=newta){
1377 char *p=scopy(newta);
1382 if(menu->typeahead!=NULL)
1383 free(menu->typeahead);
1384 menu->typeahead=newta;
1389 * Clear typeahead buffer.
1392 void menu_typeahead_clear(WMenu *menu)
1394 if(menu->typeahead!=NULL){
1395 free(menu->typeahead);
1396 menu->typeahead=NULL;
1404 /*{{{ Dynamic function table and class implementation */
1407 static DynFunTab menu_dynfuntab[]={
1408 {(DynFun*)region_fitrep, (DynFun*)menu_fitrep},
1409 {region_updategr, menu_updategr},
1410 {window_draw, menu_draw},
1411 {(DynFun*)window_press, (DynFun*)menu_press},
1412 {region_managed_remove, menu_managed_remove},
1413 {region_do_set_focus, menu_do_set_focus},
1414 {region_activated, menu_activated},
1415 {region_inactivated, menu_inactivated},
1416 {window_insstr, menu_insstr},
1417 {region_restack, menu_restack},
1418 {region_stacking, menu_stacking},
1424 IMPLCLASS(WMenu, WWindow, menu_deinit, menu_dynfuntab);