]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobox.cpp
Unicode-enabled the STC sample
[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
JS
170
171 SetCanAddEventHandler(TRUE);
2ad99bc1 172 AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y);
a4294b78 173
0d57be45 174 ChangeBackgroundColour();
a4294b78
JS
175
176 return TRUE;
4bb6408c
JS
177}
178
179
180wxRadioBox::~wxRadioBox()
181{
a4294b78
JS
182 delete[] m_radioButtonLabels;
183 delete[] m_radioButtons;
15d5ab67 184
15d5ab67 185 DetachWidget(m_mainWidget);
15d5ab67 186 XtDestroyWidget((Widget) m_mainWidget);
15d5ab67
JS
187
188 m_mainWidget = (WXWidget) 0;
4bb6408c
JS
189}
190
18128cbb 191void wxRadioBox::SetString(int item, const wxString& label)
4bb6408c 192{
a4294b78
JS
193 if (item < 0 || item >= m_noItems)
194 return;
195
196 Widget widget = (Widget) m_radioButtons[item];
197 if (label != "")
198 {
199 wxString label1(wxStripMenuCodes(label));
da494b40 200 wxXmString text( label1 );
a4294b78 201 XtVaSetValues (widget,
da494b40 202 XmNlabelString, text(),
af0bb3b1
VZ
203 XmNlabelType, XmSTRING,
204 NULL);
a4294b78 205 }
4bb6408c
JS
206}
207
208int wxRadioBox::FindString(const wxString& s) const
209{
a4294b78
JS
210 int i;
211 for (i = 0; i < m_noItems; i++)
212 if (s == m_radioButtonLabels[i])
213 return i;
4bb6408c
JS
214 return -1;
215}
216
217void wxRadioBox::SetSelection(int n)
218{
219 if ((n < 0) || (n >= m_noItems))
220 return;
4bb6408c
JS
221
222 m_selectedButton = n;
a4294b78
JS
223
224 m_inSetValue = TRUE;
225
226 XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE);
227
228 int i;
229 for (i = 0; i < m_noItems; i++)
230 if (i != n)
231 XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE);
232
233 m_inSetValue = FALSE;
4bb6408c
JS
234}
235
236// Get single selection, for single choice list items
237int wxRadioBox::GetSelection() const
238{
239 return m_selectedButton;
240}
241
242// Find string for position
243wxString wxRadioBox::GetString(int n) const
244{
a4294b78
JS
245 if ((n < 0) || (n >= m_noItems))
246 return wxEmptyString;
247 return m_radioButtonLabels[n];
4bb6408c
JS
248}
249
bfc6fde4 250void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 251{
2ad99bc1 252 bool managed = XtIsManaged((Widget) m_mainWidget);
4bb6408c 253
a4294b78 254 if (managed)
2ad99bc1 255 XtUnmanageChild ((Widget) m_mainWidget);
4bb6408c 256
a4294b78
JS
257 int xx = x; int yy = y;
258 AdjustForParentClientOrigin(xx, yy, sizeFlags);
4bb6408c 259
a4294b78 260 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2ad99bc1 261 XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL);
a4294b78 262 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2ad99bc1 263 XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL);
4bb6408c 264
2ad99bc1
JS
265 if (width > 0)
266 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL);
267 if (height > 0)
268 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL);
a4294b78 269
a4294b78 270 if (managed)
2ad99bc1 271 XtManageChild ((Widget) m_mainWidget);
4bb6408c
JS
272}
273
274// Enable a specific button
a4294b78 275void wxRadioBox::Enable(int n, bool enable)
4bb6408c 276{
a4294b78
JS
277 if ((n < 0) || (n >= m_noItems))
278 return;
279
280 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
4bb6408c
JS
281}
282
283// Enable all controls
af0bb3b1 284bool wxRadioBox::Enable(bool enable)
4bb6408c 285{
af0bb3b1
VZ
286 if ( !wxControl::Enable(enable) )
287 return FALSE;
4bb6408c 288
a4294b78
JS
289 int i;
290 for (i = 0; i < m_noItems; i++)
291 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
af0bb3b1
VZ
292
293 return TRUE;
4bb6408c
JS
294}
295
9838df2c
JS
296bool wxRadioBox::Show(bool show)
297{
298 // TODO: show/hide all children
299 return wxControl::Show(show);
300}
301
4bb6408c 302// Show a specific button
a4294b78 303void wxRadioBox::Show(int n, bool show)
4bb6408c 304{
a4294b78
JS
305 // This method isn't complete, and we try do do our best...
306 // It's main purpose isn't for allowing Show/Unshow dynamically,
307 // but rather to provide a way to design wxRadioBox such:
308 //
309 // o Val1 o Val2 o Val3
310 // o Val4 o Val6
311 // o Val7 o Val8 o Val9
312 //
313 // In my case, this is a 'direction' box, and the Show(5,False) is
314 // coupled with an Enable(5,False)
315 //
316 if ((n < 0) || (n >= m_noItems))
317 return;
318
319 XtVaSetValues ((Widget) m_radioButtons[n],
320 XmNindicatorOn, (unsigned char) show,
321 NULL);
322
323 // Please note that this is all we can do: removing the label
324 // if switching to unshow state. However, when switching
18128cbb 325 // to the on state, it's the prog. resp. to call SetString(item,...)
a4294b78
JS
326 // after this call!!
327 if (!show)
18128cbb 328 wxRadioBox::SetString (n, " ");
4bb6408c
JS
329}
330
331// For single selection items only
332wxString wxRadioBox::GetStringSelection () const
333{
334 int sel = GetSelection ();
335 if (sel > -1)
336 return this->GetString (sel);
337 else
338 return wxString("");
339}
340
341bool wxRadioBox::SetStringSelection (const wxString& s)
342{
343 int sel = FindString (s);
344 if (sel > -1)
345 {
346 SetSelection (sel);
347 return TRUE;
348 }
349 else
350 return FALSE;
351}
352
353void wxRadioBox::Command (wxCommandEvent & event)
354{
355 SetSelection (event.m_commandInt);
356 ProcessCommand (event);
357}
358
4b5f3fe6 359void wxRadioBox::ChangeFont(bool keepOriginalSize)
0d57be45 360{
94b49b93
JS
361 wxWindow::ChangeFont(keepOriginalSize);
362
da494b40
MB
363 WXFontType fontType =
364 m_font.GetFontType(XtDisplay((Widget) GetTopWidget()));
94b49b93
JS
365
366 int i;
367 for (i = 0; i < m_noItems; i++)
368 {
369 WXWidget radioButton = m_radioButtons[i];
370
371 XtVaSetValues ((Widget) radioButton,
da494b40 372 wxFont::GetFontTag(), fontType,
94b49b93
JS
373 NULL);
374 }
0d57be45
JS
375}
376
377void wxRadioBox::ChangeBackgroundColour()
378{
94b49b93
JS
379 wxWindow::ChangeBackgroundColour();
380
9838df2c
JS
381 int selectPixel = wxBLACK->AllocColour(wxGetDisplay());
382
94b49b93
JS
383 int i;
384 for (i = 0; i < m_noItems; i++)
385 {
386 WXWidget radioButton = m_radioButtons[i];
387
388 DoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE);
9838df2c
JS
389
390 XtVaSetValues ((Widget) radioButton,
391 XmNselectColor, selectPixel,
392 NULL);
94b49b93 393 }
0d57be45
JS
394}
395
396void wxRadioBox::ChangeForegroundColour()
397{
94b49b93
JS
398 wxWindow::ChangeForegroundColour();
399
400 int i;
401 for (i = 0; i < m_noItems; i++)
402 {
403 WXWidget radioButton = m_radioButtons[i];
404
405 DoChangeForegroundColour(radioButton, m_foregroundColour);
406 }
0d57be45
JS
407}
408
18128cbb
MB
409static int CalcOtherDim( int items, int dim )
410{
411 return items / dim + ( items % dim ? 1 : 0 );
412}
413
414int wxRadioBox::GetRowCount() const
415{
416 return m_windowStyle & wxRA_SPECIFY_ROWS ? m_noRowsOrCols
417 : CalcOtherDim( GetCount(), m_noRowsOrCols );
418}
419
420int wxRadioBox::GetColumnCount() const
421{
422 return m_windowStyle & wxRA_SPECIFY_COLS ? m_noRowsOrCols
423 : CalcOtherDim( GetCount(), m_noRowsOrCols );
424}
425
a4294b78 426void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 427 XmToggleButtonCallbackStruct * cbs)
a4294b78
JS
428{
429 if (!cbs->set)
430 return;
431
432 wxRadioBox *item = (wxRadioBox *) clientData;
433 int sel = -1;
434 int i;
3c85ada9 435 for (i = 0; i < item->GetCount(); i++)
a4294b78
JS
436 if (item->GetRadioButtons() && ((Widget) (item->GetRadioButtons()[i]) == w))
437 sel = i;
438 item->SetSel(sel);
439
440 if (item->InSetValue())
441 return;
442
443 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
2ad99bc1
JS
444 event.SetInt(sel);
445 event.SetString(item->GetStringSelection());
a4294b78
JS
446 event.SetEventObject(item);
447 item->ProcessCommand (event);
448}
4bb6408c 449