]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tooltip.cpp
Added comments
[wxWidgets.git] / src / msw / tooltip.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 license
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 #ifndef WX_PRECOMP
27 #include "wx/wx.h"
28 #endif
29
30 #if wxUSE_TOOLTIPS
31
32 #include "wx/tooltip.h"
33 #include "wx/msw/private.h"
34
35 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
36 #include <commctrl.h>
37 #endif
38
39 // ----------------------------------------------------------------------------
40 // global variables
41 // ----------------------------------------------------------------------------
42
43 // the tooltip parent window
44 WXHWND wxToolTip::hwndTT = NULL;
45
46 // ----------------------------------------------------------------------------
47 // private classes
48 // ----------------------------------------------------------------------------
49
50
51 // a simple wrapper around TOOLINFO Win32 structure
52 #pragma warning( disable : 4097 )
53 class wxToolInfo : public TOOLINFO
54 {
55 public:
56 wxToolInfo(wxWindow *win)
57 {
58 // initialize all members
59 #if __GNUWIN32__ && !defined(wxUSE_NORLANDER_HEADERS)
60 memset(this, 0, sizeof(TOOLINFO));
61 #else
62 ::ZeroMemory(this, sizeof(TOOLINFO));
63 #endif
64
65 cbSize = sizeof(TOOLINFO);
66 uFlags = TTF_IDISHWND;
67 uId = (UINT)win->GetHWND();
68 }
69 };
70 #pragma warning( default : 4097 )
71
72 // ----------------------------------------------------------------------------
73 // private functions
74 // ----------------------------------------------------------------------------
75
76 // send a message to the tooltip control
77 inline LRESULT SendTooltipMessage(WXHWND hwnd,
78 UINT msg,
79 WPARAM wParam,
80 void *lParam)
81 {
82 return hwnd ? ::SendMessage((HWND)hwnd, msg, wParam, (LPARAM)lParam)
83 : 0;
84 }
85
86 // send a message to all existing tooltip controls
87 static void SendTooltipMessageToAll(WXHWND hwnd,
88 UINT msg,
89 WPARAM wParam,
90 LPARAM lParam)
91 {
92 if ( hwnd )
93 (void)SendTooltipMessage((WXHWND)hwnd, msg, wParam, (void *)lParam);
94 }
95
96 // ============================================================================
97 // implementation
98 // ============================================================================
99
100 // ----------------------------------------------------------------------------
101 // static functions
102 // ----------------------------------------------------------------------------
103
104
105
106 void wxToolTip::Enable(bool flag)
107 {
108 SendTooltipMessageToAll((WXHWND)hwndTT,TTM_ACTIVATE, flag, 0);
109 }
110
111 void wxToolTip::SetDelay(long milliseconds)
112 {
113 SendTooltipMessageToAll((WXHWND)hwndTT,TTM_SETDELAYTIME, TTDT_INITIAL, milliseconds);
114 }
115
116 // ---------------------------------------------------------------------------
117 // implementation helpers
118 // ---------------------------------------------------------------------------
119
120 // create the tooltip ctrl for our parent frame if it doesn't exist yet
121 WXHWND wxToolTip::GetToolTipCtrl()
122 {
123 if ( !hwndTT )
124 {
125 hwndTT = (WXHWND)::CreateWindow(TOOLTIPS_CLASS,
126 (LPSTR)NULL,
127 TTS_ALWAYSTIP,
128 CW_USEDEFAULT, CW_USEDEFAULT,
129 CW_USEDEFAULT, CW_USEDEFAULT,
130 NULL, (HMENU)NULL,
131 wxGetInstance(),
132 NULL);
133 if ( hwndTT )
134 {
135 SetWindowPos((HWND)hwndTT, HWND_TOPMOST, 0, 0, 0, 0,
136 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
137 }
138
139 }
140
141 return (WXHWND)hwndTT;
142 }
143
144 void wxToolTip::RelayEvent(WXMSG *msg)
145 {
146 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, 0, msg);
147 }
148
149 // ----------------------------------------------------------------------------
150 // ctor & dtor
151 // ----------------------------------------------------------------------------
152
153 wxToolTip::wxToolTip(const wxString &tip)
154 : m_text(tip)
155 {
156 m_window = NULL;
157 }
158
159 wxToolTip::~wxToolTip()
160 {
161 // there is no need to Remove() this tool - it will be done automatically
162 // anyhow
163 }
164
165 // ----------------------------------------------------------------------------
166 // others
167 // ----------------------------------------------------------------------------
168
169 void wxToolTip::Remove()
170 {
171 // remove this tool from the tooltip control
172 if ( m_window )
173 {
174 wxToolInfo ti(m_window);
175 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, 0, &ti);
176 }
177 }
178
179 void wxToolTip::SetWindow(wxWindow *win)
180 {
181 Remove();
182
183 m_window = win;
184
185 if ( m_window )
186 {
187 wxToolInfo ti(m_window);
188
189 // as we store our text anyhow, it seems useless to waste system memory
190 // by asking the tooltip ctrl to remember it too - instead it will send
191 // us TTN_NEEDTEXT (via WM_NOTIFY) when it is about to be shown
192 ti.hwnd = (HWND)m_window->GetHWND();
193 ti.lpszText = LPSTR_TEXTCALLBACK;
194 // instead of: ti.lpszText = (char *)m_text.c_str();
195
196 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, 0, &ti) )
197 {
198 wxLogSysError(_("Failed to create the tooltip '%s'"),
199 m_text.c_str());
200 }
201 }
202 }
203
204 void wxToolTip::SetTip(const wxString& tip)
205 {
206 m_text = tip;
207
208 if ( m_window )
209 {
210 // update it immediately
211 wxToolInfo ti(m_window);
212 ti.lpszText = (wxChar *)m_text.c_str();
213
214 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
215 }
216 }
217
218 #endif // wxUSE_TOOLTIPS