1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/radiobox.cpp
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
16 #include "wx/radiobox.h"
20 #include "wx/arrstr.h"
24 #pragma message disable nosimpint
27 #include <Xm/LabelG.h>
28 #include <Xm/ToggleB.h>
29 #include <Xm/ToggleBG.h>
30 #include <Xm/RowColumn.h>
33 #pragma message enable nosimpint
36 #include "wx/motif/private.h"
38 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
39 XmToggleButtonCallbackStruct
* cbs
);
41 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
44 void wxRadioBox::Init()
46 m_selectedButton
= -1;
49 m_labelWidget
= (WXWidget
) 0;
52 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
53 const wxPoint
& pos
, const wxSize
& size
,
54 int n
, const wxString choices
[],
55 int majorDim
, long style
,
56 const wxValidator
& val
, const wxString
& name
)
58 if( !CreateControl( parent
, id
, pos
, size
, style
, val
, name
) )
62 m_noItems
= (unsigned int)n
;
63 m_noRowsOrCols
= majorDim
;
65 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
67 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
68 Display
* dpy
= XtDisplay(parentWidget
);
70 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
71 xmFrameWidgetClass
, parentWidget
,
72 XmNresizeHeight
, True
,
76 wxString
label1(GetLabelText(title
));
80 wxXmString
text(label1
);
81 m_labelWidget
= (WXWidget
)
82 XtVaCreateManagedWidget( label1
.mb_str(),
84 style
& wxCOLOURED
? xmLabelWidgetClass
88 xmLabelWidgetClass
, (Widget
)m_mainWidget
,
90 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
91 XmNlabelString
, text(),
92 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
93 // if it was compiled with 1.2 compatibility
94 // TODO: check this still looks OK for Motif 1.2.
95 #if (XmVersion > 1200)
96 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
98 XmNchildType
, XmFRAME_TITLE_CHILD
,
100 XmNchildVerticalAlignment
, XmALIGNMENT_CENTER
,
106 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
107 XmHORIZONTAL
: XmVERTICAL
));
108 XtSetArg (args
[1], XmNnumColumns
, GetMajorDim());
109 XtSetArg (args
[2], XmNadjustLast
, False
);
111 Widget radioBoxWidget
=
112 XmCreateRadioBox ((Widget
)m_mainWidget
, wxMOTIF_STR("radioBoxWidget"), args
, 3);
114 m_radioButtons
.reserve(n
);
115 m_radioButtonLabels
.reserve(n
);
118 for (i
= 0; i
< n
; i
++)
120 wxString
str(GetLabelText(choices
[i
]));
121 m_radioButtonLabels
.push_back(str
);
122 Widget radioItem
= XtVaCreateManagedWidget (
125 xmToggleButtonGadgetClass
, radioBoxWidget
,
127 xmToggleButtonWidgetClass
, radioBoxWidget
,
129 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
131 m_radioButtons
.push_back((WXWidget
)radioItem
);
132 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
133 (XtCallbackProc
) wxRadioBoxCallback
,
139 XtRealizeWidget((Widget
)m_mainWidget
);
140 XtManageChild (radioBoxWidget
);
141 XtManageChild ((Widget
)m_mainWidget
);
144 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
149 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
150 const wxPoint
& pos
, const wxSize
& size
,
151 const wxArrayString
& choices
,
152 int majorDim
, long style
,
153 const wxValidator
& val
, const wxString
& name
)
155 wxCArrayString
chs(choices
);
156 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
157 chs
.GetStrings(), majorDim
, style
, val
, name
);
160 wxRadioBox::~wxRadioBox()
162 DetachWidget(m_mainWidget
);
163 XtDestroyWidget((Widget
) m_mainWidget
);
165 m_mainWidget
= (WXWidget
) 0;
168 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
173 Widget widget
= (Widget
)m_radioButtons
[item
];
176 wxString
label1(GetLabelText(label
));
177 wxXmString
text( label1
);
178 m_radioButtonLabels
[item
] = label1
;
179 XtVaSetValues (widget
,
180 XmNlabelString
, text(),
181 XmNlabelType
, XmSTRING
,
186 void wxRadioBox::SetSelection(int n
)
191 m_selectedButton
= n
;
195 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], True
, False
);
197 for (unsigned int i
= 0; i
< m_noItems
; i
++)
198 if (i
!= (unsigned int)n
)
199 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], False
, False
);
201 m_inSetValue
= false;
204 // Get single selection, for single choice list items
205 int wxRadioBox::GetSelection() const
207 return m_selectedButton
;
210 // Find string for position
211 wxString
wxRadioBox::GetString(unsigned int n
) const
214 return wxEmptyString
;
215 return m_radioButtonLabels
[n
];
218 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
220 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
223 XtUnmanageChild ((Widget
) m_mainWidget
);
225 int xx
= x
; int yy
= y
;
226 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
228 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
229 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
230 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
231 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
234 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
236 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
239 XtManageChild ((Widget
) m_mainWidget
);
242 // Enable a specific button
243 bool wxRadioBox::Enable(unsigned int n
, bool enable
)
248 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
252 // Enable all controls
253 bool wxRadioBox::Enable(bool enable
)
255 if ( !wxControl::Enable(enable
) )
258 for (unsigned int i
= 0; i
< m_noItems
; i
++)
259 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
264 bool wxRadioBox::Show(bool show
)
266 // TODO: show/hide all children
267 return wxControl::Show(show
);
270 // Show a specific button
271 bool wxRadioBox::Show(unsigned int n
, bool show
)
273 // This method isn't complete, and we try do do our best...
274 // It's main purpose isn't for allowing Show/Unshow dynamically,
275 // but rather to provide a way to design wxRadioBox such:
277 // o Val1 o Val2 o Val3
279 // o Val7 o Val8 o Val9
281 // In my case, this is a 'direction' box, and the Show(5,False) is
282 // coupled with an Enable(5,False)
287 XtVaSetValues ((Widget
) m_radioButtons
[n
],
288 XmNindicatorOn
, (unsigned char) show
,
291 // Please note that this is all we can do: removing the label
292 // if switching to unshow state. However, when switching
293 // to the on state, it's the prog. resp. to call SetString(item,...)
296 wxRadioBox::SetString (n
, " ");
301 // For single selection items only
302 wxString
wxRadioBox::GetStringSelection () const
304 int sel
= GetSelection ();
305 if (sel
!= wxNOT_FOUND
)
306 return this->GetString((unsigned int)sel
);
308 return wxEmptyString
;
311 bool wxRadioBox::SetStringSelection (const wxString
& s
)
313 int sel
= FindString (s
);
323 void wxRadioBox::Command (wxCommandEvent
& event
)
325 SetSelection (event
.GetInt());
326 ProcessCommand (event
);
329 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
331 wxWindow::ChangeFont(keepOriginalSize
);
333 for (unsigned int i
= 0; i
< m_noItems
; i
++)
335 WXWidget radioButton
= m_radioButtons
[i
];
337 XtVaSetValues ((Widget
) radioButton
,
338 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay((Widget
) GetTopWidget())),
343 void wxRadioBox::ChangeBackgroundColour()
345 wxWindow::ChangeBackgroundColour();
347 wxColour colour
= *wxBLACK
;
348 WXPixel selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
350 for (unsigned int i
= 0; i
< m_noItems
; i
++)
352 WXWidget radioButton
= m_radioButtons
[i
];
354 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, true);
356 XtVaSetValues ((Widget
) radioButton
,
357 XmNselectColor
, selectPixel
,
362 void wxRadioBox::ChangeForegroundColour()
364 wxWindow::ChangeForegroundColour();
366 for (unsigned int i
= 0; i
< m_noItems
; i
++)
368 WXWidget radioButton
= m_radioButtons
[i
];
370 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
374 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
375 XmToggleButtonCallbackStruct
* cbs
)
380 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
383 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
384 for (i
= 0; i
< item
->GetCount(); i
++)
385 if (((Widget
)buttons
[i
]) == w
)
389 if (item
->InSetValue())
392 wxCommandEvent
event (wxEVT_RADIOBOX
, item
->GetId());
394 event
.SetString(item
->GetStringSelection());
395 event
.SetEventObject(item
);
396 item
->ProcessCommand (event
);
399 #endif // wxUSE_RADIOBOX