]> git.saurik.com Git - wxWidgets.git/blame - src/msw/stattext.cpp
added Show/HideNativeCaret() (patch 759924)
[wxWidgets.git] / src / msw / stattext.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
1e3a888e 2// Name: src/msw/stattext.cpp
2bda0e17
KB
3// Purpose: wxStaticText
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
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
1e6feb95
VZ
23#if wxUSE_STATTEXT
24
2bda0e17 25#ifndef WX_PRECOMP
2432b92d 26#include "wx/event.h"
2bda0e17 27#include "wx/app.h"
2432b92d 28#include "wx/brush.h"
2bda0e17
KB
29#endif
30
31#include "wx/stattext.h"
32#include "wx/msw/private.h"
33#include <stdio.h>
34
2bda0e17 35IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
2bda0e17 36
6dd16e4f
VZ
37bool 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)
2bda0e17 44{
6dd16e4f
VZ
45 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
46 return FALSE;
2bda0e17 47
6dd16e4f
VZ
48 if ( !MSWCreateControl(wxT("STATIC"), label, pos, size) )
49 return FALSE;
2bda0e17 50
6dd16e4f
VZ
51 return TRUE;
52}
c085e333 53
65bc172c
VZ
54wxBorder wxStaticText::GetDefaultBorder() const
55{
56 return wxBORDER_NONE;
57}
58
6dd16e4f
VZ
59WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const
60{
61 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
62
63 // translate the alignment flags to the Windows ones
64 //
65 // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't
66 // test for them using & operator
67 if ( style & wxALIGN_CENTRE )
68 msStyle |= SS_CENTER;
69 else if ( style & wxALIGN_RIGHT )
70 msStyle |= SS_RIGHT;
71 else
72 msStyle |= SS_LEFT;
73
74 return msStyle;
2bda0e17
KB
75}
76
f68586e5 77wxSize wxStaticText::DoGetBestSize() const
2bda0e17 78{
4b5d9823
VZ
79 wxString text(wxGetWindowText(GetHWND()));
80
81 int widthTextMax = 0, widthLine,
4fe5383d 82 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
4b5d9823 83
b826684e
VZ
84 bool lastWasAmpersand = FALSE;
85
4b5d9823 86 wxString curLine;
b826684e
VZ
87 for ( const wxChar *pc = text; ; pc++ )
88 {
89 if ( *pc == wxT('\n') || *pc == wxT('\0') )
90 {
91 if ( !curLine )
92 {
4fe5383d
VZ
93 // we can't use GetTextExtent - it will return 0 for both width
94 // and height and an empty line should count in height
95 // calculation
96 if ( !heightLineDefault )
97 heightLineDefault = heightLine;
98 if ( !heightLineDefault )
99 GetTextExtent(_T("W"), NULL, &heightLineDefault);
100
101 heightTextTotal += heightLineDefault;
102 }
b826684e
VZ
103 else
104 {
4fe5383d
VZ
105 GetTextExtent(curLine, &widthLine, &heightLine);
106 if ( widthLine > widthTextMax )
107 widthTextMax = widthLine;
108 heightTextTotal += heightLine;
109 }
4b5d9823 110
b826684e
VZ
111 if ( *pc == wxT('\n') )
112 {
4b5d9823
VZ
113 curLine.Empty();
114 }
b826684e
VZ
115 else
116 {
4b5d9823
VZ
117 // the end of string
118 break;
119 }
120 }
b826684e
VZ
121 else
122 {
123 // we shouldn't take into account the '&' which just introduces the
124 // mnemonic characters and so are not shown on the screen -- except
125 // when it is preceded by another '&' in which case it stands for a
126 // literal ampersand
127 if ( *pc == _T('&') )
128 {
129 if ( !lastWasAmpersand )
130 {
131 lastWasAmpersand = TRUE;
132
133 // skip the statement adding pc to curLine below
134 continue;
135 }
136
137 // it is a literal ampersand
138 lastWasAmpersand = FALSE;
139 }
140
141 curLine += *pc;
4b5d9823
VZ
142 }
143 }
144
4438caf4 145 return wxSize(widthTextMax, heightTextTotal);
2bda0e17
KB
146}
147
1e3a888e
VZ
148void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags)
149{
150 // we need to refresh the window after changing its size as the standard
151 // control doesn't always update itself properly
152 wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags);
153
154 Refresh();
155}
156
2bda0e17
KB
157void wxStaticText::SetLabel(const wxString& label)
158{
1e3a888e 159 wxStaticTextBase::SetLabel(label);
2bda0e17 160
185fa6bf
VZ
161 // adjust the size of the window to fit to the label unless autoresizing is
162 // disabled
163 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
164 {
165 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
166 }
2bda0e17
KB
167}
168
486fd225
RD
169
170bool wxStaticText::SetFont(const wxFont& font)
171{
172 bool ret = wxControl::SetFont(font);
173
174 // adjust the size of the window to fit to the label unless autoresizing is
175 // disabled
176 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
177 {
178 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
179 }
180
181 return ret;
182}
183
1e6feb95 184#endif // wxUSE_STATTEXT