Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / msw / helpchm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/helpchm.cpp
3 // Purpose: Help system: MS HTML Help implementation
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin at 2008-03-01: refactoring, simplification
6 // Created: 16/04/2000
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_HELP && wxUSE_MS_HTML_HELP
19
20 #include "wx/filename.h"
21 #include "wx/msw/helpchm.h"
22
23 #include "wx/dynload.h"
24
25 #ifndef WX_PRECOMP
26 #include "wx/intl.h"
27 #include "wx/app.h"
28 #endif
29
30 #include "wx/msw/private.h"
31 #include "wx/msw/htmlhelp.h"
32
33 // ----------------------------------------------------------------------------
34 // utility functions to manage the loading/unloading
35 // of hhctrl.ocx
36 // ----------------------------------------------------------------------------
37
38 #ifndef UNICODE
39 typedef HWND ( WINAPI * HTMLHELP )( HWND, LPCSTR, UINT, DWORD );
40 #define HTMLHELP_NAME wxT("HtmlHelpA")
41 #else // ANSI
42 typedef HWND ( WINAPI * HTMLHELP )( HWND, LPCWSTR, UINT, DWORD );
43 #define HTMLHELP_NAME wxT("HtmlHelpW")
44 #endif
45
46 HTMLHELP GetHtmlHelpFunction()
47 {
48 static HTMLHELP s_htmlHelp = NULL;
49
50 if ( !s_htmlHelp )
51 {
52 static wxDynamicLibrary s_dllHtmlHelp(wxT("HHCTRL.OCX"), wxDL_VERBATIM);
53
54 if ( !s_dllHtmlHelp.IsLoaded() )
55 {
56 wxLogError(_("MS HTML Help functions are unavailable because the MS HTML Help library is not installed on this machine. Please install it."));
57 }
58 else
59 {
60 s_htmlHelp = (HTMLHELP)s_dllHtmlHelp.GetSymbol(HTMLHELP_NAME);
61 if ( !s_htmlHelp )
62 {
63 wxLogError(_("Failed to initialize MS HTML Help."));
64 }
65 }
66 }
67
68 return s_htmlHelp;
69 }
70
71 // find the window to use in HtmlHelp() call: use the given one by default but
72 // fall back to the top level app window and then the desktop if it's NULL
73 static HWND GetSuitableHWND(wxWindow *win)
74 {
75 if ( !win && wxTheApp )
76 win = wxTheApp->GetTopWindow();
77
78 return win ? GetHwndOf(win) : ::GetDesktopWindow();
79 }
80
81
82 IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController, wxHelpControllerBase)
83
84 bool wxCHMHelpController::Initialize(const wxString& filename)
85 {
86 if ( !GetHtmlHelpFunction() )
87 return false;
88
89 m_helpFile = filename;
90 return true;
91 }
92
93 bool wxCHMHelpController::LoadFile(const wxString& file)
94 {
95 if (!file.IsEmpty())
96 m_helpFile = file;
97 return true;
98 }
99
100 /* static */ bool
101 wxCHMHelpController::CallHtmlHelp(wxWindow *win,
102 const wxChar *str,
103 unsigned cmd,
104 WXWPARAM param)
105 {
106 HTMLHELP htmlHelp = GetHtmlHelpFunction();
107
108 return htmlHelp && htmlHelp(GetSuitableHWND(win), str, cmd, param);
109 }
110
111 bool wxCHMHelpController::DisplayContents()
112 {
113 if (m_helpFile.IsEmpty())
114 return false;
115
116 return CallHtmlHelp(HH_DISPLAY_TOPIC);
117 }
118
119 // Use topic or HTML filename
120 bool wxCHMHelpController::DisplaySection(const wxString& section)
121 {
122 if (m_helpFile.IsEmpty())
123 return false;
124
125 // Is this an HTML file or a keyword?
126 if ( section.Find(wxT(".htm")) != wxNOT_FOUND )
127 {
128 // interpret as a file name
129 return CallHtmlHelp(HH_DISPLAY_TOPIC, wxMSW_CONV_LPCTSTR(section));
130 }
131
132 return KeywordSearch(section);
133 }
134
135 // Use context number
136 bool wxCHMHelpController::DisplaySection(int section)
137 {
138 if (m_helpFile.IsEmpty())
139 return false;
140
141 return CallHtmlHelp(HH_HELP_CONTEXT, section);
142 }
143
144 /* static */
145 bool
146 wxCHMHelpController::DoDisplayTextPopup(const wxChar *text,
147 const wxPoint& pos,
148 int contextId,
149 wxWindow *window)
150 {
151 HH_POPUP popup;
152 popup.cbStruct = sizeof(popup);
153 popup.hinst = (HINSTANCE) wxGetInstance();
154 popup.idString = contextId;
155 popup.pszText = text;
156 popup.pt.x = pos.x;
157 popup.pt.y = pos.y;
158 popup.clrForeground = ::GetSysColor(COLOR_INFOTEXT);
159 popup.clrBackground = ::GetSysColor(COLOR_INFOBK);
160 popup.rcMargins.top =
161 popup.rcMargins.left =
162 popup.rcMargins.right =
163 popup.rcMargins.bottom = -1;
164 popup.pszFont = NULL;
165
166 return CallHtmlHelp(window, NULL, HH_DISPLAY_TEXT_POPUP, &popup);
167 }
168
169 bool wxCHMHelpController::DisplayContextPopup(int contextId)
170 {
171 return DoDisplayTextPopup(NULL, wxGetMousePosition(), contextId,
172 GetParentWindow());
173 }
174
175 bool
176 wxCHMHelpController::DisplayTextPopup(const wxString& text, const wxPoint& pos)
177 {
178 return ShowContextHelpPopup(text, pos, GetParentWindow());
179 }
180
181 /* static */
182 bool wxCHMHelpController::ShowContextHelpPopup(const wxString& text,
183 const wxPoint& pos,
184 wxWindow *window)
185 {
186 return DoDisplayTextPopup(text.t_str(), pos, 0, window);
187 }
188
189 bool wxCHMHelpController::DisplayBlock(long block)
190 {
191 return DisplaySection(block);
192 }
193
194 bool wxCHMHelpController::KeywordSearch(const wxString& k,
195 wxHelpSearchMode WXUNUSED(mode))
196 {
197 if (m_helpFile.IsEmpty())
198 return false;
199
200 HH_AKLINK link;
201 link.cbStruct = sizeof(HH_AKLINK);
202 link.fReserved = FALSE;
203 link.pszKeywords = k.t_str();
204 link.pszUrl = NULL;
205 link.pszMsgText = NULL;
206 link.pszMsgTitle = NULL;
207 link.pszWindow = NULL;
208 link.fIndexOnFail = TRUE;
209
210 return CallHtmlHelp(HH_KEYWORD_LOOKUP, &link);
211 }
212
213 bool wxCHMHelpController::Quit()
214 {
215 return CallHtmlHelp(NULL, NULL, HH_CLOSE_ALL);
216 }
217
218 wxString wxCHMHelpController::GetValidFilename() const
219 {
220 wxString path, name, ext;
221 wxFileName::SplitPath(m_helpFile, &path, &name, &ext);
222
223 wxString fullName;
224 if (path.IsEmpty())
225 fullName = name + wxT(".chm");
226 else if (path.Last() == wxT('\\'))
227 fullName = path + name + wxT(".chm");
228 else
229 fullName = path + wxT("\\") + name + wxT(".chm");
230 return fullName;
231 }
232
233 #endif // wxUSE_HELP