]> git.saurik.com Git - wxWidgets.git/blob - src/motif/menuitem.cpp
reverted my Set/getTitle change
[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 #ifdef __GNUG__
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, int id,
75 const wxString& strName, const wxString& strHelp,
76 bool bCheckable,
77 wxMenu *pSubMenu)
78 {
79 wxASSERT_MSG( pParentMenu != NULL, wxT("menuitem should have a menu") );
80
81 // common init
82 m_parentMenu = pParentMenu;
83 m_subMenu = pSubMenu;
84 m_id = id;
85 m_isEnabled = TRUE;
86 m_isChecked = FALSE;
87 m_help = strHelp;
88 m_isCheckable = bCheckable;
89 m_text = strName;
90
91 // Motif-specific
92 m_menuBar = NULL;
93 m_buttonWidget = (WXWidget) NULL;
94 m_topMenu = NULL;
95 }
96
97 wxMenuItem::~wxMenuItem()
98 {
99 }
100
101 // misc
102 // ----
103
104 // delete the sub menu
105 void wxMenuItem::DeleteSubMenu()
106 {
107 wxASSERT( m_subMenu != NULL );
108
109 delete m_subMenu;
110 m_subMenu = NULL;
111 }
112
113 // change item state
114 // -----------------
115
116 void wxMenuItem::Enable(bool bDoEnable)
117 {
118 if ( m_isEnabled != bDoEnable )
119 {
120 if ( !IsSubMenu() )
121 {
122 // normal menu item
123 if (m_buttonWidget)
124 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
125 }
126 else // submenu
127 {
128 // Maybe we should apply this to all items in the submenu?
129 // Or perhaps it works anyway.
130 if (m_buttonWidget)
131 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
132 }
133
134 wxMenuItemBase::Enable(bDoEnable);
135 }
136 }
137
138 void wxMenuItem::Check(bool bDoCheck)
139 {
140 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
141
142 if ( m_isChecked != bDoCheck )
143 {
144 if ( m_buttonWidget )
145 {
146 wxASSERT_MSG( XtIsSubclass((Widget)m_buttonWidget,
147 xmToggleButtonGadgetClass),
148 wxT("checkable menu item must be a toggle button") );
149
150 XtVaSetValues((Widget)m_buttonWidget,
151 XmNset, (Boolean)bDoCheck,
152 NULL);
153 }
154
155 wxMenuItemBase::Check(bDoCheck);
156 }
157 }
158
159 /* static */
160 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
161 {
162 return wxStripMenuCodes(text);
163 }
164
165 // ----------------------------------------------------------------------------
166 // wxMenuItemBase
167 // ----------------------------------------------------------------------------
168
169 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
170 int id,
171 const wxString& name,
172 const wxString& help,
173 bool isCheckable,
174 wxMenu *subMenu)
175 {
176 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
177 }
178
179 // ----------------------------------------------------------------------------
180 // Motif-specific
181 // ----------------------------------------------------------------------------
182
183 void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu)
184 {
185 m_menuBar = menuBar;
186 m_topMenu = topMenu;
187
188 if (GetId() == -2)
189 {
190 // Id=-2 identifies a Title item.
191 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
192 (wxStripMenuCodes(m_text),
193 xmLabelGadgetClass, (Widget) menu, NULL);
194 }
195 else if ((!m_text.IsNull() && m_text != "") && (!m_subMenu))
196 {
197 wxString strName = wxStripMenuCodes(m_text);
198 if (IsCheckable())
199 {
200 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
201 xmToggleButtonGadgetClass, (Widget) menu,
202 NULL);
203 XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
204 }
205 else
206 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
207 xmPushButtonGadgetClass, (Widget) menu,
208 NULL);
209 char mnem = wxFindMnemonic (m_text);
210 if (mnem != 0)
211 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
212
213 //// TODO: proper accelerator treatment. What does wxFindAccelerator
214 //// look for?
215 strName = m_text;
216 char *accel = wxFindAccelerator (strName);
217 if (accel)
218 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
219
220 // TODO: What does this do?
221 XmString accel_str = wxFindAcceleratorText (strName);
222 if (accel_str)
223 {
224 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
225 XmStringFree (accel_str);
226 }
227
228 if (IsCheckable())
229 XtAddCallback ((Widget) m_buttonWidget,
230 XmNvalueChangedCallback,
231 (XtCallbackProc) wxMenuItemCallback,
232 (XtPointer) this);
233 else
234 XtAddCallback ((Widget) m_buttonWidget,
235 XmNactivateCallback,
236 (XtCallbackProc) wxMenuItemCallback,
237 (XtPointer) this);
238 XtAddCallback ((Widget) m_buttonWidget,
239 XmNarmCallback,
240 (XtCallbackProc) wxMenuItemArmCallback,
241 (XtPointer) this);
242 XtAddCallback ((Widget) m_buttonWidget,
243 XmNdisarmCallback,
244 (XtCallbackProc) wxMenuItemDisarmCallback,
245 (XtPointer) this);
246 }
247 else if (GetId() == -1)
248 {
249 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
250 xmSeparatorGadgetClass, (Widget) menu, NULL);
251 }
252 else if (m_subMenu)
253 {
254 m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, m_text, TRUE);
255 m_subMenu->SetButtonWidget(m_buttonWidget);
256 XtAddCallback ((Widget) m_buttonWidget,
257 XmNcascadingCallback,
258 (XtCallbackProc) wxMenuItemArmCallback,
259 (XtPointer) this);
260 }
261 if (m_buttonWidget)
262 XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled());
263 }
264
265 void wxMenuItem::DestroyItem(bool full)
266 {
267 if (GetId() == -2)
268 {
269 ; // Nothing
270
271 }
272 else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu)
273 {
274 if (m_buttonWidget)
275 {
276 if (IsCheckable())
277 XtRemoveCallback ((Widget) m_buttonWidget, XmNvalueChangedCallback,
278 wxMenuItemCallback, (XtPointer) this);
279 else
280 XtRemoveCallback ((Widget) m_buttonWidget, XmNactivateCallback,
281 wxMenuItemCallback, (XtPointer) this);
282 XtRemoveCallback ((Widget) m_buttonWidget, XmNarmCallback,
283 wxMenuItemArmCallback, (XtPointer) this);
284 XtRemoveCallback ((Widget) m_buttonWidget, XmNdisarmCallback,
285 wxMenuItemDisarmCallback, (XtPointer) this);
286 }
287 }
288 else if (GetId() == -1)
289 {
290 ; // Nothing
291
292 }
293 else if (GetSubMenu())
294 {
295 if (m_buttonWidget)
296 {
297 XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback,
298 wxMenuItemArmCallback, (XtPointer) this);
299 }
300 m_subMenu->DestroyMenu(full);
301 if (full)
302 m_buttonWidget = NULL;
303 }
304
305 if (m_buttonWidget && full)
306 {
307 XtDestroyWidget ((Widget) m_buttonWidget);
308 m_buttonWidget = (WXWidget) 0;
309 }
310 }
311
312 void wxMenuItem::SetText(const wxString& label)
313 {
314 char mnem = wxFindMnemonic (label);
315 wxString label2 = wxStripMenuCodes(label);
316
317 m_text = label;
318
319 if (m_buttonWidget)
320 {
321 wxXmString label_str(label2);
322 XtVaSetValues ((Widget) m_buttonWidget,
323 XmNlabelString, label_str(),
324 NULL);
325 if (mnem != 0)
326 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
327 char *accel = wxFindAccelerator (label2);
328 if (accel)
329 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
330
331 XmString accel_str = wxFindAcceleratorText (label2);
332 if (accel_str)
333 {
334 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
335 XmStringFree (accel_str);
336 }
337 }
338 }
339
340 // ----------------------------------------------------------------------------
341 // Motif callbacks
342 // ----------------------------------------------------------------------------
343
344 void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
345 XtPointer WXUNUSED(ptr))
346 {
347 wxMenuItem *item = (wxMenuItem *) clientData;
348 if (item)
349 {
350 if (item->IsCheckable())
351 {
352 Boolean isChecked = FALSE;
353 XtVaGetValues ((Widget) item->GetButtonWidget(), XmNset, & isChecked, NULL);
354
355 // only set the flag, don't actually check anything
356 item->wxMenuItemBase::Check(isChecked);
357 }
358 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
359 {
360 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
361 commandEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
362 commandEvent.SetInt( item->GetId() );
363
364 item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(commandEvent);
365 }
366 else if (item->GetTopMenu())
367 {
368 wxCommandEvent event (wxEVT_COMMAND_MENU_SELECTED, item->GetId());
369 event.SetEventObject(item->GetTopMenu());
370 event.SetInt( item->GetId() );
371
372 item->GetTopMenu()->ProcessCommand (event);
373 }
374 }
375 }
376
377 void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
378 XtPointer WXUNUSED(ptr))
379 {
380 wxMenuItem *item = (wxMenuItem *) clientData;
381 if (item)
382 {
383 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
384 {
385 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
386 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
387
388 item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent);
389 }
390 }
391 }
392
393 void
394 wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
395 XtPointer WXUNUSED(ptr))
396 {
397 wxMenuItem *item = (wxMenuItem *) clientData;
398 if (item)
399 {
400 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
401 {
402 // TODO: not sure this is correct, since -1 means something
403 // special to event system
404 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, -1);
405 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
406
407 item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent);
408 }
409 }
410 }
411