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