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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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"
35 #include "wx/tipwin.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
, -1, _T(""),
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));
166 wxTipWindow::~wxTipWindow()
174 void wxTipWindow::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
181 void wxTipWindow::OnDismiss()
186 #else // !wxUSE_POPUPWIN
188 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
190 if (!event
.GetActive())
194 void wxTipWindow::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
196 // Under Windows at least, we will get this immediately
197 // because when the view window is focussed, the
198 // tip window goes out of focus.
204 #endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
206 void wxTipWindow::SetBoundingRect(const wxRect
& rectBound
)
208 m_rectBound
= rectBound
;
211 void wxTipWindow::Close()
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
232 : wxWindow(parent
, -1,
233 wxDefaultPosition
, wxDefaultSize
,
237 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
238 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
241 m_creationTime
= wxGetLocalTime();
242 #endif // !wxUSE_POPUPWIN
244 m_parent
= (wxTipWindow
*)parent
;
247 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
250 dc
.SetFont(GetFont());
252 // calculate the length: we want each line be no longer than maxLength
253 // pixels and we only break lines at words boundary
255 wxCoord height
, width
,
257 m_parent
->m_heightLine
= 0;
259 bool breakLine
= FALSE
;
260 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
262 if ( *p
== _T('\n') || *p
== _T('\0') )
264 dc
.GetTextExtent(current
, &width
, &height
);
265 if ( width
> widthMax
)
268 if ( height
> m_parent
->m_heightLine
)
269 m_parent
->m_heightLine
= height
;
271 m_parent
->m_textLines
.Add(current
);
282 else if ( breakLine
&& (*p
== _T(' ') || *p
== _T('\t')) )
284 // word boundary - break the line here
285 m_parent
->m_textLines
.Add(current
);
292 dc
.GetTextExtent(current
, &width
, &height
);
293 if ( width
> maxLength
)
296 if ( width
> widthMax
)
299 if ( height
> m_parent
->m_heightLine
)
300 m_parent
->m_heightLine
= height
;
304 // take into account the border size and the margins
305 width
= 2*(TEXT_MARGIN_X
+ 1) + widthMax
;
306 height
= 2*(TEXT_MARGIN_Y
+ 1) + m_parent
->m_textLines
.GetCount()*m_parent
->m_heightLine
;
307 m_parent
->SetClientSize(width
, height
);
308 SetSize(0, 0, width
, height
);
311 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
316 wxSize size
= GetClientSize();
318 rect
.height
= size
.y
;
320 // first filll the background
321 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
322 dc
.SetPen( wxPen(GetForegroundColour(), 1, wxSOLID
) );
323 dc
.DrawRectangle(rect
);
325 // and then draw the text line by line
326 dc
.SetTextBackground(GetBackgroundColour());
327 dc
.SetTextForeground(GetForegroundColour());
328 dc
.SetFont(GetFont());
331 pt
.x
= TEXT_MARGIN_X
;
332 pt
.y
= TEXT_MARGIN_Y
;
333 size_t count
= m_parent
->m_textLines
.GetCount();
334 for ( size_t n
= 0; n
< count
; n
++ )
336 dc
.DrawText(m_parent
->m_textLines
[n
], pt
);
338 pt
.y
+= m_parent
->m_heightLine
;
342 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
347 void wxTipWindowView::OnMouseMove(wxMouseEvent
& event
)
349 const wxRect
& rectBound
= m_parent
->m_rectBound
;
351 if ( rectBound
.width
&&
352 !rectBound
.Inside(ClientToScreen(event
.GetPosition())) )
354 // mouse left the bounding rect, disappear
364 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
366 // Workaround the kill focus event happening just after creation in wxGTK
367 if (wxGetLocalTime() > m_creationTime
+ 1)
370 #endif // !wxUSE_POPUPWIN
372 #endif // wxUSE_TIPWINDOW