]>
Commit | Line | Data |
---|---|---|
3a19e16d VZ |
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 | ||
cb1a1dc9 VZ |
30 | #if wxUSE_TOOLTIPS |
31 | ||
3a19e16d VZ |
32 | #include "wx/tooltip.h" |
33 | #include "wx/msw/private.h" | |
34 | ||
65fd5cb0 | 35 | #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS) |
3a19e16d | 36 | #include <commctrl.h> |
acbd13a3 | 37 | #endif |
3a19e16d | 38 | |
086c94af VZ |
39 | // ---------------------------------------------------------------------------- |
40 | // global variables | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | // the tooltip parent window | |
18ba9da6 | 44 | WXHWND wxToolTip::hwndTT = NULL; |
066f302c | 45 | |
3a19e16d VZ |
46 | // ---------------------------------------------------------------------------- |
47 | // private classes | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
066f302c | 50 | |
3a19e16d | 51 | // a simple wrapper around TOOLINFO Win32 structure |
197dd9af | 52 | #pragma warning( disable : 4097 ) |
3a19e16d VZ |
53 | class wxToolInfo : public TOOLINFO |
54 | { | |
55 | public: | |
56 | wxToolInfo(wxWindow *win) | |
57 | { | |
58 | // initialize all members | |
18ba9da6 | 59 | #if __GNUWIN32__ && !defined(wxUSE_NORLANDER_HEADERS) |
cf0b3979 JS |
60 | memset(this, 0, sizeof(TOOLINFO)); |
61 | #else | |
3a19e16d | 62 | ::ZeroMemory(this, sizeof(TOOLINFO)); |
cf0b3979 | 63 | #endif |
3a19e16d VZ |
64 | |
65 | cbSize = sizeof(TOOLINFO); | |
66 | uFlags = TTF_IDISHWND; | |
67 | uId = (UINT)win->GetHWND(); | |
68 | } | |
69 | }; | |
197dd9af | 70 | #pragma warning( default : 4097 ) |
3a19e16d VZ |
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 | ||
16f6dfd8 | 86 | // send a message to all existing tooltip controls |
066f302c | 87 | static void SendTooltipMessageToAll(WXHWND hwnd, |
18ba9da6 RD |
88 | UINT msg, |
89 | WPARAM wParam, | |
066f302c | 90 | LPARAM lParam) |
16f6dfd8 | 91 | { |
066f302c UM |
92 | if ( hwnd ) |
93 | (void)SendTooltipMessage((WXHWND)hwnd, msg, wParam, (void *)lParam); | |
16f6dfd8 VZ |
94 | } |
95 | ||
3a19e16d VZ |
96 | // ============================================================================ |
97 | // implementation | |
98 | // ============================================================================ | |
99 | ||
100 | // ---------------------------------------------------------------------------- | |
16f6dfd8 | 101 | // static functions |
3a19e16d VZ |
102 | // ---------------------------------------------------------------------------- |
103 | ||
066f302c UM |
104 | |
105 | ||
16f6dfd8 VZ |
106 | void wxToolTip::Enable(bool flag) |
107 | { | |
066f302c | 108 | SendTooltipMessageToAll((WXHWND)hwndTT,TTM_ACTIVATE, flag, 0); |
16f6dfd8 VZ |
109 | } |
110 | ||
111 | void wxToolTip::SetDelay(long milliseconds) | |
112 | { | |
066f302c | 113 | SendTooltipMessageToAll((WXHWND)hwndTT,TTM_SETDELAYTIME, TTDT_INITIAL, milliseconds); |
16f6dfd8 VZ |
114 | } |
115 | ||
116 | // --------------------------------------------------------------------------- | |
117 | // implementation helpers | |
118 | // --------------------------------------------------------------------------- | |
119 | ||
3a19e16d VZ |
120 | // create the tooltip ctrl for our parent frame if it doesn't exist yet |
121 | WXHWND wxToolTip::GetToolTipCtrl() | |
122 | { | |
3a19e16d VZ |
123 | if ( !hwndTT ) |
124 | { | |
18ba9da6 | 125 | hwndTT = (WXHWND)::CreateWindow(TOOLTIPS_CLASS, |
3a19e16d VZ |
126 | (LPSTR)NULL, |
127 | TTS_ALWAYSTIP, | |
128 | CW_USEDEFAULT, CW_USEDEFAULT, | |
129 | CW_USEDEFAULT, CW_USEDEFAULT, | |
066f302c | 130 | NULL, (HMENU)NULL, |
18ba9da6 RD |
131 | wxGetInstance(), |
132 | NULL); | |
28195455 | 133 | if ( hwndTT ) |
086c94af VZ |
134 | { |
135 | SetWindowPos((HWND)hwndTT, HWND_TOPMOST, 0, 0, 0, 0, | |
136 | SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); | |
137 | } | |
28195455 | 138 | |
3a19e16d VZ |
139 | } |
140 | ||
141 | return (WXHWND)hwndTT; | |
142 | } | |
143 | ||
3a19e16d VZ |
144 | void wxToolTip::RelayEvent(WXMSG *msg) |
145 | { | |
146 | (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, 0, msg); | |
147 | } | |
148 | ||
3a19e16d VZ |
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); | |
837e5743 | 212 | ti.lpszText = (wxChar *)m_text.c_str(); |
3a19e16d VZ |
213 | |
214 | (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti); | |
215 | } | |
216 | } | |
cb1a1dc9 VZ |
217 | |
218 | #endif // wxUSE_TOOLTIPS |