]> git.saurik.com Git - wxWidgets.git/blob - src/common/radiocmn.cpp
Add another test for the insertion point position after SetValue().
[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 // ============================================================================
42 // implementation
43 // ============================================================================
44
45 void wxRadioBoxBase::SetMajorDim(unsigned int majorDim, long style)
46 {
47 wxCHECK_RET( majorDim != 0, wxT("major radiobox dimension can't be 0") );
48
49 m_majorDim = majorDim;
50
51 int minorDim = (GetCount() + m_majorDim - 1) / m_majorDim;
52
53 if ( style & wxRA_SPECIFY_COLS )
54 {
55 m_numCols = majorDim;
56 m_numRows = minorDim;
57 }
58 else // wxRA_SPECIFY_ROWS
59 {
60 m_numCols = minorDim;
61 m_numRows = majorDim;
62 }
63 }
64
65 int wxRadioBoxBase::GetNextItem(int item, wxDirection dir, long style) const
66 {
67 const int itemStart = item;
68
69 int count = GetCount(),
70 numCols = GetColumnCount(),
71 numRows = GetRowCount();
72
73 bool horz = (style & wxRA_SPECIFY_COLS) != 0;
74
75 do
76 {
77 switch ( dir )
78 {
79 case wxUP:
80 if ( horz )
81 {
82 item -= numCols;
83 }
84 else // vertical layout
85 {
86 if ( !item-- )
87 item = count - 1;
88 }
89 break;
90
91 case wxLEFT:
92 if ( horz )
93 {
94 if ( !item-- )
95 item = count - 1;
96 }
97 else // vertical layout
98 {
99 item -= numRows;
100 }
101 break;
102
103 case wxDOWN:
104 if ( horz )
105 {
106 item += numCols;
107 }
108 else // vertical layout
109 {
110 if ( ++item == count )
111 item = 0;
112 }
113 break;
114
115 case wxRIGHT:
116 if ( horz )
117 {
118 if ( ++item == count )
119 item = 0;
120 }
121 else // vertical layout
122 {
123 item += numRows;
124 }
125 break;
126
127 default:
128 wxFAIL_MSG( wxT("unexpected wxDirection value") );
129 return wxNOT_FOUND;
130 }
131
132 // ensure that the item is in range [0..count)
133 if ( item < 0 )
134 {
135 // first map the item to the one in the same column but in the last
136 // row
137 item += count;
138
139 // now there are 2 cases: either it is the first item of the last
140 // row in which case we need to wrap again and get to the last item
141 // or we can just go to the previous item
142 if ( item % (horz ? numCols : numRows) )
143 item--;
144 else
145 item = count - 1;
146 }
147 else if ( item >= count )
148 {
149 // same logic as above
150 item -= count;
151
152 // ... except that we need to check if this is not the last item,
153 // not the first one
154 if ( (item + 1) % (horz ? numCols : numRows) )
155 item++;
156 else
157 item = 0;
158 }
159
160 wxASSERT_MSG( item < count && item >= 0,
161 wxT("logic error in wxRadioBox::GetNextItem()") );
162 }
163 // we shouldn't select the non-active items, continue looking for a
164 // visible and shown one unless we came back to the item we started from in
165 // which case bail out to avoid infinite loop
166 while ( !(IsItemShown(item) && IsItemEnabled(item)) && item != itemStart );
167
168 return item;
169 }
170
171 #if wxUSE_TOOLTIPS
172
173 void wxRadioBoxBase::SetItemToolTip(unsigned int item, const wxString& text)
174 {
175 wxASSERT_MSG( item < GetCount(), wxT("Invalid item index") );
176
177 // extend the array to have entries for all our items on first use
178 if ( !m_itemsTooltips )
179 {
180 m_itemsTooltips = new wxToolTipArray;
181 m_itemsTooltips->resize(GetCount());
182 }
183
184 wxToolTip *tooltip = (*m_itemsTooltips)[item];
185
186 bool changed = true;
187 if ( text.empty() )
188 {
189 if ( tooltip )
190 {
191 // delete the tooltip
192 wxDELETE(tooltip);
193 }
194 else // nothing to do
195 {
196 changed = false;
197 }
198 }
199 else // non empty tooltip text
200 {
201 if ( tooltip )
202 {
203 // just change the existing tooltip text, don't change the tooltip
204 tooltip->SetTip(text);
205 changed = false;
206 }
207 else // no tooltip yet
208 {
209 // create the new one
210 tooltip = new wxToolTip(text);
211 }
212 }
213
214 if ( changed )
215 {
216 (*m_itemsTooltips)[item] = tooltip;
217 DoSetItemToolTip(item, tooltip);
218 }
219 }
220
221 void
222 wxRadioBoxBase::DoSetItemToolTip(unsigned int WXUNUSED(item),
223 wxToolTip * WXUNUSED(tooltip))
224 {
225 // per-item tooltips not implemented by default
226 }
227
228 #endif // wxUSE_TOOLTIPS
229
230 wxRadioBoxBase::~wxRadioBoxBase()
231 {
232 #if wxUSE_TOOLTIPS
233 if ( m_itemsTooltips )
234 {
235 const size_t n = m_itemsTooltips->size();
236 for ( size_t i = 0; i < n; i++ )
237 delete (*m_itemsTooltips)[i];
238
239 delete m_itemsTooltips;
240 }
241 #endif // wxUSE_TOOLTIPS
242 }
243
244 #if wxUSE_HELP
245
246 // set helptext for a particular item
247 void wxRadioBoxBase::SetItemHelpText(unsigned int n, const wxString& helpText)
248 {
249 wxCHECK_RET( n < GetCount(), wxT("Invalid item index") );
250
251 if ( m_itemsHelpTexts.empty() )
252 {
253 // once-only initialization of the array: reserve space for all items
254 m_itemsHelpTexts.Add(wxEmptyString, GetCount());
255 }
256
257 m_itemsHelpTexts[n] = helpText;
258 }
259
260 // retrieve helptext for a particular item
261 wxString wxRadioBoxBase::GetItemHelpText( unsigned int n ) const
262 {
263 wxCHECK_MSG( n < GetCount(), wxEmptyString, wxT("Invalid item index") );
264
265 return m_itemsHelpTexts.empty() ? wxString() : m_itemsHelpTexts[n];
266 }
267
268 // return help text for the item for which wxEVT_HELP was generated.
269 wxString wxRadioBoxBase::DoGetHelpTextAtPoint(const wxWindow *derived,
270 const wxPoint& pt,
271 wxHelpEvent::Origin origin) const
272 {
273 int item;
274 switch ( origin )
275 {
276 case wxHelpEvent::Origin_HelpButton:
277 item = GetItemFromPoint(pt);
278 break;
279
280 case wxHelpEvent::Origin_Keyboard:
281 item = GetSelection();
282 break;
283
284 default:
285 wxFAIL_MSG( "unknown help even origin" );
286 // fall through
287
288 case wxHelpEvent::Origin_Unknown:
289 // this value is used when we're called from GetHelpText() for the
290 // radio box itself, so don't return item-specific text in this case
291 item = wxNOT_FOUND;
292 }
293
294 if ( item != wxNOT_FOUND )
295 {
296 wxString text = GetItemHelpText(static_cast<unsigned int>(item));
297 if( !text.empty() )
298 return text;
299 }
300
301 return derived->wxWindowBase::GetHelpTextAtPoint(pt, origin);
302 }
303
304 #endif // wxUSE_HELP
305
306 #endif // wxUSE_RADIOBOX