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