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"
40 #include "wx/settings.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 static const wxCoord TEXT_MARGIN_X
= 3;
47 static const wxCoord TEXT_MARGIN_Y
= 3;
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // Viewer window to put in the frame
54 class WXDLLEXPORT wxTipWindowView
: public wxWindow
57 wxTipWindowView(wxWindow
*parent
);
60 void OnPaint(wxPaintEvent
& event
);
61 void OnMouseClick(wxMouseEvent
& event
);
62 void OnMouseMove(wxMouseEvent
& event
);
65 void OnKillFocus(wxFocusEvent
& event
);
66 #endif // wxUSE_POPUPWIN
68 // calculate the client rect we need to display the text
69 void Adjust(const wxString
& text
, wxCoord maxLength
);
72 wxTipWindow
* m_parent
;
76 #endif // !wxUSE_POPUPWIN
79 DECLARE_NO_COPY_CLASS(wxTipWindowView
)
82 // ============================================================================
84 // ============================================================================
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 BEGIN_EVENT_TABLE(wxTipWindow
, wxTipWindowBase
)
91 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick
)
92 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick
)
93 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick
)
96 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus
)
97 EVT_ACTIVATE(wxTipWindow::OnActivate
)
98 #endif // !wxUSE_POPUPWIN
101 BEGIN_EVENT_TABLE(wxTipWindowView
, wxWindow
)
102 EVT_PAINT(wxTipWindowView::OnPaint
)
104 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick
)
105 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick
)
106 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick
)
108 EVT_MOTION(wxTipWindowView::OnMouseMove
)
111 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus
)
112 #endif // !wxUSE_POPUPWIN
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 wxTipWindow::wxTipWindow(wxWindow
*parent
,
120 const wxString
& text
,
122 wxTipWindow
** windowPtr
,
125 : wxPopupTransientWindow(parent
)
127 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
128 wxDefaultPosition
, wxDefaultSize
,
129 wxNO_BORDER
| wxFRAME_NO_TASKBAR
)
132 SetTipWindowPtr(windowPtr
);
135 SetBoundingRect(*rectBounds
);
139 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
140 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
142 // set size, position and show it
143 m_view
= new wxTipWindowView(this);
144 m_view
->Adjust(text
, maxLength
);
148 wxGetMousePosition(&x
, &y
);
150 // we want to show the tip below the mouse, not over it
152 // NB: the reason we use "/ 2" here is that we don't know where the current
153 // cursors hot spot is... it would be nice if we could find this out
155 y
+= wxSystemSettings::GetMetric(wxSYS_CURSOR_Y
) / 2;
158 Position(wxPoint(x
, y
), wxSize(0,0));
161 if (!GTK_WIDGET_HAS_GRAB(m_widget
))
162 gtk_grab_add( m_widget
);
170 wxTipWindow::~wxTipWindow()
176 #ifdef wxUSE_POPUPWIN
178 if (GTK_WIDGET_HAS_GRAB(m_widget
))
179 gtk_grab_remove( m_widget
);
184 void wxTipWindow::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
191 void wxTipWindow::OnDismiss()
196 #else // !wxUSE_POPUPWIN
198 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
200 if (!event
.GetActive())
204 void wxTipWindow::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
206 // Under Windows at least, we will get this immediately
207 // because when the view window is focussed, the
208 // tip window goes out of focus.
214 #endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
216 void wxTipWindow::SetBoundingRect(const wxRect
& rectBound
)
218 m_rectBound
= rectBound
;
221 void wxTipWindow::Close()
232 if (GTK_WIDGET_HAS_GRAB(m_widget
))
233 gtk_grab_remove( m_widget
);
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
246 : wxWindow(parent
, wxID_ANY
,
247 wxDefaultPosition
, wxDefaultSize
,
251 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
252 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
255 m_creationTime
= wxGetLocalTime();
256 #endif // !wxUSE_POPUPWIN
258 m_parent
= (wxTipWindow
*)parent
;
261 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
264 dc
.SetFont(GetFont());
266 // calculate the length: we want each line be no longer than maxLength
267 // pixels and we only break lines at words boundary
269 wxCoord height
, width
,
271 m_parent
->m_heightLine
= 0;
273 bool breakLine
= false;
274 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
276 if ( *p
== _T('\n') || *p
== _T('\0') )
278 dc
.GetTextExtent(current
, &width
, &height
);
279 if ( width
> widthMax
)
282 if ( height
> m_parent
->m_heightLine
)
283 m_parent
->m_heightLine
= height
;
285 m_parent
->m_textLines
.Add(current
);
296 else if ( breakLine
&& (*p
== _T(' ') || *p
== _T('\t')) )
298 // word boundary - break the line here
299 m_parent
->m_textLines
.Add(current
);
306 dc
.GetTextExtent(current
, &width
, &height
);
307 if ( width
> maxLength
)
310 if ( width
> widthMax
)
313 if ( height
> m_parent
->m_heightLine
)
314 m_parent
->m_heightLine
= height
;
318 // take into account the border size and the margins
319 width
= 2*(TEXT_MARGIN_X
+ 1) + widthMax
;
320 height
= 2*(TEXT_MARGIN_Y
+ 1) + wx_truncate_cast(wxCoord
, m_parent
->m_textLines
.GetCount())*m_parent
->m_heightLine
;
321 m_parent
->SetClientSize(width
, height
);
322 SetSize(0, 0, width
, height
);
325 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
330 wxSize size
= GetClientSize();
332 rect
.height
= size
.y
;
334 // first filll the background
335 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
336 dc
.SetPen( wxPen(GetForegroundColour(), 1, wxSOLID
) );
337 dc
.DrawRectangle(rect
);
339 // and then draw the text line by line
340 dc
.SetTextBackground(GetBackgroundColour());
341 dc
.SetTextForeground(GetForegroundColour());
342 dc
.SetFont(GetFont());
345 pt
.x
= TEXT_MARGIN_X
;
346 pt
.y
= TEXT_MARGIN_Y
;
347 size_t count
= m_parent
->m_textLines
.GetCount();
348 for ( size_t n
= 0; n
< count
; n
++ )
350 dc
.DrawText(m_parent
->m_textLines
[n
], pt
);
352 pt
.y
+= m_parent
->m_heightLine
;
356 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
361 void wxTipWindowView::OnMouseMove(wxMouseEvent
& event
)
363 const wxRect
& rectBound
= m_parent
->m_rectBound
;
365 if ( rectBound
.width
&&
366 !rectBound
.Inside(ClientToScreen(event
.GetPosition())) )
368 // mouse left the bounding rect, disappear
378 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
380 // Workaround the kill focus event happening just after creation in wxGTK
381 if (wxGetLocalTime() > m_creationTime
+ 1)
384 #endif // !wxUSE_POPUPWIN
386 #endif // wxUSE_TIPWINDOW