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