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"
30 #include "wx/settings.h"
31 #include "wx/menuitem.h"
35 #pragma message disable nosimpint
38 #include <Xm/LabelG.h>
39 #include <Xm/CascadeBG.h>
40 #include <Xm/CascadeB.h>
41 #include <Xm/SeparatoG.h>
42 #include <Xm/PushBG.h>
43 #include <Xm/ToggleB.h>
44 #include <Xm/ToggleBG.h>
45 #include <Xm/RowColumn.h>
47 #pragma message enable nosimpint
50 #include "wx/motif/private.h"
52 // other standard headers
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // Construct a menu with optional title (then use append)
66 // Motif-specific members
68 m_menuWidget
= (WXWidget
) NULL
;
69 m_popupShell
= (WXWidget
) NULL
;
70 m_buttonWidget
= (WXWidget
) NULL
;
72 m_topLevelMenu
= NULL
;
73 m_ownedByMenuBar
= false;
75 if ( !m_title
.empty() )
82 // The wxWindow destructor will take care of deleting the submenus.
93 // Not sure if this is right
94 if (m_menuParent
&& m_menuBar
)
106 // function appends a new item or submenu to the menu
107 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*pItem
)
109 return DoInsert(GetMenuItemCount(), pItem
);
112 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
114 item
->DestroyItem(true);
116 return wxMenuBase::DoRemove(item
);
119 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
123 // this is a dynamic Append
124 #ifndef XmNpositionIndex
125 wxCHECK_MSG( pos
== GetMenuItemCount(), -1, wxT("insert not implemented"));
127 item
->CreateItem(m_menuWidget
, GetMenuBar(), m_topLevelMenu
, pos
);
130 if ( item
->IsSubMenu() )
132 item
->GetSubMenu()->m_topLevelMenu
= m_topLevelMenu
;
135 return pos
== GetMenuItemCount() ? wxMenuBase::DoAppend(item
) :
136 wxMenuBase::DoInsert(pos
, item
);
139 void wxMenu::SetTitle(const wxString
& label
)
143 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
147 wxMenuItem
*item
= node
->GetData ();
148 Widget widget
= (Widget
) item
->GetButtonWidget();
152 wxXmString
title_str(label
);
153 XtVaSetValues(widget
,
154 XmNlabelString
, title_str(),
158 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
160 // Try the menu's event handler first
161 wxEvtHandler
* const handler
= GetEventHandler();
162 bool processed
= handler
? handler
->SafelyProcessEvent(event
) : false;
164 // Try the window the menu was popped up from (and up
165 // through the hierarchy)
166 if ( !processed
&& GetInvokingWindow())
167 processed
= GetInvokingWindow()->HandleWindowEvent(event
);
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 void wxMenuBar::Init()
178 m_eventHandler
= this;
179 m_menuBarFrame
= NULL
;
180 m_mainWidget
= (WXWidget
) NULL
;
183 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxArrayString
& titles
, long WXUNUSED(style
))
185 wxASSERT( n
== titles
.GetCount() );
190 for ( size_t i
= 0; i
< n
; i
++ )
191 m_menus
.Append(menus
[i
]);
194 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
198 for ( size_t i
= 0; i
< n
; i
++ )
200 m_menus
.Append(menus
[i
]);
201 m_titles
.Add(titles
[i
]);
205 wxMenuBar::~wxMenuBar()
207 // nothing to do: wxMenuBarBase will delete the menus
210 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
212 // wxFAIL_MSG("TODO");
213 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
216 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
218 wxMenu
*menu
= GetMenu(pos
);
222 Widget w
= (Widget
)menu
->GetButtonWidget();
225 wxXmString
label_str(label
);
228 XmNlabelString
, label_str(),
231 m_titles
[pos
] = label
;
234 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
236 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
237 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
238 return m_titles
[pos
];
241 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
243 return Insert(GetMenuCount(), menu
, title
);
246 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
248 wxCHECK_MSG( pos
<= GetMenuCount(), false, wxT("invalid position") );
249 wxCHECK_MSG( menu
, false, wxT("invalid menu") );
250 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), false,
251 wxT("menu already appended") );
253 if ( m_menuBarFrame
)
255 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
,
257 wxCHECK_MSG( w
, false, wxT("failed to create menu") );
258 menu
->SetButtonWidget(w
);
261 m_titles
.Insert(title
, pos
);
263 return wxMenuBarBase::Insert(pos
, menu
, title
);
266 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
268 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
271 wxFAIL_MSG(wxT("TODO"));
276 wxMenu
*wxMenuBar::Remove(size_t pos
)
278 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
282 if ( m_menuBarFrame
)
283 menu
->DestroyMenu(true);
285 menu
->SetMenuBar(NULL
);
287 m_titles
.RemoveAt(pos
);
292 // Find the menu menuString, item itemString, and return the item id.
293 // Returns -1 if none found.
294 int wxMenuBar::FindMenuItem(const wxString
& menuString
, const wxString
& itemString
) const
296 const wxString stripped
= wxStripMenuCodes(menuString
);
298 size_t menuCount
= GetMenuCount();
299 for (size_t i
= 0; i
< menuCount
; i
++)
301 if ( wxStripMenuCodes(m_titles
[i
]) == stripped
)
302 return m_menus
.Item(i
)->GetData()->FindItem (itemString
);
307 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
312 size_t menuCount
= GetMenuCount();
313 for (size_t i
= 0; i
< menuCount
; i
++)
315 wxMenuItem
*item
= m_menus
.Item(i
)->GetData()->FindItem(id
, itemMenu
);
316 if (item
) return item
;
323 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
325 m_parent
= parent
; // bleach... override it!
331 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
333 if (!XtIsManaged((Widget) m_mainWidget))
334 XtManageChild((Widget) m_mainWidget);
336 XtMapWidget((Widget
) m_mainWidget
);
340 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(),
341 wxMOTIF_STR("MenuBar"), NULL
, 0);
342 m_mainWidget
= (WXWidget
) menuBarW
;
344 size_t menuCount
= GetMenuCount();
345 for (size_t i
= 0; i
< menuCount
; i
++)
347 wxMenu
*menu
= GetMenu(i
);
348 wxString
title(m_titles
[i
]);
349 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, i
, title
, true));
351 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
352 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
354 // tear off menu support
355 #if (XmVersion >= 1002)
356 if ( menu
->IsTearOff() )
358 XtVaSetValues(GetWidget(menu
),
359 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
361 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
362 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
363 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, true);
370 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
371 XtRealizeWidget ((Widget
) menuBarW
);
372 XtManageChild ((Widget
) menuBarW
);
373 SetMenuBarFrame(parent
);
378 // Destroy menubar, but keep data structures intact so we can recreate it.
379 bool wxMenuBar::DestroyMenuBar()
383 SetMenuBarFrame(NULL
);
387 XtUnmanageChild ((Widget
) m_mainWidget
);
388 XtUnrealizeWidget ((Widget
) m_mainWidget
);
390 size_t menuCount
= GetMenuCount();
391 for (size_t i
= 0; i
< menuCount
; i
++)
393 wxMenu
*menu
= GetMenu(i
);
394 menu
->DestroyMenu(true);
397 XtDestroyWidget((Widget
) m_mainWidget
);
398 m_mainWidget
= (WXWidget
) 0;
400 SetMenuBarFrame(NULL
);
405 // Since PopupMenu under Motif stills grab right mouse button events
406 // after it was closed, we need to delete the associated widgets to
407 // allow next PopUpMenu to appear...
408 void wxMenu::DestroyWidgetAndDetach()
412 wxMenu
*menuParent
= GetParent();
415 wxMenuItemList::compatibility_iterator node
= menuParent
->GetMenuItems().GetFirst();
418 if ( node
->GetData()->GetSubMenu() == this )
420 delete node
->GetData();
421 menuParent
->GetMenuItems().Erase(node
);
426 node
= node
->GetNext();
433 // Mark as no longer popped up
438 * Create a popup or pulldown menu.
439 * Submenus of a popup will be pulldown.
443 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
,
447 const wxString
& title
,
450 Widget menu
= (Widget
) 0;
451 Widget buttonWidget
= (Widget
) 0;
452 Display
* dpy
= XtDisplay((Widget
)parent
);
454 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
455 XtSetArg (args
[1], XmNpacking
, (m_numColumns
> 1) ? XmPACK_COLUMN
: XmPACK_TIGHT
);
460 m_font
= menuBar
->GetFont();
461 else if ( GetInvokingWindow() )
462 m_font
= GetInvokingWindow()->GetFont();
465 XtSetArg (args
[2], (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
) );
469 menu
= XmCreatePopupMenu ((Widget
) parent
, wxMOTIF_STR("popup"), args
, 3);
473 (XtCallbackProc
)wxMenuPopdownCallback
,
479 char mnem
= wxFindMnemonic (title
);
480 menu
= XmCreatePulldownMenu ((Widget
) parent
, wxMOTIF_STR("pulldown"), args
, 3);
482 wxString
title2(wxStripMenuCodes(title
));
483 wxXmString
label_str(title2
);
484 buttonWidget
= XtVaCreateManagedWidget(title2
,
486 xmCascadeButtonGadgetClass
, (Widget
) parent
,
488 xmCascadeButtonWidgetClass
, (Widget
) parent
,
490 XmNlabelString
, label_str(),
492 (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
493 XmNpositionIndex
, menuIndex
,
497 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
500 m_menuWidget
= (WXWidget
) menu
;
502 m_topLevelMenu
= topMenu
;
505 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
507 node
= node
->GetNext(), ++i
)
509 wxMenuItem
*item
= node
->GetData();
511 item
->CreateItem(menu
, menuBar
, topMenu
, i
);
519 // Destroys the Motif implementation of the menu,
520 // but maintains the wxWidgets data structures so we can
521 // do a CreateMenu again.
522 void wxMenu::DestroyMenu (bool full
)
524 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
526 node
= node
->GetNext() )
528 wxMenuItem
*item
= node
->GetData();
529 item
->SetMenuBar(NULL
);
531 item
->DestroyItem(full
);
538 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
539 XtDestroyWidget ((Widget
) m_buttonWidget
);
540 m_buttonWidget
= (WXWidget
) 0;
543 if (m_menuWidget
&& full
)
545 XtDestroyWidget((Widget
) m_menuWidget
);
546 m_menuWidget
= (WXWidget
) NULL
;
550 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
556 return m_buttonWidget
;
559 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
561 node
= node
->GetNext() )
563 wxMenuItem
*item
= node
->GetData ();
564 if (item
->GetId() == id
)
568 return item
->GetButtonWidget();
571 if (item
->GetSubMenu())
573 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
583 return (WXWidget
) NULL
;
586 void wxMenu::SetBackgroundColour(const wxColour
& col
)
588 m_backgroundColour
= col
;
592 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
594 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, true);
596 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
598 node
= node
->GetNext() )
600 wxMenuItem
* item
= node
->GetData();
601 if (item
->GetButtonWidget())
603 // This crashes because it uses gadgets
604 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, true);
606 if (item
->GetSubMenu())
607 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
611 void wxMenu::SetForegroundColour(const wxColour
& col
)
613 m_foregroundColour
= col
;
617 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
619 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
621 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
623 node
= node
->GetNext() )
625 wxMenuItem
* item
= node
->GetData();
626 if (item
->GetButtonWidget())
628 // This crashes because it uses gadgets
629 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
631 if (item
->GetSubMenu())
632 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
636 void wxMenu::ChangeFont(bool keepOriginalSize
)
638 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
639 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
640 if (!m_font
.Ok() || !m_menuWidget
)
643 Display
* dpy
= XtDisplay((Widget
) m_menuWidget
);
645 XtVaSetValues ((Widget
) m_menuWidget
,
646 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
650 XtVaSetValues ((Widget
) m_buttonWidget
,
651 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
655 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
657 node
= node
->GetNext() )
659 wxMenuItem
* item
= node
->GetData();
660 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
662 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
663 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
666 if (item
->GetSubMenu())
667 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
670 wxUnusedVar(keepOriginalSize
);
674 void wxMenu::SetFont(const wxFont
& font
)
680 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
682 if (!wxWindowBase::SetBackgroundColour(col
))
687 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
689 size_t menuCount
= GetMenuCount();
690 for (size_t i
= 0; i
< menuCount
; i
++)
691 m_menus
.Item(i
)->GetData()->SetBackgroundColour((wxColour
&) col
);
696 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
698 if (!wxWindowBase::SetForegroundColour(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
);