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