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