]>
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_windowFont = 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_windowFont.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_windowFont = 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 | void wxRadioBox::Enable(bool enable) | |
349 | { | |
350 | wxControl::Enable(enable); | |
351 | ||
352 | int i; | |
353 | for (i = 0; i < m_noItems; i++) | |
354 | XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable); | |
355 | } | |
356 | ||
357 | bool wxRadioBox::Show(bool show) | |
358 | { | |
359 | // TODO: show/hide all children | |
360 | return wxControl::Show(show); | |
361 | } | |
362 | ||
363 | // Show a specific button | |
364 | void wxRadioBox::Show(int n, bool show) | |
365 | { | |
366 | // This method isn't complete, and we try do do our best... | |
367 | // It's main purpose isn't for allowing Show/Unshow dynamically, | |
368 | // but rather to provide a way to design wxRadioBox such: | |
369 | // | |
370 | // o Val1 o Val2 o Val3 | |
371 | // o Val4 o Val6 | |
372 | // o Val7 o Val8 o Val9 | |
373 | // | |
374 | // In my case, this is a 'direction' box, and the Show(5,False) is | |
375 | // coupled with an Enable(5,False) | |
376 | // | |
377 | if ((n < 0) || (n >= m_noItems)) | |
378 | return; | |
379 | ||
380 | XtVaSetValues ((Widget) m_radioButtons[n], | |
381 | XmNindicatorOn, (unsigned char) show, | |
382 | NULL); | |
383 | ||
384 | // Please note that this is all we can do: removing the label | |
385 | // if switching to unshow state. However, when switching | |
386 | // to the on state, it's the prog. resp. to call SetLabel(item,...) | |
387 | // after this call!! | |
388 | if (!show) | |
389 | wxRadioBox::SetLabel (n, " "); | |
390 | } | |
391 | ||
392 | // For single selection items only | |
393 | wxString wxRadioBox::GetStringSelection () const | |
394 | { | |
395 | int sel = GetSelection (); | |
396 | if (sel > -1) | |
397 | return this->GetString (sel); | |
398 | else | |
399 | return wxString(""); | |
400 | } | |
401 | ||
402 | bool wxRadioBox::SetStringSelection (const wxString& s) | |
403 | { | |
404 | int sel = FindString (s); | |
405 | if (sel > -1) | |
406 | { | |
407 | SetSelection (sel); | |
408 | return TRUE; | |
409 | } | |
410 | else | |
411 | return FALSE; | |
412 | } | |
413 | ||
414 | void wxRadioBox::Command (wxCommandEvent & event) | |
415 | { | |
416 | SetSelection (event.m_commandInt); | |
417 | ProcessCommand (event); | |
418 | } | |
419 | ||
420 | void wxRadioBox::ChangeFont(bool keepOriginalSize) | |
421 | { | |
422 | wxWindow::ChangeFont(keepOriginalSize); | |
423 | ||
424 | XmFontList fontList = (XmFontList) m_windowFont.GetFontList(1.0, XtDisplay((Widget) GetTopWidget())); | |
425 | ||
426 | int i; | |
427 | for (i = 0; i < m_noItems; i++) | |
428 | { | |
429 | WXWidget radioButton = m_radioButtons[i]; | |
430 | ||
431 | XtVaSetValues ((Widget) radioButton, | |
432 | XmNfontList, fontList, | |
433 | XmNtopAttachment, XmATTACH_FORM, | |
434 | NULL); | |
435 | } | |
436 | } | |
437 | ||
438 | void wxRadioBox::ChangeBackgroundColour() | |
439 | { | |
440 | wxWindow::ChangeBackgroundColour(); | |
441 | ||
442 | DoChangeBackgroundColour((Widget) m_frameWidget, m_backgroundColour); | |
443 | ||
444 | int selectPixel = wxBLACK->AllocColour(wxGetDisplay()); | |
445 | ||
446 | int i; | |
447 | for (i = 0; i < m_noItems; i++) | |
448 | { | |
449 | WXWidget radioButton = m_radioButtons[i]; | |
450 | ||
451 | DoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE); | |
452 | ||
453 | XtVaSetValues ((Widget) radioButton, | |
454 | XmNselectColor, selectPixel, | |
455 | NULL); | |
456 | } | |
457 | } | |
458 | ||
459 | void wxRadioBox::ChangeForegroundColour() | |
460 | { | |
461 | wxWindow::ChangeForegroundColour(); | |
462 | ||
463 | int i; | |
464 | for (i = 0; i < m_noItems; i++) | |
465 | { | |
466 | WXWidget radioButton = m_radioButtons[i]; | |
467 | ||
468 | DoChangeForegroundColour(radioButton, m_foregroundColour); | |
469 | } | |
470 | } | |
471 | ||
472 | void wxRadioBoxCallback (Widget w, XtPointer clientData, | |
473 | XmToggleButtonCallbackStruct * cbs) | |
474 | { | |
475 | if (!cbs->set) | |
476 | return; | |
477 | ||
478 | wxRadioBox *item = (wxRadioBox *) clientData; | |
479 | int sel = -1; | |
480 | int i; | |
481 | for (i = 0; i < item->Number(); i++) | |
482 | if (item->GetRadioButtons() && ((Widget) (item->GetRadioButtons()[i]) == w)) | |
483 | sel = i; | |
484 | item->SetSel(sel); | |
485 | ||
486 | if (item->InSetValue()) | |
487 | return; | |
488 | ||
489 | wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId()); | |
490 | event.m_commandInt = sel; | |
491 | event.SetEventObject(item); | |
492 | item->ProcessCommand (event); | |
493 | } | |
494 |