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>
33 #include <ioncore/sizehint.h>
34 #include <ioncore/resize.h>
39 #define MENU_WIN(MENU) ((MENU)->win.win)
45 static bool extl_table_getis(ExtlTab tab, int i, const char *s, char c,
51 if(!extl_table_geti_t(tab, i, &sub))
53 ret=extl_table_get(sub, 's', c, s, p);
54 extl_unref_table(sub);
62 /*{{{ Drawing routines */
65 static void get_outer_geom(WMenu *menu, WRectangle *geom)
69 geom->w=REGION_GEOM(menu).w;
70 geom->h=REGION_GEOM(menu).h;
74 static void get_inner_geom(WMenu *menu, WRectangle *geom)
78 get_outer_geom(menu, geom);
80 if(menu->brush!=NULL){
81 grbrush_get_border_widths(menu->brush, &bdw);
84 geom->w-=bdw.left+bdw.right;
85 geom->h-=bdw.top+bdw.bottom;
86 geom->w=maxof(0, geom->w);
87 geom->h=maxof(0, geom->h);
95 GR_DEFATTR(unselected);
100 static void init_attr()
103 GR_ALLOCATTR(active);
104 GR_ALLOCATTR(inactive);
105 GR_ALLOCATTR(selected);
106 GR_ALLOCATTR(unselected);
107 GR_ALLOCATTR(normal);
108 GR_ALLOCATTR(submenu);
113 static void menu_draw_entry(WMenu *menu, int i, const WRectangle *igeom,
119 aa=(REGION_IS_ACTIVE(menu) ? GR_ATTR(active) : GR_ATTR(inactive));
120 sa=(menu->selected_entry==i ? GR_ATTR(selected) : GR_ATTR(unselected));
122 if(menu->entry_brush==NULL)
126 geom.h=menu->entry_h;
127 geom.y+=(i-menu->first_entry)*(menu->entry_h+menu->entry_spacing);
129 grbrush_begin(menu->entry_brush, &geom, GRBRUSH_AMEND|GRBRUSH_KEEP_ATTR);
131 grbrush_init_attr(menu->entry_brush, &menu->entries[i].attr);
133 grbrush_set_attr(menu->entry_brush, aa);
134 grbrush_set_attr(menu->entry_brush, sa);
136 grbrush_draw_textbox(menu->entry_brush, &geom, menu->entries[i].title,
139 grbrush_end(menu->entry_brush);
143 void menu_draw_entries(WMenu *menu, bool complete)
148 if(menu->entry_brush==NULL)
151 get_inner_geom(menu, &igeom);
153 mx=menu->first_entry+menu->vis_entries;
154 mx=(mx < menu->n_entries ? mx : menu->n_entries);
156 for(i=menu->first_entry; i<mx; i++)
157 menu_draw_entry(menu, i, &igeom, complete);
161 void menu_draw(WMenu *menu, bool complete)
163 GrAttr aa=(REGION_IS_ACTIVE(menu) ? GR_ATTR(active) : GR_ATTR(inactive));
166 if(menu->brush==NULL)
169 get_outer_geom(menu, &geom);
171 grbrush_begin(menu->brush, &geom,
172 (complete ? 0 : GRBRUSH_NO_CLEAR_OK));
174 grbrush_set_attr(menu->brush, aa);
176 grbrush_draw_border(menu->brush, &geom);
178 menu_draw_entries(menu, FALSE);
180 grbrush_end(menu->brush);
190 static void menu_calc_size(WMenu *menu, bool maxexact,
192 int *w_ret, int *h_ret)
194 GrBorderWidths bdw, e_bdw;
197 int nath, bdh, maxew=menu->max_entry_w;
199 grbrush_get_border_widths(menu->brush, &bdw);
200 grbrush_get_border_widths(menu->entry_brush, &e_bdw);
202 if(maxexact || maxew>maxw-(int)bdw.left-(int)bdw.right){
203 maxew=maxw-bdw.left-bdw.right;
206 *w_ret=maxew+bdw.left+bdw.right;
209 bdh=bdw.top+bdw.bottom;
211 if(menu->n_entries==0){
212 *h_ret=(maxexact ? maxh : bdh);
216 int vis=(maxh-bdh+e_bdw.spacing)/(e_bdw.spacing+menu->entry_h);
217 if(vis>menu->n_entries){
220 }else if(menu->selected_entry>=0){
221 if(menu->selected_entry<menu->first_entry)
222 menu->first_entry=menu->selected_entry;
223 else if(menu->selected_entry>=menu->first_entry+vis)
224 menu->first_entry=menu->selected_entry-vis+1;
228 menu->vis_entries=vis;
232 *h_ret=vis*menu->entry_h+(vis-1)*e_bdw.spacing+bdh;
235 /* Calculate new shortened entry names */
236 maxew-=e_bdw.left+e_bdw.right;
238 for(i=0; i<menu->n_entries; i++){
239 if(menu->entries[i].title){
240 free(menu->entries[i].title);
241 menu->entries[i].title=NULL;
246 if(extl_table_getis(menu->tab, i+1, "name", 's', &str)){
247 menu->entries[i].title=grbrush_make_label(menu->entry_brush,
255 void calc_size(WMenu *menu, int *w, int *h)
257 if(menu->pmenu_mode){
258 menu_calc_size(menu, FALSE, INT_MAX, INT_MAX, w, h);
260 menu_calc_size(menu, !(menu->last_fp.mode®ION_FIT_BOUNDS),
261 menu->last_fp.g.w, menu->last_fp.g.h, w, h);
266 /* Return offset from bottom-left corner of containing mplex or top-right
267 * corner of parent menu for the respective corner of menu.
269 static void get_placement_offs(WMenu *menu, int *xoff, int *yoff)
276 if(menu->brush!=NULL){
277 grbrush_get_border_widths(menu->brush, &bdw);
282 if(menu->entry_brush!=NULL){
283 grbrush_get_border_widths(menu->entry_brush, &bdw);
290 #define MINIMUM_Y_VISIBILITY 20
291 #define POINTER_OFFSET 5
293 static void menu_firstfit(WMenu *menu, bool submenu, const WRectangle *refg)
297 calc_size(menu, &(geom.w), &(geom.h));
299 if(!(menu->last_fp.mode®ION_FIT_BOUNDS)){
300 geom.x=menu->last_fp.g.x;
301 geom.y=menu->last_fp.g.y;
302 }else if(menu->pmenu_mode){
307 const WRectangle *maxg =
308 ®ION_GEOM(REGION_PARENT((WRegion*)menu));
311 geom.y+=POINTER_OFFSET;
313 if(geom.y+MINIMUM_Y_VISIBILITY>maxg->y+maxg->h){
314 geom.y=maxg->y+maxg->h-MINIMUM_Y_VISIBILITY;
315 geom.x=refg->x+POINTER_OFFSET;
316 if(geom.x+geom.w>maxg->x+maxg->w)
317 geom.x=refg->x-geom.w-POINTER_OFFSET;
321 else if(geom.x+geom.w>maxg->x+maxg->w)
322 geom.x=maxg->x+maxg->w-geom.w;
326 const WRectangle *maxg=&(menu->last_fp.g);
328 int l, r, t, b, xoff, yoff;
330 get_placement_offs(menu, &xoff, &yoff);
332 r=refg->x+refg->w+xoff;
334 b=refg->y+refg->h-yoff;
336 geom.x=maxof(l, r-geom.w);
337 if(geom.x+geom.w>maxg->x+maxg->w)
340 geom.y=minof(b-geom.h, t);
345 geom.y=maxg->y+maxg->h-geom.h;
349 window_do_fitrep(&menu->win, NULL, &geom);
353 static void menu_do_refit(WMenu *menu, WWindow *par, const WFitParams *oldfp)
357 calc_size(menu, &(geom.w), &(geom.h));
359 if(!(menu->last_fp.mode®ION_FIT_BOUNDS)){
360 geom.x=menu->last_fp.g.x;
361 geom.y=menu->last_fp.g.y;
362 }else if(menu->pmenu_mode){
363 geom.x=REGION_GEOM(menu).x;
364 geom.y=REGION_GEOM(menu).y;
366 const WRectangle *maxg=&(menu->last_fp.g);
367 int xdiff=REGION_GEOM(menu).x-oldfp->g.x;
368 int ydiff=(REGION_GEOM(menu).y+REGION_GEOM(menu).h
369 -(oldfp->g.y+oldfp->g.h));
370 geom.x=maxof(0, minof(maxg->x+xdiff, maxg->x+maxg->w-geom.w));
371 geom.y=maxof(0, minof(maxg->y+maxg->h+ydiff, maxg->y+maxg->h)-geom.h);
374 window_do_fitrep(&menu->win, par, &geom);
378 bool menu_fitrep(WMenu *menu, WWindow *par, const WFitParams *fp)
382 if(par!=NULL && !region_same_rootwin((WRegion*)par, (WRegion*)menu))
387 menu_do_refit(menu, par, &oldfp);
389 if(menu->submenu!=NULL && !menu->pmenu_mode)
390 region_fitrep((WRegion*)(menu->submenu), par, fp);
396 void menu_size_hints(WMenu *menu, WSizeHints *hints_ret)
398 int n=menu->n_entries;
399 int w=menu->max_entry_w;
400 int h=menu->entry_h*n + menu->entry_spacing*maxof(0, n-1);
402 if(menu->brush!=NULL){
404 grbrush_get_border_widths(menu->brush, &bdw);
406 w+=bdw.left+bdw.right;
407 h+=bdw.top+bdw.bottom;
410 hints_ret->min_set=TRUE;
411 hints_ret->min_width=w;
412 hints_ret->min_height=h;
419 /*{{{ Brush update */
422 static void calc_entry_dimens(WMenu *menu)
424 int i, n=extl_table_get_n(menu->tab);
431 if(extl_table_gets_s(menu->tab, title, &str)){
432 maxw=grbrush_get_text_width(title_brush, str, strlen(str));
438 if(extl_table_getis(menu->tab, i, "name", 's', &str)){
439 int w=grbrush_get_text_width(menu->entry_brush,
447 grbrush_get_border_widths(menu->entry_brush, &bdw);
448 grbrush_get_font_extents(menu->entry_brush, &fnte);
450 menu->max_entry_w=maxw+bdw.left+bdw.right;
451 menu->entry_h=fnte.max_height+bdw.top+bdw.bottom;
452 menu->entry_spacing=bdw.spacing;
456 static bool menu_init_gr(WMenu *menu, WRootWin *rootwin, Window win)
458 GrBrush *brush, *entry_brush;
460 const char *style=(menu->big_mode
464 : "input-menu-normal"));
466 const char *entry_style=(menu->big_mode
467 ? "tab-menuentry-big"
469 ? "tab-menuentry-pmenu"
470 : "tab-menuentry-normal"));
472 brush=gr_get_brush(win, rootwin, style);
477 entry_brush=grbrush_get_slave(brush, rootwin, entry_style);
479 if(entry_brush==NULL){
480 grbrush_release(brush);
484 if(menu->entry_brush!=NULL)
485 grbrush_release(menu->entry_brush);
486 if(menu->brush!=NULL)
487 grbrush_release(menu->brush);
490 menu->entry_brush=entry_brush;
492 calc_entry_dimens(menu);
498 void menu_updategr(WMenu *menu)
500 if(!menu_init_gr(menu, region_rootwin_of((WRegion*)menu),
505 menu_do_refit(menu, NULL, &(menu->last_fp));
507 region_updategr_default((WRegion*)menu);
509 window_draw((WWindow*)menu, TRUE);
513 static void menu_release_gr(WMenu *menu)
515 if(menu->entry_brush!=NULL){
516 grbrush_release(menu->entry_brush);
517 menu->entry_brush=NULL;
519 if(menu->brush!=NULL){
520 grbrush_release(menu->brush);
532 static WMenuEntry *preprocess_menu(ExtlTab tab, int *n_entries)
538 n=extl_table_get_n(tab);
544 entries=ALLOC_N(WMenuEntry, n);
551 /* Initialise entries and check submenus */
553 WMenuEntry *ent=&entries[i-1];
558 gr_stylespec_init(&ent->attr);
560 if(extl_table_geti_t(tab, i, &entry)){
565 if(extl_table_gets_s(entry, "attr", &attr)){
566 gr_stylespec_load_(&ent->attr, attr, TRUE);
570 if(extl_table_gets_f(entry, "submenu_fn", &fn)){
571 ent->flags|=WMENUENTRY_SUBMENU;
573 }else if(extl_table_gets_t(entry, "submenu", &sub)){
574 ent->flags|=WMENUENTRY_SUBMENU;
575 extl_unref_table(sub);
578 if(ent->flags&WMENUENTRY_SUBMENU)
579 gr_stylespec_set(&ent->attr, GR_ATTR(submenu));
581 extl_unref_table(entry);
589 static void deinit_entries(WMenu *menu);
592 bool menu_init(WMenu *menu, WWindow *par, const WFitParams *fp,
593 const WMenuCreateParams *params)
598 menu->entries=preprocess_menu(params->tab, &(menu->n_entries));
600 if(menu->entries==NULL){
601 warn(TR("Empty menu."));
605 menu->tab=extl_ref_table(params->tab);
606 menu->handler=extl_ref_fn(params->handler);
607 menu->pmenu_mode=params->pmenu_mode;
608 menu->big_mode=params->big_mode;
609 /*menu->cycle_bindmap=NULL;*/
613 if(params->pmenu_mode){
614 menu->selected_entry=-1;
616 menu->selected_entry=params->initial-1;
617 if(menu->selected_entry<0)
618 menu->selected_entry=0;
619 if(params->initial > menu->n_entries)
620 menu->selected_entry=0;
626 menu->entry_brush=NULL;
627 menu->entry_spacing=0;
628 menu->vis_entries=menu->n_entries;
631 menu->typeahead=NULL;
636 if(!window_init((WWindow*)menu, par, fp))
641 if(!menu_init_gr(menu, region_rootwin_of((WRegion*)par), win))
646 menu_firstfit(menu, params->submenu_mode, &(params->refg));
648 window_select_input(&(menu->win), IONCORE_EVENTMASK_NORMAL);
650 region_add_bindmap((WRegion*)menu, mod_menu_menu_bindmap);
652 region_register((WRegion*)menu);
657 window_deinit((WWindow*)menu);
659 extl_unref_table(menu->tab);
660 extl_unref_fn(menu->handler);
661 deinit_entries(menu);
666 WMenu *create_menu(WWindow *par, const WFitParams *fp,
667 const WMenuCreateParams *params)
669 CREATEOBJ_IMPL(WMenu, menu, (p, par, fp, params));
673 static void deinit_entries(WMenu *menu)
677 for(i=0; i<menu->n_entries; i++){
678 gr_stylespec_unalloc(&menu->entries[i].attr);
679 if(menu->entries[i].title!=NULL)
680 free(menu->entries[i].title);
687 void menu_deinit(WMenu *menu)
689 menu_typeahead_clear(menu);
691 if(menu->submenu!=NULL)
692 destroy_obj((Obj*)menu->submenu);
694 /*if(menu->cycle_bindmap!=NULL)
695 bindmap_destroy(menu->cycle_bindmap);*/
697 extl_unref_table(menu->tab);
698 extl_unref_fn(menu->handler);
700 deinit_entries(menu);
702 menu_release_gr(menu);
704 window_deinit((WWindow*)menu);
714 static void menu_inactivated(WMenu *menu)
716 window_draw((WWindow*)menu, FALSE);
720 static void menu_activated(WMenu *menu)
722 window_draw((WWindow*)menu, FALSE);
732 static WMenu *menu_head(WMenu *menu)
734 WMenu *m=REGION_MANAGER_CHK(menu, WMenu);
735 return (m==NULL ? menu : menu_head(m));
739 static WMenu *menu_tail(WMenu *menu)
741 return (menu->submenu==NULL ? menu : menu_tail(menu->submenu));
745 static void menu_managed_remove(WMenu *menu, WRegion *sub)
747 bool mcf=region_may_control_focus((WRegion*)menu);
749 if(sub!=(WRegion*)menu->submenu)
754 region_unset_manager(sub, (WRegion*)menu);
757 region_do_set_focus((WRegion*)menu, FALSE);
761 int get_sub_y_off(WMenu *menu, int n)
763 /* top + sum of above entries and spacings - top_of_sub */
764 return (menu->entry_h+menu->entry_spacing)*(n-menu->first_entry);
768 static void show_sub(WMenu *menu, int n)
771 WMenuCreateParams fnp;
775 par=REGION_PARENT(menu);
782 fnp.pmenu_mode=menu->pmenu_mode;
783 fnp.big_mode=menu->big_mode;
784 fnp.submenu_mode=TRUE;
786 if(menu->pmenu_mode){
787 fnp.refg.x=REGION_GEOM(menu).x+REGION_GEOM(menu).w;
788 fnp.refg.y=REGION_GEOM(menu).y+get_sub_y_off(menu, n);
792 fnp.refg=REGION_GEOM(menu);
795 fnp.tab=extl_table_none();
798 if(extl_table_getis(menu->tab, n+1, "submenu_fn", 'f', &fn)){
800 extl_call(fn, NULL, "t", &(fnp.tab));
801 extl_unprotect(NULL);
804 extl_table_getis(menu->tab, n+1, "submenu", 't', &(fnp.tab));
806 if(fnp.tab==extl_table_none())
810 fnp.handler=extl_ref_fn(menu->handler);
815 if(extl_table_getis(menu->tab, n+1, "initial", 'f', &fn)){
817 extl_call(fn, NULL, "i", &(fnp.initial));
818 extl_unprotect(NULL);
821 extl_table_getis(menu->tab, n+1, "initial", 'i', &(fnp.initial));
825 submenu=create_menu(par, &fp, &fnp);
830 menu->submenu=submenu;
831 region_set_manager((WRegion*)submenu, (WRegion*)menu);
833 region_restack((WRegion*)submenu, MENU_WIN(menu), Above);
834 region_map((WRegion*)submenu);
836 if(!menu->pmenu_mode && region_may_control_focus((WRegion*)menu))
837 region_do_set_focus((WRegion*)submenu, FALSE);
841 static void menu_do_set_focus(WMenu *menu, bool warp)
843 if(menu->submenu!=NULL)
844 region_do_set_focus((WRegion*)menu->submenu, warp);
846 window_do_set_focus((WWindow*)menu, warp);
850 void menu_restack(WMenu *menu, Window other, int mode)
852 xwindow_restack(MENU_WIN(menu), other, mode);
853 if(menu->submenu!=NULL)
854 region_restack((WRegion*)(menu->submenu), MENU_WIN(menu), Above);
858 void menu_stacking(WMenu *menu, Window *bottomret, Window *topret)
862 if(menu->submenu!=NULL)
863 region_stacking((WRegion*)(menu->submenu), bottomret, topret);
865 *bottomret=MENU_WIN(menu);
867 *topret=MENU_WIN(menu);
878 static void menu_do_select_nth(WMenu *menu, int n)
880 int oldn=menu->selected_entry;
886 if(menu->submenu!=NULL)
887 destroy_obj((Obj*)menu->submenu);
889 assert(menu->submenu==NULL);
891 menu->selected_entry=n;
894 if(n<menu->first_entry){
897 }else if(n>=menu->first_entry+menu->vis_entries){
898 menu->first_entry=n-menu->vis_entries+1;
902 if(menu->entries[n].flags&WMENUENTRY_SUBMENU &&
909 menu_draw_entries(menu, TRUE);
911 /* redraw new and old selected entry */
913 get_inner_geom(menu, &igeom);
917 menu_draw_entry(menu, oldn, &igeom, TRUE);
919 menu_draw_entry(menu, n, &igeom, TRUE);
925 * Select \var{n}:th entry in menu.
928 void menu_select_nth(WMenu *menu, int n)
932 if(n>=menu->n_entries)
935 menu_typeahead_clear(menu);
936 menu_do_select_nth(menu, n);
941 * Select previous entry in menu.
944 void menu_select_prev(WMenu *menu)
946 menu_select_nth(menu, (menu->selected_entry<=0
948 : menu->selected_entry-1));
953 * Select next entry in menu.
956 void menu_select_next(WMenu *menu)
958 menu_select_nth(menu, (menu->selected_entry+1)%menu->n_entries);
962 static void menu_do_finish(WMenu *menu)
967 WMenu *head=menu_head(menu);
969 handler=menu->handler;
970 menu->handler=extl_fn_none();
972 ok=extl_table_geti_t(menu->tab, menu->selected_entry+1, &tab);
974 if(!region_rqdispose((WRegion*)head)){
975 if(head->submenu!=NULL)
976 destroy_obj((Obj*)head->submenu);
980 extl_call(handler, "t", NULL, tab);
982 extl_unref_fn(handler);
983 extl_unref_table(tab);
988 * If selected entry is a submenu, display that.
989 * Otherwise destroy the menu and call handler for selected entry.
992 void menu_finish(WMenu *menu)
994 menu_typeahead_clear(menu);
996 if(!menu->pmenu_mode && menu->selected_entry>=0 &&
997 menu->entries[menu->selected_entry].flags&WMENUENTRY_SUBMENU){
998 show_sub(menu, menu->selected_entry);
1002 mainloop_defer_action((Obj*)menu, (WDeferredAction*)menu_do_finish);
1008 * Close \var{menu} not calling any possible finish handlers.
1011 void menu_cancel(WMenu *menu)
1013 region_defer_rqdispose((WRegion*)menu);
1022 static int scroll_time=20;
1023 static int scroll_amount=3;
1024 static WTimer *scroll_timer=NULL;
1027 static void reset_scroll_timer()
1029 if(scroll_timer!=NULL){
1030 destroy_obj((Obj*)scroll_timer);
1037 * Set module basic settings. The parameter table may contain the
1040 * \begin{tabularx}{\linewidth}{lX}
1041 * \tabhead{Field & Description}
1042 * \var{scroll_amount} & Number of pixels to scroll at a time
1043 * pointer-controlled menus when one extends
1044 * beyond a border of the screen and the pointer
1045 * touches that border. \\
1046 * \var{scroll_delay} & Time between such scrolling events in
1051 void mod_menu_set(ExtlTab tab)
1055 if(extl_table_gets_i(tab, "scroll_amount", &a))
1056 scroll_amount=maxof(0, a);
1057 if(extl_table_gets_i(tab, "scroll_delay", &t))
1058 scroll_time=maxof(0, t);
1063 * Get module basic settings. For details, see \fnref{mod_menu.set}.
1067 ExtlTab mod_menu_get()
1069 ExtlTab tab=extl_create_table();
1070 extl_table_sets_i(tab, "scroll_amount", scroll_amount);
1071 extl_table_sets_i(tab, "scroll_delay", scroll_time);
1084 static int calc_diff(const WRectangle *mg, const WRectangle *pg, int d)
1088 return mg->x+mg->w-pg->w;
1090 return mg->y+mg->h-pg->h;
1100 static int scrolld_subs(WMenu *menu, int d)
1103 WRegion *p=REGION_PARENT_REG(menu);
1104 const WRectangle *pg;
1112 diff=maxof(diff, calc_diff(®ION_GEOM(menu), pg, d));
1116 return minof(maxof(0, diff), scroll_amount);
1120 static void menu_select_entry_at(WMenu *menu, int px, int py);
1123 static void do_scroll(WMenu *menu, int xd, int yd)
1128 xwindow_pointer_pos(region_root_of((WRegion*)menu), &px, &py);
1131 g=REGION_GEOM(menu);
1135 window_do_fitrep((WWindow*)menu, NULL, &g);
1137 menu_select_entry_at(menu, px, py);
1144 static void scroll_left(WTimer *timer, WMenu *menu)
1147 do_scroll(menu, -scrolld_subs(menu, D_LEFT), 0);
1148 if(scrolld_subs(menu, D_LEFT)>0){
1149 timer_set(timer, scroll_time, (WTimerHandler*)scroll_left,
1157 static void scroll_up(WTimer *timer, WMenu *menu)
1160 do_scroll(menu, 0, -scrolld_subs(menu, D_UP));
1161 if(scrolld_subs(menu, D_UP)>0){
1162 timer_set(timer, scroll_time, (WTimerHandler*)scroll_up,
1171 static void scroll_right(WTimer *timer, WMenu *menu)
1174 do_scroll(menu, scrolld_subs(menu, D_RIGHT), 0);
1175 if(scrolld_subs(menu, D_RIGHT)>0){
1176 timer_set(timer, scroll_time, (WTimerHandler*)scroll_right,
1184 static void scroll_down(WTimer *timer, WMenu *menu)
1187 do_scroll(menu, 0, scrolld_subs(menu, D_DOWN));
1188 if(scrolld_subs(menu, D_DOWN)>0){
1189 timer_set(timer, scroll_time, (WTimerHandler*)scroll_down,
1197 static void end_scroll(WMenu *menu)
1199 reset_scroll_timer();
1202 #define SCROLL_OFFSET 10
1204 static void check_scroll(WMenu *menu, int x, int y)
1206 WRegion *parent=REGION_PARENT_REG(menu);
1208 WTimerHandler *fn=NULL;
1210 if(!menu->pmenu_mode)
1218 region_rootpos(parent, &rx, &ry);
1222 if(x<=SCROLL_OFFSET){
1223 fn=(WTimerHandler*)scroll_right;
1224 }else if(y<=SCROLL_OFFSET){
1225 fn=(WTimerHandler*)scroll_down;
1226 }else if(x>=REGION_GEOM(parent).w-SCROLL_OFFSET){
1227 fn=(WTimerHandler*)scroll_left;
1228 }else if(y>=REGION_GEOM(parent).h-SCROLL_OFFSET){
1229 fn=(WTimerHandler*)scroll_up;
1237 if(scroll_timer!=NULL){
1238 if(scroll_timer->handler==(WTimerHandler*)fn &&
1239 timer_is_set(scroll_timer)){
1243 scroll_timer=create_timer();
1244 if(scroll_timer==NULL)
1248 fn(scroll_timer, (Obj*)menu_head(menu));
1255 /*{{{ Pointer handlers */
1258 int menu_entry_at_root(WMenu *menu, int root_x, int root_y)
1260 int rx, ry, x, y, entry;
1262 region_rootpos((WRegion*)menu, &rx, &ry);
1264 get_inner_geom(menu, &ig);
1269 if(x<0 || x>=ig.w || y<0 || y>=ig.h)
1272 entry=y/(menu->entry_h+menu->entry_spacing);
1273 if(entry<0 || entry>=menu->vis_entries)
1275 return entry+menu->first_entry;
1279 int menu_entry_at_root_tree(WMenu *menu, int root_x, int root_y,
1284 menu=menu_tail(menu);
1288 if(!menu->pmenu_mode)
1289 return menu_entry_at_root(menu, root_x, root_y);
1292 entry=menu_entry_at_root(menu, root_x, root_y);
1297 menu=REGION_MANAGER_CHK(menu, WMenu);
1304 static void menu_select_entry_at(WMenu *menu, int px, int py)
1306 int entry=menu_entry_at_root_tree(menu, px, py, &menu);
1308 menu_do_select_nth(menu, entry);
1312 void menu_release(WMenu *menu, XButtonEvent *ev)
1314 int entry=menu_entry_at_root_tree(menu, ev->x_root, ev->y_root, &menu);
1317 menu_select_nth(menu, entry);
1319 }else if(menu->pmenu_mode){
1320 menu_cancel(menu_head(menu));
1325 void menu_motion(WMenu *menu, XMotionEvent *ev, int dx, int dy)
1327 menu_select_entry_at(menu, ev->x_root, ev->y_root);
1328 check_scroll(menu, ev->x_root, ev->y_root);
1332 void menu_button(WMenu *menu, XButtonEvent *ev)
1334 int entry=menu_entry_at_root_tree(menu, ev->x_root, ev->y_root, &menu);
1336 menu_select_nth(menu, entry);
1340 int menu_press(WMenu *menu, XButtonEvent *ev, WRegion **reg_ret)
1342 menu_button(menu, ev);
1343 menu=menu_head(menu);
1344 ioncore_set_drag_handlers((WRegion*)menu,
1346 (WMotionHandler*)menu_motion,
1347 (WButtonHandler*)menu_release,
1357 /*{{{ Typeahead find */
1360 static void menu_insstr(WMenu *menu, const char *buf, size_t n)
1362 size_t oldlen=(menu->typeahead==NULL ? 0 : strlen(menu->typeahead));
1363 char *newta=(char*)malloc(oldlen+n+1);
1371 memcpy(newta, menu->typeahead, oldlen);
1373 memcpy(newta+oldlen, buf, n);
1374 newta[oldlen+n]='\0';
1377 while(*newta!='\0'){
1379 entry=menu->selected_entry;
1381 if(menu->entries[entry].title!=NULL){
1382 size_t l=strlen(menu->entries[entry].title);
1383 if(libtu_strcasestr(menu->entries[entry].title, newta)){
1388 entry=(entry+1)%menu->n_entries;
1389 }while(entry!=menu->selected_entry);
1391 menu_do_select_nth(menu, entry);
1397 if(newta_orig!=newta){
1402 char *p=scopy(newta);
1407 if(menu->typeahead!=NULL)
1408 free(menu->typeahead);
1409 menu->typeahead=newta;
1414 * Clear typeahead buffer.
1417 void menu_typeahead_clear(WMenu *menu)
1419 if(menu->typeahead!=NULL){
1420 free(menu->typeahead);
1421 menu->typeahead=NULL;
1429 /*{{{ Dynamic function table and class implementation */
1432 static DynFunTab menu_dynfuntab[]={
1433 {(DynFun*)region_fitrep, (DynFun*)menu_fitrep},
1434 {region_updategr, menu_updategr},
1435 {window_draw, menu_draw},
1436 {(DynFun*)window_press, (DynFun*)menu_press},
1437 {region_managed_remove, menu_managed_remove},
1438 {region_do_set_focus, menu_do_set_focus},
1439 {region_activated, menu_activated},
1440 {region_inactivated, menu_inactivated},
1441 {window_insstr, menu_insstr},
1442 {region_restack, menu_restack},
1443 {region_stacking, menu_stacking},
1444 {region_size_hints, menu_size_hints},
1450 IMPLCLASS(WMenu, WWindow, menu_deinit, menu_dynfuntab);