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