]> git.saurik.com Git - wxWidgets.git/blame - src/motif/radiobox.cpp
Updated wxDataStream (added ByteOrder)
[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
16#include "wx/radiobox.h"
a4294b78
JS
17#include "wx/utils.h"
18
19#include <Xm/Label.h>
20#include <Xm/LabelG.h>
21#include <Xm/ToggleB.h>
22#include <Xm/ToggleBG.h>
23#include <Xm/RowColumn.h>
24#include <Xm/Form.h>
9838df2c 25#include <Xm/Frame.h>
a4294b78
JS
26
27#include <wx/motif/private.h>
28
29void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 30 XmToggleButtonCallbackStruct * cbs);
4bb6408c
JS
31
32#if !USE_SHARED_LIBRARY
33IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
34#endif
35
36// Radio box item
37wxRadioBox::wxRadioBox()
38{
39 m_selectedButton = -1;
40 m_noItems = 0;
41 m_noRowsOrCols = 0;
42 m_majorDim = 0 ;
a4294b78
JS
43
44 m_formWidget = (WXWidget) 0;
9838df2c 45 m_frameWidget = (WXWidget) 0;
a4294b78
JS
46 m_labelWidget = (WXWidget) 0;
47 m_radioButtons = (WXWidget*) NULL;
48 m_radioButtonLabels = (wxString*) NULL;
4bb6408c
JS
49}
50
51bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
52 const wxPoint& pos, const wxSize& size,
53 int n, const wxString choices[],
54 int majorDim, long style,
55 const wxValidator& val, const wxString& name)
56{
57 m_selectedButton = -1;
58 m_noItems = n;
9838df2c
JS
59 m_formWidget = (WXWidget) 0;
60 m_frameWidget = (WXWidget) 0;
a4294b78
JS
61 m_labelWidget = (WXWidget) 0;
62 m_radioButtons = (WXWidget*) NULL;
63 m_radioButtonLabels = (wxString*) NULL;
0d57be45
JS
64 m_backgroundColour = parent->GetBackgroundColour();
65 m_foregroundColour = parent->GetForegroundColour();
da175b2c 66 m_font = parent->GetFont();
4bb6408c
JS
67
68 SetName(name);
69 SetValidator(val);
70
71 parent->AddChild(this);
72
73 m_windowStyle = (long&)style;
74
75 if (id == -1)
76 m_windowId = NewControlId();
77 else
78 m_windowId = id;
79
80 m_noRowsOrCols = majorDim;
81
82 if (majorDim==0)
83 m_majorDim = n ;
84 else
85 m_majorDim = majorDim ;
86
a4294b78
JS
87 Widget parentWidget = (Widget) parent->GetClientWidget();
88
89 wxString label1(wxStripMenuCodes(title));
90
31528cd3 91 wxXmString text(label1.c_str());
a4294b78 92
31528cd3 93 Widget formWidget = XtVaCreateManagedWidget (name.c_str(),
af0bb3b1
VZ
94 xmFormWidgetClass, parentWidget,
95 XmNmarginHeight, 0,
96 XmNmarginWidth, 0,
97 NULL);
a4294b78
JS
98
99 m_formWidget = (WXWidget) formWidget;
100
da175b2c 101 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget));
a4294b78
JS
102 if (label1 != "")
103 {
31528cd3
VZ
104 wxXmString text(label1);
105 (void)XtVaCreateManagedWidget(label1.c_str(),
a4294b78 106#if wxUSE_GADGETS
31528cd3
VZ
107 style & wxCOLOURED ? xmLabelWidgetClass
108 : xmLabelGadgetClass,
af0bb3b1 109 formWidget,
a4294b78 110#else
af0bb3b1 111 xmLabelWidgetClass, formWidget,
a4294b78 112#endif
ea57084d 113 XmNfontList, fontList,
af0bb3b1
VZ
114 XmNlabelString, text,
115 NULL);
a4294b78
JS
116 }
117
9838df2c 118 Widget frameWidget = XtVaCreateManagedWidget ("frame",
af0bb3b1 119 xmFrameWidgetClass, formWidget,
9838df2c 120 XmNshadowType, XmSHADOW_IN,
af0bb3b1
VZ
121// XmNmarginHeight, 0,
122// XmNmarginWidth, 0,
123 NULL);
9838df2c
JS
124
125 m_frameWidget = (WXWidget) frameWidget;
126
a4294b78
JS
127 Arg args[3];
128
129 majorDim = (n + majorDim - 1) / majorDim;
130
131 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
af0bb3b1 132 XmHORIZONTAL : XmVERTICAL));
a4294b78
JS
133 XtSetArg (args[1], XmNnumColumns, majorDim);
134
9838df2c 135 Widget radioBoxWidget = XmCreateRadioBox (frameWidget, "radioBoxWidget", args, 2);
a4294b78 136 m_mainWidget = (WXWidget) radioBoxWidget;
4bb6408c 137
a4294b78
JS
138
139 if (m_labelWidget)
af0bb3b1
VZ
140 XtVaSetValues ((Widget) m_labelWidget,
141 XmNtopAttachment, XmATTACH_FORM,
142 XmNleftAttachment, XmATTACH_FORM,
143 XmNalignment, XmALIGNMENT_BEGINNING,
144 NULL);
a4294b78
JS
145
146 XtVaSetValues (radioBoxWidget,
af0bb3b1
VZ
147 XmNtopAttachment, m_labelWidget ? XmATTACH_WIDGET : XmATTACH_FORM,
148 XmNtopWidget, m_labelWidget ? (Widget) m_labelWidget : formWidget,
149 XmNbottomAttachment, XmATTACH_FORM,
150 XmNleftAttachment, XmATTACH_FORM,
151 XmNrightAttachment, XmATTACH_FORM,
152 NULL);
a4294b78
JS
153
154 // if (style & wxFLAT)
155 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
156
157 m_radioButtons = new WXWidget[n];
158 m_radioButtonLabels = new wxString[n];
159 int i;
160 for (i = 0; i < n; i++)
161 {
162 wxString str(wxStripMenuCodes(choices[i]));
163 m_radioButtonLabels[i] = str;
164 m_radioButtons[i] = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) str,
165#if wxUSE_GADGETS
af0bb3b1 166 xmToggleButtonGadgetClass, radioBoxWidget,
a4294b78 167#else
af0bb3b1 168 xmToggleButtonWidgetClass, radioBoxWidget,
a4294b78 169#endif
ea57084d 170 XmNfontList, fontList,
af0bb3b1 171 NULL);
a4294b78 172 XtAddCallback ((Widget) m_radioButtons[i], XmNvalueChangedCallback, (XtCallbackProc) wxRadioBoxCallback,
af0bb3b1 173 (XtCallbackProc) this);
a4294b78 174
a4294b78
JS
175 }
176 SetSelection (0);
177
da175b2c 178 m_font = parent->GetFont();
4b5f3fe6
JS
179 ChangeFont(FALSE);
180
9838df2c 181 // XtManageChild((Widget) m_formWidget);
a4294b78
JS
182 XtManageChild (radioBoxWidget);
183
184 SetCanAddEventHandler(TRUE);
185 AttachWidget (parent, m_mainWidget, m_formWidget, pos.x, pos.y, size.x, size.y);
186
0d57be45 187 ChangeBackgroundColour();
a4294b78
JS
188
189 return TRUE;
4bb6408c
JS
190}
191
192
193wxRadioBox::~wxRadioBox()
194{
a4294b78
JS
195 delete[] m_radioButtonLabels;
196 delete[] m_radioButtons;
15d5ab67 197
b412f9be 198 DetachWidget(m_formWidget);
15d5ab67
JS
199 DetachWidget(m_mainWidget);
200
201 if (m_labelWidget)
202 XtDestroyWidget((Widget) m_labelWidget);
203 XtDestroyWidget((Widget) m_mainWidget);
204 XtDestroyWidget((Widget) m_formWidget);
205
206 m_mainWidget = (WXWidget) 0;
207 m_formWidget = (WXWidget) 0;
208 m_labelWidget = (WXWidget) 0;
4bb6408c
JS
209}
210
211wxString wxRadioBox::GetLabel(int item) const
212{
a4294b78
JS
213 if (item < 0 || item >= m_noItems)
214 return wxEmptyString;
215
216 Widget widget = (Widget) m_radioButtons[item];
217 XmString text;
218 char *s;
219 XtVaGetValues (widget,
af0bb3b1
VZ
220 XmNlabelString, &text,
221 NULL);
a4294b78
JS
222
223 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
224 {
225 // Should we free 'text'???
226 XmStringFree(text);
227 wxString str(s);
228 XtFree (s);
229 return str;
230 }
231 else
232 {
233 XmStringFree(text);
234 return wxEmptyString;
235 }
4bb6408c
JS
236}
237
238void wxRadioBox::SetLabel(int item, const wxString& label)
239{
a4294b78
JS
240 if (item < 0 || item >= m_noItems)
241 return;
242
243 Widget widget = (Widget) m_radioButtons[item];
244 if (label != "")
245 {
246 wxString label1(wxStripMenuCodes(label));
247 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
248 XtVaSetValues (widget,
af0bb3b1
VZ
249 XmNlabelString, text,
250 XmNlabelType, XmSTRING,
251 NULL);
a4294b78
JS
252 XmStringFree (text);
253 }
4bb6408c
JS
254}
255
256int wxRadioBox::FindString(const wxString& s) const
257{
a4294b78
JS
258 int i;
259 for (i = 0; i < m_noItems; i++)
260 if (s == m_radioButtonLabels[i])
261 return i;
4bb6408c
JS
262 return -1;
263}
264
265void wxRadioBox::SetSelection(int n)
266{
267 if ((n < 0) || (n >= m_noItems))
268 return;
4bb6408c
JS
269
270 m_selectedButton = n;
a4294b78
JS
271
272 m_inSetValue = TRUE;
273
274 XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE);
275
276 int i;
277 for (i = 0; i < m_noItems; i++)
278 if (i != n)
279 XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE);
280
281 m_inSetValue = FALSE;
4bb6408c
JS
282}
283
284// Get single selection, for single choice list items
285int wxRadioBox::GetSelection() const
286{
287 return m_selectedButton;
288}
289
290// Find string for position
291wxString wxRadioBox::GetString(int n) const
292{
a4294b78
JS
293 if ((n < 0) || (n >= m_noItems))
294 return wxEmptyString;
295 return m_radioButtonLabels[n];
4bb6408c
JS
296}
297
bfc6fde4 298void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 299{
a4294b78 300 bool managed = XtIsManaged((Widget) m_formWidget);
4bb6408c 301
a4294b78
JS
302 if (managed)
303 XtUnmanageChild ((Widget) m_formWidget);
4bb6408c 304
a4294b78
JS
305 int xx = x; int yy = y;
306 AdjustForParentClientOrigin(xx, yy, sizeFlags);
4bb6408c 307
a4294b78
JS
308 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
309 XtVaSetValues ((Widget) m_formWidget, XmNleftAttachment, XmATTACH_SELF,
af0bb3b1 310 XmNx, xx, NULL);
a4294b78
JS
311 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
312 XtVaSetValues ((Widget) m_formWidget, XmNtopAttachment, XmATTACH_SELF,
af0bb3b1 313 XmNy, yy, NULL);
4bb6408c 314
a4294b78
JS
315 // Must set the actual RadioBox to be desired size MINUS label size
316 Dimension labelWidth = 0, labelHeight = 0, actualWidth = 0, actualHeight = 0;
4bb6408c 317
a4294b78
JS
318 if (m_labelWidget)
319 XtVaGetValues ((Widget) m_labelWidget, XmNwidth, &labelWidth, XmNheight, &labelHeight, NULL);
4bb6408c 320
a4294b78
JS
321 actualWidth = width;
322 actualHeight = height - labelHeight;
323
324 if (width > -1)
325 {
326 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, actualWidth, NULL);
327 }
328 if (height > -1)
329 {
330 XtVaSetValues ((Widget) m_mainWidget, XmNheight, actualHeight, NULL);
331 }
332 if (managed)
333 XtManageChild ((Widget) m_formWidget);
4bb6408c
JS
334}
335
336// Enable a specific button
a4294b78 337void wxRadioBox::Enable(int n, bool enable)
4bb6408c 338{
a4294b78
JS
339 if ((n < 0) || (n >= m_noItems))
340 return;
341
342 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
4bb6408c
JS
343}
344
345// Enable all controls
af0bb3b1 346bool wxRadioBox::Enable(bool enable)
4bb6408c 347{
af0bb3b1
VZ
348 if ( !wxControl::Enable(enable) )
349 return FALSE;
4bb6408c 350
a4294b78
JS
351 int i;
352 for (i = 0; i < m_noItems; i++)
353 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
af0bb3b1
VZ
354
355 return TRUE;
4bb6408c
JS
356}
357
9838df2c
JS
358bool wxRadioBox::Show(bool show)
359{
360 // TODO: show/hide all children
361 return wxControl::Show(show);
362}
363
4bb6408c 364// Show a specific button
a4294b78 365void wxRadioBox::Show(int n, bool show)
4bb6408c 366{
a4294b78
JS
367 // This method isn't complete, and we try do do our best...
368 // It's main purpose isn't for allowing Show/Unshow dynamically,
369 // but rather to provide a way to design wxRadioBox such:
370 //
371 // o Val1 o Val2 o Val3
372 // o Val4 o Val6
373 // o Val7 o Val8 o Val9
374 //
375 // In my case, this is a 'direction' box, and the Show(5,False) is
376 // coupled with an Enable(5,False)
377 //
378 if ((n < 0) || (n >= m_noItems))
379 return;
380
381 XtVaSetValues ((Widget) m_radioButtons[n],
382 XmNindicatorOn, (unsigned char) show,
383 NULL);
384
385 // Please note that this is all we can do: removing the label
386 // if switching to unshow state. However, when switching
387 // to the on state, it's the prog. resp. to call SetLabel(item,...)
388 // after this call!!
389 if (!show)
390 wxRadioBox::SetLabel (n, " ");
4bb6408c
JS
391}
392
393// For single selection items only
394wxString wxRadioBox::GetStringSelection () const
395{
396 int sel = GetSelection ();
397 if (sel > -1)
398 return this->GetString (sel);
399 else
400 return wxString("");
401}
402
403bool wxRadioBox::SetStringSelection (const wxString& s)
404{
405 int sel = FindString (s);
406 if (sel > -1)
407 {
408 SetSelection (sel);
409 return TRUE;
410 }
411 else
412 return FALSE;
413}
414
415void wxRadioBox::Command (wxCommandEvent & event)
416{
417 SetSelection (event.m_commandInt);
418 ProcessCommand (event);
419}
420
4b5f3fe6 421void wxRadioBox::ChangeFont(bool keepOriginalSize)
0d57be45 422{
94b49b93
JS
423 wxWindow::ChangeFont(keepOriginalSize);
424
da175b2c 425 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) GetTopWidget()));
94b49b93
JS
426
427 int i;
428 for (i = 0; i < m_noItems; i++)
429 {
430 WXWidget radioButton = m_radioButtons[i];
431
432 XtVaSetValues ((Widget) radioButton,
433 XmNfontList, fontList,
af0bb3b1 434 XmNtopAttachment, XmATTACH_FORM,
94b49b93
JS
435 NULL);
436 }
0d57be45
JS
437}
438
439void wxRadioBox::ChangeBackgroundColour()
440{
94b49b93
JS
441 wxWindow::ChangeBackgroundColour();
442
9838df2c
JS
443 DoChangeBackgroundColour((Widget) m_frameWidget, m_backgroundColour);
444
445 int selectPixel = wxBLACK->AllocColour(wxGetDisplay());
446
94b49b93
JS
447 int i;
448 for (i = 0; i < m_noItems; i++)
449 {
450 WXWidget radioButton = m_radioButtons[i];
451
452 DoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE);
9838df2c
JS
453
454 XtVaSetValues ((Widget) radioButton,
455 XmNselectColor, selectPixel,
456 NULL);
94b49b93 457 }
0d57be45
JS
458}
459
460void wxRadioBox::ChangeForegroundColour()
461{
94b49b93
JS
462 wxWindow::ChangeForegroundColour();
463
464 int i;
465 for (i = 0; i < m_noItems; i++)
466 {
467 WXWidget radioButton = m_radioButtons[i];
468
469 DoChangeForegroundColour(radioButton, m_foregroundColour);
470 }
0d57be45
JS
471}
472
a4294b78 473void wxRadioBoxCallback (Widget w, XtPointer clientData,
af0bb3b1 474 XmToggleButtonCallbackStruct * cbs)
a4294b78
JS
475{
476 if (!cbs->set)
477 return;
478
479 wxRadioBox *item = (wxRadioBox *) clientData;
480 int sel = -1;
481 int i;
482 for (i = 0; i < item->Number(); i++)
483 if (item->GetRadioButtons() && ((Widget) (item->GetRadioButtons()[i]) == w))
484 sel = i;
485 item->SetSel(sel);
486
487 if (item->InSetValue())
488 return;
489
490 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
491 event.m_commandInt = sel;
492 event.SetEventObject(item);
493 item->ProcessCommand (event);
494}
4bb6408c 495