]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/radiocmn.cpp
Improved wxDCPen/BrushChangers.
[wxWidgets.git] / src / common / radiocmn.cpp
... / ...
CommitLineData
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// ============================================================================
38// implementation
39// ============================================================================
40
41void wxRadioBoxBase::SetMajorDim(unsigned int majorDim, long style)
42{
43 wxCHECK_RET( majorDim != 0, _T("major radiobox dimension can't be 0") );
44
45 m_majorDim = majorDim;
46
47 int minorDim = (GetCount() + m_majorDim - 1) / m_majorDim;
48
49 if ( style & wxRA_SPECIFY_COLS )
50 {
51 m_numCols = majorDim;
52 m_numRows = minorDim;
53 }
54 else // wxRA_SPECIFY_ROWS
55 {
56 m_numCols = minorDim;
57 m_numRows = majorDim;
58 }
59}
60
61int wxRadioBoxBase::GetNextItem(int item, wxDirection dir, long style) const
62{
63 int count = GetCount(),
64 numCols = GetColumnCount(),
65 numRows = GetRowCount();
66
67 bool horz = (style & wxRA_SPECIFY_COLS) != 0;
68
69 switch ( dir )
70 {
71 case wxUP:
72 if ( horz )
73 {
74 item -= numCols;
75 }
76 else // vertical layout
77 {
78 if ( !item-- )
79 item = count - 1;
80 }
81 break;
82
83 case wxLEFT:
84 if ( horz )
85 {
86 if ( !item-- )
87 item = count - 1;
88 }
89 else // vertical layout
90 {
91 item -= numRows;
92 }
93 break;
94
95 case wxDOWN:
96 if ( horz )
97 {
98 item += numCols;
99 }
100 else // vertical layout
101 {
102 if ( ++item == count )
103 item = 0;
104 }
105 break;
106
107 case wxRIGHT:
108 if ( horz )
109 {
110 if ( ++item == count )
111 item = 0;
112 }
113 else // vertical layout
114 {
115 item += numRows;
116 }
117 break;
118
119 default:
120 wxFAIL_MSG( _T("unexpected wxDirection value") );
121 return wxNOT_FOUND;
122 }
123
124 // ensure that the item is in range [0..count)
125 if ( item < 0 )
126 {
127 // first map the item to the one in the same column but in the last row
128 item += count;
129
130 // now there are 2 cases: either it is the first item of the last row
131 // in which case we need to wrap again and get to the last item or we
132 // can just go to the previous item
133 if ( item % (horz ? numCols : numRows) )
134 item--;
135 else
136 item = count - 1;
137 }
138 else if ( item >= count )
139 {
140 // same logic as above
141 item -= count;
142
143 // ... except that we need to check if this is not the last item, not
144 // the first one
145 if ( (item + 1) % (horz ? numCols : numRows) )
146 item++;
147 else
148 item = 0;
149 }
150
151 wxASSERT_MSG( item < count && item >= 0,
152 _T("logic error in wxRadioBox::GetNextItem()") );
153
154 return item;
155}
156
157#if wxUSE_TOOLTIPS
158
159void wxRadioBoxBase::SetItemToolTip(unsigned int item, const wxString& text)
160{
161 wxASSERT_MSG( item < GetCount(), _T("Invalid item index") );
162
163 // extend the array to have entries for all our items on first use
164 if ( !m_itemsTooltips )
165 {
166 m_itemsTooltips = new wxToolTipArray;
167 m_itemsTooltips->resize(GetCount());
168 }
169
170 wxToolTip *tooltip = (*m_itemsTooltips)[item];
171
172 bool changed = true;
173 if ( text.empty() )
174 {
175 if ( tooltip )
176 {
177 // delete the tooltip
178 delete tooltip;
179 tooltip = NULL;
180 }
181 else // nothing to do
182 {
183 changed = false;
184 }
185 }
186 else // non empty tooltip text
187 {
188 if ( tooltip )
189 {
190 // just change the existing tooltip text, don't change the tooltip
191 tooltip->SetTip(text);
192 changed = false;
193 }
194 else // no tooltip yet
195 {
196 // create the new one
197 tooltip = new wxToolTip(text);
198 }
199 }
200
201 if ( changed )
202 {
203 (*m_itemsTooltips)[item] = tooltip;
204 DoSetItemToolTip(item, tooltip);
205 }
206}
207
208void
209wxRadioBoxBase::DoSetItemToolTip(unsigned int WXUNUSED(item),
210 wxToolTip * WXUNUSED(tooltip))
211{
212 // per-item tooltips not implemented by default
213}
214
215#endif // wxUSE_TOOLTIPS
216
217wxRadioBoxBase::~wxRadioBoxBase()
218{
219#if wxUSE_TOOLTIPS
220 if ( m_itemsTooltips )
221 {
222 const size_t n = m_itemsTooltips->size();
223 for ( size_t i = 0; i < n; i++ )
224 delete (*m_itemsTooltips)[i];
225
226 delete m_itemsTooltips;
227 }
228#endif // wxUSE_TOOLTIPS
229}
230
231#if WXWIN_COMPATIBILITY_2_4
232
233// these functions are deprecated and don't do anything
234int wxRadioBoxBase::GetNumberOfRowsOrCols() const
235{
236 return 1;
237}
238
239void wxRadioBoxBase::SetNumberOfRowsOrCols(int WXUNUSED(n))
240{
241}
242
243#endif // WXWIN_COMPATIBILITY_2_4
244
245#endif // wxUSE_RADIOBOX