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