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