]> git.saurik.com Git - wxWidgets.git/blob - src/motif/radiobox.cpp
Better fix for modal/modeless wizards.
[wxWidgets.git] / src / motif / radiobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 int wxRadioBox::FindString(const wxString& s) const
196 {
197 int i;
198 for (i = 0; i < m_noItems; i++)
199 if (s == m_radioButtonLabels[i])
200 return i;
201 return wxNOT_FOUND;
202 }
203
204 void wxRadioBox::SetSelection(int n)
205 {
206 if (!IsValid(n))
207 return;
208
209 m_selectedButton = n;
210
211 m_inSetValue = true;
212
213 XmToggleButtonSetState ((Widget) m_radioButtons[n], True, False);
214
215 int i;
216 for (i = 0; i < m_noItems; i++)
217 if (i != n)
218 XmToggleButtonSetState ((Widget) m_radioButtons[i], False, False);
219
220 m_inSetValue = false;
221 }
222
223 // Get single selection, for single choice list items
224 int wxRadioBox::GetSelection() const
225 {
226 return m_selectedButton;
227 }
228
229 // Find string for position
230 wxString wxRadioBox::GetString(int n) const
231 {
232 if (!IsValid(n))
233 return wxEmptyString;
234 return m_radioButtonLabels[n];
235 }
236
237 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
238 {
239 bool managed = XtIsManaged((Widget) m_mainWidget);
240
241 if (managed)
242 XtUnmanageChild ((Widget) m_mainWidget);
243
244 int xx = x; int yy = y;
245 AdjustForParentClientOrigin(xx, yy, sizeFlags);
246
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);
251
252 if (width > 0)
253 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL);
254 if (height > 0)
255 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL);
256
257 if (managed)
258 XtManageChild ((Widget) m_mainWidget);
259 }
260
261 // Enable a specific button
262 bool wxRadioBox::Enable(int n, bool enable)
263 {
264 if (!IsValid(n))
265 return false;
266
267 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
268 return true;
269 }
270
271 // Enable all controls
272 bool wxRadioBox::Enable(bool enable)
273 {
274 if ( !wxControl::Enable(enable) )
275 return false;
276
277 int i;
278 for (i = 0; i < m_noItems; i++)
279 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
280
281 return true;
282 }
283
284 bool wxRadioBox::Show(bool show)
285 {
286 // TODO: show/hide all children
287 return wxControl::Show(show);
288 }
289
290 // Show a specific button
291 bool wxRadioBox::Show(int n, bool show)
292 {
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:
296 //
297 // o Val1 o Val2 o Val3
298 // o Val4 o Val6
299 // o Val7 o Val8 o Val9
300 //
301 // In my case, this is a 'direction' box, and the Show(5,False) is
302 // coupled with an Enable(5,False)
303 //
304 if (!IsValid(n))
305 return false;
306
307 XtVaSetValues ((Widget) m_radioButtons[n],
308 XmNindicatorOn, (unsigned char) show,
309 NULL);
310
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,...)
314 // after this call!!
315 if (!show)
316 wxRadioBox::SetString (n, " ");
317
318 return true;
319 }
320
321 // For single selection items only
322 wxString wxRadioBox::GetStringSelection () const
323 {
324 int sel = GetSelection ();
325 if (sel > -1)
326 return this->GetString (sel);
327 else
328 return wxEmptyString;
329 }
330
331 bool wxRadioBox::SetStringSelection (const wxString& s)
332 {
333 int sel = FindString (s);
334 if (sel > -1)
335 {
336 SetSelection (sel);
337 return true;
338 }
339 else
340 return false;
341 }
342
343 void wxRadioBox::Command (wxCommandEvent & event)
344 {
345 SetSelection (event.GetInt());
346 ProcessCommand (event);
347 }
348
349 void wxRadioBox::ChangeFont(bool keepOriginalSize)
350 {
351 wxWindow::ChangeFont(keepOriginalSize);
352
353 int i;
354 for (i = 0; i < m_noItems; i++)
355 {
356 WXWidget radioButton = m_radioButtons[i];
357
358 XtVaSetValues ((Widget) radioButton,
359 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay((Widget) GetTopWidget())),
360 NULL);
361 }
362 }
363
364 void wxRadioBox::ChangeBackgroundColour()
365 {
366 wxWindow::ChangeBackgroundColour();
367
368 int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget));
369
370 int i;
371 for (i = 0; i < m_noItems; i++)
372 {
373 WXWidget radioButton = m_radioButtons[i];
374
375 wxDoChangeBackgroundColour(radioButton, m_backgroundColour, true);
376
377 XtVaSetValues ((Widget) radioButton,
378 XmNselectColor, selectPixel,
379 NULL);
380 }
381 }
382
383 void wxRadioBox::ChangeForegroundColour()
384 {
385 wxWindow::ChangeForegroundColour();
386
387 int i;
388 for (i = 0; i < m_noItems; i++)
389 {
390 WXWidget radioButton = m_radioButtons[i];
391
392 wxDoChangeForegroundColour(radioButton, m_foregroundColour);
393 }
394 }
395
396 static int CalcOtherDim( int items, int dim )
397 {
398 return items / dim + ( items % dim ? 1 : 0 );
399 }
400
401 int wxRadioBox::GetRowCount() const
402 {
403 return m_windowStyle & wxRA_SPECIFY_ROWS ? m_noRowsOrCols
404 : CalcOtherDim( GetCount(), m_noRowsOrCols );
405 }
406
407 int wxRadioBox::GetColumnCount() const
408 {
409 return m_windowStyle & wxRA_SPECIFY_COLS ? m_noRowsOrCols
410 : CalcOtherDim( GetCount(), m_noRowsOrCols );
411 }
412
413 void wxRadioBoxCallback (Widget w, XtPointer clientData,
414 XmToggleButtonCallbackStruct * cbs)
415 {
416 if (!cbs->set)
417 return;
418
419 wxRadioBox *item = (wxRadioBox *) clientData;
420 int sel = -1;
421 int i;
422 const wxWidgetArray& buttons = item->GetRadioButtons();
423 for (i = 0; i < item->GetCount(); i++)
424 if (((Widget)buttons[i]) == w)
425 sel = i;
426 item->SetSel(sel);
427
428 if (item->InSetValue())
429 return;
430
431 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
432 event.SetInt(sel);
433 event.SetString(item->GetStringSelection());
434 event.SetEventObject(item);
435 item->ProcessCommand (event);
436 }
437