]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tooltip.cpp
wxChoice and wxListBox GTK+ changes (wxChoice works, wxListBox still doesn't)
[wxWidgets.git] / src / msw / tooltip.cpp
CommitLineData
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
a17e237f 44WXHWND wxToolTip::hwndTT = (WXHWND)NULL;
066f302c 45
3a19e16d
VZ
46// ----------------------------------------------------------------------------
47// private classes
48// ----------------------------------------------------------------------------
49
066f302c 50
3a19e16d 51// a simple wrapper around TOOLINFO Win32 structure
26b83329
VZ
52#ifdef __VISUALC__
53 #pragma warning( disable : 4097 )
54#endif
3a19e16d
VZ
55class wxToolInfo : public TOOLINFO
56{
57public:
58 wxToolInfo(wxWindow *win)
59 {
60 // initialize all members
18ba9da6 61#if __GNUWIN32__ && !defined(wxUSE_NORLANDER_HEADERS)
cf0b3979
JS
62 memset(this, 0, sizeof(TOOLINFO));
63#else
3a19e16d 64 ::ZeroMemory(this, sizeof(TOOLINFO));
cf0b3979 65#endif
3a19e16d
VZ
66
67 cbSize = sizeof(TOOLINFO);
68 uFlags = TTF_IDISHWND;
69 uId = (UINT)win->GetHWND();
70 }
71};
26b83329
VZ
72#ifdef __VISUALC__
73 #pragma warning( default : 4097 )
74#endif
3a19e16d
VZ
75
76// ----------------------------------------------------------------------------
77// private functions
78// ----------------------------------------------------------------------------
79
80// send a message to the tooltip control
81inline 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
16f6dfd8 90// send a message to all existing tooltip controls
066f302c 91static void SendTooltipMessageToAll(WXHWND hwnd,
18ba9da6
RD
92 UINT msg,
93 WPARAM wParam,
066f302c 94 LPARAM lParam)
16f6dfd8 95{
066f302c
UM
96 if ( hwnd )
97 (void)SendTooltipMessage((WXHWND)hwnd, msg, wParam, (void *)lParam);
16f6dfd8
VZ
98}
99
3a19e16d
VZ
100// ============================================================================
101// implementation
102// ============================================================================
103
104// ----------------------------------------------------------------------------
16f6dfd8 105// static functions
3a19e16d
VZ
106// ----------------------------------------------------------------------------
107
066f302c
UM
108
109
16f6dfd8
VZ
110void wxToolTip::Enable(bool flag)
111{
066f302c 112 SendTooltipMessageToAll((WXHWND)hwndTT,TTM_ACTIVATE, flag, 0);
16f6dfd8
VZ
113}
114
115void wxToolTip::SetDelay(long milliseconds)
116{
066f302c 117 SendTooltipMessageToAll((WXHWND)hwndTT,TTM_SETDELAYTIME, TTDT_INITIAL, milliseconds);
16f6dfd8
VZ
118}
119
120// ---------------------------------------------------------------------------
121// implementation helpers
122// ---------------------------------------------------------------------------
123
3a19e16d
VZ
124// create the tooltip ctrl for our parent frame if it doesn't exist yet
125WXHWND wxToolTip::GetToolTipCtrl()
126{
3a19e16d
VZ
127 if ( !hwndTT )
128 {
18ba9da6 129 hwndTT = (WXHWND)::CreateWindow(TOOLTIPS_CLASS,
3a19e16d
VZ
130 (LPSTR)NULL,
131 TTS_ALWAYSTIP,
132 CW_USEDEFAULT, CW_USEDEFAULT,
133 CW_USEDEFAULT, CW_USEDEFAULT,
066f302c 134 NULL, (HMENU)NULL,
18ba9da6
RD
135 wxGetInstance(),
136 NULL);
28195455 137 if ( hwndTT )
086c94af
VZ
138 {
139 SetWindowPos((HWND)hwndTT, HWND_TOPMOST, 0, 0, 0, 0,
140 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
141 }
28195455 142
3a19e16d
VZ
143 }
144
145 return (WXHWND)hwndTT;
146}
147
3a19e16d
VZ
148void wxToolTip::RelayEvent(WXMSG *msg)
149{
150 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, 0, msg);
151}
152
3a19e16d
VZ
153// ----------------------------------------------------------------------------
154// ctor & dtor
155// ----------------------------------------------------------------------------
156
157wxToolTip::wxToolTip(const wxString &tip)
158 : m_text(tip)
159{
160 m_window = NULL;
161}
162
163wxToolTip::~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
173void 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
183void 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
208void 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);
837e5743 216 ti.lpszText = (wxChar *)m_text.c_str();
3a19e16d
VZ
217
218 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
219 }
220}
cb1a1dc9
VZ
221
222#endif // wxUSE_TOOLTIPS