]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobox.cpp
Applied patch [ 549256 ] fix minor bug in widgets sample
[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
f6045f99
GD
20#include "wx/defs.h"
21
4bb6408c 22#include "wx/radiobox.h"
a4294b78
JS
23#include "wx/utils.h"
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>
33#include <Xm/Form.h>
9838df2c 34#include <Xm/Frame.h>
338dd992
JJ
35#ifdef __VMS__
36#pragma message enable nosimpint
37#endif
a4294b78 38
3096bd2f 39#include "wx/motif/private.h"
a4294b78
JS
40
41void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 42 XmToggleButtonCallbackStruct * cbs);
4bb6408c 43
4bb6408c 44IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
4bb6408c
JS
45
46// Radio box item
47wxRadioBox::wxRadioBox()
48{
49 m_selectedButton = -1;
50 m_noItems = 0;
51 m_noRowsOrCols = 0;
52 m_majorDim = 0 ;
a4294b78 53
a4294b78
JS
54 m_radioButtons = (WXWidget*) NULL;
55 m_radioButtonLabels = (wxString*) NULL;
4bb6408c
JS
56}
57
58bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
59 const wxPoint& pos, const wxSize& size,
60 int n, const wxString choices[],
61 int majorDim, long style,
62 const wxValidator& val, const wxString& name)
63{
64 m_selectedButton = -1;
65 m_noItems = n;
a4294b78
JS
66 m_radioButtons = (WXWidget*) NULL;
67 m_radioButtonLabels = (wxString*) NULL;
0d57be45
JS
68 m_backgroundColour = parent->GetBackgroundColour();
69 m_foregroundColour = parent->GetForegroundColour();
da175b2c 70 m_font = parent->GetFont();
4bb6408c
JS
71
72 SetName(name);
73 SetValidator(val);
74
75 parent->AddChild(this);
76
77 m_windowStyle = (long&)style;
78
79 if (id == -1)
80 m_windowId = NewControlId();
81 else
82 m_windowId = id;
83
84 m_noRowsOrCols = majorDim;
85
86 if (majorDim==0)
87 m_majorDim = n ;
88 else
89 m_majorDim = majorDim ;
90
a4294b78
JS
91 Widget parentWidget = (Widget) parent->GetClientWidget();
92
34774093 93 m_mainWidget = XtVaCreateWidget ("radioboxframe",
2ad99bc1
JS
94 xmFrameWidgetClass, parentWidget,
95 XmNshadowType, XmSHADOW_IN,
96 XmNresizeHeight, True,
97 XmNresizeWidth, True,
af0bb3b1 98 NULL);
a4294b78 99
2ad99bc1 100 wxString label1(wxStripMenuCodes(title));
a4294b78 101
da175b2c 102 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget));
2ad99bc1 103
a4294b78
JS
104 if (label1 != "")
105 {
31528cd3
VZ
106 wxXmString text(label1);
107 (void)XtVaCreateManagedWidget(label1.c_str(),
a4294b78 108#if wxUSE_GADGETS
31528cd3
VZ
109 style & wxCOLOURED ? xmLabelWidgetClass
110 : xmLabelGadgetClass,
2ad99bc1 111 (Widget)m_mainWidget,
a4294b78 112#else
2ad99bc1 113 xmLabelWidgetClass, (Widget)m_mainWidget,
a4294b78 114#endif
ea57084d 115 XmNfontList, fontList,
98300330 116 XmNlabelString, text(),
11a2ce5a
MB
117// XmNframeChildType is not in Motif 1.2, nor in Lesstif,
118// if it was compiled with 1.2 compatibility
34774093 119// TODO: check this still looks OK for Motif 1.2.
11a2ce5a 120#if (XmVersion > 1200)
2ad99bc1 121 XmNframeChildType, XmFRAME_TITLE_CHILD,
34774093 122#endif
2ad99bc1 123 XmNchildVerticalAlignment, XmALIGNMENT_CENTER,
af0bb3b1 124 NULL);
a4294b78
JS
125 }
126
127 Arg args[3];
128
978db70e 129 m_majorDim = (n + m_majorDim - 1) / m_majorDim;
a4294b78
JS
130
131 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
af0bb3b1 132 XmHORIZONTAL : XmVERTICAL));
98300330 133 XtSetArg (args[1], XmNnumColumns, m_majorDim);
a4294b78 134
2ad99bc1 135 Widget radioBoxWidget = XmCreateRadioBox ((Widget)m_mainWidget, "radioBoxWidget", args, 2);
a4294b78
JS
136
137 // if (style & wxFLAT)
138 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
139
140 m_radioButtons = new WXWidget[n];
141 m_radioButtonLabels = new wxString[n];
142 int i;
143 for (i = 0; i < n; i++)
144 {
145 wxString str(wxStripMenuCodes(choices[i]));
146 m_radioButtonLabels[i] = str;
147 m_radioButtons[i] = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) str,
148#if wxUSE_GADGETS
af0bb3b1 149 xmToggleButtonGadgetClass, radioBoxWidget,
a4294b78 150#else
af0bb3b1 151 xmToggleButtonWidgetClass, radioBoxWidget,
a4294b78 152#endif
ea57084d 153 XmNfontList, fontList,
af0bb3b1 154 NULL);
a4294b78 155 XtAddCallback ((Widget) m_radioButtons[i], XmNvalueChangedCallback, (XtCallbackProc) wxRadioBoxCallback,
d7dcb939 156 (XtPointer) this);
a4294b78 157 }
a4294b78 158
da175b2c 159 m_font = parent->GetFont();
4b5f3fe6
JS
160 ChangeFont(FALSE);
161
2ad99bc1
JS
162 SetSelection (0);
163
164 XtRealizeWidget((Widget)m_mainWidget);
a4294b78 165 XtManageChild (radioBoxWidget);
2ad99bc1 166 XtManageChild ((Widget)m_mainWidget);
a4294b78
JS
167
168 SetCanAddEventHandler(TRUE);
2ad99bc1 169 AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y);
a4294b78 170
0d57be45 171 ChangeBackgroundColour();
a4294b78
JS
172
173 return TRUE;
4bb6408c
JS
174}
175
176
177wxRadioBox::~wxRadioBox()
178{
a4294b78
JS
179 delete[] m_radioButtonLabels;
180 delete[] m_radioButtons;
15d5ab67 181
15d5ab67 182 DetachWidget(m_mainWidget);
15d5ab67 183 XtDestroyWidget((Widget) m_mainWidget);
15d5ab67
JS
184
185 m_mainWidget = (WXWidget) 0;
4bb6408c
JS
186}
187
18128cbb 188void wxRadioBox::SetString(int item, const wxString& label)
4bb6408c 189{
a4294b78
JS
190 if (item < 0 || item >= m_noItems)
191 return;
192
193 Widget widget = (Widget) m_radioButtons[item];
194 if (label != "")
195 {
196 wxString label1(wxStripMenuCodes(label));
197 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
198 XtVaSetValues (widget,
af0bb3b1
VZ
199 XmNlabelString, text,
200 XmNlabelType, XmSTRING,
201 NULL);
a4294b78
JS
202 XmStringFree (text);
203 }
4bb6408c
JS
204}
205
206int wxRadioBox::FindString(const wxString& s) const
207{
a4294b78
JS
208 int i;
209 for (i = 0; i < m_noItems; i++)
210 if (s == m_radioButtonLabels[i])
211 return i;
4bb6408c
JS
212 return -1;
213}
214
215void wxRadioBox::SetSelection(int n)
216{
217 if ((n < 0) || (n >= m_noItems))
218 return;
4bb6408c
JS
219
220 m_selectedButton = n;
a4294b78
JS
221
222 m_inSetValue = TRUE;
223
224 XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE);
225
226 int i;
227 for (i = 0; i < m_noItems; i++)
228 if (i != n)
229 XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE);
230
231 m_inSetValue = FALSE;
4bb6408c
JS
232}
233
234// Get single selection, for single choice list items
235int wxRadioBox::GetSelection() const
236{
237 return m_selectedButton;
238}
239
240// Find string for position
241wxString wxRadioBox::GetString(int n) const
242{
a4294b78
JS
243 if ((n < 0) || (n >= m_noItems))
244 return wxEmptyString;
245 return m_radioButtonLabels[n];
4bb6408c
JS
246}
247
bfc6fde4 248void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 249{
2ad99bc1 250 bool managed = XtIsManaged((Widget) m_mainWidget);
4bb6408c 251
a4294b78 252 if (managed)
2ad99bc1 253 XtUnmanageChild ((Widget) m_mainWidget);
4bb6408c 254
a4294b78
JS
255 int xx = x; int yy = y;
256 AdjustForParentClientOrigin(xx, yy, sizeFlags);
4bb6408c 257
a4294b78 258 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2ad99bc1 259 XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL);
a4294b78 260 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2ad99bc1 261 XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL);
4bb6408c 262
2ad99bc1
JS
263 if (width > 0)
264 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL);
265 if (height > 0)
266 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL);
a4294b78 267
a4294b78 268 if (managed)
2ad99bc1 269 XtManageChild ((Widget) m_mainWidget);
4bb6408c
JS
270}
271
272// Enable a specific button
a4294b78 273void wxRadioBox::Enable(int n, bool enable)
4bb6408c 274{
a4294b78
JS
275 if ((n < 0) || (n >= m_noItems))
276 return;
277
278 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
4bb6408c
JS
279}
280
281// Enable all controls
af0bb3b1 282bool wxRadioBox::Enable(bool enable)
4bb6408c 283{
af0bb3b1
VZ
284 if ( !wxControl::Enable(enable) )
285 return FALSE;
4bb6408c 286
a4294b78
JS
287 int i;
288 for (i = 0; i < m_noItems; i++)
289 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
af0bb3b1
VZ
290
291 return TRUE;
4bb6408c
JS
292}
293
9838df2c
JS
294bool wxRadioBox::Show(bool show)
295{
296 // TODO: show/hide all children
297 return wxControl::Show(show);
298}
299
4bb6408c 300// Show a specific button
a4294b78 301void wxRadioBox::Show(int n, bool show)
4bb6408c 302{
a4294b78
JS
303 // This method isn't complete, and we try do do our best...
304 // It's main purpose isn't for allowing Show/Unshow dynamically,
305 // but rather to provide a way to design wxRadioBox such:
306 //
307 // o Val1 o Val2 o Val3
308 // o Val4 o Val6
309 // o Val7 o Val8 o Val9
310 //
311 // In my case, this is a 'direction' box, and the Show(5,False) is
312 // coupled with an Enable(5,False)
313 //
314 if ((n < 0) || (n >= m_noItems))
315 return;
316
317 XtVaSetValues ((Widget) m_radioButtons[n],
318 XmNindicatorOn, (unsigned char) show,
319 NULL);
320
321 // Please note that this is all we can do: removing the label
322 // if switching to unshow state. However, when switching
18128cbb 323 // to the on state, it's the prog. resp. to call SetString(item,...)
a4294b78
JS
324 // after this call!!
325 if (!show)
18128cbb 326 wxRadioBox::SetString (n, " ");
4bb6408c
JS
327}
328
329// For single selection items only
330wxString wxRadioBox::GetStringSelection () const
331{
332 int sel = GetSelection ();
333 if (sel > -1)
334 return this->GetString (sel);
335 else
336 return wxString("");
337}
338
339bool wxRadioBox::SetStringSelection (const wxString& s)
340{
341 int sel = FindString (s);
342 if (sel > -1)
343 {
344 SetSelection (sel);
345 return TRUE;
346 }
347 else
348 return FALSE;
349}
350
351void wxRadioBox::Command (wxCommandEvent & event)
352{
353 SetSelection (event.m_commandInt);
354 ProcessCommand (event);
355}
356
4b5f3fe6 357void wxRadioBox::ChangeFont(bool keepOriginalSize)
0d57be45 358{
94b49b93
JS
359 wxWindow::ChangeFont(keepOriginalSize);
360
da175b2c 361 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) GetTopWidget()));
94b49b93
JS
362
363 int i;
364 for (i = 0; i < m_noItems; i++)
365 {
366 WXWidget radioButton = m_radioButtons[i];
367
368 XtVaSetValues ((Widget) radioButton,
369 XmNfontList, fontList,
94b49b93
JS
370 NULL);
371 }
0d57be45
JS
372}
373
374void wxRadioBox::ChangeBackgroundColour()
375{
94b49b93
JS
376 wxWindow::ChangeBackgroundColour();
377
9838df2c
JS
378 int selectPixel = wxBLACK->AllocColour(wxGetDisplay());
379
94b49b93
JS
380 int i;
381 for (i = 0; i < m_noItems; i++)
382 {
383 WXWidget radioButton = m_radioButtons[i];
384
385 DoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE);
9838df2c
JS
386
387 XtVaSetValues ((Widget) radioButton,
388 XmNselectColor, selectPixel,
389 NULL);
94b49b93 390 }
0d57be45
JS
391}
392
393void wxRadioBox::ChangeForegroundColour()
394{
94b49b93
JS
395 wxWindow::ChangeForegroundColour();
396
397 int i;
398 for (i = 0; i < m_noItems; i++)
399 {
400 WXWidget radioButton = m_radioButtons[i];
401
402 DoChangeForegroundColour(radioButton, m_foregroundColour);
403 }
0d57be45
JS
404}
405
18128cbb
MB
406static int CalcOtherDim( int items, int dim )
407{
408 return items / dim + ( items % dim ? 1 : 0 );
409}
410
411int wxRadioBox::GetRowCount() const
412{
413 return m_windowStyle & wxRA_SPECIFY_ROWS ? m_noRowsOrCols
414 : CalcOtherDim( GetCount(), m_noRowsOrCols );
415}
416
417int wxRadioBox::GetColumnCount() const
418{
419 return m_windowStyle & wxRA_SPECIFY_COLS ? m_noRowsOrCols
420 : CalcOtherDim( GetCount(), m_noRowsOrCols );
421}
422
a4294b78 423void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 424 XmToggleButtonCallbackStruct * cbs)
a4294b78
JS
425{
426 if (!cbs->set)
427 return;
428
429 wxRadioBox *item = (wxRadioBox *) clientData;
430 int sel = -1;
431 int i;
3c85ada9 432 for (i = 0; i < item->GetCount(); i++)
a4294b78
JS
433 if (item->GetRadioButtons() && ((Widget) (item->GetRadioButtons()[i]) == w))
434 sel = i;
435 item->SetSel(sel);
436
437 if (item->InSetValue())
438 return;
439
440 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
2ad99bc1
JS
441 event.SetInt(sel);
442 event.SetString(item->GetStringSelection());
a4294b78
JS
443 event.SetEventObject(item);
444 item->ProcessCommand (event);
445}
4bb6408c 446