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