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