]>
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) 
   7 // Copyright:   (c) 1999-2006 Vadim Zeitlin 
   8 //              (c) 2007 Francesco Montorsi 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  20 // For compilers that support precompilation, includes "wx.h". 
  21 #include "wx/wxprec.h" 
  30     #include "wx/stattext.h" 
  31     #include "wx/button.h" 
  32     #include "wx/dcclient.h" 
  35     #include "wx/settings.h" 
  37     #include "wx/containr.h" 
  40 #include "wx/textwrapper.h" 
  42 #include "wx/private/markupparser.h" 
  44 extern WXDLLEXPORT_DATA(const char) wxStaticTextNameStr
[] = "staticText"; 
  46 // ---------------------------------------------------------------------------- 
  48 // ---------------------------------------------------------------------------- 
  50 wxDEFINE_FLAGS( wxStaticTextStyle 
) 
  51 wxBEGIN_FLAGS( wxStaticTextStyle 
) 
  52 // new style border flags, we put them first to 
  53 // use them for streaming out 
  54 wxFLAGS_MEMBER(wxBORDER_SIMPLE
) 
  55 wxFLAGS_MEMBER(wxBORDER_SUNKEN
) 
  56 wxFLAGS_MEMBER(wxBORDER_DOUBLE
) 
  57 wxFLAGS_MEMBER(wxBORDER_RAISED
) 
  58 wxFLAGS_MEMBER(wxBORDER_STATIC
) 
  59 wxFLAGS_MEMBER(wxBORDER_NONE
) 
  61 // old style border flags 
  62 wxFLAGS_MEMBER(wxSIMPLE_BORDER
) 
  63 wxFLAGS_MEMBER(wxSUNKEN_BORDER
) 
  64 wxFLAGS_MEMBER(wxDOUBLE_BORDER
) 
  65 wxFLAGS_MEMBER(wxRAISED_BORDER
) 
  66 wxFLAGS_MEMBER(wxSTATIC_BORDER
) 
  67 wxFLAGS_MEMBER(wxBORDER
) 
  69 // standard window styles 
  70 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
) 
  71 wxFLAGS_MEMBER(wxCLIP_CHILDREN
) 
  72 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
) 
  73 wxFLAGS_MEMBER(wxWANTS_CHARS
) 
  74 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
) 
  75 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB 
) 
  76 wxFLAGS_MEMBER(wxVSCROLL
) 
  77 wxFLAGS_MEMBER(wxHSCROLL
) 
  79 wxFLAGS_MEMBER(wxST_NO_AUTORESIZE
) 
  80 wxFLAGS_MEMBER(wxALIGN_LEFT
) 
  81 wxFLAGS_MEMBER(wxALIGN_RIGHT
) 
  82 wxFLAGS_MEMBER(wxALIGN_CENTRE
) 
  83 wxEND_FLAGS( wxStaticTextStyle 
) 
  85 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText
, wxControl
, "wx/stattext.h") 
  87 wxBEGIN_PROPERTIES_TABLE(wxStaticText
) 
  88 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString(), 0 /*flags*/, \
 
  89            wxT("Helpstring"), wxT("group")) 
  90 wxPROPERTY_FLAGS( WindowStyle
, wxStaticTextStyle
, long, SetWindowStyleFlag
, \
 
  91                  GetWindowStyleFlag
, wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, \
 
  92                  wxT("Helpstring"), wxT("group")) // style 
  93 wxEND_PROPERTIES_TABLE() 
  95 wxEMPTY_HANDLERS_TABLE(wxStaticText
) 
  97 wxCONSTRUCTOR_6( wxStaticText
, wxWindow
*, Parent
, wxWindowID
, Id
, \
 
  98                 wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long, WindowStyle 
) 
 101 // ---------------------------------------------------------------------------- 
 103 // ---------------------------------------------------------------------------- 
 105 void wxTextWrapper::Wrap(wxWindow 
*win
, const wxString
& text
, int widthMax
) 
 109     wxString::const_iterator lastSpace 
= text
.end(); 
 110     wxString::const_iterator lineStart 
= text
.begin(); 
 111     for ( wxString::const_iterator p 
= lineStart
; ; ++p 
) 
 113         if ( IsStartOfNewLine() ) 
 117             lastSpace 
= text
.end(); 
 122         if ( p 
== text
.end() || *p 
== wxT('\n') ) 
 126             if ( p 
== text
.end() ) 
 131             if ( *p 
== wxT(' ') ) 
 136             if ( widthMax 
>= 0 && lastSpace 
!= text
.end() ) 
 139                 win
->GetTextExtent(line
, &width
, NULL
); 
 141                 if ( width 
> widthMax 
) 
 143                     // remove the last word from this line 
 144                     line
.erase(lastSpace 
- lineStart
, p 
+ 1 - lineStart
); 
 147                     // go back to the last word of this line which we didn't 
 152             //else: no wrapping at all or impossible to wrap 
 158 // ---------------------------------------------------------------------------- 
 159 // wxLabelWrapper: helper class for wxStaticTextBase::Wrap() 
 160 // ---------------------------------------------------------------------------- 
 162 class wxLabelWrapper 
: public wxTextWrapper
 
 165     void WrapLabel(wxWindow 
*text
, int widthMax
) 
 168         Wrap(text
, text
->GetLabel(), widthMax
); 
 169         text
->SetLabel(m_text
); 
 173     virtual void OnOutputLine(const wxString
& line
) 
 178     virtual void OnNewLine() 
 188 // ---------------------------------------------------------------------------- 
 190 // ---------------------------------------------------------------------------- 
 192 void wxStaticTextBase::Wrap(int width
) 
 194     wxLabelWrapper wrapper
; 
 195     wrapper
.WrapLabel(this, width
); 
 198 // ---------------------------------------------------------------------------- 
 199 // wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support 
 200 // ---------------------------------------------------------------------------- 
 202 void wxStaticTextBase::UpdateLabel() 
 207     wxString newlabel 
= GetEllipsizedLabel(); 
 209     // we need to touch the "real" label (i.e. the text set inside the control, 
 210     // using port-specific functions) instead of the string returned by GetLabel(). 
 212     // In fact, we must be careful not to touch the original label passed to 
 213     // SetLabel() otherwise GetLabel() will behave in a strange way to the user 
 214     // (e.g. returning a "Ver...ing" instead of "Very long string") ! 
 215     if (newlabel 
== DoGetLabel()) 
 217     DoSetLabel(newlabel
); 
 220 wxString 
wxStaticTextBase::GetEllipsizedLabel() const 
 222     // this function should be used only by ports which do not support 
 223     // ellipsis in static texts: we first remove markup (which cannot 
 224     // be handled safely by Ellipsize()) and then ellipsize the result. 
 226     wxString 
ret(m_labelOrig
); 
 229         ret 
= Ellipsize(ret
); 
 234 wxString 
wxStaticTextBase::Ellipsize(const wxString
& label
) const 
 236     wxSize 
sz(GetSize()); 
 237     if (sz
.GetWidth() < 2 || sz
.GetHeight() < 2) 
 239         // the size of this window is not valid (yet) 
 243     wxClientDC 
dc(const_cast<wxStaticTextBase
*>(this)); 
 244     dc
.SetFont(GetFont()); 
 246     wxEllipsizeMode mode
; 
 247     if ( HasFlag(wxST_ELLIPSIZE_START
) ) 
 248         mode 
= wxELLIPSIZE_START
; 
 249     else if ( HasFlag(wxST_ELLIPSIZE_MIDDLE
) ) 
 250         mode 
= wxELLIPSIZE_MIDDLE
; 
 251     else if ( HasFlag(wxST_ELLIPSIZE_END
) ) 
 252         mode 
= wxELLIPSIZE_END
; 
 255         wxFAIL_MSG( "should only be called if have one of wxST_ELLIPSIZE_XXX" ); 
 260     return wxControl::Ellipsize(label
, dc
, mode
, sz
.GetWidth()); 
 263 #endif // wxUSE_STATTEXT