]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: 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 and Markus Holzem | |
c085e333 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
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 | #ifndef WX_PRECOMP | |
2432b92d | 24 | #include "wx/event.h" |
2bda0e17 | 25 | #include "wx/app.h" |
2432b92d | 26 | #include "wx/brush.h" |
2bda0e17 KB |
27 | #endif |
28 | ||
29 | #include "wx/stattext.h" | |
30 | #include "wx/msw/private.h" | |
31 | #include <stdio.h> | |
32 | ||
33 | #if !USE_SHARED_LIBRARY | |
34 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) | |
35 | #endif | |
36 | ||
debe6624 | 37 | bool wxStaticText::Create(wxWindow *parent, wxWindowID id, |
2bda0e17 KB |
38 | const wxString& label, |
39 | const wxPoint& pos, | |
40 | const wxSize& size, | |
debe6624 | 41 | long style, |
2bda0e17 KB |
42 | const wxString& name) |
43 | { | |
44 | SetName(name); | |
45 | if (parent) parent->AddChild(this); | |
46 | ||
fd71308f JS |
47 | SetBackgroundColour(parent->GetBackgroundColour()) ; |
48 | SetForegroundColour(parent->GetForegroundColour()) ; | |
2bda0e17 KB |
49 | |
50 | if ( id == -1 ) | |
c085e333 | 51 | m_windowId = (int)NewControlId(); |
2bda0e17 | 52 | else |
c085e333 | 53 | m_windowId = id; |
2bda0e17 KB |
54 | |
55 | int x = pos.x; | |
56 | int y = pos.y; | |
57 | int width = size.x; | |
58 | int height = size.y; | |
59 | ||
60 | m_windowStyle = style; | |
61 | ||
62 | long msStyle = WS_CHILD|WS_VISIBLE; | |
63 | if (m_windowStyle & wxALIGN_CENTRE) | |
64 | msStyle |= SS_CENTER; | |
65 | else if (m_windowStyle & wxALIGN_RIGHT) | |
66 | msStyle |= SS_RIGHT; | |
67 | else | |
68 | msStyle |= SS_LEFT; | |
69 | ||
70 | // Even with extended styles, need to combine with WS_BORDER | |
71 | // for them to look right. | |
c085e333 | 72 | if ( wxStyleHasBorder(m_windowStyle) ) |
2bda0e17 KB |
73 | msStyle |= WS_BORDER; |
74 | ||
c085e333 | 75 | m_hWnd = (WXHWND)::CreateWindowEx(MakeExtendedStyle(m_windowStyle), "STATIC", (const char *)label, |
2bda0e17 KB |
76 | msStyle, |
77 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
78 | wxGetInstance(), NULL); | |
79 | ||
c085e333 VZ |
80 | wxCHECK_MSG( m_hWnd, FALSE, "Failed to create static ctrl" ); |
81 | ||
2bda0e17 KB |
82 | #if CTL3D |
83 | /* | |
84 | if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS)) | |
85 | Ctl3dSubclassCtl(static_item); | |
86 | */ | |
87 | #endif | |
88 | ||
c085e333 | 89 | SubclassWin(m_hWnd); |
2bda0e17 | 90 | |
c0ed460c | 91 | SetFont(parent->GetFont()); |
2bda0e17 | 92 | SetSize(x, y, width, height); |
c085e333 | 93 | |
2bda0e17 KB |
94 | return TRUE; |
95 | } | |
96 | ||
debe6624 | 97 | void wxStaticText::SetSize(int x, int y, int width, int height, int sizeFlags) |
2bda0e17 KB |
98 | { |
99 | int currentX, currentY; | |
100 | GetPosition(¤tX, ¤tY); | |
101 | int x1 = x; | |
102 | int y1 = y; | |
103 | ||
104 | if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
105 | x1 = currentX; | |
106 | if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
107 | y1 = currentY; | |
108 | ||
81d66cf3 JS |
109 | AdjustForParentClientOrigin(x1, y1, sizeFlags); |
110 | ||
2bda0e17 KB |
111 | int actualWidth = width; |
112 | int actualHeight = height; | |
113 | ||
114 | char buf[300]; | |
115 | int current_width; | |
116 | int cyf; | |
117 | ||
118 | ::GetWindowText((HWND) GetHWND(), buf, 300); | |
ce3ed50d | 119 | GetTextExtent(buf, ¤t_width, &cyf, NULL, NULL, & this->GetFont()); |
2bda0e17 KB |
120 | |
121 | int ww, hh; | |
122 | GetSize(&ww, &hh); | |
123 | ||
124 | // If we're prepared to use the existing width, then... | |
125 | if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH)) | |
126 | actualWidth = ww; | |
127 | else if (width == -1) | |
128 | { | |
129 | int cx; | |
130 | int cy; | |
ce3ed50d | 131 | wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont()); |
2bda0e17 KB |
132 | actualWidth = (int)(current_width + cx) ; |
133 | } | |
134 | ||
135 | // If we're prepared to use the existing height, then... | |
136 | if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT)) | |
137 | actualHeight = hh; | |
138 | else if (height == -1) | |
139 | { | |
140 | actualHeight = (int)(cyf) ; | |
141 | } | |
142 | ||
143 | MoveWindow((HWND) GetHWND(), x1, y1, actualWidth, actualHeight, TRUE); | |
2bda0e17 KB |
144 | } |
145 | ||
146 | void wxStaticText::SetLabel(const wxString& label) | |
147 | { | |
debe6624 | 148 | int w, h; |
2bda0e17 KB |
149 | RECT rect; |
150 | ||
151 | wxWindow *parent = GetParent(); | |
152 | GetWindowRect((HWND) GetHWND(), &rect); | |
153 | ||
154 | // Since we now have the absolute screen coords, | |
155 | // if there's a parent we must subtract its top left corner | |
156 | POINT point; | |
157 | point.x = rect.left; | |
158 | point.y = rect.top; | |
159 | if (parent) | |
160 | { | |
161 | ::ScreenToClient((HWND) parent->GetHWND(), &point); | |
162 | } | |
163 | ||
ce3ed50d | 164 | GetTextExtent(label, &w, &h, NULL, NULL, & this->GetFont()); |
2bda0e17 KB |
165 | MoveWindow((HWND) GetHWND(), point.x, point.y, (int)(w + 10), (int)h, |
166 | TRUE); | |
167 | SetWindowText((HWND) GetHWND(), (const char *)label); | |
168 | } | |
169 | ||
debe6624 | 170 | WXHBRUSH wxStaticText::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, |
c085e333 | 171 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) |
2bda0e17 KB |
172 | { |
173 | /* | |
174 | #if CTL3D | |
175 | if ( m_useCtl3D ) | |
176 | { | |
177 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
c085e333 | 178 | |
2bda0e17 KB |
179 | if (hbrush != (HBRUSH) 0) |
180 | return hbrush; | |
181 | else | |
182 | return (HBRUSH)MSWDefWindowProc(message, wParam, lParam); | |
183 | } | |
184 | #endif | |
185 | */ | |
186 | ||
187 | if (GetParent()->GetTransparentBackground()) | |
188 | SetBkMode((HDC) pDC, TRANSPARENT); | |
189 | else | |
190 | SetBkMode((HDC) pDC, OPAQUE); | |
191 | ||
192 | ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); | |
193 | ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue())); | |
194 | ||
195 | wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID); | |
196 | ||
197 | // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush | |
198 | // has a zero usage count. | |
199 | // backgroundBrush->RealizeResource(); | |
200 | return (WXHBRUSH) backgroundBrush->GetResourceHandle(); | |
201 | } | |
202 | ||
203 | long wxStaticText::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
204 | { | |
205 | // Ensure that static items get messages. Some controls don't like this | |
206 | // message to be intercepted (e.g. RichEdit), hence the tests. | |
207 | if (nMsg == WM_NCHITTEST) | |
208 | return (long)HTCLIENT; | |
209 | ||
210 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); | |
211 | } | |
212 | ||
213 |