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"
18 #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
) )
62 m_noItems
= (size_t)n
;
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
);
200 for (size_t i
= 0; i
< m_noItems
; i
++)
202 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], False
, False
);
204 m_inSetValue
= false;
207 // Get single selection, for single choice list items
208 int wxRadioBox::GetSelection() const
210 return m_selectedButton
;
213 // Find string for position
214 wxString
wxRadioBox::GetString(int n
) const
217 return wxEmptyString
;
218 return m_radioButtonLabels
[n
];
221 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
223 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
226 XtUnmanageChild ((Widget
) m_mainWidget
);
228 int xx
= x
; int yy
= y
;
229 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
231 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
232 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
233 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
234 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
237 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
239 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
242 XtManageChild ((Widget
) m_mainWidget
);
245 // Enable a specific button
246 bool wxRadioBox::Enable(int n
, bool enable
)
251 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
255 // Enable all controls
256 bool wxRadioBox::Enable(bool enable
)
258 if ( !wxControl::Enable(enable
) )
261 for (size_t i
= 0; i
< m_noItems
; i
++)
262 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
267 bool wxRadioBox::Show(bool show
)
269 // TODO: show/hide all children
270 return wxControl::Show(show
);
273 // Show a specific button
274 bool wxRadioBox::Show(int n
, bool show
)
276 // This method isn't complete, and we try do do our best...
277 // It's main purpose isn't for allowing Show/Unshow dynamically,
278 // but rather to provide a way to design wxRadioBox such:
280 // o Val1 o Val2 o Val3
282 // o Val7 o Val8 o Val9
284 // In my case, this is a 'direction' box, and the Show(5,False) is
285 // coupled with an Enable(5,False)
290 XtVaSetValues ((Widget
) m_radioButtons
[n
],
291 XmNindicatorOn
, (unsigned char) show
,
294 // Please note that this is all we can do: removing the label
295 // if switching to unshow state. However, when switching
296 // to the on state, it's the prog. resp. to call SetString(item,...)
299 wxRadioBox::SetString (n
, " ");
304 // For single selection items only
305 wxString
wxRadioBox::GetStringSelection () const
307 int sel
= GetSelection ();
309 return this->GetString (sel
);
311 return wxEmptyString
;
314 bool wxRadioBox::SetStringSelection (const wxString
& s
)
316 int sel
= FindString (s
);
326 void wxRadioBox::Command (wxCommandEvent
& event
)
328 SetSelection (event
.GetInt());
329 ProcessCommand (event
);
332 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
334 wxWindow::ChangeFont(keepOriginalSize
);
336 for (size_t i
= 0; i
< m_noItems
; i
++)
338 WXWidget radioButton
= m_radioButtons
[i
];
340 XtVaSetValues ((Widget
) radioButton
,
341 wxFont::GetFontTag(), m_font
.GetFontTypeC(XtDisplay((Widget
) GetTopWidget())),
346 void wxRadioBox::ChangeBackgroundColour()
348 wxWindow::ChangeBackgroundColour();
350 int selectPixel
= wxBLACK
->AllocColour(XtDisplay((Widget
)m_mainWidget
));
352 for (size_t i
= 0; i
< m_noItems
; i
++)
354 WXWidget radioButton
= m_radioButtons
[i
];
356 wxDoChangeBackgroundColour(radioButton
, m_backgroundColour
, true);
358 XtVaSetValues ((Widget
) radioButton
,
359 XmNselectColor
, selectPixel
,
364 void wxRadioBox::ChangeForegroundColour()
366 wxWindow::ChangeForegroundColour();
368 for (size_t i
= 0; i
< m_noItems
; i
++)
370 WXWidget radioButton
= m_radioButtons
[i
];
372 wxDoChangeForegroundColour(radioButton
, m_foregroundColour
);
376 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
377 XmToggleButtonCallbackStruct
* cbs
)
382 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
385 const wxWidgetArray
& buttons
= item
->GetRadioButtons();
386 for (i
= 0; i
< item
->GetCount(); i
++)
387 if (((Widget
)buttons
[i
]) == w
)
391 if (item
->InSetValue())
394 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
396 event
.SetString(item
->GetStringSelection());
397 event
.SetEventObject(item
);
398 item
->ProcessCommand (event
);
401 #endif // wxUSE_RADIOBOX