1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/tipwin.cpp
3 // Purpose: implementation of wxTipWindow
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/tipwin.h"
31 #include "wx/dcclient.h"
33 #include "wx/settings.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 static const wxCoord TEXT_MARGIN_X
= 3;
41 static const wxCoord TEXT_MARGIN_Y
= 3;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // Viewer window to put in the frame
48 class WXDLLEXPORT wxTipWindowView
: public wxWindow
51 wxTipWindowView(wxWindow
*parent
);
54 void OnPaint(wxPaintEvent
& event
);
55 void OnMouseClick(wxMouseEvent
& event
);
56 void OnMouseMove(wxMouseEvent
& event
);
59 void OnKillFocus(wxFocusEvent
& event
);
60 #endif // wxUSE_POPUPWIN
62 // calculate the client rect we need to display the text
63 void Adjust(const wxString
& text
, wxCoord maxLength
);
66 wxTipWindow
* m_parent
;
70 #endif // !wxUSE_POPUPWIN
73 wxDECLARE_NO_COPY_CLASS(wxTipWindowView
);
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 BEGIN_EVENT_TABLE(wxTipWindow
, wxTipWindowBase
)
85 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick
)
86 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick
)
87 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick
)
90 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus
)
91 EVT_ACTIVATE(wxTipWindow::OnActivate
)
92 #endif // !wxUSE_POPUPWIN
95 BEGIN_EVENT_TABLE(wxTipWindowView
, wxWindow
)
96 EVT_PAINT(wxTipWindowView::OnPaint
)
98 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick
)
99 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick
)
100 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick
)
102 EVT_MOTION(wxTipWindowView::OnMouseMove
)
105 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus
)
106 #endif // !wxUSE_POPUPWIN
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 wxTipWindow::wxTipWindow(wxWindow
*parent
,
114 const wxString
& text
,
116 wxTipWindow
** windowPtr
,
119 : wxPopupTransientWindow(parent
)
121 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
122 wxDefaultPosition
, wxDefaultSize
,
123 wxNO_BORDER
| wxFRAME_NO_TASKBAR
)
126 SetTipWindowPtr(windowPtr
);
129 SetBoundingRect(*rectBounds
);
133 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
134 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
136 // set size, position and show it
137 m_view
= new wxTipWindowView(this);
138 m_view
->Adjust(text
, maxLength
);
142 wxGetMousePosition(&x
, &y
);
144 // we want to show the tip below the mouse, not over it
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
149 y
+= wxSystemSettings::GetMetric(wxSYS_CURSOR_Y
) / 2;
152 Position(wxPoint(x
, y
), wxSize(0,0));
155 m_view
->CaptureMouse();
163 wxTipWindow::~wxTipWindow()
171 if ( m_view
->HasCapture() )
172 m_view
->ReleaseMouse();
177 void wxTipWindow::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
184 void wxTipWindow::OnDismiss()
189 #else // !wxUSE_POPUPWIN
191 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
193 if (!event
.GetActive())
197 void wxTipWindow::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
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.
207 #endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
209 void wxTipWindow::SetBoundingRect(const wxRect
& rectBound
)
211 m_rectBound
= rectBound
;
214 void wxTipWindow::Close()
225 if ( m_view
->HasCapture() )
226 m_view
->ReleaseMouse();
228 // Under OS X we get destroyed because of wxEVT_KILL_FOCUS generated by
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
243 : wxWindow(parent
, wxID_ANY
,
244 wxDefaultPosition
, wxDefaultSize
,
248 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
249 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
252 m_creationTime
= wxGetLocalTime();
253 #endif // !wxUSE_POPUPWIN
255 m_parent
= (wxTipWindow
*)parent
;
258 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
261 dc
.SetFont(GetFont());
263 // calculate the length: we want each line be no longer than maxLength
264 // pixels and we only break lines at words boundary
266 wxCoord height
, width
,
268 m_parent
->m_heightLine
= 0;
270 bool breakLine
= false;
271 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
273 if ( *p
== wxT('\n') || *p
== wxT('\0') )
275 dc
.GetTextExtent(current
, &width
, &height
);
276 if ( width
> widthMax
)
279 if ( height
> m_parent
->m_heightLine
)
280 m_parent
->m_heightLine
= height
;
282 m_parent
->m_textLines
.Add(current
);
293 else if ( breakLine
&& (*p
== wxT(' ') || *p
== wxT('\t')) )
295 // word boundary - break the line here
296 m_parent
->m_textLines
.Add(current
);
303 dc
.GetTextExtent(current
, &width
, &height
);
304 if ( width
> maxLength
)
307 if ( width
> widthMax
)
310 if ( height
> m_parent
->m_heightLine
)
311 m_parent
->m_heightLine
= height
;
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
);
322 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
327 wxSize size
= GetClientSize();
329 rect
.height
= size
.y
;
331 // first filll the background
332 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
333 dc
.SetPen(wxPen(GetForegroundColour(), 1, wxPENSTYLE_SOLID
));
334 dc
.DrawRectangle(rect
);
336 // and then draw the text line by line
337 dc
.SetTextBackground(GetBackgroundColour());
338 dc
.SetTextForeground(GetForegroundColour());
339 dc
.SetFont(GetFont());
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
++ )
347 dc
.DrawText(m_parent
->m_textLines
[n
], pt
);
349 pt
.y
+= m_parent
->m_heightLine
;
353 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
358 void wxTipWindowView::OnMouseMove(wxMouseEvent
& event
)
360 const wxRect
& rectBound
= m_parent
->m_rectBound
;
362 if ( rectBound
.width
&&
363 !rectBound
.Contains(ClientToScreen(event
.GetPosition())) )
365 // mouse left the bounding rect, disappear
375 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
377 // Workaround the kill focus event happening just after creation in wxGTK
378 if (wxGetLocalTime() > m_creationTime
+ 1)
381 #endif // !wxUSE_POPUPWIN
383 #endif // wxUSE_TIPWINDOW