1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
18 #pragma implementation "menu.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
26 #include "wx/menuitem.h"
31 #include "wx/settings.h"
34 #pragma message disable nosimpint
37 #include <Xm/LabelG.h>
38 #include <Xm/CascadeBG.h>
39 #include <Xm/CascadeB.h>
40 #include <Xm/SeparatoG.h>
41 #include <Xm/PushBG.h>
42 #include <Xm/ToggleB.h>
43 #include <Xm/ToggleBG.h>
44 #include <Xm/RowColumn.h>
46 #pragma message enable nosimpint
49 #include "wx/motif/private.h"
51 // other standard headers
54 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
55 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // Construct a menu with optional title (then use append)
68 // Motif-specific members
70 m_menuWidget
= (WXWidget
) NULL
;
71 m_popupShell
= (WXWidget
) NULL
;
72 m_buttonWidget
= (WXWidget
) NULL
;
74 m_topLevelMenu
= (wxMenu
*) NULL
;
75 m_ownedByMenuBar
= FALSE
;
79 Append(wxID_SEPARATOR
, m_title
) ;
83 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU
);
84 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT
);
85 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
88 // The wxWindow destructor will take care of deleting the submenus.
99 // Not sure if this is right
100 if (m_menuParent
&& m_menuBar
)
112 // function appends a new item or submenu to the menu
113 bool wxMenu::DoAppend(wxMenuItem
*pItem
)
117 // this is a dynamic Append
118 pItem
->CreateItem(m_menuWidget
, m_menuBar
, m_topLevelMenu
);
121 if ( pItem
->IsSubMenu() )
123 pItem
->GetSubMenu()->m_topLevelMenu
= m_topLevelMenu
;
126 return wxMenuBase::DoAppend(pItem
);
129 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
131 item
->DestroyItem(TRUE
);
133 return wxMenuBase::DoRemove(item
);
136 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
138 if ( !wxMenuBase::DoInsert(pos
, item
) )
141 wxFAIL_MSG(wxT("not implemented"));
146 void wxMenu::SetTitle(const wxString
& label
)
150 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
154 wxMenuItem
*item
= node
->GetData ();
155 Widget widget
= (Widget
) item
->GetButtonWidget();
159 wxXmString
title_str(label
);
160 XtVaSetValues(widget
,
161 XmNlabelString
, title_str(),
165 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
167 bool processed
= FALSE
;
169 #if wxUSE_MENU_CALLBACK
173 (void) (*(m_callback
)) (*this, event
);
176 #endif // wxUSE_MENU_CALLBACK
178 // Try the menu's event handler
179 if ( !processed
&& GetEventHandler())
181 processed
= GetEventHandler()->ProcessEvent(event
);
183 // Try the window the menu was popped up from (and up
184 // through the hierarchy)
185 if ( !processed
&& GetInvokingWindow())
186 processed
= GetInvokingWindow()->ProcessEvent(event
);
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 void wxMenuBar::Init()
197 m_eventHandler
= this;
198 m_menuBarFrame
= NULL
;
199 m_mainWidget
= (WXWidget
) NULL
;
200 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU
);
201 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT
);
202 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
205 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[])
209 for ( int i
= 0; i
< n
; i
++ )
211 m_menus
.Append(menus
[i
]);
212 m_titles
.Add(titles
[i
]);
216 wxMenuBar::~wxMenuBar()
218 // nothing to do: wxMenuBarBase will delete the menus
221 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
223 // wxFAIL_MSG("TODO");
224 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
227 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
229 wxMenu
*menu
= GetMenu(pos
);
233 Widget w
= (Widget
)menu
->GetButtonWidget();
236 wxXmString
label_str(label
);
239 XmNlabelString
, label_str(),
244 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
248 wxMenu
*menu
= GetMenu(pos
);
251 Widget w
= (Widget
)menu
->GetButtonWidget();
256 XmNlabelString
, &text
,
260 if ( XmStringGetLtoR(text
, XmSTRING_DEFAULT_CHARSET
, &s
) )
272 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
274 wxCHECK_MSG( menu
, FALSE
, wxT("invalid menu") );
275 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), FALSE
,
276 wxT("menu already appended") );
278 if ( m_menuBarFrame
)
280 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
, title
, TRUE
);
281 wxCHECK_MSG( w
, FALSE
, wxT("failed to create menu") );
282 menu
->SetButtonWidget(w
);
285 menu
->SetMenuBar(this);
289 return wxMenuBarBase::Append(menu
, title
);
292 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
294 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
297 wxFAIL_MSG(wxT("TODO"));
302 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
304 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
307 wxFAIL_MSG(wxT("TODO"));
312 wxMenu
*wxMenuBar::Remove(size_t pos
)
314 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
318 if ( m_menuBarFrame
)
319 menu
->DestroyMenu(TRUE
);
321 menu
->SetMenuBar(NULL
);
323 m_titles
.Remove(pos
);
328 // Find the menu menuString, item itemString, and return the item id.
329 // Returns -1 if none found.
330 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
334 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
336 size_t menuCount
= GetMenuCount();
337 for (size_t i
= 0; i
< menuCount
; i
++)
339 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
340 if (strcmp (buf1
, buf2
) == 0)
341 return m_menus
[i
]->FindItem (itemString
);
346 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
351 wxMenuItem
*item
= NULL
;
352 size_t menuCount
= GetMenuCount();
353 for (size_t i
= 0; i
< menuCount
; i
++)
354 if ((item
= m_menus
[i
]->FindItem(id
, itemMenu
)))
360 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
364 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
366 if (!XtIsManaged((Widget) m_mainWidget))
367 XtManageChild((Widget) m_mainWidget);
369 XtMapWidget((Widget
) m_mainWidget
);
373 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(), "MenuBar", NULL
, 0);
374 m_mainWidget
= (WXWidget
) menuBarW
;
376 size_t menuCount
= GetMenuCount();
377 for (size_t i
= 0; i
< menuCount
; i
++)
379 wxMenu
*menu
= GetMenu(i
);
380 wxString
title(m_titles
[i
]);
381 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, title
, TRUE
));
383 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
384 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
386 // tear off menu support
387 #if (XmVersion >= 1002)
388 if ( menu
->IsTearOff() )
390 XtVaSetValues(GetWidget(menu
),
391 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
393 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
394 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
395 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, TRUE
);
400 SetBackgroundColour(m_backgroundColour
);
401 SetForegroundColour(m_foregroundColour
);
404 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
405 XtRealizeWidget ((Widget
) menuBarW
);
406 XtManageChild ((Widget
) menuBarW
);
407 SetMenuBarFrame(parent
);
412 // Destroy menubar, but keep data structures intact so we can recreate it.
413 bool wxMenuBar::DestroyMenuBar()
417 SetMenuBarFrame((wxFrame
*) NULL
);
421 XtUnmanageChild ((Widget
) m_mainWidget
);
422 XtUnrealizeWidget ((Widget
) m_mainWidget
);
424 size_t menuCount
= GetMenuCount();
425 for (size_t i
= 0; i
< menuCount
; i
++)
427 wxMenu
*menu
= GetMenu(i
);
428 menu
->DestroyMenu(TRUE
);
431 XtDestroyWidget((Widget
) m_mainWidget
);
432 m_mainWidget
= (WXWidget
) 0;
434 SetMenuBarFrame((wxFrame
*) NULL
);
440 static XtWorkProcId WorkProcMenuId
;
442 /* Since PopupMenu under Motif stills grab right mouse button events
443 * after it was closed, we need to delete the associated widgets to
444 * allow next PopUpMenu to appear...
447 int PostDeletionOfMenu( XtPointer
* clientData
)
449 XtRemoveWorkProc(WorkProcMenuId
);
450 wxMenu
*menu
= (wxMenu
*)clientData
;
452 if (menu
->GetMainWidget())
454 wxMenu
*menuParent
= menu
->GetParent();
457 wxMenuItemList::Node
*node
= menuParent
->GetMenuItems().GetFirst();
460 if ( node
->GetData()->GetSubMenu() == menu
)
462 menuParent
->GetMenuItems().DeleteNode(node
);
467 node
= node
->GetNext();
471 menu
->DestroyMenu(TRUE
);
474 // Mark as no longer popped up
481 wxMenuPopdownCallback(Widget
WXUNUSED(w
), XtPointer clientData
,
482 XtPointer
WXUNUSED(ptr
))
484 wxMenu
*menu
= (wxMenu
*)clientData
;
486 // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr>
487 /* Since Callbacks of MenuItems are not yet processed, we put a
488 * background job which will be done when system will be idle.
489 * What awful hack!! :(
492 WorkProcMenuId
= XtAppAddWorkProc(
493 (XtAppContext
) wxTheApp
->GetAppContext(),
494 (XtWorkProc
) PostDeletionOfMenu
,
496 // Apparently not found in Motif headers
497 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
501 * Create a popup or pulldown menu.
502 * Submenus of a popup will be pulldown.
506 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
, WXWidget parent
, wxMenu
* topMenu
, const wxString
& title
, bool pullDown
)
508 Widget menu
= (Widget
) 0;
509 Widget buttonWidget
= (Widget
) 0;
511 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
512 XtSetArg (args
[1], XmNpacking
, XmPACK_COLUMN
);
516 menu
= XmCreatePopupMenu ((Widget
) parent
, "popup", args
, 2);
519 (XtCallbackProc
)wxMenuPopdownCallback
,
524 char mnem
= wxFindMnemonic (title
);
525 wxStripMenuCodes ((char*) (const char*) title
, wxBuffer
);
527 menu
= XmCreatePulldownMenu ((Widget
) parent
, "pulldown", args
, 2);
529 wxString
title2(wxStripMenuCodes(title
));
530 wxXmString
label_str(title2
);
531 buttonWidget
= XtVaCreateManagedWidget(title2
,
533 xmCascadeButtonGadgetClass
, (Widget
) parent
,
535 xmCascadeButtonWidgetClass
, (Widget
) parent
,
537 XmNlabelString
, label_str(),
542 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
545 m_menuWidget
= (WXWidget
) menu
;
548 m_topLevelMenu
= topMenu
;
550 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
552 node
= node
->GetNext() )
554 wxMenuItem
*item
= node
->GetData();
556 item
->CreateItem(menu
, menuBar
, topMenu
);
559 SetBackgroundColour(m_backgroundColour
);
560 SetForegroundColour(m_foregroundColour
);
566 // Destroys the Motif implementation of the menu,
567 // but maintains the wxWindows data structures so we can
568 // do a CreateMenu again.
569 void wxMenu::DestroyMenu (bool full
)
571 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
573 node
= node
->GetNext() )
575 wxMenuItem
*item
= node
->GetData();
576 item
->SetMenuBar((wxMenuBar
*) NULL
);
578 item
->DestroyItem(full
);
585 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
586 XtDestroyWidget ((Widget
) m_buttonWidget
);
587 m_buttonWidget
= (WXWidget
) 0;
590 if (m_menuWidget
&& full
)
592 XtDestroyWidget((Widget
) m_menuWidget
);
593 m_menuWidget
= (WXWidget
) NULL
;
597 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
602 *it
= (wxMenuItem
*) NULL
;
603 return m_buttonWidget
;
606 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
608 node
= node
->GetNext() )
610 wxMenuItem
*item
= node
->GetData ();
611 if (item
->GetId() == id
)
615 return item
->GetButtonWidget();
618 if (item
->GetSubMenu())
620 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
629 *it
= (wxMenuItem
*) NULL
;
630 return (WXWidget
) NULL
;
633 void wxMenu::SetBackgroundColour(const wxColour
& col
)
635 m_backgroundColour
= col
;
637 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
639 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, TRUE
);
641 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
643 node
= node
->GetNext() )
645 wxMenuItem
* item
= node
->GetData();
646 if (item
->GetButtonWidget())
648 // This crashes because it uses gadgets
649 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, TRUE);
651 if (item
->GetSubMenu())
652 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
656 void wxMenu::SetForegroundColour(const wxColour
& col
)
658 m_foregroundColour
= col
;
660 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
662 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
664 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
666 node
= node
->GetNext() )
668 wxMenuItem
* item
= node
->GetData();
669 if (item
->GetButtonWidget())
671 // This crashes because it uses gadgets
672 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
674 if (item
->GetSubMenu())
675 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
679 void wxMenu::ChangeFont(bool keepOriginalSize
)
681 // lesstif 0.87 hangs when setting XmNfontList
682 #ifndef LESSTIF_VERSION
683 if (!m_font
.Ok() || !m_menuWidget
)
686 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) m_menuWidget
));
688 XtVaSetValues ((Widget
) m_menuWidget
,
689 XmNfontList
, fontList
,
693 XtVaSetValues ((Widget
) m_buttonWidget
,
694 XmNfontList
, fontList
,
698 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
700 node
= node
->GetNext() )
702 wxMenuItem
* item
= node
->GetData();
703 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
705 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
706 XmNfontList
, fontList
,
709 if (item
->GetSubMenu())
710 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
715 void wxMenu::SetFont(const wxFont
& font
)
721 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
723 m_backgroundColour
= col
;
725 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
727 size_t menuCount
= GetMenuCount();
728 for (size_t i
= 0; i
< menuCount
; i
++)
729 m_menus
[i
]->SetBackgroundColour((wxColour
&) col
);
734 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
736 m_foregroundColour
= col
;
738 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
740 size_t menuCount
= GetMenuCount();
741 for (size_t i
= 0; i
< menuCount
; i
++)
742 m_menus
[i
]->SetForegroundColour((wxColour
&) col
);
747 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
749 // Nothing to do for menubar, fonts are kept in wxMenus
752 bool wxMenuBar::SetFont(const wxFont
& font
)
757 size_t menuCount
= GetMenuCount();
758 for (size_t i
= 0; i
< menuCount
; i
++)
759 m_menus
[i
]->SetFont(font
);