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 // ----------------------------------------------------------------------------
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::GetSystemColour(wxSYS_COLOUR_MENU
);
88 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT
);
89 m_font
= wxSystemSettings::GetSystemFont(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 bool 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 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
142 if ( !wxMenuBase::DoInsert(pos
, item
) )
145 wxFAIL_MSG(wxT("not implemented"));
150 void wxMenu::SetTitle(const wxString
& label
)
154 wxMenuItemList::Node
*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::GetSystemColour(wxSYS_COLOUR_MENU
);
205 m_foregroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT
);
206 m_font
= wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
);
209 wxMenuBar::wxMenuBar(int n
, wxMenu
*menus
[], const wxString titles
[])
213 for ( int 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
252 wxMenu
*menu
= GetMenu(pos
);
255 Widget w
= (Widget
)menu
->GetButtonWidget();
260 XmNlabelString
, &text
,
264 if ( XmStringGetLtoR(text
, XmSTRING_DEFAULT_CHARSET
, &s
) )
276 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
278 wxCHECK_MSG( menu
, FALSE
, wxT("invalid menu") );
279 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), FALSE
,
280 wxT("menu already appended") );
282 if ( m_menuBarFrame
)
284 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
, title
, TRUE
);
285 wxCHECK_MSG( w
, FALSE
, wxT("failed to create menu") );
286 menu
->SetButtonWidget(w
);
289 //menu->SetMenuBar(this);
293 return wxMenuBarBase::Append(menu
, title
);
296 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
298 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
301 wxFAIL_MSG(wxT("TODO"));
306 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
308 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
311 wxFAIL_MSG(wxT("TODO"));
316 wxMenu
*wxMenuBar::Remove(size_t pos
)
318 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
322 if ( m_menuBarFrame
)
323 menu
->DestroyMenu(TRUE
);
325 menu
->SetMenuBar(NULL
);
327 m_titles
.Remove(pos
);
332 // Find the menu menuString, item itemString, and return the item id.
333 // Returns -1 if none found.
334 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
338 wxStripMenuCodes ((char *)(const char *)menuString
, buf1
);
340 size_t menuCount
= GetMenuCount();
341 for (size_t i
= 0; i
< menuCount
; i
++)
343 wxStripMenuCodes ((char *)(const char *)m_titles
[i
], buf2
);
344 if (strcmp (buf1
, buf2
) == 0)
345 return m_menus
[i
]->FindItem (itemString
);
350 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
355 wxMenuItem
*item
= NULL
;
356 size_t menuCount
= GetMenuCount();
357 for (size_t i
= 0; i
< menuCount
; i
++)
358 if ((item
= m_menus
[i
]->FindItem(id
, itemMenu
)))
364 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
368 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
370 if (!XtIsManaged((Widget) m_mainWidget))
371 XtManageChild((Widget) m_mainWidget);
373 XtMapWidget((Widget
) m_mainWidget
);
377 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(), "MenuBar", NULL
, 0);
378 m_mainWidget
= (WXWidget
) menuBarW
;
380 size_t menuCount
= GetMenuCount();
381 for (size_t i
= 0; i
< menuCount
; i
++)
383 wxMenu
*menu
= GetMenu(i
);
384 wxString
title(m_titles
[i
]);
385 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, title
, TRUE
));
387 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
388 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
390 // tear off menu support
391 #if (XmVersion >= 1002)
392 if ( menu
->IsTearOff() )
394 XtVaSetValues(GetWidget(menu
),
395 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
397 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
398 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
399 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, TRUE
);
404 SetBackgroundColour(m_backgroundColour
);
405 SetForegroundColour(m_foregroundColour
);
408 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
409 XtRealizeWidget ((Widget
) menuBarW
);
410 XtManageChild ((Widget
) menuBarW
);
411 SetMenuBarFrame(parent
);
416 // Destroy menubar, but keep data structures intact so we can recreate it.
417 bool wxMenuBar::DestroyMenuBar()
421 SetMenuBarFrame((wxFrame
*) NULL
);
425 XtUnmanageChild ((Widget
) m_mainWidget
);
426 XtUnrealizeWidget ((Widget
) m_mainWidget
);
428 size_t menuCount
= GetMenuCount();
429 for (size_t i
= 0; i
< menuCount
; i
++)
431 wxMenu
*menu
= GetMenu(i
);
432 menu
->DestroyMenu(TRUE
);
435 XtDestroyWidget((Widget
) m_mainWidget
);
436 m_mainWidget
= (WXWidget
) 0;
438 SetMenuBarFrame((wxFrame
*) NULL
);
444 static XtWorkProcId WorkProcMenuId
;
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...
451 int PostDeletionOfMenu( XtPointer
* clientData
)
453 XtRemoveWorkProc(WorkProcMenuId
);
454 wxMenu
*menu
= (wxMenu
*)clientData
;
456 if (menu
->GetMainWidget())
458 wxMenu
*menuParent
= menu
->GetParent();
461 wxMenuItemList::Node
*node
= menuParent
->GetMenuItems().GetFirst();
464 if ( node
->GetData()->GetSubMenu() == menu
)
466 menuParent
->GetMenuItems().DeleteNode(node
);
471 node
= node
->GetNext();
475 menu
->DestroyMenu(TRUE
);
478 // Mark as no longer popped up
485 wxMenuPopdownCallback(Widget
WXUNUSED(w
), XtPointer clientData
,
486 XtPointer
WXUNUSED(ptr
))
488 wxMenu
*menu
= (wxMenu
*)clientData
;
490 // Added by JOREL Jean-Charles <jjorel@silr.ireste.fr>
491 /* Since Callbacks of MenuItems are not yet processed, we put a
492 * background job which will be done when system will be idle.
493 * What awful hack!! :(
496 WorkProcMenuId
= XtAppAddWorkProc(
497 (XtAppContext
) wxTheApp
->GetAppContext(),
498 (XtWorkProc
) PostDeletionOfMenu
,
500 // Apparently not found in Motif headers
501 // XtVaSetValues( w, XmNpopupEnabled, XmPOPUP_DISABLED, NULL );
505 * Create a popup or pulldown menu.
506 * Submenus of a popup will be pulldown.
510 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
, WXWidget parent
, wxMenu
* topMenu
, const wxString
& title
, bool pullDown
)
512 Widget menu
= (Widget
) 0;
513 Widget buttonWidget
= (Widget
) 0;
515 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
516 XtSetArg (args
[1], XmNpacking
, XmPACK_COLUMN
);
520 menu
= XmCreatePopupMenu ((Widget
) parent
, "popup", args
, 2);
523 (XtCallbackProc
)wxMenuPopdownCallback
,
528 char mnem
= wxFindMnemonic (title
);
529 wxStripMenuCodes ((char*) (const char*) title
, wxBuffer
);
531 menu
= XmCreatePulldownMenu ((Widget
) parent
, "pulldown", args
, 2);
533 wxString
title2(wxStripMenuCodes(title
));
534 wxXmString
label_str(title2
);
535 buttonWidget
= XtVaCreateManagedWidget(title2
,
537 xmCascadeButtonGadgetClass
, (Widget
) parent
,
539 xmCascadeButtonWidgetClass
, (Widget
) parent
,
541 XmNlabelString
, label_str(),
546 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
549 m_menuWidget
= (WXWidget
) menu
;
552 m_topLevelMenu
= topMenu
;
554 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
556 node
= node
->GetNext() )
558 wxMenuItem
*item
= node
->GetData();
560 item
->CreateItem(menu
, menuBar
, topMenu
);
563 SetBackgroundColour(m_backgroundColour
);
564 SetForegroundColour(m_foregroundColour
);
570 // Destroys the Motif implementation of the menu,
571 // but maintains the wxWindows data structures so we can
572 // do a CreateMenu again.
573 void wxMenu::DestroyMenu (bool full
)
575 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
577 node
= node
->GetNext() )
579 wxMenuItem
*item
= node
->GetData();
580 item
->SetMenuBar((wxMenuBar
*) NULL
);
582 item
->DestroyItem(full
);
589 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
590 XtDestroyWidget ((Widget
) m_buttonWidget
);
591 m_buttonWidget
= (WXWidget
) 0;
594 if (m_menuWidget
&& full
)
596 XtDestroyWidget((Widget
) m_menuWidget
);
597 m_menuWidget
= (WXWidget
) NULL
;
601 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
606 *it
= (wxMenuItem
*) NULL
;
607 return m_buttonWidget
;
610 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
612 node
= node
->GetNext() )
614 wxMenuItem
*item
= node
->GetData ();
615 if (item
->GetId() == id
)
619 return item
->GetButtonWidget();
622 if (item
->GetSubMenu())
624 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
633 *it
= (wxMenuItem
*) NULL
;
634 return (WXWidget
) NULL
;
637 void wxMenu::SetBackgroundColour(const wxColour
& col
)
639 m_backgroundColour
= col
;
641 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
643 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, TRUE
);
645 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
647 node
= node
->GetNext() )
649 wxMenuItem
* item
= node
->GetData();
650 if (item
->GetButtonWidget())
652 // This crashes because it uses gadgets
653 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, TRUE);
655 if (item
->GetSubMenu())
656 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
660 void wxMenu::SetForegroundColour(const wxColour
& col
)
662 m_foregroundColour
= col
;
664 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
666 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
668 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
670 node
= node
->GetNext() )
672 wxMenuItem
* item
= node
->GetData();
673 if (item
->GetButtonWidget())
675 // This crashes because it uses gadgets
676 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
678 if (item
->GetSubMenu())
679 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
683 void wxMenu::ChangeFont(bool keepOriginalSize
)
685 // lesstif 0.87 hangs when setting XmNfontList
686 #ifndef LESSTIF_VERSION
687 if (!m_font
.Ok() || !m_menuWidget
)
690 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) m_menuWidget
));
692 XtVaSetValues ((Widget
) m_menuWidget
,
693 XmNfontList
, fontList
,
697 XtVaSetValues ((Widget
) m_buttonWidget
,
698 XmNfontList
, fontList
,
702 for ( wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
704 node
= node
->GetNext() )
706 wxMenuItem
* item
= node
->GetData();
707 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
709 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
710 XmNfontList
, fontList
,
713 if (item
->GetSubMenu())
714 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
719 void wxMenu::SetFont(const wxFont
& font
)
725 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
727 m_backgroundColour
= col
;
729 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
731 size_t menuCount
= GetMenuCount();
732 for (size_t i
= 0; i
< menuCount
; i
++)
733 m_menus
[i
]->SetBackgroundColour((wxColour
&) col
);
738 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
740 m_foregroundColour
= col
;
742 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
744 size_t menuCount
= GetMenuCount();
745 for (size_t i
= 0; i
< menuCount
; i
++)
746 m_menus
[i
]->SetForegroundColour((wxColour
&) col
);
751 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
753 // Nothing to do for menubar, fonts are kept in wxMenus
756 bool wxMenuBar::SetFont(const wxFont
& font
)
761 size_t menuCount
= GetMenuCount();
762 for (size_t i
= 0; i
< menuCount
; i
++)
763 m_menus
[i
]->SetFont(font
);