]>
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 int wxControlBase::FindAccelIndex(const wxString
& label
, wxString
*labelOnly
)
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('&');
173 labelOnly
->Alloc(label
.length());
177 for ( wxString::const_iterator pc
= label
.begin(); pc
!= label
.end(); ++pc
)
179 if ( *pc
== MNEMONIC_PREFIX
)
182 if ( pc
== label
.end() )
184 else if ( *pc
!= MNEMONIC_PREFIX
)
186 if ( indexAccel
== -1 )
188 // remember it (-1 is for MNEMONIC_PREFIX itself
189 indexAccel
= pc
- label
.begin() - 1;
193 wxFAIL_MSG(_T("duplicate accel char in control label"));
207 #define wxELLIPSE_REPLACEMENT wxT("...")
210 wxString
wxControlBase::Ellipsize(const wxString
& label
, const wxDC
& dc
,
211 wxEllipsizeMode mode
, int maxFinalWidth
)
213 wxArrayInt charOffsets
;
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;
220 // NB: we must handle correctly labels with newlines:
224 for ( wxString::const_iterator pc
= label
.begin(); ; ++pc
)
226 if ( pc
== label
.end() || *pc
== _T('\n') )
228 len
= curLine
.length();
230 dc
.GetPartialTextExtents(curLine
, charOffsets
))
232 wxASSERT(charOffsets
.GetCount() == len
);
234 size_t totalWidth
= charOffsets
.Last();
235 if ( totalWidth
> (size_t)maxFinalWidth
)
237 // we need to ellipsize this row
238 int excessPixels
= totalWidth
- maxFinalWidth
+
240 marginWidth
; // security margin (NEEDED!)
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?
245 if (mode
== wxELLIPSIZE_START
)
249 nChars
< len
&& charOffsets
[nChars
] < excessPixels
;
253 else if (mode
== wxELLIPSIZE_MIDDLE
)
255 // the start & end of the removed span of chars
257 size_t endChar
= len
/2;
260 for ( ; removed
< excessPixels
; )
264 // width of the initialChar-th character
265 int width
= charOffsets
[initialChar
] -
266 charOffsets
[initialChar
-1];
268 // remove the initialChar-th character
273 if (endChar
< len
- 1 &&
274 removed
< excessPixels
)
276 // width of the (endChar+1)-th character
277 int width
= charOffsets
[endChar
+1] -
278 charOffsets
[endChar
];
280 // remove the endChar-th character
285 if (initialChar
== 0 && endChar
== len
-1)
293 nChars
= endChar
- initialChar
+ 1;
297 wxASSERT(mode
== wxELLIPSIZE_END
);
300 int maxWidth
= totalWidth
- excessPixels
;
303 charOffsets
[initialChar
] < maxWidth
;
307 if (initialChar
== 0)
313 initialChar
--; // go back one character
314 nChars
= len
- initialChar
;
320 // need to remove the entire row!
325 // erase nChars characters after initialChar (included):
326 curLine
.erase(initialChar
, nChars
+1);
328 // if there is space for the replacement dots, add them
329 if (maxFinalWidth
> replacementWidth
)
330 curLine
.insert(initialChar
, wxELLIPSE_REPLACEMENT
);
333 // if everything was ok, we should have shortened this line
334 // enough to make it fit in maxFinalWidth:
335 wxASSERT(dc
.GetTextExtent(curLine
).GetWidth() < maxFinalWidth
);
339 // add this (ellipsized) row to the rest of the label
341 if ( pc
== label
.end() )
343 // NOTE: this is the return which always exits the function
352 // we need to remove mnemonics from the label for correct calculations
353 else if ( *pc
== _T('&') )
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
361 // we need also to expand tabs to properly calc their size
362 else if ( *pc
== _T('\t') )
364 // Windows natively expands the TABs to 6 spaces. Do the same:
373 // this return would generate a
374 // warning C4702: unreachable code
375 // with MSVC since the function always exits from inside the loop
379 wxBorder
wxControlBase::GetDefaultBorder() const
381 return wxBORDER_THEME
;
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
391 wxStaticBitmapBase::~wxStaticBitmapBase()
393 // this destructor is required for Darwin
396 wxSize
wxStaticBitmapBase::DoGetBestSize() const
399 wxBitmap bmp
= GetBitmap();
401 best
= wxSize(bmp
.GetWidth(), bmp
.GetHeight());
403 // this is completely arbitrary
404 best
= wxSize(16, 16);
409 #endif // wxUSE_STATBMP
411 #endif // wxUSE_CONTROLS