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