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