]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/motif/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 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #include "wx/menu.h" | |
24 | #include "wx/menuitem.h" | |
25 | #include "wx/utils.h" | |
26 | #include "wx/frame.h" | |
27 | ||
28 | #ifdef __VMS__ | |
29 | #pragma message disable nosimpint | |
30 | #endif | |
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> | |
40 | #ifdef __VMS__ | |
41 | #pragma message enable nosimpint | |
42 | #endif | |
43 | ||
44 | #include "wx/motif/private.h" | |
45 | ||
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); | |
53 | ||
54 | // ============================================================================ | |
55 | // implementation | |
56 | // ============================================================================ | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // dynamic classes implementation | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) | |
63 | ||
64 | // ---------------------------------------------------------------------------- | |
65 | // wxMenuItem | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
68 | // ctor & dtor | |
69 | // ----------- | |
70 | ||
71 | wxMenuItem::wxMenuItem(wxMenu *pParentMenu, | |
72 | int id, | |
73 | const wxString& strName, | |
74 | const wxString& strHelp, | |
75 | wxItemKind kind, | |
76 | wxMenu *pSubMenu) | |
77 | : wxMenuItemBase(pParentMenu, id, strName, strHelp, kind, pSubMenu) | |
78 | { | |
79 | // Motif-specific | |
80 | m_menuBar = NULL; | |
81 | m_buttonWidget = (WXWidget) NULL; | |
82 | m_topMenu = NULL; | |
83 | } | |
84 | ||
85 | wxMenuItem::~wxMenuItem() | |
86 | { | |
87 | } | |
88 | ||
89 | // change item state | |
90 | // ----------------- | |
91 | ||
92 | void wxMenuItem::Enable(bool bDoEnable) | |
93 | { | |
94 | if ( m_isEnabled != bDoEnable ) | |
95 | { | |
96 | if ( !IsSubMenu() ) | |
97 | { | |
98 | // normal menu item | |
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 | } | |
109 | ||
110 | wxMenuItemBase::Enable(bDoEnable); | |
111 | } | |
112 | } | |
113 | ||
114 | void wxMenuItem::Check(bool bDoCheck) | |
115 | { | |
116 | wxCHECK_RET( IsCheckable(), "only checkable items may be checked" ); | |
117 | ||
118 | if ( m_isChecked != bDoCheck ) | |
119 | { | |
120 | if ( m_buttonWidget ) | |
121 | { | |
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); | |
129 | } | |
130 | ||
131 | wxMenuItemBase::Check(bDoCheck); | |
132 | } | |
133 | } | |
134 | ||
135 | /* static */ | |
136 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) | |
137 | { | |
138 | return wxStripMenuCodes(text); | |
139 | } | |
140 | ||
141 | // ---------------------------------------------------------------------------- | |
142 | // wxMenuItemBase | |
143 | // ---------------------------------------------------------------------------- | |
144 | ||
145 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
146 | int id, | |
147 | const wxString& name, | |
148 | const wxString& help, | |
149 | wxItemKind kind, | |
150 | wxMenu *subMenu) | |
151 | { | |
152 | return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); | |
153 | } | |
154 | ||
155 | // ---------------------------------------------------------------------------- | |
156 | // Motif-specific | |
157 | // ---------------------------------------------------------------------------- | |
158 | ||
159 | void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, | |
160 | wxMenu * topMenu, size_t index) | |
161 | { | |
162 | m_menuBar = menuBar; | |
163 | m_topMenu = topMenu; | |
164 | ||
165 | if (GetId() == -3) | |
166 | { | |
167 | // Id=-3 identifies a Title item. | |
168 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget | |
169 | (wxStripMenuCodes(m_text), | |
170 | xmLabelGadgetClass, (Widget) menu, NULL); | |
171 | } | |
172 | else if (!m_text.empty() && !m_subMenu) | |
173 | { | |
174 | wxString strName = wxStripMenuCodes(m_text); | |
175 | if (IsCheckable()) | |
176 | { | |
177 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName, | |
178 | xmToggleButtonGadgetClass, (Widget) menu, | |
179 | #ifdef XmNpositionIndex | |
180 | XmNpositionIndex, index, | |
181 | #endif | |
182 | NULL); | |
183 | XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL); | |
184 | } | |
185 | else | |
186 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName, | |
187 | xmPushButtonGadgetClass, (Widget) menu, | |
188 | #ifdef XmNpositionIndex | |
189 | XmNpositionIndex, index, | |
190 | #endif | |
191 | NULL); | |
192 | char mnem = wxFindMnemonic (m_text); | |
193 | if (mnem != 0) | |
194 | XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL); | |
195 | ||
196 | //// TODO: proper accelerator treatment. What does wxFindAccelerator | |
197 | //// look for? | |
198 | strName = m_text; | |
199 | char *accel = wxFindAccelerator (strName); | |
200 | if (accel) | |
201 | XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL); | |
202 | ||
203 | // TODO: What does this do? | |
204 | XmString accel_str = wxFindAcceleratorText (strName); | |
205 | if (accel_str) | |
206 | { | |
207 | XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL); | |
208 | XmStringFree (accel_str); | |
209 | } | |
210 | ||
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); | |
229 | } | |
230 | else if (GetId() == wxID_SEPARATOR) | |
231 | { | |
232 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator", | |
233 | xmSeparatorGadgetClass, (Widget) menu, | |
234 | #ifndef XmNpositionIndex | |
235 | XmNpositionIndex, index, | |
236 | #endif | |
237 | NULL); | |
238 | } | |
239 | else if (m_subMenu) | |
240 | { | |
241 | m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, index, m_text, true); | |
242 | m_subMenu->SetButtonWidget(m_buttonWidget); | |
243 | XtAddCallback ((Widget) m_buttonWidget, | |
244 | XmNcascadingCallback, | |
245 | (XtCallbackProc) wxMenuItemArmCallback, | |
246 | (XtPointer) this); | |
247 | } | |
248 | if (m_buttonWidget) | |
249 | XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled()); | |
250 | } | |
251 | ||
252 | void wxMenuItem::DestroyItem(bool full) | |
253 | { | |
254 | if (GetId() == -3) | |
255 | { | |
256 | ; // Nothing | |
257 | ||
258 | } | |
259 | else if (!m_text.empty() && !m_subMenu) | |
260 | { | |
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 | } | |
274 | } | |
275 | else if (GetId() == wxID_SEPARATOR) | |
276 | { | |
277 | ; // Nothing | |
278 | ||
279 | } | |
280 | else if (GetSubMenu()) | |
281 | { | |
282 | if (m_buttonWidget) | |
283 | { | |
284 | XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback, | |
285 | wxMenuItemArmCallback, (XtPointer) this); | |
286 | } | |
287 | m_subMenu->DestroyMenu(full); | |
288 | if (full) | |
289 | m_buttonWidget = NULL; | |
290 | } | |
291 | ||
292 | if (m_buttonWidget && full) | |
293 | { | |
294 | XtDestroyWidget ((Widget) m_buttonWidget); | |
295 | m_buttonWidget = (WXWidget) 0; | |
296 | } | |
297 | } | |
298 | ||
299 | void wxMenuItem::SetText(const wxString& label) | |
300 | { | |
301 | char mnem = wxFindMnemonic (label); | |
302 | wxString label2 = wxStripMenuCodes(label); | |
303 | ||
304 | m_text = label; | |
305 | ||
306 | if (m_buttonWidget) | |
307 | { | |
308 | wxXmString label_str(label2); | |
309 | XtVaSetValues ((Widget) m_buttonWidget, | |
310 | XmNlabelString, label_str(), | |
311 | NULL); | |
312 | if (mnem != 0) | |
313 | XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL); | |
314 | char *accel = wxFindAccelerator (label2); | |
315 | if (accel) | |
316 | XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL); | |
317 | ||
318 | XmString accel_str = wxFindAcceleratorText (label2); | |
319 | if (accel_str) | |
320 | { | |
321 | XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL); | |
322 | XmStringFree (accel_str); | |
323 | } | |
324 | } | |
325 | } | |
326 | ||
327 | // ---------------------------------------------------------------------------- | |
328 | // Motif callbacks | |
329 | // ---------------------------------------------------------------------------- | |
330 | ||
331 | void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData, | |
332 | XtPointer WXUNUSED(ptr)) | |
333 | { | |
334 | wxMenuItem *item = (wxMenuItem *) clientData; | |
335 | if (item) | |
336 | { | |
337 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, item->GetId()); | |
338 | event.SetInt( item->GetId() ); | |
339 | ||
340 | if (item->IsCheckable()) | |
341 | { | |
342 | Boolean isChecked = false; | |
343 | XtVaGetValues ((Widget) item->GetButtonWidget(), | |
344 | XmNset, & isChecked, | |
345 | NULL); | |
346 | ||
347 | // only set the flag, don't actually check anything | |
348 | item->wxMenuItemBase::Check(isChecked); | |
349 | event.SetInt(isChecked); | |
350 | } | |
351 | ||
352 | if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame()) | |
353 | { | |
354 | event.SetEventObject(item->GetMenuBar()->GetMenuBarFrame()); | |
355 | ||
356 | item->GetMenuBar()->GetMenuBarFrame() | |
357 | ->GetEventHandler()->ProcessEvent(event); | |
358 | } | |
359 | // this is the child of a popup menu | |
360 | else if (item->GetTopMenu()) | |
361 | { | |
362 | event.SetEventObject(item->GetTopMenu()); | |
363 | ||
364 | item->GetTopMenu()->ProcessCommand (event); | |
365 | ||
366 | // Since PopupMenu under Motif still grab right mouse | |
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(); | |
373 | } | |
374 | } | |
375 | } | |
376 | ||
377 | void wxMenuItemArmCallback (Widget WXUNUSED(w), XtPointer clientData, | |
378 | XtPointer WXUNUSED(ptr)) | |
379 | { | |
380 | wxMenuItem *item = (wxMenuItem *) clientData; | |
381 | if (item) | |
382 | { | |
383 | if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame()) | |
384 | { | |
385 | wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId()); | |
386 | menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame()); | |
387 | ||
388 | item->GetMenuBar()->GetMenuBarFrame() | |
389 | ->GetEventHandler()->ProcessEvent(menuEvent); | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
394 | void | |
395 | wxMenuItemDisarmCallback (Widget WXUNUSED(w), XtPointer clientData, | |
396 | XtPointer WXUNUSED(ptr)) | |
397 | { | |
398 | wxMenuItem *item = (wxMenuItem *) clientData; | |
399 | if (item) | |
400 | { | |
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()); | |
407 | ||
408 | item->GetMenuBar()->GetMenuBarFrame() | |
409 | ->GetEventHandler()->ProcessEvent(menuEvent); | |
410 | } | |
411 | } | |
412 | } |