]> git.saurik.com Git - wxWidgets.git/blame - src/motif/menuitem.cpp
Fixed event handling bug as suggested by Lech Wiktor Piotrowski.
[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 160
51c9a5db
MB
161void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar,
162 wxMenu * topMenu, size_t index)
50414e24 163{
2d120f83
JS
164 m_menuBar = menuBar;
165 m_topMenu = topMenu;
31528cd3 166
15b845f2 167 if (GetId() == -3)
50414e24 168 {
15b845f2 169 // Id=-3 identifies a Title item.
31528cd3 170 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
9874b4ee 171 (wxStripMenuCodes(m_text),
2d120f83 172 xmLabelGadgetClass, (Widget) menu, NULL);
50414e24 173 }
9874b4ee 174 else if ((!m_text.IsNull() && m_text != "") && (!m_subMenu))
50414e24 175 {
9874b4ee 176 wxString strName = wxStripMenuCodes(m_text);
2d120f83
JS
177 if (IsCheckable())
178 {
31528cd3 179 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
2d120f83 180 xmToggleButtonGadgetClass, (Widget) menu,
51c9a5db
MB
181#ifdef XmNpositionIndex
182 XmNpositionIndex, index,
183#endif
2d120f83
JS
184 NULL);
185 XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
186 }
187 else
31528cd3 188 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
2d120f83 189 xmPushButtonGadgetClass, (Widget) menu,
51c9a5db
MB
190#ifdef XmNpositionIndex
191 XmNpositionIndex, index,
192#endif
2d120f83 193 NULL);
9874b4ee 194 char mnem = wxFindMnemonic (m_text);
2d120f83
JS
195 if (mnem != 0)
196 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
31528cd3 197
2d120f83
JS
198 //// TODO: proper accelerator treatment. What does wxFindAccelerator
199 //// look for?
9874b4ee 200 strName = m_text;
31528cd3 201 char *accel = wxFindAccelerator (strName);
2d120f83
JS
202 if (accel)
203 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
31528cd3 204
2d120f83 205 // TODO: What does this do?
31528cd3 206 XmString accel_str = wxFindAcceleratorText (strName);
2d120f83
JS
207 if (accel_str)
208 {
209 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
210 XmStringFree (accel_str);
211 }
31528cd3 212
2d120f83
JS
213 if (IsCheckable())
214 XtAddCallback ((Widget) m_buttonWidget,
215 XmNvalueChangedCallback,
216 (XtCallbackProc) wxMenuItemCallback,
217 (XtPointer) this);
218 else
219 XtAddCallback ((Widget) m_buttonWidget,
220 XmNactivateCallback,
221 (XtCallbackProc) wxMenuItemCallback,
222 (XtPointer) this);
223 XtAddCallback ((Widget) m_buttonWidget,
224 XmNarmCallback,
225 (XtCallbackProc) wxMenuItemArmCallback,
226 (XtPointer) this);
227 XtAddCallback ((Widget) m_buttonWidget,
228 XmNdisarmCallback,
229 (XtCallbackProc) wxMenuItemDisarmCallback,
230 (XtPointer) this);
50414e24 231 }
15b845f2 232 else if (GetId() == wxID_SEPARATOR)
50414e24 233 {
2d120f83 234 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
51c9a5db
MB
235 xmSeparatorGadgetClass, (Widget) menu,
236#ifndef XmNpositionIndex
237 XmNpositionIndex, index,
238#endif
239 NULL);
50414e24 240 }
9874b4ee 241 else if (m_subMenu)
50414e24 242 {
51c9a5db 243 m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, index, m_text, true);
9874b4ee 244 m_subMenu->SetButtonWidget(m_buttonWidget);
2d120f83
JS
245 XtAddCallback ((Widget) m_buttonWidget,
246 XmNcascadingCallback,
247 (XtCallbackProc) wxMenuItemArmCallback,
248 (XtPointer) this);
50414e24 249 }
2d120f83
JS
250 if (m_buttonWidget)
251 XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled());
50414e24
JS
252}
253
254void wxMenuItem::DestroyItem(bool full)
255{
15b845f2 256 if (GetId() == -3)
50414e24 257 {
9874b4ee 258 ; // Nothing
31528cd3 259
50414e24 260 }
9874b4ee 261 else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu)
50414e24 262 {
2d120f83
JS
263 if (m_buttonWidget)
264 {
265 if (IsCheckable())
266 XtRemoveCallback ((Widget) m_buttonWidget, XmNvalueChangedCallback,
267 wxMenuItemCallback, (XtPointer) this);
268 else
269 XtRemoveCallback ((Widget) m_buttonWidget, XmNactivateCallback,
270 wxMenuItemCallback, (XtPointer) this);
271 XtRemoveCallback ((Widget) m_buttonWidget, XmNarmCallback,
272 wxMenuItemArmCallback, (XtPointer) this);
273 XtRemoveCallback ((Widget) m_buttonWidget, XmNdisarmCallback,
274 wxMenuItemDisarmCallback, (XtPointer) this);
275 }
50414e24 276 }
15b845f2 277 else if (GetId() == wxID_SEPARATOR)
50414e24 278 {
9874b4ee 279 ; // Nothing
31528cd3 280
50414e24 281 }
2d120f83 282 else if (GetSubMenu())
50414e24 283 {
2d120f83
JS
284 if (m_buttonWidget)
285 {
286 XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback,
287 wxMenuItemArmCallback, (XtPointer) this);
288 }
9874b4ee 289 m_subMenu->DestroyMenu(full);
2d120f83
JS
290 if (full)
291 m_buttonWidget = NULL;
50414e24 292 }
31528cd3 293
2d120f83 294 if (m_buttonWidget && full)
50414e24 295 {
2d120f83
JS
296 XtDestroyWidget ((Widget) m_buttonWidget);
297 m_buttonWidget = (WXWidget) 0;
50414e24
JS
298 }
299}
300
9874b4ee 301void wxMenuItem::SetText(const wxString& label)
50414e24 302{
2d120f83 303 char mnem = wxFindMnemonic (label);
31528cd3
VZ
304 wxString label2 = wxStripMenuCodes(label);
305
9874b4ee 306 m_text = label;
31528cd3 307
2d120f83 308 if (m_buttonWidget)
50414e24 309 {
31528cd3 310 wxXmString label_str(label2);
2d120f83 311 XtVaSetValues ((Widget) m_buttonWidget,
66cbc96d 312 XmNlabelString, label_str(),
2d120f83 313 NULL);
2d120f83
JS
314 if (mnem != 0)
315 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
31528cd3 316 char *accel = wxFindAccelerator (label2);
2d120f83
JS
317 if (accel)
318 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
31528cd3
VZ
319
320 XmString accel_str = wxFindAcceleratorText (label2);
2d120f83 321 if (accel_str)
50414e24 322 {
2d120f83
JS
323 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
324 XmStringFree (accel_str);
50414e24
JS
325 }
326 }
327}
328
9874b4ee
VZ
329// ----------------------------------------------------------------------------
330// Motif callbacks
331// ----------------------------------------------------------------------------
332
af111fc3
JS
333void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
334 XtPointer WXUNUSED(ptr))
50414e24 335{
2d120f83
JS
336 wxMenuItem *item = (wxMenuItem *) clientData;
337 if (item)
50414e24 338 {
092276a8
MB
339 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
340 event.SetInt( item->GetId() );
341
2d120f83
JS
342 if (item->IsCheckable())
343 {
96be256b 344 Boolean isChecked = false;
092276a8
MB
345 XtVaGetValues ((Widget) item->GetButtonWidget(),
346 XmNset, & isChecked,
347 NULL);
9874b4ee
VZ
348
349 // only set the flag, don't actually check anything
350 item->wxMenuItemBase::Check(isChecked);
092276a8 351 event.SetInt(isChecked);
2d120f83 352 }
092276a8 353
2d120f83
JS
354 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
355 {
092276a8 356 event.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 357
092276a8
MB
358 item->GetMenuBar()->GetMenuBarFrame()
359 ->GetEventHandler()->ProcessEvent(event);
2d120f83 360 }
7e1bcfa8 361 // this is the child of a popup menu
2d120f83
JS
362 else if (item->GetTopMenu())
363 {
2d120f83 364 event.SetEventObject(item->GetTopMenu());
31528cd3 365
2d120f83 366 item->GetTopMenu()->ProcessCommand (event);
7e1bcfa8 367
092276a8 368 // Since PopupMenu under Motif still grab right mouse
7e1bcfa8
MB
369 // button events after it was closed, we need to delete
370 // the associated widgets to allow next PopUpMenu to
371 // appear; this needs to be done there because doing it in
372 // a WorkProc as before may cause crashes if a menu item causes
373 // the parent window of the menu to be destroyed
374 item->GetTopMenu()->DestroyWidgetAndDetach();
2d120f83 375 }
50414e24
JS
376 }
377}
378
af111fc3
JS
379void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
380 XtPointer WXUNUSED(ptr))
50414e24 381{
2d120f83
JS
382 wxMenuItem *item = (wxMenuItem *) clientData;
383 if (item)
50414e24 384 {
2d120f83
JS
385 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
386 {
387 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
388 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 389
092276a8
MB
390 item->GetMenuBar()->GetMenuBarFrame()
391 ->GetEventHandler()->ProcessEvent(menuEvent);
2d120f83 392 }
50414e24
JS
393 }
394}
395
31528cd3 396void
af111fc3
JS
397wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
398 XtPointer WXUNUSED(ptr))
50414e24 399{
2d120f83
JS
400 wxMenuItem *item = (wxMenuItem *) clientData;
401 if (item)
50414e24 402 {
2d120f83
JS
403 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
404 {
405 // TODO: not sure this is correct, since -1 means something
406 // special to event system
407 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, -1);
408 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 409
092276a8
MB
410 item->GetMenuBar()->GetMenuBarFrame()
411 ->GetEventHandler()->ProcessEvent(menuEvent);
2d120f83 412 }
50414e24
JS
413 }
414}
415