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