]> git.saurik.com Git - wxWidgets.git/blob - src/motif/menuitem.cpp
c680eaeaadd7d25aab7401913e603b3e8226681e
[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,
162 wxMenu * topMenu, size_t index)
163 {
164 m_menuBar = menuBar;
165 m_topMenu = topMenu;
166
167 if (GetId() == -3)
168 {
169 // Id=-3 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 #ifdef XmNpositionIndex
182 XmNpositionIndex, index,
183 #endif
184 NULL);
185 XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
186 }
187 else
188 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
189 xmPushButtonGadgetClass, (Widget) menu,
190 #ifdef XmNpositionIndex
191 XmNpositionIndex, index,
192 #endif
193 NULL);
194 char mnem = wxFindMnemonic (m_text);
195 if (mnem != 0)
196 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
197
198 //// TODO: proper accelerator treatment. What does wxFindAccelerator
199 //// look for?
200 strName = m_text;
201 char *accel = wxFindAccelerator (strName);
202 if (accel)
203 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
204
205 // TODO: What does this do?
206 XmString accel_str = wxFindAcceleratorText (strName);
207 if (accel_str)
208 {
209 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
210 XmStringFree (accel_str);
211 }
212
213 if (IsCheckable())
214 XtAddCallback ((Widget) m_buttonWidget,
215 XmNvalueChangedCallback,
216 (XtCallbackProc) wxMenuItemCallback,
217 (XtPointer) this);
218 else
219 XtAddCallback ((Widget) m_buttonWidget,
220 XmNactivateCallback,
221 (XtCallbackProc) wxMenuItemCallback,
222 (XtPointer) this);
223 XtAddCallback ((Widget) m_buttonWidget,
224 XmNarmCallback,
225 (XtCallbackProc) wxMenuItemArmCallback,
226 (XtPointer) this);
227 XtAddCallback ((Widget) m_buttonWidget,
228 XmNdisarmCallback,
229 (XtCallbackProc) wxMenuItemDisarmCallback,
230 (XtPointer) this);
231 }
232 else if (GetId() == wxID_SEPARATOR)
233 {
234 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
235 xmSeparatorGadgetClass, (Widget) menu,
236 #ifndef XmNpositionIndex
237 XmNpositionIndex, index,
238 #endif
239 NULL);
240 }
241 else if (m_subMenu)
242 {
243 m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, index, m_text, true);
244 m_subMenu->SetButtonWidget(m_buttonWidget);
245 XtAddCallback ((Widget) m_buttonWidget,
246 XmNcascadingCallback,
247 (XtCallbackProc) wxMenuItemArmCallback,
248 (XtPointer) this);
249 }
250 if (m_buttonWidget)
251 XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled());
252 }
253
254 void wxMenuItem::DestroyItem(bool full)
255 {
256 if (GetId() == -3)
257 {
258 ; // Nothing
259
260 }
261 else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu)
262 {
263 if (m_buttonWidget)
264 {
265 if (IsCheckable())
266 XtRemoveCallback ((Widget) m_buttonWidget, XmNvalueChangedCallback,
267 wxMenuItemCallback, (XtPointer) this);
268 else
269 XtRemoveCallback ((Widget) m_buttonWidget, XmNactivateCallback,
270 wxMenuItemCallback, (XtPointer) this);
271 XtRemoveCallback ((Widget) m_buttonWidget, XmNarmCallback,
272 wxMenuItemArmCallback, (XtPointer) this);
273 XtRemoveCallback ((Widget) m_buttonWidget, XmNdisarmCallback,
274 wxMenuItemDisarmCallback, (XtPointer) this);
275 }
276 }
277 else if (GetId() == wxID_SEPARATOR)
278 {
279 ; // Nothing
280
281 }
282 else if (GetSubMenu())
283 {
284 if (m_buttonWidget)
285 {
286 XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback,
287 wxMenuItemArmCallback, (XtPointer) this);
288 }
289 m_subMenu->DestroyMenu(full);
290 if (full)
291 m_buttonWidget = NULL;
292 }
293
294 if (m_buttonWidget && full)
295 {
296 XtDestroyWidget ((Widget) m_buttonWidget);
297 m_buttonWidget = (WXWidget) 0;
298 }
299 }
300
301 void wxMenuItem::SetText(const wxString& label)
302 {
303 char mnem = wxFindMnemonic (label);
304 wxString label2 = wxStripMenuCodes(label);
305
306 m_text = label;
307
308 if (m_buttonWidget)
309 {
310 wxXmString label_str(label2);
311 XtVaSetValues ((Widget) m_buttonWidget,
312 XmNlabelString, label_str(),
313 NULL);
314 if (mnem != 0)
315 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
316 char *accel = wxFindAccelerator (label2);
317 if (accel)
318 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
319
320 XmString accel_str = wxFindAcceleratorText (label2);
321 if (accel_str)
322 {
323 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
324 XmStringFree (accel_str);
325 }
326 }
327 }
328
329 // ----------------------------------------------------------------------------
330 // Motif callbacks
331 // ----------------------------------------------------------------------------
332
333 void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
334 XtPointer WXUNUSED(ptr))
335 {
336 wxMenuItem *item = (wxMenuItem *) clientData;
337 if (item)
338 {
339 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
340 event.SetInt( item->GetId() );
341
342 if (item->IsCheckable())
343 {
344 Boolean isChecked = false;
345 XtVaGetValues ((Widget) item->GetButtonWidget(),
346 XmNset, & isChecked,
347 NULL);
348
349 // only set the flag, don't actually check anything
350 item->wxMenuItemBase::Check(isChecked);
351 event.SetInt(isChecked);
352 }
353
354 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
355 {
356 event.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
357
358 item->GetMenuBar()->GetMenuBarFrame()
359 ->GetEventHandler()->ProcessEvent(event);
360 }
361 // this is the child of a popup menu
362 else if (item->GetTopMenu())
363 {
364 event.SetEventObject(item->GetTopMenu());
365
366 item->GetTopMenu()->ProcessCommand (event);
367
368 // Since PopupMenu under Motif still grab right mouse
369 // button events after it was closed, we need to delete
370 // the associated widgets to allow next PopUpMenu to
371 // appear; this needs to be done there because doing it in
372 // a WorkProc as before may cause crashes if a menu item causes
373 // the parent window of the menu to be destroyed
374 item->GetTopMenu()->DestroyWidgetAndDetach();
375 }
376 }
377 }
378
379 void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
380 XtPointer WXUNUSED(ptr))
381 {
382 wxMenuItem *item = (wxMenuItem *) clientData;
383 if (item)
384 {
385 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
386 {
387 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
388 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
389
390 item->GetMenuBar()->GetMenuBarFrame()
391 ->GetEventHandler()->ProcessEvent(menuEvent);
392 }
393 }
394 }
395
396 void
397 wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
398 XtPointer WXUNUSED(ptr))
399 {
400 wxMenuItem *item = (wxMenuItem *) clientData;
401 if (item)
402 {
403 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
404 {
405 // TODO: not sure this is correct, since -1 means something
406 // special to event system
407 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, -1);
408 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
409
410 item->GetMenuBar()->GetMenuBarFrame()
411 ->GetEventHandler()->ProcessEvent(menuEvent);
412 }
413 }
414 }
415