Added wxRichToolTip class.
[wxWidgets.git] / src / msw / richtooltip.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/richtooltip.cpp
3 // Purpose: Native MSW implementation of wxRichToolTip.
4 // Author: Vadim Zeitlin
5 // Created: 2011-10-18
6 // RCS-ID: $Id: wxhead.cpp,v 1.11 2010-04-22 12:44:51 zeitlin Exp $
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_RICHTOOLTIP
27
28 #ifndef WX_PRECOMP
29 #endif // WX_PRECOMP
30
31 #include "wx/private/richtooltip.h"
32 #include "wx/generic/private/richtooltip.h"
33 #include "wx/msw/private.h"
34 #include "wx/msw/uxtheme.h"
35
36 // ============================================================================
37 // wxRichToolTipMSWImpl: the real implementation.
38 // ============================================================================
39
40 class wxRichToolTipMSWImpl : public wxRichToolTipGenericImpl
41 {
42 public:
43 wxRichToolTipMSWImpl(const wxString& title, const wxString& message) :
44 wxRichToolTipGenericImpl(title, message)
45 {
46 // So far so good...
47 m_canUseNative = true;
48
49 m_ttiIcon = TTI_NONE;
50 }
51
52 virtual void SetBackgroundColour(const wxColour& col,
53 const wxColour& colEnd)
54 {
55 // Setting background colour is not supported neither.
56 m_canUseNative = false;
57
58 wxRichToolTipGenericImpl::SetBackgroundColour(col, colEnd);
59 }
60
61 virtual void SetCustomIcon(const wxIcon& icon)
62 {
63 // Custom icons are not supported by EM_SHOWBALLOONTIP.
64 m_canUseNative = false;
65
66 wxRichToolTipGenericImpl::SetCustomIcon(icon);
67 }
68
69 virtual void SetStandardIcon(int icon)
70 {
71 wxRichToolTipGenericImpl::SetStandardIcon(icon);
72 if ( !m_canUseNative )
73 return;
74
75 switch ( icon & wxICON_MASK )
76 {
77 case wxICON_WARNING:
78 m_ttiIcon = TTI_WARNING;
79 break;
80
81 case wxICON_ERROR:
82 m_ttiIcon = TTI_ERROR;
83 break;
84
85 case wxICON_INFORMATION:
86 m_ttiIcon = TTI_INFO;
87 break;
88
89 case wxICON_QUESTION:
90 wxFAIL_MSG("Question icon doesn't make sense for a tooltip");
91 break;
92
93 case wxICON_NONE:
94 m_ttiIcon = TTI_NONE;
95 break;
96 }
97 }
98
99 virtual void SetTimeout(unsigned milliseconds)
100 {
101 // We don't support changing the timeout (maybe TTM_SETDELAYTIME could
102 // be used for this?).
103 m_canUseNative = false;
104
105 wxRichToolTipGenericImpl::SetTimeout(milliseconds);
106 }
107
108 virtual void SetTipKind(wxTipKind tipKind)
109 {
110 // Setting non-default tip is not supported.
111 if ( tipKind != wxTipKind_Auto )
112 m_canUseNative = false;
113
114 wxRichToolTipGenericImpl::SetTipKind(tipKind);
115 }
116
117 virtual void SetTitleFont(const wxFont& font)
118 {
119 // Setting non-default font is not supported.
120 m_canUseNative = false;
121
122 wxRichToolTipGenericImpl::SetTitleFont(font);
123 }
124
125 virtual void ShowFor(wxWindow* win)
126 {
127 // TODO: We could use native tooltip control to show native balloon
128 // tooltips for any window but right now we use the simple
129 // EM_SHOWBALLOONTIP API which can only be used with text
130 // controls.
131 if ( m_canUseNative )
132 {
133 wxTextCtrl* const text = wxDynamicCast(win, wxTextCtrl);
134 if ( text )
135 {
136 EDITBALLOONTIP ebt;
137 ebt.cbStruct = sizeof(EDITBALLOONTIP);
138 ebt.pszTitle = m_title.wc_str();
139 ebt.pszText = m_message.wc_str();
140 ebt.ttiIcon = m_ttiIcon;
141 if ( Edit_ShowBalloonTip(GetHwndOf(text), &ebt) )
142 return;
143 }
144 }
145
146 // Don't set m_canUseNative to false here, we could be able to use the
147 // native tooltips if we're called for a different window the next
148 // time.
149 wxRichToolTipGenericImpl::ShowFor(win);
150 }
151
152 private:
153 // If this is false, we've been requested to do something that the native
154 // version doesn't support and so need to fall back to the generic one.
155 bool m_canUseNative;
156
157 // One of TTI_NONE, TTI_INFO, TTI_WARNING or TTI_ERROR.
158 int m_ttiIcon;
159 };
160
161 /* static */
162 wxRichToolTipImpl*
163 wxRichToolTipImpl::Create(const wxString& title, const wxString& message)
164 {
165 // EM_SHOWBALLOONTIP is only implemented by comctl32.dll v6 so don't even
166 // bother using the native implementation if we're not using themes.
167 if ( wxUxThemeEngine::GetIfActive() )
168 return new wxRichToolTipMSWImpl(title, message);
169
170 return new wxRichToolTipGenericImpl(title, message);
171 }
172
173 #endif // wxUSE_RICHTOOLTIP