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 #include <Xm/LabelG.h>
35 #include <Xm/CascadeBG.h>
36 #include <Xm/CascadeB.h>
37 #include <Xm/SeparatoG.h>
38 #include <Xm/PushBG.h>
39 #include <Xm/ToggleB.h>
40 #include <Xm/ToggleBG.h>
41 #include <Xm/RowColumn.h>
43 #include "wx/motif/private.h"
45 // other standard headers
48 #if !USE_SHARED_LIBRARY
49 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
50 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
53 // ============================================================================
55 // ============================================================================
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // Construct a menu with optional title (then use append)
64 // Motif-specific members
66 m_menuWidget
= (WXWidget
) NULL
;
67 m_popupShell
= (WXWidget
) NULL
;
68 m_buttonWidget
= (WXWidget
) NULL
;
70 m_topLevelMenu
= (wxMenu
*) NULL
;
71 m_ownedByMenuBar
= FALSE
;
75 Append(wxID_SEPARATOR
, m_title
) ;
79 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU
);
80 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT
);
81 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
84 // The wxWindow destructor will take care of deleting the submenus.
95 // Not sure if this is right
96 if (m_menuParent
&& m_menuBar
)
108 // function appends a new item or submenu to the menu
109 bool wxMenu::DoAppend(wxMenuItem
*pItem
)
113 // this is a dynamic Append
114 pItem
->CreateItem(m_menuWidget
, m_menuBar
, m_topLevelMenu
);
117 if ( pItem
->IsSubMenu() )
119 pItem
->GetSubMenu()->m_topLevelMenu
= m_topLevelMenu
;
122 return wxMenuBase::DoAppend(pItem
);
125 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
127 item
->DestroyItem(TRUE
);
129 return wxMenuBase::DoRemove(item
);
132 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
134 if ( !wxMenuBase::DoInsert(pos
, item
) )
137 wxFAIL_MSG(wxT("not implemented"));
142 void wxMenu::SetTitle(const wxString
& label
)
146 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
150 wxMenuItem
*item
= node
->GetData ();
151 Widget widget
= (Widget
) item
->GetButtonWidget();
155 wxXmString
title_str(label
);
156 XtVaSetValues(widget
,
157 XmNlabelString
, title_str(),
161 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
163 bool processed
= FALSE
;
165 #if WXWIN_COMPATIBILITY
169 (void) (*(m_callback
)) (*this, event
);
172 #endif // WXWIN_COMPATIBILITY
174 // Try the menu's event handler
175 if ( !processed
&& GetEventHandler())
177 processed
= GetEventHandler()->ProcessEvent(event
);
179 // Try the window the menu was popped up from (and up
180 // through the hierarchy)
181 if ( !processed
&& GetInvokingWindow())
182 processed
= GetInvokingWindow()->ProcessEvent(event
);
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 void wxMenuBar::Init()
193 m_eventHandler
= this;
194 m_menuBarFrame
= NULL
;
195 m_mainWidget
= (WXWidget
) NULL
;
196 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU
);
197 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT
);
198 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
201 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[])
205 for ( int i
= 0; i
< n
; i
++ )
207 m_menus
.Append(menus
[i
]);
208 m_titles
.Add(titles
[i
]);
212 wxMenuBar::~wxMenuBar()
214 // nothing to do: wxMenuBarBase will delete the menus
217 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
219 // wxFAIL_MSG("TODO");
220 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
223 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
225 wxMenu
*menu
= GetMenu(pos
);
229 Widget w
= (Widget
)menu
->GetButtonWidget();
232 wxXmString
label_str(label
);
235 XmNlabelString
, label_str(),
240 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
244 wxMenu
*menu
= GetMenu(pos
);
247 Widget w
= (Widget
)menu
->GetButtonWidget();
252 XmNlabelString
, &text
,
256 if ( XmStringGetLtoR(text
, XmSTRING_DEFAULT_CHARSET
, &s
) )
268 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
270 wxCHECK_MSG( menu
, FALSE
, wxT("invalid menu") );
271 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), FALSE
,
272 wxT("menu already appended") );
274 if ( m_menuBarFrame
)
276 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
, title
, TRUE
);
277 wxCHECK_MSG( w
, FALSE
, wxT("failed to create menu") );
278 menu
->SetButtonWidget(w
);
281 menu
->SetMenuBar(this);
285 return wxMenuBarBase::Append(menu
, title
);
288 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
290 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
293 wxFAIL_MSG(wxT("TODO"));
298 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
300 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
303 wxFAIL_MSG(wxT("TODO"));
308 wxMenu
*wxMenuBar::Remove(size_t pos
)
310 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
314 if ( m_menuBarFrame
)
315 menu
->DestroyMenu(TRUE
);
317 menu
->SetMenuBar(NULL
);
319 m_titles
.Remove(pos
);
324 // Find the menu menuString, item itemString, and return the item id.
325 // Returns -1 if none found.
326 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
330 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
332 size_t menuCount
= GetMenuCount();
333 for (size_t i
= 0; i
< menuCount
; i
++)
335 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
336 if (strcmp (buf1
, buf2
) == 0)
337 return m_menus
[i
]->FindItem (itemString
);
342 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
347 wxMenuItem
*item
= NULL
;
348 size_t menuCount
= GetMenuCount();
349 for (size_t i
= 0; i
< menuCount
; i
++)
350 if ((item
= m_menus
[i
]->FindItem(id
, itemMenu
)))
356 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
360 XtVaSetValues((Widget
) parent
->GetMainWindowWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
362 if (!XtIsManaged((Widget) m_mainWidget))
363 XtManageChild((Widget) m_mainWidget);
365 XtMapWidget((Widget
) m_mainWidget
);
369 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWindowWidget(), "MenuBar", NULL
, 0);
370 m_mainWidget
= (WXWidget
) menuBarW
;
372 size_t menuCount
= GetMenuCount();
373 for (size_t i
= 0; i
< menuCount
; i
++)
375 wxMenu
*menu
= GetMenu(i
);
376 wxString
title(m_titles
[i
]);
377 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, title
, TRUE
));
379 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
380 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
382 // tear off menu support
383 #if (XmVersion >= 1002)
384 if ( menu
->IsTearOff() )
386 XtVaSetValues(GetWidget(menu
),
387 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
389 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
390 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
391 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, TRUE
);
396 SetBackgroundColour(m_backgroundColour
);
397 SetForegroundColour(m_foregroundColour
);
400 XtVaSetValues((Widget
) parent
->GetMainWindowWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
401 XtRealizeWidget ((Widget
) menuBarW
);
402 XtManageChild ((Widget
) menuBarW
);
403 SetMenuBarFrame(parent
);
408 // Destroy menubar, but keep data structures intact so we can recreate it.
409 bool wxMenuBar::DestroyMenuBar()
413 SetMenuBarFrame((wxFrame
*) NULL
);
417 XtUnmanageChild ((Widget
) m_mainWidget
);
418 XtUnrealizeWidget ((Widget
) m_mainWidget
);
420 size_t menuCount
= GetMenuCount();
421 for (size_t i
= 0; i
< menuCount
; i
++)
423 wxMenu
*menu
= GetMenu(i
);
424 menu
->DestroyMenu(TRUE
);
427 XtDestroyWidget((Widget
) m_mainWidget
);
428 m_mainWidget
= (WXWidget
) 0;
430 SetMenuBarFrame((wxFrame
*) NULL
);
436 static XtWorkProcId WorkProcMenuId
;
438 /* Since PopupMenu under Motif stills grab right mouse button events
439 * after it was closed, we need to delete the associated widgets to
440 * allow next PopUpMenu to appear...
443 int PostDeletionOfMenu( XtPointer
* clientData
)
445 XtRemoveWorkProc(WorkProcMenuId
);
446 wxMenu
*menu
= (wxMenu
*)clientData
;
448 if (menu
->GetMainWidget())
450 wxMenu
*menuParent
= menu
->GetParent();
453 wxMenuItemList::Node
*node
= menuParent
->GetMenuItems().GetFirst();
456 if ( node
->GetData()->GetSubMenu() == menu
)
458 menuParent
->GetMenuItems().DeleteNode(node
);
463 node
= node
->GetNext();
467 menu
->DestroyMenu(TRUE
);
470 // Mark as no longer popped up
477 wxMenuPopdownCallback(Widget
WXUNUSED(w
), XtPointer clientData
,
478 XtPointer
WXUNUSED(ptr
))
480 wxMenu
*menu
= (wxMenu
*)clientData
;
482 // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr>
483 /* Since Callbacks of MenuItems are not yet processed, we put a
484 * background job which will be done when system will be idle.
485 * What awful hack!! :(
488 WorkProcMenuId
= XtAppAddWorkProc(
489 (XtAppContext
) wxTheApp
->GetAppContext(),
490 (XtWorkProc
) PostDeletionOfMenu
,
492 // Apparently not found in Motif headers
493 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
497 * Create a popup or pulldown menu.
498 * Submenus of a popup will be pulldown.
502 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
, WXWidget parent
, wxMenu
* topMenu
, const wxString
& title
, bool pullDown
)
504 Widget menu
= (Widget
) 0;
505 Widget buttonWidget
= (Widget
) 0;
507 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
508 XtSetArg (args
[1], XmNpacking
, XmPACK_COLUMN
);
512 menu
= XmCreatePopupMenu ((Widget
) parent
, "popup", args
, 2);
515 (XtCallbackProc
)wxMenuPopdownCallback
,
520 char mnem
= wxFindMnemonic (title
);
521 wxStripMenuCodes ((char*) (const char*) title
, wxBuffer
);
523 menu
= XmCreatePulldownMenu ((Widget
) parent
, "pulldown", args
, 2);
525 wxString
title2(wxStripMenuCodes(title
));
526 wxXmString
label_str(title2
);
527 buttonWidget
= XtVaCreateManagedWidget(title2
,
529 xmCascadeButtonGadgetClass
, (Widget
) parent
,
531 xmCascadeButtonWidgetClass
, (Widget
) parent
,
533 XmNlabelString
, label_str(),
538 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
541 m_menuWidget
= (WXWidget
) menu
;
544 m_topLevelMenu
= topMenu
;
546 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
548 node
= node
->GetNext() )
550 wxMenuItem
*item
= node
->GetData();
552 item
->CreateItem(menu
, menuBar
, topMenu
);
555 SetBackgroundColour(m_backgroundColour
);
556 SetForegroundColour(m_foregroundColour
);
562 // Destroys the Motif implementation of the menu,
563 // but maintains the wxWindows data structures so we can
564 // do a CreateMenu again.
565 void wxMenu::DestroyMenu (bool full
)
567 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
569 node
= node
->GetNext() )
571 wxMenuItem
*item
= node
->GetData();
572 item
->SetMenuBar((wxMenuBar
*) NULL
);
574 item
->DestroyItem(full
);
581 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
582 XtDestroyWidget ((Widget
) m_buttonWidget
);
583 m_buttonWidget
= (WXWidget
) 0;
586 if (m_menuWidget
&& full
)
588 XtDestroyWidget((Widget
) m_menuWidget
);
589 m_menuWidget
= (WXWidget
) NULL
;
593 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
598 *it
= (wxMenuItem
*) NULL
;
599 return m_buttonWidget
;
602 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
604 node
= node
->GetNext() )
606 wxMenuItem
*item
= node
->GetData ();
607 if (item
->GetId() == id
)
611 return item
->GetButtonWidget();
614 if (item
->GetSubMenu())
616 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
625 *it
= (wxMenuItem
*) NULL
;
626 return (WXWidget
) NULL
;
629 void wxMenu::SetBackgroundColour(const wxColour
& col
)
631 m_backgroundColour
= col
;
633 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
635 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, TRUE
);
637 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
639 node
= node
->GetNext() )
641 wxMenuItem
* item
= node
->GetData();
642 if (item
->GetButtonWidget())
644 // This crashes because it uses gadgets
645 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, TRUE);
647 if (item
->GetSubMenu())
648 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
652 void wxMenu::SetForegroundColour(const wxColour
& col
)
654 m_foregroundColour
= col
;
656 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
658 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
660 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
662 node
= node
->GetNext() )
664 wxMenuItem
* item
= node
->GetData();
665 if (item
->GetButtonWidget())
667 // This crashes because it uses gadgets
668 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
670 if (item
->GetSubMenu())
671 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
675 void wxMenu::ChangeFont(bool keepOriginalSize
)
677 // lesstif 0.87 hangs when setting XmNfontList
678 #ifndef LESSTIF_VERSION
679 if (!m_font
.Ok() || !m_menuWidget
)
682 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) m_menuWidget
));
684 XtVaSetValues ((Widget
) m_menuWidget
,
685 XmNfontList
, fontList
,
689 XtVaSetValues ((Widget
) m_buttonWidget
,
690 XmNfontList
, fontList
,
694 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
696 node
= node
->GetNext() )
698 wxMenuItem
* item
= node
->GetData();
699 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
701 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
702 XmNfontList
, fontList
,
705 if (item
->GetSubMenu())
706 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
711 void wxMenu::SetFont(const wxFont
& font
)
717 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
719 m_backgroundColour
= col
;
721 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
723 size_t menuCount
= GetMenuCount();
724 for (size_t i
= 0; i
< menuCount
; i
++)
725 m_menus
[i
]->SetBackgroundColour((wxColour
&) col
);
730 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
732 m_foregroundColour
= col
;
734 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
736 size_t menuCount
= GetMenuCount();
737 for (size_t i
= 0; i
< menuCount
; i
++)
738 m_menus
[i
]->SetForegroundColour((wxColour
&) col
);
743 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
745 // Nothing to do for menubar, fonts are kept in wxMenus
748 bool wxMenuBar::SetFont(const wxFont
& font
)
753 size_t menuCount
= GetMenuCount();
754 for (size_t i
= 0; i
< menuCount
; i
++)
755 m_menus
[i
]->SetFont(font
);