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