1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
26 #include "wx/menuitem.h"
31 #include "wx/settings.h"
34 #pragma message disable nosimpint
35 #define XtDisplay XTDISPLAY
36 #define XtWindow XTWINDOW
39 #include <Xm/LabelG.h>
40 #include <Xm/CascadeBG.h>
41 #include <Xm/CascadeB.h>
42 #include <Xm/SeparatoG.h>
43 #include <Xm/PushBG.h>
44 #include <Xm/ToggleB.h>
45 #include <Xm/ToggleBG.h>
46 #include <Xm/RowColumn.h>
48 #pragma message enable nosimpint
51 #include "wx/motif/private.h"
53 // other standard headers
56 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
57 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
59 // ============================================================================
61 // ============================================================================
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // Construct a menu with optional title (then use append)
70 // Motif-specific members
72 m_menuWidget
= (WXWidget
) NULL
;
73 m_popupShell
= (WXWidget
) NULL
;
74 m_buttonWidget
= (WXWidget
) NULL
;
76 m_topLevelMenu
= (wxMenu
*) NULL
;
77 m_ownedByMenuBar
= false;
79 if ( !m_title
.empty() )
85 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
86 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
87 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
90 // The wxWindow destructor will take care of deleting the submenus.
101 // Not sure if this is right
102 if (m_menuParent
&& m_menuBar
)
114 // function appends a new item or submenu to the menu
115 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*pItem
)
119 // this is a dynamic Append
120 pItem
->CreateItem(m_menuWidget
, GetMenuBar(), m_topLevelMenu
);
123 if ( pItem
->IsSubMenu() )
125 pItem
->GetSubMenu()->m_topLevelMenu
= m_topLevelMenu
;
128 return wxMenuBase::DoAppend(pItem
);
131 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
133 item
->DestroyItem(true);
135 return wxMenuBase::DoRemove(item
);
138 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
140 if ( wxMenuBase::DoInsert(pos
, item
) )
143 wxFAIL_MSG(wxT("DoInsert not implemented; or error in wxMenuBase::DoInsert"));
148 void wxMenu::SetTitle(const wxString
& label
)
152 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
156 wxMenuItem
*item
= node
->GetData ();
157 Widget widget
= (Widget
) item
->GetButtonWidget();
161 wxXmString
title_str(label
);
162 XtVaSetValues(widget
,
163 XmNlabelString
, title_str(),
167 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
169 bool processed
= false;
171 // Try the menu's event handler
172 if ( !processed
&& GetEventHandler())
174 processed
= GetEventHandler()->ProcessEvent(event
);
176 // Try the window the menu was popped up from (and up
177 // through the hierarchy)
178 if ( !processed
&& GetInvokingWindow())
179 processed
= GetInvokingWindow()->ProcessEvent(event
);
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 void wxMenuBar::Init()
190 m_eventHandler
= this;
191 m_menuBarFrame
= NULL
;
192 m_mainWidget
= (WXWidget
) NULL
;
193 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
194 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
195 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
198 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxArrayString
& titles
, long WXUNUSED(style
))
200 wxASSERT( n
== titles
.GetCount() );
205 for ( size_t i
= 0; i
< n
; i
++ )
206 m_menus
.Append(menus
[i
]);
209 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
213 for ( size_t i
= 0; i
< n
; i
++ )
215 m_menus
.Append(menus
[i
]);
216 m_titles
.Add(titles
[i
]);
220 wxMenuBar::~wxMenuBar()
222 // nothing to do: wxMenuBarBase will delete the menus
225 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
227 // wxFAIL_MSG("TODO");
228 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
231 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
233 wxMenu
*menu
= GetMenu(pos
);
237 Widget w
= (Widget
)menu
->GetButtonWidget();
240 wxXmString
label_str(label
);
243 XmNlabelString
, label_str(),
248 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
250 wxMenu
*menu
= GetMenu(pos
);
253 Widget w
= (Widget
)menu
->GetButtonWidget();
258 XmNlabelString
, &text
,
261 return wxXmStringToString( text
);
265 return wxEmptyString
;
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
);
283 return wxMenuBarBase::Append(menu
, title
);
286 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
288 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
291 wxFAIL_MSG(wxT("TODO"));
296 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
298 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
301 wxFAIL_MSG(wxT("TODO"));
306 wxMenu
*wxMenuBar::Remove(size_t pos
)
308 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
312 if ( m_menuBarFrame
)
313 menu
->DestroyMenu(true);
315 menu
->SetMenuBar(NULL
);
317 m_titles
.RemoveAt(pos
);
322 // Find the menu menuString, item itemString, and return the item id.
323 // Returns -1 if none found.
324 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
328 wxStripMenuCodes (wxConstCast(menuString
.c_str(), char), buf1
);
330 size_t menuCount
= GetMenuCount();
331 for (size_t i
= 0; i
< menuCount
; i
++)
333 wxStripMenuCodes (wxConstCast(m_titles
[i
].c_str(), char), buf2
);
334 if (strcmp (buf1
, buf2
) == 0)
335 return m_menus
.Item(i
)->GetData()->FindItem (itemString
);
340 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
345 wxMenuItem
*item
= NULL
;
346 size_t menuCount
= GetMenuCount();
347 for (size_t i
= 0; i
< menuCount
; i
++)
348 if ((item
= m_menus
.Item(i
)->GetData()->FindItem(id
, itemMenu
)))
354 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
358 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
360 if (!XtIsManaged((Widget) m_mainWidget))
361 XtManageChild((Widget) m_mainWidget);
363 XtMapWidget((Widget
) m_mainWidget
);
367 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(),
368 wxMOTIF_STR("MenuBar"), NULL
, 0);
369 m_mainWidget
= (WXWidget
) menuBarW
;
371 size_t menuCount
= GetMenuCount();
372 for (size_t i
= 0; i
< menuCount
; i
++)
374 wxMenu
*menu
= GetMenu(i
);
375 wxString
title(m_titles
[i
]);
376 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, title
, true));
378 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
379 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
381 // tear off menu support
382 #if (XmVersion >= 1002)
383 if ( menu
->IsTearOff() )
385 XtVaSetValues(GetWidget(menu
),
386 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
388 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
389 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
390 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, true);
395 SetBackgroundColour(m_backgroundColour
);
396 SetForegroundColour(m_foregroundColour
);
399 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
400 XtRealizeWidget ((Widget
) menuBarW
);
401 XtManageChild ((Widget
) menuBarW
);
402 SetMenuBarFrame(parent
);
407 // Destroy menubar, but keep data structures intact so we can recreate it.
408 bool wxMenuBar::DestroyMenuBar()
412 SetMenuBarFrame((wxFrame
*) NULL
);
416 XtUnmanageChild ((Widget
) m_mainWidget
);
417 XtUnrealizeWidget ((Widget
) m_mainWidget
);
419 size_t menuCount
= GetMenuCount();
420 for (size_t i
= 0; i
< menuCount
; i
++)
422 wxMenu
*menu
= GetMenu(i
);
423 menu
->DestroyMenu(true);
426 XtDestroyWidget((Widget
) m_mainWidget
);
427 m_mainWidget
= (WXWidget
) 0;
429 SetMenuBarFrame((wxFrame
*) NULL
);
434 // Since PopupMenu under Motif stills grab right mouse button events
435 // after it was closed, we need to delete the associated widgets to
436 // allow next PopUpMenu to appear...
437 void wxMenu::DestroyWidgetAndDetach()
441 wxMenu
*menuParent
= GetParent();
444 wxMenuItemList::compatibility_iterator node
= menuParent
->GetMenuItems().GetFirst();
447 if ( node
->GetData()->GetSubMenu() == this )
449 delete node
->GetData();
450 menuParent
->GetMenuItems().Erase(node
);
455 node
= node
->GetNext();
462 // Mark as no longer popped up
467 * Create a popup or pulldown menu.
468 * Submenus of a popup will be pulldown.
472 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
, WXWidget parent
, wxMenu
* topMenu
, const wxString
& title
, bool pullDown
)
474 Widget menu
= (Widget
) 0;
475 Widget buttonWidget
= (Widget
) 0;
477 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
478 XtSetArg (args
[1], XmNpacking
, (m_numColumns
> 1) ? XmPACK_COLUMN
: XmPACK_TIGHT
);
482 menu
= XmCreatePopupMenu ((Widget
) parent
, wxMOTIF_STR("popup"), args
, 2);
486 (XtCallbackProc
)wxMenuPopdownCallback
,
492 char mnem
= wxFindMnemonic (title
);
493 menu
= XmCreatePulldownMenu ((Widget
) parent
, wxMOTIF_STR("pulldown"), args
, 2);
495 wxString
title2(wxStripMenuCodes(title
));
496 wxXmString
label_str(title2
);
497 buttonWidget
= XtVaCreateManagedWidget(title2
,
499 xmCascadeButtonGadgetClass
, (Widget
) parent
,
501 xmCascadeButtonWidgetClass
, (Widget
) parent
,
503 XmNlabelString
, label_str(),
508 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
511 m_menuWidget
= (WXWidget
) menu
;
513 m_topLevelMenu
= topMenu
;
515 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
517 node
= node
->GetNext() )
519 wxMenuItem
*item
= node
->GetData();
521 item
->CreateItem(menu
, menuBar
, topMenu
);
524 SetBackgroundColour(m_backgroundColour
);
525 SetForegroundColour(m_foregroundColour
);
531 // Destroys the Motif implementation of the menu,
532 // but maintains the wxWidgets data structures so we can
533 // do a CreateMenu again.
534 void wxMenu::DestroyMenu (bool full
)
536 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
538 node
= node
->GetNext() )
540 wxMenuItem
*item
= node
->GetData();
541 item
->SetMenuBar((wxMenuBar
*) NULL
);
543 item
->DestroyItem(full
);
550 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
551 XtDestroyWidget ((Widget
) m_buttonWidget
);
552 m_buttonWidget
= (WXWidget
) 0;
555 if (m_menuWidget
&& full
)
557 XtDestroyWidget((Widget
) m_menuWidget
);
558 m_menuWidget
= (WXWidget
) NULL
;
562 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
567 *it
= (wxMenuItem
*) NULL
;
568 return m_buttonWidget
;
571 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
573 node
= node
->GetNext() )
575 wxMenuItem
*item
= node
->GetData ();
576 if (item
->GetId() == id
)
580 return item
->GetButtonWidget();
583 if (item
->GetSubMenu())
585 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
594 *it
= (wxMenuItem
*) NULL
;
595 return (WXWidget
) NULL
;
598 void wxMenu::SetBackgroundColour(const wxColour
& col
)
600 m_backgroundColour
= col
;
602 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
604 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, true);
606 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
608 node
= node
->GetNext() )
610 wxMenuItem
* item
= node
->GetData();
611 if (item
->GetButtonWidget())
613 // This crashes because it uses gadgets
614 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, true);
616 if (item
->GetSubMenu())
617 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
621 void wxMenu::SetForegroundColour(const wxColour
& col
)
623 m_foregroundColour
= col
;
625 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
627 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
629 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
631 node
= node
->GetNext() )
633 wxMenuItem
* item
= node
->GetData();
634 if (item
->GetButtonWidget())
636 // This crashes because it uses gadgets
637 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
639 if (item
->GetSubMenu())
640 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
644 void wxMenu::ChangeFont(bool keepOriginalSize
)
646 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
647 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
648 if (!m_font
.Ok() || !m_menuWidget
)
651 Display
* dpy
= XtDisplay((Widget
) m_menuWidget
);
653 XtVaSetValues ((Widget
) m_menuWidget
,
654 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
658 XtVaSetValues ((Widget
) m_buttonWidget
,
659 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
663 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
665 node
= node
->GetNext() )
667 wxMenuItem
* item
= node
->GetData();
668 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
670 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
671 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
674 if (item
->GetSubMenu())
675 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
680 void wxMenu::SetFont(const wxFont
& font
)
686 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
688 m_backgroundColour
= col
;
690 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
692 size_t menuCount
= GetMenuCount();
693 for (size_t i
= 0; i
< menuCount
; i
++)
694 m_menus
.Item(i
)->GetData()->SetBackgroundColour((wxColour
&) col
);
699 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
701 m_foregroundColour
= col
;
703 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
705 size_t menuCount
= GetMenuCount();
706 for (size_t i
= 0; i
< menuCount
; i
++)
707 m_menus
.Item(i
)->GetData()->SetForegroundColour((wxColour
&) col
);
712 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
714 // Nothing to do for menubar, fonts are kept in wxMenus
717 bool wxMenuBar::SetFont(const wxFont
& font
)
722 size_t menuCount
= GetMenuCount();
723 for (size_t i
= 0; i
< menuCount
; i
++)
724 m_menus
.Item(i
)->GetData()->SetFont(font
);