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;
54 m_labelWidget
= (WXWidget
) 0;
57 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
58 const wxPoint
& pos
, const wxSize
& size
,
59 int n
, const wxString choices
[],
60 int majorDim
, long style
,
61 const wxValidator
& val
, const wxString
& name
)
63 if( !CreateControl( parent
, id
, pos
, size
, style
, val
, name
) )
67 m_noItems
= (unsigned int)n
;
68 m_noRowsOrCols
= majorDim
;
70 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
72 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
73 Display
* dpy
= XtDisplay(parentWidget
);
75 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
76 xmFrameWidgetClass
, parentWidget
,
77 XmNresizeHeight
, True
,
81 wxString
label1(GetLabelText(title
));
85 wxXmString
text(label1
);
86 m_labelWidget
= (WXWidget
)
87 XtVaCreateManagedWidget( label1
.mb_str(),
89 style
& wxCOLOURED
? xmLabelWidgetClass
93 xmLabelWidgetClass
, (Widget
)m_mainWidget
,
95 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
96 XmNlabelString
, text(),
97 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
98 // if it was compiled with 1.2 compatibility
99 // TODO: check this still looks OK for Motif 1.2.
100 #if (XmVersion > 1200)
101 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
103 XmNchildType
, XmFRAME_TITLE_CHILD
,
105 XmNchildVerticalAlignment
, XmALIGNMENT_CENTER
,
111 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
112 XmHORIZONTAL
: XmVERTICAL
));
113 XtSetArg (args
[1], XmNnumColumns
, GetMajorDim());
114 XtSetArg (args
[2], XmNadjustLast
, False
);
116 Widget radioBoxWidget
=
117 XmCreateRadioBox ((Widget
)m_mainWidget
, wxMOTIF_STR("radioBoxWidget"), args
, 3);
119 m_radioButtons
.reserve(n
);
120 m_radioButtonLabels
.reserve(n
);
123 for (i
= 0; i
< n
; i
++)
125 wxString
str(GetLabelText(choices
[i
]));
126 m_radioButtonLabels
.push_back(str
);
127 Widget radioItem
= XtVaCreateManagedWidget (
128 wxConstCast(str
.mb_str(), char),
130 xmToggleButtonGadgetClass
, radioBoxWidget
,
132 xmToggleButtonWidgetClass
, radioBoxWidget
,
134 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
136 m_radioButtons
.push_back((WXWidget
)radioItem
);
137 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
138 (XtCallbackProc
) wxRadioBoxCallback
,
144 XtRealizeWidget((Widget
)m_mainWidget
);
145 XtManageChild (radioBoxWidget
);
146 XtManageChild ((Widget
)m_mainWidget
);
149 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
154 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
155 const wxPoint
& pos
, const wxSize
& size
,
156 const wxArrayString
& choices
,
157 int majorDim
, long style
,
158 const wxValidator
& val
, const wxString
& name
)
160 wxCArrayString
chs(choices
);
161 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
162 chs
.GetStrings(), majorDim
, style
, val
, name
);
165 wxRadioBox::~wxRadioBox()
167 DetachWidget(m_mainWidget
);
168 XtDestroyWidget((Widget
) m_mainWidget
);
170 m_mainWidget
= (WXWidget
) 0;
173 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
178 Widget widget
= (Widget
)m_radioButtons
[item
];
181 wxString
label1(GetLabelText(label
));
182 wxXmString
text( label1
);
183 m_radioButtonLabels
[item
] = label1
;
184 XtVaSetValues (widget
,
185 XmNlabelString
, text(),
186 XmNlabelType
, XmSTRING
,
191 void wxRadioBox::SetSelection(int n
)
196 m_selectedButton
= n
;
200 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], True
, False
);
202 for (unsigned int i
= 0; i
< m_noItems
; i
++)
203 if (i
!= (unsigned int)n
)
204 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], False
, False
);
206 m_inSetValue
= false;
209 // Get single selection, for single choice list items
210 int wxRadioBox::GetSelection() const
212 return m_selectedButton
;
215 // Find string for position
216 wxString
wxRadioBox::GetString(unsigned int n
) const
219 return wxEmptyString
;
220 return m_radioButtonLabels
[n
];
223 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
225 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
228 XtUnmanageChild ((Widget
) m_mainWidget
);
230 int xx
= x
; int yy
= y
;
231 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
233 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
234 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
235 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
236 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
239 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
241 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
244 XtManageChild ((Widget
) m_mainWidget
);
247 // Enable a specific button
248 bool wxRadioBox::Enable(unsigned int n
, bool enable
)
253 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
257 // Enable all controls
258 bool wxRadioBox::Enable(bool enable
)
260 if ( !wxControl::Enable(enable
) )
263 for (unsigned int i
= 0; i
< m_noItems
; i
++)
264 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
269 bool wxRadioBox::Show(bool show
)
271 // TODO: show/hide all children
272 return wxControl::Show(show
);
275 // Show a specific button
276 bool wxRadioBox::Show(unsigned int n
, bool show
)
278 // This method isn't complete, and we try do do our best...
279 // It's main purpose isn't for allowing Show/Unshow dynamically,
280 // but rather to provide a way to design wxRadioBox such:
282 // o Val1 o Val2 o Val3
284 // o Val7 o Val8 o Val9
286 // In my case, this is a 'direction' box, and the Show(5,False) is
287 // coupled with an Enable(5,False)
292 XtVaSetValues ((Widget
) m_radioButtons
[n
],
293 XmNindicatorOn
, (unsigned char) show
,
296 // Please note that this is all we can do: removing the label
297 // if switching to unshow state. However, when switching
298 // to the on state, it's the prog. resp. to call SetString(item,...)
301 wxRadioBox::SetString (n
, " ");
306 // For single selection items only
307 wxString
wxRadioBox::GetStringSelection () const
309 int sel
= GetSelection ();
310 if (sel
!= wxNOT_FOUND
)
311 return this->GetString((unsigned int)sel
);
313 return wxEmptyString
;
316 bool wxRadioBox::SetStringSelection (const wxString
& s
)
318 int sel
= FindString (s
);
328 void wxRadioBox::Command (wxCommandEvent
& event
)
330 SetSelection (event
.GetInt());
331 ProcessCommand (event
);
334 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
336 wxWindow::ChangeFont(keepOriginalSize
);
338 for (unsigned int i
= 0; i
< m_noItems
; i
++)
340 WXWidget radioButton
= m_radioButtons
[i
];
342 XtVaSetValues ((Widget
) radioButton
,
343 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay((Widget
) GetTopWidget())),
348 void wxRadioBox::ChangeBackgroundColour()
350 wxWindow::ChangeBackgroundColour();
352 wxColour colour
= *wxBLACK
;
353 WXPixel selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
355 for (unsigned int i
= 0; i
< m_noItems
; i
++)
357 WXWidget radioButton
= m_radioButtons
[i
];
359 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, true);
361 XtVaSetValues ((Widget
) radioButton
,
362 XmNselectColor
, selectPixel
,
367 void wxRadioBox::ChangeForegroundColour()
369 wxWindow::ChangeForegroundColour();
371 for (unsigned int i
= 0; i
< m_noItems
; i
++)
373 WXWidget radioButton
= m_radioButtons
[i
];
375 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
379 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
380 XmToggleButtonCallbackStruct
* cbs
)
385 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
388 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
389 for (i
= 0; i
< item
->GetCount(); i
++)
390 if (((Widget
)buttons
[i
]) == w
)
394 if (item
->InSetValue())
397 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
399 event
.SetString(item
->GetStringSelection());
400 event
.SetEventObject(item
);
401 item
->ProcessCommand (event
);
404 #endif // wxUSE_RADIOBOX