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