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 (void)XtVaCreateManagedWidget( label1
.c_str(),
107 style
& wxCOLOURED
? xmLabelWidgetClass
108 : xmLabelGadgetClass
,
109 (Widget
)m_mainWidget
,
112 (Widget
)m_mainWidget
,
114 wxFont::GetFontTag(), fontType
,
115 XmNlabelString
, text(),
116 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
117 // if it was compiled with 1.2 compatibility
118 // TODO: check this still looks OK for Motif 1.2.
119 #if (XmVersion > 1200)
120 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
122 XmNchildType
, XmFRAME_TITLE_CHILD
,
124 XmNchildVerticalAlignment
,
131 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
133 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
134 XmHORIZONTAL
: XmVERTICAL
));
135 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
137 Widget radioBoxWidget
= XmCreateRadioBox ((Widget
)m_mainWidget
, "radioBoxWidget", args
, 2);
139 // if (style & wxFLAT)
140 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
142 m_radioButtons
= new WXWidget
[n
];
143 m_radioButtonLabels
= new wxString
[n
];
145 for (i
= 0; i
< n
; i
++)
147 wxString
str(wxStripMenuCodes(choices
[i
]));
148 m_radioButtonLabels
[i
] = str
;
149 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
151 xmToggleButtonGadgetClass
, radioBoxWidget
,
153 xmToggleButtonWidgetClass
, radioBoxWidget
,
155 wxFont::GetFontTag(), fontType
,
157 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
161 m_font
= parent
->GetFont();
166 XtRealizeWidget((Widget
)m_mainWidget
);
167 XtManageChild (radioBoxWidget
);
168 XtManageChild ((Widget
)m_mainWidget
);
170 SetCanAddEventHandler(TRUE
);
171 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
173 ChangeBackgroundColour();
179 wxRadioBox::~wxRadioBox()
181 delete[] m_radioButtonLabels
;
182 delete[] m_radioButtons
;
184 DetachWidget(m_mainWidget
);
185 XtDestroyWidget((Widget
) m_mainWidget
);
187 m_mainWidget
= (WXWidget
) 0;
190 void wxRadioBox::SetString(int item
, const wxString
& label
)
192 if (item
< 0 || item
>= m_noItems
)
195 Widget widget
= (Widget
) m_radioButtons
[item
];
198 wxString
label1(wxStripMenuCodes(label
));
199 wxXmString
text( label1
);
200 XtVaSetValues (widget
,
201 XmNlabelString
, text(),
202 XmNlabelType
, XmSTRING
,
207 int wxRadioBox::FindString(const wxString
& s
) const
210 for (i
= 0; i
< m_noItems
; i
++)
211 if (s
== m_radioButtonLabels
[i
])
216 void wxRadioBox::SetSelection(int n
)
218 if ((n
< 0) || (n
>= m_noItems
))
221 m_selectedButton
= n
;
225 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
228 for (i
= 0; i
< m_noItems
; i
++)
230 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
232 m_inSetValue
= FALSE
;
235 // Get single selection, for single choice list items
236 int wxRadioBox::GetSelection() const
238 return m_selectedButton
;
241 // Find string for position
242 wxString
wxRadioBox::GetString(int n
) const
244 if ((n
< 0) || (n
>= m_noItems
))
245 return wxEmptyString
;
246 return m_radioButtonLabels
[n
];
249 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
251 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
254 XtUnmanageChild ((Widget
) m_mainWidget
);
256 int xx
= x
; int yy
= y
;
257 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
259 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
260 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
261 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
262 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
265 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
267 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
270 XtManageChild ((Widget
) m_mainWidget
);
273 // Enable a specific button
274 void wxRadioBox::Enable(int n
, bool enable
)
276 if ((n
< 0) || (n
>= m_noItems
))
279 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
282 // Enable all controls
283 bool wxRadioBox::Enable(bool enable
)
285 if ( !wxControl::Enable(enable
) )
289 for (i
= 0; i
< m_noItems
; i
++)
290 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
295 bool wxRadioBox::Show(bool show
)
297 // TODO: show/hide all children
298 return wxControl::Show(show
);
301 // Show a specific button
302 void wxRadioBox::Show(int n
, bool show
)
304 // This method isn't complete, and we try do do our best...
305 // It's main purpose isn't for allowing Show/Unshow dynamically,
306 // but rather to provide a way to design wxRadioBox such:
308 // o Val1 o Val2 o Val3
310 // o Val7 o Val8 o Val9
312 // In my case, this is a 'direction' box, and the Show(5,False) is
313 // coupled with an Enable(5,False)
315 if ((n
< 0) || (n
>= m_noItems
))
318 XtVaSetValues ((Widget
) m_radioButtons
[n
],
319 XmNindicatorOn
, (unsigned char) show
,
322 // Please note that this is all we can do: removing the label
323 // if switching to unshow state. However, when switching
324 // to the on state, it's the prog. resp. to call SetString(item,...)
327 wxRadioBox::SetString (n
, " ");
330 // For single selection items only
331 wxString
wxRadioBox::GetStringSelection () const
333 int sel
= GetSelection ();
335 return this->GetString (sel
);
340 bool wxRadioBox::SetStringSelection (const wxString
& s
)
342 int sel
= FindString (s
);
352 void wxRadioBox::Command (wxCommandEvent
& event
)
354 SetSelection (event
.m_commandInt
);
355 ProcessCommand (event
);
358 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
360 wxWindow::ChangeFont(keepOriginalSize
);
362 WXFontType fontType
=
363 m_font
.GetFontType(XtDisplay((Widget
) GetTopWidget()));
366 for (i
= 0; i
< m_noItems
; i
++)
368 WXWidget radioButton
= m_radioButtons
[i
];
370 XtVaSetValues ((Widget
) radioButton
,
371 wxFont::GetFontTag(), fontType
,
376 void wxRadioBox::ChangeBackgroundColour()
378 wxWindow::ChangeBackgroundColour();
380 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
383 for (i
= 0; i
< m_noItems
; i
++)
385 WXWidget radioButton
= m_radioButtons
[i
];
387 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
389 XtVaSetValues ((Widget
) radioButton
,
390 XmNselectColor
, selectPixel
,
395 void wxRadioBox::ChangeForegroundColour()
397 wxWindow::ChangeForegroundColour();
400 for (i
= 0; i
< m_noItems
; i
++)
402 WXWidget radioButton
= m_radioButtons
[i
];
404 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
408 static int CalcOtherDim( int items
, int dim
)
410 return items
/ dim
+ ( items
% dim
? 1 : 0 );
413 int wxRadioBox::GetRowCount() const
415 return m_windowStyle
& wxRA_SPECIFY_ROWS
? m_noRowsOrCols
416 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
419 int wxRadioBox::GetColumnCount() const
421 return m_windowStyle
& wxRA_SPECIFY_COLS
? m_noRowsOrCols
422 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
425 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
426 XmToggleButtonCallbackStruct
* cbs
)
431 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
434 for (i
= 0; i
< item
->GetCount(); i
++)
435 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
439 if (item
->InSetValue())
442 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
444 event
.SetString(item
->GetStringSelection());
445 event
.SetEventObject(item
);
446 item
->ProcessCommand (event
);