]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobox.cpp
VisualAge C++ V4.0 configuration files
[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
31528cd3 91 Widget formWidget = XtVaCreateManagedWidget (name.c_str(),
af0bb3b1
VZ
92 xmFormWidgetClass, parentWidget,
93 XmNmarginHeight, 0,
94 XmNmarginWidth, 0,
95 NULL);
a4294b78
JS
96
97 m_formWidget = (WXWidget) formWidget;
98
da175b2c 99 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget));
a4294b78
JS
100 if (label1 != "")
101 {
31528cd3
VZ
102 wxXmString text(label1);
103 (void)XtVaCreateManagedWidget(label1.c_str(),
a4294b78 104#if wxUSE_GADGETS
31528cd3
VZ
105 style & wxCOLOURED ? xmLabelWidgetClass
106 : xmLabelGadgetClass,
af0bb3b1 107 formWidget,
a4294b78 108#else
af0bb3b1 109 xmLabelWidgetClass, formWidget,
a4294b78 110#endif
ea57084d 111 XmNfontList, fontList,
98300330 112 XmNlabelString, text(),
af0bb3b1 113 NULL);
a4294b78
JS
114 }
115
9838df2c 116 Widget frameWidget = XtVaCreateManagedWidget ("frame",
af0bb3b1 117 xmFrameWidgetClass, formWidget,
9838df2c 118 XmNshadowType, XmSHADOW_IN,
af0bb3b1
VZ
119// XmNmarginHeight, 0,
120// XmNmarginWidth, 0,
121 NULL);
9838df2c
JS
122
123 m_frameWidget = (WXWidget) frameWidget;
124
a4294b78
JS
125 Arg args[3];
126
978db70e 127 m_majorDim = (n + m_majorDim - 1) / m_majorDim;
a4294b78
JS
128
129 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
af0bb3b1 130 XmHORIZONTAL : XmVERTICAL));
98300330 131 XtSetArg (args[1], XmNnumColumns, m_majorDim);
a4294b78 132
9838df2c 133 Widget radioBoxWidget = XmCreateRadioBox (frameWidget, "radioBoxWidget", args, 2);
a4294b78 134 m_mainWidget = (WXWidget) radioBoxWidget;
4bb6408c 135
a4294b78
JS
136
137 if (m_labelWidget)
af0bb3b1
VZ
138 XtVaSetValues ((Widget) m_labelWidget,
139 XmNtopAttachment, XmATTACH_FORM,
140 XmNleftAttachment, XmATTACH_FORM,
141 XmNalignment, XmALIGNMENT_BEGINNING,
142 NULL);
a4294b78
JS
143
144 XtVaSetValues (radioBoxWidget,
af0bb3b1
VZ
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);
a4294b78
JS
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
af0bb3b1 164 xmToggleButtonGadgetClass, radioBoxWidget,
a4294b78 165#else
af0bb3b1 166 xmToggleButtonWidgetClass, radioBoxWidget,
a4294b78 167#endif
ea57084d 168 XmNfontList, fontList,
af0bb3b1 169 NULL);
a4294b78 170 XtAddCallback ((Widget) m_radioButtons[i], XmNvalueChangedCallback, (XtCallbackProc) wxRadioBoxCallback,
af0bb3b1 171 (XtCallbackProc) this);
a4294b78 172
a4294b78
JS
173 }
174 SetSelection (0);
175
da175b2c 176 m_font = parent->GetFont();
4b5f3fe6
JS
177 ChangeFont(FALSE);
178
9838df2c 179 // XtManageChild((Widget) m_formWidget);
a4294b78
JS
180 XtManageChild (radioBoxWidget);
181
182 SetCanAddEventHandler(TRUE);
183 AttachWidget (parent, m_mainWidget, m_formWidget, pos.x, pos.y, size.x, size.y);
184
0d57be45 185 ChangeBackgroundColour();
a4294b78
JS
186
187 return TRUE;
4bb6408c
JS
188}
189
190
191wxRadioBox::~wxRadioBox()
192{
a4294b78
JS
193 delete[] m_radioButtonLabels;
194 delete[] m_radioButtons;
15d5ab67 195
b412f9be 196 DetachWidget(m_formWidget);
15d5ab67
JS
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;
4bb6408c
JS
207}
208
209wxString wxRadioBox::GetLabel(int item) const
210{
a4294b78
JS
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,
af0bb3b1
VZ
218 XmNlabelString, &text,
219 NULL);
a4294b78
JS
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 }
4bb6408c
JS
234}
235
236void wxRadioBox::SetLabel(int item, const wxString& label)
237{
a4294b78
JS
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,
af0bb3b1
VZ
247 XmNlabelString, text,
248 XmNlabelType, XmSTRING,
249 NULL);
a4294b78
JS
250 XmStringFree (text);
251 }
4bb6408c
JS
252}
253
254int wxRadioBox::FindString(const wxString& s) const
255{
a4294b78
JS
256 int i;
257 for (i = 0; i < m_noItems; i++)
258 if (s == m_radioButtonLabels[i])
259 return i;
4bb6408c
JS
260 return -1;
261}
262
263void wxRadioBox::SetSelection(int n)
264{
265 if ((n < 0) || (n >= m_noItems))
266 return;
4bb6408c
JS
267
268 m_selectedButton = n;
a4294b78
JS
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;
4bb6408c
JS
280}
281
282// Get single selection, for single choice list items
283int wxRadioBox::GetSelection() const
284{
285 return m_selectedButton;
286}
287
288// Find string for position
289wxString wxRadioBox::GetString(int n) const
290{
a4294b78
JS
291 if ((n < 0) || (n >= m_noItems))
292 return wxEmptyString;
293 return m_radioButtonLabels[n];
4bb6408c
JS
294}
295
bfc6fde4 296void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 297{
a4294b78 298 bool managed = XtIsManaged((Widget) m_formWidget);
4bb6408c 299
a4294b78
JS
300 if (managed)
301 XtUnmanageChild ((Widget) m_formWidget);
4bb6408c 302
a4294b78
JS
303 int xx = x; int yy = y;
304 AdjustForParentClientOrigin(xx, yy, sizeFlags);
4bb6408c 305
a4294b78
JS
306 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
307 XtVaSetValues ((Widget) m_formWidget, XmNleftAttachment, XmATTACH_SELF,
af0bb3b1 308 XmNx, xx, NULL);
a4294b78
JS
309 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
310 XtVaSetValues ((Widget) m_formWidget, XmNtopAttachment, XmATTACH_SELF,
af0bb3b1 311 XmNy, yy, NULL);
4bb6408c 312
a4294b78
JS
313 // Must set the actual RadioBox to be desired size MINUS label size
314 Dimension labelWidth = 0, labelHeight = 0, actualWidth = 0, actualHeight = 0;
4bb6408c 315
a4294b78
JS
316 if (m_labelWidget)
317 XtVaGetValues ((Widget) m_labelWidget, XmNwidth, &labelWidth, XmNheight, &labelHeight, NULL);
4bb6408c 318
a4294b78
JS
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);
4bb6408c
JS
332}
333
334// Enable a specific button
a4294b78 335void wxRadioBox::Enable(int n, bool enable)
4bb6408c 336{
a4294b78
JS
337 if ((n < 0) || (n >= m_noItems))
338 return;
339
340 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
4bb6408c
JS
341}
342
343// Enable all controls
af0bb3b1 344bool wxRadioBox::Enable(bool enable)
4bb6408c 345{
af0bb3b1
VZ
346 if ( !wxControl::Enable(enable) )
347 return FALSE;
4bb6408c 348
a4294b78
JS
349 int i;
350 for (i = 0; i < m_noItems; i++)
351 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
af0bb3b1
VZ
352
353 return TRUE;
4bb6408c
JS
354}
355
9838df2c
JS
356bool wxRadioBox::Show(bool show)
357{
358 // TODO: show/hide all children
359 return wxControl::Show(show);
360}
361
4bb6408c 362// Show a specific button
a4294b78 363void wxRadioBox::Show(int n, bool show)
4bb6408c 364{
a4294b78
JS
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, " ");
4bb6408c
JS
389}
390
391// For single selection items only
392wxString wxRadioBox::GetStringSelection () const
393{
394 int sel = GetSelection ();
395 if (sel > -1)
396 return this->GetString (sel);
397 else
398 return wxString("");
399}
400
401bool 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
413void wxRadioBox::Command (wxCommandEvent & event)
414{
415 SetSelection (event.m_commandInt);
416 ProcessCommand (event);
417}
418
4b5f3fe6 419void wxRadioBox::ChangeFont(bool keepOriginalSize)
0d57be45 420{
94b49b93
JS
421 wxWindow::ChangeFont(keepOriginalSize);
422
da175b2c 423 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) GetTopWidget()));
94b49b93
JS
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,
af0bb3b1 432 XmNtopAttachment, XmATTACH_FORM,
94b49b93
JS
433 NULL);
434 }
0d57be45
JS
435}
436
437void wxRadioBox::ChangeBackgroundColour()
438{
94b49b93
JS
439 wxWindow::ChangeBackgroundColour();
440
9838df2c
JS
441 DoChangeBackgroundColour((Widget) m_frameWidget, m_backgroundColour);
442
443 int selectPixel = wxBLACK->AllocColour(wxGetDisplay());
444
94b49b93
JS
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);
9838df2c
JS
451
452 XtVaSetValues ((Widget) radioButton,
453 XmNselectColor, selectPixel,
454 NULL);
94b49b93 455 }
0d57be45
JS
456}
457
458void wxRadioBox::ChangeForegroundColour()
459{
94b49b93
JS
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 }
0d57be45
JS
469}
470
a4294b78 471void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 472 XmToggleButtonCallbackStruct * cbs)
a4294b78
JS
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}
4bb6408c 493