]>
Commit | Line | Data |
---|---|---|
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 | ||
45 | static void wxMenuItemCallback(Widget w, XtPointer clientData, XtPointer ptr); | |
46 | static void wxMenuItemArmCallback(Widget w, XtPointer clientData, XtPointer ptr); | |
47 | static 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 | ||
68 | wxMenuItem::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 | 91 | wxMenuItem::~wxMenuItem() |
4bb6408c JS |
92 | { |
93 | } | |
94 | ||
95 | // misc | |
96 | // ---- | |
97 | ||
98 | // delete the sub menu | |
99 | void 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 | ||
110 | void 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 | ||
132 | void 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 | ||
153 | //// Motif-specific | |
154 | ||
155 | void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu) | |
156 | { | |
2d120f83 JS |
157 | m_menuBar = menuBar; |
158 | m_topMenu = topMenu; | |
31528cd3 | 159 | |
2d120f83 | 160 | if (GetId() == -2) |
50414e24 | 161 | { |
2d120f83 | 162 | // Id=-2 identifies a Title item. |
31528cd3 | 163 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget |
9874b4ee | 164 | (wxStripMenuCodes(m_text), |
2d120f83 | 165 | xmLabelGadgetClass, (Widget) menu, NULL); |
50414e24 | 166 | } |
9874b4ee | 167 | else if ((!m_text.IsNull() && m_text != "") && (!m_subMenu)) |
50414e24 | 168 | { |
9874b4ee | 169 | wxString strName = wxStripMenuCodes(m_text); |
2d120f83 JS |
170 | if (IsCheckable()) |
171 | { | |
31528cd3 | 172 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName, |
2d120f83 JS |
173 | xmToggleButtonGadgetClass, (Widget) menu, |
174 | NULL); | |
175 | XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL); | |
176 | } | |
177 | else | |
31528cd3 | 178 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName, |
2d120f83 JS |
179 | xmPushButtonGadgetClass, (Widget) menu, |
180 | NULL); | |
9874b4ee | 181 | char mnem = wxFindMnemonic (m_text); |
2d120f83 JS |
182 | if (mnem != 0) |
183 | XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL); | |
31528cd3 | 184 | |
2d120f83 JS |
185 | //// TODO: proper accelerator treatment. What does wxFindAccelerator |
186 | //// look for? | |
9874b4ee | 187 | strName = m_text; |
31528cd3 | 188 | char *accel = wxFindAccelerator (strName); |
2d120f83 JS |
189 | if (accel) |
190 | XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL); | |
31528cd3 | 191 | |
2d120f83 | 192 | // TODO: What does this do? |
31528cd3 | 193 | XmString accel_str = wxFindAcceleratorText (strName); |
2d120f83 JS |
194 | if (accel_str) |
195 | { | |
196 | XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL); | |
197 | XmStringFree (accel_str); | |
198 | } | |
31528cd3 | 199 | |
2d120f83 JS |
200 | if (IsCheckable()) |
201 | XtAddCallback ((Widget) m_buttonWidget, | |
202 | XmNvalueChangedCallback, | |
203 | (XtCallbackProc) wxMenuItemCallback, | |
204 | (XtPointer) this); | |
205 | else | |
206 | XtAddCallback ((Widget) m_buttonWidget, | |
207 | XmNactivateCallback, | |
208 | (XtCallbackProc) wxMenuItemCallback, | |
209 | (XtPointer) this); | |
210 | XtAddCallback ((Widget) m_buttonWidget, | |
211 | XmNarmCallback, | |
212 | (XtCallbackProc) wxMenuItemArmCallback, | |
213 | (XtPointer) this); | |
214 | XtAddCallback ((Widget) m_buttonWidget, | |
215 | XmNdisarmCallback, | |
216 | (XtCallbackProc) wxMenuItemDisarmCallback, | |
217 | (XtPointer) this); | |
50414e24 | 218 | } |
2d120f83 | 219 | else if (GetId() == -1) |
50414e24 | 220 | { |
2d120f83 JS |
221 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator", |
222 | xmSeparatorGadgetClass, (Widget) menu, NULL); | |
50414e24 | 223 | } |
9874b4ee | 224 | else if (m_subMenu) |
50414e24 | 225 | { |
9874b4ee VZ |
226 | m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, m_text, TRUE); |
227 | m_subMenu->SetButtonWidget(m_buttonWidget); | |
2d120f83 JS |
228 | XtAddCallback ((Widget) m_buttonWidget, |
229 | XmNcascadingCallback, | |
230 | (XtCallbackProc) wxMenuItemArmCallback, | |
231 | (XtPointer) this); | |
50414e24 | 232 | } |
2d120f83 JS |
233 | if (m_buttonWidget) |
234 | XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled()); | |
50414e24 JS |
235 | } |
236 | ||
237 | void wxMenuItem::DestroyItem(bool full) | |
238 | { | |
2d120f83 | 239 | if (GetId() == -2) |
50414e24 | 240 | { |
9874b4ee | 241 | ; // Nothing |
31528cd3 | 242 | |
50414e24 | 243 | } |
9874b4ee | 244 | else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu) |
50414e24 | 245 | { |
2d120f83 JS |
246 | if (m_buttonWidget) |
247 | { | |
248 | if (IsCheckable()) | |
249 | XtRemoveCallback ((Widget) m_buttonWidget, XmNvalueChangedCallback, | |
250 | wxMenuItemCallback, (XtPointer) this); | |
251 | else | |
252 | XtRemoveCallback ((Widget) m_buttonWidget, XmNactivateCallback, | |
253 | wxMenuItemCallback, (XtPointer) this); | |
254 | XtRemoveCallback ((Widget) m_buttonWidget, XmNarmCallback, | |
255 | wxMenuItemArmCallback, (XtPointer) this); | |
256 | XtRemoveCallback ((Widget) m_buttonWidget, XmNdisarmCallback, | |
257 | wxMenuItemDisarmCallback, (XtPointer) this); | |
258 | } | |
50414e24 | 259 | } |
2d120f83 | 260 | else if (GetId() == -1) |
50414e24 | 261 | { |
9874b4ee | 262 | ; // Nothing |
31528cd3 | 263 | |
50414e24 | 264 | } |
2d120f83 | 265 | else if (GetSubMenu()) |
50414e24 | 266 | { |
2d120f83 JS |
267 | if (m_buttonWidget) |
268 | { | |
269 | XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback, | |
270 | wxMenuItemArmCallback, (XtPointer) this); | |
271 | } | |
9874b4ee | 272 | m_subMenu->DestroyMenu(full); |
2d120f83 JS |
273 | if (full) |
274 | m_buttonWidget = NULL; | |
50414e24 | 275 | } |
31528cd3 | 276 | |
2d120f83 | 277 | if (m_buttonWidget && full) |
50414e24 | 278 | { |
2d120f83 JS |
279 | XtDestroyWidget ((Widget) m_buttonWidget); |
280 | m_buttonWidget = (WXWidget) 0; | |
50414e24 JS |
281 | } |
282 | } | |
283 | ||
9874b4ee | 284 | void wxMenuItem::SetText(const wxString& label) |
50414e24 | 285 | { |
2d120f83 | 286 | char mnem = wxFindMnemonic (label); |
31528cd3 VZ |
287 | wxString label2 = wxStripMenuCodes(label); |
288 | ||
9874b4ee | 289 | m_text = label; |
31528cd3 | 290 | |
2d120f83 | 291 | if (m_buttonWidget) |
50414e24 | 292 | { |
31528cd3 | 293 | wxXmString label_str(label2); |
2d120f83 JS |
294 | XtVaSetValues ((Widget) m_buttonWidget, |
295 | XmNlabelString, label_str, | |
296 | NULL); | |
2d120f83 JS |
297 | if (mnem != 0) |
298 | XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL); | |
31528cd3 | 299 | char *accel = wxFindAccelerator (label2); |
2d120f83 JS |
300 | if (accel) |
301 | XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL); | |
31528cd3 VZ |
302 | |
303 | XmString accel_str = wxFindAcceleratorText (label2); | |
2d120f83 | 304 | if (accel_str) |
50414e24 | 305 | { |
2d120f83 JS |
306 | XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL); |
307 | XmStringFree (accel_str); | |
50414e24 JS |
308 | } |
309 | } | |
310 | } | |
311 | ||
9874b4ee VZ |
312 | // ---------------------------------------------------------------------------- |
313 | // Motif callbacks | |
314 | // ---------------------------------------------------------------------------- | |
315 | ||
af111fc3 JS |
316 | void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData, |
317 | XtPointer WXUNUSED(ptr)) | |
50414e24 | 318 | { |
2d120f83 JS |
319 | wxMenuItem *item = (wxMenuItem *) clientData; |
320 | if (item) | |
50414e24 | 321 | { |
2d120f83 JS |
322 | if (item->IsCheckable()) |
323 | { | |
324 | Boolean isChecked = FALSE; | |
325 | XtVaGetValues ((Widget) item->GetButtonWidget(), XmNset, & isChecked, NULL); | |
9874b4ee VZ |
326 | |
327 | // only set the flag, don't actually check anything | |
328 | item->wxMenuItemBase::Check(isChecked); | |
2d120f83 JS |
329 | } |
330 | if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame()) | |
331 | { | |
332 | wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, item->GetId()); | |
333 | commandEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame()); | |
334 | commandEvent.SetInt( item->GetId() ); | |
31528cd3 | 335 | |
2d120f83 JS |
336 | item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(commandEvent); |
337 | } | |
338 | else if (item->GetTopMenu()) | |
339 | { | |
340 | wxCommandEvent event (wxEVT_COMMAND_MENU_SELECTED, item->GetId()); | |
341 | event.SetEventObject(item->GetTopMenu()); | |
342 | event.SetInt( item->GetId() ); | |
31528cd3 | 343 | |
2d120f83 JS |
344 | item->GetTopMenu()->ProcessCommand (event); |
345 | } | |
50414e24 JS |
346 | } |
347 | } | |
348 | ||
af111fc3 JS |
349 | void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData, |
350 | XtPointer WXUNUSED(ptr)) | |
50414e24 | 351 | { |
2d120f83 JS |
352 | wxMenuItem *item = (wxMenuItem *) clientData; |
353 | if (item) | |
50414e24 | 354 | { |
2d120f83 JS |
355 | if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame()) |
356 | { | |
357 | wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId()); | |
358 | menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame()); | |
31528cd3 | 359 | |
2d120f83 JS |
360 | item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent); |
361 | } | |
50414e24 JS |
362 | } |
363 | } | |
364 | ||
31528cd3 | 365 | void |
af111fc3 JS |
366 | wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData, |
367 | XtPointer WXUNUSED(ptr)) | |
50414e24 | 368 | { |
2d120f83 JS |
369 | wxMenuItem *item = (wxMenuItem *) clientData; |
370 | if (item) | |
50414e24 | 371 | { |
2d120f83 JS |
372 | if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame()) |
373 | { | |
374 | // TODO: not sure this is correct, since -1 means something | |
375 | // special to event system | |
376 | wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, -1); | |
377 | menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame()); | |
31528cd3 | 378 | |
2d120f83 JS |
379 | item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent); |
380 | } | |
50414e24 JS |
381 | } |
382 | } | |
383 |