]>
Commit | Line | Data |
---|---|---|
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 | // headers & declarations | |
14 | // ============================================================================ | |
15 | ||
16 | #include "wx/menu.h" | |
17 | #include "wx/menuitem.h" | |
18 | #include "wx/utils.h" | |
19 | #include "wx/frame.h" | |
20 | ||
21 | #include <Xm/Label.h> | |
22 | #include <Xm/LabelG.h> | |
23 | #include <Xm/CascadeBG.h> | |
24 | #include <Xm/CascadeB.h> | |
25 | #include <Xm/SeparatoG.h> | |
26 | #include <Xm/PushBG.h> | |
27 | #include <Xm/ToggleB.h> | |
28 | #include <Xm/ToggleBG.h> | |
29 | #include <Xm/RowColumn.h> | |
30 | ||
31 | #include "wx/motif/private.h" | |
32 | ||
33 | void wxMenuItemCallback (Widget w, XtPointer clientData, | |
34 | XtPointer ptr); | |
35 | void wxMenuItemArmCallback (Widget w, XtPointer clientData, | |
36 | XtPointer ptr); | |
37 | void wxMenuItemDisarmCallback (Widget w, XtPointer clientData, | |
38 | XtPointer ptr); | |
39 | ||
40 | // ============================================================================ | |
41 | // implementation | |
42 | // ============================================================================ | |
43 | ||
44 | // ---------------------------------------------------------------------------- | |
45 | // dynamic classes implementation | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
48 | #if !USE_SHARED_LIBRARY | |
49 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) | |
50 | #endif //USE_SHARED_LIBRARY | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // wxMenuItem | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | // ctor & dtor | |
57 | // ----------- | |
58 | ||
59 | wxMenuItem::wxMenuItem(wxMenu *pParentMenu, int id, | |
60 | const wxString& strName, const wxString& strHelp, | |
61 | bool bCheckable, | |
62 | wxMenu *pSubMenu) : | |
63 | m_strHelp(strHelp), | |
64 | m_bCheckable(bCheckable), | |
65 | m_strName(strName) | |
66 | { | |
67 | wxASSERT( pParentMenu != NULL ); | |
68 | ||
69 | m_pParentMenu = pParentMenu; | |
70 | m_pSubMenu = pSubMenu; | |
71 | m_idItem = id; | |
72 | m_bEnabled = TRUE; | |
73 | m_bChecked = FALSE; | |
74 | ||
75 | //// Motif-specific | |
76 | m_menuBar = NULL; | |
77 | m_buttonWidget = (WXWidget) NULL; | |
78 | m_topMenu = NULL; | |
79 | } | |
80 | ||
81 | wxMenuItem::~wxMenuItem() | |
82 | { | |
83 | } | |
84 | ||
85 | // misc | |
86 | // ---- | |
87 | ||
88 | // delete the sub menu | |
89 | void wxMenuItem::DeleteSubMenu() | |
90 | { | |
91 | wxASSERT( m_pSubMenu != NULL ); | |
92 | ||
93 | delete m_pSubMenu; | |
94 | m_pSubMenu = NULL; | |
95 | } | |
96 | ||
97 | // change item state | |
98 | // ----------------- | |
99 | ||
100 | void wxMenuItem::Enable(bool bDoEnable) | |
101 | { | |
102 | if ( m_bEnabled != bDoEnable ) | |
103 | { | |
104 | if ( m_pSubMenu == NULL ) | |
105 | { // normal menu item | |
106 | if (m_buttonWidget) | |
107 | XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable); | |
108 | } | |
109 | else // submenu | |
110 | { | |
111 | // Maybe we should apply this to all items in the submenu? | |
112 | // Or perhaps it works anyway. | |
113 | if (m_buttonWidget) | |
114 | XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable); | |
115 | } | |
116 | ||
117 | m_bEnabled = bDoEnable; | |
118 | } | |
119 | } | |
120 | ||
121 | void wxMenuItem::Check(bool bDoCheck) | |
122 | { | |
123 | wxCHECK_RET( IsCheckable(), "only checkable items may be checked" ); | |
124 | ||
125 | if ( m_bChecked != bDoCheck ) | |
126 | { | |
127 | if (m_buttonWidget && XtIsSubclass ((Widget) m_buttonWidget, xmToggleButtonGadgetClass)) | |
128 | { | |
129 | XtVaSetValues ( (Widget) m_buttonWidget, XmNset, (Boolean) bDoCheck, NULL); | |
130 | } | |
131 | m_bChecked = bDoCheck; | |
132 | } | |
133 | } | |
134 | ||
135 | //// Motif-specific | |
136 | ||
137 | void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu) | |
138 | { | |
139 | m_menuBar = menuBar; | |
140 | m_topMenu = topMenu; | |
141 | ||
142 | if (GetId() == -2) | |
143 | { | |
144 | // Id=-2 identifies a Title item. | |
145 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget | |
146 | (wxStripMenuCodes(m_strName), | |
147 | xmLabelGadgetClass, (Widget) menu, NULL); | |
148 | } | |
149 | else if ((!m_strName.IsNull() && m_strName != "") && (!m_pSubMenu)) | |
150 | { | |
151 | wxString strName = wxStripMenuCodes(m_strName); | |
152 | if (IsCheckable()) | |
153 | { | |
154 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName, | |
155 | xmToggleButtonGadgetClass, (Widget) menu, | |
156 | NULL); | |
157 | XtVaSetValues ((Widget) m_buttonWidget, XmNset, (Boolean) IsChecked(), NULL); | |
158 | } | |
159 | else | |
160 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName, | |
161 | xmPushButtonGadgetClass, (Widget) menu, | |
162 | NULL); | |
163 | char mnem = wxFindMnemonic (m_strName); | |
164 | if (mnem != 0) | |
165 | XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL); | |
166 | ||
167 | //// TODO: proper accelerator treatment. What does wxFindAccelerator | |
168 | //// look for? | |
169 | strName = m_strName; | |
170 | char *accel = wxFindAccelerator (strName); | |
171 | if (accel) | |
172 | XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL); | |
173 | ||
174 | // TODO: What does this do? | |
175 | XmString accel_str = wxFindAcceleratorText (strName); | |
176 | if (accel_str) | |
177 | { | |
178 | XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL); | |
179 | XmStringFree (accel_str); | |
180 | } | |
181 | ||
182 | if (IsCheckable()) | |
183 | XtAddCallback ((Widget) m_buttonWidget, | |
184 | XmNvalueChangedCallback, | |
185 | (XtCallbackProc) wxMenuItemCallback, | |
186 | (XtPointer) this); | |
187 | else | |
188 | XtAddCallback ((Widget) m_buttonWidget, | |
189 | XmNactivateCallback, | |
190 | (XtCallbackProc) wxMenuItemCallback, | |
191 | (XtPointer) this); | |
192 | XtAddCallback ((Widget) m_buttonWidget, | |
193 | XmNarmCallback, | |
194 | (XtCallbackProc) wxMenuItemArmCallback, | |
195 | (XtPointer) this); | |
196 | XtAddCallback ((Widget) m_buttonWidget, | |
197 | XmNdisarmCallback, | |
198 | (XtCallbackProc) wxMenuItemDisarmCallback, | |
199 | (XtPointer) this); | |
200 | } | |
201 | else if (GetId() == -1) | |
202 | { | |
203 | m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator", | |
204 | xmSeparatorGadgetClass, (Widget) menu, NULL); | |
205 | } | |
206 | else if (m_pSubMenu) | |
207 | { | |
208 | m_buttonWidget = m_pSubMenu->CreateMenu (menuBar, menu, topMenu, m_strName, TRUE); | |
209 | m_pSubMenu->SetButtonWidget(m_buttonWidget); | |
210 | XtAddCallback ((Widget) m_buttonWidget, | |
211 | XmNcascadingCallback, | |
212 | (XtCallbackProc) wxMenuItemArmCallback, | |
213 | (XtPointer) this); | |
214 | } | |
215 | if (m_buttonWidget) | |
216 | XtSetSensitive ((Widget) m_buttonWidget, (Boolean) IsEnabled()); | |
217 | } | |
218 | ||
219 | void wxMenuItem::DestroyItem(bool full) | |
220 | { | |
221 | if (GetId() == -2) | |
222 | { | |
223 | ; // Nothing | |
224 | ||
225 | } | |
226 | else if ((!m_strName.IsNull() && (m_strName != "")) && !m_pSubMenu) | |
227 | { | |
228 | if (m_buttonWidget) | |
229 | { | |
230 | if (IsCheckable()) | |
231 | XtRemoveCallback ((Widget) m_buttonWidget, XmNvalueChangedCallback, | |
232 | wxMenuItemCallback, (XtPointer) this); | |
233 | else | |
234 | XtRemoveCallback ((Widget) m_buttonWidget, XmNactivateCallback, | |
235 | wxMenuItemCallback, (XtPointer) this); | |
236 | XtRemoveCallback ((Widget) m_buttonWidget, XmNarmCallback, | |
237 | wxMenuItemArmCallback, (XtPointer) this); | |
238 | XtRemoveCallback ((Widget) m_buttonWidget, XmNdisarmCallback, | |
239 | wxMenuItemDisarmCallback, (XtPointer) this); | |
240 | } | |
241 | } | |
242 | else if (GetId() == -1) | |
243 | { | |
244 | ; // Nothing | |
245 | ||
246 | } | |
247 | else if (GetSubMenu()) | |
248 | { | |
249 | if (m_buttonWidget) | |
250 | { | |
251 | XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback, | |
252 | wxMenuItemArmCallback, (XtPointer) this); | |
253 | } | |
254 | m_pSubMenu->DestroyMenu(full); | |
255 | if (full) | |
256 | m_buttonWidget = NULL; | |
257 | } | |
258 | ||
259 | if (m_buttonWidget && full) | |
260 | { | |
261 | XtDestroyWidget ((Widget) m_buttonWidget); | |
262 | m_buttonWidget = (WXWidget) 0; | |
263 | } | |
264 | } | |
265 | ||
266 | void wxMenuItem::SetLabel(const wxString& label) | |
267 | { | |
268 | char mnem = wxFindMnemonic (label); | |
269 | wxString label2 = wxStripMenuCodes(label); | |
270 | ||
271 | m_strName = label; | |
272 | ||
273 | if (m_buttonWidget) | |
274 | { | |
275 | wxXmString label_str(label2); | |
276 | XtVaSetValues ((Widget) m_buttonWidget, | |
277 | XmNlabelString, label_str, | |
278 | NULL); | |
279 | if (mnem != 0) | |
280 | XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL); | |
281 | char *accel = wxFindAccelerator (label2); | |
282 | if (accel) | |
283 | XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL); | |
284 | ||
285 | XmString accel_str = wxFindAcceleratorText (label2); | |
286 | if (accel_str) | |
287 | { | |
288 | XtVaSetValues ((Widget) m_buttonWidget, XmNacceleratorText, accel_str, NULL); | |
289 | XmStringFree (accel_str); | |
290 | } | |
291 | } | |
292 | } | |
293 | ||
294 | void wxMenuItemCallback (Widget w, XtPointer clientData, | |
295 | XtPointer ptr) | |
296 | { | |
297 | wxMenuItem *item = (wxMenuItem *) clientData; | |
298 | if (item) | |
299 | { | |
300 | if (item->IsCheckable()) | |
301 | { | |
302 | Boolean isChecked = FALSE; | |
303 | XtVaGetValues ((Widget) item->GetButtonWidget(), XmNset, & isChecked, NULL); | |
304 | item->SetChecked(isChecked); | |
305 | } | |
306 | if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame()) | |
307 | { | |
308 | wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, item->GetId()); | |
309 | commandEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame()); | |
310 | commandEvent.SetInt( item->GetId() ); | |
311 | ||
312 | item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(commandEvent); | |
313 | } | |
314 | else if (item->GetTopMenu()) | |
315 | { | |
316 | wxCommandEvent event (wxEVT_COMMAND_MENU_SELECTED, item->GetId()); | |
317 | event.SetEventObject(item->GetTopMenu()); | |
318 | event.SetInt( item->GetId() ); | |
319 | ||
320 | item->GetTopMenu()->ProcessCommand (event); | |
321 | } | |
322 | } | |
323 | } | |
324 | ||
325 | void wxMenuItemArmCallback (Widget w, XtPointer clientData, | |
326 | XtPointer ptr) | |
327 | { | |
328 | wxMenuItem *item = (wxMenuItem *) clientData; | |
329 | if (item) | |
330 | { | |
331 | if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame()) | |
332 | { | |
333 | wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, item->GetId()); | |
334 | menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame()); | |
335 | ||
336 | item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent); | |
337 | } | |
338 | } | |
339 | } | |
340 | ||
341 | void | |
342 | wxMenuItemDisarmCallback (Widget w, XtPointer clientData, | |
343 | XtPointer ptr) | |
344 | { | |
345 | wxMenuItem *item = (wxMenuItem *) clientData; | |
346 | if (item) | |
347 | { | |
348 | if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame()) | |
349 | { | |
350 | // TODO: not sure this is correct, since -1 means something | |
351 | // special to event system | |
352 | wxMenuEvent menuEvent(wxEVT_MENU_HIGHLIGHT, -1); | |
353 | menuEvent.SetEventObject(item->GetMenuBar()->GetMenuBarFrame()); | |
354 | ||
355 | item->GetMenuBar()->GetMenuBarFrame()->GetEventHandler()->ProcessEvent(menuEvent); | |
356 | } | |
357 | } | |
358 | } | |
359 |