]> git.saurik.com Git - wxWidgets.git/blame - src/common/radiocmn.cpp
wxDataViewChoiceRenderer's editor control should have the same size as the cell.
[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
7// RCS-ID: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
526954c5 9// Licence: wxWindows licence
e421922f
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
e421922f
VZ
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
c670c855
VZ
33#if wxUSE_TOOLTIPS
34 #include "wx/tooltip.h"
35#endif // wxUSE_TOOLTIPS
36
dc26eeb3
VZ
37#if wxUSE_HELP
38 #include "wx/cshelp.h"
39#endif
40
f313deaa
PC
41extern WXDLLEXPORT_DATA(const char) wxRadioBoxNameStr[] = "radioBox";
42
e421922f
VZ
43// ============================================================================
44// implementation
45// ============================================================================
46
28953245
SC
47// ----------------------------------------------------------------------------
48// XTI
49// ----------------------------------------------------------------------------
50
51// TODO: wxCONSTRUCTOR
52#if 0 // wxUSE_EXTENDED_RTTI
53wxDEFINE_FLAGS( wxRadioBoxStyle )
54
55wxBEGIN_FLAGS( wxRadioBoxStyle )
56// new style border flags, we put them first to
57// use them for streaming out
58wxFLAGS_MEMBER(wxBORDER_SIMPLE)
59wxFLAGS_MEMBER(wxBORDER_SUNKEN)
60wxFLAGS_MEMBER(wxBORDER_DOUBLE)
61wxFLAGS_MEMBER(wxBORDER_RAISED)
62wxFLAGS_MEMBER(wxBORDER_STATIC)
63wxFLAGS_MEMBER(wxBORDER_NONE)
64
65// old style border flags
66wxFLAGS_MEMBER(wxSIMPLE_BORDER)
67wxFLAGS_MEMBER(wxSUNKEN_BORDER)
68wxFLAGS_MEMBER(wxDOUBLE_BORDER)
69wxFLAGS_MEMBER(wxRAISED_BORDER)
70wxFLAGS_MEMBER(wxSTATIC_BORDER)
71wxFLAGS_MEMBER(wxBORDER)
72
73// standard window styles
74wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
75wxFLAGS_MEMBER(wxCLIP_CHILDREN)
76wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
77wxFLAGS_MEMBER(wxWANTS_CHARS)
78wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
79wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
80wxFLAGS_MEMBER(wxVSCROLL)
81wxFLAGS_MEMBER(wxHSCROLL)
82
83wxFLAGS_MEMBER(wxRA_SPECIFY_COLS)
84wxFLAGS_MEMBER(wxRA_HORIZONTAL)
85wxFLAGS_MEMBER(wxRA_SPECIFY_ROWS)
86wxFLAGS_MEMBER(wxRA_VERTICAL)
87
88wxEND_FLAGS( wxRadioBoxStyle )
89
90IMPLEMENT_DYNAMIC_CLASS_XTI(wxRadioBox, wxControl,"wx/radiobox.h")
91
92wxBEGIN_PROPERTIES_TABLE(wxRadioBox)
93wxEVENT_PROPERTY( Select , wxEVT_COMMAND_RADIOBOX_SELECTED , wxCommandEvent )
94wxPROPERTY_FLAGS( WindowStyle , wxRadioBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
95wxEND_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
aa61d352 112void wxRadioBoxBase::SetMajorDim(unsigned int majorDim, long style)
21e0a4d5 113{
9a83f860 114 wxCHECK_RET( majorDim != 0, wxT("major radiobox dimension can't be 0") );
21e0a4d5
VZ
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
e421922f
VZ
132int wxRadioBoxBase::GetNextItem(int item, wxDirection dir, long style) const
133{
05d31b3a
VZ
134 const int itemStart = item;
135
e421922f
VZ
136 int count = GetCount(),
137 numCols = GetColumnCount(),
138 numRows = GetRowCount();
139
140 bool horz = (style & wxRA_SPECIFY_COLS) != 0;
141
05d31b3a 142 do
e421922f 143 {
05d31b3a
VZ
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:
9a83f860 195 wxFAIL_MSG( wxT("unexpected wxDirection value") );
05d31b3a
VZ
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,
9a83f860 228 wxT("logic error in wxRadioBox::GetNextItem()") );
e421922f 229 }
05d31b3a
VZ
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 );
e421922f
VZ
234
235 return item;
236}
237
c670c855
VZ
238#if wxUSE_TOOLTIPS
239
240void wxRadioBoxBase::SetItemToolTip(unsigned int item, const wxString& text)
241{
9a83f860 242 wxASSERT_MSG( item < GetCount(), wxT("Invalid item index") );
c670c855
VZ
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
5276b0a5 259 wxDELETE(tooltip);
c670c855
VZ
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
288void
289wxRadioBoxBase::DoSetItemToolTip(unsigned int WXUNUSED(item),
290 wxToolTip * WXUNUSED(tooltip))
291{
292 // per-item tooltips not implemented by default
293}
294
6337cacc
WS
295#endif // wxUSE_TOOLTIPS
296
c670c855
VZ
297wxRadioBoxBase::~wxRadioBoxBase()
298{
6337cacc 299#if wxUSE_TOOLTIPS
c670c855
VZ
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 }
c670c855 308#endif // wxUSE_TOOLTIPS
6337cacc 309}
c670c855 310
dc26eeb3
VZ
311#if wxUSE_HELP
312
313// set helptext for a particular item
314void wxRadioBoxBase::SetItemHelpText(unsigned int n, const wxString& helpText)
315{
9a83f860 316 wxCHECK_RET( n < GetCount(), wxT("Invalid item index") );
dc26eeb3
VZ
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
328wxString wxRadioBoxBase::GetItemHelpText( unsigned int n ) const
329{
9a83f860 330 wxCHECK_MSG( n < GetCount(), wxEmptyString, wxT("Invalid item index") );
dc26eeb3
VZ
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.
336wxString wxRadioBoxBase::DoGetHelpTextAtPoint(const wxWindow *derived,
337 const wxPoint& pt,
338 wxHelpEvent::Origin origin) const
339{
6eb872ea
VZ
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 }
dc26eeb3
VZ
360
361 if ( item != wxNOT_FOUND )
362 {
5c33522f 363 wxString text = GetItemHelpText(static_cast<unsigned int>(item));
dc26eeb3
VZ
364 if( !text.empty() )
365 return text;
366 }
367
368 return derived->wxWindowBase::GetHelpTextAtPoint(pt, origin);
369}
370
371#endif // wxUSE_HELP
372
e421922f 373#endif // wxUSE_RADIOBOX