]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/tipwin.cpp
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 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
58 BEGIN_EVENT_TABLE(wxTipWindow
, wxPopupTransientWindow
)
60 BEGIN_EVENT_TABLE(wxTipWindow
, wxFrame
)
62 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick
)
63 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick
)
64 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick
)
66 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus
)
67 EVT_ACTIVATE(wxTipWindow::OnActivate
)
72 // Viewer window to put in the frame
73 class wxTipWindowView
: public wxWindow
76 wxTipWindowView(wxWindow
*parent
);
79 void OnPaint(wxPaintEvent
& event
);
80 void OnMouseClick(wxMouseEvent
& event
);
82 void OnKillFocus(wxFocusEvent
& event
);
84 // calculate the client rect we need to display the text
85 void Adjust(const wxString
& text
, wxCoord maxLength
);
89 wxTipWindow
* m_parent
;
94 BEGIN_EVENT_TABLE(wxTipWindowView
, wxWindow
)
95 EVT_PAINT(wxTipWindowView::OnPaint
)
96 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick
)
97 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick
)
98 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick
)
100 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus
)
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 wxTipWindow::wxTipWindow(wxWindow
*parent
,
109 const wxString
& text
,
110 wxCoord maxLength
, wxTipWindow
** windowPtr
)
112 : wxPopupTransientWindow(parent
)
114 : wxFrame(parent
, -1, _T(""),
115 wxDefaultPosition
, wxDefaultSize
,
116 wxNO_BORDER
| wxFRAME_NO_TASKBAR
)
119 m_windowPtr
= windowPtr
;
122 SetForegroundColour(*wxBLACK
);
125 wxColour
bkCol(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_INFOBK
));
127 wxColour
bkCol(wxColour(255, 255, 225));
129 SetBackgroundColour(bkCol
);
131 // set size, position and show it
132 wxTipWindowView
* tipWindowView
= new wxTipWindowView(this);
133 tipWindowView
->Adjust(text
, maxLength
);
134 tipWindowView
->SetFocus();
136 wxGetMousePosition(&x
, &y
);
138 Position(wxPoint(x
, y
+10), wxSize(0,0));
139 Popup(tipWindowView
);
146 wxTipWindow::~wxTipWindow()
154 void wxTipWindow::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
160 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
162 if (!event
.GetActive())
166 void wxTipWindow::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
168 // Under Windows at least, we will get this immediately
169 // because when the view window is focussed, the
170 // tip window goes out of focus.
178 void wxTipWindow::Close()
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
193 : wxWindow(parent
, -1,
194 wxDefaultPosition
, wxDefaultSize
,
198 SetForegroundColour(*wxBLACK
);
200 wxColour
bkCol(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_INFOBK
));
202 wxColour
bkCol(wxColour(255, 255, 225));
204 SetBackgroundColour(bkCol
);
205 m_creationTime
= wxGetLocalTime();
206 m_parent
= (wxTipWindow
*)parent
;
209 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
212 dc
.SetFont(GetFont());
214 // calculate the length: we want each line be no longer than maxLength
215 // pixels and we only break lines at words boundary
217 wxCoord height
, width
,
219 m_parent
->m_heightLine
= 0;
221 bool breakLine
= FALSE
;
222 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
224 if ( *p
== _T('\n') || *p
== _T('\0') )
226 dc
.GetTextExtent(current
, &width
, &height
);
227 if ( width
> widthMax
)
230 if ( height
> m_parent
->m_heightLine
)
231 m_parent
->m_heightLine
= height
;
233 m_parent
->m_textLines
.Add(current
);
244 else if ( breakLine
&& (*p
== _T(' ') || *p
== _T('\t')) )
246 // word boundary - break the line here
247 m_parent
->m_textLines
.Add(current
);
254 dc
.GetTextExtent(current
, &width
, &height
);
255 if ( width
> maxLength
)
258 if ( width
> widthMax
)
261 if ( height
> m_parent
->m_heightLine
)
262 m_parent
->m_heightLine
= height
;
266 // take into account the border size and the margins
267 width
= 2*(TEXT_MARGIN_X
+ 1) + widthMax
;
268 height
= 2*(TEXT_MARGIN_Y
+ 1) + m_parent
->m_textLines
.GetCount()*m_parent
->m_heightLine
;
269 m_parent
->SetClientSize(width
, height
);
270 SetSize(0, 0, width
, height
);
273 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
278 wxSize size
= GetClientSize();
280 rect
.height
= size
.y
;
282 // first filll the background
283 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
284 dc
.SetPen( wxPen(GetForegroundColour(), 1, wxSOLID
) );
285 dc
.DrawRectangle(rect
);
287 // and then draw the text line by line
288 dc
.SetTextBackground(GetBackgroundColour());
289 dc
.SetTextForeground(GetForegroundColour());
290 dc
.SetFont(GetFont());
293 pt
.x
= TEXT_MARGIN_X
;
294 pt
.y
= TEXT_MARGIN_Y
;
295 size_t count
= m_parent
->m_textLines
.GetCount();
296 for ( size_t n
= 0; n
< count
; n
++ )
298 dc
.DrawText(m_parent
->m_textLines
[n
], pt
);
300 pt
.y
+= m_parent
->m_heightLine
;
304 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
310 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
312 // Workaround the kill focus event happening just after creation in wxGTK
313 if (wxGetLocalTime() > m_creationTime
+ 1)