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
81 // ============================================================================
83 // ============================================================================
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 BEGIN_EVENT_TABLE(wxTipWindow
, wxTipWindowBase
)
90 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick
)
91 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick
)
92 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick
)
95 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus
)
96 EVT_ACTIVATE(wxTipWindow::OnActivate
)
97 #endif // !wxUSE_POPUPWIN
100 BEGIN_EVENT_TABLE(wxTipWindowView
, wxWindow
)
101 EVT_PAINT(wxTipWindowView::OnPaint
)
103 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick
)
104 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick
)
105 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick
)
107 EVT_MOTION(wxTipWindowView::OnMouseMove
)
110 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus
)
111 #endif // !wxUSE_POPUPWIN
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 wxTipWindow::wxTipWindow(wxWindow
*parent
,
119 const wxString
& text
,
121 wxTipWindow
** windowPtr
,
124 : wxPopupTransientWindow(parent
)
126 : wxFrame(parent
, -1, _T(""),
127 wxDefaultPosition
, wxDefaultSize
,
128 wxNO_BORDER
| wxFRAME_NO_TASKBAR
)
131 SetTipWindowPtr(windowPtr
);
134 SetBoundingRect(*rectBounds
);
138 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
139 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
141 // set size, position and show it
142 m_view
= new wxTipWindowView(this);
143 m_view
->Adjust(text
, maxLength
);
147 wxGetMousePosition(&x
, &y
);
149 // we want to show the tip below the mouse, not over it
151 // NB: the reason we use "/ 2" here is that we don't know where the current
152 // cursors hot spot is... it would be nice if we could find this out
154 y
+= wxSystemSettings::GetMetric(wxSYS_CURSOR_Y
) / 2;
157 Position(wxPoint(x
, y
), wxSize(0, 0));
165 wxTipWindow::~wxTipWindow()
173 void wxTipWindow::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
180 void wxTipWindow::OnDismiss()
185 #else // !wxUSE_POPUPWIN
187 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
189 if (!event
.GetActive())
193 void wxTipWindow::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
195 // Under Windows at least, we will get this immediately
196 // because when the view window is focussed, the
197 // tip window goes out of focus.
203 #endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
205 void wxTipWindow::SetBoundingRect(const wxRect
& rectBound
)
207 m_rectBound
= rectBound
;
210 void wxTipWindow::Close()
226 // ----------------------------------------------------------------------------
228 // ----------------------------------------------------------------------------
230 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
231 : wxWindow(parent
, -1,
232 wxDefaultPosition
, wxDefaultSize
,
236 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
237 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
240 m_creationTime
= wxGetLocalTime();
241 #endif // !wxUSE_POPUPWIN
243 m_parent
= (wxTipWindow
*)parent
;
246 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
249 dc
.SetFont(GetFont());
251 // calculate the length: we want each line be no longer than maxLength
252 // pixels and we only break lines at words boundary
254 wxCoord height
, width
,
256 m_parent
->m_heightLine
= 0;
258 bool breakLine
= FALSE
;
259 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
261 if ( *p
== _T('\n') || *p
== _T('\0') )
263 dc
.GetTextExtent(current
, &width
, &height
);
264 if ( width
> widthMax
)
267 if ( height
> m_parent
->m_heightLine
)
268 m_parent
->m_heightLine
= height
;
270 m_parent
->m_textLines
.Add(current
);
281 else if ( breakLine
&& (*p
== _T(' ') || *p
== _T('\t')) )
283 // word boundary - break the line here
284 m_parent
->m_textLines
.Add(current
);
291 dc
.GetTextExtent(current
, &width
, &height
);
292 if ( width
> maxLength
)
295 if ( width
> widthMax
)
298 if ( height
> m_parent
->m_heightLine
)
299 m_parent
->m_heightLine
= height
;
303 // take into account the border size and the margins
304 width
= 2*(TEXT_MARGIN_X
+ 1) + widthMax
;
305 height
= 2*(TEXT_MARGIN_Y
+ 1) + m_parent
->m_textLines
.GetCount()*m_parent
->m_heightLine
;
306 m_parent
->SetClientSize(width
, height
);
307 SetSize(0, 0, width
, height
);
310 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
315 wxSize size
= GetClientSize();
317 rect
.height
= size
.y
;
319 // first filll the background
320 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
321 dc
.SetPen( wxPen(GetForegroundColour(), 1, wxSOLID
) );
322 dc
.DrawRectangle(rect
);
324 // and then draw the text line by line
325 dc
.SetTextBackground(GetBackgroundColour());
326 dc
.SetTextForeground(GetForegroundColour());
327 dc
.SetFont(GetFont());
330 pt
.x
= TEXT_MARGIN_X
;
331 pt
.y
= TEXT_MARGIN_Y
;
332 size_t count
= m_parent
->m_textLines
.GetCount();
333 for ( size_t n
= 0; n
< count
; n
++ )
335 dc
.DrawText(m_parent
->m_textLines
[n
], pt
);
337 pt
.y
+= m_parent
->m_heightLine
;
341 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
346 void wxTipWindowView::OnMouseMove(wxMouseEvent
& event
)
348 const wxRect
& rectBound
= m_parent
->m_rectBound
;
350 if ( rectBound
.width
&&
351 !rectBound
.Inside(ClientToScreen(event
.GetPosition())) )
353 // mouse left the bounding rect, disappear
363 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
365 // Workaround the kill focus event happening just after creation in wxGTK
366 if (wxGetLocalTime() > m_creationTime
+ 1)
369 #endif // !wxUSE_POPUPWIN
371 #endif // wxUSE_TIPWINDOW