Add markup support to generic wxStaticText implementation.
[wxWidgets.git] / src / generic / stattextg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/stattextg.cpp
3 // Purpose: wxGenericStaticText
4 // Author: Marcin Wojdyr
5 // Created: 2008-06-26
6 // RCS-ID: $Id$
7 // Copyright: Marcin Wojdyr
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #if wxUSE_STATTEXT
18
19 #ifndef WX_PRECOMP
20 #include "wx/dcclient.h"
21 #include "wx/settings.h"
22 #include "wx/validate.h"
23 #endif
24
25 #include "wx/generic/stattextg.h"
26
27 #if wxUSE_MARKUP
28 #include "wx/generic/private/markuptext.h"
29 #endif // wxUSE_MARKUP
30
31 IMPLEMENT_DYNAMIC_CLASS(wxGenericStaticText, wxStaticTextBase)
32
33
34 bool wxGenericStaticText::Create(wxWindow *parent,
35 wxWindowID id,
36 const wxString &label,
37 const wxPoint &pos,
38 const wxSize &size,
39 long style,
40 const wxString &name)
41 {
42 if ( !wxControl::Create(parent, id, pos, size, style,
43 wxDefaultValidator, name) )
44 return false;
45
46 SetLabel(label);
47 SetInitialSize(size);
48 Connect(wxEVT_PAINT, wxPaintEventHandler(wxGenericStaticText::OnPaint));
49 return true;
50 }
51
52 wxGenericStaticText::~wxGenericStaticText()
53 {
54 #if wxUSE_MARKUP
55 delete m_markupText;
56 #endif // wxUSE_MARKUP
57 }
58
59 void wxGenericStaticText::DoDrawLabel(wxDC& dc, const wxRect& rect)
60 {
61 #if wxUSE_MARKUP
62 if ( m_markupText )
63 m_markupText->Render(dc, rect, wxMarkupText::Render_ShowAccels);
64 else
65 #endif // wxUSE_MARKUP
66 dc.DrawLabel(m_label, rect, GetAlignment(), m_mnemonic);
67 }
68
69 void wxGenericStaticText::OnPaint(wxPaintEvent& WXUNUSED(event))
70 {
71 wxPaintDC dc(this);
72
73 wxRect rect = GetClientRect();
74 if ( IsEnabled() )
75 {
76 dc.SetTextForeground(
77 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT));
78 }
79 else // paint disabled text
80 {
81 // draw shadow of the text
82 dc.SetTextForeground(
83 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNHIGHLIGHT));
84 wxRect rectShadow = rect;
85 rectShadow.Offset(1, 1);
86 DoDrawLabel(dc, rectShadow);
87 dc.SetTextForeground(
88 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW));
89 }
90 DoDrawLabel(dc, rect);
91 }
92
93
94 wxSize wxGenericStaticText::DoGetBestClientSize() const
95 {
96 wxClientDC dc(wxConstCast(this, wxGenericStaticText));
97
98 #if wxUSE_MARKUP
99 if ( m_markupText )
100 return m_markupText->Measure(dc);
101 #endif // wxUSE_MARKUP
102
103 return dc.GetMultiLineTextExtent(GetLabel());
104 }
105
106 void wxGenericStaticText::SetLabel(const wxString& label)
107 {
108 wxControl::SetLabel(label);
109 DoSetLabel(GetEllipsizedLabel());
110 if ( !HasFlag(wxST_NO_AUTORESIZE) && !IsEllipsized() )
111 InvalidateBestSize();
112
113 #if wxUSE_MARKUP
114 if ( m_markupText )
115 {
116 delete m_markupText;
117 m_markupText = NULL;
118 }
119 #endif // wxUSE_MARKUP
120
121 Refresh();
122 }
123
124 void wxGenericStaticText::DoSetLabel(const wxString& label)
125 {
126 m_mnemonic = FindAccelIndex(label, &m_label);
127 }
128
129 #if wxUSE_MARKUP
130
131 bool wxGenericStaticText::DoSetLabelMarkup(const wxString& markup)
132 {
133 if ( !wxStaticTextBase::DoSetLabelMarkup(markup) )
134 return false;
135
136 if ( !m_markupText )
137 m_markupText = new wxMarkupText(markup);
138 else
139 m_markupText->SetMarkup(markup);
140
141 if ( !HasFlag(wxST_NO_AUTORESIZE) )
142 InvalidateBestSize();
143
144 Refresh();
145
146 return true;
147 }
148
149 #endif // wxUSE_MARKUP
150
151 bool wxGenericStaticText::SetFont(const wxFont &font)
152 {
153 if ( !wxControl::SetFont(font) )
154 return false;
155 if ( !HasFlag(wxST_NO_AUTORESIZE) )
156 InvalidateBestSize();
157 Refresh();
158 return true;
159 }
160
161 void wxGenericStaticText::DoSetSize(int x, int y, int width, int height,
162 int sizeFlags)
163 {
164 wxStaticTextBase::DoSetSize(x, y, width, height, sizeFlags);
165 UpdateLabel();
166 }
167
168
169 #endif // wxUSE_STATTEXT