]> git.saurik.com Git - wxWidgets.git/blame - src/generic/tipwin.cpp
added missing return on error
[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>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "tipwin.h"
22#endif
23
24// For compilers that support precompilatixon, includes "wx/wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/dcclient.h"
33#endif // WX_PRECOMP
34
35#include "wx/tipwin.h"
173e8bbf 36#include "wx/timer.h"
01fa3fe7
VZ
37
38// ----------------------------------------------------------------------------
39// constants
40// ----------------------------------------------------------------------------
41
42static const wxCoord TEXT_MARGIN_X = 3;
43static const wxCoord TEXT_MARGIN_Y = 3;
44
45// ============================================================================
46// implementation
47// ============================================================================
48
49// ----------------------------------------------------------------------------
50// event tables
51// ----------------------------------------------------------------------------
52
53BEGIN_EVENT_TABLE(wxTipWindow, wxFrame)
01fa3fe7
VZ
54 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick)
55 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick)
56 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick)
129caadd
JS
57 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus)
58 EVT_ACTIVATE(wxTipWindow::OnActivate)
01fa3fe7
VZ
59END_EVENT_TABLE()
60
173e8bbf
JS
61// Viewer window to put in the frame
62class wxTipWindowView: public wxWindow
63{
64public:
65 wxTipWindowView(wxWindow *parent);
66
67 // event handlers
68 void OnPaint(wxPaintEvent& event);
69 void OnMouseClick(wxMouseEvent& event);
70 void OnKillFocus(wxFocusEvent& event);
71
72 // calculate the client rect we need to display the text
73 void Adjust(const wxString& text, wxCoord maxLength);
74
75 long m_creationTime;
76
77 DECLARE_EVENT_TABLE()
78};
79
80BEGIN_EVENT_TABLE(wxTipWindowView, wxWindow)
81 EVT_PAINT(wxTipWindowView::OnPaint)
82 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick)
83 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick)
84 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick)
85 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus)
86END_EVENT_TABLE()
87
88
01fa3fe7
VZ
89// ----------------------------------------------------------------------------
90// wxTipWindow
91// ----------------------------------------------------------------------------
92
93wxTipWindow::wxTipWindow(wxWindow *parent,
94 const wxString& text,
173e8bbf 95 wxCoord maxLength, wxTipWindow** windowPtr)
01fa3fe7
VZ
96 : wxFrame(parent, -1, _T(""),
97 wxDefaultPosition, wxDefaultSize,
98 wxNO_BORDER | wxFRAME_FLOAT_ON_PARENT)
99{
100 // set colours
101 SetForegroundColour(*wxBLACK);
9806a0e7 102 SetBackgroundColour(wxColour(255, 255, 231));
01fa3fe7
VZ
103 // set position and size
104 int x, y;
105 wxGetMousePosition(&x, &y);
106 Move(x, y + 20);
107
173e8bbf
JS
108 wxTipWindowView* tipWindowView = new wxTipWindowView(this);
109 tipWindowView->Adjust(text, maxLength);
01fa3fe7 110
173e8bbf 111 m_windowPtr = windowPtr;
01fa3fe7
VZ
112
113 Show(TRUE);
173e8bbf
JS
114 tipWindowView->SetFocus();
115}
116
117wxTipWindow::~wxTipWindow()
118{
119 if (m_windowPtr)
120 {
121 *m_windowPtr = NULL;
122 }
123}
124
33ac7e6f 125void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event))
173e8bbf
JS
126{
127 Close();
128}
129
130void wxTipWindow::OnActivate(wxActivateEvent& event)
131{
132 if (!event.GetActive())
133 Close();
134}
135
33ac7e6f 136void wxTipWindow::OnKillFocus(wxFocusEvent& WXUNUSED(event))
173e8bbf 137{
9c0b5547
JS
138 // Under Windows at least, we will get this immediately
139 // because when the view window is focussed, the
140 // tip window goes out of focus.
141#ifdef __WXGTK__
173e8bbf 142 Close();
9c0b5547 143#endif
173e8bbf
JS
144}
145
146// ----------------------------------------------------------------------------
147// wxTipWindowView
148// ----------------------------------------------------------------------------
149
150wxTipWindowView::wxTipWindowView(wxWindow *parent)
151 : wxWindow(parent, -1,
152 wxDefaultPosition, wxDefaultSize,
153 wxNO_BORDER)
154{
155 // set colours
156 SetForegroundColour(*wxBLACK);
9806a0e7 157 SetBackgroundColour(wxColour(255, 255, 231));
173e8bbf 158 m_creationTime = wxGetLocalTime();
01fa3fe7
VZ
159}
160
173e8bbf 161void wxTipWindowView::Adjust(const wxString& text, wxCoord maxLength)
01fa3fe7 162{
173e8bbf 163 wxTipWindow* parent = (wxTipWindow*) GetParent();
01fa3fe7
VZ
164 wxClientDC dc(this);
165 dc.SetFont(GetFont());
166
167 // calculate the length: we want each line be no longer than maxLength
168 // pixels and we only break lines at words boundary
169 wxString current;
170 wxCoord height, width,
171 widthMax = 0;
173e8bbf 172 parent->m_heightLine = 0;
01fa3fe7
VZ
173
174 bool breakLine = FALSE;
175 for ( const wxChar *p = text.c_str(); ; p++ )
176 {
177 if ( *p == _T('\n') || *p == _T('\0') )
178 {
179 dc.GetTextExtent(current, &width, &height);
180 if ( width > widthMax )
181 widthMax = width;
182
173e8bbf
JS
183 if ( height > parent->m_heightLine )
184 parent->m_heightLine = height;
01fa3fe7 185
173e8bbf 186 parent->m_textLines.Add(current);
01fa3fe7
VZ
187
188 if ( !*p )
189 {
190 // end of text
191 break;
192 }
193
194 current.clear();
195 breakLine = FALSE;
196 }
197 else if ( breakLine && (*p == _T(' ') || *p == _T('\t')) )
198 {
199 // word boundary - break the line here
173e8bbf 200 parent->m_textLines.Add(current);
01fa3fe7
VZ
201 current.clear();
202 breakLine = FALSE;
203 }
204 else // line goes on
205 {
206 current += *p;
207 dc.GetTextExtent(current, &width, &height);
208 if ( width > maxLength )
209 breakLine = TRUE;
210
211 if ( width > widthMax )
212 widthMax = width;
213
173e8bbf
JS
214 if ( height > parent->m_heightLine )
215 parent->m_heightLine = height;
01fa3fe7
VZ
216 }
217 }
218
219 // take into account the border size and the margins
173e8bbf
JS
220 GetParent()->SetClientSize(2*(TEXT_MARGIN_X + 1) + widthMax,
221 2*(TEXT_MARGIN_Y + 1) + parent->m_textLines.GetCount()*parent->m_heightLine);
01fa3fe7
VZ
222}
223
33ac7e6f 224void wxTipWindowView::OnPaint(wxPaintEvent& WXUNUSED(event))
01fa3fe7 225{
173e8bbf
JS
226 wxTipWindow* parent = (wxTipWindow*) GetParent();
227 if (!parent)
228 return;
229
01fa3fe7
VZ
230 wxPaintDC dc(this);
231
232 wxRect rect;
233 wxSize size = GetClientSize();
234 rect.width = size.x;
235 rect.height = size.y;
236
237 // first filll the background
238 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
129caadd
JS
239
240 // Under Windows, you apparently get a thin black border whether you like it or not :-(
241#ifdef __WXMSW__
242 dc.SetPen( * wxTRANSPARENT_PEN );
243#else
98e8d44a 244 dc.SetPen( * wxBLACK_PEN );
129caadd 245#endif
01fa3fe7
VZ
246 dc.DrawRectangle(rect);
247
248 // and then draw the text line by line
249 dc.SetFont(GetFont());
250
251 wxPoint pt;
252 pt.x = TEXT_MARGIN_X;
253 pt.y = TEXT_MARGIN_Y;
173e8bbf 254 size_t count = parent->m_textLines.GetCount();
01fa3fe7
VZ
255 for ( size_t n = 0; n < count; n++ )
256 {
173e8bbf 257 dc.DrawText(parent->m_textLines[n], pt);
01fa3fe7 258
173e8bbf 259 pt.y += parent->m_heightLine;
01fa3fe7
VZ
260 }
261}
262
33ac7e6f 263void wxTipWindowView::OnMouseClick(wxMouseEvent& WXUNUSED(event))
129caadd 264{
173e8bbf 265 GetParent()->Close();
129caadd 266}
01fa3fe7 267
33ac7e6f 268void wxTipWindowView::OnKillFocus(wxFocusEvent& WXUNUSED(event))
129caadd 269{
173e8bbf
JS
270 // Workaround the kill focus event happening just after creation in wxGTK
271 if (wxGetLocalTime() > m_creationTime + 1)
272 GetParent()->Close();
01fa3fe7 273}
33ac7e6f 274