fixes for compilation without wxUSE_TOOLTIPS
[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__)
36 #include <commctrl.h>
37 #endif
38
39 // ----------------------------------------------------------------------------
40 // private classes
41 // ----------------------------------------------------------------------------
42
43 // a simple wrapper around TOOLINFO Win32 structure
44 class wxToolInfo : public TOOLINFO
45 {
46 public:
47 wxToolInfo(wxWindow *win)
48 {
49 // initialize all members
50 ::ZeroMemory(this, sizeof(TOOLINFO));
51
52 cbSize = sizeof(TOOLINFO);
53 uFlags = TTF_IDISHWND;
54 uId = (UINT)win->GetHWND();
55 }
56 };
57
58 // ----------------------------------------------------------------------------
59 // private functions
60 // ----------------------------------------------------------------------------
61
62 // send a message to the tooltip control
63 inline LRESULT SendTooltipMessage(WXHWND hwnd,
64 UINT msg,
65 WPARAM wParam,
66 void *lParam)
67 {
68 return hwnd ? ::SendMessage((HWND)hwnd, msg, wParam, (LPARAM)lParam)
69 : 0;
70 }
71
72 // ============================================================================
73 // implementation
74 // ============================================================================
75
76 // ----------------------------------------------------------------------------
77 // "semiglobal" functions - these methods work with the tooltip control which
78 // is shared among all the wxToolTips of the same frame
79 // ----------------------------------------------------------------------------
80
81 // create the tooltip ctrl for our parent frame if it doesn't exist yet
82 WXHWND wxToolTip::GetToolTipCtrl()
83 {
84 wxWindow *parent = m_window;
85 while ( parent && !parent->IsKindOf(CLASSINFO(wxFrame)) )
86 {
87 parent = parent->GetParent();
88 }
89
90 wxCHECK_MSG( parent, 0, "can't create tooltip control outside a frame" );
91
92 wxFrame *frame = (wxFrame *)parent;
93 HWND hwndTT = (HWND)frame->GetToolTipCtrl();
94 if ( !hwndTT )
95 {
96 hwndTT = ::CreateWindow(TOOLTIPS_CLASS,
97 (LPSTR)NULL,
98 TTS_ALWAYSTIP,
99 CW_USEDEFAULT, CW_USEDEFAULT,
100 CW_USEDEFAULT, CW_USEDEFAULT,
101 (HWND)frame->GetHWND(), (HMENU)NULL,
102 wxGetInstance(), NULL);
103
104 if ( hwndTT )
105 {
106 frame->SetToolTipCtrl((WXHWND)hwndTT);
107 }
108 else
109 {
110 wxLogSysError(_("Can not create tooltip control"));
111 }
112 }
113
114 return (WXHWND)hwndTT;
115 }
116
117 void wxToolTip::Enable(bool flag)
118 {
119 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_ACTIVATE, flag, 0);
120 }
121
122 void wxToolTip::RelayEvent(WXMSG *msg)
123 {
124 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, 0, msg);
125 }
126
127 void wxToolTip::SetDelay(long milliseconds)
128 {
129 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_SETDELAYTIME,
130 TTDT_INITIAL, (void *)milliseconds);
131 }
132
133 // ----------------------------------------------------------------------------
134 // ctor & dtor
135 // ----------------------------------------------------------------------------
136
137 wxToolTip::wxToolTip(const wxString &tip)
138 : m_text(tip)
139 {
140 m_window = NULL;
141 }
142
143 wxToolTip::~wxToolTip()
144 {
145 // there is no need to Remove() this tool - it will be done automatically
146 // anyhow
147 }
148
149 // ----------------------------------------------------------------------------
150 // others
151 // ----------------------------------------------------------------------------
152
153 void wxToolTip::Remove()
154 {
155 // remove this tool from the tooltip control
156 if ( m_window )
157 {
158 wxToolInfo ti(m_window);
159 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_DELTOOL, 0, &ti);
160 }
161 }
162
163 void wxToolTip::SetWindow(wxWindow *win)
164 {
165 Remove();
166
167 m_window = win;
168
169 if ( m_window )
170 {
171 wxToolInfo ti(m_window);
172
173 // as we store our text anyhow, it seems useless to waste system memory
174 // by asking the tooltip ctrl to remember it too - instead it will send
175 // us TTN_NEEDTEXT (via WM_NOTIFY) when it is about to be shown
176 ti.hwnd = (HWND)m_window->GetHWND();
177 ti.lpszText = LPSTR_TEXTCALLBACK;
178 // instead of: ti.lpszText = (char *)m_text.c_str();
179
180 if ( !SendTooltipMessage(GetToolTipCtrl(), TTM_ADDTOOL, 0, &ti) )
181 {
182 wxLogSysError(_("Failed to create the tooltip '%s'"),
183 m_text.c_str());
184 }
185 }
186 }
187
188 void wxToolTip::SetTip(const wxString& tip)
189 {
190 m_text = tip;
191
192 if ( m_window )
193 {
194 // update it immediately
195 wxToolInfo ti(m_window);
196 ti.lpszText = (char *)m_text.c_str();
197
198 (void)SendTooltipMessage(GetToolTipCtrl(), TTM_UPDATETIPTEXT, 0, &ti);
199 }
200 }
201
202 #endif // wxUSE_TOOLTIPS