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"
28 #include "wx/dcclient.h"
33 #include "wx/tipwin.h"
38 #include "wx/settings.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 static const wxCoord TEXT_MARGIN_X
= 3;
45 static const wxCoord TEXT_MARGIN_Y
= 3;
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // Viewer window to put in the frame
52 class WXDLLEXPORT wxTipWindowView
: public wxWindow
55 wxTipWindowView(wxWindow
*parent
);
58 void OnPaint(wxPaintEvent
& event
);
59 void OnMouseClick(wxMouseEvent
& event
);
60 void OnMouseMove(wxMouseEvent
& event
);
63 void OnKillFocus(wxFocusEvent
& event
);
64 #endif // wxUSE_POPUPWIN
66 // calculate the client rect we need to display the text
67 void Adjust(const wxString
& text
, wxCoord maxLength
);
70 wxTipWindow
* m_parent
;
74 #endif // !wxUSE_POPUPWIN
77 DECLARE_NO_COPY_CLASS(wxTipWindowView
)
80 // ============================================================================
82 // ============================================================================
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 BEGIN_EVENT_TABLE(wxTipWindow
, wxTipWindowBase
)
89 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick
)
90 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick
)
91 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick
)
94 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus
)
95 EVT_ACTIVATE(wxTipWindow::OnActivate
)
96 #endif // !wxUSE_POPUPWIN
99 BEGIN_EVENT_TABLE(wxTipWindowView
, wxWindow
)
100 EVT_PAINT(wxTipWindowView::OnPaint
)
102 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick
)
103 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick
)
104 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick
)
106 EVT_MOTION(wxTipWindowView::OnMouseMove
)
109 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus
)
110 #endif // !wxUSE_POPUPWIN
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 wxTipWindow::wxTipWindow(wxWindow
*parent
,
118 const wxString
& text
,
120 wxTipWindow
** windowPtr
,
123 : wxPopupTransientWindow(parent
)
125 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
126 wxDefaultPosition
, wxDefaultSize
,
127 wxNO_BORDER
| wxFRAME_NO_TASKBAR
)
130 SetTipWindowPtr(windowPtr
);
133 SetBoundingRect(*rectBounds
);
137 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
138 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
140 // set size, position and show it
141 m_view
= new wxTipWindowView(this);
142 m_view
->Adjust(text
, maxLength
);
146 wxGetMousePosition(&x
, &y
);
148 // we want to show the tip below the mouse, not over it
150 // NB: the reason we use "/ 2" here is that we don't know where the current
151 // cursors hot spot is... it would be nice if we could find this out
153 y
+= wxSystemSettings::GetMetric(wxSYS_CURSOR_Y
) / 2;
156 Position(wxPoint(x
, y
), wxSize(0,0));
159 if (!GTK_WIDGET_HAS_GRAB(m_widget
))
160 gtk_grab_add( m_widget
);
168 wxTipWindow::~wxTipWindow()
174 #ifdef wxUSE_POPUPWIN
176 if (GTK_WIDGET_HAS_GRAB(m_widget
))
177 gtk_grab_remove( m_widget
);
182 void wxTipWindow::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
189 void wxTipWindow::OnDismiss()
194 #else // !wxUSE_POPUPWIN
196 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
198 if (!event
.GetActive())
202 void wxTipWindow::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
204 // Under Windows at least, we will get this immediately
205 // because when the view window is focussed, the
206 // tip window goes out of focus.
212 #endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
214 void wxTipWindow::SetBoundingRect(const wxRect
& rectBound
)
216 m_rectBound
= rectBound
;
219 void wxTipWindow::Close()
230 if (GTK_WIDGET_HAS_GRAB(m_widget
))
231 gtk_grab_remove( m_widget
);
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
== _T('\n') || *p
== _T('\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
== _T(' ') || *p
== _T('\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(), wxSOLID
));
334 dc
.SetPen( wxPen(GetForegroundColour(), 1, wxSOLID
) );
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
.Inside(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