create stock GDI objects on demand; use const with GDI objects appropriately (patch...
[wxWidgets.git] / src / motif / radiobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/radiobox.cpp
3 // Purpose: wxRadioBox
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_RADIOBOX
16
17 #ifdef __VMS
18 #define XtDisplay XTDISPLAY
19 #endif
20
21 #include "wx/radiobox.h"
22 #include "wx/utils.h"
23 #include "wx/arrstr.h"
24
25 #ifdef __VMS__
26 #pragma message disable nosimpint
27 #endif
28 #include <Xm/Label.h>
29 #include <Xm/LabelG.h>
30 #include <Xm/ToggleB.h>
31 #include <Xm/ToggleBG.h>
32 #include <Xm/RowColumn.h>
33 #include <Xm/Frame.h>
34 #ifdef __VMS__
35 #pragma message enable nosimpint
36 #endif
37
38 #include "wx/motif/private.h"
39
40 void wxRadioBoxCallback (Widget w, XtPointer clientData,
41 XmToggleButtonCallbackStruct * cbs);
42
43 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
44
45 // Radio box item
46 void wxRadioBox::Init()
47 {
48 m_selectedButton = -1;
49 m_noItems = 0;
50 m_noRowsOrCols = 0;
51 }
52
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)
58 {
59 if( !CreateControl( parent, id, pos, size, style, val, name ) )
60 return false;
61
62 m_noItems = (unsigned int)n;
63 m_noRowsOrCols = majorDim;
64
65 SetMajorDim(majorDim == 0 ? n : majorDim, style);
66
67 Widget parentWidget = (Widget) parent->GetClientWidget();
68 Display* dpy = XtDisplay(parentWidget);
69
70 m_mainWidget = XtVaCreateWidget ("radioboxframe",
71 xmFrameWidgetClass, parentWidget,
72 XmNresizeHeight, True,
73 XmNresizeWidth, True,
74 NULL);
75
76 wxString label1(wxStripMenuCodes(title));
77
78 if (!label1.empty())
79 {
80 wxXmString text(label1);
81 m_labelWidget = (WXWidget)
82 XtVaCreateManagedWidget( label1.c_str(),
83 #if wxUSE_GADGETS
84 style & wxCOLOURED ? xmLabelWidgetClass
85 : xmLabelGadgetClass,
86 (Widget)m_mainWidget,
87 #else
88 xmLabelWidgetClass, (Widget)m_mainWidget,
89 #endif
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,
97 #else
98 XmNchildType, XmFRAME_TITLE_CHILD,
99 #endif
100 XmNchildVerticalAlignment, XmALIGNMENT_CENTER,
101 NULL);
102 }
103
104 Arg args[3];
105
106 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
107 XmHORIZONTAL : XmVERTICAL));
108 XtSetArg (args[1], XmNnumColumns, GetMajorDim());
109 XtSetArg (args[2], XmNadjustLast, False);
110
111 Widget radioBoxWidget =
112 XmCreateRadioBox ((Widget)m_mainWidget, wxMOTIF_STR("radioBoxWidget"), args, 3);
113
114 m_radioButtons.reserve(n);
115 m_radioButtonLabels.reserve(n);
116
117 int i;
118 for (i = 0; i < n; i++)
119 {
120 wxString str(wxStripMenuCodes(choices[i]));
121 m_radioButtonLabels.push_back(str);
122 Widget radioItem = XtVaCreateManagedWidget (
123 wxConstCast(str.c_str(), char),
124 #if wxUSE_GADGETS
125 xmToggleButtonGadgetClass, radioBoxWidget,
126 #else
127 xmToggleButtonWidgetClass, radioBoxWidget,
128 #endif
129 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
130 NULL);
131 m_radioButtons.push_back((WXWidget)radioItem);
132 XtAddCallback (radioItem, XmNvalueChangedCallback,
133 (XtCallbackProc) wxRadioBoxCallback,
134 (XtPointer) this);
135 }
136
137 ChangeFont(false);
138
139 SetSelection (0);
140
141 XtRealizeWidget((Widget)m_mainWidget);
142 XtManageChild (radioBoxWidget);
143 XtManageChild ((Widget)m_mainWidget);
144
145 AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y);
146
147 ChangeBackgroundColour();
148
149 return true;
150 }
151
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)
157 {
158 wxCArrayString chs(choices);
159 return Create(parent, id, title, pos, size, chs.GetCount(),
160 chs.GetStrings(), majorDim, style, val, name);
161 }
162
163 wxRadioBox::~wxRadioBox()
164 {
165 DetachWidget(m_mainWidget);
166 XtDestroyWidget((Widget) m_mainWidget);
167
168 m_mainWidget = (WXWidget) 0;
169 }
170
171 void wxRadioBox::SetString(unsigned int item, const wxString& label)
172 {
173 if (!IsValid(item))
174 return;
175
176 Widget widget = (Widget)m_radioButtons[item];
177 if (!label.empty())
178 {
179 wxString label1(wxStripMenuCodes(label));
180 wxXmString text( label1 );
181 m_radioButtonLabels[item] = label1;
182 XtVaSetValues (widget,
183 XmNlabelString, text(),
184 XmNlabelType, XmSTRING,
185 NULL);
186 }
187 }
188
189 void wxRadioBox::SetSelection(int n)
190 {
191 if (!IsValid(n))
192 return;
193
194 m_selectedButton = n;
195
196 m_inSetValue = true;
197
198 XmToggleButtonSetState ((Widget) m_radioButtons[n], True, False);
199
200 for (unsigned int i = 0; i < m_noItems; i++)
201 if (i != (unsigned int)n)
202 XmToggleButtonSetState ((Widget) m_radioButtons[i], False, False);
203
204 m_inSetValue = false;
205 }
206
207 // Get single selection, for single choice list items
208 int wxRadioBox::GetSelection() const
209 {
210 return m_selectedButton;
211 }
212
213 // Find string for position
214 wxString wxRadioBox::GetString(unsigned int n) const
215 {
216 if (!IsValid(n))
217 return wxEmptyString;
218 return m_radioButtonLabels[n];
219 }
220
221 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
222 {
223 bool managed = XtIsManaged((Widget) m_mainWidget);
224
225 if (managed)
226 XtUnmanageChild ((Widget) m_mainWidget);
227
228 int xx = x; int yy = y;
229 AdjustForParentClientOrigin(xx, yy, sizeFlags);
230
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);
235
236 if (width > 0)
237 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL);
238 if (height > 0)
239 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL);
240
241 if (managed)
242 XtManageChild ((Widget) m_mainWidget);
243 }
244
245 // Enable a specific button
246 bool wxRadioBox::Enable(unsigned int n, bool enable)
247 {
248 if (!IsValid(n))
249 return false;
250
251 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
252 return true;
253 }
254
255 // Enable all controls
256 bool wxRadioBox::Enable(bool enable)
257 {
258 if ( !wxControl::Enable(enable) )
259 return false;
260
261 for (unsigned int i = 0; i < m_noItems; i++)
262 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
263
264 return true;
265 }
266
267 bool wxRadioBox::Show(bool show)
268 {
269 // TODO: show/hide all children
270 return wxControl::Show(show);
271 }
272
273 // Show a specific button
274 bool wxRadioBox::Show(unsigned int n, bool show)
275 {
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:
279 //
280 // o Val1 o Val2 o Val3
281 // o Val4 o Val6
282 // o Val7 o Val8 o Val9
283 //
284 // In my case, this is a 'direction' box, and the Show(5,False) is
285 // coupled with an Enable(5,False)
286 //
287 if (!IsValid(n))
288 return false;
289
290 XtVaSetValues ((Widget) m_radioButtons[n],
291 XmNindicatorOn, (unsigned char) show,
292 NULL);
293
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,...)
297 // after this call!!
298 if (!show)
299 wxRadioBox::SetString (n, " ");
300
301 return true;
302 }
303
304 // For single selection items only
305 wxString wxRadioBox::GetStringSelection () const
306 {
307 int sel = GetSelection ();
308 if (sel != wxNOT_FOUND)
309 return this->GetString((unsigned int)sel);
310 else
311 return wxEmptyString;
312 }
313
314 bool wxRadioBox::SetStringSelection (const wxString& s)
315 {
316 int sel = FindString (s);
317 if (sel > -1)
318 {
319 SetSelection (sel);
320 return true;
321 }
322 else
323 return false;
324 }
325
326 void wxRadioBox::Command (wxCommandEvent & event)
327 {
328 SetSelection (event.GetInt());
329 ProcessCommand (event);
330 }
331
332 void wxRadioBox::ChangeFont(bool keepOriginalSize)
333 {
334 wxWindow::ChangeFont(keepOriginalSize);
335
336 for (unsigned int i = 0; i < m_noItems; i++)
337 {
338 WXWidget radioButton = m_radioButtons[i];
339
340 XtVaSetValues ((Widget) radioButton,
341 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay((Widget) GetTopWidget())),
342 NULL);
343 }
344 }
345
346 void wxRadioBox::ChangeBackgroundColour()
347 {
348 wxWindow::ChangeBackgroundColour();
349
350 wxColour colour = *wxBLACK;
351 int selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
352
353 for (unsigned int i = 0; i < m_noItems; i++)
354 {
355 WXWidget radioButton = m_radioButtons[i];
356
357 wxDoChangeBackgroundColour(radioButton, m_backgroundColour, true);
358
359 XtVaSetValues ((Widget) radioButton,
360 XmNselectColor, selectPixel,
361 NULL);
362 }
363 }
364
365 void wxRadioBox::ChangeForegroundColour()
366 {
367 wxWindow::ChangeForegroundColour();
368
369 for (unsigned int i = 0; i < m_noItems; i++)
370 {
371 WXWidget radioButton = m_radioButtons[i];
372
373 wxDoChangeForegroundColour(radioButton, m_foregroundColour);
374 }
375 }
376
377 void wxRadioBoxCallback (Widget w, XtPointer clientData,
378 XmToggleButtonCallbackStruct * cbs)
379 {
380 if (!cbs->set)
381 return;
382
383 wxRadioBox *item = (wxRadioBox *) clientData;
384 int sel = -1;
385 unsigned int i;
386 const wxWidgetArray& buttons = item->GetRadioButtons();
387 for (i = 0; i < item->GetCount(); i++)
388 if (((Widget)buttons[i]) == w)
389 sel = (int)i;
390 item->SetSel(sel);
391
392 if (item->InSetValue())
393 return;
394
395 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
396 event.SetInt(sel);
397 event.SetString(item->GetStringSelection());
398 event.SetEventObject(item);
399 item->ProcessCommand (event);
400 }
401
402 #endif // wxUSE_RADIOBOX