]> git.saurik.com Git - wxWidgets.git/blob - src/msw/stattext.cpp
fix for the initial size of the dialogs
[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 #if wxUSE_STATTEXT
24
25 #ifndef WX_PRECOMP
26 #include "wx/event.h"
27 #include "wx/app.h"
28 #include "wx/brush.h"
29 #endif
30
31 #include "wx/stattext.h"
32 #include "wx/msw/private.h"
33 #include <stdio.h>
34
35 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
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
64 if ( m_windowStyle & wxCLIP_SIBLINGS )
65 msStyle |= WS_CLIPSIBLINGS;
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.
75 if ( wxStyleHasBorder(m_windowStyle) )
76 msStyle |= WS_BORDER;
77
78 m_hWnd = (WXHWND)::CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("STATIC"), (const wxChar *)label,
79 msStyle,
80 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
81 wxGetInstance(), NULL);
82
83 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static ctrl") );
84
85 #if wxUSE_CTL3D
86 /*
87 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
88 Ctl3dSubclassCtl(static_item);
89 */
90 #endif
91
92 SubclassWin(m_hWnd);
93
94 wxControl::SetFont(parent->GetFont());
95 SetSize(x, y, width, height);
96
97 return TRUE;
98 }
99
100 wxSize wxStaticText::DoGetBestSize() const
101 {
102 wxString text(wxGetWindowText(GetHWND()));
103
104 int widthTextMax = 0, widthLine,
105 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
106
107 wxString curLine;
108 for ( const wxChar *pc = text; ; pc++ ) {
109 if ( *pc == wxT('\n') || *pc == wxT('\0') ) {
110 if ( !curLine ) {
111 // we can't use GetTextExtent - it will return 0 for both width
112 // and height and an empty line should count in height
113 // calculation
114 if ( !heightLineDefault )
115 heightLineDefault = heightLine;
116 if ( !heightLineDefault )
117 GetTextExtent(_T("W"), NULL, &heightLineDefault);
118
119 heightTextTotal += heightLineDefault;
120 }
121 else {
122 GetTextExtent(curLine, &widthLine, &heightLine);
123 if ( widthLine > widthTextMax )
124 widthTextMax = widthLine;
125 heightTextTotal += heightLine;
126 }
127
128 if ( *pc == wxT('\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 return wxSize(widthTextMax, heightTextTotal);
142 }
143
144 void wxStaticText::SetLabel(const wxString& label)
145 {
146 SetWindowText(GetHwnd(), label);
147
148 // adjust the size of the window to fit to the label unless autoresizing is
149 // disabled
150 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
151 {
152 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
153
154 Refresh();
155 }
156 }
157
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
173
174 long wxStaticText::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
175 {
176 // Ensure that static items get messages. Some controls don't like this
177 // message to be intercepted (e.g. RichEdit), hence the tests.
178 if (nMsg == WM_NCHITTEST)
179 return (long)HTCLIENT;
180
181 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
182 }
183
184 #endif // wxUSE_STATTEXT