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