]> git.saurik.com Git - wxWidgets.git/blob - src/common/radiocmn.cpp
use WX_ASSERT_STR/SIZET_EQUAL instead of CPPUNIT_ASSERT_EQUAL with casts
[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 // License: 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, _T("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 int count = GetCount(),
68 numCols = GetColumnCount(),
69 numRows = GetRowCount();
70
71 bool horz = (style & wxRA_SPECIFY_COLS) != 0;
72
73 switch ( dir )
74 {
75 case wxUP:
76 if ( horz )
77 {
78 item -= numCols;
79 }
80 else // vertical layout
81 {
82 if ( !item-- )
83 item = count - 1;
84 }
85 break;
86
87 case wxLEFT:
88 if ( horz )
89 {
90 if ( !item-- )
91 item = count - 1;
92 }
93 else // vertical layout
94 {
95 item -= numRows;
96 }
97 break;
98
99 case wxDOWN:
100 if ( horz )
101 {
102 item += numCols;
103 }
104 else // vertical layout
105 {
106 if ( ++item == count )
107 item = 0;
108 }
109 break;
110
111 case wxRIGHT:
112 if ( horz )
113 {
114 if ( ++item == count )
115 item = 0;
116 }
117 else // vertical layout
118 {
119 item += numRows;
120 }
121 break;
122
123 default:
124 wxFAIL_MSG( _T("unexpected wxDirection value") );
125 return wxNOT_FOUND;
126 }
127
128 // ensure that the item is in range [0..count)
129 if ( item < 0 )
130 {
131 // first map the item to the one in the same column but in the last row
132 item += count;
133
134 // now there are 2 cases: either it is the first item of the last row
135 // in which case we need to wrap again and get to the last item or we
136 // can just go to the previous item
137 if ( item % (horz ? numCols : numRows) )
138 item--;
139 else
140 item = count - 1;
141 }
142 else if ( item >= count )
143 {
144 // same logic as above
145 item -= count;
146
147 // ... except that we need to check if this is not the last item, not
148 // the first one
149 if ( (item + 1) % (horz ? numCols : numRows) )
150 item++;
151 else
152 item = 0;
153 }
154
155 wxASSERT_MSG( item < count && item >= 0,
156 _T("logic error in wxRadioBox::GetNextItem()") );
157
158 return item;
159 }
160
161 #if wxUSE_TOOLTIPS
162
163 void wxRadioBoxBase::SetItemToolTip(unsigned int item, const wxString& text)
164 {
165 wxASSERT_MSG( item < GetCount(), _T("Invalid item index") );
166
167 // extend the array to have entries for all our items on first use
168 if ( !m_itemsTooltips )
169 {
170 m_itemsTooltips = new wxToolTipArray;
171 m_itemsTooltips->resize(GetCount());
172 }
173
174 wxToolTip *tooltip = (*m_itemsTooltips)[item];
175
176 bool changed = true;
177 if ( text.empty() )
178 {
179 if ( tooltip )
180 {
181 // delete the tooltip
182 delete tooltip;
183 tooltip = NULL;
184 }
185 else // nothing to do
186 {
187 changed = false;
188 }
189 }
190 else // non empty tooltip text
191 {
192 if ( tooltip )
193 {
194 // just change the existing tooltip text, don't change the tooltip
195 tooltip->SetTip(text);
196 changed = false;
197 }
198 else // no tooltip yet
199 {
200 // create the new one
201 tooltip = new wxToolTip(text);
202 }
203 }
204
205 if ( changed )
206 {
207 (*m_itemsTooltips)[item] = tooltip;
208 DoSetItemToolTip(item, tooltip);
209 }
210 }
211
212 void
213 wxRadioBoxBase::DoSetItemToolTip(unsigned int WXUNUSED(item),
214 wxToolTip * WXUNUSED(tooltip))
215 {
216 // per-item tooltips not implemented by default
217 }
218
219 #endif // wxUSE_TOOLTIPS
220
221 wxRadioBoxBase::~wxRadioBoxBase()
222 {
223 #if wxUSE_TOOLTIPS
224 if ( m_itemsTooltips )
225 {
226 const size_t n = m_itemsTooltips->size();
227 for ( size_t i = 0; i < n; i++ )
228 delete (*m_itemsTooltips)[i];
229
230 delete m_itemsTooltips;
231 }
232 #endif // wxUSE_TOOLTIPS
233 }
234
235 #if wxUSE_HELP
236
237 // set helptext for a particular item
238 void wxRadioBoxBase::SetItemHelpText(unsigned int n, const wxString& helpText)
239 {
240 wxCHECK_RET( n < GetCount(), _T("Invalid item index") );
241
242 if ( m_itemsHelpTexts.empty() )
243 {
244 // once-only initialization of the array: reserve space for all items
245 m_itemsHelpTexts.Add(wxEmptyString, GetCount());
246 }
247
248 m_itemsHelpTexts[n] = helpText;
249 }
250
251 // retrieve helptext for a particular item
252 wxString wxRadioBoxBase::GetItemHelpText( unsigned int n ) const
253 {
254 wxCHECK_MSG( n < GetCount(), wxEmptyString, _T("Invalid item index") );
255
256 return m_itemsHelpTexts.empty() ? wxString() : m_itemsHelpTexts[n];
257 }
258
259 // return help text for the item for which wxEVT_HELP was generated.
260 wxString wxRadioBoxBase::DoGetHelpTextAtPoint(const wxWindow *derived,
261 const wxPoint& pt,
262 wxHelpEvent::Origin origin) const
263 {
264 const int item = origin == wxHelpEvent::Origin_HelpButton
265 ? GetItemFromPoint(pt)
266 : GetSelection();
267
268 if ( item != wxNOT_FOUND )
269 {
270 wxString text = GetItemHelpText(wx_static_cast(unsigned int, item));
271 if( !text.empty() )
272 return text;
273 }
274
275 return derived->wxWindowBase::GetHelpTextAtPoint(pt, origin);
276 }
277
278 #endif // wxUSE_HELP
279
280 #endif // wxUSE_RADIOBOX