1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/tipwin.cpp
3 // Purpose: implementation of wxTipWindow
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/tipwin.h"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 static const wxCoord TEXT_MARGIN_X
= 3;
42 static const wxCoord TEXT_MARGIN_Y
= 3;
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // Viewer window to put in the frame
49 class WXDLLEXPORT wxTipWindowView
: public wxWindow
52 wxTipWindowView(wxWindow
*parent
);
55 void OnPaint(wxPaintEvent
& event
);
56 void OnMouseClick(wxMouseEvent
& event
);
57 void OnMouseMove(wxMouseEvent
& event
);
60 void OnKillFocus(wxFocusEvent
& event
);
61 #endif // wxUSE_POPUPWIN
63 // calculate the client rect we need to display the text
64 void Adjust(const wxString
& text
, wxCoord maxLength
);
67 wxTipWindow
* m_parent
;
71 #endif // !wxUSE_POPUPWIN
74 wxDECLARE_NO_COPY_CLASS(wxTipWindowView
);
77 // ============================================================================
79 // ============================================================================
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 BEGIN_EVENT_TABLE(wxTipWindow
, wxTipWindowBase
)
86 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick
)
87 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick
)
88 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick
)
91 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus
)
92 EVT_ACTIVATE(wxTipWindow::OnActivate
)
93 #endif // !wxUSE_POPUPWIN
96 BEGIN_EVENT_TABLE(wxTipWindowView
, wxWindow
)
97 EVT_PAINT(wxTipWindowView::OnPaint
)
99 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick
)
100 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick
)
101 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick
)
103 EVT_MOTION(wxTipWindowView::OnMouseMove
)
106 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus
)
107 #endif // !wxUSE_POPUPWIN
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 wxTipWindow::wxTipWindow(wxWindow
*parent
,
115 const wxString
& text
,
117 wxTipWindow
** windowPtr
,
120 : wxPopupTransientWindow(parent
)
122 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
123 wxDefaultPosition
, wxDefaultSize
,
124 wxNO_BORDER
| wxFRAME_NO_TASKBAR
)
127 SetTipWindowPtr(windowPtr
);
130 SetBoundingRect(*rectBounds
);
134 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
135 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
137 // set size, position and show it
138 m_view
= new wxTipWindowView(this);
139 m_view
->Adjust(text
, maxLength
);
143 wxGetMousePosition(&x
, &y
);
145 // we want to show the tip below the mouse, not over it
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
150 y
+= wxSystemSettings::GetMetric(wxSYS_CURSOR_Y
) / 2;
153 Position(wxPoint(x
, y
), wxSize(0,0));
156 m_view
->CaptureMouse();
164 wxTipWindow::~wxTipWindow()
172 if ( m_view
->HasCapture() )
173 m_view
->ReleaseMouse();
178 void wxTipWindow::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
185 void wxTipWindow::OnDismiss()
190 #else // !wxUSE_POPUPWIN
192 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
194 if (!event
.GetActive())
198 void wxTipWindow::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
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.
208 #endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
210 void wxTipWindow::SetBoundingRect(const wxRect
& rectBound
)
212 m_rectBound
= rectBound
;
215 void wxTipWindow::Close()
226 if ( m_view
->HasCapture() )
227 m_view
->ReleaseMouse();
229 // Under OS X we get destroyed because of wxEVT_KILL_FOCUS generated by
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
244 : wxWindow(parent
, wxID_ANY
,
245 wxDefaultPosition
, wxDefaultSize
,
249 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
250 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
253 m_creationTime
= wxGetLocalTime();
254 #endif // !wxUSE_POPUPWIN
256 m_parent
= (wxTipWindow
*)parent
;
259 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
262 dc
.SetFont(GetFont());
264 // calculate the length: we want each line be no longer than maxLength
265 // pixels and we only break lines at words boundary
267 wxCoord height
, width
,
269 m_parent
->m_heightLine
= 0;
271 bool breakLine
= false;
272 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
274 if ( *p
== wxT('\n') || *p
== wxT('\0') )
276 dc
.GetTextExtent(current
, &width
, &height
);
277 if ( width
> widthMax
)
280 if ( height
> m_parent
->m_heightLine
)
281 m_parent
->m_heightLine
= height
;
283 m_parent
->m_textLines
.Add(current
);
294 else if ( breakLine
&& (*p
== wxT(' ') || *p
== wxT('\t')) )
296 // word boundary - break the line here
297 m_parent
->m_textLines
.Add(current
);
304 dc
.GetTextExtent(current
, &width
, &height
);
305 if ( width
> maxLength
)
308 if ( width
> widthMax
)
311 if ( height
> m_parent
->m_heightLine
)
312 m_parent
->m_heightLine
= height
;
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
);
323 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
328 wxSize size
= GetClientSize();
330 rect
.height
= size
.y
;
332 // first filll the background
333 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
334 dc
.SetPen(wxPen(GetForegroundColour(), 1, wxPENSTYLE_SOLID
));
335 dc
.DrawRectangle(rect
);
337 // and then draw the text line by line
338 dc
.SetTextBackground(GetBackgroundColour());
339 dc
.SetTextForeground(GetForegroundColour());
340 dc
.SetFont(GetFont());
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
++ )
348 dc
.DrawText(m_parent
->m_textLines
[n
], pt
);
350 pt
.y
+= m_parent
->m_heightLine
;
354 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
359 void wxTipWindowView::OnMouseMove(wxMouseEvent
& event
)
361 const wxRect
& rectBound
= m_parent
->m_rectBound
;
363 if ( rectBound
.width
&&
364 !rectBound
.Contains(ClientToScreen(event
.GetPosition())) )
366 // mouse left the bounding rect, disappear
376 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
378 // Workaround the kill focus event happening just after creation in wxGTK
379 if (wxGetLocalTime() > m_creationTime
+ 1)
382 #endif // !wxUSE_POPUPWIN
384 #endif // wxUSE_TIPWINDOW