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