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