]>
Commit | Line | Data |
---|---|---|
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 | ||
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) | |
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 |
59 | END_EVENT_TABLE() |
60 | ||
173e8bbf JS |
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 | ||
01fa3fe7 VZ |
89 | // ---------------------------------------------------------------------------- |
90 | // wxTipWindow | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | wxTipWindow::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); | |
173e8bbf | 102 | #if !defined(__WXPM__) |
01fa3fe7 | 103 | SetBackgroundColour(wxColour(0xc3ffff)); |
5b3ed311 DW |
104 | #else |
105 | // What is 0xc3ffff, try some legable documentation for those of us who don't memorize hex codes?? | |
106 | SetBackgroundColour(wxColour(*wxWHITE)); | |
107 | #endif | |
01fa3fe7 VZ |
108 | // set position and size |
109 | int x, y; | |
110 | wxGetMousePosition(&x, &y); | |
111 | Move(x, y + 20); | |
112 | ||
173e8bbf JS |
113 | wxTipWindowView* tipWindowView = new wxTipWindowView(this); |
114 | tipWindowView->Adjust(text, maxLength); | |
01fa3fe7 | 115 | |
173e8bbf | 116 | m_windowPtr = windowPtr; |
01fa3fe7 VZ |
117 | |
118 | Show(TRUE); | |
173e8bbf JS |
119 | tipWindowView->SetFocus(); |
120 | } | |
121 | ||
122 | wxTipWindow::~wxTipWindow() | |
123 | { | |
124 | if (m_windowPtr) | |
125 | { | |
126 | *m_windowPtr = NULL; | |
127 | } | |
128 | } | |
129 | ||
33ac7e6f | 130 | void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event)) |
173e8bbf JS |
131 | { |
132 | Close(); | |
133 | } | |
134 | ||
135 | void wxTipWindow::OnActivate(wxActivateEvent& event) | |
136 | { | |
137 | if (!event.GetActive()) | |
138 | Close(); | |
139 | } | |
140 | ||
33ac7e6f | 141 | void wxTipWindow::OnKillFocus(wxFocusEvent& WXUNUSED(event)) |
173e8bbf | 142 | { |
9c0b5547 JS |
143 | // Under Windows at least, we will get this immediately |
144 | // because when the view window is focussed, the | |
145 | // tip window goes out of focus. | |
146 | #ifdef __WXGTK__ | |
173e8bbf | 147 | Close(); |
9c0b5547 | 148 | #endif |
173e8bbf JS |
149 | } |
150 | ||
151 | // ---------------------------------------------------------------------------- | |
152 | // wxTipWindowView | |
153 | // ---------------------------------------------------------------------------- | |
154 | ||
155 | wxTipWindowView::wxTipWindowView(wxWindow *parent) | |
156 | : wxWindow(parent, -1, | |
157 | wxDefaultPosition, wxDefaultSize, | |
158 | wxNO_BORDER) | |
159 | { | |
160 | // set colours | |
161 | SetForegroundColour(*wxBLACK); | |
162 | #if !defined(__WXPM__) | |
163 | SetBackgroundColour(wxColour(0xc3ffff)); | |
164 | #else | |
165 | // What is 0xc3ffff, try some legable documentation for those of us who don't memorize hex codes?? | |
166 | SetBackgroundColour(wxColour(*wxWHITE)); | |
167 | #endif | |
168 | m_creationTime = wxGetLocalTime(); | |
01fa3fe7 VZ |
169 | } |
170 | ||
173e8bbf | 171 | void wxTipWindowView::Adjust(const wxString& text, wxCoord maxLength) |
01fa3fe7 | 172 | { |
173e8bbf | 173 | wxTipWindow* parent = (wxTipWindow*) GetParent(); |
01fa3fe7 VZ |
174 | wxClientDC dc(this); |
175 | dc.SetFont(GetFont()); | |
176 | ||
177 | // calculate the length: we want each line be no longer than maxLength | |
178 | // pixels and we only break lines at words boundary | |
179 | wxString current; | |
180 | wxCoord height, width, | |
181 | widthMax = 0; | |
173e8bbf | 182 | parent->m_heightLine = 0; |
01fa3fe7 VZ |
183 | |
184 | bool breakLine = FALSE; | |
185 | for ( const wxChar *p = text.c_str(); ; p++ ) | |
186 | { | |
187 | if ( *p == _T('\n') || *p == _T('\0') ) | |
188 | { | |
189 | dc.GetTextExtent(current, &width, &height); | |
190 | if ( width > widthMax ) | |
191 | widthMax = width; | |
192 | ||
173e8bbf JS |
193 | if ( height > parent->m_heightLine ) |
194 | parent->m_heightLine = height; | |
01fa3fe7 | 195 | |
173e8bbf | 196 | parent->m_textLines.Add(current); |
01fa3fe7 VZ |
197 | |
198 | if ( !*p ) | |
199 | { | |
200 | // end of text | |
201 | break; | |
202 | } | |
203 | ||
204 | current.clear(); | |
205 | breakLine = FALSE; | |
206 | } | |
207 | else if ( breakLine && (*p == _T(' ') || *p == _T('\t')) ) | |
208 | { | |
209 | // word boundary - break the line here | |
173e8bbf | 210 | parent->m_textLines.Add(current); |
01fa3fe7 VZ |
211 | current.clear(); |
212 | breakLine = FALSE; | |
213 | } | |
214 | else // line goes on | |
215 | { | |
216 | current += *p; | |
217 | dc.GetTextExtent(current, &width, &height); | |
218 | if ( width > maxLength ) | |
219 | breakLine = TRUE; | |
220 | ||
221 | if ( width > widthMax ) | |
222 | widthMax = width; | |
223 | ||
173e8bbf JS |
224 | if ( height > parent->m_heightLine ) |
225 | parent->m_heightLine = height; | |
01fa3fe7 VZ |
226 | } |
227 | } | |
228 | ||
229 | // take into account the border size and the margins | |
173e8bbf JS |
230 | GetParent()->SetClientSize(2*(TEXT_MARGIN_X + 1) + widthMax, |
231 | 2*(TEXT_MARGIN_Y + 1) + parent->m_textLines.GetCount()*parent->m_heightLine); | |
01fa3fe7 VZ |
232 | } |
233 | ||
33ac7e6f | 234 | void wxTipWindowView::OnPaint(wxPaintEvent& WXUNUSED(event)) |
01fa3fe7 | 235 | { |
173e8bbf JS |
236 | wxTipWindow* parent = (wxTipWindow*) GetParent(); |
237 | if (!parent) | |
238 | return; | |
239 | ||
01fa3fe7 VZ |
240 | wxPaintDC dc(this); |
241 | ||
242 | wxRect rect; | |
243 | wxSize size = GetClientSize(); | |
244 | rect.width = size.x; | |
245 | rect.height = size.y; | |
246 | ||
247 | // first filll the background | |
248 | dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID)); | |
129caadd JS |
249 | |
250 | // Under Windows, you apparently get a thin black border whether you like it or not :-( | |
251 | #ifdef __WXMSW__ | |
252 | dc.SetPen( * wxTRANSPARENT_PEN ); | |
253 | #else | |
98e8d44a | 254 | dc.SetPen( * wxBLACK_PEN ); |
129caadd | 255 | #endif |
01fa3fe7 VZ |
256 | dc.DrawRectangle(rect); |
257 | ||
258 | // and then draw the text line by line | |
259 | dc.SetFont(GetFont()); | |
260 | ||
261 | wxPoint pt; | |
262 | pt.x = TEXT_MARGIN_X; | |
263 | pt.y = TEXT_MARGIN_Y; | |
173e8bbf | 264 | size_t count = parent->m_textLines.GetCount(); |
01fa3fe7 VZ |
265 | for ( size_t n = 0; n < count; n++ ) |
266 | { | |
173e8bbf | 267 | dc.DrawText(parent->m_textLines[n], pt); |
01fa3fe7 | 268 | |
173e8bbf | 269 | pt.y += parent->m_heightLine; |
01fa3fe7 VZ |
270 | } |
271 | } | |
272 | ||
33ac7e6f | 273 | void wxTipWindowView::OnMouseClick(wxMouseEvent& WXUNUSED(event)) |
129caadd | 274 | { |
173e8bbf | 275 | GetParent()->Close(); |
129caadd | 276 | } |
01fa3fe7 | 277 | |
33ac7e6f | 278 | void wxTipWindowView::OnKillFocus(wxFocusEvent& WXUNUSED(event)) |
129caadd | 279 | { |
173e8bbf JS |
280 | // Workaround the kill focus event happening just after creation in wxGTK |
281 | if (wxGetLocalTime() > m_creationTime + 1) | |
282 | GetParent()->Close(); | |
01fa3fe7 | 283 | } |
33ac7e6f | 284 |