]> git.saurik.com Git - wxWidgets.git/blob - src/msw/stattext.cpp
da9906d9881f95fa5d42527de4c3c2b930f0b68f
[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/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), _T("STATIC"), (const wxChar *)label,
76 msStyle,
77 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
78 wxGetInstance(), NULL);
79
80 wxCHECK_MSG( m_hWnd, FALSE, _T("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(&currentX, &currentY);
101
102 int x1 = x;
103 int y1 = y;
104
105 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
106 x1 = currentX;
107 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
108 y1 = currentY;
109
110 AdjustForParentClientOrigin(x1, y1, sizeFlags);
111
112 int actualWidth = width;
113 int actualHeight = height;
114
115 wxString text(wxGetWindowText(GetHWND()));
116
117 int widthTextMax = 0, widthLine,
118 heightTextTotal = 0, heightLine;
119
120 wxString curLine;
121 for ( const char *pc = text; ; pc++ ) {
122 if ( *pc == '\n' || *pc == '\0' ) {
123 GetTextExtent(curLine, &widthLine, &heightLine);
124 if ( widthLine > widthTextMax )
125 widthTextMax = widthLine;
126 heightTextTotal += heightLine;
127
128 if ( *pc == '\n' ) {
129 curLine.Empty();
130 }
131 else {
132 // the end of string
133 break;
134 }
135 }
136 else {
137 curLine += *pc;
138 }
139 }
140
141 int ww, hh;
142 GetSize(&ww, &hh);
143
144 // If we're prepared to use the existing width, then...
145 if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH))
146 {
147 actualWidth = ww;
148 }
149 else if (width == -1)
150 {
151 actualWidth = widthTextMax;
152 }
153
154 // If we're prepared to use the existing height, then...
155 if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT))
156 {
157 actualHeight = hh;
158 }
159 else if (height == -1)
160 {
161 actualHeight = heightTextTotal;
162 }
163
164 MoveWindow(GetHwnd(), x1, y1, actualWidth, actualHeight, TRUE);
165 }
166
167 void wxStaticText::SetLabel(const wxString& label)
168 {
169 SetWindowText(GetHwnd(), label);
170
171 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
172 }
173
174 WXHBRUSH wxStaticText::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
175 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
176 {
177 /*
178 #if wxUSE_CTL3D
179 if ( m_useCtl3D )
180 {
181 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
182
183 if (hbrush != (HBRUSH) 0)
184 return hbrush;
185 else
186 return (HBRUSH)MSWDefWindowProc(message, wParam, lParam);
187 }
188 #endif
189 */
190
191 if (GetParent()->GetTransparentBackground())
192 SetBkMode((HDC) pDC, TRANSPARENT);
193 else
194 SetBkMode((HDC) pDC, OPAQUE);
195
196 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
197 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
198
199 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
200
201 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
202 // has a zero usage count.
203 // backgroundBrush->RealizeResource();
204 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
205 }
206
207 long wxStaticText::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
208 {
209 // Ensure that static items get messages. Some controls don't like this
210 // message to be intercepted (e.g. RichEdit), hence the tests.
211 if (nMsg == WM_NCHITTEST)
212 return (long)HTCLIENT;
213
214 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
215 }
216
217