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
)
70 bool linedrawn
= true;
71 while( paragraph
.Length() > 0 )
73 dc
.GetTextExtent( paragraph
, &width
, &height
) ;
75 if ( width
> m_width
)
77 for ( int p
= paragraph
.Length() -1 ; p
> 0 ; --p
)
79 if ((punct
.Find(paragraph
[p
]) != wxNOT_FOUND
) || !linedrawn
)
81 int blank
= (paragraph
[p
] == ' ') ? 0 : 1;
83 dc
.GetTextExtent( paragraph
.Left(p
+ blank
) , &width
, &height
) ;
85 if ( width
<= m_width
)
88 if ( HasFlag( wxALIGN_CENTER
) )
90 pos
+= ( m_width
- width
) / 2 ;
92 else if ( HasFlag( wxALIGN_RIGHT
) )
94 pos
+= ( m_width
- width
) ;
97 dc
.DrawText( paragraph
.Left(p
+ blank
), pos
, y
) ;
99 paragraph
= paragraph
.Mid(p
+1) ;
111 if ( HasFlag( wxALIGN_CENTER
) )
113 pos
+= ( m_width
- width
) / 2 ;
115 else if ( HasFlag( wxALIGN_RIGHT
) )
117 pos
+= ( m_width
- width
) ;
120 dc
.DrawText( paragraph
, pos
, y
) ;
127 void wxStaticText::OnDraw( wxDC
&dc
)
129 if (m_width
<= 0 || m_height
<= 0)
132 if ( !IsWindowHilited( (WindowRef
) MacGetRootWindow() ) &&
133 ( GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
)
134 || GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
) ) )
136 dc
.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ;
140 dc
.SetTextForeground( GetForegroundColour() ) ;
145 wxString text
= m_label
;
146 while (i
< text
.Length())
148 paragraph
+= text
[i
];
150 if (text
[i
] == 13 || text
[i
] == 10)
151 DrawParagraph(dc
, paragraph
);
155 if (paragraph
.Length() > 0)
156 DrawParagraph(dc
, paragraph
);
159 void wxStaticText::OnPaint( wxPaintEvent
&event
)
165 wxSize
wxStaticText::DoGetBestSize() const
167 int widthTextMax
= 0, widthLine
,
168 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
171 for ( const wxChar
*pc
= m_label
; ; pc
++ )
173 if ( *pc
== wxT('\n') || *pc
== wxT('\0') )
177 // we can't use GetTextExtent - it will return 0 for both width
178 // and height and an empty line should count in height
180 if ( !heightLineDefault
)
181 heightLineDefault
= heightLine
;
182 if ( !heightLineDefault
)
183 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
185 heightTextTotal
+= heightLineDefault
;
187 heightTextTotal
++; // FIXME: why is this necessary?
191 GetTextExtent(curLine
, &widthLine
, &heightLine
);
192 if ( widthLine
> widthTextMax
)
193 widthTextMax
= widthLine
;
194 heightTextTotal
+= heightLine
;
196 heightTextTotal
++; // FIXME: why is this necessary?
199 if ( *pc
== wxT('\n') ) {
212 return wxSize(widthTextMax
, heightTextTotal
);
215 void wxStaticText::SetLabel(const wxString
& st
)
219 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
220 SetSize( GetBestSize() ) ;