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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "tipwin.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dcclient.h"
37 #include "wx/tipwin.h"
42 #include "wx/settings.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 static const wxCoord TEXT_MARGIN_X
= 3;
49 static const wxCoord TEXT_MARGIN_Y
= 3;
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // Viewer window to put in the frame
56 class WXDLLEXPORT wxTipWindowView
: public wxWindow
59 wxTipWindowView(wxWindow
*parent
);
62 void OnPaint(wxPaintEvent
& event
);
63 void OnMouseClick(wxMouseEvent
& event
);
64 void OnMouseMove(wxMouseEvent
& event
);
67 void OnKillFocus(wxFocusEvent
& event
);
68 #endif // wxUSE_POPUPWIN
70 // calculate the client rect we need to display the text
71 void Adjust(const wxString
& text
, wxCoord maxLength
);
74 wxTipWindow
* m_parent
;
78 #endif // !wxUSE_POPUPWIN
81 DECLARE_NO_COPY_CLASS(wxTipWindowView
)
84 // ============================================================================
86 // ============================================================================
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 BEGIN_EVENT_TABLE(wxTipWindow
, wxTipWindowBase
)
93 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick
)
94 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick
)
95 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick
)
98 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus
)
99 EVT_ACTIVATE(wxTipWindow::OnActivate
)
100 #endif // !wxUSE_POPUPWIN
103 BEGIN_EVENT_TABLE(wxTipWindowView
, wxWindow
)
104 EVT_PAINT(wxTipWindowView::OnPaint
)
106 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick
)
107 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick
)
108 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick
)
110 EVT_MOTION(wxTipWindowView::OnMouseMove
)
113 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus
)
114 #endif // !wxUSE_POPUPWIN
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 wxTipWindow::wxTipWindow(wxWindow
*parent
,
122 const wxString
& text
,
124 wxTipWindow
** windowPtr
,
127 : wxPopupTransientWindow(parent
)
129 : wxFrame(parent
, wxID_ANY
, wxEmptyString
,
130 wxDefaultPosition
, wxDefaultSize
,
131 wxNO_BORDER
| wxFRAME_NO_TASKBAR
)
134 SetTipWindowPtr(windowPtr
);
137 SetBoundingRect(*rectBounds
);
141 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
142 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
144 // set size, position and show it
145 m_view
= new wxTipWindowView(this);
146 m_view
->Adjust(text
, maxLength
);
150 wxGetMousePosition(&x
, &y
);
152 // we want to show the tip below the mouse, not over it
154 // NB: the reason we use "/ 2" here is that we don't know where the current
155 // cursors hot spot is... it would be nice if we could find this out
157 y
+= wxSystemSettings::GetMetric(wxSYS_CURSOR_Y
) / 2;
160 Position(wxPoint(x
, y
), wxSize(0, 0));
163 if (!GTK_WIDGET_HAS_GRAB(m_widget
))
164 gtk_grab_add( m_widget
);
172 wxTipWindow::~wxTipWindow()
178 #ifdef wxUSE_POPUPWIN
180 if (GTK_WIDGET_HAS_GRAB(m_widget
))
181 gtk_grab_remove( m_widget
);
186 void wxTipWindow::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
193 void wxTipWindow::OnDismiss()
198 #else // !wxUSE_POPUPWIN
200 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
202 if (!event
.GetActive())
206 void wxTipWindow::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
208 // Under Windows at least, we will get this immediately
209 // because when the view window is focussed, the
210 // tip window goes out of focus.
216 #endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
218 void wxTipWindow::SetBoundingRect(const wxRect
& rectBound
)
220 m_rectBound
= rectBound
;
223 void wxTipWindow::Close()
234 if (GTK_WIDGET_HAS_GRAB(m_widget
))
235 gtk_grab_remove( m_widget
);
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
248 : wxWindow(parent
, wxID_ANY
,
249 wxDefaultPosition
, wxDefaultSize
,
253 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
254 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
257 m_creationTime
= wxGetLocalTime();
258 #endif // !wxUSE_POPUPWIN
260 m_parent
= (wxTipWindow
*)parent
;
263 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
266 dc
.SetFont(GetFont());
268 // calculate the length: we want each line be no longer than maxLength
269 // pixels and we only break lines at words boundary
271 wxCoord height
, width
,
273 m_parent
->m_heightLine
= 0;
275 bool breakLine
= false;
276 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
278 if ( *p
== _T('\n') || *p
== _T('\0') )
280 dc
.GetTextExtent(current
, &width
, &height
);
281 if ( width
> widthMax
)
284 if ( height
> m_parent
->m_heightLine
)
285 m_parent
->m_heightLine
= height
;
287 m_parent
->m_textLines
.Add(current
);
298 else if ( breakLine
&& (*p
== _T(' ') || *p
== _T('\t')) )
300 // word boundary - break the line here
301 m_parent
->m_textLines
.Add(current
);
308 dc
.GetTextExtent(current
, &width
, &height
);
309 if ( width
> maxLength
)
312 if ( width
> widthMax
)
315 if ( height
> m_parent
->m_heightLine
)
316 m_parent
->m_heightLine
= height
;
320 // take into account the border size and the margins
321 width
= 2*(TEXT_MARGIN_X
+ 1) + widthMax
;
322 height
= 2*(TEXT_MARGIN_Y
+ 1) + m_parent
->m_textLines
.GetCount()*m_parent
->m_heightLine
;
323 m_parent
->SetClientSize(width
, height
);
324 SetSize(0, 0, width
, height
);
327 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
332 wxSize size
= GetClientSize();
334 rect
.height
= size
.y
;
336 // first filll the background
337 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
338 dc
.SetPen( wxPen(GetForegroundColour(), 1, wxSOLID
) );
339 dc
.DrawRectangle(rect
);
341 // and then draw the text line by line
342 dc
.SetTextBackground(GetBackgroundColour());
343 dc
.SetTextForeground(GetForegroundColour());
344 dc
.SetFont(GetFont());
347 pt
.x
= TEXT_MARGIN_X
;
348 pt
.y
= TEXT_MARGIN_Y
;
349 size_t count
= m_parent
->m_textLines
.GetCount();
350 for ( size_t n
= 0; n
< count
; n
++ )
352 dc
.DrawText(m_parent
->m_textLines
[n
], pt
);
354 pt
.y
+= m_parent
->m_heightLine
;
358 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
363 void wxTipWindowView::OnMouseMove(wxMouseEvent
& event
)
365 const wxRect
& rectBound
= m_parent
->m_rectBound
;
367 if ( rectBound
.width
&&
368 !rectBound
.Inside(ClientToScreen(event
.GetPosition())) )
370 // mouse left the bounding rect, disappear
380 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
382 // Workaround the kill focus event happening just after creation in wxGTK
383 if (wxGetLocalTime() > m_creationTime
+ 1)
386 #endif // !wxUSE_POPUPWIN
388 #endif // wxUSE_TIPWINDOW