]>
git.saurik.com Git - wxWidgets.git/blob - src/common/stattextcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/stattextcmn.cpp
3 // Purpose: common (to all ports) wxStaticText functions
4 // Author: Vadim Zeitlin, Francesco Montorsi
5 // Created: 2007-01-07 (extracted from dlgcmn.cpp)
6 // Copyright: (c) 1999-2006 Vadim Zeitlin
7 // (c) 2007 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
29 #include "wx/stattext.h"
30 #include "wx/button.h"
31 #include "wx/dcclient.h"
34 #include "wx/settings.h"
36 #include "wx/containr.h"
39 #include "wx/textwrapper.h"
41 #include "wx/private/markupparser.h"
43 extern WXDLLEXPORT_DATA(const char) wxStaticTextNameStr
[] = "staticText";
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 wxDEFINE_FLAGS( wxStaticTextStyle
)
50 wxBEGIN_FLAGS( wxStaticTextStyle
)
51 // new style border flags, we put them first to
52 // use them for streaming out
53 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
54 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
55 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
56 wxFLAGS_MEMBER(wxBORDER_RAISED
)
57 wxFLAGS_MEMBER(wxBORDER_STATIC
)
58 wxFLAGS_MEMBER(wxBORDER_NONE
)
60 // old style border flags
61 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
62 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
63 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
64 wxFLAGS_MEMBER(wxRAISED_BORDER
)
65 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
66 wxFLAGS_MEMBER(wxBORDER
)
68 // standard window styles
69 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
70 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
71 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
72 wxFLAGS_MEMBER(wxWANTS_CHARS
)
73 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
74 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
75 wxFLAGS_MEMBER(wxVSCROLL
)
76 wxFLAGS_MEMBER(wxHSCROLL
)
78 wxFLAGS_MEMBER(wxST_NO_AUTORESIZE
)
79 wxFLAGS_MEMBER(wxALIGN_LEFT
)
80 wxFLAGS_MEMBER(wxALIGN_RIGHT
)
81 wxFLAGS_MEMBER(wxALIGN_CENTRE
)
82 wxEND_FLAGS( wxStaticTextStyle
)
84 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText
, wxControl
, "wx/stattext.h")
86 wxBEGIN_PROPERTIES_TABLE(wxStaticText
)
87 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString(), 0 /*flags*/, \
88 wxT("Helpstring"), wxT("group"))
89 wxPROPERTY_FLAGS( WindowStyle
, wxStaticTextStyle
, long, SetWindowStyleFlag
, \
90 GetWindowStyleFlag
, wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, \
91 wxT("Helpstring"), wxT("group")) // style
92 wxEND_PROPERTIES_TABLE()
94 wxEMPTY_HANDLERS_TABLE(wxStaticText
)
96 wxCONSTRUCTOR_6( wxStaticText
, wxWindow
*, Parent
, wxWindowID
, Id
, \
97 wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long, WindowStyle
)
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 void wxTextWrapper::Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
)
108 wxString::const_iterator lastSpace
= text
.end();
109 wxString::const_iterator lineStart
= text
.begin();
110 for ( wxString::const_iterator p
= lineStart
; ; ++p
)
112 if ( IsStartOfNewLine() )
116 lastSpace
= text
.end();
121 if ( p
== text
.end() || *p
== wxT('\n') )
125 if ( p
== text
.end() )
130 if ( *p
== wxT(' ') )
135 if ( widthMax
>= 0 && lastSpace
!= text
.end() )
138 win
->GetTextExtent(line
, &width
, NULL
);
140 if ( width
> widthMax
)
142 // remove the last word from this line
143 line
.erase(lastSpace
- lineStart
, p
+ 1 - lineStart
);
146 // go back to the last word of this line which we didn't
151 //else: no wrapping at all or impossible to wrap
157 // ----------------------------------------------------------------------------
158 // wxLabelWrapper: helper class for wxStaticTextBase::Wrap()
159 // ----------------------------------------------------------------------------
161 class wxLabelWrapper
: public wxTextWrapper
164 void WrapLabel(wxWindow
*text
, int widthMax
)
167 Wrap(text
, text
->GetLabel(), widthMax
);
168 text
->SetLabel(m_text
);
172 virtual void OnOutputLine(const wxString
& line
)
177 virtual void OnNewLine()
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 void wxStaticTextBase::Wrap(int width
)
193 wxLabelWrapper wrapper
;
194 wrapper
.WrapLabel(this, width
);
197 // ----------------------------------------------------------------------------
198 // wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support
199 // ----------------------------------------------------------------------------
201 void wxStaticTextBase::UpdateLabel()
206 wxString newlabel
= GetEllipsizedLabel();
208 // we need to touch the "real" label (i.e. the text set inside the control,
209 // using port-specific functions) instead of the string returned by GetLabel().
211 // In fact, we must be careful not to touch the original label passed to
212 // SetLabel() otherwise GetLabel() will behave in a strange way to the user
213 // (e.g. returning a "Ver...ing" instead of "Very long string") !
214 if (newlabel
== DoGetLabel())
216 DoSetLabel(newlabel
);
219 wxString
wxStaticTextBase::GetEllipsizedLabel() const
221 // this function should be used only by ports which do not support
222 // ellipsis in static texts: we first remove markup (which cannot
223 // be handled safely by Ellipsize()) and then ellipsize the result.
225 wxString
ret(m_labelOrig
);
228 ret
= Ellipsize(ret
);
233 wxString
wxStaticTextBase::Ellipsize(const wxString
& label
) const
235 wxSize
sz(GetSize());
236 if (sz
.GetWidth() < 2 || sz
.GetHeight() < 2)
238 // the size of this window is not valid (yet)
242 wxClientDC
dc(const_cast<wxStaticTextBase
*>(this));
243 dc
.SetFont(GetFont());
245 wxEllipsizeMode mode
;
246 if ( HasFlag(wxST_ELLIPSIZE_START
) )
247 mode
= wxELLIPSIZE_START
;
248 else if ( HasFlag(wxST_ELLIPSIZE_MIDDLE
) )
249 mode
= wxELLIPSIZE_MIDDLE
;
250 else if ( HasFlag(wxST_ELLIPSIZE_END
) )
251 mode
= wxELLIPSIZE_END
;
254 wxFAIL_MSG( "should only be called if have one of wxST_ELLIPSIZE_XXX" );
259 return wxControl::Ellipsize(label
, dc
, mode
, sz
.GetWidth());
262 #endif // wxUSE_STATTEXT