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