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 // ----------------------------------------------------------------------------
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
);
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
143 const wxString
& name
,
144 const wxString
& help
,
148 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 void wxMenuItem::CreateItem (WXWidget menu
, wxMenuBar
* menuBar
,
156 wxMenu
* topMenu
, size_t index
)
163 // Id=-3 identifies a Title item.
164 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget
165 (wxStripMenuCodes(m_text
),
166 xmLabelGadgetClass
, (Widget
) menu
, NULL
);
168 else if (!IsSeparator() && !m_subMenu
)
170 wxString txt
= m_text
;
172 if (m_text
.IsEmpty())
174 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
175 txt
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
178 wxString strName
= wxStripMenuCodes(txt
);
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 (IsSeparator())
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 (IsSeparator())
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::SetItemLabel(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 ->HandleWindowEvent(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 ->HandleWindowEvent(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 ->HandleWindowEvent(menuEvent
);