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