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
36 #define XtDisplay XTDISPLAY
37 #define XtWindow XTWINDOW
40 #include <Xm/LabelG.h>
41 #include <Xm/CascadeBG.h>
42 #include <Xm/CascadeB.h>
43 #include <Xm/SeparatoG.h>
44 #include <Xm/PushBG.h>
45 #include <Xm/ToggleB.h>
46 #include <Xm/ToggleBG.h>
47 #include <Xm/RowColumn.h>
49 #pragma message enable nosimpint
52 #include "wx/motif/private.h"
54 // other standard headers
57 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
58 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
60 // ============================================================================
62 // ============================================================================
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // Construct a menu with optional title (then use append)
71 // Motif-specific members
73 m_menuWidget
= (WXWidget
) NULL
;
74 m_popupShell
= (WXWidget
) NULL
;
75 m_buttonWidget
= (WXWidget
) NULL
;
77 m_topLevelMenu
= (wxMenu
*) NULL
;
78 m_ownedByMenuBar
= false;
80 if ( !m_title
.empty() )
87 // The wxWindow destructor will take care of deleting the submenus.
98 // Not sure if this is right
99 if (m_menuParent
&& m_menuBar
)
111 // function appends a new item or submenu to the menu
112 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*pItem
)
114 return DoInsert(GetMenuItemCount(), pItem
);
117 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
119 item
->DestroyItem(true);
121 return wxMenuBase::DoRemove(item
);
124 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
128 // this is a dynamic Append
129 #ifndef XmNpositionIndex
130 wxCHECK_MSG( pos
== GetMenuItemCount(), -1, wxT("insert not implemented"));
132 item
->CreateItem(m_menuWidget
, GetMenuBar(), m_topLevelMenu
, pos
);
135 if ( item
->IsSubMenu() )
137 item
->GetSubMenu()->m_topLevelMenu
= m_topLevelMenu
;
140 return pos
== GetMenuItemCount() ? wxMenuBase::DoAppend(item
) :
141 wxMenuBase::DoInsert(pos
, item
);
144 void wxMenu::SetTitle(const wxString
& label
)
148 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
152 wxMenuItem
*item
= node
->GetData ();
153 Widget widget
= (Widget
) item
->GetButtonWidget();
157 wxXmString
title_str(label
);
158 XtVaSetValues(widget
,
159 XmNlabelString
, title_str(),
163 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
165 // Try the menu's event handler first
166 wxEvtHandler
* const handler
= GetEventHandler();
167 bool processed
= handler
? handler
->SafelyProcessEvent(event
) : false;
169 // Try the window the menu was popped up from (and up
170 // through the hierarchy)
171 if ( !processed
&& GetInvokingWindow())
172 processed
= GetInvokingWindow()->HandleWindowEvent(event
);
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 void wxMenuBar::Init()
183 m_eventHandler
= this;
184 m_menuBarFrame
= NULL
;
185 m_mainWidget
= (WXWidget
) NULL
;
188 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxArrayString
& titles
, long WXUNUSED(style
))
190 wxASSERT( n
== titles
.GetCount() );
195 for ( size_t i
= 0; i
< n
; i
++ )
196 m_menus
.Append(menus
[i
]);
199 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
203 for ( size_t i
= 0; i
< n
; i
++ )
205 m_menus
.Append(menus
[i
]);
206 m_titles
.Add(titles
[i
]);
210 wxMenuBar::~wxMenuBar()
212 // nothing to do: wxMenuBarBase will delete the menus
215 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
217 // wxFAIL_MSG("TODO");
218 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
221 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
223 wxMenu
*menu
= GetMenu(pos
);
227 Widget w
= (Widget
)menu
->GetButtonWidget();
230 wxXmString
label_str(label
);
233 XmNlabelString
, label_str(),
236 m_titles
[pos
] = label
;
239 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
241 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
242 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
243 return m_titles
[pos
];
246 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
248 return Insert(GetMenuCount(), menu
, title
);
251 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
253 wxCHECK_MSG( pos
<= GetMenuCount(), false, wxT("invalid position") );
254 wxCHECK_MSG( menu
, false, wxT("invalid menu") );
255 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), false,
256 wxT("menu already appended") );
258 if ( m_menuBarFrame
)
260 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
,
262 wxCHECK_MSG( w
, false, wxT("failed to create menu") );
263 menu
->SetButtonWidget(w
);
266 m_titles
.Insert(title
, pos
);
268 return wxMenuBarBase::Insert(pos
, menu
, title
);
271 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
273 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
276 wxFAIL_MSG(wxT("TODO"));
281 wxMenu
*wxMenuBar::Remove(size_t pos
)
283 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
287 if ( m_menuBarFrame
)
288 menu
->DestroyMenu(true);
290 menu
->SetMenuBar(NULL
);
292 m_titles
.RemoveAt(pos
);
297 // Find the menu menuString, item itemString, and return the item id.
298 // Returns -1 if none found.
299 int wxMenuBar::FindMenuItem(const wxString
& menuString
, const wxString
& itemString
) const
301 const wxString stripped
= wxStripMenuCodes(menuString
);
303 size_t menuCount
= GetMenuCount();
304 for (size_t i
= 0; i
< menuCount
; i
++)
306 if ( wxStripMenuCodes(m_titles
[i
]) == stripped
)
307 return m_menus
.Item(i
)->GetData()->FindItem (itemString
);
312 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
317 size_t menuCount
= GetMenuCount();
318 for (size_t i
= 0; i
< menuCount
; i
++)
320 wxMenuItem
*item
= m_menus
.Item(i
)->GetData()->FindItem(id
, itemMenu
);
321 if (item
) return item
;
328 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
330 m_parent
= parent
; // bleach... override it!
336 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
338 if (!XtIsManaged((Widget) m_mainWidget))
339 XtManageChild((Widget) m_mainWidget);
341 XtMapWidget((Widget
) m_mainWidget
);
345 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(),
346 wxMOTIF_STR("MenuBar"), NULL
, 0);
347 m_mainWidget
= (WXWidget
) menuBarW
;
349 size_t menuCount
= GetMenuCount();
350 for (size_t i
= 0; i
< menuCount
; i
++)
352 wxMenu
*menu
= GetMenu(i
);
353 wxString
title(m_titles
[i
]);
354 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, i
, title
, true));
356 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
357 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
359 // tear off menu support
360 #if (XmVersion >= 1002)
361 if ( menu
->IsTearOff() )
363 XtVaSetValues(GetWidget(menu
),
364 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
366 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
367 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
368 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, true);
375 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
376 XtRealizeWidget ((Widget
) menuBarW
);
377 XtManageChild ((Widget
) menuBarW
);
378 SetMenuBarFrame(parent
);
383 // Destroy menubar, but keep data structures intact so we can recreate it.
384 bool wxMenuBar::DestroyMenuBar()
388 SetMenuBarFrame((wxFrame
*) NULL
);
392 XtUnmanageChild ((Widget
) m_mainWidget
);
393 XtUnrealizeWidget ((Widget
) m_mainWidget
);
395 size_t menuCount
= GetMenuCount();
396 for (size_t i
= 0; i
< menuCount
; i
++)
398 wxMenu
*menu
= GetMenu(i
);
399 menu
->DestroyMenu(true);
402 XtDestroyWidget((Widget
) m_mainWidget
);
403 m_mainWidget
= (WXWidget
) 0;
405 SetMenuBarFrame((wxFrame
*) NULL
);
410 // Since PopupMenu under Motif stills grab right mouse button events
411 // after it was closed, we need to delete the associated widgets to
412 // allow next PopUpMenu to appear...
413 void wxMenu::DestroyWidgetAndDetach()
417 wxMenu
*menuParent
= GetParent();
420 wxMenuItemList::compatibility_iterator node
= menuParent
->GetMenuItems().GetFirst();
423 if ( node
->GetData()->GetSubMenu() == this )
425 delete node
->GetData();
426 menuParent
->GetMenuItems().Erase(node
);
431 node
= node
->GetNext();
438 // Mark as no longer popped up
443 * Create a popup or pulldown menu.
444 * Submenus of a popup will be pulldown.
448 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
,
451 size_t WXUNUSED(index
),
452 const wxString
& title
,
455 Widget menu
= (Widget
) 0;
456 Widget buttonWidget
= (Widget
) 0;
457 Display
* dpy
= XtDisplay((Widget
)parent
);
459 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
460 XtSetArg (args
[1], XmNpacking
, (m_numColumns
> 1) ? XmPACK_COLUMN
: XmPACK_TIGHT
);
465 m_font
= menuBar
->GetFont();
466 else if ( GetInvokingWindow() )
467 m_font
= GetInvokingWindow()->GetFont();
470 XtSetArg (args
[2], (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
) );
474 menu
= XmCreatePopupMenu ((Widget
) parent
, wxMOTIF_STR("popup"), args
, 3);
478 (XtCallbackProc
)wxMenuPopdownCallback
,
484 char mnem
= wxFindMnemonic (title
);
485 menu
= XmCreatePulldownMenu ((Widget
) parent
, wxMOTIF_STR("pulldown"), args
, 3);
487 wxString
title2(wxStripMenuCodes(title
));
488 wxXmString
label_str(title2
);
489 buttonWidget
= XtVaCreateManagedWidget(title2
,
491 xmCascadeButtonGadgetClass
, (Widget
) parent
,
493 xmCascadeButtonWidgetClass
, (Widget
) parent
,
495 XmNlabelString
, label_str(),
497 (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
498 XmNpositionIndex
, index
,
502 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
505 m_menuWidget
= (WXWidget
) menu
;
507 m_topLevelMenu
= topMenu
;
510 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
512 node
= node
->GetNext(), ++i
)
514 wxMenuItem
*item
= node
->GetData();
516 item
->CreateItem(menu
, menuBar
, topMenu
, i
);
524 // Destroys the Motif implementation of the menu,
525 // but maintains the wxWidgets data structures so we can
526 // do a CreateMenu again.
527 void wxMenu::DestroyMenu (bool full
)
529 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
531 node
= node
->GetNext() )
533 wxMenuItem
*item
= node
->GetData();
534 item
->SetMenuBar((wxMenuBar
*) NULL
);
536 item
->DestroyItem(full
);
543 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
544 XtDestroyWidget ((Widget
) m_buttonWidget
);
545 m_buttonWidget
= (WXWidget
) 0;
548 if (m_menuWidget
&& full
)
550 XtDestroyWidget((Widget
) m_menuWidget
);
551 m_menuWidget
= (WXWidget
) NULL
;
555 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
560 *it
= (wxMenuItem
*) NULL
;
561 return m_buttonWidget
;
564 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
566 node
= node
->GetNext() )
568 wxMenuItem
*item
= node
->GetData ();
569 if (item
->GetId() == id
)
573 return item
->GetButtonWidget();
576 if (item
->GetSubMenu())
578 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
587 *it
= (wxMenuItem
*) NULL
;
588 return (WXWidget
) NULL
;
591 void wxMenu::SetBackgroundColour(const wxColour
& col
)
593 m_backgroundColour
= col
;
597 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
599 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, true);
601 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
603 node
= node
->GetNext() )
605 wxMenuItem
* item
= node
->GetData();
606 if (item
->GetButtonWidget())
608 // This crashes because it uses gadgets
609 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, true);
611 if (item
->GetSubMenu())
612 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
616 void wxMenu::SetForegroundColour(const wxColour
& col
)
618 m_foregroundColour
= col
;
622 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
624 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
626 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
628 node
= node
->GetNext() )
630 wxMenuItem
* item
= node
->GetData();
631 if (item
->GetButtonWidget())
633 // This crashes because it uses gadgets
634 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
636 if (item
->GetSubMenu())
637 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
641 void wxMenu::ChangeFont(bool keepOriginalSize
)
643 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
644 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
645 if (!m_font
.Ok() || !m_menuWidget
)
648 Display
* dpy
= XtDisplay((Widget
) m_menuWidget
);
650 XtVaSetValues ((Widget
) m_menuWidget
,
651 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
655 XtVaSetValues ((Widget
) m_buttonWidget
,
656 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
660 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
662 node
= node
->GetNext() )
664 wxMenuItem
* item
= node
->GetData();
665 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
667 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
668 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
671 if (item
->GetSubMenu())
672 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
675 wxUnusedVar(keepOriginalSize
);
679 void wxMenu::SetFont(const wxFont
& font
)
685 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
687 if (!wxWindowBase::SetBackgroundColour(col
))
692 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
694 size_t menuCount
= GetMenuCount();
695 for (size_t i
= 0; i
< menuCount
; i
++)
696 m_menus
.Item(i
)->GetData()->SetBackgroundColour((wxColour
&) col
);
701 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
703 if (!wxWindowBase::SetForegroundColour(col
))
708 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
710 size_t menuCount
= GetMenuCount();
711 for (size_t i
= 0; i
< menuCount
; i
++)
712 m_menus
.Item(i
)->GetData()->SetForegroundColour((wxColour
&) col
);
717 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
719 // Nothing to do for menubar, fonts are kept in wxMenus
722 bool wxMenuBar::SetFont(const wxFont
& font
)
727 size_t menuCount
= GetMenuCount();
728 for (size_t i
= 0; i
< menuCount
; i
++)
729 m_menus
.Item(i
)->GetData()->SetFont(font
);