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