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