1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/menuitem.cpp
3 // Purpose: wxMenuItem implementation
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"
23 #include "wx/menuitem.h"
24 #include "wx/stockitem.h"
33 #pragma message disable nosimpint
36 #include <Xm/LabelG.h>
37 #include <Xm/CascadeBG.h>
38 #include <Xm/CascadeB.h>
39 #include <Xm/SeparatoG.h>
40 #include <Xm/PushBG.h>
41 #include <Xm/ToggleB.h>
42 #include <Xm/ToggleBG.h>
43 #include <Xm/RowColumn.h>
45 #pragma message enable nosimpint
48 #include "wx/motif/private.h"
50 // ----------------------------------------------------------------------------
51 // functions prototypes
52 // ----------------------------------------------------------------------------
54 static void wxMenuItemCallback(Widget w
, XtPointer clientData
, XtPointer ptr
);
55 static void wxMenuItemArmCallback(Widget w
, XtPointer clientData
, XtPointer ptr
);
56 static void wxMenuItemDisarmCallback(Widget w
, XtPointer clientData
, XtPointer ptr
);
58 // ============================================================================
60 // ============================================================================
62 // ----------------------------------------------------------------------------
63 // dynamic classes implementation
64 // ----------------------------------------------------------------------------
66 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
75 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
77 const wxString
& strName
,
78 const wxString
& strHelp
,
81 : wxMenuItemBase(pParentMenu
, id
, strName
, strHelp
, kind
, pSubMenu
)
85 m_buttonWidget
= (WXWidget
) NULL
;
89 wxMenuItem::~wxMenuItem()
96 void wxMenuItem::Enable(bool bDoEnable
)
98 if ( m_isEnabled
!= bDoEnable
)
104 XtSetSensitive( (Widget
) m_buttonWidget
, (Boolean
) bDoEnable
);
108 // Maybe we should apply this to all items in the submenu?
109 // Or perhaps it works anyway.
111 XtSetSensitive( (Widget
) m_buttonWidget
, (Boolean
) bDoEnable
);
114 wxMenuItemBase::Enable(bDoEnable
);
118 void wxMenuItem::Check(bool bDoCheck
)
120 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
122 if ( m_isChecked
!= bDoCheck
)
124 if ( m_buttonWidget
)
126 wxASSERT_MSG( XtIsSubclass((Widget
)m_buttonWidget
,
127 xmToggleButtonGadgetClass
),
128 wxT("checkable menu item must be a toggle button") );
130 XtVaSetValues((Widget
)m_buttonWidget
,
131 XmNset
, (Boolean
)bDoCheck
,
135 wxMenuItemBase::Check(bDoCheck
);
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
145 const wxString
& name
,
146 const wxString
& help
,
150 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 void wxMenuItem::CreateItem (WXWidget menu
, wxMenuBar
* menuBar
,
158 wxMenu
* topMenu
, size_t index
)
165 // Id=-3 identifies a Title item.
166 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget
167 (wxStripMenuCodes(m_text
),
168 xmLabelGadgetClass
, (Widget
) menu
, NULL
);
170 else if (!IsSeparator() && !m_subMenu
)
172 wxString txt
= m_text
;
174 if (m_text
.IsEmpty())
176 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
177 txt
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
180 wxString strName
= wxStripMenuCodes(txt
);
183 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (strName
,
184 xmToggleButtonGadgetClass
, (Widget
) menu
,
185 #ifdef XmNpositionIndex
186 XmNpositionIndex
, index
,
189 XtVaSetValues ((Widget
) m_buttonWidget
, XmNset
, (Boolean
) IsChecked(), NULL
);
192 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (strName
,
193 xmPushButtonGadgetClass
, (Widget
) menu
,
194 #ifdef XmNpositionIndex
195 XmNpositionIndex
, index
,
198 char mnem
= wxFindMnemonic (m_text
);
200 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmnemonic
, mnem
, NULL
);
202 //// TODO: proper accelerator treatment. What does wxFindAccelerator
205 char *accel
= wxFindAccelerator (strName
);
207 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
209 // TODO: What does this do?
210 XmString accel_str
= wxFindAcceleratorText (strName
);
213 XtVaSetValues ((Widget
) m_buttonWidget
, XmNacceleratorText
, accel_str
, NULL
);
214 XmStringFree (accel_str
);
218 XtAddCallback ((Widget
) m_buttonWidget
,
219 XmNvalueChangedCallback
,
220 (XtCallbackProc
) wxMenuItemCallback
,
223 XtAddCallback ((Widget
) m_buttonWidget
,
225 (XtCallbackProc
) wxMenuItemCallback
,
227 XtAddCallback ((Widget
) m_buttonWidget
,
229 (XtCallbackProc
) wxMenuItemArmCallback
,
231 XtAddCallback ((Widget
) m_buttonWidget
,
233 (XtCallbackProc
) wxMenuItemDisarmCallback
,
236 else if (IsSeparator())
238 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget ("separator",
239 xmSeparatorGadgetClass
, (Widget
) menu
,
240 #ifndef XmNpositionIndex
241 XmNpositionIndex
, index
,
247 m_buttonWidget
= m_subMenu
->CreateMenu (menuBar
, menu
, topMenu
, index
, m_text
, true);
248 m_subMenu
->SetButtonWidget(m_buttonWidget
);
249 XtAddCallback ((Widget
) m_buttonWidget
,
250 XmNcascadingCallback
,
251 (XtCallbackProc
) wxMenuItemArmCallback
,
255 XtSetSensitive ((Widget
) m_buttonWidget
, (Boolean
) IsEnabled());
258 void wxMenuItem::DestroyItem(bool full
)
265 else if (!m_text
.empty() && !m_subMenu
)
270 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNvalueChangedCallback
,
271 wxMenuItemCallback
, (XtPointer
) this);
273 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNactivateCallback
,
274 wxMenuItemCallback
, (XtPointer
) this);
275 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNarmCallback
,
276 wxMenuItemArmCallback
, (XtPointer
) this);
277 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNdisarmCallback
,
278 wxMenuItemDisarmCallback
, (XtPointer
) this);
281 else if (IsSeparator())
286 else if (GetSubMenu())
290 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNcascadingCallback
,
291 wxMenuItemArmCallback
, (XtPointer
) this);
293 m_subMenu
->DestroyMenu(full
);
295 m_buttonWidget
= NULL
;
298 if (m_buttonWidget
&& full
)
300 XtDestroyWidget ((Widget
) m_buttonWidget
);
301 m_buttonWidget
= (WXWidget
) 0;
305 void wxMenuItem::SetItemLabel(const wxString
& label
)
307 char mnem
= wxFindMnemonic (label
);
308 wxString label2
= wxStripMenuCodes(label
);
314 wxXmString
label_str(label2
);
315 XtVaSetValues ((Widget
) m_buttonWidget
,
316 XmNlabelString
, label_str(),
319 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmnemonic
, mnem
, NULL
);
320 char *accel
= wxFindAccelerator (label2
);
322 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
324 XmString accel_str
= wxFindAcceleratorText (label2
);
327 XtVaSetValues ((Widget
) m_buttonWidget
, XmNacceleratorText
, accel_str
, NULL
);
328 XmStringFree (accel_str
);
333 // ----------------------------------------------------------------------------
335 // ----------------------------------------------------------------------------
337 void wxMenuItemCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
338 XtPointer
WXUNUSED(ptr
))
340 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
343 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, item
->GetId());
344 event
.SetInt( item
->GetId() );
346 if (item
->IsCheckable())
348 Boolean isChecked
= false;
349 XtVaGetValues ((Widget
) item
->GetButtonWidget(),
353 // only set the flag, don't actually check anything
354 item
->wxMenuItemBase::Check(isChecked
);
355 event
.SetInt(isChecked
);
358 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
360 event
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
362 item
->GetMenuBar()->GetMenuBarFrame()
363 ->HandleWindowEvent(event
);
365 // this is the child of a popup menu
366 else if (item
->GetTopMenu())
368 event
.SetEventObject(item
->GetTopMenu());
370 item
->GetTopMenu()->ProcessCommand (event
);
372 // Since PopupMenu under Motif still grab right mouse
373 // button events after it was closed, we need to delete
374 // the associated widgets to allow next PopUpMenu to
375 // appear; this needs to be done there because doing it in
376 // a WorkProc as before may cause crashes if a menu item causes
377 // the parent window of the menu to be destroyed
378 item
->GetTopMenu()->DestroyWidgetAndDetach();
383 void wxMenuItemArmCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
384 XtPointer
WXUNUSED(ptr
))
386 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
389 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
391 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, item
->GetId());
392 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
394 item
->GetMenuBar()->GetMenuBarFrame()
395 ->HandleWindowEvent(menuEvent
);
401 wxMenuItemDisarmCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
402 XtPointer
WXUNUSED(ptr
))
404 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
407 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
409 // TODO: not sure this is correct, since -1 means something
410 // special to event system
411 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, -1);
412 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
414 item
->GetMenuBar()->GetMenuBarFrame()
415 ->HandleWindowEvent(menuEvent
);