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
168 int widthTextMax
= 0, widthLine
,
169 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
172 for ( const wxChar
*pc
= m_label
; ; pc
++ )
174 if ( *pc
== wxT('\n') || *pc
== wxT('\0') )
178 // we can't use GetTextExtent - it will return 0 for both width
179 // and height and an empty line should count in height
181 if ( !heightLineDefault
)
182 heightLineDefault
= heightLine
;
183 if ( !heightLineDefault
)
184 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
186 heightTextTotal
+= heightLineDefault
;
188 heightTextTotal
++; // FIXME: why is this necessary?
192 GetTextExtent(curLine
, &widthLine
, &heightLine
);
193 if ( widthLine
> widthTextMax
)
194 widthTextMax
= widthLine
;
195 heightTextTotal
+= heightLine
;
197 heightTextTotal
++; // FIXME: why is this necessary?
200 if ( *pc
== wxT('\n') ) {
213 return wxSize(widthTextMax
, heightTextTotal
);
216 void wxStaticText::SetLabel(const wxString
& st
)
220 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
221 SetSize( GetBestSize() ) ;