1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #define XtDisplay XTDISPLAY
21 #include "wx/radiobox.h"
23 #include "wx/arrstr.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();
72 Display
* dpy
= XtDisplay(parentWidget
);
74 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
75 xmFrameWidgetClass
, parentWidget
,
76 XmNresizeHeight
, True
,
80 wxString
label1(wxStripMenuCodes(title
));
84 wxXmString
text(label1
);
85 m_labelWidget
= (WXWidget
)
86 XtVaCreateManagedWidget( label1
.c_str(),
88 style
& wxCOLOURED
? xmLabelWidgetClass
92 xmLabelWidgetClass
, (Widget
)m_mainWidget
,
94 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
95 XmNlabelString
, text(),
96 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
97 // if it was compiled with 1.2 compatibility
98 // TODO: check this still looks OK for Motif 1.2.
99 #if (XmVersion > 1200)
100 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
102 XmNchildType
, XmFRAME_TITLE_CHILD
,
104 XmNchildVerticalAlignment
, XmALIGNMENT_CENTER
,
110 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
112 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
113 XmHORIZONTAL
: XmVERTICAL
));
114 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
115 XtSetArg (args
[2], XmNadjustLast
, False
);
117 Widget radioBoxWidget
=
118 XmCreateRadioBox ((Widget
)m_mainWidget
, wxMOTIF_STR("radioBoxWidget"), args
, 3);
120 m_radioButtons
.reserve(n
);
121 m_radioButtonLabels
.reserve(n
);
124 for (i
= 0; i
< n
; i
++)
126 wxString
str(wxStripMenuCodes(choices
[i
]));
127 m_radioButtonLabels
.push_back(str
);
128 Widget radioItem
= XtVaCreateManagedWidget (
129 wxConstCast(str
.c_str(), char),
131 xmToggleButtonGadgetClass
, radioBoxWidget
,
133 xmToggleButtonWidgetClass
, radioBoxWidget
,
135 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
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();
158 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
159 const wxPoint
& pos
, const wxSize
& size
,
160 const wxArrayString
& choices
,
161 int majorDim
, long style
,
162 const wxValidator
& val
, const wxString
& name
)
164 wxCArrayString
chs(choices
);
165 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
166 chs
.GetStrings(), majorDim
, style
, val
, name
);
169 wxRadioBox::~wxRadioBox()
171 DetachWidget(m_mainWidget
);
172 XtDestroyWidget((Widget
) m_mainWidget
);
174 m_mainWidget
= (WXWidget
) 0;
177 void wxRadioBox::SetString(int item
, const wxString
& label
)
182 Widget widget
= (Widget
) m_radioButtons
[item
];
185 wxString
label1(wxStripMenuCodes(label
));
186 wxXmString
text( label1
);
187 m_radioButtonLabels
[item
] = label1
;
188 XtVaSetValues (widget
,
189 XmNlabelString
, text(),
190 XmNlabelType
, XmSTRING
,
195 int wxRadioBox::FindString(const wxString
& s
) const
198 for (i
= 0; i
< m_noItems
; i
++)
199 if (s
== m_radioButtonLabels
[i
])
204 void wxRadioBox::SetSelection(int n
)
209 m_selectedButton
= n
;
213 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], True
, False
);
216 for (i
= 0; i
< m_noItems
; i
++)
218 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], False
, False
);
220 m_inSetValue
= false;
223 // Get single selection, for single choice list items
224 int wxRadioBox::GetSelection() const
226 return m_selectedButton
;
229 // Find string for position
230 wxString
wxRadioBox::GetString(int n
) const
233 return wxEmptyString
;
234 return m_radioButtonLabels
[n
];
237 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
239 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
242 XtUnmanageChild ((Widget
) m_mainWidget
);
244 int xx
= x
; int yy
= y
;
245 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
247 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
248 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
249 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
250 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
253 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
255 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
258 XtManageChild ((Widget
) m_mainWidget
);
261 // Enable a specific button
262 bool wxRadioBox::Enable(int n
, bool enable
)
267 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
271 // Enable all controls
272 bool wxRadioBox::Enable(bool enable
)
274 if ( !wxControl::Enable(enable
) )
278 for (i
= 0; i
< m_noItems
; i
++)
279 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
284 bool wxRadioBox::Show(bool show
)
286 // TODO: show/hide all children
287 return wxControl::Show(show
);
290 // Show a specific button
291 bool wxRadioBox::Show(int n
, bool show
)
293 // This method isn't complete, and we try do do our best...
294 // It's main purpose isn't for allowing Show/Unshow dynamically,
295 // but rather to provide a way to design wxRadioBox such:
297 // o Val1 o Val2 o Val3
299 // o Val7 o Val8 o Val9
301 // In my case, this is a 'direction' box, and the Show(5,False) is
302 // coupled with an Enable(5,False)
307 XtVaSetValues ((Widget
) m_radioButtons
[n
],
308 XmNindicatorOn
, (unsigned char) show
,
311 // Please note that this is all we can do: removing the label
312 // if switching to unshow state. However, when switching
313 // to the on state, it's the prog. resp. to call SetString(item,...)
316 wxRadioBox::SetString (n
, " ");
321 // For single selection items only
322 wxString
wxRadioBox::GetStringSelection () const
324 int sel
= GetSelection ();
326 return this->GetString (sel
);
328 return wxEmptyString
;
331 bool wxRadioBox::SetStringSelection (const wxString
& s
)
333 int sel
= FindString (s
);
343 void wxRadioBox::Command (wxCommandEvent
& event
)
345 SetSelection (event
.GetInt());
346 ProcessCommand (event
);
349 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
351 wxWindow::ChangeFont(keepOriginalSize
);
354 for (i
= 0; i
< m_noItems
; i
++)
356 WXWidget radioButton
= m_radioButtons
[i
];
358 XtVaSetValues ((Widget
) radioButton
,
359 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay((Widget
) GetTopWidget())),
364 void wxRadioBox::ChangeBackgroundColour()
366 wxWindow::ChangeBackgroundColour();
368 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
371 for (i
= 0; i
< m_noItems
; i
++)
373 WXWidget radioButton
= m_radioButtons
[i
];
375 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, true);
377 XtVaSetValues ((Widget
) radioButton
,
378 XmNselectColor
, selectPixel
,
383 void wxRadioBox::ChangeForegroundColour()
385 wxWindow::ChangeForegroundColour();
388 for (i
= 0; i
< m_noItems
; i
++)
390 WXWidget radioButton
= m_radioButtons
[i
];
392 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
396 static int CalcOtherDim( int items
, int dim
)
398 return items
/ dim
+ ( items
% dim
? 1 : 0 );
401 int wxRadioBox::GetRowCount() const
403 return m_windowStyle
& wxRA_SPECIFY_ROWS
? m_noRowsOrCols
404 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
407 int wxRadioBox::GetColumnCount() const
409 return m_windowStyle
& wxRA_SPECIFY_COLS
? m_noRowsOrCols
410 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
413 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
414 XmToggleButtonCallbackStruct
* cbs
)
419 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
422 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
423 for (i
= 0; i
< item
->GetCount(); i
++)
424 if (((Widget
)buttons
[i
]) == w
)
428 if (item
->InSetValue())
431 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
433 event
.SetString(item
->GetStringSelection());
434 event
.SetEventObject(item
);
435 item
->ProcessCommand (event
);