Avoid double destruction of wxTipWindow under wxOSX.
[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 licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TIPWINDOW
28
29 #include "wx/tipwin.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/dcclient.h"
33 #include "wx/timer.h"
34 #include "wx/settings.h"
35 #endif // WX_PRECOMP
36
37 // ----------------------------------------------------------------------------
38 // constants
39 // ----------------------------------------------------------------------------
40
41 static const wxCoord TEXT_MARGIN_X = 3;
42 static const wxCoord TEXT_MARGIN_Y = 3;
43
44 // ----------------------------------------------------------------------------
45 // wxTipWindowView
46 // ----------------------------------------------------------------------------
47
48 // Viewer window to put in the frame
49 class WXDLLEXPORT wxTipWindowView : public wxWindow
50 {
51 public:
52 wxTipWindowView(wxWindow *parent);
53
54 // event handlers
55 void OnPaint(wxPaintEvent& event);
56 void OnMouseClick(wxMouseEvent& event);
57 void OnMouseMove(wxMouseEvent& event);
58
59 #if !wxUSE_POPUPWIN
60 void OnKillFocus(wxFocusEvent& event);
61 #endif // wxUSE_POPUPWIN
62
63 // calculate the client rect we need to display the text
64 void Adjust(const wxString& text, wxCoord maxLength);
65
66 private:
67 wxTipWindow* m_parent;
68
69 #if !wxUSE_POPUPWIN
70 long m_creationTime;
71 #endif // !wxUSE_POPUPWIN
72
73 DECLARE_EVENT_TABLE()
74 wxDECLARE_NO_COPY_CLASS(wxTipWindowView);
75 };
76
77 // ============================================================================
78 // implementation
79 // ============================================================================
80
81 // ----------------------------------------------------------------------------
82 // event tables
83 // ----------------------------------------------------------------------------
84
85 BEGIN_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
94 END_EVENT_TABLE()
95
96 BEGIN_EVENT_TABLE(wxTipWindowView, wxWindow)
97 EVT_PAINT(wxTipWindowView::OnPaint)
98
99 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick)
100 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick)
101 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick)
102
103 EVT_MOTION(wxTipWindowView::OnMouseMove)
104
105 #if !wxUSE_POPUPWIN
106 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus)
107 #endif // !wxUSE_POPUPWIN
108 END_EVENT_TABLE()
109
110 // ----------------------------------------------------------------------------
111 // wxTipWindow
112 // ----------------------------------------------------------------------------
113
114 wxTipWindow::wxTipWindow(wxWindow *parent,
115 const wxString& text,
116 wxCoord maxLength,
117 wxTipWindow** windowPtr,
118 wxRect *rectBounds)
119 #if wxUSE_POPUPWIN
120 : wxPopupTransientWindow(parent)
121 #else
122 : wxFrame(parent, wxID_ANY, wxEmptyString,
123 wxDefaultPosition, wxDefaultSize,
124 wxNO_BORDER | wxFRAME_NO_TASKBAR )
125 #endif
126 {
127 SetTipWindowPtr(windowPtr);
128 if ( rectBounds )
129 {
130 SetBoundingRect(*rectBounds);
131 }
132
133 // set colours
134 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT));
135 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));
136
137 // set size, position and show it
138 m_view = new wxTipWindowView(this);
139 m_view->Adjust(text, maxLength);
140 m_view->SetFocus();
141
142 int x, y;
143 wxGetMousePosition(&x, &y);
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
152 #if wxUSE_POPUPWIN
153 Position(wxPoint(x, y), wxSize(0,0));
154 Popup(m_view);
155 #ifdef __WXGTK__
156 m_view->CaptureMouse();
157 #endif
158 #else
159 Move(x, y);
160 Show(true);
161 #endif
162 }
163
164 wxTipWindow::~wxTipWindow()
165 {
166 if ( m_windowPtr )
167 {
168 *m_windowPtr = NULL;
169 }
170 #if wxUSE_POPUPWIN
171 #ifdef __WXGTK__
172 if ( m_view->HasCapture() )
173 m_view->ReleaseMouse();
174 #endif
175 #endif
176 }
177
178 void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event))
179 {
180 Close();
181 }
182
183 #if wxUSE_POPUPWIN
184
185 void wxTipWindow::OnDismiss()
186 {
187 Close();
188 }
189
190 #else // !wxUSE_POPUPWIN
191
192 void wxTipWindow::OnActivate(wxActivateEvent& event)
193 {
194 if (!event.GetActive())
195 Close();
196 }
197
198 void 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 }
207
208 #endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
209
210 void wxTipWindow::SetBoundingRect(const wxRect& rectBound)
211 {
212 m_rectBound = rectBound;
213 }
214
215 void wxTipWindow::Close()
216 {
217 if ( m_windowPtr )
218 {
219 *m_windowPtr = NULL;
220 m_windowPtr = NULL;
221 }
222
223 #if wxUSE_POPUPWIN
224 Show(false);
225 #ifdef __WXGTK__
226 if ( m_view->HasCapture() )
227 m_view->ReleaseMouse();
228 #endif
229 // Under OS X we get destroyed because of wxEVT_KILL_FOCUS generated by
230 // Show(false).
231 #ifndef __WXOSX__
232 Destroy();
233 #endif
234 #else
235 wxFrame::Close();
236 #endif
237 }
238
239 // ----------------------------------------------------------------------------
240 // wxTipWindowView
241 // ----------------------------------------------------------------------------
242
243 wxTipWindowView::wxTipWindowView(wxWindow *parent)
244 : wxWindow(parent, wxID_ANY,
245 wxDefaultPosition, wxDefaultSize,
246 wxNO_BORDER)
247 {
248 // set colours
249 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT));
250 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));
251
252 #if !wxUSE_POPUPWIN
253 m_creationTime = wxGetLocalTime();
254 #endif // !wxUSE_POPUPWIN
255
256 m_parent = (wxTipWindow*)parent;
257 }
258
259 void wxTipWindowView::Adjust(const wxString& text, wxCoord maxLength)
260 {
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;
269 m_parent->m_heightLine = 0;
270
271 bool breakLine = false;
272 for ( const wxChar *p = text.c_str(); ; p++ )
273 {
274 if ( *p == wxT('\n') || *p == wxT('\0') )
275 {
276 dc.GetTextExtent(current, &width, &height);
277 if ( width > widthMax )
278 widthMax = width;
279
280 if ( height > m_parent->m_heightLine )
281 m_parent->m_heightLine = height;
282
283 m_parent->m_textLines.Add(current);
284
285 if ( !*p )
286 {
287 // end of text
288 break;
289 }
290
291 current.clear();
292 breakLine = false;
293 }
294 else if ( breakLine && (*p == wxT(' ') || *p == wxT('\t')) )
295 {
296 // word boundary - break the line here
297 m_parent->m_textLines.Add(current);
298 current.clear();
299 breakLine = false;
300 }
301 else // line goes on
302 {
303 current += *p;
304 dc.GetTextExtent(current, &width, &height);
305 if ( width > maxLength )
306 breakLine = true;
307
308 if ( width > widthMax )
309 widthMax = width;
310
311 if ( height > m_parent->m_heightLine )
312 m_parent->m_heightLine = height;
313 }
314 }
315
316 // take into account the border size and the margins
317 width = 2*(TEXT_MARGIN_X + 1) + widthMax;
318 height = 2*(TEXT_MARGIN_Y + 1) + wx_truncate_cast(wxCoord, m_parent->m_textLines.GetCount())*m_parent->m_heightLine;
319 m_parent->SetClientSize(width, height);
320 SetSize(0, 0, width, height);
321 }
322
323 void wxTipWindowView::OnPaint(wxPaintEvent& WXUNUSED(event))
324 {
325 wxPaintDC dc(this);
326
327 wxRect rect;
328 wxSize size = GetClientSize();
329 rect.width = size.x;
330 rect.height = size.y;
331
332 // first filll the background
333 dc.SetBrush(wxBrush(GetBackgroundColour(), wxBRUSHSTYLE_SOLID));
334 dc.SetPen(wxPen(GetForegroundColour(), 1, wxPENSTYLE_SOLID));
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
354 void wxTipWindowView::OnMouseClick(wxMouseEvent& WXUNUSED(event))
355 {
356 m_parent->Close();
357 }
358
359 void wxTipWindowView::OnMouseMove(wxMouseEvent& event)
360 {
361 const wxRect& rectBound = m_parent->m_rectBound;
362
363 if ( rectBound.width &&
364 !rectBound.Contains(ClientToScreen(event.GetPosition())) )
365 {
366 // mouse left the bounding rect, disappear
367 m_parent->Close();
368 }
369 else
370 {
371 event.Skip();
372 }
373 }
374
375 #if !wxUSE_POPUPWIN
376 void 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 }
382 #endif // !wxUSE_POPUPWIN
383
384 #endif // wxUSE_TIPWINDOW