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