1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "radiobox.h"
16 #include "wx/radiobox.h"
20 #include <Xm/LabelG.h>
21 #include <Xm/ToggleB.h>
22 #include <Xm/ToggleBG.h>
23 #include <Xm/RowColumn.h>
26 #include <wx/motif/private.h>
28 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
29 XmToggleButtonCallbackStruct
* cbs
);
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
36 wxRadioBox::wxRadioBox()
38 m_selectedButton
= -1;
43 m_formWidget
= (WXWidget
) 0;
44 m_labelWidget
= (WXWidget
) 0;
45 m_radioButtons
= (WXWidget
*) NULL
;
46 m_radioButtonLabels
= (wxString
*) NULL
;
49 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
50 const wxPoint
& pos
, const wxSize
& size
,
51 int n
, const wxString choices
[],
52 int majorDim
, long style
,
53 const wxValidator
& val
, const wxString
& name
)
55 m_selectedButton
= -1;
57 m_labelWidget
= (WXWidget
) 0;
58 m_radioButtons
= (WXWidget
*) NULL
;
59 m_radioButtonLabels
= (wxString
*) NULL
;
60 m_backgroundColour
= parent
->GetBackgroundColour();
61 m_foregroundColour
= parent
->GetForegroundColour();
62 m_windowFont
= parent
->GetFont();
67 parent
->AddChild(this);
69 m_windowStyle
= (long&)style
;
72 m_windowId
= NewControlId();
76 m_noRowsOrCols
= majorDim
;
81 m_majorDim
= majorDim
;
83 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
85 wxString
label1(wxStripMenuCodes(title
));
87 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
89 Widget formWidget
= XtVaCreateManagedWidget ((char*) (const char*) name
,
90 xmFormWidgetClass
, parentWidget
,
95 m_formWidget
= (WXWidget
) formWidget
;
97 XmFontList fontList
= (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay(parentWidget
));
100 text
= XmStringCreateSimple ((char*) (const char*) label1
);
101 Widget labelWidget
= XtVaCreateManagedWidget ((char*) (const char*) label1
,
104 xmLabelWidgetClass
: xmLabelGadgetClass
,
107 xmLabelWidgetClass
, formWidget
,
109 XmNfontList
, fontList
,
110 XmNlabelString
, text
,
118 majorDim
= (n
+ majorDim
- 1) / majorDim
;
120 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
121 XmHORIZONTAL
: XmVERTICAL
));
122 XtSetArg (args
[1], XmNnumColumns
, majorDim
);
124 Widget radioBoxWidget
= XmCreateRadioBox (formWidget
, "radioBoxWidget", args
, 2);
125 m_mainWidget
= (WXWidget
) radioBoxWidget
;
129 XtVaSetValues ((Widget
) m_labelWidget
,
130 XmNtopAttachment
, XmATTACH_FORM
,
131 XmNleftAttachment
, XmATTACH_FORM
,
132 XmNalignment
, XmALIGNMENT_BEGINNING
,
135 XtVaSetValues (radioBoxWidget
,
136 XmNtopAttachment
, m_labelWidget
? XmATTACH_WIDGET
: XmATTACH_FORM
,
137 XmNtopWidget
, m_labelWidget
? (Widget
) m_labelWidget
: formWidget
,
138 XmNbottomAttachment
, XmATTACH_FORM
,
139 XmNleftAttachment
, XmATTACH_FORM
,
142 // if (style & wxFLAT)
143 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
145 m_radioButtons
= new WXWidget
[n
];
146 m_radioButtonLabels
= new wxString
[n
];
148 for (i
= 0; i
< n
; i
++)
150 wxString
str(wxStripMenuCodes(choices
[i
]));
151 m_radioButtonLabels
[i
] = str
;
152 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
154 xmToggleButtonGadgetClass
, radioBoxWidget
,
156 xmToggleButtonWidgetClass
, radioBoxWidget
,
158 XmNfontList
, fontList
,
160 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
161 (XtCallbackProc
) this);
166 m_windowFont
= parent
->GetFont();
169 XtManageChild (radioBoxWidget
);
171 SetCanAddEventHandler(TRUE
);
172 AttachWidget (parent
, m_mainWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
174 ChangeBackgroundColour();
180 wxRadioBox::~wxRadioBox()
182 delete[] m_radioButtonLabels
;
183 delete[] m_radioButtons
;
186 wxString
wxRadioBox::GetLabel(int item
) const
188 if (item
< 0 || item
>= m_noItems
)
189 return wxEmptyString
;
191 Widget widget
= (Widget
) m_radioButtons
[item
];
194 XtVaGetValues (widget
,
195 XmNlabelString
, &text
,
198 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
200 // Should we free 'text'???
209 return wxEmptyString
;
213 void wxRadioBox::SetLabel(int item
, const wxString
& label
)
215 if (item
< 0 || item
>= m_noItems
)
218 Widget widget
= (Widget
) m_radioButtons
[item
];
221 wxString
label1(wxStripMenuCodes(label
));
222 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
223 XtVaSetValues (widget
,
224 XmNlabelString
, text
,
225 XmNlabelType
, XmSTRING
,
231 int wxRadioBox::FindString(const wxString
& s
) const
234 for (i
= 0; i
< m_noItems
; i
++)
235 if (s
== m_radioButtonLabels
[i
])
240 void wxRadioBox::SetSelection(int n
)
242 if ((n
< 0) || (n
>= m_noItems
))
245 m_selectedButton
= n
;
249 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
252 for (i
= 0; i
< m_noItems
; i
++)
254 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
256 m_inSetValue
= FALSE
;
259 // Get single selection, for single choice list items
260 int wxRadioBox::GetSelection() const
262 return m_selectedButton
;
265 // Find string for position
266 wxString
wxRadioBox::GetString(int n
) const
268 if ((n
< 0) || (n
>= m_noItems
))
269 return wxEmptyString
;
270 return m_radioButtonLabels
[n
];
273 void wxRadioBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
275 bool managed
= XtIsManaged((Widget
) m_formWidget
);
278 XtUnmanageChild ((Widget
) m_formWidget
);
280 int xx
= x
; int yy
= y
;
281 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
283 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
284 XtVaSetValues ((Widget
) m_formWidget
, XmNleftAttachment
, XmATTACH_SELF
,
286 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
287 XtVaSetValues ((Widget
) m_formWidget
, XmNtopAttachment
, XmATTACH_SELF
,
290 // Must set the actual RadioBox to be desired size MINUS label size
291 Dimension labelWidth
= 0, labelHeight
= 0, actualWidth
= 0, actualHeight
= 0;
294 XtVaGetValues ((Widget
) m_labelWidget
, XmNwidth
, &labelWidth
, XmNheight
, &labelHeight
, NULL
);
297 actualHeight
= height
- labelHeight
;
301 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, actualWidth
, NULL
);
305 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, actualHeight
, NULL
);
308 XtManageChild ((Widget
) m_formWidget
);
311 // Enable a specific button
312 void wxRadioBox::Enable(int n
, bool enable
)
314 if ((n
< 0) || (n
>= m_noItems
))
317 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
320 // Enable all controls
321 void wxRadioBox::Enable(bool enable
)
323 wxControl::Enable(enable
);
326 for (i
= 0; i
< m_noItems
; i
++)
327 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
330 // Show a specific button
331 void wxRadioBox::Show(int n
, bool show
)
333 // This method isn't complete, and we try do do our best...
334 // It's main purpose isn't for allowing Show/Unshow dynamically,
335 // but rather to provide a way to design wxRadioBox such:
337 // o Val1 o Val2 o Val3
339 // o Val7 o Val8 o Val9
341 // In my case, this is a 'direction' box, and the Show(5,False) is
342 // coupled with an Enable(5,False)
344 if ((n
< 0) || (n
>= m_noItems
))
347 XtVaSetValues ((Widget
) m_radioButtons
[n
],
348 XmNindicatorOn
, (unsigned char) show
,
351 // Please note that this is all we can do: removing the label
352 // if switching to unshow state. However, when switching
353 // to the on state, it's the prog. resp. to call SetLabel(item,...)
356 wxRadioBox::SetLabel (n
, " ");
359 // For single selection items only
360 wxString
wxRadioBox::GetStringSelection () const
362 int sel
= GetSelection ();
364 return this->GetString (sel
);
369 bool wxRadioBox::SetStringSelection (const wxString
& s
)
371 int sel
= FindString (s
);
381 void wxRadioBox::Command (wxCommandEvent
& event
)
383 SetSelection (event
.m_commandInt
);
384 ProcessCommand (event
);
387 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
392 void wxRadioBox::ChangeBackgroundColour()
397 void wxRadioBox::ChangeForegroundColour()
402 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
403 XmToggleButtonCallbackStruct
* cbs
)
408 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
411 for (i
= 0; i
< item
->Number(); i
++)
412 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
416 if (item
->InSetValue())
419 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
420 event
.m_commandInt
= sel
;
421 event
.SetEventObject(item
);
422 item
->ProcessCommand (event
);