]> git.saurik.com Git - wxWidgets.git/blob - src/motif/menuitem.cpp
Clear is also expected to clear the text
[wxWidgets.git] / src / motif / menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/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/menuitem.h"
24 #include "wx/stockitem.h"
25
26 #ifndef WX_PRECOMP
27 #include "wx/utils.h"
28 #include "wx/frame.h"
29 #include "wx/menu.h"
30 #endif
31
32 #ifdef __VMS__
33 #pragma message disable nosimpint
34 #endif
35 #include <Xm/Label.h>
36 #include <Xm/LabelG.h>
37 #include <Xm/CascadeBG.h>
38 #include <Xm/CascadeB.h>
39 #include <Xm/SeparatoG.h>
40 #include <Xm/PushBG.h>
41 #include <Xm/ToggleB.h>
42 #include <Xm/ToggleBG.h>
43 #include <Xm/RowColumn.h>
44 #ifdef __VMS__
45 #pragma message enable nosimpint
46 #endif
47
48 #include "wx/motif/private.h"
49
50 // ----------------------------------------------------------------------------
51 // functions prototypes
52 // ----------------------------------------------------------------------------
53
54 static void wxMenuItemCallback(Widget w, XtPointer clientData, XtPointer ptr);
55 static void wxMenuItemArmCallback(Widget w, XtPointer clientData, XtPointer ptr);
56 static void wxMenuItemDisarmCallback(Widget w, XtPointer clientData, XtPointer ptr);
57
58 // ============================================================================
59 // implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // dynamic classes implementation
64 // ----------------------------------------------------------------------------
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 // ----------------------------------------------------------------------------
138 // wxMenuItemBase
139 // ----------------------------------------------------------------------------
140
141 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
142 int id,
143 const wxString& name,
144 const wxString& help,
145 wxItemKind kind,
146 wxMenu *subMenu)
147 {
148 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
149 }
150
151 // ----------------------------------------------------------------------------
152 // Motif-specific
153 // ----------------------------------------------------------------------------
154
155 void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar,
156 wxMenu * topMenu, size_t index)
157 {
158 m_menuBar = menuBar;
159 m_topMenu = topMenu;
160
161 if (GetId() == -3)
162 {
163 // Id=-3 identifies a Title item.
164 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
165 (wxStripMenuCodes(m_text),
166 xmLabelGadgetClass, (Widget) menu, NULL);
167 }
168 else if (!IsSeparator() && !m_subMenu)
169 {
170 wxString txt = m_text;
171
172 if (m_text.IsEmpty())
173 {
174 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
175 txt = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR|wxSTOCK_WITH_MNEMONIC);
176 }
177
178 wxString strName = wxStripMenuCodes(txt);
179 if (IsCheckable())
180 {
181 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
182 xmToggleButtonGadgetClass, (Widget) menu,
183 #ifdef XmNpositionIndex
184 XmNpositionIndex, index,
185 #endif
186 NULL);
187 XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
188 }
189 else
190 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
191 xmPushButtonGadgetClass, (Widget) menu,
192 #ifdef XmNpositionIndex
193 XmNpositionIndex, index,
194 #endif
195 NULL);
196 char mnem = wxFindMnemonic (m_text);
197 if (mnem != 0)
198 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
199
200 //// TODO: proper accelerator treatment. What does wxFindAccelerator
201 //// look for?
202 strName = m_text;
203 char *accel = wxFindAccelerator (strName);
204 if (accel)
205 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
206
207 // TODO: What does this do?
208 XmString accel_str = wxFindAcceleratorText (strName);
209 if (accel_str)
210 {
211 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
212 XmStringFree (accel_str);
213 }
214
215 if (IsCheckable())
216 XtAddCallback ((Widget) m_buttonWidget,
217 XmNvalueChangedCallback,
218 (XtCallbackProc) wxMenuItemCallback,
219 (XtPointer) this);
220 else
221 XtAddCallback ((Widget) m_buttonWidget,
222 XmNactivateCallback,
223 (XtCallbackProc) wxMenuItemCallback,
224 (XtPointer) this);
225 XtAddCallback ((Widget) m_buttonWidget,
226 XmNarmCallback,
227 (XtCallbackProc) wxMenuItemArmCallback,
228 (XtPointer) this);
229 XtAddCallback ((Widget) m_buttonWidget,
230 XmNdisarmCallback,
231 (XtCallbackProc) wxMenuItemDisarmCallback,
232 (XtPointer) this);
233 }
234 else if (IsSeparator())
235 {
236 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
237 xmSeparatorGadgetClass, (Widget) menu,
238 #ifndef XmNpositionIndex
239 XmNpositionIndex, index,
240 #endif
241 NULL);
242 }
243 else if (m_subMenu)
244 {
245 m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, index, m_text, true);
246 m_subMenu->SetButtonWidget(m_buttonWidget);
247 XtAddCallback ((Widget) m_buttonWidget,
248 XmNcascadingCallback,
249 (XtCallbackProc) wxMenuItemArmCallback,
250 (XtPointer) this);
251 }
252 if (m_buttonWidget)
253 XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled());
254 }
255
256 void wxMenuItem::DestroyItem(bool full)
257 {
258 if (GetId() == -3)
259 {
260 ; // Nothing
261
262 }
263 else if (!m_text.empty() && !m_subMenu)
264 {
265 if (m_buttonWidget)
266 {
267 if (IsCheckable())
268 XtRemoveCallback ((Widget) m_buttonWidget, XmNvalueChangedCallback,
269 wxMenuItemCallback, (XtPointer) this);
270 else
271 XtRemoveCallback ((Widget) m_buttonWidget, XmNactivateCallback,
272 wxMenuItemCallback, (XtPointer) this);
273 XtRemoveCallback ((Widget) m_buttonWidget, XmNarmCallback,
274 wxMenuItemArmCallback, (XtPointer) this);
275 XtRemoveCallback ((Widget) m_buttonWidget, XmNdisarmCallback,
276 wxMenuItemDisarmCallback, (XtPointer) this);
277 }
278 }
279 else if (IsSeparator())
280 {
281 ; // Nothing
282
283 }
284 else if (GetSubMenu())
285 {
286 if (m_buttonWidget)
287 {
288 XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback,
289 wxMenuItemArmCallback, (XtPointer) this);
290 }
291 m_subMenu->DestroyMenu(full);
292 if (full)
293 m_buttonWidget = NULL;
294 }
295
296 if (m_buttonWidget && full)
297 {
298 XtDestroyWidget ((Widget) m_buttonWidget);
299 m_buttonWidget = (WXWidget) 0;
300 }
301 }
302
303 void wxMenuItem::SetItemLabel(const wxString& label)
304 {
305 char mnem = wxFindMnemonic (label);
306 wxString label2 = wxStripMenuCodes(label);
307
308 m_text = label;
309
310 if (m_buttonWidget)
311 {
312 wxXmString label_str(label2);
313 XtVaSetValues ((Widget) m_buttonWidget,
314 XmNlabelString, label_str(),
315 NULL);
316 if (mnem != 0)
317 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
318 char *accel = wxFindAccelerator (label2);
319 if (accel)
320 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
321
322 XmString accel_str = wxFindAcceleratorText (label2);
323 if (accel_str)
324 {
325 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
326 XmStringFree (accel_str);
327 }
328 }
329 }
330
331 // ----------------------------------------------------------------------------
332 // Motif callbacks
333 // ----------------------------------------------------------------------------
334
335 void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
336 XtPointer WXUNUSED(ptr))
337 {
338 wxMenuItem *item = (wxMenuItem *) clientData;
339 if (item)
340 {
341 wxCommandEvent event(wxEVT_MENU, item->GetId());
342 event.SetInt( item->GetId() );
343
344 if (item->IsCheckable())
345 {
346 Boolean isChecked = false;
347 XtVaGetValues ((Widget) item->GetButtonWidget(),
348 XmNset, & isChecked,
349 NULL);
350
351 // only set the flag, don't actually check anything
352 item->wxMenuItemBase::Check(isChecked);
353 event.SetInt(isChecked);
354 }
355
356 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
357 {
358 event.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
359
360 item->GetMenuBar()->GetMenuBarFrame()
361 ->HandleWindowEvent(event);
362 }
363 // this is the child of a popup menu
364 else if (item->GetTopMenu())
365 {
366 event.SetEventObject(item->GetTopMenu());
367
368 item->GetTopMenu()->ProcessCommand (event);
369
370 // Since PopupMenu under Motif still grab right mouse
371 // button events after it was closed, we need to delete
372 // the associated widgets to allow next PopUpMenu to
373 // appear; this needs to be done there because doing it in
374 // a WorkProc as before may cause crashes if a menu item causes
375 // the parent window of the menu to be destroyed
376 item->GetTopMenu()->DestroyWidgetAndDetach();
377 }
378 }
379 }
380
381 void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
382 XtPointer WXUNUSED(ptr))
383 {
384 wxMenuItem *item = (wxMenuItem *) clientData;
385 if (item)
386 {
387 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
388 {
389 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
390 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
391
392 item->GetMenuBar()->GetMenuBarFrame()
393 ->HandleWindowEvent(menuEvent);
394 }
395 }
396 }
397
398 void
399 wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
400 XtPointer WXUNUSED(ptr))
401 {
402 wxMenuItem *item = (wxMenuItem *) clientData;
403 if (item)
404 {
405 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
406 {
407 // TODO: not sure this is correct, since -1 means something
408 // special to event system
409 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, -1);
410 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
411
412 item->GetMenuBar()->GetMenuBarFrame()
413 ->HandleWindowEvent(menuEvent);
414 }
415 }
416 }