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