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"
17 #define XtDisplay XTDISPLAY
22 #include "wx/radiobox.h"
26 #pragma message disable nosimpint
29 #include <Xm/LabelG.h>
30 #include <Xm/ToggleB.h>
31 #include <Xm/ToggleBG.h>
32 #include <Xm/RowColumn.h>
35 #pragma message enable nosimpint
38 #include "wx/motif/private.h"
40 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
41 XmToggleButtonCallbackStruct
* cbs
);
43 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
46 void wxRadioBox::Init()
48 m_selectedButton
= -1;
54 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
55 const wxPoint
& pos
, const wxSize
& size
,
56 int n
, const wxString choices
[],
57 int majorDim
, long style
,
58 const wxValidator
& val
, const wxString
& name
)
60 if( !CreateControl( parent
, id
, pos
, size
, style
, val
, name
) )
64 m_noRowsOrCols
= majorDim
;
69 m_majorDim
= majorDim
;
71 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
73 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
74 xmFrameWidgetClass
, parentWidget
,
75 XmNresizeHeight
, True
,
79 wxString
label1(wxStripMenuCodes(title
));
81 WXFontType fontType
= m_font
.GetFontType(XtDisplay(parentWidget
));
85 wxXmString
text(label1
);
86 m_labelWidget
= (WXWidget
)
87 XtVaCreateManagedWidget( label1
.c_str(),
89 style
& wxCOLOURED
? xmLabelWidgetClass
96 wxFont::GetFontTag(), fontType
,
97 XmNlabelString
, text(),
98 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
99 // if it was compiled with 1.2 compatibility
100 // TODO: check this still looks OK for Motif 1.2.
101 #if (XmVersion > 1200)
102 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
104 XmNchildType
, XmFRAME_TITLE_CHILD
,
106 XmNchildVerticalAlignment
,
113 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
115 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
116 XmHORIZONTAL
: XmVERTICAL
));
117 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
119 Widget radioBoxWidget
= XmCreateRadioBox ((Widget
)m_mainWidget
, "radioBoxWidget", args
, 2);
121 m_radioButtons
.reserve(n
);
122 m_radioButtonLabels
.reserve(n
);
125 for (i
= 0; i
< n
; i
++)
127 wxString
str(wxStripMenuCodes(choices
[i
]));
128 m_radioButtonLabels
.push_back(str
);
129 Widget radioItem
= XtVaCreateManagedWidget (wxConstCast(str
.c_str(), char),
131 xmToggleButtonGadgetClass
, radioBoxWidget
,
133 xmToggleButtonWidgetClass
, radioBoxWidget
,
135 wxFont::GetFontTag(), fontType
,
137 m_radioButtons
.push_back((WXWidget
)radioItem
);
138 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
139 (XtCallbackProc
) wxRadioBoxCallback
,
147 XtRealizeWidget((Widget
)m_mainWidget
);
148 XtManageChild (radioBoxWidget
);
149 XtManageChild ((Widget
)m_mainWidget
);
151 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
153 ChangeBackgroundColour();
159 wxRadioBox::~wxRadioBox()
161 DetachWidget(m_mainWidget
);
162 XtDestroyWidget((Widget
) m_mainWidget
);
164 m_mainWidget
= (WXWidget
) 0;
167 void wxRadioBox::SetString(int item
, const wxString
& label
)
169 if (item
< 0 || item
>= m_noItems
)
172 Widget widget
= (Widget
) m_radioButtons
[item
];
175 wxString
label1(wxStripMenuCodes(label
));
176 wxXmString
text( label1
);
177 m_radioButtonLabels
[item
] = label1
;
178 XtVaSetValues (widget
,
179 XmNlabelString
, text(),
180 XmNlabelType
, XmSTRING
,
185 int wxRadioBox::FindString(const wxString
& s
) const
188 for (i
= 0; i
< m_noItems
; i
++)
189 if (s
== m_radioButtonLabels
[i
])
194 void wxRadioBox::SetSelection(int n
)
196 if ((n
< 0) || (n
>= m_noItems
))
199 m_selectedButton
= n
;
203 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
206 for (i
= 0; i
< m_noItems
; i
++)
208 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
210 m_inSetValue
= FALSE
;
213 // Get single selection, for single choice list items
214 int wxRadioBox::GetSelection() const
216 return m_selectedButton
;
219 // Find string for position
220 wxString
wxRadioBox::GetString(int n
) const
222 if ((n
< 0) || (n
>= m_noItems
))
223 return wxEmptyString
;
224 return m_radioButtonLabels
[n
];
227 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
229 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
232 XtUnmanageChild ((Widget
) m_mainWidget
);
234 int xx
= x
; int yy
= y
;
235 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
237 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
238 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
239 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
240 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
243 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
245 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
248 XtManageChild ((Widget
) m_mainWidget
);
251 // Enable a specific button
252 void wxRadioBox::Enable(int n
, bool enable
)
254 if ((n
< 0) || (n
>= m_noItems
))
257 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
260 // Enable all controls
261 bool wxRadioBox::Enable(bool enable
)
263 if ( !wxControl::Enable(enable
) )
267 for (i
= 0; i
< m_noItems
; i
++)
268 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
273 bool wxRadioBox::Show(bool show
)
275 // TODO: show/hide all children
276 return wxControl::Show(show
);
279 // Show a specific button
280 void wxRadioBox::Show(int n
, bool show
)
282 // This method isn't complete, and we try do do our best...
283 // It's main purpose isn't for allowing Show/Unshow dynamically,
284 // but rather to provide a way to design wxRadioBox such:
286 // o Val1 o Val2 o Val3
288 // o Val7 o Val8 o Val9
290 // In my case, this is a 'direction' box, and the Show(5,False) is
291 // coupled with an Enable(5,False)
293 if ((n
< 0) || (n
>= m_noItems
))
296 XtVaSetValues ((Widget
) m_radioButtons
[n
],
297 XmNindicatorOn
, (unsigned char) show
,
300 // Please note that this is all we can do: removing the label
301 // if switching to unshow state. However, when switching
302 // to the on state, it's the prog. resp. to call SetString(item,...)
305 wxRadioBox::SetString (n
, " ");
308 // For single selection items only
309 wxString
wxRadioBox::GetStringSelection () const
311 int sel
= GetSelection ();
313 return this->GetString (sel
);
318 bool wxRadioBox::SetStringSelection (const wxString
& s
)
320 int sel
= FindString (s
);
330 void wxRadioBox::Command (wxCommandEvent
& event
)
332 SetSelection (event
.m_commandInt
);
333 ProcessCommand (event
);
336 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
338 wxWindow::ChangeFont(keepOriginalSize
);
340 WXFontType fontType
=
341 m_font
.GetFontType(XtDisplay((Widget
) GetTopWidget()));
344 for (i
= 0; i
< m_noItems
; i
++)
346 WXWidget radioButton
= m_radioButtons
[i
];
348 XtVaSetValues ((Widget
) radioButton
,
349 wxFont::GetFontTag(), fontType
,
354 void wxRadioBox::ChangeBackgroundColour()
356 wxWindow::ChangeBackgroundColour();
358 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
361 for (i
= 0; i
< m_noItems
; i
++)
363 WXWidget radioButton
= m_radioButtons
[i
];
365 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
367 XtVaSetValues ((Widget
) radioButton
,
368 XmNselectColor
, selectPixel
,
373 void wxRadioBox::ChangeForegroundColour()
375 wxWindow::ChangeForegroundColour();
378 for (i
= 0; i
< m_noItems
; i
++)
380 WXWidget radioButton
= m_radioButtons
[i
];
382 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
386 static int CalcOtherDim( int items
, int dim
)
388 return items
/ dim
+ ( items
% dim
? 1 : 0 );
391 int wxRadioBox::GetRowCount() const
393 return m_windowStyle
& wxRA_SPECIFY_ROWS
? m_noRowsOrCols
394 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
397 int wxRadioBox::GetColumnCount() const
399 return m_windowStyle
& wxRA_SPECIFY_COLS
? m_noRowsOrCols
400 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
403 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
404 XmToggleButtonCallbackStruct
* cbs
)
409 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
412 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
413 for (i
= 0; i
< item
->GetCount(); i
++)
414 if (((Widget
)buttons
[i
]) == w
)
418 if (item
->InSetValue())
421 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
423 event
.SetString(item
->GetStringSelection());
424 event
.SetEventObject(item
);
425 item
->ProcessCommand (event
);