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