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
;
76 m_buttonWidget
= (WXWidget
) NULL
;
80 wxMenuItem::~wxMenuItem()
87 // delete the sub menu
88 void wxMenuItem::DeleteSubMenu()
90 wxASSERT( m_pSubMenu
!= NULL
);
99 void wxMenuItem::Enable(bool bDoEnable
)
101 if ( m_bEnabled
!= bDoEnable
)
103 if ( m_pSubMenu
== NULL
)
104 { // normal menu item
106 XtSetSensitive( (Widget
) m_buttonWidget
, (Boolean
) bDoEnable
);
110 // Maybe we should apply this to all items in the submenu?
111 // Or perhaps it works anyway.
113 XtSetSensitive( (Widget
) m_buttonWidget
, (Boolean
) bDoEnable
);
116 m_bEnabled
= bDoEnable
;
120 void wxMenuItem::Check(bool bDoCheck
)
122 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
124 if ( m_bChecked
!= bDoCheck
)
126 if (m_buttonWidget
&& XtIsSubclass ((Widget
) m_buttonWidget
, xmToggleButtonGadgetClass
))
128 XtVaSetValues ( (Widget
) m_buttonWidget
, XmNset
, (Boolean
) bDoCheck
, NULL
);
130 m_bChecked
= bDoCheck
;
136 void wxMenuItem::CreateItem (WXWidget menu
, wxMenuBar
* menuBar
, wxMenu
* topMenu
)
143 // Id=-2 identifies a Title item.
144 wxStripMenuCodes ((char*) (const char*) m_strName
, wxBuffer
);
145 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (wxBuffer
,
146 xmLabelGadgetClass
, (Widget
) menu
, NULL
);
148 else if ((!m_strName
.IsNull() && m_strName
!= "") && (!m_pSubMenu
))
150 wxStripMenuCodes ((char*) (const char*) m_strName
, wxBuffer
);
153 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (wxBuffer
,
154 xmToggleButtonGadgetClass
, (Widget
) menu
,
156 XtVaSetValues ((Widget
) m_buttonWidget
, XmNset
, (Boolean
) IsChecked(), NULL
);
159 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (wxBuffer
,
160 xmPushButtonGadgetClass
, (Widget
) menu
,
162 char mnem
= wxFindMnemonic (m_strName
);
164 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmnemonic
, mnem
, NULL
);
166 //// TODO: proper accelerator treatment. What does wxFindAccelerator
168 strcpy(wxBuffer
, (char*) (const char*) m_strName
);
169 char *accel
= wxFindAccelerator (wxBuffer
);
171 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
173 // TODO: What does this do?
174 strcpy(wxBuffer
, (char*) (const char*) m_strName
);
175 XmString accel_str
= wxFindAcceleratorText (wxBuffer
);
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 wxStripMenuCodes ((char*) (const char*) label
, wxBuffer
);
275 XmString label_str
= XmStringCreateSimple (wxBuffer
);
276 XtVaSetValues ((Widget
) m_buttonWidget
,
277 XmNlabelString
, label_str
,
279 XmStringFree (label_str
);
281 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmnemonic
, mnem
, NULL
);
282 strcpy(wxBuffer
, (char*) (const char*) label
);
283 char *accel
= wxFindAccelerator (wxBuffer
);
285 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
287 strcpy(wxBuffer
, (char*) (const char*) label
);
288 XmString accel_str
= wxFindAcceleratorText (wxBuffer
);
291 XtVaSetValues ((Widget
) m_buttonWidget
, XmNacceleratorText
, accel_str
, NULL
);
292 XmStringFree (accel_str
);
297 void wxMenuItemCallback (Widget w
, XtPointer clientData
,
300 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
303 if (item
->IsCheckable())
305 Boolean isChecked
= FALSE
;
306 XtVaGetValues ((Widget
) item
->GetButtonWidget(), XmNset
, & isChecked
, NULL
);
307 item
->SetChecked(isChecked
);
309 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
311 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, item
->GetId());
312 commandEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
314 item
->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(commandEvent
);
316 else if (item
->GetTopMenu())
318 wxCommandEvent
event (wxEVT_COMMAND_MENU_SELECTED
, item
->GetId());
319 event
.SetEventObject(item
->GetTopMenu());
320 item
->GetTopMenu()->ProcessCommand (event
);
326 wxMenuItemArmCallback (Widget w
, XtPointer clientData
,
329 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
332 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
334 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, item
->GetId());
335 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
337 item
->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent
);
343 wxMenuItemDisarmCallback (Widget w
, XtPointer clientData
,
346 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
349 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
351 // TODO: not sure this is correct, since -1 means something
352 // special to event system
353 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, -1);
354 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
356 item
->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent
);