]> git.saurik.com Git - wxWidgets.git/blame - src/msw/stattext.cpp
Applied patch #421554: implementation of wxEVT_CONTEXT_MENU
[wxWidgets.git] / src / msw / stattext.cpp
CommitLineData
2bda0e17
KB
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
c085e333 9// Licence: wxWindows license
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
23#ifndef WX_PRECOMP
2432b92d 24#include "wx/event.h"
2bda0e17 25#include "wx/app.h"
2432b92d 26#include "wx/brush.h"
2bda0e17
KB
27#endif
28
29#include "wx/stattext.h"
30#include "wx/msw/private.h"
31#include <stdio.h>
32
2bda0e17 33IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
2bda0e17 34
debe6624 35bool wxStaticText::Create(wxWindow *parent, wxWindowID id,
2bda0e17
KB
36 const wxString& label,
37 const wxPoint& pos,
38 const wxSize& size,
debe6624 39 long style,
2bda0e17
KB
40 const wxString& name)
41{
42 SetName(name);
43 if (parent) parent->AddChild(this);
44
fd71308f
JS
45 SetBackgroundColour(parent->GetBackgroundColour()) ;
46 SetForegroundColour(parent->GetForegroundColour()) ;
2bda0e17
KB
47
48 if ( id == -1 )
c085e333 49 m_windowId = (int)NewControlId();
2bda0e17 50 else
c085e333 51 m_windowId = id;
2bda0e17
KB
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
b0766406
JS
60 long msStyle = WS_CHILD | WS_VISIBLE;
61
62 if ( m_windowStyle & wxCLIP_SIBLINGS )
63 msStyle |= WS_CLIPSIBLINGS;
2bda0e17
KB
64 if (m_windowStyle & wxALIGN_CENTRE)
65 msStyle |= SS_CENTER;
66 else if (m_windowStyle & wxALIGN_RIGHT)
67 msStyle |= SS_RIGHT;
68 else
69 msStyle |= SS_LEFT;
70
71 // Even with extended styles, need to combine with WS_BORDER
72 // for them to look right.
c085e333 73 if ( wxStyleHasBorder(m_windowStyle) )
2bda0e17
KB
74 msStyle |= WS_BORDER;
75
223d09f6 76 m_hWnd = (WXHWND)::CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("STATIC"), (const wxChar *)label,
2bda0e17
KB
77 msStyle,
78 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
79 wxGetInstance(), NULL);
80
223d09f6 81 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static ctrl") );
c085e333 82
1f112209 83#if wxUSE_CTL3D
2bda0e17
KB
84/*
85 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
86 Ctl3dSubclassCtl(static_item);
87*/
88#endif
89
c085e333 90 SubclassWin(m_hWnd);
2bda0e17 91
486fd225 92 wxControl::SetFont(parent->GetFont());
2bda0e17 93 SetSize(x, y, width, height);
c085e333 94
2bda0e17
KB
95 return TRUE;
96}
97
f68586e5 98wxSize wxStaticText::DoGetBestSize() const
2bda0e17 99{
4b5d9823
VZ
100 wxString text(wxGetWindowText(GetHWND()));
101
102 int widthTextMax = 0, widthLine,
4fe5383d 103 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
4b5d9823
VZ
104
105 wxString curLine;
54c13c66 106 for ( const wxChar *pc = text; ; pc++ ) {
223d09f6 107 if ( *pc == wxT('\n') || *pc == wxT('\0') ) {
4fe5383d
VZ
108 if ( !curLine ) {
109 // we can't use GetTextExtent - it will return 0 for both width
110 // and height and an empty line should count in height
111 // calculation
112 if ( !heightLineDefault )
113 heightLineDefault = heightLine;
114 if ( !heightLineDefault )
115 GetTextExtent(_T("W"), NULL, &heightLineDefault);
116
117 heightTextTotal += heightLineDefault;
118 }
119 else {
120 GetTextExtent(curLine, &widthLine, &heightLine);
121 if ( widthLine > widthTextMax )
122 widthTextMax = widthLine;
123 heightTextTotal += heightLine;
124 }
4b5d9823 125
223d09f6 126 if ( *pc == wxT('\n') ) {
4b5d9823
VZ
127 curLine.Empty();
128 }
129 else {
130 // the end of string
131 break;
132 }
133 }
134 else {
135 curLine += *pc;
136 }
137 }
138
4438caf4 139 return wxSize(widthTextMax, heightTextTotal);
2bda0e17
KB
140}
141
142void wxStaticText::SetLabel(const wxString& label)
143{
4b5d9823 144 SetWindowText(GetHwnd(), label);
2bda0e17 145
185fa6bf
VZ
146 // adjust the size of the window to fit to the label unless autoresizing is
147 // disabled
148 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
149 {
150 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
151 }
2bda0e17
KB
152}
153
486fd225
RD
154
155bool wxStaticText::SetFont(const wxFont& font)
156{
157 bool ret = wxControl::SetFont(font);
158
159 // adjust the size of the window to fit to the label unless autoresizing is
160 // disabled
161 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
162 {
163 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
164 }
165
166 return ret;
167}
168
169
2bda0e17
KB
170long wxStaticText::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
171{
172 // Ensure that static items get messages. Some controls don't like this
173 // message to be intercepted (e.g. RichEdit), hence the tests.
174 if (nMsg == WM_NCHITTEST)
175 return (long)HTCLIENT;
176
177 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
178}
179
180