1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/ctrlcmn.cpp
3 // Purpose: wxControl common interface
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/control.h"
34 #include "wx/radiobut.h"
35 #include "wx/statbmp.h"
36 #include "wx/bitmap.h"
37 #include "wx/utils.h" // for wxStripMenuCodes()
40 const char wxControlNameStr
[] = "control";
42 // ============================================================================
44 // ============================================================================
46 wxControlBase::~wxControlBase()
48 // this destructor is required for Darwin
51 bool wxControlBase::Create(wxWindow
*parent
,
56 const wxValidator
& wxVALIDATOR_PARAM(validator
),
59 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
63 SetValidator(validator
);
64 #endif // wxUSE_VALIDATORS
69 bool wxControlBase::CreateControl(wxWindowBase
*parent
,
74 const wxValidator
& validator
,
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
80 wxCHECK_MSG( parent
, false, wxT("all controls must have parents") );
82 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
85 parent
->AddChild(this);
91 wxString
wxControlBase::GetLabelText(const wxString
& label
)
93 // we don't want strip the TABs here, just the mnemonics
94 return wxStripMenuCodes(label
, wxStrip_Mnemonics
);
97 void wxControlBase::Command(wxCommandEvent
& event
)
99 (void)GetEventHandler()->ProcessEvent(event
);
102 void wxControlBase::InitCommandEvent(wxCommandEvent
& event
) const
104 event
.SetEventObject((wxControlBase
*)this); // const_cast
106 // event.SetId(GetId()); -- this is usuall done in the event ctor
108 switch ( m_clientDataType
)
110 case wxClientData_Void
:
111 event
.SetClientData(GetClientData());
114 case wxClientData_Object
:
115 event
.SetClientObject(GetClientObject());
118 case wxClientData_None
:
124 bool wxControlBase::SetFont(const wxFont
& font
)
126 InvalidateBestSize();
127 return wxWindow::SetFont(font
);
130 // wxControl-specific processing after processing the update event
131 void wxControlBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
134 wxWindowBase::DoUpdateWindowUI(event
);
137 if ( event
.GetSetText() )
139 if ( event
.GetText() != GetLabel() )
140 SetLabel(event
.GetText());
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.
147 if ( event
.GetSetChecked() )
149 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
151 radiobtn
->SetValue(event
.GetChecked());
153 #endif // wxUSE_RADIOBTN
157 wxString
wxControlBase::RemoveMnemonics(const wxString
& str
)
159 return wxStripMenuCodes(str
, wxStrip_Mnemonics
);
163 wxString
wxControlBase::EscapeMnemonics(const wxString
& text
)
165 wxString
label(text
);
166 label
.Replace("&", "&&");
171 int wxControlBase::FindAccelIndex(const wxString
& label
, wxString
*labelOnly
)
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('&');
181 labelOnly
->Alloc(label
.length());
185 for ( wxString::const_iterator pc
= label
.begin(); pc
!= label
.end(); ++pc
)
187 if ( *pc
== MNEMONIC_PREFIX
)
190 if ( pc
== label
.end() )
192 else if ( *pc
!= MNEMONIC_PREFIX
)
194 if ( indexAccel
== -1 )
196 // remember it (-1 is for MNEMONIC_PREFIX itself
197 indexAccel
= pc
- label
.begin() - 1;
201 wxFAIL_MSG(_T("duplicate accel char in control label"));
215 wxBorder
wxControlBase::GetDefaultBorder() const
217 return wxBORDER_THEME
;
220 // ----------------------------------------------------------------------------
221 // wxControlBase - ellipsization code
222 // ----------------------------------------------------------------------------
224 #define wxELLIPSE_REPLACEMENT wxT("...")
226 /* static and protected */
227 wxString
wxControlBase::DoEllipsizeSingleLine(const wxString
& curLine
, const wxDC
& dc
,
228 wxEllipsizeMode mode
, int maxFinalWidth
,
229 int replacementWidth
, int marginWidth
)
231 wxASSERT_MSG(replacementWidth
> 0 && marginWidth
> 0,
232 "Invalid parameters");
233 wxASSERT_MSG(!curLine
.Contains('\n'),
234 "Use Ellipsize() instead!");
236 // NOTE: this function assumes that any mnemonic/tab character has already
237 // been handled if it was necessary to handle them (see Ellipsize())
239 if (maxFinalWidth
<= 0)
240 return wxEmptyString
;
242 wxArrayInt charOffsets
;
243 size_t len
= curLine
.length();
245 !dc
.GetPartialTextExtents(curLine
, charOffsets
))
248 wxASSERT(charOffsets
.GetCount() == len
);
250 size_t totalWidth
= charOffsets
.Last();
251 if ( totalWidth
<= (size_t)maxFinalWidth
)
252 return curLine
; // we don't need to do any ellipsization!
254 int excessPixels
= totalWidth
- maxFinalWidth
+
256 marginWidth
; // security margin (NEEDED!)
257 wxASSERT(excessPixels
>0);
259 // remove characters in excess
260 size_t initialChar
, // index of first char to erase
261 nChars
; // how many chars do we need to erase?
265 case wxELLIPSIZE_START
:
268 nChars
< len
&& charOffsets
[nChars
] < excessPixels
;
273 case wxELLIPSIZE_MIDDLE
:
275 // the start & end of the removed span of chars
277 size_t endChar
= len
/2;
280 for ( ; removed
< excessPixels
; )
284 // width of the initialChar-th character
285 int width
= charOffsets
[initialChar
] -
286 charOffsets
[initialChar
-1];
288 // remove the initialChar-th character
293 if (endChar
< len
- 1 &&
294 removed
< excessPixels
)
296 // width of the (endChar+1)-th character
297 int width
= charOffsets
[endChar
+1] -
298 charOffsets
[endChar
];
300 // remove the endChar-th character
305 if (initialChar
== 0 && endChar
== len
-1)
313 nChars
= endChar
- initialChar
+ 1;
317 case wxELLIPSIZE_END
:
321 int maxWidth
= totalWidth
- excessPixels
;
322 for ( initialChar
= 0;
323 initialChar
< len
&& charOffsets
[initialChar
] < maxWidth
;
327 if (initialChar
== 0)
333 //initialChar--; // go back one character
334 nChars
= len
- initialChar
;
340 wxFAIL_MSG("invalid ellipsize mode");
344 wxString
ret(curLine
);
347 // need to remove the entire row!
352 // erase nChars characters after initialChar (included):
353 ret
.erase(initialChar
, nChars
+1);
355 // if there is space for the replacement dots, add them
356 if (maxFinalWidth
> replacementWidth
)
357 ret
.insert(initialChar
, wxELLIPSE_REPLACEMENT
);
360 // if everything was ok, we should have shortened this line
361 // enough to make it fit in maxFinalWidth:
362 wxASSERT(dc
.GetTextExtent(ret
).GetWidth() < maxFinalWidth
);
368 wxString
wxControlBase::Ellipsize(const wxString
& label
, const wxDC
& dc
,
369 wxEllipsizeMode mode
, int maxFinalWidth
,
374 // these cannot be cached between different Ellipsize() calls as they can
375 // change because of e.g. a font change; however we calculate them only once
376 // when ellipsizing multiline labels:
377 int replacementWidth
= dc
.GetTextExtent(wxELLIPSE_REPLACEMENT
).GetWidth();
378 int marginWidth
= dc
.GetCharWidth();
380 // NB: we must handle correctly labels with newlines:
382 for ( wxString::const_iterator pc
= label
.begin(); ; ++pc
)
384 if ( pc
== label
.end() || *pc
== wxS('\n') )
386 curLine
= DoEllipsizeSingleLine(curLine
, dc
, mode
, maxFinalWidth
,
387 replacementWidth
, marginWidth
);
389 // add this (ellipsized) row to the rest of the label
391 if ( pc
== label
.end() )
393 // NOTE: this is the return which always exits the function
402 // we need to remove mnemonics from the label for correct calculations
403 else if ( *pc
== wxS('&') && (flags
& wxELLIPSIZE_PROCESS_MNEMONICS
) != 0 )
405 // pc+1 is safe: at worst we'll be at end()
406 wxString::const_iterator next
= pc
+ 1;
407 if ( next
!= label
.end() && *next
== wxS('&') )
408 curLine
+= wxS('&'); // && becomes &
409 //else: remove this ampersand
411 // we need also to expand tabs to properly calc their size
412 else if ( *pc
== wxS('\t') && (flags
& wxELLIPSIZE_EXPAND_TAB
) != 0 )
414 // Windows natively expands the TABs to 6 spaces. Do the same:
423 // this return would generate a
424 // warning C4702: unreachable code
425 // with MSVC since the function always exits from inside the loop
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
437 wxStaticBitmapBase::~wxStaticBitmapBase()
439 // this destructor is required for Darwin
442 wxSize
wxStaticBitmapBase::DoGetBestSize() const
445 wxBitmap bmp
= GetBitmap();
447 best
= wxSize(bmp
.GetWidth(), bmp
.GetHeight());
449 // this is completely arbitrary
450 best
= wxSize(16, 16);
455 #endif // wxUSE_STATBMP
457 #endif // wxUSE_CONTROLS