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