/* Copyright (C) 2000-2012 by George Williams */ /* * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include "gdraw.h" #include "ggadgetP.h" #include "gkeysym.h" #include "gresourceP.h" #include "gwidget.h" #include "hotkeys.h" #include "prefs.h" #include "ustring.h" #include "utype.h" static GBox menubar_box = GBOX_EMPTY; /* Don't initialize here */ static GBox menu_box = GBOX_EMPTY; /* Don't initialize here */ static GResFont menu_font = GRESFONT_INIT("400 10pt " SANS_UI_FAMILIES); static GResFont menubar_font = GRESFONT_INIT("400 10pt " SANS_UI_FAMILIES); #ifdef __Mac static int mac_menu_icons = true; #else static int mac_menu_icons = false; #endif static int menu_3d_look = 1; // The 3D look is the default/legacy setting. static int menu_grabs=true; static int mask_set=0; 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 */ static GResInfo gmenu_ri; GResInfo gmenubar_ri = { &gmenu_ri, &ggadget_ri,&gmenu_ri, NULL, &menubar_box, &menubar_font, NULL, NULL, N_("Menu Bar"), N_("Menu Bar"), "GMenuBar", "Gdraw", false, false, omf_border_shape|omf_border_width|box_foreground_border_outer, { 0, bs_rect, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, GBOX_EMPTY, NULL, NULL, NULL }; static struct resed menu_re[] = { {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 }, {N_("3D Look"), "3DLook", rt_bool, &menu_3d_look, N_("Whether the menu is presented with a 3D appearance"), NULL, { 0 }, 0, 0 }, {N_("Grab"), "Grab", rt_bool, &menu_grabs, N_("Whether the menu 'grabs' the mouse pointer"), NULL, { 0 }, 0, 0 }, RESED_EMPTY }; extern GResInfo gmatrixedit_ri; static GResInfo gmenu_ri = { &gmatrixedit_ri, &ggadget_ri,&gmenubar_ri, NULL, &menu_box, &menu_font, NULL, menu_re, N_("Menu"), N_("Menu"), "GMenu", "Gdraw", false, false, omf_border_shape|omf_padding|box_foreground_border_outer, { 0, bs_rect, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, GBOX_EMPTY, NULL, NULL, NULL }; static void GMenuInit(void) { GResEditDoInit(&gmenubar_ri); GResEditDoInit(&gmenu_ri); } char* HKTextInfoToUntranslatedText( char* text_untranslated ); char* HKTextInfoToUntranslatedTextFromTextInfo( GTextInfo* ti ); static void GMenuBarChangeSelection(GMenuBar *mb, int newsel,GEvent *); static struct gmenu *GMenuCreateSubMenu(struct gmenu *parent,GMenuItem *mi,int disable); static struct gmenu *GMenuCreatePulldownMenu(GMenuBar *mb,GMenuItem *mi, int disabled); static struct gmenu *most_recent_popup_menu = NULL; typedef struct gmenu { unsigned int hasticks: 1; unsigned int pressed: 1; unsigned int initial_press: 1; unsigned int scrollup: 1; unsigned int freemi: 1; unsigned int disabled: 1; unsigned int dying: 1; unsigned int hidden: 1; unsigned int any_unmasked_shortcuts: 1; /* Only set for popup menus. Else info in menubar */ int bp; int tickoff, tioff, rightedge; int width, height; int line_with_mouse; int offtop, lcnt, mcnt; GMenuItem *mi; int fh, as; GWindow w; GBox *box; struct gmenu *parent, *child; struct gmenubar *menubar; GWindow owner; GTimer *scrollit; /* No longer in use, see comment below */ FontInstance *font; void (*donecallback)(GWindow owner); GIC *gic; char subMenuName[100]; /* The code below still contains the old code for using up/down arrows */ /* in the menu instead of a scrollbar. If vsb==NULL and the menu doesn't */ /* fit on the screen, then the old code will be implemented (normally */ /* that won't happen, but if I ever want to re-enable it, this is how */ /* The up arrow was placed at the top of the menu IFF there were lines off */ /* the top, similarly for the bottom. Clicking on either one would scroll */ /* in the indicated directions */ GGadget *vsb; } GMenu; static int GMenuGetMenuPathRecurse( GMenuItem** stack, GMenuItem *basemi, GMenuItem *targetmi ) { GMenuItem *mi = basemi; int i; for ( i=0; mi[i].ti.text || mi[i].ti.text_untranslated || mi[i].ti.image || mi[i].ti.line; ++i ) { // TRACE("text_untranslated: %s\n", mi[i].ti.text_untranslated ); if ( mi[i].sub ) { // TRACE("GMenuGetMenuPathRecurse() going down on %s\n", u_to_c(mi[i].ti.text)); stack[0] = &mi[i]; int rc = GMenuGetMenuPathRecurse( &stack[1], mi[i].sub, targetmi ); if( rc == 1 ) return rc; stack[0] = 0; } if( mi[i].ti.text ) { // TRACE("GMenuGetMenuPathRecurse() inspecting %s %p %p\n", u_to_c(mi[i].ti.text),mi[i],targetmi); GMenuItem* cur = &mi[i]; if( cur == targetmi ) { stack[0] = cur; return 1; } } } return 0; } /** * Get a string describing the path from basemi to the targetmi. * For example, if the basemi is the first mi in the whole menu structure * and targetmi is to the edit / select all menu item then the return * value is: * edit.select all * * If you can't get to targetmi from basemi then return 0. * If something doesn't make sense then return 0. * * Do not free the return value, it is a pointer into a buffer owned * by this function. */ static char* GMenuGetMenuPath( GMenuItem *basemi, GMenuItem *targetmi ) { GMenuItem* stack[1024]; memset(stack, 0, sizeof(stack)); if( !targetmi->ti.text ) return 0; // TRACE("GMenuGetMenuPath() base %s\n", u_to_c(basemi->ti.text)); // TRACE("GMenuGetMenuPath() target %s\n", u_to_c(targetmi->ti.text)); /* { */ /* int i=0; */ /* GMenuItem *mi = basemi; */ /* for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) { */ /* if( mi[i].ti.text ) { */ /* TRACE("GMenuGetMenuPath() xbase %s\n", u_to_c(mi[i].ti.text)); */ /* } */ /* } */ /* } */ /* TRACE("GMenuGetMenuPath() starting...\n"); */ GMenuItem *mi = basemi; int i; for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) { if( mi[i].ti.text ) { memset(stack, 0, sizeof(stack)); // TRACE("GMenuGetMenuPath() xbase %s\n", u_to_c(mi[i].ti.text)); // TRACE("GMenuGetMenuPath() untrans %s\n", mi[i].ti.text_untranslated); GMenuGetMenuPathRecurse( stack, &mi[i], targetmi ); if( stack[0] != 0 ) { // TRACE("GMenuGetMenuPath() have stack[0]...\n"); break; } } } if( stack[0] == 0 ) { return 0; } static char buffer[PATH_MAX]; buffer[0] = '\0'; for( i=0; stack[i]; i++ ) { if( i ) strcat(buffer,"."); if( stack[i]->ti.text_untranslated ) { char* tmp = HKTextInfoToUntranslatedTextFromTextInfo( &stack[i]->ti ); // TRACE("adding %s\n", HKTextInfoToUntranslatedTextFromTextInfo( &stack[i]->ti )); strcat( buffer, tmp); free(tmp); } else if( stack[i]->ti.text ) { cu_strcat(buffer,stack[i]->ti.text); } // TRACE("GMenuGetMenuPath() stack at i:%d mi %s\n", i, u_to_c(stack[i]->ti.text)); } return buffer; } static void GMenuDrawCheckMark(struct gmenu *m, Color fg, int ybase, int r2l) { int as = m->as; int pt = GDrawPointsToPixels(m->w,1); int x = r2l ? m->width-m->tioff+2*pt : m->tickoff; while ( pt>1 && 2*pt>=as/3 ) --pt; GDrawSetLineWidth(m->w,pt); GDrawDrawLine(m->w,x+2*pt,ybase-as/3,x+as/3,ybase-2*pt,fg); GDrawDrawLine(m->w,x+2*pt,ybase-as/3-pt,x+as/3,ybase-3*pt,fg); GDrawDrawLine(m->w,x+as/3,ybase-2*pt,x+as/3+as/5,ybase-2*pt-as/4,fg); 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); 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); } static void GMenuDrawUncheckMark(struct gmenu *m, Color fg, int ybase, int r2l) { } static void GMenuDrawArrow(struct gmenu *m, int ybase, int r2l) { int pt = GDrawPointsToPixels(m->w,1); int as = 2*(m->as/2); int x = r2l ? m->bp+2*pt : m->rightedge-2*pt; GPoint p[3]; GDrawSetLineWidth(m->w,pt); if ( r2l ) { p[0].x = x; p[0].y = ybase-as/2; p[1].x = x+1*(as/2); p[1].y = ybase; p[2].x = p[1].x; p[2].y = ybase-as; // If rendering menus in standard (3-dimensional) look, use the shadow colors for fake relief. // Otherwise, use foreground colors. if (menu_3d_look) { GDrawDrawLine(m->w,p[0].x,p[0].y,p[2].x,p[2].y,m->box->border_brighter); GDrawDrawLine(m->w,p[0].x+pt,p[0].y,p[2].x+pt,p[2].y+pt,m->box->border_brighter); GDrawDrawLine(m->w,p[1].x,p[1].y,p[0].x,p[0].y,m->box->border_darkest); GDrawDrawLine(m->w,p[1].x+pt,p[1].y-pt,p[0].x-pt,p[0].y,m->box->border_darkest); } else { GDrawDrawLine(m->w,p[0].x,p[0].y,p[2].x,p[2].y,m->box->main_foreground); GDrawDrawLine(m->w,p[0].x+pt,p[0].y,p[2].x+pt,p[2].y+pt,m->box->main_foreground); GDrawDrawLine(m->w,p[1].x,p[1].y,p[0].x,p[0].y,m->box->main_foreground); GDrawDrawLine(m->w,p[1].x+pt,p[1].y-pt,p[0].x-pt,p[0].y,m->box->main_foreground); } } else { p[0].x = x; p[0].y = ybase-as/2; p[1].x = x-1*(as/2); p[1].y = ybase; p[2].x = p[1].x; p[2].y = ybase-as; if (menu_3d_look) { GDrawDrawLine(m->w,p[0].x,p[0].y,p[2].x,p[2].y,m->box->border_brighter); GDrawDrawLine(m->w,p[0].x-pt,p[0].y,p[2].x+pt,p[2].y+pt,m->box->border_brighter); GDrawDrawLine(m->w,p[1].x,p[1].y,p[0].x,p[0].y,m->box->border_darkest); GDrawDrawLine(m->w,p[1].x+pt,p[1].y-pt,p[0].x-pt,p[0].y,m->box->border_darkest); } else { GDrawDrawLine(m->w,p[0].x,p[0].y,p[2].x,p[2].y,m->box->main_foreground); GDrawDrawLine(m->w,p[0].x-pt,p[0].y,p[2].x+pt,p[2].y+pt,m->box->main_foreground); GDrawDrawLine(m->w,p[1].x,p[1].y,p[0].x,p[0].y,m->box->main_foreground); GDrawDrawLine(m->w,p[1].x+pt,p[1].y-pt,p[0].x-pt,p[0].y,m->box->main_foreground); } } } static void GMenuDrawUpArrow(struct gmenu *m, int ybase) { int pt = GDrawPointsToPixels(m->w,1); int x = (m->rightedge+m->tickoff)/2; int as = 2*(m->as/2); GPoint p[3]; p[0].x = x; p[0].y = ybase - as; p[1].x = x-as; p[1].y = ybase; p[2].x = x+as; p[2].y = ybase; GDrawSetLineWidth(m->w,pt); // If rendering menus in standard (3-dimensional) look, use the shadow colors for fake relief. // Otherwise, use foreground colors. if (menu_3d_look) { GDrawDrawLine(m->w,p[0].x,p[0].y,p[1].x,p[1].y,m->box->border_brightest); GDrawDrawLine(m->w,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,m->box->border_brightest); GDrawDrawLine(m->w,p[1].x,p[1].y,p[2].x,p[2].y,m->box->border_darker); GDrawDrawLine(m->w,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,m->box->border_darker); GDrawDrawLine(m->w,p[2].x,p[2].y,p[0].x,p[0].y,m->box->border_darkest); GDrawDrawLine(m->w,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,m->box->border_darkest); } else { GDrawDrawLine(m->w,p[0].x,p[0].y,p[1].x,p[1].y,m->box->main_foreground); GDrawDrawLine(m->w,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,m->box->main_foreground); GDrawDrawLine(m->w,p[1].x,p[1].y,p[2].x,p[2].y,m->box->main_foreground); GDrawDrawLine(m->w,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,m->box->main_foreground); GDrawDrawLine(m->w,p[2].x,p[2].y,p[0].x,p[0].y,m->box->main_foreground); GDrawDrawLine(m->w,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,m->box->main_foreground); } } static void GMenuDrawDownArrow(struct gmenu *m, int ybase) { int pt = GDrawPointsToPixels(m->w,1); int x = (m->rightedge+m->tickoff)/2; int as = 2*(m->as/2); GPoint p[3]; p[0].x = x; p[0].y = ybase; p[1].x = x-as; p[1].y = ybase - as; p[2].x = x+as; p[2].y = ybase - as; GDrawSetLineWidth(m->w,pt); // If rendering menus in standard (3-dimensional) look, use the shadow colors for fake relief. // Otherwise, use foreground colors. if (menu_3d_look) { GDrawDrawLine(m->w,p[0].x,p[0].y,p[1].x,p[1].y,m->box->border_darker); GDrawDrawLine(m->w,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,m->box->border_darker); GDrawDrawLine(m->w,p[1].x,p[1].y,p[2].x,p[2].y,m->box->border_brightest); GDrawDrawLine(m->w,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,m->box->border_brightest); GDrawDrawLine(m->w,p[2].x,p[2].y,p[0].x,p[0].y,m->box->border_darkest); GDrawDrawLine(m->w,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,m->box->border_darkest); } else { GDrawDrawLine(m->w,p[0].x,p[0].y,p[1].x,p[1].y,m->box->main_foreground); GDrawDrawLine(m->w,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,m->box->main_foreground); GDrawDrawLine(m->w,p[1].x,p[1].y,p[2].x,p[2].y,m->box->main_foreground); GDrawDrawLine(m->w,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,m->box->main_foreground); GDrawDrawLine(m->w,p[2].x,p[2].y,p[0].x,p[0].y,m->box->main_foreground); GDrawDrawLine(m->w,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,m->box->main_foreground); } } /** * Return the menu bar at the top of this menu list * */ static GMenuBar * getTopLevelMenubar( struct gmenu *m ) { while( m->parent ) { m = m->parent; } return m->menubar; } static int GMenuDrawMenuLine(struct gmenu *m, GMenuItem *mi, int y,GWindow pixmap) { unichar_t shortbuf[300]; int as = GTextInfoGetAs(m->w,&mi->ti,m->font); int h, width; Color fg = m->box->main_foreground; GRect old, new; int ybase = y+as; int r2l = false; int x; //printf("GMenuDrawMenuLine(top)\n"); /* if(mi->ti.text) */ /* TRACE("GMenuDrawMenuLine() mi:%s\n",u_to_c(mi->ti.text)); */ new.x = m->tickoff; new.width = m->rightedge-m->tickoff; new.y = y; new.height = GTextInfoGetHeight(pixmap,&mi->ti,m->font); GDrawPushClip(pixmap,&new,&old); if ( mi->ti.fg!=COLOR_DEFAULT && mi->ti.fg!=COLOR_UNKNOWN ) fg = mi->ti.fg; if ( mi->ti.disabled || m->disabled ) fg = m->box->disabled_foreground; if ( fg==COLOR_DEFAULT ) fg = GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)); if ( mi->ti.text!=NULL && isrighttoleft(mi->ti.text[0]) ) r2l = true; if ( r2l ) x = m->width-m->tioff-GTextInfoGetWidth(pixmap,&mi->ti,m->font); else x = m->tioff; h = GTextInfoDraw(pixmap,x,y,&mi->ti,m->font, (mi->ti.disabled || m->disabled )?m->box->disabled_foreground:fg, m->box->active_border,new.y+new.height, -1, -1); if ( mi->ti.checkable ) { if ( mi->ti.checked ) GMenuDrawCheckMark(m,fg,ybase,r2l); else GMenuDrawUncheckMark(m,fg,ybase,r2l); } if ( mi->sub!=NULL ) GMenuDrawArrow(m,ybase,r2l); else { /* TRACE("m->menubar: %p\n", m->menubar ); */ /* TRACE("m->parent: %p\n", m->parent ); */ /* TRACE("m->toplevel: %p\n", getTopLevelMenubar(m)); */ /** * Grab the hotkey if there is one. First we work out the * menubar for this menu item, and then get the path from the * menubar to the menuitem and pass that to the hotkey system * to get the hotkey if there is one for that menu item. */ GMenuBar* toplevel = getTopLevelMenubar(m); Hotkey* hk = 0; if( toplevel ) { hk = hotkeyFindByMenuPath( toplevel->g.base, GMenuGetMenuPath( toplevel->mi, mi )); } else if( m->owner && strlen(m->subMenuName) ) { hk = hotkeyFindByMenuPathInSubMenu( m->owner, m->subMenuName, GMenuGetMenuPath( m->mi, mi )); } shortbuf[0] = '\0'; if( hk ) { /* TRACE("m->menubar->mi: %p\n", toplevel->mi ); */ /* TRACE("m->menubar->window: %p\n", toplevel->g.base ); */ /* TRACE("drawline... hk: %p\n", hk ); */ char* keydesc = hk->text; if( mac_menu_icons ) { keydesc = hotkeyTextToMacModifiers( keydesc ); } utf82u_strcpy( shortbuf, keydesc ); if( keydesc != hk->text ) free( keydesc ); } width = GDrawGetTextWidth(pixmap,shortbuf,-1); if ( r2l ) { GDrawDrawText(pixmap,m->bp,ybase,shortbuf,-1,fg); } else { int x = m->rightedge-width; GDrawDrawText(pixmap,x,ybase,shortbuf,-1,fg); } } GDrawPopClip(pixmap,&old); return( y + h ); } static int gmenu_expose(struct gmenu *m, GEvent *event,GWindow pixmap) { GRect old1, old2; GRect r; int i; /* TRACE("gmenu_expose() m:%p ev:%p\n",m,event); */ GDrawPushClip(pixmap,&event->u.expose.rect,&old1); r.x = 0; r.width = m->width; r.y = 0; r.height = m->height; GBoxDrawBackground(pixmap,&r,m->box,gs_active,false); GBoxDrawBorder(pixmap,&r,m->box,gs_active,false); r.x = m->tickoff; r.width = m->rightedge-m->tickoff; r.y = m->bp; r.height = m->height - 2*m->bp; GDrawPushClip(pixmap,&r,&old2); for ( i = event->u.expose.rect.y/m->fh+m->offtop; imcnt && i<=(event->u.expose.rect.y+event->u.expose.rect.height)/m->fh+m->offtop; ++i ) { if ( i==m->offtop && m->offtop!=0 && m->vsb==NULL ) GMenuDrawUpArrow(m, m->bp+m->as); else if ( m->lcnt!=m->mcnt && i==m->lcnt+m->offtop-1 && i!=m->mcnt-1 ) { if ( m->vsb==NULL ) GMenuDrawDownArrow(m, m->bp+(i-m->offtop)*m->fh+m->as); else GMenuDrawMenuLine(m, &m->mi[i], m->bp+(i-m->offtop)*m->fh, pixmap); break; /* Otherwise we get bits of the line after the last */ } else GMenuDrawMenuLine(m, &m->mi[i], m->bp+(i-m->offtop)*m->fh, pixmap); } GDrawPopClip(pixmap,&old2); GDrawPopClip(pixmap,&old1); return( true ); } static void GMenuSetPressed(struct gmenu *m, int pressed) { while ( m->child!=NULL ) m = m->child; while ( m->parent!=NULL ) { m->pressed = pressed; m = m->parent; } m->pressed = pressed; if ( m->menubar!=NULL ) m->menubar->pressed = pressed; } static void _GMenuDestroy(struct gmenu *m) { if ( m->dying ) return; m->dying = true; if ( m->line_with_mouse!=-1 ) m->mi[m->line_with_mouse].ti.selected = false; if ( m->child!=NULL ) _GMenuDestroy(m->child); if ( m->parent!=NULL ) m->parent->child = NULL; else if ( m->menubar!=NULL ) { m->menubar->child = NULL; m->menubar->pressed = false; _GWidget_ClearPopupOwner((GGadget *) (m->menubar)); _GWidget_ClearGrabGadget((GGadget *) (m->menubar)); GMenuBarChangeSelection(m->menubar,-1,NULL); } GDrawDestroyWindow(m->w); /* data are freed when we get the destroy event !!!! */ } static void GMenuDestroy(struct gmenu *m) { GDrawPointerUngrab(GDrawGetDisplayOfWindow(m->w)); if ( menu_grabs && m->parent!=NULL ) GDrawPointerGrab(m->parent->w); _GMenuDestroy(m); } static void GMenuHideAll(struct gmenu *m) { if ( m!=NULL ) { struct gmenu *s = m; GDrawPointerUngrab(GDrawGetDisplayOfWindow(m->w)); while ( m->parent ) m = m->parent; while ( m ) { m->hidden = true; GDrawSetVisible(m->w,false); m=m->child; } GDrawSync(GDrawGetDisplayOfWindow(s->w)); GDrawProcessPendingEvents(GDrawGetDisplayOfWindow(s->w)); } } static void GMenuDismissAll(struct gmenu *m) { if ( m!=NULL ) { while ( m->parent ) m = m->parent; GMenuDestroy(m); } } static void UnsetInitialPress(struct gmenu *m) { while ( m!=NULL ) { m->initial_press = false; if ( m->menubar!=NULL ) m->menubar->initial_press = false; m = m->parent; } } static void GMenuChangeSelection(struct gmenu *m, int newsel,GEvent *event) { int old=m->line_with_mouse; if ( old==newsel ) return; if ( newsel==m->mcnt ) return; if ( m->child!=NULL ) { GMenuDestroy(m->child); m->child = NULL; } UnsetInitialPress(m); m->line_with_mouse = newsel; if ( newsel!=-1 ) m->mi[newsel].ti.selected = true; if ( old!=-1 ) m->mi[old].ti.selected = false; GDrawRequestExpose(m->w, NULL, false); if ( newsel!=-1 ) { if ( m->mi[newsel].moveto!=NULL ) (m->mi[newsel].moveto)(m->owner,&m->mi[newsel],event); if ( m->mi[newsel].sub!=NULL ) m->child = GMenuCreateSubMenu(m,m->mi[newsel].sub, m->disabled || m->mi[newsel].ti.disabled); } } static void GMenuBarChangeSelection(GMenuBar *mb, int newsel,GEvent *event) { int old=mb->entry_with_mouse; GMenuItem *mi; if ( old==newsel ) return; if ( mb->child!=NULL ) { int waspressed = mb->pressed; GMenuDestroy(mb->child); mb->child = NULL; mb->pressed = waspressed; } mb->entry_with_mouse = newsel; if ( newsel!=-1 ) mb->mi[newsel].ti.selected = true; if ( old!=-1 ) mb->mi[old].ti.selected = false; _ggadget_redraw(&mb->g); if ( newsel!=-1 ) { mi = newsel==mb->lastmi ? mb->fake : &mb->mi[newsel]; if ( mi->moveto!=NULL ) (mi->moveto)(mb->g.base,mi,event); if ( mi->sub!=NULL ) mb->child = GMenuCreatePulldownMenu(mb,mi->sub,mi->ti.disabled); } } static int MParentInitialPress(struct gmenu *m) { if ( m->parent!=NULL ) return( m->parent->initial_press ); else if ( m->menubar!=NULL ) return( m->menubar->initial_press ); return( false ); } static int gmenu_mouse(struct gmenu *m, GEvent *event) { GPoint p; struct gmenu *testm; if ( m->hidden || (m->child!=NULL && m->child->hidden)) return( true ); if ( event->type == et_crossing ) { if ( !event->u.crossing.entered ) UnsetInitialPress(m); return( true ); } else if ((event->type==et_mouseup || event->type==et_mousedown) && (event->u.mouse.button>=4 && event->u.mouse.button<=7) ) { //From scrollbar.c: X treats a scroll as a mousedown/mouseup event return GGadgetDispatchEvent(m->vsb,event); } p.x = event->u.mouse.x; p.y = event->u.mouse.y; for ( testm=m; testm->child!=NULL; testm = testm->child ); if ( testm->scrollit && testm!=m ) { GDrawCancelTimer(testm->scrollit); testm->scrollit = NULL; } for ( ; testm!=NULL; testm=testm->parent ) if ( GDrawEventInWindow(testm->w,event) ) break; if ( testm!=m && testm!=NULL ) { GDrawPointerGrab(testm->w); GDrawTranslateCoordinates(m->w,testm->w,&p); m = testm; } else if ( testm==NULL /*&& event->u.mouse.y<0*/ ) {/* menubars can be below the menu if no room on screen */ for ( testm=m; testm->parent!=NULL; testm=testm->parent ); if ( testm->menubar!=NULL ) { GDrawTranslateCoordinates(m->w,testm->menubar->g.base,&p); if ( p.x>=0 && p.y>=0 && p.xmenubar->g.inner.x+testm->menubar->g.inner.width && p.ymenubar->g.inner.y+testm->menubar->g.inner.height ) { /*GDrawPointerGrab(testm->menubar->g.base);*/ /* Don't do this */ event->u.mouse.x = p.x; event->u.mouse.y = p.y; return( (GDrawGetEH(testm->menubar->g.base))(testm->menubar->g.base,event)); } } testm = NULL; } if ( testm==NULL ) { if ( event->type==et_mousedown ) GMenuDismissAll(m); else if ( event->type==et_mouseup ) GMenuSetPressed(m,false); else if ( event->type==et_mousemove ) /* A displayed menu tracks the pointer whether or not a button is * held (sticky menus): moving off the menu clears the highlight. */ GMenuChangeSelection(m,-1,event); return( true ); } event->u.mouse.x = p.x; event->u.mouse.y = p.y; event->w = m->w; if ( event->type==et_mousemove || event->type == et_mousedown ) { int l = (event->u.mouse.y-m->bp)/m->fh; int i = l + m->offtop; if ( m->scrollit!=NULL ) ; else if ( event->u.mouse.ybp && event->type==et_mousedown ) GMenuDismissAll(m); else if ( l==0 && m->offtop!=0 && m->vsb==NULL ) { GMenuChangeSelection(m,-1,event); if ( m->scrollit==NULL ) m->scrollit = GDrawRequestTimer(m->w,1,_GScrollBar_RepeatTime,m); m->scrollup = true; } else if ( l>=m->lcnt-1 && m->offtop+m->lcntmcnt && m->vsb==NULL ) { GMenuChangeSelection(m,-1,event); if ( m->scrollit==NULL ) m->scrollit = GDrawRequestTimer(m->w,1,_GScrollBar_RepeatTime,m); m->scrollup = false; } else if ( event->type == et_mousedown && m->child!=NULL && i == m->line_with_mouse ) { GMenuChangeSelection(m,-1,event); } else if ( i >= m->mcnt ){ GMenuChangeSelection(m,-1,event); } else GMenuChangeSelection(m,i,event); if ( event->type == et_mousedown ) { GMenuSetPressed(m,true); if ( m->child!=NULL ) m->initial_press = true; } } else if ( event->type == et_mouseup && m->child==NULL ) { if ( m->scrollit!=NULL ) { GDrawCancelTimer(m->scrollit); m->scrollit = NULL; } else if ( event->u.mouse.y>=m->bp && event->u.mouse.x>=0 && event->u.mouse.yheight-m->bp && event->u.mouse.x < m->width && !MParentInitialPress(m)) { int l = (event->u.mouse.y-m->bp)/m->fh; int i = l + m->offtop; if ( !( l==0 && m->offtop!=0 && m->vsb==NULL ) && !( l==m->lcnt-1 && m->offtop+m->lcntmcnt && m->vsb==NULL ) && !m->disabled && !m->mi[i].ti.disabled && !m->mi[i].ti.line ) { if ( m->mi[i].ti.checkable ) m->mi[i].ti.checked = !m->mi[i].ti.checked; GMenuHideAll(m); GMenuDismissAll(m); if ( m->mi[i].invoke!=NULL ) (m->mi[i].invoke)(m->owner,&m->mi[i],event); } } } else if ( event->type == et_mouseup ) { UnsetInitialPress(m); GMenuSetPressed(m,false); } else return( false ); return( true ); } static int gmenu_timer(struct gmenu *m, GEvent *event) { if ( m->scrollup ) { if ( m->offtop==0 ) return(true); if ( --m->offtop<0 ) m->offtop = 0; } else { if ( m->offtop == m->mcnt-m->lcnt ) return( true ); ++m->offtop; if ( m->offtop + m->lcnt > m->mcnt ) m->offtop = m->mcnt-m->lcnt; } GDrawRequestExpose(m->w, NULL, false); return( true ); } static int GMenuKeyInvoke(struct gmenu *m, int i) { GMenuChangeSelection(m,i,NULL); if ( m->mi[i].ti.checkable ) m->mi[i].ti.checked = !m->mi[i].ti.checked; if ( m->mi[i].sub==NULL ) { GMenuHideAll(m); } if ( m->mi[i].invoke!=NULL ) (m->mi[i].invoke)(m->owner,&m->mi[i],NULL); if ( m->mi[i].sub==NULL ) { GMenuDismissAll(m); } return( true ); } static int GMenuBarKeyInvoke(struct gmenubar *mb, int i) { GMenuBarChangeSelection(mb,i,NULL); if ( mb->mi[i].invoke!=NULL ) (mb->mi[i].invoke)(mb->g.base,&mb->mi[i],NULL); return( true ); } /** * Remove any instances of 'ch' from 'ret' and return 'ret' * * When ch is encountered it is removed from the string, with all the * characters following it moved left to cover over the space ch used * to occupy. */ static char* str_remove_all_single_char( char * ret, char ch ) { char* src = ret; char* dst = ret; for( ; *src; src++ ) { if( *src == ch ) continue; *dst = *src; dst++; } *dst = '\0'; return ret; } /** * Given a text_untranslated which may be either the hotkey of format: * * Foo|ShortCutKey * Win*Foo|ShortCutKey * _Foo * * return just the english text for the entry, ie, Foo * * The return value is owned by this function, do not free it. */ char* HKTextInfoToUntranslatedText(char *text_untranslated) { char ret[PATH_MAX+1]; char* pt; int i; strncpy(ret,text_untranslated,PATH_MAX); if( (pt=strchr(ret,'*')) ) for (i=0; pt[i]!='\0'; i++) ret[i]=pt[i+1]; if( (pt=strchr(ret,'|')) ) *pt = '\0'; str_remove_all_single_char( ret, '_' ); return copy(ret); } /** * Call HKTextInfoToUntranslatedText on ti->text_untranslated * guarding against the chance of null for ti, and ti->text_untranslated */ char* HKTextInfoToUntranslatedTextFromTextInfo( GTextInfo* ti ) { if( !ti ) return 0; if( !ti->text_untranslated ) return 0; return HKTextInfoToUntranslatedText( ti->text_untranslated ); } /** * return true if the prefix matches the first segment of the given action. * For example, * return will be 0 when action = foo.bar.baz prefix = bar * return will be 0 when action = foo.bar.baz prefix = fooo * return will be 1 when action = foo.bar.baz prefix = foo * return will be 1 when action = baz prefix = baz */ static int HKActionMatchesFirstPartOf( char* action, char* prefix_const, int munge ) { char prefix[PATH_MAX+1]; char* pt = 0; strncpy( prefix, prefix_const, PATH_MAX ); if( munge ) { char *tofree = HKTextInfoToUntranslatedText(prefix_const); strncpy( prefix, tofree,PATH_MAX ); free(tofree); } // TRACE("munge:%d prefix2:%s\n", munge, prefix ); pt = strchr(action,'.'); if( !pt ) return( 0==strcmp(action,prefix) ); int l = pt - action; if( strlen(prefix) < l ) return 0; int rc = strncmp( action, prefix, l ); return rc == 0; } /** * Call HKActionMatchesFirstPartOf on the given ti. */ static int HKActionMatchesFirstPartOfTextInfo( char* action, GTextInfo* ti ) { if( ti->text_untranslated ) return HKActionMatchesFirstPartOf( action, ti->text_untranslated, 1 ); if( ti->text ) return HKActionMatchesFirstPartOf( action, u_to_c(ti->text), 0 ); return 0; } static char* HKActionPointerPastLeftmostKey( char* action ) { char* pt = strchr(action,'.'); if( !pt ) return 0; return pt + 1; } static GMenuItem *GMenuSearchActionRecursive( GWindow gw, GMenuItem *mi, char* action, GEvent *event, int call_moveto) { // TRACE("GMenuSearchAction() action:%s\n", action ); int i; for ( i=0; mi[i].ti.text || mi[i].ti.text_untranslated || mi[i].ti.image || mi[i].ti.line; ++i ) { // if( mi[i].ti.text ) // TRACE("GMenuSearchActionRecursive() text : %s\n", u_to_c(mi[i].ti.text) ); // TRACE("GMenuSearchActionRecursive() text_untranslated: %s\n", mi[i].ti.text_untranslated ); if ( call_moveto && mi[i].moveto != NULL) (mi[i].moveto)(gw,&(mi[i]),event); if ( mi[i].sub ) { if( HKActionMatchesFirstPartOfTextInfo( action, &mi[i].ti )) { char* subaction = HKActionPointerPastLeftmostKey(action); // TRACE("GMenuSearchAction() action:%s descending menu:%s\n", action, u_to_c(mi[i].ti.text) ); GMenuItem *ret = GMenuSearchActionRecursive(gw,mi[i].sub,subaction,event,call_moveto); if ( ret!=NULL ) return( ret ); } } else { // TRACE("GMenuSearchAction() action:%s testing menu:%s\n", action, u_to_c(mi[i].ti.text) ); if( HKActionMatchesFirstPartOfTextInfo( action, &mi[i].ti )) { // TRACE("GMenuSearchAction() matching final menu part! action:%s\n", action ); return &mi[i]; } } } return( NULL ); } static GMenuItem *GMenuSearchAction( GWindow gw, GMenuItem *mi, char* action, GEvent *event, int call_moveto) { char* windowType = GDrawGetWindowTypeName( gw ); if( !windowType ) return 0; // TRACE("GMenuSearchAction() windowtype:%s\n", windowType ); int actionlen = strlen(action); int prefixlen = strlen(windowType) + 1 + strlen("Menu."); if( actionlen < prefixlen ) { return 0; } action += prefixlen; GMenuItem * ret = GMenuSearchActionRecursive( gw, mi, action, event, call_moveto ); return ret; } static GMenuItem *GMenuSearchShortcut(GWindow gw, GMenuItem *mi, GEvent *event, int call_moveto) { int i; for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) { if ( call_moveto && mi[i].moveto != NULL) (mi[i].moveto)(gw,&(mi[i]),event); if ( mi[i].sub==NULL && (menumask&event->u.chr.state)==mi[i].short_mask && GDrawShortcutKeyMatches(event, mi[i].shortcut) ) return( &mi[i]); else if ( mi[i].sub!=NULL ) { GMenuItem *ret = GMenuSearchShortcut(gw,mi[i].sub,event,call_moveto); if ( ret!=NULL ) return( ret ); } } return( NULL ); } static int GMenuSpecialKeys(struct gmenu *m, unichar_t keysym, GEvent *event) { switch ( keysym ) { case GK_Escape: GMenuDestroy(m); return( true ); case GK_Return: if ( m->line_with_mouse==-1 ) { int ns=0; while ( nsmcnt && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) ++ns; if ( nsmcnt ) GMenuChangeSelection(m,ns,event); } else if ( m->mi[m->line_with_mouse].sub!=NULL && m->child==NULL ) { m->child = GMenuCreateSubMenu(m,m->mi[m->line_with_mouse].sub, m->disabled || m->mi[m->line_with_mouse].ti.disabled); } else { int i = m->line_with_mouse; if ( !m->disabled && !m->mi[i].ti.disabled && !m->mi[i].ti.line ) { if ( m->mi[i].ti.checkable ) m->mi[i].ti.checked = !m->mi[i].ti.checked; GMenuDismissAll(m); if ( m->mi[i].invoke!=NULL ) (m->mi[i].invoke)(m->owner,&m->mi[i],event); } else GMenuDismissAll(m); } return( true ); case GK_Left: case GK_KP_Left: if ( m->parent!=NULL ) { GMenuDestroy(m); return( true ); } else if ( m->menubar!=NULL ) { GMenuBar *mb = m->menubar; int en = mb->entry_with_mouse; int lastmi = mb->fake[0].sub!=NULL ? mb->lastmi+1 : mb->lastmi; if ( en>0 ) { GMenuBarChangeSelection(mb,en-1,event); } else GMenuBarChangeSelection(mb,lastmi-1,event); return( true ); } /* Else fall into the "Up" case */ case GK_Up: case GK_KP_Up: case GK_Page_Up: case GK_KP_Page_Up: { int ns; if ( keysym!=GK_Left && keysym!=GK_KP_Left ) { while ( m->line_with_mouse==-1 && m->parent!=NULL ) { GMenu *p = m->parent; GMenuDestroy(m); m = p; } } ns = m->line_with_mouse-1; while ( ns>=0 && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) --ns; if ( ns<0 ) { ns = m->mcnt-1; while ( ns>=0 && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) --ns; } if ( ns<0 && m->line_with_mouse==-1 ) { /* Nothing selectable? get rid of menu */ GMenuDestroy(m); return( true ); } if ( ns<0 ) ns = -1; GMenuChangeSelection(m,ns,NULL); return( true ); } case GK_Right: case GK_KP_Right: if ( m->line_with_mouse!=-1 && m->mi[m->line_with_mouse].sub!=NULL && m->child==NULL ) { m->child = GMenuCreateSubMenu(m,m->mi[m->line_with_mouse].sub, m->disabled || m->mi[m->line_with_mouse].ti.disabled); return( true ); } else if ( m->parent==NULL && m->menubar!=NULL ) { GMenuBar *mb = m->menubar; int en = mb->entry_with_mouse; int lastmi = mb->fake[0].sub!=NULL ? mb->lastmi+1 : mb->lastmi; if ( en+1line_with_mouse==-1 && m->parent!=NULL ) { GMenu *p = m->parent; GMenuDestroy(m); m = p; } } ns = m->line_with_mouse+1; while ( nsmcnt && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) ++ns; if ( ns>=m->mcnt ) { ns = 0; while ( nsmcnt && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) ++ns; } if ( ns>=m->mcnt && m->line_with_mouse==-1 ) { /* Nothing selectable? get rid of menu */ GMenuDestroy(m); return( true ); } GMenuChangeSelection(m,ns,event); return( true ); } case GK_Home: case GK_KP_Home: { int ns=0; while ( nsmcnt && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) ++ns; if ( ns!=m->mcnt ) GMenuChangeSelection(m,ns,event); return( true ); } case GK_End: case GK_KP_End: { int ns=m->mcnt-1; while ( ns>=0 && (m->mi[ns].ti.disabled || m->mi[ns].ti.line)) --ns; if ( ns>=0 ) GMenuChangeSelection(m,ns,event); return( true ); } } return( false ); } static int gmenu_key(struct gmenu *m, GEvent *event) { int i; GMenuItem *mi; GMenu *top; if ( m->dying ) return( false ); if ( event->u.chr.state&ksm_meta && !(event->u.chr.state&(menumask&~(ksm_meta|ksm_shift)))) { /* Only look for mneumonics in the child */ while ( m->child!=NULL ) m = m->child; for ( i=0; imcnt; ++i ) { if ( !m->disabled && !m->mi[i].ti.disabled && GDrawShortcutKeyMatches(event, m->mi[i].ti.mnemonic) ) { GMenuKeyInvoke(m,i); return( true ); } } } /* then look for shortcuts everywhere */ if ( (event->u.chr.state&(menumask&~ksm_shift)) || event->u.chr.keysym>=GK_Special ) { for ( top = m; top->parent!=NULL ; top = top->parent ); if ( top->menubar!=NULL ) mi = GMenuSearchShortcut(top->owner,top->menubar->mi,event,false); else mi = GMenuSearchShortcut(top->owner,top->mi,event,false); if ( mi!=NULL ) { if ( mi->ti.checkable ) mi->ti.checked = !mi->ti.checked; GMenuHideAll(top); if ( mi->invoke!=NULL ) (mi->invoke)(m->owner,mi,event); GMenuDestroy(m); return( true ); } for ( ; m->child!=NULL ; m = m->child ); return( GMenuSpecialKeys(m,event->u.chr.keysym,event)); } return( false ); } static int gmenu_destroy(struct gmenu *m) { if ( most_recent_popup_menu==m ) most_recent_popup_menu = NULL; if ( m->donecallback ) (m->donecallback)(m->owner); if ( m->freemi ) GMenuItemArrayFree(m->mi); free(m); return( true ); } static int gmenu_eh(GWindow w,GEvent *ge) { GMenu *m = (GMenu *) GDrawGetUserData(w); switch ( ge->type ) { case et_map: /* I need to initialize the input context, but I can't do that until */ /* the menu pops up */ if ( ge->u.map.is_visible && m->gic!=NULL ) GDrawSetGIC(w,m->gic,0,20); return( true ); case et_expose: return( gmenu_expose(m,ge,w)); case et_char: return( gmenu_key(m,ge)); case et_mousemove: case et_mousedown: case et_mouseup: case et_crossing: return( gmenu_mouse(m,ge)); case et_timer: return( gmenu_timer(m,ge)); case et_destroy: return( gmenu_destroy(m)); case et_close: GMenuDestroy(m); return( true ); default: break; } return( false ); } static int gmenu_scroll(GGadget *g, GEvent *event) { enum sb sbt = event->u.control.u.sb.type; GMenu *m = (GMenu *) (g->data); int newpos = m->offtop; if ( sbt==et_sb_top ) newpos = 0; else if ( sbt==et_sb_bottom ) newpos = m->mcnt-m->lcnt; else if ( sbt==et_sb_up ) { --newpos; } else if ( sbt==et_sb_down ) { ++newpos; } else if ( sbt==et_sb_uppage ) { if ( m->lcnt!=1 ) /* Normally we leave one line in window from before, except if only one line fits */ newpos -= m->lcnt-1; else newpos -= 1; } else if ( sbt==et_sb_downpage ) { if ( m->lcnt!=1 ) /* Normally we leave one line in window from before, except if only one line fits */ newpos += m->lcnt-1; else newpos += 1; } else /* if ( sbt==et_sb_thumb || sbt==et_sb_thumbrelease ) */ { newpos = event->u.control.u.sb.pos; } if ( newpos+m->lcnt > m->mcnt ) newpos = m->mcnt-m->lcnt; if ( newpos<0 ) newpos = 0; if ( newpos!= m->offtop ) { m->offtop = newpos; GScrollBarSetPos(m->vsb,newpos); GDrawRequestExpose(m->w,NULL,false); } return( true ); } static GMenu *_GMenu_Create( GMenuBar* toplevel, GWindow owner, GMenuItem *mi, GPoint *where, int awidth, int aheight, GFont *font, int disable, char* subMenuName ) { GMenu *m = calloc(1,sizeof(GMenu)); GRect pos; GDisplay *disp = GDrawGetDisplayOfWindow(owner); GWindowAttrs pattrs; int i, width, max_iwidth = 0, max_hkwidth = 0; unichar_t buffer[300]; extern int _GScrollBar_Width; int ds, ld, temp, lh; int sbwidth = 0; GRect screen; m->owner = owner; m->mi = mi; m->disabled = disable; m->font = font; m->box = &menu_box; m->tickoff = m->tioff = m->bp = GBoxBorderWidth(owner,m->box); m->line_with_mouse = -1; if( subMenuName ) strncpy(m->subMenuName,subMenuName,sizeof(m->subMenuName)-1); /* Mnemonics in menus don't work under gnome. Turning off nodecor makes them */ /* work, but that seems a high price to pay */ pattrs.mask = wam_events|wam_nodecor|wam_positioned|wam_cursor|wam_transient|wam_verytransient; pattrs.event_masks = -1; pattrs.nodecoration = true; pattrs.positioned = true; pattrs.cursor = ct_pointer; pattrs.transient = GWidgetGetTopWidget(owner); pos.x = pos.y = 0; pos.width = pos.height = 100; m->w = GDrawCreateTopWindow(disp,&pos,gmenu_eh,m,&pattrs); m->gic = GDrawCreateInputContext(m->w,gic_root|gic_orlesser); GDrawWindowFontMetrics(m->w,m->font,&m->as, &ds, &ld); m->fh = m->as + ds + 1; /* I need some extra space, else mneumonic underlines look bad */ lh = m->fh; GDrawSetFont(m->w,m->font); m->hasticks = false; for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) { if ( mi[i].ti.checkable ) m->hasticks = true; temp = GTextInfoGetWidth(m->w,&mi[i].ti,m->font); if (temp > max_iwidth) max_iwidth = temp; uc_strcpy(buffer,""); /** * Grab the hotkey if there is one. First we work out the * menubar for this menu item, and then get the path from the * menubar to the menuitem and pass that to the hotkey system * to get the hotkey if there is one for that menu item. * * If we have a hotkey then set the width to the text porition * first and then add the modifier icons if there are to be * any for the key combination. */ // TRACE("_gmenu_create() toplevel:%p owner:%p\n", toplevel, owner ); Hotkey* hk = 0; if( toplevel ) { hk = hotkeyFindByMenuPath( toplevel->g.base, GMenuGetMenuPath( toplevel->mi, &mi[i] )); } else if( owner && strlen(m->subMenuName) ) { hk = hotkeyFindByMenuPathInSubMenu( owner, m->subMenuName, GMenuGetMenuPath( mi, &mi[i] )); } // TRACE("hk:%p\n", hk); if( hk ) { char* keydesc = hk->text; if( mac_menu_icons ) { // keydesc = hotkeyTextWithoutModifiers( keydesc ); } uc_strcpy( buffer, keydesc ); temp = GDrawGetTextWidth(m->w,buffer,-1); if (temp > max_hkwidth) max_hkwidth = temp; /* if( short_mask && mac_menu_icons ) { */ /* temp += GMenuMacIconsWidth( m, short_mask ); */ /* } */ } temp = GTextInfoGetHeight(m->w,&mi[i].ti,m->font); if ( temp>lh ) { if ( temp>3*m->fh/2 ) temp = 3*m->fh/2; lh = temp; } } m->fh = lh; m->mcnt = m->lcnt = i; //Width: Max item length + max length of hotkey text + padding width = max_iwidth + max_hkwidth + GDrawPointsToPixels(m->w, 10); if ( m->hasticks ) { int ticklen = m->as + GDrawPointsToPixels(m->w,5); width += ticklen; m->tioff += ticklen; } m->width = pos.width = width + 2*m->bp; m->rightedge = m->width - m->bp; m->height = pos.height = i*m->fh + 2*m->bp; GDrawGetSize(GDrawGetRoot(disp),&screen); sbwidth = 0; /* On the mac, the menu bar takes up the top twenty pixels or so of screen */ /* so never put a menu that high */ #define MAC_MENUBAR 20 if ( pos.height > screen.height-MAC_MENUBAR-m->fh ) { GGadgetData gd; m->lcnt = (screen.height-MAC_MENUBAR-m->fh-2*m->bp)/m->fh; m->height = pos.height = m->lcnt*m->fh + 2*m->bp; /* It's too long, so add a scrollbar */ sbwidth = GDrawPointsToPixels(owner,_GScrollBar_Width); pos.width += sbwidth; memset(&gd,'\0',sizeof(gd)); gd.pos.y = 0; gd.pos.height = pos.height; gd.pos.width = sbwidth; gd.pos.x = m->width; gd.flags = gg_visible|gg_enabled|gg_pos_in_pixels|gg_sb_vert|gg_pos_use0; gd.handle_controlevent = gmenu_scroll; m->vsb = GScrollBarCreate(m->w,&gd,m); GScrollBarSetBounds(m->vsb,0,m->mcnt,m->lcnt); } pos.x = where->x; pos.y = where->y; if ( pos.y + pos.height > screen.height-MAC_MENUBAR ) { if ( where->y+aheight-pos.height >= MAC_MENUBAR ) pos.y = where->y+aheight-pos.height; else { pos.y = MAC_MENUBAR; /* Ok, it's going to overlap the press point if we got here */ /* let's see if we can shift it left/right a bit so it won't */ if ( awidth<0 ) /* Oh, well, I guess it won't. It's a submenu and we've already */ /* moved off to the left */; else if ( pos.x+awidth+pos.width+3=0 ) pos.x -= pos.width+3; else { /* There doesn't seem much we can do in this case */ ; } } } if ( pos.x+pos.width > screen.width ) { if ( where->x+awidth-pos.width >= 0 ) pos.x = where->x+awidth-pos.width-3; else pos.x = 0; } GDrawResize(m->w,pos.width,pos.height); GDrawMove(m->w,pos.x,pos.y); GDrawSetVisible(m->w,true); if ( menu_grabs ) GDrawPointerGrab(m->w); return( m ); } static GMenu *GMenuCreateSubMenu(GMenu *parent,GMenuItem *mi,int disable) { GPoint p; GMenu *m; char *subMenuName = 0; p.x = parent->width; p.y = (parent->line_with_mouse-parent->offtop)*parent->fh + parent->bp; GDrawTranslateCoordinates(parent->w,GDrawGetRoot(GDrawGetDisplayOfWindow(parent->w)),&p); m = _GMenu_Create(getTopLevelMenubar(parent),parent->owner,mi,&p,-parent->width,parent->fh, parent->font, disable, subMenuName ); m->parent = parent; m->pressed = parent->pressed; return( m ); } static GMenu *GMenuCreatePulldownMenu(GMenuBar *mb,GMenuItem *mi,int disabled) { GPoint p; GMenu *m; char *subMenuName = 0; p.x = mb->g.inner.x + mb->xs[mb->entry_with_mouse]- GBoxDrawnWidth(mb->g.base,&menu_box ); p.y = mb->g.r.y + mb->g.r.height; GDrawTranslateCoordinates(mb->g.base,GDrawGetRoot(GDrawGetDisplayOfWindow(mb->g.base)),&p); m = _GMenu_Create( mb, mb->g.base, mi, &p, mb->xs[mb->entry_with_mouse+1]-mb->xs[mb->entry_with_mouse], -mb->g.r.height, mb->font, disabled, subMenuName ); m->menubar = mb; m->pressed = mb->pressed; _GWidget_SetPopupOwner((GGadget *) mb); return( m ); } GWindow _GMenuCreatePopupMenu( GWindow owner,GEvent *event, GMenuItem *mi, void (*donecallback)(GWindow) ) { char *subMenuName = 0; return _GMenuCreatePopupMenuWithName( owner, event, mi, subMenuName, donecallback ); } GWindow _GMenuCreatePopupMenuWithName( GWindow owner,GEvent *event, GMenuItem *mi, char *subMenuName, void (*donecallback)(GWindow) ) { GPoint p; GMenu *m; GEvent e; GMenuInit(); p.x = event->u.mouse.x; p.y = event->u.mouse.y; GDrawTranslateCoordinates(owner,GDrawGetRoot(GDrawGetDisplayOfWindow(owner)),&p); m = _GMenu_Create( 0, owner, GMenuItemArrayCopy(mi,NULL), &p, 0, 0, menu_font.fi,false, subMenuName ); m->any_unmasked_shortcuts = GMenuItemArrayAnyUnmasked(m->mi); GDrawPointerUngrab(GDrawGetDisplayOfWindow(owner)); GDrawPointerGrab(m->w); GDrawGetPointerPosition(m->w,&e); if ( e.u.mouse.state & (ksm_button1|ksm_button2|ksm_button3) ) m->pressed = m->initial_press = true; m->donecallback = donecallback; m->freemi = true; most_recent_popup_menu = m; return( m->w ); } GWindow GMenuCreatePopupMenu(GWindow owner,GEvent *event, GMenuItem *mi) { char* subMenuName = 0; return( _GMenuCreatePopupMenuWithName(owner,event,mi,subMenuName,NULL)); } GWindow GMenuCreatePopupMenuWithName(GWindow owner,GEvent *event, char* subMenuName, GMenuItem *mi) { return( _GMenuCreatePopupMenuWithName(owner,event,mi,subMenuName,NULL)); } int GMenuPopupCheckKey(GEvent *event) { if ( most_recent_popup_menu==NULL ) return( false ); return( gmenu_key(most_recent_popup_menu,event) ); } static int GMenuBarCheckHotkey(GWindow top, GGadget *g, GEvent *event) { // TRACE("GMenuBarCheckKey(top) keysym:%d upper:%d lower:%d\n",keysym,toupper(keysym),tolower(keysym)); // see if we should skip processing (e.g. no modifier key pressed) GMenuBar *mb = (GMenuBar *) g; GWindow w = GGadgetGetWindow(g); GGadget* focus = GWindowGetFocusGadgetOfWindow(w); if (GGadgetGetSkipHotkeyProcessing(focus)) return 0; // TRACE("GMenuBarCheckKey(2) keysym:%d upper:%d lower:%d\n",keysym,toupper(keysym),tolower(keysym)); /* then look for hotkeys everywhere */ if( hotkeySystemGetCanUseMacCommand() ) { // If Mac command key was pressed, swap with the control key. if ((event->u.chr.state & (ksm_cmdmacosx|ksm_control)) == ksm_cmdmacosx) { event->u.chr.state ^= (ksm_cmdmacosx|ksm_control); } } // TRACE("about to look for hotkey in new system...state:%d keysym:%d\n", event->u.chr.state, event->u.chr.keysym ); // TRACE(" has ksm_control:%d\n", (event->u.chr.state & ksm_control )); // TRACE(" has ksm_meta:%d\n", (event->u.chr.state & ksm_meta )); // TRACE(" has ksm_shift:%d\n", (event->u.chr.state & ksm_shift )); event->u.chr.state |= ksm_numlock; // TRACE("about2 to look for hotkey in new system...state:%d keysym:%d\n", event->u.chr.state, event->u.chr.keysym ); /** * Mask off the parts we don't explicitly care about */ event->u.chr.state &= ( ksm_control | ksm_meta | ksm_shift | ksm_option ); // TRACE("about3 to look for hotkey in new system...state:%d keysym:%d\n", event->u.chr.state, event->u.chr.keysym ); // TRACE(" has ksm_control:%d\n", (event->u.chr.state & ksm_control )); // TRACE(" has ksm_meta:%d\n", (event->u.chr.state & ksm_meta )); // TRACE(" has ksm_shift:%d\n", (event->u.chr.state & ksm_shift )); if( GGadgetGetSkipUnQualifiedHotkeyProcessing(focus) && !event->u.chr.state ) { TRACE("skipping unqualified hotkey for widget g:%p\n", g); return 0; } struct dlistnodeExternal* node= hotkeyFindAllByEvent( top, event ); struct dlistnode* hklist = (struct dlistnode*)node; for( ; node; node=(struct dlistnodeExternal*)(node->next) ) { Hotkey* hk = (Hotkey*)node->ptr; // TRACE("hotkey found by event! hk:%p\n", hk ); // TRACE("hotkey found by event! action:%s\n", hk->action ); int skipkey = false; if( cv_auto_goto ) { if( !hk->state ) skipkey = true; // TRACE("hotkey state:%d skip:%d\n", hk->state, skipkey ); } if( !skipkey ) { GMenuItem *mi = GMenuSearchAction(mb->g.base,mb->mi,hk->action,event,mb->child==NULL); if ( mi ) { // TRACE("GMenuBarCheckKey(x) have mi... :%p\n", mi ); // TRACE("GMenuBarCheckKey(x) have mitext:%s\n", u_to_c(mi->ti.text) ); if ( mi->ti.checkable && !mi->ti.disabled ) mi->ti.checked = !mi->ti.checked; if ( mi->invoke!=NULL && !mi->ti.disabled ) (mi->invoke)(mb->g.base,mi,NULL); if ( mb->child != NULL ) GMenuDestroy(mb->child); return( true ); } else { // TRACE("hotkey found for event must be a non menu action... action:%s\n", hk->action ); } } // TRACE("END hotkey found by event! hk:%p\n", hk ); } dlist_free_external(&hklist); // TRACE("menubarcheckkey(e1)\n"); return false; } int GMenuBarCheckKey(GWindow top, GGadget *g, GEvent *event) { int i; GMenuBar *mb = (GMenuBar *) g; unichar_t keysym = event->u.chr.keysym; if ( g==NULL || keysym==0 ) return( false ); /* exit if no gadget or key */ if ( event->u.chr.state&ksm_meta && !(event->u.chr.state&(menumask&~(ksm_meta|ksm_shift)))) { /* Only look for mneumonics in the leaf of the displayed menu structure */ if ( mb->child!=NULL ) return( gmenu_key(mb->child,event)); /* this routine will do shortcuts too */ for ( i=0; imtot; ++i ) { if ( !mb->mi[i].ti.disabled && GDrawShortcutKeyMatches(event, mb->mi[i].ti.mnemonic) ) { GMenuBarKeyInvoke(mb,i); return( true ); } } } // See if it matches a hotkey if (GMenuBarCheckHotkey(top, g, event)) { return true; } if ( mb->child ) { GMenu *m; for ( m=mb->child; m->child!=NULL; m = m->child ) { } return( GMenuSpecialKeys(m,event->u.chr.keysym,event)); } // TRACE("menubarcheckkey(e2)\n"); if ( event->u.chr.keysym==GK_Menu && most_recent_popup_menu==NULL ) GMenuCreatePopupMenu(event->w,event, mb->mi); return( false ); } static void GMenuBarDrawDownArrow(GWindow pixmap, GMenuBar *mb, int x) { int pt = GDrawPointsToPixels(pixmap,1); int size = 2*(mb->g.inner.height/3); int ybase = mb->g.inner.y + size + (mb->g.inner.height-size)/2; GPoint p[3]; p[0].x = x+size; p[0].y = ybase; p[1].x = x; p[1].y = ybase - size; p[2].x = x+2*size; p[2].y = ybase - size; GDrawSetLineWidth(pixmap,pt); // If rendering menus in standard (3-dimensional) look, use the shadow colors for fake relief. // Otherwise, use foreground colors. if (menu_3d_look) { GDrawDrawLine(pixmap,p[0].x,p[0].y,p[1].x,p[1].y,mb->g.box->border_darker); GDrawDrawLine(pixmap,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,mb->g.box->border_darker); GDrawDrawLine(pixmap,p[1].x,p[1].y,p[2].x,p[2].y,mb->g.box->border_brightest); GDrawDrawLine(pixmap,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,mb->g.box->border_brightest); GDrawDrawLine(pixmap,p[2].x,p[2].y,p[0].x,p[0].y,mb->g.box->border_darkest); GDrawDrawLine(pixmap,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,mb->g.box->border_darkest); } else { GDrawDrawLine(pixmap,p[0].x,p[0].y,p[1].x,p[1].y,mb->g.box->main_foreground); GDrawDrawLine(pixmap,p[0].x,p[0].y+pt,p[1].x+pt,p[1].y,mb->g.box->main_foreground); GDrawDrawLine(pixmap,p[1].x,p[1].y,p[2].x,p[2].y,mb->g.box->main_foreground); GDrawDrawLine(pixmap,p[1].x+pt,p[1].y,p[2].x-pt,p[2].y,mb->g.box->main_foreground); GDrawDrawLine(pixmap,p[2].x,p[2].y,p[0].x,p[0].y,mb->g.box->main_foreground); GDrawDrawLine(pixmap,p[2].x-pt,p[2].y,p[0].x,p[0].y+pt,mb->g.box->main_foreground); } } static int gmenubar_expose(GWindow pixmap, GGadget *g, GEvent *expose) { GMenuBar *mb = (GMenuBar *) g; GRect r,old1,old2, old3; Color fg = g->state==gs_disabled?g->box->disabled_foreground: g->box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)): g->box->main_foreground; int i; if ( fg==COLOR_DEFAULT ) fg = GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(mb->g.base)); GDrawPushClip(pixmap,&g->r,&old1); GBoxDrawBackground(pixmap,&g->r,g->box, g->state,false); GBoxDrawBorder(pixmap,&g->r,g->box,g->state,false); GDrawPushClip(pixmap,&g->inner,&old2); GDrawSetFont(pixmap,mb->font); r = g->inner; for ( i=0; ilastmi; ++i ) { int cellx = mb->xs[i]+mb->g.inner.x; int cellw = mb->xs[i+1]-mb->xs[i]; /* Draw the active title's highlight ourselves, with horizontal padding * and filling the bar height, rather than relying on GTextInfoDraw's * text-tight fill (which hugs the glyphs and looks cramped on macOS). * We then pass COLOR_DEFAULT below so GTextInfoDraw skips its own fill. */ if ( mb->mi[i].ti.selected && mb->g.box->active_border!=COLOR_DEFAULT ) { GRect hl; int padx = GDrawPointsToPixels(mb->g.base,5); int textw = cellw - GDrawPointsToPixels(mb->g.base,8); /* cell = 8pt gap + text */ hl.x = cellx - padx; hl.width = textw + 2*padx; if ( hl.x < g->inner.x ) { hl.width -= g->inner.x - hl.x; hl.x = g->inner.x; } hl.y = g->inner.y; hl.height = g->inner.height; GDrawFillRect(pixmap,&hl,mb->g.box->active_border); } r.x = cellx; r.width = cellw; GDrawPushClip(pixmap,&r,&old3); GTextInfoDraw(pixmap,r.x,r.y,&mb->mi[i].ti,mb->font, mb->mi[i].ti.disabled?mb->g.box->disabled_foreground:fg, COLOR_DEFAULT,r.y+r.height, mb->ascender, mb->descender); GDrawPopClip(pixmap,&old3); } if ( imtot ) { GMenuBarDrawDownArrow(pixmap,mb,mb->xs[i]+mb->g.inner.x); } GDrawPopClip(pixmap,&old2); GDrawPopClip(pixmap,&old1); return( true ); } static int GMenuBarIndex(GMenuBar *mb, int x ) { int i; if ( x<0 ) return( -1 ); for ( i=0; i< mb->lastmi; ++i ) if ( xg.inner.x+mb->xs[i+1] ) return( i ); if ( mb->lastmi!=mb->mtot ) return( mb->lastmi ); return( -1 ); } static int gmenubar_mouse(GGadget *g, GEvent *event) { GMenuBar *mb = (GMenuBar *) g; int which; if ( mb->child!=NULL && mb->child->hidden ) return( true ); if ( event->type == et_mousedown ) { mb->pressed = true; if ( mb->child!=NULL ) GMenuSetPressed(mb->child,true); which = GMenuBarIndex(mb,event->u.mouse.x); if ( which==mb->entry_with_mouse && mb->child!=NULL ) GMenuDestroy(mb->child); else { mb->initial_press = true; GMenuBarChangeSelection(mb,which,event); } } else if ( event->type == et_mousemove && mb->entry_with_mouse!=-1 ) { /* Once a menu is active on the bar, moving across it switches menus on * hover. Keyed off entry_with_mouse rather than button state or child!=NULL * so that hovering a disabled top-level menu (which opens no pulldown) * doesn't end navigation. */ if ( GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) { int which = GMenuBarIndex(mb,event->u.mouse.x); /* Don't let the pointer passing over a gap between titles (which==-1) * close the open menu -- that causes the menu to flicker shut and * reopen. Only switch to another real title. */ if ( which!=-1 ) GMenuBarChangeSelection(mb,which,event); } else if ( mb->child!=NULL ) { GPoint p; p.x = event->u.mouse.x; p.y = event->u.mouse.y; GDrawTranslateCoordinates(mb->g.base,mb->child->w,&p); if ( p.x>=0 && p.y>=0 && p.xchild->width && p.ychild->height ) { GDrawPointerUngrab(GDrawGetDisplayOfWindow(mb->g.base)); GDrawPointerGrab(mb->child->w); event->u.mouse.x = p.x; event->u.mouse.y = p.y; event->w = mb->child->w; gmenu_mouse(mb->child,event); } } } else if ( event->type == et_mouseup && (!mb->initial_press || !GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y))) { GMenuBarChangeSelection(mb,-1,event); mb->pressed = false; } else if ( event->type == et_mouseup ) { mb->initial_press = mb->pressed = false; if ( mb->child!=NULL ) GMenuSetPressed(mb->child,false); } return( true ); } static void gmenubar_destroy(GGadget *g) { GMenuBar *mb = (GMenuBar *) g; if ( g==NULL ) return; if ( mb->child!=NULL ) { GMenuDestroy(mb->child); GDrawSync(NULL); GDrawProcessPendingEvents(NULL); /* popup's destroy routine must execute before we die */ } GMenuItemArrayFree(mb->mi); free(mb->xs); _ggadget_destroy(g); } static void GMenuBarSetFont(GGadget *g,FontInstance *new) { GMenuBar *b = (GMenuBar *) g; b->font = new; } static FontInstance *GMenuBarGetFont(GGadget *g) { GMenuBar *b = (GMenuBar *) g; return( b->font ); } static void GMenuBarTestSize(GMenuBar *mb) { int arrow_size = mb->g.inner.height; int i; if ( mb->xs[mb->mtot]<=mb->g.inner.width+4 ) { mb->lastmi = mb->mtot; } else { for ( i=mb->mtot-1; i>0 && mb->xs[i]>mb->g.inner.width-arrow_size; --i ); mb->lastmi = i; memset(&mb->fake,0,sizeof(GMenuItem)); mb->fake[0].sub = mb->mi+mb->lastmi; } } static void GMenuBarResize(GGadget *g, int32_t width, int32_t height) { _ggadget_resize(g,width,height); GMenuBarTestSize((GMenuBar *) g); } struct gfuncs gmenubar_funcs = { 0, sizeof(struct gfuncs), gmenubar_expose, gmenubar_mouse, NULL, NULL, NULL, NULL, NULL, _ggadget_redraw, _ggadget_move, GMenuBarResize, _ggadget_setvisible, _ggadget_setenabled, _ggadget_getsize, _ggadget_getinnersize, gmenubar_destroy, NULL, NULL, NULL, NULL, NULL, GMenuBarSetFont, GMenuBarGetFont, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; static void GMenuBarFit(GMenuBar *mb,GGadgetData *gd) { int bp = GBoxBorderWidth(mb->g.base,mb->g.box ); GRect r; if ( gd->pos.x <= 0 ) mb->g.r.x = 0; if ( gd->pos.y <= 0 ) mb->g.r.y = 0; if ( mb->g.r.width == 0 ) { GDrawGetSize(mb->g.base,&r); mb->g.r.width = r.width-mb->g.r.x; } if ( mb->g.r.height == 0 ) { int ld, i; GTextBounds bounds; GDrawWindowFontMetrics(mb->g.base, mb->font, &mb->ascender, &mb->descender, &ld); GDrawSetFont(mb->g.base, mb->font); for ( i=0; imtot; ++i ) { GDrawGetTextBounds(mb->g.base, mb->mi[i].ti.text, -1, &bounds); if ( mb->ascenderascender = bounds.as; if ( mb->descenderdescender = bounds.ds; } mb->g.r.height = mb->ascender+mb->descender+2*bp; } mb->g.inner.x = mb->g.r.x + bp; mb->g.inner.y = mb->g.r.y + bp; mb->g.inner.width = mb->g.r.width - 2*bp; mb->g.inner.height = mb->g.r.height - 2*bp; } static void GMenuBarFindXs(GMenuBar *mb) { int i, wid; GDrawSetFont(mb->g.base,mb->font); wid = GDrawPointsToPixels(mb->g.base,8); mb->xs[0] = GDrawPointsToPixels(mb->g.base,2); for ( i=0; imtot; ++i ) mb->xs[i+1] = mb->xs[i]+wid+GTextInfoGetWidth(mb->g.base,&mb->mi[i].ti,NULL); GMenuBarTestSize(mb); } static void MenuMaskInit(GMenuItem *mi) { int mask = GMenuItemArrayMask(mi); if ( mask_set ) menumask |= mask; else { menumask = mask; mask_set = true; } } GGadget *GMenuBarCreate(struct gwindow *base, GGadgetData *gd,void *data) { GMenuBar *mb = calloc(1,sizeof(GMenuBar)); GMenuInit(); mb->g.funcs = &gmenubar_funcs; _GGadget_Create(&mb->g,base,gd,data,&menubar_box); mb->mi = GMenuItemArrayCopy(gd->u.menu,&mb->mtot); mb->xs = malloc((mb->mtot+1)*sizeof(uint16_t)); mb->entry_with_mouse = -1; mb->font = menubar_font.fi; GMenuBarFit(mb,gd); GMenuBarFindXs(mb); MenuMaskInit(mb->mi); mb->any_unmasked_shortcuts = GMenuItemArrayAnyUnmasked(mb->mi); if ( gd->flags & gg_group_end ) _GGadgetCloseGroup(&mb->g); _GWidget_SetMenuBar(&mb->g); mb->g.takes_input = true; return( &mb->g ); } GGadget *GMenu2BarCreate(struct gwindow *base, GGadgetData *gd,void *data) { GMenuBar *mb = calloc(1,sizeof(GMenuBar)); GMenuInit(); mb->g.funcs = &gmenubar_funcs; _GGadget_Create(&mb->g,base,gd,data,&menubar_box); mb->mi = GMenuItem2ArrayCopy(gd->u.menu2,&mb->mtot); mb->xs = malloc((mb->mtot+1)*sizeof(uint16_t)); mb->entry_with_mouse = -1; mb->font = menubar_font.fi; GMenuBarFit(mb,gd); GMenuBarFindXs(mb); MenuMaskInit(mb->mi); mb->any_unmasked_shortcuts = GMenuItemArrayAnyUnmasked(mb->mi); if ( gd->flags & gg_group_end ) _GGadgetCloseGroup(&mb->g); _GWidget_SetMenuBar(&mb->g); mb->g.takes_input = true; return( &mb->g ); } /* ************************************************************************** */ static GMenuItem *GMenuBarFindMid(GMenuItem *mi, int mid) { int i; GMenuItem *ret; for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) { if ( mi[i].mid == mid ) return( &mi[i]); if ( mi[i].sub!=NULL ) { ret = GMenuBarFindMid( mi[i].sub, mid ); if ( ret!=NULL ) return( ret ); } } return( NULL ); } void GMenuBarSetItemChecked(GGadget *g, int mid, int check) { GMenuBar *mb = (GMenuBar *) g; GMenuItem *item; item = GMenuBarFindMid(mb->mi,mid); if ( item!=NULL ) item->ti.checked = check; } void GMenuBarSetItemEnabled(GGadget *g, int mid, int enabled) { GMenuBar *mb = (GMenuBar *) g; GMenuItem *item; item = GMenuBarFindMid(mb->mi,mid); if ( item!=NULL ) item->ti.disabled = !enabled; } void GMenuBarSetItemName(GGadget *g, int mid, const unichar_t *name) { GMenuBar *mb = (GMenuBar *) g; GMenuItem *item; item = GMenuBarFindMid(mb->mi,mid); if ( item!=NULL ) { free( item->ti.text ); item->ti.text = u_copy(name); } } /* Check to see if event matches the given shortcut, expressed in our standard*/ /* syntax and subject to gettext translation */ int GMenuIsCommand(GEvent *event,char *shortcut) { GMenuItem foo; if ( event->type!=et_char ) return( false ); memset(&foo,0,sizeof(foo)); GMenuItemParseShortCut(&foo,shortcut); return( (menumask&event->u.chr.state)==foo.short_mask && GDrawShortcutKeyMatches(event, foo.shortcut) ); } int GMenuMask(void) { return( menumask ); } int GMenuAnyUnmaskedShortcuts(GGadget *mb1, GGadget *mb2) { if ( most_recent_popup_menu!=NULL && most_recent_popup_menu->any_unmasked_shortcuts ) return( true ); if ( mb1!=NULL && ((GMenuBar *) mb1)->any_unmasked_shortcuts ) return( true ); if ( mb2!=NULL && ((GMenuBar *) mb2)->any_unmasked_shortcuts ) return( true ); return( false ); }