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