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