]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobox.cpp
compilation fix for Mac and reformatted the code
[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>
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
46wxRadioBox::wxRadioBox()
47{
48 m_selectedButton = -1;
49 m_noItems = 0;
50 m_noRowsOrCols = 0;
51 m_majorDim = 0 ;
a4294b78 52
a4294b78
JS
53 m_radioButtons = (WXWidget*) NULL;
54 m_radioButtonLabels = (wxString*) NULL;
4bb6408c
JS
55}
56
57bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
58 const wxPoint& pos, const wxSize& size,
59 int n, const wxString choices[],
60 int majorDim, long style,
61 const wxValidator& val, const wxString& name)
62{
63 m_selectedButton = -1;
64 m_noItems = n;
a4294b78
JS
65 m_radioButtons = (WXWidget*) NULL;
66 m_radioButtonLabels = (wxString*) NULL;
0d57be45
JS
67 m_backgroundColour = parent->GetBackgroundColour();
68 m_foregroundColour = parent->GetForegroundColour();
da175b2c 69 m_font = parent->GetFont();
4bb6408c
JS
70
71 SetName(name);
72 SetValidator(val);
73
74 parent->AddChild(this);
75
76 m_windowStyle = (long&)style;
77
78 if (id == -1)
79 m_windowId = NewControlId();
80 else
81 m_windowId = id;
82
83 m_noRowsOrCols = majorDim;
84
85 if (majorDim==0)
86 m_majorDim = n ;
87 else
88 m_majorDim = majorDim ;
89
a4294b78
JS
90 Widget parentWidget = (Widget) parent->GetClientWidget();
91
34774093 92 m_mainWidget = XtVaCreateWidget ("radioboxframe",
2ad99bc1 93 xmFrameWidgetClass, parentWidget,
2ad99bc1
JS
94 XmNresizeHeight, True,
95 XmNresizeWidth, True,
af0bb3b1 96 NULL);
a4294b78 97
2ad99bc1 98 wxString label1(wxStripMenuCodes(title));
a4294b78 99
da494b40 100 WXFontType fontType = m_font.GetFontType(XtDisplay(parentWidget));
2ad99bc1 101
a4294b78
JS
102 if (label1 != "")
103 {
31528cd3 104 wxXmString text(label1);
3dd709d8
MB
105 m_labelWidget = (WXWidget)
106 XtVaCreateManagedWidget( label1.c_str(),
a4294b78 107#if wxUSE_GADGETS
da494b40
MB
108 style & wxCOLOURED ? xmLabelWidgetClass
109 : xmLabelGadgetClass,
110 (Widget)m_mainWidget,
a4294b78 111#else
da494b40
MB
112 xmLabelWidgetClass,
113 (Widget)m_mainWidget,
a4294b78 114#endif
da494b40
MB
115 wxFont::GetFontTag(), fontType,
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)
da494b40 121 XmNframeChildType, XmFRAME_TITLE_CHILD,
2b5f62a0 122#else
da494b40 123 XmNchildType, XmFRAME_TITLE_CHILD,
34774093 124#endif
da494b40
MB
125 XmNchildVerticalAlignment,
126 XmALIGNMENT_CENTER,
127 NULL);
a4294b78
JS
128 }
129
130 Arg args[3];
131
978db70e 132 m_majorDim = (n + m_majorDim - 1) / m_majorDim;
a4294b78
JS
133
134 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
af0bb3b1 135 XmHORIZONTAL : XmVERTICAL));
98300330 136 XtSetArg (args[1], XmNnumColumns, m_majorDim);
a4294b78 137
2ad99bc1 138 Widget radioBoxWidget = XmCreateRadioBox ((Widget)m_mainWidget, "radioBoxWidget", args, 2);
a4294b78
JS
139
140 // if (style & wxFLAT)
141 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
142
143 m_radioButtons = new WXWidget[n];
144 m_radioButtonLabels = new wxString[n];
145 int i;
146 for (i = 0; i < n; i++)
147 {
148 wxString str(wxStripMenuCodes(choices[i]));
149 m_radioButtonLabels[i] = str;
d3a80c92 150 m_radioButtons[i] = (WXWidget) XtVaCreateManagedWidget (wxConstCast(str.c_str(), char),
a4294b78 151#if wxUSE_GADGETS
af0bb3b1 152 xmToggleButtonGadgetClass, radioBoxWidget,
a4294b78 153#else
da494b40 154 xmToggleButtonWidgetClass, radioBoxWidget,
a4294b78 155#endif
da494b40 156 wxFont::GetFontTag(), fontType,
af0bb3b1 157 NULL);
a4294b78 158 XtAddCallback ((Widget) m_radioButtons[i], XmNvalueChangedCallback, (XtCallbackProc) wxRadioBoxCallback,
d7dcb939 159 (XtPointer) this);
a4294b78 160 }
a4294b78 161
da175b2c 162 m_font = parent->GetFont();
4b5f3fe6
JS
163 ChangeFont(FALSE);
164
2ad99bc1
JS
165 SetSelection (0);
166
167 XtRealizeWidget((Widget)m_mainWidget);
a4294b78 168 XtManageChild (radioBoxWidget);
2ad99bc1 169 XtManageChild ((Widget)m_mainWidget);
a4294b78 170
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));
da494b40 199 wxXmString text( label1 );
a4294b78 200 XtVaSetValues (widget,
da494b40 201 XmNlabelString, text(),
af0bb3b1
VZ
202 XmNlabelType, XmSTRING,
203 NULL);
a4294b78 204 }
4bb6408c
JS
205}
206
207int wxRadioBox::FindString(const wxString& s) const
208{
a4294b78
JS
209 int i;
210 for (i = 0; i < m_noItems; i++)
211 if (s == m_radioButtonLabels[i])
212 return i;
4bb6408c
JS
213 return -1;
214}
215
216void wxRadioBox::SetSelection(int n)
217{
218 if ((n < 0) || (n >= m_noItems))
219 return;
4bb6408c
JS
220
221 m_selectedButton = n;
a4294b78
JS
222
223 m_inSetValue = TRUE;
224
225 XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE);
226
227 int i;
228 for (i = 0; i < m_noItems; i++)
229 if (i != n)
230 XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE);
231
232 m_inSetValue = FALSE;
4bb6408c
JS
233}
234
235// Get single selection, for single choice list items
236int wxRadioBox::GetSelection() const
237{
238 return m_selectedButton;
239}
240
241// Find string for position
242wxString wxRadioBox::GetString(int n) const
243{
a4294b78
JS
244 if ((n < 0) || (n >= m_noItems))
245 return wxEmptyString;
246 return m_radioButtonLabels[n];
4bb6408c
JS
247}
248
bfc6fde4 249void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 250{
2ad99bc1 251 bool managed = XtIsManaged((Widget) m_mainWidget);
4bb6408c 252
a4294b78 253 if (managed)
2ad99bc1 254 XtUnmanageChild ((Widget) m_mainWidget);
4bb6408c 255
a4294b78
JS
256 int xx = x; int yy = y;
257 AdjustForParentClientOrigin(xx, yy, sizeFlags);
4bb6408c 258
a4294b78 259 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2ad99bc1 260 XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL);
a4294b78 261 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2ad99bc1 262 XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL);
4bb6408c 263
2ad99bc1
JS
264 if (width > 0)
265 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL);
266 if (height > 0)
267 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL);
a4294b78 268
a4294b78 269 if (managed)
2ad99bc1 270 XtManageChild ((Widget) m_mainWidget);
4bb6408c
JS
271}
272
273// Enable a specific button
a4294b78 274void wxRadioBox::Enable(int n, bool enable)
4bb6408c 275{
a4294b78
JS
276 if ((n < 0) || (n >= m_noItems))
277 return;
278
279 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
4bb6408c
JS
280}
281
282// Enable all controls
af0bb3b1 283bool wxRadioBox::Enable(bool enable)
4bb6408c 284{
af0bb3b1
VZ
285 if ( !wxControl::Enable(enable) )
286 return FALSE;
4bb6408c 287
a4294b78
JS
288 int i;
289 for (i = 0; i < m_noItems; i++)
290 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
af0bb3b1
VZ
291
292 return TRUE;
4bb6408c
JS
293}
294
9838df2c
JS
295bool wxRadioBox::Show(bool show)
296{
297 // TODO: show/hide all children
298 return wxControl::Show(show);
299}
300
4bb6408c 301// Show a specific button
a4294b78 302void wxRadioBox::Show(int n, bool show)
4bb6408c 303{
a4294b78
JS
304 // This method isn't complete, and we try do do our best...
305 // It's main purpose isn't for allowing Show/Unshow dynamically,
306 // but rather to provide a way to design wxRadioBox such:
307 //
308 // o Val1 o Val2 o Val3
309 // o Val4 o Val6
310 // o Val7 o Val8 o Val9
311 //
312 // In my case, this is a 'direction' box, and the Show(5,False) is
313 // coupled with an Enable(5,False)
314 //
315 if ((n < 0) || (n >= m_noItems))
316 return;
317
318 XtVaSetValues ((Widget) m_radioButtons[n],
319 XmNindicatorOn, (unsigned char) show,
320 NULL);
321
322 // Please note that this is all we can do: removing the label
323 // if switching to unshow state. However, when switching
18128cbb 324 // to the on state, it's the prog. resp. to call SetString(item,...)
a4294b78
JS
325 // after this call!!
326 if (!show)
18128cbb 327 wxRadioBox::SetString (n, " ");
4bb6408c
JS
328}
329
330// For single selection items only
331wxString wxRadioBox::GetStringSelection () const
332{
333 int sel = GetSelection ();
334 if (sel > -1)
335 return this->GetString (sel);
336 else
337 return wxString("");
338}
339
340bool wxRadioBox::SetStringSelection (const wxString& s)
341{
342 int sel = FindString (s);
343 if (sel > -1)
344 {
345 SetSelection (sel);
346 return TRUE;
347 }
348 else
349 return FALSE;
350}
351
352void wxRadioBox::Command (wxCommandEvent & event)
353{
354 SetSelection (event.m_commandInt);
355 ProcessCommand (event);
356}
357
4b5f3fe6 358void wxRadioBox::ChangeFont(bool keepOriginalSize)
0d57be45 359{
94b49b93
JS
360 wxWindow::ChangeFont(keepOriginalSize);
361
da494b40
MB
362 WXFontType fontType =
363 m_font.GetFontType(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,
da494b40 371 wxFont::GetFontTag(), fontType,
94b49b93
JS
372 NULL);
373 }
0d57be45
JS
374}
375
376void wxRadioBox::ChangeBackgroundColour()
377{
94b49b93
JS
378 wxWindow::ChangeBackgroundColour();
379
eb6fa4b4 380 int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c 381
94b49b93
JS
382 int i;
383 for (i = 0; i < m_noItems; i++)
384 {
385 WXWidget radioButton = m_radioButtons[i];
386
a8680e3e 387 wxDoChangeBackgroundColour(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
a8680e3e 404 wxDoChangeForegroundColour(radioButton, m_foregroundColour);
94b49b93 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