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