]>
Commit | Line | Data |
---|---|---|
3a19e16d | 1 | /////////////////////////////////////////////////////////////////////////////// |
623d5f80 | 2 | // Name: src/msw/tooltip.cpp |
3a19e16d VZ |
3 | // Purpose: wxToolTip class implementation for MSW |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 31.01.99 | |
3a19e16d | 7 | // Copyright: (c) 1999 Vadim Zeitlin |
65571936 | 8 | // Licence: wxWindows licence |
3a19e16d VZ |
9 | /////////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | #include "wx/wxprec.h" | |
20 | ||
21 | #ifdef __BORLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
cb1a1dc9 VZ |
25 | #if wxUSE_TOOLTIPS |
26 | ||
3a19e16d | 27 | #include "wx/tooltip.h" |
623d5f80 WS |
28 | |
29 | #ifndef WX_PRECOMP | |
57bd4c60 | 30 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
623d5f80 WS |
31 | #include "wx/app.h" |
32 | #include "wx/control.h" | |
623d5f80 WS |
33 | #endif |
34 | ||
543a9921 | 35 | #include "wx/tokenzr.h" |
0ce0f1e9 | 36 | #include "wx/vector.h" |
3a19e16d VZ |
37 | #include "wx/msw/private.h" |
38 | ||
7709915e VZ |
39 | #ifndef TTTOOLINFO_V1_SIZE |
40 | #define TTTOOLINFO_V1_SIZE 0x28 | |
41 | #endif | |
42 | ||
3be7ca64 VZ |
43 | #ifndef TTF_TRANSPARENT |
44 | #define TTF_TRANSPARENT 0x0100 | |
45 | #endif | |
46 | ||
f048e32f VZ |
47 | // VZ: normally, the trick with subclassing the tooltip control and processing |
48 | // TTM_WINDOWFROMPOINT should work but, somehow, it doesn't. I leave the | |
49 | // code here for now (but it's not compiled) in case we need it later. | |
50 | // | |
8614c467 VZ |
51 | // For now I use an ugly workaround and process TTN_NEEDTEXT directly in |
52 | // radio button wnd proc - fixing TTM_WINDOWFROMPOINT code would be nice | |
53 | // because it would then work for all controls, not only radioboxes but for | |
54 | // now I don't understand what's wrong with it... | |
55 | #define wxUSE_TTM_WINDOWFROMPOINT 0 | |
f048e32f | 56 | |
086c94af VZ |
57 | // ---------------------------------------------------------------------------- |
58 | // global variables | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | // the tooltip parent window | |
f048e32f VZ |
62 | WXHWND wxToolTip::ms_hwndTT = (WXHWND)NULL; |
63 | ||
be8b4385 VZ |
64 | // new tooltip maximum width, default value is set on first call to wxToolTip::Add() |
65 | int wxToolTip::ms_maxWidth = 0; | |
66 | ||
f048e32f VZ |
67 | #if wxUSE_TTM_WINDOWFROMPOINT |
68 | ||
69 | // the tooltip window proc | |
70 | static WNDPROC gs_wndprocToolTip = (WNDPROC)NULL; | |
71 | ||
72 | #endif // wxUSE_TTM_WINDOWFROMPOINT | |
066f302c | 73 | |
3a19e16d VZ |
74 | // ---------------------------------------------------------------------------- |
75 | // private classes | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
0ce0f1e9 VZ |
78 | // This is simply a wrapper for vector<HWND> but defined as a class to hide the |
79 | // details from the public header. | |
80 | class wxToolTipOtherWindows : public wxVector<WXHWND> | |
81 | { | |
82 | }; | |
83 | ||
bb448552 | 84 | // a wrapper around TOOLINFO Win32 structure |
26b83329 | 85 | #ifdef __VISUALC__ |
11c7d5b6 | 86 | #pragma warning( disable : 4097 ) // we inherit from a typedef - so what? |
26b83329 | 87 | #endif |
bb448552 | 88 | |
3a19e16d VZ |
89 | class wxToolInfo : public TOOLINFO |
90 | { | |
91 | public: | |
6418ad5e | 92 | wxToolInfo(HWND hwndOwner, unsigned int id, const wxRect& rc) |
3a19e16d VZ |
93 | { |
94 | // initialize all members | |
95 | ::ZeroMemory(this, sizeof(TOOLINFO)); | |
96 | ||
bb448552 | 97 | // the structure TOOLINFO has been extended with a 4 byte field in |
7709915e VZ |
98 | // version 4.70 of comctl32.dll and another one in 5.01 but we don't |
99 | // use these extended fields so use the old struct size to ensure that | |
100 | // the tooltips work on old (Windows 95) systems too | |
101 | cbSize = TTTOOLINFO_V1_SIZE; | |
bb448552 | 102 | |
2b5f62a0 | 103 | hwnd = hwndOwner; |
6418ad5e FM |
104 | |
105 | if (rc.IsEmpty()) | |
106 | { | |
107 | uFlags = TTF_IDISHWND; | |
108 | uId = (UINT_PTR)hwndOwner; | |
109 | } | |
110 | else | |
111 | { | |
112 | // this tooltip must be shown only if the mouse hovers a specific rect | |
113 | // of the hwnd parameter! | |
03647350 VZ |
114 | rect.left = rc.GetLeft(); |
115 | rect.right = rc.GetRight(); | |
116 | rect.top = rc.GetTop(); | |
117 | rect.bottom = rc.GetBottom(); | |
6418ad5e FM |
118 | |
119 | // note that not setting TTF_IDISHWND from the uFlags member means that the | |
120 | // ti.uId field should not contain the HWND but rather as MSDN says an | |
121 | // "Application-defined identifier of the tool"; this is used internally by | |
122 | // Windows to distinguish the different tooltips attached to the same window | |
123 | uId = id; | |
124 | } | |
7709915e | 125 | |
613db756 VZ |
126 | // we use TTF_TRANSPARENT to fix a problem which arises at least with |
127 | // the text controls but may presumably happen with other controls | |
128 | // which display the tooltip at mouse position: it can start flashing | |
129 | // then as the control gets "focus lost" events and dismisses the | |
130 | // tooltip which then reappears because mouse remains hovering over the | |
131 | // control, see SF patch 1821229 | |
132 | if ( wxApp::GetComCtl32Version() >= 470 ) | |
133 | { | |
134 | uFlags |= TTF_TRANSPARENT; | |
135 | } | |
3a19e16d VZ |
136 | } |
137 | }; | |
bb448552 | 138 | |
26b83329 VZ |
139 | #ifdef __VISUALC__ |
140 | #pragma warning( default : 4097 ) | |
141 | #endif | |
3a19e16d VZ |
142 | |
143 | // ---------------------------------------------------------------------------- | |
144 | // private functions | |
145 | // ---------------------------------------------------------------------------- | |
146 | ||
0c5405b7 VZ |
147 | // send a message to the tooltip control if it exists |
148 | // | |
149 | // NB: wParam is always 0 for the TTM_XXX messages we use | |
150 | static inline LRESULT SendTooltipMessage(WXHWND hwnd, UINT msg, void *lParam) | |
3a19e16d | 151 | { |
0c5405b7 | 152 | return hwnd ? ::SendMessage((HWND)hwnd, msg, 0, (LPARAM)lParam) : 0; |
3a19e16d VZ |
153 | } |
154 | ||
16f6dfd8 | 155 | // send a message to all existing tooltip controls |
0c5405b7 VZ |
156 | static inline void |
157 | SendTooltipMessageToAll(WXHWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) | |
16f6dfd8 | 158 | { |
0c5405b7 VZ |
159 | if ( hwnd ) |
160 | ::SendMessage((HWND)hwnd, msg, wParam, lParam); | |
16f6dfd8 VZ |
161 | } |
162 | ||
3a19e16d VZ |
163 | // ============================================================================ |
164 | // implementation | |
165 | // ============================================================================ | |
166 | ||
f048e32f VZ |
167 | #if wxUSE_TTM_WINDOWFROMPOINT |
168 | ||
3a19e16d | 169 | // ---------------------------------------------------------------------------- |
f048e32f | 170 | // window proc for our tooltip control |
3a19e16d VZ |
171 | // ---------------------------------------------------------------------------- |
172 | ||
f048e32f VZ |
173 | LRESULT APIENTRY wxToolTipWndProc(HWND hwndTT, |
174 | UINT msg, | |
175 | WPARAM wParam, | |
176 | LPARAM lParam) | |
177 | { | |
178 | if ( msg == TTM_WINDOWFROMPOINT ) | |
179 | { | |
180 | LPPOINT ppt = (LPPOINT)lParam; | |
8614c467 | 181 | |
3103e8a9 | 182 | // the window on which event occurred |
f048e32f VZ |
183 | HWND hwnd = ::WindowFromPoint(*ppt); |
184 | ||
8614c467 VZ |
185 | OutputDebugString("TTM_WINDOWFROMPOINT: "); |
186 | OutputDebugString(wxString::Format("0x%08x => ", hwnd)); | |
187 | ||
77ffb593 | 188 | // return a HWND corresponding to a wxWindow because only wxWidgets are |
f048e32f | 189 | // associated with tooltips using TTM_ADDTOOL |
8614c467 | 190 | wxWindow *win = wxGetWindowFromHWND((WXHWND)hwnd); |
f048e32f | 191 | |
8614c467 | 192 | if ( win ) |
f048e32f | 193 | { |
8614c467 VZ |
194 | hwnd = GetHwndOf(win); |
195 | OutputDebugString(wxString::Format("0x%08x\r\n", hwnd)); | |
196 | ||
197 | #if 0 | |
f048e32f VZ |
198 | // modify the point too! |
199 | RECT rect; | |
200 | GetWindowRect(hwnd, &rect); | |
201 | ||
8614c467 VZ |
202 | ppt->x = (rect.right - rect.left) / 2; |
203 | ppt->y = (rect.bottom - rect.top) / 2; | |
204 | #endif // 0 | |
f048e32f VZ |
205 | return (LRESULT)hwnd; |
206 | } | |
8614c467 VZ |
207 | else |
208 | { | |
209 | OutputDebugString("no window\r\n"); | |
210 | } | |
f048e32f VZ |
211 | } |
212 | ||
8caa4ed1 | 213 | return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip, hwndTT, msg, wParam, lParam); |
f048e32f VZ |
214 | } |
215 | ||
216 | #endif // wxUSE_TTM_WINDOWFROMPOINT | |
217 | ||
218 | // ---------------------------------------------------------------------------- | |
219 | // static functions | |
220 | // ---------------------------------------------------------------------------- | |
066f302c | 221 | |
16f6dfd8 VZ |
222 | void wxToolTip::Enable(bool flag) |
223 | { | |
a7d8506b JS |
224 | // Make sure the tooltip has been created |
225 | (void) GetToolTipCtrl(); | |
226 | ||
f048e32f | 227 | SendTooltipMessageToAll(ms_hwndTT, TTM_ACTIVATE, flag, 0); |
16f6dfd8 VZ |
228 | } |
229 | ||
230 | void wxToolTip::SetDelay(long milliseconds) | |
231 | { | |
a7d8506b JS |
232 | // Make sure the tooltip has been created |
233 | (void) GetToolTipCtrl(); | |
234 | ||
f048e32f VZ |
235 | SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME, |
236 | TTDT_INITIAL, milliseconds); | |
16f6dfd8 VZ |
237 | } |
238 | ||
becac1ef VZ |
239 | void wxToolTip::SetAutoPop(long milliseconds) |
240 | { | |
241 | SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME, | |
242 | TTDT_AUTOPOP, milliseconds); | |
243 | } | |
244 | ||
245 | void wxToolTip::SetReshow(long milliseconds) | |
246 | { | |
247 | SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME, | |
248 | TTDT_RESHOW, milliseconds); | |
249 | } | |
250 | ||
be8b4385 VZ |
251 | void wxToolTip::SetMaxWidth(int width) |
252 | { | |
9a83f860 | 253 | wxASSERT_MSG( width == -1 || width >= 0, wxT("invalid width value") ); |
be8b4385 VZ |
254 | |
255 | ms_maxWidth = width; | |
256 | } | |
257 | ||
16f6dfd8 VZ |
258 | // --------------------------------------------------------------------------- |
259 | // implementation helpers | |
260 | // --------------------------------------------------------------------------- | |
261 | ||
3a19e16d | 262 | // create the tooltip ctrl for our parent frame if it doesn't exist yet |
4453b38d | 263 | /* static */ |
3a19e16d VZ |
264 | WXHWND wxToolTip::GetToolTipCtrl() |
265 | { | |
f048e32f | 266 | if ( !ms_hwndTT ) |
3a19e16d | 267 | { |
978af864 VZ |
268 | WXDWORD exflags = 0; |
269 | if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft ) | |
270 | { | |
271 | exflags |= WS_EX_LAYOUTRTL; | |
272 | } | |
273 | ||
c5d7205c VZ |
274 | // we want to show the tooltips always (even when the window is not |
275 | // active) and we don't want to strip "&"s from them | |
978af864 VZ |
276 | ms_hwndTT = (WXHWND)::CreateWindowEx(exflags, |
277 | TOOLTIPS_CLASS, | |
278 | (LPCTSTR)NULL, | |
279 | TTS_ALWAYSTIP | TTS_NOPREFIX, | |
280 | CW_USEDEFAULT, CW_USEDEFAULT, | |
281 | CW_USEDEFAULT, CW_USEDEFAULT, | |
282 | NULL, (HMENU)NULL, | |
283 | wxGetInstance(), | |
284 | NULL); | |
f048e32f | 285 | if ( ms_hwndTT ) |
086c94af | 286 | { |
f048e32f VZ |
287 | HWND hwnd = (HWND)ms_hwndTT; |
288 | SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, | |
086c94af | 289 | SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); |
28195455 | 290 | |
f048e32f VZ |
291 | #if wxUSE_TTM_WINDOWFROMPOINT |
292 | // subclass the newly created control | |
975b6bcf | 293 | gs_wndprocToolTip = wxSetWindowProc(hwnd, wxToolTipWndProc); |
f048e32f VZ |
294 | #endif // wxUSE_TTM_WINDOWFROMPOINT |
295 | } | |
3a19e16d VZ |
296 | } |
297 | ||
f048e32f | 298 | return ms_hwndTT; |
3a19e16d VZ |
299 | } |
300 | ||
4453b38d | 301 | /* static */ |
3a19e16d VZ |
302 | void wxToolTip::RelayEvent(WXMSG *msg) |
303 | { | |
0c5405b7 | 304 | (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, msg); |
3a19e16d VZ |
305 | } |
306 | ||
3a19e16d VZ |
307 | // ---------------------------------------------------------------------------- |
308 | // ctor & dtor | |
309 | // ---------------------------------------------------------------------------- | |
310 | ||
b342f8f0 RD |
311 | IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject) |
312 | ||
3a19e16d VZ |
313 | wxToolTip::wxToolTip(const wxString &tip) |
314 | : m_text(tip) | |
315 | { | |
316 | m_window = NULL; | |
0ce0f1e9 | 317 | m_others = NULL; |
6418ad5e FM |
318 | |
319 | // make sure m_rect.IsEmpty() == true | |
320 | m_rect.SetWidth(0); | |
321 | m_rect.SetHeight(0); | |
322 | ||
323 | // since m_rect is not valid, m_id is ignored by wxToolInfo ctor... | |
324 | m_id = 0; | |
325 | } | |
326 | ||
327 | wxToolTip::wxToolTip(wxWindow* win, unsigned int id, const wxString &tip, const wxRect& rc) | |
328 | : m_text(tip), m_rect(rc), m_id(id) | |
329 | { | |
330 | m_window = NULL; | |
0ce0f1e9 | 331 | m_others = NULL; |
6418ad5e FM |
332 | |
333 | SetWindow(win); | |
3a19e16d VZ |
334 | } |
335 | ||
336 | wxToolTip::~wxToolTip() | |
337 | { | |
d8909578 | 338 | // the tooltip has to be removed before deleting. Otherwise, if it is visible |
623d5f80 WS |
339 | // while being deleted, there will be a delay before it goes away. |
340 | Remove(); | |
0ce0f1e9 VZ |
341 | |
342 | delete m_others; | |
3a19e16d VZ |
343 | } |
344 | ||
345 | // ---------------------------------------------------------------------------- | |
346 | // others | |
347 | // ---------------------------------------------------------------------------- | |
348 | ||
6418ad5e FM |
349 | /* static */ |
350 | void wxToolTip::Remove(WXHWND hWnd, unsigned int id, const wxRect& rc) | |
0c5405b7 | 351 | { |
6418ad5e FM |
352 | wxToolInfo ti((HWND)hWnd, id, rc); |
353 | ||
0c5405b7 VZ |
354 | (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, &ti); |
355 | } | |
356 | ||
0ce0f1e9 | 357 | void wxToolTip::DoRemove(WXHWND hWnd) |
3a19e16d | 358 | { |
a48cf5e2 | 359 | if ( m_window && hWnd == m_window->GetHWND() ) |
0ce0f1e9 VZ |
360 | { |
361 | // Remove the tooltip from the main window. | |
362 | Remove(hWnd, m_id, m_rect); | |
363 | } | |
364 | else | |
3a19e16d | 365 | { |
0ce0f1e9 VZ |
366 | // Not really sure what to pass to remove in this case... |
367 | Remove(hWnd, 0, wxRect()); | |
3a19e16d VZ |
368 | } |
369 | } | |
370 | ||
0ce0f1e9 VZ |
371 | void wxToolTip::Remove() |
372 | { | |
373 | DoForAllWindows(&wxToolTip::DoRemove); | |
374 | } | |
375 | ||
f7dd07f6 | 376 | void wxToolTip::AddOtherWindow(WXHWND hWnd) |
0ce0f1e9 VZ |
377 | { |
378 | if ( !m_others ) | |
379 | m_others = new wxToolTipOtherWindows; | |
380 | ||
381 | m_others->push_back(hWnd); | |
382 | ||
f7dd07f6 | 383 | DoAddHWND(hWnd); |
0ce0f1e9 VZ |
384 | } |
385 | ||
f7dd07f6 | 386 | void wxToolTip::DoAddHWND(WXHWND hWnd) |
f048e32f VZ |
387 | { |
388 | HWND hwnd = (HWND)hWnd; | |
389 | ||
6418ad5e | 390 | wxToolInfo ti(hwnd, m_id, m_rect); |
f048e32f | 391 | |
2b5f62a0 VZ |
392 | // another possibility would be to specify LPSTR_TEXTCALLBACK here as we |
393 | // store the tooltip text ourselves anyhow, and provide it in response to | |
394 | // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79 | |
395 | // character tooltips as this is the size of the szText buffer in | |
396 | // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips | |
397 | // of any length | |
f048e32f | 398 | ti.hwnd = hwnd; |
017dc06b | 399 | ti.lpszText = wxMSW_CONV_LPTSTR(m_text); |
f048e32f | 400 | |
0c5405b7 | 401 | if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) ) |
f048e32f | 402 | { |
9a83f860 | 403 | wxLogDebug(wxT("Failed to create the tooltip '%s'"), m_text.c_str()); |
8c4209f7 VZ |
404 | |
405 | return; | |
f048e32f | 406 | } |
8c4209f7 | 407 | |
5b59df83 | 408 | #ifdef TTM_SETMAXTIPWIDTH |
8c4209f7 VZ |
409 | if ( wxApp::GetComCtl32Version() >= 470 ) |
410 | { | |
411 | // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the | |
412 | // extent of its first line as max value | |
413 | HFONT hfont = (HFONT) | |
414 | SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT, 0); | |
0c5405b7 | 415 | |
8c4209f7 VZ |
416 | if ( !hfont ) |
417 | { | |
418 | hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT); | |
be8b4385 VZ |
419 | if ( !hfont ) |
420 | { | |
8c4209f7 | 421 | wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)")); |
be8b4385 | 422 | } |
8c4209f7 | 423 | } |
8614c467 | 424 | |
8c4209f7 VZ |
425 | MemoryHDC hdc; |
426 | if ( !hdc ) | |
427 | { | |
428 | wxLogLastError(wxT("CreateCompatibleDC(NULL)")); | |
429 | } | |
8614c467 | 430 | |
8c4209f7 VZ |
431 | if ( !SelectObject(hdc, hfont) ) |
432 | { | |
433 | wxLogLastError(wxT("SelectObject(hfont)")); | |
434 | } | |
8614c467 | 435 | |
8c4209f7 VZ |
436 | // find the width of the widest line |
437 | int maxWidth = 0; | |
9a83f860 | 438 | wxStringTokenizer tokenizer(m_text, wxT("\n")); |
8c4209f7 VZ |
439 | while ( tokenizer.HasMoreTokens() ) |
440 | { | |
441 | const wxString token = tokenizer.GetNextToken(); | |
be8b4385 | 442 | |
8c4209f7 | 443 | SIZE sz; |
017dc06b | 444 | if ( !::GetTextExtentPoint32(hdc, token.t_str(), |
8c4209f7 VZ |
445 | token.length(), &sz) ) |
446 | { | |
447 | wxLogLastError(wxT("GetTextExtentPoint32")); | |
8614c467 | 448 | } |
be8b4385 | 449 | |
8c4209f7 VZ |
450 | if ( sz.cx > maxWidth ) |
451 | maxWidth = sz.cx; | |
452 | } | |
453 | ||
454 | // limit size to ms_maxWidth, if set | |
455 | if ( ms_maxWidth == 0 ) | |
456 | { | |
457 | // this is more or less arbitrary but seems to work well | |
458 | static const int DEFAULT_MAX_WIDTH = 400; | |
8614c467 | 459 | |
8c4209f7 | 460 | ms_maxWidth = wxGetClientDisplayRect().width / 2; |
be8b4385 | 461 | |
8c4209f7 VZ |
462 | if ( ms_maxWidth > DEFAULT_MAX_WIDTH ) |
463 | ms_maxWidth = DEFAULT_MAX_WIDTH; | |
464 | } | |
be8b4385 | 465 | |
8c4209f7 VZ |
466 | if ( ms_maxWidth != -1 && maxWidth > ms_maxWidth ) |
467 | maxWidth = ms_maxWidth; | |
be8b4385 | 468 | |
8c4209f7 VZ |
469 | // only set a new width if it is bigger than the current setting: |
470 | // otherwise adding a tooltip with shorter line(s) than a previous | |
471 | // one would result in breaking the longer lines unnecessarily as | |
472 | // all our tooltips share the same maximal width | |
473 | if ( maxWidth > SendTooltipMessage(GetToolTipCtrl(), | |
474 | TTM_GETMAXTIPWIDTH, 0) ) | |
475 | { | |
476 | SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH, | |
477 | wxUIntToPtr(maxWidth)); | |
be8b4385 | 478 | } |
8c4209f7 VZ |
479 | } |
480 | else | |
be8b4385 | 481 | #endif // TTM_SETMAXTIPWIDTH |
8c4209f7 VZ |
482 | { |
483 | // replace the '\n's with spaces because otherwise they appear as | |
484 | // unprintable characters in the tooltip string | |
9a83f860 | 485 | m_text.Replace(wxT("\n"), wxT(" ")); |
017dc06b | 486 | ti.lpszText = wxMSW_CONV_LPTSTR(m_text); |
be8b4385 | 487 | |
8c4209f7 VZ |
488 | if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) ) |
489 | { | |
9a83f860 | 490 | wxLogDebug(wxT("Failed to create the tooltip '%s'"), m_text.c_str()); |
8614c467 VZ |
491 | } |
492 | } | |
f048e32f VZ |
493 | } |
494 | ||
3a19e16d VZ |
495 | void wxToolTip::SetWindow(wxWindow *win) |
496 | { | |
497 | Remove(); | |
498 | ||
499 | m_window = win; | |
500 | ||
f048e32f | 501 | // add the window itself |
3a19e16d VZ |
502 | if ( m_window ) |
503 | { | |
5e0b0955 | 504 | DoAddHWND(m_window->GetHWND()); |
f048e32f | 505 | } |
d4e5272b | 506 | #if !defined(__WXUNIVERSAL__) |
d6b9cc87 | 507 | // and all of its subcontrols (e.g. radio buttons in a radiobox) as well |
f048e32f VZ |
508 | wxControl *control = wxDynamicCast(m_window, wxControl); |
509 | if ( control ) | |
510 | { | |
bf25c88b | 511 | const wxArrayLong& subcontrols = control->GetSubcontrols(); |
8614c467 | 512 | size_t count = subcontrols.GetCount(); |
f048e32f | 513 | for ( size_t n = 0; n < count; n++ ) |
3a19e16d | 514 | { |
8614c467 | 515 | int id = subcontrols[n]; |
f048e32f | 516 | HWND hwnd = GetDlgItem(GetHwndOf(m_window), id); |
8614c467 | 517 | if ( !hwnd ) |
f048e32f | 518 | { |
0ce0f1e9 | 519 | // maybe it's a child of parent of the control, in fact? |
8614c467 | 520 | // (radiobuttons are subcontrols, i.e. children of the radiobox |
77ffb593 | 521 | // for wxWidgets but are its siblings at Windows level) |
8614c467 | 522 | hwnd = GetDlgItem(GetHwndOf(m_window->GetParent()), id); |
f048e32f | 523 | } |
8614c467 VZ |
524 | |
525 | // must have it by now! | |
9a83f860 | 526 | wxASSERT_MSG( hwnd, wxT("no hwnd for subcontrol?") ); |
8614c467 | 527 | |
f7dd07f6 | 528 | AddOtherWindow((WXHWND)hwnd); |
3a19e16d VZ |
529 | } |
530 | } | |
d4e5272b | 531 | #endif // !defined(__WXUNIVERSAL__) |
3a19e16d VZ |
532 | } |
533 | ||
6418ad5e FM |
534 | void wxToolTip::SetRect(const wxRect& rc) |
535 | { | |
536 | m_rect = rc; | |
537 | ||
538 | if ( m_window ) | |
539 | { | |
540 | wxToolInfo ti(GetHwndOf(m_window), m_id, m_rect); | |
541 | (void)SendTooltipMessage(GetToolTipCtrl(), TTM_NEWTOOLRECT, &ti); | |
542 | } | |
543 | } | |
544 | ||
3a19e16d VZ |
545 | void wxToolTip::SetTip(const wxString& tip) |
546 | { | |
547 | m_text = tip; | |
548 | ||
0ce0f1e9 VZ |
549 | DoForAllWindows(&wxToolTip::DoSetTip); |
550 | } | |
551 | ||
552 | void wxToolTip::DoSetTip(WXHWND hWnd) | |
553 | { | |
554 | // update the tip text shown by the control | |
555 | wxToolInfo ti((HWND)hWnd, m_id, m_rect); | |
556 | ||
557 | // for some reason, changing the tooltip text directly results in | |
558 | // repaint of the controls under it, see #10520 -- but this doesn't | |
559 | // happen if we reset it first | |
560 | ti.lpszText = const_cast<wxChar *>(wxT("")); | |
561 | (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti); | |
562 | ||
017dc06b | 563 | ti.lpszText = wxMSW_CONV_LPTSTR(m_text); |
0ce0f1e9 VZ |
564 | (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti); |
565 | } | |
566 | ||
567 | void wxToolTip::DoForAllWindows(void (wxToolTip::*func)(WXHWND)) | |
568 | { | |
a48cf5e2 | 569 | if ( m_window ) |
3a19e16d | 570 | { |
a48cf5e2 | 571 | (this->*func)(m_window->GetHWND()); |
0ce0f1e9 | 572 | } |
3a19e16d | 573 | |
0ce0f1e9 VZ |
574 | if ( m_others ) |
575 | { | |
576 | for ( wxToolTipOtherWindows::const_iterator it = m_others->begin(); | |
577 | it != m_others->end(); | |
578 | ++it ) | |
579 | { | |
580 | (this->*func)(*it); | |
581 | } | |
3a19e16d VZ |
582 | } |
583 | } | |
cb1a1dc9 VZ |
584 | |
585 | #endif // wxUSE_TOOLTIPS |