1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxStaticText
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
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
;
54 m_label
= wxStripMenuCodes(label
) ;
56 bool ret
= wxControl::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
62 const wxString punct
= wxT(" ,.-;:!?");
64 void wxStaticText::DrawParagraph(wxDC
&dc
, wxString paragraph
, int &y
)
68 if (paragraph
.Length() == 0)
71 dc
.GetTextExtent( wxT("H"), &width
, &height
);
79 bool linedrawn
= true;
80 while( paragraph
.Length() > 0 )
82 dc
.GetTextExtent( paragraph
, &width
, &height
) ;
84 if ( width
> m_width
)
86 for ( size_t p
= paragraph
.Length() - 1 ; p
> 0 ; --p
)
88 if ((punct
.Find(paragraph
[p
]) != wxNOT_FOUND
) || !linedrawn
)
90 int blank
= (paragraph
[p
] == ' ') ? 0 : 1;
92 dc
.GetTextExtent( paragraph
.Left(p
+ blank
) , &width
, &height
) ;
94 if ( width
<= m_width
)
97 if ( HasFlag( wxALIGN_CENTER
) )
99 pos
+= ( m_width
- width
) / 2 ;
101 else if ( HasFlag( wxALIGN_RIGHT
) )
103 pos
+= ( m_width
- width
) ;
106 dc
.DrawText( paragraph
.Left(p
+ blank
), pos
, y
) ;
108 paragraph
= paragraph
.Mid(p
+1) ;
120 if ( HasFlag( wxALIGN_CENTER
) )
122 pos
+= ( m_width
- width
) / 2 ;
124 else if ( HasFlag( wxALIGN_RIGHT
) )
126 pos
+= ( m_width
- width
) ;
129 dc
.DrawText( paragraph
, pos
, y
) ;
130 paragraph
=wxEmptyString
;
136 void wxStaticText::OnDraw( wxDC
&dc
)
138 if (m_width
<= 0 || m_height
<= 0)
142 wxRect rect(0,0,m_width,m_height) ;
143 dc.SetFont(*wxSMALL_FONT) ;
145 dc.DrawRectangle(rect) ;
147 if ( !IsWindowHilited( (WindowRef
) MacGetRootWindow() ) &&
148 ( GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)
149 || GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
) ) )
151 dc
.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ;
155 dc
.SetTextForeground( GetForegroundColour() ) ;
160 wxString text
= m_label
;
162 while (i
< text
.Length())
165 if (text
[i
] == 13 || text
[i
] == 10)
167 DrawParagraph(dc
, paragraph
,y
);
168 paragraph
= wxEmptyString
;
172 paragraph
+= text
[i
];
176 if (paragraph
.Length() > 0)
177 DrawParagraph(dc
, paragraph
,y
);
180 void wxStaticText::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
186 wxSize
wxStaticText::DoGetBestSize() const
188 int widthTextMax
= 0, widthLine
,
189 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
192 for ( const wxChar
*pc
= m_label
; ; pc
++ )
194 if ( *pc
== wxT('\n') || *pc
== wxT('\0') )
198 // we can't use GetTextExtent - it will return 0 for both width
199 // and height and an empty line should count in height
201 if ( !heightLineDefault
)
202 heightLineDefault
= heightLine
;
203 if ( !heightLineDefault
)
204 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
206 heightTextTotal
+= heightLineDefault
;
208 heightTextTotal
++; // FIXME: why is this necessary?
212 GetTextExtent(curLine
, &widthLine
, &heightLine
);
213 if ( widthLine
> widthTextMax
)
214 widthTextMax
= widthLine
;
215 heightTextTotal
+= heightLine
;
217 heightTextTotal
++; // FIXME: why is this necessary?
220 if ( *pc
== wxT('\n') ) {
233 return wxSize(widthTextMax
, heightTextTotal
);
236 void wxStaticText::SetLabel(const wxString
& st
)
240 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
241 SetSize( GetBestSize() ) ;
247 bool wxStaticText::SetFont(const wxFont
& font
)
249 bool ret
= wxControl::SetFont(font
);
251 // adjust the size of the window to fit to the label unless autoresizing is
253 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
254 SetSize( GetBestSize() );