1. wxFontMapper seems to work for wxMSW
[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 // a simple wrapper around TOOLINFO Win32 structure
51 #ifdef __VISUALC__
52 #pragma warning( disable : 4097 ) // we inherit from a typedef - so what?
53 #endif
54 class wxToolInfo : public TOOLINFO
55 {
56 public:
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 };
67 #ifdef __VISUALC__
68 #pragma warning( default : 4097 )
69 #endif
70
71 // ----------------------------------------------------------------------------
72 // private functions
73 // ----------------------------------------------------------------------------
74
75 // send a message to the tooltip control
76 inline 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
85 // send a message to all existing tooltip controls
86 static void SendTooltipMessageToAll(WXHWND hwnd,
87 UINT msg,
88 WPARAM wParam,
89 LPARAM lParam)
90 {
91 if ( hwnd )
92 (void)SendTooltipMessage((WXHWND)hwnd, msg, wParam, (void *)lParam);
93 }
94
95 // ============================================================================
96 // implementation
97 // ============================================================================
98
99 // ----------------------------------------------------------------------------
100 // static functions
101 // ----------------------------------------------------------------------------
102
103
104
105 void wxToolTip::Enable(bool flag)
106 {
107 SendTooltipMessageToAll((WXHWND)hwndTT,TTM_ACTIVATE, flag, 0);
108 }
109
110 void wxToolTip::SetDelay(long milliseconds)
111 {
112 SendTooltipMessageToAll((WXHWND)hwndTT,TTM_SETDELAYTIME, TTDT_INITIAL, milliseconds);
113 }
114
115 // ---------------------------------------------------------------------------
116 // implementation helpers
117 // ---------------------------------------------------------------------------
118
119 // create the tooltip ctrl for our parent frame if it doesn't exist yet
120 WXHWND wxToolTip::GetToolTipCtrl()
121 {
122 if ( !hwndTT )
123 {
124 hwndTT = (WXHWND)::CreateWindow(TOOLTIPS_CLASS,
125 (LPSTR)NULL,
126 TTS_ALWAYSTIP,
127 CW_USEDEFAULT, CW_USEDEFAULT,
128 CW_USEDEFAULT, CW_USEDEFAULT,
129 NULL, (HMENU)NULL,
130 wxGetInstance(),
131 NULL);
132 if ( hwndTT )
133 {
134 SetWindowPos((HWND)hwndTT, HWND_TOPMOST, 0, 0, 0, 0,
135 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
136 }
137
138 }
139
140 return (WXHWND)hwndTT;
141 }
142
143 void wxToolTip::RelayEvent(WXMSG *msg)
144 {
145 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, 0, msg);
146 }
147
148 // ----------------------------------------------------------------------------
149 // ctor & dtor
150 // ----------------------------------------------------------------------------
151
152 wxToolTip::wxToolTip(const wxString &tip)
153 : m_text(tip)
154 {
155 m_window = NULL;
156 }
157
158 wxToolTip::~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
168 void 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
178 void 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
203 void 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);
211 ti.lpszText = (wxChar *)m_text.c_str();
212
213 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
214 }
215 }
216
217 #endif // wxUSE_TOOLTIPS