]> git.saurik.com Git - wxWidgets.git/blob - src/generic/tipwin.cpp
corrected conditional compilation
[wxWidgets.git] / src / generic / tipwin.cpp
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"
36 #include "wx/timer.h"
37
38 // ----------------------------------------------------------------------------
39 // constants
40 // ----------------------------------------------------------------------------
41
42 static const wxCoord TEXT_MARGIN_X = 3;
43 static const wxCoord TEXT_MARGIN_Y = 3;
44
45 // ============================================================================
46 // implementation
47 // ============================================================================
48
49 // ----------------------------------------------------------------------------
50 // event tables
51 // ----------------------------------------------------------------------------
52
53 BEGIN_EVENT_TABLE(wxTipWindow, wxFrame)
54 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick)
55 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick)
56 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick)
57 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus)
58 EVT_ACTIVATE(wxTipWindow::OnActivate)
59 END_EVENT_TABLE()
60
61 // Viewer window to put in the frame
62 class wxTipWindowView: public wxWindow
63 {
64 public:
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
80 BEGIN_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)
86 END_EVENT_TABLE()
87
88
89 // ----------------------------------------------------------------------------
90 // wxTipWindow
91 // ----------------------------------------------------------------------------
92
93 wxTipWindow::wxTipWindow(wxWindow *parent,
94 const wxString& text,
95 wxCoord maxLength, wxTipWindow** windowPtr)
96 : wxFrame(parent, -1, _T(""),
97 wxDefaultPosition, wxDefaultSize,
98 wxNO_BORDER | wxFRAME_FLOAT_ON_PARENT)
99 {
100 // set colours
101 SetForegroundColour(*wxBLACK);
102 SetBackgroundColour(wxColour(255, 255, 231));
103 // set position and size
104 int x, y;
105 wxGetMousePosition(&x, &y);
106 Move(x, y + 20);
107
108 wxTipWindowView* tipWindowView = new wxTipWindowView(this);
109 tipWindowView->Adjust(text, maxLength);
110
111 m_windowPtr = windowPtr;
112
113 Show(TRUE);
114 tipWindowView->SetFocus();
115 }
116
117 wxTipWindow::~wxTipWindow()
118 {
119 if (m_windowPtr)
120 {
121 *m_windowPtr = NULL;
122 }
123 }
124
125 void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event))
126 {
127 Close();
128 }
129
130 void wxTipWindow::OnActivate(wxActivateEvent& event)
131 {
132 if (!event.GetActive())
133 Close();
134 }
135
136 void wxTipWindow::OnKillFocus(wxFocusEvent& WXUNUSED(event))
137 {
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__
142 Close();
143 #endif
144 }
145
146 // ----------------------------------------------------------------------------
147 // wxTipWindowView
148 // ----------------------------------------------------------------------------
149
150 wxTipWindowView::wxTipWindowView(wxWindow *parent)
151 : wxWindow(parent, -1,
152 wxDefaultPosition, wxDefaultSize,
153 wxNO_BORDER)
154 {
155 // set colours
156 SetForegroundColour(*wxBLACK);
157 SetBackgroundColour(wxColour(255, 255, 231));
158 m_creationTime = wxGetLocalTime();
159 }
160
161 void wxTipWindowView::Adjust(const wxString& text, wxCoord maxLength)
162 {
163 wxTipWindow* parent = (wxTipWindow*) GetParent();
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;
172 parent->m_heightLine = 0;
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
183 if ( height > parent->m_heightLine )
184 parent->m_heightLine = height;
185
186 parent->m_textLines.Add(current);
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
200 parent->m_textLines.Add(current);
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
214 if ( height > parent->m_heightLine )
215 parent->m_heightLine = height;
216 }
217 }
218
219 // take into account the border size and the margins
220 GetParent()->SetClientSize(2*(TEXT_MARGIN_X + 1) + widthMax,
221 2*(TEXT_MARGIN_Y + 1) + parent->m_textLines.GetCount()*parent->m_heightLine);
222 }
223
224 void wxTipWindowView::OnPaint(wxPaintEvent& WXUNUSED(event))
225 {
226 wxTipWindow* parent = (wxTipWindow*) GetParent();
227 if (!parent)
228 return;
229
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));
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
244 dc.SetPen( * wxBLACK_PEN );
245 #endif
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;
254 size_t count = parent->m_textLines.GetCount();
255 for ( size_t n = 0; n < count; n++ )
256 {
257 dc.DrawText(parent->m_textLines[n], pt);
258
259 pt.y += parent->m_heightLine;
260 }
261 }
262
263 void wxTipWindowView::OnMouseClick(wxMouseEvent& WXUNUSED(event))
264 {
265 GetParent()->Close();
266 }
267
268 void wxTipWindowView::OnKillFocus(wxFocusEvent& WXUNUSED(event))
269 {
270 // Workaround the kill focus event happening just after creation in wxGTK
271 if (wxGetLocalTime() > m_creationTime + 1)
272 GetParent()->Close();
273 }
274