]>
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 | ||
223d09f6 | 75 | m_hWnd = (WXHWND)::CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("STATIC"), (const wxChar *)label, |
2bda0e17 KB |
76 | msStyle, |
77 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
78 | wxGetInstance(), NULL); | |
79 | ||
223d09f6 | 80 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static ctrl") ); |
c085e333 | 81 | |
1f112209 | 82 | #if wxUSE_CTL3D |
2bda0e17 KB |
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 | ||
4438caf4 | 97 | wxSize wxStaticText::DoGetBestSize() |
2bda0e17 | 98 | { |
4b5d9823 VZ |
99 | wxString text(wxGetWindowText(GetHWND())); |
100 | ||
101 | int widthTextMax = 0, widthLine, | |
4fe5383d | 102 | heightTextTotal = 0, heightLineDefault = 0, heightLine = 0; |
4b5d9823 VZ |
103 | |
104 | wxString curLine; | |
54c13c66 | 105 | for ( const wxChar *pc = text; ; pc++ ) { |
223d09f6 | 106 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) { |
4fe5383d VZ |
107 | if ( !curLine ) { |
108 | // we can't use GetTextExtent - it will return 0 for both width | |
109 | // and height and an empty line should count in height | |
110 | // calculation | |
111 | if ( !heightLineDefault ) | |
112 | heightLineDefault = heightLine; | |
113 | if ( !heightLineDefault ) | |
114 | GetTextExtent(_T("W"), NULL, &heightLineDefault); | |
115 | ||
116 | heightTextTotal += heightLineDefault; | |
117 | } | |
118 | else { | |
119 | GetTextExtent(curLine, &widthLine, &heightLine); | |
120 | if ( widthLine > widthTextMax ) | |
121 | widthTextMax = widthLine; | |
122 | heightTextTotal += heightLine; | |
123 | } | |
4b5d9823 | 124 | |
223d09f6 | 125 | if ( *pc == wxT('\n') ) { |
4b5d9823 VZ |
126 | curLine.Empty(); |
127 | } | |
128 | else { | |
129 | // the end of string | |
130 | break; | |
131 | } | |
132 | } | |
133 | else { | |
134 | curLine += *pc; | |
135 | } | |
136 | } | |
137 | ||
4438caf4 | 138 | return wxSize(widthTextMax, heightTextTotal); |
2bda0e17 KB |
139 | } |
140 | ||
141 | void wxStaticText::SetLabel(const wxString& label) | |
142 | { | |
4b5d9823 | 143 | SetWindowText(GetHwnd(), label); |
2bda0e17 | 144 | |
185fa6bf VZ |
145 | // adjust the size of the window to fit to the label unless autoresizing is |
146 | // disabled | |
147 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
148 | { | |
149 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); | |
150 | } | |
2bda0e17 KB |
151 | } |
152 | ||
debe6624 | 153 | WXHBRUSH wxStaticText::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, |
c085e333 | 154 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) |
2bda0e17 KB |
155 | { |
156 | /* | |
1f112209 | 157 | #if wxUSE_CTL3D |
2bda0e17 KB |
158 | if ( m_useCtl3D ) |
159 | { | |
160 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
c085e333 | 161 | |
2bda0e17 KB |
162 | if (hbrush != (HBRUSH) 0) |
163 | return hbrush; | |
164 | else | |
165 | return (HBRUSH)MSWDefWindowProc(message, wParam, lParam); | |
166 | } | |
167 | #endif | |
168 | */ | |
169 | ||
170 | if (GetParent()->GetTransparentBackground()) | |
171 | SetBkMode((HDC) pDC, TRANSPARENT); | |
172 | else | |
173 | SetBkMode((HDC) pDC, OPAQUE); | |
174 | ||
175 | ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); | |
176 | ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue())); | |
177 | ||
178 | wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID); | |
179 | ||
180 | // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush | |
181 | // has a zero usage count. | |
182 | // backgroundBrush->RealizeResource(); | |
183 | return (WXHBRUSH) backgroundBrush->GetResourceHandle(); | |
184 | } | |
185 | ||
186 | long wxStaticText::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
187 | { | |
188 | // Ensure that static items get messages. Some controls don't like this | |
189 | // message to be intercepted (e.g. RichEdit), hence the tests. | |
190 | if (nMsg == WM_NCHITTEST) | |
191 | return (long)HTCLIENT; | |
192 | ||
193 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); | |
194 | } | |
195 | ||
196 |