]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobox.cpp
Fix for always failing assert in GetValue. Save the same value that
[wxWidgets.git] / src / motif / radiobox.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/motif/radiobox.cpp
4bb6408c
JS
3// Purpose: wxRadioBox
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
8228b893
WS
15#if wxUSE_RADIOBOX
16
bcd055ae
JJ
17#ifdef __VMS
18#define XtDisplay XTDISPLAY
19#endif
20
4bb6408c 21#include "wx/radiobox.h"
de6185e2
WS
22
23#ifndef WX_PRECOMP
24 #include "wx/utils.h"
aaa6d89a 25 #include "wx/arrstr.h"
de6185e2
WS
26#endif
27
338dd992
JJ
28#ifdef __VMS__
29#pragma message disable nosimpint
30#endif
a4294b78
JS
31#include <Xm/Label.h>
32#include <Xm/LabelG.h>
33#include <Xm/ToggleB.h>
34#include <Xm/ToggleBG.h>
35#include <Xm/RowColumn.h>
9838df2c 36#include <Xm/Frame.h>
338dd992
JJ
37#ifdef __VMS__
38#pragma message enable nosimpint
39#endif
a4294b78 40
3096bd2f 41#include "wx/motif/private.h"
a4294b78
JS
42
43void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 44 XmToggleButtonCallbackStruct * cbs);
4bb6408c 45
4bb6408c 46IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
4bb6408c
JS
47
48// Radio box item
3bdb8629 49void wxRadioBox::Init()
4bb6408c
JS
50{
51 m_selectedButton = -1;
52 m_noItems = 0;
53 m_noRowsOrCols = 0;
4bb6408c
JS
54}
55
56bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
57 const wxPoint& pos, const wxSize& size,
58 int n, const wxString choices[],
59 int majorDim, long style,
60 const wxValidator& val, const wxString& name)
61{
3bdb8629
MB
62 if( !CreateControl( parent, id, pos, size, style, val, name ) )
63 return false;
4bb6408c 64
aa61d352 65 m_noItems = (unsigned int)n;
4bb6408c
JS
66 m_noRowsOrCols = majorDim;
67
21e0a4d5 68 SetMajorDim(majorDim == 0 ? n : majorDim, style);
4bb6408c 69
a4294b78 70 Widget parentWidget = (Widget) parent->GetClientWidget();
73608949 71 Display* dpy = XtDisplay(parentWidget);
a4294b78 72
34774093 73 m_mainWidget = XtVaCreateWidget ("radioboxframe",
73608949
MB
74 xmFrameWidgetClass, parentWidget,
75 XmNresizeHeight, True,
76 XmNresizeWidth, True,
77 NULL);
a4294b78 78
32cd189d 79 wxString label1(GetLabelText(title));
a4294b78 80
1a87edf2 81 if (!label1.empty())
a4294b78 82 {
31528cd3 83 wxXmString text(label1);
3dd709d8
MB
84 m_labelWidget = (WXWidget)
85 XtVaCreateManagedWidget( label1.c_str(),
a4294b78 86#if wxUSE_GADGETS
73608949
MB
87 style & wxCOLOURED ? xmLabelWidgetClass
88 : xmLabelGadgetClass,
89 (Widget)m_mainWidget,
a4294b78 90#else
73608949 91 xmLabelWidgetClass, (Widget)m_mainWidget,
a4294b78 92#endif
73608949
MB
93 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
94 XmNlabelString, text(),
11a2ce5a
MB
95// XmNframeChildType is not in Motif 1.2, nor in Lesstif,
96// if it was compiled with 1.2 compatibility
34774093 97// TODO: check this still looks OK for Motif 1.2.
11a2ce5a 98#if (XmVersion > 1200)
73608949 99 XmNframeChildType, XmFRAME_TITLE_CHILD,
2b5f62a0 100#else
73608949 101 XmNchildType, XmFRAME_TITLE_CHILD,
34774093 102#endif
73608949
MB
103 XmNchildVerticalAlignment, XmALIGNMENT_CENTER,
104 NULL);
a4294b78
JS
105 }
106
107 Arg args[3];
108
a4294b78 109 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
af0bb3b1 110 XmHORIZONTAL : XmVERTICAL));
21e0a4d5 111 XtSetArg (args[1], XmNnumColumns, GetMajorDim());
795c00bd 112 XtSetArg (args[2], XmNadjustLast, False);
a4294b78 113
795c00bd 114 Widget radioBoxWidget =
da8cf723 115 XmCreateRadioBox ((Widget)m_mainWidget, wxMOTIF_STR("radioBoxWidget"), args, 3);
a4294b78 116
3bdb8629
MB
117 m_radioButtons.reserve(n);
118 m_radioButtonLabels.reserve(n);
a4294b78 119
a4294b78
JS
120 int i;
121 for (i = 0; i < n; i++)
122 {
32cd189d 123 wxString str(GetLabelText(choices[i]));
3bdb8629 124 m_radioButtonLabels.push_back(str);
73608949
MB
125 Widget radioItem = XtVaCreateManagedWidget (
126 wxConstCast(str.c_str(), char),
a4294b78 127#if wxUSE_GADGETS
73608949 128 xmToggleButtonGadgetClass, radioBoxWidget,
a4294b78 129#else
73608949 130 xmToggleButtonWidgetClass, radioBoxWidget,
a4294b78 131#endif
73608949
MB
132 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
133 NULL);
3bdb8629
MB
134 m_radioButtons.push_back((WXWidget)radioItem);
135 XtAddCallback (radioItem, XmNvalueChangedCallback,
136 (XtCallbackProc) wxRadioBoxCallback,
137 (XtPointer) this);
a4294b78 138 }
a4294b78 139
96be256b 140 ChangeFont(false);
4b5f3fe6 141
2ad99bc1 142 SetSelection (0);
1a87edf2 143
2ad99bc1 144 XtRealizeWidget((Widget)m_mainWidget);
a4294b78 145 XtManageChild (radioBoxWidget);
2ad99bc1 146 XtManageChild ((Widget)m_mainWidget);
a4294b78 147
2ad99bc1 148 AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y);
a4294b78 149
0d57be45 150 ChangeBackgroundColour();
a4294b78 151
96be256b 152 return true;
4bb6408c
JS
153}
154
584ad2a3
MB
155bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
156 const wxPoint& pos, const wxSize& size,
157 const wxArrayString& choices,
158 int majorDim, long style,
159 const wxValidator& val, const wxString& name)
160{
161 wxCArrayString chs(choices);
162 return Create(parent, id, title, pos, size, chs.GetCount(),
163 chs.GetStrings(), majorDim, style, val, name);
164}
4bb6408c
JS
165
166wxRadioBox::~wxRadioBox()
167{
15d5ab67 168 DetachWidget(m_mainWidget);
15d5ab67 169 XtDestroyWidget((Widget) m_mainWidget);
15d5ab67
JS
170
171 m_mainWidget = (WXWidget) 0;
4bb6408c
JS
172}
173
aa61d352 174void wxRadioBox::SetString(unsigned int item, const wxString& label)
4bb6408c 175{
789f6795 176 if (!IsValid(item))
a4294b78
JS
177 return;
178
aa61d352 179 Widget widget = (Widget)m_radioButtons[item];
1a87edf2 180 if (!label.empty())
a4294b78 181 {
32cd189d 182 wxString label1(GetLabelText(label));
da494b40 183 wxXmString text( label1 );
3bdb8629 184 m_radioButtonLabels[item] = label1;
a4294b78 185 XtVaSetValues (widget,
da494b40 186 XmNlabelString, text(),
af0bb3b1
VZ
187 XmNlabelType, XmSTRING,
188 NULL);
a4294b78 189 }
4bb6408c
JS
190}
191
4bb6408c
JS
192void wxRadioBox::SetSelection(int n)
193{
789f6795 194 if (!IsValid(n))
4bb6408c 195 return;
4bb6408c
JS
196
197 m_selectedButton = n;
a4294b78 198
96be256b 199 m_inSetValue = true;
a4294b78 200
96be256b 201 XmToggleButtonSetState ((Widget) m_radioButtons[n], True, False);
a4294b78 202
aa61d352
VZ
203 for (unsigned int i = 0; i < m_noItems; i++)
204 if (i != (unsigned int)n)
96be256b 205 XmToggleButtonSetState ((Widget) m_radioButtons[i], False, False);
a4294b78 206
96be256b 207 m_inSetValue = false;
4bb6408c
JS
208}
209
210// Get single selection, for single choice list items
211int wxRadioBox::GetSelection() const
212{
213 return m_selectedButton;
214}
215
216// Find string for position
aa61d352 217wxString wxRadioBox::GetString(unsigned int n) const
4bb6408c 218{
789f6795 219 if (!IsValid(n))
a4294b78
JS
220 return wxEmptyString;
221 return m_radioButtonLabels[n];
4bb6408c
JS
222}
223
bfc6fde4 224void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 225{
2ad99bc1 226 bool managed = XtIsManaged((Widget) m_mainWidget);
4bb6408c 227
a4294b78 228 if (managed)
2ad99bc1 229 XtUnmanageChild ((Widget) m_mainWidget);
4bb6408c 230
a4294b78
JS
231 int xx = x; int yy = y;
232 AdjustForParentClientOrigin(xx, yy, sizeFlags);
4bb6408c 233
a4294b78 234 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2ad99bc1 235 XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL);
a4294b78 236 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
1a87edf2 237 XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL);
4bb6408c 238
2ad99bc1
JS
239 if (width > 0)
240 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL);
241 if (height > 0)
242 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL);
a4294b78 243
a4294b78 244 if (managed)
2ad99bc1 245 XtManageChild ((Widget) m_mainWidget);
4bb6408c
JS
246}
247
248// Enable a specific button
aa61d352 249bool wxRadioBox::Enable(unsigned int n, bool enable)
4bb6408c 250{
789f6795 251 if (!IsValid(n))
1a87edf2 252 return false;
a4294b78
JS
253
254 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
1a87edf2 255 return true;
4bb6408c
JS
256}
257
258// Enable all controls
af0bb3b1 259bool wxRadioBox::Enable(bool enable)
4bb6408c 260{
af0bb3b1 261 if ( !wxControl::Enable(enable) )
96be256b 262 return false;
4bb6408c 263
aa61d352 264 for (unsigned int i = 0; i < m_noItems; i++)
a4294b78 265 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
af0bb3b1 266
96be256b 267 return true;
4bb6408c
JS
268}
269
9838df2c
JS
270bool wxRadioBox::Show(bool show)
271{
272 // TODO: show/hide all children
273 return wxControl::Show(show);
274}
275
4bb6408c 276// Show a specific button
aa61d352 277bool wxRadioBox::Show(unsigned int n, bool show)
4bb6408c 278{
a4294b78
JS
279 // This method isn't complete, and we try do do our best...
280 // It's main purpose isn't for allowing Show/Unshow dynamically,
281 // but rather to provide a way to design wxRadioBox such:
282 //
1a87edf2
WS
283 // o Val1 o Val2 o Val3
284 // o Val4 o Val6
285 // o Val7 o Val8 o Val9
a4294b78
JS
286 //
287 // In my case, this is a 'direction' box, and the Show(5,False) is
288 // coupled with an Enable(5,False)
289 //
789f6795
WS
290 if (!IsValid(n))
291 return false;
a4294b78
JS
292
293 XtVaSetValues ((Widget) m_radioButtons[n],
294 XmNindicatorOn, (unsigned char) show,
295 NULL);
296
297 // Please note that this is all we can do: removing the label
298 // if switching to unshow state. However, when switching
18128cbb 299 // to the on state, it's the prog. resp. to call SetString(item,...)
a4294b78
JS
300 // after this call!!
301 if (!show)
18128cbb 302 wxRadioBox::SetString (n, " ");
789f6795
WS
303
304 return true;
4bb6408c
JS
305}
306
307// For single selection items only
308wxString wxRadioBox::GetStringSelection () const
309{
310 int sel = GetSelection ();
aa61d352
VZ
311 if (sel != wxNOT_FOUND)
312 return this->GetString((unsigned int)sel);
4bb6408c 313 else
1a87edf2 314 return wxEmptyString;
4bb6408c
JS
315}
316
317bool wxRadioBox::SetStringSelection (const wxString& s)
318{
319 int sel = FindString (s);
320 if (sel > -1)
321 {
322 SetSelection (sel);
96be256b 323 return true;
4bb6408c
JS
324 }
325 else
96be256b 326 return false;
4bb6408c
JS
327}
328
329void wxRadioBox::Command (wxCommandEvent & event)
330{
687706f5 331 SetSelection (event.GetInt());
4bb6408c
JS
332 ProcessCommand (event);
333}
334
4b5f3fe6 335void wxRadioBox::ChangeFont(bool keepOriginalSize)
0d57be45 336{
94b49b93
JS
337 wxWindow::ChangeFont(keepOriginalSize);
338
aa61d352 339 for (unsigned int i = 0; i < m_noItems; i++)
94b49b93
JS
340 {
341 WXWidget radioButton = m_radioButtons[i];
342
343 XtVaSetValues ((Widget) radioButton,
73608949 344 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay((Widget) GetTopWidget())),
94b49b93
JS
345 NULL);
346 }
0d57be45
JS
347}
348
349void wxRadioBox::ChangeBackgroundColour()
350{
94b49b93
JS
351 wxWindow::ChangeBackgroundColour();
352
f516d986 353 wxColour colour = *wxBLACK;
3e0071d9 354 WXPixel selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c 355
aa61d352 356 for (unsigned int i = 0; i < m_noItems; i++)
94b49b93
JS
357 {
358 WXWidget radioButton = m_radioButtons[i];
359
96be256b 360 wxDoChangeBackgroundColour(radioButton, m_backgroundColour, true);
9838df2c
JS
361
362 XtVaSetValues ((Widget) radioButton,
363 XmNselectColor, selectPixel,
364 NULL);
94b49b93 365 }
0d57be45
JS
366}
367
368void wxRadioBox::ChangeForegroundColour()
369{
94b49b93
JS
370 wxWindow::ChangeForegroundColour();
371
aa61d352 372 for (unsigned int i = 0; i < m_noItems; i++)
94b49b93
JS
373 {
374 WXWidget radioButton = m_radioButtons[i];
375
a8680e3e 376 wxDoChangeForegroundColour(radioButton, m_foregroundColour);
94b49b93 377 }
0d57be45
JS
378}
379
a4294b78 380void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 381 XmToggleButtonCallbackStruct * cbs)
a4294b78
JS
382{
383 if (!cbs->set)
384 return;
385
386 wxRadioBox *item = (wxRadioBox *) clientData;
387 int sel = -1;
aa61d352 388 unsigned int i;
3bdb8629 389 const wxWidgetArray& buttons = item->GetRadioButtons();
3c85ada9 390 for (i = 0; i < item->GetCount(); i++)
3bdb8629 391 if (((Widget)buttons[i]) == w)
36e14c2b 392 sel = (int)i;
a4294b78
JS
393 item->SetSel(sel);
394
395 if (item->InSetValue())
396 return;
397
398 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
2ad99bc1
JS
399 event.SetInt(sel);
400 event.SetString(item->GetStringSelection());
a4294b78
JS
401 event.SetEventObject(item);
402 item->ProcessCommand (event);
403}
8228b893
WS
404
405#endif // wxUSE_RADIOBOX