]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobox.cpp
& operator should be &&
[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"
a4294b78 22#include "wx/utils.h"
584ad2a3 23#include "wx/arrstr.h"
a4294b78 24
338dd992
JJ
25#ifdef __VMS__
26#pragma message disable nosimpint
27#endif
a4294b78
JS
28#include <Xm/Label.h>
29#include <Xm/LabelG.h>
30#include <Xm/ToggleB.h>
31#include <Xm/ToggleBG.h>
32#include <Xm/RowColumn.h>
9838df2c 33#include <Xm/Frame.h>
338dd992
JJ
34#ifdef __VMS__
35#pragma message enable nosimpint
36#endif
a4294b78 37
3096bd2f 38#include "wx/motif/private.h"
a4294b78
JS
39
40void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 41 XmToggleButtonCallbackStruct * cbs);
4bb6408c 42
4bb6408c 43IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
4bb6408c
JS
44
45// Radio box item
3bdb8629 46void wxRadioBox::Init()
4bb6408c
JS
47{
48 m_selectedButton = -1;
49 m_noItems = 0;
50 m_noRowsOrCols = 0;
4bb6408c
JS
51}
52
53bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
54 const wxPoint& pos, const wxSize& size,
55 int n, const wxString choices[],
56 int majorDim, long style,
57 const wxValidator& val, const wxString& name)
58{
3bdb8629
MB
59 if( !CreateControl( parent, id, pos, size, style, val, name ) )
60 return false;
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
2ad99bc1 76 wxString label1(wxStripMenuCodes(title));
a4294b78 77
1a87edf2 78 if (!label1.empty())
a4294b78 79 {
31528cd3 80 wxXmString text(label1);
3dd709d8
MB
81 m_labelWidget = (WXWidget)
82 XtVaCreateManagedWidget( label1.c_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 {
120 wxString str(wxStripMenuCodes(choices[i]));
3bdb8629 121 m_radioButtonLabels.push_back(str);
73608949
MB
122 Widget radioItem = XtVaCreateManagedWidget (
123 wxConstCast(str.c_str(), char),
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
96be256b 137 ChangeFont(false);
4b5f3fe6 138
2ad99bc1 139 SetSelection (0);
1a87edf2 140
2ad99bc1 141 XtRealizeWidget((Widget)m_mainWidget);
a4294b78 142 XtManageChild (radioBoxWidget);
2ad99bc1 143 XtManageChild ((Widget)m_mainWidget);
a4294b78 144
2ad99bc1 145 AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y);
a4294b78 146
0d57be45 147 ChangeBackgroundColour();
a4294b78 148
96be256b 149 return true;
4bb6408c
JS
150}
151
584ad2a3
MB
152bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
153 const wxPoint& pos, const wxSize& size,
154 const wxArrayString& choices,
155 int majorDim, long style,
156 const wxValidator& val, const wxString& name)
157{
158 wxCArrayString chs(choices);
159 return Create(parent, id, title, pos, size, chs.GetCount(),
160 chs.GetStrings(), majorDim, style, val, name);
161}
4bb6408c
JS
162
163wxRadioBox::~wxRadioBox()
164{
15d5ab67 165 DetachWidget(m_mainWidget);
15d5ab67 166 XtDestroyWidget((Widget) m_mainWidget);
15d5ab67
JS
167
168 m_mainWidget = (WXWidget) 0;
4bb6408c
JS
169}
170
aa61d352 171void wxRadioBox::SetString(unsigned int item, const wxString& label)
4bb6408c 172{
789f6795 173 if (!IsValid(item))
a4294b78
JS
174 return;
175
aa61d352 176 Widget widget = (Widget)m_radioButtons[item];
1a87edf2 177 if (!label.empty())
a4294b78
JS
178 {
179 wxString label1(wxStripMenuCodes(label));
da494b40 180 wxXmString text( label1 );
3bdb8629 181 m_radioButtonLabels[item] = label1;
a4294b78 182 XtVaSetValues (widget,
da494b40 183 XmNlabelString, text(),
af0bb3b1
VZ
184 XmNlabelType, XmSTRING,
185 NULL);
a4294b78 186 }
4bb6408c
JS
187}
188
4bb6408c
JS
189void wxRadioBox::SetSelection(int n)
190{
789f6795 191 if (!IsValid(n))
4bb6408c 192 return;
4bb6408c
JS
193
194 m_selectedButton = n;
a4294b78 195
96be256b 196 m_inSetValue = true;
a4294b78 197
96be256b 198 XmToggleButtonSetState ((Widget) m_radioButtons[n], True, False);
a4294b78 199
aa61d352
VZ
200 for (unsigned int i = 0; i < m_noItems; i++)
201 if (i != (unsigned int)n)
96be256b 202 XmToggleButtonSetState ((Widget) m_radioButtons[i], False, False);
a4294b78 203
96be256b 204 m_inSetValue = false;
4bb6408c
JS
205}
206
207// Get single selection, for single choice list items
208int wxRadioBox::GetSelection() const
209{
210 return m_selectedButton;
211}
212
213// Find string for position
aa61d352 214wxString wxRadioBox::GetString(unsigned int n) const
4bb6408c 215{
789f6795 216 if (!IsValid(n))
a4294b78
JS
217 return wxEmptyString;
218 return m_radioButtonLabels[n];
4bb6408c
JS
219}
220
bfc6fde4 221void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 222{
2ad99bc1 223 bool managed = XtIsManaged((Widget) m_mainWidget);
4bb6408c 224
a4294b78 225 if (managed)
2ad99bc1 226 XtUnmanageChild ((Widget) m_mainWidget);
4bb6408c 227
a4294b78
JS
228 int xx = x; int yy = y;
229 AdjustForParentClientOrigin(xx, yy, sizeFlags);
4bb6408c 230
a4294b78 231 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2ad99bc1 232 XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL);
a4294b78 233 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
1a87edf2 234 XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL);
4bb6408c 235
2ad99bc1
JS
236 if (width > 0)
237 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL);
238 if (height > 0)
239 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL);
a4294b78 240
a4294b78 241 if (managed)
2ad99bc1 242 XtManageChild ((Widget) m_mainWidget);
4bb6408c
JS
243}
244
245// Enable a specific button
aa61d352 246bool wxRadioBox::Enable(unsigned int n, bool enable)
4bb6408c 247{
789f6795 248 if (!IsValid(n))
1a87edf2 249 return false;
a4294b78
JS
250
251 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
1a87edf2 252 return true;
4bb6408c
JS
253}
254
255// Enable all controls
af0bb3b1 256bool wxRadioBox::Enable(bool enable)
4bb6408c 257{
af0bb3b1 258 if ( !wxControl::Enable(enable) )
96be256b 259 return false;
4bb6408c 260
aa61d352 261 for (unsigned int i = 0; i < m_noItems; i++)
a4294b78 262 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
af0bb3b1 263
96be256b 264 return true;
4bb6408c
JS
265}
266
9838df2c
JS
267bool wxRadioBox::Show(bool show)
268{
269 // TODO: show/hide all children
270 return wxControl::Show(show);
271}
272
4bb6408c 273// Show a specific button
aa61d352 274bool wxRadioBox::Show(unsigned int n, bool show)
4bb6408c 275{
a4294b78
JS
276 // This method isn't complete, and we try do do our best...
277 // It's main purpose isn't for allowing Show/Unshow dynamically,
278 // but rather to provide a way to design wxRadioBox such:
279 //
1a87edf2
WS
280 // o Val1 o Val2 o Val3
281 // o Val4 o Val6
282 // o Val7 o Val8 o Val9
a4294b78
JS
283 //
284 // In my case, this is a 'direction' box, and the Show(5,False) is
285 // coupled with an Enable(5,False)
286 //
789f6795
WS
287 if (!IsValid(n))
288 return false;
a4294b78
JS
289
290 XtVaSetValues ((Widget) m_radioButtons[n],
291 XmNindicatorOn, (unsigned char) show,
292 NULL);
293
294 // Please note that this is all we can do: removing the label
295 // if switching to unshow state. However, when switching
18128cbb 296 // to the on state, it's the prog. resp. to call SetString(item,...)
a4294b78
JS
297 // after this call!!
298 if (!show)
18128cbb 299 wxRadioBox::SetString (n, " ");
789f6795
WS
300
301 return true;
4bb6408c
JS
302}
303
304// For single selection items only
305wxString wxRadioBox::GetStringSelection () const
306{
307 int sel = GetSelection ();
aa61d352
VZ
308 if (sel != wxNOT_FOUND)
309 return this->GetString((unsigned int)sel);
4bb6408c 310 else
1a87edf2 311 return wxEmptyString;
4bb6408c
JS
312}
313
314bool wxRadioBox::SetStringSelection (const wxString& s)
315{
316 int sel = FindString (s);
317 if (sel > -1)
318 {
319 SetSelection (sel);
96be256b 320 return true;
4bb6408c
JS
321 }
322 else
96be256b 323 return false;
4bb6408c
JS
324}
325
326void wxRadioBox::Command (wxCommandEvent & event)
327{
687706f5 328 SetSelection (event.GetInt());
4bb6408c
JS
329 ProcessCommand (event);
330}
331
4b5f3fe6 332void wxRadioBox::ChangeFont(bool keepOriginalSize)
0d57be45 333{
94b49b93
JS
334 wxWindow::ChangeFont(keepOriginalSize);
335
aa61d352 336 for (unsigned int i = 0; i < m_noItems; i++)
94b49b93
JS
337 {
338 WXWidget radioButton = m_radioButtons[i];
339
340 XtVaSetValues ((Widget) radioButton,
73608949 341 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay((Widget) GetTopWidget())),
94b49b93
JS
342 NULL);
343 }
0d57be45
JS
344}
345
346void wxRadioBox::ChangeBackgroundColour()
347{
94b49b93
JS
348 wxWindow::ChangeBackgroundColour();
349
f516d986
VZ
350 wxColour colour = *wxBLACK;
351 int selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c 352
aa61d352 353 for (unsigned int i = 0; i < m_noItems; i++)
94b49b93
JS
354 {
355 WXWidget radioButton = m_radioButtons[i];
356
96be256b 357 wxDoChangeBackgroundColour(radioButton, m_backgroundColour, true);
9838df2c
JS
358
359 XtVaSetValues ((Widget) radioButton,
360 XmNselectColor, selectPixel,
361 NULL);
94b49b93 362 }
0d57be45
JS
363}
364
365void wxRadioBox::ChangeForegroundColour()
366{
94b49b93
JS
367 wxWindow::ChangeForegroundColour();
368
aa61d352 369 for (unsigned int i = 0; i < m_noItems; i++)
94b49b93
JS
370 {
371 WXWidget radioButton = m_radioButtons[i];
372
a8680e3e 373 wxDoChangeForegroundColour(radioButton, m_foregroundColour);
94b49b93 374 }
0d57be45
JS
375}
376
a4294b78 377void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 378 XmToggleButtonCallbackStruct * cbs)
a4294b78
JS
379{
380 if (!cbs->set)
381 return;
382
383 wxRadioBox *item = (wxRadioBox *) clientData;
384 int sel = -1;
aa61d352 385 unsigned int i;
3bdb8629 386 const wxWidgetArray& buttons = item->GetRadioButtons();
3c85ada9 387 for (i = 0; i < item->GetCount(); i++)
3bdb8629 388 if (((Widget)buttons[i]) == w)
36e14c2b 389 sel = (int)i;
a4294b78
JS
390 item->SetSel(sel);
391
392 if (item->InSetValue())
393 return;
394
395 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
2ad99bc1
JS
396 event.SetInt(sel);
397 event.SetString(item->GetStringSelection());
a4294b78
JS
398 event.SetEventObject(item);
399 item->ProcessCommand (event);
400}
8228b893
WS
401
402#endif // wxUSE_RADIOBOX