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"
27 #include "wx/arrstr.h"
30 #pragma message disable nosimpint
33 #include <Xm/LabelG.h>
34 #include <Xm/ToggleB.h>
35 #include <Xm/ToggleBG.h>
36 #include <Xm/RowColumn.h>
39 #pragma message enable nosimpint
42 #include "wx/motif/private.h"
44 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
45 XmToggleButtonCallbackStruct
* cbs
);
47 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
50 void wxRadioBox::Init()
52 m_selectedButton
= -1;
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
) )
66 m_noItems
= (unsigned int)n
;
67 m_noRowsOrCols
= majorDim
;
69 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
71 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
72 Display
* dpy
= XtDisplay(parentWidget
);
74 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
75 xmFrameWidgetClass
, parentWidget
,
76 XmNresizeHeight
, True
,
80 wxString
label1(wxStripMenuCodes(title
));
84 wxXmString
text(label1
);
85 m_labelWidget
= (WXWidget
)
86 XtVaCreateManagedWidget( label1
.c_str(),
88 style
& wxCOLOURED
? xmLabelWidgetClass
92 xmLabelWidgetClass
, (Widget
)m_mainWidget
,
94 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
95 XmNlabelString
, text(),
96 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
97 // if it was compiled with 1.2 compatibility
98 // TODO: check this still looks OK for Motif 1.2.
99 #if (XmVersion > 1200)
100 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
102 XmNchildType
, XmFRAME_TITLE_CHILD
,
104 XmNchildVerticalAlignment
, XmALIGNMENT_CENTER
,
110 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
111 XmHORIZONTAL
: XmVERTICAL
));
112 XtSetArg (args
[1], XmNnumColumns
, GetMajorDim());
113 XtSetArg (args
[2], XmNadjustLast
, False
);
115 Widget radioBoxWidget
=
116 XmCreateRadioBox ((Widget
)m_mainWidget
, wxMOTIF_STR("radioBoxWidget"), args
, 3);
118 m_radioButtons
.reserve(n
);
119 m_radioButtonLabels
.reserve(n
);
122 for (i
= 0; i
< n
; i
++)
124 wxString
str(wxStripMenuCodes(choices
[i
]));
125 m_radioButtonLabels
.push_back(str
);
126 Widget radioItem
= XtVaCreateManagedWidget (
127 wxConstCast(str
.c_str(), char),
129 xmToggleButtonGadgetClass
, radioBoxWidget
,
131 xmToggleButtonWidgetClass
, radioBoxWidget
,
133 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
135 m_radioButtons
.push_back((WXWidget
)radioItem
);
136 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
137 (XtCallbackProc
) wxRadioBoxCallback
,
145 XtRealizeWidget((Widget
)m_mainWidget
);
146 XtManageChild (radioBoxWidget
);
147 XtManageChild ((Widget
)m_mainWidget
);
149 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
151 ChangeBackgroundColour();
156 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
157 const wxPoint
& pos
, const wxSize
& size
,
158 const wxArrayString
& choices
,
159 int majorDim
, long style
,
160 const wxValidator
& val
, const wxString
& name
)
162 wxCArrayString
chs(choices
);
163 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
164 chs
.GetStrings(), majorDim
, style
, val
, name
);
167 wxRadioBox::~wxRadioBox()
169 DetachWidget(m_mainWidget
);
170 XtDestroyWidget((Widget
) m_mainWidget
);
172 m_mainWidget
= (WXWidget
) 0;
175 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
180 Widget widget
= (Widget
)m_radioButtons
[item
];
183 wxString
label1(wxStripMenuCodes(label
));
184 wxXmString
text( label1
);
185 m_radioButtonLabels
[item
] = label1
;
186 XtVaSetValues (widget
,
187 XmNlabelString
, text(),
188 XmNlabelType
, XmSTRING
,
193 void wxRadioBox::SetSelection(int n
)
198 m_selectedButton
= n
;
202 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], True
, False
);
204 for (unsigned int i
= 0; i
< m_noItems
; i
++)
205 if (i
!= (unsigned int)n
)
206 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], False
, False
);
208 m_inSetValue
= false;
211 // Get single selection, for single choice list items
212 int wxRadioBox::GetSelection() const
214 return m_selectedButton
;
217 // Find string for position
218 wxString
wxRadioBox::GetString(unsigned int n
) const
221 return wxEmptyString
;
222 return m_radioButtonLabels
[n
];
225 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
227 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
230 XtUnmanageChild ((Widget
) m_mainWidget
);
232 int xx
= x
; int yy
= y
;
233 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
235 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
236 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
237 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
238 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
241 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
243 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
246 XtManageChild ((Widget
) m_mainWidget
);
249 // Enable a specific button
250 bool wxRadioBox::Enable(unsigned int n
, bool enable
)
255 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
259 // Enable all controls
260 bool wxRadioBox::Enable(bool enable
)
262 if ( !wxControl::Enable(enable
) )
265 for (unsigned int i
= 0; i
< m_noItems
; i
++)
266 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
271 bool wxRadioBox::Show(bool show
)
273 // TODO: show/hide all children
274 return wxControl::Show(show
);
277 // Show a specific button
278 bool wxRadioBox::Show(unsigned int n
, bool show
)
280 // This method isn't complete, and we try do do our best...
281 // It's main purpose isn't for allowing Show/Unshow dynamically,
282 // but rather to provide a way to design wxRadioBox such:
284 // o Val1 o Val2 o Val3
286 // o Val7 o Val8 o Val9
288 // In my case, this is a 'direction' box, and the Show(5,False) is
289 // coupled with an Enable(5,False)
294 XtVaSetValues ((Widget
) m_radioButtons
[n
],
295 XmNindicatorOn
, (unsigned char) show
,
298 // Please note that this is all we can do: removing the label
299 // if switching to unshow state. However, when switching
300 // to the on state, it's the prog. resp. to call SetString(item,...)
303 wxRadioBox::SetString (n
, " ");
308 // For single selection items only
309 wxString
wxRadioBox::GetStringSelection () const
311 int sel
= GetSelection ();
312 if (sel
!= wxNOT_FOUND
)
313 return this->GetString((unsigned int)sel
);
315 return wxEmptyString
;
318 bool wxRadioBox::SetStringSelection (const wxString
& s
)
320 int sel
= FindString (s
);
330 void wxRadioBox::Command (wxCommandEvent
& event
)
332 SetSelection (event
.GetInt());
333 ProcessCommand (event
);
336 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
338 wxWindow::ChangeFont(keepOriginalSize
);
340 for (unsigned int i
= 0; i
< m_noItems
; i
++)
342 WXWidget radioButton
= m_radioButtons
[i
];
344 XtVaSetValues ((Widget
) radioButton
,
345 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay((Widget
) GetTopWidget())),
350 void wxRadioBox::ChangeBackgroundColour()
352 wxWindow::ChangeBackgroundColour();
354 wxColour colour
= *wxBLACK
;
355 int selectPixel
= colour
.AllocColour(XtDisplay((Widget
)m_mainWidget
));
357 for (unsigned int i
= 0; i
< m_noItems
; i
++)
359 WXWidget radioButton
= m_radioButtons
[i
];
361 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, true);
363 XtVaSetValues ((Widget
) radioButton
,
364 XmNselectColor
, selectPixel
,
369 void wxRadioBox::ChangeForegroundColour()
371 wxWindow::ChangeForegroundColour();
373 for (unsigned int i
= 0; i
< m_noItems
; i
++)
375 WXWidget radioButton
= m_radioButtons
[i
];
377 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
381 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
382 XmToggleButtonCallbackStruct
* cbs
)
387 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
390 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
391 for (i
= 0; i
< item
->GetCount(); i
++)
392 if (((Widget
)buttons
[i
]) == w
)
396 if (item
->InSetValue())
399 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
401 event
.SetString(item
->GetStringSelection());
402 event
.SetEventObject(item
);
403 item
->ProcessCommand (event
);
406 #endif // wxUSE_RADIOBOX