]> git.saurik.com Git - wxWidgets.git/blame - src/motif/menuitem.cpp
Missing #includes to allow wx/mac/carbon/tooltip.h be included first by user.
[wxWidgets.git] / src / motif / menuitem.cpp
CommitLineData
4bb6408c 1///////////////////////////////////////////////////////////////////////////////
7520f3da 2// Name: src/motif/menuitem.cpp
4bb6408c
JS
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
4bb6408c 23#include "wx/menuitem.h"
de6185e2
WS
24
25#ifndef WX_PRECOMP
26 #include "wx/utils.h"
76b49cf4 27 #include "wx/frame.h"
de6185e2
WS
28#endif
29
30#include "wx/menu.h"
50414e24 31
338dd992
JJ
32#ifdef __VMS__
33#pragma message disable nosimpint
34#endif
50414e24
JS
35#include <Xm/Label.h>
36#include <Xm/LabelG.h>
37#include <Xm/CascadeBG.h>
38#include <Xm/CascadeB.h>
39#include <Xm/SeparatoG.h>
40#include <Xm/PushBG.h>
41#include <Xm/ToggleB.h>
42#include <Xm/ToggleBG.h>
43#include <Xm/RowColumn.h>
338dd992
JJ
44#ifdef __VMS__
45#pragma message enable nosimpint
46#endif
50414e24
JS
47
48#include "wx/motif/private.h"
49
9874b4ee
VZ
50// ----------------------------------------------------------------------------
51// functions prototypes
52// ----------------------------------------------------------------------------
53
54static void wxMenuItemCallback(Widget w, XtPointer clientData, XtPointer ptr);
55static void wxMenuItemArmCallback(Widget w, XtPointer clientData, XtPointer ptr);
56static void wxMenuItemDisarmCallback(Widget w, XtPointer clientData, XtPointer ptr);
4bb6408c
JS
57
58// ============================================================================
59// implementation
60// ============================================================================
61
62// ----------------------------------------------------------------------------
63// dynamic classes implementation
64// ----------------------------------------------------------------------------
65
d65c269b 66IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
4bb6408c
JS
67
68// ----------------------------------------------------------------------------
69// wxMenuItem
70// ----------------------------------------------------------------------------
71
72// ctor & dtor
73// -----------
74
d65c269b
VZ
75wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
76 int id,
77 const wxString& strName,
78 const wxString& strHelp,
79 wxItemKind kind,
9874b4ee 80 wxMenu *pSubMenu)
d65c269b 81 : wxMenuItemBase(pParentMenu, id, strName, strHelp, kind, pSubMenu)
4bb6408c 82{
9874b4ee
VZ
83 // Motif-specific
84 m_menuBar = NULL;
2d120f83 85 m_buttonWidget = (WXWidget) NULL;
9874b4ee 86 m_topMenu = NULL;
4bb6408c
JS
87}
88
31528cd3 89wxMenuItem::~wxMenuItem()
4bb6408c
JS
90{
91}
92
4bb6408c
JS
93// change item state
94// -----------------
95
96void wxMenuItem::Enable(bool bDoEnable)
97{
9d08a1ed 98 if ( m_isEnabled != bDoEnable )
4bb6408c 99 {
9874b4ee
VZ
100 if ( !IsSubMenu() )
101 {
102 // normal menu item
2d120f83
JS
103 if (m_buttonWidget)
104 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
105 }
106 else // submenu
107 {
108 // Maybe we should apply this to all items in the submenu?
109 // Or perhaps it works anyway.
110 if (m_buttonWidget)
111 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
112 }
31528cd3 113
9874b4ee 114 wxMenuItemBase::Enable(bDoEnable);
4bb6408c 115 }
4bb6408c
JS
116}
117
118void wxMenuItem::Check(bool bDoCheck)
119{
2d120f83 120 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
31528cd3 121
9874b4ee 122 if ( m_isChecked != bDoCheck )
50414e24 123 {
9874b4ee 124 if ( m_buttonWidget )
2d120f83 125 {
9874b4ee
VZ
126 wxASSERT_MSG( XtIsSubclass((Widget)m_buttonWidget,
127 xmToggleButtonGadgetClass),
128 wxT("checkable menu item must be a toggle button") );
129
130 XtVaSetValues((Widget)m_buttonWidget,
131 XmNset, (Boolean)bDoCheck,
132 NULL);
2d120f83 133 }
9874b4ee
VZ
134
135 wxMenuItemBase::Check(bDoCheck);
50414e24 136 }
50414e24
JS
137}
138
3b59cdbf
VZ
139/* static */
140wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
c71830c3 141{
c2754d29 142 return wxStripMenuCodes(text);
c71830c3
VZ
143}
144
c71830c3
VZ
145// ----------------------------------------------------------------------------
146// wxMenuItemBase
147// ----------------------------------------------------------------------------
148
149wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
150 int id,
151 const wxString& name,
152 const wxString& help,
d65c269b 153 wxItemKind kind,
c71830c3
VZ
154 wxMenu *subMenu)
155{
d65c269b 156 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
c71830c3
VZ
157}
158
159// ----------------------------------------------------------------------------
160// Motif-specific
161// ----------------------------------------------------------------------------
50414e24 162
51c9a5db
MB
163void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar,
164 wxMenu * topMenu, size_t index)
50414e24 165{
2d120f83
JS
166 m_menuBar = menuBar;
167 m_topMenu = topMenu;
31528cd3 168
15b845f2 169 if (GetId() == -3)
50414e24 170 {
15b845f2 171 // Id=-3 identifies a Title item.
31528cd3 172 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
9874b4ee 173 (wxStripMenuCodes(m_text),
2d120f83 174 xmLabelGadgetClass, (Widget) menu, NULL);
50414e24 175 }
7520f3da 176 else if (!m_text.empty() && !m_subMenu)
50414e24 177 {
9874b4ee 178 wxString strName = wxStripMenuCodes(m_text);
2d120f83
JS
179 if (IsCheckable())
180 {
31528cd3 181 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
2d120f83 182 xmToggleButtonGadgetClass, (Widget) menu,
51c9a5db
MB
183#ifdef XmNpositionIndex
184 XmNpositionIndex, index,
185#endif
2d120f83
JS
186 NULL);
187 XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
188 }
189 else
31528cd3 190 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
2d120f83 191 xmPushButtonGadgetClass, (Widget) menu,
51c9a5db
MB
192#ifdef XmNpositionIndex
193 XmNpositionIndex, index,
194#endif
2d120f83 195 NULL);
9874b4ee 196 char mnem = wxFindMnemonic (m_text);
2d120f83
JS
197 if (mnem != 0)
198 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
31528cd3 199
2d120f83
JS
200 //// TODO: proper accelerator treatment. What does wxFindAccelerator
201 //// look for?
9874b4ee 202 strName = m_text;
31528cd3 203 char *accel = wxFindAccelerator (strName);
2d120f83
JS
204 if (accel)
205 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
31528cd3 206
2d120f83 207 // TODO: What does this do?
31528cd3 208 XmString accel_str = wxFindAcceleratorText (strName);
2d120f83
JS
209 if (accel_str)
210 {
211 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
212 XmStringFree (accel_str);
213 }
31528cd3 214
2d120f83
JS
215 if (IsCheckable())
216 XtAddCallback ((Widget) m_buttonWidget,
217 XmNvalueChangedCallback,
218 (XtCallbackProc) wxMenuItemCallback,
219 (XtPointer) this);
220 else
221 XtAddCallback ((Widget) m_buttonWidget,
222 XmNactivateCallback,
223 (XtCallbackProc) wxMenuItemCallback,
224 (XtPointer) this);
225 XtAddCallback ((Widget) m_buttonWidget,
226 XmNarmCallback,
227 (XtCallbackProc) wxMenuItemArmCallback,
228 (XtPointer) this);
229 XtAddCallback ((Widget) m_buttonWidget,
230 XmNdisarmCallback,
231 (XtCallbackProc) wxMenuItemDisarmCallback,
232 (XtPointer) this);
50414e24 233 }
15b845f2 234 else if (GetId() == wxID_SEPARATOR)
50414e24 235 {
2d120f83 236 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
51c9a5db
MB
237 xmSeparatorGadgetClass, (Widget) menu,
238#ifndef XmNpositionIndex
239 XmNpositionIndex, index,
240#endif
241 NULL);
50414e24 242 }
9874b4ee 243 else if (m_subMenu)
50414e24 244 {
51c9a5db 245 m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, index, m_text, true);
9874b4ee 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{
15b845f2 258 if (GetId() == -3)
50414e24 259 {
9874b4ee 260 ; // Nothing
31528cd3 261
50414e24 262 }
7520f3da 263 else if (!m_text.empty() && !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 }
15b845f2 279 else if (GetId() == wxID_SEPARATOR)
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 {
092276a8
MB
341 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
342 event.SetInt( item->GetId() );
343
2d120f83
JS
344 if (item->IsCheckable())
345 {
96be256b 346 Boolean isChecked = false;
092276a8
MB
347 XtVaGetValues ((Widget) item->GetButtonWidget(),
348 XmNset, & isChecked,
349 NULL);
9874b4ee
VZ
350
351 // only set the flag, don't actually check anything
352 item->wxMenuItemBase::Check(isChecked);
092276a8 353 event.SetInt(isChecked);
2d120f83 354 }
092276a8 355
2d120f83
JS
356 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
357 {
092276a8 358 event.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 359
092276a8
MB
360 item->GetMenuBar()->GetMenuBarFrame()
361 ->GetEventHandler()->ProcessEvent(event);
2d120f83 362 }
7e1bcfa8 363 // this is the child of a popup menu
2d120f83
JS
364 else if (item->GetTopMenu())
365 {
2d120f83 366 event.SetEventObject(item->GetTopMenu());
31528cd3 367
2d120f83 368 item->GetTopMenu()->ProcessCommand (event);
7e1bcfa8 369
092276a8 370 // Since PopupMenu under Motif still grab right mouse
7e1bcfa8
MB
371 // button events after it was closed, we need to delete
372 // the associated widgets to allow next PopUpMenu to
373 // appear; this needs to be done there because doing it in
374 // a WorkProc as before may cause crashes if a menu item causes
375 // the parent window of the menu to be destroyed
376 item->GetTopMenu()->DestroyWidgetAndDetach();
2d120f83 377 }
50414e24
JS
378 }
379}
380
af111fc3
JS
381void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
382 XtPointer WXUNUSED(ptr))
50414e24 383{
2d120f83
JS
384 wxMenuItem *item = (wxMenuItem *) clientData;
385 if (item)
50414e24 386 {
2d120f83
JS
387 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
388 {
389 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
390 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 391
092276a8
MB
392 item->GetMenuBar()->GetMenuBarFrame()
393 ->GetEventHandler()->ProcessEvent(menuEvent);
2d120f83 394 }
50414e24
JS
395 }
396}
397
31528cd3 398void
af111fc3
JS
399wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
400 XtPointer WXUNUSED(ptr))
50414e24 401{
2d120f83
JS
402 wxMenuItem *item = (wxMenuItem *) clientData;
403 if (item)
50414e24 404 {
2d120f83
JS
405 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
406 {
407 // TODO: not sure this is correct, since -1 means something
408 // special to event system
409 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, -1);
410 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 411
092276a8
MB
412 item->GetMenuBar()->GetMenuBarFrame()
413 ->GetEventHandler()->ProcessEvent(menuEvent);
2d120f83 414 }
50414e24
JS
415 }
416}