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();
77 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
78 xmFrameWidgetClass
, parentWidget
,
79 XmNresizeHeight
, True
,
83 wxString
label1(wxStripMenuCodes(title
));
85 WXFontType fontType
= m_font
.GetFontType(XtDisplay(parentWidget
));
89 wxXmString
text(label1
);
90 m_labelWidget
= (WXWidget
)
91 XtVaCreateManagedWidget( label1
.c_str(),
93 style
& wxCOLOURED
? xmLabelWidgetClass
100 wxFont::GetFontTag(), fontType
,
101 XmNlabelString
, text(),
102 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
103 // if it was compiled with 1.2 compatibility
104 // TODO: check this still looks OK for Motif 1.2.
105 #if (XmVersion > 1200)
106 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
108 XmNchildType
, XmFRAME_TITLE_CHILD
,
110 XmNchildVerticalAlignment
,
117 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
119 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
120 XmHORIZONTAL
: XmVERTICAL
));
121 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
122 XtSetArg (args
[2], XmNadjustLast
, False
);
124 Widget radioBoxWidget
=
125 XmCreateRadioBox ((Widget
)m_mainWidget
, "radioBoxWidget", args
, 3);
127 m_radioButtons
.reserve(n
);
128 m_radioButtonLabels
.reserve(n
);
131 for (i
= 0; i
< n
; i
++)
133 wxString
str(wxStripMenuCodes(choices
[i
]));
134 m_radioButtonLabels
.push_back(str
);
135 Widget radioItem
= XtVaCreateManagedWidget (wxConstCast(str
.c_str(), char),
137 xmToggleButtonGadgetClass
, radioBoxWidget
,
139 xmToggleButtonWidgetClass
, radioBoxWidget
,
141 wxFont::GetFontTag(), fontType
,
143 m_radioButtons
.push_back((WXWidget
)radioItem
);
144 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
145 (XtCallbackProc
) wxRadioBoxCallback
,
153 XtRealizeWidget((Widget
)m_mainWidget
);
154 XtManageChild (radioBoxWidget
);
155 XtManageChild ((Widget
)m_mainWidget
);
157 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
159 ChangeBackgroundColour();
164 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
165 const wxPoint
& pos
, const wxSize
& size
,
166 const wxArrayString
& choices
,
167 int majorDim
, long style
,
168 const wxValidator
& val
, const wxString
& name
)
170 wxCArrayString
chs(choices
);
171 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
172 chs
.GetStrings(), majorDim
, style
, val
, name
);
175 wxRadioBox::~wxRadioBox()
177 DetachWidget(m_mainWidget
);
178 XtDestroyWidget((Widget
) m_mainWidget
);
180 m_mainWidget
= (WXWidget
) 0;
183 void wxRadioBox::SetString(int item
, const wxString
& label
)
185 if (item
< 0 || item
>= m_noItems
)
188 Widget widget
= (Widget
) m_radioButtons
[item
];
191 wxString
label1(wxStripMenuCodes(label
));
192 wxXmString
text( label1
);
193 m_radioButtonLabels
[item
] = label1
;
194 XtVaSetValues (widget
,
195 XmNlabelString
, text(),
196 XmNlabelType
, XmSTRING
,
201 int wxRadioBox::FindString(const wxString
& s
) const
204 for (i
= 0; i
< m_noItems
; i
++)
205 if (s
== m_radioButtonLabels
[i
])
210 void wxRadioBox::SetSelection(int n
)
212 if ((n
< 0) || (n
>= m_noItems
))
215 m_selectedButton
= n
;
219 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
222 for (i
= 0; i
< m_noItems
; i
++)
224 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
226 m_inSetValue
= FALSE
;
229 // Get single selection, for single choice list items
230 int wxRadioBox::GetSelection() const
232 return m_selectedButton
;
235 // Find string for position
236 wxString
wxRadioBox::GetString(int n
) const
238 if ((n
< 0) || (n
>= m_noItems
))
239 return wxEmptyString
;
240 return m_radioButtonLabels
[n
];
243 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
245 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
248 XtUnmanageChild ((Widget
) m_mainWidget
);
250 int xx
= x
; int yy
= y
;
251 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
253 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
254 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
255 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
256 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
259 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
261 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
264 XtManageChild ((Widget
) m_mainWidget
);
267 // Enable a specific button
268 void wxRadioBox::Enable(int n
, bool enable
)
270 if ((n
< 0) || (n
>= m_noItems
))
273 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
276 // Enable all controls
277 bool wxRadioBox::Enable(bool enable
)
279 if ( !wxControl::Enable(enable
) )
283 for (i
= 0; i
< m_noItems
; i
++)
284 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
289 bool wxRadioBox::Show(bool show
)
291 // TODO: show/hide all children
292 return wxControl::Show(show
);
295 // Show a specific button
296 void wxRadioBox::Show(int n
, bool show
)
298 // This method isn't complete, and we try do do our best...
299 // It's main purpose isn't for allowing Show/Unshow dynamically,
300 // but rather to provide a way to design wxRadioBox such:
302 // o Val1 o Val2 o Val3
304 // o Val7 o Val8 o Val9
306 // In my case, this is a 'direction' box, and the Show(5,False) is
307 // coupled with an Enable(5,False)
309 if ((n
< 0) || (n
>= m_noItems
))
312 XtVaSetValues ((Widget
) m_radioButtons
[n
],
313 XmNindicatorOn
, (unsigned char) show
,
316 // Please note that this is all we can do: removing the label
317 // if switching to unshow state. However, when switching
318 // to the on state, it's the prog. resp. to call SetString(item,...)
321 wxRadioBox::SetString (n
, " ");
324 // For single selection items only
325 wxString
wxRadioBox::GetStringSelection () const
327 int sel
= GetSelection ();
329 return this->GetString (sel
);
334 bool wxRadioBox::SetStringSelection (const wxString
& s
)
336 int sel
= FindString (s
);
346 void wxRadioBox::Command (wxCommandEvent
& event
)
348 SetSelection (event
.m_commandInt
);
349 ProcessCommand (event
);
352 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
354 wxWindow::ChangeFont(keepOriginalSize
);
356 WXFontType fontType
=
357 m_font
.GetFontType(XtDisplay((Widget
) GetTopWidget()));
360 for (i
= 0; i
< m_noItems
; i
++)
362 WXWidget radioButton
= m_radioButtons
[i
];
364 XtVaSetValues ((Widget
) radioButton
,
365 wxFont::GetFontTag(), fontType
,
370 void wxRadioBox::ChangeBackgroundColour()
372 wxWindow::ChangeBackgroundColour();
374 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
377 for (i
= 0; i
< m_noItems
; i
++)
379 WXWidget radioButton
= m_radioButtons
[i
];
381 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
383 XtVaSetValues ((Widget
) radioButton
,
384 XmNselectColor
, selectPixel
,
389 void wxRadioBox::ChangeForegroundColour()
391 wxWindow::ChangeForegroundColour();
394 for (i
= 0; i
< m_noItems
; i
++)
396 WXWidget radioButton
= m_radioButtons
[i
];
398 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
402 static int CalcOtherDim( int items
, int dim
)
404 return items
/ dim
+ ( items
% dim
? 1 : 0 );
407 int wxRadioBox::GetRowCount() const
409 return m_windowStyle
& wxRA_SPECIFY_ROWS
? m_noRowsOrCols
410 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
413 int wxRadioBox::GetColumnCount() const
415 return m_windowStyle
& wxRA_SPECIFY_COLS
? m_noRowsOrCols
416 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
419 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
420 XmToggleButtonCallbackStruct
* cbs
)
425 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
428 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
429 for (i
= 0; i
< item
->GetCount(); i
++)
430 if (((Widget
)buttons
[i
]) == w
)
434 if (item
->InSetValue())
437 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
439 event
.SetString(item
->GetStringSelection());
440 event
.SetEventObject(item
);
441 item
->ProcessCommand (event
);