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