]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobox.cpp
corrected ::Clear
[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
2ad99bc1
JS
93 m_mainWidget = XtVaCreateWidget ("radiobxoframe",
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(),
2ad99bc1
JS
117 XmNframeChildType, XmFRAME_TITLE_CHILD,
118 XmNchildVerticalAlignment, XmALIGNMENT_CENTER,
af0bb3b1 119 NULL);
a4294b78
JS
120 }
121
122 Arg args[3];
123
978db70e 124 m_majorDim = (n + m_majorDim - 1) / m_majorDim;
a4294b78
JS
125
126 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
af0bb3b1 127 XmHORIZONTAL : XmVERTICAL));
98300330 128 XtSetArg (args[1], XmNnumColumns, m_majorDim);
a4294b78 129
2ad99bc1 130 Widget radioBoxWidget = XmCreateRadioBox ((Widget)m_mainWidget, "radioBoxWidget", args, 2);
a4294b78
JS
131
132 // if (style & wxFLAT)
133 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
134
135 m_radioButtons = new WXWidget[n];
136 m_radioButtonLabels = new wxString[n];
137 int i;
138 for (i = 0; i < n; i++)
139 {
140 wxString str(wxStripMenuCodes(choices[i]));
141 m_radioButtonLabels[i] = str;
142 m_radioButtons[i] = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) str,
143#if wxUSE_GADGETS
af0bb3b1 144 xmToggleButtonGadgetClass, radioBoxWidget,
a4294b78 145#else
af0bb3b1 146 xmToggleButtonWidgetClass, radioBoxWidget,
a4294b78 147#endif
ea57084d 148 XmNfontList, fontList,
af0bb3b1 149 NULL);
a4294b78 150 XtAddCallback ((Widget) m_radioButtons[i], XmNvalueChangedCallback, (XtCallbackProc) wxRadioBoxCallback,
d7dcb939 151 (XtPointer) this);
a4294b78 152 }
a4294b78 153
da175b2c 154 m_font = parent->GetFont();
4b5f3fe6
JS
155 ChangeFont(FALSE);
156
2ad99bc1
JS
157 SetSelection (0);
158
159 XtRealizeWidget((Widget)m_mainWidget);
a4294b78 160 XtManageChild (radioBoxWidget);
2ad99bc1 161 XtManageChild ((Widget)m_mainWidget);
a4294b78
JS
162
163 SetCanAddEventHandler(TRUE);
2ad99bc1 164 AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y);
a4294b78 165
0d57be45 166 ChangeBackgroundColour();
a4294b78
JS
167
168 return TRUE;
4bb6408c
JS
169}
170
171
172wxRadioBox::~wxRadioBox()
173{
a4294b78
JS
174 delete[] m_radioButtonLabels;
175 delete[] m_radioButtons;
15d5ab67 176
15d5ab67 177 DetachWidget(m_mainWidget);
15d5ab67 178 XtDestroyWidget((Widget) m_mainWidget);
15d5ab67
JS
179
180 m_mainWidget = (WXWidget) 0;
4bb6408c
JS
181}
182
18128cbb 183void wxRadioBox::SetString(int item, const wxString& label)
4bb6408c 184{
a4294b78
JS
185 if (item < 0 || item >= m_noItems)
186 return;
187
188 Widget widget = (Widget) m_radioButtons[item];
189 if (label != "")
190 {
191 wxString label1(wxStripMenuCodes(label));
192 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
193 XtVaSetValues (widget,
af0bb3b1
VZ
194 XmNlabelString, text,
195 XmNlabelType, XmSTRING,
196 NULL);
a4294b78
JS
197 XmStringFree (text);
198 }
4bb6408c
JS
199}
200
201int wxRadioBox::FindString(const wxString& s) const
202{
a4294b78
JS
203 int i;
204 for (i = 0; i < m_noItems; i++)
205 if (s == m_radioButtonLabels[i])
206 return i;
4bb6408c
JS
207 return -1;
208}
209
210void wxRadioBox::SetSelection(int n)
211{
212 if ((n < 0) || (n >= m_noItems))
213 return;
4bb6408c
JS
214
215 m_selectedButton = n;
a4294b78
JS
216
217 m_inSetValue = TRUE;
218
219 XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE);
220
221 int i;
222 for (i = 0; i < m_noItems; i++)
223 if (i != n)
224 XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE);
225
226 m_inSetValue = FALSE;
4bb6408c
JS
227}
228
229// Get single selection, for single choice list items
230int wxRadioBox::GetSelection() const
231{
232 return m_selectedButton;
233}
234
235// Find string for position
236wxString wxRadioBox::GetString(int n) const
237{
a4294b78
JS
238 if ((n < 0) || (n >= m_noItems))
239 return wxEmptyString;
240 return m_radioButtonLabels[n];
4bb6408c
JS
241}
242
bfc6fde4 243void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 244{
2ad99bc1 245 bool managed = XtIsManaged((Widget) m_mainWidget);
4bb6408c 246
a4294b78 247 if (managed)
2ad99bc1 248 XtUnmanageChild ((Widget) m_mainWidget);
4bb6408c 249
a4294b78
JS
250 int xx = x; int yy = y;
251 AdjustForParentClientOrigin(xx, yy, sizeFlags);
4bb6408c 252
a4294b78 253 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2ad99bc1 254 XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL);
a4294b78 255 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2ad99bc1 256 XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL);
4bb6408c 257
2ad99bc1
JS
258 if (width > 0)
259 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL);
260 if (height > 0)
261 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL);
a4294b78 262
a4294b78 263 if (managed)
2ad99bc1 264 XtManageChild ((Widget) m_mainWidget);
4bb6408c
JS
265}
266
267// Enable a specific button
a4294b78 268void wxRadioBox::Enable(int n, bool enable)
4bb6408c 269{
a4294b78
JS
270 if ((n < 0) || (n >= m_noItems))
271 return;
272
273 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
4bb6408c
JS
274}
275
276// Enable all controls
af0bb3b1 277bool wxRadioBox::Enable(bool enable)
4bb6408c 278{
af0bb3b1
VZ
279 if ( !wxControl::Enable(enable) )
280 return FALSE;
4bb6408c 281
a4294b78
JS
282 int i;
283 for (i = 0; i < m_noItems; i++)
284 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
af0bb3b1
VZ
285
286 return TRUE;
4bb6408c
JS
287}
288
9838df2c
JS
289bool wxRadioBox::Show(bool show)
290{
291 // TODO: show/hide all children
292 return wxControl::Show(show);
293}
294
4bb6408c 295// Show a specific button
a4294b78 296void wxRadioBox::Show(int n, bool show)
4bb6408c 297{
a4294b78
JS
298 // This method isn't complete, and we try do do our best...
299 // It's main purpose isn't for allowing Show/Unshow dynamically,
300 // but rather to provide a way to design wxRadioBox such:
301 //
302 // o Val1 o Val2 o Val3
303 // o Val4 o Val6
304 // o Val7 o Val8 o Val9
305 //
306 // In my case, this is a 'direction' box, and the Show(5,False) is
307 // coupled with an Enable(5,False)
308 //
309 if ((n < 0) || (n >= m_noItems))
310 return;
311
312 XtVaSetValues ((Widget) m_radioButtons[n],
313 XmNindicatorOn, (unsigned char) show,
314 NULL);
315
316 // Please note that this is all we can do: removing the label
317 // if switching to unshow state. However, when switching
18128cbb 318 // to the on state, it's the prog. resp. to call SetString(item,...)
a4294b78
JS
319 // after this call!!
320 if (!show)
18128cbb 321 wxRadioBox::SetString (n, " ");
4bb6408c
JS
322}
323
324// For single selection items only
325wxString wxRadioBox::GetStringSelection () const
326{
327 int sel = GetSelection ();
328 if (sel > -1)
329 return this->GetString (sel);
330 else
331 return wxString("");
332}
333
334bool wxRadioBox::SetStringSelection (const wxString& s)
335{
336 int sel = FindString (s);
337 if (sel > -1)
338 {
339 SetSelection (sel);
340 return TRUE;
341 }
342 else
343 return FALSE;
344}
345
346void wxRadioBox::Command (wxCommandEvent & event)
347{
348 SetSelection (event.m_commandInt);
349 ProcessCommand (event);
350}
351
4b5f3fe6 352void wxRadioBox::ChangeFont(bool keepOriginalSize)
0d57be45 353{
94b49b93
JS
354 wxWindow::ChangeFont(keepOriginalSize);
355
da175b2c 356 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) GetTopWidget()));
94b49b93
JS
357
358 int i;
359 for (i = 0; i < m_noItems; i++)
360 {
361 WXWidget radioButton = m_radioButtons[i];
362
363 XtVaSetValues ((Widget) radioButton,
364 XmNfontList, fontList,
94b49b93
JS
365 NULL);
366 }
0d57be45
JS
367}
368
369void wxRadioBox::ChangeBackgroundColour()
370{
94b49b93
JS
371 wxWindow::ChangeBackgroundColour();
372
9838df2c
JS
373 int selectPixel = wxBLACK->AllocColour(wxGetDisplay());
374
94b49b93
JS
375 int i;
376 for (i = 0; i < m_noItems; i++)
377 {
378 WXWidget radioButton = m_radioButtons[i];
379
380 DoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE);
9838df2c
JS
381
382 XtVaSetValues ((Widget) radioButton,
383 XmNselectColor, selectPixel,
384 NULL);
94b49b93 385 }
0d57be45
JS
386}
387
388void wxRadioBox::ChangeForegroundColour()
389{
94b49b93
JS
390 wxWindow::ChangeForegroundColour();
391
392 int i;
393 for (i = 0; i < m_noItems; i++)
394 {
395 WXWidget radioButton = m_radioButtons[i];
396
397 DoChangeForegroundColour(radioButton, m_foregroundColour);
398 }
0d57be45
JS
399}
400
18128cbb
MB
401static int CalcOtherDim( int items, int dim )
402{
403 return items / dim + ( items % dim ? 1 : 0 );
404}
405
406int wxRadioBox::GetRowCount() const
407{
408 return m_windowStyle & wxRA_SPECIFY_ROWS ? m_noRowsOrCols
409 : CalcOtherDim( GetCount(), m_noRowsOrCols );
410}
411
412int wxRadioBox::GetColumnCount() const
413{
414 return m_windowStyle & wxRA_SPECIFY_COLS ? m_noRowsOrCols
415 : CalcOtherDim( GetCount(), m_noRowsOrCols );
416}
417
a4294b78 418void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 419 XmToggleButtonCallbackStruct * cbs)
a4294b78
JS
420{
421 if (!cbs->set)
422 return;
423
424 wxRadioBox *item = (wxRadioBox *) clientData;
425 int sel = -1;
426 int i;
3c85ada9 427 for (i = 0; i < item->GetCount(); i++)
a4294b78
JS
428 if (item->GetRadioButtons() && ((Widget) (item->GetRadioButtons()[i]) == w))
429 sel = i;
430 item->SetSel(sel);
431
432 if (item->InSetValue())
433 return;
434
435 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
2ad99bc1
JS
436 event.SetInt(sel);
437 event.SetString(item->GetStringSelection());
a4294b78
JS
438 event.SetEventObject(item);
439 item->ProcessCommand (event);
440}
4bb6408c 441