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()
38 #include "wx/settings.h"
41 const char wxControlNameStr
[] = "control";
43 // ============================================================================
45 // ============================================================================
47 wxControlBase::~wxControlBase()
49 // this destructor is required for Darwin
52 bool wxControlBase::Create(wxWindow
*parent
,
57 const wxValidator
& wxVALIDATOR_PARAM(validator
),
60 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
64 SetValidator(validator
);
65 #endif // wxUSE_VALIDATORS
70 bool wxControlBase::CreateControl(wxWindowBase
*parent
,
75 const wxValidator
& validator
,
78 // even if it's possible to create controls without parents in some port,
79 // it should surely be discouraged because it doesn't work at all under
81 wxCHECK_MSG( parent
, false, wxT("all controls must have parents") );
83 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
86 parent
->AddChild(this);
92 wxString
wxControlBase::GetLabelText(const wxString
& label
)
94 // we don't want strip the TABs here, just the mnemonics
95 return wxStripMenuCodes(label
, wxStrip_Mnemonics
);
98 void wxControlBase::Command(wxCommandEvent
& event
)
100 (void)GetEventHandler()->ProcessEvent(event
);
103 void wxControlBase::InitCommandEvent(wxCommandEvent
& event
) const
105 event
.SetEventObject((wxControlBase
*)this); // const_cast
107 // event.SetId(GetId()); -- this is usuall done in the event ctor
109 switch ( m_clientDataType
)
111 case wxClientData_Void
:
112 event
.SetClientData(GetClientData());
115 case wxClientData_Object
:
116 event
.SetClientObject(GetClientObject());
119 case wxClientData_None
:
125 bool wxControlBase::SetFont(const wxFont
& font
)
127 InvalidateBestSize();
128 return wxWindow::SetFont(font
);
131 // wxControl-specific processing after processing the update event
132 void wxControlBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
135 wxWindowBase::DoUpdateWindowUI(event
);
138 if ( event
.GetSetText() )
140 if ( event
.GetText() != GetLabel() )
141 SetLabel(event
.GetText());
144 // Unfortunately we don't yet have common base class for
145 // wxRadioButton, so we handle updates of radiobuttons here.
146 // TODO: If once wxRadioButtonBase will exist, move this code there.
148 if ( event
.GetSetChecked() )
150 wxRadioButton
*radiobtn
= wxDynamicCastThis(wxRadioButton
);
152 radiobtn
->SetValue(event
.GetChecked());
154 #endif // wxUSE_RADIOBTN
158 wxString
wxControlBase::RemoveMnemonics(const wxString
& str
)
160 return wxStripMenuCodes(str
, wxStrip_Mnemonics
);
164 wxString
wxControlBase::EscapeMnemonics(const wxString
& text
)
166 wxString
label(text
);
167 label
.Replace("&", "&&");
172 int wxControlBase::FindAccelIndex(const wxString
& label
, wxString
*labelOnly
)
174 // the character following MNEMONIC_PREFIX is the accelerator for this
175 // control unless it is MNEMONIC_PREFIX too - this allows to insert
176 // literal MNEMONIC_PREFIX chars into the label
177 static const wxChar MNEMONIC_PREFIX
= wxT('&');
182 labelOnly
->Alloc(label
.length());
186 for ( wxString::const_iterator pc
= label
.begin(); pc
!= label
.end(); ++pc
)
188 if ( *pc
== MNEMONIC_PREFIX
)
191 if ( pc
== label
.end() )
193 else if ( *pc
!= MNEMONIC_PREFIX
)
195 if ( indexAccel
== -1 )
197 // remember it (-1 is for MNEMONIC_PREFIX itself
198 indexAccel
= pc
- label
.begin() - 1;
202 wxFAIL_MSG(wxT("duplicate accel char in control label"));
216 wxBorder
wxControlBase::GetDefaultBorder() const
218 return wxBORDER_THEME
;
221 /* static */ wxVisualAttributes
222 wxControlBase::GetCompositeControlsDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
224 wxVisualAttributes attrs
;
225 attrs
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
226 attrs
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
227 attrs
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
232 // ----------------------------------------------------------------------------
233 // wxControlBase - ellipsization code
234 // ----------------------------------------------------------------------------
236 #define wxELLIPSE_REPLACEMENT wxT("...")
238 /* static and protected */
239 wxString
wxControlBase::DoEllipsizeSingleLine(const wxString
& curLine
, const wxDC
& dc
,
240 wxEllipsizeMode mode
, int maxFinalWidth
,
241 int replacementWidth
, int marginWidth
)
243 wxASSERT_MSG(replacementWidth
> 0 && marginWidth
> 0,
244 "Invalid parameters");
245 wxASSERT_MSG(!curLine
.Contains('\n'),
246 "Use Ellipsize() instead!");
248 // NOTE: this function assumes that any mnemonic/tab character has already
249 // been handled if it was necessary to handle them (see Ellipsize())
251 if (maxFinalWidth
<= 0)
252 return wxEmptyString
;
254 wxArrayInt charOffsets
;
255 size_t len
= curLine
.length();
257 !dc
.GetPartialTextExtents(curLine
, charOffsets
))
260 wxASSERT(charOffsets
.GetCount() == len
);
262 size_t totalWidth
= charOffsets
.Last();
263 if ( totalWidth
<= (size_t)maxFinalWidth
)
264 return curLine
; // we don't need to do any ellipsization!
266 int excessPixels
= totalWidth
- maxFinalWidth
+
268 marginWidth
; // security margin (NEEDED!)
269 wxASSERT(excessPixels
>0);
271 // remove characters in excess
272 size_t initialChar
, // index of first char to erase
273 nChars
; // how many chars do we need to erase?
277 case wxELLIPSIZE_START
:
280 nChars
< len
&& charOffsets
[nChars
] < excessPixels
;
285 case wxELLIPSIZE_MIDDLE
:
287 // the start & end of the removed span of chars
289 size_t endChar
= len
/2;
292 for ( ; removed
< excessPixels
; )
296 // width of the initialChar-th character
297 int width
= charOffsets
[initialChar
] -
298 charOffsets
[initialChar
-1];
300 // remove the initialChar-th character
305 if (endChar
< len
- 1 &&
306 removed
< excessPixels
)
308 // width of the (endChar+1)-th character
309 int width
= charOffsets
[endChar
+1] -
310 charOffsets
[endChar
];
312 // remove the endChar-th character
317 if (initialChar
== 0 && endChar
== len
-1)
325 nChars
= endChar
- initialChar
+ 1;
329 case wxELLIPSIZE_END
:
333 int maxWidth
= totalWidth
- excessPixels
;
334 for ( initialChar
= 0;
335 initialChar
< len
&& charOffsets
[initialChar
] < maxWidth
;
339 if (initialChar
== 0)
345 //initialChar--; // go back one character
346 nChars
= len
- initialChar
;
352 wxFAIL_MSG("invalid ellipsize mode");
356 wxString
ret(curLine
);
359 // need to remove the entire row!
364 // erase nChars characters after initialChar (included):
365 ret
.erase(initialChar
, nChars
+1);
367 // if there is space for the replacement dots, add them
368 if (maxFinalWidth
> replacementWidth
)
369 ret
.insert(initialChar
, wxELLIPSE_REPLACEMENT
);
372 // if everything was ok, we should have shortened this line
373 // enough to make it fit in maxFinalWidth:
374 wxASSERT(dc
.GetTextExtent(ret
).GetWidth() < maxFinalWidth
);
380 wxString
wxControlBase::Ellipsize(const wxString
& label
, const wxDC
& dc
,
381 wxEllipsizeMode mode
, int maxFinalWidth
,
386 // these cannot be cached between different Ellipsize() calls as they can
387 // change because of e.g. a font change; however we calculate them only once
388 // when ellipsizing multiline labels:
389 int replacementWidth
= dc
.GetTextExtent(wxELLIPSE_REPLACEMENT
).GetWidth();
390 int marginWidth
= dc
.GetCharWidth();
392 // NB: we must handle correctly labels with newlines:
394 for ( wxString::const_iterator pc
= label
.begin(); ; ++pc
)
396 if ( pc
== label
.end() || *pc
== wxS('\n') )
398 curLine
= DoEllipsizeSingleLine(curLine
, dc
, mode
, maxFinalWidth
,
399 replacementWidth
, marginWidth
);
401 // add this (ellipsized) row to the rest of the label
403 if ( pc
== label
.end() )
405 // NOTE: this is the return which always exits the function
414 // we need to remove mnemonics from the label for correct calculations
415 else if ( *pc
== wxS('&') && (flags
& wxELLIPSIZE_PROCESS_MNEMONICS
) != 0 )
417 // pc+1 is safe: at worst we'll be at end()
418 wxString::const_iterator next
= pc
+ 1;
419 if ( next
!= label
.end() && *next
== wxS('&') )
420 curLine
+= wxS('&'); // && becomes &
421 //else: remove this ampersand
423 // we need also to expand tabs to properly calc their size
424 else if ( *pc
== wxS('\t') && (flags
& wxELLIPSIZE_EXPAND_TAB
) != 0 )
426 // Windows natively expands the TABs to 6 spaces. Do the same:
435 // this return would generate a
436 // warning C4702: unreachable code
437 // with MSVC since the function always exits from inside the loop
443 // ----------------------------------------------------------------------------
445 // ----------------------------------------------------------------------------
449 wxStaticBitmapBase::~wxStaticBitmapBase()
451 // this destructor is required for Darwin
454 wxSize
wxStaticBitmapBase::DoGetBestSize() const
457 wxBitmap bmp
= GetBitmap();
459 best
= wxSize(bmp
.GetWidth(), bmp
.GetHeight());
461 // this is completely arbitrary
462 best
= wxSize(16, 16);
467 #endif // wxUSE_STATBMP
469 #endif // wxUSE_CONTROLS