]> git.saurik.com Git - wxWidgets.git/blame - src/generic/tipwin.cpp
Improved size handling.
[wxWidgets.git] / src / generic / tipwin.cpp
CommitLineData
01fa3fe7
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/tipwin.cpp
3// Purpose: implementation of wxTipWindow
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 10.09.00
7// RCS-ID: $Id$
8// Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
01fa3fe7
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
01fa3fe7
VZ
20// For compilers that support precompilatixon, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
28 #include "wx/dcclient.h"
29#endif // WX_PRECOMP
654070ca
VZ
30#ifdef __WXGTK__
31 #include <gtk/gtk.h>
32#endif
01fa3fe7 33#include "wx/tipwin.h"
f38bcae5
VZ
34
35#if wxUSE_TIPWINDOW
36
173e8bbf 37#include "wx/timer.h"
8cb172b4 38#include "wx/settings.h"
01fa3fe7
VZ
39
40// ----------------------------------------------------------------------------
41// constants
42// ----------------------------------------------------------------------------
43
44static const wxCoord TEXT_MARGIN_X = 3;
45static const wxCoord TEXT_MARGIN_Y = 3;
46
01fa3fe7 47// ----------------------------------------------------------------------------
dafbe8c0 48// wxTipWindowView
01fa3fe7
VZ
49// ----------------------------------------------------------------------------
50
961c54c3 51// Viewer window to put in the frame
dafbe8c0 52class WXDLLEXPORT wxTipWindowView : public wxWindow
961c54c3
RD
53{
54public:
55 wxTipWindowView(wxWindow *parent);
56
57 // event handlers
58 void OnPaint(wxPaintEvent& event);
59 void OnMouseClick(wxMouseEvent& event);
dafbe8c0
VZ
60 void OnMouseMove(wxMouseEvent& event);
61
961c54c3
RD
62#if !wxUSE_POPUPWIN
63 void OnKillFocus(wxFocusEvent& event);
dafbe8c0
VZ
64#endif // wxUSE_POPUPWIN
65
961c54c3
RD
66 // calculate the client rect we need to display the text
67 void Adjust(const wxString& text, wxCoord maxLength);
68
69private:
961c54c3
RD
70 wxTipWindow* m_parent;
71
dafbe8c0
VZ
72#if !wxUSE_POPUPWIN
73 long m_creationTime;
74#endif // !wxUSE_POPUPWIN
75
76 DECLARE_EVENT_TABLE()
22f3361e 77 DECLARE_NO_COPY_CLASS(wxTipWindowView)
961c54c3
RD
78};
79
dafbe8c0
VZ
80// ============================================================================
81// implementation
82// ============================================================================
83
84// ----------------------------------------------------------------------------
85// event tables
86// ----------------------------------------------------------------------------
87
88BEGIN_EVENT_TABLE(wxTipWindow, wxTipWindowBase)
89 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick)
90 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick)
91 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick)
92
93#if !wxUSE_POPUPWIN
94 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus)
95 EVT_ACTIVATE(wxTipWindow::OnActivate)
96#endif // !wxUSE_POPUPWIN
97END_EVENT_TABLE()
98
961c54c3
RD
99BEGIN_EVENT_TABLE(wxTipWindowView, wxWindow)
100 EVT_PAINT(wxTipWindowView::OnPaint)
dafbe8c0 101
961c54c3
RD
102 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick)
103 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick)
104 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick)
dafbe8c0
VZ
105
106 EVT_MOTION(wxTipWindowView::OnMouseMove)
107
961c54c3
RD
108#if !wxUSE_POPUPWIN
109 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus)
dafbe8c0 110#endif // !wxUSE_POPUPWIN
961c54c3
RD
111END_EVENT_TABLE()
112
01fa3fe7
VZ
113// ----------------------------------------------------------------------------
114// wxTipWindow
115// ----------------------------------------------------------------------------
116
117wxTipWindow::wxTipWindow(wxWindow *parent,
118 const wxString& text,
dafbe8c0
VZ
119 wxCoord maxLength,
120 wxTipWindow** windowPtr,
121 wxRect *rectBounds)
961c54c3 122#if wxUSE_POPUPWIN
8962e1d9 123 : wxPopupTransientWindow(parent)
961c54c3 124#else
ca65c044 125 : wxFrame(parent, wxID_ANY, wxEmptyString,
961c54c3
RD
126 wxDefaultPosition, wxDefaultSize,
127 wxNO_BORDER | wxFRAME_NO_TASKBAR )
128#endif
01fa3fe7 129{
dafbe8c0
VZ
130 SetTipWindowPtr(windowPtr);
131 if ( rectBounds )
132 {
133 SetBoundingRect(*rectBounds);
134 }
8962e1d9 135
01fa3fe7 136 // set colours
dafbe8c0
VZ
137 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT));
138 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));
04ef50df 139
961c54c3 140 // set size, position and show it
dafbe8c0
VZ
141 m_view = new wxTipWindowView(this);
142 m_view->Adjust(text, maxLength);
143 m_view->SetFocus();
144
01fa3fe7
VZ
145 int x, y;
146 wxGetMousePosition(&x, &y);
dafbe8c0
VZ
147
148 // we want to show the tip below the mouse, not over it
149 //
150 // NB: the reason we use "/ 2" here is that we don't know where the current
151 // cursors hot spot is... it would be nice if we could find this out
152 // though
153 y += wxSystemSettings::GetMetric(wxSYS_CURSOR_Y) / 2;
154
961c54c3 155#if wxUSE_POPUPWIN
c47addef 156 Position(wxPoint(x, y), wxSize(0,0));
dafbe8c0 157 Popup(m_view);
654070ca
VZ
158 #ifdef __WXGTK__
159 if (!GTK_WIDGET_HAS_GRAB(m_widget))
160 gtk_grab_add( m_widget );
ca65c044 161 #endif
961c54c3 162#else
dafbe8c0 163 Move(x, y);
ca65c044 164 Show(true);
961c54c3 165#endif
173e8bbf
JS
166}
167
168wxTipWindow::~wxTipWindow()
169{
dafbe8c0 170 if ( m_windowPtr )
173e8bbf
JS
171 {
172 *m_windowPtr = NULL;
173 }
654070ca
VZ
174 #ifdef wxUSE_POPUPWIN
175 #ifdef __WXGTK__
176 if (GTK_WIDGET_HAS_GRAB(m_widget))
177 gtk_grab_remove( m_widget );
178 #endif
179 #endif
173e8bbf
JS
180}
181
33ac7e6f 182void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event))
173e8bbf
JS
183{
184 Close();
185}
186
dafbe8c0
VZ
187#if wxUSE_POPUPWIN
188
189void wxTipWindow::OnDismiss()
190{
191 Close();
192}
193
194#else // !wxUSE_POPUPWIN
195
961c54c3 196void wxTipWindow::OnActivate(wxActivateEvent& event)
173e8bbf 197{
961c54c3
RD
198 if (!event.GetActive())
199 Close();
200}
8962e1d9 201
961c54c3
RD
202void wxTipWindow::OnKillFocus(wxFocusEvent& WXUNUSED(event))
203{
204 // Under Windows at least, we will get this immediately
205 // because when the view window is focussed, the
206 // tip window goes out of focus.
207#ifdef __WXGTK__
208 Close();
209#endif
210}
8962e1d9 211
dafbe8c0
VZ
212#endif // wxUSE_POPUPWIN // !wxUSE_POPUPWIN
213
214void wxTipWindow::SetBoundingRect(const wxRect& rectBound)
215{
216 m_rectBound = rectBound;
217}
8962e1d9 218
961c54c3
RD
219void wxTipWindow::Close()
220{
dafbe8c0
VZ
221 if ( m_windowPtr )
222 {
223 *m_windowPtr = NULL;
224 m_windowPtr = NULL;
225 }
226
961c54c3 227#if wxUSE_POPUPWIN
ca65c044 228 Show(false);
654070ca
VZ
229 #ifdef __WXGTK__
230 if (GTK_WIDGET_HAS_GRAB(m_widget))
231 gtk_grab_remove( m_widget );
ca65c044 232 #endif
961c54c3
RD
233 Destroy();
234#else
235 wxFrame::Close();
236#endif
237}
8962e1d9 238
961c54c3
RD
239// ----------------------------------------------------------------------------
240// wxTipWindowView
241// ----------------------------------------------------------------------------
8962e1d9 242
961c54c3 243wxTipWindowView::wxTipWindowView(wxWindow *parent)
ca65c044 244 : wxWindow(parent, wxID_ANY,
dafbe8c0
VZ
245 wxDefaultPosition, wxDefaultSize,
246 wxNO_BORDER)
961c54c3
RD
247{
248 // set colours
dafbe8c0
VZ
249 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT));
250 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));
dafbe8c0
VZ
251
252#if !wxUSE_POPUPWIN
961c54c3 253 m_creationTime = wxGetLocalTime();
dafbe8c0
VZ
254#endif // !wxUSE_POPUPWIN
255
961c54c3 256 m_parent = (wxTipWindow*)parent;
173e8bbf
JS
257}
258
961c54c3 259void wxTipWindowView::Adjust(const wxString& text, wxCoord maxLength)
173e8bbf 260{
8962e1d9
RD
261 wxClientDC dc(this);
262 dc.SetFont(GetFont());
263
264 // calculate the length: we want each line be no longer than maxLength
265 // pixels and we only break lines at words boundary
266 wxString current;
267 wxCoord height, width,
268 widthMax = 0;
961c54c3 269 m_parent->m_heightLine = 0;
8962e1d9 270
ca65c044 271 bool breakLine = false;
8962e1d9
RD
272 for ( const wxChar *p = text.c_str(); ; p++ )
273 {
274 if ( *p == _T('\n') || *p == _T('\0') )
275 {
276 dc.GetTextExtent(current, &width, &height);
277 if ( width > widthMax )
278 widthMax = width;
279
961c54c3
RD
280 if ( height > m_parent->m_heightLine )
281 m_parent->m_heightLine = height;
8962e1d9 282
961c54c3 283 m_parent->m_textLines.Add(current);
8962e1d9
RD
284
285 if ( !*p )
286 {
287 // end of text
288 break;
289 }
290
291 current.clear();
ca65c044 292 breakLine = false;
8962e1d9
RD
293 }
294 else if ( breakLine && (*p == _T(' ') || *p == _T('\t')) )
295 {
296 // word boundary - break the line here
961c54c3 297 m_parent->m_textLines.Add(current);
8962e1d9 298 current.clear();
ca65c044 299 breakLine = false;
8962e1d9
RD
300 }
301 else // line goes on
302 {
303 current += *p;
304 dc.GetTextExtent(current, &width, &height);
305 if ( width > maxLength )
ca65c044 306 breakLine = true;
8962e1d9
RD
307
308 if ( width > widthMax )
309 widthMax = width;
310
961c54c3
RD
311 if ( height > m_parent->m_heightLine )
312 m_parent->m_heightLine = height;
8962e1d9
RD
313 }
314 }
315
316 // take into account the border size and the margins
961c54c3 317 width = 2*(TEXT_MARGIN_X + 1) + widthMax;
4a10ea8b 318 height = 2*(TEXT_MARGIN_Y + 1) + wx_truncate_cast(wxCoord, m_parent->m_textLines.GetCount())*m_parent->m_heightLine;
961c54c3
RD
319 m_parent->SetClientSize(width, height);
320 SetSize(0, 0, width, height);
173e8bbf
JS
321}
322
961c54c3
RD
323void wxTipWindowView::OnPaint(wxPaintEvent& WXUNUSED(event))
324{
325 wxPaintDC dc(this);
8962e1d9 326
961c54c3
RD
327 wxRect rect;
328 wxSize size = GetClientSize();
329 rect.width = size.x;
330 rect.height = size.y;
331
332 // first filll the background
333 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
334 dc.SetPen( wxPen(GetForegroundColour(), 1, wxSOLID) );
335 dc.DrawRectangle(rect);
336
337 // and then draw the text line by line
338 dc.SetTextBackground(GetBackgroundColour());
339 dc.SetTextForeground(GetForegroundColour());
340 dc.SetFont(GetFont());
341
342 wxPoint pt;
343 pt.x = TEXT_MARGIN_X;
344 pt.y = TEXT_MARGIN_Y;
345 size_t count = m_parent->m_textLines.GetCount();
346 for ( size_t n = 0; n < count; n++ )
347 {
348 dc.DrawText(m_parent->m_textLines[n], pt);
349
350 pt.y += m_parent->m_heightLine;
351 }
352}
353
354void wxTipWindowView::OnMouseClick(wxMouseEvent& WXUNUSED(event))
8962e1d9 355{
961c54c3 356 m_parent->Close();
8962e1d9
RD
357}
358
dafbe8c0
VZ
359void wxTipWindowView::OnMouseMove(wxMouseEvent& event)
360{
361 const wxRect& rectBound = m_parent->m_rectBound;
362
363 if ( rectBound.width &&
364 !rectBound.Inside(ClientToScreen(event.GetPosition())) )
365 {
366 // mouse left the bounding rect, disappear
367 m_parent->Close();
368 }
369 else
370 {
371 event.Skip();
372 }
373}
374
961c54c3
RD
375#if !wxUSE_POPUPWIN
376void wxTipWindowView::OnKillFocus(wxFocusEvent& WXUNUSED(event))
377{
378 // Workaround the kill focus event happening just after creation in wxGTK
379 if (wxGetLocalTime() > m_creationTime + 1)
380 m_parent->Close();
381}
dafbe8c0 382#endif // !wxUSE_POPUPWIN
8962e1d9 383
dafbe8c0 384#endif // wxUSE_TIPWINDOW