1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/ctrlcmn.cpp
3 // Purpose: wxControl common interface
4 // Author: Vadim Zeitlin
7 // Copyright: (c) wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/control.h"
33 #include "wx/radiobut.h"
34 #include "wx/statbmp.h"
35 #include "wx/bitmap.h"
36 #include "wx/utils.h" // for wxStripMenuCodes()
37 #include "wx/settings.h"
40 #include "wx/private/markupparser.h"
42 const char wxControlNameStr
[] = "control";
44 // ============================================================================
46 // ============================================================================
48 wxControlBase::~wxControlBase()
50 // this destructor is required for Darwin
53 bool wxControlBase::Create(wxWindow
*parent
,
58 const wxValidator
& wxVALIDATOR_PARAM(validator
),
61 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
65 SetValidator(validator
);
66 #endif // wxUSE_VALIDATORS
71 bool wxControlBase::CreateControl(wxWindowBase
*parent
,
76 const wxValidator
& validator
,
79 // even if it's possible to create controls without parents in some port,
80 // it should surely be discouraged because it doesn't work at all under
82 wxCHECK_MSG( parent
, false, wxT("all controls must have parents") );
84 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
87 parent
->AddChild(this);
92 void wxControlBase::Command(wxCommandEvent
& event
)
94 (void)GetEventHandler()->ProcessEvent(event
);
97 void wxControlBase::InitCommandEvent(wxCommandEvent
& event
) const
99 event
.SetEventObject(const_cast<wxControlBase
*>(this));
101 // event.SetId(GetId()); -- this is usuall done in the event ctor
103 switch ( m_clientDataType
)
105 case wxClientData_Void
:
106 event
.SetClientData(GetClientData());
109 case wxClientData_Object
:
110 event
.SetClientObject(GetClientObject());
113 case wxClientData_None
:
119 bool wxControlBase::SetFont(const wxFont
& font
)
121 InvalidateBestSize();
122 return wxWindow::SetFont(font
);
125 // wxControl-specific processing after processing the update event
126 void wxControlBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
129 wxWindowBase::DoUpdateWindowUI(event
);
132 if ( event
.GetSetText() )
134 if ( event
.GetText() != GetLabel() )
135 SetLabel(event
.GetText());
138 // Unfortunately we don't yet have common base class for
139 // wxRadioButton, so we handle updates of radiobuttons here.
140 // TODO: If once wxRadioButtonBase will exist, move this code there.
142 if ( event
.GetSetChecked() )
144 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
146 radiobtn
->SetValue(event
.GetChecked());
148 #endif // wxUSE_RADIOBTN
151 wxSize
wxControlBase::DoGetSizeFromTextSize(int WXUNUSED(xlen
),
152 int WXUNUSED(ylen
)) const
154 return wxSize(-1, -1);
158 wxString
wxControlBase::GetLabelText(const wxString
& label
)
160 // we don't want strip the TABs here, just the mnemonics
161 return wxStripMenuCodes(label
, wxStrip_Mnemonics
);
165 wxString
wxControlBase::RemoveMnemonics(const wxString
& str
)
167 // we don't want strip the TABs here, just the mnemonics
168 return wxStripMenuCodes(str
, wxStrip_Mnemonics
);
172 wxString
wxControlBase::EscapeMnemonics(const wxString
& text
)
174 wxString
label(text
);
175 label
.Replace("&", "&&");
180 int wxControlBase::FindAccelIndex(const wxString
& label
, wxString
*labelOnly
)
182 // the character following MNEMONIC_PREFIX is the accelerator for this
183 // control unless it is MNEMONIC_PREFIX too - this allows to insert
184 // literal MNEMONIC_PREFIX chars into the label
185 static const wxChar MNEMONIC_PREFIX
= wxT('&');
190 labelOnly
->Alloc(label
.length());
194 for ( wxString::const_iterator pc
= label
.begin(); pc
!= label
.end(); ++pc
)
196 if ( *pc
== MNEMONIC_PREFIX
)
199 if ( pc
== label
.end() )
201 else if ( *pc
!= MNEMONIC_PREFIX
)
203 if ( indexAccel
== -1 )
205 // remember it (-1 is for MNEMONIC_PREFIX itself
206 indexAccel
= pc
- label
.begin() - 1;
210 wxFAIL_MSG(wxT("duplicate accel char in control label"));
224 wxBorder
wxControlBase::GetDefaultBorder() const
226 return wxBORDER_THEME
;
229 /* static */ wxVisualAttributes
230 wxControlBase::GetCompositeControlsDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
232 wxVisualAttributes attrs
;
233 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
234 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
235 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
240 // ----------------------------------------------------------------------------
241 // wxControl markup support
242 // ----------------------------------------------------------------------------
247 wxString
wxControlBase::RemoveMarkup(const wxString
& markup
)
249 return wxMarkupParser::Strip(markup
);
252 bool wxControlBase::DoSetLabelMarkup(const wxString
& markup
)
254 const wxString label
= RemoveMarkup(markup
);
255 if ( label
.empty() && !markup
.empty() )
263 #endif // wxUSE_MARKUP
265 // ----------------------------------------------------------------------------
266 // wxControlBase - ellipsization code
267 // ----------------------------------------------------------------------------
269 #define wxELLIPSE_REPLACEMENT wxS("...")
274 struct EllipsizeCalculator
276 EllipsizeCalculator(const wxString
& s
, const wxDC
& dc
,
277 int maxFinalWidthPx
, int replacementWidthPx
)
279 m_initialCharToRemove(0),
281 m_outputNeedsUpdate(true),
284 m_maxFinalWidthPx(maxFinalWidthPx
),
285 m_replacementWidthPx(replacementWidthPx
)
287 m_isOk
= dc
.GetPartialTextExtents(s
, m_charOffsetsPx
);
288 wxASSERT( m_charOffsetsPx
.GetCount() == s
.length() );
291 bool IsOk() const { return m_isOk
; }
293 bool EllipsizationNotNeeded() const
295 // NOTE: charOffsetsPx[n] is the width in pixels of the first n characters (with the last one INCLUDED)
296 // thus charOffsetsPx[len-1] is the total width of the string
297 return m_charOffsetsPx
.Last() <= m_maxFinalWidthPx
;
300 void Init(size_t initialCharToRemove
, size_t nCharsToRemove
)
302 m_initialCharToRemove
= initialCharToRemove
;
303 m_nCharsToRemove
= nCharsToRemove
;
311 void RemoveFromStart()
313 m_initialCharToRemove
--;
317 size_t GetFirstRemoved() const { return m_initialCharToRemove
; }
318 size_t GetLastRemoved() const { return m_initialCharToRemove
+ m_nCharsToRemove
- 1; }
320 const wxString
& GetEllipsizedText()
322 if ( m_outputNeedsUpdate
)
324 wxASSERT(m_initialCharToRemove
<= m_str
.length() - 1); // see valid range for initialCharToRemove above
325 wxASSERT(m_nCharsToRemove
>= 1 && m_nCharsToRemove
<= m_str
.length() - m_initialCharToRemove
); // see valid range for nCharsToRemove above
327 // erase m_nCharsToRemove characters after m_initialCharToRemove (included);
328 // e.g. if we have the string "foobar" (len = 6)
330 // \--- m_initialCharToRemove = 2
331 // and m_nCharsToRemove = 2, then we get "foar"
333 m_output
.replace(m_initialCharToRemove
, m_nCharsToRemove
, wxELLIPSE_REPLACEMENT
);
341 if ( m_nCharsToRemove
== m_str
.length() )
342 return true; // that's the best we could do
344 // Width calculation using partial extents is just an inaccurate
345 // estimate: partial extents have sub-pixel precision and are rounded
346 // by GetPartialTextExtents(); replacing part of the string with "..."
347 // may change them too thanks to changes in ligatures, kerning etc.
349 // The correct algorithm would be to call GetTextExtent() in every step
350 // of ellipsization, but that would be too expensive, especially when
351 // the difference is just a few pixels. So we use partial extents to
352 // estimate string width and only verify it with GetTextExtent() when
355 int estimatedWidth
= m_replacementWidthPx
; // length of "..."
357 // length of text before the removed part:
358 if ( m_initialCharToRemove
> 0 )
359 estimatedWidth
+= m_charOffsetsPx
[m_initialCharToRemove
- 1];
361 // length of text after the removed part:
363 if ( GetLastRemoved() < m_str
.length() )
364 estimatedWidth
+= m_charOffsetsPx
.Last() - m_charOffsetsPx
[GetLastRemoved()];
366 if ( estimatedWidth
> m_maxFinalWidthPx
)
369 return m_dc
.GetTextExtent(GetEllipsizedText()).GetWidth() <= m_maxFinalWidthPx
;
372 // calculation state:
374 // REMEMBER: indexes inside the string have a valid range of [0;len-1] if not otherwise constrained
375 // lengths/counts of characters (e.g. nCharsToRemove) have a
376 // valid range of [0;len] if not otherwise constrained
377 // NOTE: since this point we know we have for sure a non-empty string from which we need
378 // to remove _at least_ one character (thus nCharsToRemove below is constrained to be >= 1)
380 // index of first character to erase, valid range is [0;len-1]:
381 size_t m_initialCharToRemove
;
382 // how many chars do we need to erase? valid range is [0;len-m_initialCharToRemove]
383 size_t m_nCharsToRemove
;
386 bool m_outputNeedsUpdate
;
391 int m_maxFinalWidthPx
;
392 int m_replacementWidthPx
;
393 wxArrayInt m_charOffsetsPx
;
398 } // anonymous namespace
400 /* static and protected */
401 wxString
wxControlBase::DoEllipsizeSingleLine(const wxString
& curLine
, const wxDC
& dc
,
402 wxEllipsizeMode mode
, int maxFinalWidthPx
,
403 int replacementWidthPx
)
405 wxASSERT_MSG(replacementWidthPx
> 0, "Invalid parameters");
406 wxASSERT_LEVEL_2_MSG(!curLine
.Contains('\n'),
407 "Use Ellipsize() instead!");
409 wxASSERT_MSG( mode
!= wxELLIPSIZE_NONE
, "shouldn't be called at all then" );
411 // NOTE: this function assumes that any mnemonic/tab character has already
412 // been handled if it was necessary to handle them (see Ellipsize())
414 if (maxFinalWidthPx
<= 0)
415 return wxEmptyString
;
417 size_t len
= curLine
.length();
421 EllipsizeCalculator
calc(curLine
, dc
, maxFinalWidthPx
, replacementWidthPx
);
426 if ( calc
.EllipsizationNotNeeded() )
429 // let's compute the range of characters to remove depending on the ellipsization mode:
432 case wxELLIPSIZE_START
:
435 while ( !calc
.IsShortEnough() )
436 calc
.RemoveFromEnd();
438 // always show at least one character of the string:
439 if ( calc
.m_nCharsToRemove
== len
)
440 return wxString(wxELLIPSE_REPLACEMENT
) + curLine
[len
-1];
445 case wxELLIPSIZE_MIDDLE
:
447 // NOTE: the following piece of code works also when len == 1
449 // start the removal process from the middle of the string
450 // i.e. separe the string in three parts:
451 // - the first one to preserve, valid range [0;initialCharToRemove-1] or the empty range if initialCharToRemove==0
452 // - the second one to remove, valid range [initialCharToRemove;endCharToRemove]
453 // - the third one to preserve, valid range [endCharToRemove+1;len-1] or the empty range if endCharToRemove==len-1
454 // NOTE: empty range != range [0;0] since the range [0;0] contains 1 character (the zero-th one)!
458 bool removeFromStart
= true;
460 while ( !calc
.IsShortEnough() )
462 const bool canRemoveFromStart
= calc
.GetFirstRemoved() > 0;
463 const bool canRemoveFromEnd
= calc
.GetLastRemoved() < len
- 1;
465 if ( !canRemoveFromStart
&& !canRemoveFromEnd
)
467 // we need to remove all the characters of the string!
471 // Remove from the beginning in even steps and from the end
472 // in odd steps, unless we exhausted one side already:
473 removeFromStart
= !removeFromStart
;
474 if ( removeFromStart
&& !canRemoveFromStart
)
475 removeFromStart
= false;
476 else if ( !removeFromStart
&& !canRemoveFromEnd
)
477 removeFromStart
= true;
479 if ( removeFromStart
)
480 calc
.RemoveFromStart();
482 calc
.RemoveFromEnd();
485 // Always show at least one character of the string.
486 // Additionally, if there's only one character left, prefer
488 if ( calc
.m_nCharsToRemove
== len
||
489 calc
.m_nCharsToRemove
== len
- 1 )
491 return curLine
[0] + wxString(wxELLIPSE_REPLACEMENT
);
496 case wxELLIPSIZE_END
:
498 calc
.Init(len
- 1, 1);
499 while ( !calc
.IsShortEnough() )
500 calc
.RemoveFromStart();
502 // always show at least one character of the string:
503 if ( calc
.m_nCharsToRemove
== len
)
504 return curLine
[0] + wxString(wxELLIPSE_REPLACEMENT
);
509 case wxELLIPSIZE_NONE
:
511 wxFAIL_MSG("invalid ellipsize mode");
515 return calc
.GetEllipsizedText();
519 wxString
wxControlBase::Ellipsize(const wxString
& label
, const wxDC
& dc
,
520 wxEllipsizeMode mode
, int maxFinalWidth
,
525 // these cannot be cached between different Ellipsize() calls as they can
526 // change because of e.g. a font change; however we calculate them only once
527 // when ellipsizing multiline labels:
528 int replacementWidth
= dc
.GetTextExtent(wxELLIPSE_REPLACEMENT
).GetWidth();
530 // NB: we must handle correctly labels with newlines:
532 for ( wxString::const_iterator pc
= label
.begin(); ; ++pc
)
534 if ( pc
== label
.end() || *pc
== wxS('\n') )
536 curLine
= DoEllipsizeSingleLine(curLine
, dc
, mode
, maxFinalWidth
,
539 // add this (ellipsized) row to the rest of the label
541 if ( pc
== label
.end() )
547 // we need to remove mnemonics from the label for correct calculations
548 else if ( *pc
== wxS('&') && (flags
& wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS
) )
550 // pc+1 is safe: at worst we'll be at end()
551 wxString::const_iterator next
= pc
+ 1;
552 if ( next
!= label
.end() && *next
== wxS('&') )
553 curLine
+= wxS('&'); // && becomes &
554 //else: remove this ampersand
556 // we need also to expand tabs to properly calc their size
557 else if ( *pc
== wxS('\t') && (flags
& wxELLIPSIZE_FLAGS_EXPAND_TABS
) )
559 // Windows natively expands the TABs to 6 spaces. Do the same:
571 // ----------------------------------------------------------------------------
573 // ----------------------------------------------------------------------------
577 wxStaticBitmapBase::~wxStaticBitmapBase()
579 // this destructor is required for Darwin
582 wxSize
wxStaticBitmapBase::DoGetBestSize() const
585 wxBitmap bmp
= GetBitmap();
587 best
= wxSize(bmp
.GetWidth(), bmp
.GetHeight());
589 // this is completely arbitrary
590 best
= wxSize(16, 16);
595 #endif // wxUSE_STATBMP
597 #endif // wxUSE_CONTROLS