]> git.saurik.com Git - wxWidgets.git/blob - src/generic/tipwin.cpp
Add wxPanel::SetBackgroundBitmap().
[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 #ifdef 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 Destroy();
230 #else
231 wxFrame::Close();
232 #endif
233 }
234
235 // ----------------------------------------------------------------------------
236 // wxTipWindowView
237 // ----------------------------------------------------------------------------
238
239 wxTipWindowView::wxTipWindowView(wxWindow *parent)
240 : wxWindow(parent, wxID_ANY,
241 wxDefaultPosition, wxDefaultSize,
242 wxNO_BORDER)
243 {
244 // set colours
245 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT));
246 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));
247
248 #if !wxUSE_POPUPWIN
249 m_creationTime = wxGetLocalTime();
250 #endif // !wxUSE_POPUPWIN
251
252 m_parent = (wxTipWindow*)parent;
253 }
254
255 void wxTipWindowView::Adjust(const wxString& text, wxCoord maxLength)
256 {
257 wxClientDC dc(this);
258 dc.SetFont(GetFont());
259
260 // calculate the length: we want each line be no longer than maxLength
261 // pixels and we only break lines at words boundary
262 wxString current;
263 wxCoord height, width,
264 widthMax = 0;
265 m_parent->m_heightLine = 0;
266
267 bool breakLine = false;
268 for ( const wxChar *p = text.c_str(); ; p++ )
269 {
270 if ( *p == wxT('\n') || *p == wxT('\0') )
271 {
272 dc.GetTextExtent(current, &width, &height);
273 if ( width > widthMax )
274 widthMax = width;
275
276 if ( height > m_parent->m_heightLine )
277 m_parent->m_heightLine = height;
278
279 m_parent->m_textLines.Add(current);
280
281 if ( !*p )
282 {
283 // end of text
284 break;
285 }
286
287 current.clear();
288 breakLine = false;
289 }
290 else if ( breakLine && (*p == wxT(' ') || *p == wxT('\t')) )
291 {
292 // word boundary - break the line here
293 m_parent->m_textLines.Add(current);
294 current.clear();
295 breakLine = false;
296 }
297 else // line goes on
298 {
299 current += *p;
300 dc.GetTextExtent(current, &width, &height);
301 if ( width > maxLength )
302 breakLine = true;
303
304 if ( width > widthMax )
305 widthMax = width;
306
307 if ( height > m_parent->m_heightLine )
308 m_parent->m_heightLine = height;
309 }
310 }
311
312 // take into account the border size and the margins
313 width = 2*(TEXT_MARGIN_X + 1) + widthMax;
314 height = 2*(TEXT_MARGIN_Y + 1) + wx_truncate_cast(wxCoord, m_parent->m_textLines.GetCount())*m_parent->m_heightLine;
315 m_parent->SetClientSize(width, height);
316 SetSize(0, 0, width, height);
317 }
318
319 void wxTipWindowView::OnPaint(wxPaintEvent& WXUNUSED(event))
320 {
321 wxPaintDC dc(this);
322
323 wxRect rect;
324 wxSize size = GetClientSize();
325 rect.width = size.x;
326 rect.height = size.y;
327
328 // first filll the background
329 dc.SetBrush(wxBrush(GetBackgroundColour(), wxBRUSHSTYLE_SOLID));
330 dc.SetPen(wxPen(GetForegroundColour(), 1, wxPENSTYLE_SOLID));
331 dc.DrawRectangle(rect);
332
333 // and then draw the text line by line
334 dc.SetTextBackground(GetBackgroundColour());
335 dc.SetTextForeground(GetForegroundColour());
336 dc.SetFont(GetFont());
337
338 wxPoint pt;
339 pt.x = TEXT_MARGIN_X;
340 pt.y = TEXT_MARGIN_Y;
341 size_t count = m_parent->m_textLines.GetCount();
342 for ( size_t n = 0; n < count; n++ )
343 {
344 dc.DrawText(m_parent->m_textLines[n], pt);
345
346 pt.y += m_parent->m_heightLine;
347 }
348 }
349
350 void wxTipWindowView::OnMouseClick(wxMouseEvent& WXUNUSED(event))
351 {
352 m_parent->Close();
353 }
354
355 void wxTipWindowView::OnMouseMove(wxMouseEvent& event)
356 {
357 const wxRect& rectBound = m_parent->m_rectBound;
358
359 if ( rectBound.width &&
360 !rectBound.Contains(ClientToScreen(event.GetPosition())) )
361 {
362 // mouse left the bounding rect, disappear
363 m_parent->Close();
364 }
365 else
366 {
367 event.Skip();
368 }
369 }
370
371 #if !wxUSE_POPUPWIN
372 void wxTipWindowView::OnKillFocus(wxFocusEvent& WXUNUSED(event))
373 {
374 // Workaround the kill focus event happening just after creation in wxGTK
375 if (wxGetLocalTime() > m_creationTime + 1)
376 m_parent->Close();
377 }
378 #endif // !wxUSE_POPUPWIN
379
380 #endif // wxUSE_TIPWINDOW