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