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