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::SetMenuLabel(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(),
242 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
244 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
245 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
246 return m_titles
[pos
];
249 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
251 return Insert(GetMenuCount(), menu
, title
);
254 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
256 wxCHECK_MSG( pos
<= GetMenuCount(), false, wxT("invalid position") );
257 wxCHECK_MSG( menu
, false, wxT("invalid menu") );
258 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), false,
259 wxT("menu already appended") );
261 if ( m_menuBarFrame
)
263 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
,
265 wxCHECK_MSG( w
, false, wxT("failed to create menu") );
266 menu
->SetButtonWidget(w
);
269 m_titles
.Insert(title
, pos
);
271 return wxMenuBarBase::Insert(pos
, menu
, title
);
274 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
276 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
279 wxFAIL_MSG(wxT("TODO"));
284 wxMenu
*wxMenuBar::Remove(size_t pos
)
286 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
290 if ( m_menuBarFrame
)
291 menu
->DestroyMenu(true);
293 menu
->SetMenuBar(NULL
);
295 m_titles
.RemoveAt(pos
);
300 // Find the menu menuString, item itemString, and return the item id.
301 // Returns -1 if none found.
302 int wxMenuBar::FindMenuItem(const wxString
& menuString
, const wxString
& itemString
) const
304 const wxString stripped
= wxStripMenuCodes(menuString
);
306 size_t menuCount
= GetMenuCount();
307 for (size_t i
= 0; i
< menuCount
; i
++)
309 if ( wxStripMenuCodes(m_titles
[i
]) == stripped
)
310 return m_menus
.Item(i
)->GetData()->FindItem (itemString
);
315 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
320 size_t menuCount
= GetMenuCount();
321 for (size_t i
= 0; i
< menuCount
; i
++)
323 wxMenuItem
*item
= m_menus
.Item(i
)->GetData()->FindItem(id
, itemMenu
);
324 if (item
) return item
;
331 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
333 m_parent
= parent
; // bleach... override it!
339 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
341 if (!XtIsManaged((Widget) m_mainWidget))
342 XtManageChild((Widget) m_mainWidget);
344 XtMapWidget((Widget
) m_mainWidget
);
348 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(),
349 wxMOTIF_STR("MenuBar"), NULL
, 0);
350 m_mainWidget
= (WXWidget
) menuBarW
;
352 size_t menuCount
= GetMenuCount();
353 for (size_t i
= 0; i
< menuCount
; i
++)
355 wxMenu
*menu
= GetMenu(i
);
356 wxString
title(m_titles
[i
]);
357 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, i
, title
, true));
359 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
360 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
362 // tear off menu support
363 #if (XmVersion >= 1002)
364 if ( menu
->IsTearOff() )
366 XtVaSetValues(GetWidget(menu
),
367 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
369 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
370 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
371 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, true);
378 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
379 XtRealizeWidget ((Widget
) menuBarW
);
380 XtManageChild ((Widget
) menuBarW
);
381 SetMenuBarFrame(parent
);
386 // Destroy menubar, but keep data structures intact so we can recreate it.
387 bool wxMenuBar::DestroyMenuBar()
391 SetMenuBarFrame((wxFrame
*) NULL
);
395 XtUnmanageChild ((Widget
) m_mainWidget
);
396 XtUnrealizeWidget ((Widget
) m_mainWidget
);
398 size_t menuCount
= GetMenuCount();
399 for (size_t i
= 0; i
< menuCount
; i
++)
401 wxMenu
*menu
= GetMenu(i
);
402 menu
->DestroyMenu(true);
405 XtDestroyWidget((Widget
) m_mainWidget
);
406 m_mainWidget
= (WXWidget
) 0;
408 SetMenuBarFrame((wxFrame
*) NULL
);
413 // Since PopupMenu under Motif stills grab right mouse button events
414 // after it was closed, we need to delete the associated widgets to
415 // allow next PopUpMenu to appear...
416 void wxMenu::DestroyWidgetAndDetach()
420 wxMenu
*menuParent
= GetParent();
423 wxMenuItemList::compatibility_iterator node
= menuParent
->GetMenuItems().GetFirst();
426 if ( node
->GetData()->GetSubMenu() == this )
428 delete node
->GetData();
429 menuParent
->GetMenuItems().Erase(node
);
434 node
= node
->GetNext();
441 // Mark as no longer popped up
446 * Create a popup or pulldown menu.
447 * Submenus of a popup will be pulldown.
451 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
,
454 size_t WXUNUSED(index
),
455 const wxString
& title
,
458 Widget menu
= (Widget
) 0;
459 Widget buttonWidget
= (Widget
) 0;
460 Display
* dpy
= XtDisplay((Widget
)parent
);
462 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
463 XtSetArg (args
[1], XmNpacking
, (m_numColumns
> 1) ? XmPACK_COLUMN
: XmPACK_TIGHT
);
468 m_font
= menuBar
->GetFont();
469 else if ( GetInvokingWindow() )
470 m_font
= GetInvokingWindow()->GetFont();
473 XtSetArg (args
[2], (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
) );
477 menu
= XmCreatePopupMenu ((Widget
) parent
, wxMOTIF_STR("popup"), args
, 3);
481 (XtCallbackProc
)wxMenuPopdownCallback
,
487 char mnem
= wxFindMnemonic (title
);
488 menu
= XmCreatePulldownMenu ((Widget
) parent
, wxMOTIF_STR("pulldown"), args
, 3);
490 wxString
title2(wxStripMenuCodes(title
));
491 wxXmString
label_str(title2
);
492 buttonWidget
= XtVaCreateManagedWidget(title2
,
494 xmCascadeButtonGadgetClass
, (Widget
) parent
,
496 xmCascadeButtonWidgetClass
, (Widget
) parent
,
498 XmNlabelString
, label_str(),
500 (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
501 XmNpositionIndex
, index
,
505 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
508 m_menuWidget
= (WXWidget
) menu
;
510 m_topLevelMenu
= topMenu
;
513 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
515 node
= node
->GetNext(), ++i
)
517 wxMenuItem
*item
= node
->GetData();
519 item
->CreateItem(menu
, menuBar
, topMenu
, i
);
527 // Destroys the Motif implementation of the menu,
528 // but maintains the wxWidgets data structures so we can
529 // do a CreateMenu again.
530 void wxMenu::DestroyMenu (bool full
)
532 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
534 node
= node
->GetNext() )
536 wxMenuItem
*item
= node
->GetData();
537 item
->SetMenuBar((wxMenuBar
*) NULL
);
539 item
->DestroyItem(full
);
546 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
547 XtDestroyWidget ((Widget
) m_buttonWidget
);
548 m_buttonWidget
= (WXWidget
) 0;
551 if (m_menuWidget
&& full
)
553 XtDestroyWidget((Widget
) m_menuWidget
);
554 m_menuWidget
= (WXWidget
) NULL
;
558 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
563 *it
= (wxMenuItem
*) NULL
;
564 return m_buttonWidget
;
567 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
569 node
= node
->GetNext() )
571 wxMenuItem
*item
= node
->GetData ();
572 if (item
->GetId() == id
)
576 return item
->GetButtonWidget();
579 if (item
->GetSubMenu())
581 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
590 *it
= (wxMenuItem
*) NULL
;
591 return (WXWidget
) NULL
;
594 void wxMenu::SetBackgroundColour(const wxColour
& col
)
596 m_backgroundColour
= col
;
600 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
602 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, true);
604 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
606 node
= node
->GetNext() )
608 wxMenuItem
* item
= node
->GetData();
609 if (item
->GetButtonWidget())
611 // This crashes because it uses gadgets
612 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, true);
614 if (item
->GetSubMenu())
615 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
619 void wxMenu::SetForegroundColour(const wxColour
& col
)
621 m_foregroundColour
= col
;
625 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
627 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
629 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
631 node
= node
->GetNext() )
633 wxMenuItem
* item
= node
->GetData();
634 if (item
->GetButtonWidget())
636 // This crashes because it uses gadgets
637 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
639 if (item
->GetSubMenu())
640 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
644 void wxMenu::ChangeFont(bool keepOriginalSize
)
646 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
647 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
648 if (!m_font
.Ok() || !m_menuWidget
)
651 Display
* dpy
= XtDisplay((Widget
) m_menuWidget
);
653 XtVaSetValues ((Widget
) m_menuWidget
,
654 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
658 XtVaSetValues ((Widget
) m_buttonWidget
,
659 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
663 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
665 node
= node
->GetNext() )
667 wxMenuItem
* item
= node
->GetData();
668 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
670 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
671 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
674 if (item
->GetSubMenu())
675 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
678 wxUnusedVar(keepOriginalSize
);
682 void wxMenu::SetFont(const wxFont
& font
)
688 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
690 if (!wxWindowBase::SetBackgroundColour(col
))
695 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
697 size_t menuCount
= GetMenuCount();
698 for (size_t i
= 0; i
< menuCount
; i
++)
699 m_menus
.Item(i
)->GetData()->SetBackgroundColour((wxColour
&) col
);
704 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
706 if (!wxWindowBase::SetForegroundColour(col
))
711 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
713 size_t menuCount
= GetMenuCount();
714 for (size_t i
= 0; i
< menuCount
; i
++)
715 m_menus
.Item(i
)->GetData()->SetForegroundColour((wxColour
&) col
);
720 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
722 // Nothing to do for menubar, fonts are kept in wxMenus
725 bool wxMenuBar::SetFont(const wxFont
& font
)
730 size_t menuCount
= GetMenuCount();
731 for (size_t i
= 0; i
< menuCount
; i
++)
732 m_menus
.Item(i
)->GetData()->SetFont(font
);