1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/radiobox.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/radiobox.h"
21 #include "wx/arrstr.h"
25 #pragma message disable nosimpint
28 #include <Xm/LabelG.h>
29 #include <Xm/ToggleB.h>
30 #include <Xm/ToggleBG.h>
31 #include <Xm/RowColumn.h>
34 #pragma message enable nosimpint
37 #include "wx/motif/private.h"
39 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
40 XmToggleButtonCallbackStruct
* cbs
);
42 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
45 void wxRadioBox::Init()
47 m_selectedButton
= -1;
50 m_labelWidget
= (WXWidget
) 0;
53 bool 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
)
59 if( !CreateControl( parent
, id
, pos
, size
, style
, val
, name
) )
63 m_noItems
= (unsigned int)n
;
64 m_noRowsOrCols
= majorDim
;
66 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
68 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
69 Display
* dpy
= XtDisplay(parentWidget
);
71 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
72 xmFrameWidgetClass
, parentWidget
,
73 XmNresizeHeight
, True
,
77 wxString
label1(GetLabelText(title
));
81 wxXmString
text(label1
);
82 m_labelWidget
= (WXWidget
)
83 XtVaCreateManagedWidget( label1
.mb_str(),
85 style
& wxCOLOURED
? xmLabelWidgetClass
89 xmLabelWidgetClass
, (Widget
)m_mainWidget
,
91 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
92 XmNlabelString
, text(),
93 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
94 // if it was compiled with 1.2 compatibility
95 // TODO: check this still looks OK for Motif 1.2.
96 #if (XmVersion > 1200)
97 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
99 XmNchildType
, XmFRAME_TITLE_CHILD
,
101 XmNchildVerticalAlignment
, XmALIGNMENT_CENTER
,
107 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
108 XmHORIZONTAL
: XmVERTICAL
));
109 XtSetArg (args
[1], XmNnumColumns
, GetMajorDim());
110 XtSetArg (args
[2], XmNadjustLast
, False
);
112 Widget radioBoxWidget
=
113 XmCreateRadioBox ((Widget
)m_mainWidget
, wxMOTIF_STR("radioBoxWidget"), args
, 3);
115 m_radioButtons
.reserve(n
);
116 m_radioButtonLabels
.reserve(n
);
119 for (i
= 0; i
< n
; i
++)
121 wxString
str(GetLabelText(choices
[i
]));
122 m_radioButtonLabels
.push_back(str
);
123 Widget radioItem
= XtVaCreateManagedWidget (
126 xmToggleButtonGadgetClass
, radioBoxWidget
,
128 xmToggleButtonWidgetClass
, radioBoxWidget
,
130 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
132 m_radioButtons
.push_back((WXWidget
)radioItem
);
133 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
134 (XtCallbackProc
) wxRadioBoxCallback
,
140 XtRealizeWidget((Widget
)m_mainWidget
);
141 XtManageChild (radioBoxWidget
);
142 XtManageChild ((Widget
)m_mainWidget
);
145 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
150 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
151 const wxPoint
& pos
, const wxSize
& size
,
152 const wxArrayString
& choices
,
153 int majorDim
, long style
,
154 const wxValidator
& val
, const wxString
& name
)
156 wxCArrayString
chs(choices
);
157 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
158 chs
.GetStrings(), majorDim
, style
, val
, name
);
161 wxRadioBox::~wxRadioBox()
163 DetachWidget(m_mainWidget
);
164 XtDestroyWidget((Widget
) m_mainWidget
);
166 m_mainWidget
= (WXWidget
) 0;
169 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
174 Widget widget
= (Widget
)m_radioButtons
[item
];
177 wxString
label1(GetLabelText(label
));
178 wxXmString
text( label1
);
179 m_radioButtonLabels
[item
] = label1
;
180 XtVaSetValues (widget
,
181 XmNlabelString
, text(),
182 XmNlabelType
, XmSTRING
,
187 void wxRadioBox::SetSelection(int n
)
192 m_selectedButton
= n
;
196 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], True
, False
);
198 for (unsigned int i
= 0; i
< m_noItems
; i
++)
199 if (i
!= (unsigned int)n
)
200 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], False
, False
);
202 m_inSetValue
= false;
205 // Get single selection, for single choice list items
206 int wxRadioBox::GetSelection() const
208 return m_selectedButton
;
211 // Find string for position
212 wxString
wxRadioBox::GetString(unsigned int n
) const
215 return wxEmptyString
;
216 return m_radioButtonLabels
[n
];
219 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
221 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
224 XtUnmanageChild ((Widget
) m_mainWidget
);
226 int xx
= x
; int yy
= y
;
227 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
229 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
230 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
231 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
232 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
235 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
237 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
240 XtManageChild ((Widget
) m_mainWidget
);
243 // Enable a specific button
244 bool wxRadioBox::Enable(unsigned int n
, bool enable
)
249 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
253 // Enable all controls
254 bool wxRadioBox::Enable(bool enable
)
256 if ( !wxControl::Enable(enable
) )
259 for (unsigned int i
= 0; i
< m_noItems
; i
++)
260 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
265 bool wxRadioBox::Show(bool show
)
267 // TODO: show/hide all children
268 return wxControl::Show(show
);
271 // Show a specific button
272 bool wxRadioBox::Show(unsigned int n
, bool show
)
274 // This method isn't complete, and we try do do our best...
275 // It's main purpose isn't for allowing Show/Unshow dynamically,
276 // but rather to provide a way to design wxRadioBox such:
278 // o Val1 o Val2 o Val3
280 // o Val7 o Val8 o Val9
282 // In my case, this is a 'direction' box, and the Show(5,False) is
283 // coupled with an Enable(5,False)
288 XtVaSetValues ((Widget
) m_radioButtons
[n
],
289 XmNindicatorOn
, (unsigned char) show
,
292 // Please note that this is all we can do: removing the label
293 // if switching to unshow state. However, when switching
294 // to the on state, it's the prog. resp. to call SetString(item,...)
297 wxRadioBox::SetString (n
, " ");
302 // For single selection items only
303 wxString
wxRadioBox::GetStringSelection () const
305 int sel
= GetSelection ();
306 if (sel
!= wxNOT_FOUND
)
307 return this->GetString((unsigned int)sel
);
309 return wxEmptyString
;
312 bool wxRadioBox::SetStringSelection (const wxString
& s
)
314 int sel
= FindString (s
);
324 void wxRadioBox::Command (wxCommandEvent
& event
)
326 SetSelection (event
.GetInt());
327 ProcessCommand (event
);
330 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
332 wxWindow::ChangeFont(keepOriginalSize
);
334 for (unsigned int i
= 0; i
< m_noItems
; i
++)
336 WXWidget radioButton
= m_radioButtons
[i
];
338 XtVaSetValues ((Widget
) radioButton
,
339 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay((Widget
) GetTopWidget())),
344 void wxRadioBox::ChangeBackgroundColour()
346 wxWindow::ChangeBackgroundColour();
348 wxColour colour
= *wxBLACK
;
349 WXPixel selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
351 for (unsigned int i
= 0; i
< m_noItems
; i
++)
353 WXWidget radioButton
= m_radioButtons
[i
];
355 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, true);
357 XtVaSetValues ((Widget
) radioButton
,
358 XmNselectColor
, selectPixel
,
363 void wxRadioBox::ChangeForegroundColour()
365 wxWindow::ChangeForegroundColour();
367 for (unsigned int i
= 0; i
< m_noItems
; i
++)
369 WXWidget radioButton
= m_radioButtons
[i
];
371 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
375 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
376 XmToggleButtonCallbackStruct
* cbs
)
381 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
384 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
385 for (i
= 0; i
< item
->GetCount(); i
++)
386 if (((Widget
)buttons
[i
]) == w
)
390 if (item
->InSetValue())
393 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
395 event
.SetString(item
->GetStringSelection());
396 event
.SetEventObject(item
);
397 item
->ProcessCommand (event
);
400 #endif // wxUSE_RADIOBOX