1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/radiobox.cpp
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;
53 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
54 const wxPoint
& pos
, const wxSize
& size
,
55 int n
, const wxString choices
[],
56 int majorDim
, long style
,
57 const wxValidator
& val
, const wxString
& name
)
59 if( !CreateControl( parent
, id
, pos
, size
, style
, val
, name
) )
63 m_noRowsOrCols
= majorDim
;
65 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
67 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
68 Display
* dpy
= XtDisplay(parentWidget
);
70 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
71 xmFrameWidgetClass
, parentWidget
,
72 XmNresizeHeight
, True
,
76 wxString
label1(wxStripMenuCodes(title
));
80 wxXmString
text(label1
);
81 m_labelWidget
= (WXWidget
)
82 XtVaCreateManagedWidget( label1
.c_str(),
84 style
& wxCOLOURED
? xmLabelWidgetClass
88 xmLabelWidgetClass
, (Widget
)m_mainWidget
,
90 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
91 XmNlabelString
, text(),
92 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
93 // if it was compiled with 1.2 compatibility
94 // TODO: check this still looks OK for Motif 1.2.
95 #if (XmVersion > 1200)
96 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
98 XmNchildType
, XmFRAME_TITLE_CHILD
,
100 XmNchildVerticalAlignment
, XmALIGNMENT_CENTER
,
106 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
107 XmHORIZONTAL
: XmVERTICAL
));
108 XtSetArg (args
[1], XmNnumColumns
, GetMajorDim());
109 XtSetArg (args
[2], XmNadjustLast
, False
);
111 Widget radioBoxWidget
=
112 XmCreateRadioBox ((Widget
)m_mainWidget
, wxMOTIF_STR("radioBoxWidget"), args
, 3);
114 m_radioButtons
.reserve(n
);
115 m_radioButtonLabels
.reserve(n
);
118 for (i
= 0; i
< n
; i
++)
120 wxString
str(wxStripMenuCodes(choices
[i
]));
121 m_radioButtonLabels
.push_back(str
);
122 Widget radioItem
= XtVaCreateManagedWidget (
123 wxConstCast(str
.c_str(), char),
125 xmToggleButtonGadgetClass
, radioBoxWidget
,
127 xmToggleButtonWidgetClass
, radioBoxWidget
,
129 wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
),
131 m_radioButtons
.push_back((WXWidget
)radioItem
);
132 XtAddCallback (radioItem
, XmNvalueChangedCallback
,
133 (XtCallbackProc
) wxRadioBoxCallback
,
141 XtRealizeWidget((Widget
)m_mainWidget
);
142 XtManageChild (radioBoxWidget
);
143 XtManageChild ((Widget
)m_mainWidget
);
145 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
147 ChangeBackgroundColour();
152 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
153 const wxPoint
& pos
, const wxSize
& size
,
154 const wxArrayString
& choices
,
155 int majorDim
, long style
,
156 const wxValidator
& val
, const wxString
& name
)
158 wxCArrayString
chs(choices
);
159 return Create(parent
, id
, title
, pos
, size
, chs
.GetCount(),
160 chs
.GetStrings(), majorDim
, style
, val
, name
);
163 wxRadioBox::~wxRadioBox()
165 DetachWidget(m_mainWidget
);
166 XtDestroyWidget((Widget
) m_mainWidget
);
168 m_mainWidget
= (WXWidget
) 0;
171 void wxRadioBox::SetString(int item
, const wxString
& label
)
176 Widget widget
= (Widget
) m_radioButtons
[item
];
179 wxString
label1(wxStripMenuCodes(label
));
180 wxXmString
text( label1
);
181 m_radioButtonLabels
[item
] = label1
;
182 XtVaSetValues (widget
,
183 XmNlabelString
, text(),
184 XmNlabelType
, XmSTRING
,
189 void wxRadioBox::SetSelection(int n
)
194 m_selectedButton
= n
;
198 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], True
, False
);
201 for (i
= 0; i
< m_noItems
; i
++)
203 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], False
, False
);
205 m_inSetValue
= false;
208 // Get single selection, for single choice list items
209 int wxRadioBox::GetSelection() const
211 return m_selectedButton
;
214 // Find string for position
215 wxString
wxRadioBox::GetString(int n
) const
218 return wxEmptyString
;
219 return m_radioButtonLabels
[n
];
222 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
224 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
227 XtUnmanageChild ((Widget
) m_mainWidget
);
229 int xx
= x
; int yy
= y
;
230 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
232 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
233 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
234 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
235 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
238 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
240 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
243 XtManageChild ((Widget
) m_mainWidget
);
246 // Enable a specific button
247 bool wxRadioBox::Enable(int n
, bool enable
)
252 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
256 // Enable all controls
257 bool wxRadioBox::Enable(bool enable
)
259 if ( !wxControl::Enable(enable
) )
263 for (i
= 0; i
< m_noItems
; i
++)
264 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
269 bool wxRadioBox::Show(bool show
)
271 // TODO: show/hide all children
272 return wxControl::Show(show
);
275 // Show a specific button
276 bool wxRadioBox::Show(int n
, bool show
)
278 // This method isn't complete, and we try do do our best...
279 // It's main purpose isn't for allowing Show/Unshow dynamically,
280 // but rather to provide a way to design wxRadioBox such:
282 // o Val1 o Val2 o Val3
284 // o Val7 o Val8 o Val9
286 // In my case, this is a 'direction' box, and the Show(5,False) is
287 // coupled with an Enable(5,False)
292 XtVaSetValues ((Widget
) m_radioButtons
[n
],
293 XmNindicatorOn
, (unsigned char) show
,
296 // Please note that this is all we can do: removing the label
297 // if switching to unshow state. However, when switching
298 // to the on state, it's the prog. resp. to call SetString(item,...)
301 wxRadioBox::SetString (n
, " ");
306 // For single selection items only
307 wxString
wxRadioBox::GetStringSelection () const
309 int sel
= GetSelection ();
311 return this->GetString (sel
);
313 return wxEmptyString
;
316 bool wxRadioBox::SetStringSelection (const wxString
& s
)
318 int sel
= FindString (s
);
328 void wxRadioBox::Command (wxCommandEvent
& event
)
330 SetSelection (event
.GetInt());
331 ProcessCommand (event
);
334 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
336 wxWindow::ChangeFont(keepOriginalSize
);
339 for (i
= 0; i
< m_noItems
; i
++)
341 WXWidget radioButton
= m_radioButtons
[i
];
343 XtVaSetValues ((Widget
) radioButton
,
344 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay((Widget
) GetTopWidget())),
349 void wxRadioBox::ChangeBackgroundColour()
351 wxWindow::ChangeBackgroundColour();
353 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
356 for (i
= 0; i
< m_noItems
; i
++)
358 WXWidget radioButton
= m_radioButtons
[i
];
360 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, true);
362 XtVaSetValues ((Widget
) radioButton
,
363 XmNselectColor
, selectPixel
,
368 void wxRadioBox::ChangeForegroundColour()
370 wxWindow::ChangeForegroundColour();
373 for (i
= 0; i
< m_noItems
; i
++)
375 WXWidget radioButton
= m_radioButtons
[i
];
377 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
381 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
382 XmToggleButtonCallbackStruct
* cbs
)
387 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
390 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
391 for (i
= 0; i
< item
->GetCount(); i
++)
392 if (((Widget
)buttons
[i
]) == w
)
396 if (item
->InSetValue())
399 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
401 event
.SetString(item
->GetStringSelection());
402 event
.SetEventObject(item
);
403 item
->ProcessCommand (event
);