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 // ----------------------------------------------------------------------------
28 #include "wx/menuitem.h"
33 #include "wx/settings.h"
36 #pragma message disable nosimpint
37 #define XtDisplay XTDISPLAY
38 #define XtWindow XTWINDOW
41 #include <Xm/LabelG.h>
42 #include <Xm/CascadeBG.h>
43 #include <Xm/CascadeB.h>
44 #include <Xm/SeparatoG.h>
45 #include <Xm/PushBG.h>
46 #include <Xm/ToggleB.h>
47 #include <Xm/ToggleBG.h>
48 #include <Xm/RowColumn.h>
50 #pragma message enable nosimpint
53 #include "wx/motif/private.h"
55 // other standard headers
58 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
59 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // Construct a menu with optional title (then use append)
72 // Motif-specific members
74 m_menuWidget
= (WXWidget
) NULL
;
75 m_popupShell
= (WXWidget
) NULL
;
76 m_buttonWidget
= (WXWidget
) NULL
;
78 m_topLevelMenu
= (wxMenu
*) NULL
;
79 m_ownedByMenuBar
= FALSE
;
83 Append(wxID_SEPARATOR
, m_title
) ;
87 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
88 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
89 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
92 // The wxWindow destructor will take care of deleting the submenus.
103 // Not sure if this is right
104 if (m_menuParent
&& m_menuBar
)
116 // function appends a new item or submenu to the menu
117 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*pItem
)
121 // this is a dynamic Append
122 pItem
->CreateItem(m_menuWidget
, m_menuBar
, m_topLevelMenu
);
125 if ( pItem
->IsSubMenu() )
127 pItem
->GetSubMenu()->m_topLevelMenu
= m_topLevelMenu
;
130 return wxMenuBase::DoAppend(pItem
);
133 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
135 item
->DestroyItem(TRUE
);
137 return wxMenuBase::DoRemove(item
);
140 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
142 if ( wxMenuBase::DoInsert(pos
, item
) )
145 wxFAIL_MSG(wxT("DoInsert not implemented; or error in wxMenuBase::DoInsert"));
150 void wxMenu::SetTitle(const wxString
& label
)
154 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
158 wxMenuItem
*item
= node
->GetData ();
159 Widget widget
= (Widget
) item
->GetButtonWidget();
163 wxXmString
title_str(label
);
164 XtVaSetValues(widget
,
165 XmNlabelString
, title_str(),
169 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
171 bool processed
= FALSE
;
173 #if wxUSE_MENU_CALLBACK
177 (void) (*(m_callback
)) (*this, event
);
180 #endif // wxUSE_MENU_CALLBACK
182 // Try the menu's event handler
183 if ( !processed
&& GetEventHandler())
185 processed
= GetEventHandler()->ProcessEvent(event
);
187 // Try the window the menu was popped up from (and up
188 // through the hierarchy)
189 if ( !processed
&& GetInvokingWindow())
190 processed
= GetInvokingWindow()->ProcessEvent(event
);
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
199 void wxMenuBar::Init()
201 m_eventHandler
= this;
202 m_menuBarFrame
= NULL
;
203 m_mainWidget
= (WXWidget
) NULL
;
204 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
205 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
206 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
209 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxArrayString
& titles
)
211 wxASSERT( size_t(n
) == titles
.GetCount() );
216 for ( int i
= 0; i
< n
; i
++ )
217 m_menus
.Append(menus
[i
]);
220 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[])
224 for ( int i
= 0; i
< n
; i
++ )
226 m_menus
.Append(menus
[i
]);
227 m_titles
.Add(titles
[i
]);
231 wxMenuBar::~wxMenuBar()
233 // nothing to do: wxMenuBarBase will delete the menus
236 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
238 // wxFAIL_MSG("TODO");
239 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
242 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
244 wxMenu
*menu
= GetMenu(pos
);
248 Widget w
= (Widget
)menu
->GetButtonWidget();
251 wxXmString
label_str(label
);
254 XmNlabelString
, label_str(),
259 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
261 wxMenu
*menu
= GetMenu(pos
);
264 Widget w
= (Widget
)menu
->GetButtonWidget();
269 XmNlabelString
, &text
,
272 return wxXmStringToString( text
);
276 return wxEmptyString
;
279 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
281 wxCHECK_MSG( menu
, FALSE
, wxT("invalid menu") );
282 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), FALSE
,
283 wxT("menu already appended") );
285 if ( m_menuBarFrame
)
287 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
, title
, TRUE
);
288 wxCHECK_MSG( w
, FALSE
, wxT("failed to create menu") );
289 menu
->SetButtonWidget(w
);
292 //menu->SetMenuBar(this);
296 return wxMenuBarBase::Append(menu
, title
);
299 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
301 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
304 wxFAIL_MSG(wxT("TODO"));
309 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
311 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
314 wxFAIL_MSG(wxT("TODO"));
319 wxMenu
*wxMenuBar::Remove(size_t pos
)
321 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
325 if ( m_menuBarFrame
)
326 menu
->DestroyMenu(TRUE
);
328 menu
->SetMenuBar(NULL
);
330 m_titles
.RemoveAt(pos
);
335 // Find the menu menuString, item itemString, and return the item id.
336 // Returns -1 if none found.
337 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
341 wxStripMenuCodes (wxConstCast(menuString
.c_str(), char), buf1
);
343 size_t menuCount
= GetMenuCount();
344 for (size_t i
= 0; i
< menuCount
; i
++)
346 wxStripMenuCodes (wxConstCast(m_titles
[i
].c_str(), char), buf2
);
347 if (strcmp (buf1
, buf2
) == 0)
348 return m_menus
.Item(i
)->GetData()->FindItem (itemString
);
353 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
358 wxMenuItem
*item
= NULL
;
359 size_t menuCount
= GetMenuCount();
360 for (size_t i
= 0; i
< menuCount
; i
++)
361 if ((item
= m_menus
.Item(i
)->GetData()->FindItem(id
, itemMenu
)))
367 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
371 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
373 if (!XtIsManaged((Widget) m_mainWidget))
374 XtManageChild((Widget) m_mainWidget);
376 XtMapWidget((Widget
) m_mainWidget
);
380 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(), "MenuBar", NULL
, 0);
381 m_mainWidget
= (WXWidget
) menuBarW
;
383 size_t menuCount
= GetMenuCount();
384 for (size_t i
= 0; i
< menuCount
; i
++)
386 wxMenu
*menu
= GetMenu(i
);
387 wxString
title(m_titles
[i
]);
388 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, title
, TRUE
));
390 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
391 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
393 // tear off menu support
394 #if (XmVersion >= 1002)
395 if ( menu
->IsTearOff() )
397 XtVaSetValues(GetWidget(menu
),
398 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
400 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
401 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
402 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, TRUE
);
407 SetBackgroundColour(m_backgroundColour
);
408 SetForegroundColour(m_foregroundColour
);
411 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
412 XtRealizeWidget ((Widget
) menuBarW
);
413 XtManageChild ((Widget
) menuBarW
);
414 SetMenuBarFrame(parent
);
419 // Destroy menubar, but keep data structures intact so we can recreate it.
420 bool wxMenuBar::DestroyMenuBar()
424 SetMenuBarFrame((wxFrame
*) NULL
);
428 XtUnmanageChild ((Widget
) m_mainWidget
);
429 XtUnrealizeWidget ((Widget
) m_mainWidget
);
431 size_t menuCount
= GetMenuCount();
432 for (size_t i
= 0; i
< menuCount
; i
++)
434 wxMenu
*menu
= GetMenu(i
);
435 menu
->DestroyMenu(TRUE
);
438 XtDestroyWidget((Widget
) m_mainWidget
);
439 m_mainWidget
= (WXWidget
) 0;
441 SetMenuBarFrame((wxFrame
*) NULL
);
446 // Since PopupMenu under Motif stills grab right mouse button events
447 // after it was closed, we need to delete the associated widgets to
448 // allow next PopUpMenu to appear...
449 void wxMenu::DestroyWidgetAndDetach()
453 wxMenu
*menuParent
= GetParent();
456 wxMenuItemList::compatibility_iterator node
= menuParent
->GetMenuItems().GetFirst();
459 if ( node
->GetData()->GetSubMenu() == this )
461 delete node
->GetData();
462 menuParent
->GetMenuItems().Erase(node
);
467 node
= node
->GetNext();
474 // Mark as no longer popped up
479 * Create a popup or pulldown menu.
480 * Submenus of a popup will be pulldown.
484 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
, WXWidget parent
, wxMenu
* topMenu
, const wxString
& title
, bool pullDown
)
486 Widget menu
= (Widget
) 0;
487 Widget buttonWidget
= (Widget
) 0;
489 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
490 XtSetArg (args
[1], XmNpacking
, (m_numColumns
> 1) ? XmPACK_COLUMN
: XmPACK_TIGHT
);
494 menu
= XmCreatePopupMenu ((Widget
) parent
, "popup", args
, 2);
498 (XtCallbackProc
)wxMenuPopdownCallback
,
504 char mnem
= wxFindMnemonic (title
);
505 menu
= XmCreatePulldownMenu ((Widget
) parent
, "pulldown", args
, 2);
507 wxString
title2(wxStripMenuCodes(title
));
508 wxXmString
label_str(title2
);
509 buttonWidget
= XtVaCreateManagedWidget(title2
,
511 xmCascadeButtonGadgetClass
, (Widget
) parent
,
513 xmCascadeButtonWidgetClass
, (Widget
) parent
,
515 XmNlabelString
, label_str(),
520 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
523 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 wxWindows 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
);