]> git.saurik.com Git - wxWidgets.git/blob - src/msw/stattext.cpp
reuse code from wxDC::GetMultiLineTextExtent() instead of duplicating it here
[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 #endif
30
31 #include "wx/stattext.h"
32 #include "wx/msw/private.h"
33 #include <stdio.h>
34
35 #if wxUSE_EXTENDED_RTTI
36 WX_DEFINE_FLAGS( wxStaticTextStyle )
37
38 wxBEGIN_FLAGS( wxStaticTextStyle )
39 // new style border flags, we put them first to
40 // use them for streaming out
41 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
42 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
43 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
44 wxFLAGS_MEMBER(wxBORDER_RAISED)
45 wxFLAGS_MEMBER(wxBORDER_STATIC)
46 wxFLAGS_MEMBER(wxBORDER_NONE)
47
48 // old style border flags
49 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
50 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
51 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
52 wxFLAGS_MEMBER(wxRAISED_BORDER)
53 wxFLAGS_MEMBER(wxSTATIC_BORDER)
54 wxFLAGS_MEMBER(wxBORDER)
55
56 // standard window styles
57 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
58 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
59 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
60 wxFLAGS_MEMBER(wxWANTS_CHARS)
61 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
62 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
63 wxFLAGS_MEMBER(wxVSCROLL)
64 wxFLAGS_MEMBER(wxHSCROLL)
65
66 wxFLAGS_MEMBER(wxST_NO_AUTORESIZE)
67 wxFLAGS_MEMBER(wxALIGN_LEFT)
68 wxFLAGS_MEMBER(wxALIGN_RIGHT)
69 wxFLAGS_MEMBER(wxALIGN_CENTRE)
70
71 wxEND_FLAGS( wxStaticTextStyle )
72
73 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText, wxControl,"wx/stattext.h")
74
75 wxBEGIN_PROPERTIES_TABLE(wxStaticText)
76 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
77 wxPROPERTY_FLAGS( WindowStyle , wxStaticTextStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
78 wxEND_PROPERTIES_TABLE()
79
80 wxBEGIN_HANDLERS_TABLE(wxStaticText)
81 wxEND_HANDLERS_TABLE()
82
83 wxCONSTRUCTOR_6( wxStaticText , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
84 #else
85 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
86 #endif
87
88 bool wxStaticText::Create(wxWindow *parent,
89 wxWindowID id,
90 const wxString& label,
91 const wxPoint& pos,
92 const wxSize& size,
93 long style,
94 const wxString& name)
95 {
96 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
97 return false;
98
99 if ( !MSWCreateControl(wxT("STATIC"), label, pos, size) )
100 return false;
101
102 return true;
103 }
104
105 wxBorder wxStaticText::GetDefaultBorder() const
106 {
107 return wxBORDER_NONE;
108 }
109
110 WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const
111 {
112 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
113
114 // translate the alignment flags to the Windows ones
115 //
116 // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't
117 // test for them using & operator
118 if ( style & wxALIGN_CENTRE )
119 msStyle |= SS_CENTER;
120 else if ( style & wxALIGN_RIGHT )
121 msStyle |= SS_RIGHT;
122 else
123 msStyle |= SS_LEFT;
124
125 return msStyle;
126 }
127
128 wxSize wxStaticText::DoGetBestSize() const
129 {
130 wxClientDC dc(wx_const_cast(wxStaticText *, this));
131 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
132
133 wxCoord widthTextMax, heightTextTotal;
134 dc.GetMultiLineTextExtent(GetLabel(), &widthTextMax, &heightTextTotal);
135
136 #ifdef __WXWINCE__
137 if ( widthTextMax )
138 widthTextMax += 2;
139 #endif // __WXWINCE__
140
141 return wxSize(widthTextMax, heightTextTotal);
142 }
143
144 void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags)
145 {
146 // we need to refresh the window after changing its size as the standard
147 // control doesn't always update itself properly
148 wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags);
149
150 Refresh();
151 }
152
153 void wxStaticText::SetLabel(const wxString& label)
154 {
155 wxStaticTextBase::SetLabel(label);
156
157 // adjust the size of the window to fit to the label unless autoresizing is
158 // disabled
159 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
160 {
161 InvalidateBestSize();
162 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
163 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
164 }
165 }
166
167
168 bool wxStaticText::SetFont(const wxFont& font)
169 {
170 bool ret = wxControl::SetFont(font);
171
172 // adjust the size of the window to fit to the label unless autoresizing is
173 // disabled
174 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
175 {
176 InvalidateBestSize();
177 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
178 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
179 }
180
181 return ret;
182 }
183
184 #endif // wxUSE_STATTEXT