]> git.saurik.com Git - wxWidgets.git/blob - src/msw/stattext.cpp
Added more 3d rendering to row and col labels and removed sunken border style from...
[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 #ifndef WX_PRECOMP
24 #include "wx/event.h"
25 #include "wx/app.h"
26 #include "wx/brush.h"
27 #endif
28
29 #include "wx/stattext.h"
30 #include "wx/msw/private.h"
31 #include <stdio.h>
32
33 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
34
35 bool wxStaticText::Create(wxWindow *parent, wxWindowID id,
36 const wxString& label,
37 const wxPoint& pos,
38 const wxSize& size,
39 long style,
40 const wxString& name)
41 {
42 SetName(name);
43 if (parent) parent->AddChild(this);
44
45 SetBackgroundColour(parent->GetBackgroundColour()) ;
46 SetForegroundColour(parent->GetForegroundColour()) ;
47
48 if ( id == -1 )
49 m_windowId = (int)NewControlId();
50 else
51 m_windowId = id;
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.
70 if ( wxStyleHasBorder(m_windowStyle) )
71 msStyle |= WS_BORDER;
72
73 m_hWnd = (WXHWND)::CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("STATIC"), (const wxChar *)label,
74 msStyle,
75 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
76 wxGetInstance(), NULL);
77
78 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static ctrl") );
79
80 #if wxUSE_CTL3D
81 /*
82 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
83 Ctl3dSubclassCtl(static_item);
84 */
85 #endif
86
87 SubclassWin(m_hWnd);
88
89 SetFont(parent->GetFont());
90 SetSize(x, y, width, height);
91
92 return TRUE;
93 }
94
95 wxSize wxStaticText::DoGetBestSize() const
96 {
97 wxString text(wxGetWindowText(GetHWND()));
98
99 int widthTextMax = 0, widthLine,
100 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
101
102 wxString curLine;
103 for ( const wxChar *pc = text; ; pc++ ) {
104 if ( *pc == wxT('\n') || *pc == wxT('\0') ) {
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 }
122
123 if ( *pc == wxT('\n') ) {
124 curLine.Empty();
125 }
126 else {
127 // the end of string
128 break;
129 }
130 }
131 else {
132 curLine += *pc;
133 }
134 }
135
136 return wxSize(widthTextMax, heightTextTotal);
137 }
138
139 void wxStaticText::SetLabel(const wxString& label)
140 {
141 SetWindowText(GetHwnd(), label);
142
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 }
149 }
150
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