]> git.saurik.com Git - wxWidgets.git/blame - src/motif/menuitem.cpp
replace apple extended code
[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
14f355c2 16#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
9874b4ee
VZ
17 #pragma implementation "menuitem.h"
18#endif
19
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
1248b41f
MB
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
f6045f99
GD
27#include "wx/defs.h"
28
4bb6408c
JS
29#include "wx/menu.h"
30#include "wx/menuitem.h"
50414e24
JS
31#include "wx/utils.h"
32#include "wx/frame.h"
33
338dd992
JJ
34#ifdef __VMS__
35#pragma message disable nosimpint
36#endif
50414e24
JS
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>
338dd992
JJ
46#ifdef __VMS__
47#pragma message enable nosimpint
48#endif
50414e24
JS
49
50#include "wx/motif/private.h"
51
9874b4ee
VZ
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);
4bb6408c
JS
59
60// ============================================================================
61// implementation
62// ============================================================================
63
64// ----------------------------------------------------------------------------
65// dynamic classes implementation
66// ----------------------------------------------------------------------------
67
d65c269b 68IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
4bb6408c
JS
69
70// ----------------------------------------------------------------------------
71// wxMenuItem
72// ----------------------------------------------------------------------------
73
74// ctor & dtor
75// -----------
76
d65c269b
VZ
77wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
78 int id,
79 const wxString& strName,
80 const wxString& strHelp,
81 wxItemKind kind,
9874b4ee 82 wxMenu *pSubMenu)
d65c269b 83 : wxMenuItemBase(pParentMenu, id, strName, strHelp, kind, pSubMenu)
4bb6408c 84{
9874b4ee
VZ
85 // Motif-specific
86 m_menuBar = NULL;
2d120f83 87 m_buttonWidget = (WXWidget) NULL;
9874b4ee 88 m_topMenu = NULL;
4bb6408c
JS
89}
90
31528cd3 91wxMenuItem::~wxMenuItem()
4bb6408c
JS
92{
93}
94
4bb6408c
JS
95// change item state
96// -----------------
97
98void wxMenuItem::Enable(bool bDoEnable)
99{
9d08a1ed 100 if ( m_isEnabled != bDoEnable )
4bb6408c 101 {
9874b4ee
VZ
102 if ( !IsSubMenu() )
103 {
104 // normal menu item
2d120f83
JS
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 }
31528cd3 115
9874b4ee 116 wxMenuItemBase::Enable(bDoEnable);
4bb6408c 117 }
4bb6408c
JS
118}
119
120void wxMenuItem::Check(bool bDoCheck)
121{
2d120f83 122 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
31528cd3 123
9874b4ee 124 if ( m_isChecked != bDoCheck )
50414e24 125 {
9874b4ee 126 if ( m_buttonWidget )
2d120f83 127 {
9874b4ee
VZ
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);
2d120f83 135 }
9874b4ee
VZ
136
137 wxMenuItemBase::Check(bDoCheck);
50414e24 138 }
50414e24
JS
139}
140
3b59cdbf
VZ
141/* static */
142wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
c71830c3 143{
c2754d29 144 return wxStripMenuCodes(text);
c71830c3
VZ
145}
146
c71830c3
VZ
147// ----------------------------------------------------------------------------
148// wxMenuItemBase
149// ----------------------------------------------------------------------------
150
151wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
152 int id,
153 const wxString& name,
154 const wxString& help,
d65c269b 155 wxItemKind kind,
c71830c3
VZ
156 wxMenu *subMenu)
157{
d65c269b 158 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
c71830c3
VZ
159}
160
161// ----------------------------------------------------------------------------
162// Motif-specific
163// ----------------------------------------------------------------------------
50414e24
JS
164
165void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu)
166{
2d120f83
JS
167 m_menuBar = menuBar;
168 m_topMenu = topMenu;
31528cd3 169
15b845f2 170 if (GetId() == -3)
50414e24 171 {
15b845f2 172 // Id=-3 identifies a Title item.
31528cd3 173 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
9874b4ee 174 (wxStripMenuCodes(m_text),
2d120f83 175 xmLabelGadgetClass, (Widget) menu, NULL);
50414e24 176 }
9874b4ee 177 else if ((!m_text.IsNull() && m_text != "") && (!m_subMenu))
50414e24 178 {
9874b4ee 179 wxString strName = wxStripMenuCodes(m_text);
2d120f83
JS
180 if (IsCheckable())
181 {
31528cd3 182 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
2d120f83
JS
183 xmToggleButtonGadgetClass, (Widget) menu,
184 NULL);
185 XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
186 }
187 else
31528cd3 188 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
2d120f83
JS
189 xmPushButtonGadgetClass, (Widget) menu,
190 NULL);
9874b4ee 191 char mnem = wxFindMnemonic (m_text);
2d120f83
JS
192 if (mnem != 0)
193 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
31528cd3 194
2d120f83
JS
195 //// TODO: proper accelerator treatment. What does wxFindAccelerator
196 //// look for?
9874b4ee 197 strName = m_text;
31528cd3 198 char *accel = wxFindAccelerator (strName);
2d120f83
JS
199 if (accel)
200 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
31528cd3 201
2d120f83 202 // TODO: What does this do?
31528cd3 203 XmString accel_str = wxFindAcceleratorText (strName);
2d120f83
JS
204 if (accel_str)
205 {
206 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
207 XmStringFree (accel_str);
208 }
31528cd3 209
2d120f83
JS
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);
50414e24 228 }
15b845f2 229 else if (GetId() == wxID_SEPARATOR)
50414e24 230 {
2d120f83
JS
231 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
232 xmSeparatorGadgetClass, (Widget) menu, NULL);
50414e24 233 }
9874b4ee 234 else if (m_subMenu)
50414e24 235 {
9874b4ee
VZ
236 m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, m_text, TRUE);
237 m_subMenu->SetButtonWidget(m_buttonWidget);
2d120f83
JS
238 XtAddCallback ((Widget) m_buttonWidget,
239 XmNcascadingCallback,
240 (XtCallbackProc) wxMenuItemArmCallback,
241 (XtPointer) this);
50414e24 242 }
2d120f83
JS
243 if (m_buttonWidget)
244 XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled());
50414e24
JS
245}
246
247void wxMenuItem::DestroyItem(bool full)
248{
15b845f2 249 if (GetId() == -3)
50414e24 250 {
9874b4ee 251 ; // Nothing
31528cd3 252
50414e24 253 }
9874b4ee 254 else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu)
50414e24 255 {
2d120f83
JS
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 }
50414e24 269 }
15b845f2 270 else if (GetId() == wxID_SEPARATOR)
50414e24 271 {
9874b4ee 272 ; // Nothing
31528cd3 273
50414e24 274 }
2d120f83 275 else if (GetSubMenu())
50414e24 276 {
2d120f83
JS
277 if (m_buttonWidget)
278 {
279 XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback,
280 wxMenuItemArmCallback, (XtPointer) this);
281 }
9874b4ee 282 m_subMenu->DestroyMenu(full);
2d120f83
JS
283 if (full)
284 m_buttonWidget = NULL;
50414e24 285 }
31528cd3 286
2d120f83 287 if (m_buttonWidget && full)
50414e24 288 {
2d120f83
JS
289 XtDestroyWidget ((Widget) m_buttonWidget);
290 m_buttonWidget = (WXWidget) 0;
50414e24
JS
291 }
292}
293
9874b4ee 294void wxMenuItem::SetText(const wxString& label)
50414e24 295{
2d120f83 296 char mnem = wxFindMnemonic (label);
31528cd3
VZ
297 wxString label2 = wxStripMenuCodes(label);
298
9874b4ee 299 m_text = label;
31528cd3 300
2d120f83 301 if (m_buttonWidget)
50414e24 302 {
31528cd3 303 wxXmString label_str(label2);
2d120f83 304 XtVaSetValues ((Widget) m_buttonWidget,
66cbc96d 305 XmNlabelString, label_str(),
2d120f83 306 NULL);
2d120f83
JS
307 if (mnem != 0)
308 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
31528cd3 309 char *accel = wxFindAccelerator (label2);
2d120f83
JS
310 if (accel)
311 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
31528cd3
VZ
312
313 XmString accel_str = wxFindAcceleratorText (label2);
2d120f83 314 if (accel_str)
50414e24 315 {
2d120f83
JS
316 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
317 XmStringFree (accel_str);
50414e24
JS
318 }
319 }
320}
321
9874b4ee
VZ
322// ----------------------------------------------------------------------------
323// Motif callbacks
324// ----------------------------------------------------------------------------
325
af111fc3
JS
326void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
327 XtPointer WXUNUSED(ptr))
50414e24 328{
2d120f83
JS
329 wxMenuItem *item = (wxMenuItem *) clientData;
330 if (item)
50414e24 331 {
092276a8
MB
332 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
333 event.SetInt( item->GetId() );
334
2d120f83
JS
335 if (item->IsCheckable())
336 {
337 Boolean isChecked = FALSE;
092276a8
MB
338 XtVaGetValues ((Widget) item->GetButtonWidget(),
339 XmNset, & isChecked,
340 NULL);
9874b4ee
VZ
341
342 // only set the flag, don't actually check anything
343 item->wxMenuItemBase::Check(isChecked);
092276a8 344 event.SetInt(isChecked);
2d120f83 345 }
092276a8 346
2d120f83
JS
347 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
348 {
092276a8 349 event.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 350
092276a8
MB
351 item->GetMenuBar()->GetMenuBarFrame()
352 ->GetEventHandler()->ProcessEvent(event);
2d120f83 353 }
7e1bcfa8 354 // this is the child of a popup menu
2d120f83
JS
355 else if (item->GetTopMenu())
356 {
2d120f83 357 event.SetEventObject(item->GetTopMenu());
31528cd3 358
2d120f83 359 item->GetTopMenu()->ProcessCommand (event);
7e1bcfa8 360
092276a8 361 // Since PopupMenu under Motif still grab right mouse
7e1bcfa8
MB
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();
2d120f83 368 }
50414e24
JS
369 }
370}
371
af111fc3
JS
372void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
373 XtPointer WXUNUSED(ptr))
50414e24 374{
2d120f83
JS
375 wxMenuItem *item = (wxMenuItem *) clientData;
376 if (item)
50414e24 377 {
2d120f83
JS
378 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
379 {
380 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
381 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 382
092276a8
MB
383 item->GetMenuBar()->GetMenuBarFrame()
384 ->GetEventHandler()->ProcessEvent(menuEvent);
2d120f83 385 }
50414e24
JS
386 }
387}
388
31528cd3 389void
af111fc3
JS
390wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
391 XtPointer WXUNUSED(ptr))
50414e24 392{
2d120f83
JS
393 wxMenuItem *item = (wxMenuItem *) clientData;
394 if (item)
50414e24 395 {
2d120f83
JS
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());
31528cd3 402
092276a8
MB
403 item->GetMenuBar()->GetMenuBarFrame()
404 ->GetEventHandler()->ProcessEvent(menuEvent);
2d120f83 405 }
50414e24
JS
406 }
407}
408