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