]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobox.cpp
fix wxStringOutputStream::Write() in Unicode build when the output overlaps a boundar...
[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
8228b893
WS
15#if wxUSE_RADIOBOX
16
bcd055ae
JJ
17#ifdef __VMS
18#define XtDisplay XTDISPLAY
19#endif
20
4bb6408c 21#include "wx/radiobox.h"
de6185e2
WS
22
23#ifndef WX_PRECOMP
24 #include "wx/utils.h"
aaa6d89a 25 #include "wx/arrstr.h"
de6185e2
WS
26#endif
27
338dd992
JJ
28#ifdef __VMS__
29#pragma message disable nosimpint
30#endif
a4294b78
JS
31#include <Xm/Label.h>
32#include <Xm/LabelG.h>
33#include <Xm/ToggleB.h>
34#include <Xm/ToggleBG.h>
35#include <Xm/RowColumn.h>
9838df2c 36#include <Xm/Frame.h>
338dd992
JJ
37#ifdef __VMS__
38#pragma message enable nosimpint
39#endif
a4294b78 40
3096bd2f 41#include "wx/motif/private.h"
a4294b78
JS
42
43void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 44 XmToggleButtonCallbackStruct * cbs);
4bb6408c 45
4bb6408c 46IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
4bb6408c
JS
47
48// Radio box item
3bdb8629 49void wxRadioBox::Init()
4bb6408c
JS
50{
51 m_selectedButton = -1;
52 m_noItems = 0;
53 m_noRowsOrCols = 0;
105fbe1f 54 m_labelWidget = (WXWidget) 0;
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{
3bdb8629
MB
63 if( !CreateControl( parent, id, pos, size, style, val, name ) )
64 return false;
105fbe1f 65 PreCreation();
4bb6408c 66
aa61d352 67 m_noItems = (unsigned int)n;
4bb6408c
JS
68 m_noRowsOrCols = majorDim;
69
21e0a4d5 70 SetMajorDim(majorDim == 0 ? n : majorDim, style);
4bb6408c 71
a4294b78 72 Widget parentWidget = (Widget) parent->GetClientWidget();
73608949 73 Display* dpy = XtDisplay(parentWidget);
a4294b78 74
34774093 75 m_mainWidget = XtVaCreateWidget ("radioboxframe",
73608949
MB
76 xmFrameWidgetClass, parentWidget,
77 XmNresizeHeight, True,
78 XmNresizeWidth, True,
79 NULL);
a4294b78 80
32cd189d 81 wxString label1(GetLabelText(title));
a4294b78 82
1a87edf2 83 if (!label1.empty())
a4294b78 84 {
31528cd3 85 wxXmString text(label1);
3dd709d8 86 m_labelWidget = (WXWidget)
9b135950 87 XtVaCreateManagedWidget( label1.mb_str(),
a4294b78 88#if wxUSE_GADGETS
73608949
MB
89 style & wxCOLOURED ? xmLabelWidgetClass
90 : xmLabelGadgetClass,
91 (Widget)m_mainWidget,
a4294b78 92#else
73608949 93 xmLabelWidgetClass, (Widget)m_mainWidget,
a4294b78 94#endif
73608949
MB
95 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
96 XmNlabelString, text(),
11a2ce5a
MB
97// XmNframeChildType is not in Motif 1.2, nor in Lesstif,
98// if it was compiled with 1.2 compatibility
34774093 99// TODO: check this still looks OK for Motif 1.2.
11a2ce5a 100#if (XmVersion > 1200)
73608949 101 XmNframeChildType, XmFRAME_TITLE_CHILD,
2b5f62a0 102#else
73608949 103 XmNchildType, XmFRAME_TITLE_CHILD,
34774093 104#endif
73608949
MB
105 XmNchildVerticalAlignment, XmALIGNMENT_CENTER,
106 NULL);
a4294b78
JS
107 }
108
109 Arg args[3];
110
a4294b78 111 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
af0bb3b1 112 XmHORIZONTAL : XmVERTICAL));
21e0a4d5 113 XtSetArg (args[1], XmNnumColumns, GetMajorDim());
795c00bd 114 XtSetArg (args[2], XmNadjustLast, False);
a4294b78 115
795c00bd 116 Widget radioBoxWidget =
da8cf723 117 XmCreateRadioBox ((Widget)m_mainWidget, wxMOTIF_STR("radioBoxWidget"), args, 3);
a4294b78 118
3bdb8629
MB
119 m_radioButtons.reserve(n);
120 m_radioButtonLabels.reserve(n);
a4294b78 121
a4294b78
JS
122 int i;
123 for (i = 0; i < n; i++)
124 {
32cd189d 125 wxString str(GetLabelText(choices[i]));
3bdb8629 126 m_radioButtonLabels.push_back(str);
73608949 127 Widget radioItem = XtVaCreateManagedWidget (
9b135950 128 wxConstCast(str.mb_str(), char),
a4294b78 129#if wxUSE_GADGETS
73608949 130 xmToggleButtonGadgetClass, radioBoxWidget,
a4294b78 131#else
73608949 132 xmToggleButtonWidgetClass, radioBoxWidget,
a4294b78 133#endif
73608949
MB
134 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
135 NULL);
3bdb8629
MB
136 m_radioButtons.push_back((WXWidget)radioItem);
137 XtAddCallback (radioItem, XmNvalueChangedCallback,
138 (XtCallbackProc) wxRadioBoxCallback,
139 (XtPointer) this);
a4294b78 140 }
a4294b78 141
2ad99bc1 142 SetSelection (0);
1a87edf2 143
2ad99bc1 144 XtRealizeWidget((Widget)m_mainWidget);
a4294b78 145 XtManageChild (radioBoxWidget);
2ad99bc1 146 XtManageChild ((Widget)m_mainWidget);
a4294b78 147
105fbe1f 148 PostCreation();
2ad99bc1 149 AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y);
a4294b78 150
96be256b 151 return true;
4bb6408c
JS
152}
153
584ad2a3
MB
154bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
155 const wxPoint& pos, const wxSize& size,
156 const wxArrayString& choices,
157 int majorDim, long style,
158 const wxValidator& val, const wxString& name)
159{
160 wxCArrayString chs(choices);
161 return Create(parent, id, title, pos, size, chs.GetCount(),
162 chs.GetStrings(), majorDim, style, val, name);
163}
4bb6408c
JS
164
165wxRadioBox::~wxRadioBox()
166{
15d5ab67 167 DetachWidget(m_mainWidget);
15d5ab67 168 XtDestroyWidget((Widget) m_mainWidget);
15d5ab67
JS
169
170 m_mainWidget = (WXWidget) 0;
4bb6408c
JS
171}
172
aa61d352 173void wxRadioBox::SetString(unsigned int item, const wxString& label)
4bb6408c 174{
789f6795 175 if (!IsValid(item))
a4294b78
JS
176 return;
177
aa61d352 178 Widget widget = (Widget)m_radioButtons[item];
1a87edf2 179 if (!label.empty())
a4294b78 180 {
32cd189d 181 wxString label1(GetLabelText(label));
da494b40 182 wxXmString text( label1 );
3bdb8629 183 m_radioButtonLabels[item] = label1;
a4294b78 184 XtVaSetValues (widget,
da494b40 185 XmNlabelString, text(),
af0bb3b1
VZ
186 XmNlabelType, XmSTRING,
187 NULL);
a4294b78 188 }
4bb6408c
JS
189}
190
4bb6408c
JS
191void wxRadioBox::SetSelection(int n)
192{
789f6795 193 if (!IsValid(n))
4bb6408c 194 return;
4bb6408c
JS
195
196 m_selectedButton = n;
a4294b78 197
96be256b 198 m_inSetValue = true;
a4294b78 199
96be256b 200 XmToggleButtonSetState ((Widget) m_radioButtons[n], True, False);
a4294b78 201
aa61d352
VZ
202 for (unsigned int i = 0; i < m_noItems; i++)
203 if (i != (unsigned int)n)
96be256b 204 XmToggleButtonSetState ((Widget) m_radioButtons[i], False, False);
a4294b78 205
96be256b 206 m_inSetValue = false;
4bb6408c
JS
207}
208
209// Get single selection, for single choice list items
210int wxRadioBox::GetSelection() const
211{
212 return m_selectedButton;
213}
214
215// Find string for position
aa61d352 216wxString wxRadioBox::GetString(unsigned int n) const
4bb6408c 217{
789f6795 218 if (!IsValid(n))
a4294b78
JS
219 return wxEmptyString;
220 return m_radioButtonLabels[n];
4bb6408c
JS
221}
222
bfc6fde4 223void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 224{
2ad99bc1 225 bool managed = XtIsManaged((Widget) m_mainWidget);
4bb6408c 226
a4294b78 227 if (managed)
2ad99bc1 228 XtUnmanageChild ((Widget) m_mainWidget);
4bb6408c 229
a4294b78
JS
230 int xx = x; int yy = y;
231 AdjustForParentClientOrigin(xx, yy, sizeFlags);
4bb6408c 232
a4294b78 233 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2ad99bc1 234 XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL);
a4294b78 235 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
1a87edf2 236 XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL);
4bb6408c 237
2ad99bc1
JS
238 if (width > 0)
239 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL);
240 if (height > 0)
241 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL);
a4294b78 242
a4294b78 243 if (managed)
2ad99bc1 244 XtManageChild ((Widget) m_mainWidget);
4bb6408c
JS
245}
246
247// Enable a specific button
aa61d352 248bool wxRadioBox::Enable(unsigned int n, bool enable)
4bb6408c 249{
789f6795 250 if (!IsValid(n))
1a87edf2 251 return false;
a4294b78
JS
252
253 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
1a87edf2 254 return true;
4bb6408c
JS
255}
256
257// Enable all controls
af0bb3b1 258bool wxRadioBox::Enable(bool enable)
4bb6408c 259{
af0bb3b1 260 if ( !wxControl::Enable(enable) )
96be256b 261 return false;
4bb6408c 262
aa61d352 263 for (unsigned int i = 0; i < m_noItems; i++)
a4294b78 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
aa61d352 276bool wxRadioBox::Show(unsigned 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 ();
aa61d352
VZ
310 if (sel != wxNOT_FOUND)
311 return this->GetString((unsigned int)sel);
4bb6408c 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
aa61d352 338 for (unsigned int i = 0; i < m_noItems; i++)
94b49b93
JS
339 {
340 WXWidget radioButton = m_radioButtons[i];
341
342 XtVaSetValues ((Widget) radioButton,
73608949 343 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay((Widget) GetTopWidget())),
94b49b93
JS
344 NULL);
345 }
0d57be45
JS
346}
347
348void wxRadioBox::ChangeBackgroundColour()
349{
94b49b93
JS
350 wxWindow::ChangeBackgroundColour();
351
f516d986 352 wxColour colour = *wxBLACK;
3e0071d9 353 WXPixel selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
9838df2c 354
aa61d352 355 for (unsigned int i = 0; i < m_noItems; i++)
94b49b93
JS
356 {
357 WXWidget radioButton = m_radioButtons[i];
358
96be256b 359 wxDoChangeBackgroundColour(radioButton, m_backgroundColour, true);
9838df2c
JS
360
361 XtVaSetValues ((Widget) radioButton,
362 XmNselectColor, selectPixel,
363 NULL);
94b49b93 364 }
0d57be45
JS
365}
366
367void wxRadioBox::ChangeForegroundColour()
368{
94b49b93
JS
369 wxWindow::ChangeForegroundColour();
370
aa61d352 371 for (unsigned int i = 0; i < m_noItems; i++)
94b49b93
JS
372 {
373 WXWidget radioButton = m_radioButtons[i];
374
a8680e3e 375 wxDoChangeForegroundColour(radioButton, m_foregroundColour);
94b49b93 376 }
0d57be45
JS
377}
378
a4294b78 379void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 380 XmToggleButtonCallbackStruct * cbs)
a4294b78
JS
381{
382 if (!cbs->set)
383 return;
384
385 wxRadioBox *item = (wxRadioBox *) clientData;
386 int sel = -1;
aa61d352 387 unsigned int i;
3bdb8629 388 const wxWidgetArray& buttons = item->GetRadioButtons();
3c85ada9 389 for (i = 0; i < item->GetCount(); i++)
3bdb8629 390 if (((Widget)buttons[i]) == w)
36e14c2b 391 sel = (int)i;
a4294b78
JS
392 item->SetSel(sel);
393
394 if (item->InSetValue())
395 return;
396
397 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
2ad99bc1
JS
398 event.SetInt(sel);
399 event.SetString(item->GetStringSelection());
a4294b78
JS
400 event.SetEventObject(item);
401 item->ProcessCommand (event);
402}
8228b893
WS
403
404#endif // wxUSE_RADIOBOX