]> git.saurik.com Git - wxWidgets.git/blob - src/motif/menuitem.cpp
remove extraneous semicolons (patch 1299687)
[wxWidgets.git] / src / motif / menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #include "wx/defs.h"
24
25 #include "wx/menu.h"
26 #include "wx/menuitem.h"
27 #include "wx/utils.h"
28 #include "wx/frame.h"
29
30 #ifdef __VMS__
31 #pragma message disable nosimpint
32 #endif
33 #include <Xm/Label.h>
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>
42 #ifdef __VMS__
43 #pragma message enable nosimpint
44 #endif
45
46 #include "wx/motif/private.h"
47
48 // ----------------------------------------------------------------------------
49 // functions prototypes
50 // ----------------------------------------------------------------------------
51
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);
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // dynamic classes implementation
62 // ----------------------------------------------------------------------------
63
64 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
65
66 // ----------------------------------------------------------------------------
67 // wxMenuItem
68 // ----------------------------------------------------------------------------
69
70 // ctor & dtor
71 // -----------
72
73 wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
74 int id,
75 const wxString& strName,
76 const wxString& strHelp,
77 wxItemKind kind,
78 wxMenu *pSubMenu)
79 : wxMenuItemBase(pParentMenu, id, strName, strHelp, kind, pSubMenu)
80 {
81 // Motif-specific
82 m_menuBar = NULL;
83 m_buttonWidget = (WXWidget) NULL;
84 m_topMenu = NULL;
85 }
86
87 wxMenuItem::~wxMenuItem()
88 {
89 }
90
91 // change item state
92 // -----------------
93
94 void wxMenuItem::Enable(bool bDoEnable)
95 {
96 if ( m_isEnabled != bDoEnable )
97 {
98 if ( !IsSubMenu() )
99 {
100 // normal menu item
101 if (m_buttonWidget)
102 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
103 }
104 else // submenu
105 {
106 // Maybe we should apply this to all items in the submenu?
107 // Or perhaps it works anyway.
108 if (m_buttonWidget)
109 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
110 }
111
112 wxMenuItemBase::Enable(bDoEnable);
113 }
114 }
115
116 void wxMenuItem::Check(bool bDoCheck)
117 {
118 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
119
120 if ( m_isChecked != bDoCheck )
121 {
122 if ( m_buttonWidget )
123 {
124 wxASSERT_MSG( XtIsSubclass((Widget)m_buttonWidget,
125 xmToggleButtonGadgetClass),
126 wxT("checkable menu item must be a toggle button") );
127
128 XtVaSetValues((Widget)m_buttonWidget,
129 XmNset, (Boolean)bDoCheck,
130 NULL);
131 }
132
133 wxMenuItemBase::Check(bDoCheck);
134 }
135 }
136
137 /* static */
138 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
139 {
140 return wxStripMenuCodes(text);
141 }
142
143 // ----------------------------------------------------------------------------
144 // wxMenuItemBase
145 // ----------------------------------------------------------------------------
146
147 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
148 int id,
149 const wxString& name,
150 const wxString& help,
151 wxItemKind kind,
152 wxMenu *subMenu)
153 {
154 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
155 }
156
157 // ----------------------------------------------------------------------------
158 // Motif-specific
159 // ----------------------------------------------------------------------------
160
161 void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu)
162 {
163 m_menuBar = menuBar;
164 m_topMenu = topMenu;
165
166 if (GetId() == -3)
167 {
168 // Id=-3 identifies a Title item.
169 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
170 (wxStripMenuCodes(m_text),
171 xmLabelGadgetClass, (Widget) menu, NULL);
172 }
173 else if ((!m_text.IsNull() && m_text != "") && (!m_subMenu))
174 {
175 wxString strName = wxStripMenuCodes(m_text);
176 if (IsCheckable())
177 {
178 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
179 xmToggleButtonGadgetClass, (Widget) menu,
180 NULL);
181 XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
182 }
183 else
184 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
185 xmPushButtonGadgetClass, (Widget) menu,
186 NULL);
187 char mnem = wxFindMnemonic (m_text);
188 if (mnem != 0)
189 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
190
191 //// TODO: proper accelerator treatment. What does wxFindAccelerator
192 //// look for?
193 strName = m_text;
194 char *accel = wxFindAccelerator (strName);
195 if (accel)
196 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
197
198 // TODO: What does this do?
199 XmString accel_str = wxFindAcceleratorText (strName);
200 if (accel_str)
201 {
202 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
203 XmStringFree (accel_str);
204 }
205
206 if (IsCheckable())
207 XtAddCallback ((Widget) m_buttonWidget,
208 XmNvalueChangedCallback,
209 (XtCallbackProc) wxMenuItemCallback,
210 (XtPointer) this);
211 else
212 XtAddCallback ((Widget) m_buttonWidget,
213 XmNactivateCallback,
214 (XtCallbackProc) wxMenuItemCallback,
215 (XtPointer) this);
216 XtAddCallback ((Widget) m_buttonWidget,
217 XmNarmCallback,
218 (XtCallbackProc) wxMenuItemArmCallback,
219 (XtPointer) this);
220 XtAddCallback ((Widget) m_buttonWidget,
221 XmNdisarmCallback,
222 (XtCallbackProc) wxMenuItemDisarmCallback,
223 (XtPointer) this);
224 }
225 else if (GetId() == wxID_SEPARATOR)
226 {
227 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
228 xmSeparatorGadgetClass, (Widget) menu, NULL);
229 }
230 else if (m_subMenu)
231 {
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,
237 (XtPointer) this);
238 }
239 if (m_buttonWidget)
240 XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled());
241 }
242
243 void wxMenuItem::DestroyItem(bool full)
244 {
245 if (GetId() == -3)
246 {
247 ; // Nothing
248
249 }
250 else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu)
251 {
252 if (m_buttonWidget)
253 {
254 if (IsCheckable())
255 XtRemoveCallback ((Widget) m_buttonWidget, XmNvalueChangedCallback,
256 wxMenuItemCallback, (XtPointer) this);
257 else
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);
264 }
265 }
266 else if (GetId() == wxID_SEPARATOR)
267 {
268 ; // Nothing
269
270 }
271 else if (GetSubMenu())
272 {
273 if (m_buttonWidget)
274 {
275 XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback,
276 wxMenuItemArmCallback, (XtPointer) this);
277 }
278 m_subMenu->DestroyMenu(full);
279 if (full)
280 m_buttonWidget = NULL;
281 }
282
283 if (m_buttonWidget && full)
284 {
285 XtDestroyWidget ((Widget) m_buttonWidget);
286 m_buttonWidget = (WXWidget) 0;
287 }
288 }
289
290 void wxMenuItem::SetText(const wxString& label)
291 {
292 char mnem = wxFindMnemonic (label);
293 wxString label2 = wxStripMenuCodes(label);
294
295 m_text = label;
296
297 if (m_buttonWidget)
298 {
299 wxXmString label_str(label2);
300 XtVaSetValues ((Widget) m_buttonWidget,
301 XmNlabelString, label_str(),
302 NULL);
303 if (mnem != 0)
304 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
305 char *accel = wxFindAccelerator (label2);
306 if (accel)
307 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
308
309 XmString accel_str = wxFindAcceleratorText (label2);
310 if (accel_str)
311 {
312 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
313 XmStringFree (accel_str);
314 }
315 }
316 }
317
318 // ----------------------------------------------------------------------------
319 // Motif callbacks
320 // ----------------------------------------------------------------------------
321
322 void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
323 XtPointer WXUNUSED(ptr))
324 {
325 wxMenuItem *item = (wxMenuItem *) clientData;
326 if (item)
327 {
328 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
329 event.SetInt( item->GetId() );
330
331 if (item->IsCheckable())
332 {
333 Boolean isChecked = false;
334 XtVaGetValues ((Widget) item->GetButtonWidget(),
335 XmNset, & isChecked,
336 NULL);
337
338 // only set the flag, don't actually check anything
339 item->wxMenuItemBase::Check(isChecked);
340 event.SetInt(isChecked);
341 }
342
343 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
344 {
345 event.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
346
347 item->GetMenuBar()->GetMenuBarFrame()
348 ->GetEventHandler()->ProcessEvent(event);
349 }
350 // this is the child of a popup menu
351 else if (item->GetTopMenu())
352 {
353 event.SetEventObject(item->GetTopMenu());
354
355 item->GetTopMenu()->ProcessCommand (event);
356
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();
364 }
365 }
366 }
367
368 void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
369 XtPointer WXUNUSED(ptr))
370 {
371 wxMenuItem *item = (wxMenuItem *) clientData;
372 if (item)
373 {
374 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
375 {
376 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
377 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
378
379 item->GetMenuBar()->GetMenuBarFrame()
380 ->GetEventHandler()->ProcessEvent(menuEvent);
381 }
382 }
383 }
384
385 void
386 wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
387 XtPointer WXUNUSED(ptr))
388 {
389 wxMenuItem *item = (wxMenuItem *) clientData;
390 if (item)
391 {
392 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
393 {
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());
398
399 item->GetMenuBar()->GetMenuBarFrame()
400 ->GetEventHandler()->ProcessEvent(menuEvent);
401 }
402 }
403 }
404