]>
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 | ||
2bda0e17 | 33 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) |
2bda0e17 | 34 | |
debe6624 | 35 | bool wxStaticText::Create(wxWindow *parent, wxWindowID id, |
2bda0e17 KB |
36 | const wxString& label, |
37 | const wxPoint& pos, | |
38 | const wxSize& size, | |
debe6624 | 39 | long style, |
2bda0e17 KB |
40 | const wxString& name) |
41 | { | |
42 | SetName(name); | |
43 | if (parent) parent->AddChild(this); | |
44 | ||
fd71308f JS |
45 | SetBackgroundColour(parent->GetBackgroundColour()) ; |
46 | SetForegroundColour(parent->GetForegroundColour()) ; | |
2bda0e17 KB |
47 | |
48 | if ( id == -1 ) | |
c085e333 | 49 | m_windowId = (int)NewControlId(); |
2bda0e17 | 50 | else |
c085e333 | 51 | m_windowId = id; |
2bda0e17 KB |
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. | |
c085e333 | 70 | if ( wxStyleHasBorder(m_windowStyle) ) |
2bda0e17 KB |
71 | msStyle |= WS_BORDER; |
72 | ||
223d09f6 | 73 | m_hWnd = (WXHWND)::CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("STATIC"), (const wxChar *)label, |
2bda0e17 KB |
74 | msStyle, |
75 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
76 | wxGetInstance(), NULL); | |
77 | ||
223d09f6 | 78 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static ctrl") ); |
c085e333 | 79 | |
1f112209 | 80 | #if wxUSE_CTL3D |
2bda0e17 KB |
81 | /* |
82 | if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS)) | |
83 | Ctl3dSubclassCtl(static_item); | |
84 | */ | |
85 | #endif | |
86 | ||
c085e333 | 87 | SubclassWin(m_hWnd); |
2bda0e17 | 88 | |
c0ed460c | 89 | SetFont(parent->GetFont()); |
2bda0e17 | 90 | SetSize(x, y, width, height); |
c085e333 | 91 | |
2bda0e17 KB |
92 | return TRUE; |
93 | } | |
94 | ||
f68586e5 | 95 | wxSize wxStaticText::DoGetBestSize() const |
2bda0e17 | 96 | { |
4b5d9823 VZ |
97 | wxString text(wxGetWindowText(GetHWND())); |
98 | ||
99 | int widthTextMax = 0, widthLine, | |
4fe5383d | 100 | heightTextTotal = 0, heightLineDefault = 0, heightLine = 0; |
4b5d9823 VZ |
101 | |
102 | wxString curLine; | |
54c13c66 | 103 | for ( const wxChar *pc = text; ; pc++ ) { |
223d09f6 | 104 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) { |
4fe5383d VZ |
105 | if ( !curLine ) { |
106 | // we can't use GetTextExtent - it will return 0 for both width | |
107 | // and height and an empty line should count in height | |
108 | // calculation | |
109 | if ( !heightLineDefault ) | |
110 | heightLineDefault = heightLine; | |
111 | if ( !heightLineDefault ) | |
112 | GetTextExtent(_T("W"), NULL, &heightLineDefault); | |
113 | ||
114 | heightTextTotal += heightLineDefault; | |
115 | } | |
116 | else { | |
117 | GetTextExtent(curLine, &widthLine, &heightLine); | |
118 | if ( widthLine > widthTextMax ) | |
119 | widthTextMax = widthLine; | |
120 | heightTextTotal += heightLine; | |
121 | } | |
4b5d9823 | 122 | |
223d09f6 | 123 | if ( *pc == wxT('\n') ) { |
4b5d9823 VZ |
124 | curLine.Empty(); |
125 | } | |
126 | else { | |
127 | // the end of string | |
128 | break; | |
129 | } | |
130 | } | |
131 | else { | |
132 | curLine += *pc; | |
133 | } | |
134 | } | |
135 | ||
4438caf4 | 136 | return wxSize(widthTextMax, heightTextTotal); |
2bda0e17 KB |
137 | } |
138 | ||
139 | void wxStaticText::SetLabel(const wxString& label) | |
140 | { | |
4b5d9823 | 141 | SetWindowText(GetHwnd(), label); |
2bda0e17 | 142 | |
185fa6bf VZ |
143 | // adjust the size of the window to fit to the label unless autoresizing is |
144 | // disabled | |
145 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
146 | { | |
147 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); | |
148 | } | |
2bda0e17 KB |
149 | } |
150 | ||
2bda0e17 KB |
151 | long wxStaticText::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
152 | { | |
153 | // Ensure that static items get messages. Some controls don't like this | |
154 | // message to be intercepted (e.g. RichEdit), hence the tests. | |
155 | if (nMsg == WM_NCHITTEST) | |
156 | return (long)HTCLIENT; | |
157 | ||
158 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); | |
159 | } | |
160 | ||
161 |