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