]> git.saurik.com Git - wxWidgets.git/blob - src/msw/stattext.cpp
Fixed toolbar crash for MinGW/Cygwin
[wxWidgets.git] / src / msw / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/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 SubclassWin(m_hWnd);
86
87 wxControl::SetFont(parent->GetFont());
88 SetSize(x, y, width, height);
89
90 return TRUE;
91 }
92
93 wxSize wxStaticText::DoGetBestSize() const
94 {
95 wxString text(wxGetWindowText(GetHWND()));
96
97 int widthTextMax = 0, widthLine,
98 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
99
100 wxString curLine;
101 for ( const wxChar *pc = text; ; pc++ ) {
102 if ( *pc == wxT('\n') || *pc == wxT('\0') ) {
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 }
120
121 if ( *pc == wxT('\n') ) {
122 curLine.Empty();
123 }
124 else {
125 // the end of string
126 break;
127 }
128 }
129 else {
130 curLine += *pc;
131 }
132 }
133
134 return wxSize(widthTextMax, heightTextTotal);
135 }
136
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
146 void wxStaticText::SetLabel(const wxString& label)
147 {
148 wxStaticTextBase::SetLabel(label);
149
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 }
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 #endif // wxUSE_STATTEXT