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 wxStripMenuCodes ((char*) (const char*) m_strName
, wxBuffer
);
146 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (wxBuffer
,
147 xmLabelGadgetClass
, (Widget
) menu
, NULL
);
149 else if ((!m_strName
.IsNull() && m_strName
!= "") && (!m_pSubMenu
))
151 wxStripMenuCodes ((char*) (const char*) m_strName
, wxBuffer
);
154 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (wxBuffer
,
155 xmToggleButtonGadgetClass
, (Widget
) menu
,
157 XtVaSetValues ((Widget
) m_buttonWidget
, XmNset
, (Boolean
) IsChecked(), NULL
);
160 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget (wxBuffer
,
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
169 strcpy(wxBuffer
, (char*) (const char*) m_strName
);
170 char *accel
= wxFindAccelerator (wxBuffer
);
172 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
174 // TODO: What does this do?
175 strcpy(wxBuffer
, (char*) (const char*) m_strName
);
176 XmString accel_str
= wxFindAcceleratorText (wxBuffer
);
179 XtVaSetValues ((Widget
) m_buttonWidget
, XmNacceleratorText
, accel_str
, NULL
);
180 XmStringFree (accel_str
);
184 XtAddCallback ((Widget
) m_buttonWidget
,
185 XmNvalueChangedCallback
,
186 (XtCallbackProc
) wxMenuItemCallback
,
189 XtAddCallback ((Widget
) m_buttonWidget
,
191 (XtCallbackProc
) wxMenuItemCallback
,
193 XtAddCallback ((Widget
) m_buttonWidget
,
195 (XtCallbackProc
) wxMenuItemArmCallback
,
197 XtAddCallback ((Widget
) m_buttonWidget
,
199 (XtCallbackProc
) wxMenuItemDisarmCallback
,
202 else if (GetId() == -1)
204 m_buttonWidget
= (WXWidget
) XtVaCreateManagedWidget ("separator",
205 xmSeparatorGadgetClass
, (Widget
) menu
, NULL
);
209 m_buttonWidget
= m_pSubMenu
->CreateMenu (menuBar
, menu
, topMenu
, m_strName
, TRUE
);
210 m_pSubMenu
->SetButtonWidget(m_buttonWidget
);
211 XtAddCallback ((Widget
) m_buttonWidget
,
212 XmNcascadingCallback
,
213 (XtCallbackProc
) wxMenuItemArmCallback
,
217 XtSetSensitive ((Widget
) m_buttonWidget
, (Boolean
) IsEnabled());
220 void wxMenuItem::DestroyItem(bool full
)
227 else if ((!m_strName
.IsNull() && (m_strName
!= "")) && !m_pSubMenu
)
232 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNvalueChangedCallback
,
233 wxMenuItemCallback
, (XtPointer
) this);
235 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNactivateCallback
,
236 wxMenuItemCallback
, (XtPointer
) this);
237 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNarmCallback
,
238 wxMenuItemArmCallback
, (XtPointer
) this);
239 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNdisarmCallback
,
240 wxMenuItemDisarmCallback
, (XtPointer
) this);
243 else if (GetId() == -1)
248 else if (GetSubMenu())
252 XtRemoveCallback ((Widget
) m_buttonWidget
, XmNcascadingCallback
,
253 wxMenuItemArmCallback
, (XtPointer
) this);
255 m_pSubMenu
->DestroyMenu(full
);
257 m_buttonWidget
= NULL
;
260 if (m_buttonWidget
&& full
)
262 XtDestroyWidget ((Widget
) m_buttonWidget
);
263 m_buttonWidget
= (WXWidget
) 0;
267 void wxMenuItem::SetLabel(const wxString
& label
)
269 char mnem
= wxFindMnemonic (label
);
270 wxStripMenuCodes ((char*) (const char*) label
, wxBuffer
);
276 XmString label_str
= XmStringCreateSimple (wxBuffer
);
277 XtVaSetValues ((Widget
) m_buttonWidget
,
278 XmNlabelString
, label_str
,
280 XmStringFree (label_str
);
282 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmnemonic
, mnem
, NULL
);
283 strcpy(wxBuffer
, (char*) (const char*) label
);
284 char *accel
= wxFindAccelerator (wxBuffer
);
286 XtVaSetValues ((Widget
) m_buttonWidget
, XmNaccelerator
, accel
, NULL
);
288 strcpy(wxBuffer
, (char*) (const char*) label
);
289 XmString accel_str
= wxFindAcceleratorText (wxBuffer
);
292 XtVaSetValues ((Widget
) m_buttonWidget
, XmNacceleratorText
, accel_str
, NULL
);
293 XmStringFree (accel_str
);
298 void wxMenuItemCallback (Widget w
, XtPointer clientData
,
301 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
304 if (item
->IsCheckable())
306 Boolean isChecked
= FALSE
;
307 XtVaGetValues ((Widget
) item
->GetButtonWidget(), XmNset
, & isChecked
, NULL
);
308 item
->SetChecked(isChecked
);
310 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
312 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, item
->GetId());
313 commandEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
314 commandEvent
.SetInt( item
->GetId() );
316 item
->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(commandEvent
);
318 else if (item
->GetTopMenu())
320 wxCommandEvent
event (wxEVT_COMMAND_MENU_SELECTED
, item
->GetId());
321 event
.SetEventObject(item
->GetTopMenu());
322 event
.SetInt( item
->GetId() );
324 item
->GetTopMenu()->ProcessCommand (event
);
329 void wxMenuItemArmCallback (Widget w
, XtPointer clientData
,
332 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
335 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
337 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, item
->GetId());
338 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
340 item
->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent
);
346 wxMenuItemDisarmCallback (Widget w
, XtPointer clientData
,
349 wxMenuItem
*item
= (wxMenuItem
*) clientData
;
352 if (item
->GetMenuBar() && item
->GetMenuBar()->GetMenuBarFrame())
354 // TODO: not sure this is correct, since -1 means something
355 // special to event system
356 wxMenuEvent
menuEvent(wxEVT_MENU_HIGHLIGHT
, -1);
357 menuEvent
.SetEventObject(item
->GetMenuBar()->GetMenuBarFrame());
359 item
->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent
);