]> git.saurik.com Git - wxWidgets.git/blob - src/motif/radiobox.cpp
ignore clicks on a toolbar but outside any button
[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/Form.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 wxRadioBox::wxRadioBox()
48 {
49 m_selectedButton = -1;
50 m_noItems = 0;
51 m_noRowsOrCols = 0;
52 m_majorDim = 0 ;
53
54 m_formWidget = (WXWidget) 0;
55 m_frameWidget = (WXWidget) 0;
56 m_labelWidget = (WXWidget) 0;
57 m_radioButtons = (WXWidget*) NULL;
58 m_radioButtonLabels = (wxString*) NULL;
59 }
60
61 bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title,
62 const wxPoint& pos, const wxSize& size,
63 int n, const wxString choices[],
64 int majorDim, long style,
65 const wxValidator& val, const wxString& name)
66 {
67 m_selectedButton = -1;
68 m_noItems = n;
69 m_formWidget = (WXWidget) 0;
70 m_frameWidget = (WXWidget) 0;
71 m_labelWidget = (WXWidget) 0;
72 m_radioButtons = (WXWidget*) NULL;
73 m_radioButtonLabels = (wxString*) NULL;
74 m_backgroundColour = parent->GetBackgroundColour();
75 m_foregroundColour = parent->GetForegroundColour();
76 m_font = parent->GetFont();
77
78 SetName(name);
79 SetValidator(val);
80
81 parent->AddChild(this);
82
83 m_windowStyle = (long&)style;
84
85 if (id == -1)
86 m_windowId = NewControlId();
87 else
88 m_windowId = id;
89
90 m_noRowsOrCols = majorDim;
91
92 if (majorDim==0)
93 m_majorDim = n ;
94 else
95 m_majorDim = majorDim ;
96
97 Widget parentWidget = (Widget) parent->GetClientWidget();
98
99 wxString label1(wxStripMenuCodes(title));
100
101 Widget formWidget = XtVaCreateManagedWidget (name.c_str(),
102 xmFormWidgetClass, parentWidget,
103 XmNmarginHeight, 0,
104 XmNmarginWidth, 0,
105 NULL);
106
107 m_formWidget = (WXWidget) formWidget;
108
109 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget));
110 if (label1 != "")
111 {
112 wxXmString text(label1);
113 (void)XtVaCreateManagedWidget(label1.c_str(),
114 #if wxUSE_GADGETS
115 style & wxCOLOURED ? xmLabelWidgetClass
116 : xmLabelGadgetClass,
117 formWidget,
118 #else
119 xmLabelWidgetClass, formWidget,
120 #endif
121 XmNfontList, fontList,
122 XmNlabelString, text(),
123 NULL);
124 }
125
126 Widget frameWidget = XtVaCreateManagedWidget ("frame",
127 xmFrameWidgetClass, formWidget,
128 XmNshadowType, XmSHADOW_IN,
129 // XmNmarginHeight, 0,
130 // XmNmarginWidth, 0,
131 NULL);
132
133 m_frameWidget = (WXWidget) frameWidget;
134
135 Arg args[3];
136
137 m_majorDim = (n + m_majorDim - 1) / m_majorDim;
138
139 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
140 XmHORIZONTAL : XmVERTICAL));
141 XtSetArg (args[1], XmNnumColumns, m_majorDim);
142
143 Widget radioBoxWidget = XmCreateRadioBox (frameWidget, "radioBoxWidget", args, 2);
144 m_mainWidget = (WXWidget) radioBoxWidget;
145
146
147 if (m_labelWidget)
148 XtVaSetValues ((Widget) m_labelWidget,
149 XmNtopAttachment, XmATTACH_FORM,
150 XmNleftAttachment, XmATTACH_FORM,
151 XmNalignment, XmALIGNMENT_BEGINNING,
152 NULL);
153
154 XtVaSetValues (radioBoxWidget,
155 XmNtopAttachment, m_labelWidget ? XmATTACH_WIDGET : XmATTACH_FORM,
156 XmNtopWidget, m_labelWidget ? (Widget) m_labelWidget : formWidget,
157 XmNbottomAttachment, XmATTACH_FORM,
158 XmNleftAttachment, XmATTACH_FORM,
159 XmNrightAttachment, XmATTACH_FORM,
160 NULL);
161
162 // if (style & wxFLAT)
163 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
164
165 m_radioButtons = new WXWidget[n];
166 m_radioButtonLabels = new wxString[n];
167 int i;
168 for (i = 0; i < n; i++)
169 {
170 wxString str(wxStripMenuCodes(choices[i]));
171 m_radioButtonLabels[i] = str;
172 m_radioButtons[i] = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) str,
173 #if wxUSE_GADGETS
174 xmToggleButtonGadgetClass, radioBoxWidget,
175 #else
176 xmToggleButtonWidgetClass, radioBoxWidget,
177 #endif
178 XmNfontList, fontList,
179 NULL);
180 XtAddCallback ((Widget) m_radioButtons[i], XmNvalueChangedCallback, (XtCallbackProc) wxRadioBoxCallback,
181 (XtPointer) this);
182 }
183 SetSelection (0);
184
185 m_font = parent->GetFont();
186 ChangeFont(FALSE);
187
188 // XtManageChild((Widget) m_formWidget);
189 XtManageChild (radioBoxWidget);
190
191 SetCanAddEventHandler(TRUE);
192 AttachWidget (parent, m_mainWidget, m_formWidget, pos.x, pos.y, size.x, size.y);
193
194 ChangeBackgroundColour();
195
196 return TRUE;
197 }
198
199
200 wxRadioBox::~wxRadioBox()
201 {
202 delete[] m_radioButtonLabels;
203 delete[] m_radioButtons;
204
205 DetachWidget(m_formWidget);
206 DetachWidget(m_mainWidget);
207
208 if (m_labelWidget)
209 XtDestroyWidget((Widget) m_labelWidget);
210 XtDestroyWidget((Widget) m_mainWidget);
211 XtDestroyWidget((Widget) m_formWidget);
212
213 m_mainWidget = (WXWidget) 0;
214 m_formWidget = (WXWidget) 0;
215 m_labelWidget = (WXWidget) 0;
216 }
217
218 wxString wxRadioBox::GetLabel(int item) const
219 {
220 if (item < 0 || item >= m_noItems)
221 return wxEmptyString;
222
223 Widget widget = (Widget) m_radioButtons[item];
224 XmString text;
225 char *s;
226 XtVaGetValues (widget,
227 XmNlabelString, &text,
228 NULL);
229
230 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
231 {
232 // Should we free 'text'???
233 XmStringFree(text);
234 wxString str(s);
235 XtFree (s);
236 return str;
237 }
238 else
239 {
240 XmStringFree(text);
241 return wxEmptyString;
242 }
243 }
244
245 void wxRadioBox::SetLabel(int item, const wxString& label)
246 {
247 if (item < 0 || item >= m_noItems)
248 return;
249
250 Widget widget = (Widget) m_radioButtons[item];
251 if (label != "")
252 {
253 wxString label1(wxStripMenuCodes(label));
254 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
255 XtVaSetValues (widget,
256 XmNlabelString, text,
257 XmNlabelType, XmSTRING,
258 NULL);
259 XmStringFree (text);
260 }
261 }
262
263 int wxRadioBox::FindString(const wxString& s) const
264 {
265 int i;
266 for (i = 0; i < m_noItems; i++)
267 if (s == m_radioButtonLabels[i])
268 return i;
269 return -1;
270 }
271
272 void wxRadioBox::SetSelection(int n)
273 {
274 if ((n < 0) || (n >= m_noItems))
275 return;
276
277 m_selectedButton = n;
278
279 m_inSetValue = TRUE;
280
281 XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE);
282
283 int i;
284 for (i = 0; i < m_noItems; i++)
285 if (i != n)
286 XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE);
287
288 m_inSetValue = FALSE;
289 }
290
291 // Get single selection, for single choice list items
292 int wxRadioBox::GetSelection() const
293 {
294 return m_selectedButton;
295 }
296
297 // Find string for position
298 wxString wxRadioBox::GetString(int n) const
299 {
300 if ((n < 0) || (n >= m_noItems))
301 return wxEmptyString;
302 return m_radioButtonLabels[n];
303 }
304
305 void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags)
306 {
307 bool managed = XtIsManaged((Widget) m_formWidget);
308
309 if (managed)
310 XtUnmanageChild ((Widget) m_formWidget);
311
312 int xx = x; int yy = y;
313 AdjustForParentClientOrigin(xx, yy, sizeFlags);
314
315 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
316 XtVaSetValues ((Widget) m_formWidget, XmNleftAttachment, XmATTACH_SELF,
317 XmNx, xx, NULL);
318 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
319 XtVaSetValues ((Widget) m_formWidget, XmNtopAttachment, XmATTACH_SELF,
320 XmNy, yy, NULL);
321
322 // Must set the actual RadioBox to be desired size MINUS label size
323 Dimension labelWidth = 0, labelHeight = 0, actualWidth = 0, actualHeight = 0;
324
325 if (m_labelWidget)
326 XtVaGetValues ((Widget) m_labelWidget, XmNwidth, &labelWidth, XmNheight, &labelHeight, NULL);
327
328 actualWidth = width;
329 actualHeight = height - labelHeight;
330
331 if (width > -1)
332 {
333 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, actualWidth, NULL);
334 }
335 if (height > -1)
336 {
337 XtVaSetValues ((Widget) m_mainWidget, XmNheight, actualHeight, NULL);
338 }
339 if (managed)
340 XtManageChild ((Widget) m_formWidget);
341 }
342
343 // Enable a specific button
344 void wxRadioBox::Enable(int n, bool enable)
345 {
346 if ((n < 0) || (n >= m_noItems))
347 return;
348
349 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
350 }
351
352 // Enable all controls
353 bool wxRadioBox::Enable(bool enable)
354 {
355 if ( !wxControl::Enable(enable) )
356 return FALSE;
357
358 int i;
359 for (i = 0; i < m_noItems; i++)
360 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
361
362 return TRUE;
363 }
364
365 bool wxRadioBox::Show(bool show)
366 {
367 // TODO: show/hide all children
368 return wxControl::Show(show);
369 }
370
371 // Show a specific button
372 void wxRadioBox::Show(int n, bool show)
373 {
374 // This method isn't complete, and we try do do our best...
375 // It's main purpose isn't for allowing Show/Unshow dynamically,
376 // but rather to provide a way to design wxRadioBox such:
377 //
378 // o Val1 o Val2 o Val3
379 // o Val4 o Val6
380 // o Val7 o Val8 o Val9
381 //
382 // In my case, this is a 'direction' box, and the Show(5,False) is
383 // coupled with an Enable(5,False)
384 //
385 if ((n < 0) || (n >= m_noItems))
386 return;
387
388 XtVaSetValues ((Widget) m_radioButtons[n],
389 XmNindicatorOn, (unsigned char) show,
390 NULL);
391
392 // Please note that this is all we can do: removing the label
393 // if switching to unshow state. However, when switching
394 // to the on state, it's the prog. resp. to call SetLabel(item,...)
395 // after this call!!
396 if (!show)
397 wxRadioBox::SetLabel (n, " ");
398 }
399
400 // For single selection items only
401 wxString wxRadioBox::GetStringSelection () const
402 {
403 int sel = GetSelection ();
404 if (sel > -1)
405 return this->GetString (sel);
406 else
407 return wxString("");
408 }
409
410 bool wxRadioBox::SetStringSelection (const wxString& s)
411 {
412 int sel = FindString (s);
413 if (sel > -1)
414 {
415 SetSelection (sel);
416 return TRUE;
417 }
418 else
419 return FALSE;
420 }
421
422 void wxRadioBox::Command (wxCommandEvent & event)
423 {
424 SetSelection (event.m_commandInt);
425 ProcessCommand (event);
426 }
427
428 void wxRadioBox::ChangeFont(bool keepOriginalSize)
429 {
430 wxWindow::ChangeFont(keepOriginalSize);
431
432 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) GetTopWidget()));
433
434 int i;
435 for (i = 0; i < m_noItems; i++)
436 {
437 WXWidget radioButton = m_radioButtons[i];
438
439 XtVaSetValues ((Widget) radioButton,
440 XmNfontList, fontList,
441 XmNtopAttachment, XmATTACH_FORM,
442 NULL);
443 }
444 }
445
446 void wxRadioBox::ChangeBackgroundColour()
447 {
448 wxWindow::ChangeBackgroundColour();
449
450 DoChangeBackgroundColour((Widget) m_frameWidget, m_backgroundColour);
451
452 int selectPixel = wxBLACK->AllocColour(wxGetDisplay());
453
454 int i;
455 for (i = 0; i < m_noItems; i++)
456 {
457 WXWidget radioButton = m_radioButtons[i];
458
459 DoChangeBackgroundColour(radioButton, m_backgroundColour, TRUE);
460
461 XtVaSetValues ((Widget) radioButton,
462 XmNselectColor, selectPixel,
463 NULL);
464 }
465 }
466
467 void wxRadioBox::ChangeForegroundColour()
468 {
469 wxWindow::ChangeForegroundColour();
470
471 int i;
472 for (i = 0; i < m_noItems; i++)
473 {
474 WXWidget radioButton = m_radioButtons[i];
475
476 DoChangeForegroundColour(radioButton, m_foregroundColour);
477 }
478 }
479
480 void wxRadioBoxCallback (Widget w, XtPointer clientData,
481 XmToggleButtonCallbackStruct * cbs)
482 {
483 if (!cbs->set)
484 return;
485
486 wxRadioBox *item = (wxRadioBox *) clientData;
487 int sel = -1;
488 int i;
489 for (i = 0; i < item->GetCount(); i++)
490 if (item->GetRadioButtons() && ((Widget) (item->GetRadioButtons()[i]) == w))
491 sel = i;
492 item->SetSel(sel);
493
494 if (item->InSetValue())
495 return;
496
497 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
498 event.m_commandInt = sel;
499 event.SetEventObject(item);
500 item->ProcessCommand (event);
501 }
502