1 ///////////////////////////////////////////////////////////////////////////////
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"
26 #include "wx/menuitem.h"
31 #pragma message disable nosimpint
34 #include <Xm/LabelG.h>
35 #include <Xm/CascadeBG.h>
36 #include <Xm/CascadeB.h>
37 #include <Xm/SeparatoG.h>
38 #include <Xm/PushBG.h>
39 #include <Xm/ToggleB.h>
40 #include <Xm/ToggleBG.h>
41 #include <Xm/RowColumn.h>
43 #pragma message enable nosimpint
46 #include "wx/motif/private.h"
48 // ----------------------------------------------------------------------------
49 // functions prototypes
50 // ----------------------------------------------------------------------------
52 static void wxMenuItemCallback(Widget w
, XtPointer clientData
, XtPointer ptr
);
53 static void wxMenuItemArmCallback(Widget w
, XtPointer clientData
, XtPointer ptr
);
54 static void wxMenuItemDisarmCallback(Widget w
, XtPointer clientData
, XtPointer ptr
);
56 // ============================================================================
58 // ============================================================================
60 // ----------------------------------------------------------------------------
61 // dynamic classes implementation
62 // ----------------------------------------------------------------------------
64 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
73 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
75 const wxString
& strName
,
76 const wxString
& strHelp
,
79 : wxMenuItemBase(pParentMenu
, id
, strName
, strHelp
, kind
, pSubMenu
)
83 m_buttonWidget
= (WXWidget
) NULL
;
87 wxMenuItem::~wxMenuItem()
94 void wxMenuItem::Enable(bool bDoEnable
)
96 if ( m_isEnabled
!= bDoEnable
)
102 XtSetSensitive( (Widget
) m_buttonWidget
, (Boolean
) bDoEnable
);
106 // Maybe we should apply this to all items in the submenu?
107 // Or perhaps it works anyway.
109 XtSetSensitive( (Widget
) m_buttonWidget
, (Boolean
) bDoEnable
);
112 wxMenuItemBase::Enable(bDoEnable
);
116 void wxMenuItem::Check(bool bDoCheck
)
118 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
120 if ( m_isChecked
!= bDoCheck
)
122 if ( m_buttonWidget
)
124 wxASSERT_MSG( XtIsSubclass((Widget
)m_buttonWidget
,
125 xmToggleButtonGadgetClass
),
126 wxT("checkable menu item must be a toggle button") );
128 XtVaSetValues((Widget
)m_buttonWidget
,
129 XmNset
, (Boolean
)bDoCheck
,
133 wxMenuItemBase::Check(bDoCheck
);
138 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
140 return wxStripMenuCodes(text
);
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
149 const wxString
& name
,
150 const wxString
& help
,
154 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 void wxMenuItem::CreateItem (WXWidget menu
, wxMenuBar
* menuBar
, wxMenu
* topMenu
)
168 // Id=-3 identifies a Title item.
169 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget
170 (wxStripMenuCodes(m_text
),
171 xmLabelGadgetClass
, (Widget
) menu
, NULL
);
173 else if ((!m_text
.IsNull() && m_text
!= "") && (!m_subMenu
))
175 wxString strName
= wxStripMenuCodes(m_text
);
178 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (strName
,
179 xmToggleButtonGadgetClass
, (Widget
) menu
,
181 XtVaSetValues ((Widget
) m_buttonWidget
, XmNset
, (Boolean
) IsChecked(), NULL
);
184 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (strName
,
185 xmPushButtonGadgetClass
, (Widget
) menu
,
187 char mnem
= wxFindMnemonic (m_text
);
189 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmnemonic
, mnem
, NULL
);
191 //// TODO: proper accelerator treatment. What does wxFindAccelerator
194 char *accel
= wxFindAccelerator (strName
);
196 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
198 // TODO: What does this do?
199 XmString accel_str
= wxFindAcceleratorText (strName
);
202 XtVaSetValues ((Widget
) m_buttonWidget
, XmNacceleratorText
, accel_str
, NULL
);
203 XmStringFree (accel_str
);
207 XtAddCallback ((Widget
) m_buttonWidget
,
208 XmNvalueChangedCallback
,
209 (XtCallbackProc
) wxMenuItemCallback
,
212 XtAddCallback ((Widget
) m_buttonWidget
,
214 (XtCallbackProc
) wxMenuItemCallback
,
216 XtAddCallback ((Widget
) m_buttonWidget
,
218 (XtCallbackProc
) wxMenuItemArmCallback
,
220 XtAddCallback ((Widget
) m_buttonWidget
,
222 (XtCallbackProc
) wxMenuItemDisarmCallback
,
225 else if (GetId() == wxID_SEPARATOR
)
227 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget ("separator",
228 xmSeparatorGadgetClass
, (Widget
) menu
, NULL
);
232 m_buttonWidget
= m_subMenu
->CreateMenu (menuBar
, menu
, topMenu
, m_text
, true);
233 m_subMenu
->SetButtonWidget(m_buttonWidget
);
234 XtAddCallback ((Widget
) m_buttonWidget
,
235 XmNcascadingCallback
,
236 (XtCallbackProc
) wxMenuItemArmCallback
,
240 XtSetSensitive ((Widget
) m_buttonWidget
, (Boolean
) IsEnabled());
243 void wxMenuItem::DestroyItem(bool full
)
250 else if ((!m_text
.IsNull() && (m_text
!= "")) && !m_subMenu
)
255 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNvalueChangedCallback
,
256 wxMenuItemCallback
, (XtPointer
) this);
258 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNactivateCallback
,
259 wxMenuItemCallback
, (XtPointer
) this);
260 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNarmCallback
,
261 wxMenuItemArmCallback
, (XtPointer
) this);
262 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNdisarmCallback
,
263 wxMenuItemDisarmCallback
, (XtPointer
) this);
266 else if (GetId() == wxID_SEPARATOR
)
271 else if (GetSubMenu())
275 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNcascadingCallback
,
276 wxMenuItemArmCallback
, (XtPointer
) this);
278 m_subMenu
->DestroyMenu(full
);
280 m_buttonWidget
= NULL
;
283 if (m_buttonWidget
&& full
)
285 XtDestroyWidget ((Widget
) m_buttonWidget
);
286 m_buttonWidget
= (WXWidget
) 0;
290 void wxMenuItem::SetText(const wxString
& label
)
292 char mnem
= wxFindMnemonic (label
);
293 wxString label2
= wxStripMenuCodes(label
);
299 wxXmString
label_str(label2
);
300 XtVaSetValues ((Widget
) m_buttonWidget
,
301 XmNlabelString
, label_str(),
304 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmnemonic
, mnem
, NULL
);
305 char *accel
= wxFindAccelerator (label2
);
307 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
309 XmString accel_str
= wxFindAcceleratorText (label2
);
312 XtVaSetValues ((Widget
) m_buttonWidget
, XmNacceleratorText
, accel_str
, NULL
);
313 XmStringFree (accel_str
);
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 void wxMenuItemCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
323 XtPointer
WXUNUSED(ptr
))
325 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
328 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, item
->GetId());
329 event
.SetInt( item
->GetId() );
331 if (item
->IsCheckable())
333 Boolean isChecked
= false;
334 XtVaGetValues ((Widget
) item
->GetButtonWidget(),
338 // only set the flag, don't actually check anything
339 item
->wxMenuItemBase::Check(isChecked
);
340 event
.SetInt(isChecked
);
343 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
345 event
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
347 item
->GetMenuBar()->GetMenuBarFrame()
348 ->GetEventHandler()->ProcessEvent(event
);
350 // this is the child of a popup menu
351 else if (item
->GetTopMenu())
353 event
.SetEventObject(item
->GetTopMenu());
355 item
->GetTopMenu()->ProcessCommand (event
);
357 // Since PopupMenu under Motif still grab right mouse
358 // button events after it was closed, we need to delete
359 // the associated widgets to allow next PopUpMenu to
360 // appear; this needs to be done there because doing it in
361 // a WorkProc as before may cause crashes if a menu item causes
362 // the parent window of the menu to be destroyed
363 item
->GetTopMenu()->DestroyWidgetAndDetach();
368 void wxMenuItemArmCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
369 XtPointer
WXUNUSED(ptr
))
371 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
374 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
376 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, item
->GetId());
377 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
379 item
->GetMenuBar()->GetMenuBarFrame()
380 ->GetEventHandler()->ProcessEvent(menuEvent
);
386 wxMenuItemDisarmCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
387 XtPointer
WXUNUSED(ptr
))
389 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
392 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
394 // TODO: not sure this is correct, since -1 means something
395 // special to event system
396 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, -1);
397 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
399 item
->GetMenuBar()->GetMenuBarFrame()
400 ->GetEventHandler()->ProcessEvent(menuEvent
);