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
325 wxStripMenuCodes (wxConstCast(menuString
.c_str(), char), buf1
);
327 size_t menuCount
= GetMenuCount();
328 for (size_t i
= 0; i
< menuCount
; i
++)
330 wxStripMenuCodes (wxConstCast(m_titles
[i
].c_str(), char), buf2
);
331 if (strcmp (buf1
, buf2
) == 0)
332 return m_menus
.Item(i
)->GetData()->FindItem (itemString
);
337 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
** itemMenu
) const
342 size_t menuCount
= GetMenuCount();
343 for (size_t i
= 0; i
< menuCount
; i
++)
345 wxMenuItem
*item
= m_menus
.Item(i
)->GetData()->FindItem(id
, itemMenu
);
346 if (item
) return item
;
353 bool wxMenuBar::CreateMenuBar(wxFrame
* parent
)
357 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
359 if (!XtIsManaged((Widget) m_mainWidget))
360 XtManageChild((Widget) m_mainWidget);
362 XtMapWidget((Widget
) m_mainWidget
);
366 Widget menuBarW
= XmCreateMenuBar ((Widget
) parent
->GetMainWidget(),
367 wxMOTIF_STR("MenuBar"), NULL
, 0);
368 m_mainWidget
= (WXWidget
) menuBarW
;
370 size_t menuCount
= GetMenuCount();
371 for (size_t i
= 0; i
< menuCount
; i
++)
373 wxMenu
*menu
= GetMenu(i
);
374 wxString
title(m_titles
[i
]);
375 menu
->SetButtonWidget(menu
->CreateMenu (this, menuBarW
, menu
, i
, title
, true));
377 if (strcmp (wxStripMenuCodes(title
), "Help") == 0)
378 XtVaSetValues ((Widget
) menuBarW
, XmNmenuHelpWidget
, (Widget
) menu
->GetButtonWidget(), NULL
);
380 // tear off menu support
381 #if (XmVersion >= 1002)
382 if ( menu
->IsTearOff() )
384 XtVaSetValues(GetWidget(menu
),
385 XmNtearOffModel
, XmTEAR_OFF_ENABLED
,
387 Widget tearOff
= XmGetTearOffControl(GetWidget(menu
));
388 wxDoChangeForegroundColour((Widget
) tearOff
, m_foregroundColour
);
389 wxDoChangeBackgroundColour((Widget
) tearOff
, m_backgroundColour
, true);
394 SetBackgroundColour(m_backgroundColour
);
395 SetForegroundColour(m_foregroundColour
);
398 XtVaSetValues((Widget
) parent
->GetMainWidget(), XmNmenuBar
, (Widget
) m_mainWidget
, NULL
);
399 XtRealizeWidget ((Widget
) menuBarW
);
400 XtManageChild ((Widget
) menuBarW
);
401 SetMenuBarFrame(parent
);
406 // Destroy menubar, but keep data structures intact so we can recreate it.
407 bool wxMenuBar::DestroyMenuBar()
411 SetMenuBarFrame((wxFrame
*) NULL
);
415 XtUnmanageChild ((Widget
) m_mainWidget
);
416 XtUnrealizeWidget ((Widget
) m_mainWidget
);
418 size_t menuCount
= GetMenuCount();
419 for (size_t i
= 0; i
< menuCount
; i
++)
421 wxMenu
*menu
= GetMenu(i
);
422 menu
->DestroyMenu(true);
425 XtDestroyWidget((Widget
) m_mainWidget
);
426 m_mainWidget
= (WXWidget
) 0;
428 SetMenuBarFrame((wxFrame
*) NULL
);
433 // Since PopupMenu under Motif stills grab right mouse button events
434 // after it was closed, we need to delete the associated widgets to
435 // allow next PopUpMenu to appear...
436 void wxMenu::DestroyWidgetAndDetach()
440 wxMenu
*menuParent
= GetParent();
443 wxMenuItemList::compatibility_iterator node
= menuParent
->GetMenuItems().GetFirst();
446 if ( node
->GetData()->GetSubMenu() == this )
448 delete node
->GetData();
449 menuParent
->GetMenuItems().Erase(node
);
454 node
= node
->GetNext();
461 // Mark as no longer popped up
466 * Create a popup or pulldown menu.
467 * Submenus of a popup will be pulldown.
471 WXWidget
wxMenu::CreateMenu (wxMenuBar
* menuBar
,
474 size_t WXUNUSED(index
),
475 const wxString
& title
,
478 Widget menu
= (Widget
) 0;
479 Widget buttonWidget
= (Widget
) 0;
481 XtSetArg (args
[0], XmNnumColumns
, m_numColumns
);
482 XtSetArg (args
[1], XmNpacking
, (m_numColumns
> 1) ? XmPACK_COLUMN
: XmPACK_TIGHT
);
486 menu
= XmCreatePopupMenu ((Widget
) parent
, wxMOTIF_STR("popup"), args
, 2);
490 (XtCallbackProc
)wxMenuPopdownCallback
,
496 char mnem
= wxFindMnemonic (title
);
497 menu
= XmCreatePulldownMenu ((Widget
) parent
, wxMOTIF_STR("pulldown"), args
, 2);
499 wxString
title2(wxStripMenuCodes(title
));
500 wxXmString
label_str(title2
);
501 buttonWidget
= XtVaCreateManagedWidget(title2
,
503 xmCascadeButtonGadgetClass
, (Widget
) parent
,
505 xmCascadeButtonWidgetClass
, (Widget
) parent
,
507 XmNlabelString
, label_str(),
512 XtVaSetValues (buttonWidget
, XmNmnemonic
, mnem
, NULL
);
515 m_menuWidget
= (WXWidget
) menu
;
517 m_topLevelMenu
= topMenu
;
520 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
522 node
= node
->GetNext(), ++i
)
524 wxMenuItem
*item
= node
->GetData();
526 item
->CreateItem(menu
, menuBar
, topMenu
, i
);
529 SetBackgroundColour(m_backgroundColour
);
530 SetForegroundColour(m_foregroundColour
);
536 // Destroys the Motif implementation of the menu,
537 // but maintains the wxWidgets data structures so we can
538 // do a CreateMenu again.
539 void wxMenu::DestroyMenu (bool full
)
541 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
543 node
= node
->GetNext() )
545 wxMenuItem
*item
= node
->GetData();
546 item
->SetMenuBar((wxMenuBar
*) NULL
);
548 item
->DestroyItem(full
);
555 XtVaSetValues((Widget
) m_buttonWidget
, XmNsubMenuId
, NULL
, NULL
);
556 XtDestroyWidget ((Widget
) m_buttonWidget
);
557 m_buttonWidget
= (WXWidget
) 0;
560 if (m_menuWidget
&& full
)
562 XtDestroyWidget((Widget
) m_menuWidget
);
563 m_menuWidget
= (WXWidget
) NULL
;
567 WXWidget
wxMenu::FindMenuItem (int id
, wxMenuItem
** it
) const
572 *it
= (wxMenuItem
*) NULL
;
573 return m_buttonWidget
;
576 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
578 node
= node
->GetNext() )
580 wxMenuItem
*item
= node
->GetData ();
581 if (item
->GetId() == id
)
585 return item
->GetButtonWidget();
588 if (item
->GetSubMenu())
590 WXWidget w
= item
->GetSubMenu()->FindMenuItem (id
, it
);
599 *it
= (wxMenuItem
*) NULL
;
600 return (WXWidget
) NULL
;
603 void wxMenu::SetBackgroundColour(const wxColour
& col
)
605 m_backgroundColour
= col
;
607 wxDoChangeBackgroundColour(m_menuWidget
, (wxColour
&) col
);
609 wxDoChangeBackgroundColour(m_buttonWidget
, (wxColour
&) col
, true);
611 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
613 node
= node
->GetNext() )
615 wxMenuItem
* item
= node
->GetData();
616 if (item
->GetButtonWidget())
618 // This crashes because it uses gadgets
619 // wxDoChangeBackgroundColour(item->GetButtonWidget(), (wxColour&) col, true);
621 if (item
->GetSubMenu())
622 item
->GetSubMenu()->SetBackgroundColour((wxColour
&) col
);
626 void wxMenu::SetForegroundColour(const wxColour
& col
)
628 m_foregroundColour
= col
;
630 wxDoChangeForegroundColour(m_menuWidget
, (wxColour
&) col
);
632 wxDoChangeForegroundColour(m_buttonWidget
, (wxColour
&) col
);
634 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
636 node
= node
->GetNext() )
638 wxMenuItem
* item
= node
->GetData();
639 if (item
->GetButtonWidget())
641 // This crashes because it uses gadgets
642 // wxDoChangeForegroundColour(item->GetButtonWidget(), (wxColour&) col);
644 if (item
->GetSubMenu())
645 item
->GetSubMenu()->SetForegroundColour((wxColour
&) col
);
649 void wxMenu::ChangeFont(bool keepOriginalSize
)
651 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
652 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
653 if (!m_font
.Ok() || !m_menuWidget
)
656 Display
* dpy
= XtDisplay((Widget
) m_menuWidget
);
658 XtVaSetValues ((Widget
) m_menuWidget
,
659 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
663 XtVaSetValues ((Widget
) m_buttonWidget
,
664 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
668 for ( wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
670 node
= node
->GetNext() )
672 wxMenuItem
* item
= node
->GetData();
673 if (m_menuWidget
&& item
->GetButtonWidget() && m_font
.Ok())
675 XtVaSetValues ((Widget
) item
->GetButtonWidget(),
676 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
679 if (item
->GetSubMenu())
680 item
->GetSubMenu()->ChangeFont(keepOriginalSize
);
683 wxUnusedVar(keepOriginalSize
);
687 void wxMenu::SetFont(const wxFont
& font
)
693 bool wxMenuBar::SetBackgroundColour(const wxColour
& col
)
695 m_backgroundColour
= col
;
697 wxDoChangeBackgroundColour(m_mainWidget
, (wxColour
&) col
);
699 size_t menuCount
= GetMenuCount();
700 for (size_t i
= 0; i
< menuCount
; i
++)
701 m_menus
.Item(i
)->GetData()->SetBackgroundColour((wxColour
&) col
);
706 bool wxMenuBar::SetForegroundColour(const wxColour
& col
)
708 m_foregroundColour
= col
;
710 wxDoChangeForegroundColour(m_mainWidget
, (wxColour
&) col
);
712 size_t menuCount
= GetMenuCount();
713 for (size_t i
= 0; i
< menuCount
; i
++)
714 m_menus
.Item(i
)->GetData()->SetForegroundColour((wxColour
&) col
);
719 void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize
))
721 // Nothing to do for menubar, fonts are kept in wxMenus
724 bool wxMenuBar::SetFont(const wxFont
& font
)
729 size_t menuCount
= GetMenuCount();
730 for (size_t i
= 0; i
< menuCount
; i
++)
731 m_menus
.Item(i
)->GetData()->SetFont(font
);