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