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