1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxStaticText
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWidgets 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
,
44 m_label
= wxStripMenuCodes(label
) ;
46 if ( !wxControl::Create( parent
, id
, pos
, size
, style
,
47 wxDefaultValidator
, name
) )
57 const wxString punct
= wxT(" ,.-;:!?");
59 void wxStaticText::DrawParagraph(wxDC
&dc
, wxString paragraph
, int &y
)
63 if (paragraph
.Length() == 0)
66 dc
.GetTextExtent( wxT("H"), &width
, &height
);
74 bool linedrawn
= true;
75 while( paragraph
.Length() > 0 )
77 dc
.GetTextExtent( paragraph
, &width
, &height
) ;
79 if ( width
> m_width
)
81 for ( size_t p
= paragraph
.Length() - 1 ; p
> 0 ; --p
)
83 if ((punct
.Find(paragraph
[p
]) != wxNOT_FOUND
) || !linedrawn
)
85 int blank
= (paragraph
[p
] == ' ') ? 0 : 1;
87 dc
.GetTextExtent( paragraph
.Left(p
+ blank
) , &width
, &height
) ;
89 if ( width
<= m_width
)
92 if ( HasFlag( wxALIGN_CENTER
) )
94 pos
+= ( m_width
- width
) / 2 ;
96 else if ( HasFlag( wxALIGN_RIGHT
) )
98 pos
+= ( m_width
- width
) ;
101 dc
.DrawText( paragraph
.Left(p
+ blank
), pos
, y
) ;
103 paragraph
= paragraph
.Mid(p
+1) ;
115 if ( HasFlag( wxALIGN_CENTER
) )
117 pos
+= ( m_width
- width
) / 2 ;
119 else if ( HasFlag( wxALIGN_RIGHT
) )
121 pos
+= ( m_width
- width
) ;
124 dc
.DrawText( paragraph
, pos
, y
) ;
125 paragraph
=wxEmptyString
;
131 void wxStaticText::OnDraw( wxDC
&dc
)
133 if (m_width
<= 0 || m_height
<= 0)
137 wxRect rect(0,0,m_width,m_height) ;
138 dc.SetFont(*wxSMALL_FONT) ;
140 dc.DrawRectangle(rect) ;
142 if ( !IsWindowHilited( (WindowRef
) MacGetRootWindow() ) &&
143 ( GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)
144 || GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
) ) )
146 dc
.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ;
150 dc
.SetTextForeground( GetForegroundColour() ) ;
155 wxString text
= m_label
;
157 while (i
< text
.Length())
160 if (text
[i
] == 13 || text
[i
] == 10)
162 DrawParagraph(dc
, paragraph
,y
);
163 paragraph
= wxEmptyString
;
167 paragraph
+= text
[i
];
171 if (paragraph
.Length() > 0)
172 DrawParagraph(dc
, paragraph
,y
);
175 void wxStaticText::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
181 wxSize
wxStaticText::DoGetBestSize() const
183 int widthTextMax
= 0, widthLine
,
184 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
187 for ( const wxChar
*pc
= m_label
; ; pc
++ )
189 if ( *pc
== wxT('\n') || *pc
== wxT('\r') || *pc
== wxT('\0') )
193 // we can't use GetTextExtent - it will return 0 for both width
194 // and height and an empty line should count in height
196 if ( !heightLineDefault
)
197 heightLineDefault
= heightLine
;
198 if ( !heightLineDefault
)
199 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
201 heightTextTotal
+= heightLineDefault
;
203 heightTextTotal
++; // FIXME: why is this necessary?
207 GetTextExtent(curLine
, &widthLine
, &heightLine
);
208 if ( widthLine
> widthTextMax
)
209 widthTextMax
= widthLine
;
210 heightTextTotal
+= heightLine
;
212 heightTextTotal
++; // FIXME: why is this necessary?
215 if ( *pc
== wxT('\n') || *pc
== wxT('\r')) {
228 return wxSize(widthTextMax
, heightTextTotal
);
231 void wxStaticText::SetLabel(const wxString
& st
)
235 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
237 // temporary fix until layout measurement and drawing are in synch again
239 SetSize( GetBestSize() ) ;
245 bool wxStaticText::SetFont(const wxFont
& font
)
247 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
) )
255 // temporary fix until layout measurement and drawing are in synch again
257 SetSize( GetBestSize() );