]> git.saurik.com Git - wxWidgets.git/blame - src/motif/menuitem.cpp
Commited patch for wxSizer::Replace()
[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"
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
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 161
51c9a5db
MB
162void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar,
163 wxMenu * topMenu, size_t index)
50414e24 164{
2d120f83
JS
165 m_menuBar = menuBar;
166 m_topMenu = topMenu;
31528cd3 167
15b845f2 168 if (GetId() == -3)
50414e24 169 {
15b845f2 170 // Id=-3 identifies a Title item.
31528cd3 171 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
9874b4ee 172 (wxStripMenuCodes(m_text),
2d120f83 173 xmLabelGadgetClass, (Widget) menu, NULL);
50414e24 174 }
7520f3da 175 else if (!m_text.empty() && !m_subMenu)
50414e24 176 {
9874b4ee 177 wxString strName = wxStripMenuCodes(m_text);
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 }
15b845f2 233 else if (GetId() == wxID_SEPARATOR)
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 }
15b845f2 278 else if (GetId() == wxID_SEPARATOR)
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
9874b4ee 302void wxMenuItem::SetText(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 {
092276a8
MB
340 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
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
MB
359 item->GetMenuBar()->GetMenuBarFrame()
360 ->GetEventHandler()->ProcessEvent(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
MB
391 item->GetMenuBar()->GetMenuBarFrame()
392 ->GetEventHandler()->ProcessEvent(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
MB
411 item->GetMenuBar()->GetMenuBarFrame()
412 ->GetEventHandler()->ProcessEvent(menuEvent);
2d120f83 413 }
50414e24
JS
414 }
415}