]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/motif/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 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #if wxUSE_RADIOBOX | |
16 | ||
17 | #ifdef __VMS | |
18 | #define XtDisplay XTDISPLAY | |
19 | #endif | |
20 | ||
21 | #include "wx/radiobox.h" | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/utils.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/arrstr.h" | |
28 | ||
29 | #ifdef __VMS__ | |
30 | #pragma message disable nosimpint | |
31 | #endif | |
32 | #include <Xm/Label.h> | |
33 | #include <Xm/LabelG.h> | |
34 | #include <Xm/ToggleB.h> | |
35 | #include <Xm/ToggleBG.h> | |
36 | #include <Xm/RowColumn.h> | |
37 | #include <Xm/Frame.h> | |
38 | #ifdef __VMS__ | |
39 | #pragma message enable nosimpint | |
40 | #endif | |
41 | ||
42 | #include "wx/motif/private.h" | |
43 | ||
44 | void wxRadioBoxCallback (Widget w, XtPointer clientData, | |
45 | XmToggleButtonCallbackStruct * cbs); | |
46 | ||
47 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox, wxControl) | |
48 | ||
49 | // Radio box item | |
50 | void wxRadioBox::Init() | |
51 | { | |
52 | m_selectedButton = -1; | |
53 | m_noItems = 0; | |
54 | m_noRowsOrCols = 0; | |
55 | } | |
56 | ||
57 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title, | |
58 | const wxPoint& pos, const wxSize& size, | |
59 | int n, const wxString choices[], | |
60 | int majorDim, long style, | |
61 | const wxValidator& val, const wxString& name) | |
62 | { | |
63 | if( !CreateControl( parent, id, pos, size, style, val, name ) ) | |
64 | return false; | |
65 | ||
66 | m_noItems = (unsigned int)n; | |
67 | m_noRowsOrCols = majorDim; | |
68 | ||
69 | SetMajorDim(majorDim == 0 ? n : majorDim, style); | |
70 | ||
71 | Widget parentWidget = (Widget) parent->GetClientWidget(); | |
72 | Display* dpy = XtDisplay(parentWidget); | |
73 | ||
74 | m_mainWidget = XtVaCreateWidget ("radioboxframe", | |
75 | xmFrameWidgetClass, parentWidget, | |
76 | XmNresizeHeight, True, | |
77 | XmNresizeWidth, True, | |
78 | NULL); | |
79 | ||
80 | wxString label1(wxStripMenuCodes(title)); | |
81 | ||
82 | if (!label1.empty()) | |
83 | { | |
84 | wxXmString text(label1); | |
85 | m_labelWidget = (WXWidget) | |
86 | XtVaCreateManagedWidget( label1.c_str(), | |
87 | #if wxUSE_GADGETS | |
88 | style & wxCOLOURED ? xmLabelWidgetClass | |
89 | : xmLabelGadgetClass, | |
90 | (Widget)m_mainWidget, | |
91 | #else | |
92 | xmLabelWidgetClass, (Widget)m_mainWidget, | |
93 | #endif | |
94 | wxFont::GetFontTag(), m_font.GetFontTypeC(dpy), | |
95 | XmNlabelString, text(), | |
96 | // XmNframeChildType is not in Motif 1.2, nor in Lesstif, | |
97 | // if it was compiled with 1.2 compatibility | |
98 | // TODO: check this still looks OK for Motif 1.2. | |
99 | #if (XmVersion > 1200) | |
100 | XmNframeChildType, XmFRAME_TITLE_CHILD, | |
101 | #else | |
102 | XmNchildType, XmFRAME_TITLE_CHILD, | |
103 | #endif | |
104 | XmNchildVerticalAlignment, XmALIGNMENT_CENTER, | |
105 | NULL); | |
106 | } | |
107 | ||
108 | Arg args[3]; | |
109 | ||
110 | XtSetArg (args[0], XmNorientation, ((style & wxHORIZONTAL) == wxHORIZONTAL ? | |
111 | XmHORIZONTAL : XmVERTICAL)); | |
112 | XtSetArg (args[1], XmNnumColumns, GetMajorDim()); | |
113 | XtSetArg (args[2], XmNadjustLast, False); | |
114 | ||
115 | Widget radioBoxWidget = | |
116 | XmCreateRadioBox ((Widget)m_mainWidget, wxMOTIF_STR("radioBoxWidget"), args, 3); | |
117 | ||
118 | m_radioButtons.reserve(n); | |
119 | m_radioButtonLabels.reserve(n); | |
120 | ||
121 | int i; | |
122 | for (i = 0; i < n; i++) | |
123 | { | |
124 | wxString str(wxStripMenuCodes(choices[i])); | |
125 | m_radioButtonLabels.push_back(str); | |
126 | Widget radioItem = XtVaCreateManagedWidget ( | |
127 | wxConstCast(str.c_str(), char), | |
128 | #if wxUSE_GADGETS | |
129 | xmToggleButtonGadgetClass, radioBoxWidget, | |
130 | #else | |
131 | xmToggleButtonWidgetClass, radioBoxWidget, | |
132 | #endif | |
133 | wxFont::GetFontTag(), m_font.GetFontTypeC(dpy), | |
134 | NULL); | |
135 | m_radioButtons.push_back((WXWidget)radioItem); | |
136 | XtAddCallback (radioItem, XmNvalueChangedCallback, | |
137 | (XtCallbackProc) wxRadioBoxCallback, | |
138 | (XtPointer) this); | |
139 | } | |
140 | ||
141 | ChangeFont(false); | |
142 | ||
143 | SetSelection (0); | |
144 | ||
145 | XtRealizeWidget((Widget)m_mainWidget); | |
146 | XtManageChild (radioBoxWidget); | |
147 | XtManageChild ((Widget)m_mainWidget); | |
148 | ||
149 | AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y); | |
150 | ||
151 | ChangeBackgroundColour(); | |
152 | ||
153 | return true; | |
154 | } | |
155 | ||
156 | bool wxRadioBox::Create(wxWindow *parent, wxWindowID id, const wxString& title, | |
157 | const wxPoint& pos, const wxSize& size, | |
158 | const wxArrayString& choices, | |
159 | int majorDim, long style, | |
160 | const wxValidator& val, const wxString& name) | |
161 | { | |
162 | wxCArrayString chs(choices); | |
163 | return Create(parent, id, title, pos, size, chs.GetCount(), | |
164 | chs.GetStrings(), majorDim, style, val, name); | |
165 | } | |
166 | ||
167 | wxRadioBox::~wxRadioBox() | |
168 | { | |
169 | DetachWidget(m_mainWidget); | |
170 | XtDestroyWidget((Widget) m_mainWidget); | |
171 | ||
172 | m_mainWidget = (WXWidget) 0; | |
173 | } | |
174 | ||
175 | void wxRadioBox::SetString(unsigned int item, const wxString& label) | |
176 | { | |
177 | if (!IsValid(item)) | |
178 | return; | |
179 | ||
180 | Widget widget = (Widget)m_radioButtons[item]; | |
181 | if (!label.empty()) | |
182 | { | |
183 | wxString label1(wxStripMenuCodes(label)); | |
184 | wxXmString text( label1 ); | |
185 | m_radioButtonLabels[item] = label1; | |
186 | XtVaSetValues (widget, | |
187 | XmNlabelString, text(), | |
188 | XmNlabelType, XmSTRING, | |
189 | NULL); | |
190 | } | |
191 | } | |
192 | ||
193 | void wxRadioBox::SetSelection(int n) | |
194 | { | |
195 | if (!IsValid(n)) | |
196 | return; | |
197 | ||
198 | m_selectedButton = n; | |
199 | ||
200 | m_inSetValue = true; | |
201 | ||
202 | XmToggleButtonSetState ((Widget) m_radioButtons[n], True, False); | |
203 | ||
204 | for (unsigned int i = 0; i < m_noItems; i++) | |
205 | if (i != (unsigned int)n) | |
206 | XmToggleButtonSetState ((Widget) m_radioButtons[i], False, False); | |
207 | ||
208 | m_inSetValue = false; | |
209 | } | |
210 | ||
211 | // Get single selection, for single choice list items | |
212 | int wxRadioBox::GetSelection() const | |
213 | { | |
214 | return m_selectedButton; | |
215 | } | |
216 | ||
217 | // Find string for position | |
218 | wxString wxRadioBox::GetString(unsigned int n) const | |
219 | { | |
220 | if (!IsValid(n)) | |
221 | return wxEmptyString; | |
222 | return m_radioButtonLabels[n]; | |
223 | } | |
224 | ||
225 | void wxRadioBox::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
226 | { | |
227 | bool managed = XtIsManaged((Widget) m_mainWidget); | |
228 | ||
229 | if (managed) | |
230 | XtUnmanageChild ((Widget) m_mainWidget); | |
231 | ||
232 | int xx = x; int yy = y; | |
233 | AdjustForParentClientOrigin(xx, yy, sizeFlags); | |
234 | ||
235 | if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
236 | XtVaSetValues ((Widget) m_mainWidget, XmNx, xx, NULL); | |
237 | if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
238 | XtVaSetValues ((Widget) m_mainWidget, XmNy, yy, NULL); | |
239 | ||
240 | if (width > 0) | |
241 | XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width, NULL); | |
242 | if (height > 0) | |
243 | XtVaSetValues ((Widget) m_mainWidget, XmNheight, height, NULL); | |
244 | ||
245 | if (managed) | |
246 | XtManageChild ((Widget) m_mainWidget); | |
247 | } | |
248 | ||
249 | // Enable a specific button | |
250 | bool wxRadioBox::Enable(unsigned int n, bool enable) | |
251 | { | |
252 | if (!IsValid(n)) | |
253 | return false; | |
254 | ||
255 | XtSetSensitive ((Widget) m_radioButtons[n], (Boolean) enable); | |
256 | return true; | |
257 | } | |
258 | ||
259 | // Enable all controls | |
260 | bool wxRadioBox::Enable(bool enable) | |
261 | { | |
262 | if ( !wxControl::Enable(enable) ) | |
263 | return false; | |
264 | ||
265 | for (unsigned int i = 0; i < m_noItems; i++) | |
266 | XtSetSensitive ((Widget) m_radioButtons[i], (Boolean) enable); | |
267 | ||
268 | return true; | |
269 | } | |
270 | ||
271 | bool wxRadioBox::Show(bool show) | |
272 | { | |
273 | // TODO: show/hide all children | |
274 | return wxControl::Show(show); | |
275 | } | |
276 | ||
277 | // Show a specific button | |
278 | bool wxRadioBox::Show(unsigned int n, bool show) | |
279 | { | |
280 | // This method isn't complete, and we try do do our best... | |
281 | // It's main purpose isn't for allowing Show/Unshow dynamically, | |
282 | // but rather to provide a way to design wxRadioBox such: | |
283 | // | |
284 | // o Val1 o Val2 o Val3 | |
285 | // o Val4 o Val6 | |
286 | // o Val7 o Val8 o Val9 | |
287 | // | |
288 | // In my case, this is a 'direction' box, and the Show(5,False) is | |
289 | // coupled with an Enable(5,False) | |
290 | // | |
291 | if (!IsValid(n)) | |
292 | return false; | |
293 | ||
294 | XtVaSetValues ((Widget) m_radioButtons[n], | |
295 | XmNindicatorOn, (unsigned char) show, | |
296 | NULL); | |
297 | ||
298 | // Please note that this is all we can do: removing the label | |
299 | // if switching to unshow state. However, when switching | |
300 | // to the on state, it's the prog. resp. to call SetString(item,...) | |
301 | // after this call!! | |
302 | if (!show) | |
303 | wxRadioBox::SetString (n, " "); | |
304 | ||
305 | return true; | |
306 | } | |
307 | ||
308 | // For single selection items only | |
309 | wxString wxRadioBox::GetStringSelection () const | |
310 | { | |
311 | int sel = GetSelection (); | |
312 | if (sel != wxNOT_FOUND) | |
313 | return this->GetString((unsigned int)sel); | |
314 | else | |
315 | return wxEmptyString; | |
316 | } | |
317 | ||
318 | bool wxRadioBox::SetStringSelection (const wxString& s) | |
319 | { | |
320 | int sel = FindString (s); | |
321 | if (sel > -1) | |
322 | { | |
323 | SetSelection (sel); | |
324 | return true; | |
325 | } | |
326 | else | |
327 | return false; | |
328 | } | |
329 | ||
330 | void wxRadioBox::Command (wxCommandEvent & event) | |
331 | { | |
332 | SetSelection (event.GetInt()); | |
333 | ProcessCommand (event); | |
334 | } | |
335 | ||
336 | void wxRadioBox::ChangeFont(bool keepOriginalSize) | |
337 | { | |
338 | wxWindow::ChangeFont(keepOriginalSize); | |
339 | ||
340 | for (unsigned int i = 0; i < m_noItems; i++) | |
341 | { | |
342 | WXWidget radioButton = m_radioButtons[i]; | |
343 | ||
344 | XtVaSetValues ((Widget) radioButton, | |
345 | wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay((Widget) GetTopWidget())), | |
346 | NULL); | |
347 | } | |
348 | } | |
349 | ||
350 | void wxRadioBox::ChangeBackgroundColour() | |
351 | { | |
352 | wxWindow::ChangeBackgroundColour(); | |
353 | ||
354 | wxColour colour = *wxBLACK; | |
355 | int selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget)); | |
356 | ||
357 | for (unsigned int i = 0; i < m_noItems; i++) | |
358 | { | |
359 | WXWidget radioButton = m_radioButtons[i]; | |
360 | ||
361 | wxDoChangeBackgroundColour(radioButton, m_backgroundColour, true); | |
362 | ||
363 | XtVaSetValues ((Widget) radioButton, | |
364 | XmNselectColor, selectPixel, | |
365 | NULL); | |
366 | } | |
367 | } | |
368 | ||
369 | void wxRadioBox::ChangeForegroundColour() | |
370 | { | |
371 | wxWindow::ChangeForegroundColour(); | |
372 | ||
373 | for (unsigned int i = 0; i < m_noItems; i++) | |
374 | { | |
375 | WXWidget radioButton = m_radioButtons[i]; | |
376 | ||
377 | wxDoChangeForegroundColour(radioButton, m_foregroundColour); | |
378 | } | |
379 | } | |
380 | ||
381 | void wxRadioBoxCallback (Widget w, XtPointer clientData, | |
382 | XmToggleButtonCallbackStruct * cbs) | |
383 | { | |
384 | if (!cbs->set) | |
385 | return; | |
386 | ||
387 | wxRadioBox *item = (wxRadioBox *) clientData; | |
388 | int sel = -1; | |
389 | unsigned int i; | |
390 | const wxWidgetArray& buttons = item->GetRadioButtons(); | |
391 | for (i = 0; i < item->GetCount(); i++) | |
392 | if (((Widget)buttons[i]) == w) | |
393 | sel = (int)i; | |
394 | item->SetSel(sel); | |
395 | ||
396 | if (item->InSetValue()) | |
397 | return; | |
398 | ||
399 | wxCommandEvent event (wxEVT_COMMAND_RADIOBOX_SELECTED, item->GetId()); | |
400 | event.SetInt(sel); | |
401 | event.SetString(item->GetStringSelection()); | |
402 | event.SetEventObject(item); | |
403 | item->ProcessCommand (event); | |
404 | } | |
405 | ||
406 | #endif // wxUSE_RADIOBOX |