]> git.saurik.com Git - wxWidgets.git/blame - src/common/ctrlcmn.cpp
remove wxSOCKET_MAX_EVENT, it is not really necessary and results in gcc warnings...
[wxWidgets.git] / src / common / ctrlcmn.cpp
CommitLineData
2d61b48d 1/////////////////////////////////////////////////////////////////////////////
0e2d2986 2// Name: src/common/ctrlcmn.cpp
2d61b48d
VZ
3// Purpose: wxControl common interface
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 26.07.99
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
2d61b48d
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2d61b48d
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
1e6feb95
VZ
27#if wxUSE_CONTROLS
28
93fbbe07
WS
29#include "wx/control.h"
30
2d61b48d 31#ifndef WX_PRECOMP
02b4f9fd 32 #include "wx/dc.h"
2d61b48d 33 #include "wx/log.h"
0e2d2986 34 #include "wx/radiobut.h"
e267406e 35 #include "wx/statbmp.h"
1e6feb95 36 #include "wx/bitmap.h"
74639764 37 #include "wx/utils.h" // for wxStripMenuCodes()
0bca0373 38#endif
1e6feb95 39
f36e602b 40const char wxControlNameStr[] = "control";
e7445ff8 41
2d61b48d
VZ
42// ============================================================================
43// implementation
44// ============================================================================
45
799ea011
GD
46wxControlBase::~wxControlBase()
47{
48 // this destructor is required for Darwin
49}
50
1e6feb95
VZ
51bool wxControlBase::Create(wxWindow *parent,
52 wxWindowID id,
53 const wxPoint &pos,
54 const wxSize &size,
55 long style,
ac8d0c11 56 const wxValidator& wxVALIDATOR_PARAM(validator),
1e6feb95
VZ
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
2d61b48d
VZ
69bool 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
c9d59ee7 80 wxCHECK_MSG( parent, false, wxT("all controls must have parents") );
2d61b48d
VZ
81
82 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
c9d59ee7 83 return false;
2d61b48d
VZ
84
85 parent->AddChild(this);
86
c9d59ee7 87 return true;
2d61b48d
VZ
88}
89
74639764
VZ
90/* static */
91wxString 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
2d61b48d
VZ
97void wxControlBase::Command(wxCommandEvent& event)
98{
bfbd6dc1 99 (void)GetEventHandler()->ProcessEvent(event);
2d61b48d 100}
bfbd6dc1
VZ
101
102void 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 {
1e6feb95 110 case wxClientData_Void:
bfbd6dc1
VZ
111 event.SetClientData(GetClientData());
112 break;
113
1e6feb95 114 case wxClientData_Object:
bfbd6dc1
VZ
115 event.SetClientObject(GetClientObject());
116 break;
117
1e6feb95 118 case wxClientData_None:
bfbd6dc1
VZ
119 // nothing to do
120 ;
121 }
122}
123
9f884528
RD
124bool wxControlBase::SetFont(const wxFont& font)
125{
126 InvalidateBestSize();
127 return wxWindow::SetFont(font);
128}
129
a3a4105d
VZ
130// wxControl-specific processing after processing the update event
131void 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
39bc0347
VZ
156/* static */
157wxString wxControlBase::RemoveMnemonics(const wxString& str)
158{
159 return wxStripMenuCodes(str, wxStrip_Mnemonics);
160}
161
916eabe6
VZ
162/* static */
163int 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
5c87527c
FM
207#define wxELLIPSE_REPLACEMENT wxT("...")
208
209/* static */
210wxString 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?
7d27b626 245 if (mode == wxELLIPSIZE_START)
5c87527c
FM
246 {
247 initialChar = 0;
248 for (nChars=0;
249 nChars < len && charOffsets[nChars] < excessPixels;
250 nChars++)
251 ;
252 }
7d27b626 253 else if (mode == wxELLIPSIZE_MIDDLE)
5c87527c
FM
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 {
7d27b626 297 wxASSERT(mode == wxELLIPSIZE_END);
5c87527c
FM
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
7d27b626 334 // enough to make it fit in maxFinalWidth:
5c87527c
FM
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 {
7d27b626 343 // NOTE: this is the return which always exits the function
5c87527c
FM
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
02b4f9fd 373 // this return would generate a
7d27b626 374 // warning C4702: unreachable code
02b4f9fd 375 // with MSVC since the function always exits from inside the loop
7d27b626 376 //return ret;
5c87527c
FM
377}
378
dc797d8e
JS
379wxBorder wxControlBase::GetDefaultBorder() const
380{
381 return wxBORDER_THEME;
382}
383
384
1e6feb95
VZ
385// ----------------------------------------------------------------------------
386// wxStaticBitmap
387// ----------------------------------------------------------------------------
388
389#if wxUSE_STATBMP
390
799ea011
GD
391wxStaticBitmapBase::~wxStaticBitmapBase()
392{
393 // this destructor is required for Darwin
394}
395
b3fcfa4d 396wxSize wxStaticBitmapBase::DoGetBestSize() const
1e6feb95 397{
9f884528 398 wxSize best;
1e6feb95
VZ
399 wxBitmap bmp = GetBitmap();
400 if ( bmp.Ok() )
9f884528
RD
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;
1e6feb95
VZ
407}
408
409#endif // wxUSE_STATBMP
410
411#endif // wxUSE_CONTROLS