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 // Try the menu's event handler
177 if ( !processed
&& GetEventHandler())
179 processed
= GetEventHandler()->ProcessEvent(event
);
181 // Try the window the menu was popped up from (and up
182 // through the hierarchy)
183 if ( !processed
&& GetInvokingWindow())
184 processed
= GetInvokingWindow()->ProcessEvent(event
);
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 void wxMenuBar::Init()
195 m_eventHandler
= this;
196 m_menuBarFrame
= NULL
;
197 m_mainWidget
= (WXWidget
) NULL
;
198 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
199 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
200 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
203 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxArrayString
& titles
, long WXUNUSED(style
))
205 wxASSERT( n
== titles
.GetCount() );
210 for ( size_t i
= 0; i
< n
; i
++ )
211 m_menus
.Append(menus
[i
]);
214 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
218 for ( size_t i
= 0; i
< n
; i
++ )
220 m_menus
.Append(menus
[i
]);
221 m_titles
.Add(titles
[i
]);
225 wxMenuBar::~wxMenuBar()
227 // nothing to do: wxMenuBarBase will delete the menus
230 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
232 // wxFAIL_MSG("TODO");
233 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
236 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
238 wxMenu
*menu
= GetMenu(pos
);
242 Widget w
= (Widget
)menu
->GetButtonWidget();
245 wxXmString
label_str(label
);
248 XmNlabelString
, label_str(),
253 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
255 wxMenu
*menu
= GetMenu(pos
);
258 Widget w
= (Widget
)menu
->GetButtonWidget();
263 XmNlabelString
, &text
,
266 return wxXmStringToString( text
);
270 return wxEmptyString
;
273 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
275 wxCHECK_MSG( menu
, false, wxT("invalid menu") );
276 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), false,
277 wxT("menu already appended") );
279 if ( m_menuBarFrame
)
281 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
, title
, true);
282 wxCHECK_MSG( w
, false, wxT("failed to create menu") );
283 menu
->SetButtonWidget(w
);
288 return wxMenuBarBase::Append(menu
, title
);
291 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
293 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
296 wxFAIL_MSG(wxT("TODO"));
301 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
303 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
306 wxFAIL_MSG(wxT("TODO"));
311 wxMenu
*wxMenuBar::Remove(size_t pos
)
313 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
317 if ( m_menuBarFrame
)
318 menu
->DestroyMenu(true);
320 menu
->SetMenuBar(NULL
);
322 m_titles
.RemoveAt(pos
);
327 // Find the menu menuString, item itemString, and return the item id.
328 // Returns -1 if none found.
329 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
333 wxStripMenuCodes (wxConstCast(menuString
.c_str(), char), buf1
);
335 size_t menuCount
= GetMenuCount();
336 for (size_t i
= 0; i
< menuCount
; i
++)
338 wxStripMenuCodes (wxConstCast(m_titles
[i
].c_str(), char), buf2
);
339 if (strcmp (buf1
, buf2
) == 0)
340 return m_menus
.Item(i
)->GetData()->FindItem (itemString
);
345 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
350 wxMenuItem
*item
= NULL
;
351 size_t menuCount
= GetMenuCount();
352 for (size_t i
= 0; i
< menuCount
; i
++)
353 if ((item
= m_menus
.Item(i
)->GetData()->FindItem(id
, itemMenu
)))
359 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
363 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
365 if (!XtIsManaged((Widget) m_mainWidget))
366 XtManageChild((Widget) m_mainWidget);
368 XtMapWidget((Widget
) m_mainWidget
);
372 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(), "MenuBar", NULL
, 0);
373 m_mainWidget
= (WXWidget
) menuBarW
;
375 size_t menuCount
= GetMenuCount();
376 for (size_t i
= 0; i
< menuCount
; i
++)
378 wxMenu
*menu
= GetMenu(i
);
379 wxString
title(m_titles
[i
]);
380 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, title
, true));
382 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
383 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
385 // tear off menu support
386 #if (XmVersion >= 1002)
387 if ( menu
->IsTearOff() )
389 XtVaSetValues(GetWidget(menu
),
390 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
392 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
393 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
394 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, true);
399 SetBackgroundColour(m_backgroundColour
);
400 SetForegroundColour(m_foregroundColour
);
403 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
404 XtRealizeWidget ((Widget
) menuBarW
);
405 XtManageChild ((Widget
) menuBarW
);
406 SetMenuBarFrame(parent
);
411 // Destroy menubar, but keep data structures intact so we can recreate it.
412 bool wxMenuBar::DestroyMenuBar()
416 SetMenuBarFrame((wxFrame
*) NULL
);
420 XtUnmanageChild ((Widget
) m_mainWidget
);
421 XtUnrealizeWidget ((Widget
) m_mainWidget
);
423 size_t menuCount
= GetMenuCount();
424 for (size_t i
= 0; i
< menuCount
; i
++)
426 wxMenu
*menu
= GetMenu(i
);
427 menu
->DestroyMenu(true);
430 XtDestroyWidget((Widget
) m_mainWidget
);
431 m_mainWidget
= (WXWidget
) 0;
433 SetMenuBarFrame((wxFrame
*) NULL
);
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...
441 void wxMenu::DestroyWidgetAndDetach()
445 wxMenu
*menuParent
= GetParent();
448 wxMenuItemList::compatibility_iterator node
= menuParent
->GetMenuItems().GetFirst();
451 if ( node
->GetData()->GetSubMenu() == this )
453 delete node
->GetData();
454 menuParent
->GetMenuItems().Erase(node
);
459 node
= node
->GetNext();
466 // Mark as no longer popped up
471 * Create a popup or pulldown menu.
472 * Submenus of a popup will be pulldown.
476 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
, WXWidget parent
, wxMenu
* topMenu
, const wxString
& title
, bool pullDown
)
478 Widget menu
= (Widget
) 0;
479 Widget buttonWidget
= (Widget
) 0;
481 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
482 XtSetArg (args
[1], XmNpacking
, (m_numColumns
> 1) ? XmPACK_COLUMN
: XmPACK_TIGHT
);
486 menu
= XmCreatePopupMenu ((Widget
) parent
, "popup", args
, 2);
490 (XtCallbackProc
)wxMenuPopdownCallback
,
496 char mnem
= wxFindMnemonic (title
);
497 menu
= XmCreatePulldownMenu ((Widget
) parent
, "pulldown", args
, 2);
499 wxString
title2(wxStripMenuCodes(title
));
500 wxXmString
label_str(title2
);
501 buttonWidget
= XtVaCreateManagedWidget(title2
,
503 xmCascadeButtonGadgetClass
, (Widget
) parent
,
505 xmCascadeButtonWidgetClass
, (Widget
) parent
,
507 XmNlabelString
, label_str(),
512 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
515 m_menuWidget
= (WXWidget
) menu
;
517 m_topLevelMenu
= topMenu
;
519 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
521 node
= node
->GetNext() )
523 wxMenuItem
*item
= node
->GetData();
525 item
->CreateItem(menu
, menuBar
, topMenu
);
528 SetBackgroundColour(m_backgroundColour
);
529 SetForegroundColour(m_foregroundColour
);
535 // Destroys the Motif implementation of the menu,
536 // but maintains the wxWidgets data structures so we can
537 // do a CreateMenu again.
538 void wxMenu::DestroyMenu (bool full
)
540 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
542 node
= node
->GetNext() )
544 wxMenuItem
*item
= node
->GetData();
545 item
->SetMenuBar((wxMenuBar
*) NULL
);
547 item
->DestroyItem(full
);
554 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
555 XtDestroyWidget ((Widget
) m_buttonWidget
);
556 m_buttonWidget
= (WXWidget
) 0;
559 if (m_menuWidget
&& full
)
561 XtDestroyWidget((Widget
) m_menuWidget
);
562 m_menuWidget
= (WXWidget
) NULL
;
566 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
571 *it
= (wxMenuItem
*) NULL
;
572 return m_buttonWidget
;
575 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
577 node
= node
->GetNext() )
579 wxMenuItem
*item
= node
->GetData ();
580 if (item
->GetId() == id
)
584 return item
->GetButtonWidget();
587 if (item
->GetSubMenu())
589 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
598 *it
= (wxMenuItem
*) NULL
;
599 return (WXWidget
) NULL
;
602 void wxMenu::SetBackgroundColour(const wxColour
& col
)
604 m_backgroundColour
= col
;
606 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
608 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, true);
610 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
612 node
= node
->GetNext() )
614 wxMenuItem
* item
= node
->GetData();
615 if (item
->GetButtonWidget())
617 // This crashes because it uses gadgets
618 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, true);
620 if (item
->GetSubMenu())
621 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
625 void wxMenu::SetForegroundColour(const wxColour
& col
)
627 m_foregroundColour
= col
;
629 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
631 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
633 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
635 node
= node
->GetNext() )
637 wxMenuItem
* item
= node
->GetData();
638 if (item
->GetButtonWidget())
640 // This crashes because it uses gadgets
641 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
643 if (item
->GetSubMenu())
644 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
648 void wxMenu::ChangeFont(bool keepOriginalSize
)
650 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
651 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
652 if (!m_font
.Ok() || !m_menuWidget
)
655 Display
* dpy
= XtDisplay((Widget
) m_menuWidget
);
657 XtVaSetValues ((Widget
) m_menuWidget
,
658 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
662 XtVaSetValues ((Widget
) m_buttonWidget
,
663 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
667 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
669 node
= node
->GetNext() )
671 wxMenuItem
* item
= node
->GetData();
672 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
674 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
675 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
678 if (item
->GetSubMenu())
679 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
684 void wxMenu::SetFont(const wxFont
& font
)
690 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
692 m_backgroundColour
= col
;
694 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
696 size_t menuCount
= GetMenuCount();
697 for (size_t i
= 0; i
< menuCount
; i
++)
698 m_menus
.Item(i
)->GetData()->SetBackgroundColour((wxColour
&) col
);
703 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
705 m_foregroundColour
= col
;
707 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
709 size_t menuCount
= GetMenuCount();
710 for (size_t i
= 0; i
< menuCount
; i
++)
711 m_menus
.Item(i
)->GetData()->SetForegroundColour((wxColour
&) col
);
716 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
718 // Nothing to do for menubar, fonts are kept in wxMenus
721 bool wxMenuBar::SetFont(const wxFont
& font
)
726 size_t menuCount
= GetMenuCount();
727 for (size_t i
= 0; i
< menuCount
; i
++)
728 m_menus
.Item(i
)->GetData()->SetFont(font
);