1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
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 wxRadioBox::wxRadioBox()
48 m_selectedButton
= -1;
53 m_radioButtons
= (WXWidget
*) NULL
;
54 m_radioButtonLabels
= (wxString
*) NULL
;
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 m_selectedButton
= -1;
65 m_radioButtons
= (WXWidget
*) NULL
;
66 m_radioButtonLabels
= (wxString
*) NULL
;
67 m_backgroundColour
= parent
->GetBackgroundColour();
68 m_foregroundColour
= parent
->GetForegroundColour();
69 m_font
= parent
->GetFont();
74 parent
->AddChild(this);
76 m_windowStyle
= (long&)style
;
79 m_windowId
= NewControlId();
83 m_noRowsOrCols
= majorDim
;
88 m_majorDim
= majorDim
;
90 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
92 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
93 xmFrameWidgetClass
, parentWidget
,
94 XmNresizeHeight
, True
,
98 wxString
label1(wxStripMenuCodes(title
));
100 WXFontType fontType
= m_font
.GetFontType(XtDisplay(parentWidget
));
104 wxXmString
text(label1
);
105 m_labelWidget
= (WXWidget
)
106 XtVaCreateManagedWidget( label1
.c_str(),
108 style
& wxCOLOURED
? xmLabelWidgetClass
109 : xmLabelGadgetClass
,
110 (Widget
)m_mainWidget
,
113 (Widget
)m_mainWidget
,
115 wxFont::GetFontTag(), fontType
,
116 XmNlabelString
, text(),
117 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
118 // if it was compiled with 1.2 compatibility
119 // TODO: check this still looks OK for Motif 1.2.
120 #if (XmVersion > 1200)
121 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
123 XmNchildType
, XmFRAME_TITLE_CHILD
,
125 XmNchildVerticalAlignment
,
132 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
134 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
135 XmHORIZONTAL
: XmVERTICAL
));
136 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
138 Widget radioBoxWidget
= XmCreateRadioBox ((Widget
)m_mainWidget
, "radioBoxWidget", args
, 2);
140 // if (style & wxFLAT)
141 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
143 m_radioButtons
= new WXWidget
[n
];
144 m_radioButtonLabels
= new wxString
[n
];
146 for (i
= 0; i
< n
; i
++)
148 wxString
str(wxStripMenuCodes(choices
[i
]));
149 m_radioButtonLabels
[i
] = str
;
150 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget (wxConstCast(str
.c_str(), char),
152 xmToggleButtonGadgetClass
, radioBoxWidget
,
154 xmToggleButtonWidgetClass
, radioBoxWidget
,
156 wxFont::GetFontTag(), fontType
,
158 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
162 m_font
= parent
->GetFont();
167 XtRealizeWidget((Widget
)m_mainWidget
);
168 XtManageChild (radioBoxWidget
);
169 XtManageChild ((Widget
)m_mainWidget
);
171 SetCanAddEventHandler(TRUE
);
172 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
174 ChangeBackgroundColour();
180 wxRadioBox::~wxRadioBox()
182 delete[] m_radioButtonLabels
;
183 delete[] m_radioButtons
;
185 DetachWidget(m_mainWidget
);
186 XtDestroyWidget((Widget
) m_mainWidget
);
188 m_mainWidget
= (WXWidget
) 0;
191 void wxRadioBox::SetString(int item
, const wxString
& label
)
193 if (item
< 0 || item
>= m_noItems
)
196 Widget widget
= (Widget
) m_radioButtons
[item
];
199 wxString
label1(wxStripMenuCodes(label
));
200 wxXmString
text( label1
);
201 XtVaSetValues (widget
,
202 XmNlabelString
, text(),
203 XmNlabelType
, XmSTRING
,
208 int wxRadioBox::FindString(const wxString
& s
) const
211 for (i
= 0; i
< m_noItems
; i
++)
212 if (s
== m_radioButtonLabels
[i
])
217 void wxRadioBox::SetSelection(int n
)
219 if ((n
< 0) || (n
>= m_noItems
))
222 m_selectedButton
= n
;
226 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
229 for (i
= 0; i
< m_noItems
; i
++)
231 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
233 m_inSetValue
= FALSE
;
236 // Get single selection, for single choice list items
237 int wxRadioBox::GetSelection() const
239 return m_selectedButton
;
242 // Find string for position
243 wxString
wxRadioBox::GetString(int n
) const
245 if ((n
< 0) || (n
>= m_noItems
))
246 return wxEmptyString
;
247 return m_radioButtonLabels
[n
];
250 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
252 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
255 XtUnmanageChild ((Widget
) m_mainWidget
);
257 int xx
= x
; int yy
= y
;
258 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
260 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
261 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
262 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
263 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
266 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
268 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
271 XtManageChild ((Widget
) m_mainWidget
);
274 // Enable a specific button
275 void wxRadioBox::Enable(int n
, bool enable
)
277 if ((n
< 0) || (n
>= m_noItems
))
280 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
283 // Enable all controls
284 bool wxRadioBox::Enable(bool enable
)
286 if ( !wxControl::Enable(enable
) )
290 for (i
= 0; i
< m_noItems
; i
++)
291 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
296 bool wxRadioBox::Show(bool show
)
298 // TODO: show/hide all children
299 return wxControl::Show(show
);
302 // Show a specific button
303 void wxRadioBox::Show(int n
, bool show
)
305 // This method isn't complete, and we try do do our best...
306 // It's main purpose isn't for allowing Show/Unshow dynamically,
307 // but rather to provide a way to design wxRadioBox such:
309 // o Val1 o Val2 o Val3
311 // o Val7 o Val8 o Val9
313 // In my case, this is a 'direction' box, and the Show(5,False) is
314 // coupled with an Enable(5,False)
316 if ((n
< 0) || (n
>= m_noItems
))
319 XtVaSetValues ((Widget
) m_radioButtons
[n
],
320 XmNindicatorOn
, (unsigned char) show
,
323 // Please note that this is all we can do: removing the label
324 // if switching to unshow state. However, when switching
325 // to the on state, it's the prog. resp. to call SetString(item,...)
328 wxRadioBox::SetString (n
, " ");
331 // For single selection items only
332 wxString
wxRadioBox::GetStringSelection () const
334 int sel
= GetSelection ();
336 return this->GetString (sel
);
341 bool wxRadioBox::SetStringSelection (const wxString
& s
)
343 int sel
= FindString (s
);
353 void wxRadioBox::Command (wxCommandEvent
& event
)
355 SetSelection (event
.m_commandInt
);
356 ProcessCommand (event
);
359 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
361 wxWindow::ChangeFont(keepOriginalSize
);
363 WXFontType fontType
=
364 m_font
.GetFontType(XtDisplay((Widget
) GetTopWidget()));
367 for (i
= 0; i
< m_noItems
; i
++)
369 WXWidget radioButton
= m_radioButtons
[i
];
371 XtVaSetValues ((Widget
) radioButton
,
372 wxFont::GetFontTag(), fontType
,
377 void wxRadioBox::ChangeBackgroundColour()
379 wxWindow::ChangeBackgroundColour();
381 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
384 for (i
= 0; i
< m_noItems
; i
++)
386 WXWidget radioButton
= m_radioButtons
[i
];
388 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
390 XtVaSetValues ((Widget
) radioButton
,
391 XmNselectColor
, selectPixel
,
396 void wxRadioBox::ChangeForegroundColour()
398 wxWindow::ChangeForegroundColour();
401 for (i
= 0; i
< m_noItems
; i
++)
403 WXWidget radioButton
= m_radioButtons
[i
];
405 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
409 static int CalcOtherDim( int items
, int dim
)
411 return items
/ dim
+ ( items
% dim
? 1 : 0 );
414 int wxRadioBox::GetRowCount() const
416 return m_windowStyle
& wxRA_SPECIFY_ROWS
? m_noRowsOrCols
417 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
420 int wxRadioBox::GetColumnCount() const
422 return m_windowStyle
& wxRA_SPECIFY_COLS
? m_noRowsOrCols
423 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
426 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
427 XmToggleButtonCallbackStruct
* cbs
)
432 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
435 for (i
= 0; i
< item
->GetCount(); i
++)
436 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
440 if (item
->InSetValue())
443 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
445 event
.SetString(item
->GetStringSelection());
446 event
.SetEventObject(item
);
447 item
->ProcessCommand (event
);