]> git.saurik.com Git - wxWidgets.git/blob - src/msw/stattext.cpp
6a2056e2afd2f838da72701f0df655df317600cb
[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,
38 wxWindowID id,
39 const wxString& label,
40 const wxPoint& pos,
41 const wxSize& size,
42 long style,
43 const wxString& name)
44 {
45 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
46 return FALSE;
47
48 if ( !MSWCreateControl(wxT("STATIC"), label, pos, size) )
49 return FALSE;
50
51 return TRUE;
52 }
53
54 WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const
55 {
56 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
57
58 // translate the alignment flags to the Windows ones
59 //
60 // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't
61 // test for them using & operator
62 if ( style & wxALIGN_CENTRE )
63 msStyle |= SS_CENTER;
64 else if ( style & wxALIGN_RIGHT )
65 msStyle |= SS_RIGHT;
66 else
67 msStyle |= SS_LEFT;
68
69 return msStyle;
70 }
71
72 wxSize wxStaticText::DoGetBestSize() const
73 {
74 wxString text(wxGetWindowText(GetHWND()));
75
76 int widthTextMax = 0, widthLine,
77 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
78
79 wxString curLine;
80 for ( const wxChar *pc = text; ; pc++ ) {
81 if ( *pc == wxT('\n') || *pc == wxT('\0') ) {
82 if ( !curLine ) {
83 // we can't use GetTextExtent - it will return 0 for both width
84 // and height and an empty line should count in height
85 // calculation
86 if ( !heightLineDefault )
87 heightLineDefault = heightLine;
88 if ( !heightLineDefault )
89 GetTextExtent(_T("W"), NULL, &heightLineDefault);
90
91 heightTextTotal += heightLineDefault;
92 }
93 else {
94 GetTextExtent(curLine, &widthLine, &heightLine);
95 if ( widthLine > widthTextMax )
96 widthTextMax = widthLine;
97 heightTextTotal += heightLine;
98 }
99
100 if ( *pc == wxT('\n') ) {
101 curLine.Empty();
102 }
103 else {
104 // the end of string
105 break;
106 }
107 }
108 else {
109 curLine += *pc;
110 }
111 }
112
113 return wxSize(widthTextMax, heightTextTotal);
114 }
115
116 void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags)
117 {
118 // we need to refresh the window after changing its size as the standard
119 // control doesn't always update itself properly
120 wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags);
121
122 Refresh();
123 }
124
125 void wxStaticText::SetLabel(const wxString& label)
126 {
127 wxStaticTextBase::SetLabel(label);
128
129 // adjust the size of the window to fit to the label unless autoresizing is
130 // disabled
131 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
132 {
133 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
134 }
135 }
136
137
138 bool wxStaticText::SetFont(const wxFont& font)
139 {
140 bool ret = wxControl::SetFont(font);
141
142 // adjust the size of the window to fit to the label unless autoresizing is
143 // disabled
144 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
145 {
146 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
147 }
148
149 return ret;
150 }
151
152 #endif // wxUSE_STATTEXT