add wxGenericStaticText (#9656)
[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/dc.h"
21 #include "wx/validate.h"
22 #endif
23
24 #include "wx/generic/stattextg.h"
25
26
27 IMPLEMENT_DYNAMIC_CLASS(wxGenericStaticText, wxStaticTextBase)
28
29
30 bool wxGenericStaticText::Create(wxWindow *parent,
31 wxWindowID id,
32 const wxString &label,
33 const wxPoint &pos,
34 const wxSize &size,
35 long style,
36 const wxString &name)
37 {
38 if ( !wxControl::Create(parent, id, pos, size, style,
39 wxDefaultValidator, name) )
40 return false;
41
42 SetLabel(label);
43 SetInitialSize(size);
44 Connect(wxEVT_PAINT, wxPaintEventHandler(wxGenericStaticText::OnPaint));
45 return true;
46 }
47
48 void wxGenericStaticText::OnPaint(wxPaintEvent& WXUNUSED(event))
49 {
50 if ( m_label.empty() )
51 return;
52 wxPaintDC dc(this);
53 PrepareDC(dc);
54
55 wxRect rect = GetClientRect();
56 if ( IsEnabled() )
57 {
58 dc.SetTextForeground(
59 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT));
60 }
61 else // paint disabled text
62 {
63 // draw shadow of the text
64 dc.SetTextForeground(
65 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNHIGHLIGHT));
66 wxRect rectShadow = rect;
67 rectShadow.Offset(1, 1);
68 dc.DrawLabel(m_label, rectShadow, GetAlignment(), m_mnemonic);
69 dc.SetTextForeground(
70 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW));
71 }
72 dc.DrawLabel(m_label, wxNullBitmap, rect, GetAlignment(), m_mnemonic);
73 }
74
75
76 wxSize wxGenericStaticText::DoGetBestClientSize() const
77 {
78 wxClientDC dc(wxConstCast(this, wxGenericStaticText));
79 wxCoord width, height;
80 dc.GetMultiLineTextExtent(GetLabel(), &width, &height);
81 return wxSize(width, height);
82 }
83
84 wxSize wxGenericStaticText::DoGetBestSize() const
85 {
86 wxSize ret = DoGetBestClientSize();
87 CacheBestSize(ret);
88 return ret;
89 }
90
91
92 void wxGenericStaticText::SetLabel(const wxString& label)
93 {
94 wxControl::SetLabel(label);
95 DoSetLabel(GetEllipsizedLabelWithoutMarkup());
96 if ( !HasFlag(wxST_NO_AUTORESIZE) && !IsEllipsized() )
97 InvalidateBestSize();
98 Refresh();
99 }
100
101 void wxGenericStaticText::DoSetLabel(const wxString& label)
102 {
103 m_mnemonic = FindAccelIndex(label, &m_label);
104 }
105
106 bool wxGenericStaticText::SetFont(const wxFont &font)
107 {
108 if ( !wxControl::SetFont(font) )
109 return false;
110 if ( !HasFlag(wxST_NO_AUTORESIZE) )
111 InvalidateBestSize();
112 Refresh();
113 return true;
114 }
115
116 void wxGenericStaticText::DoSetSize(int x, int y, int width, int height,
117 int sizeFlags)
118 {
119 wxStaticTextBase::DoSetSize(x, y, width, height, sizeFlags);
120 UpdateLabel();
121 }
122
123
124 #endif // wxUSE_STATTEXT