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