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"
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
);
140 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
142 return wxStripMenuCodes(text
);
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
151 const wxString
& name
,
152 const wxString
& help
,
156 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 void wxMenuItem::CreateItem (WXWidget menu
, wxMenuBar
* menuBar
,
164 wxMenu
* topMenu
, size_t index
)
171 // Id=-3 identifies a Title item.
172 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget
173 (wxStripMenuCodes(m_text
),
174 xmLabelGadgetClass
, (Widget
) menu
, NULL
);
176 else if (!m_text
.empty() && !m_subMenu
)
178 wxString strName
= wxStripMenuCodes(m_text
);
181 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (strName
,
182 xmToggleButtonGadgetClass
, (Widget
) menu
,
183 #ifdef XmNpositionIndex
184 XmNpositionIndex
, index
,
187 XtVaSetValues ((Widget
) m_buttonWidget
, XmNset
, (Boolean
) IsChecked(), NULL
);
190 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (strName
,
191 xmPushButtonGadgetClass
, (Widget
) menu
,
192 #ifdef XmNpositionIndex
193 XmNpositionIndex
, index
,
196 char mnem
= wxFindMnemonic (m_text
);
198 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmnemonic
, mnem
, NULL
);
200 //// TODO: proper accelerator treatment. What does wxFindAccelerator
203 char *accel
= wxFindAccelerator (strName
);
205 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
207 // TODO: What does this do?
208 XmString accel_str
= wxFindAcceleratorText (strName
);
211 XtVaSetValues ((Widget
) m_buttonWidget
, XmNacceleratorText
, accel_str
, NULL
);
212 XmStringFree (accel_str
);
216 XtAddCallback ((Widget
) m_buttonWidget
,
217 XmNvalueChangedCallback
,
218 (XtCallbackProc
) wxMenuItemCallback
,
221 XtAddCallback ((Widget
) m_buttonWidget
,
223 (XtCallbackProc
) wxMenuItemCallback
,
225 XtAddCallback ((Widget
) m_buttonWidget
,
227 (XtCallbackProc
) wxMenuItemArmCallback
,
229 XtAddCallback ((Widget
) m_buttonWidget
,
231 (XtCallbackProc
) wxMenuItemDisarmCallback
,
234 else if (GetId() == wxID_SEPARATOR
)
236 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget ("separator",
237 xmSeparatorGadgetClass
, (Widget
) menu
,
238 #ifndef XmNpositionIndex
239 XmNpositionIndex
, index
,
245 m_buttonWidget
= m_subMenu
->CreateMenu (menuBar
, menu
, topMenu
, index
, m_text
, true);
246 m_subMenu
->SetButtonWidget(m_buttonWidget
);
247 XtAddCallback ((Widget
) m_buttonWidget
,
248 XmNcascadingCallback
,
249 (XtCallbackProc
) wxMenuItemArmCallback
,
253 XtSetSensitive ((Widget
) m_buttonWidget
, (Boolean
) IsEnabled());
256 void wxMenuItem::DestroyItem(bool full
)
263 else if (!m_text
.empty() && !m_subMenu
)
268 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNvalueChangedCallback
,
269 wxMenuItemCallback
, (XtPointer
) this);
271 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNactivateCallback
,
272 wxMenuItemCallback
, (XtPointer
) this);
273 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNarmCallback
,
274 wxMenuItemArmCallback
, (XtPointer
) this);
275 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNdisarmCallback
,
276 wxMenuItemDisarmCallback
, (XtPointer
) this);
279 else if (GetId() == wxID_SEPARATOR
)
284 else if (GetSubMenu())
288 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNcascadingCallback
,
289 wxMenuItemArmCallback
, (XtPointer
) this);
291 m_subMenu
->DestroyMenu(full
);
293 m_buttonWidget
= NULL
;
296 if (m_buttonWidget
&& full
)
298 XtDestroyWidget ((Widget
) m_buttonWidget
);
299 m_buttonWidget
= (WXWidget
) 0;
303 void wxMenuItem::SetText(const wxString
& label
)
305 char mnem
= wxFindMnemonic (label
);
306 wxString label2
= wxStripMenuCodes(label
);
312 wxXmString
label_str(label2
);
313 XtVaSetValues ((Widget
) m_buttonWidget
,
314 XmNlabelString
, label_str(),
317 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmnemonic
, mnem
, NULL
);
318 char *accel
= wxFindAccelerator (label2
);
320 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
322 XmString accel_str
= wxFindAcceleratorText (label2
);
325 XtVaSetValues ((Widget
) m_buttonWidget
, XmNacceleratorText
, accel_str
, NULL
);
326 XmStringFree (accel_str
);
331 // ----------------------------------------------------------------------------
333 // ----------------------------------------------------------------------------
335 void wxMenuItemCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
336 XtPointer
WXUNUSED(ptr
))
338 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
341 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, item
->GetId());
342 event
.SetInt( item
->GetId() );
344 if (item
->IsCheckable())
346 Boolean isChecked
= false;
347 XtVaGetValues ((Widget
) item
->GetButtonWidget(),
351 // only set the flag, don't actually check anything
352 item
->wxMenuItemBase::Check(isChecked
);
353 event
.SetInt(isChecked
);
356 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
358 event
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
360 item
->GetMenuBar()->GetMenuBarFrame()
361 ->GetEventHandler()->ProcessEvent(event
);
363 // this is the child of a popup menu
364 else if (item
->GetTopMenu())
366 event
.SetEventObject(item
->GetTopMenu());
368 item
->GetTopMenu()->ProcessCommand (event
);
370 // Since PopupMenu under Motif still grab right mouse
371 // button events after it was closed, we need to delete
372 // the associated widgets to allow next PopUpMenu to
373 // appear; this needs to be done there because doing it in
374 // a WorkProc as before may cause crashes if a menu item causes
375 // the parent window of the menu to be destroyed
376 item
->GetTopMenu()->DestroyWidgetAndDetach();
381 void wxMenuItemArmCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
382 XtPointer
WXUNUSED(ptr
))
384 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
387 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
389 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, item
->GetId());
390 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
392 item
->GetMenuBar()->GetMenuBarFrame()
393 ->GetEventHandler()->ProcessEvent(menuEvent
);
399 wxMenuItemDisarmCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
400 XtPointer
WXUNUSED(ptr
))
402 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
405 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
407 // TODO: not sure this is correct, since -1 means something
408 // special to event system
409 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, -1);
410 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
412 item
->GetMenuBar()->GetMenuBarFrame()
413 ->GetEventHandler()->ProcessEvent(menuEvent
);