1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxStaticText 
   8 // Copyright:   (c) AUTHOR 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  13 #pragma implementation "stattext.h" 
  17 #include "wx/stattext.h" 
  18 #include "wx/notebook.h" 
  19 #include "wx/tabctrl.h" 
  21 #include "wx/dcclient.h" 
  23 #include "wx/settings.h" 
  27 #if !USE_SHARED_LIBRARY 
  28 IMPLEMENT_DYNAMIC_CLASS(wxStaticText
, wxControl
) 
  31 #include "wx/mac/uma.h" 
  33 BEGIN_EVENT_TABLE(wxStaticText
, wxStaticTextBase
) 
  34     EVT_PAINT(wxStaticText::OnPaint
) 
  37 bool wxStaticText::Create(wxWindow 
*parent
, wxWindowID id
, 
  38            const wxString
& label
, 
  45     m_backgroundColour 
= parent
->GetBackgroundColour() ; 
  46     m_foregroundColour 
= parent
->GetForegroundColour() ; 
  49         m_windowId 
= (int)NewControlId(); 
  53     m_windowStyle 
= style
; 
  56     bool ret 
= wxControl::Create( parent
, id
, pos
, size
, style 
, wxDefaultValidator 
, name 
); 
  62 const wxString punct 
= " ,.-;:!?"; 
  64 void wxStaticText::DrawParagraph(wxDC 
&dc
, wxString paragraph
, int &y
) 
  70   bool linedrawn 
= true; 
  71   while( paragraph
.Length() > 0 ) 
  73         dc
.GetTextExtent( paragraph 
, &width 
, &height 
) ; 
  75         if ( width 
> m_width 
) 
  77             for ( int p 
= paragraph
.Length() -1 ; p 
> 0 ; --p 
) 
  79               if ((punct
.Find(paragraph
[p
]) != wxNOT_FOUND
) || !linedrawn
) 
  81                   int blank 
= (paragraph
[p
] == ' ') ? 0 : 1; 
  83                     dc
.GetTextExtent( paragraph
.Left(p 
+ blank
) , &width 
, &height 
) ; 
  85                     if ( width 
<= m_width 
) 
  88                         if ( HasFlag( wxALIGN_CENTER 
) ) 
  90                             pos 
+= ( m_width 
- width 
) / 2 ; 
  92                         else if ( HasFlag( wxALIGN_RIGHT 
) ) 
  94                             pos 
+= ( m_width 
- width 
) ; 
  97                     dc
.DrawText( paragraph
.Left(p 
+ blank
), pos 
, y
) ; 
  99                         paragraph 
= paragraph
.Mid(p
+1) ; 
 111             if ( HasFlag( wxALIGN_CENTER 
) ) 
 113                 pos 
+= ( m_width 
- width 
) / 2 ; 
 115             else if ( HasFlag( wxALIGN_RIGHT 
) ) 
 117                 pos 
+= ( m_width 
- width 
) ; 
 120         dc
.DrawText( paragraph
, pos 
, y
) ; 
 127 void wxStaticText::OnDraw( wxDC 
&dc 
) 
 129     if (m_width 
<= 0 || m_height 
<= 0) 
 133     wxRect rect(0,0,m_width,m_height) ; 
 134     dc.SetFont(*wxSMALL_FONT) ; 
 136     dc.DrawRectangle(rect) ; 
 138   if ( !IsWindowHilited( (WindowRef
) MacGetRootWindow() ) &&  
 139     ( GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE 
)  
 140       || GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
) ) ) 
 142     dc
.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ; 
 146     dc
.SetTextForeground( GetForegroundColour() ) ; 
 151   wxString text 
= m_label
; 
 153   while (i 
< text
.Length()) 
 156         if (text
[i
] == 13 || text
[i
] == 10) 
 158             DrawParagraph(dc
, paragraph
,y
); 
 163         paragraph 
+= text
[i
]; 
 167   if (paragraph
.Length() > 0) 
 168           DrawParagraph(dc
, paragraph
,y
); 
 171 void wxStaticText::OnPaint( wxPaintEvent 
&event 
)  
 177 wxSize 
wxStaticText::DoGetBestSize() const 
 179     int widthTextMax 
= 0, widthLine
, 
 180         heightTextTotal 
= 0, heightLineDefault 
= 0, heightLine 
= 0; 
 183     for ( const wxChar 
*pc 
= m_label
; ; pc
++ ) 
 185         if ( *pc 
== wxT('\n') || *pc 
== wxT('\0') ) 
 189                 // we can't use GetTextExtent - it will return 0 for both width 
 190                 // and height and an empty line should count in height 
 192                 if ( !heightLineDefault 
) 
 193                     heightLineDefault 
= heightLine
; 
 194                 if ( !heightLineDefault 
) 
 195                     GetTextExtent(_T("W"), NULL
, &heightLineDefault
); 
 197                 heightTextTotal 
+= heightLineDefault
; 
 199                 heightTextTotal
++;  // FIXME: why is this necessary? 
 203                 GetTextExtent(curLine
, &widthLine
, &heightLine
); 
 204                 if ( widthLine 
> widthTextMax 
) 
 205                     widthTextMax 
= widthLine
; 
 206                 heightTextTotal 
+= heightLine
; 
 208                 heightTextTotal
++;  // FIXME: why is this necessary? 
 211             if ( *pc 
== wxT('\n') ) { 
 224     return wxSize(widthTextMax
, heightTextTotal
); 
 227 void wxStaticText::SetLabel(const wxString
& st 
) 
 231         if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) ) 
 232                 SetSize( GetBestSize() ) ;