]> git.saurik.com Git - wxWidgets.git/blob - src/msw/helpchm.cpp
[this message is for the previous commit as well]
[wxWidgets.git] / src / msw / helpchm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: helpchm.cpp
3 // Purpose: Help system: MS HTML Help implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 16/04/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "helpchm.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_HELP && wxUSE_MS_HTML_HELP && defined(__WIN95__)
24
25 #include "wx/filefn.h"
26 #include "wx/msw/helpchm.h"
27
28 #include "wx/dynlib.h"
29
30 #include "wx/msw/private.h"
31
32 // instead of including htmlhelp.h, duplicate the things from it we need here
33
34 enum
35 {
36 HH_DISPLAY_TOPIC,
37 HH_DISPLAY_TOC,
38 HH_DISPLAY_INDEX,
39 HH_DISPLAY_SEARCH,
40 HH_SET_WIN_TYPE,
41 HH_GET_WIN_TYPE,
42 HH_GET_WIN_HANDLE,
43 HH_ENUM_INFO_TYPE,
44 HH_SET_INFO_TYPE,
45 HH_SYNC,
46 HH_RESERVED1,
47 HH_RESERVED2,
48 HH_RESERVED3,
49 HH_KEYWORD_LOOKUP,
50 HH_DISPLAY_TEXT_POPUP,
51 HH_HELP_CONTEXT,
52 HH_TP_HELP_CONTEXTMENU,
53 HH_TP_HELP_WM_HELP,
54 HH_CLOSE_ALL,
55 HH_ALINK_LOOKUP,
56 HH_GET_LAST_ERROR,
57 HH_ENUM_CATEGORY,
58 HH_ENUM_CATEGORY_IT,
59 HH_RESET_IT_FILTER,
60 HH_SET_INCLUSIVE_FILTER,
61 HH_SET_EXCLUSIVE_FILTER
62 };
63
64 struct HH_POPUP
65 {
66 int cbStruct;
67 HINSTANCE hinst;
68 UINT idString;
69 LPCTSTR pszText;
70 POINT pt;
71 COLORREF clrForeground;
72 COLORREF clrBackground;
73 RECT rcMargins;
74 LPCTSTR pszFont;
75 };
76
77 struct HH_AKLINK
78 {
79 int cbStruct;
80 BOOL fReserved;
81 LPCTSTR pszKeywords;
82 LPCTSTR pszUrl;
83 LPCTSTR pszMsgText;
84 LPCTSTR pszMsgTitle;
85 LPCTSTR pszWindow;
86 BOOL fIndexOnFail;
87 };
88
89 // ----------------------------------------------------------------------------
90 // utility functions to manage the loading/unloading
91 // of hhctrl.ocx
92 // ----------------------------------------------------------------------------
93
94 #ifndef UNICODE
95 typedef HWND ( WINAPI * HTMLHELP )( HWND, LPCSTR, UINT, DWORD );
96 #define HTMLHELP_NAME "HtmlHelpA"
97 #else // ANSI
98 typedef HWND ( WINAPI * HTMLHELP )( HWND, LPCWSTR, UINT, DWORD );
99 #define HTMLHELP_NAME "HtmlHelpW"
100 #endif
101
102 // dll symbol handle
103 static HTMLHELP gs_htmlHelp = 0;
104
105 static bool LoadHtmlHelpLibrary()
106 {
107 wxPluginLibrary *lib = wxPluginManager::LoadLibrary( _T("HHCTRL.OCX"), wxDL_DEFAULT | wxDL_VERBATIM );
108
109 if( !lib )
110 {
111 wxLogError(_("MS HTML Help functions are unavailable because the MS HTML Help library is not installed on this machine. Please install it."));
112 return FALSE;
113 }
114 else
115 {
116 gs_htmlHelp = (HTMLHELP)lib->GetSymbol( HTMLHELP_NAME );
117
118 if( !gs_htmlHelp )
119 {
120 wxLogError(_("Failed to initialize MS HTML Help."));
121
122 lib->UnrefLib();
123 return FALSE ;
124 }
125 }
126
127 return TRUE;
128 }
129
130 static void UnloadHtmlHelpLibrary()
131 {
132 if ( gs_htmlHelp )
133 {
134 wxPluginManager::UnloadLibrary( _T("HHCTRL.OCX") );
135
136 gs_htmlHelp = 0;
137 }
138 }
139
140 static HWND GetSuitableHWND()
141 {
142 if (wxTheApp->GetTopWindow())
143 return (HWND) wxTheApp->GetTopWindow()->GetHWND();
144 else
145 return GetDesktopWindow();
146 }
147
148 IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController, wxHelpControllerBase)
149
150 bool wxCHMHelpController::Initialize(const wxString& filename)
151 {
152 // warn on failure
153 if( !LoadHtmlHelpLibrary() )
154 return FALSE;
155
156 m_helpFile = filename;
157 return TRUE;
158 }
159
160 bool wxCHMHelpController::LoadFile(const wxString& file)
161 {
162 if (!file.IsEmpty())
163 m_helpFile = file;
164 return TRUE;
165 }
166
167 bool wxCHMHelpController::DisplayContents()
168 {
169 if (m_helpFile.IsEmpty()) return FALSE;
170
171 wxString str = GetValidFilename(m_helpFile);
172
173 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_DISPLAY_TOPIC, 0L);
174 return TRUE;
175 }
176
177 // Use topic or HTML filename
178 bool wxCHMHelpController::DisplaySection(const wxString& section)
179 {
180 if (m_helpFile.IsEmpty()) return FALSE;
181
182 wxString str = GetValidFilename(m_helpFile);
183
184 // Is this an HTML file or a keyword?
185 bool isFilename = (section.Find(wxT(".htm")) != -1);
186
187 if (isFilename)
188 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_DISPLAY_TOPIC, (DWORD) (const wxChar*) section);
189 else
190 KeywordSearch(section);
191 return TRUE;
192 }
193
194 // Use context number
195 bool wxCHMHelpController::DisplaySection(int section)
196 {
197 if (m_helpFile.IsEmpty()) return FALSE;
198
199 wxString str = GetValidFilename(m_helpFile);
200
201 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_HELP_CONTEXT, (DWORD)section);
202 return TRUE;
203 }
204
205 bool wxCHMHelpController::DisplayContextPopup(int contextId)
206 {
207 if (m_helpFile.IsEmpty()) return FALSE;
208
209 wxString str = GetValidFilename(m_helpFile);
210
211 // We also have to specify the popups file (default is cshelp.txt).
212 // str += wxT("::/cshelp.txt");
213
214 HH_POPUP popup;
215 popup.cbStruct = sizeof(popup);
216 popup.hinst = (HINSTANCE) wxGetInstance();
217 popup.idString = contextId ;
218
219 GetCursorPos(& popup.pt);
220 popup.clrForeground = (COLORREF)-1;
221 popup.clrBackground = (COLORREF)-1;
222 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
223 popup.pszFont = NULL;
224 popup.pszText = NULL;
225
226 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_DISPLAY_TEXT_POPUP, (DWORD) & popup);
227 return TRUE;
228 }
229
230 bool wxCHMHelpController::DisplayTextPopup(const wxString& text, const wxPoint& pos)
231 {
232 HH_POPUP popup;
233 popup.cbStruct = sizeof(popup);
234 popup.hinst = (HINSTANCE) wxGetInstance();
235 popup.idString = 0 ;
236 popup.pt.x = pos.x; popup.pt.y = pos.y;
237 popup.clrForeground = (COLORREF)-1;
238 popup.clrBackground = (COLORREF)-1;
239 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
240 popup.pszFont = NULL;
241 popup.pszText = (const wxChar*) text;
242
243 gs_htmlHelp(GetSuitableHWND(), NULL, HH_DISPLAY_TEXT_POPUP, (DWORD) & popup);
244 return TRUE;
245 }
246
247 bool wxCHMHelpController::DisplayBlock(long block)
248 {
249 return DisplaySection(block);
250 }
251
252 bool wxCHMHelpController::KeywordSearch(const wxString& k)
253 {
254 if (m_helpFile.IsEmpty()) return FALSE;
255
256 wxString str = GetValidFilename(m_helpFile);
257
258 HH_AKLINK link;
259 link.cbStruct = sizeof(HH_AKLINK) ;
260 link.fReserved = FALSE ;
261 link.pszKeywords = k.c_str() ;
262 link.pszUrl = NULL ;
263 link.pszMsgText = NULL ;
264 link.pszMsgTitle = NULL ;
265 link.pszWindow = NULL ;
266 link.fIndexOnFail = TRUE ;
267
268 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_KEYWORD_LOOKUP, (DWORD)& link);
269 return TRUE;
270 }
271
272 bool wxCHMHelpController::Quit()
273 {
274 gs_htmlHelp(GetSuitableHWND(), 0, HH_CLOSE_ALL, 0L);
275
276 return TRUE;
277 }
278
279 // Append extension if necessary.
280 wxString wxCHMHelpController::GetValidFilename(const wxString& file) const
281 {
282 wxString path, name, ext;
283 wxSplitPath(file, & path, & name, & ext);
284
285 wxString fullName;
286 if (path.IsEmpty())
287 fullName = name + wxT(".chm");
288 else if (path.Last() == wxT('\\'))
289 fullName = path + name + wxT(".chm");
290 else
291 fullName = path + wxT("\\") + name + wxT(".chm");
292 return fullName;
293 }
294
295 wxCHMHelpController::~wxCHMHelpController()
296 {
297 UnloadHtmlHelpLibrary();
298 }
299
300 #endif // wxUSE_HELP
301