]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobox.cpp
Changed a few true -> TRUE
[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
af0bb3b1 9// Licence: wxWindows licence
4bb6408c
JS
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,
af0bb3b1 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();
da175b2c 66 m_font = 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,
af0bb3b1
VZ
94 xmFormWidgetClass, parentWidget,
95 XmNmarginHeight, 0,
96 XmNmarginWidth, 0,
97 NULL);
a4294b78
JS
98
99 m_formWidget = (WXWidget) formWidget;
100
da175b2c 101 XmFontList fontList = (XmFontList) m_font.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
af0bb3b1
VZ
107 style & wxCOLOURED ?
108 xmLabelWidgetClass : xmLabelGadgetClass,
109 formWidget,
a4294b78 110#else
af0bb3b1 111 xmLabelWidgetClass, formWidget,
a4294b78 112#endif
ea57084d 113 XmNfontList, fontList,
af0bb3b1
VZ
114 XmNlabelString, text,
115 NULL);
a4294b78 116
a4294b78
JS
117 XmStringFree (text);
118 }
119
9838df2c 120 Widget frameWidget = XtVaCreateManagedWidget ("frame",
af0bb3b1 121 xmFrameWidgetClass, formWidget,
9838df2c 122 XmNshadowType, XmSHADOW_IN,
af0bb3b1
VZ
123// XmNmarginHeight, 0,
124// XmNmarginWidth, 0,
125 NULL);
9838df2c
JS
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 ?
af0bb3b1 134 XmHORIZONTAL : XmVERTICAL));
a4294b78
JS
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)
af0bb3b1
VZ
142 XtVaSetValues ((Widget) m_labelWidget,
143 XmNtopAttachment, XmATTACH_FORM,
144 XmNleftAttachment, XmATTACH_FORM,
145 XmNalignment, XmALIGNMENT_BEGINNING,
146 NULL);
a4294b78
JS
147
148 XtVaSetValues (radioBoxWidget,
af0bb3b1
VZ
149 XmNtopAttachment, m_labelWidget ? XmATTACH_WIDGET : XmATTACH_FORM,
150 XmNtopWidget, m_labelWidget ? (Widget) m_labelWidget : formWidget,
151 XmNbottomAttachment, XmATTACH_FORM,
152 XmNleftAttachment, XmATTACH_FORM,
153 XmNrightAttachment, XmATTACH_FORM,
154 NULL);
a4294b78
JS
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
af0bb3b1 168 xmToggleButtonGadgetClass, radioBoxWidget,
a4294b78 169#else
af0bb3b1 170 xmToggleButtonWidgetClass, radioBoxWidget,
a4294b78 171#endif
ea57084d 172 XmNfontList, fontList,
af0bb3b1 173 NULL);
a4294b78 174 XtAddCallback ((Widget) m_radioButtons[i], XmNvalueChangedCallback, (XtCallbackProc) wxRadioBoxCallback,
af0bb3b1 175 (XtCallbackProc) this);
a4294b78 176
a4294b78
JS
177 }
178 SetSelection (0);
179
da175b2c 180 m_font = parent->GetFont();
4b5f3fe6
JS
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,
af0bb3b1
VZ
222 XmNlabelString, &text,
223 NULL);
a4294b78
JS
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,
af0bb3b1
VZ
251 XmNlabelString, text,
252 XmNlabelType, XmSTRING,
253 NULL);
a4294b78
JS
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,
af0bb3b1 312 XmNx, xx, NULL);
a4294b78
JS
313 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
314 XtVaSetValues ((Widget) m_formWidget, XmNtopAttachment, XmATTACH_SELF,
af0bb3b1 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
af0bb3b1 348bool wxRadioBox::Enable(bool enable)
4bb6408c 349{
af0bb3b1
VZ
350 if ( !wxControl::Enable(enable) )
351 return FALSE;
4bb6408c 352
a4294b78
JS
353 int i;
354 for (i = 0; i < m_noItems; i++)
355 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
af0bb3b1
VZ
356
357 return TRUE;
4bb6408c
JS
358}
359
9838df2c
JS
360bool wxRadioBox::Show(bool show)
361{
362 // TODO: show/hide all children
363 return wxControl::Show(show);
364}
365
4bb6408c 366// Show a specific button
a4294b78 367void wxRadioBox::Show(int n, bool show)
4bb6408c 368{
a4294b78
JS
369 // This method isn't complete, and we try do do our best...
370 // It's main purpose isn't for allowing Show/Unshow dynamically,
371 // but rather to provide a way to design wxRadioBox such:
372 //
373 // o Val1 o Val2 o Val3
374 // o Val4 o Val6
375 // o Val7 o Val8 o Val9
376 //
377 // In my case, this is a 'direction' box, and the Show(5,False) is
378 // coupled with an Enable(5,False)
379 //
380 if ((n < 0) || (n >= m_noItems))
381 return;
382
383 XtVaSetValues ((Widget) m_radioButtons[n],
384 XmNindicatorOn, (unsigned char) show,
385 NULL);
386
387 // Please note that this is all we can do: removing the label
388 // if switching to unshow state. However, when switching
389 // to the on state, it's the prog. resp. to call SetLabel(item,...)
390 // after this call!!
391 if (!show)
392 wxRadioBox::SetLabel (n, " ");
4bb6408c
JS
393}
394
395// For single selection items only
396wxString wxRadioBox::GetStringSelection () const
397{
398 int sel = GetSelection ();
399 if (sel > -1)
400 return this->GetString (sel);
401 else
402 return wxString("");
403}
404
405bool wxRadioBox::SetStringSelection (const wxString& s)
406{
407 int sel = FindString (s);
408 if (sel > -1)
409 {
410 SetSelection (sel);
411 return TRUE;
412 }
413 else
414 return FALSE;
415}
416
417void wxRadioBox::Command (wxCommandEvent & event)
418{
419 SetSelection (event.m_commandInt);
420 ProcessCommand (event);
421}
422
4b5f3fe6 423void wxRadioBox::ChangeFont(bool keepOriginalSize)
0d57be45 424{
94b49b93
JS
425 wxWindow::ChangeFont(keepOriginalSize);
426
da175b2c 427 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) GetTopWidget()));
94b49b93
JS
428
429 int i;
430 for (i = 0; i < m_noItems; i++)
431 {
432 WXWidget radioButton = m_radioButtons[i];
433
434 XtVaSetValues ((Widget) radioButton,
435 XmNfontList, fontList,
af0bb3b1 436 XmNtopAttachment, XmATTACH_FORM,
94b49b93
JS
437 NULL);
438 }
0d57be45
JS
439}
440
441void wxRadioBox::ChangeBackgroundColour()
442{
94b49b93
JS
443 wxWindow::ChangeBackgroundColour();
444
9838df2c
JS
445 DoChangeBackgroundColour((Widget) m_frameWidget, m_backgroundColour);
446
447 int selectPixel = wxBLACK->AllocColour(wxGetDisplay());
448
94b49b93
JS
449 int i;
450 for (i = 0; i < m_noItems; i++)
451 {
452 WXWidget radioButton = m_radioButtons[i];
453
454 DoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE);
9838df2c
JS
455
456 XtVaSetValues ((Widget) radioButton,
457 XmNselectColor, selectPixel,
458 NULL);
94b49b93 459 }
0d57be45
JS
460}
461
462void wxRadioBox::ChangeForegroundColour()
463{
94b49b93
JS
464 wxWindow::ChangeForegroundColour();
465
466 int i;
467 for (i = 0; i < m_noItems; i++)
468 {
469 WXWidget radioButton = m_radioButtons[i];
470
471 DoChangeForegroundColour(radioButton, m_foregroundColour);
472 }
0d57be45
JS
473}
474
a4294b78 475void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 476 XmToggleButtonCallbackStruct * cbs)
a4294b78
JS
477{
478 if (!cbs->set)
479 return;
480
481 wxRadioBox *item = (wxRadioBox *) clientData;
482 int sel = -1;
483 int i;
484 for (i = 0; i < item->Number(); i++)
485 if (item->GetRadioButtons() && ((Widget) (item->GetRadioButtons()[i]) == w))
486 sel = i;
487 item->SetSel(sel);
488
489 if (item->InSetValue())
490 return;
491
492 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
493 event.m_commandInt = sel;
494 event.SetEventObject(item);
495 item->ProcessCommand (event);
496}
4bb6408c 497