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