]> git.saurik.com Git - wxWidgets.git/blob - src/msw/stattext.cpp
Added wxPaintDCEx class, to handle the case where an HDC
[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 #ifdef __GNUG__
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 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
36
37 bool wxStaticText::Create(wxWindow *parent,
38 wxWindowID id,
39 const wxString& label,
40 const wxPoint& pos,
41 const wxSize& size,
42 long style,
43 const wxString& name)
44 {
45 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
46 return FALSE;
47
48 if ( !MSWCreateControl(wxT("STATIC"), label, pos, size) )
49 return FALSE;
50
51 return TRUE;
52 }
53
54 wxBorder wxStaticText::GetDefaultBorder() const
55 {
56 return wxBORDER_NONE;
57 }
58
59 WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const
60 {
61 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
62
63 // translate the alignment flags to the Windows ones
64 //
65 // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't
66 // test for them using & operator
67 if ( style & wxALIGN_CENTRE )
68 msStyle |= SS_CENTER;
69 else if ( style & wxALIGN_RIGHT )
70 msStyle |= SS_RIGHT;
71 else
72 msStyle |= SS_LEFT;
73
74 return msStyle;
75 }
76
77 wxSize wxStaticText::DoGetBestSize() const
78 {
79 wxString text(wxGetWindowText(GetHWND()));
80
81 int widthTextMax = 0, widthLine,
82 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
83
84 bool lastWasAmpersand = FALSE;
85
86 wxString curLine;
87 for ( const wxChar *pc = text; ; pc++ )
88 {
89 if ( *pc == wxT('\n') || *pc == wxT('\0') )
90 {
91 if ( !curLine )
92 {
93 // we can't use GetTextExtent - it will return 0 for both width
94 // and height and an empty line should count in height
95 // calculation
96 if ( !heightLineDefault )
97 heightLineDefault = heightLine;
98 if ( !heightLineDefault )
99 GetTextExtent(_T("W"), NULL, &heightLineDefault);
100
101 heightTextTotal += heightLineDefault;
102 }
103 else
104 {
105 GetTextExtent(curLine, &widthLine, &heightLine);
106 if ( widthLine > widthTextMax )
107 widthTextMax = widthLine;
108 heightTextTotal += heightLine;
109 }
110
111 if ( *pc == wxT('\n') )
112 {
113 curLine.Empty();
114 }
115 else
116 {
117 // the end of string
118 break;
119 }
120 }
121 else
122 {
123 // we shouldn't take into account the '&' which just introduces the
124 // mnemonic characters and so are not shown on the screen -- except
125 // when it is preceded by another '&' in which case it stands for a
126 // literal ampersand
127 if ( *pc == _T('&') )
128 {
129 if ( !lastWasAmpersand )
130 {
131 lastWasAmpersand = TRUE;
132
133 // skip the statement adding pc to curLine below
134 continue;
135 }
136
137 // it is a literal ampersand
138 lastWasAmpersand = FALSE;
139 }
140
141 curLine += *pc;
142 }
143 }
144
145 return wxSize(widthTextMax, heightTextTotal);
146 }
147
148 void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags)
149 {
150 // we need to refresh the window after changing its size as the standard
151 // control doesn't always update itself properly
152 wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags);
153
154 Refresh();
155 }
156
157 void wxStaticText::SetLabel(const wxString& label)
158 {
159 wxStaticTextBase::SetLabel(label);
160
161 // adjust the size of the window to fit to the label unless autoresizing is
162 // disabled
163 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
164 {
165 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
166 }
167 }
168
169
170 bool wxStaticText::SetFont(const wxFont& font)
171 {
172 bool ret = wxControl::SetFont(font);
173
174 // adjust the size of the window to fit to the label unless autoresizing is
175 // disabled
176 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
177 {
178 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
179 }
180
181 return ret;
182 }
183
184 #endif // wxUSE_STATTEXT