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