1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/menu.cpp
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
)
117 return DoInsert(GetMenuItemCount(), pItem
);
120 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
122 item
->DestroyItem(true);
124 return wxMenuBase::DoRemove(item
);
127 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
131 // this is a dynamic Append
132 #ifndef XmNpositionIndex
133 wxCHECK_MSG( pos
== GetMenuItemCount(), -1, wxT("insert not implemented"));
135 item
->CreateItem(m_menuWidget
, GetMenuBar(), m_topLevelMenu
, pos
);
138 if ( item
->IsSubMenu() )
140 item
->GetSubMenu()->m_topLevelMenu
= m_topLevelMenu
;
143 return pos
== GetMenuItemCount() ? wxMenuBase::DoAppend(item
) :
144 wxMenuBase::DoInsert(pos
, item
);
147 void wxMenu::SetTitle(const wxString
& label
)
151 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
155 wxMenuItem
*item
= node
->GetData ();
156 Widget widget
= (Widget
) item
->GetButtonWidget();
160 wxXmString
title_str(label
);
161 XtVaSetValues(widget
,
162 XmNlabelString
, title_str(),
166 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
168 bool processed
= false;
170 // Try the menu's event handler
171 if ( !processed
&& GetEventHandler())
173 processed
= GetEventHandler()->ProcessEvent(event
);
175 // Try the window the menu was popped up from (and up
176 // through the hierarchy)
177 if ( !processed
&& GetInvokingWindow())
178 processed
= GetInvokingWindow()->ProcessEvent(event
);
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 void wxMenuBar::Init()
189 m_eventHandler
= this;
190 m_menuBarFrame
= NULL
;
191 m_mainWidget
= (WXWidget
) NULL
;
192 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
193 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
194 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
197 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxArrayString
& titles
, long WXUNUSED(style
))
199 wxASSERT( n
== titles
.GetCount() );
204 for ( size_t i
= 0; i
< n
; i
++ )
205 m_menus
.Append(menus
[i
]);
208 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
212 for ( size_t i
= 0; i
< n
; i
++ )
214 m_menus
.Append(menus
[i
]);
215 m_titles
.Add(titles
[i
]);
219 wxMenuBar::~wxMenuBar()
221 // nothing to do: wxMenuBarBase will delete the menus
224 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
226 // wxFAIL_MSG("TODO");
227 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
230 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
232 wxMenu
*menu
= GetMenu(pos
);
236 Widget w
= (Widget
)menu
->GetButtonWidget();
239 wxXmString
label_str(label
);
242 XmNlabelString
, label_str(),
247 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
249 wxMenu
*menu
= GetMenu(pos
);
252 Widget w
= (Widget
)menu
->GetButtonWidget();
257 XmNlabelString
, &text
,
260 return wxXmStringToString( text
);
264 return wxEmptyString
;
267 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
269 return Insert(GetMenuCount(), menu
, title
);
272 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
274 wxCHECK_MSG( pos
<= GetMenuCount(), false, wxT("invalid position") );
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
,
283 wxCHECK_MSG( w
, false, wxT("failed to create menu") );
284 menu
->SetButtonWidget(w
);
287 m_titles
.Insert(title
, pos
);
289 return wxMenuBarBase::Insert(pos
, menu
, title
);
292 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
294 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
297 wxFAIL_MSG(wxT("TODO"));
302 wxMenu
*wxMenuBar::Remove(size_t pos
)
304 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
308 if ( m_menuBarFrame
)
309 menu
->DestroyMenu(true);
311 menu
->SetMenuBar(NULL
);
313 m_titles
.RemoveAt(pos
);
318 // Find the menu menuString, item itemString, and return the item id.
319 // Returns -1 if none found.
320 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
324 wxStripMenuCodes (wxConstCast(menuString
.c_str(), char), buf1
);
326 size_t menuCount
= GetMenuCount();
327 for (size_t i
= 0; i
< menuCount
; i
++)
329 wxStripMenuCodes (wxConstCast(m_titles
[i
].c_str(), char), buf2
);
330 if (strcmp (buf1
, buf2
) == 0)
331 return m_menus
.Item(i
)->GetData()->FindItem (itemString
);
336 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
341 size_t menuCount
= GetMenuCount();
342 for (size_t i
= 0; i
< menuCount
; i
++)
344 wxMenuItem
*item
= m_menus
.Item(i
)->GetData()->FindItem(id
, itemMenu
);
345 if (item
) return item
;
352 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
356 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
358 if (!XtIsManaged((Widget) m_mainWidget))
359 XtManageChild((Widget) m_mainWidget);
361 XtMapWidget((Widget
) m_mainWidget
);
365 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(),
366 wxMOTIF_STR("MenuBar"), NULL
, 0);
367 m_mainWidget
= (WXWidget
) menuBarW
;
369 size_t menuCount
= GetMenuCount();
370 for (size_t i
= 0; i
< menuCount
; i
++)
372 wxMenu
*menu
= GetMenu(i
);
373 wxString
title(m_titles
[i
]);
374 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, i
, title
, true));
376 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
377 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
379 // tear off menu support
380 #if (XmVersion >= 1002)
381 if ( menu
->IsTearOff() )
383 XtVaSetValues(GetWidget(menu
),
384 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
386 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
387 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
388 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, true);
393 SetBackgroundColour(m_backgroundColour
);
394 SetForegroundColour(m_foregroundColour
);
397 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
398 XtRealizeWidget ((Widget
) menuBarW
);
399 XtManageChild ((Widget
) menuBarW
);
400 SetMenuBarFrame(parent
);
405 // Destroy menubar, but keep data structures intact so we can recreate it.
406 bool wxMenuBar::DestroyMenuBar()
410 SetMenuBarFrame((wxFrame
*) NULL
);
414 XtUnmanageChild ((Widget
) m_mainWidget
);
415 XtUnrealizeWidget ((Widget
) m_mainWidget
);
417 size_t menuCount
= GetMenuCount();
418 for (size_t i
= 0; i
< menuCount
; i
++)
420 wxMenu
*menu
= GetMenu(i
);
421 menu
->DestroyMenu(true);
424 XtDestroyWidget((Widget
) m_mainWidget
);
425 m_mainWidget
= (WXWidget
) 0;
427 SetMenuBarFrame((wxFrame
*) NULL
);
432 // Since PopupMenu under Motif stills grab right mouse button events
433 // after it was closed, we need to delete the associated widgets to
434 // allow next PopUpMenu to appear...
435 void wxMenu::DestroyWidgetAndDetach()
439 wxMenu
*menuParent
= GetParent();
442 wxMenuItemList::compatibility_iterator node
= menuParent
->GetMenuItems().GetFirst();
445 if ( node
->GetData()->GetSubMenu() == this )
447 delete node
->GetData();
448 menuParent
->GetMenuItems().Erase(node
);
453 node
= node
->GetNext();
460 // Mark as no longer popped up
465 * Create a popup or pulldown menu.
466 * Submenus of a popup will be pulldown.
470 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
,
473 size_t WXUNUSED(index
),
474 const wxString
& title
,
477 Widget menu
= (Widget
) 0;
478 Widget buttonWidget
= (Widget
) 0;
480 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
481 XtSetArg (args
[1], XmNpacking
, (m_numColumns
> 1) ? XmPACK_COLUMN
: XmPACK_TIGHT
);
485 menu
= XmCreatePopupMenu ((Widget
) parent
, wxMOTIF_STR("popup"), args
, 2);
489 (XtCallbackProc
)wxMenuPopdownCallback
,
495 char mnem
= wxFindMnemonic (title
);
496 menu
= XmCreatePulldownMenu ((Widget
) parent
, wxMOTIF_STR("pulldown"), args
, 2);
498 wxString
title2(wxStripMenuCodes(title
));
499 wxXmString
label_str(title2
);
500 buttonWidget
= XtVaCreateManagedWidget(title2
,
502 xmCascadeButtonGadgetClass
, (Widget
) parent
,
504 xmCascadeButtonWidgetClass
, (Widget
) parent
,
506 XmNlabelString
, label_str(),
511 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
514 m_menuWidget
= (WXWidget
) menu
;
516 m_topLevelMenu
= topMenu
;
519 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
521 node
= node
->GetNext(), ++i
)
523 wxMenuItem
*item
= node
->GetData();
525 item
->CreateItem(menu
, menuBar
, topMenu
, i
);
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
);
682 wxUnusedVar(keepOriginalSize
);
686 void wxMenu::SetFont(const wxFont
& font
)
692 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
694 m_backgroundColour
= col
;
696 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
698 size_t menuCount
= GetMenuCount();
699 for (size_t i
= 0; i
< menuCount
; i
++)
700 m_menus
.Item(i
)->GetData()->SetBackgroundColour((wxColour
&) col
);
705 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
707 m_foregroundColour
= col
;
709 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
711 size_t menuCount
= GetMenuCount();
712 for (size_t i
= 0; i
< menuCount
; i
++)
713 m_menus
.Item(i
)->GetData()->SetForegroundColour((wxColour
&) col
);
718 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
720 // Nothing to do for menubar, fonts are kept in wxMenus
723 bool wxMenuBar::SetFont(const wxFont
& font
)
728 size_t menuCount
= GetMenuCount();
729 for (size_t i
= 0; i
< menuCount
; i
++)
730 m_menus
.Item(i
)->GetData()->SetFont(font
);