]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tooltip.cpp
add operator* taking doubles to wxSize
[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 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_TOOLTIPS
27
28 #include "wx/tooltip.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/app.h"
33 #include "wx/control.h"
34 #endif
35
36 #include "wx/tokenzr.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 // a wrapper around TOOLINFO Win32 structure
79 #ifdef __VISUALC__
80 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
81 #endif
82
83 class wxToolInfo : public TOOLINFO
84 {
85 public:
86 wxToolInfo(HWND hwndOwner)
87 {
88 // initialize all members
89 ::ZeroMemory(this, sizeof(TOOLINFO));
90
91 // the structure TOOLINFO has been extended with a 4 byte field in
92 // version 4.70 of comctl32.dll and another one in 5.01 but we don't
93 // use these extended fields so use the old struct size to ensure that
94 // the tooltips work on old (Windows 95) systems too
95 cbSize = TTTOOLINFO_V1_SIZE;
96
97 hwnd = hwndOwner;
98 uFlags = TTF_IDISHWND;
99
100 // we use TTF_TRANSPARENT to fix a problem which arises at least with
101 // the text controls but may presumably happen with other controls
102 // which display the tooltip at mouse position: it can start flashing
103 // then as the control gets "focus lost" events and dismisses the
104 // tooltip which then reappears because mouse remains hovering over the
105 // control, see SF patch 1821229
106 if ( wxApp::GetComCtl32Version() >= 470 )
107 {
108 uFlags |= TTF_TRANSPARENT;
109 }
110
111 uId = (UINT_PTR)hwndOwner;
112 }
113 };
114
115 #ifdef __VISUALC__
116 #pragma warning( default : 4097 )
117 #endif
118
119 // ----------------------------------------------------------------------------
120 // private functions
121 // ----------------------------------------------------------------------------
122
123 // send a message to the tooltip control if it exists
124 //
125 // NB: wParam is always 0 for the TTM_XXX messages we use
126 static inline LRESULT SendTooltipMessage(WXHWND hwnd, UINT msg, void *lParam)
127 {
128 return hwnd ? ::SendMessage((HWND)hwnd, msg, 0, (LPARAM)lParam) : 0;
129 }
130
131 // send a message to all existing tooltip controls
132 static inline void
133 SendTooltipMessageToAll(WXHWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
134 {
135 if ( hwnd )
136 ::SendMessage((HWND)hwnd, msg, wParam, lParam);
137 }
138
139 // ============================================================================
140 // implementation
141 // ============================================================================
142
143 #if wxUSE_TTM_WINDOWFROMPOINT
144
145 // ----------------------------------------------------------------------------
146 // window proc for our tooltip control
147 // ----------------------------------------------------------------------------
148
149 LRESULT APIENTRY wxToolTipWndProc(HWND hwndTT,
150 UINT msg,
151 WPARAM wParam,
152 LPARAM lParam)
153 {
154 if ( msg == TTM_WINDOWFROMPOINT )
155 {
156 LPPOINT ppt = (LPPOINT)lParam;
157
158 // the window on which event occurred
159 HWND hwnd = ::WindowFromPoint(*ppt);
160
161 OutputDebugString("TTM_WINDOWFROMPOINT: ");
162 OutputDebugString(wxString::Format("0x%08x => ", hwnd));
163
164 // return a HWND corresponding to a wxWindow because only wxWidgets are
165 // associated with tooltips using TTM_ADDTOOL
166 wxWindow *win = wxGetWindowFromHWND((WXHWND)hwnd);
167
168 if ( win )
169 {
170 hwnd = GetHwndOf(win);
171 OutputDebugString(wxString::Format("0x%08x\r\n", hwnd));
172
173 #if 0
174 // modify the point too!
175 RECT rect;
176 GetWindowRect(hwnd, &rect);
177
178 ppt->x = (rect.right - rect.left) / 2;
179 ppt->y = (rect.bottom - rect.top) / 2;
180 #endif // 0
181 return (LRESULT)hwnd;
182 }
183 else
184 {
185 OutputDebugString("no window\r\n");
186 }
187 }
188
189 return ::CallWindowProc(CASTWNDPROC gs_wndprocToolTip, hwndTT, msg, wParam, lParam);
190 }
191
192 #endif // wxUSE_TTM_WINDOWFROMPOINT
193
194 // ----------------------------------------------------------------------------
195 // static functions
196 // ----------------------------------------------------------------------------
197
198 void wxToolTip::Enable(bool flag)
199 {
200 SendTooltipMessageToAll(ms_hwndTT, TTM_ACTIVATE, flag, 0);
201 }
202
203 void wxToolTip::SetDelay(long milliseconds)
204 {
205 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
206 TTDT_INITIAL, milliseconds);
207 }
208
209 void wxToolTip::SetAutoPop(long milliseconds)
210 {
211 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
212 TTDT_AUTOPOP, milliseconds);
213 }
214
215 void wxToolTip::SetReshow(long milliseconds)
216 {
217 SendTooltipMessageToAll(ms_hwndTT, TTM_SETDELAYTIME,
218 TTDT_RESHOW, milliseconds);
219 }
220
221 void wxToolTip::SetMaxWidth(int width)
222 {
223 wxASSERT_MSG( width == -1 || width >= 0, _T("invalid width value") );
224
225 ms_maxWidth = width;
226 }
227
228 // ---------------------------------------------------------------------------
229 // implementation helpers
230 // ---------------------------------------------------------------------------
231
232 // create the tooltip ctrl for our parent frame if it doesn't exist yet
233 /* static */
234 WXHWND wxToolTip::GetToolTipCtrl()
235 {
236 if ( !ms_hwndTT )
237 {
238 WXDWORD exflags = 0;
239 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
240 {
241 exflags |= WS_EX_LAYOUTRTL;
242 }
243
244 // we want to show the tooltips always (even when the window is not
245 // active) and we don't want to strip "&"s from them
246 ms_hwndTT = (WXHWND)::CreateWindowEx(exflags,
247 TOOLTIPS_CLASS,
248 (LPCTSTR)NULL,
249 TTS_ALWAYSTIP | TTS_NOPREFIX,
250 CW_USEDEFAULT, CW_USEDEFAULT,
251 CW_USEDEFAULT, CW_USEDEFAULT,
252 NULL, (HMENU)NULL,
253 wxGetInstance(),
254 NULL);
255 if ( ms_hwndTT )
256 {
257 HWND hwnd = (HWND)ms_hwndTT;
258 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
259 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
260
261 #if wxUSE_TTM_WINDOWFROMPOINT
262 // subclass the newly created control
263 gs_wndprocToolTip = wxSetWindowProc(hwnd, wxToolTipWndProc);
264 #endif // wxUSE_TTM_WINDOWFROMPOINT
265 }
266 }
267
268 return ms_hwndTT;
269 }
270
271 /* static */
272 void wxToolTip::RelayEvent(WXMSG *msg)
273 {
274 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, msg);
275 }
276
277 // ----------------------------------------------------------------------------
278 // ctor & dtor
279 // ----------------------------------------------------------------------------
280
281 IMPLEMENT_ABSTRACT_CLASS(wxToolTip, wxObject)
282
283 wxToolTip::wxToolTip(const wxString &tip)
284 : m_text(tip)
285 {
286 m_window = NULL;
287 }
288
289 wxToolTip::~wxToolTip()
290 {
291 // the tooltip has to be removed before deleting. Otherwise, if it is visible
292 // while being deleted, there will be a delay before it goes away.
293 Remove();
294 }
295
296 // ----------------------------------------------------------------------------
297 // others
298 // ----------------------------------------------------------------------------
299
300 void wxToolTip::Remove(WXHWND hWnd)
301 {
302 wxToolInfo ti((HWND)hWnd);
303 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, &ti);
304 }
305
306 void wxToolTip::Remove()
307 {
308 // remove this tool from the tooltip control
309 if ( m_window )
310 {
311 Remove(m_window->GetHWND());
312 }
313 }
314
315 void wxToolTip::Add(WXHWND hWnd)
316 {
317 HWND hwnd = (HWND)hWnd;
318
319 wxToolInfo ti(hwnd);
320
321 // another possibility would be to specify LPSTR_TEXTCALLBACK here as we
322 // store the tooltip text ourselves anyhow, and provide it in response to
323 // TTN_NEEDTEXT (sent via WM_NOTIFY), but then we would be limited to 79
324 // character tooltips as this is the size of the szText buffer in
325 // NMTTDISPINFO struct -- and setting the tooltip here we can have tooltips
326 // of any length
327 ti.hwnd = hwnd;
328 ti.lpszText = (wxChar *)m_text.wx_str(); // const_cast
329
330 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
331 {
332 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
333 }
334 else
335 {
336 #ifdef TTM_SETMAXTIPWIDTH
337 if ( wxApp::GetComCtl32Version() >= 470 )
338 {
339 // use TTM_SETMAXTIPWIDTH to make tooltip multiline using the
340 // extent of its first line as max value
341 HFONT hfont = (HFONT)
342 SendTooltipMessage(GetToolTipCtrl(), WM_GETFONT, 0);
343
344 if ( !hfont )
345 {
346 hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
347 if ( !hfont )
348 {
349 wxLogLastError(wxT("GetStockObject(DEFAULT_GUI_FONT)"));
350 }
351 }
352
353 MemoryHDC hdc;
354 if ( !hdc )
355 {
356 wxLogLastError(wxT("CreateCompatibleDC(NULL)"));
357 }
358
359 if ( !SelectObject(hdc, hfont) )
360 {
361 wxLogLastError(wxT("SelectObject(hfont)"));
362 }
363
364 // find the width of the widest line
365 int maxWidth = 0;
366 wxStringTokenizer tokenizer(m_text, _T("\n"));
367 while ( tokenizer.HasMoreTokens() )
368 {
369 const wxString token = tokenizer.GetNextToken();
370
371 SIZE sz;
372 if ( !::GetTextExtentPoint32(hdc, token.wx_str(),
373 token.length(), &sz) )
374 {
375 wxLogLastError(wxT("GetTextExtentPoint32"));
376 }
377
378 if ( sz.cx > maxWidth )
379 maxWidth = sz.cx;
380 }
381
382 // limit size to ms_maxWidth, if set
383 if ( ms_maxWidth == 0 )
384 {
385 // this is more or less arbitrary but seems to work well
386 static const int DEFAULT_MAX_WIDTH = 400;
387
388 ms_maxWidth = wxGetClientDisplayRect().width / 2;
389
390 if ( ms_maxWidth > DEFAULT_MAX_WIDTH )
391 ms_maxWidth = DEFAULT_MAX_WIDTH;
392 }
393
394 if ( ms_maxWidth != -1 && maxWidth > ms_maxWidth )
395 maxWidth = ms_maxWidth;
396
397 // only set a new width if it is bigger than the current setting
398 SendTooltipMessage(GetToolTipCtrl(), TTM_SETMAXTIPWIDTH,
399 wxUIntToPtr(maxWidth));
400 }
401 else
402 #endif // TTM_SETMAXTIPWIDTH
403 {
404 // replace the '\n's with spaces because otherwise they appear as
405 // unprintable characters in the tooltip string
406 m_text.Replace(_T("\n"), _T(" "));
407 ti.lpszText = (wxChar *)m_text.wx_str(); // const_cast
408
409 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, &ti) )
410 {
411 wxLogDebug(_T("Failed to create the tooltip '%s'"), m_text.c_str());
412 }
413 }
414 }
415 }
416
417 void wxToolTip::SetWindow(wxWindow *win)
418 {
419 Remove();
420
421 m_window = win;
422
423 // add the window itself
424 if ( m_window )
425 {
426 Add(m_window->GetHWND());
427 }
428 #if !defined(__WXUNIVERSAL__)
429 // and all of its subcontrols (e.g. radio buttons in a radiobox) as well
430 wxControl *control = wxDynamicCast(m_window, wxControl);
431 if ( control )
432 {
433 const wxArrayLong& subcontrols = control->GetSubcontrols();
434 size_t count = subcontrols.GetCount();
435 for ( size_t n = 0; n < count; n++ )
436 {
437 int id = subcontrols[n];
438 HWND hwnd = GetDlgItem(GetHwndOf(m_window), id);
439 if ( !hwnd )
440 {
441 // may be it's a child of parent of the control, in fact?
442 // (radiobuttons are subcontrols, i.e. children of the radiobox
443 // for wxWidgets but are its siblings at Windows level)
444 hwnd = GetDlgItem(GetHwndOf(m_window->GetParent()), id);
445 }
446
447 // must have it by now!
448 wxASSERT_MSG( hwnd, _T("no hwnd for subcontrol?") );
449
450 Add((WXHWND)hwnd);
451 }
452 }
453 #endif // !defined(__WXUNIVERSAL__)
454 }
455
456 void wxToolTip::SetTip(const wxString& tip)
457 {
458 m_text = tip;
459
460 if ( m_window )
461 {
462 // update the tip text shown by the control
463 wxToolInfo ti(GetHwndOf(m_window));
464 ti.lpszText = (wxChar *)m_text.wx_str();
465
466 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, &ti);
467 }
468 }
469
470 #endif // wxUSE_TOOLTIPS