]> git.saurik.com Git - wxWidgets.git/blob - src/msw/stattext.cpp
Improve standard button spacing on Windows
[wxWidgets.git] / src / msw / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/stattext.cpp
3 // Purpose: wxStaticText
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "stattext.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_STATTEXT
24
25 #ifndef WX_PRECOMP
26 #include "wx/event.h"
27 #include "wx/app.h"
28 #include "wx/brush.h"
29 #include "wx/dcclient.h"
30 #include "wx/settings.h"
31 #endif
32
33 #include "wx/stattext.h"
34 #include "wx/msw/private.h"
35
36 #if wxUSE_EXTENDED_RTTI
37 WX_DEFINE_FLAGS( wxStaticTextStyle )
38
39 wxBEGIN_FLAGS( wxStaticTextStyle )
40 // new style border flags, we put them first to
41 // use them for streaming out
42 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
43 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
44 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
45 wxFLAGS_MEMBER(wxBORDER_RAISED)
46 wxFLAGS_MEMBER(wxBORDER_STATIC)
47 wxFLAGS_MEMBER(wxBORDER_NONE)
48
49 // old style border flags
50 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
51 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
52 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
53 wxFLAGS_MEMBER(wxRAISED_BORDER)
54 wxFLAGS_MEMBER(wxSTATIC_BORDER)
55 wxFLAGS_MEMBER(wxBORDER)
56
57 // standard window styles
58 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
59 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
60 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
61 wxFLAGS_MEMBER(wxWANTS_CHARS)
62 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
63 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
64 wxFLAGS_MEMBER(wxVSCROLL)
65 wxFLAGS_MEMBER(wxHSCROLL)
66
67 wxFLAGS_MEMBER(wxST_NO_AUTORESIZE)
68 wxFLAGS_MEMBER(wxALIGN_LEFT)
69 wxFLAGS_MEMBER(wxALIGN_RIGHT)
70 wxFLAGS_MEMBER(wxALIGN_CENTRE)
71
72 wxEND_FLAGS( wxStaticTextStyle )
73
74 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText, wxControl,"wx/stattext.h")
75
76 wxBEGIN_PROPERTIES_TABLE(wxStaticText)
77 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
78 wxPROPERTY_FLAGS( WindowStyle , wxStaticTextStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
79 wxEND_PROPERTIES_TABLE()
80
81 wxBEGIN_HANDLERS_TABLE(wxStaticText)
82 wxEND_HANDLERS_TABLE()
83
84 wxCONSTRUCTOR_6( wxStaticText , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
85 #else
86 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
87 #endif
88
89 bool wxStaticText::Create(wxWindow *parent,
90 wxWindowID id,
91 const wxString& label,
92 const wxPoint& pos,
93 const wxSize& size,
94 long style,
95 const wxString& name)
96 {
97 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
98 return false;
99
100 if ( !MSWCreateControl(wxT("STATIC"), label, pos, size) )
101 return false;
102
103 return true;
104 }
105
106 wxBorder wxStaticText::GetDefaultBorder() const
107 {
108 return wxBORDER_NONE;
109 }
110
111 WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const
112 {
113 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
114
115 // translate the alignment flags to the Windows ones
116 //
117 // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't
118 // test for them using & operator
119 if ( style & wxALIGN_CENTRE )
120 msStyle |= SS_CENTER;
121 else if ( style & wxALIGN_RIGHT )
122 msStyle |= SS_RIGHT;
123 else
124 msStyle |= SS_LEFT;
125
126 return msStyle;
127 }
128
129 WXHBRUSH wxStaticText::DoMSWControlColor(WXHDC pDC, wxColour colBg)
130 {
131 // If this control has a non-standard fg colour but still has the standard
132 // bg then we need to also give it a non-standard bg otherwise the fg
133 // setting has no effect.
134 WXHBRUSH hbr = wxControl::DoMSWControlColor(pDC, colBg);
135 if (!hbr && m_hasFgCol)
136 {
137 hbr = MSWGetBgBrushForChild(pDC, this);
138 if (!hbr)
139 {
140 HDC hdc = (HDC)pDC;
141 wxColour bg = GetBackgroundColour();
142 ::SetBkColor(hdc, wxColourToRGB(bg));
143 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(bg, wxSOLID);
144 hbr = (WXHBRUSH)brush->GetResourceHandle();
145 }
146 }
147 return hbr;
148 }
149
150 wxSize wxStaticText::DoGetBestSize() const
151 {
152 wxClientDC dc(wx_const_cast(wxStaticText *, this));
153 wxFont font(GetFont());
154 if (!font.Ok())
155 font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
156
157 dc.SetFont(font);
158
159 wxCoord widthTextMax, heightTextTotal;
160 dc.GetMultiLineTextExtent(GetLabel(), &widthTextMax, &heightTextTotal);
161
162 #ifdef __WXWINCE__
163 if ( widthTextMax )
164 widthTextMax += 2;
165 #endif // __WXWINCE__
166
167 return wxSize(widthTextMax, heightTextTotal);
168 }
169
170 void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags)
171 {
172 // we need to refresh the window after changing its size as the standard
173 // control doesn't always update itself properly
174 wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags);
175
176 Refresh();
177 }
178
179 void wxStaticText::SetLabel(const wxString& label)
180 {
181 wxStaticTextBase::SetLabel(label);
182
183 // adjust the size of the window to fit to the label unless autoresizing is
184 // disabled
185 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
186 {
187 InvalidateBestSize();
188 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
189 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
190 }
191 }
192
193
194 bool wxStaticText::SetFont(const wxFont& font)
195 {
196 bool ret = wxControl::SetFont(font);
197
198 // adjust the size of the window to fit to the label unless autoresizing is
199 // disabled
200 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
201 {
202 InvalidateBestSize();
203 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
204 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
205 }
206
207 return ret;
208 }
209
210 #endif // wxUSE_STATTEXT