]> git.saurik.com Git - wxWidgets.git/blob - src/common/ctrlcmn.cpp
Forgot this
[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 wxString wxControlBase::EscapeMnemonics(const wxString& text)
164 {
165 wxString label(text);
166 label.Replace("&", "&&");
167 return label;
168 }
169
170 /* static */
171 int wxControlBase::FindAccelIndex(const wxString& label, wxString *labelOnly)
172 {
173 // the character following MNEMONIC_PREFIX is the accelerator for this
174 // control unless it is MNEMONIC_PREFIX too - this allows to insert
175 // literal MNEMONIC_PREFIX chars into the label
176 static const wxChar MNEMONIC_PREFIX = _T('&');
177
178 if ( labelOnly )
179 {
180 labelOnly->Empty();
181 labelOnly->Alloc(label.length());
182 }
183
184 int indexAccel = -1;
185 for ( wxString::const_iterator pc = label.begin(); pc != label.end(); ++pc )
186 {
187 if ( *pc == MNEMONIC_PREFIX )
188 {
189 ++pc; // skip it
190 if ( pc == label.end() )
191 break;
192 else if ( *pc != MNEMONIC_PREFIX )
193 {
194 if ( indexAccel == -1 )
195 {
196 // remember it (-1 is for MNEMONIC_PREFIX itself
197 indexAccel = pc - label.begin() - 1;
198 }
199 else
200 {
201 wxFAIL_MSG(_T("duplicate accel char in control label"));
202 }
203 }
204 }
205
206 if ( labelOnly )
207 {
208 *labelOnly += *pc;
209 }
210 }
211
212 return indexAccel;
213 }
214
215 #define wxELLIPSE_REPLACEMENT wxT("...")
216
217 /* static */
218 wxString wxControlBase::Ellipsize(const wxString& label, const wxDC& dc,
219 wxEllipsizeMode mode, int maxFinalWidth)
220 {
221 wxArrayInt charOffsets;
222 wxString ret;
223
224 // these cannot be cached as they can change because of e.g. a font change
225 int replacementWidth = dc.GetTextExtent(wxELLIPSE_REPLACEMENT).GetWidth();
226 int marginWidth = dc.GetCharWidth()*2;
227
228 // NB: we must handle correctly labels with newlines:
229 wxString curLine;
230 wxSize reqsize;
231 size_t len;
232 for ( wxString::const_iterator pc = label.begin(); ; ++pc )
233 {
234 if ( pc == label.end() || *pc == _T('\n') )
235 {
236 len = curLine.length();
237 if (len > 0 &&
238 dc.GetPartialTextExtents(curLine, charOffsets))
239 {
240 wxASSERT(charOffsets.GetCount() == len);
241
242 size_t totalWidth = charOffsets.Last();
243 if ( totalWidth > (size_t)maxFinalWidth )
244 {
245 // we need to ellipsize this row
246 int excessPixels = totalWidth - maxFinalWidth +
247 replacementWidth +
248 marginWidth; // security margin (NEEDED!)
249
250 // remove characters in excess
251 size_t initialChar, // index of first char to erase
252 nChars; // how many chars do we need to erase?
253 if (mode == wxELLIPSIZE_START)
254 {
255 initialChar = 0;
256 for (nChars=0;
257 nChars < len && charOffsets[nChars] < excessPixels;
258 nChars++)
259 ;
260 }
261 else if (mode == wxELLIPSIZE_MIDDLE)
262 {
263 // the start & end of the removed span of chars
264 initialChar = len/2;
265 size_t endChar = len/2;
266
267 int removed = 0;
268 for ( ; removed < excessPixels; )
269 {
270 if (initialChar > 0)
271 {
272 // width of the initialChar-th character
273 int width = charOffsets[initialChar] -
274 charOffsets[initialChar-1];
275
276 // remove the initialChar-th character
277 removed += width;
278 initialChar--;
279 }
280
281 if (endChar < len - 1 &&
282 removed < excessPixels)
283 {
284 // width of the (endChar+1)-th character
285 int width = charOffsets[endChar+1] -
286 charOffsets[endChar];
287
288 // remove the endChar-th character
289 removed += width;
290 endChar++;
291 }
292
293 if (initialChar == 0 && endChar == len-1)
294 {
295 nChars = len+1;
296 break;
297 }
298 }
299
300 initialChar++;
301 nChars = endChar - initialChar + 1;
302 }
303 else
304 {
305 wxASSERT(mode == wxELLIPSIZE_END);
306 wxASSERT(len > 0);
307
308 int maxWidth = totalWidth - excessPixels;
309 for (initialChar=0;
310 initialChar < len &&
311 charOffsets[initialChar] < maxWidth;
312 initialChar++)
313 ;
314
315 if (initialChar == 0)
316 {
317 nChars = len;
318 }
319 else
320 {
321 initialChar--; // go back one character
322 nChars = len - initialChar;
323 }
324 }
325
326 if (nChars > len)
327 {
328 // need to remove the entire row!
329 curLine.clear();
330 }
331 else
332 {
333 // erase nChars characters after initialChar (included):
334 curLine.erase(initialChar, nChars+1);
335
336 // if there is space for the replacement dots, add them
337 if (maxFinalWidth > replacementWidth)
338 curLine.insert(initialChar, wxELLIPSE_REPLACEMENT);
339 }
340
341 // if everything was ok, we should have shortened this line
342 // enough to make it fit in maxFinalWidth:
343 wxASSERT(dc.GetTextExtent(curLine).GetWidth() < maxFinalWidth);
344 }
345 }
346
347 // add this (ellipsized) row to the rest of the label
348 ret << curLine;
349 if ( pc == label.end() )
350 {
351 // NOTE: this is the return which always exits the function
352 return ret;
353 }
354 else
355 {
356 ret << *pc;
357 curLine.clear();
358 }
359 }
360 // we need to remove mnemonics from the label for correct calculations
361 else if ( *pc == _T('&') )
362 {
363 // pc+1 is safe: at worst we'll be at end()
364 wxString::const_iterator next = pc + 1;
365 if ( next != label.end() && *next == _T('&') )
366 curLine += _T('&'); // && becomes &
367 //else: remove this ampersand
368 }
369 // we need also to expand tabs to properly calc their size
370 else if ( *pc == _T('\t') )
371 {
372 // Windows natively expands the TABs to 6 spaces. Do the same:
373 curLine += wxT(" ");
374 }
375 else
376 {
377 curLine += *pc;
378 }
379 }
380
381 // this return would generate a
382 // warning C4702: unreachable code
383 // with MSVC since the function always exits from inside the loop
384 //return ret;
385 }
386
387 wxBorder wxControlBase::GetDefaultBorder() const
388 {
389 return wxBORDER_THEME;
390 }
391
392
393 // ----------------------------------------------------------------------------
394 // wxStaticBitmap
395 // ----------------------------------------------------------------------------
396
397 #if wxUSE_STATBMP
398
399 wxStaticBitmapBase::~wxStaticBitmapBase()
400 {
401 // this destructor is required for Darwin
402 }
403
404 wxSize wxStaticBitmapBase::DoGetBestSize() const
405 {
406 wxSize best;
407 wxBitmap bmp = GetBitmap();
408 if ( bmp.Ok() )
409 best = wxSize(bmp.GetWidth(), bmp.GetHeight());
410 else
411 // this is completely arbitrary
412 best = wxSize(16, 16);
413 CacheBestSize(best);
414 return best;
415 }
416
417 #endif // wxUSE_STATBMP
418
419 #endif // wxUSE_CONTROLS