]> git.saurik.com Git - wxWidgets.git/blob - src/common/ctrlcmn.cpp
build fixes for wxUSE_IPV6==1 under Windows
[wxWidgets.git] / src / common / ctrlcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/ctrlcmn.cpp
3 // Purpose: wxControl common interface
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 26.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
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_CONTROLS
28
29 #include "wx/control.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/dc.h"
33 #include "wx/log.h"
34 #include "wx/radiobut.h"
35 #include "wx/statbmp.h"
36 #include "wx/bitmap.h"
37 #include "wx/utils.h" // for wxStripMenuCodes()
38 #endif
39
40 const char wxControlNameStr[] = "control";
41
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 wxControlBase::~wxControlBase()
47 {
48 // this destructor is required for Darwin
49 }
50
51 bool wxControlBase::Create(wxWindow *parent,
52 wxWindowID id,
53 const wxPoint &pos,
54 const wxSize &size,
55 long style,
56 const wxValidator& wxVALIDATOR_PARAM(validator),
57 const wxString &name)
58 {
59 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
60
61 #if wxUSE_VALIDATORS
62 if ( ret )
63 SetValidator(validator);
64 #endif // wxUSE_VALIDATORS
65
66 return ret;
67 }
68
69 bool wxControlBase::CreateControl(wxWindowBase *parent,
70 wxWindowID id,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxValidator& validator,
75 const wxString& name)
76 {
77 // even if it's possible to create controls without parents in some port,
78 // it should surely be discouraged because it doesn't work at all under
79 // Windows
80 wxCHECK_MSG( parent, false, wxT("all controls must have parents") );
81
82 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
83 return false;
84
85 parent->AddChild(this);
86
87 return true;
88 }
89
90 /* static */
91 wxString wxControlBase::GetLabelText(const wxString& label)
92 {
93 // we don't want strip the TABs here, just the mnemonics
94 return wxStripMenuCodes(label, wxStrip_Mnemonics);
95 }
96
97 void wxControlBase::Command(wxCommandEvent& event)
98 {
99 (void)GetEventHandler()->ProcessEvent(event);
100 }
101
102 void wxControlBase::InitCommandEvent(wxCommandEvent& event) const
103 {
104 event.SetEventObject((wxControlBase *)this); // const_cast
105
106 // event.SetId(GetId()); -- this is usuall done in the event ctor
107
108 switch ( m_clientDataType )
109 {
110 case wxClientData_Void:
111 event.SetClientData(GetClientData());
112 break;
113
114 case wxClientData_Object:
115 event.SetClientObject(GetClientObject());
116 break;
117
118 case wxClientData_None:
119 // nothing to do
120 ;
121 }
122 }
123
124 bool wxControlBase::SetFont(const wxFont& font)
125 {
126 InvalidateBestSize();
127 return wxWindow::SetFont(font);
128 }
129
130 // wxControl-specific processing after processing the update event
131 void wxControlBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
132 {
133 // call inherited
134 wxWindowBase::DoUpdateWindowUI(event);
135
136 // update label
137 if ( event.GetSetText() )
138 {
139 if ( event.GetText() != GetLabel() )
140 SetLabel(event.GetText());
141 }
142
143 // Unfortunately we don't yet have common base class for
144 // wxRadioButton, so we handle updates of radiobuttons here.
145 // TODO: If once wxRadioButtonBase will exist, move this code there.
146 #if wxUSE_RADIOBTN
147 if ( event.GetSetChecked() )
148 {
149 wxRadioButton *radiobtn = wxDynamicCastThis(wxRadioButton);
150 if ( radiobtn )
151 radiobtn->SetValue(event.GetChecked());
152 }
153 #endif // wxUSE_RADIOBTN
154 }
155
156 /* static */
157 wxString wxControlBase::RemoveMnemonics(const wxString& str)
158 {
159 return wxStripMenuCodes(str, wxStrip_Mnemonics);
160 }
161
162 /* static */
163 int wxControlBase::FindAccelIndex(const wxString& label, wxString *labelOnly)
164 {
165 // the character following MNEMONIC_PREFIX is the accelerator for this
166 // control unless it is MNEMONIC_PREFIX too - this allows to insert
167 // literal MNEMONIC_PREFIX chars into the label
168 static const wxChar MNEMONIC_PREFIX = _T('&');
169
170 if ( labelOnly )
171 {
172 labelOnly->Empty();
173 labelOnly->Alloc(label.length());
174 }
175
176 int indexAccel = -1;
177 for ( wxString::const_iterator pc = label.begin(); pc != label.end(); ++pc )
178 {
179 if ( *pc == MNEMONIC_PREFIX )
180 {
181 ++pc; // skip it
182 if ( pc == label.end() )
183 break;
184 else if ( *pc != MNEMONIC_PREFIX )
185 {
186 if ( indexAccel == -1 )
187 {
188 // remember it (-1 is for MNEMONIC_PREFIX itself
189 indexAccel = pc - label.begin() - 1;
190 }
191 else
192 {
193 wxFAIL_MSG(_T("duplicate accel char in control label"));
194 }
195 }
196 }
197
198 if ( labelOnly )
199 {
200 *labelOnly += *pc;
201 }
202 }
203
204 return indexAccel;
205 }
206
207 #define wxELLIPSE_REPLACEMENT wxT("...")
208
209 /* static */
210 wxString wxControlBase::Ellipsize(const wxString& label, const wxDC& dc,
211 wxEllipsizeMode mode, int maxFinalWidth)
212 {
213 wxArrayInt charOffsets;
214 wxString ret;
215
216 // these cannot be cached as they can change because of e.g. a font change
217 int replacementWidth = dc.GetTextExtent(wxELLIPSE_REPLACEMENT).GetWidth();
218 int marginWidth = dc.GetCharWidth()*2;
219
220 // NB: we must handle correctly labels with newlines:
221 wxString curLine;
222 wxSize reqsize;
223 size_t len;
224 for ( wxString::const_iterator pc = label.begin(); ; ++pc )
225 {
226 if ( pc == label.end() || *pc == _T('\n') )
227 {
228 len = curLine.length();
229 if (len > 0 &&
230 dc.GetPartialTextExtents(curLine, charOffsets))
231 {
232 wxASSERT(charOffsets.GetCount() == len);
233
234 size_t totalWidth = charOffsets.Last();
235 if ( totalWidth > (size_t)maxFinalWidth )
236 {
237 // we need to ellipsize this row
238 int excessPixels = totalWidth - maxFinalWidth +
239 replacementWidth +
240 marginWidth; // security margin (NEEDED!)
241
242 // remove characters in excess
243 size_t initialChar, // index of first char to erase
244 nChars; // how many chars do we need to erase?
245 if (mode == wxELLIPSIZE_START)
246 {
247 initialChar = 0;
248 for (nChars=0;
249 nChars < len && charOffsets[nChars] < excessPixels;
250 nChars++)
251 ;
252 }
253 else if (mode == wxELLIPSIZE_MIDDLE)
254 {
255 // the start & end of the removed span of chars
256 initialChar = len/2;
257 size_t endChar = len/2;
258
259 int removed = 0;
260 for ( ; removed < excessPixels; )
261 {
262 if (initialChar > 0)
263 {
264 // width of the initialChar-th character
265 int width = charOffsets[initialChar] -
266 charOffsets[initialChar-1];
267
268 // remove the initialChar-th character
269 removed += width;
270 initialChar--;
271 }
272
273 if (endChar < len - 1 &&
274 removed < excessPixels)
275 {
276 // width of the (endChar+1)-th character
277 int width = charOffsets[endChar+1] -
278 charOffsets[endChar];
279
280 // remove the endChar-th character
281 removed += width;
282 endChar++;
283 }
284
285 if (initialChar == 0 && endChar == len-1)
286 {
287 nChars = len+1;
288 break;
289 }
290 }
291
292 initialChar++;
293 nChars = endChar - initialChar + 1;
294 }
295 else
296 {
297 wxASSERT(mode == wxELLIPSIZE_END);
298 wxASSERT(len > 0);
299
300 int maxWidth = totalWidth - excessPixels;
301 for (initialChar=0;
302 initialChar < len &&
303 charOffsets[initialChar] < maxWidth;
304 initialChar++)
305 ;
306
307 if (initialChar == 0)
308 {
309 nChars = len;
310 }
311 else
312 {
313 initialChar--; // go back one character
314 nChars = len - initialChar;
315 }
316 }
317
318 if (nChars > len)
319 {
320 // need to remove the entire row!
321 curLine.clear();
322 }
323 else
324 {
325 // erase nChars characters after initialChar (included):
326 curLine.erase(initialChar, nChars+1);
327
328 // if there is space for the replacement dots, add them
329 if (maxFinalWidth > replacementWidth)
330 curLine.insert(initialChar, wxELLIPSE_REPLACEMENT);
331 }
332
333 // if everything was ok, we should have shortened this line
334 // enough to make it fit in maxFinalWidth:
335 wxASSERT(dc.GetTextExtent(curLine).GetWidth() < maxFinalWidth);
336 }
337 }
338
339 // add this (ellipsized) row to the rest of the label
340 ret << curLine;
341 if ( pc == label.end() )
342 {
343 // NOTE: this is the return which always exits the function
344 return ret;
345 }
346 else
347 {
348 ret << *pc;
349 curLine.clear();
350 }
351 }
352 // we need to remove mnemonics from the label for correct calculations
353 else if ( *pc == _T('&') )
354 {
355 // pc+1 is safe: at worst we'll be at end()
356 wxString::const_iterator next = pc + 1;
357 if ( next != label.end() && *next == _T('&') )
358 curLine += _T('&'); // && becomes &
359 //else: remove this ampersand
360 }
361 // we need also to expand tabs to properly calc their size
362 else if ( *pc == _T('\t') )
363 {
364 // Windows natively expands the TABs to 6 spaces. Do the same:
365 curLine += wxT(" ");
366 }
367 else
368 {
369 curLine += *pc;
370 }
371 }
372
373 // this return would generate a
374 // warning C4702: unreachable code
375 // with MSVC since the function always exits from inside the loop
376 //return ret;
377 }
378
379 wxBorder wxControlBase::GetDefaultBorder() const
380 {
381 return wxBORDER_THEME;
382 }
383
384
385 // ----------------------------------------------------------------------------
386 // wxStaticBitmap
387 // ----------------------------------------------------------------------------
388
389 #if wxUSE_STATBMP
390
391 wxStaticBitmapBase::~wxStaticBitmapBase()
392 {
393 // this destructor is required for Darwin
394 }
395
396 wxSize wxStaticBitmapBase::DoGetBestSize() const
397 {
398 wxSize best;
399 wxBitmap bmp = GetBitmap();
400 if ( bmp.Ok() )
401 best = wxSize(bmp.GetWidth(), bmp.GetHeight());
402 else
403 // this is completely arbitrary
404 best = wxSize(16, 16);
405 CacheBestSize(best);
406 return best;
407 }
408
409 #endif // wxUSE_STATBMP
410
411 #endif // wxUSE_CONTROLS