]>
Commit | Line | Data |
---|---|---|
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 | |
9 | // Licence: wxWindows license | |
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 | |
24 | #include "wx/event.h" | |
25 | #include "wx/app.h" | |
26 | #include "wx/brush.h" | |
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 | ||
37 | bool wxStaticText::Create(wxWindow *parent, wxWindowID id, | |
38 | const wxString& label, | |
39 | const wxPoint& pos, | |
40 | const wxSize& size, | |
41 | long style, | |
42 | const wxString& name) | |
43 | { | |
44 | SetName(name); | |
45 | if (parent) parent->AddChild(this); | |
46 | ||
47 | SetBackgroundColour(parent->GetBackgroundColour()) ; | |
48 | SetForegroundColour(parent->GetForegroundColour()) ; | |
49 | ||
50 | if ( id == -1 ) | |
51 | m_windowId = (int)NewControlId(); | |
52 | else | |
53 | m_windowId = id; | |
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. | |
72 | if ( wxStyleHasBorder(m_windowStyle) ) | |
73 | msStyle |= WS_BORDER; | |
74 | ||
75 | m_hWnd = (WXHWND)::CreateWindowEx(MakeExtendedStyle(m_windowStyle), "STATIC", (const char *)label, | |
76 | msStyle, | |
77 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
78 | wxGetInstance(), NULL); | |
79 | ||
80 | wxCHECK_MSG( m_hWnd, FALSE, "Failed to create static ctrl" ); | |
81 | ||
82 | #if wxUSE_CTL3D | |
83 | /* | |
84 | if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS)) | |
85 | Ctl3dSubclassCtl(static_item); | |
86 | */ | |
87 | #endif | |
88 | ||
89 | SubclassWin(m_hWnd); | |
90 | ||
91 | SetFont(parent->GetFont()); | |
92 | SetSize(x, y, width, height); | |
93 | ||
94 | return TRUE; | |
95 | } | |
96 | ||
97 | void wxStaticText::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
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 | ||
109 | AdjustForParentClientOrigin(x1, y1, sizeFlags); | |
110 | ||
111 | int actualWidth = width; | |
112 | int actualHeight = height; | |
113 | ||
114 | int current_width; | |
115 | int cyf; | |
116 | ||
117 | wxString text(wxGetWindowText(GetHWND())); | |
118 | GetTextExtent(text, ¤t_width, &cyf, NULL, NULL, & GetFont()); | |
119 | ||
120 | int ww, hh; | |
121 | GetSize(&ww, &hh); | |
122 | ||
123 | // If we're prepared to use the existing width, then... | |
124 | if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH)) | |
125 | actualWidth = ww; | |
126 | else if (width == -1) | |
127 | { | |
128 | int cx; | |
129 | int cy; | |
130 | wxGetCharSize(GetHWND(), &cx, &cy, & GetFont()); | |
131 | actualWidth = (int)(current_width + cx) ; | |
132 | } | |
133 | ||
134 | // If we're prepared to use the existing height, then... | |
135 | if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT)) | |
136 | actualHeight = hh; | |
137 | else if (height == -1) | |
138 | { | |
139 | actualHeight = (int)(cyf) ; | |
140 | } | |
141 | ||
142 | MoveWindow((HWND) GetHWND(), x1, y1, actualWidth, actualHeight, TRUE); | |
143 | } | |
144 | ||
145 | void wxStaticText::SetLabel(const wxString& label) | |
146 | { | |
147 | wxWindow *parent = GetParent(); | |
148 | ||
149 | RECT rect; | |
150 | GetWindowRect((HWND) GetHWND(), &rect); | |
151 | ||
152 | // Since we now have the absolute screen coords, if there's a parent we must | |
153 | // subtract its top left corner | |
154 | POINT point; | |
155 | point.x = rect.left; | |
156 | point.y = rect.top; | |
157 | if (parent) | |
158 | { | |
159 | ::ScreenToClient((HWND) parent->GetHWND(), &point); | |
160 | } | |
161 | ||
162 | int w, h; | |
163 | GetTextExtent(label, &w, &h, NULL, NULL, & GetFont()); | |
164 | MoveWindow((HWND) GetHWND(), point.x, point.y, (int)(w + 10), (int)h, TRUE); | |
165 | SetWindowText((HWND) GetHWND(), (const char *)label); | |
166 | } | |
167 | ||
168 | WXHBRUSH wxStaticText::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, | |
169 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) | |
170 | { | |
171 | /* | |
172 | #if wxUSE_CTL3D | |
173 | if ( m_useCtl3D ) | |
174 | { | |
175 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
176 | ||
177 | if (hbrush != (HBRUSH) 0) | |
178 | return hbrush; | |
179 | else | |
180 | return (HBRUSH)MSWDefWindowProc(message, wParam, lParam); | |
181 | } | |
182 | #endif | |
183 | */ | |
184 | ||
185 | if (GetParent()->GetTransparentBackground()) | |
186 | SetBkMode((HDC) pDC, TRANSPARENT); | |
187 | else | |
188 | SetBkMode((HDC) pDC, OPAQUE); | |
189 | ||
190 | ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); | |
191 | ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue())); | |
192 | ||
193 | wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID); | |
194 | ||
195 | // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush | |
196 | // has a zero usage count. | |
197 | // backgroundBrush->RealizeResource(); | |
198 | return (WXHBRUSH) backgroundBrush->GetResourceHandle(); | |
199 | } | |
200 | ||
201 | long wxStaticText::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
202 | { | |
203 | // Ensure that static items get messages. Some controls don't like this | |
204 | // message to be intercepted (e.g. RichEdit), hence the tests. | |
205 | if (nMsg == WM_NCHITTEST) | |
206 | return (long)HTCLIENT; | |
207 | ||
208 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); | |
209 | } | |
210 | ||
211 |