fixed compilation after wxTipWindow base class change, added wxUSE_TIPWINDOW
[wxWidgets.git] / src / generic / tipwin.cpp
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"
36
37 #if wxUSE_TIPWINDOW
38
39 #include "wx/timer.h"
40 #include "wx/settings.h"
41
42 // ----------------------------------------------------------------------------
43 // constants
44 // ----------------------------------------------------------------------------
45
46 static const wxCoord TEXT_MARGIN_X = 3;
47 static const wxCoord TEXT_MARGIN_Y = 3;
48
49 // ============================================================================
50 // implementation
51 // ============================================================================
52
53 // ----------------------------------------------------------------------------
54 // event tables
55 // ----------------------------------------------------------------------------
56
57 BEGIN_EVENT_TABLE(wxTipWindow, wxPopupTransientWindow)
58 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick)
59 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick)
60 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick)
61 EVT_PAINT(wxTipWindow::OnPaint)
62 END_EVENT_TABLE()
63
64
65 // ----------------------------------------------------------------------------
66 // wxTipWindow
67 // ----------------------------------------------------------------------------
68
69 wxTipWindow::wxTipWindow(wxWindow *parent,
70 const wxString& text,
71 wxCoord maxLength, wxTipWindow** windowPtr)
72 : wxPopupTransientWindow(parent)
73 {
74 m_windowPtr = windowPtr;
75
76 // set colours
77 SetForegroundColour(*wxBLACK);
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
86 // set size and position
87 Adjust(text, maxLength);
88 int x, y;
89 wxGetMousePosition(&x, &y);
90 Position(wxPoint(x, y+10), wxSize(0,0));
91
92 //Show(TRUE);
93 Popup();
94 SetFocus();
95 }
96
97 wxTipWindow::~wxTipWindow()
98 {
99 if (m_windowPtr)
100 {
101 *m_windowPtr = NULL;
102 }
103 }
104
105 void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event))
106 {
107 Close();
108 }
109
110
111 void wxTipWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
112 {
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 }
139 }
140
141
142 void wxTipWindow::Adjust(const wxString& text, wxCoord maxLength)
143 {
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);
202 }
203
204
205 void wxTipWindow::Close()
206 {
207 Show(FALSE);
208 Destroy();
209 }
210
211 #endif // wxUSE_TIPWINDOW
212