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
);
118 XtSetArg (args
[2], XmNadjustLast
, False
);
120 Widget radioBoxWidget
=
121 XmCreateRadioBox ((Widget
)m_mainWidget
, "radioBoxWidget", args
, 3);
123 m_radioButtons
.reserve(n
);
124 m_radioButtonLabels
.reserve(n
);
127 for (i
= 0; i
< n
; i
++)
129 wxString
str(wxStripMenuCodes(choices
[i
]));
130 m_radioButtonLabels
.push_back(str
);
131 Widget radioItem
= XtVaCreateManagedWidget (wxConstCast(str
.c_str(), char),
133 xmToggleButtonGadgetClass
, radioBoxWidget
,
135 xmToggleButtonWidgetClass
, radioBoxWidget
,
137 wxFont::GetFontTag(), fontType
,
139 m_radioButtons
.push_back((WXWidget
)radioItem
);
140 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
141 (XtCallbackProc
) wxRadioBoxCallback
,
149 XtRealizeWidget((Widget
)m_mainWidget
);
150 XtManageChild (radioBoxWidget
);
151 XtManageChild ((Widget
)m_mainWidget
);
153 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
155 ChangeBackgroundColour();
161 wxRadioBox::~wxRadioBox()
163 DetachWidget(m_mainWidget
);
164 XtDestroyWidget((Widget
) m_mainWidget
);
166 m_mainWidget
= (WXWidget
) 0;
169 void wxRadioBox::SetString(int item
, const wxString
& label
)
171 if (item
< 0 || item
>= m_noItems
)
174 Widget widget
= (Widget
) m_radioButtons
[item
];
177 wxString
label1(wxStripMenuCodes(label
));
178 wxXmString
text( label1
);
179 m_radioButtonLabels
[item
] = label1
;
180 XtVaSetValues (widget
,
181 XmNlabelString
, text(),
182 XmNlabelType
, XmSTRING
,
187 int wxRadioBox::FindString(const wxString
& s
) const
190 for (i
= 0; i
< m_noItems
; i
++)
191 if (s
== m_radioButtonLabels
[i
])
196 void wxRadioBox::SetSelection(int n
)
198 if ((n
< 0) || (n
>= m_noItems
))
201 m_selectedButton
= n
;
205 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
208 for (i
= 0; i
< m_noItems
; i
++)
210 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
212 m_inSetValue
= FALSE
;
215 // Get single selection, for single choice list items
216 int wxRadioBox::GetSelection() const
218 return m_selectedButton
;
221 // Find string for position
222 wxString
wxRadioBox::GetString(int n
) const
224 if ((n
< 0) || (n
>= m_noItems
))
225 return wxEmptyString
;
226 return m_radioButtonLabels
[n
];
229 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
231 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
234 XtUnmanageChild ((Widget
) m_mainWidget
);
236 int xx
= x
; int yy
= y
;
237 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
239 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
240 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
241 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
242 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
245 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
247 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
250 XtManageChild ((Widget
) m_mainWidget
);
253 // Enable a specific button
254 void wxRadioBox::Enable(int n
, bool enable
)
256 if ((n
< 0) || (n
>= m_noItems
))
259 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
262 // Enable all controls
263 bool wxRadioBox::Enable(bool enable
)
265 if ( !wxControl::Enable(enable
) )
269 for (i
= 0; i
< m_noItems
; i
++)
270 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
275 bool wxRadioBox::Show(bool show
)
277 // TODO: show/hide all children
278 return wxControl::Show(show
);
281 // Show a specific button
282 void wxRadioBox::Show(int n
, bool show
)
284 // This method isn't complete, and we try do do our best...
285 // It's main purpose isn't for allowing Show/Unshow dynamically,
286 // but rather to provide a way to design wxRadioBox such:
288 // o Val1 o Val2 o Val3
290 // o Val7 o Val8 o Val9
292 // In my case, this is a 'direction' box, and the Show(5,False) is
293 // coupled with an Enable(5,False)
295 if ((n
< 0) || (n
>= m_noItems
))
298 XtVaSetValues ((Widget
) m_radioButtons
[n
],
299 XmNindicatorOn
, (unsigned char) show
,
302 // Please note that this is all we can do: removing the label
303 // if switching to unshow state. However, when switching
304 // to the on state, it's the prog. resp. to call SetString(item,...)
307 wxRadioBox::SetString (n
, " ");
310 // For single selection items only
311 wxString
wxRadioBox::GetStringSelection () const
313 int sel
= GetSelection ();
315 return this->GetString (sel
);
320 bool wxRadioBox::SetStringSelection (const wxString
& s
)
322 int sel
= FindString (s
);
332 void wxRadioBox::Command (wxCommandEvent
& event
)
334 SetSelection (event
.m_commandInt
);
335 ProcessCommand (event
);
338 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
340 wxWindow::ChangeFont(keepOriginalSize
);
342 WXFontType fontType
=
343 m_font
.GetFontType(XtDisplay((Widget
) GetTopWidget()));
346 for (i
= 0; i
< m_noItems
; i
++)
348 WXWidget radioButton
= m_radioButtons
[i
];
350 XtVaSetValues ((Widget
) radioButton
,
351 wxFont::GetFontTag(), fontType
,
356 void wxRadioBox::ChangeBackgroundColour()
358 wxWindow::ChangeBackgroundColour();
360 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
363 for (i
= 0; i
< m_noItems
; i
++)
365 WXWidget radioButton
= m_radioButtons
[i
];
367 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
369 XtVaSetValues ((Widget
) radioButton
,
370 XmNselectColor
, selectPixel
,
375 void wxRadioBox::ChangeForegroundColour()
377 wxWindow::ChangeForegroundColour();
380 for (i
= 0; i
< m_noItems
; i
++)
382 WXWidget radioButton
= m_radioButtons
[i
];
384 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
388 static int CalcOtherDim( int items
, int dim
)
390 return items
/ dim
+ ( items
% dim
? 1 : 0 );
393 int wxRadioBox::GetRowCount() const
395 return m_windowStyle
& wxRA_SPECIFY_ROWS
? m_noRowsOrCols
396 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
399 int wxRadioBox::GetColumnCount() const
401 return m_windowStyle
& wxRA_SPECIFY_COLS
? m_noRowsOrCols
402 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
405 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
406 XmToggleButtonCallbackStruct
* cbs
)
411 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
414 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
415 for (i
= 0; i
< item
->GetCount(); i
++)
416 if (((Widget
)buttons
[i
]) == w
)
420 if (item
->InSetValue())
423 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
425 event
.SetString(item
->GetStringSelection());
426 event
.SetEventObject(item
);
427 item
->ProcessCommand (event
);