]> git.saurik.com Git - wxWidgets.git/blame - src/motif/menuitem.cpp
one more note
[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
9874b4ee
VZ
16#ifdef __GNUG__
17 #pragma implementation "menuitem.h"
18#endif
19
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
4bb6408c
JS
24#include "wx/menu.h"
25#include "wx/menuitem.h"
50414e24
JS
26#include "wx/utils.h"
27#include "wx/frame.h"
28
29#include <Xm/Label.h>
30#include <Xm/LabelG.h>
31#include <Xm/CascadeBG.h>
32#include <Xm/CascadeB.h>
33#include <Xm/SeparatoG.h>
34#include <Xm/PushBG.h>
35#include <Xm/ToggleB.h>
36#include <Xm/ToggleBG.h>
37#include <Xm/RowColumn.h>
38
39#include "wx/motif/private.h"
40
9874b4ee
VZ
41// ----------------------------------------------------------------------------
42// functions prototypes
43// ----------------------------------------------------------------------------
44
45static void wxMenuItemCallback(Widget w, XtPointer clientData, XtPointer ptr);
46static void wxMenuItemArmCallback(Widget w, XtPointer clientData, XtPointer ptr);
47static void wxMenuItemDisarmCallback(Widget w, XtPointer clientData, XtPointer ptr);
4bb6408c
JS
48
49// ============================================================================
50// implementation
51// ============================================================================
52
53// ----------------------------------------------------------------------------
54// dynamic classes implementation
55// ----------------------------------------------------------------------------
56
57#if !USE_SHARED_LIBRARY
9874b4ee 58 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
4bb6408c
JS
59#endif //USE_SHARED_LIBRARY
60
61// ----------------------------------------------------------------------------
62// wxMenuItem
63// ----------------------------------------------------------------------------
64
65// ctor & dtor
66// -----------
67
68wxMenuItem::wxMenuItem(wxMenu *pParentMenu, int id,
69 const wxString& strName, const wxString& strHelp,
70 bool bCheckable,
9874b4ee 71 wxMenu *pSubMenu)
4bb6408c 72{
9874b4ee
VZ
73 wxASSERT_MSG( pParentMenu != NULL, wxT("menuitem should have a menu") );
74
75 // common init
76 m_parentMenu = pParentMenu;
77 m_subMenu = pSubMenu;
78 m_id = id;
79 m_isEnabled = TRUE;
80 m_isChecked = FALSE;
81 m_help = strHelp;
82 m_isCheckable = bCheckable;
83 m_text = strName;
84
85 // Motif-specific
86 m_menuBar = NULL;
2d120f83 87 m_buttonWidget = (WXWidget) NULL;
9874b4ee 88 m_topMenu = NULL;
4bb6408c
JS
89}
90
31528cd3 91wxMenuItem::~wxMenuItem()
4bb6408c
JS
92{
93}
94
95// misc
96// ----
97
98// delete the sub menu
99void wxMenuItem::DeleteSubMenu()
100{
9874b4ee 101 wxASSERT( m_subMenu != NULL );
31528cd3 102
9874b4ee
VZ
103 delete m_subMenu;
104 m_subMenu = NULL;
4bb6408c
JS
105}
106
107// change item state
108// -----------------
109
110void wxMenuItem::Enable(bool bDoEnable)
111{
9874b4ee 112 if ( m_isChecked != bDoEnable )
4bb6408c 113 {
9874b4ee
VZ
114 if ( !IsSubMenu() )
115 {
116 // normal menu item
2d120f83
JS
117 if (m_buttonWidget)
118 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
119 }
120 else // submenu
121 {
122 // Maybe we should apply this to all items in the submenu?
123 // Or perhaps it works anyway.
124 if (m_buttonWidget)
125 XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable);
126 }
31528cd3 127
9874b4ee 128 wxMenuItemBase::Enable(bDoEnable);
4bb6408c 129 }
4bb6408c
JS
130}
131
132void wxMenuItem::Check(bool bDoCheck)
133{
2d120f83 134 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
31528cd3 135
9874b4ee 136 if ( m_isChecked != bDoCheck )
50414e24 137 {
9874b4ee 138 if ( m_buttonWidget )
2d120f83 139 {
9874b4ee
VZ
140 wxASSERT_MSG( XtIsSubclass((Widget)m_buttonWidget,
141 xmToggleButtonGadgetClass),
142 wxT("checkable menu item must be a toggle button") );
143
144 XtVaSetValues((Widget)m_buttonWidget,
145 XmNset, (Boolean)bDoCheck,
146 NULL);
2d120f83 147 }
9874b4ee
VZ
148
149 wxMenuItemBase::Check(bDoCheck);
50414e24 150 }
50414e24
JS
151}
152
c71830c3
VZ
153wxString wxMenuItem::GetLabel() const
154{
155 return wxStripMenuCodes(m_text);
156}
157
158
159// ----------------------------------------------------------------------------
160// wxMenuItemBase
161// ----------------------------------------------------------------------------
162
163wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
164 int id,
165 const wxString& name,
166 const wxString& help,
167 bool isCheckable,
168 wxMenu *subMenu)
169{
170 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
171}
172
173// ----------------------------------------------------------------------------
174// Motif-specific
175// ----------------------------------------------------------------------------
50414e24
JS
176
177void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu)
178{
2d120f83
JS
179 m_menuBar = menuBar;
180 m_topMenu = topMenu;
31528cd3 181
2d120f83 182 if (GetId() == -2)
50414e24 183 {
2d120f83 184 // Id=-2 identifies a Title item.
31528cd3 185 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
9874b4ee 186 (wxStripMenuCodes(m_text),
2d120f83 187 xmLabelGadgetClass, (Widget) menu, NULL);
50414e24 188 }
9874b4ee 189 else if ((!m_text.IsNull() && m_text != "") && (!m_subMenu))
50414e24 190 {
9874b4ee 191 wxString strName = wxStripMenuCodes(m_text);
2d120f83
JS
192 if (IsCheckable())
193 {
31528cd3 194 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
2d120f83
JS
195 xmToggleButtonGadgetClass, (Widget) menu,
196 NULL);
197 XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
198 }
199 else
31528cd3 200 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
2d120f83
JS
201 xmPushButtonGadgetClass, (Widget) menu,
202 NULL);
9874b4ee 203 char mnem = wxFindMnemonic (m_text);
2d120f83
JS
204 if (mnem != 0)
205 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
31528cd3 206
2d120f83
JS
207 //// TODO: proper accelerator treatment. What does wxFindAccelerator
208 //// look for?
9874b4ee 209 strName = m_text;
31528cd3 210 char *accel = wxFindAccelerator (strName);
2d120f83
JS
211 if (accel)
212 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
31528cd3 213
2d120f83 214 // TODO: What does this do?
31528cd3 215 XmString accel_str = wxFindAcceleratorText (strName);
2d120f83
JS
216 if (accel_str)
217 {
218 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
219 XmStringFree (accel_str);
220 }
31528cd3 221
2d120f83
JS
222 if (IsCheckable())
223 XtAddCallback ((Widget) m_buttonWidget,
224 XmNvalueChangedCallback,
225 (XtCallbackProc) wxMenuItemCallback,
226 (XtPointer) this);
227 else
228 XtAddCallback ((Widget) m_buttonWidget,
229 XmNactivateCallback,
230 (XtCallbackProc) wxMenuItemCallback,
231 (XtPointer) this);
232 XtAddCallback ((Widget) m_buttonWidget,
233 XmNarmCallback,
234 (XtCallbackProc) wxMenuItemArmCallback,
235 (XtPointer) this);
236 XtAddCallback ((Widget) m_buttonWidget,
237 XmNdisarmCallback,
238 (XtCallbackProc) wxMenuItemDisarmCallback,
239 (XtPointer) this);
50414e24 240 }
2d120f83 241 else if (GetId() == -1)
50414e24 242 {
2d120f83
JS
243 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
244 xmSeparatorGadgetClass, (Widget) menu, NULL);
50414e24 245 }
9874b4ee 246 else if (m_subMenu)
50414e24 247 {
9874b4ee
VZ
248 m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, m_text, TRUE);
249 m_subMenu->SetButtonWidget(m_buttonWidget);
2d120f83
JS
250 XtAddCallback ((Widget) m_buttonWidget,
251 XmNcascadingCallback,
252 (XtCallbackProc) wxMenuItemArmCallback,
253 (XtPointer) this);
50414e24 254 }
2d120f83
JS
255 if (m_buttonWidget)
256 XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled());
50414e24
JS
257}
258
259void wxMenuItem::DestroyItem(bool full)
260{
2d120f83 261 if (GetId() == -2)
50414e24 262 {
9874b4ee 263 ; // Nothing
31528cd3 264
50414e24 265 }
9874b4ee 266 else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu)
50414e24 267 {
2d120f83
JS
268 if (m_buttonWidget)
269 {
270 if (IsCheckable())
271 XtRemoveCallback ((Widget) m_buttonWidget, XmNvalueChangedCallback,
272 wxMenuItemCallback, (XtPointer) this);
273 else
274 XtRemoveCallback ((Widget) m_buttonWidget, XmNactivateCallback,
275 wxMenuItemCallback, (XtPointer) this);
276 XtRemoveCallback ((Widget) m_buttonWidget, XmNarmCallback,
277 wxMenuItemArmCallback, (XtPointer) this);
278 XtRemoveCallback ((Widget) m_buttonWidget, XmNdisarmCallback,
279 wxMenuItemDisarmCallback, (XtPointer) this);
280 }
50414e24 281 }
2d120f83 282 else if (GetId() == -1)
50414e24 283 {
9874b4ee 284 ; // Nothing
31528cd3 285
50414e24 286 }
2d120f83 287 else if (GetSubMenu())
50414e24 288 {
2d120f83
JS
289 if (m_buttonWidget)
290 {
291 XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback,
292 wxMenuItemArmCallback, (XtPointer) this);
293 }
9874b4ee 294 m_subMenu->DestroyMenu(full);
2d120f83
JS
295 if (full)
296 m_buttonWidget = NULL;
50414e24 297 }
31528cd3 298
2d120f83 299 if (m_buttonWidget && full)
50414e24 300 {
2d120f83
JS
301 XtDestroyWidget ((Widget) m_buttonWidget);
302 m_buttonWidget = (WXWidget) 0;
50414e24
JS
303 }
304}
305
9874b4ee 306void wxMenuItem::SetText(const wxString& label)
50414e24 307{
2d120f83 308 char mnem = wxFindMnemonic (label);
31528cd3
VZ
309 wxString label2 = wxStripMenuCodes(label);
310
9874b4ee 311 m_text = label;
31528cd3 312
2d120f83 313 if (m_buttonWidget)
50414e24 314 {
31528cd3 315 wxXmString label_str(label2);
2d120f83 316 XtVaSetValues ((Widget) m_buttonWidget,
66cbc96d 317 XmNlabelString, label_str(),
2d120f83 318 NULL);
2d120f83
JS
319 if (mnem != 0)
320 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
31528cd3 321 char *accel = wxFindAccelerator (label2);
2d120f83
JS
322 if (accel)
323 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
31528cd3
VZ
324
325 XmString accel_str = wxFindAcceleratorText (label2);
2d120f83 326 if (accel_str)
50414e24 327 {
2d120f83
JS
328 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
329 XmStringFree (accel_str);
50414e24
JS
330 }
331 }
332}
333
9874b4ee
VZ
334// ----------------------------------------------------------------------------
335// Motif callbacks
336// ----------------------------------------------------------------------------
337
af111fc3
JS
338void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
339 XtPointer WXUNUSED(ptr))
50414e24 340{
2d120f83
JS
341 wxMenuItem *item = (wxMenuItem *) clientData;
342 if (item)
50414e24 343 {
2d120f83
JS
344 if (item->IsCheckable())
345 {
346 Boolean isChecked = FALSE;
347 XtVaGetValues ((Widget) item->GetButtonWidget(), XmNset, & isChecked, NULL);
9874b4ee
VZ
348
349 // only set the flag, don't actually check anything
350 item->wxMenuItemBase::Check(isChecked);
2d120f83
JS
351 }
352 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
353 {
354 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, item->GetId());
355 commandEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
356 commandEvent.SetInt( item->GetId() );
31528cd3 357
2d120f83
JS
358 item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(commandEvent);
359 }
360 else if (item->GetTopMenu())
361 {
362 wxCommandEvent event (wxEVT_COMMAND_MENU_SELECTED, item->GetId());
363 event.SetEventObject(item->GetTopMenu());
364 event.SetInt( item->GetId() );
31528cd3 365
2d120f83
JS
366 item->GetTopMenu()->ProcessCommand (event);
367 }
50414e24
JS
368 }
369}
370
af111fc3
JS
371void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
372 XtPointer WXUNUSED(ptr))
50414e24 373{
2d120f83
JS
374 wxMenuItem *item = (wxMenuItem *) clientData;
375 if (item)
50414e24 376 {
2d120f83
JS
377 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
378 {
379 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
380 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 381
2d120f83
JS
382 item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent);
383 }
50414e24
JS
384 }
385}
386
31528cd3 387void
af111fc3
JS
388wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
389 XtPointer WXUNUSED(ptr))
50414e24 390{
2d120f83
JS
391 wxMenuItem *item = (wxMenuItem *) clientData;
392 if (item)
50414e24 393 {
2d120f83
JS
394 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
395 {
396 // TODO: not sure this is correct, since -1 means something
397 // special to event system
398 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, -1);
399 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
31528cd3 400
2d120f83
JS
401 item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent);
402 }
50414e24
JS
403 }
404}
405