1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "radiobox.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtDisplay XTDISPLAY
25 #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;
58 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
59 const wxPoint
& pos
, const wxSize
& size
,
60 int n
, const wxString choices
[],
61 int majorDim
, long style
,
62 const wxValidator
& val
, const wxString
& name
)
64 if( !CreateControl( parent
, id
, pos
, size
, style
, val
, name
) )
68 m_noRowsOrCols
= majorDim
;
73 m_majorDim
= majorDim
;
75 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
76 Display
* dpy
= XtDisplay(parentWidget
);
78 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
79 xmFrameWidgetClass
, parentWidget
,
80 XmNresizeHeight
, True
,
84 wxString
label1(wxStripMenuCodes(title
));
88 wxXmString
text(label1
);
89 m_labelWidget
= (WXWidget
)
90 XtVaCreateManagedWidget( label1
.c_str(),
92 style
& wxCOLOURED
? xmLabelWidgetClass
96 xmLabelWidgetClass
, (Widget
)m_mainWidget
,
98 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
99 XmNlabelString
, text(),
100 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
101 // if it was compiled with 1.2 compatibility
102 // TODO: check this still looks OK for Motif 1.2.
103 #if (XmVersion > 1200)
104 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
106 XmNchildType
, XmFRAME_TITLE_CHILD
,
108 XmNchildVerticalAlignment
, XmALIGNMENT_CENTER
,
114 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
116 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
117 XmHORIZONTAL
: XmVERTICAL
));
118 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
119 XtSetArg (args
[2], XmNadjustLast
, False
);
121 Widget radioBoxWidget
=
122 XmCreateRadioBox ((Widget
)m_mainWidget
, "radioBoxWidget", args
, 3);
124 m_radioButtons
.reserve(n
);
125 m_radioButtonLabels
.reserve(n
);
128 for (i
= 0; i
< n
; i
++)
130 wxString
str(wxStripMenuCodes(choices
[i
]));
131 m_radioButtonLabels
.push_back(str
);
132 Widget radioItem
= XtVaCreateManagedWidget (
133 wxConstCast(str
.c_str(), char),
135 xmToggleButtonGadgetClass
, radioBoxWidget
,
137 xmToggleButtonWidgetClass
, radioBoxWidget
,
139 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
141 m_radioButtons
.push_back((WXWidget
)radioItem
);
142 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
143 (XtCallbackProc
) wxRadioBoxCallback
,
151 XtRealizeWidget((Widget
)m_mainWidget
);
152 XtManageChild (radioBoxWidget
);
153 XtManageChild ((Widget
)m_mainWidget
);
155 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
157 ChangeBackgroundColour();
162 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
163 const wxPoint
& pos
, const wxSize
& size
,
164 const wxArrayString
& choices
,
165 int majorDim
, long style
,
166 const wxValidator
& val
, const wxString
& name
)
168 wxCArrayString
chs(choices
);
169 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
170 chs
.GetStrings(), majorDim
, style
, val
, name
);
173 wxRadioBox::~wxRadioBox()
175 DetachWidget(m_mainWidget
);
176 XtDestroyWidget((Widget
) m_mainWidget
);
178 m_mainWidget
= (WXWidget
) 0;
181 void wxRadioBox::SetString(int item
, const wxString
& label
)
186 Widget widget
= (Widget
) m_radioButtons
[item
];
189 wxString
label1(wxStripMenuCodes(label
));
190 wxXmString
text( label1
);
191 m_radioButtonLabels
[item
] = label1
;
192 XtVaSetValues (widget
,
193 XmNlabelString
, text(),
194 XmNlabelType
, XmSTRING
,
199 int wxRadioBox::FindString(const wxString
& s
) const
202 for (i
= 0; i
< m_noItems
; i
++)
203 if (s
== m_radioButtonLabels
[i
])
208 void wxRadioBox::SetSelection(int n
)
213 m_selectedButton
= n
;
217 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], True
, False
);
220 for (i
= 0; i
< m_noItems
; i
++)
222 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], False
, False
);
224 m_inSetValue
= false;
227 // Get single selection, for single choice list items
228 int wxRadioBox::GetSelection() const
230 return m_selectedButton
;
233 // Find string for position
234 wxString
wxRadioBox::GetString(int n
) const
237 return wxEmptyString
;
238 return m_radioButtonLabels
[n
];
241 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
243 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
246 XtUnmanageChild ((Widget
) m_mainWidget
);
248 int xx
= x
; int yy
= y
;
249 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
251 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
252 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
253 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
254 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
257 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
259 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
262 XtManageChild ((Widget
) m_mainWidget
);
265 // Enable a specific button
266 bool wxRadioBox::Enable(int n
, bool enable
)
271 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
275 // Enable all controls
276 bool wxRadioBox::Enable(bool enable
)
278 if ( !wxControl::Enable(enable
) )
282 for (i
= 0; i
< m_noItems
; i
++)
283 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
288 bool wxRadioBox::Show(bool show
)
290 // TODO: show/hide all children
291 return wxControl::Show(show
);
294 // Show a specific button
295 bool wxRadioBox::Show(int n
, bool show
)
297 // This method isn't complete, and we try do do our best...
298 // It's main purpose isn't for allowing Show/Unshow dynamically,
299 // but rather to provide a way to design wxRadioBox such:
301 // o Val1 o Val2 o Val3
303 // o Val7 o Val8 o Val9
305 // In my case, this is a 'direction' box, and the Show(5,False) is
306 // coupled with an Enable(5,False)
311 XtVaSetValues ((Widget
) m_radioButtons
[n
],
312 XmNindicatorOn
, (unsigned char) show
,
315 // Please note that this is all we can do: removing the label
316 // if switching to unshow state. However, when switching
317 // to the on state, it's the prog. resp. to call SetString(item,...)
320 wxRadioBox::SetString (n
, " ");
325 // For single selection items only
326 wxString
wxRadioBox::GetStringSelection () const
328 int sel
= GetSelection ();
330 return this->GetString (sel
);
332 return wxEmptyString
;
335 bool wxRadioBox::SetStringSelection (const wxString
& s
)
337 int sel
= FindString (s
);
347 void wxRadioBox::Command (wxCommandEvent
& event
)
349 SetSelection (event
.GetInt());
350 ProcessCommand (event
);
353 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
355 wxWindow::ChangeFont(keepOriginalSize
);
358 for (i
= 0; i
< m_noItems
; i
++)
360 WXWidget radioButton
= m_radioButtons
[i
];
362 XtVaSetValues ((Widget
) radioButton
,
363 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay((Widget
) GetTopWidget())),
368 void wxRadioBox::ChangeBackgroundColour()
370 wxWindow::ChangeBackgroundColour();
372 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
375 for (i
= 0; i
< m_noItems
; i
++)
377 WXWidget radioButton
= m_radioButtons
[i
];
379 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, true);
381 XtVaSetValues ((Widget
) radioButton
,
382 XmNselectColor
, selectPixel
,
387 void wxRadioBox::ChangeForegroundColour()
389 wxWindow::ChangeForegroundColour();
392 for (i
= 0; i
< m_noItems
; i
++)
394 WXWidget radioButton
= m_radioButtons
[i
];
396 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
400 static int CalcOtherDim( int items
, int dim
)
402 return items
/ dim
+ ( items
% dim
? 1 : 0 );
405 int wxRadioBox::GetRowCount() const
407 return m_windowStyle
& wxRA_SPECIFY_ROWS
? m_noRowsOrCols
408 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
411 int wxRadioBox::GetColumnCount() const
413 return m_windowStyle
& wxRA_SPECIFY_COLS
? m_noRowsOrCols
414 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
417 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
418 XmToggleButtonCallbackStruct
* cbs
)
423 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
426 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
427 for (i
= 0; i
< item
->GetCount(); i
++)
428 if (((Widget
)buttons
[i
]) == w
)
432 if (item
->InSetValue())
435 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
437 event
.SetString(item
->GetStringSelection());
438 event
.SetEventObject(item
);
439 item
->ProcessCommand (event
);