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