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
)
68 if (paragraph
.Length() == 0)
71 dc
.GetTextExtent( "H", &width
, &height
);
80 bool linedrawn
= true;
81 while( paragraph
.Length() > 0 )
83 dc
.GetTextExtent( paragraph
, &width
, &height
) ;
85 if ( width
> m_width
)
87 for ( int p
= paragraph
.Length() -1 ; p
> 0 ; --p
)
89 if ((punct
.Find(paragraph
[p
]) != wxNOT_FOUND
) || !linedrawn
)
91 int blank
= (paragraph
[p
] == ' ') ? 0 : 1;
93 dc
.GetTextExtent( paragraph
.Left(p
+ blank
) , &width
, &height
) ;
95 if ( width
<= m_width
)
98 if ( HasFlag( wxALIGN_CENTER
) )
100 pos
+= ( m_width
- width
) / 2 ;
102 else if ( HasFlag( wxALIGN_RIGHT
) )
104 pos
+= ( m_width
- width
) ;
107 dc
.DrawText( paragraph
.Left(p
+ blank
), pos
, y
) ;
109 paragraph
= paragraph
.Mid(p
+1) ;
121 if ( HasFlag( wxALIGN_CENTER
) )
123 pos
+= ( m_width
- width
) / 2 ;
125 else if ( HasFlag( wxALIGN_RIGHT
) )
127 pos
+= ( m_width
- width
) ;
130 dc
.DrawText( paragraph
, pos
, y
) ;
137 void wxStaticText::OnDraw( wxDC
&dc
)
139 if (m_width
<= 0 || m_height
<= 0)
143 wxRect rect(0,0,m_width,m_height) ;
144 dc.SetFont(*wxSMALL_FONT) ;
146 dc.DrawRectangle(rect) ;
148 if ( !IsWindowHilited( (WindowRef
) MacGetRootWindow() ) &&
149 ( GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)
150 || GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
) ) )
152 dc
.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ;
156 dc
.SetTextForeground( GetForegroundColour() ) ;
161 wxString text
= m_label
;
163 while (i
< text
.Length())
166 if (text
[i
] == 13 || text
[i
] == 10)
168 DrawParagraph(dc
, paragraph
,y
);
173 paragraph
+= text
[i
];
177 if (paragraph
.Length() > 0)
178 DrawParagraph(dc
, paragraph
,y
);
181 void wxStaticText::OnPaint( wxPaintEvent
&event
)
187 wxSize
wxStaticText::DoGetBestSize() const
189 int widthTextMax
= 0, widthLine
,
190 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
193 for ( const wxChar
*pc
= m_label
; ; pc
++ )
195 if ( *pc
== wxT('\n') || *pc
== wxT('\0') )
199 // we can't use GetTextExtent - it will return 0 for both width
200 // and height and an empty line should count in height
202 if ( !heightLineDefault
)
203 heightLineDefault
= heightLine
;
204 if ( !heightLineDefault
)
205 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
207 heightTextTotal
+= heightLineDefault
;
209 heightTextTotal
++; // FIXME: why is this necessary?
213 GetTextExtent(curLine
, &widthLine
, &heightLine
);
214 if ( widthLine
> widthTextMax
)
215 widthTextMax
= widthLine
;
216 heightTextTotal
+= heightLine
;
218 heightTextTotal
++; // FIXME: why is this necessary?
221 if ( *pc
== wxT('\n') ) {
234 return wxSize(widthTextMax
, heightTextTotal
);
237 void wxStaticText::SetLabel(const wxString
& st
)
241 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
242 SetSize( GetBestSize() ) ;
248 bool wxStaticText::SetFont(const wxFont
& font
)
250 bool ret
= wxControl::SetFont(font
);
252 // adjust the size of the window to fit to the label unless autoresizing is
254 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
255 SetSize( GetBestSize() );