]> git.saurik.com Git - wxWidgets.git/blob - src/common/radiocmn.cpp
adding implementation for osx
[wxWidgets.git] / src / common / radiocmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/radiocmn.cpp
3 // Purpose: wxRadioBox methods common to all ports
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 03.06.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_RADIOBOX
28
29 #ifndef WX_PRECOMP
30 #include "wx/radiobox.h"
31 #endif //WX_PRECOMP
32
33 #if wxUSE_TOOLTIPS
34 #include "wx/tooltip.h"
35 #endif // wxUSE_TOOLTIPS
36
37 #if wxUSE_HELP
38 #include "wx/cshelp.h"
39 #endif
40
41 extern WXDLLEXPORT_DATA(const char) wxRadioBoxNameStr[] = "radioBox";
42
43 // ============================================================================
44 // implementation
45 // ============================================================================
46
47 // ----------------------------------------------------------------------------
48 // XTI
49 // ----------------------------------------------------------------------------
50
51 // TODO: wxCONSTRUCTOR
52 #if 0 // wxUSE_EXTENDED_RTTI
53 wxDEFINE_FLAGS( wxRadioBoxStyle )
54
55 wxBEGIN_FLAGS( wxRadioBoxStyle )
56 // new style border flags, we put them first to
57 // use them for streaming out
58 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
59 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
60 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
61 wxFLAGS_MEMBER(wxBORDER_RAISED)
62 wxFLAGS_MEMBER(wxBORDER_STATIC)
63 wxFLAGS_MEMBER(wxBORDER_NONE)
64
65 // old style border flags
66 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
67 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
68 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
69 wxFLAGS_MEMBER(wxRAISED_BORDER)
70 wxFLAGS_MEMBER(wxSTATIC_BORDER)
71 wxFLAGS_MEMBER(wxBORDER)
72
73 // standard window styles
74 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
75 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
76 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
77 wxFLAGS_MEMBER(wxWANTS_CHARS)
78 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
79 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
80 wxFLAGS_MEMBER(wxVSCROLL)
81 wxFLAGS_MEMBER(wxHSCROLL)
82
83 wxFLAGS_MEMBER(wxRA_SPECIFY_COLS)
84 wxFLAGS_MEMBER(wxRA_HORIZONTAL)
85 wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS)
86 wxFLAGS_MEMBER(wxRA_VERTICAL)
87
88 wxEND_FLAGS( wxRadioBoxStyle )
89
90 IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox, wxControl,"wx/radiobox.h")
91
92 wxBEGIN_PROPERTIES_TABLE(wxRadioBox)
93 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_RADIOBOX_SELECTED , wxCommandEvent )
94 wxPROPERTY_FLAGS( WindowStyle , wxRadioBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
95 wxEND_PROPERTIES_TABLE()
96
97 /*
98 selection
99 content
100 label
101 dimension
102 item
103 */
104
105 #endif
106
107
108 // ----------------------------------------------------------------------------
109 // wxRadioBoxBase
110 // ----------------------------------------------------------------------------
111
112 void wxRadioBoxBase::SetMajorDim(unsigned int majorDim, long style)
113 {
114 wxCHECK_RET( majorDim != 0, wxT("major radiobox dimension can't be 0") );
115
116 m_majorDim = majorDim;
117
118 int minorDim = (GetCount() + m_majorDim - 1) / m_majorDim;
119
120 if ( style & wxRA_SPECIFY_COLS )
121 {
122 m_numCols = majorDim;
123 m_numRows = minorDim;
124 }
125 else // wxRA_SPECIFY_ROWS
126 {
127 m_numCols = minorDim;
128 m_numRows = majorDim;
129 }
130 }
131
132 int wxRadioBoxBase::GetNextItem(int item, wxDirection dir, long style) const
133 {
134 const int itemStart = item;
135
136 int count = GetCount(),
137 numCols = GetColumnCount(),
138 numRows = GetRowCount();
139
140 bool horz = (style & wxRA_SPECIFY_COLS) != 0;
141
142 do
143 {
144 switch ( dir )
145 {
146 case wxUP:
147 if ( horz )
148 {
149 item -= numCols;
150 }
151 else // vertical layout
152 {
153 if ( !item-- )
154 item = count - 1;
155 }
156 break;
157
158 case wxLEFT:
159 if ( horz )
160 {
161 if ( !item-- )
162 item = count - 1;
163 }
164 else // vertical layout
165 {
166 item -= numRows;
167 }
168 break;
169
170 case wxDOWN:
171 if ( horz )
172 {
173 item += numCols;
174 }
175 else // vertical layout
176 {
177 if ( ++item == count )
178 item = 0;
179 }
180 break;
181
182 case wxRIGHT:
183 if ( horz )
184 {
185 if ( ++item == count )
186 item = 0;
187 }
188 else // vertical layout
189 {
190 item += numRows;
191 }
192 break;
193
194 default:
195 wxFAIL_MSG( wxT("unexpected wxDirection value") );
196 return wxNOT_FOUND;
197 }
198
199 // ensure that the item is in range [0..count)
200 if ( item < 0 )
201 {
202 // first map the item to the one in the same column but in the last
203 // row
204 item += count;
205
206 // now there are 2 cases: either it is the first item of the last
207 // row in which case we need to wrap again and get to the last item
208 // or we can just go to the previous item
209 if ( item % (horz ? numCols : numRows) )
210 item--;
211 else
212 item = count - 1;
213 }
214 else if ( item >= count )
215 {
216 // same logic as above
217 item -= count;
218
219 // ... except that we need to check if this is not the last item,
220 // not the first one
221 if ( (item + 1) % (horz ? numCols : numRows) )
222 item++;
223 else
224 item = 0;
225 }
226
227 wxASSERT_MSG( item < count && item >= 0,
228 wxT("logic error in wxRadioBox::GetNextItem()") );
229 }
230 // we shouldn't select the non-active items, continue looking for a
231 // visible and shown one unless we came back to the item we started from in
232 // which case bail out to avoid infinite loop
233 while ( !(IsItemShown(item) && IsItemEnabled(item)) && item != itemStart );
234
235 return item;
236 }
237
238 #if wxUSE_TOOLTIPS
239
240 void wxRadioBoxBase::SetItemToolTip(unsigned int item, const wxString& text)
241 {
242 wxASSERT_MSG( item < GetCount(), wxT("Invalid item index") );
243
244 // extend the array to have entries for all our items on first use
245 if ( !m_itemsTooltips )
246 {
247 m_itemsTooltips = new wxToolTipArray;
248 m_itemsTooltips->resize(GetCount());
249 }
250
251 wxToolTip *tooltip = (*m_itemsTooltips)[item];
252
253 bool changed = true;
254 if ( text.empty() )
255 {
256 if ( tooltip )
257 {
258 // delete the tooltip
259 wxDELETE(tooltip);
260 }
261 else // nothing to do
262 {
263 changed = false;
264 }
265 }
266 else // non empty tooltip text
267 {
268 if ( tooltip )
269 {
270 // just change the existing tooltip text, don't change the tooltip
271 tooltip->SetTip(text);
272 changed = false;
273 }
274 else // no tooltip yet
275 {
276 // create the new one
277 tooltip = new wxToolTip(text);
278 }
279 }
280
281 if ( changed )
282 {
283 (*m_itemsTooltips)[item] = tooltip;
284 DoSetItemToolTip(item, tooltip);
285 }
286 }
287
288 void
289 wxRadioBoxBase::DoSetItemToolTip(unsigned int WXUNUSED(item),
290 wxToolTip * WXUNUSED(tooltip))
291 {
292 // per-item tooltips not implemented by default
293 }
294
295 #endif // wxUSE_TOOLTIPS
296
297 wxRadioBoxBase::~wxRadioBoxBase()
298 {
299 #if wxUSE_TOOLTIPS
300 if ( m_itemsTooltips )
301 {
302 const size_t n = m_itemsTooltips->size();
303 for ( size_t i = 0; i < n; i++ )
304 delete (*m_itemsTooltips)[i];
305
306 delete m_itemsTooltips;
307 }
308 #endif // wxUSE_TOOLTIPS
309 }
310
311 #if wxUSE_HELP
312
313 // set helptext for a particular item
314 void wxRadioBoxBase::SetItemHelpText(unsigned int n, const wxString& helpText)
315 {
316 wxCHECK_RET( n < GetCount(), wxT("Invalid item index") );
317
318 if ( m_itemsHelpTexts.empty() )
319 {
320 // once-only initialization of the array: reserve space for all items
321 m_itemsHelpTexts.Add(wxEmptyString, GetCount());
322 }
323
324 m_itemsHelpTexts[n] = helpText;
325 }
326
327 // retrieve helptext for a particular item
328 wxString wxRadioBoxBase::GetItemHelpText( unsigned int n ) const
329 {
330 wxCHECK_MSG( n < GetCount(), wxEmptyString, wxT("Invalid item index") );
331
332 return m_itemsHelpTexts.empty() ? wxString() : m_itemsHelpTexts[n];
333 }
334
335 // return help text for the item for which wxEVT_HELP was generated.
336 wxString wxRadioBoxBase::DoGetHelpTextAtPoint(const wxWindow *derived,
337 const wxPoint& pt,
338 wxHelpEvent::Origin origin) const
339 {
340 int item;
341 switch ( origin )
342 {
343 case wxHelpEvent::Origin_HelpButton:
344 item = GetItemFromPoint(pt);
345 break;
346
347 case wxHelpEvent::Origin_Keyboard:
348 item = GetSelection();
349 break;
350
351 default:
352 wxFAIL_MSG( "unknown help even origin" );
353 // fall through
354
355 case wxHelpEvent::Origin_Unknown:
356 // this value is used when we're called from GetHelpText() for the
357 // radio box itself, so don't return item-specific text in this case
358 item = wxNOT_FOUND;
359 }
360
361 if ( item != wxNOT_FOUND )
362 {
363 wxString text = GetItemHelpText(static_cast<unsigned int>(item));
364 if( !text.empty() )
365 return text;
366 }
367
368 return derived->wxWindowBase::GetHelpTextAtPoint(pt, origin);
369 }
370
371 #endif // wxUSE_HELP
372
373 #endif // wxUSE_RADIOBOX