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"
18 #define XtDisplay XTDISPLAY
21 #include "wx/radiobox.h"
25 #include "wx/arrstr.h"
29 #pragma message disable nosimpint
32 #include <Xm/LabelG.h>
33 #include <Xm/ToggleB.h>
34 #include <Xm/ToggleBG.h>
35 #include <Xm/RowColumn.h>
38 #pragma message enable nosimpint
41 #include "wx/motif/private.h"
43 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
44 XmToggleButtonCallbackStruct
* cbs
);
46 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
49 void wxRadioBox::Init()
51 m_selectedButton
= -1;
56 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
57 const wxPoint
& pos
, const wxSize
& size
,
58 int n
, const wxString choices
[],
59 int majorDim
, long style
,
60 const wxValidator
& val
, const wxString
& name
)
62 if( !CreateControl( parent
, id
, pos
, size
, style
, val
, name
) )
65 m_noItems
= (unsigned int)n
;
66 m_noRowsOrCols
= majorDim
;
68 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
70 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
71 Display
* dpy
= XtDisplay(parentWidget
);
73 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
74 xmFrameWidgetClass
, parentWidget
,
75 XmNresizeHeight
, True
,
79 wxString
label1(GetLabelText(title
));
83 wxXmString
text(label1
);
84 m_labelWidget
= (WXWidget
)
85 XtVaCreateManagedWidget( label1
.c_str(),
87 style
& wxCOLOURED
? xmLabelWidgetClass
91 xmLabelWidgetClass
, (Widget
)m_mainWidget
,
93 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
94 XmNlabelString
, text(),
95 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
96 // if it was compiled with 1.2 compatibility
97 // TODO: check this still looks OK for Motif 1.2.
98 #if (XmVersion > 1200)
99 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
101 XmNchildType
, XmFRAME_TITLE_CHILD
,
103 XmNchildVerticalAlignment
, XmALIGNMENT_CENTER
,
109 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
110 XmHORIZONTAL
: XmVERTICAL
));
111 XtSetArg (args
[1], XmNnumColumns
, GetMajorDim());
112 XtSetArg (args
[2], XmNadjustLast
, False
);
114 Widget radioBoxWidget
=
115 XmCreateRadioBox ((Widget
)m_mainWidget
, wxMOTIF_STR("radioBoxWidget"), args
, 3);
117 m_radioButtons
.reserve(n
);
118 m_radioButtonLabels
.reserve(n
);
121 for (i
= 0; i
< n
; i
++)
123 wxString
str(GetLabelText(choices
[i
]));
124 m_radioButtonLabels
.push_back(str
);
125 Widget radioItem
= XtVaCreateManagedWidget (
126 wxConstCast(str
.c_str(), char),
128 xmToggleButtonGadgetClass
, radioBoxWidget
,
130 xmToggleButtonWidgetClass
, radioBoxWidget
,
132 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
134 m_radioButtons
.push_back((WXWidget
)radioItem
);
135 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
136 (XtCallbackProc
) wxRadioBoxCallback
,
144 XtRealizeWidget((Widget
)m_mainWidget
);
145 XtManageChild (radioBoxWidget
);
146 XtManageChild ((Widget
)m_mainWidget
);
148 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
150 ChangeBackgroundColour();
155 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
156 const wxPoint
& pos
, const wxSize
& size
,
157 const wxArrayString
& choices
,
158 int majorDim
, long style
,
159 const wxValidator
& val
, const wxString
& name
)
161 wxCArrayString
chs(choices
);
162 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
163 chs
.GetStrings(), majorDim
, style
, val
, name
);
166 wxRadioBox::~wxRadioBox()
168 DetachWidget(m_mainWidget
);
169 XtDestroyWidget((Widget
) m_mainWidget
);
171 m_mainWidget
= (WXWidget
) 0;
174 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
179 Widget widget
= (Widget
)m_radioButtons
[item
];
182 wxString
label1(GetLabelText(label
));
183 wxXmString
text( label1
);
184 m_radioButtonLabels
[item
] = label1
;
185 XtVaSetValues (widget
,
186 XmNlabelString
, text(),
187 XmNlabelType
, XmSTRING
,
192 void wxRadioBox::SetSelection(int n
)
197 m_selectedButton
= n
;
201 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], True
, False
);
203 for (unsigned int i
= 0; i
< m_noItems
; i
++)
204 if (i
!= (unsigned int)n
)
205 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], False
, False
);
207 m_inSetValue
= false;
210 // Get single selection, for single choice list items
211 int wxRadioBox::GetSelection() const
213 return m_selectedButton
;
216 // Find string for position
217 wxString
wxRadioBox::GetString(unsigned int n
) const
220 return wxEmptyString
;
221 return m_radioButtonLabels
[n
];
224 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
226 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
229 XtUnmanageChild ((Widget
) m_mainWidget
);
231 int xx
= x
; int yy
= y
;
232 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
234 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
235 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
236 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
237 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
240 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
242 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
245 XtManageChild ((Widget
) m_mainWidget
);
248 // Enable a specific button
249 bool wxRadioBox::Enable(unsigned int n
, bool enable
)
254 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
258 // Enable all controls
259 bool wxRadioBox::Enable(bool enable
)
261 if ( !wxControl::Enable(enable
) )
264 for (unsigned int i
= 0; i
< m_noItems
; i
++)
265 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
270 bool wxRadioBox::Show(bool show
)
272 // TODO: show/hide all children
273 return wxControl::Show(show
);
276 // Show a specific button
277 bool wxRadioBox::Show(unsigned int n
, bool show
)
279 // This method isn't complete, and we try do do our best...
280 // It's main purpose isn't for allowing Show/Unshow dynamically,
281 // but rather to provide a way to design wxRadioBox such:
283 // o Val1 o Val2 o Val3
285 // o Val7 o Val8 o Val9
287 // In my case, this is a 'direction' box, and the Show(5,False) is
288 // coupled with an Enable(5,False)
293 XtVaSetValues ((Widget
) m_radioButtons
[n
],
294 XmNindicatorOn
, (unsigned char) show
,
297 // Please note that this is all we can do: removing the label
298 // if switching to unshow state. However, when switching
299 // to the on state, it's the prog. resp. to call SetString(item,...)
302 wxRadioBox::SetString (n
, " ");
307 // For single selection items only
308 wxString
wxRadioBox::GetStringSelection () const
310 int sel
= GetSelection ();
311 if (sel
!= wxNOT_FOUND
)
312 return this->GetString((unsigned int)sel
);
314 return wxEmptyString
;
317 bool wxRadioBox::SetStringSelection (const wxString
& s
)
319 int sel
= FindString (s
);
329 void wxRadioBox::Command (wxCommandEvent
& event
)
331 SetSelection (event
.GetInt());
332 ProcessCommand (event
);
335 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
337 wxWindow::ChangeFont(keepOriginalSize
);
339 for (unsigned int i
= 0; i
< m_noItems
; i
++)
341 WXWidget radioButton
= m_radioButtons
[i
];
343 XtVaSetValues ((Widget
) radioButton
,
344 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay((Widget
) GetTopWidget())),
349 void wxRadioBox::ChangeBackgroundColour()
351 wxWindow::ChangeBackgroundColour();
353 wxColour colour
= *wxBLACK
;
354 int selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
356 for (unsigned int i
= 0; i
< m_noItems
; i
++)
358 WXWidget radioButton
= m_radioButtons
[i
];
360 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, true);
362 XtVaSetValues ((Widget
) radioButton
,
363 XmNselectColor
, selectPixel
,
368 void wxRadioBox::ChangeForegroundColour()
370 wxWindow::ChangeForegroundColour();
372 for (unsigned int i
= 0; i
< m_noItems
; i
++)
374 WXWidget radioButton
= m_radioButtons
[i
];
376 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
380 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
381 XmToggleButtonCallbackStruct
* cbs
)
386 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
389 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
390 for (i
= 0; i
< item
->GetCount(); i
++)
391 if (((Widget
)buttons
[i
]) == w
)
395 if (item
->InSetValue())
398 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
400 event
.SetString(item
->GetStringSelection());
401 event
.SetEventObject(item
);
402 item
->ProcessCommand (event
);
405 #endif // wxUSE_RADIOBOX