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