]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/menuitem.cpp
compilation fixes
[wxWidgets.git] / src / motif / menuitem.cpp
... / ...
CommitLineData
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// ============================================================================
13// declarations
14// ============================================================================
15
16#ifdef __GNUG__
17 #pragma implementation "menuitem.h"
18#endif
19
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
24#include "wx/menu.h"
25#include "wx/menuitem.h"
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
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);
48
49// ============================================================================
50// implementation
51// ============================================================================
52
53// ----------------------------------------------------------------------------
54// dynamic classes implementation
55// ----------------------------------------------------------------------------
56
57#if !USE_SHARED_LIBRARY
58 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
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,
71 wxMenu *pSubMenu)
72{
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;
87 m_buttonWidget = (WXWidget) NULL;
88 m_topMenu = NULL;
89}
90
91wxMenuItem::~wxMenuItem()
92{
93}
94
95// misc
96// ----
97
98// delete the sub menu
99void wxMenuItem::DeleteSubMenu()
100{
101 wxASSERT( m_subMenu != NULL );
102
103 delete m_subMenu;
104 m_subMenu = NULL;
105}
106
107// change item state
108// -----------------
109
110void wxMenuItem::Enable(bool bDoEnable)
111{
112 if ( m_isChecked != bDoEnable )
113 {
114 if ( !IsSubMenu() )
115 {
116 // normal menu item
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 }
127
128 wxMenuItemBase::Enable(bDoEnable);
129 }
130}
131
132void wxMenuItem::Check(bool bDoCheck)
133{
134 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
135
136 if ( m_isChecked != bDoCheck )
137 {
138 if ( m_buttonWidget )
139 {
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);
147 }
148
149 wxMenuItemBase::Check(bDoCheck);
150 }
151}
152
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// ----------------------------------------------------------------------------
176
177void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu)
178{
179 m_menuBar = menuBar;
180 m_topMenu = topMenu;
181
182 if (GetId() == -2)
183 {
184 // Id=-2 identifies a Title item.
185 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget
186 (wxStripMenuCodes(m_text),
187 xmLabelGadgetClass, (Widget) menu, NULL);
188 }
189 else if ((!m_text.IsNull() && m_text != "") && (!m_subMenu))
190 {
191 wxString strName = wxStripMenuCodes(m_text);
192 if (IsCheckable())
193 {
194 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
195 xmToggleButtonGadgetClass, (Widget) menu,
196 NULL);
197 XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL);
198 }
199 else
200 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName,
201 xmPushButtonGadgetClass, (Widget) menu,
202 NULL);
203 char mnem = wxFindMnemonic (m_text);
204 if (mnem != 0)
205 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
206
207 //// TODO: proper accelerator treatment. What does wxFindAccelerator
208 //// look for?
209 strName = m_text;
210 char *accel = wxFindAccelerator (strName);
211 if (accel)
212 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
213
214 // TODO: What does this do?
215 XmString accel_str = wxFindAcceleratorText (strName);
216 if (accel_str)
217 {
218 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
219 XmStringFree (accel_str);
220 }
221
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);
240 }
241 else if (GetId() == -1)
242 {
243 m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator",
244 xmSeparatorGadgetClass, (Widget) menu, NULL);
245 }
246 else if (m_subMenu)
247 {
248 m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, m_text, TRUE);
249 m_subMenu->SetButtonWidget(m_buttonWidget);
250 XtAddCallback ((Widget) m_buttonWidget,
251 XmNcascadingCallback,
252 (XtCallbackProc) wxMenuItemArmCallback,
253 (XtPointer) this);
254 }
255 if (m_buttonWidget)
256 XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled());
257}
258
259void wxMenuItem::DestroyItem(bool full)
260{
261 if (GetId() == -2)
262 {
263 ; // Nothing
264
265 }
266 else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu)
267 {
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 }
281 }
282 else if (GetId() == -1)
283 {
284 ; // Nothing
285
286 }
287 else if (GetSubMenu())
288 {
289 if (m_buttonWidget)
290 {
291 XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback,
292 wxMenuItemArmCallback, (XtPointer) this);
293 }
294 m_subMenu->DestroyMenu(full);
295 if (full)
296 m_buttonWidget = NULL;
297 }
298
299 if (m_buttonWidget && full)
300 {
301 XtDestroyWidget ((Widget) m_buttonWidget);
302 m_buttonWidget = (WXWidget) 0;
303 }
304}
305
306void wxMenuItem::SetText(const wxString& label)
307{
308 char mnem = wxFindMnemonic (label);
309 wxString label2 = wxStripMenuCodes(label);
310
311 m_text = label;
312
313 if (m_buttonWidget)
314 {
315 wxXmString label_str(label2);
316 XtVaSetValues ((Widget) m_buttonWidget,
317 XmNlabelString, label_str,
318 NULL);
319 if (mnem != 0)
320 XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL);
321 char *accel = wxFindAccelerator (label2);
322 if (accel)
323 XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL);
324
325 XmString accel_str = wxFindAcceleratorText (label2);
326 if (accel_str)
327 {
328 XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL);
329 XmStringFree (accel_str);
330 }
331 }
332}
333
334// ----------------------------------------------------------------------------
335// Motif callbacks
336// ----------------------------------------------------------------------------
337
338void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData,
339 XtPointer WXUNUSED(ptr))
340{
341 wxMenuItem *item = (wxMenuItem *) clientData;
342 if (item)
343 {
344 if (item->IsCheckable())
345 {
346 Boolean isChecked = FALSE;
347 XtVaGetValues ((Widget) item->GetButtonWidget(), XmNset, & isChecked, NULL);
348
349 // only set the flag, don't actually check anything
350 item->wxMenuItemBase::Check(isChecked);
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() );
357
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() );
365
366 item->GetTopMenu()->ProcessCommand (event);
367 }
368 }
369}
370
371void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData,
372 XtPointer WXUNUSED(ptr))
373{
374 wxMenuItem *item = (wxMenuItem *) clientData;
375 if (item)
376 {
377 if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame())
378 {
379 wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId());
380 menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame());
381
382 item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent);
383 }
384 }
385}
386
387void
388wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData,
389 XtPointer WXUNUSED(ptr))
390{
391 wxMenuItem *item = (wxMenuItem *) clientData;
392 if (item)
393 {
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());
400
401 item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent);
402 }
403 }
404}
405