]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/radiobox.cpp
More Motif additions: mdi and sashtest samples now just about work!
[wxWidgets.git] / src / motif / radiobox.cpp
... / ...
CommitLineData
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#include "wx/radiobox.h"
17#include "wx/utils.h"
18
19#include <Xm/Label.h>
20#include <Xm/LabelG.h>
21#include <Xm/ToggleB.h>
22#include <Xm/ToggleBG.h>
23#include <Xm/RowColumn.h>
24#include <Xm/Form.h>
25
26#include <wx/motif/private.h>
27
28void wxRadioBoxCallback (Widget w, XtPointer clientData,
29 XmToggleButtonCallbackStruct * cbs);
30
31#if !USE_SHARED_LIBRARY
32IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl)
33#endif
34
35// Radio box item
36wxRadioBox::wxRadioBox()
37{
38 m_selectedButton = -1;
39 m_noItems = 0;
40 m_noRowsOrCols = 0;
41 m_majorDim = 0 ;
42
43 m_formWidget = (WXWidget) 0;
44 m_labelWidget = (WXWidget) 0;
45 m_radioButtons = (WXWidget*) NULL;
46 m_radioButtonLabels = (wxString*) NULL;
47}
48
49bool 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)
54{
55 m_selectedButton = -1;
56 m_noItems = n;
57 m_labelWidget = (WXWidget) 0;
58 m_radioButtons = (WXWidget*) NULL;
59 m_radioButtonLabels = (wxString*) NULL;
60
61 SetName(name);
62 SetValidator(val);
63
64 parent->AddChild(this);
65
66 m_windowStyle = (long&)style;
67
68 if (id == -1)
69 m_windowId = NewControlId();
70 else
71 m_windowId = id;
72
73 m_noRowsOrCols = majorDim;
74
75 if (majorDim==0)
76 m_majorDim = n ;
77 else
78 m_majorDim = majorDim ;
79
80 Widget parentWidget = (Widget) parent->GetClientWidget();
81
82 wxString label1(wxStripMenuCodes(title));
83
84 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
85
86 Widget formWidget = XtVaCreateManagedWidget ((char*) (const char*) name,
87 xmFormWidgetClass, parentWidget,
88 XmNmarginHeight, 0,
89 XmNmarginWidth, 0,
90 NULL);
91
92 m_formWidget = (WXWidget) formWidget;
93
94 if (label1 != "")
95 {
96 text = XmStringCreateSimple ((char*) (const char*) label1);
97 Widget labelWidget = XtVaCreateManagedWidget ((char*) (const char*) label1,
98#if wxUSE_GADGETS
99 style & wxCOLOURED ?
100 xmLabelWidgetClass : xmLabelGadgetClass,
101 formWidget,
102#else
103 xmLabelWidgetClass, formWidget,
104#endif
105 XmNlabelString, text,
106 NULL);
107
108/* TODO: change label font
109 if (labelFont)
110 XtVaSetValues (labelWidget,
111 XmNfontList, labelFont->GetInternalFont (XtDisplay(formWidget)),
112 NULL);
113*/
114
115 XmStringFree (text);
116 }
117
118 Arg args[3];
119
120 majorDim = (n + majorDim - 1) / majorDim;
121
122 XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ?
123 XmHORIZONTAL : XmVERTICAL));
124 XtSetArg (args[1], XmNnumColumns, majorDim);
125
126 Widget radioBoxWidget = XmCreateRadioBox (formWidget, "radioBoxWidget", args, 2);
127 m_mainWidget = (WXWidget) radioBoxWidget;
128
129
130 if (m_labelWidget)
131 XtVaSetValues ((Widget) m_labelWidget,
132 XmNtopAttachment, XmATTACH_FORM,
133 XmNleftAttachment, XmATTACH_FORM,
134 XmNalignment, XmALIGNMENT_BEGINNING,
135 NULL);
136
137 XtVaSetValues (radioBoxWidget,
138 XmNtopAttachment, m_labelWidget ? XmATTACH_WIDGET : XmATTACH_FORM,
139 XmNtopWidget, m_labelWidget ? (Widget) m_labelWidget : formWidget,
140 XmNbottomAttachment, XmATTACH_FORM,
141 XmNleftAttachment, XmATTACH_FORM,
142 NULL);
143
144 // if (style & wxFLAT)
145 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
146
147 m_radioButtons = new WXWidget[n];
148 m_radioButtonLabels = new wxString[n];
149 int i;
150 for (i = 0; i < n; i++)
151 {
152 wxString str(wxStripMenuCodes(choices[i]));
153 m_radioButtonLabels[i] = str;
154 m_radioButtons[i] = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) str,
155#if wxUSE_GADGETS
156 xmToggleButtonGadgetClass, radioBoxWidget,
157#else
158 xmToggleButtonWidgetClass, radioBoxWidget,
159#endif
160 NULL);
161 XtAddCallback ((Widget) m_radioButtons[i], XmNvalueChangedCallback, (XtCallbackProc) wxRadioBoxCallback,
162 (XtCallbackProc) this);
163
164/* TODO: set font
165 if (buttonFont)
166 XtVaSetValues ((Widget) m_radioButtons[i],
167 XmNfontList, buttonFont->GetInternalFont (XtDisplay(formWidget)),
168 NULL);
169*/
170 }
171 SetSelection (0);
172
173 XtManageChild (radioBoxWidget);
174
175 SetCanAddEventHandler(TRUE);
176 AttachWidget (parent, m_mainWidget, m_formWidget, pos.x, pos.y, size.x, size.y);
177
178 SetFont(* parent->GetFont());
179 ChangeColour(m_mainWidget);
180
181 return TRUE;
182}
183
184
185wxRadioBox::~wxRadioBox()
186{
187 delete[] m_radioButtonLabels;
188 delete[] m_radioButtons;
189}
190
191wxString wxRadioBox::GetLabel(int item) const
192{
193 if (item < 0 || item >= m_noItems)
194 return wxEmptyString;
195
196 Widget widget = (Widget) m_radioButtons[item];
197 XmString text;
198 char *s;
199 XtVaGetValues (widget,
200 XmNlabelString, &text,
201 NULL);
202
203 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
204 {
205 // Should we free 'text'???
206 XmStringFree(text);
207 wxString str(s);
208 XtFree (s);
209 return str;
210 }
211 else
212 {
213 XmStringFree(text);
214 return wxEmptyString;
215 }
216}
217
218void wxRadioBox::SetLabel(int item, const wxString& label)
219{
220 if (item < 0 || item >= m_noItems)
221 return;
222
223 Widget widget = (Widget) m_radioButtons[item];
224 if (label != "")
225 {
226 wxString label1(wxStripMenuCodes(label));
227 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
228 XtVaSetValues (widget,
229 XmNlabelString, text,
230 XmNlabelType, XmSTRING,
231 NULL);
232 XmStringFree (text);
233 }
234}
235
236int wxRadioBox::FindString(const wxString& s) const
237{
238 int i;
239 for (i = 0; i < m_noItems; i++)
240 if (s == m_radioButtonLabels[i])
241 return i;
242 return -1;
243}
244
245void wxRadioBox::SetSelection(int n)
246{
247 if ((n < 0) || (n >= m_noItems))
248 return;
249
250 m_selectedButton = n;
251
252 m_inSetValue = TRUE;
253
254 XmToggleButtonSetState ((Widget) m_radioButtons[n], TRUE, FALSE);
255
256 int i;
257 for (i = 0; i < m_noItems; i++)
258 if (i != n)
259 XmToggleButtonSetState ((Widget) m_radioButtons[i], FALSE, FALSE);
260
261 m_inSetValue = FALSE;
262}
263
264// Get single selection, for single choice list items
265int wxRadioBox::GetSelection() const
266{
267 return m_selectedButton;
268}
269
270// Find string for position
271wxString wxRadioBox::GetString(int n) const
272{
273 if ((n < 0) || (n >= m_noItems))
274 return wxEmptyString;
275 return m_radioButtonLabels[n];
276}
277
278void wxRadioBox::SetSize(int x, int y, int width, int height, int sizeFlags)
279{
280 bool managed = XtIsManaged((Widget) m_formWidget);
281
282 if (managed)
283 XtUnmanageChild ((Widget) m_formWidget);
284
285 int xx = x; int yy = y;
286 AdjustForParentClientOrigin(xx, yy, sizeFlags);
287
288 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
289 XtVaSetValues ((Widget) m_formWidget, XmNleftAttachment, XmATTACH_SELF,
290 XmNx, xx, NULL);
291 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
292 XtVaSetValues ((Widget) m_formWidget, XmNtopAttachment, XmATTACH_SELF,
293 XmNy, yy, NULL);
294
295 // Must set the actual RadioBox to be desired size MINUS label size
296 Dimension labelWidth = 0, labelHeight = 0, actualWidth = 0, actualHeight = 0;
297
298 if (m_labelWidget)
299 XtVaGetValues ((Widget) m_labelWidget, XmNwidth, &labelWidth, XmNheight, &labelHeight, NULL);
300
301 actualWidth = width;
302 actualHeight = height - labelHeight;
303
304 if (width > -1)
305 {
306 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, actualWidth, NULL);
307 }
308 if (height > -1)
309 {
310 XtVaSetValues ((Widget) m_mainWidget, XmNheight, actualHeight, NULL);
311 }
312 if (managed)
313 XtManageChild ((Widget) m_formWidget);
314}
315
316// Enable a specific button
317void wxRadioBox::Enable(int n, bool enable)
318{
319 if ((n < 0) || (n >= m_noItems))
320 return;
321
322 XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable);
323}
324
325// Enable all controls
326void wxRadioBox::Enable(bool enable)
327{
328 wxControl::Enable(enable);
329
330 int i;
331 for (i = 0; i < m_noItems; i++)
332 XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable);
333}
334
335// Show a specific button
336void wxRadioBox::Show(int n, bool show)
337{
338 // This method isn't complete, and we try do do our best...
339 // It's main purpose isn't for allowing Show/Unshow dynamically,
340 // but rather to provide a way to design wxRadioBox such:
341 //
342 // o Val1 o Val2 o Val3
343 // o Val4 o Val6
344 // o Val7 o Val8 o Val9
345 //
346 // In my case, this is a 'direction' box, and the Show(5,False) is
347 // coupled with an Enable(5,False)
348 //
349 if ((n < 0) || (n >= m_noItems))
350 return;
351
352 XtVaSetValues ((Widget) m_radioButtons[n],
353 XmNindicatorOn, (unsigned char) show,
354 NULL);
355
356 // Please note that this is all we can do: removing the label
357 // if switching to unshow state. However, when switching
358 // to the on state, it's the prog. resp. to call SetLabel(item,...)
359 // after this call!!
360 if (!show)
361 wxRadioBox::SetLabel (n, " ");
362}
363
364// For single selection items only
365wxString wxRadioBox::GetStringSelection () const
366{
367 int sel = GetSelection ();
368 if (sel > -1)
369 return this->GetString (sel);
370 else
371 return wxString("");
372}
373
374bool wxRadioBox::SetStringSelection (const wxString& s)
375{
376 int sel = FindString (s);
377 if (sel > -1)
378 {
379 SetSelection (sel);
380 return TRUE;
381 }
382 else
383 return FALSE;
384}
385
386void wxRadioBox::Command (wxCommandEvent & event)
387{
388 SetSelection (event.m_commandInt);
389 ProcessCommand (event);
390}
391
392void wxRadioBoxCallback (Widget w, XtPointer clientData,
393 XmToggleButtonCallbackStruct * cbs)
394{
395 if (!cbs->set)
396 return;
397
398 wxRadioBox *item = (wxRadioBox *) clientData;
399 int sel = -1;
400 int i;
401 for (i = 0; i < item->Number(); i++)
402 if (item->GetRadioButtons() && ((Widget) (item->GetRadioButtons()[i]) == w))
403 sel = i;
404 item->SetSel(sel);
405
406 if (item->InSetValue())
407 return;
408
409 wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId());
410 event.m_commandInt = sel;
411 event.SetEventObject(item);
412 item->ProcessCommand (event);
413}
414