]> git.saurik.com Git - wxWidgets.git/blob - src/generic/tipwin.cpp
fixed DrawTextFormatted to work in O(n) instead of O(n^2) if the text doesn't fit...
[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 #ifdef __GNUG__
21 #pragma implementation "tipwin.h"
22 #endif
23
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/dcclient.h"
33 #endif // WX_PRECOMP
34
35 #include "wx/tipwin.h"
36
37 #if wxUSE_TIPWINDOW
38
39 #include "wx/timer.h"
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, -1, _T(""),
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 #else
161 Move(x, y);
162 Show(TRUE);
163 #endif
164 }
165
166 wxTipWindow::~wxTipWindow()
167 {
168 if ( m_windowPtr )
169 {
170 *m_windowPtr = NULL;
171 }
172 }
173
174 void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event))
175 {
176 Close();
177 }
178
179 #if wxUSE_POPUPWIN
180
181 void wxTipWindow::OnDismiss()
182 {
183 Close();
184 }
185
186 #else // !wxUSE_POPUPWIN
187
188 void wxTipWindow::OnActivate(wxActivateEvent& event)
189 {
190 if (!event.GetActive())
191 Close();
192 }
193
194 void wxTipWindow::OnKillFocus(wxFocusEvent& WXUNUSED(event))
195 {
196 // Under Windows at least, we will get this immediately
197 // because when the view window is focussed, the
198 // tip window goes out of focus.
199 #ifdef __WXGTK__
200 Close();
201 #endif
202 }
203
204 #endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
205
206 void wxTipWindow::SetBoundingRect(const wxRect& rectBound)
207 {
208 m_rectBound = rectBound;
209 }
210
211 void wxTipWindow::Close()
212 {
213 if ( m_windowPtr )
214 {
215 *m_windowPtr = NULL;
216 m_windowPtr = NULL;
217 }
218
219 #if wxUSE_POPUPWIN
220 Show(FALSE);
221 Destroy();
222 #else
223 wxFrame::Close();
224 #endif
225 }
226
227 // ----------------------------------------------------------------------------
228 // wxTipWindowView
229 // ----------------------------------------------------------------------------
230
231 wxTipWindowView::wxTipWindowView(wxWindow *parent)
232 : wxWindow(parent, -1,
233 wxDefaultPosition, wxDefaultSize,
234 wxNO_BORDER)
235 {
236 // set colours
237 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT));
238 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));
239
240 #if !wxUSE_POPUPWIN
241 m_creationTime = wxGetLocalTime();
242 #endif // !wxUSE_POPUPWIN
243
244 m_parent = (wxTipWindow*)parent;
245 }
246
247 void wxTipWindowView::Adjust(const wxString& text, wxCoord maxLength)
248 {
249 wxClientDC dc(this);
250 dc.SetFont(GetFont());
251
252 // calculate the length: we want each line be no longer than maxLength
253 // pixels and we only break lines at words boundary
254 wxString current;
255 wxCoord height, width,
256 widthMax = 0;
257 m_parent->m_heightLine = 0;
258
259 bool breakLine = FALSE;
260 for ( const wxChar *p = text.c_str(); ; p++ )
261 {
262 if ( *p == _T('\n') || *p == _T('\0') )
263 {
264 dc.GetTextExtent(current, &width, &height);
265 if ( width > widthMax )
266 widthMax = width;
267
268 if ( height > m_parent->m_heightLine )
269 m_parent->m_heightLine = height;
270
271 m_parent->m_textLines.Add(current);
272
273 if ( !*p )
274 {
275 // end of text
276 break;
277 }
278
279 current.clear();
280 breakLine = FALSE;
281 }
282 else if ( breakLine && (*p == _T(' ') || *p == _T('\t')) )
283 {
284 // word boundary - break the line here
285 m_parent->m_textLines.Add(current);
286 current.clear();
287 breakLine = FALSE;
288 }
289 else // line goes on
290 {
291 current += *p;
292 dc.GetTextExtent(current, &width, &height);
293 if ( width > maxLength )
294 breakLine = TRUE;
295
296 if ( width > widthMax )
297 widthMax = width;
298
299 if ( height > m_parent->m_heightLine )
300 m_parent->m_heightLine = height;
301 }
302 }
303
304 // take into account the border size and the margins
305 width = 2*(TEXT_MARGIN_X + 1) + widthMax;
306 height = 2*(TEXT_MARGIN_Y + 1) + m_parent->m_textLines.GetCount()*m_parent->m_heightLine;
307 m_parent->SetClientSize(width, height);
308 SetSize(0, 0, width, height);
309 }
310
311 void wxTipWindowView::OnPaint(wxPaintEvent& WXUNUSED(event))
312 {
313 wxPaintDC dc(this);
314
315 wxRect rect;
316 wxSize size = GetClientSize();
317 rect.width = size.x;
318 rect.height = size.y;
319
320 // first filll the background
321 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
322 dc.SetPen( wxPen(GetForegroundColour(), 1, wxSOLID) );
323 dc.DrawRectangle(rect);
324
325 // and then draw the text line by line
326 dc.SetTextBackground(GetBackgroundColour());
327 dc.SetTextForeground(GetForegroundColour());
328 dc.SetFont(GetFont());
329
330 wxPoint pt;
331 pt.x = TEXT_MARGIN_X;
332 pt.y = TEXT_MARGIN_Y;
333 size_t count = m_parent->m_textLines.GetCount();
334 for ( size_t n = 0; n < count; n++ )
335 {
336 dc.DrawText(m_parent->m_textLines[n], pt);
337
338 pt.y += m_parent->m_heightLine;
339 }
340 }
341
342 void wxTipWindowView::OnMouseClick(wxMouseEvent& WXUNUSED(event))
343 {
344 m_parent->Close();
345 }
346
347 void wxTipWindowView::OnMouseMove(wxMouseEvent& event)
348 {
349 const wxRect& rectBound = m_parent->m_rectBound;
350
351 if ( rectBound.width &&
352 !rectBound.Inside(ClientToScreen(event.GetPosition())) )
353 {
354 // mouse left the bounding rect, disappear
355 m_parent->Close();
356 }
357 else
358 {
359 event.Skip();
360 }
361 }
362
363 #if !wxUSE_POPUPWIN
364 void wxTipWindowView::OnKillFocus(wxFocusEvent& WXUNUSED(event))
365 {
366 // Workaround the kill focus event happening just after creation in wxGTK
367 if (wxGetLocalTime() > m_creationTime + 1)
368 m_parent->Close();
369 }
370 #endif // !wxUSE_POPUPWIN
371
372 #endif // wxUSE_TIPWINDOW