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() )
86 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
87 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
88 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
91 // The wxWindow destructor will take care of deleting the submenus.
102 // Not sure if this is right
103 if (m_menuParent
&& m_menuBar
)
115 // function appends a new item or submenu to the menu
116 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*pItem
)
118 return DoInsert(GetMenuItemCount(), pItem
);
121 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
123 item
->DestroyItem(true);
125 return wxMenuBase::DoRemove(item
);
128 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
132 // this is a dynamic Append
133 #ifndef XmNpositionIndex
134 wxCHECK_MSG( pos
== GetMenuItemCount(), -1, wxT("insert not implemented"));
136 item
->CreateItem(m_menuWidget
, GetMenuBar(), m_topLevelMenu
, pos
);
139 if ( item
->IsSubMenu() )
141 item
->GetSubMenu()->m_topLevelMenu
= m_topLevelMenu
;
144 return pos
== GetMenuItemCount() ? wxMenuBase::DoAppend(item
) :
145 wxMenuBase::DoInsert(pos
, item
);
148 void wxMenu::SetTitle(const wxString
& label
)
152 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
156 wxMenuItem
*item
= node
->GetData ();
157 Widget widget
= (Widget
) item
->GetButtonWidget();
161 wxXmString
title_str(label
);
162 XtVaSetValues(widget
,
163 XmNlabelString
, title_str(),
167 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
169 bool processed
= false;
171 // Try the menu's event handler
172 if ( !processed
&& GetEventHandler())
174 processed
= GetEventHandler()->ProcessEvent(event
);
176 // Try the window the menu was popped up from (and up
177 // through the hierarchy)
178 if ( !processed
&& GetInvokingWindow())
179 processed
= GetInvokingWindow()->ProcessEvent(event
);
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 void wxMenuBar::Init()
190 m_eventHandler
= this;
191 m_menuBarFrame
= NULL
;
192 m_mainWidget
= (WXWidget
) NULL
;
193 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
194 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
195 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
198 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxArrayString
& titles
, long WXUNUSED(style
))
200 wxASSERT( n
== titles
.GetCount() );
205 for ( size_t i
= 0; i
< n
; i
++ )
206 m_menus
.Append(menus
[i
]);
209 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
213 for ( size_t i
= 0; i
< n
; i
++ )
215 m_menus
.Append(menus
[i
]);
216 m_titles
.Add(titles
[i
]);
220 wxMenuBar::~wxMenuBar()
222 // nothing to do: wxMenuBarBase will delete the menus
225 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
227 // wxFAIL_MSG("TODO");
228 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
231 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
233 wxMenu
*menu
= GetMenu(pos
);
237 Widget w
= (Widget
)menu
->GetButtonWidget();
240 wxXmString
label_str(label
);
243 XmNlabelString
, label_str(),
248 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
250 wxMenu
*menu
= GetMenu(pos
);
253 Widget w
= (Widget
)menu
->GetButtonWidget();
258 XmNlabelString
, &text
,
261 return wxXmStringToString( text
);
265 return wxEmptyString
;
268 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
270 return Insert(GetMenuCount(), menu
, title
);
273 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
275 wxCHECK_MSG( pos
<= GetMenuCount(), false, wxT("invalid position") );
276 wxCHECK_MSG( menu
, false, wxT("invalid menu") );
277 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), false,
278 wxT("menu already appended") );
280 if ( m_menuBarFrame
)
282 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
,
284 wxCHECK_MSG( w
, false, wxT("failed to create menu") );
285 menu
->SetButtonWidget(w
);
288 m_titles
.Insert(title
, pos
);
290 return wxMenuBarBase::Insert(pos
, menu
, title
);
293 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
295 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
298 wxFAIL_MSG(wxT("TODO"));
303 wxMenu
*wxMenuBar::Remove(size_t pos
)
305 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
309 if ( m_menuBarFrame
)
310 menu
->DestroyMenu(true);
312 menu
->SetMenuBar(NULL
);
314 m_titles
.RemoveAt(pos
);
319 // Find the menu menuString, item itemString, and return the item id.
320 // Returns -1 if none found.
321 int wxMenuBar::FindMenuItem(const wxString
& menuString
, const wxString
& itemString
) const
323 const wxString stripped
= wxStripMenuCodes(menuString
);
325 size_t menuCount
= GetMenuCount();
326 for (size_t i
= 0; i
< menuCount
; i
++)
328 if ( wxStripMenuCodes(m_titles
[i
]) == stripped
)
329 return m_menus
.Item(i
)->GetData()->FindItem (itemString
);
334 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
339 size_t menuCount
= GetMenuCount();
340 for (size_t i
= 0; i
< menuCount
; i
++)
342 wxMenuItem
*item
= m_menus
.Item(i
)->GetData()->FindItem(id
, itemMenu
);
343 if (item
) return item
;
350 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
354 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
356 if (!XtIsManaged((Widget) m_mainWidget))
357 XtManageChild((Widget) m_mainWidget);
359 XtMapWidget((Widget
) m_mainWidget
);
363 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(),
364 wxMOTIF_STR("MenuBar"), NULL
, 0);
365 m_mainWidget
= (WXWidget
) menuBarW
;
367 size_t menuCount
= GetMenuCount();
368 for (size_t i
= 0; i
< menuCount
; i
++)
370 wxMenu
*menu
= GetMenu(i
);
371 wxString
title(m_titles
[i
]);
372 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, i
, title
, true));
374 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
375 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
377 // tear off menu support
378 #if (XmVersion >= 1002)
379 if ( menu
->IsTearOff() )
381 XtVaSetValues(GetWidget(menu
),
382 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
384 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
385 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
386 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, true);
391 SetBackgroundColour(m_backgroundColour
);
392 SetForegroundColour(m_foregroundColour
);
395 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
396 XtRealizeWidget ((Widget
) menuBarW
);
397 XtManageChild ((Widget
) menuBarW
);
398 SetMenuBarFrame(parent
);
403 // Destroy menubar, but keep data structures intact so we can recreate it.
404 bool wxMenuBar::DestroyMenuBar()
408 SetMenuBarFrame((wxFrame
*) NULL
);
412 XtUnmanageChild ((Widget
) m_mainWidget
);
413 XtUnrealizeWidget ((Widget
) m_mainWidget
);
415 size_t menuCount
= GetMenuCount();
416 for (size_t i
= 0; i
< menuCount
; i
++)
418 wxMenu
*menu
= GetMenu(i
);
419 menu
->DestroyMenu(true);
422 XtDestroyWidget((Widget
) m_mainWidget
);
423 m_mainWidget
= (WXWidget
) 0;
425 SetMenuBarFrame((wxFrame
*) NULL
);
430 // Since PopupMenu under Motif stills grab right mouse button events
431 // after it was closed, we need to delete the associated widgets to
432 // allow next PopUpMenu to appear...
433 void wxMenu::DestroyWidgetAndDetach()
437 wxMenu
*menuParent
= GetParent();
440 wxMenuItemList::compatibility_iterator node
= menuParent
->GetMenuItems().GetFirst();
443 if ( node
->GetData()->GetSubMenu() == this )
445 delete node
->GetData();
446 menuParent
->GetMenuItems().Erase(node
);
451 node
= node
->GetNext();
458 // Mark as no longer popped up
463 * Create a popup or pulldown menu.
464 * Submenus of a popup will be pulldown.
468 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
,
471 size_t WXUNUSED(index
),
472 const wxString
& title
,
475 Widget menu
= (Widget
) 0;
476 Widget buttonWidget
= (Widget
) 0;
478 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
479 XtSetArg (args
[1], XmNpacking
, (m_numColumns
> 1) ? XmPACK_COLUMN
: XmPACK_TIGHT
);
483 menu
= XmCreatePopupMenu ((Widget
) parent
, wxMOTIF_STR("popup"), args
, 2);
487 (XtCallbackProc
)wxMenuPopdownCallback
,
493 char mnem
= wxFindMnemonic (title
);
494 menu
= XmCreatePulldownMenu ((Widget
) parent
, wxMOTIF_STR("pulldown"), args
, 2);
496 wxString
title2(wxStripMenuCodes(title
));
497 wxXmString
label_str(title2
);
498 buttonWidget
= XtVaCreateManagedWidget(title2
,
500 xmCascadeButtonGadgetClass
, (Widget
) parent
,
502 xmCascadeButtonWidgetClass
, (Widget
) parent
,
504 XmNlabelString
, label_str(),
509 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
512 m_menuWidget
= (WXWidget
) menu
;
514 m_topLevelMenu
= topMenu
;
517 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
519 node
= node
->GetNext(), ++i
)
521 wxMenuItem
*item
= node
->GetData();
523 item
->CreateItem(menu
, menuBar
, topMenu
, i
);
526 SetBackgroundColour(m_backgroundColour
);
527 SetForegroundColour(m_foregroundColour
);
533 // Destroys the Motif implementation of the menu,
534 // but maintains the wxWidgets data structures so we can
535 // do a CreateMenu again.
536 void wxMenu::DestroyMenu (bool full
)
538 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
540 node
= node
->GetNext() )
542 wxMenuItem
*item
= node
->GetData();
543 item
->SetMenuBar((wxMenuBar
*) NULL
);
545 item
->DestroyItem(full
);
552 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
553 XtDestroyWidget ((Widget
) m_buttonWidget
);
554 m_buttonWidget
= (WXWidget
) 0;
557 if (m_menuWidget
&& full
)
559 XtDestroyWidget((Widget
) m_menuWidget
);
560 m_menuWidget
= (WXWidget
) NULL
;
564 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
569 *it
= (wxMenuItem
*) NULL
;
570 return m_buttonWidget
;
573 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
575 node
= node
->GetNext() )
577 wxMenuItem
*item
= node
->GetData ();
578 if (item
->GetId() == id
)
582 return item
->GetButtonWidget();
585 if (item
->GetSubMenu())
587 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
596 *it
= (wxMenuItem
*) NULL
;
597 return (WXWidget
) NULL
;
600 void wxMenu::SetBackgroundColour(const wxColour
& col
)
602 m_backgroundColour
= col
;
604 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
606 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, true);
608 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
610 node
= node
->GetNext() )
612 wxMenuItem
* item
= node
->GetData();
613 if (item
->GetButtonWidget())
615 // This crashes because it uses gadgets
616 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, true);
618 if (item
->GetSubMenu())
619 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
623 void wxMenu::SetForegroundColour(const wxColour
& col
)
625 m_foregroundColour
= col
;
627 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
629 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
631 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
633 node
= node
->GetNext() )
635 wxMenuItem
* item
= node
->GetData();
636 if (item
->GetButtonWidget())
638 // This crashes because it uses gadgets
639 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
641 if (item
->GetSubMenu())
642 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
646 void wxMenu::ChangeFont(bool keepOriginalSize
)
648 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
649 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
650 if (!m_font
.Ok() || !m_menuWidget
)
653 Display
* dpy
= XtDisplay((Widget
) m_menuWidget
);
655 XtVaSetValues ((Widget
) m_menuWidget
,
656 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
660 XtVaSetValues ((Widget
) m_buttonWidget
,
661 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
665 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
667 node
= node
->GetNext() )
669 wxMenuItem
* item
= node
->GetData();
670 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
672 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
673 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
676 if (item
->GetSubMenu())
677 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
680 wxUnusedVar(keepOriginalSize
);
684 void wxMenu::SetFont(const wxFont
& font
)
690 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
692 m_backgroundColour
= col
;
694 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
696 size_t menuCount
= GetMenuCount();
697 for (size_t i
= 0; i
< menuCount
; i
++)
698 m_menus
.Item(i
)->GetData()->SetBackgroundColour((wxColour
&) col
);
703 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
705 m_foregroundColour
= col
;
707 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
709 size_t menuCount
= GetMenuCount();
710 for (size_t i
= 0; i
< menuCount
; i
++)
711 m_menus
.Item(i
)->GetData()->SetForegroundColour((wxColour
&) col
);
716 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
718 // Nothing to do for menubar, fonts are kept in wxMenus
721 bool wxMenuBar::SetFont(const wxFont
& font
)
726 size_t menuCount
= GetMenuCount();
727 for (size_t i
= 0; i
< menuCount
; i
++)
728 m_menus
.Item(i
)->GetData()->SetFont(font
);