]> git.saurik.com Git - wxWidgets.git/blob - src/motif/radiobox.cpp
fix for wx-config --inplace --rezflags output in wxCocoa (and reorganization of 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 #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 XtSetArg (args[2], XmNadjustLast, False);
119
120 Widget radioBoxWidget =
121 XmCreateRadioBox ((Widget)m_mainWidget, "radioBoxWidget", args, 3);
122
123 m_radioButtons.reserve(n);
124 m_radioButtonLabels.reserve(n);
125
126 int i;
127 for (i = 0; i < n; i++)
128 {
129 wxString str(wxStripMenuCodes(choices[i]));
130 m_radioButtonLabels.push_back(str);
131 Widget radioItem = XtVaCreateManagedWidget (wxConstCast(str.c_str(), char),
132 #if wxUSE_GADGETS
133 xmToggleButtonGadgetClass, radioBoxWidget,
134 #else
135 xmToggleButtonWidgetClass, radioBoxWidget,
136 #endif
137 wxFont::GetFontTag(), fontType,
138 NULL);
139 m_radioButtons.push_back((WXWidget)radioItem);
140 XtAddCallback (radioItem, XmNvalueChangedCallback,
141 (XtCallbackProc) wxRadioBoxCallback,
142 (XtPointer) this);
143 }
144
145 ChangeFont(FALSE);
146
147 SetSelection (0);
148
149 XtRealizeWidget((Widget)m_mainWidget);
150 XtManageChild (radioBoxWidget);
151 XtManageChild ((Widget)m_mainWidget);
152
153 AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y);
154
155 ChangeBackgroundColour();
156
157 return TRUE;
158 }
159
160
161 wxRadioBox::~wxRadioBox()
162 {
163 DetachWidget(m_mainWidget);
164 XtDestroyWidget((Widget) m_mainWidget);
165
166 m_mainWidget = (WXWidget) 0;
167 }
168
169 void wxRadioBox::SetString(int item, const wxString& label)
170 {
171 if (item < 0 || item >= m_noItems)
172 return;
173
174 Widget widget = (Widget) m_radioButtons[item];
175 if (label != "")
176 {
177 wxString label1(wxStripMenuCodes(label));
178 wxXmString text( label1 );
179 m_radioButtonLabels[item] = label1;
180 XtVaSetValues (widget,
181 XmNlabelString, text(),
182 XmNlabelType, XmSTRING,
183 NULL);
184 }
185 }
186
187 int wxRadioBox::FindString(const wxString& s) const
188 {
189 int i;
190 for (i = 0; i < m_noItems; i++)
191 if (s == m_radioButtonLabels[i])
192 return i;
193 return -1;
194 }
195
196 void wxRadioBox::SetSelection(int n)
197 {
198 if ((n < 0) || (n >= m_noItems))
199 return;
200
201 m_selectedButton = n;
202
203 m_inSetValue = TRUE;
204
205 XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE);
206
207 int i;
208 for (i = 0; i < m_noItems; i++)
209 if (i != n)
210 XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE);
211
212 m_inSetValue = FALSE;
213 }
214
215 // Get single selection, for single choice list items
216 int wxRadioBox::GetSelection() const
217 {
218 return m_selectedButton;
219 }
220
221 // Find string for position
222 wxString wxRadioBox::GetString(int n) const
223 {
224 if ((n < 0) || (n >= m_noItems))
225 return wxEmptyString;
226 return m_radioButtonLabels[n];
227 }
228
229 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
230 {
231 bool managed = XtIsManaged((Widget) m_mainWidget);
232
233 if (managed)
234 XtUnmanageChild ((Widget) m_mainWidget);
235
236 int xx = x; int yy = y;
237 AdjustForParentClientOrigin(xx, yy, sizeFlags);
238
239 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
240 XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL);
241 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
242 XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL);
243
244 if (width > 0)
245 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL);
246 if (height > 0)
247 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL);
248
249 if (managed)
250 XtManageChild ((Widget) m_mainWidget);
251 }
252
253 // Enable a specific button
254 void wxRadioBox::Enable(int n, bool enable)
255 {
256 if ((n < 0) || (n >= m_noItems))
257 return;
258
259 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
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 void 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 ((n < 0) || (n >= m_noItems))
296 return;
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
310 // For single selection items only
311 wxString wxRadioBox::GetStringSelection () const
312 {
313 int sel = GetSelection ();
314 if (sel > -1)
315 return this->GetString (sel);
316 else
317 return wxString("");
318 }
319
320 bool wxRadioBox::SetStringSelection (const wxString& s)
321 {
322 int sel = FindString (s);
323 if (sel > -1)
324 {
325 SetSelection (sel);
326 return TRUE;
327 }
328 else
329 return FALSE;
330 }
331
332 void wxRadioBox::Command (wxCommandEvent & event)
333 {
334 SetSelection (event.m_commandInt);
335 ProcessCommand (event);
336 }
337
338 void wxRadioBox::ChangeFont(bool keepOriginalSize)
339 {
340 wxWindow::ChangeFont(keepOriginalSize);
341
342 WXFontType fontType =
343 m_font.GetFontType(XtDisplay((Widget) GetTopWidget()));
344
345 int i;
346 for (i = 0; i < m_noItems; i++)
347 {
348 WXWidget radioButton = m_radioButtons[i];
349
350 XtVaSetValues ((Widget) radioButton,
351 wxFont::GetFontTag(), fontType,
352 NULL);
353 }
354 }
355
356 void wxRadioBox::ChangeBackgroundColour()
357 {
358 wxWindow::ChangeBackgroundColour();
359
360 int selectPixel = wxBLACK->AllocColour(XtDisplay((Widget)m_mainWidget));
361
362 int i;
363 for (i = 0; i < m_noItems; i++)
364 {
365 WXWidget radioButton = m_radioButtons[i];
366
367 wxDoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE);
368
369 XtVaSetValues ((Widget) radioButton,
370 XmNselectColor, selectPixel,
371 NULL);
372 }
373 }
374
375 void wxRadioBox::ChangeForegroundColour()
376 {
377 wxWindow::ChangeForegroundColour();
378
379 int i;
380 for (i = 0; i < m_noItems; i++)
381 {
382 WXWidget radioButton = m_radioButtons[i];
383
384 wxDoChangeForegroundColour(radioButton, m_foregroundColour);
385 }
386 }
387
388 static int CalcOtherDim( int items, int dim )
389 {
390 return items / dim + ( items % dim ? 1 : 0 );
391 }
392
393 int wxRadioBox::GetRowCount() const
394 {
395 return m_windowStyle & wxRA_SPECIFY_ROWS ? m_noRowsOrCols
396 : CalcOtherDim( GetCount(), m_noRowsOrCols );
397 }
398
399 int wxRadioBox::GetColumnCount() const
400 {
401 return m_windowStyle & wxRA_SPECIFY_COLS ? m_noRowsOrCols
402 : CalcOtherDim( GetCount(), m_noRowsOrCols );
403 }
404
405 void wxRadioBoxCallback (Widget w, XtPointer clientData,
406 XmToggleButtonCallbackStruct * cbs)
407 {
408 if (!cbs->set)
409 return;
410
411 wxRadioBox *item = (wxRadioBox *) clientData;
412 int sel = -1;
413 int i;
414 const wxWidgetArray& buttons = item->GetRadioButtons();
415 for (i = 0; i < item->GetCount(); i++)
416 if (((Widget)buttons[i]) == w)
417 sel = i;
418 item->SetSel(sel);
419
420 if (item->InSetValue())
421 return;
422
423 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
424 event.SetInt(sel);
425 event.SetString(item->GetStringSelection());
426 event.SetEventObject(item);
427 item->ProcessCommand (event);
428 }
429