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