]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
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 | |
65571936 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
4bb6408c JS |
13 | #pragma implementation "radiobox.h" |
14 | #endif | |
15 | ||
1248b41f MB |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
bcd055ae JJ |
19 | #ifdef __VMS |
20 | #define XtDisplay XTDISPLAY | |
21 | #endif | |
22 | ||
f6045f99 GD |
23 | #include "wx/defs.h" |
24 | ||
4bb6408c | 25 | #include "wx/radiobox.h" |
a4294b78 | 26 | #include "wx/utils.h" |
584ad2a3 | 27 | #include "wx/arrstr.h" |
a4294b78 | 28 | |
338dd992 JJ |
29 | #ifdef __VMS__ |
30 | #pragma message disable nosimpint | |
31 | #endif | |
a4294b78 JS |
32 | #include <Xm/Label.h> |
33 | #include <Xm/LabelG.h> | |
34 | #include <Xm/ToggleB.h> | |
35 | #include <Xm/ToggleBG.h> | |
36 | #include <Xm/RowColumn.h> | |
9838df2c | 37 | #include <Xm/Frame.h> |
338dd992 JJ |
38 | #ifdef __VMS__ |
39 | #pragma message enable nosimpint | |
40 | #endif | |
a4294b78 | 41 | |
3096bd2f | 42 | #include "wx/motif/private.h" |
a4294b78 JS |
43 | |
44 | void wxRadioBoxCallback (Widget w, XtPointer clientData, | |
af0bb3b1 | 45 | XmToggleButtonCallbackStruct * cbs); |
4bb6408c | 46 | |
4bb6408c | 47 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) |
4bb6408c JS |
48 | |
49 | // Radio box item | |
3bdb8629 | 50 | void wxRadioBox::Init() |
4bb6408c JS |
51 | { |
52 | m_selectedButton = -1; | |
53 | m_noItems = 0; | |
54 | m_noRowsOrCols = 0; | |
55 | m_majorDim = 0 ; | |
56 | } | |
57 | ||
58 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title, | |
59 | const wxPoint& pos, const wxSize& size, | |
60 | int n, const wxString choices[], | |
61 | int majorDim, long style, | |
62 | const wxValidator& val, const wxString& name) | |
63 | { | |
3bdb8629 MB |
64 | if( !CreateControl( parent, id, pos, size, style, val, name ) ) |
65 | return false; | |
4bb6408c | 66 | |
3bdb8629 | 67 | m_noItems = n; |
4bb6408c JS |
68 | m_noRowsOrCols = majorDim; |
69 | ||
70 | if (majorDim==0) | |
71 | m_majorDim = n ; | |
72 | else | |
73 | m_majorDim = majorDim ; | |
74 | ||
a4294b78 JS |
75 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
76 | ||
34774093 | 77 | m_mainWidget = XtVaCreateWidget ("radioboxframe", |
2ad99bc1 | 78 | xmFrameWidgetClass, parentWidget, |
2ad99bc1 JS |
79 | XmNresizeHeight, True, |
80 | XmNresizeWidth, True, | |
af0bb3b1 | 81 | NULL); |
a4294b78 | 82 | |
2ad99bc1 | 83 | wxString label1(wxStripMenuCodes(title)); |
a4294b78 | 84 | |
da494b40 | 85 | WXFontType fontType = m_font.GetFontType(XtDisplay(parentWidget)); |
2ad99bc1 | 86 | |
a4294b78 JS |
87 | if (label1 != "") |
88 | { | |
31528cd3 | 89 | wxXmString text(label1); |
3dd709d8 MB |
90 | m_labelWidget = (WXWidget) |
91 | XtVaCreateManagedWidget( label1.c_str(), | |
a4294b78 | 92 | #if wxUSE_GADGETS |
da494b40 MB |
93 | style & wxCOLOURED ? xmLabelWidgetClass |
94 | : xmLabelGadgetClass, | |
95 | (Widget)m_mainWidget, | |
a4294b78 | 96 | #else |
da494b40 MB |
97 | xmLabelWidgetClass, |
98 | (Widget)m_mainWidget, | |
a4294b78 | 99 | #endif |
da494b40 MB |
100 | wxFont::GetFontTag(), fontType, |
101 | XmNlabelString, text(), | |
11a2ce5a MB |
102 | // XmNframeChildType is not in Motif 1.2, nor in Lesstif, |
103 | // if it was compiled with 1.2 compatibility | |
34774093 | 104 | // TODO: check this still looks OK for Motif 1.2. |
11a2ce5a | 105 | #if (XmVersion > 1200) |
da494b40 | 106 | XmNframeChildType, XmFRAME_TITLE_CHILD, |
2b5f62a0 | 107 | #else |
da494b40 | 108 | XmNchildType, XmFRAME_TITLE_CHILD, |
34774093 | 109 | #endif |
da494b40 MB |
110 | XmNchildVerticalAlignment, |
111 | XmALIGNMENT_CENTER, | |
112 | NULL); | |
a4294b78 JS |
113 | } |
114 | ||
115 | Arg args[3]; | |
116 | ||
978db70e | 117 | m_majorDim = (n + m_majorDim - 1) / m_majorDim; |
a4294b78 JS |
118 | |
119 | XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ? | |
af0bb3b1 | 120 | XmHORIZONTAL : XmVERTICAL)); |
98300330 | 121 | XtSetArg (args[1], XmNnumColumns, m_majorDim); |
795c00bd | 122 | XtSetArg (args[2], XmNadjustLast, False); |
a4294b78 | 123 | |
795c00bd MB |
124 | Widget radioBoxWidget = |
125 | XmCreateRadioBox ((Widget)m_mainWidget, "radioBoxWidget", args, 3); | |
a4294b78 | 126 | |
3bdb8629 MB |
127 | m_radioButtons.reserve(n); |
128 | m_radioButtonLabels.reserve(n); | |
a4294b78 | 129 | |
a4294b78 JS |
130 | int i; |
131 | for (i = 0; i < n; i++) | |
132 | { | |
133 | wxString str(wxStripMenuCodes(choices[i])); | |
3bdb8629 MB |
134 | m_radioButtonLabels.push_back(str); |
135 | Widget radioItem = XtVaCreateManagedWidget (wxConstCast(str.c_str(), char), | |
a4294b78 | 136 | #if wxUSE_GADGETS |
af0bb3b1 | 137 | xmToggleButtonGadgetClass, radioBoxWidget, |
a4294b78 | 138 | #else |
da494b40 | 139 | xmToggleButtonWidgetClass, radioBoxWidget, |
a4294b78 | 140 | #endif |
da494b40 | 141 | wxFont::GetFontTag(), fontType, |
af0bb3b1 | 142 | NULL); |
3bdb8629 MB |
143 | m_radioButtons.push_back((WXWidget)radioItem); |
144 | XtAddCallback (radioItem, XmNvalueChangedCallback, | |
145 | (XtCallbackProc) wxRadioBoxCallback, | |
146 | (XtPointer) this); | |
a4294b78 | 147 | } |
a4294b78 | 148 | |
4b5f3fe6 JS |
149 | ChangeFont(FALSE); |
150 | ||
2ad99bc1 JS |
151 | SetSelection (0); |
152 | ||
153 | XtRealizeWidget((Widget)m_mainWidget); | |
a4294b78 | 154 | XtManageChild (radioBoxWidget); |
2ad99bc1 | 155 | XtManageChild ((Widget)m_mainWidget); |
a4294b78 | 156 | |
2ad99bc1 | 157 | AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y); |
a4294b78 | 158 | |
0d57be45 | 159 | ChangeBackgroundColour(); |
a4294b78 JS |
160 | |
161 | return TRUE; | |
4bb6408c JS |
162 | } |
163 | ||
584ad2a3 MB |
164 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title, |
165 | const wxPoint& pos, const wxSize& size, | |
166 | const wxArrayString& choices, | |
167 | int majorDim, long style, | |
168 | const wxValidator& val, const wxString& name) | |
169 | { | |
170 | wxCArrayString chs(choices); | |
171 | return Create(parent, id, title, pos, size, chs.GetCount(), | |
172 | chs.GetStrings(), majorDim, style, val, name); | |
173 | } | |
4bb6408c JS |
174 | |
175 | wxRadioBox::~wxRadioBox() | |
176 | { | |
15d5ab67 | 177 | DetachWidget(m_mainWidget); |
15d5ab67 | 178 | XtDestroyWidget((Widget) m_mainWidget); |
15d5ab67 JS |
179 | |
180 | m_mainWidget = (WXWidget) 0; | |
4bb6408c JS |
181 | } |
182 | ||
18128cbb | 183 | void wxRadioBox::SetString(int item, const wxString& label) |
4bb6408c | 184 | { |
a4294b78 JS |
185 | if (item < 0 || item >= m_noItems) |
186 | return; | |
187 | ||
188 | Widget widget = (Widget) m_radioButtons[item]; | |
189 | if (label != "") | |
190 | { | |
191 | wxString label1(wxStripMenuCodes(label)); | |
da494b40 | 192 | wxXmString text( label1 ); |
3bdb8629 | 193 | m_radioButtonLabels[item] = label1; |
a4294b78 | 194 | XtVaSetValues (widget, |
da494b40 | 195 | XmNlabelString, text(), |
af0bb3b1 VZ |
196 | XmNlabelType, XmSTRING, |
197 | NULL); | |
a4294b78 | 198 | } |
4bb6408c JS |
199 | } |
200 | ||
201 | int wxRadioBox::FindString(const wxString& s) const | |
202 | { | |
a4294b78 JS |
203 | int i; |
204 | for (i = 0; i < m_noItems; i++) | |
205 | if (s == m_radioButtonLabels[i]) | |
206 | return i; | |
4bb6408c JS |
207 | return -1; |
208 | } | |
209 | ||
210 | void wxRadioBox::SetSelection(int n) | |
211 | { | |
212 | if ((n < 0) || (n >= m_noItems)) | |
213 | return; | |
4bb6408c JS |
214 | |
215 | m_selectedButton = n; | |
a4294b78 JS |
216 | |
217 | m_inSetValue = TRUE; | |
218 | ||
219 | XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE); | |
220 | ||
221 | int i; | |
222 | for (i = 0; i < m_noItems; i++) | |
223 | if (i != n) | |
224 | XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE); | |
225 | ||
226 | m_inSetValue = FALSE; | |
4bb6408c JS |
227 | } |
228 | ||
229 | // Get single selection, for single choice list items | |
230 | int wxRadioBox::GetSelection() const | |
231 | { | |
232 | return m_selectedButton; | |
233 | } | |
234 | ||
235 | // Find string for position | |
236 | wxString wxRadioBox::GetString(int n) const | |
237 | { | |
a4294b78 JS |
238 | if ((n < 0) || (n >= m_noItems)) |
239 | return wxEmptyString; | |
240 | return m_radioButtonLabels[n]; | |
4bb6408c JS |
241 | } |
242 | ||
bfc6fde4 | 243 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
4bb6408c | 244 | { |
2ad99bc1 | 245 | bool managed = XtIsManaged((Widget) m_mainWidget); |
4bb6408c | 246 | |
a4294b78 | 247 | if (managed) |
2ad99bc1 | 248 | XtUnmanageChild ((Widget) m_mainWidget); |
4bb6408c | 249 | |
a4294b78 JS |
250 | int xx = x; int yy = y; |
251 | AdjustForParentClientOrigin(xx, yy, sizeFlags); | |
4bb6408c | 252 | |
a4294b78 | 253 | if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
2ad99bc1 | 254 | XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL); |
a4294b78 | 255 | if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
2ad99bc1 | 256 | XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL); |
4bb6408c | 257 | |
2ad99bc1 JS |
258 | if (width > 0) |
259 | XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL); | |
260 | if (height > 0) | |
261 | XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL); | |
a4294b78 | 262 | |
a4294b78 | 263 | if (managed) |
2ad99bc1 | 264 | XtManageChild ((Widget) m_mainWidget); |
4bb6408c JS |
265 | } |
266 | ||
267 | // Enable a specific button | |
a4294b78 | 268 | void wxRadioBox::Enable(int n, bool enable) |
4bb6408c | 269 | { |
a4294b78 JS |
270 | if ((n < 0) || (n >= m_noItems)) |
271 | return; | |
272 | ||
273 | XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable); | |
4bb6408c JS |
274 | } |
275 | ||
276 | // Enable all controls | |
af0bb3b1 | 277 | bool wxRadioBox::Enable(bool enable) |
4bb6408c | 278 | { |
af0bb3b1 VZ |
279 | if ( !wxControl::Enable(enable) ) |
280 | return FALSE; | |
4bb6408c | 281 | |
a4294b78 JS |
282 | int i; |
283 | for (i = 0; i < m_noItems; i++) | |
284 | XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable); | |
af0bb3b1 VZ |
285 | |
286 | return TRUE; | |
4bb6408c JS |
287 | } |
288 | ||
9838df2c JS |
289 | bool wxRadioBox::Show(bool show) |
290 | { | |
291 | // TODO: show/hide all children | |
292 | return wxControl::Show(show); | |
293 | } | |
294 | ||
4bb6408c | 295 | // Show a specific button |
a4294b78 | 296 | void wxRadioBox::Show(int n, bool show) |
4bb6408c | 297 | { |
a4294b78 JS |
298 | // This method isn't complete, and we try do do our best... |
299 | // It's main purpose isn't for allowing Show/Unshow dynamically, | |
300 | // but rather to provide a way to design wxRadioBox such: | |
301 | // | |
302 | // o Val1 o Val2 o Val3 | |
303 | // o Val4 o Val6 | |
304 | // o Val7 o Val8 o Val9 | |
305 | // | |
306 | // In my case, this is a 'direction' box, and the Show(5,False) is | |
307 | // coupled with an Enable(5,False) | |
308 | // | |
309 | if ((n < 0) || (n >= m_noItems)) | |
310 | return; | |
311 | ||
312 | XtVaSetValues ((Widget) m_radioButtons[n], | |
313 | XmNindicatorOn, (unsigned char) show, | |
314 | NULL); | |
315 | ||
316 | // Please note that this is all we can do: removing the label | |
317 | // if switching to unshow state. However, when switching | |
18128cbb | 318 | // to the on state, it's the prog. resp. to call SetString(item,...) |
a4294b78 JS |
319 | // after this call!! |
320 | if (!show) | |
18128cbb | 321 | wxRadioBox::SetString (n, " "); |
4bb6408c JS |
322 | } |
323 | ||
324 | // For single selection items only | |
325 | wxString wxRadioBox::GetStringSelection () const | |
326 | { | |
327 | int sel = GetSelection (); | |
328 | if (sel > -1) | |
329 | return this->GetString (sel); | |
330 | else | |
331 | return wxString(""); | |
332 | } | |
333 | ||
334 | bool wxRadioBox::SetStringSelection (const wxString& s) | |
335 | { | |
336 | int sel = FindString (s); | |
337 | if (sel > -1) | |
338 | { | |
339 | SetSelection (sel); | |
340 | return TRUE; | |
341 | } | |
342 | else | |
343 | return FALSE; | |
344 | } | |
345 | ||
346 | void wxRadioBox::Command (wxCommandEvent & event) | |
347 | { | |
687706f5 | 348 | SetSelection (event.GetInt()); |
4bb6408c JS |
349 | ProcessCommand (event); |
350 | } | |
351 | ||
4b5f3fe6 | 352 | void wxRadioBox::ChangeFont(bool keepOriginalSize) |
0d57be45 | 353 | { |
94b49b93 JS |
354 | wxWindow::ChangeFont(keepOriginalSize); |
355 | ||
da494b40 MB |
356 | WXFontType fontType = |
357 | m_font.GetFontType(XtDisplay((Widget) GetTopWidget())); | |
94b49b93 JS |
358 | |
359 | int i; | |
360 | for (i = 0; i < m_noItems; i++) | |
361 | { | |
362 | WXWidget radioButton = m_radioButtons[i]; | |
363 | ||
364 | XtVaSetValues ((Widget) radioButton, | |
da494b40 | 365 | wxFont::GetFontTag(), fontType, |
94b49b93 JS |
366 | NULL); |
367 | } | |
0d57be45 JS |
368 | } |
369 | ||
370 | void wxRadioBox::ChangeBackgroundColour() | |
371 | { | |
94b49b93 JS |
372 | wxWindow::ChangeBackgroundColour(); |
373 | ||
eb6fa4b4 | 374 | int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget)); |
9838df2c | 375 | |
94b49b93 JS |
376 | int i; |
377 | for (i = 0; i < m_noItems; i++) | |
378 | { | |
379 | WXWidget radioButton = m_radioButtons[i]; | |
380 | ||
a8680e3e | 381 | wxDoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE); |
9838df2c JS |
382 | |
383 | XtVaSetValues ((Widget) radioButton, | |
384 | XmNselectColor, selectPixel, | |
385 | NULL); | |
94b49b93 | 386 | } |
0d57be45 JS |
387 | } |
388 | ||
389 | void wxRadioBox::ChangeForegroundColour() | |
390 | { | |
94b49b93 JS |
391 | wxWindow::ChangeForegroundColour(); |
392 | ||
393 | int i; | |
394 | for (i = 0; i < m_noItems; i++) | |
395 | { | |
396 | WXWidget radioButton = m_radioButtons[i]; | |
397 | ||
a8680e3e | 398 | wxDoChangeForegroundColour(radioButton, m_foregroundColour); |
94b49b93 | 399 | } |
0d57be45 JS |
400 | } |
401 | ||
18128cbb MB |
402 | static int CalcOtherDim( int items, int dim ) |
403 | { | |
404 | return items / dim + ( items % dim ? 1 : 0 ); | |
405 | } | |
406 | ||
407 | int wxRadioBox::GetRowCount() const | |
408 | { | |
409 | return m_windowStyle & wxRA_SPECIFY_ROWS ? m_noRowsOrCols | |
410 | : CalcOtherDim( GetCount(), m_noRowsOrCols ); | |
411 | } | |
412 | ||
413 | int wxRadioBox::GetColumnCount() const | |
414 | { | |
415 | return m_windowStyle & wxRA_SPECIFY_COLS ? m_noRowsOrCols | |
416 | : CalcOtherDim( GetCount(), m_noRowsOrCols ); | |
417 | } | |
418 | ||
a4294b78 | 419 | void wxRadioBoxCallback (Widget w, XtPointer clientData, |
af0bb3b1 | 420 | XmToggleButtonCallbackStruct * cbs) |
a4294b78 JS |
421 | { |
422 | if (!cbs->set) | |
423 | return; | |
424 | ||
425 | wxRadioBox *item = (wxRadioBox *) clientData; | |
426 | int sel = -1; | |
427 | int i; | |
3bdb8629 | 428 | const wxWidgetArray& buttons = item->GetRadioButtons(); |
3c85ada9 | 429 | for (i = 0; i < item->GetCount(); i++) |
3bdb8629 | 430 | if (((Widget)buttons[i]) == w) |
a4294b78 JS |
431 | sel = i; |
432 | item->SetSel(sel); | |
433 | ||
434 | if (item->InSetValue()) | |
435 | return; | |
436 | ||
437 | wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId()); | |
2ad99bc1 JS |
438 | event.SetInt(sel); |
439 | event.SetString(item->GetStringSelection()); | |
a4294b78 JS |
440 | event.SetEventObject(item); |
441 | item->ProcessCommand (event); | |
442 | } | |
4bb6408c | 443 |