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