]> git.saurik.com Git - wxWidgets.git/blame - src/motif/menuitem.cpp
implemented Freeze/Thaw() (patch 922156)
[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
14f355c2 16#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
9874b4ee
VZ
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
4bb6408c
JS
92// change item state
93// -----------------
94
95void wxMenuItem::Enable(bool bDoEnable)
96{
9d08a1ed 97 if ( m_isEnabled != bDoEnable )
4bb6408c 98 {
9874b4ee
VZ
99 if ( !IsSubMenu() )
100 {
101 // normal menu item
2d120f83
JS
102 if (m_buttonWidget)
103 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
104 }
105 else // submenu
106 {
107 // Maybe we should apply this to all items in the submenu?
108 // Or perhaps it works anyway.
109 if (m_buttonWidget)
110 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
111 }
31528cd3 112
9874b4ee 113 wxMenuItemBase::Enable(bDoEnable);
4bb6408c 114 }
4bb6408c
JS
115}
116
117void wxMenuItem::Check(bool bDoCheck)
118{
2d120f83 119 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
31528cd3 120
9874b4ee 121 if ( m_isChecked != bDoCheck )
50414e24 122 {
9874b4ee 123 if ( m_buttonWidget )
2d120f83 124 {
9874b4ee
VZ
125 wxASSERT_MSG( XtIsSubclass((Widget)m_buttonWidget,
126 xmToggleButtonGadgetClass),
127 wxT("checkable menu item must be a toggle button") );
128
129 XtVaSetValues((Widget)m_buttonWidget,
130 XmNset, (Boolean)bDoCheck,
131 NULL);
2d120f83 132 }
9874b4ee
VZ
133
134 wxMenuItemBase::Check(bDoCheck);
50414e24 135 }
50414e24
JS
136}
137
3b59cdbf
VZ
138/* static */
139wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
c71830c3 140{
c2754d29 141 return wxStripMenuCodes(text);
c71830c3
VZ
142}
143
c71830c3
VZ
144// ----------------------------------------------------------------------------
145// wxMenuItemBase
146// ----------------------------------------------------------------------------
147
148wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
149 int id,
150 const wxString& name,
151 const wxString& help,
d65c269b 152 wxItemKind kind,
c71830c3
VZ
153 wxMenu *subMenu)
154{
d65c269b 155 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
c71830c3
VZ
156}
157
158// ----------------------------------------------------------------------------
159// Motif-specific
160// ----------------------------------------------------------------------------
50414e24
JS
161
162void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu)
163{
2d120f83
JS
164 m_menuBar = menuBar;
165 m_topMenu = topMenu;
31528cd3 166
2d120f83 167 if (GetId() == -2)
50414e24 168 {
2d120f83 169 // Id=-2 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
JS
180 xmToggleButtonGadgetClass, (Widget) menu,
181 NULL);
182 XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
183 }
184 else
31528cd3 185 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
2d120f83
JS
186 xmPushButtonGadgetClass, (Widget) menu,
187 NULL);
9874b4ee 188 char mnem = wxFindMnemonic (m_text);
2d120f83
JS
189 if (mnem != 0)
190 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
31528cd3 191
2d120f83
JS
192 //// TODO: proper accelerator treatment. What does wxFindAccelerator
193 //// look for?
9874b4ee 194 strName = m_text;
31528cd3 195 char *accel = wxFindAccelerator (strName);
2d120f83
JS
196 if (accel)
197 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
31528cd3 198
2d120f83 199 // TODO: What does this do?
31528cd3 200 XmString accel_str = wxFindAcceleratorText (strName);
2d120f83
JS
201 if (accel_str)
202 {
203 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
204 XmStringFree (accel_str);
205 }
31528cd3 206
2d120f83
JS
207 if (IsCheckable())
208 XtAddCallback ((Widget) m_buttonWidget,
209 XmNvalueChangedCallback,
210 (XtCallbackProc) wxMenuItemCallback,
211 (XtPointer) this);
212 else
213 XtAddCallback ((Widget) m_buttonWidget,
214 XmNactivateCallback,
215 (XtCallbackProc) wxMenuItemCallback,
216 (XtPointer) this);
217 XtAddCallback ((Widget) m_buttonWidget,
218 XmNarmCallback,
219 (XtCallbackProc) wxMenuItemArmCallback,
220 (XtPointer) this);
221 XtAddCallback ((Widget) m_buttonWidget,
222 XmNdisarmCallback,
223 (XtCallbackProc) wxMenuItemDisarmCallback,
224 (XtPointer) this);
50414e24 225 }
2d120f83 226 else if (GetId() == -1)
50414e24 227 {
2d120f83
JS
228 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
229 xmSeparatorGadgetClass, (Widget) menu, NULL);
50414e24 230 }
9874b4ee 231 else if (m_subMenu)
50414e24 232 {
9874b4ee
VZ
233 m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, m_text, TRUE);
234 m_subMenu->SetButtonWidget(m_buttonWidget);
2d120f83
JS
235 XtAddCallback ((Widget) m_buttonWidget,
236 XmNcascadingCallback,
237 (XtCallbackProc) wxMenuItemArmCallback,
238 (XtPointer) this);
50414e24 239 }
2d120f83
JS
240 if (m_buttonWidget)
241 XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled());
50414e24
JS
242}
243
244void wxMenuItem::DestroyItem(bool full)
245{
2d120f83 246 if (GetId() == -2)
50414e24 247 {
9874b4ee 248 ; // Nothing
31528cd3 249
50414e24 250 }
9874b4ee 251 else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu)
50414e24 252 {
2d120f83
JS
253 if (m_buttonWidget)
254 {
255 if (IsCheckable())
256 XtRemoveCallback ((Widget) m_buttonWidget, XmNvalueChangedCallback,
257 wxMenuItemCallback, (XtPointer) this);
258 else
259 XtRemoveCallback ((Widget) m_buttonWidget, XmNactivateCallback,
260 wxMenuItemCallback, (XtPointer) this);
261 XtRemoveCallback ((Widget) m_buttonWidget, XmNarmCallback,
262 wxMenuItemArmCallback, (XtPointer) this);
263 XtRemoveCallback ((Widget) m_buttonWidget, XmNdisarmCallback,
264 wxMenuItemDisarmCallback, (XtPointer) this);
265 }
50414e24 266 }
2d120f83 267 else if (GetId() == -1)
50414e24 268 {
9874b4ee 269 ; // Nothing
31528cd3 270
50414e24 271 }
2d120f83 272 else if (GetSubMenu())
50414e24 273 {
2d120f83
JS
274 if (m_buttonWidget)
275 {
276 XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback,
277 wxMenuItemArmCallback, (XtPointer) this);
278 }
9874b4ee 279 m_subMenu->DestroyMenu(full);
2d120f83
JS
280 if (full)
281 m_buttonWidget = NULL;
50414e24 282 }
31528cd3 283
2d120f83 284 if (m_buttonWidget && full)
50414e24 285 {
2d120f83
JS
286 XtDestroyWidget ((Widget) m_buttonWidget);
287 m_buttonWidget = (WXWidget) 0;
50414e24
JS
288 }
289}
290
9874b4ee 291void wxMenuItem::SetText(const wxString& label)
50414e24 292{
2d120f83 293 char mnem = wxFindMnemonic (label);
31528cd3
VZ
294 wxString label2 = wxStripMenuCodes(label);
295
9874b4ee 296 m_text = label;
31528cd3 297
2d120f83 298 if (m_buttonWidget)
50414e24 299 {
31528cd3 300 wxXmString label_str(label2);
2d120f83 301 XtVaSetValues ((Widget) m_buttonWidget,
66cbc96d 302 XmNlabelString, label_str(),
2d120f83 303 NULL);
2d120f83
JS
304 if (mnem != 0)
305 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
31528cd3 306 char *accel = wxFindAccelerator (label2);
2d120f83
JS
307 if (accel)
308 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
31528cd3
VZ
309
310 XmString accel_str = wxFindAcceleratorText (label2);
2d120f83 311 if (accel_str)
50414e24 312 {
2d120f83
JS
313 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
314 XmStringFree (accel_str);
50414e24
JS
315 }
316 }
317}
318
9874b4ee
VZ
319// ----------------------------------------------------------------------------
320// Motif callbacks
321// ----------------------------------------------------------------------------
322
af111fc3
JS
323void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
324 XtPointer WXUNUSED(ptr))
50414e24 325{
2d120f83
JS
326 wxMenuItem *item = (wxMenuItem *) clientData;
327 if (item)
50414e24 328 {
092276a8
MB
329 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
330 event.SetInt( item->GetId() );
331
2d120f83
JS
332 if (item->IsCheckable())
333 {
334 Boolean isChecked = FALSE;
092276a8
MB
335 XtVaGetValues ((Widget) item->GetButtonWidget(),
336 XmNset, & isChecked,
337 NULL);
9874b4ee
VZ
338
339 // only set the flag, don't actually check anything
340 item->wxMenuItemBase::Check(isChecked);
092276a8 341 event.SetInt(isChecked);
2d120f83 342 }
092276a8 343
2d120f83
JS
344 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
345 {
092276a8 346 event.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 347
092276a8
MB
348 item->GetMenuBar()->GetMenuBarFrame()
349 ->GetEventHandler()->ProcessEvent(event);
2d120f83 350 }
7e1bcfa8 351 // this is the child of a popup menu
2d120f83
JS
352 else if (item->GetTopMenu())
353 {
2d120f83 354 event.SetEventObject(item->GetTopMenu());
31528cd3 355
2d120f83 356 item->GetTopMenu()->ProcessCommand (event);
7e1bcfa8 357
092276a8 358 // Since PopupMenu under Motif still grab right mouse
7e1bcfa8
MB
359 // button events after it was closed, we need to delete
360 // the associated widgets to allow next PopUpMenu to
361 // appear; this needs to be done there because doing it in
362 // a WorkProc as before may cause crashes if a menu item causes
363 // the parent window of the menu to be destroyed
364 item->GetTopMenu()->DestroyWidgetAndDetach();
2d120f83 365 }
50414e24
JS
366 }
367}
368
af111fc3
JS
369void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
370 XtPointer WXUNUSED(ptr))
50414e24 371{
2d120f83
JS
372 wxMenuItem *item = (wxMenuItem *) clientData;
373 if (item)
50414e24 374 {
2d120f83
JS
375 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
376 {
377 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
378 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 379
092276a8
MB
380 item->GetMenuBar()->GetMenuBarFrame()
381 ->GetEventHandler()->ProcessEvent(menuEvent);
2d120f83 382 }
50414e24
JS
383 }
384}
385
31528cd3 386void
af111fc3
JS
387wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
388 XtPointer WXUNUSED(ptr))
50414e24 389{
2d120f83
JS
390 wxMenuItem *item = (wxMenuItem *) clientData;
391 if (item)
50414e24 392 {
2d120f83
JS
393 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
394 {
395 // TODO: not sure this is correct, since -1 means something
396 // special to event system
397 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, -1);
398 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 399
092276a8
MB
400 item->GetMenuBar()->GetMenuBarFrame()
401 ->GetEventHandler()->ProcessEvent(menuEvent);
2d120f83 402 }
50414e24
JS
403 }
404}
405