]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tooltip.cpp
Remove support for Gnome printing from wxGTK.
[wxWidgets.git] / src / msw / tooltip.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/tooltip.cpp
3 // Purpose: wxToolTip class implementation for MSW
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 31.01.99
7 // Copyright: (c) 1999 Vadim Zeitlin
8 // Licence: wxWindows licence
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
25 #if wxUSE_TOOLTIPS
26
27 #include "wx/tooltip.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
31 #include "wx/app.h"
32 #include "wx/control.h"
33 #endif
34
35 #include "wx/tokenzr.h"
36 #include "wx/vector.h"
37 #include "wx/msw/private.h"
38
39 #ifndef TTTOOLINFO_V1_SIZE
40 #define TTTOOLINFO_V1_SIZE 0x28
41 #endif
42
43 #ifndef TTF_TRANSPARENT
44 #define TTF_TRANSPARENT 0x0100
45 #endif
46
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 //
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
56
57 // ----------------------------------------------------------------------------
58 // global variables
59 // ----------------------------------------------------------------------------
60
61 // the tooltip parent window
62 WXHWND wxToolTip::ms_hwndTT = (WXHWND)NULL;
63
64 // new tooltip maximum width, default value is set on first call to wxToolTip::Add()
65 int wxToolTip::ms_maxWidth = 0;
66
67 #if wxUSE_TTM_WINDOWFROMPOINT
68
69 // the tooltip window proc
70 static WNDPROC gs_wndprocToolTip = (WNDPROC)NULL;
71
72 #endif // wxUSE_TTM_WINDOWFROMPOINT
73
74 // ----------------------------------------------------------------------------
75 // private classes
76 // ----------------------------------------------------------------------------
77
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
84 // a wrapper around TOOLINFO Win32 structure
85 #ifdef __VISUALC__
86 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
87 #endif
88
89 class wxToolInfo : public TOOLINFO
90 {
91 public:
92 wxToolInfo(HWND hwndOwner, unsigned int id, const wxRect& rc)
93 {
94 // initialize all members
95 ::ZeroMemory(this, sizeof(TOOLINFO));
96
97 // the structure TOOLINFO has been extended with a 4 byte field in
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;
102
103 hwnd = hwndOwner;
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!
114 rect.left = rc.GetLeft();
115 rect.right = rc.GetRight();
116 rect.top = rc.GetTop();
117 rect.bottom = rc.GetBottom();
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 }
125
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 }
136 }
137 };
138
139 #ifdef __VISUALC__
140 #pragma warning( default : 4097 )
141 #endif
142
143 // ----------------------------------------------------------------------------
144 // private functions
145 // ----------------------------------------------------------------------------
146
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)
151 {
152 return hwnd ? ::SendMessage((HWND)hwnd, msg, 0, (LPARAM)lParam) : 0;
153 }
154
155 // send a message to all existing tooltip controls
156 static inline void
157 SendTooltipMessageToAll(WXHWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
158 {
159 if ( hwnd )
160 ::SendMessage((HWND)hwnd, msg, wParam, lParam);
161 }
162
163 // ============================================================================
164 // implementation
165 // ============================================================================
166
167 #if wxUSE_TTM_WINDOWFROMPOINT
168
169 // ----------------------------------------------------------------------------
170 // window proc for our tooltip control
171 // ----------------------------------------------------------------------------
172
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;
181
182 // the window on which event occurred
183 HWND hwnd = ::WindowFromPoint(*ppt);
184
185 OutputDebugString("TTM_WINDOWFROMPOINT: ");
186 OutputDebugString(wxString::Format("0x%08x => ", hwnd));
187
188 // return a HWND corresponding to a wxWindow because only wxWidgets are
189 // associated with tooltips using TTM_ADDTOOL
190 wxWindow *win = wxGetWindowFromHWND((WXHWND)hwnd);
191
192 if ( win )
193 {
194 hwnd = GetHwndOf(win);
195 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd));
196
197 #if 0
198 // modify the point too!
199 RECT rect;
200 GetWindowRect(hwnd, &rect);
201
202 ppt->x = (rect.right - rect.left) / 2;
203 ppt->y = (rect.bottom - rect.top) / 2;
204 #endif // 0
205 return (LRESULT)hwnd;
206 }
207 else
208 {
209 OutputDebugString("no window\r\n");
210 }
211 }
212
213 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip, hwndTT, msg, wParam, lParam);
214 }
215
216 #endif // wxUSE_TTM_WINDOWFROMPOINT
217
218 // ----------------------------------------------------------------------------
219 // static functions
220 // ----------------------------------------------------------------------------
221
222 void wxToolTip::Enable(bool flag)
223 {
224 // Make sure the tooltip has been created
225 (void) GetToolTipCtrl();
226
227 SendTooltipMessageToAll(ms_hwndTT, TTM_ACTIVATE, flag, 0);
228 }
229
230 void wxToolTip::SetDelay(long milliseconds)
231 {
232 // Make sure the tooltip has been created
233 (void) GetToolTipCtrl();
234
235 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
236 TTDT_INITIAL, milliseconds);
237 }
238
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
251 void wxToolTip::SetMaxWidth(int width)
252 {
253 wxASSERT_MSG( width == -1 || width >= 0, wxT("invalid width value") );
254
255 ms_maxWidth = width;
256 }
257
258 // ---------------------------------------------------------------------------
259 // implementation helpers
260 // ---------------------------------------------------------------------------
261
262 // create the tooltip ctrl for our parent frame if it doesn't exist yet
263 /* static */
264 WXHWND wxToolTip::GetToolTipCtrl()
265 {
266 if ( !ms_hwndTT )
267 {
268 WXDWORD exflags = 0;
269 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
270 {
271 exflags |= WS_EX_LAYOUTRTL;
272 }
273
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
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);
285 if ( ms_hwndTT )
286 {
287 HWND hwnd = (HWND)ms_hwndTT;
288 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
289 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
290
291 #if wxUSE_TTM_WINDOWFROMPOINT
292 // subclass the newly created control
293 gs_wndprocToolTip = wxSetWindowProc(hwnd, wxToolTipWndProc);
294 #endif // wxUSE_TTM_WINDOWFROMPOINT
295 }
296 }
297
298 return ms_hwndTT;
299 }
300
301 /* static */
302 void wxToolTip::RelayEvent(WXMSG *msg)
303 {
304 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, msg);
305 }
306
307 // ----------------------------------------------------------------------------
308 // ctor & dtor
309 // ----------------------------------------------------------------------------
310
311 IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
312
313 wxToolTip::wxToolTip(const wxString &tip)
314 : m_text(tip)
315 {
316 m_window = NULL;
317 m_others = NULL;
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;
331 m_others = NULL;
332
333 SetWindow(win);
334 }
335
336 wxToolTip::~wxToolTip()
337 {
338 // the tooltip has to be removed before deleting. Otherwise, if it is visible
339 // while being deleted, there will be a delay before it goes away.
340 Remove();
341
342 delete m_others;
343 }
344
345 // ----------------------------------------------------------------------------
346 // others
347 // ----------------------------------------------------------------------------
348
349 /* static */
350 void wxToolTip::Remove(WXHWND hWnd, unsigned int id, const wxRect& rc)
351 {
352 wxToolInfo ti((HWND)hWnd, id, rc);
353
354 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, &ti);
355 }
356
357 void wxToolTip::DoRemove(WXHWND hWnd)
358 {
359 if ( m_window && hWnd == m_window->GetHWND() )
360 {
361 // Remove the tooltip from the main window.
362 Remove(hWnd, m_id, m_rect);
363 }
364 else
365 {
366 // Not really sure what to pass to remove in this case...
367 Remove(hWnd, 0, wxRect());
368 }
369 }
370
371 void wxToolTip::Remove()
372 {
373 DoForAllWindows(&wxToolTip::DoRemove);
374 }
375
376 void wxToolTip::AddOtherWindow(WXHWND hWnd)
377 {
378 if ( !m_others )
379 m_others = new wxToolTipOtherWindows;
380
381 m_others->push_back(hWnd);
382
383 DoAddHWND(hWnd);
384 }
385
386 void wxToolTip::DoAddHWND(WXHWND hWnd)
387 {
388 HWND hwnd = (HWND)hWnd;
389
390 wxToolInfo ti(hwnd, m_id, m_rect);
391
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
398 ti.hwnd = hwnd;
399 ti.lpszText = wxMSW_CONV_LPTSTR(m_text);
400
401 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
402 {
403 wxLogDebug(wxT("Failed to create the tooltip '%s'"), m_text.c_str());
404
405 return;
406 }
407
408 #ifdef TTM_SETMAXTIPWIDTH
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);
415
416 if ( !hfont )
417 {
418 hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
419 if ( !hfont )
420 {
421 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
422 }
423 }
424
425 MemoryHDC hdc;
426 if ( !hdc )
427 {
428 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
429 }
430
431 if ( !SelectObject(hdc, hfont) )
432 {
433 wxLogLastError(wxT("SelectObject(hfont)"));
434 }
435
436 // find the width of the widest line
437 int maxWidth = 0;
438 wxStringTokenizer tokenizer(m_text, wxT("\n"));
439 while ( tokenizer.HasMoreTokens() )
440 {
441 const wxString token = tokenizer.GetNextToken();
442
443 SIZE sz;
444 if ( !::GetTextExtentPoint32(hdc, token.t_str(),
445 token.length(), &sz) )
446 {
447 wxLogLastError(wxT("GetTextExtentPoint32"));
448 }
449
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;
459
460 ms_maxWidth = wxGetClientDisplayRect().width / 2;
461
462 if ( ms_maxWidth > DEFAULT_MAX_WIDTH )
463 ms_maxWidth = DEFAULT_MAX_WIDTH;
464 }
465
466 if ( ms_maxWidth != -1 && maxWidth > ms_maxWidth )
467 maxWidth = ms_maxWidth;
468
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));
478 }
479 }
480 else
481 #endif // TTM_SETMAXTIPWIDTH
482 {
483 // replace the '\n's with spaces because otherwise they appear as
484 // unprintable characters in the tooltip string
485 m_text.Replace(wxT("\n"), wxT(" "));
486 ti.lpszText = wxMSW_CONV_LPTSTR(m_text);
487
488 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
489 {
490 wxLogDebug(wxT("Failed to create the tooltip '%s'"), m_text.c_str());
491 }
492 }
493 }
494
495 void wxToolTip::SetWindow(wxWindow *win)
496 {
497 Remove();
498
499 m_window = win;
500
501 // add the window itself
502 if ( m_window )
503 {
504 DoAddHWND(m_window->GetHWND());
505 }
506 #if !defined(__WXUNIVERSAL__)
507 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
508 wxControl *control = wxDynamicCast(m_window, wxControl);
509 if ( control )
510 {
511 const wxArrayLong& subcontrols = control->GetSubcontrols();
512 size_t count = subcontrols.GetCount();
513 for ( size_t n = 0; n < count; n++ )
514 {
515 int id = subcontrols[n];
516 HWND hwnd = GetDlgItem(GetHwndOf(m_window), id);
517 if ( !hwnd )
518 {
519 // maybe it's a child of parent of the control, in fact?
520 // (radiobuttons are subcontrols, i.e. children of the radiobox
521 // for wxWidgets but are its siblings at Windows level)
522 hwnd = GetDlgItem(GetHwndOf(m_window->GetParent()), id);
523 }
524
525 // must have it by now!
526 wxASSERT_MSG( hwnd, wxT("no hwnd for subcontrol?") );
527
528 AddOtherWindow((WXHWND)hwnd);
529 }
530 }
531 #endif // !defined(__WXUNIVERSAL__)
532 }
533
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
545 void wxToolTip::SetTip(const wxString& tip)
546 {
547 m_text = tip;
548
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
563 ti.lpszText = wxMSW_CONV_LPTSTR(m_text);
564 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti);
565 }
566
567 void wxToolTip::DoForAllWindows(void (wxToolTip::*func)(WXHWND))
568 {
569 if ( m_window )
570 {
571 (this->*func)(m_window->GetHWND());
572 }
573
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 }
582 }
583 }
584
585 #endif // wxUSE_TOOLTIPS