]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1e3a888e | 2 | // Name: src/msw/stattext.cpp |
2bda0e17 KB |
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 | ||
1e6feb95 VZ |
23 | #if wxUSE_STATTEXT |
24 | ||
2bda0e17 | 25 | #ifndef WX_PRECOMP |
2432b92d | 26 | #include "wx/event.h" |
2bda0e17 | 27 | #include "wx/app.h" |
2432b92d | 28 | #include "wx/brush.h" |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #include "wx/stattext.h" | |
32 | #include "wx/msw/private.h" | |
33 | #include <stdio.h> | |
34 | ||
2bda0e17 | 35 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) |
2bda0e17 | 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 | ||
b0766406 JS |
62 | long msStyle = WS_CHILD | WS_VISIBLE; |
63 | ||
64 | if ( m_windowStyle & wxCLIP_SIBLINGS ) | |
65 | msStyle |= WS_CLIPSIBLINGS; | |
2bda0e17 KB |
66 | if (m_windowStyle & wxALIGN_CENTRE) |
67 | msStyle |= SS_CENTER; | |
68 | else if (m_windowStyle & wxALIGN_RIGHT) | |
69 | msStyle |= SS_RIGHT; | |
70 | else | |
71 | msStyle |= SS_LEFT; | |
72 | ||
73 | // Even with extended styles, need to combine with WS_BORDER | |
74 | // for them to look right. | |
c085e333 | 75 | if ( wxStyleHasBorder(m_windowStyle) ) |
2bda0e17 KB |
76 | msStyle |= WS_BORDER; |
77 | ||
223d09f6 | 78 | m_hWnd = (WXHWND)::CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("STATIC"), (const wxChar *)label, |
2bda0e17 KB |
79 | msStyle, |
80 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
81 | wxGetInstance(), NULL); | |
82 | ||
223d09f6 | 83 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static ctrl") ); |
c085e333 | 84 | |
c085e333 | 85 | SubclassWin(m_hWnd); |
2bda0e17 | 86 | |
486fd225 | 87 | wxControl::SetFont(parent->GetFont()); |
2bda0e17 | 88 | SetSize(x, y, width, height); |
c085e333 | 89 | |
2bda0e17 KB |
90 | return TRUE; |
91 | } | |
92 | ||
f68586e5 | 93 | wxSize wxStaticText::DoGetBestSize() const |
2bda0e17 | 94 | { |
4b5d9823 VZ |
95 | wxString text(wxGetWindowText(GetHWND())); |
96 | ||
97 | int widthTextMax = 0, widthLine, | |
4fe5383d | 98 | heightTextTotal = 0, heightLineDefault = 0, heightLine = 0; |
4b5d9823 VZ |
99 | |
100 | wxString curLine; | |
54c13c66 | 101 | for ( const wxChar *pc = text; ; pc++ ) { |
223d09f6 | 102 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) { |
4fe5383d VZ |
103 | if ( !curLine ) { |
104 | // we can't use GetTextExtent - it will return 0 for both width | |
105 | // and height and an empty line should count in height | |
106 | // calculation | |
107 | if ( !heightLineDefault ) | |
108 | heightLineDefault = heightLine; | |
109 | if ( !heightLineDefault ) | |
110 | GetTextExtent(_T("W"), NULL, &heightLineDefault); | |
111 | ||
112 | heightTextTotal += heightLineDefault; | |
113 | } | |
114 | else { | |
115 | GetTextExtent(curLine, &widthLine, &heightLine); | |
116 | if ( widthLine > widthTextMax ) | |
117 | widthTextMax = widthLine; | |
118 | heightTextTotal += heightLine; | |
119 | } | |
4b5d9823 | 120 | |
223d09f6 | 121 | if ( *pc == wxT('\n') ) { |
4b5d9823 VZ |
122 | curLine.Empty(); |
123 | } | |
124 | else { | |
125 | // the end of string | |
126 | break; | |
127 | } | |
128 | } | |
129 | else { | |
130 | curLine += *pc; | |
131 | } | |
132 | } | |
133 | ||
4438caf4 | 134 | return wxSize(widthTextMax, heightTextTotal); |
2bda0e17 KB |
135 | } |
136 | ||
1e3a888e VZ |
137 | void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags) |
138 | { | |
139 | // we need to refresh the window after changing its size as the standard | |
140 | // control doesn't always update itself properly | |
141 | wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags); | |
142 | ||
143 | Refresh(); | |
144 | } | |
145 | ||
2bda0e17 KB |
146 | void wxStaticText::SetLabel(const wxString& label) |
147 | { | |
1e3a888e | 148 | wxStaticTextBase::SetLabel(label); |
2bda0e17 | 149 | |
185fa6bf VZ |
150 | // adjust the size of the window to fit to the label unless autoresizing is |
151 | // disabled | |
152 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
153 | { | |
154 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); | |
155 | } | |
2bda0e17 KB |
156 | } |
157 | ||
486fd225 RD |
158 | |
159 | bool wxStaticText::SetFont(const wxFont& font) | |
160 | { | |
161 | bool ret = wxControl::SetFont(font); | |
162 | ||
163 | // adjust the size of the window to fit to the label unless autoresizing is | |
164 | // disabled | |
165 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
166 | { | |
167 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); | |
168 | } | |
169 | ||
170 | return ret; | |
171 | } | |
172 | ||
a9d171bd JS |
173 | long wxStaticText::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
174 | { | |
58cf0491 JS |
175 | // Ensure that static items get messages. Some controls don't like this |
176 | // message to be intercepted (e.g. RichEdit), hence the tests. | |
177 | // Messes up display with Windows XP, apparently, so have to | |
178 | // do explicit hit-testing in wxWindowMSW. | |
179 | #if 0 | |
180 | if (nMsg == WM_NCHITTEST) | |
181 | return (long)HTCLIENT; | |
182 | #endif | |
183 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); | |
a9d171bd | 184 | } |
1e6feb95 | 185 | #endif // wxUSE_STATTEXT |