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"
37 #include "wx/settings.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 static const wxCoord TEXT_MARGIN_X = 3;
44 static const wxCoord TEXT_MARGIN_Y = 3;
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 BEGIN_EVENT_TABLE(wxTipWindow, wxFrame)
55 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick)
56 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick)
57 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick)
58 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus)
59 EVT_ACTIVATE(wxTipWindow::OnActivate)
62 // Viewer window to put in the frame
63 class wxTipWindowView: public wxWindow
66 wxTipWindowView(wxWindow *parent);
69 void OnPaint(wxPaintEvent& event);
70 void OnMouseClick(wxMouseEvent& event);
71 void OnKillFocus(wxFocusEvent& event);
73 // calculate the client rect we need to display the text
74 void Adjust(const wxString& text, wxCoord maxLength);
81 BEGIN_EVENT_TABLE(wxTipWindowView, wxWindow)
82 EVT_PAINT(wxTipWindowView::OnPaint)
83 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick)
84 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick)
85 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick)
86 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus)
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 wxTipWindow::wxTipWindow(wxWindow *parent,
96 wxCoord maxLength, wxTipWindow** windowPtr)
97 : wxFrame(parent, -1, _T(""),
98 wxDefaultPosition, wxDefaultSize,
99 wxNO_BORDER | wxFRAME_NO_TASKBAR )
102 SetForegroundColour(*wxBLACK);
105 wxColour bkCol(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_INFOBK));
107 wxColour bkCol(wxColour(255, 255, 225));
109 SetBackgroundColour(bkCol);
111 // set position and size
113 wxGetMousePosition(&x, &y);
116 wxTipWindowView* tipWindowView = new wxTipWindowView(this);
117 tipWindowView->Adjust(text, maxLength);
119 m_windowPtr = windowPtr;
122 tipWindowView->SetFocus();
125 wxTipWindow::~wxTipWindow()
133 void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event))
138 void wxTipWindow::OnActivate(wxActivateEvent& event)
140 if (!event.GetActive())
144 void wxTipWindow::OnKillFocus(wxFocusEvent& WXUNUSED(event))
146 // Under Windows at least, we will get this immediately
147 // because when the view window is focussed, the
148 // tip window goes out of focus.
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 wxTipWindowView::wxTipWindowView(wxWindow *parent)
159 : wxWindow(parent, -1,
160 wxDefaultPosition, wxDefaultSize,
164 SetForegroundColour(*wxBLACK);
166 wxColour bkCol(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_INFOBK));
168 wxColour bkCol(wxColour(255, 255, 225));
170 SetBackgroundColour(bkCol);
171 m_creationTime = wxGetLocalTime();
174 void wxTipWindowView::Adjust(const wxString& text, wxCoord maxLength)
176 wxTipWindow* parent = (wxTipWindow*) GetParent();
178 dc.SetFont(GetFont());
180 // calculate the length: we want each line be no longer than maxLength
181 // pixels and we only break lines at words boundary
183 wxCoord height, width,
185 parent->m_heightLine = 0;
187 bool breakLine = FALSE;
188 for ( const wxChar *p = text.c_str(); ; p++ )
190 if ( *p == _T('\n') || *p == _T('\0') )
192 dc.GetTextExtent(current, &width, &height);
193 if ( width > widthMax )
196 if ( height > parent->m_heightLine )
197 parent->m_heightLine = height;
199 parent->m_textLines.Add(current);
210 else if ( breakLine && (*p == _T(' ') || *p == _T('\t')) )
212 // word boundary - break the line here
213 parent->m_textLines.Add(current);
220 dc.GetTextExtent(current, &width, &height);
221 if ( width > maxLength )
224 if ( width > widthMax )
227 if ( height > parent->m_heightLine )
228 parent->m_heightLine = height;
232 // take into account the border size and the margins
233 GetParent()->SetClientSize(2*(TEXT_MARGIN_X + 1) + widthMax,
234 2*(TEXT_MARGIN_Y + 1) + parent->m_textLines.GetCount()*parent->m_heightLine);
237 void wxTipWindowView::OnPaint(wxPaintEvent& WXUNUSED(event))
239 wxTipWindow* parent = (wxTipWindow*) GetParent();
246 wxSize size = GetClientSize();
248 rect.height = size.y;
250 // first filll the background
251 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
253 dc.SetPen( * wxBLACK_PEN );
254 dc.DrawRectangle(rect);
256 // and then draw the text line by line
257 dc.SetFont(GetFont());
260 pt.x = TEXT_MARGIN_X;
261 pt.y = TEXT_MARGIN_Y;
262 size_t count = parent->m_textLines.GetCount();
263 for ( size_t n = 0; n < count; n++ )
265 dc.DrawText(parent->m_textLines[n], pt);
267 pt.y += parent->m_heightLine;
271 void wxTipWindowView::OnMouseClick(wxMouseEvent& WXUNUSED(event))
273 GetParent()->Close();
276 void wxTipWindowView::OnKillFocus(wxFocusEvent& WXUNUSED(event))
278 // Workaround the kill focus event happening just after creation in wxGTK
279 if (wxGetLocalTime() > m_creationTime + 1)
280 GetParent()->Close();