]>
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 | ||
bcd055ae JJ |
15 | #ifdef __VMS |
16 | #define XtDisplay XTDISPLAY | |
17 | #endif | |
18 | ||
f6045f99 GD |
19 | #include "wx/defs.h" |
20 | ||
4bb6408c | 21 | #include "wx/radiobox.h" |
a4294b78 | 22 | #include "wx/utils.h" |
584ad2a3 | 23 | #include "wx/arrstr.h" |
a4294b78 | 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 | 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 | ||
978db70e | 110 | m_majorDim = (n + m_majorDim - 1) / m_majorDim; |
a4294b78 JS |
111 | |
112 | XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ? | |
af0bb3b1 | 113 | XmHORIZONTAL : XmVERTICAL)); |
98300330 | 114 | XtSetArg (args[1], XmNnumColumns, m_majorDim); |
795c00bd | 115 | XtSetArg (args[2], XmNadjustLast, False); |
a4294b78 | 116 | |
795c00bd | 117 | Widget radioBoxWidget = |
da8cf723 | 118 | XmCreateRadioBox ((Widget)m_mainWidget, wxMOTIF_STR("radioBoxWidget"), args, 3); |
a4294b78 | 119 | |
3bdb8629 MB |
120 | m_radioButtons.reserve(n); |
121 | m_radioButtonLabels.reserve(n); | |
a4294b78 | 122 | |
a4294b78 JS |
123 | int i; |
124 | for (i = 0; i < n; i++) | |
125 | { | |
126 | wxString str(wxStripMenuCodes(choices[i])); | |
3bdb8629 | 127 | m_radioButtonLabels.push_back(str); |
73608949 MB |
128 | Widget radioItem = XtVaCreateManagedWidget ( |
129 | wxConstCast(str.c_str(), char), | |
a4294b78 | 130 | #if wxUSE_GADGETS |
73608949 | 131 | xmToggleButtonGadgetClass, radioBoxWidget, |
a4294b78 | 132 | #else |
73608949 | 133 | xmToggleButtonWidgetClass, radioBoxWidget, |
a4294b78 | 134 | #endif |
73608949 MB |
135 | wxFont::GetFontTag(), m_font.GetFontTypeC(dpy), |
136 | NULL); | |
3bdb8629 MB |
137 | m_radioButtons.push_back((WXWidget)radioItem); |
138 | XtAddCallback (radioItem, XmNvalueChangedCallback, | |
139 | (XtCallbackProc) wxRadioBoxCallback, | |
140 | (XtPointer) this); | |
a4294b78 | 141 | } |
a4294b78 | 142 | |
96be256b | 143 | ChangeFont(false); |
4b5f3fe6 | 144 | |
2ad99bc1 | 145 | SetSelection (0); |
1a87edf2 | 146 | |
2ad99bc1 | 147 | XtRealizeWidget((Widget)m_mainWidget); |
a4294b78 | 148 | XtManageChild (radioBoxWidget); |
2ad99bc1 | 149 | XtManageChild ((Widget)m_mainWidget); |
a4294b78 | 150 | |
2ad99bc1 | 151 | AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y); |
a4294b78 | 152 | |
0d57be45 | 153 | ChangeBackgroundColour(); |
a4294b78 | 154 | |
96be256b | 155 | return true; |
4bb6408c JS |
156 | } |
157 | ||
584ad2a3 MB |
158 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title, |
159 | const wxPoint& pos, const wxSize& size, | |
160 | const wxArrayString& choices, | |
161 | int majorDim, long style, | |
162 | const wxValidator& val, const wxString& name) | |
163 | { | |
164 | wxCArrayString chs(choices); | |
165 | return Create(parent, id, title, pos, size, chs.GetCount(), | |
166 | chs.GetStrings(), majorDim, style, val, name); | |
167 | } | |
4bb6408c JS |
168 | |
169 | wxRadioBox::~wxRadioBox() | |
170 | { | |
15d5ab67 | 171 | DetachWidget(m_mainWidget); |
15d5ab67 | 172 | XtDestroyWidget((Widget) m_mainWidget); |
15d5ab67 JS |
173 | |
174 | m_mainWidget = (WXWidget) 0; | |
4bb6408c JS |
175 | } |
176 | ||
18128cbb | 177 | void wxRadioBox::SetString(int item, const wxString& label) |
4bb6408c | 178 | { |
789f6795 | 179 | if (!IsValid(item)) |
a4294b78 JS |
180 | return; |
181 | ||
182 | Widget widget = (Widget) m_radioButtons[item]; | |
1a87edf2 | 183 | if (!label.empty()) |
a4294b78 JS |
184 | { |
185 | wxString label1(wxStripMenuCodes(label)); | |
da494b40 | 186 | wxXmString text( label1 ); |
3bdb8629 | 187 | m_radioButtonLabels[item] = label1; |
a4294b78 | 188 | XtVaSetValues (widget, |
da494b40 | 189 | XmNlabelString, text(), |
af0bb3b1 VZ |
190 | XmNlabelType, XmSTRING, |
191 | NULL); | |
a4294b78 | 192 | } |
4bb6408c JS |
193 | } |
194 | ||
4bb6408c JS |
195 | void wxRadioBox::SetSelection(int n) |
196 | { | |
789f6795 | 197 | if (!IsValid(n)) |
4bb6408c | 198 | return; |
4bb6408c JS |
199 | |
200 | m_selectedButton = n; | |
a4294b78 | 201 | |
96be256b | 202 | m_inSetValue = true; |
a4294b78 | 203 | |
96be256b | 204 | XmToggleButtonSetState ((Widget) m_radioButtons[n], True, False); |
a4294b78 JS |
205 | |
206 | int i; | |
207 | for (i = 0; i < m_noItems; i++) | |
208 | if (i != n) | |
96be256b | 209 | XmToggleButtonSetState ((Widget) m_radioButtons[i], False, False); |
a4294b78 | 210 | |
96be256b | 211 | m_inSetValue = false; |
4bb6408c JS |
212 | } |
213 | ||
214 | // Get single selection, for single choice list items | |
215 | int wxRadioBox::GetSelection() const | |
216 | { | |
217 | return m_selectedButton; | |
218 | } | |
219 | ||
220 | // Find string for position | |
221 | wxString wxRadioBox::GetString(int n) const | |
222 | { | |
789f6795 | 223 | if (!IsValid(n)) |
a4294b78 JS |
224 | return wxEmptyString; |
225 | return m_radioButtonLabels[n]; | |
4bb6408c JS |
226 | } |
227 | ||
bfc6fde4 | 228 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
4bb6408c | 229 | { |
2ad99bc1 | 230 | bool managed = XtIsManaged((Widget) m_mainWidget); |
4bb6408c | 231 | |
a4294b78 | 232 | if (managed) |
2ad99bc1 | 233 | XtUnmanageChild ((Widget) m_mainWidget); |
4bb6408c | 234 | |
a4294b78 JS |
235 | int xx = x; int yy = y; |
236 | AdjustForParentClientOrigin(xx, yy, sizeFlags); | |
4bb6408c | 237 | |
a4294b78 | 238 | if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
2ad99bc1 | 239 | XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL); |
a4294b78 | 240 | if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) |
1a87edf2 | 241 | XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL); |
4bb6408c | 242 | |
2ad99bc1 JS |
243 | if (width > 0) |
244 | XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL); | |
245 | if (height > 0) | |
246 | XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL); | |
a4294b78 | 247 | |
a4294b78 | 248 | if (managed) |
2ad99bc1 | 249 | XtManageChild ((Widget) m_mainWidget); |
4bb6408c JS |
250 | } |
251 | ||
252 | // Enable a specific button | |
1a87edf2 | 253 | bool wxRadioBox::Enable(int n, bool enable) |
4bb6408c | 254 | { |
789f6795 | 255 | if (!IsValid(n)) |
1a87edf2 | 256 | return false; |
a4294b78 JS |
257 | |
258 | XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable); | |
1a87edf2 | 259 | return true; |
4bb6408c JS |
260 | } |
261 | ||
262 | // Enable all controls | |
af0bb3b1 | 263 | bool wxRadioBox::Enable(bool enable) |
4bb6408c | 264 | { |
af0bb3b1 | 265 | if ( !wxControl::Enable(enable) ) |
96be256b | 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 | 271 | |
96be256b | 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 |
789f6795 | 282 | bool 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 | // | |
1a87edf2 WS |
288 | // o Val1 o Val2 o Val3 |
289 | // o Val4 o Val6 | |
290 | // o Val7 o Val8 o Val9 | |
a4294b78 JS |
291 | // |
292 | // In my case, this is a 'direction' box, and the Show(5,False) is | |
293 | // coupled with an Enable(5,False) | |
294 | // | |
789f6795 WS |
295 | if (!IsValid(n)) |
296 | return false; | |
a4294b78 JS |
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, " "); |
789f6795 WS |
308 | |
309 | return true; | |
4bb6408c JS |
310 | } |
311 | ||
312 | // For single selection items only | |
313 | wxString wxRadioBox::GetStringSelection () const | |
314 | { | |
315 | int sel = GetSelection (); | |
316 | if (sel > -1) | |
317 | return this->GetString (sel); | |
318 | else | |
1a87edf2 | 319 | return wxEmptyString; |
4bb6408c JS |
320 | } |
321 | ||
322 | bool wxRadioBox::SetStringSelection (const wxString& s) | |
323 | { | |
324 | int sel = FindString (s); | |
325 | if (sel > -1) | |
326 | { | |
327 | SetSelection (sel); | |
96be256b | 328 | return true; |
4bb6408c JS |
329 | } |
330 | else | |
96be256b | 331 | return false; |
4bb6408c JS |
332 | } |
333 | ||
334 | void wxRadioBox::Command (wxCommandEvent & event) | |
335 | { | |
687706f5 | 336 | SetSelection (event.GetInt()); |
4bb6408c JS |
337 | ProcessCommand (event); |
338 | } | |
339 | ||
4b5f3fe6 | 340 | void wxRadioBox::ChangeFont(bool keepOriginalSize) |
0d57be45 | 341 | { |
94b49b93 JS |
342 | wxWindow::ChangeFont(keepOriginalSize); |
343 | ||
94b49b93 JS |
344 | int i; |
345 | for (i = 0; i < m_noItems; i++) | |
346 | { | |
347 | WXWidget radioButton = m_radioButtons[i]; | |
348 | ||
349 | XtVaSetValues ((Widget) radioButton, | |
73608949 | 350 | wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay((Widget) GetTopWidget())), |
94b49b93 JS |
351 | NULL); |
352 | } | |
0d57be45 JS |
353 | } |
354 | ||
355 | void wxRadioBox::ChangeBackgroundColour() | |
356 | { | |
94b49b93 JS |
357 | wxWindow::ChangeBackgroundColour(); |
358 | ||
eb6fa4b4 | 359 | int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget)); |
9838df2c | 360 | |
94b49b93 JS |
361 | int i; |
362 | for (i = 0; i < m_noItems; i++) | |
363 | { | |
364 | WXWidget radioButton = m_radioButtons[i]; | |
365 | ||
96be256b | 366 | wxDoChangeBackgroundColour(radioButton, m_backgroundColour, true); |
9838df2c JS |
367 | |
368 | XtVaSetValues ((Widget) radioButton, | |
369 | XmNselectColor, selectPixel, | |
370 | NULL); | |
94b49b93 | 371 | } |
0d57be45 JS |
372 | } |
373 | ||
374 | void wxRadioBox::ChangeForegroundColour() | |
375 | { | |
94b49b93 JS |
376 | wxWindow::ChangeForegroundColour(); |
377 | ||
378 | int i; | |
379 | for (i = 0; i < m_noItems; i++) | |
380 | { | |
381 | WXWidget radioButton = m_radioButtons[i]; | |
382 | ||
a8680e3e | 383 | wxDoChangeForegroundColour(radioButton, m_foregroundColour); |
94b49b93 | 384 | } |
0d57be45 JS |
385 | } |
386 | ||
18128cbb MB |
387 | static int CalcOtherDim( int items, int dim ) |
388 | { | |
389 | return items / dim + ( items % dim ? 1 : 0 ); | |
390 | } | |
391 | ||
392 | int wxRadioBox::GetRowCount() const | |
393 | { | |
394 | return m_windowStyle & wxRA_SPECIFY_ROWS ? m_noRowsOrCols | |
395 | : CalcOtherDim( GetCount(), m_noRowsOrCols ); | |
396 | } | |
397 | ||
398 | int wxRadioBox::GetColumnCount() const | |
399 | { | |
400 | return m_windowStyle & wxRA_SPECIFY_COLS ? m_noRowsOrCols | |
401 | : CalcOtherDim( GetCount(), m_noRowsOrCols ); | |
402 | } | |
403 | ||
a4294b78 | 404 | void wxRadioBoxCallback (Widget w, XtPointer clientData, |
af0bb3b1 | 405 | XmToggleButtonCallbackStruct * cbs) |
a4294b78 JS |
406 | { |
407 | if (!cbs->set) | |
408 | return; | |
409 | ||
410 | wxRadioBox *item = (wxRadioBox *) clientData; | |
411 | int sel = -1; | |
412 | int i; | |
3bdb8629 | 413 | const wxWidgetArray& buttons = item->GetRadioButtons(); |
3c85ada9 | 414 | for (i = 0; i < item->GetCount(); i++) |
3bdb8629 | 415 | if (((Widget)buttons[i]) == w) |
a4294b78 JS |
416 | sel = i; |
417 | item->SetSel(sel); | |
418 | ||
419 | if (item->InSetValue()) | |
420 | return; | |
421 | ||
422 | wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId()); | |
2ad99bc1 JS |
423 | event.SetInt(sel); |
424 | event.SetString(item->GetStringSelection()); | |
a4294b78 JS |
425 | event.SetEventObject(item); |
426 | item->ProcessCommand (event); | |
427 | } |