]> git.saurik.com Git - wxWidgets.git/blame - src/generic/tipwin.cpp
don't use wxTheXXXList in wxXXX ctor/dtor, only objects explicitly created
[wxWidgets.git] / src / generic / tipwin.cpp
CommitLineData
01fa3fe7
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/tipwin.cpp
3// Purpose: implementation of wxTipWindow
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 10.09.00
7// RCS-ID: $Id$
8// Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "tipwin.h"
22#endif
23
24// For compilers that support precompilatixon, includes "wx/wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/dcclient.h"
33#endif // WX_PRECOMP
34
35#include "wx/tipwin.h"
f38bcae5
VZ
36
37#if wxUSE_TIPWINDOW
38
173e8bbf 39#include "wx/timer.h"
8cb172b4 40#include "wx/settings.h"
01fa3fe7
VZ
41
42// ----------------------------------------------------------------------------
43// constants
44// ----------------------------------------------------------------------------
45
46static const wxCoord TEXT_MARGIN_X = 3;
47static const wxCoord TEXT_MARGIN_Y = 3;
48
49// ============================================================================
50// implementation
51// ============================================================================
52
53// ----------------------------------------------------------------------------
54// event tables
55// ----------------------------------------------------------------------------
56
8962e1d9 57BEGIN_EVENT_TABLE(wxTipWindow, wxPopupTransientWindow)
01fa3fe7
VZ
58 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick)
59 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick)
60 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick)
8962e1d9 61 EVT_PAINT(wxTipWindow::OnPaint)
01fa3fe7
VZ
62END_EVENT_TABLE()
63
8962e1d9 64
01fa3fe7
VZ
65// ----------------------------------------------------------------------------
66// wxTipWindow
67// ----------------------------------------------------------------------------
68
69wxTipWindow::wxTipWindow(wxWindow *parent,
70 const wxString& text,
173e8bbf 71 wxCoord maxLength, wxTipWindow** windowPtr)
8962e1d9 72 : wxPopupTransientWindow(parent)
01fa3fe7 73{
8962e1d9
RD
74 m_windowPtr = windowPtr;
75
01fa3fe7
VZ
76 // set colours
77 SetForegroundColour(*wxBLACK);
04ef50df
JS
78
79#ifdef __WXMSW__
80 wxColour bkCol(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_INFOBK));
81#else
82 wxColour bkCol(wxColour(255, 255, 225));
83#endif
84 SetBackgroundColour(bkCol);
85
8962e1d9
RD
86 // set size and position
87 Adjust(text, maxLength);
01fa3fe7
VZ
88 int x, y;
89 wxGetMousePosition(&x, &y);
8962e1d9 90 Position(wxPoint(x, y+10), wxSize(0,0));
01fa3fe7 91
8962e1d9
RD
92 //Show(TRUE);
93 Popup();
94 SetFocus();
173e8bbf
JS
95}
96
97wxTipWindow::~wxTipWindow()
98{
99 if (m_windowPtr)
100 {
101 *m_windowPtr = NULL;
102 }
103}
104
33ac7e6f 105void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event))
173e8bbf
JS
106{
107 Close();
108}
109
8962e1d9
RD
110
111void wxTipWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
173e8bbf 112{
8962e1d9
RD
113 wxPaintDC dc(this);
114
115 wxRect rect;
116 wxSize size = GetClientSize();
117 rect.width = size.x;
118 rect.height = size.y;
119
120 // first filll the background
121 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
122
123 dc.SetPen( * wxBLACK_PEN );
124 dc.DrawRectangle(rect);
125
126 // and then draw the text line by line
127 dc.SetFont(GetFont());
128
129 wxPoint pt;
130 pt.x = TEXT_MARGIN_X;
131 pt.y = TEXT_MARGIN_Y;
132 size_t count = m_textLines.GetCount();
133 for ( size_t n = 0; n < count; n++ )
134 {
135 dc.DrawText(m_textLines[n], pt);
136
137 pt.y += m_heightLine;
138 }
173e8bbf
JS
139}
140
8962e1d9
RD
141
142void wxTipWindow::Adjust(const wxString& text, wxCoord maxLength)
173e8bbf 143{
8962e1d9
RD
144 wxClientDC dc(this);
145 dc.SetFont(GetFont());
146
147 // calculate the length: we want each line be no longer than maxLength
148 // pixels and we only break lines at words boundary
149 wxString current;
150 wxCoord height, width,
151 widthMax = 0;
152 m_heightLine = 0;
153
154 bool breakLine = FALSE;
155 for ( const wxChar *p = text.c_str(); ; p++ )
156 {
157 if ( *p == _T('\n') || *p == _T('\0') )
158 {
159 dc.GetTextExtent(current, &width, &height);
160 if ( width > widthMax )
161 widthMax = width;
162
163 if ( height > m_heightLine )
164 m_heightLine = height;
165
166 m_textLines.Add(current);
167
168 if ( !*p )
169 {
170 // end of text
171 break;
172 }
173
174 current.clear();
175 breakLine = FALSE;
176 }
177 else if ( breakLine && (*p == _T(' ') || *p == _T('\t')) )
178 {
179 // word boundary - break the line here
180 m_textLines.Add(current);
181 current.clear();
182 breakLine = FALSE;
183 }
184 else // line goes on
185 {
186 current += *p;
187 dc.GetTextExtent(current, &width, &height);
188 if ( width > maxLength )
189 breakLine = TRUE;
190
191 if ( width > widthMax )
192 widthMax = width;
193
194 if ( height > m_heightLine )
195 m_heightLine = height;
196 }
197 }
198
199 // take into account the border size and the margins
200 SetClientSize(2*(TEXT_MARGIN_X + 1) + widthMax,
201 2*(TEXT_MARGIN_Y + 1) + m_textLines.GetCount() * m_heightLine);
173e8bbf
JS
202}
203
8962e1d9
RD
204
205void wxTipWindow::Close()
206{
207 Show(FALSE);
208 Destroy();
209}
210
f38bcae5 211#endif // wxUSE_TIPWINDOW
8962e1d9 212