]> git.saurik.com Git - wxWidgets.git/blame - src/generic/tipwin.cpp
Add wxAnyScrollHelperBase to reduce code duplication in wxVarScrollHelperBase.
[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
01fa3fe7 7// Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 8// Licence: wxWindows licence
01fa3fe7
VZ
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
95427194 19// For compilers that support precompilation, includes "wx/wx.h".
01fa3fe7
VZ
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
c0badb70
WS
26#if wxUSE_TIPWINDOW
27
28#include "wx/tipwin.h"
29
01fa3fe7
VZ
30#ifndef WX_PRECOMP
31 #include "wx/dcclient.h"
c0badb70 32 #include "wx/timer.h"
9eddec69 33 #include "wx/settings.h"
01fa3fe7 34#endif // WX_PRECOMP
c0badb70 35
01fa3fe7
VZ
36// ----------------------------------------------------------------------------
37// constants
38// ----------------------------------------------------------------------------
39
40static const wxCoord TEXT_MARGIN_X = 3;
41static const wxCoord TEXT_MARGIN_Y = 3;
42
01fa3fe7 43// ----------------------------------------------------------------------------
dafbe8c0 44// wxTipWindowView
01fa3fe7
VZ
45// ----------------------------------------------------------------------------
46
961c54c3 47// Viewer window to put in the frame
dafbe8c0 48class WXDLLEXPORT wxTipWindowView : public wxWindow
961c54c3
RD
49{
50public:
51 wxTipWindowView(wxWindow *parent);
52
53 // event handlers
54 void OnPaint(wxPaintEvent& event);
55 void OnMouseClick(wxMouseEvent& event);
dafbe8c0
VZ
56 void OnMouseMove(wxMouseEvent& event);
57
961c54c3
RD
58#if !wxUSE_POPUPWIN
59 void OnKillFocus(wxFocusEvent& event);
dafbe8c0
VZ
60#endif // wxUSE_POPUPWIN
61
961c54c3
RD
62 // calculate the client rect we need to display the text
63 void Adjust(const wxString& text, wxCoord maxLength);
64
65private:
961c54c3
RD
66 wxTipWindow* m_parent;
67
dafbe8c0
VZ
68#if !wxUSE_POPUPWIN
69 long m_creationTime;
70#endif // !wxUSE_POPUPWIN
71
72 DECLARE_EVENT_TABLE()
c0c133e1 73 wxDECLARE_NO_COPY_CLASS(wxTipWindowView);
961c54c3
RD
74};
75
dafbe8c0
VZ
76// ============================================================================
77// implementation
78// ============================================================================
79
80// ----------------------------------------------------------------------------
81// event tables
82// ----------------------------------------------------------------------------
83
84BEGIN_EVENT_TABLE(wxTipWindow, wxTipWindowBase)
85 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick)
86 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick)
87 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick)
88
89#if !wxUSE_POPUPWIN
90 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus)
91 EVT_ACTIVATE(wxTipWindow::OnActivate)
92#endif // !wxUSE_POPUPWIN
93END_EVENT_TABLE()
94
961c54c3
RD
95BEGIN_EVENT_TABLE(wxTipWindowView, wxWindow)
96 EVT_PAINT(wxTipWindowView::OnPaint)
dafbe8c0 97
961c54c3
RD
98 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick)
99 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick)
100 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick)
dafbe8c0
VZ
101
102 EVT_MOTION(wxTipWindowView::OnMouseMove)
103
961c54c3
RD
104#if !wxUSE_POPUPWIN
105 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus)
dafbe8c0 106#endif // !wxUSE_POPUPWIN
961c54c3
RD
107END_EVENT_TABLE()
108
01fa3fe7
VZ
109// ----------------------------------------------------------------------------
110// wxTipWindow
111// ----------------------------------------------------------------------------
112
113wxTipWindow::wxTipWindow(wxWindow *parent,
114 const wxString& text,
dafbe8c0
VZ
115 wxCoord maxLength,
116 wxTipWindow** windowPtr,
117 wxRect *rectBounds)
961c54c3 118#if wxUSE_POPUPWIN
8962e1d9 119 : wxPopupTransientWindow(parent)
961c54c3 120#else
ca65c044 121 : wxFrame(parent, wxID_ANY, wxEmptyString,
961c54c3
RD
122 wxDefaultPosition, wxDefaultSize,
123 wxNO_BORDER | wxFRAME_NO_TASKBAR )
124#endif
01fa3fe7 125{
dafbe8c0
VZ
126 SetTipWindowPtr(windowPtr);
127 if ( rectBounds )
128 {
129 SetBoundingRect(*rectBounds);
130 }
8962e1d9 131
01fa3fe7 132 // set colours
dafbe8c0
VZ
133 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT));
134 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));
04ef50df 135
961c54c3 136 // set size, position and show it
dafbe8c0
VZ
137 m_view = new wxTipWindowView(this);
138 m_view->Adjust(text, maxLength);
139 m_view->SetFocus();
140
01fa3fe7
VZ
141 int x, y;
142 wxGetMousePosition(&x, &y);
dafbe8c0
VZ
143
144 // we want to show the tip below the mouse, not over it
145 //
146 // NB: the reason we use "/ 2" here is that we don't know where the current
147 // cursors hot spot is... it would be nice if we could find this out
148 // though
149 y += wxSystemSettings::GetMetric(wxSYS_CURSOR_Y) / 2;
150
961c54c3 151#if wxUSE_POPUPWIN
c47addef 152 Position(wxPoint(x, y), wxSize(0,0));
dafbe8c0 153 Popup(m_view);
654070ca 154 #ifdef __WXGTK__
e16ceb3c 155 m_view->CaptureMouse();
ca65c044 156 #endif
961c54c3 157#else
dafbe8c0 158 Move(x, y);
ca65c044 159 Show(true);
961c54c3 160#endif
173e8bbf
JS
161}
162
163wxTipWindow::~wxTipWindow()
164{
dafbe8c0 165 if ( m_windowPtr )
173e8bbf
JS
166 {
167 *m_windowPtr = NULL;
168 }
32b183b5 169 #if wxUSE_POPUPWIN
654070ca 170 #ifdef __WXGTK__
e16ceb3c
VZ
171 if ( m_view->HasCapture() )
172 m_view->ReleaseMouse();
654070ca
VZ
173 #endif
174 #endif
173e8bbf
JS
175}
176
33ac7e6f 177void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event))
173e8bbf
JS
178{
179 Close();
180}
181
dafbe8c0
VZ
182#if wxUSE_POPUPWIN
183
184void wxTipWindow::OnDismiss()
185{
186 Close();
187}
188
189#else // !wxUSE_POPUPWIN
190
961c54c3 191void wxTipWindow::OnActivate(wxActivateEvent& event)
173e8bbf 192{
961c54c3
RD
193 if (!event.GetActive())
194 Close();
195}
8962e1d9 196
961c54c3
RD
197void wxTipWindow::OnKillFocus(wxFocusEvent& WXUNUSED(event))
198{
199 // Under Windows at least, we will get this immediately
200 // because when the view window is focussed, the
201 // tip window goes out of focus.
202#ifdef __WXGTK__
203 Close();
204#endif
205}
8962e1d9 206
dafbe8c0
VZ
207#endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
208
209void wxTipWindow::SetBoundingRect(const wxRect& rectBound)
210{
211 m_rectBound = rectBound;
212}
8962e1d9 213
961c54c3
RD
214void wxTipWindow::Close()
215{
dafbe8c0
VZ
216 if ( m_windowPtr )
217 {
218 *m_windowPtr = NULL;
219 m_windowPtr = NULL;
220 }
221
961c54c3 222#if wxUSE_POPUPWIN
ca65c044 223 Show(false);
654070ca 224 #ifdef __WXGTK__
e16ceb3c
VZ
225 if ( m_view->HasCapture() )
226 m_view->ReleaseMouse();
ca65c044 227 #endif
baa6b332
VZ
228 // Under OS X we get destroyed because of wxEVT_KILL_FOCUS generated by
229 // Show(false).
230 #ifndef __WXOSX__
231 Destroy();
232 #endif
961c54c3
RD
233#else
234 wxFrame::Close();
235#endif
236}
8962e1d9 237
961c54c3
RD
238// ----------------------------------------------------------------------------
239// wxTipWindowView
240// ----------------------------------------------------------------------------
8962e1d9 241
961c54c3 242wxTipWindowView::wxTipWindowView(wxWindow *parent)
ca65c044 243 : wxWindow(parent, wxID_ANY,
dafbe8c0
VZ
244 wxDefaultPosition, wxDefaultSize,
245 wxNO_BORDER)
961c54c3
RD
246{
247 // set colours
dafbe8c0
VZ
248 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT));
249 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));
dafbe8c0
VZ
250
251#if !wxUSE_POPUPWIN
961c54c3 252 m_creationTime = wxGetLocalTime();
dafbe8c0
VZ
253#endif // !wxUSE_POPUPWIN
254
961c54c3 255 m_parent = (wxTipWindow*)parent;
173e8bbf
JS
256}
257
961c54c3 258void wxTipWindowView::Adjust(const wxString& text, wxCoord maxLength)
173e8bbf 259{
8962e1d9
RD
260 wxClientDC dc(this);
261 dc.SetFont(GetFont());
262
263 // calculate the length: we want each line be no longer than maxLength
264 // pixels and we only break lines at words boundary
265 wxString current;
266 wxCoord height, width,
267 widthMax = 0;
961c54c3 268 m_parent->m_heightLine = 0;
8962e1d9 269
ca65c044 270 bool breakLine = false;
8962e1d9
RD
271 for ( const wxChar *p = text.c_str(); ; p++ )
272 {
9a83f860 273 if ( *p == wxT('\n') || *p == wxT('\0') )
8962e1d9
RD
274 {
275 dc.GetTextExtent(current, &width, &height);
276 if ( width > widthMax )
277 widthMax = width;
278
961c54c3
RD
279 if ( height > m_parent->m_heightLine )
280 m_parent->m_heightLine = height;
8962e1d9 281
961c54c3 282 m_parent->m_textLines.Add(current);
8962e1d9
RD
283
284 if ( !*p )
285 {
286 // end of text
287 break;
288 }
289
290 current.clear();
ca65c044 291 breakLine = false;
8962e1d9 292 }
9a83f860 293 else if ( breakLine && (*p == wxT(' ') || *p == wxT('\t')) )
8962e1d9
RD
294 {
295 // word boundary - break the line here
961c54c3 296 m_parent->m_textLines.Add(current);
8962e1d9 297 current.clear();
ca65c044 298 breakLine = false;
8962e1d9
RD
299 }
300 else // line goes on
301 {
302 current += *p;
303 dc.GetTextExtent(current, &width, &height);
304 if ( width > maxLength )
ca65c044 305 breakLine = true;
8962e1d9
RD
306
307 if ( width > widthMax )
308 widthMax = width;
309
961c54c3
RD
310 if ( height > m_parent->m_heightLine )
311 m_parent->m_heightLine = height;
8962e1d9
RD
312 }
313 }
314
315 // take into account the border size and the margins
961c54c3 316 width = 2*(TEXT_MARGIN_X + 1) + widthMax;
4a10ea8b 317 height = 2*(TEXT_MARGIN_Y + 1) + wx_truncate_cast(wxCoord, m_parent->m_textLines.GetCount())*m_parent->m_heightLine;
961c54c3
RD
318 m_parent->SetClientSize(width, height);
319 SetSize(0, 0, width, height);
173e8bbf
JS
320}
321
961c54c3
RD
322void wxTipWindowView::OnPaint(wxPaintEvent& WXUNUSED(event))
323{
324 wxPaintDC dc(this);
8962e1d9 325
961c54c3
RD
326 wxRect rect;
327 wxSize size = GetClientSize();
328 rect.width = size.x;
329 rect.height = size.y;
330
331 // first filll the background
04ee05f9
PC
332 dc.SetBrush(wxBrush(GetBackgroundColour(), wxBRUSHSTYLE_SOLID));
333 dc.SetPen(wxPen(GetForegroundColour(), 1, wxPENSTYLE_SOLID));
961c54c3
RD
334 dc.DrawRectangle(rect);
335
336 // and then draw the text line by line
337 dc.SetTextBackground(GetBackgroundColour());
338 dc.SetTextForeground(GetForegroundColour());
339 dc.SetFont(GetFont());
340
341 wxPoint pt;
342 pt.x = TEXT_MARGIN_X;
343 pt.y = TEXT_MARGIN_Y;
344 size_t count = m_parent->m_textLines.GetCount();
345 for ( size_t n = 0; n < count; n++ )
346 {
347 dc.DrawText(m_parent->m_textLines[n], pt);
348
349 pt.y += m_parent->m_heightLine;
350 }
351}
352
353void wxTipWindowView::OnMouseClick(wxMouseEvent& WXUNUSED(event))
8962e1d9 354{
961c54c3 355 m_parent->Close();
8962e1d9
RD
356}
357
dafbe8c0
VZ
358void wxTipWindowView::OnMouseMove(wxMouseEvent& event)
359{
360 const wxRect& rectBound = m_parent->m_rectBound;
361
362 if ( rectBound.width &&
22a35096 363 !rectBound.Contains(ClientToScreen(event.GetPosition())) )
dafbe8c0
VZ
364 {
365 // mouse left the bounding rect, disappear
366 m_parent->Close();
367 }
368 else
369 {
370 event.Skip();
371 }
372}
373
961c54c3
RD
374#if !wxUSE_POPUPWIN
375void wxTipWindowView::OnKillFocus(wxFocusEvent& WXUNUSED(event))
376{
377 // Workaround the kill focus event happening just after creation in wxGTK
378 if (wxGetLocalTime() > m_creationTime + 1)
379 m_parent->Close();
380}
dafbe8c0 381#endif // !wxUSE_POPUPWIN
8962e1d9 382
dafbe8c0 383#endif // wxUSE_TIPWINDOW