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