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 bool processed
= false;
167 // Try the menu's event handler
168 if ( !processed
&& GetEventHandler())
170 processed
= GetEventHandler()->ProcessEvent(event
);
172 // Try the window the menu was popped up from (and up
173 // through the hierarchy)
174 if ( !processed
&& GetInvokingWindow())
175 processed
= GetInvokingWindow()->ProcessEvent(event
);
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 void wxMenuBar::Init()
186 m_eventHandler
= this;
187 m_menuBarFrame
= NULL
;
188 m_mainWidget
= (WXWidget
) NULL
;
191 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxArrayString
& titles
, long WXUNUSED(style
))
193 wxASSERT( n
== titles
.GetCount() );
198 for ( size_t i
= 0; i
< n
; i
++ )
199 m_menus
.Append(menus
[i
]);
202 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
206 for ( size_t i
= 0; i
< n
; i
++ )
208 m_menus
.Append(menus
[i
]);
209 m_titles
.Add(titles
[i
]);
213 wxMenuBar::~wxMenuBar()
215 // nothing to do: wxMenuBarBase will delete the menus
218 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
220 // wxFAIL_MSG("TODO");
221 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
224 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
226 wxMenu
*menu
= GetMenu(pos
);
230 Widget w
= (Widget
)menu
->GetButtonWidget();
233 wxXmString
label_str(label
);
236 XmNlabelString
, label_str(),
241 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
243 wxMenu
*menu
= GetMenu(pos
);
246 Widget w
= (Widget
)menu
->GetButtonWidget();
251 XmNlabelString
, &text
,
254 return wxXmStringToString( text
);
258 return wxEmptyString
;
261 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
263 return Insert(GetMenuCount(), menu
, title
);
266 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
268 wxCHECK_MSG( pos
<= GetMenuCount(), false, wxT("invalid position") );
269 wxCHECK_MSG( menu
, false, wxT("invalid menu") );
270 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), false,
271 wxT("menu already appended") );
273 if ( m_menuBarFrame
)
275 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
,
277 wxCHECK_MSG( w
, false, wxT("failed to create menu") );
278 menu
->SetButtonWidget(w
);
281 m_titles
.Insert(title
, pos
);
283 return wxMenuBarBase::Insert(pos
, menu
, title
);
286 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
288 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
291 wxFAIL_MSG(wxT("TODO"));
296 wxMenu
*wxMenuBar::Remove(size_t pos
)
298 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
302 if ( m_menuBarFrame
)
303 menu
->DestroyMenu(true);
305 menu
->SetMenuBar(NULL
);
307 m_titles
.RemoveAt(pos
);
312 // Find the menu menuString, item itemString, and return the item id.
313 // Returns -1 if none found.
314 int wxMenuBar::FindMenuItem(const wxString
& menuString
, const wxString
& itemString
) const
316 const wxString stripped
= wxStripMenuCodes(menuString
);
318 size_t menuCount
= GetMenuCount();
319 for (size_t i
= 0; i
< menuCount
; i
++)
321 if ( wxStripMenuCodes(m_titles
[i
]) == stripped
)
322 return m_menus
.Item(i
)->GetData()->FindItem (itemString
);
327 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
332 size_t menuCount
= GetMenuCount();
333 for (size_t i
= 0; i
< menuCount
; i
++)
335 wxMenuItem
*item
= m_menus
.Item(i
)->GetData()->FindItem(id
, itemMenu
);
336 if (item
) return item
;
343 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
345 m_parent
= parent
; // bleach... override it!
351 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
353 if (!XtIsManaged((Widget) m_mainWidget))
354 XtManageChild((Widget) m_mainWidget);
356 XtMapWidget((Widget
) m_mainWidget
);
360 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(),
361 wxMOTIF_STR("MenuBar"), NULL
, 0);
362 m_mainWidget
= (WXWidget
) menuBarW
;
364 size_t menuCount
= GetMenuCount();
365 for (size_t i
= 0; i
< menuCount
; i
++)
367 wxMenu
*menu
= GetMenu(i
);
368 wxString
title(m_titles
[i
]);
369 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, i
, title
, true));
371 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
372 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
374 // tear off menu support
375 #if (XmVersion >= 1002)
376 if ( menu
->IsTearOff() )
378 XtVaSetValues(GetWidget(menu
),
379 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
381 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
382 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
383 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, true);
390 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
391 XtRealizeWidget ((Widget
) menuBarW
);
392 XtManageChild ((Widget
) menuBarW
);
393 SetMenuBarFrame(parent
);
398 // Destroy menubar, but keep data structures intact so we can recreate it.
399 bool wxMenuBar::DestroyMenuBar()
403 SetMenuBarFrame((wxFrame
*) NULL
);
407 XtUnmanageChild ((Widget
) m_mainWidget
);
408 XtUnrealizeWidget ((Widget
) m_mainWidget
);
410 size_t menuCount
= GetMenuCount();
411 for (size_t i
= 0; i
< menuCount
; i
++)
413 wxMenu
*menu
= GetMenu(i
);
414 menu
->DestroyMenu(true);
417 XtDestroyWidget((Widget
) m_mainWidget
);
418 m_mainWidget
= (WXWidget
) 0;
420 SetMenuBarFrame((wxFrame
*) NULL
);
425 // Since PopupMenu under Motif stills grab right mouse button events
426 // after it was closed, we need to delete the associated widgets to
427 // allow next PopUpMenu to appear...
428 void wxMenu::DestroyWidgetAndDetach()
432 wxMenu
*menuParent
= GetParent();
435 wxMenuItemList::compatibility_iterator node
= menuParent
->GetMenuItems().GetFirst();
438 if ( node
->GetData()->GetSubMenu() == this )
440 delete node
->GetData();
441 menuParent
->GetMenuItems().Erase(node
);
446 node
= node
->GetNext();
453 // Mark as no longer popped up
458 * Create a popup or pulldown menu.
459 * Submenus of a popup will be pulldown.
463 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
,
466 size_t WXUNUSED(index
),
467 const wxString
& title
,
470 Widget menu
= (Widget
) 0;
471 Widget buttonWidget
= (Widget
) 0;
472 Display
* dpy
= XtDisplay((Widget
)parent
);
474 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
475 XtSetArg (args
[1], XmNpacking
, (m_numColumns
> 1) ? XmPACK_COLUMN
: XmPACK_TIGHT
);
480 m_font
= menuBar
->GetFont();
481 else if ( GetInvokingWindow() )
482 m_font
= GetInvokingWindow()->GetFont();
485 XtSetArg (args
[2], (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
) );
489 menu
= XmCreatePopupMenu ((Widget
) parent
, wxMOTIF_STR("popup"), args
, 3);
493 (XtCallbackProc
)wxMenuPopdownCallback
,
499 char mnem
= wxFindMnemonic (title
);
500 menu
= XmCreatePulldownMenu ((Widget
) parent
, wxMOTIF_STR("pulldown"), args
, 3);
502 wxString
title2(wxStripMenuCodes(title
));
503 wxXmString
label_str(title2
);
504 buttonWidget
= XtVaCreateManagedWidget(title2
,
506 xmCascadeButtonGadgetClass
, (Widget
) parent
,
508 xmCascadeButtonWidgetClass
, (Widget
) parent
,
510 XmNlabelString
, label_str(),
512 (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
513 XmNpositionIndex
, index
,
517 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
520 m_menuWidget
= (WXWidget
) menu
;
522 m_topLevelMenu
= topMenu
;
525 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
527 node
= node
->GetNext(), ++i
)
529 wxMenuItem
*item
= node
->GetData();
531 item
->CreateItem(menu
, menuBar
, topMenu
, i
);
539 // Destroys the Motif implementation of the menu,
540 // but maintains the wxWidgets data structures so we can
541 // do a CreateMenu again.
542 void wxMenu::DestroyMenu (bool full
)
544 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
546 node
= node
->GetNext() )
548 wxMenuItem
*item
= node
->GetData();
549 item
->SetMenuBar((wxMenuBar
*) NULL
);
551 item
->DestroyItem(full
);
558 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
559 XtDestroyWidget ((Widget
) m_buttonWidget
);
560 m_buttonWidget
= (WXWidget
) 0;
563 if (m_menuWidget
&& full
)
565 XtDestroyWidget((Widget
) m_menuWidget
);
566 m_menuWidget
= (WXWidget
) NULL
;
570 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
575 *it
= (wxMenuItem
*) NULL
;
576 return m_buttonWidget
;
579 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
581 node
= node
->GetNext() )
583 wxMenuItem
*item
= node
->GetData ();
584 if (item
->GetId() == id
)
588 return item
->GetButtonWidget();
591 if (item
->GetSubMenu())
593 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
602 *it
= (wxMenuItem
*) NULL
;
603 return (WXWidget
) NULL
;
606 void wxMenu::SetBackgroundColour(const wxColour
& col
)
608 m_backgroundColour
= col
;
612 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
614 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, true);
616 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
618 node
= node
->GetNext() )
620 wxMenuItem
* item
= node
->GetData();
621 if (item
->GetButtonWidget())
623 // This crashes because it uses gadgets
624 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, true);
626 if (item
->GetSubMenu())
627 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
631 void wxMenu::SetForegroundColour(const wxColour
& col
)
633 m_foregroundColour
= col
;
637 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
639 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
641 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
643 node
= node
->GetNext() )
645 wxMenuItem
* item
= node
->GetData();
646 if (item
->GetButtonWidget())
648 // This crashes because it uses gadgets
649 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
651 if (item
->GetSubMenu())
652 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
656 void wxMenu::ChangeFont(bool keepOriginalSize
)
658 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
659 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
660 if (!m_font
.Ok() || !m_menuWidget
)
663 Display
* dpy
= XtDisplay((Widget
) m_menuWidget
);
665 XtVaSetValues ((Widget
) m_menuWidget
,
666 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
670 XtVaSetValues ((Widget
) m_buttonWidget
,
671 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
675 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
677 node
= node
->GetNext() )
679 wxMenuItem
* item
= node
->GetData();
680 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
682 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
683 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
686 if (item
->GetSubMenu())
687 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
690 wxUnusedVar(keepOriginalSize
);
694 void wxMenu::SetFont(const wxFont
& font
)
700 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
702 if (!wxWindowBase::SetBackgroundColour(col
))
707 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
709 size_t menuCount
= GetMenuCount();
710 for (size_t i
= 0; i
< menuCount
; i
++)
711 m_menus
.Item(i
)->GetData()->SetBackgroundColour((wxColour
&) col
);
716 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
718 if (!wxWindowBase::SetForegroundColour(col
))
723 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
725 size_t menuCount
= GetMenuCount();
726 for (size_t i
= 0; i
< menuCount
; i
++)
727 m_menus
.Item(i
)->GetData()->SetForegroundColour((wxColour
&) col
);
732 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
734 // Nothing to do for menubar, fonts are kept in wxMenus
737 bool wxMenuBar::SetFont(const wxFont
& font
)
742 size_t menuCount
= GetMenuCount();
743 for (size_t i
= 0; i
< menuCount
; i
++)
744 m_menus
.Item(i
)->GetData()->SetFont(font
);