1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxStaticText
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/stattext.h"
14 #include "wx/notebook.h"
15 #include "wx/tabctrl.h"
17 #include "wx/dcclient.h"
19 #include "wx/settings.h"
23 IMPLEMENT_DYNAMIC_CLASS(wxStaticText
, wxControl
)
25 #include "wx/mac/uma.h"
27 BEGIN_EVENT_TABLE(wxStaticText
, wxStaticTextBase
)
28 EVT_PAINT(wxStaticText::OnPaint
)
31 bool wxStaticText::Create(wxWindow
*parent
, wxWindowID id
,
32 const wxString
& label
,
38 m_label
= wxStripMenuCodes(label
) ;
40 if ( !wxControl::Create( parent
, id
, pos
, size
, style
,
41 wxDefaultValidator
, name
) )
51 const wxString punct
= wxT(" ,.-;:!?");
53 void wxStaticText::DrawParagraph(wxDC
&dc
, wxString paragraph
, int &y
)
57 if (paragraph
.Length() == 0)
60 dc
.GetTextExtent( wxT("H"), &width
, &height
);
68 bool linedrawn
= true;
69 while( paragraph
.Length() > 0 )
71 dc
.GetTextExtent( paragraph
, &width
, &height
) ;
73 if ( width
> m_width
)
75 for ( size_t p
= paragraph
.Length() - 1 ; p
> 0 ; --p
)
77 if ((punct
.Find(paragraph
[p
]) != wxNOT_FOUND
) || !linedrawn
)
79 int blank
= (paragraph
[p
] == ' ') ? 0 : 1;
81 dc
.GetTextExtent( paragraph
.Left(p
+ blank
) , &width
, &height
) ;
83 if ( width
<= m_width
)
86 if ( HasFlag( wxALIGN_CENTER
) )
88 pos
+= ( m_width
- width
) / 2 ;
90 else if ( HasFlag( wxALIGN_RIGHT
) )
92 pos
+= ( m_width
- width
) ;
95 dc
.DrawText( paragraph
.Left(p
+ blank
), pos
, y
) ;
97 paragraph
= paragraph
.Mid(p
+1) ;
109 if ( HasFlag( wxALIGN_CENTER
) )
111 pos
+= ( m_width
- width
) / 2 ;
113 else if ( HasFlag( wxALIGN_RIGHT
) )
115 pos
+= ( m_width
- width
) ;
118 dc
.DrawText( paragraph
, pos
, y
) ;
119 paragraph
=wxEmptyString
;
125 void wxStaticText::OnDraw( wxDC
&dc
)
127 if (m_width
<= 0 || m_height
<= 0)
131 wxRect rect(0,0,m_width,m_height) ;
132 dc.SetFont(*wxSMALL_FONT) ;
134 dc.DrawRectangle(rect) ;
136 if ( !IsWindowHilited( (WindowRef
) MacGetRootWindow() ) &&
137 ( GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)
138 || GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
) ) )
140 dc
.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ;
144 dc
.SetTextForeground( GetForegroundColour() ) ;
149 wxString text
= m_label
;
151 while (i
< text
.Length())
154 if (text
[i
] == 13 || text
[i
] == 10)
156 DrawParagraph(dc
, paragraph
,y
);
157 paragraph
= wxEmptyString
;
161 paragraph
+= text
[i
];
165 if (paragraph
.Length() > 0)
166 DrawParagraph(dc
, paragraph
,y
);
169 void wxStaticText::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
175 wxSize
wxStaticText::DoGetBestSize() const
177 int widthTextMax
= 0, widthLine
,
178 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
181 for ( const wxChar
*pc
= m_label
; ; pc
++ )
183 if ( *pc
== wxT('\n') || *pc
== wxT('\r') || *pc
== wxT('\0') )
187 // we can't use GetTextExtent - it will return 0 for both width
188 // and height and an empty line should count in height
190 if ( !heightLineDefault
)
191 heightLineDefault
= heightLine
;
192 if ( !heightLineDefault
)
193 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
195 heightTextTotal
+= heightLineDefault
;
197 heightTextTotal
++; // FIXME: why is this necessary?
201 GetTextExtent(curLine
, &widthLine
, &heightLine
);
202 if ( widthLine
> widthTextMax
)
203 widthTextMax
= widthLine
;
204 heightTextTotal
+= heightLine
;
206 heightTextTotal
++; // FIXME: why is this necessary?
209 if ( *pc
== wxT('\n') || *pc
== wxT('\r')) {
222 return wxSize(widthTextMax
, heightTextTotal
);
225 void wxStaticText::SetLabel(const wxString
& st
)
229 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
231 // temporary fix until layout measurement and drawing are in synch again
233 InvalidateBestSize();
234 SetSize( GetBestSize() ) ;
240 bool wxStaticText::SetFont(const wxFont
& font
)
242 bool ret
= wxControl::SetFont(font
);
246 // adjust the size of the window to fit to the label unless autoresizing is
248 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
250 // temporary fix until layout measurement and drawing are in synch again
252 InvalidateBestSize();
253 SetSize( GetBestSize() );