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();
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
240 : wxWindow(parent
, wxID_ANY
,
241 wxDefaultPosition
, wxDefaultSize
,
245 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
246 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
249 m_creationTime
= wxGetLocalTime();
250 #endif // !wxUSE_POPUPWIN
252 m_parent
= (wxTipWindow
*)parent
;
255 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
258 dc
.SetFont(GetFont());
260 // calculate the length: we want each line be no longer than maxLength
261 // pixels and we only break lines at words boundary
263 wxCoord height
, width
,
265 m_parent
->m_heightLine
= 0;
267 bool breakLine
= false;
268 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
270 if ( *p
== wxT('\n') || *p
== wxT('\0') )
272 dc
.GetTextExtent(current
, &width
, &height
);
273 if ( width
> widthMax
)
276 if ( height
> m_parent
->m_heightLine
)
277 m_parent
->m_heightLine
= height
;
279 m_parent
->m_textLines
.Add(current
);
290 else if ( breakLine
&& (*p
== wxT(' ') || *p
== wxT('\t')) )
292 // word boundary - break the line here
293 m_parent
->m_textLines
.Add(current
);
300 dc
.GetTextExtent(current
, &width
, &height
);
301 if ( width
> maxLength
)
304 if ( width
> widthMax
)
307 if ( height
> m_parent
->m_heightLine
)
308 m_parent
->m_heightLine
= height
;
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
);
319 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
324 wxSize size
= GetClientSize();
326 rect
.height
= size
.y
;
328 // first filll the background
329 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
330 dc
.SetPen(wxPen(GetForegroundColour(), 1, wxPENSTYLE_SOLID
));
331 dc
.DrawRectangle(rect
);
333 // and then draw the text line by line
334 dc
.SetTextBackground(GetBackgroundColour());
335 dc
.SetTextForeground(GetForegroundColour());
336 dc
.SetFont(GetFont());
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
++ )
344 dc
.DrawText(m_parent
->m_textLines
[n
], pt
);
346 pt
.y
+= m_parent
->m_heightLine
;
350 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
355 void wxTipWindowView::OnMouseMove(wxMouseEvent
& event
)
357 const wxRect
& rectBound
= m_parent
->m_rectBound
;
359 if ( rectBound
.width
&&
360 !rectBound
.Contains(ClientToScreen(event
.GetPosition())) )
362 // mouse left the bounding rect, disappear
372 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
374 // Workaround the kill focus event happening just after creation in wxGTK
375 if (wxGetLocalTime() > m_creationTime
+ 1)
378 #endif // !wxUSE_POPUPWIN
380 #endif // wxUSE_TIPWINDOW