Debian Codegraph @

gdraw/gmenu.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "gdraw.h"
31 #include "ggadgetP.h"
32 #include "gkeysym.h"
33 #include "gresourceP.h"
34 #include "gwidget.h"
35 #include "hotkeys.h"
36 #include "prefs.h"
37 #include "ustring.h"
38 #include "utype.h"
39
40 static GBox menubar_box = GBOX_EMPTY; /* Don't initialize here */
41 static GBox menu_box = GBOX_EMPTY; /* Don't initialize here */
42 static GResFont menu_font = GRESFONT_INIT("400 10pt " SANS_UI_FAMILIES);
43 static GResFont menubar_font = GRESFONT_INIT("400 10pt " SANS_UI_FAMILIES);
44 #ifdef __Mac
45 static int mac_menu_icons = true;
46 #else
47 static int mac_menu_icons = false;
48 #endif
49 static int menu_3d_look = 1; // The 3D look is the default/legacy setting.
50 static int menu_grabs=true;
51 static int mask_set=0;
52 static int menumask = ksm_control|ksm_meta|ksm_shift; /* These are the modifier masks expected in menus. Will be overridden by what's actually there */
53
54 static GResInfo gmenu_ri;
55 GResInfo gmenubar_ri = {
56 &gmenu_ri, &ggadget_ri,&gmenu_ri, NULL,
57 &menubar_box,
58 &menubar_font,
59 NULL,
60 NULL,
61 N_("Menu Bar"),
62 N_("Menu Bar"),
63 "GMenuBar",
64 "Gdraw",
65 false,
66 false,
67 omf_border_shape|omf_border_width|box_foreground_border_outer,
68 { 0, bs_rect, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
69 GBOX_EMPTY,
70 NULL,
71 NULL,
72 NULL
73 };
74 static struct resed menu_re[] = {
75 {N_("MacIcons"), "MacIcons", rt_bool, &mac_menu_icons, N_("Whether to use mac-like icons to indicate modifiers (for instance ^ for Control)\nor to use an abbreviation (for instance \"Cnt-\")"), NULL, { 0 }, 0, 0 },
76 {N_("3D Look"), "3DLook", rt_bool, &menu_3d_look, N_("Whether the menu is presented with a 3D appearance"), NULL, { 0 }, 0, 0 },
77 {N_("Grab"), "Grab", rt_bool, &menu_grabs, N_("Whether the menu 'grabs' the mouse pointer"), NULL, { 0 }, 0, 0 },
78 RESED_EMPTY
79 };
80 extern GResInfo gmatrixedit_ri;
81 static GResInfo gmenu_ri = {
82 &gmatrixedit_ri, &ggadget_ri,&gmenubar_ri, NULL,
83 &menu_box,
84 &menu_font,
85 NULL,
86 menu_re,
87 N_("Menu"),
88 N_("Menu"),
89 "GMenu",
90 "Gdraw",
91 false,
92 false,
93 omf_border_shape|omf_padding|box_foreground_border_outer,
94 { 0, bs_rect, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
95 GBOX_EMPTY,
96 NULL,
97 NULL,
98 NULL
99 };
100
101 static void GMenuInit(void) {
102 GResEditDoInit(&gmenubar_ri);
103 GResEditDoInit(&gmenu_ri);
104 }
105
106 char* HKTextInfoToUntranslatedText( char* text_untranslated );
107 char* HKTextInfoToUntranslatedTextFromTextInfo( GTextInfo* ti );
108
109 static void GMenuBarChangeSelection(GMenuBar *mb, int newsel,GEvent *);
110 static struct gmenu *GMenuCreateSubMenu(struct gmenu *parent,GMenuItem *mi,int disable);
111 static struct gmenu *GMenuCreatePulldownMenu(GMenuBar *mb,GMenuItem *mi, int disabled);
112
113 static struct gmenu *most_recent_popup_menu = NULL;
114
115 typedef struct gmenu {
116 unsigned int hasticks: 1;
117 unsigned int pressed: 1;
118 unsigned int initial_press: 1;
119 unsigned int scrollup: 1;
120 unsigned int freemi: 1;
121 unsigned int disabled: 1;
122 unsigned int dying: 1;
123 unsigned int hidden: 1;
124 unsigned int any_unmasked_shortcuts: 1; /* Only set for popup menus. Else info in menubar */
125 int bp;
126 int tickoff, tioff, rightedge;
127 int width, height;
128 int line_with_mouse;
129 int offtop, lcnt, mcnt;
130 GMenuItem *mi;
131 int fh, as;
132 GWindow w;
133 GBox *box;
134 struct gmenu *parent, *child;
135 struct gmenubar *menubar;
136 GWindow owner;
137 GTimer *scrollit; /* No longer in use, see comment below */
138 FontInstance *font;
139 void (*donecallback)(GWindow owner);
140 GIC *gic;
141 char subMenuName[100];
142 /* The code below still contains the old code for using up/down arrows */
143 /* in the menu instead of a scrollbar. If vsb==NULL and the menu doesn't */
144 /* fit on the screen, then the old code will be implemented (normally */
145 /* that won't happen, but if I ever want to re-enable it, this is how */
146 /* The up arrow was placed at the top of the menu IFF there were lines off */
147 /* the top, similarly for the bottom. Clicking on either one would scroll */
148 /* in the indicated directions */
149 GGadget *vsb;
150 } GMenu;
151
152 static int GMenuGetMenuPathRecurse( GMenuItem** stack,
153 GMenuItem *basemi,
154 GMenuItem *targetmi ) {
155 GMenuItem *mi = basemi;
156 int i;
157 for ( i=0; mi[i].ti.text || mi[i].ti.text_untranslated || mi[i].ti.image || mi[i].ti.line; ++i ) {
158 // TRACE("text_untranslated: %s\n", mi[i].ti.text_untranslated );
159 if ( mi[i].sub ) {
160 // TRACE("GMenuGetMenuPathRecurse() going down on %s\n", u_to_c(mi[i].ti.text));
161 stack[0] = &mi[i];
162 int rc = GMenuGetMenuPathRecurse( &stack[1], mi[i].sub, targetmi );
163 if( rc == 1 )
164 return rc;
165 stack[0] = 0;
166 }
167 if( mi[i].ti.text ) {
168 // TRACE("GMenuGetMenuPathRecurse() inspecting %s %p %p\n", u_to_c(mi[i].ti.text),mi[i],targetmi);
169 GMenuItem* cur = &mi[i];
170 if( cur == targetmi ) {
171 stack[0] = cur;
172 return 1;
173 }
174 }
175 }
176 return 0;
177 }
178
179 /**
180 * Get a string describing the path from basemi to the targetmi.
181 * For example, if the basemi is the first mi in the whole menu structure
182 * and targetmi is to the edit / select all menu item then the return
183 * value is:
184 * edit.select all
185 *
186 * If you can't get to targetmi from basemi then return 0.
187 * If something doesn't make sense then return 0.
188 *
189 * Do not free the return value, it is a pointer into a buffer owned
190 * by this function.
191 */
192 static char* GMenuGetMenuPath( GMenuItem *basemi, GMenuItem *targetmi ) {
193 GMenuItem* stack[1024];
194 memset(stack, 0, sizeof(stack));
195 if( !targetmi->ti.text )
196 return 0;
197
198 // TRACE("GMenuGetMenuPath() base %s\n", u_to_c(basemi->ti.text));
199 // TRACE("GMenuGetMenuPath() target %s\n", u_to_c(targetmi->ti.text));
200
201 /* { */
202 /* int i=0; */
203 /* GMenuItem *mi = basemi; */
204 /* for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) { */
205 /* if( mi[i].ti.text ) { */
206 /* TRACE("GMenuGetMenuPath() xbase %s\n", u_to_c(mi[i].ti.text)); */
207 /* } */
208 /* } */
209 /* } */
210 /* TRACE("GMenuGetMenuPath() starting...\n"); */
211
212 GMenuItem *mi = basemi;
213 int i;
214 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) {
215 if( mi[i].ti.text ) {
216 memset(stack, 0, sizeof(stack));
217 // TRACE("GMenuGetMenuPath() xbase %s\n", u_to_c(mi[i].ti.text));
218 // TRACE("GMenuGetMenuPath() untrans %s\n", mi[i].ti.text_untranslated);
219 GMenuGetMenuPathRecurse( stack, &mi[i], targetmi );
220 if( stack[0] != 0 ) {
221 // TRACE("GMenuGetMenuPath() have stack[0]...\n");
222 break;
223 }
224 }
225 }
226 if( stack[0] == 0 ) {
227 return 0;
228 }
229
230 static char buffer[PATH_MAX];
231 buffer[0] = '\0';
232 for( i=0; stack[i]; i++ ) {
233 if( i )
234 strcat(buffer,".");
235 if( stack[i]->ti.text_untranslated )
236 {
237 char* tmp = HKTextInfoToUntranslatedTextFromTextInfo( &stack[i]->ti );
238 // TRACE("adding %s\n", HKTextInfoToUntranslatedTextFromTextInfo( &stack[i]->ti ));
239 strcat( buffer, tmp);
240 free(tmp);
241 }
242 else if( stack[i]->ti.text )
243 {
244 cu_strcat(buffer,stack[i]->ti.text);
245 }
246 // TRACE("GMenuGetMenuPath() stack at i:%d mi %s\n", i, u_to_c(stack[i]->ti.text));
247 }
248 return buffer;
249 }
250
251 static void GMenuDrawCheckMark(struct gmenu *m, Color fg, int ybase, int r2l) {
252 int as = m->as;
253 int pt = GDrawPointsToPixels(m->w,1);
254 int x = r2l ? m->width-m->tioff+2*pt : m->tickoff;
255
256 while ( pt>1 && 2*pt>=as/3 ) --pt;
257 GDrawSetLineWidth(m->w,pt);
258 GDrawDrawLine(m->w,x+2*pt,ybase-as/3,x+as/3,ybase-2*pt,fg);
259 GDrawDrawLine(m->w,x+2*pt,ybase-as/3-pt,x+as/3,ybase-3*pt,fg);
260 GDrawDrawLine(m->w,x+as/3,ybase-2*pt,x+as/3+as/5,ybase-2*pt-as/4,fg);
261 GDrawDrawLine(m->w,x+as/3+as/5,ybase-2*pt-as/4,x+as/3+2*as/5,ybase-2*pt-as/3-as/7,fg);
262 GDrawDrawLine(m->w,x+as/3+2*as/5,ybase-2*pt-as/3-as/7,x+as/3+3*as/5,ybase-2*pt-as/3-as/7-as/8,fg);
263 }
264
265 static void GMenuDrawUncheckMark(struct gmenu *m, Color fg, int ybase, int r2l) {
266 }
267
268 static void GMenuDrawArrow(struct gmenu *m, int ybase, int r2l) {
269 int pt = GDrawPointsToPixels(m->w,1);
270 int as = 2*(m->as/2);
271 int x = r2l ? m->bp+2*pt : m->rightedge-2*pt;
272 GPoint p[3];
273
274 GDrawSetLineWidth(m->w,pt);
275 if ( r2l ) {
276 p[0].x = x; p[0].y = ybase-as/2;
277 p[1].x = x+1*(as/2); p[1].y = ybase;
278 p[2].x = p[1].x; p[2].y = ybase-as;
279
280 // If rendering menus in standard (3-dimensional) look, use the shadow colors for fake relief.
281 // Otherwise, use foreground colors.
282 if (menu_3d_look) {
283 GDrawDrawLine(m->w,p[0].x,p[0].y,p[2].x,p[2].y,m->box->border_brighter);
284 GDrawDrawLine(m->w,p[0].x+pt,p[0].y,p[2].x+pt,p[2].y+pt,m->box->border_brighter);
285 GDrawDrawLine(m->w,p[1].x,p[1].y,p[0].x,p[0].y,m->box->border_darkest);
286 GDrawDrawLine(m->w,p[1].x+pt,p[1].y-pt,p[0].x-pt,p[0].y,m->box->border_darkest);
287 } else {
288 GDrawDrawLine(m->w,p[0].x,p[0].y,p[2].x,p[2].y,m->box->main_foreground);
289 GDrawDrawLine(m->w,p[0].x+pt,p[0].y,p[2].x+pt,p[2].y+pt,m->box->main_foreground);
290 GDrawDrawLine(m->w,p[1].x,p[1].y,p[0].x,p[0].y,m->box->main_foreground);
291 GDrawDrawLine(m->w,p[1].x+pt,p[1].y-pt,p[0].x-pt,p[0].y,m->box->main_foreground);
292 }
293 } else {
294 p[0].x = x; p[0].y = ybase-as/2;
295 p[1].x = x-1*(as/2); p[1].y = ybase;
296 p[2].x = p[1].x; p[2].y = ybase-as;
297
298 if (menu_3d_look) {
299 GDrawDrawLine(m->w,p[0].x,p[0].y,p[2].x,p[2].y,m->box->border_brighter);
300 GDrawDrawLine(m->w,p[0].x-pt,p[0].y,p[2].x+pt,p[2].y+pt,m->box->border_brighter);
301 GDrawDrawLine(m->w,p[1].x,p[1].y,p[0].x,p[0].y,m->box->border_darkest);
302 GDrawDrawLine(m->w,p[1].x+pt,p[1].y-pt,p[0].x-pt,p[0].y,m->box->border_darkest);
303 } else {
304 GDrawDrawLine(m->w,p[0].x,p[0].y,p[2].x,p[2].y,m->box->main_foreground);
305 GDrawDrawLine(m->w,p[0].x-pt,p[0].y,p[2].x+pt,p[2].y+pt,m->box->main_foreground);
306 GDrawDrawLine(m->w,p[1].x,p[1].y,p[0].x,p[0].y,m->box->main_foreground);
307 GDrawDrawLine(m->w,p[1].x+pt,p[1].y-pt,p[0].x-pt,p[0].y,m->box->main_foreground);
308 }
309 }
310 }
311
312 static void GMenuDrawUpArrow(struct gmenu *m, int ybase) {
313 int pt = GDrawPointsToPixels(m->w,1);
314 int x = (m->rightedge+m->tickoff)/2;
315 int as = 2*(m->as/2);
316 GPoint p[3];
317
318 p[0].x = x; p[0].y = ybase - as;
319 p[1].x = x-as; p[1].y = ybase;
320 p[2].x = x+as; p[2].y = ybase;
321
322 GDrawSetLineWidth(m->w,pt);
323
324 // If rendering menus in standard (3-dimensional) look, use the shadow colors for fake relief.
325 // Otherwise, use foreground colors.
326 if (menu_3d_look) {
327 GDrawDrawLine(m->w,p[0].x,p[0].y,p[1].x,p[1].y,m->box->border_brightest);
328 GDrawDrawLine(m->w,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,m->box->border_brightest);
329 GDrawDrawLine(m->w,p[1].x,p[1].y,p[2].x,p[2].y,m->box->border_darker);
330 GDrawDrawLine(m->w,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,m->box->border_darker);
331 GDrawDrawLine(m->w,p[2].x,p[2].y,p[0].x,p[0].y,m->box->border_darkest);
332 GDrawDrawLine(m->w,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,m->box->border_darkest);
333 } else {
334 GDrawDrawLine(m->w,p[0].x,p[0].y,p[1].x,p[1].y,m->box->main_foreground);
335 GDrawDrawLine(m->w,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,m->box->main_foreground);
336 GDrawDrawLine(m->w,p[1].x,p[1].y,p[2].x,p[2].y,m->box->main_foreground);
337 GDrawDrawLine(m->w,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,m->box->main_foreground);
338 GDrawDrawLine(m->w,p[2].x,p[2].y,p[0].x,p[0].y,m->box->main_foreground);
339 GDrawDrawLine(m->w,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,m->box->main_foreground);
340 }
341 }
342
343 static void GMenuDrawDownArrow(struct gmenu *m, int ybase) {
344 int pt = GDrawPointsToPixels(m->w,1);
345 int x = (m->rightedge+m->tickoff)/2;
346 int as = 2*(m->as/2);
347 GPoint p[3];
348
349 p[0].x = x; p[0].y = ybase;
350 p[1].x = x-as; p[1].y = ybase - as;
351 p[2].x = x+as; p[2].y = ybase - as;
352
353 GDrawSetLineWidth(m->w,pt);
354
355 // If rendering menus in standard (3-dimensional) look, use the shadow colors for fake relief.
356 // Otherwise, use foreground colors.
357 if (menu_3d_look) {
358 GDrawDrawLine(m->w,p[0].x,p[0].y,p[1].x,p[1].y,m->box->border_darker);
359 GDrawDrawLine(m->w,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,m->box->border_darker);
360 GDrawDrawLine(m->w,p[1].x,p[1].y,p[2].x,p[2].y,m->box->border_brightest);
361 GDrawDrawLine(m->w,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,m->box->border_brightest);
362 GDrawDrawLine(m->w,p[2].x,p[2].y,p[0].x,p[0].y,m->box->border_darkest);
363 GDrawDrawLine(m->w,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,m->box->border_darkest);
364 } else {
365 GDrawDrawLine(m->w,p[0].x,p[0].y,p[1].x,p[1].y,m->box->main_foreground);
366 GDrawDrawLine(m->w,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,m->box->main_foreground);
367 GDrawDrawLine(m->w,p[1].x,p[1].y,p[2].x,p[2].y,m->box->main_foreground);
368 GDrawDrawLine(m->w,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,m->box->main_foreground);
369 GDrawDrawLine(m->w,p[2].x,p[2].y,p[0].x,p[0].y,m->box->main_foreground);
370 GDrawDrawLine(m->w,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,m->box->main_foreground);
371 }
372 }
373
374 /**
375 * Return the menu bar at the top of this menu list
376 * */
377 static GMenuBar * getTopLevelMenubar( struct gmenu *m ) {
378 while( m->parent ) {
379 m = m->parent;
380 }
381 return m->menubar;
382 }
383
384
385 static int GMenuDrawMenuLine(struct gmenu *m, GMenuItem *mi, int y,GWindow pixmap) {
386 unichar_t shortbuf[300];
387 int as = GTextInfoGetAs(m->w,&mi->ti,m->font);
388 int h, width;
389 Color fg = m->box->main_foreground;
390 GRect old, new;
391 int ybase = y+as;
392 int r2l = false;
393 int x;
394
395 //printf("GMenuDrawMenuLine(top)\n");
396 /* if(mi->ti.text) */
397 /* TRACE("GMenuDrawMenuLine() mi:%s\n",u_to_c(mi->ti.text)); */
398
399 new.x = m->tickoff; new.width = m->rightedge-m->tickoff;
400 new.y = y; new.height = GTextInfoGetHeight(pixmap,&mi->ti,m->font);
401 GDrawPushClip(pixmap,&new,&old);
402
403 if ( mi->ti.fg!=COLOR_DEFAULT && mi->ti.fg!=COLOR_UNKNOWN )
404 fg = mi->ti.fg;
405 if ( mi->ti.disabled || m->disabled )
406 fg = m->box->disabled_foreground;
407 if ( fg==COLOR_DEFAULT )
408 fg = GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap));
409 if ( mi->ti.text!=NULL && isrighttoleft(mi->ti.text[0]) )
410 r2l = true;
411
412 if ( r2l )
413 x = m->width-m->tioff-GTextInfoGetWidth(pixmap,&mi->ti,m->font);
414 else
415 x = m->tioff;
416 h = GTextInfoDraw(pixmap,x,y,&mi->ti,m->font,
417 (mi->ti.disabled || m->disabled )?m->box->disabled_foreground:fg,
418 m->box->active_border,new.y+new.height, -1, -1);
419 if ( mi->ti.checkable ) {
420 if ( mi->ti.checked )
421 GMenuDrawCheckMark(m,fg,ybase,r2l);
422 else
423 GMenuDrawUncheckMark(m,fg,ybase,r2l);
424 }
425
426 if ( mi->sub!=NULL )
427 GMenuDrawArrow(m,ybase,r2l);
428 else
429 {
430 /* TRACE("m->menubar: %p\n", m->menubar ); */
431 /* TRACE("m->parent: %p\n", m->parent ); */
432 /* TRACE("m->toplevel: %p\n", getTopLevelMenubar(m)); */
433
434 /**
435 * Grab the hotkey if there is one. First we work out the
436 * menubar for this menu item, and then get the path from the
437 * menubar to the menuitem and pass that to the hotkey system
438 * to get the hotkey if there is one for that menu item.
439 */
440 GMenuBar* toplevel = getTopLevelMenubar(m);
441 Hotkey* hk = 0;
442 if( toplevel )
443 {
444 hk = hotkeyFindByMenuPath( toplevel->g.base,
445 GMenuGetMenuPath( toplevel->mi, mi ));
446 }
447 else if( m->owner && strlen(m->subMenuName) )
448 {
449 hk = hotkeyFindByMenuPathInSubMenu( m->owner, m->subMenuName,
450 GMenuGetMenuPath( m->mi, mi ));
451 }
452
453 shortbuf[0] = '\0';
454
455 if( hk )
456 {
457 /* TRACE("m->menubar->mi: %p\n", toplevel->mi ); */
458 /* TRACE("m->menubar->window: %p\n", toplevel->g.base ); */
459 /* TRACE("drawline... hk: %p\n", hk ); */
460 char* keydesc = hk->text;
461 if( mac_menu_icons )
462 {
463 keydesc = hotkeyTextToMacModifiers( keydesc );
464 }
465 utf82u_strcpy( shortbuf, keydesc );
466 if( keydesc != hk->text )
467 free( keydesc );
468 }
469
470 width = GDrawGetTextWidth(pixmap,shortbuf,-1);
471 if ( r2l )
472 {
473 GDrawDrawText(pixmap,m->bp,ybase,shortbuf,-1,fg);
474 }
475 else
476 {
477 int x = m->rightedge-width;
478 GDrawDrawText(pixmap,x,ybase,shortbuf,-1,fg);
479 }
480 }
481
482 GDrawPopClip(pixmap,&old);
483 return( y + h );
484 }
485
486 static int gmenu_expose(struct gmenu *m, GEvent *event,GWindow pixmap) {
487 GRect old1, old2;
488 GRect r;
489 int i;
490
491 /* TRACE("gmenu_expose() m:%p ev:%p\n",m,event); */
492 GDrawPushClip(pixmap,&event->u.expose.rect,&old1);
493 r.x = 0; r.width = m->width; r.y = 0; r.height = m->height;
494 GBoxDrawBackground(pixmap,&r,m->box,gs_active,false);
495 GBoxDrawBorder(pixmap,&r,m->box,gs_active,false);
496 r.x = m->tickoff; r.width = m->rightedge-m->tickoff;
497 r.y = m->bp; r.height = m->height - 2*m->bp;
498 GDrawPushClip(pixmap,&r,&old2);
499 for ( i = event->u.expose.rect.y/m->fh+m->offtop; i<m->mcnt &&
500 i<=(event->u.expose.rect.y+event->u.expose.rect.height)/m->fh+m->offtop;
501 ++i ) {
502 if ( i==m->offtop && m->offtop!=0 && m->vsb==NULL )
503 GMenuDrawUpArrow(m, m->bp+m->as);
504 else if ( m->lcnt!=m->mcnt && i==m->lcnt+m->offtop-1 && i!=m->mcnt-1 ) {
505 if ( m->vsb==NULL )
506 GMenuDrawDownArrow(m, m->bp+(i-m->offtop)*m->fh+m->as);
507 else
508 GMenuDrawMenuLine(m, &m->mi[i], m->bp+(i-m->offtop)*m->fh, pixmap);
509 break; /* Otherwise we get bits of the line after the last */
510 } else
511 GMenuDrawMenuLine(m, &m->mi[i], m->bp+(i-m->offtop)*m->fh, pixmap);
512 }
513 GDrawPopClip(pixmap,&old2);
514 GDrawPopClip(pixmap,&old1);
515 return( true );
516 }
517
518 static void GMenuSetPressed(struct gmenu *m, int pressed) {
519 while ( m->child!=NULL ) m = m->child;
520 while ( m->parent!=NULL ) {
521 m->pressed = pressed;
522 m = m->parent;
523 }
524 m->pressed = pressed;
525 if ( m->menubar!=NULL )
526 m->menubar->pressed = pressed;
527 }
528
529 static void _GMenuDestroy(struct gmenu *m) {
530 if ( m->dying )
531 return;
532 m->dying = true;
533 if ( m->line_with_mouse!=-1 )
534 m->mi[m->line_with_mouse].ti.selected = false;
535 if ( m->child!=NULL )
536 _GMenuDestroy(m->child);
537 if ( m->parent!=NULL )
538 m->parent->child = NULL;
539 else if ( m->menubar!=NULL ) {
540 m->menubar->child = NULL;
541 m->menubar->pressed = false;
542 _GWidget_ClearPopupOwner((GGadget *) (m->menubar));
543 _GWidget_ClearGrabGadget((GGadget *) (m->menubar));
544 GMenuBarChangeSelection(m->menubar,-1,NULL);
545 }
546 GDrawDestroyWindow(m->w);
547 /* data are freed when we get the destroy event !!!! */
548 }
549
550 static void GMenuDestroy(struct gmenu *m) {
551 GDrawPointerUngrab(GDrawGetDisplayOfWindow(m->w));
552 if ( menu_grabs && m->parent!=NULL )
553 GDrawPointerGrab(m->parent->w);
554 _GMenuDestroy(m);
555 }
556
557 static void GMenuHideAll(struct gmenu *m) {
558 if ( m!=NULL ) {
559 struct gmenu *s = m;
560 GDrawPointerUngrab(GDrawGetDisplayOfWindow(m->w));
561 while ( m->parent ) m = m->parent;
562 while ( m ) {
563 m->hidden = true;
564 GDrawSetVisible(m->w,false);
565 m=m->child;
566 }
567 GDrawSync(GDrawGetDisplayOfWindow(s->w));
568 GDrawProcessPendingEvents(GDrawGetDisplayOfWindow(s->w));
569 }
570 }
571
572 static void GMenuDismissAll(struct gmenu *m) {
573 if ( m!=NULL ) {
574 while ( m->parent ) m = m->parent;
575 GMenuDestroy(m);
576 }
577 }
578
579 static void UnsetInitialPress(struct gmenu *m) {
580 while ( m!=NULL ) {
581 m->initial_press = false;
582 if ( m->menubar!=NULL ) m->menubar->initial_press = false;
583 m = m->parent;
584 }
585 }
586
587 static void GMenuChangeSelection(struct gmenu *m, int newsel,GEvent *event) {
588 int old=m->line_with_mouse;
589
590 if ( old==newsel )
591 return;
592 if ( newsel==m->mcnt )
593 return;
594
595 if ( m->child!=NULL ) {
596 GMenuDestroy(m->child);
597 m->child = NULL;
598 }
599 UnsetInitialPress(m);
600 m->line_with_mouse = newsel;
601 if ( newsel!=-1 )
602 m->mi[newsel].ti.selected = true;
603 if ( old!=-1 )
604 m->mi[old].ti.selected = false;
605
606 GDrawRequestExpose(m->w, NULL, false);
607 if ( newsel!=-1 ) {
608 if ( m->mi[newsel].moveto!=NULL )
609 (m->mi[newsel].moveto)(m->owner,&m->mi[newsel],event);
610 if ( m->mi[newsel].sub!=NULL )
611 m->child = GMenuCreateSubMenu(m,m->mi[newsel].sub,
612 m->disabled || m->mi[newsel].ti.disabled);
613 }
614 }
615
616 static void GMenuBarChangeSelection(GMenuBar *mb, int newsel,GEvent *event) {
617 int old=mb->entry_with_mouse;
618 GMenuItem *mi;
619
620 if ( old==newsel )
621 return;
622 if ( mb->child!=NULL ) {
623 int waspressed = mb->pressed;
624 GMenuDestroy(mb->child);
625 mb->child = NULL;
626 mb->pressed = waspressed;
627 }
628 mb->entry_with_mouse = newsel;
629 if ( newsel!=-1 )
630 mb->mi[newsel].ti.selected = true;
631 if ( old!=-1 )
632 mb->mi[old].ti.selected = false;
633
634 _ggadget_redraw(&mb->g);
635 if ( newsel!=-1 ) {
636 mi = newsel==mb->lastmi ? mb->fake : &mb->mi[newsel];
637 if ( mi->moveto!=NULL )
638 (mi->moveto)(mb->g.base,mi,event);
639 if ( mi->sub!=NULL )
640 mb->child = GMenuCreatePulldownMenu(mb,mi->sub,mi->ti.disabled);
641 }
642 }
643
644 static int MParentInitialPress(struct gmenu *m) {
645 if ( m->parent!=NULL )
646 return( m->parent->initial_press );
647 else if ( m->menubar!=NULL )
648 return( m->menubar->initial_press );
649
650 return( false );
651 }
652
653 static int gmenu_mouse(struct gmenu *m, GEvent *event) {
654 GPoint p;
655 struct gmenu *testm;
656
657 if ( m->hidden || (m->child!=NULL && m->child->hidden))
658 return( true );
659
660 if ( event->type == et_crossing ) {
661 if ( !event->u.crossing.entered )
662 UnsetInitialPress(m);
663 return( true );
664 } else if ((event->type==et_mouseup || event->type==et_mousedown) &&
665 (event->u.mouse.button>=4 && event->u.mouse.button<=7) ) {
666 //From scrollbar.c: X treats a scroll as a mousedown/mouseup event
667 return GGadgetDispatchEvent(m->vsb,event);
668 }
669
670 p.x = event->u.mouse.x; p.y = event->u.mouse.y;
671
672 for ( testm=m; testm->child!=NULL; testm = testm->child );
673 if ( testm->scrollit && testm!=m ) {
674 GDrawCancelTimer(testm->scrollit);
675 testm->scrollit = NULL;
676 }
677 for ( ; testm!=NULL; testm=testm->parent )
678 if ( GDrawEventInWindow(testm->w,event) )
679 break;
680
681 if ( testm!=m && testm!=NULL ) {
682 GDrawPointerGrab(testm->w);
683 GDrawTranslateCoordinates(m->w,testm->w,&p);
684 m = testm;
685 } else if ( testm==NULL /*&& event->u.mouse.y<0*/ ) {/* menubars can be below the menu if no room on screen */
686 for ( testm=m; testm->parent!=NULL; testm=testm->parent );
687 if ( testm->menubar!=NULL ) {
688 GDrawTranslateCoordinates(m->w,testm->menubar->g.base,&p);
689 if ( p.x>=0 && p.y>=0 &&
690 p.x<testm->menubar->g.inner.x+testm->menubar->g.inner.width &&
691 p.y<testm->menubar->g.inner.y+testm->menubar->g.inner.height ) {
692 /*GDrawPointerGrab(testm->menubar->g.base);*/ /* Don't do this */
693 event->u.mouse.x = p.x; event->u.mouse.y = p.y;
694 return( (GDrawGetEH(testm->menubar->g.base))(testm->menubar->g.base,event));
695 }
696 }
697 testm = NULL;
698 }
699 if ( testm==NULL ) {
700 if ( event->type==et_mousedown )
701 GMenuDismissAll(m);
702 else if ( event->type==et_mouseup )
703 GMenuSetPressed(m,false);
704 else if ( event->type==et_mousemove )
705 /* A displayed menu tracks the pointer whether or not a button is
706 * held (sticky menus): moving off the menu clears the highlight. */
707 GMenuChangeSelection(m,-1,event);
708 return( true );
709 }
710
711 event->u.mouse.x = p.x; event->u.mouse.y = p.y; event->w = m->w;
712 if ( event->type==et_mousemove || event->type == et_mousedown ) {
713 int l = (event->u.mouse.y-m->bp)/m->fh;
714 int i = l + m->offtop;
715 if ( m->scrollit!=NULL )
716 ;
717 else if ( event->u.mouse.y<m->bp && event->type==et_mousedown )
718 GMenuDismissAll(m);
719 else if ( l==0 && m->offtop!=0 && m->vsb==NULL ) {
720 GMenuChangeSelection(m,-1,event);
721 if ( m->scrollit==NULL )
722 m->scrollit = GDrawRequestTimer(m->w,1,_GScrollBar_RepeatTime,m);
723 m->scrollup = true;
724 } else if ( l>=m->lcnt-1 && m->offtop+m->lcnt<m->mcnt && m->vsb==NULL ) {
725 GMenuChangeSelection(m,-1,event);
726 if ( m->scrollit==NULL )
727 m->scrollit = GDrawRequestTimer(m->w,1,_GScrollBar_RepeatTime,m);
728 m->scrollup = false;
729 } else if ( event->type == et_mousedown && m->child!=NULL &&
730 i == m->line_with_mouse ) {
731 GMenuChangeSelection(m,-1,event);
732 } else if ( i >= m->mcnt ){
733 GMenuChangeSelection(m,-1,event);
734 } else
735 GMenuChangeSelection(m,i,event);
736 if ( event->type == et_mousedown ) {
737 GMenuSetPressed(m,true);
738 if ( m->child!=NULL )
739 m->initial_press = true;
740 }
741 } else if ( event->type == et_mouseup && m->child==NULL ) {
742 if ( m->scrollit!=NULL ) {
743 GDrawCancelTimer(m->scrollit);
744 m->scrollit = NULL;
745 } else if ( event->u.mouse.y>=m->bp && event->u.mouse.x>=0 &&
746 event->u.mouse.y<m->height-m->bp &&
747 event->u.mouse.x < m->width &&
748 !MParentInitialPress(m)) {
749 int l = (event->u.mouse.y-m->bp)/m->fh;
750 int i = l + m->offtop;
751 if ( !( l==0 && m->offtop!=0 && m->vsb==NULL ) &&
752 !( l==m->lcnt-1 && m->offtop+m->lcnt<m->mcnt && m->vsb==NULL ) &&
753 !m->disabled &&
754 !m->mi[i].ti.disabled && !m->mi[i].ti.line ) {
755 if ( m->mi[i].ti.checkable )
756 m->mi[i].ti.checked = !m->mi[i].ti.checked;
757 GMenuHideAll(m);
758 GMenuDismissAll(m);
759 if ( m->mi[i].invoke!=NULL )
760 (m->mi[i].invoke)(m->owner,&m->mi[i],event);
761 }
762 }
763 } else if ( event->type == et_mouseup ) {
764 UnsetInitialPress(m);
765 GMenuSetPressed(m,false);
766 } else
767 return( false );
768
769 return( true );
770 }
771
772 static int gmenu_timer(struct gmenu *m, GEvent *event) {
773 if ( m->scrollup ) {
774 if ( m->offtop==0 )
775 return(true);
776 if ( --m->offtop<0 ) m->offtop = 0;
777 } else {
778 if ( m->offtop == m->mcnt-m->lcnt )
779 return( true );
780 ++m->offtop;
781 if ( m->offtop + m->lcnt > m->mcnt )
782 m->offtop = m->mcnt-m->lcnt;
783 }
784 GDrawRequestExpose(m->w, NULL, false);
785 return( true );
786 }
787
788 static int GMenuKeyInvoke(struct gmenu *m, int i) {
789 GMenuChangeSelection(m,i,NULL);
790 if ( m->mi[i].ti.checkable )
791 m->mi[i].ti.checked = !m->mi[i].ti.checked;
792 if ( m->mi[i].sub==NULL ) {
793 GMenuHideAll(m);
794 }
795 if ( m->mi[i].invoke!=NULL )
796 (m->mi[i].invoke)(m->owner,&m->mi[i],NULL);
797 if ( m->mi[i].sub==NULL ) {
798 GMenuDismissAll(m);
799 }
800 return( true );
801 }
802
803 static int GMenuBarKeyInvoke(struct gmenubar *mb, int i) {
804 GMenuBarChangeSelection(mb,i,NULL);
805 if ( mb->mi[i].invoke!=NULL )
806 (mb->mi[i].invoke)(mb->g.base,&mb->mi[i],NULL);
807 return( true );
808 }
809
810 /**
811 * Remove any instances of 'ch' from 'ret' and return 'ret'
812 *
813 * When ch is encountered it is removed from the string, with all the
814 * characters following it moved left to cover over the space ch used
815 * to occupy.
816 */
817 static char* str_remove_all_single_char( char * ret, char ch )
818 {
819 char* src = ret;
820 char* dst = ret;
821 for( ; *src; src++ )
822 {
823 if( *src == ch )
824 continue;
825
826 *dst = *src;
827 dst++;
828 }
829 *dst = '\0';
830 return ret;
831 }
832
833 /**
834 * Given a text_untranslated which may be either the hotkey of format:
835 *
836 * Foo|ShortCutKey
837 * Win*Foo|ShortCutKey
838 * _Foo
839 *
840 * return just the english text for the entry, ie, Foo
841 *
842 * The return value is owned by this function, do not free it.
843 */
844 char* HKTextInfoToUntranslatedText(char *text_untranslated) {
845 char ret[PATH_MAX+1];
846 char* pt;
847 int i;
848
849 strncpy(ret,text_untranslated,PATH_MAX);
850
851 if( (pt=strchr(ret,'*')) )
852 for (i=0; pt[i]!='\0'; i++)
853 ret[i]=pt[i+1];
854 if( (pt=strchr(ret,'|')) )
855 *pt = '\0';
856 str_remove_all_single_char( ret, '_' );
857 return copy(ret);
858 }
859
860 /**
861 * Call HKTextInfoToUntranslatedText on ti->text_untranslated
862 * guarding against the chance of null for ti, and ti->text_untranslated
863 */
864 char* HKTextInfoToUntranslatedTextFromTextInfo( GTextInfo* ti )
865 {
866 if( !ti )
867 return 0;
868 if( !ti->text_untranslated )
869 return 0;
870 return HKTextInfoToUntranslatedText( ti->text_untranslated );
871 }
872
873
874 /**
875 * return true if the prefix matches the first segment of the given action.
876 * For example,
877 * return will be 0 when action = foo.bar.baz prefix = bar
878 * return will be 0 when action = foo.bar.baz prefix = fooo
879 * return will be 1 when action = foo.bar.baz prefix = foo
880 * return will be 1 when action = baz prefix = baz
881 */
882 static int HKActionMatchesFirstPartOf( char* action, char* prefix_const, int munge )
883 {
884 char prefix[PATH_MAX+1];
885 char* pt = 0;
886 strncpy( prefix, prefix_const, PATH_MAX );
887 if( munge ) {
888 char *tofree = HKTextInfoToUntranslatedText(prefix_const);
889 strncpy( prefix, tofree,PATH_MAX );
890 free(tofree);
891 }
892 // TRACE("munge:%d prefix2:%s\n", munge, prefix );
893
894 pt = strchr(action,'.');
895 if( !pt )
896 return( 0==strcmp(action,prefix) );
897
898 int l = pt - action;
899 if( strlen(prefix) < l )
900 return 0;
901 int rc = strncmp( action, prefix, l );
902 return rc == 0;
903 }
904
905 /**
906 * Call HKActionMatchesFirstPartOf on the given ti.
907 */
908 static int HKActionMatchesFirstPartOfTextInfo( char* action, GTextInfo* ti )
909 {
910 if( ti->text_untranslated )
911 return HKActionMatchesFirstPartOf( action, ti->text_untranslated, 1 );
912 if( ti->text )
913 return HKActionMatchesFirstPartOf( action, u_to_c(ti->text), 0 );
914 return 0;
915 }
916
917 static char* HKActionPointerPastLeftmostKey( char* action ) {
918 char* pt = strchr(action,'.');
919 if( !pt )
920 return 0;
921 return pt + 1;
922 }
923
924
925
926 static GMenuItem *GMenuSearchActionRecursive( GWindow gw,
927 GMenuItem *mi,
928 char* action,
929 GEvent *event,
930 int call_moveto) {
931
932 // TRACE("GMenuSearchAction() action:%s\n", action );
933 int i;
934 for ( i=0; mi[i].ti.text || mi[i].ti.text_untranslated || mi[i].ti.image || mi[i].ti.line; ++i ) {
935 // if( mi[i].ti.text )
936 // TRACE("GMenuSearchActionRecursive() text : %s\n", u_to_c(mi[i].ti.text) );
937 // TRACE("GMenuSearchActionRecursive() text_untranslated: %s\n", mi[i].ti.text_untranslated );
938 if ( call_moveto && mi[i].moveto != NULL)
939 (mi[i].moveto)(gw,&(mi[i]),event);
940
941 if ( mi[i].sub ) {
942 if( HKActionMatchesFirstPartOfTextInfo( action, &mi[i].ti )) {
943 char* subaction = HKActionPointerPastLeftmostKey(action);
944
945 // TRACE("GMenuSearchAction() action:%s descending menu:%s\n", action, u_to_c(mi[i].ti.text) );
946 GMenuItem *ret = GMenuSearchActionRecursive(gw,mi[i].sub,subaction,event,call_moveto);
947 if ( ret!=NULL )
948 return( ret );
949 }
950 } else {
951 // TRACE("GMenuSearchAction() action:%s testing menu:%s\n", action, u_to_c(mi[i].ti.text) );
952 if( HKActionMatchesFirstPartOfTextInfo( action, &mi[i].ti )) {
953 // TRACE("GMenuSearchAction() matching final menu part! action:%s\n", action );
954 return &mi[i];
955 }
956 }
957
958 }
959 return( NULL );
960 }
961 static GMenuItem *GMenuSearchAction( GWindow gw,
962 GMenuItem *mi,
963 char* action,
964 GEvent *event,
965 int call_moveto) {
966 char* windowType = GDrawGetWindowTypeName( gw );
967 if( !windowType )
968 return 0;
969 // TRACE("GMenuSearchAction() windowtype:%s\n", windowType );
970 int actionlen = strlen(action);
971 int prefixlen = strlen(windowType) + 1 + strlen("Menu.");
972 if( actionlen < prefixlen ) {
973 return 0;
974 }
975 action += prefixlen;
976 GMenuItem * ret = GMenuSearchActionRecursive( gw, mi, action,
977 event, call_moveto );
978 return ret;
979 }
980
981
982
983 static GMenuItem *GMenuSearchShortcut(GWindow gw, GMenuItem *mi, GEvent *event,
984 int call_moveto) {
985 int i;
986
987 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) {
988 if ( call_moveto && mi[i].moveto != NULL)
989 (mi[i].moveto)(gw,&(mi[i]),event);
990 if ( mi[i].sub==NULL &&
991 (menumask&event->u.chr.state)==mi[i].short_mask &&
992 GDrawShortcutKeyMatches(event, mi[i].shortcut) )
993 return( &mi[i]);
994 else if ( mi[i].sub!=NULL ) {
995 GMenuItem *ret = GMenuSearchShortcut(gw,mi[i].sub,event,call_moveto);
996 if ( ret!=NULL )
997 return( ret );
998 }
999 }
1000 return( NULL );
1001 }
1002
1003 static int GMenuSpecialKeys(struct gmenu *m, unichar_t keysym, GEvent *event) {
1004 switch ( keysym ) {
1005 case GK_Escape:
1006 GMenuDestroy(m);
1007 return( true );
1008 case GK_Return:
1009 if ( m->line_with_mouse==-1 ) {
1010 int ns=0;
1011 while ( ns<m->mcnt && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) ++ns;
1012 if ( ns<m->mcnt )
1013 GMenuChangeSelection(m,ns,event);
1014 } else if ( m->mi[m->line_with_mouse].sub!=NULL &&
1015 m->child==NULL ) {
1016 m->child = GMenuCreateSubMenu(m,m->mi[m->line_with_mouse].sub,
1017 m->disabled || m->mi[m->line_with_mouse].ti.disabled);
1018 } else {
1019 int i = m->line_with_mouse;
1020 if ( !m->disabled && !m->mi[i].ti.disabled && !m->mi[i].ti.line ) {
1021 if ( m->mi[i].ti.checkable )
1022 m->mi[i].ti.checked = !m->mi[i].ti.checked;
1023 GMenuDismissAll(m);
1024 if ( m->mi[i].invoke!=NULL )
1025 (m->mi[i].invoke)(m->owner,&m->mi[i],event);
1026 } else
1027 GMenuDismissAll(m);
1028 }
1029 return( true );
1030 case GK_Left: case GK_KP_Left:
1031 if ( m->parent!=NULL ) {
1032 GMenuDestroy(m);
1033 return( true );
1034 } else if ( m->menubar!=NULL ) {
1035 GMenuBar *mb = m->menubar;
1036 int en = mb->entry_with_mouse;
1037 int lastmi = mb->fake[0].sub!=NULL ? mb->lastmi+1 : mb->lastmi;
1038 if ( en>0 ) {
1039 GMenuBarChangeSelection(mb,en-1,event);
1040 } else
1041 GMenuBarChangeSelection(mb,lastmi-1,event);
1042 return( true );
1043 }
1044 /* Else fall into the "Up" case */
1045 case GK_Up: case GK_KP_Up: case GK_Page_Up: case GK_KP_Page_Up: {
1046 int ns;
1047 if ( keysym!=GK_Left && keysym!=GK_KP_Left ) {
1048 while ( m->line_with_mouse==-1 && m->parent!=NULL ) {
1049 GMenu *p = m->parent;
1050 GMenuDestroy(m);
1051 m = p;
1052 }
1053 }
1054 ns = m->line_with_mouse-1;
1055 while ( ns>=0 && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) --ns;
1056 if ( ns<0 ) {
1057 ns = m->mcnt-1;
1058 while ( ns>=0 && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) --ns;
1059 }
1060 if ( ns<0 && m->line_with_mouse==-1 ) { /* Nothing selectable? get rid of menu */
1061 GMenuDestroy(m);
1062 return( true );
1063 }
1064 if ( ns<0 ) ns = -1;
1065 GMenuChangeSelection(m,ns,NULL);
1066 return( true );
1067 }
1068 case GK_Right: case GK_KP_Right:
1069 if ( m->line_with_mouse!=-1 &&
1070 m->mi[m->line_with_mouse].sub!=NULL && m->child==NULL ) {
1071 m->child = GMenuCreateSubMenu(m,m->mi[m->line_with_mouse].sub,
1072 m->disabled || m->mi[m->line_with_mouse].ti.disabled);
1073 return( true );
1074 } else if ( m->parent==NULL && m->menubar!=NULL ) {
1075 GMenuBar *mb = m->menubar;
1076 int en = mb->entry_with_mouse;
1077 int lastmi = mb->fake[0].sub!=NULL ? mb->lastmi+1 : mb->lastmi;
1078 if ( en+1<lastmi ) {
1079 GMenuBarChangeSelection(mb,en+1,event);
1080 } else
1081 GMenuBarChangeSelection(mb,0,event);
1082 return( true );
1083 }
1084 /* Fall through into the "Down" case */
1085 case GK_Down: case GK_KP_Down: case GK_Page_Down: case GK_KP_Page_Down: {
1086 int ns;
1087 if ( keysym!=GK_Right && keysym!=GK_KP_Right ) {
1088 while ( m->line_with_mouse==-1 && m->parent!=NULL ) {
1089 GMenu *p = m->parent;
1090 GMenuDestroy(m);
1091 m = p;
1092 }
1093 }
1094 ns = m->line_with_mouse+1;
1095 while ( ns<m->mcnt && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) ++ns;
1096 if ( ns>=m->mcnt ) {
1097 ns = 0;
1098 while ( ns<m->mcnt && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) ++ns;
1099 }
1100 if ( ns>=m->mcnt && m->line_with_mouse==-1 ) { /* Nothing selectable? get rid of menu */
1101 GMenuDestroy(m);
1102 return( true );
1103 }
1104 GMenuChangeSelection(m,ns,event);
1105 return( true );
1106 }
1107 case GK_Home: case GK_KP_Home: {
1108 int ns=0;
1109 while ( ns<m->mcnt && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) ++ns;
1110 if ( ns!=m->mcnt )
1111 GMenuChangeSelection(m,ns,event);
1112 return( true );
1113 }
1114 case GK_End: case GK_KP_End: {
1115 int ns=m->mcnt-1;
1116 while ( ns>=0 && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) --ns;
1117 if ( ns>=0 )
1118 GMenuChangeSelection(m,ns,event);
1119 return( true );
1120 }
1121 }
1122 return( false );
1123 }
1124
1125 static int gmenu_key(struct gmenu *m, GEvent *event) {
1126 int i;
1127 GMenuItem *mi;
1128 GMenu *top;
1129
1130 if ( m->dying )
1131 return( false );
1132
1133 if ( event->u.chr.state&ksm_meta && !(event->u.chr.state&(menumask&~(ksm_meta|ksm_shift)))) {
1134 /* Only look for mneumonics in the child */
1135 while ( m->child!=NULL )
1136 m = m->child;
1137 for ( i=0; i<m->mcnt; ++i ) {
1138 if ( !m->disabled &&
1139 !m->mi[i].ti.disabled &&
1140 GDrawShortcutKeyMatches(event, m->mi[i].ti.mnemonic) ) {
1141 GMenuKeyInvoke(m,i);
1142 return( true );
1143 }
1144 }
1145 }
1146
1147 /* then look for shortcuts everywhere */
1148 if ( (event->u.chr.state&(menumask&~ksm_shift)) ||
1149 event->u.chr.keysym>=GK_Special ) {
1150 for ( top = m; top->parent!=NULL ; top = top->parent );
1151 if ( top->menubar!=NULL )
1152 mi = GMenuSearchShortcut(top->owner,top->menubar->mi,event,false);
1153 else
1154 mi = GMenuSearchShortcut(top->owner,top->mi,event,false);
1155 if ( mi!=NULL ) {
1156 if ( mi->ti.checkable )
1157 mi->ti.checked = !mi->ti.checked;
1158 GMenuHideAll(top);
1159 if ( mi->invoke!=NULL )
1160 (mi->invoke)(m->owner,mi,event);
1161 GMenuDestroy(m);
1162 return( true );
1163 }
1164 for ( ; m->child!=NULL ; m = m->child );
1165 return( GMenuSpecialKeys(m,event->u.chr.keysym,event));
1166 }
1167
1168 return( false );
1169 }
1170
1171 static int gmenu_destroy(struct gmenu *m) {
1172 if ( most_recent_popup_menu==m )
1173 most_recent_popup_menu = NULL;
1174 if ( m->donecallback )
1175 (m->donecallback)(m->owner);
1176 if ( m->freemi )
1177 GMenuItemArrayFree(m->mi);
1178 free(m);
1179 return( true );
1180 }
1181
1182 static int gmenu_eh(GWindow w,GEvent *ge) {
1183 GMenu *m = (GMenu *) GDrawGetUserData(w);
1184
1185 switch ( ge->type ) {
1186 case et_map:
1187 /* I need to initialize the input context, but I can't do that until */
1188 /* the menu pops up */
1189 if ( ge->u.map.is_visible && m->gic!=NULL )
1190 GDrawSetGIC(w,m->gic,0,20);
1191 return( true );
1192 case et_expose:
1193 return( gmenu_expose(m,ge,w));
1194 case et_char:
1195 return( gmenu_key(m,ge));
1196 case et_mousemove: case et_mousedown: case et_mouseup: case et_crossing:
1197 return( gmenu_mouse(m,ge));
1198 case et_timer:
1199 return( gmenu_timer(m,ge));
1200 case et_destroy:
1201 return( gmenu_destroy(m));
1202 case et_close:
1203 GMenuDestroy(m);
1204 return( true );
1205 default: break;
1206 }
1207 return( false );
1208 }
1209
1210 static int gmenu_scroll(GGadget *g, GEvent *event) {
1211 enum sb sbt = event->u.control.u.sb.type;
1212 GMenu *m = (GMenu *) (g->data);
1213 int newpos = m->offtop;
1214
1215 if ( sbt==et_sb_top )
1216 newpos = 0;
1217 else if ( sbt==et_sb_bottom )
1218 newpos = m->mcnt-m->lcnt;
1219 else if ( sbt==et_sb_up ) {
1220 --newpos;
1221 } else if ( sbt==et_sb_down ) {
1222 ++newpos;
1223 } else if ( sbt==et_sb_uppage ) {
1224 if ( m->lcnt!=1 ) /* Normally we leave one line in window from before, except if only one line fits */
1225 newpos -= m->lcnt-1;
1226 else
1227 newpos -= 1;
1228 } else if ( sbt==et_sb_downpage ) {
1229 if ( m->lcnt!=1 ) /* Normally we leave one line in window from before, except if only one line fits */
1230 newpos += m->lcnt-1;
1231 else
1232 newpos += 1;
1233 } else /* if ( sbt==et_sb_thumb || sbt==et_sb_thumbrelease ) */ {
1234 newpos = event->u.control.u.sb.pos;
1235 }
1236 if ( newpos+m->lcnt > m->mcnt )
1237 newpos = m->mcnt-m->lcnt;
1238 if ( newpos<0 )
1239 newpos = 0;
1240 if ( newpos!= m->offtop ) {
1241 m->offtop = newpos;
1242 GScrollBarSetPos(m->vsb,newpos);
1243 GDrawRequestExpose(m->w,NULL,false);
1244 }
1245 return( true );
1246 }
1247
1248 static GMenu *_GMenu_Create( GMenuBar* toplevel,
1249 GWindow owner,
1250 GMenuItem *mi,
1251 GPoint *where,
1252 int awidth, int aheight,
1253 GFont *font, int disable,
1254 char* subMenuName )
1255 {
1256 GMenu *m = calloc(1,sizeof(GMenu));
1257 GRect pos;
1258 GDisplay *disp = GDrawGetDisplayOfWindow(owner);
1259 GWindowAttrs pattrs;
1260 int i, width, max_iwidth = 0, max_hkwidth = 0;
1261 unichar_t buffer[300];
1262 extern int _GScrollBar_Width;
1263 int ds, ld, temp, lh;
1264 int sbwidth = 0;
1265 GRect screen;
1266
1267 m->owner = owner;
1268 m->mi = mi;
1269 m->disabled = disable;
1270 m->font = font;
1271 m->box = &menu_box;
1272 m->tickoff = m->tioff = m->bp = GBoxBorderWidth(owner,m->box);
1273 m->line_with_mouse = -1;
1274 if( subMenuName )
1275 strncpy(m->subMenuName,subMenuName,sizeof(m->subMenuName)-1);
1276 /* Mnemonics in menus don't work under gnome. Turning off nodecor makes them */
1277 /* work, but that seems a high price to pay */
1278 pattrs.mask = wam_events|wam_nodecor|wam_positioned|wam_cursor|wam_transient|wam_verytransient;
1279 pattrs.event_masks = -1;
1280 pattrs.nodecoration = true;
1281 pattrs.positioned = true;
1282 pattrs.cursor = ct_pointer;
1283 pattrs.transient = GWidgetGetTopWidget(owner);
1284
1285 pos.x = pos.y = 0; pos.width = pos.height = 100;
1286
1287 m->w = GDrawCreateTopWindow(disp,&pos,gmenu_eh,m,&pattrs);
1288 m->gic = GDrawCreateInputContext(m->w,gic_root|gic_orlesser);
1289 GDrawWindowFontMetrics(m->w,m->font,&m->as, &ds, &ld);
1290 m->fh = m->as + ds + 1; /* I need some extra space, else mneumonic underlines look bad */
1291 lh = m->fh;
1292
1293 GDrawSetFont(m->w,m->font);
1294 m->hasticks = false;
1295 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) {
1296 if ( mi[i].ti.checkable )
1297 m->hasticks = true;
1298 temp = GTextInfoGetWidth(m->w,&mi[i].ti,m->font);
1299 if (temp > max_iwidth)
1300 max_iwidth = temp;
1301
1302 uc_strcpy(buffer,"");
1303 /**
1304 * Grab the hotkey if there is one. First we work out the
1305 * menubar for this menu item, and then get the path from the
1306 * menubar to the menuitem and pass that to the hotkey system
1307 * to get the hotkey if there is one for that menu item.
1308 *
1309 * If we have a hotkey then set the width to the text porition
1310 * first and then add the modifier icons if there are to be
1311 * any for the key combination.
1312 */
1313 // TRACE("_gmenu_create() toplevel:%p owner:%p\n", toplevel, owner );
1314
1315 Hotkey* hk = 0;
1316 if( toplevel ) {
1317 hk = hotkeyFindByMenuPath( toplevel->g.base,
1318 GMenuGetMenuPath( toplevel->mi, &mi[i] ));
1319 }
1320 else if( owner && strlen(m->subMenuName) )
1321 {
1322 hk = hotkeyFindByMenuPathInSubMenu( owner, m->subMenuName,
1323 GMenuGetMenuPath( mi, &mi[i] ));
1324 }
1325
1326 // TRACE("hk:%p\n", hk);
1327 if( hk )
1328 {
1329 char* keydesc = hk->text;
1330 if( mac_menu_icons )
1331 {
1332 // keydesc = hotkeyTextWithoutModifiers( keydesc );
1333 }
1334 uc_strcpy( buffer, keydesc );
1335
1336 temp = GDrawGetTextWidth(m->w,buffer,-1);
1337 if (temp > max_hkwidth)
1338 max_hkwidth = temp;
1339 /* if( short_mask && mac_menu_icons ) { */
1340 /* temp += GMenuMacIconsWidth( m, short_mask ); */
1341 /* } */
1342 }
1343
1344 temp = GTextInfoGetHeight(m->w,&mi[i].ti,m->font);
1345 if ( temp>lh ) {
1346 if ( temp>3*m->fh/2 )
1347 temp = 3*m->fh/2;
1348 lh = temp;
1349 }
1350 }
1351 m->fh = lh;
1352 m->mcnt = m->lcnt = i;
1353
1354 //Width: Max item length + max length of hotkey text + padding
1355 width = max_iwidth + max_hkwidth + GDrawPointsToPixels(m->w, 10);
1356 if ( m->hasticks ) {
1357 int ticklen = m->as + GDrawPointsToPixels(m->w,5);
1358 width += ticklen;
1359 m->tioff += ticklen;
1360 }
1361 m->width = pos.width = width + 2*m->bp;
1362 m->rightedge = m->width - m->bp;
1363 m->height = pos.height = i*m->fh + 2*m->bp;
1364 GDrawGetSize(GDrawGetRoot(disp),&screen);
1365
1366 sbwidth = 0;
1367 /* On the mac, the menu bar takes up the top twenty pixels or so of screen */
1368 /* so never put a menu that high */
1369 #define MAC_MENUBAR 20
1370 if ( pos.height > screen.height-MAC_MENUBAR-m->fh ) {
1371 GGadgetData gd;
1372
1373 m->lcnt = (screen.height-MAC_MENUBAR-m->fh-2*m->bp)/m->fh;
1374 m->height = pos.height = m->lcnt*m->fh + 2*m->bp;
1375
1376 /* It's too long, so add a scrollbar */
1377 sbwidth = GDrawPointsToPixels(owner,_GScrollBar_Width);
1378 pos.width += sbwidth;
1379 memset(&gd,'\0',sizeof(gd));
1380 gd.pos.y = 0; gd.pos.height = pos.height;
1381 gd.pos.width = sbwidth;
1382 gd.pos.x = m->width;
1383 gd.flags = gg_visible|gg_enabled|gg_pos_in_pixels|gg_sb_vert|gg_pos_use0;
1384 gd.handle_controlevent = gmenu_scroll;
1385 m->vsb = GScrollBarCreate(m->w,&gd,m);
1386 GScrollBarSetBounds(m->vsb,0,m->mcnt,m->lcnt);
1387 }
1388
1389 pos.x = where->x; pos.y = where->y;
1390 if ( pos.y + pos.height > screen.height-MAC_MENUBAR ) {
1391 if ( where->y+aheight-pos.height >= MAC_MENUBAR )
1392 pos.y = where->y+aheight-pos.height;
1393 else {
1394 pos.y = MAC_MENUBAR;
1395 /* Ok, it's going to overlap the press point if we got here */
1396 /* let's see if we can shift it left/right a bit so it won't */
1397 if ( awidth<0 )
1398 /* Oh, well, I guess it won't. It's a submenu and we've already */
1399 /* moved off to the left */;
1400 else if ( pos.x+awidth+pos.width+3<screen.width )
1401 pos.x += awidth+3;
1402 else if ( pos.x-pos.width-3>=0 )
1403 pos.x -= pos.width+3;
1404 else {
1405 /* There doesn't seem much we can do in this case */
1406 ;
1407 }
1408 }
1409 }
1410 if ( pos.x+pos.width > screen.width ) {
1411 if ( where->x+awidth-pos.width >= 0 )
1412 pos.x = where->x+awidth-pos.width-3;
1413 else
1414 pos.x = 0;
1415 }
1416 GDrawResize(m->w,pos.width,pos.height);
1417 GDrawMove(m->w,pos.x,pos.y);
1418
1419 GDrawSetVisible(m->w,true);
1420 if ( menu_grabs )
1421 GDrawPointerGrab(m->w);
1422 return( m );
1423 }
1424
1425 static GMenu *GMenuCreateSubMenu(GMenu *parent,GMenuItem *mi,int disable) {
1426 GPoint p;
1427 GMenu *m;
1428 char *subMenuName = 0;
1429
1430 p.x = parent->width;
1431 p.y = (parent->line_with_mouse-parent->offtop)*parent->fh + parent->bp;
1432 GDrawTranslateCoordinates(parent->w,GDrawGetRoot(GDrawGetDisplayOfWindow(parent->w)),&p);
1433 m = _GMenu_Create(getTopLevelMenubar(parent),parent->owner,mi,&p,-parent->width,parent->fh,
1434 parent->font, disable, subMenuName );
1435 m->parent = parent;
1436 m->pressed = parent->pressed;
1437 return( m );
1438 }
1439
1440 static GMenu *GMenuCreatePulldownMenu(GMenuBar *mb,GMenuItem *mi,int disabled) {
1441 GPoint p;
1442 GMenu *m;
1443 char *subMenuName = 0;
1444
1445 p.x = mb->g.inner.x + mb->xs[mb->entry_with_mouse]-
1446 GBoxDrawnWidth(mb->g.base,&menu_box );
1447 p.y = mb->g.r.y + mb->g.r.height;
1448 GDrawTranslateCoordinates(mb->g.base,GDrawGetRoot(GDrawGetDisplayOfWindow(mb->g.base)),&p);
1449 m = _GMenu_Create( mb, mb->g.base, mi, &p,
1450 mb->xs[mb->entry_with_mouse+1]-mb->xs[mb->entry_with_mouse],
1451 -mb->g.r.height,
1452 mb->font, disabled, subMenuName );
1453 m->menubar = mb;
1454 m->pressed = mb->pressed;
1455 _GWidget_SetPopupOwner((GGadget *) mb);
1456 return( m );
1457 }
1458
1459 GWindow _GMenuCreatePopupMenu( GWindow owner,GEvent *event, GMenuItem *mi,
1460 void (*donecallback)(GWindow) )
1461 {
1462 char *subMenuName = 0;
1463 return _GMenuCreatePopupMenuWithName( owner, event, mi, subMenuName, donecallback );
1464 }
1465
1466
1467 GWindow _GMenuCreatePopupMenuWithName( GWindow owner,GEvent *event, GMenuItem *mi,
1468 char *subMenuName,
1469 void (*donecallback)(GWindow) )
1470 {
1471 GPoint p;
1472 GMenu *m;
1473 GEvent e;
1474
1475 GMenuInit();
1476
1477 p.x = event->u.mouse.x;
1478 p.y = event->u.mouse.y;
1479 GDrawTranslateCoordinates(owner,GDrawGetRoot(GDrawGetDisplayOfWindow(owner)),&p);
1480 m = _GMenu_Create( 0, owner, GMenuItemArrayCopy(mi,NULL), &p,
1481 0, 0, menu_font.fi,false, subMenuName );
1482 m->any_unmasked_shortcuts = GMenuItemArrayAnyUnmasked(m->mi);
1483 GDrawPointerUngrab(GDrawGetDisplayOfWindow(owner));
1484 GDrawPointerGrab(m->w);
1485 GDrawGetPointerPosition(m->w,&e);
1486 if ( e.u.mouse.state & (ksm_button1|ksm_button2|ksm_button3) )
1487 m->pressed = m->initial_press = true;
1488 m->donecallback = donecallback;
1489 m->freemi = true;
1490 most_recent_popup_menu = m;
1491 return( m->w );
1492 }
1493
1494 GWindow GMenuCreatePopupMenu(GWindow owner,GEvent *event, GMenuItem *mi)
1495 {
1496 char* subMenuName = 0;
1497 return( _GMenuCreatePopupMenuWithName(owner,event,mi,subMenuName,NULL));
1498 }
1499
1500 GWindow GMenuCreatePopupMenuWithName(GWindow owner,GEvent *event,
1501 char* subMenuName, GMenuItem *mi)
1502 {
1503 return( _GMenuCreatePopupMenuWithName(owner,event,mi,subMenuName,NULL));
1504 }
1505
1506
1507 int GMenuPopupCheckKey(GEvent *event) {
1508
1509 if ( most_recent_popup_menu==NULL ) return( false );
1510
1511 return( gmenu_key(most_recent_popup_menu,event) );
1512 }
1513
1514
1515
1516 static int GMenuBarCheckHotkey(GWindow top, GGadget *g, GEvent *event) {
1517 // TRACE("GMenuBarCheckKey(top) keysym:%d upper:%d lower:%d\n",keysym,toupper(keysym),tolower(keysym));
1518 // see if we should skip processing (e.g. no modifier key pressed)
1519 GMenuBar *mb = (GMenuBar *) g;
1520 GWindow w = GGadgetGetWindow(g);
1521 GGadget* focus = GWindowGetFocusGadgetOfWindow(w);
1522 if (GGadgetGetSkipHotkeyProcessing(focus))
1523 return 0;
1524
1525 // TRACE("GMenuBarCheckKey(2) keysym:%d upper:%d lower:%d\n",keysym,toupper(keysym),tolower(keysym));
1526
1527 /* then look for hotkeys everywhere */
1528
1529 if( hotkeySystemGetCanUseMacCommand() )
1530 {
1531 // If Mac command key was pressed, swap with the control key.
1532 if ((event->u.chr.state & (ksm_cmdmacosx|ksm_control)) == ksm_cmdmacosx) {
1533 event->u.chr.state ^= (ksm_cmdmacosx|ksm_control);
1534 }
1535 }
1536
1537 // TRACE("about to look for hotkey in new system...state:%d keysym:%d\n", event->u.chr.state, event->u.chr.keysym );
1538 // TRACE(" has ksm_control:%d\n", (event->u.chr.state & ksm_control ));
1539 // TRACE(" has ksm_meta:%d\n", (event->u.chr.state & ksm_meta ));
1540 // TRACE(" has ksm_shift:%d\n", (event->u.chr.state & ksm_shift ));
1541
1542 event->u.chr.state |= ksm_numlock;
1543 // TRACE("about2 to look for hotkey in new system...state:%d keysym:%d\n", event->u.chr.state, event->u.chr.keysym );
1544
1545 /**
1546 * Mask off the parts we don't explicitly care about
1547 */
1548 event->u.chr.state &= ( ksm_control | ksm_meta | ksm_shift | ksm_option );
1549
1550 // TRACE("about3 to look for hotkey in new system...state:%d keysym:%d\n", event->u.chr.state, event->u.chr.keysym );
1551 // TRACE(" has ksm_control:%d\n", (event->u.chr.state & ksm_control ));
1552 // TRACE(" has ksm_meta:%d\n", (event->u.chr.state & ksm_meta ));
1553 // TRACE(" has ksm_shift:%d\n", (event->u.chr.state & ksm_shift ));
1554
1555 if( GGadgetGetSkipUnQualifiedHotkeyProcessing(focus) && !event->u.chr.state )
1556 {
1557 TRACE("skipping unqualified hotkey for widget g:%p\n", g);
1558 return 0;
1559 }
1560
1561
1562 struct dlistnodeExternal* node= hotkeyFindAllByEvent( top, event );
1563 struct dlistnode* hklist = (struct dlistnode*)node;
1564 for( ; node; node=(struct dlistnodeExternal*)(node->next) ) {
1565 Hotkey* hk = (Hotkey*)node->ptr;
1566 // TRACE("hotkey found by event! hk:%p\n", hk );
1567 // TRACE("hotkey found by event! action:%s\n", hk->action );
1568
1569 int skipkey = false;
1570
1571 if( cv_auto_goto )
1572 {
1573 if( !hk->state )
1574 skipkey = true;
1575 // TRACE("hotkey state:%d skip:%d\n", hk->state, skipkey );
1576 }
1577
1578 if( !skipkey )
1579 {
1580 GMenuItem *mi = GMenuSearchAction(mb->g.base,mb->mi,hk->action,event,mb->child==NULL);
1581 if ( mi )
1582 {
1583 // TRACE("GMenuBarCheckKey(x) have mi... :%p\n", mi );
1584 // TRACE("GMenuBarCheckKey(x) have mitext:%s\n", u_to_c(mi->ti.text) );
1585 if ( mi->ti.checkable && !mi->ti.disabled )
1586 mi->ti.checked = !mi->ti.checked;
1587 if ( mi->invoke!=NULL && !mi->ti.disabled )
1588 (mi->invoke)(mb->g.base,mi,NULL);
1589 if ( mb->child != NULL )
1590 GMenuDestroy(mb->child);
1591 return( true );
1592 }
1593 else
1594 {
1595 // TRACE("hotkey found for event must be a non menu action... action:%s\n", hk->action );
1596
1597 }
1598 }
1599
1600 // TRACE("END hotkey found by event! hk:%p\n", hk );
1601 }
1602 dlist_free_external(&hklist);
1603
1604 // TRACE("menubarcheckkey(e1)\n");
1605 return false;
1606 }
1607
1608
1609 int GMenuBarCheckKey(GWindow top, GGadget *g, GEvent *event) {
1610 int i;
1611 GMenuBar *mb = (GMenuBar *) g;
1612 unichar_t keysym = event->u.chr.keysym;
1613
1614 if ( g==NULL || keysym==0 ) return( false ); /* exit if no gadget or key */
1615
1616
1617 if ( event->u.chr.state&ksm_meta && !(event->u.chr.state&(menumask&~(ksm_meta|ksm_shift)))) {
1618 /* Only look for mneumonics in the leaf of the displayed menu structure */
1619 if ( mb->child!=NULL )
1620 return( gmenu_key(mb->child,event)); /* this routine will do shortcuts too */
1621
1622 for ( i=0; i<mb->mtot; ++i ) {
1623 if ( !mb->mi[i].ti.disabled && GDrawShortcutKeyMatches(event, mb->mi[i].ti.mnemonic) ) {
1624 GMenuBarKeyInvoke(mb,i);
1625 return( true );
1626 }
1627 }
1628 }
1629
1630 // See if it matches a hotkey
1631 if (GMenuBarCheckHotkey(top, g, event)) {
1632 return true;
1633 }
1634
1635 if ( mb->child )
1636 {
1637 GMenu *m;
1638 for ( m=mb->child; m->child!=NULL; m = m->child )
1639 {
1640 }
1641 return( GMenuSpecialKeys(m,event->u.chr.keysym,event));
1642 }
1643
1644 // TRACE("menubarcheckkey(e2)\n");
1645 if ( event->u.chr.keysym==GK_Menu && most_recent_popup_menu==NULL )
1646 GMenuCreatePopupMenu(event->w,event, mb->mi);
1647
1648 return( false );
1649 }
1650
1651 static void GMenuBarDrawDownArrow(GWindow pixmap, GMenuBar *mb, int x) {
1652 int pt = GDrawPointsToPixels(pixmap,1);
1653 int size = 2*(mb->g.inner.height/3);
1654 int ybase = mb->g.inner.y + size + (mb->g.inner.height-size)/2;
1655 GPoint p[3];
1656
1657 p[0].x = x+size; p[0].y = ybase;
1658 p[1].x = x; p[1].y = ybase - size;
1659 p[2].x = x+2*size; p[2].y = ybase - size;
1660
1661 GDrawSetLineWidth(pixmap,pt);
1662
1663 // If rendering menus in standard (3-dimensional) look, use the shadow colors for fake relief.
1664 // Otherwise, use foreground colors.
1665 if (menu_3d_look) {
1666 GDrawDrawLine(pixmap,p[0].x,p[0].y,p[1].x,p[1].y,mb->g.box->border_darker);
1667 GDrawDrawLine(pixmap,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,mb->g.box->border_darker);
1668 GDrawDrawLine(pixmap,p[1].x,p[1].y,p[2].x,p[2].y,mb->g.box->border_brightest);
1669 GDrawDrawLine(pixmap,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,mb->g.box->border_brightest);
1670 GDrawDrawLine(pixmap,p[2].x,p[2].y,p[0].x,p[0].y,mb->g.box->border_darkest);
1671 GDrawDrawLine(pixmap,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,mb->g.box->border_darkest);
1672 } else {
1673 GDrawDrawLine(pixmap,p[0].x,p[0].y,p[1].x,p[1].y,mb->g.box->main_foreground);
1674 GDrawDrawLine(pixmap,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,mb->g.box->main_foreground);
1675 GDrawDrawLine(pixmap,p[1].x,p[1].y,p[2].x,p[2].y,mb->g.box->main_foreground);
1676 GDrawDrawLine(pixmap,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,mb->g.box->main_foreground);
1677 GDrawDrawLine(pixmap,p[2].x,p[2].y,p[0].x,p[0].y,mb->g.box->main_foreground);
1678 GDrawDrawLine(pixmap,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,mb->g.box->main_foreground);
1679 }
1680 }
1681
1682 static int gmenubar_expose(GWindow pixmap, GGadget *g, GEvent *expose) {
1683 GMenuBar *mb = (GMenuBar *) g;
1684 GRect r,old1,old2, old3;
1685 Color fg = g->state==gs_disabled?g->box->disabled_foreground:
1686 g->box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)):
1687 g->box->main_foreground;
1688 int i;
1689
1690 if ( fg==COLOR_DEFAULT )
1691 fg = GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(mb->g.base));
1692
1693 GDrawPushClip(pixmap,&g->r,&old1);
1694
1695 GBoxDrawBackground(pixmap,&g->r,g->box, g->state,false);
1696 GBoxDrawBorder(pixmap,&g->r,g->box,g->state,false);
1697 GDrawPushClip(pixmap,&g->inner,&old2);
1698 GDrawSetFont(pixmap,mb->font);
1699
1700 r = g->inner;
1701 for ( i=0; i<mb->lastmi; ++i ) {
1702 int cellx = mb->xs[i]+mb->g.inner.x;
1703 int cellw = mb->xs[i+1]-mb->xs[i];
1704 /* Draw the active title's highlight ourselves, with horizontal padding
1705 * and filling the bar height, rather than relying on GTextInfoDraw's
1706 * text-tight fill (which hugs the glyphs and looks cramped on macOS).
1707 * We then pass COLOR_DEFAULT below so GTextInfoDraw skips its own fill. */
1708 if ( mb->mi[i].ti.selected && mb->g.box->active_border!=COLOR_DEFAULT ) {
1709 GRect hl;
1710 int padx = GDrawPointsToPixels(mb->g.base,5);
1711 int textw = cellw - GDrawPointsToPixels(mb->g.base,8); /* cell = 8pt gap + text */
1712 hl.x = cellx - padx; hl.width = textw + 2*padx;
1713 if ( hl.x < g->inner.x ) { hl.width -= g->inner.x - hl.x; hl.x = g->inner.x; }
1714 hl.y = g->inner.y; hl.height = g->inner.height;
1715 GDrawFillRect(pixmap,&hl,mb->g.box->active_border);
1716 }
1717 r.x = cellx; r.width = cellw;
1718 GDrawPushClip(pixmap,&r,&old3);
1719 GTextInfoDraw(pixmap,r.x,r.y,&mb->mi[i].ti,mb->font,
1720 mb->mi[i].ti.disabled?mb->g.box->disabled_foreground:fg,
1721 COLOR_DEFAULT,r.y+r.height, mb->ascender,
1722 mb->descender);
1723 GDrawPopClip(pixmap,&old3);
1724 }
1725 if ( i<mb->mtot ) {
1726 GMenuBarDrawDownArrow(pixmap,mb,mb->xs[i]+mb->g.inner.x);
1727 }
1728
1729 GDrawPopClip(pixmap,&old2);
1730 GDrawPopClip(pixmap,&old1);
1731 return( true );
1732 }
1733
1734 static int GMenuBarIndex(GMenuBar *mb, int x ) {
1735 int i;
1736
1737 if ( x<0 )
1738 return( -1 );
1739 for ( i=0; i< mb->lastmi; ++i )
1740 if ( x<mb->g.inner.x+mb->xs[i+1] )
1741 return( i );
1742 if ( mb->lastmi!=mb->mtot )
1743 return( mb->lastmi );
1744
1745 return( -1 );
1746 }
1747
1748 static int gmenubar_mouse(GGadget *g, GEvent *event) {
1749 GMenuBar *mb = (GMenuBar *) g;
1750 int which;
1751
1752 if ( mb->child!=NULL && mb->child->hidden )
1753 return( true );
1754
1755 if ( event->type == et_mousedown ) {
1756 mb->pressed = true;
1757 if ( mb->child!=NULL )
1758 GMenuSetPressed(mb->child,true);
1759 which = GMenuBarIndex(mb,event->u.mouse.x);
1760 if ( which==mb->entry_with_mouse && mb->child!=NULL )
1761 GMenuDestroy(mb->child);
1762 else {
1763 mb->initial_press = true;
1764 GMenuBarChangeSelection(mb,which,event);
1765 }
1766 } else if ( event->type == et_mousemove && mb->entry_with_mouse!=-1 ) {
1767 /* Once a menu is active on the bar, moving across it switches menus on
1768 * hover. Keyed off entry_with_mouse rather than button state or child!=NULL
1769 * so that hovering a disabled top-level menu (which opens no pulldown)
1770 * doesn't end navigation. */
1771 if ( GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) {
1772 int which = GMenuBarIndex(mb,event->u.mouse.x);
1773 /* Don't let the pointer passing over a gap between titles (which==-1)
1774 * close the open menu -- that causes the menu to flicker shut and
1775 * reopen. Only switch to another real title. */
1776 if ( which!=-1 )
1777 GMenuBarChangeSelection(mb,which,event);
1778 } else if ( mb->child!=NULL ) {
1779 GPoint p;
1780
1781 p.x = event->u.mouse.x; p.y = event->u.mouse.y;
1782 GDrawTranslateCoordinates(mb->g.base,mb->child->w,&p);
1783 if ( p.x>=0 && p.y>=0 && p.x<mb->child->width && p.y<mb->child->height ) {
1784 GDrawPointerUngrab(GDrawGetDisplayOfWindow(mb->g.base));
1785 GDrawPointerGrab(mb->child->w);
1786 event->u.mouse.x = p.x; event->u.mouse.y = p.y;
1787 event->w = mb->child->w;
1788 gmenu_mouse(mb->child,event);
1789 }
1790 }
1791 } else if ( event->type == et_mouseup &&
1792 (!mb->initial_press ||
1793 !GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y))) {
1794 GMenuBarChangeSelection(mb,-1,event);
1795 mb->pressed = false;
1796 } else if ( event->type == et_mouseup ) {
1797 mb->initial_press = mb->pressed = false;
1798 if ( mb->child!=NULL )
1799 GMenuSetPressed(mb->child,false);
1800 }
1801 return( true );
1802 }
1803
1804 static void gmenubar_destroy(GGadget *g) {
1805 GMenuBar *mb = (GMenuBar *) g;
1806 if ( g==NULL )
1807 return;
1808 if ( mb->child!=NULL ) {
1809 GMenuDestroy(mb->child);
1810 GDrawSync(NULL);
1811 GDrawProcessPendingEvents(NULL); /* popup's destroy routine must execute before we die */
1812 }
1813 GMenuItemArrayFree(mb->mi);
1814 free(mb->xs);
1815 _ggadget_destroy(g);
1816 }
1817
1818 static void GMenuBarSetFont(GGadget *g,FontInstance *new) {
1819 GMenuBar *b = (GMenuBar *) g;
1820 b->font = new;
1821 }
1822
1823 static FontInstance *GMenuBarGetFont(GGadget *g) {
1824 GMenuBar *b = (GMenuBar *) g;
1825 return( b->font );
1826 }
1827
1828 static void GMenuBarTestSize(GMenuBar *mb) {
1829 int arrow_size = mb->g.inner.height;
1830 int i;
1831
1832 if ( mb->xs[mb->mtot]<=mb->g.inner.width+4 ) {
1833 mb->lastmi = mb->mtot;
1834 } else {
1835 for ( i=mb->mtot-1; i>0 && mb->xs[i]>mb->g.inner.width-arrow_size; --i );
1836 mb->lastmi = i;
1837 memset(&mb->fake,0,sizeof(GMenuItem));
1838 mb->fake[0].sub = mb->mi+mb->lastmi;
1839 }
1840 }
1841
1842 static void GMenuBarResize(GGadget *g, int32_t width, int32_t height) {
1843 _ggadget_resize(g,width,height);
1844 GMenuBarTestSize((GMenuBar *) g);
1845 }
1846
1847 struct gfuncs gmenubar_funcs = {
1848 0,
1849 sizeof(struct gfuncs),
1850
1851 gmenubar_expose,
1852 gmenubar_mouse,
1853 NULL,
1854 NULL,
1855 NULL,
1856 NULL,
1857 NULL,
1858
1859 _ggadget_redraw,
1860 _ggadget_move,
1861 GMenuBarResize,
1862 _ggadget_setvisible,
1863 _ggadget_setenabled,
1864 _ggadget_getsize,
1865 _ggadget_getinnersize,
1866
1867 gmenubar_destroy,
1868
1869 NULL,
1870 NULL,
1871 NULL,
1872 NULL,
1873 NULL,
1874 GMenuBarSetFont,
1875 GMenuBarGetFont,
1876
1877 NULL,
1878 NULL,
1879 NULL,
1880 NULL,
1881 NULL,
1882 NULL,
1883 NULL,
1884 NULL,
1885 NULL,
1886 NULL,
1887 NULL,
1888
1889 NULL,
1890 NULL,
1891 NULL,
1892 NULL
1893 };
1894
1895 static void GMenuBarFit(GMenuBar *mb,GGadgetData *gd) {
1896 int bp = GBoxBorderWidth(mb->g.base,mb->g.box );
1897 GRect r;
1898
1899 if ( gd->pos.x <= 0 )
1900 mb->g.r.x = 0;
1901 if ( gd->pos.y <= 0 )
1902 mb->g.r.y = 0;
1903 if ( mb->g.r.width == 0 ) {
1904 GDrawGetSize(mb->g.base,&r);
1905 mb->g.r.width = r.width-mb->g.r.x;
1906 }
1907 if ( mb->g.r.height == 0 ) {
1908 int ld, i;
1909 GTextBounds bounds;
1910 GDrawWindowFontMetrics(mb->g.base, mb->font, &mb->ascender,
1911 &mb->descender, &ld);
1912 GDrawSetFont(mb->g.base, mb->font);
1913 for ( i=0; i<mb->mtot; ++i ) {
1914 GDrawGetTextBounds(mb->g.base, mb->mi[i].ti.text, -1, &bounds);
1915 if ( mb->ascender<bounds.as )
1916 mb->ascender = bounds.as;
1917 if ( mb->descender<bounds.ds )
1918 mb->descender = bounds.ds;
1919 }
1920 mb->g.r.height = mb->ascender+mb->descender+2*bp;
1921 }
1922 mb->g.inner.x = mb->g.r.x + bp;
1923 mb->g.inner.y = mb->g.r.y + bp;
1924 mb->g.inner.width = mb->g.r.width - 2*bp;
1925 mb->g.inner.height = mb->g.r.height - 2*bp;
1926 }
1927
1928 static void GMenuBarFindXs(GMenuBar *mb) {
1929 int i, wid;
1930
1931 GDrawSetFont(mb->g.base,mb->font);
1932 wid = GDrawPointsToPixels(mb->g.base,8);
1933 mb->xs[0] = GDrawPointsToPixels(mb->g.base,2);
1934 for ( i=0; i<mb->mtot; ++i )
1935 mb->xs[i+1] = mb->xs[i]+wid+GTextInfoGetWidth(mb->g.base,&mb->mi[i].ti,NULL);
1936 GMenuBarTestSize(mb);
1937 }
1938
1939 static void MenuMaskInit(GMenuItem *mi) {
1940 int mask = GMenuItemArrayMask(mi);
1941 if ( mask_set )
1942 menumask |= mask;
1943 else {
1944 menumask = mask;
1945 mask_set = true;
1946 }
1947 }
1948
1949 GGadget *GMenuBarCreate(struct gwindow *base, GGadgetData *gd,void *data) {
1950 GMenuBar *mb = calloc(1,sizeof(GMenuBar));
1951
1952 GMenuInit();
1953 mb->g.funcs = &gmenubar_funcs;
1954 _GGadget_Create(&mb->g,base,gd,data,&menubar_box);
1955
1956 mb->mi = GMenuItemArrayCopy(gd->u.menu,&mb->mtot);
1957 mb->xs = malloc((mb->mtot+1)*sizeof(uint16_t));
1958 mb->entry_with_mouse = -1;
1959 mb->font = menubar_font.fi;
1960
1961 GMenuBarFit(mb,gd);
1962 GMenuBarFindXs(mb);
1963
1964 MenuMaskInit(mb->mi);
1965 mb->any_unmasked_shortcuts = GMenuItemArrayAnyUnmasked(mb->mi);
1966
1967 if ( gd->flags & gg_group_end )
1968 _GGadgetCloseGroup(&mb->g);
1969 _GWidget_SetMenuBar(&mb->g);
1970
1971 mb->g.takes_input = true;
1972 return( &mb->g );
1973 }
1974
1975 GGadget *GMenu2BarCreate(struct gwindow *base, GGadgetData *gd,void *data) {
1976 GMenuBar *mb = calloc(1,sizeof(GMenuBar));
1977
1978 GMenuInit();
1979 mb->g.funcs = &gmenubar_funcs;
1980 _GGadget_Create(&mb->g,base,gd,data,&menubar_box);
1981
1982 mb->mi = GMenuItem2ArrayCopy(gd->u.menu2,&mb->mtot);
1983 mb->xs = malloc((mb->mtot+1)*sizeof(uint16_t));
1984 mb->entry_with_mouse = -1;
1985 mb->font = menubar_font.fi;
1986
1987 GMenuBarFit(mb,gd);
1988 GMenuBarFindXs(mb);
1989
1990 MenuMaskInit(mb->mi);
1991 mb->any_unmasked_shortcuts = GMenuItemArrayAnyUnmasked(mb->mi);
1992
1993 if ( gd->flags & gg_group_end )
1994 _GGadgetCloseGroup(&mb->g);
1995 _GWidget_SetMenuBar(&mb->g);
1996
1997 mb->g.takes_input = true;
1998 return( &mb->g );
1999 }
2000
2001 /* ************************************************************************** */
2002 static GMenuItem *GMenuBarFindMid(GMenuItem *mi, int mid) {
2003 int i;
2004 GMenuItem *ret;
2005
2006 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) {
2007 if ( mi[i].mid == mid )
2008 return( &mi[i]);
2009 if ( mi[i].sub!=NULL ) {
2010 ret = GMenuBarFindMid( mi[i].sub, mid );
2011 if ( ret!=NULL )
2012 return( ret );
2013 }
2014 }
2015 return( NULL );
2016 }
2017
2018 void GMenuBarSetItemChecked(GGadget *g, int mid, int check) {
2019 GMenuBar *mb = (GMenuBar *) g;
2020 GMenuItem *item;
2021
2022 item = GMenuBarFindMid(mb->mi,mid);
2023 if ( item!=NULL )
2024 item->ti.checked = check;
2025 }
2026
2027 void GMenuBarSetItemEnabled(GGadget *g, int mid, int enabled) {
2028 GMenuBar *mb = (GMenuBar *) g;
2029 GMenuItem *item;
2030
2031 item = GMenuBarFindMid(mb->mi,mid);
2032 if ( item!=NULL )
2033 item->ti.disabled = !enabled;
2034 }
2035
2036 void GMenuBarSetItemName(GGadget *g, int mid, const unichar_t *name) {
2037 GMenuBar *mb = (GMenuBar *) g;
2038 GMenuItem *item;
2039
2040 item = GMenuBarFindMid(mb->mi,mid);
2041 if ( item!=NULL ) {
2042 free( item->ti.text );
2043 item->ti.text = u_copy(name);
2044 }
2045 }
2046
2047 /* Check to see if event matches the given shortcut, expressed in our standard*/
2048 /* syntax and subject to gettext translation */
2049 int GMenuIsCommand(GEvent *event,char *shortcut) {
2050 GMenuItem foo;
2051
2052 if ( event->type!=et_char )
2053 return( false );
2054
2055 memset(&foo,0,sizeof(foo));
2056
2057 GMenuItemParseShortCut(&foo,shortcut);
2058
2059 return( (menumask&event->u.chr.state)==foo.short_mask && GDrawShortcutKeyMatches(event, foo.shortcut) );
2060 }
2061
2062 int GMenuMask(void) {
2063 return( menumask );
2064 }
2065
2066 int GMenuAnyUnmaskedShortcuts(GGadget *mb1, GGadget *mb2) {
2067
2068 if ( most_recent_popup_menu!=NULL && most_recent_popup_menu->any_unmasked_shortcuts )
2069 return( true );
2070
2071 if ( mb1!=NULL && ((GMenuBar *) mb1)->any_unmasked_shortcuts )
2072 return( true );
2073
2074 if ( mb2!=NULL && ((GMenuBar *) mb2)->any_unmasked_shortcuts )
2075 return( true );
2076
2077 return( false );
2078 }