]> git.saurik.com Git - wxWidgets.git/blob - src/motif/radiobox.cpp
avoid hidden GetImageList
[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 #ifdef __VMS
16 #define XtDisplay XTDISPLAY
17 #endif
18
19 #include "wx/defs.h"
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 m_majorDim = 0 ;
52 }
53
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)
59 {
60 if( !CreateControl( parent, id, pos, size, style, val, name ) )
61 return false;
62
63 m_noItems = n;
64 m_noRowsOrCols = majorDim;
65
66 if (majorDim==0)
67 m_majorDim = n ;
68 else
69 m_majorDim = majorDim ;
70
71 Widget parentWidget = (Widget) parent->GetClientWidget();
72 Display* dpy = XtDisplay(parentWidget);
73
74 m_mainWidget = XtVaCreateWidget ("radioboxframe",
75 xmFrameWidgetClass, parentWidget,
76 XmNresizeHeight, True,
77 XmNresizeWidth, True,
78 NULL);
79
80 wxString label1(wxStripMenuCodes(title));
81
82 if (!label1.empty())
83 {
84 wxXmString text(label1);
85 m_labelWidget = (WXWidget)
86 XtVaCreateManagedWidget( label1.c_str(),
87 #if wxUSE_GADGETS
88 style & wxCOLOURED ? xmLabelWidgetClass
89 : xmLabelGadgetClass,
90 (Widget)m_mainWidget,
91 #else
92 xmLabelWidgetClass, (Widget)m_mainWidget,
93 #endif
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,
101 #else
102 XmNchildType, XmFRAME_TITLE_CHILD,
103 #endif
104 XmNchildVerticalAlignment, XmALIGNMENT_CENTER,
105 NULL);
106 }
107
108 Arg args[3];
109
110 m_majorDim = (n + m_majorDim - 1) / m_majorDim;
111
112 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
113 XmHORIZONTAL : XmVERTICAL));
114 XtSetArg (args[1], XmNnumColumns, m_majorDim);
115 XtSetArg (args[2], XmNadjustLast, False);
116
117 Widget radioBoxWidget =
118 XmCreateRadioBox ((Widget)m_mainWidget, wxMOTIF_STR("radioBoxWidget"), args, 3);
119
120 m_radioButtons.reserve(n);
121 m_radioButtonLabels.reserve(n);
122
123 int i;
124 for (i = 0; i < n; i++)
125 {
126 wxString str(wxStripMenuCodes(choices[i]));
127 m_radioButtonLabels.push_back(str);
128 Widget radioItem = XtVaCreateManagedWidget (
129 wxConstCast(str.c_str(), char),
130 #if wxUSE_GADGETS
131 xmToggleButtonGadgetClass, radioBoxWidget,
132 #else
133 xmToggleButtonWidgetClass, radioBoxWidget,
134 #endif
135 wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
136 NULL);
137 m_radioButtons.push_back((WXWidget)radioItem);
138 XtAddCallback (radioItem, XmNvalueChangedCallback,
139 (XtCallbackProc) wxRadioBoxCallback,
140 (XtPointer) this);
141 }
142
143 ChangeFont(false);
144
145 SetSelection (0);
146
147 XtRealizeWidget((Widget)m_mainWidget);
148 XtManageChild (radioBoxWidget);
149 XtManageChild ((Widget)m_mainWidget);
150
151 AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y);
152
153 ChangeBackgroundColour();
154
155 return true;
156 }
157
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)
163 {
164 wxCArrayString chs(choices);
165 return Create(parent, id, title, pos, size, chs.GetCount(),
166 chs.GetStrings(), majorDim, style, val, name);
167 }
168
169 wxRadioBox::~wxRadioBox()
170 {
171 DetachWidget(m_mainWidget);
172 XtDestroyWidget((Widget) m_mainWidget);
173
174 m_mainWidget = (WXWidget) 0;
175 }
176
177 void wxRadioBox::SetString(int item, const wxString& label)
178 {
179 if (!IsValid(item))
180 return;
181
182 Widget widget = (Widget) m_radioButtons[item];
183 if (!label.empty())
184 {
185 wxString label1(wxStripMenuCodes(label));
186 wxXmString text( label1 );
187 m_radioButtonLabels[item] = label1;
188 XtVaSetValues (widget,
189 XmNlabelString, text(),
190 XmNlabelType, XmSTRING,
191 NULL);
192 }
193 }
194
195 void wxRadioBox::SetSelection(int n)
196 {
197 if (!IsValid(n))
198 return;
199
200 m_selectedButton = n;
201
202 m_inSetValue = true;
203
204 XmToggleButtonSetState ((Widget) m_radioButtons[n], True, False);
205
206 int i;
207 for (i = 0; i < m_noItems; i++)
208 if (i != n)
209 XmToggleButtonSetState ((Widget) m_radioButtons[i], False, False);
210
211 m_inSetValue = false;
212 }
213
214 // Get single selection, for single choice list items
215 int wxRadioBox::GetSelection() const
216 {
217 return m_selectedButton;
218 }
219
220 // Find string for position
221 wxString wxRadioBox::GetString(int n) const
222 {
223 if (!IsValid(n))
224 return wxEmptyString;
225 return m_radioButtonLabels[n];
226 }
227
228 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
229 {
230 bool managed = XtIsManaged((Widget) m_mainWidget);
231
232 if (managed)
233 XtUnmanageChild ((Widget) m_mainWidget);
234
235 int xx = x; int yy = y;
236 AdjustForParentClientOrigin(xx, yy, sizeFlags);
237
238 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
239 XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL);
240 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
241 XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL);
242
243 if (width > 0)
244 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL);
245 if (height > 0)
246 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL);
247
248 if (managed)
249 XtManageChild ((Widget) m_mainWidget);
250 }
251
252 // Enable a specific button
253 bool wxRadioBox::Enable(int n, bool enable)
254 {
255 if (!IsValid(n))
256 return false;
257
258 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
259 return true;
260 }
261
262 // Enable all controls
263 bool wxRadioBox::Enable(bool enable)
264 {
265 if ( !wxControl::Enable(enable) )
266 return false;
267
268 int i;
269 for (i = 0; i < m_noItems; i++)
270 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
271
272 return true;
273 }
274
275 bool wxRadioBox::Show(bool show)
276 {
277 // TODO: show/hide all children
278 return wxControl::Show(show);
279 }
280
281 // Show a specific button
282 bool wxRadioBox::Show(int n, bool show)
283 {
284 // This method isn't complete, and we try do do our best...
285 // It's main purpose isn't for allowing Show/Unshow dynamically,
286 // but rather to provide a way to design wxRadioBox such:
287 //
288 // o Val1 o Val2 o Val3
289 // o Val4 o Val6
290 // o Val7 o Val8 o Val9
291 //
292 // In my case, this is a 'direction' box, and the Show(5,False) is
293 // coupled with an Enable(5,False)
294 //
295 if (!IsValid(n))
296 return false;
297
298 XtVaSetValues ((Widget) m_radioButtons[n],
299 XmNindicatorOn, (unsigned char) show,
300 NULL);
301
302 // Please note that this is all we can do: removing the label
303 // if switching to unshow state. However, when switching
304 // to the on state, it's the prog. resp. to call SetString(item,...)
305 // after this call!!
306 if (!show)
307 wxRadioBox::SetString (n, " ");
308
309 return true;
310 }
311
312 // For single selection items only
313 wxString wxRadioBox::GetStringSelection () const
314 {
315 int sel = GetSelection ();
316 if (sel > -1)
317 return this->GetString (sel);
318 else
319 return wxEmptyString;
320 }
321
322 bool wxRadioBox::SetStringSelection (const wxString& s)
323 {
324 int sel = FindString (s);
325 if (sel > -1)
326 {
327 SetSelection (sel);
328 return true;
329 }
330 else
331 return false;
332 }
333
334 void wxRadioBox::Command (wxCommandEvent & event)
335 {
336 SetSelection (event.GetInt());
337 ProcessCommand (event);
338 }
339
340 void wxRadioBox::ChangeFont(bool keepOriginalSize)
341 {
342 wxWindow::ChangeFont(keepOriginalSize);
343
344 int i;
345 for (i = 0; i < m_noItems; i++)
346 {
347 WXWidget radioButton = m_radioButtons[i];
348
349 XtVaSetValues ((Widget) radioButton,
350 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay((Widget) GetTopWidget())),
351 NULL);
352 }
353 }
354
355 void wxRadioBox::ChangeBackgroundColour()
356 {
357 wxWindow::ChangeBackgroundColour();
358
359 int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget));
360
361 int i;
362 for (i = 0; i < m_noItems; i++)
363 {
364 WXWidget radioButton = m_radioButtons[i];
365
366 wxDoChangeBackgroundColour(radioButton, m_backgroundColour, true);
367
368 XtVaSetValues ((Widget) radioButton,
369 XmNselectColor, selectPixel,
370 NULL);
371 }
372 }
373
374 void wxRadioBox::ChangeForegroundColour()
375 {
376 wxWindow::ChangeForegroundColour();
377
378 int i;
379 for (i = 0; i < m_noItems; i++)
380 {
381 WXWidget radioButton = m_radioButtons[i];
382
383 wxDoChangeForegroundColour(radioButton, m_foregroundColour);
384 }
385 }
386
387 static int CalcOtherDim( int items, int dim )
388 {
389 return items / dim + ( items % dim ? 1 : 0 );
390 }
391
392 int wxRadioBox::GetRowCount() const
393 {
394 return m_windowStyle & wxRA_SPECIFY_ROWS ? m_noRowsOrCols
395 : CalcOtherDim( GetCount(), m_noRowsOrCols );
396 }
397
398 int wxRadioBox::GetColumnCount() const
399 {
400 return m_windowStyle & wxRA_SPECIFY_COLS ? m_noRowsOrCols
401 : CalcOtherDim( GetCount(), m_noRowsOrCols );
402 }
403
404 void wxRadioBoxCallback (Widget w, XtPointer clientData,
405 XmToggleButtonCallbackStruct * cbs)
406 {
407 if (!cbs->set)
408 return;
409
410 wxRadioBox *item = (wxRadioBox *) clientData;
411 int sel = -1;
412 int i;
413 const wxWidgetArray& buttons = item->GetRadioButtons();
414 for (i = 0; i < item->GetCount(); i++)
415 if (((Widget)buttons[i]) == w)
416 sel = i;
417 item->SetSel(sel);
418
419 if (item->InSetValue())
420 return;
421
422 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
423 event.SetInt(sel);
424 event.SetString(item->GetStringSelection());
425 event.SetEventObject(item);
426 item->ProcessCommand (event);
427 }