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