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