]> git.saurik.com Git - wxWidgets.git/blame - src/motif/menuitem.cpp
Fix recreating of wxBitmapComboBox using untyped client data.
[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
4bb6408c 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
4bb6408c
JS
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
9874b4ee 12// declarations
4bb6408c
JS
13// ============================================================================
14
9874b4ee
VZ
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
1248b41f
MB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
4bb6408c 22#include "wx/menuitem.h"
ee0a94cf 23#include "wx/stockitem.h"
de6185e2
WS
24
25#ifndef WX_PRECOMP
26 #include "wx/utils.h"
76b49cf4 27 #include "wx/frame.h"
3b3dc801 28 #include "wx/menu.h"
de6185e2
WS
29#endif
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
4bb6408c
JS
65// ----------------------------------------------------------------------------
66// wxMenuItem
67// ----------------------------------------------------------------------------
68
69// ctor & dtor
70// -----------
71
d65c269b
VZ
72wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
73 int id,
74 const wxString& strName,
75 const wxString& strHelp,
76 wxItemKind kind,
9874b4ee 77 wxMenu *pSubMenu)
d65c269b 78 : wxMenuItemBase(pParentMenu, id, strName, strHelp, kind, pSubMenu)
4bb6408c 79{
9874b4ee
VZ
80 // Motif-specific
81 m_menuBar = NULL;
2d120f83 82 m_buttonWidget = (WXWidget) NULL;
9874b4ee 83 m_topMenu = NULL;
4bb6408c
JS
84}
85
31528cd3 86wxMenuItem::~wxMenuItem()
4bb6408c
JS
87{
88}
89
4bb6408c
JS
90// change item state
91// -----------------
92
93void wxMenuItem::Enable(bool bDoEnable)
94{
9d08a1ed 95 if ( m_isEnabled != bDoEnable )
4bb6408c 96 {
9874b4ee
VZ
97 if ( !IsSubMenu() )
98 {
99 // normal menu item
2d120f83
JS
100 if (m_buttonWidget)
101 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
102 }
103 else // submenu
104 {
105 // Maybe we should apply this to all items in the submenu?
106 // Or perhaps it works anyway.
107 if (m_buttonWidget)
108 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
109 }
31528cd3 110
9874b4ee 111 wxMenuItemBase::Enable(bDoEnable);
4bb6408c 112 }
4bb6408c
JS
113}
114
115void wxMenuItem::Check(bool bDoCheck)
116{
2d120f83 117 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
31528cd3 118
9874b4ee 119 if ( m_isChecked != bDoCheck )
50414e24 120 {
9874b4ee 121 if ( m_buttonWidget )
2d120f83 122 {
9874b4ee
VZ
123 wxASSERT_MSG( XtIsSubclass((Widget)m_buttonWidget,
124 xmToggleButtonGadgetClass),
125 wxT("checkable menu item must be a toggle button") );
126
127 XtVaSetValues((Widget)m_buttonWidget,
128 XmNset, (Boolean)bDoCheck,
129 NULL);
2d120f83 130 }
9874b4ee
VZ
131
132 wxMenuItemBase::Check(bDoCheck);
50414e24 133 }
50414e24
JS
134}
135
c71830c3
VZ
136// ----------------------------------------------------------------------------
137// wxMenuItemBase
138// ----------------------------------------------------------------------------
139
140wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
141 int id,
142 const wxString& name,
143 const wxString& help,
d65c269b 144 wxItemKind kind,
c71830c3
VZ
145 wxMenu *subMenu)
146{
d65c269b 147 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
c71830c3
VZ
148}
149
150// ----------------------------------------------------------------------------
151// Motif-specific
152// ----------------------------------------------------------------------------
50414e24 153
51c9a5db
MB
154void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar,
155 wxMenu * topMenu, size_t index)
50414e24 156{
2d120f83
JS
157 m_menuBar = menuBar;
158 m_topMenu = topMenu;
31528cd3 159
15b845f2 160 if (GetId() == -3)
50414e24 161 {
15b845f2 162 // Id=-3 identifies a Title item.
31528cd3 163 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
9874b4ee 164 (wxStripMenuCodes(m_text),
2d120f83 165 xmLabelGadgetClass, (Widget) menu, NULL);
50414e24 166 }
ee0a94cf 167 else if (!IsSeparator() && !m_subMenu)
50414e24 168 {
ee0a94cf
RR
169 wxString txt = m_text;
170
171 if (m_text.IsEmpty())
172 {
4fc3eebc 173 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
ee0a94cf
RR
174 txt = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR|wxSTOCK_WITH_MNEMONIC);
175 }
176
177 wxString strName = wxStripMenuCodes(txt);
2d120f83
JS
178 if (IsCheckable())
179 {
31528cd3 180 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
2d120f83 181 xmToggleButtonGadgetClass, (Widget) menu,
51c9a5db
MB
182#ifdef XmNpositionIndex
183 XmNpositionIndex, index,
184#endif
2d120f83
JS
185 NULL);
186 XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
187 }
188 else
31528cd3 189 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
2d120f83 190 xmPushButtonGadgetClass, (Widget) menu,
51c9a5db
MB
191#ifdef XmNpositionIndex
192 XmNpositionIndex, index,
193#endif
2d120f83 194 NULL);
9874b4ee 195 char mnem = wxFindMnemonic (m_text);
2d120f83
JS
196 if (mnem != 0)
197 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
31528cd3 198
2d120f83
JS
199 //// TODO: proper accelerator treatment. What does wxFindAccelerator
200 //// look for?
9874b4ee 201 strName = m_text;
31528cd3 202 char *accel = wxFindAccelerator (strName);
2d120f83
JS
203 if (accel)
204 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
31528cd3 205
2d120f83 206 // TODO: What does this do?
31528cd3 207 XmString accel_str = wxFindAcceleratorText (strName);
2d120f83
JS
208 if (accel_str)
209 {
210 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
211 XmStringFree (accel_str);
212 }
31528cd3 213
2d120f83
JS
214 if (IsCheckable())
215 XtAddCallback ((Widget) m_buttonWidget,
216 XmNvalueChangedCallback,
217 (XtCallbackProc) wxMenuItemCallback,
218 (XtPointer) this);
219 else
220 XtAddCallback ((Widget) m_buttonWidget,
221 XmNactivateCallback,
222 (XtCallbackProc) wxMenuItemCallback,
223 (XtPointer) this);
224 XtAddCallback ((Widget) m_buttonWidget,
225 XmNarmCallback,
226 (XtCallbackProc) wxMenuItemArmCallback,
227 (XtPointer) this);
228 XtAddCallback ((Widget) m_buttonWidget,
229 XmNdisarmCallback,
230 (XtCallbackProc) wxMenuItemDisarmCallback,
231 (XtPointer) this);
50414e24 232 }
ee0a94cf 233 else if (IsSeparator())
50414e24 234 {
2d120f83 235 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
51c9a5db
MB
236 xmSeparatorGadgetClass, (Widget) menu,
237#ifndef XmNpositionIndex
238 XmNpositionIndex, index,
239#endif
240 NULL);
50414e24 241 }
9874b4ee 242 else if (m_subMenu)
50414e24 243 {
51c9a5db 244 m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, index, m_text, true);
9874b4ee 245 m_subMenu->SetButtonWidget(m_buttonWidget);
2d120f83
JS
246 XtAddCallback ((Widget) m_buttonWidget,
247 XmNcascadingCallback,
248 (XtCallbackProc) wxMenuItemArmCallback,
249 (XtPointer) this);
50414e24 250 }
2d120f83
JS
251 if (m_buttonWidget)
252 XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled());
50414e24
JS
253}
254
255void wxMenuItem::DestroyItem(bool full)
256{
15b845f2 257 if (GetId() == -3)
50414e24 258 {
9874b4ee 259 ; // Nothing
31528cd3 260
50414e24 261 }
7520f3da 262 else if (!m_text.empty() && !m_subMenu)
50414e24 263 {
2d120f83
JS
264 if (m_buttonWidget)
265 {
266 if (IsCheckable())
267 XtRemoveCallback ((Widget) m_buttonWidget, XmNvalueChangedCallback,
268 wxMenuItemCallback, (XtPointer) this);
269 else
270 XtRemoveCallback ((Widget) m_buttonWidget, XmNactivateCallback,
271 wxMenuItemCallback, (XtPointer) this);
272 XtRemoveCallback ((Widget) m_buttonWidget, XmNarmCallback,
273 wxMenuItemArmCallback, (XtPointer) this);
274 XtRemoveCallback ((Widget) m_buttonWidget, XmNdisarmCallback,
275 wxMenuItemDisarmCallback, (XtPointer) this);
276 }
50414e24 277 }
fa7134b0 278 else if (IsSeparator())
50414e24 279 {
9874b4ee 280 ; // Nothing
31528cd3 281
50414e24 282 }
2d120f83 283 else if (GetSubMenu())
50414e24 284 {
2d120f83
JS
285 if (m_buttonWidget)
286 {
287 XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback,
288 wxMenuItemArmCallback, (XtPointer) this);
289 }
9874b4ee 290 m_subMenu->DestroyMenu(full);
2d120f83
JS
291 if (full)
292 m_buttonWidget = NULL;
50414e24 293 }
31528cd3 294
2d120f83 295 if (m_buttonWidget && full)
50414e24 296 {
2d120f83
JS
297 XtDestroyWidget ((Widget) m_buttonWidget);
298 m_buttonWidget = (WXWidget) 0;
50414e24
JS
299 }
300}
301
52af3158 302void wxMenuItem::SetItemLabel(const wxString& label)
50414e24 303{
2d120f83 304 char mnem = wxFindMnemonic (label);
31528cd3
VZ
305 wxString label2 = wxStripMenuCodes(label);
306
9874b4ee 307 m_text = label;
31528cd3 308
2d120f83 309 if (m_buttonWidget)
50414e24 310 {
31528cd3 311 wxXmString label_str(label2);
2d120f83 312 XtVaSetValues ((Widget) m_buttonWidget,
66cbc96d 313 XmNlabelString, label_str(),
2d120f83 314 NULL);
2d120f83
JS
315 if (mnem != 0)
316 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
31528cd3 317 char *accel = wxFindAccelerator (label2);
2d120f83
JS
318 if (accel)
319 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
31528cd3
VZ
320
321 XmString accel_str = wxFindAcceleratorText (label2);
2d120f83 322 if (accel_str)
50414e24 323 {
2d120f83
JS
324 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
325 XmStringFree (accel_str);
50414e24
JS
326 }
327 }
328}
329
9874b4ee
VZ
330// ----------------------------------------------------------------------------
331// Motif callbacks
332// ----------------------------------------------------------------------------
333
af111fc3
JS
334void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
335 XtPointer WXUNUSED(ptr))
50414e24 336{
2d120f83
JS
337 wxMenuItem *item = (wxMenuItem *) clientData;
338 if (item)
50414e24 339 {
ce7fe42e 340 wxCommandEvent event(wxEVT_MENU, item->GetId());
092276a8
MB
341 event.SetInt( item->GetId() );
342
2d120f83
JS
343 if (item->IsCheckable())
344 {
96be256b 345 Boolean isChecked = false;
092276a8
MB
346 XtVaGetValues ((Widget) item->GetButtonWidget(),
347 XmNset, & isChecked,
348 NULL);
9874b4ee
VZ
349
350 // only set the flag, don't actually check anything
351 item->wxMenuItemBase::Check(isChecked);
092276a8 352 event.SetInt(isChecked);
2d120f83 353 }
092276a8 354
2d120f83
JS
355 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
356 {
092276a8 357 event.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 358
092276a8 359 item->GetMenuBar()->GetMenuBarFrame()
937013e0 360 ->HandleWindowEvent(event);
2d120f83 361 }
7e1bcfa8 362 // this is the child of a popup menu
2d120f83
JS
363 else if (item->GetTopMenu())
364 {
2d120f83 365 event.SetEventObject(item->GetTopMenu());
31528cd3 366
2d120f83 367 item->GetTopMenu()->ProcessCommand (event);
7e1bcfa8 368
092276a8 369 // Since PopupMenu under Motif still grab right mouse
7e1bcfa8
MB
370 // button events after it was closed, we need to delete
371 // the associated widgets to allow next PopUpMenu to
372 // appear; this needs to be done there because doing it in
373 // a WorkProc as before may cause crashes if a menu item causes
374 // the parent window of the menu to be destroyed
375 item->GetTopMenu()->DestroyWidgetAndDetach();
2d120f83 376 }
50414e24
JS
377 }
378}
379
af111fc3
JS
380void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
381 XtPointer WXUNUSED(ptr))
50414e24 382{
2d120f83
JS
383 wxMenuItem *item = (wxMenuItem *) clientData;
384 if (item)
50414e24 385 {
2d120f83
JS
386 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
387 {
388 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
389 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 390
092276a8 391 item->GetMenuBar()->GetMenuBarFrame()
937013e0 392 ->HandleWindowEvent(menuEvent);
2d120f83 393 }
50414e24
JS
394 }
395}
396
31528cd3 397void
af111fc3
JS
398wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
399 XtPointer WXUNUSED(ptr))
50414e24 400{
2d120f83
JS
401 wxMenuItem *item = (wxMenuItem *) clientData;
402 if (item)
50414e24 403 {
2d120f83
JS
404 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
405 {
406 // TODO: not sure this is correct, since -1 means something
407 // special to event system
408 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, -1);
409 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 410
092276a8 411 item->GetMenuBar()->GetMenuBarFrame()
937013e0 412 ->HandleWindowEvent(menuEvent);
2d120f83 413 }
50414e24
JS
414 }
415}