]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: radiobox.cpp | |
3 | // Purpose: wxRadioBox | |
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 | #ifdef __GNUG__ | |
13 | #pragma implementation "radiobox.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/radiobox.h" | |
17 | #include "wx/utils.h" | |
18 | ||
19 | #ifdef __VMS__ | |
20 | #pragma message disable nosimpint | |
21 | #endif | |
22 | #include <Xm/Label.h> | |
23 | #include <Xm/LabelG.h> | |
24 | #include <Xm/ToggleB.h> | |
25 | #include <Xm/ToggleBG.h> | |
26 | #include <Xm/RowColumn.h> | |
27 | #include <Xm/Form.h> | |
28 | #include <Xm/Frame.h> | |
29 | #ifdef __VMS__ | |
30 | #pragma message enable nosimpint | |
31 | #endif | |
32 | ||
33 | #include "wx/motif/private.h" | |
34 | ||
35 | void wxRadioBoxCallback (Widget w, XtPointer clientData, | |
36 | XmToggleButtonCallbackStruct * cbs); | |
37 | ||
38 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) | |
39 | ||
40 | // Radio box item | |
41 | wxRadioBox::wxRadioBox() | |
42 | { | |
43 | m_selectedButton = -1; | |
44 | m_noItems = 0; | |
45 | m_noRowsOrCols = 0; | |
46 | m_majorDim = 0 ; | |
47 | ||
48 | m_formWidget = (WXWidget) 0; | |
49 | m_frameWidget = (WXWidget) 0; | |
50 | m_labelWidget = (WXWidget) 0; | |
51 | m_radioButtons = (WXWidget*) NULL; | |
52 | m_radioButtonLabels = (wxString*) NULL; | |
53 | } | |
54 | ||
55 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title, | |
56 | const wxPoint& pos, const wxSize& size, | |
57 | int n, const wxString choices[], | |
58 | int majorDim, long style, | |
59 | const wxValidator& val, const wxString& name) | |
60 | { | |
61 | m_selectedButton = -1; | |
62 | m_noItems = n; | |
63 | m_formWidget = (WXWidget) 0; | |
64 | m_frameWidget = (WXWidget) 0; | |
65 | m_labelWidget = (WXWidget) 0; | |
66 | m_radioButtons = (WXWidget*) NULL; | |
67 | m_radioButtonLabels = (wxString*) NULL; | |
68 | m_backgroundColour = parent->GetBackgroundColour(); | |
69 | m_foregroundColour = parent->GetForegroundColour(); | |
70 | m_font = parent->GetFont(); | |
71 | ||
72 | SetName(name); | |
73 | SetValidator(val); | |
74 | ||
75 | parent->AddChild(this); | |
76 | ||
77 | m_windowStyle = (long&)style; | |
78 | ||
79 | if (id == -1) | |
80 | m_windowId = NewControlId(); | |
81 | else | |
82 | m_windowId = id; | |
83 | ||
84 | m_noRowsOrCols = majorDim; | |
85 | ||
86 | if (majorDim==0) | |
87 | m_majorDim = n ; | |
88 | else | |
89 | m_majorDim = majorDim ; | |
90 | ||
91 | Widget parentWidget = (Widget) parent->GetClientWidget(); | |
92 | ||
93 | wxString label1(wxStripMenuCodes(title)); | |
94 | ||
95 | Widget formWidget = XtVaCreateManagedWidget (name.c_str(), | |
96 | xmFormWidgetClass, parentWidget, | |
97 | XmNmarginHeight, 0, | |
98 | XmNmarginWidth, 0, | |
99 | NULL); | |
100 | ||
101 | m_formWidget = (WXWidget) formWidget; | |
102 | ||
103 | XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget)); | |
104 | if (label1 != "") | |
105 | { | |
106 | wxXmString text(label1); | |
107 | (void)XtVaCreateManagedWidget(label1.c_str(), | |
108 | #if wxUSE_GADGETS | |
109 | style & wxCOLOURED ? xmLabelWidgetClass | |
110 | : xmLabelGadgetClass, | |
111 | formWidget, | |
112 | #else | |
113 | xmLabelWidgetClass, formWidget, | |
114 | #endif | |
115 | XmNfontList, fontList, | |
116 | XmNlabelString, text(), | |
117 | NULL); | |
118 | } | |
119 | ||
120 | Widget frameWidget = XtVaCreateManagedWidget ("frame", | |
121 | xmFrameWidgetClass, formWidget, | |
122 | XmNshadowType, XmSHADOW_IN, | |
123 | // XmNmarginHeight, 0, | |
124 | // XmNmarginWidth, 0, | |
125 | NULL); | |
126 | ||
127 | m_frameWidget = (WXWidget) frameWidget; | |
128 | ||
129 | Arg args[3]; | |
130 | ||
131 | m_majorDim = (n + m_majorDim - 1) / m_majorDim; | |
132 | ||
133 | XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ? | |
134 | XmHORIZONTAL : XmVERTICAL)); | |
135 | XtSetArg (args[1], XmNnumColumns, m_majorDim); | |
136 | ||
137 | Widget radioBoxWidget = XmCreateRadioBox (frameWidget, "radioBoxWidget", args, 2); | |
138 | m_mainWidget = (WXWidget) radioBoxWidget; | |
139 | ||
140 | ||
141 | if (m_labelWidget) | |
142 | XtVaSetValues ((Widget) m_labelWidget, | |
143 | XmNtopAttachment, XmATTACH_FORM, | |
144 | XmNleftAttachment, XmATTACH_FORM, | |
145 | XmNalignment, XmALIGNMENT_BEGINNING, | |
146 | NULL); | |
147 | ||
148 | XtVaSetValues (radioBoxWidget, | |
149 | XmNtopAttachment, m_labelWidget ? XmATTACH_WIDGET : XmATTACH_FORM, | |
150 | XmNtopWidget, m_labelWidget ? (Widget) m_labelWidget : formWidget, | |
151 | XmNbottomAttachment, XmATTACH_FORM, | |
152 | XmNleftAttachment, XmATTACH_FORM, | |
153 | XmNrightAttachment, XmATTACH_FORM, | |
154 | NULL); | |
155 | ||
156 | // if (style & wxFLAT) | |
157 | // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL); | |
158 | ||
159 | m_radioButtons = new WXWidget[n]; | |
160 | m_radioButtonLabels = new wxString[n]; | |
161 | int i; | |
162 | for (i = 0; i < n; i++) | |
163 | { | |
164 | wxString str(wxStripMenuCodes(choices[i])); | |
165 | m_radioButtonLabels[i] = str; | |
166 | m_radioButtons[i] = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) str, | |
167 | #if wxUSE_GADGETS | |
168 | xmToggleButtonGadgetClass, radioBoxWidget, | |
169 | #else | |
170 | xmToggleButtonWidgetClass, radioBoxWidget, | |
171 | #endif | |
172 | XmNfontList, fontList, | |
173 | NULL); | |
174 | #ifdef __VMS__ | |
175 | #pragma message disable voiincconext | |
176 | // VMS gives here the compiler warning: | |
177 | // conversion from pointer to function to void* permitted | |
178 | // as an extension | |
179 | #endif | |
180 | XtAddCallback ((Widget) m_radioButtons[i], XmNvalueChangedCallback, (XtCallbackProc) wxRadioBoxCallback, | |
181 | (XtCallbackProc) this); | |
182 | #ifdef __VMS__ | |
183 | #pragma message enable voiincconext | |
184 | #endif | |
185 | ||
186 | } | |
187 | SetSelection (0); | |
188 | ||
189 | m_font = parent->GetFont(); | |
190 | ChangeFont(FALSE); | |
191 | ||
192 | // XtManageChild((Widget) m_formWidget); | |
193 | XtManageChild (radioBoxWidget); | |
194 | ||
195 | SetCanAddEventHandler(TRUE); | |
196 | AttachWidget (parent, m_mainWidget, m_formWidget, pos.x, pos.y, size.x, size.y); | |
197 | ||
198 | ChangeBackgroundColour(); | |
199 | ||
200 | return TRUE; | |
201 | } | |
202 | ||
203 | ||
204 | wxRadioBox::~wxRadioBox() | |
205 | { | |
206 | delete[] m_radioButtonLabels; | |
207 | delete[] m_radioButtons; | |
208 | ||
209 | DetachWidget(m_formWidget); | |
210 | DetachWidget(m_mainWidget); | |
211 | ||
212 | if (m_labelWidget) | |
213 | XtDestroyWidget((Widget) m_labelWidget); | |
214 | XtDestroyWidget((Widget) m_mainWidget); | |
215 | XtDestroyWidget((Widget) m_formWidget); | |
216 | ||
217 | m_mainWidget = (WXWidget) 0; | |
218 | m_formWidget = (WXWidget) 0; | |
219 | m_labelWidget = (WXWidget) 0; | |
220 | } | |
221 | ||
222 | wxString wxRadioBox::GetLabel(int item) const | |
223 | { | |
224 | if (item < 0 || item >= m_noItems) | |
225 | return wxEmptyString; | |
226 | ||
227 | Widget widget = (Widget) m_radioButtons[item]; | |
228 | XmString text; | |
229 | char *s; | |
230 | XtVaGetValues (widget, | |
231 | XmNlabelString, &text, | |
232 | NULL); | |
233 | ||
234 | if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s)) | |
235 | { | |
236 | // Should we free 'text'??? | |
237 | XmStringFree(text); | |
238 | wxString str(s); | |
239 | XtFree (s); | |
240 | return str; | |
241 | } | |
242 | else | |
243 | { | |
244 | XmStringFree(text); | |
245 | return wxEmptyString; | |
246 | } | |
247 | } | |
248 | ||
249 | void wxRadioBox::SetLabel(int item, const wxString& label) | |
250 | { | |
251 | if (item < 0 || item >= m_noItems) | |
252 | return; | |
253 | ||
254 | Widget widget = (Widget) m_radioButtons[item]; | |
255 | if (label != "") | |
256 | { | |
257 | wxString label1(wxStripMenuCodes(label)); | |
258 | XmString text = XmStringCreateSimple ((char*) (const char*) label1); | |
259 | XtVaSetValues (widget, | |
260 | XmNlabelString, text, | |
261 | XmNlabelType, XmSTRING, | |
262 | NULL); | |
263 | XmStringFree (text); | |
264 | } | |
265 | } | |
266 | ||
267 | int wxRadioBox::FindString(const wxString& s) const | |
268 | { | |
269 | int i; | |
270 | for (i = 0; i < m_noItems; i++) | |
271 | if (s == m_radioButtonLabels[i]) | |
272 | return i; | |
273 | return -1; | |
274 | } | |
275 | ||
276 | void wxRadioBox::SetSelection(int n) | |
277 | { | |
278 | if ((n < 0) || (n >= m_noItems)) | |
279 | return; | |
280 | ||
281 | m_selectedButton = n; | |
282 | ||
283 | m_inSetValue = TRUE; | |
284 | ||
285 | XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE); | |
286 | ||
287 | int i; | |
288 | for (i = 0; i < m_noItems; i++) | |
289 | if (i != n) | |
290 | XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE); | |
291 | ||
292 | m_inSetValue = FALSE; | |
293 | } | |
294 | ||
295 | // Get single selection, for single choice list items | |
296 | int wxRadioBox::GetSelection() const | |
297 | { | |
298 | return m_selectedButton; | |
299 | } | |
300 | ||
301 | // Find string for position | |
302 | wxString wxRadioBox::GetString(int n) const | |
303 | { | |
304 | if ((n < 0) || (n >= m_noItems)) | |
305 | return wxEmptyString; | |
306 | return m_radioButtonLabels[n]; | |
307 | } | |
308 | ||
309 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
310 | { | |
311 | bool managed = XtIsManaged((Widget) m_formWidget); | |
312 | ||
313 | if (managed) | |
314 | XtUnmanageChild ((Widget) m_formWidget); | |
315 | ||
316 | int xx = x; int yy = y; | |
317 | AdjustForParentClientOrigin(xx, yy, sizeFlags); | |
318 | ||
319 | if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
320 | XtVaSetValues ((Widget) m_formWidget, XmNleftAttachment, XmATTACH_SELF, | |
321 | XmNx, xx, NULL); | |
322 | if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
323 | XtVaSetValues ((Widget) m_formWidget, XmNtopAttachment, XmATTACH_SELF, | |
324 | XmNy, yy, NULL); | |
325 | ||
326 | // Must set the actual RadioBox to be desired size MINUS label size | |
327 | Dimension labelWidth = 0, labelHeight = 0, actualWidth = 0, actualHeight = 0; | |
328 | ||
329 | if (m_labelWidget) | |
330 | XtVaGetValues ((Widget) m_labelWidget, XmNwidth, &labelWidth, XmNheight, &labelHeight, NULL); | |
331 | ||
332 | actualWidth = width; | |
333 | actualHeight = height - labelHeight; | |
334 | ||
335 | if (width > -1) | |
336 | { | |
337 | XtVaSetValues ((Widget) m_mainWidget, XmNwidth, actualWidth, NULL); | |
338 | } | |
339 | if (height > -1) | |
340 | { | |
341 | XtVaSetValues ((Widget) m_mainWidget, XmNheight, actualHeight, NULL); | |
342 | } | |
343 | if (managed) | |
344 | XtManageChild ((Widget) m_formWidget); | |
345 | } | |
346 | ||
347 | // Enable a specific button | |
348 | void wxRadioBox::Enable(int n, bool enable) | |
349 | { | |
350 | if ((n < 0) || (n >= m_noItems)) | |
351 | return; | |
352 | ||
353 | XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable); | |
354 | } | |
355 | ||
356 | // Enable all controls | |
357 | bool wxRadioBox::Enable(bool enable) | |
358 | { | |
359 | if ( !wxControl::Enable(enable) ) | |
360 | return FALSE; | |
361 | ||
362 | int i; | |
363 | for (i = 0; i < m_noItems; i++) | |
364 | XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable); | |
365 | ||
366 | return TRUE; | |
367 | } | |
368 | ||
369 | bool wxRadioBox::Show(bool show) | |
370 | { | |
371 | // TODO: show/hide all children | |
372 | return wxControl::Show(show); | |
373 | } | |
374 | ||
375 | // Show a specific button | |
376 | void wxRadioBox::Show(int n, bool show) | |
377 | { | |
378 | // This method isn't complete, and we try do do our best... | |
379 | // It's main purpose isn't for allowing Show/Unshow dynamically, | |
380 | // but rather to provide a way to design wxRadioBox such: | |
381 | // | |
382 | // o Val1 o Val2 o Val3 | |
383 | // o Val4 o Val6 | |
384 | // o Val7 o Val8 o Val9 | |
385 | // | |
386 | // In my case, this is a 'direction' box, and the Show(5,False) is | |
387 | // coupled with an Enable(5,False) | |
388 | // | |
389 | if ((n < 0) || (n >= m_noItems)) | |
390 | return; | |
391 | ||
392 | XtVaSetValues ((Widget) m_radioButtons[n], | |
393 | XmNindicatorOn, (unsigned char) show, | |
394 | NULL); | |
395 | ||
396 | // Please note that this is all we can do: removing the label | |
397 | // if switching to unshow state. However, when switching | |
398 | // to the on state, it's the prog. resp. to call SetLabel(item,...) | |
399 | // after this call!! | |
400 | if (!show) | |
401 | wxRadioBox::SetLabel (n, " "); | |
402 | } | |
403 | ||
404 | // For single selection items only | |
405 | wxString wxRadioBox::GetStringSelection () const | |
406 | { | |
407 | int sel = GetSelection (); | |
408 | if (sel > -1) | |
409 | return this->GetString (sel); | |
410 | else | |
411 | return wxString(""); | |
412 | } | |
413 | ||
414 | bool wxRadioBox::SetStringSelection (const wxString& s) | |
415 | { | |
416 | int sel = FindString (s); | |
417 | if (sel > -1) | |
418 | { | |
419 | SetSelection (sel); | |
420 | return TRUE; | |
421 | } | |
422 | else | |
423 | return FALSE; | |
424 | } | |
425 | ||
426 | void wxRadioBox::Command (wxCommandEvent & event) | |
427 | { | |
428 | SetSelection (event.m_commandInt); | |
429 | ProcessCommand (event); | |
430 | } | |
431 | ||
432 | void wxRadioBox::ChangeFont(bool keepOriginalSize) | |
433 | { | |
434 | wxWindow::ChangeFont(keepOriginalSize); | |
435 | ||
436 | XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) GetTopWidget())); | |
437 | ||
438 | int i; | |
439 | for (i = 0; i < m_noItems; i++) | |
440 | { | |
441 | WXWidget radioButton = m_radioButtons[i]; | |
442 | ||
443 | XtVaSetValues ((Widget) radioButton, | |
444 | XmNfontList, fontList, | |
445 | XmNtopAttachment, XmATTACH_FORM, | |
446 | NULL); | |
447 | } | |
448 | } | |
449 | ||
450 | void wxRadioBox::ChangeBackgroundColour() | |
451 | { | |
452 | wxWindow::ChangeBackgroundColour(); | |
453 | ||
454 | DoChangeBackgroundColour((Widget) m_frameWidget, m_backgroundColour); | |
455 | ||
456 | int selectPixel = wxBLACK->AllocColour(wxGetDisplay()); | |
457 | ||
458 | int i; | |
459 | for (i = 0; i < m_noItems; i++) | |
460 | { | |
461 | WXWidget radioButton = m_radioButtons[i]; | |
462 | ||
463 | DoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE); | |
464 | ||
465 | XtVaSetValues ((Widget) radioButton, | |
466 | XmNselectColor, selectPixel, | |
467 | NULL); | |
468 | } | |
469 | } | |
470 | ||
471 | void wxRadioBox::ChangeForegroundColour() | |
472 | { | |
473 | wxWindow::ChangeForegroundColour(); | |
474 | ||
475 | int i; | |
476 | for (i = 0; i < m_noItems; i++) | |
477 | { | |
478 | WXWidget radioButton = m_radioButtons[i]; | |
479 | ||
480 | DoChangeForegroundColour(radioButton, m_foregroundColour); | |
481 | } | |
482 | } | |
483 | ||
484 | void wxRadioBoxCallback (Widget w, XtPointer clientData, | |
485 | XmToggleButtonCallbackStruct * cbs) | |
486 | { | |
487 | if (!cbs->set) | |
488 | return; | |
489 | ||
490 | wxRadioBox *item = (wxRadioBox *) clientData; | |
491 | int sel = -1; | |
492 | int i; | |
493 | for (i = 0; i < item->Number(); i++) | |
494 | if (item->GetRadioButtons() && ((Widget) (item->GetRadioButtons()[i]) == w)) | |
495 | sel = i; | |
496 | item->SetSel(sel); | |
497 | ||
498 | if (item->InSetValue()) | |
499 | return; | |
500 | ||
501 | wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId()); | |
502 | event.m_commandInt = sel; | |
503 | event.SetEventObject(item); | |
504 | item->ProcessCommand (event); | |
505 | } | |
506 |