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