]>
git.saurik.com Git - wxWidgets.git/blob - src/common/ctrlcmn.cpp
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 #define wxELLIPSE_REPLACEMENT wxT("...")
218 wxString
wxControlBase::Ellipsize(const wxString
& label
, const wxDC
& dc
,
219 wxEllipsizeMode mode
, int maxFinalWidth
)
221 wxArrayInt charOffsets
;
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;
228 // NB: we must handle correctly labels with newlines:
232 for ( wxString::const_iterator pc
= label
.begin(); ; ++pc
)
234 if ( pc
== label
.end() || *pc
== _T('\n') )
236 len
= curLine
.length();
238 dc
.GetPartialTextExtents(curLine
, charOffsets
))
240 wxASSERT(charOffsets
.GetCount() == len
);
242 size_t totalWidth
= charOffsets
.Last();
243 if ( totalWidth
> (size_t)maxFinalWidth
)
245 // we need to ellipsize this row
246 int excessPixels
= totalWidth
- maxFinalWidth
+
248 marginWidth
; // security margin (NEEDED!)
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
)
257 nChars
< len
&& charOffsets
[nChars
] < excessPixels
;
261 else if (mode
== wxELLIPSIZE_MIDDLE
)
263 // the start & end of the removed span of chars
265 size_t endChar
= len
/2;
268 for ( ; removed
< excessPixels
; )
272 // width of the initialChar-th character
273 int width
= charOffsets
[initialChar
] -
274 charOffsets
[initialChar
-1];
276 // remove the initialChar-th character
281 if (endChar
< len
- 1 &&
282 removed
< excessPixels
)
284 // width of the (endChar+1)-th character
285 int width
= charOffsets
[endChar
+1] -
286 charOffsets
[endChar
];
288 // remove the endChar-th character
293 if (initialChar
== 0 && endChar
== len
-1)
301 nChars
= endChar
- initialChar
+ 1;
305 wxASSERT(mode
== wxELLIPSIZE_END
);
308 int maxWidth
= totalWidth
- excessPixels
;
311 charOffsets
[initialChar
] < maxWidth
;
315 if (initialChar
== 0)
321 initialChar
--; // go back one character
322 nChars
= len
- initialChar
;
328 // need to remove the entire row!
333 // erase nChars characters after initialChar (included):
334 curLine
.erase(initialChar
, nChars
+1);
336 // if there is space for the replacement dots, add them
337 if (maxFinalWidth
> replacementWidth
)
338 curLine
.insert(initialChar
, wxELLIPSE_REPLACEMENT
);
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
);
347 // add this (ellipsized) row to the rest of the label
349 if ( pc
== label
.end() )
351 // NOTE: this is the return which always exits the function
360 // we need to remove mnemonics from the label for correct calculations
361 else if ( *pc
== _T('&') )
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
369 // we need also to expand tabs to properly calc their size
370 else if ( *pc
== _T('\t') )
372 // Windows natively expands the TABs to 6 spaces. Do the same:
381 // this return would generate a
382 // warning C4702: unreachable code
383 // with MSVC since the function always exits from inside the loop
387 wxBorder
wxControlBase::GetDefaultBorder() const
389 return wxBORDER_THEME
;
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
399 wxStaticBitmapBase::~wxStaticBitmapBase()
401 // this destructor is required for Darwin
404 wxSize
wxStaticBitmapBase::DoGetBestSize() const
407 wxBitmap bmp
= GetBitmap();
409 best
= wxSize(bmp
.GetWidth(), bmp
.GetHeight());
411 // this is completely arbitrary
412 best
= wxSize(16, 16);
417 #endif // wxUSE_STATBMP
419 #endif // wxUSE_CONTROLS