]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobox.cpp
Warning fix about assigning unused value + little source cleaning.
[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
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4bb6408c
JS
13#pragma implementation "radiobox.h"
14#endif
15
1248b41f
MB
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
bcd055ae
JJ
19#ifdef __VMS
20#define XtDisplay XTDISPLAY
21#endif
22
f6045f99
GD
23#include "wx/defs.h"
24
4bb6408c 25#include "wx/radiobox.h"
a4294b78 26#include "wx/utils.h"
584ad2a3 27#include "wx/arrstr.h"
a4294b78 28
338dd992
JJ
29#ifdef __VMS__
30#pragma message disable nosimpint
31#endif
a4294b78
JS
32#include <Xm/Label.h>
33#include <Xm/LabelG.h>
34#include <Xm/ToggleB.h>
35#include <Xm/ToggleBG.h>
36#include <Xm/RowColumn.h>
9838df2c 37#include <Xm/Frame.h>
338dd992
JJ
38#ifdef __VMS__
39#pragma message enable nosimpint
40#endif
a4294b78 41
3096bd2f 42#include "wx/motif/private.h"
a4294b78
JS
43
44void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 45 XmToggleButtonCallbackStruct * cbs);
4bb6408c 46
4bb6408c 47IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
4bb6408c
JS
48
49// Radio box item
3bdb8629 50void wxRadioBox::Init()
4bb6408c
JS
51{
52 m_selectedButton = -1;
53 m_noItems = 0;
54 m_noRowsOrCols = 0;
55 m_majorDim = 0 ;
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{
3bdb8629
MB
64 if( !CreateControl( parent, id, pos, size, style, val, name ) )
65 return false;
4bb6408c 66
3bdb8629 67 m_noItems = n;
4bb6408c
JS
68 m_noRowsOrCols = majorDim;
69
70 if (majorDim==0)
71 m_majorDim = n ;
72 else
73 m_majorDim = majorDim ;
74
a4294b78
JS
75 Widget parentWidget = (Widget) parent->GetClientWidget();
76
34774093 77 m_mainWidget = XtVaCreateWidget ("radioboxframe",
2ad99bc1 78 xmFrameWidgetClass, parentWidget,
2ad99bc1
JS
79 XmNresizeHeight, True,
80 XmNresizeWidth, True,
af0bb3b1 81 NULL);
a4294b78 82
2ad99bc1 83 wxString label1(wxStripMenuCodes(title));
a4294b78 84
da494b40 85 WXFontType fontType = m_font.GetFontType(XtDisplay(parentWidget));
2ad99bc1 86
1a87edf2 87 if (!label1.empty())
a4294b78 88 {
31528cd3 89 wxXmString text(label1);
3dd709d8
MB
90 m_labelWidget = (WXWidget)
91 XtVaCreateManagedWidget( label1.c_str(),
a4294b78 92#if wxUSE_GADGETS
da494b40
MB
93 style & wxCOLOURED ? xmLabelWidgetClass
94 : xmLabelGadgetClass,
95 (Widget)m_mainWidget,
a4294b78 96#else
1a87edf2 97 xmLabelWidgetClass,
da494b40 98 (Widget)m_mainWidget,
a4294b78 99#endif
da494b40
MB
100 wxFont::GetFontTag(), fontType,
101 XmNlabelString, text(),
11a2ce5a
MB
102// XmNframeChildType is not in Motif 1.2, nor in Lesstif,
103// if it was compiled with 1.2 compatibility
34774093 104// TODO: check this still looks OK for Motif 1.2.
11a2ce5a 105#if (XmVersion > 1200)
da494b40 106 XmNframeChildType, XmFRAME_TITLE_CHILD,
2b5f62a0 107#else
da494b40 108 XmNchildType, XmFRAME_TITLE_CHILD,
34774093 109#endif
1a87edf2 110 XmNchildVerticalAlignment,
da494b40
MB
111 XmALIGNMENT_CENTER,
112 NULL);
a4294b78
JS
113 }
114
115 Arg args[3];
116
978db70e 117 m_majorDim = (n + m_majorDim - 1) / m_majorDim;
a4294b78
JS
118
119 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
af0bb3b1 120 XmHORIZONTAL : XmVERTICAL));
98300330 121 XtSetArg (args[1], XmNnumColumns, m_majorDim);
795c00bd 122 XtSetArg (args[2], XmNadjustLast, False);
a4294b78 123
795c00bd
MB
124 Widget radioBoxWidget =
125 XmCreateRadioBox ((Widget)m_mainWidget, "radioBoxWidget", args, 3);
a4294b78 126
3bdb8629
MB
127 m_radioButtons.reserve(n);
128 m_radioButtonLabels.reserve(n);
a4294b78 129
a4294b78
JS
130 int i;
131 for (i = 0; i < n; i++)
132 {
133 wxString str(wxStripMenuCodes(choices[i]));
3bdb8629
MB
134 m_radioButtonLabels.push_back(str);
135 Widget radioItem = XtVaCreateManagedWidget (wxConstCast(str.c_str(), char),
a4294b78 136#if wxUSE_GADGETS
af0bb3b1 137 xmToggleButtonGadgetClass, radioBoxWidget,
a4294b78 138#else
da494b40 139 xmToggleButtonWidgetClass, radioBoxWidget,
a4294b78 140#endif
da494b40 141 wxFont::GetFontTag(), fontType,
af0bb3b1 142 NULL);
3bdb8629
MB
143 m_radioButtons.push_back((WXWidget)radioItem);
144 XtAddCallback (radioItem, XmNvalueChangedCallback,
145 (XtCallbackProc) wxRadioBoxCallback,
146 (XtPointer) this);
a4294b78 147 }
a4294b78 148
96be256b 149 ChangeFont(false);
4b5f3fe6 150
2ad99bc1 151 SetSelection (0);
1a87edf2 152
2ad99bc1 153 XtRealizeWidget((Widget)m_mainWidget);
a4294b78 154 XtManageChild (radioBoxWidget);
2ad99bc1 155 XtManageChild ((Widget)m_mainWidget);
a4294b78 156
2ad99bc1 157 AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y);
a4294b78 158
0d57be45 159 ChangeBackgroundColour();
a4294b78 160
96be256b 161 return true;
4bb6408c
JS
162}
163
584ad2a3
MB
164bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
165 const wxPoint& pos, const wxSize& size,
166 const wxArrayString& choices,
167 int majorDim, long style,
168 const wxValidator& val, const wxString& name)
169{
170 wxCArrayString chs(choices);
171 return Create(parent, id, title, pos, size, chs.GetCount(),
172 chs.GetStrings(), majorDim, style, val, name);
173}
4bb6408c
JS
174
175wxRadioBox::~wxRadioBox()
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{
789f6795 185 if (!IsValid(item))
a4294b78
JS
186 return;
187
188 Widget widget = (Widget) m_radioButtons[item];
1a87edf2 189 if (!label.empty())
a4294b78
JS
190 {
191 wxString label1(wxStripMenuCodes(label));
da494b40 192 wxXmString text( label1 );
3bdb8629 193 m_radioButtonLabels[item] = label1;
a4294b78 194 XtVaSetValues (widget,
da494b40 195 XmNlabelString, text(),
af0bb3b1
VZ
196 XmNlabelType, XmSTRING,
197 NULL);
a4294b78 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;
789f6795 207 return wxNOT_FOUND;
4bb6408c
JS
208}
209
210void wxRadioBox::SetSelection(int n)
211{
789f6795 212 if (!IsValid(n))
4bb6408c 213 return;
4bb6408c
JS
214
215 m_selectedButton = n;
a4294b78 216
96be256b 217 m_inSetValue = true;
a4294b78 218
96be256b 219 XmToggleButtonSetState ((Widget) m_radioButtons[n], True, False);
a4294b78
JS
220
221 int i;
222 for (i = 0; i < m_noItems; i++)
223 if (i != n)
96be256b 224 XmToggleButtonSetState ((Widget) m_radioButtons[i], False, False);
a4294b78 225
96be256b 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{
789f6795 238 if (!IsValid(n))
a4294b78
JS
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))
1a87edf2 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
1a87edf2 268bool wxRadioBox::Enable(int n, bool enable)
4bb6408c 269{
789f6795 270 if (!IsValid(n))
1a87edf2 271 return false;
a4294b78
JS
272
273 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
1a87edf2 274 return true;
4bb6408c
JS
275}
276
277// Enable all controls
af0bb3b1 278bool wxRadioBox::Enable(bool enable)
4bb6408c 279{
af0bb3b1 280 if ( !wxControl::Enable(enable) )
96be256b 281 return false;
4bb6408c 282
a4294b78
JS
283 int i;
284 for (i = 0; i < m_noItems; i++)
285 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
af0bb3b1 286
96be256b 287 return true;
4bb6408c
JS
288}
289
9838df2c
JS
290bool wxRadioBox::Show(bool show)
291{
292 // TODO: show/hide all children
293 return wxControl::Show(show);
294}
295
4bb6408c 296// Show a specific button
789f6795 297bool wxRadioBox::Show(int n, bool show)
4bb6408c 298{
a4294b78
JS
299 // This method isn't complete, and we try do do our best...
300 // It's main purpose isn't for allowing Show/Unshow dynamically,
301 // but rather to provide a way to design wxRadioBox such:
302 //
1a87edf2
WS
303 // o Val1 o Val2 o Val3
304 // o Val4 o Val6
305 // o Val7 o Val8 o Val9
a4294b78
JS
306 //
307 // In my case, this is a 'direction' box, and the Show(5,False) is
308 // coupled with an Enable(5,False)
309 //
789f6795
WS
310 if (!IsValid(n))
311 return false;
a4294b78
JS
312
313 XtVaSetValues ((Widget) m_radioButtons[n],
314 XmNindicatorOn, (unsigned char) show,
315 NULL);
316
317 // Please note that this is all we can do: removing the label
318 // if switching to unshow state. However, when switching
18128cbb 319 // to the on state, it's the prog. resp. to call SetString(item,...)
a4294b78
JS
320 // after this call!!
321 if (!show)
18128cbb 322 wxRadioBox::SetString (n, " ");
789f6795
WS
323
324 return true;
4bb6408c
JS
325}
326
327// For single selection items only
328wxString wxRadioBox::GetStringSelection () const
329{
330 int sel = GetSelection ();
331 if (sel > -1)
332 return this->GetString (sel);
333 else
1a87edf2 334 return wxEmptyString;
4bb6408c
JS
335}
336
337bool wxRadioBox::SetStringSelection (const wxString& s)
338{
339 int sel = FindString (s);
340 if (sel > -1)
341 {
342 SetSelection (sel);
96be256b 343 return true;
4bb6408c
JS
344 }
345 else
96be256b 346 return false;
4bb6408c
JS
347}
348
349void wxRadioBox::Command (wxCommandEvent & event)
350{
687706f5 351 SetSelection (event.GetInt());
4bb6408c
JS
352 ProcessCommand (event);
353}
354
4b5f3fe6 355void wxRadioBox::ChangeFont(bool keepOriginalSize)
0d57be45 356{
94b49b93
JS
357 wxWindow::ChangeFont(keepOriginalSize);
358
da494b40
MB
359 WXFontType fontType =
360 m_font.GetFontType(XtDisplay((Widget) GetTopWidget()));
94b49b93
JS
361
362 int i;
363 for (i = 0; i < m_noItems; i++)
364 {
365 WXWidget radioButton = m_radioButtons[i];
366
367 XtVaSetValues ((Widget) radioButton,
da494b40 368 wxFont::GetFontTag(), fontType,
94b49b93
JS
369 NULL);
370 }
0d57be45
JS
371}
372
373void wxRadioBox::ChangeBackgroundColour()
374{
94b49b93
JS
375 wxWindow::ChangeBackgroundColour();
376
eb6fa4b4 377 int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c 378
94b49b93
JS
379 int i;
380 for (i = 0; i < m_noItems; i++)
381 {
382 WXWidget radioButton = m_radioButtons[i];
383
96be256b 384 wxDoChangeBackgroundColour(radioButton, m_backgroundColour, true);
9838df2c
JS
385
386 XtVaSetValues ((Widget) radioButton,
387 XmNselectColor, selectPixel,
388 NULL);
94b49b93 389 }
0d57be45
JS
390}
391
392void wxRadioBox::ChangeForegroundColour()
393{
94b49b93
JS
394 wxWindow::ChangeForegroundColour();
395
396 int i;
397 for (i = 0; i < m_noItems; i++)
398 {
399 WXWidget radioButton = m_radioButtons[i];
400
a8680e3e 401 wxDoChangeForegroundColour(radioButton, m_foregroundColour);
94b49b93 402 }
0d57be45
JS
403}
404
18128cbb
MB
405static int CalcOtherDim( int items, int dim )
406{
407 return items / dim + ( items % dim ? 1 : 0 );
408}
409
410int wxRadioBox::GetRowCount() const
411{
412 return m_windowStyle & wxRA_SPECIFY_ROWS ? m_noRowsOrCols
413 : CalcOtherDim( GetCount(), m_noRowsOrCols );
414}
415
416int wxRadioBox::GetColumnCount() const
417{
418 return m_windowStyle & wxRA_SPECIFY_COLS ? m_noRowsOrCols
419 : CalcOtherDim( GetCount(), m_noRowsOrCols );
420}
421
a4294b78 422void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 423 XmToggleButtonCallbackStruct * cbs)
a4294b78
JS
424{
425 if (!cbs->set)
426 return;
427
428 wxRadioBox *item = (wxRadioBox *) clientData;
429 int sel = -1;
430 int i;
3bdb8629 431 const wxWidgetArray& buttons = item->GetRadioButtons();
3c85ada9 432 for (i = 0; i < item->GetCount(); i++)
3bdb8629 433 if (((Widget)buttons[i]) == w)
a4294b78
JS
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