1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
18 #pragma implementation "menu.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
31 #include "wx/menuitem.h"
36 #include "wx/settings.h"
39 #pragma message disable nosimpint
40 #define XtDisplay XTDISPLAY
41 #define XtWindow XTWINDOW
44 #include <Xm/LabelG.h>
45 #include <Xm/CascadeBG.h>
46 #include <Xm/CascadeB.h>
47 #include <Xm/SeparatoG.h>
48 #include <Xm/PushBG.h>
49 #include <Xm/ToggleB.h>
50 #include <Xm/ToggleBG.h>
51 #include <Xm/RowColumn.h>
53 #pragma message enable nosimpint
56 #include "wx/motif/private.h"
58 // other standard headers
61 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
62 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // Construct a menu with optional title (then use append)
75 // Motif-specific members
77 m_menuWidget
= (WXWidget
) NULL
;
78 m_popupShell
= (WXWidget
) NULL
;
79 m_buttonWidget
= (WXWidget
) NULL
;
81 m_topLevelMenu
= (wxMenu
*) NULL
;
82 m_ownedByMenuBar
= false;
84 if ( !m_title
.empty() )
90 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
91 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
92 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
95 // The wxWindow destructor will take care of deleting the submenus.
106 // Not sure if this is right
107 if (m_menuParent
&& m_menuBar
)
119 // function appends a new item or submenu to the menu
120 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*pItem
)
124 // this is a dynamic Append
125 pItem
->CreateItem(m_menuWidget
, GetMenuBar(), m_topLevelMenu
);
128 if ( pItem
->IsSubMenu() )
130 pItem
->GetSubMenu()->m_topLevelMenu
= m_topLevelMenu
;
133 return wxMenuBase::DoAppend(pItem
);
136 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
138 item
->DestroyItem(true);
140 return wxMenuBase::DoRemove(item
);
143 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
145 if ( wxMenuBase::DoInsert(pos
, item
) )
148 wxFAIL_MSG(wxT("DoInsert not implemented; or error in wxMenuBase::DoInsert"));
153 void wxMenu::SetTitle(const wxString
& label
)
157 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
161 wxMenuItem
*item
= node
->GetData ();
162 Widget widget
= (Widget
) item
->GetButtonWidget();
166 wxXmString
title_str(label
);
167 XtVaSetValues(widget
,
168 XmNlabelString
, title_str(),
172 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
174 bool processed
= false;
176 #if wxUSE_MENU_CALLBACK
180 (void) (*(m_callback
)) (*this, event
);
183 #endif // wxUSE_MENU_CALLBACK
185 // Try the menu's event handler
186 if ( !processed
&& GetEventHandler())
188 processed
= GetEventHandler()->ProcessEvent(event
);
190 // Try the window the menu was popped up from (and up
191 // through the hierarchy)
192 if ( !processed
&& GetInvokingWindow())
193 processed
= GetInvokingWindow()->ProcessEvent(event
);
198 // ----------------------------------------------------------------------------
200 // ----------------------------------------------------------------------------
202 void wxMenuBar::Init()
204 m_eventHandler
= this;
205 m_menuBarFrame
= NULL
;
206 m_mainWidget
= (WXWidget
) NULL
;
207 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
208 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
209 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
212 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxArrayString
& titles
, long WXUNUSED(style
))
214 wxASSERT( n
== titles
.GetCount() );
219 for ( size_t i
= 0; i
< n
; i
++ )
220 m_menus
.Append(menus
[i
]);
223 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
227 for ( size_t i
= 0; i
< n
; i
++ )
229 m_menus
.Append(menus
[i
]);
230 m_titles
.Add(titles
[i
]);
234 wxMenuBar::~wxMenuBar()
236 // nothing to do: wxMenuBarBase will delete the menus
239 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
241 // wxFAIL_MSG("TODO");
242 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
245 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
247 wxMenu
*menu
= GetMenu(pos
);
251 Widget w
= (Widget
)menu
->GetButtonWidget();
254 wxXmString
label_str(label
);
257 XmNlabelString
, label_str(),
262 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
264 wxMenu
*menu
= GetMenu(pos
);
267 Widget w
= (Widget
)menu
->GetButtonWidget();
272 XmNlabelString
, &text
,
275 return wxXmStringToString( text
);
279 return wxEmptyString
;
282 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
284 wxCHECK_MSG( menu
, false, wxT("invalid menu") );
285 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), false,
286 wxT("menu already appended") );
288 if ( m_menuBarFrame
)
290 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
, title
, true);
291 wxCHECK_MSG( w
, false, wxT("failed to create menu") );
292 menu
->SetButtonWidget(w
);
297 return wxMenuBarBase::Append(menu
, title
);
300 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
302 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
305 wxFAIL_MSG(wxT("TODO"));
310 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
312 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
315 wxFAIL_MSG(wxT("TODO"));
320 wxMenu
*wxMenuBar::Remove(size_t pos
)
322 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
326 if ( m_menuBarFrame
)
327 menu
->DestroyMenu(true);
329 menu
->SetMenuBar(NULL
);
331 m_titles
.RemoveAt(pos
);
336 // Find the menu menuString, item itemString, and return the item id.
337 // Returns -1 if none found.
338 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
342 wxStripMenuCodes (wxConstCast(menuString
.c_str(), char), buf1
);
344 size_t menuCount
= GetMenuCount();
345 for (size_t i
= 0; i
< menuCount
; i
++)
347 wxStripMenuCodes (wxConstCast(m_titles
[i
].c_str(), char), buf2
);
348 if (strcmp (buf1
, buf2
) == 0)
349 return m_menus
.Item(i
)->GetData()->FindItem (itemString
);
354 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
359 wxMenuItem
*item
= NULL
;
360 size_t menuCount
= GetMenuCount();
361 for (size_t i
= 0; i
< menuCount
; i
++)
362 if ((item
= m_menus
.Item(i
)->GetData()->FindItem(id
, itemMenu
)))
368 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
372 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
374 if (!XtIsManaged((Widget) m_mainWidget))
375 XtManageChild((Widget) m_mainWidget);
377 XtMapWidget((Widget
) m_mainWidget
);
381 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(), "MenuBar", NULL
, 0);
382 m_mainWidget
= (WXWidget
) menuBarW
;
384 size_t menuCount
= GetMenuCount();
385 for (size_t i
= 0; i
< menuCount
; i
++)
387 wxMenu
*menu
= GetMenu(i
);
388 wxString
title(m_titles
[i
]);
389 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, title
, true));
391 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
392 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
394 // tear off menu support
395 #if (XmVersion >= 1002)
396 if ( menu
->IsTearOff() )
398 XtVaSetValues(GetWidget(menu
),
399 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
401 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
402 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
403 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, true);
408 SetBackgroundColour(m_backgroundColour
);
409 SetForegroundColour(m_foregroundColour
);
412 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
413 XtRealizeWidget ((Widget
) menuBarW
);
414 XtManageChild ((Widget
) menuBarW
);
415 SetMenuBarFrame(parent
);
420 // Destroy menubar, but keep data structures intact so we can recreate it.
421 bool wxMenuBar::DestroyMenuBar()
425 SetMenuBarFrame((wxFrame
*) NULL
);
429 XtUnmanageChild ((Widget
) m_mainWidget
);
430 XtUnrealizeWidget ((Widget
) m_mainWidget
);
432 size_t menuCount
= GetMenuCount();
433 for (size_t i
= 0; i
< menuCount
; i
++)
435 wxMenu
*menu
= GetMenu(i
);
436 menu
->DestroyMenu(true);
439 XtDestroyWidget((Widget
) m_mainWidget
);
440 m_mainWidget
= (WXWidget
) 0;
442 SetMenuBarFrame((wxFrame
*) NULL
);
447 // Since PopupMenu under Motif stills grab right mouse button events
448 // after it was closed, we need to delete the associated widgets to
449 // allow next PopUpMenu to appear...
450 void wxMenu::DestroyWidgetAndDetach()
454 wxMenu
*menuParent
= GetParent();
457 wxMenuItemList::compatibility_iterator node
= menuParent
->GetMenuItems().GetFirst();
460 if ( node
->GetData()->GetSubMenu() == this )
462 delete node
->GetData();
463 menuParent
->GetMenuItems().Erase(node
);
468 node
= node
->GetNext();
475 // Mark as no longer popped up
480 * Create a popup or pulldown menu.
481 * Submenus of a popup will be pulldown.
485 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
, WXWidget parent
, wxMenu
* topMenu
, const wxString
& title
, bool pullDown
)
487 Widget menu
= (Widget
) 0;
488 Widget buttonWidget
= (Widget
) 0;
490 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
491 XtSetArg (args
[1], XmNpacking
, (m_numColumns
> 1) ? XmPACK_COLUMN
: XmPACK_TIGHT
);
495 menu
= XmCreatePopupMenu ((Widget
) parent
, "popup", args
, 2);
499 (XtCallbackProc
)wxMenuPopdownCallback
,
505 char mnem
= wxFindMnemonic (title
);
506 menu
= XmCreatePulldownMenu ((Widget
) parent
, "pulldown", args
, 2);
508 wxString
title2(wxStripMenuCodes(title
));
509 wxXmString
label_str(title2
);
510 buttonWidget
= XtVaCreateManagedWidget(title2
,
512 xmCascadeButtonGadgetClass
, (Widget
) parent
,
514 xmCascadeButtonWidgetClass
, (Widget
) parent
,
516 XmNlabelString
, label_str(),
521 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
524 m_menuWidget
= (WXWidget
) menu
;
526 m_topLevelMenu
= topMenu
;
528 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
530 node
= node
->GetNext() )
532 wxMenuItem
*item
= node
->GetData();
534 item
->CreateItem(menu
, menuBar
, topMenu
);
537 SetBackgroundColour(m_backgroundColour
);
538 SetForegroundColour(m_foregroundColour
);
544 // Destroys the Motif implementation of the menu,
545 // but maintains the wxWidgets data structures so we can
546 // do a CreateMenu again.
547 void wxMenu::DestroyMenu (bool full
)
549 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
551 node
= node
->GetNext() )
553 wxMenuItem
*item
= node
->GetData();
554 item
->SetMenuBar((wxMenuBar
*) NULL
);
556 item
->DestroyItem(full
);
563 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
564 XtDestroyWidget ((Widget
) m_buttonWidget
);
565 m_buttonWidget
= (WXWidget
) 0;
568 if (m_menuWidget
&& full
)
570 XtDestroyWidget((Widget
) m_menuWidget
);
571 m_menuWidget
= (WXWidget
) NULL
;
575 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
580 *it
= (wxMenuItem
*) NULL
;
581 return m_buttonWidget
;
584 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
586 node
= node
->GetNext() )
588 wxMenuItem
*item
= node
->GetData ();
589 if (item
->GetId() == id
)
593 return item
->GetButtonWidget();
596 if (item
->GetSubMenu())
598 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
607 *it
= (wxMenuItem
*) NULL
;
608 return (WXWidget
) NULL
;
611 void wxMenu::SetBackgroundColour(const wxColour
& col
)
613 m_backgroundColour
= col
;
615 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
617 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, true);
619 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
621 node
= node
->GetNext() )
623 wxMenuItem
* item
= node
->GetData();
624 if (item
->GetButtonWidget())
626 // This crashes because it uses gadgets
627 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, true);
629 if (item
->GetSubMenu())
630 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
634 void wxMenu::SetForegroundColour(const wxColour
& col
)
636 m_foregroundColour
= col
;
638 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
640 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
642 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
644 node
= node
->GetNext() )
646 wxMenuItem
* item
= node
->GetData();
647 if (item
->GetButtonWidget())
649 // This crashes because it uses gadgets
650 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
652 if (item
->GetSubMenu())
653 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
657 void wxMenu::ChangeFont(bool keepOriginalSize
)
659 // Lesstif 0.87 hangs here, but 0.93 does not
660 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
661 if (!m_font
.Ok() || !m_menuWidget
)
664 WXFontType fontType
= m_font
.GetFontType(XtDisplay((Widget
) m_menuWidget
));
666 XtVaSetValues ((Widget
) m_menuWidget
,
667 wxFont::GetFontTag(), fontType
,
671 XtVaSetValues ((Widget
) m_buttonWidget
,
672 wxFont::GetFontTag(), fontType
,
676 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
678 node
= node
->GetNext() )
680 wxMenuItem
* item
= node
->GetData();
681 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
683 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
684 wxFont::GetFontTag(), fontType
,
687 if (item
->GetSubMenu())
688 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
693 void wxMenu::SetFont(const wxFont
& font
)
699 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
701 m_backgroundColour
= col
;
703 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
705 size_t menuCount
= GetMenuCount();
706 for (size_t i
= 0; i
< menuCount
; i
++)
707 m_menus
.Item(i
)->GetData()->SetBackgroundColour((wxColour
&) col
);
712 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
714 m_foregroundColour
= col
;
716 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
718 size_t menuCount
= GetMenuCount();
719 for (size_t i
= 0; i
< menuCount
; i
++)
720 m_menus
.Item(i
)->GetData()->SetForegroundColour((wxColour
&) col
);
725 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
727 // Nothing to do for menubar, fonts are kept in wxMenus
730 bool wxMenuBar::SetFont(const wxFont
& font
)
735 size_t menuCount
= GetMenuCount();
736 for (size_t i
= 0; i
< menuCount
; i
++)
737 m_menus
.Item(i
)->GetData()->SetFont(font
);