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 IMPLEMENT_DYNAMIC_CLASS(wxStaticText
, wxControl
)
29 #include "wx/mac/uma.h"
31 BEGIN_EVENT_TABLE(wxStaticText
, wxStaticTextBase
)
32 EVT_PAINT(wxStaticText::OnPaint
)
35 bool wxStaticText::Create(wxWindow
*parent
, wxWindowID id
,
36 const wxString
& label
,
42 m_label
= wxStripMenuCodes(label
) ;
44 if ( !wxControl::Create( parent
, id
, pos
, size
, style
,
45 wxDefaultValidator
, name
) )
55 const wxString punct
= wxT(" ,.-;:!?");
57 void wxStaticText::DrawParagraph(wxDC
&dc
, wxString paragraph
, int &y
)
61 if (paragraph
.Length() == 0)
64 dc
.GetTextExtent( wxT("H"), &width
, &height
);
72 bool linedrawn
= true;
73 while( paragraph
.Length() > 0 )
75 dc
.GetTextExtent( paragraph
, &width
, &height
) ;
77 if ( width
> m_width
)
79 for ( size_t p
= paragraph
.Length() - 1 ; p
> 0 ; --p
)
81 if ((punct
.Find(paragraph
[p
]) != wxNOT_FOUND
) || !linedrawn
)
83 int blank
= (paragraph
[p
] == ' ') ? 0 : 1;
85 dc
.GetTextExtent( paragraph
.Left(p
+ blank
) , &width
, &height
) ;
87 if ( width
<= m_width
)
90 if ( HasFlag( wxALIGN_CENTER
) )
92 pos
+= ( m_width
- width
) / 2 ;
94 else if ( HasFlag( wxALIGN_RIGHT
) )
96 pos
+= ( m_width
- width
) ;
99 dc
.DrawText( paragraph
.Left(p
+ blank
), pos
, y
) ;
101 paragraph
= paragraph
.Mid(p
+1) ;
113 if ( HasFlag( wxALIGN_CENTER
) )
115 pos
+= ( m_width
- width
) / 2 ;
117 else if ( HasFlag( wxALIGN_RIGHT
) )
119 pos
+= ( m_width
- width
) ;
122 dc
.DrawText( paragraph
, pos
, y
) ;
123 paragraph
=wxEmptyString
;
129 void wxStaticText::OnDraw( wxDC
&dc
)
131 if (m_width
<= 0 || m_height
<= 0)
135 wxRect rect(0,0,m_width,m_height) ;
136 dc.SetFont(*wxSMALL_FONT) ;
138 dc.DrawRectangle(rect) ;
140 if ( !IsWindowHilited( (WindowRef
) MacGetRootWindow() ) &&
141 ( GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)
142 || GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
) ) )
144 dc
.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ;
148 dc
.SetTextForeground( GetForegroundColour() ) ;
153 wxString text
= m_label
;
155 while (i
< text
.Length())
158 if (text
[i
] == 13 || text
[i
] == 10)
160 DrawParagraph(dc
, paragraph
,y
);
161 paragraph
= wxEmptyString
;
165 paragraph
+= text
[i
];
169 if (paragraph
.Length() > 0)
170 DrawParagraph(dc
, paragraph
,y
);
173 void wxStaticText::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
179 wxSize
wxStaticText::DoGetBestSize() const
181 int widthTextMax
= 0, widthLine
,
182 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
185 for ( const wxChar
*pc
= m_label
; ; pc
++ )
187 if ( *pc
== wxT('\n') || *pc
== wxT('\r') || *pc
== wxT('\0') )
191 // we can't use GetTextExtent - it will return 0 for both width
192 // and height and an empty line should count in height
194 if ( !heightLineDefault
)
195 heightLineDefault
= heightLine
;
196 if ( !heightLineDefault
)
197 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
199 heightTextTotal
+= heightLineDefault
;
201 heightTextTotal
++; // FIXME: why is this necessary?
205 GetTextExtent(curLine
, &widthLine
, &heightLine
);
206 if ( widthLine
> widthTextMax
)
207 widthTextMax
= widthLine
;
208 heightTextTotal
+= heightLine
;
210 heightTextTotal
++; // FIXME: why is this necessary?
213 if ( *pc
== wxT('\n') || *pc
== wxT('\r')) {
226 return wxSize(widthTextMax
, heightTextTotal
);
229 void wxStaticText::SetLabel(const wxString
& st
)
233 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
235 // temporary fix until layout measurement and drawing are in synch again
237 InvalidateBestSize();
238 SetSize( GetBestSize() ) ;
244 bool wxStaticText::SetFont(const wxFont
& font
)
246 bool ret
= wxControl::SetFont(font
);
250 // adjust the size of the window to fit to the label unless autoresizing is
252 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
254 // temporary fix until layout measurement and drawing are in synch again
256 InvalidateBestSize();
257 SetSize( GetBestSize() );