1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
17 #include "wx/menuitem.h"
22 #include <Xm/LabelG.h>
23 #include <Xm/CascadeBG.h>
24 #include <Xm/CascadeB.h>
25 #include <Xm/SeparatoG.h>
26 #include <Xm/PushBG.h>
27 #include <Xm/ToggleB.h>
28 #include <Xm/ToggleBG.h>
29 #include <Xm/RowColumn.h>
31 #include "wx/motif/private.h"
33 void wxMenuItemCallback (Widget w
, XtPointer clientData
,
35 void wxMenuItemArmCallback (Widget w
, XtPointer clientData
,
37 void wxMenuItemDisarmCallback (Widget w
, XtPointer clientData
,
40 // ============================================================================
42 // ============================================================================
44 // ----------------------------------------------------------------------------
45 // dynamic classes implementation
46 // ----------------------------------------------------------------------------
48 #if !USE_SHARED_LIBRARY
49 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
50 #endif //USE_SHARED_LIBRARY
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
59 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
, int id
,
60 const wxString
& strName
, const wxString
& strHelp
,
64 m_bCheckable(bCheckable
),
67 wxASSERT( pParentMenu
!= NULL
);
69 m_pParentMenu
= pParentMenu
;
70 m_pSubMenu
= pSubMenu
;
77 m_buttonWidget
= (WXWidget
) NULL
;
81 wxMenuItem::~wxMenuItem()
88 // delete the sub menu
89 void wxMenuItem::DeleteSubMenu()
91 wxASSERT( m_pSubMenu
!= NULL
);
100 void wxMenuItem::Enable(bool bDoEnable
)
102 if ( m_bEnabled
!= bDoEnable
)
104 if ( m_pSubMenu
== NULL
)
105 { // normal menu item
107 XtSetSensitive( (Widget
) m_buttonWidget
, (Boolean
) bDoEnable
);
111 // Maybe we should apply this to all items in the submenu?
112 // Or perhaps it works anyway.
114 XtSetSensitive( (Widget
) m_buttonWidget
, (Boolean
) bDoEnable
);
117 m_bEnabled
= bDoEnable
;
121 void wxMenuItem::Check(bool bDoCheck
)
123 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
125 if ( m_bChecked
!= bDoCheck
)
127 if (m_buttonWidget
&& XtIsSubclass ((Widget
) m_buttonWidget
, xmToggleButtonGadgetClass
))
129 XtVaSetValues ( (Widget
) m_buttonWidget
, XmNset
, (Boolean
) bDoCheck
, NULL
);
131 m_bChecked
= bDoCheck
;
137 void wxMenuItem::CreateItem (WXWidget menu
, wxMenuBar
* menuBar
, wxMenu
* topMenu
)
144 // Id=-2 identifies a Title item.
145 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget
146 (wxStripMenuCodes(m_strName
),
147 xmLabelGadgetClass
, (Widget
) menu
, NULL
);
149 else if ((!m_strName
.IsNull() && m_strName
!= "") && (!m_pSubMenu
))
151 wxString strName
= wxStripMenuCodes(m_strName
);
154 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (strName
,
155 xmToggleButtonGadgetClass
, (Widget
) menu
,
157 XtVaSetValues ((Widget
) m_buttonWidget
, XmNset
, (Boolean
) IsChecked(), NULL
);
160 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (strName
,
161 xmPushButtonGadgetClass
, (Widget
) menu
,
163 char mnem
= wxFindMnemonic (m_strName
);
165 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmnemonic
, mnem
, NULL
);
167 //// TODO: proper accelerator treatment. What does wxFindAccelerator
170 char *accel
= wxFindAccelerator (strName
);
172 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
174 // TODO: What does this do?
175 XmString accel_str
= wxFindAcceleratorText (strName
);
178 XtVaSetValues ((Widget
) m_buttonWidget
, XmNacceleratorText
, accel_str
, NULL
);
179 XmStringFree (accel_str
);
183 XtAddCallback ((Widget
) m_buttonWidget
,
184 XmNvalueChangedCallback
,
185 (XtCallbackProc
) wxMenuItemCallback
,
188 XtAddCallback ((Widget
) m_buttonWidget
,
190 (XtCallbackProc
) wxMenuItemCallback
,
192 XtAddCallback ((Widget
) m_buttonWidget
,
194 (XtCallbackProc
) wxMenuItemArmCallback
,
196 XtAddCallback ((Widget
) m_buttonWidget
,
198 (XtCallbackProc
) wxMenuItemDisarmCallback
,
201 else if (GetId() == -1)
203 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget ("separator",
204 xmSeparatorGadgetClass
, (Widget
) menu
, NULL
);
208 m_buttonWidget
= m_pSubMenu
->CreateMenu (menuBar
, menu
, topMenu
, m_strName
, TRUE
);
209 m_pSubMenu
->SetButtonWidget(m_buttonWidget
);
210 XtAddCallback ((Widget
) m_buttonWidget
,
211 XmNcascadingCallback
,
212 (XtCallbackProc
) wxMenuItemArmCallback
,
216 XtSetSensitive ((Widget
) m_buttonWidget
, (Boolean
) IsEnabled());
219 void wxMenuItem::DestroyItem(bool full
)
226 else if ((!m_strName
.IsNull() && (m_strName
!= "")) && !m_pSubMenu
)
231 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNvalueChangedCallback
,
232 wxMenuItemCallback
, (XtPointer
) this);
234 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNactivateCallback
,
235 wxMenuItemCallback
, (XtPointer
) this);
236 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNarmCallback
,
237 wxMenuItemArmCallback
, (XtPointer
) this);
238 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNdisarmCallback
,
239 wxMenuItemDisarmCallback
, (XtPointer
) this);
242 else if (GetId() == -1)
247 else if (GetSubMenu())
251 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNcascadingCallback
,
252 wxMenuItemArmCallback
, (XtPointer
) this);
254 m_pSubMenu
->DestroyMenu(full
);
256 m_buttonWidget
= NULL
;
259 if (m_buttonWidget
&& full
)
261 XtDestroyWidget ((Widget
) m_buttonWidget
);
262 m_buttonWidget
= (WXWidget
) 0;
266 void wxMenuItem::SetLabel(const wxString
& label
)
268 char mnem
= wxFindMnemonic (label
);
269 wxString label2
= wxStripMenuCodes(label
);
275 wxXmString
label_str(label2
);
276 XtVaSetValues ((Widget
) m_buttonWidget
,
277 XmNlabelString
, label_str
,
280 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmnemonic
, mnem
, NULL
);
281 char *accel
= wxFindAccelerator (label2
);
283 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
285 XmString accel_str
= wxFindAcceleratorText (label2
);
288 XtVaSetValues ((Widget
) m_buttonWidget
, XmNacceleratorText
, accel_str
, NULL
);
289 XmStringFree (accel_str
);
294 void wxMenuItemCallback (Widget w
, XtPointer clientData
,
297 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
300 if (item
->IsCheckable())
302 Boolean isChecked
= FALSE
;
303 XtVaGetValues ((Widget
) item
->GetButtonWidget(), XmNset
, & isChecked
, NULL
);
304 item
->SetChecked(isChecked
);
306 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
308 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, item
->GetId());
309 commandEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
310 commandEvent
.SetInt( item
->GetId() );
312 item
->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(commandEvent
);
314 else if (item
->GetTopMenu())
316 wxCommandEvent
event (wxEVT_COMMAND_MENU_SELECTED
, item
->GetId());
317 event
.SetEventObject(item
->GetTopMenu());
318 event
.SetInt( item
->GetId() );
320 item
->GetTopMenu()->ProcessCommand (event
);
325 void wxMenuItemArmCallback (Widget w
, XtPointer clientData
,
328 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
331 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
333 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, item
->GetId());
334 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
336 item
->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent
);
342 wxMenuItemDisarmCallback (Widget w
, XtPointer clientData
,
345 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
348 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
350 // TODO: not sure this is correct, since -1 means something
351 // special to event system
352 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, -1);
353 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
355 item
->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent
);