1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "radiobox.h"
17 #define XtDisplay XTDISPLAY
22 #include "wx/radiobox.h"
26 #pragma message disable nosimpint
29 #include <Xm/LabelG.h>
30 #include <Xm/ToggleB.h>
31 #include <Xm/ToggleBG.h>
32 #include <Xm/RowColumn.h>
36 #pragma message enable nosimpint
39 #include "wx/motif/private.h"
41 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
42 XmToggleButtonCallbackStruct
* cbs
);
44 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
, wxControl
)
47 wxRadioBox::wxRadioBox()
49 m_selectedButton
= -1;
54 m_radioButtons
= (WXWidget
*) NULL
;
55 m_radioButtonLabels
= (wxString
*) NULL
;
58 bool wxRadioBox::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
59 const wxPoint
& pos
, const wxSize
& size
,
60 int n
, const wxString choices
[],
61 int majorDim
, long style
,
62 const wxValidator
& val
, const wxString
& name
)
64 m_selectedButton
= -1;
66 m_radioButtons
= (WXWidget
*) NULL
;
67 m_radioButtonLabels
= (wxString
*) NULL
;
68 m_backgroundColour
= parent
->GetBackgroundColour();
69 m_foregroundColour
= parent
->GetForegroundColour();
70 m_font
= parent
->GetFont();
75 parent
->AddChild(this);
77 m_windowStyle
= (long&)style
;
80 m_windowId
= NewControlId();
84 m_noRowsOrCols
= majorDim
;
89 m_majorDim
= majorDim
;
91 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
93 m_mainWidget
= XtVaCreateWidget ("radioboxframe",
94 xmFrameWidgetClass
, parentWidget
,
95 XmNshadowType
, XmSHADOW_IN
,
96 XmNresizeHeight
, True
,
100 wxString
label1(wxStripMenuCodes(title
));
102 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
106 wxXmString
text(label1
);
107 (void)XtVaCreateManagedWidget(label1
.c_str(),
109 style
& wxCOLOURED
? xmLabelWidgetClass
110 : xmLabelGadgetClass
,
111 (Widget
)m_mainWidget
,
113 xmLabelWidgetClass
, (Widget
)m_mainWidget
,
115 XmNfontList
, fontList
,
116 XmNlabelString
, text(),
117 // XmNframeChildType is not in Motif 1.2, nor in Lesstif,
118 // if it was compiled with 1.2 compatibility
119 // TODO: check this still looks OK for Motif 1.2.
120 #if (XmVersion > 1200)
121 XmNframeChildType
, XmFRAME_TITLE_CHILD
,
123 XmNchildVerticalAlignment
, XmALIGNMENT_CENTER
,
129 m_majorDim
= (n
+ m_majorDim
- 1) / m_majorDim
;
131 XtSetArg (args
[0], XmNorientation
, ((style
& wxHORIZONTAL
) == wxHORIZONTAL
?
132 XmHORIZONTAL
: XmVERTICAL
));
133 XtSetArg (args
[1], XmNnumColumns
, m_majorDim
);
135 Widget radioBoxWidget
= XmCreateRadioBox ((Widget
)m_mainWidget
, "radioBoxWidget", args
, 2);
137 // if (style & wxFLAT)
138 // XtVaSetValues (radioBoxWidget, XmNborderWidth, 1, NULL);
140 m_radioButtons
= new WXWidget
[n
];
141 m_radioButtonLabels
= new wxString
[n
];
143 for (i
= 0; i
< n
; i
++)
145 wxString
str(wxStripMenuCodes(choices
[i
]));
146 m_radioButtonLabels
[i
] = str
;
147 m_radioButtons
[i
] = (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) str
,
149 xmToggleButtonGadgetClass
, radioBoxWidget
,
151 xmToggleButtonWidgetClass
, radioBoxWidget
,
153 XmNfontList
, fontList
,
155 XtAddCallback ((Widget
) m_radioButtons
[i
], XmNvalueChangedCallback
, (XtCallbackProc
) wxRadioBoxCallback
,
159 m_font
= parent
->GetFont();
164 XtRealizeWidget((Widget
)m_mainWidget
);
165 XtManageChild (radioBoxWidget
);
166 XtManageChild ((Widget
)m_mainWidget
);
168 SetCanAddEventHandler(TRUE
);
169 AttachWidget (parent
, m_mainWidget
, NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
171 ChangeBackgroundColour();
177 wxRadioBox::~wxRadioBox()
179 delete[] m_radioButtonLabels
;
180 delete[] m_radioButtons
;
182 DetachWidget(m_mainWidget
);
183 XtDestroyWidget((Widget
) m_mainWidget
);
185 m_mainWidget
= (WXWidget
) 0;
188 void wxRadioBox::SetString(int item
, const wxString
& label
)
190 if (item
< 0 || item
>= m_noItems
)
193 Widget widget
= (Widget
) m_radioButtons
[item
];
196 wxString
label1(wxStripMenuCodes(label
));
197 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
198 XtVaSetValues (widget
,
199 XmNlabelString
, text
,
200 XmNlabelType
, XmSTRING
,
206 int wxRadioBox::FindString(const wxString
& s
) const
209 for (i
= 0; i
< m_noItems
; i
++)
210 if (s
== m_radioButtonLabels
[i
])
215 void wxRadioBox::SetSelection(int n
)
217 if ((n
< 0) || (n
>= m_noItems
))
220 m_selectedButton
= n
;
224 XmToggleButtonSetState ((Widget
) m_radioButtons
[n
], TRUE
, FALSE
);
227 for (i
= 0; i
< m_noItems
; i
++)
229 XmToggleButtonSetState ((Widget
) m_radioButtons
[i
], FALSE
, FALSE
);
231 m_inSetValue
= FALSE
;
234 // Get single selection, for single choice list items
235 int wxRadioBox::GetSelection() const
237 return m_selectedButton
;
240 // Find string for position
241 wxString
wxRadioBox::GetString(int n
) const
243 if ((n
< 0) || (n
>= m_noItems
))
244 return wxEmptyString
;
245 return m_radioButtonLabels
[n
];
248 void wxRadioBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
250 bool managed
= XtIsManaged((Widget
) m_mainWidget
);
253 XtUnmanageChild ((Widget
) m_mainWidget
);
255 int xx
= x
; int yy
= y
;
256 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
258 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
259 XtVaSetValues ((Widget
) m_mainWidget
, XmNx
, xx
, NULL
);
260 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
261 XtVaSetValues ((Widget
) m_mainWidget
, XmNy
, yy
, NULL
);
264 XtVaSetValues ((Widget
) m_mainWidget
, XmNwidth
, width
, NULL
);
266 XtVaSetValues ((Widget
) m_mainWidget
, XmNheight
, height
, NULL
);
269 XtManageChild ((Widget
) m_mainWidget
);
272 // Enable a specific button
273 void wxRadioBox::Enable(int n
, bool enable
)
275 if ((n
< 0) || (n
>= m_noItems
))
278 XtSetSensitive ((Widget
) m_radioButtons
[n
], (Boolean
) enable
);
281 // Enable all controls
282 bool wxRadioBox::Enable(bool enable
)
284 if ( !wxControl::Enable(enable
) )
288 for (i
= 0; i
< m_noItems
; i
++)
289 XtSetSensitive ((Widget
) m_radioButtons
[i
], (Boolean
) enable
);
294 bool wxRadioBox::Show(bool show
)
296 // TODO: show/hide all children
297 return wxControl::Show(show
);
300 // Show a specific button
301 void wxRadioBox::Show(int n
, bool show
)
303 // This method isn't complete, and we try do do our best...
304 // It's main purpose isn't for allowing Show/Unshow dynamically,
305 // but rather to provide a way to design wxRadioBox such:
307 // o Val1 o Val2 o Val3
309 // o Val7 o Val8 o Val9
311 // In my case, this is a 'direction' box, and the Show(5,False) is
312 // coupled with an Enable(5,False)
314 if ((n
< 0) || (n
>= m_noItems
))
317 XtVaSetValues ((Widget
) m_radioButtons
[n
],
318 XmNindicatorOn
, (unsigned char) show
,
321 // Please note that this is all we can do: removing the label
322 // if switching to unshow state. However, when switching
323 // to the on state, it's the prog. resp. to call SetString(item,...)
326 wxRadioBox::SetString (n
, " ");
329 // For single selection items only
330 wxString
wxRadioBox::GetStringSelection () const
332 int sel
= GetSelection ();
334 return this->GetString (sel
);
339 bool wxRadioBox::SetStringSelection (const wxString
& s
)
341 int sel
= FindString (s
);
351 void wxRadioBox::Command (wxCommandEvent
& event
)
353 SetSelection (event
.m_commandInt
);
354 ProcessCommand (event
);
357 void wxRadioBox::ChangeFont(bool keepOriginalSize
)
359 wxWindow::ChangeFont(keepOriginalSize
);
361 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) GetTopWidget()));
364 for (i
= 0; i
< m_noItems
; i
++)
366 WXWidget radioButton
= m_radioButtons
[i
];
368 XtVaSetValues ((Widget
) radioButton
,
369 XmNfontList
, fontList
,
374 void wxRadioBox::ChangeBackgroundColour()
376 wxWindow::ChangeBackgroundColour();
378 int selectPixel
= wxBLACK
->AllocColour(wxGetDisplay());
381 for (i
= 0; i
< m_noItems
; i
++)
383 WXWidget radioButton
= m_radioButtons
[i
];
385 DoChangeBackgroundColour(radioButton
, m_backgroundColour
, TRUE
);
387 XtVaSetValues ((Widget
) radioButton
,
388 XmNselectColor
, selectPixel
,
393 void wxRadioBox::ChangeForegroundColour()
395 wxWindow::ChangeForegroundColour();
398 for (i
= 0; i
< m_noItems
; i
++)
400 WXWidget radioButton
= m_radioButtons
[i
];
402 DoChangeForegroundColour(radioButton
, m_foregroundColour
);
406 static int CalcOtherDim( int items
, int dim
)
408 return items
/ dim
+ ( items
% dim
? 1 : 0 );
411 int wxRadioBox::GetRowCount() const
413 return m_windowStyle
& wxRA_SPECIFY_ROWS
? m_noRowsOrCols
414 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
417 int wxRadioBox::GetColumnCount() const
419 return m_windowStyle
& wxRA_SPECIFY_COLS
? m_noRowsOrCols
420 : CalcOtherDim( GetCount(), m_noRowsOrCols
);
423 void wxRadioBoxCallback (Widget w
, XtPointer clientData
,
424 XmToggleButtonCallbackStruct
* cbs
)
429 wxRadioBox
*item
= (wxRadioBox
*) clientData
;
432 for (i
= 0; i
< item
->GetCount(); i
++)
433 if (item
->GetRadioButtons() && ((Widget
) (item
->GetRadioButtons()[i
]) == w
))
437 if (item
->InSetValue())
440 wxCommandEvent
event (wxEVT_COMMAND_RADIOBOX_SELECTED
, item
->GetId());
442 event
.SetString(item
->GetStringSelection());
443 event
.SetEventObject(item
);
444 item
->ProcessCommand (event
);