]> git.saurik.com Git - wxWidgets.git/blob - src/generic/tipwin.cpp
build fix
[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 #ifndef WX_PRECOMP
28 #include "wx/dcclient.h"
29 #endif // WX_PRECOMP
30 #ifdef __WXGTK__
31 #include <gtk/gtk.h>
32 #endif
33 #include "wx/tipwin.h"
34
35 #if wxUSE_TIPWINDOW
36
37 #include "wx/timer.h"
38 #include "wx/settings.h"
39
40 // ----------------------------------------------------------------------------
41 // constants
42 // ----------------------------------------------------------------------------
43
44 static const wxCoord TEXT_MARGIN_X = 3;
45 static const wxCoord TEXT_MARGIN_Y = 3;
46
47 // ----------------------------------------------------------------------------
48 // wxTipWindowView
49 // ----------------------------------------------------------------------------
50
51 // Viewer window to put in the frame
52 class WXDLLEXPORT wxTipWindowView : public wxWindow
53 {
54 public:
55 wxTipWindowView(wxWindow *parent);
56
57 // event handlers
58 void OnPaint(wxPaintEvent& event);
59 void OnMouseClick(wxMouseEvent& event);
60 void OnMouseMove(wxMouseEvent& event);
61
62 #if !wxUSE_POPUPWIN
63 void OnKillFocus(wxFocusEvent& event);
64 #endif // wxUSE_POPUPWIN
65
66 // calculate the client rect we need to display the text
67 void Adjust(const wxString& text, wxCoord maxLength);
68
69 private:
70 wxTipWindow* m_parent;
71
72 #if !wxUSE_POPUPWIN
73 long m_creationTime;
74 #endif // !wxUSE_POPUPWIN
75
76 DECLARE_EVENT_TABLE()
77 DECLARE_NO_COPY_CLASS(wxTipWindowView)
78 };
79
80 // ============================================================================
81 // implementation
82 // ============================================================================
83
84 // ----------------------------------------------------------------------------
85 // event tables
86 // ----------------------------------------------------------------------------
87
88 BEGIN_EVENT_TABLE(wxTipWindow, wxTipWindowBase)
89 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick)
90 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick)
91 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick)
92
93 #if !wxUSE_POPUPWIN
94 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus)
95 EVT_ACTIVATE(wxTipWindow::OnActivate)
96 #endif // !wxUSE_POPUPWIN
97 END_EVENT_TABLE()
98
99 BEGIN_EVENT_TABLE(wxTipWindowView, wxWindow)
100 EVT_PAINT(wxTipWindowView::OnPaint)
101
102 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick)
103 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick)
104 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick)
105
106 EVT_MOTION(wxTipWindowView::OnMouseMove)
107
108 #if !wxUSE_POPUPWIN
109 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus)
110 #endif // !wxUSE_POPUPWIN
111 END_EVENT_TABLE()
112
113 // ----------------------------------------------------------------------------
114 // wxTipWindow
115 // ----------------------------------------------------------------------------
116
117 wxTipWindow::wxTipWindow(wxWindow *parent,
118 const wxString& text,
119 wxCoord maxLength,
120 wxTipWindow** windowPtr,
121 wxRect *rectBounds)
122 #if wxUSE_POPUPWIN
123 : wxPopupTransientWindow(parent)
124 #else
125 : wxFrame(parent, wxID_ANY, wxEmptyString,
126 wxDefaultPosition, wxDefaultSize,
127 wxNO_BORDER | wxFRAME_NO_TASKBAR )
128 #endif
129 {
130 SetTipWindowPtr(windowPtr);
131 if ( rectBounds )
132 {
133 SetBoundingRect(*rectBounds);
134 }
135
136 // set colours
137 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT));
138 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));
139
140 // set size, position and show it
141 m_view = new wxTipWindowView(this);
142 m_view->Adjust(text, maxLength);
143 m_view->SetFocus();
144
145 int x, y;
146 wxGetMousePosition(&x, &y);
147
148 // we want to show the tip below the mouse, not over it
149 //
150 // NB: the reason we use "/ 2" here is that we don't know where the current
151 // cursors hot spot is... it would be nice if we could find this out
152 // though
153 y += wxSystemSettings::GetMetric(wxSYS_CURSOR_Y) / 2;
154
155 #if wxUSE_POPUPWIN
156 Position(wxPoint(x, y), wxSize(0,0));
157 Popup(m_view);
158 #ifdef __WXGTK__
159 if (!GTK_WIDGET_HAS_GRAB(m_widget))
160 gtk_grab_add( m_widget );
161 #endif
162 #else
163 Move(x, y);
164 Show(true);
165 #endif
166 }
167
168 wxTipWindow::~wxTipWindow()
169 {
170 if ( m_windowPtr )
171 {
172 *m_windowPtr = NULL;
173 }
174 #ifdef wxUSE_POPUPWIN
175 #ifdef __WXGTK__
176 if (GTK_WIDGET_HAS_GRAB(m_widget))
177 gtk_grab_remove( m_widget );
178 #endif
179 #endif
180 }
181
182 void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event))
183 {
184 Close();
185 }
186
187 #if wxUSE_POPUPWIN
188
189 void wxTipWindow::OnDismiss()
190 {
191 Close();
192 }
193
194 #else // !wxUSE_POPUPWIN
195
196 void wxTipWindow::OnActivate(wxActivateEvent& event)
197 {
198 if (!event.GetActive())
199 Close();
200 }
201
202 void wxTipWindow::OnKillFocus(wxFocusEvent& WXUNUSED(event))
203 {
204 // Under Windows at least, we will get this immediately
205 // because when the view window is focussed, the
206 // tip window goes out of focus.
207 #ifdef __WXGTK__
208 Close();
209 #endif
210 }
211
212 #endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
213
214 void wxTipWindow::SetBoundingRect(const wxRect& rectBound)
215 {
216 m_rectBound = rectBound;
217 }
218
219 void wxTipWindow::Close()
220 {
221 if ( m_windowPtr )
222 {
223 *m_windowPtr = NULL;
224 m_windowPtr = NULL;
225 }
226
227 #if wxUSE_POPUPWIN
228 Show(false);
229 #ifdef __WXGTK__
230 if (GTK_WIDGET_HAS_GRAB(m_widget))
231 gtk_grab_remove( m_widget );
232 #endif
233 Destroy();
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 == _T('\n') || *p == _T('\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 == _T(' ') || *p == _T('\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(), wxSOLID));
334 dc.SetPen( wxPen(GetForegroundColour(), 1, wxSOLID) );
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.Inside(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