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