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"
24 #include "wx/menuitem.h"
29 #include "wx/settings.h"
32 #pragma message disable nosimpint
33 #define XtDisplay XTDISPLAY
34 #define XtWindow XTWINDOW
37 #include <Xm/LabelG.h>
38 #include <Xm/CascadeBG.h>
39 #include <Xm/CascadeB.h>
40 #include <Xm/SeparatoG.h>
41 #include <Xm/PushBG.h>
42 #include <Xm/ToggleB.h>
43 #include <Xm/ToggleBG.h>
44 #include <Xm/RowColumn.h>
46 #pragma message enable nosimpint
49 #include "wx/motif/private.h"
51 // other standard headers
54 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
55 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // Construct a menu with optional title (then use append)
68 // Motif-specific members
70 m_menuWidget
= (WXWidget
) NULL
;
71 m_popupShell
= (WXWidget
) NULL
;
72 m_buttonWidget
= (WXWidget
) NULL
;
74 m_topLevelMenu
= (wxMenu
*) NULL
;
75 m_ownedByMenuBar
= false;
77 if ( !m_title
.empty() )
83 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
84 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
85 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
88 // The wxWindow destructor will take care of deleting the submenus.
99 // Not sure if this is right
100 if (m_menuParent
&& m_menuBar
)
112 // function appends a new item or submenu to the menu
113 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*pItem
)
115 return DoInsert(GetMenuItemCount(), pItem
);
118 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
120 item
->DestroyItem(true);
122 return wxMenuBase::DoRemove(item
);
125 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
129 // this is a dynamic Append
130 #ifndef XmNpositionIndex
131 wxCHECK_MSG( pos
== GetMenuItemCount(), -1, wxT("insert not implemented"));
133 item
->CreateItem(m_menuWidget
, GetMenuBar(), m_topLevelMenu
, pos
);
136 if ( item
->IsSubMenu() )
138 item
->GetSubMenu()->m_topLevelMenu
= m_topLevelMenu
;
141 return pos
== GetMenuItemCount() ? wxMenuBase::DoAppend(item
) :
142 wxMenuBase::DoInsert(pos
, item
);
145 void wxMenu::SetTitle(const wxString
& label
)
149 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
153 wxMenuItem
*item
= node
->GetData ();
154 Widget widget
= (Widget
) item
->GetButtonWidget();
158 wxXmString
title_str(label
);
159 XtVaSetValues(widget
,
160 XmNlabelString
, title_str(),
164 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
166 bool processed
= false;
168 // Try the menu's event handler
169 if ( !processed
&& GetEventHandler())
171 processed
= GetEventHandler()->ProcessEvent(event
);
173 // Try the window the menu was popped up from (and up
174 // through the hierarchy)
175 if ( !processed
&& GetInvokingWindow())
176 processed
= GetInvokingWindow()->ProcessEvent(event
);
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 void wxMenuBar::Init()
187 m_eventHandler
= this;
188 m_menuBarFrame
= NULL
;
189 m_mainWidget
= (WXWidget
) NULL
;
190 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENU
);
191 m_foregroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT
);
192 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
195 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxArrayString
& titles
, long WXUNUSED(style
))
197 wxASSERT( n
== titles
.GetCount() );
202 for ( size_t i
= 0; i
< n
; i
++ )
203 m_menus
.Append(menus
[i
]);
206 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
210 for ( size_t i
= 0; i
< n
; i
++ )
212 m_menus
.Append(menus
[i
]);
213 m_titles
.Add(titles
[i
]);
217 wxMenuBar::~wxMenuBar()
219 // nothing to do: wxMenuBarBase will delete the menus
222 void wxMenuBar::EnableTop(size_t WXUNUSED(pos
), bool WXUNUSED(flag
))
224 // wxFAIL_MSG("TODO");
225 // wxLogWarning("wxMenuBar::EnableTop not yet implemented.");
228 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
230 wxMenu
*menu
= GetMenu(pos
);
234 Widget w
= (Widget
)menu
->GetButtonWidget();
237 wxXmString
label_str(label
);
240 XmNlabelString
, label_str(),
245 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
247 wxMenu
*menu
= GetMenu(pos
);
250 Widget w
= (Widget
)menu
->GetButtonWidget();
255 XmNlabelString
, &text
,
258 return wxXmStringToString( text
);
262 return wxEmptyString
;
265 bool wxMenuBar::Append(wxMenu
* menu
, const wxString
& title
)
267 return Insert(GetMenuCount(), menu
, title
);
270 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
272 wxCHECK_MSG( pos
<= GetMenuCount(), false, wxT("invalid position") );
273 wxCHECK_MSG( menu
, false, wxT("invalid menu") );
274 wxCHECK_MSG( !menu
->GetParent() && !menu
->GetButtonWidget(), false,
275 wxT("menu already appended") );
277 if ( m_menuBarFrame
)
279 WXWidget w
= menu
->CreateMenu(this, GetMainWidget(), menu
,
281 wxCHECK_MSG( w
, false, wxT("failed to create menu") );
282 menu
->SetButtonWidget(w
);
285 m_titles
.Insert(title
, pos
);
287 return wxMenuBarBase::Insert(pos
, menu
, title
);
290 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
292 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
295 wxFAIL_MSG(wxT("TODO"));
300 wxMenu
*wxMenuBar::Remove(size_t pos
)
302 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
306 if ( m_menuBarFrame
)
307 menu
->DestroyMenu(true);
309 menu
->SetMenuBar(NULL
);
311 m_titles
.RemoveAt(pos
);
316 // Find the menu menuString, item itemString, and return the item id.
317 // Returns -1 if none found.
318 int wxMenuBar::FindMenuItem (const wxString
& menuString
, const wxString
& itemString
) const
322 wxStripMenuCodes (wxConstCast(menuString
.c_str(), char), buf1
);
324 size_t menuCount
= GetMenuCount();
325 for (size_t i
= 0; i
< menuCount
; i
++)
327 wxStripMenuCodes (wxConstCast(m_titles
[i
].c_str(), char), buf2
);
328 if (strcmp (buf1
, buf2
) == 0)
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
);