Motif and other mods
[wxWidgets.git] / src / motif / radiobox.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "radiobox.h"
14 #endif
15
16 #include "wx/radiobox.h"
17 #include "wx/utils.h"
18
19 #include <Xm/Label.h>
20 #include <Xm/LabelG.h>
21 #include <Xm/ToggleB.h>
22 #include <Xm/ToggleBG.h>
23 #include <Xm/RowColumn.h>
24 #include <Xm/Form.h>
25
26 #include <wx/motif/private.h>
27
28 void wxRadioBoxCallback (Widget w, XtPointer clientData,
29 XmToggleButtonCallbackStruct * cbs);
30
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
33 #endif
34
35 // Radio box item
36 wxRadioBox::wxRadioBox()
37 {
38 m_selectedButton = -1;
39 m_noItems = 0;
40 m_noRowsOrCols = 0;
41 m_majorDim = 0 ;
42
43 m_formWidget = (WXWidget) 0;
44 m_labelWidget = (WXWidget) 0;
45 m_radioButtons = (WXWidget*) NULL;
46 m_radioButtonLabels = (wxString*) NULL;
47 }
48
49 bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
50 const wxPoint& pos, const wxSize& size,
51 int n, const wxString choices[],
52 int majorDim, long style,
53 const wxValidator& val, const wxString& name)
54 {
55 m_selectedButton = -1;
56 m_noItems = n;
57 m_labelWidget = (WXWidget) 0;
58 m_radioButtons = (WXWidget*) NULL;
59 m_radioButtonLabels = (wxString*) NULL;
60 m_backgroundColour = parent->GetBackgroundColour();
61 m_foregroundColour = parent->GetForegroundColour();
62
63 SetName(name);
64 SetValidator(val);
65
66 parent->AddChild(this);
67
68 m_windowStyle = (long&)style;
69
70 if (id == -1)
71 m_windowId = NewControlId();
72 else
73 m_windowId = id;
74
75 m_noRowsOrCols = majorDim;
76
77 if (majorDim==0)
78 m_majorDim = n ;
79 else
80 m_majorDim = majorDim ;
81
82 Widget parentWidget = (Widget) parent->GetClientWidget();
83
84 wxString label1(wxStripMenuCodes(title));
85
86 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
87
88 Widget formWidget = XtVaCreateManagedWidget ((char*) (const char*) name,
89 xmFormWidgetClass, parentWidget,
90 XmNmarginHeight, 0,
91 XmNmarginWidth, 0,
92 NULL);
93
94 m_formWidget = (WXWidget) formWidget;
95
96 if (label1 != "")
97 {
98 text = XmStringCreateSimple ((char*) (const char*) label1);
99 Widget labelWidget = XtVaCreateManagedWidget ((char*) (const char*) label1,
100 #if wxUSE_GADGETS
101 style & wxCOLOURED ?
102 xmLabelWidgetClass : xmLabelGadgetClass,
103 formWidget,
104 #else
105 xmLabelWidgetClass, formWidget,
106 #endif
107 XmNlabelString, text,
108 NULL);
109
110 /* TODO: change label font
111 if (labelFont)
112 XtVaSetValues (labelWidget,
113 XmNfontList, labelFont->GetInternalFont (XtDisplay(formWidget)),
114 NULL);
115 */
116
117 XmStringFree (text);
118 }
119
120 Arg args[3];
121
122 majorDim = (n + majorDim - 1) / majorDim;
123
124 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
125 XmHORIZONTAL : XmVERTICAL));
126 XtSetArg (args[1], XmNnumColumns, majorDim);
127
128 Widget radioBoxWidget = XmCreateRadioBox (formWidget, "radioBoxWidget", args, 2);
129 m_mainWidget = (WXWidget) radioBoxWidget;
130
131
132 if (m_labelWidget)
133 XtVaSetValues ((Widget) m_labelWidget,
134 XmNtopAttachment, XmATTACH_FORM,
135 XmNleftAttachment, XmATTACH_FORM,
136 XmNalignment, XmALIGNMENT_BEGINNING,
137 NULL);
138
139 XtVaSetValues (radioBoxWidget,
140 XmNtopAttachment, m_labelWidget ? XmATTACH_WIDGET : XmATTACH_FORM,
141 XmNtopWidget, m_labelWidget ? (Widget) m_labelWidget : formWidget,
142 XmNbottomAttachment, XmATTACH_FORM,
143 XmNleftAttachment, XmATTACH_FORM,
144 NULL);
145
146 // if (style & wxFLAT)
147 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
148
149 m_radioButtons = new WXWidget[n];
150 m_radioButtonLabels = new wxString[n];
151 int i;
152 for (i = 0; i < n; i++)
153 {
154 wxString str(wxStripMenuCodes(choices[i]));
155 m_radioButtonLabels[i] = str;
156 m_radioButtons[i] = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) str,
157 #if wxUSE_GADGETS
158 xmToggleButtonGadgetClass, radioBoxWidget,
159 #else
160 xmToggleButtonWidgetClass, radioBoxWidget,
161 #endif
162 NULL);
163 XtAddCallback ((Widget) m_radioButtons[i], XmNvalueChangedCallback, (XtCallbackProc) wxRadioBoxCallback,
164 (XtCallbackProc) this);
165
166 /* TODO: set font
167 if (buttonFont)
168 XtVaSetValues ((Widget) m_radioButtons[i],
169 XmNfontList, buttonFont->GetInternalFont (XtDisplay(formWidget)),
170 NULL);
171 */
172 }
173 SetSelection (0);
174
175 m_windowFont = parent->GetFont();
176 ChangeFont(FALSE);
177
178 XtManageChild (radioBoxWidget);
179
180 SetCanAddEventHandler(TRUE);
181 AttachWidget (parent, m_mainWidget, m_formWidget, pos.x, pos.y, size.x, size.y);
182
183 ChangeBackgroundColour();
184
185 return TRUE;
186 }
187
188
189 wxRadioBox::~wxRadioBox()
190 {
191 delete[] m_radioButtonLabels;
192 delete[] m_radioButtons;
193 }
194
195 wxString wxRadioBox::GetLabel(int item) const
196 {
197 if (item < 0 || item >= m_noItems)
198 return wxEmptyString;
199
200 Widget widget = (Widget) m_radioButtons[item];
201 XmString text;
202 char *s;
203 XtVaGetValues (widget,
204 XmNlabelString, &text,
205 NULL);
206
207 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
208 {
209 // Should we free 'text'???
210 XmStringFree(text);
211 wxString str(s);
212 XtFree (s);
213 return str;
214 }
215 else
216 {
217 XmStringFree(text);
218 return wxEmptyString;
219 }
220 }
221
222 void wxRadioBox::SetLabel(int item, const wxString& label)
223 {
224 if (item < 0 || item >= m_noItems)
225 return;
226
227 Widget widget = (Widget) m_radioButtons[item];
228 if (label != "")
229 {
230 wxString label1(wxStripMenuCodes(label));
231 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
232 XtVaSetValues (widget,
233 XmNlabelString, text,
234 XmNlabelType, XmSTRING,
235 NULL);
236 XmStringFree (text);
237 }
238 }
239
240 int wxRadioBox::FindString(const wxString& s) const
241 {
242 int i;
243 for (i = 0; i < m_noItems; i++)
244 if (s == m_radioButtonLabels[i])
245 return i;
246 return -1;
247 }
248
249 void wxRadioBox::SetSelection(int n)
250 {
251 if ((n < 0) || (n >= m_noItems))
252 return;
253
254 m_selectedButton = n;
255
256 m_inSetValue = TRUE;
257
258 XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE);
259
260 int i;
261 for (i = 0; i < m_noItems; i++)
262 if (i != n)
263 XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE);
264
265 m_inSetValue = FALSE;
266 }
267
268 // Get single selection, for single choice list items
269 int wxRadioBox::GetSelection() const
270 {
271 return m_selectedButton;
272 }
273
274 // Find string for position
275 wxString wxRadioBox::GetString(int n) const
276 {
277 if ((n < 0) || (n >= m_noItems))
278 return wxEmptyString;
279 return m_radioButtonLabels[n];
280 }
281
282 void wxRadioBox::SetSize(int x, int y, int width, int height, int sizeFlags)
283 {
284 bool managed = XtIsManaged((Widget) m_formWidget);
285
286 if (managed)
287 XtUnmanageChild ((Widget) m_formWidget);
288
289 int xx = x; int yy = y;
290 AdjustForParentClientOrigin(xx, yy, sizeFlags);
291
292 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
293 XtVaSetValues ((Widget) m_formWidget, XmNleftAttachment, XmATTACH_SELF,
294 XmNx, xx, NULL);
295 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
296 XtVaSetValues ((Widget) m_formWidget, XmNtopAttachment, XmATTACH_SELF,
297 XmNy, yy, NULL);
298
299 // Must set the actual RadioBox to be desired size MINUS label size
300 Dimension labelWidth = 0, labelHeight = 0, actualWidth = 0, actualHeight = 0;
301
302 if (m_labelWidget)
303 XtVaGetValues ((Widget) m_labelWidget, XmNwidth, &labelWidth, XmNheight, &labelHeight, NULL);
304
305 actualWidth = width;
306 actualHeight = height - labelHeight;
307
308 if (width > -1)
309 {
310 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, actualWidth, NULL);
311 }
312 if (height > -1)
313 {
314 XtVaSetValues ((Widget) m_mainWidget, XmNheight, actualHeight, NULL);
315 }
316 if (managed)
317 XtManageChild ((Widget) m_formWidget);
318 }
319
320 // Enable a specific button
321 void wxRadioBox::Enable(int n, bool enable)
322 {
323 if ((n < 0) || (n >= m_noItems))
324 return;
325
326 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
327 }
328
329 // Enable all controls
330 void wxRadioBox::Enable(bool enable)
331 {
332 wxControl::Enable(enable);
333
334 int i;
335 for (i = 0; i < m_noItems; i++)
336 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
337 }
338
339 // Show a specific button
340 void wxRadioBox::Show(int n, bool show)
341 {
342 // This method isn't complete, and we try do do our best...
343 // It's main purpose isn't for allowing Show/Unshow dynamically,
344 // but rather to provide a way to design wxRadioBox such:
345 //
346 // o Val1 o Val2 o Val3
347 // o Val4 o Val6
348 // o Val7 o Val8 o Val9
349 //
350 // In my case, this is a 'direction' box, and the Show(5,False) is
351 // coupled with an Enable(5,False)
352 //
353 if ((n < 0) || (n >= m_noItems))
354 return;
355
356 XtVaSetValues ((Widget) m_radioButtons[n],
357 XmNindicatorOn, (unsigned char) show,
358 NULL);
359
360 // Please note that this is all we can do: removing the label
361 // if switching to unshow state. However, when switching
362 // to the on state, it's the prog. resp. to call SetLabel(item,...)
363 // after this call!!
364 if (!show)
365 wxRadioBox::SetLabel (n, " ");
366 }
367
368 // For single selection items only
369 wxString wxRadioBox::GetStringSelection () const
370 {
371 int sel = GetSelection ();
372 if (sel > -1)
373 return this->GetString (sel);
374 else
375 return wxString("");
376 }
377
378 bool wxRadioBox::SetStringSelection (const wxString& s)
379 {
380 int sel = FindString (s);
381 if (sel > -1)
382 {
383 SetSelection (sel);
384 return TRUE;
385 }
386 else
387 return FALSE;
388 }
389
390 void wxRadioBox::Command (wxCommandEvent & event)
391 {
392 SetSelection (event.m_commandInt);
393 ProcessCommand (event);
394 }
395
396 void wxRadioBox::ChangeFont(bool keepOriginalSize)
397 {
398 // TODO
399 }
400
401 void wxRadioBox::ChangeBackgroundColour()
402 {
403 // TODO
404 }
405
406 void wxRadioBox::ChangeForegroundColour()
407 {
408 // TODO
409 }
410
411 void wxRadioBoxCallback (Widget w, XtPointer clientData,
412 XmToggleButtonCallbackStruct * cbs)
413 {
414 if (!cbs->set)
415 return;
416
417 wxRadioBox *item = (wxRadioBox *) clientData;
418 int sel = -1;
419 int i;
420 for (i = 0; i < item->Number(); i++)
421 if (item->GetRadioButtons() && ((Widget) (item->GetRadioButtons()[i]) == w))
422 sel = i;
423 item->SetSel(sel);
424
425 if (item->InSetValue())
426 return;
427
428 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
429 event.m_commandInt = sel;
430 event.SetEventObject(item);
431 item->ProcessCommand (event);
432 }
433