]> git.saurik.com Git - wxWidgets.git/blob - src/msw/helpchm.cpp
fixed wxProgressDialog for ranges > 65535
[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 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #endif
26
27 #include "wx/filefn.h"
28
29 #if wxUSE_HELP && wxUSE_MS_HTML_HELP && defined(__WIN95__)
30 #include "wx/msw/helpchm.h"
31
32 #include "wx/dynlib.h"
33
34 #ifndef WX_PRECOMP
35 #include <windows.h>
36 #endif
37
38 // This is found in the HTML Help Workshop installation,
39 // along with htmlhelp.lib.
40 #include <htmlhelp.h>
41
42 #include <time.h>
43
44 #ifdef __WXMSW__
45 #include "wx/msw/private.h"
46 #endif
47
48 #include <string.h>
49
50 // utility functions to manage the loading/unloading
51 // of hhctrl.ocx
52 #ifndef UNICODE
53 typedef HWND ( WINAPI * HTMLHELP )( HWND, LPCSTR, UINT, DWORD );
54 #define HTMLHELP_NAME "HtmlHelpA"
55 #else
56 typedef HWND ( WINAPI * HTMLHELP )( HWND, LPCWSTR, UINT, DWORD );
57 #define HTMLHELP_NAME "HtmlHelpW"
58 #endif
59 // dll handle/reference count
60 static HTMLHELP gs_htmlHelp = 0;
61 static wxDllType gs_dllHandle = 0;
62 static int gs_dllCount = 0;
63
64 static bool LoadHtmlHelpLibrary()
65 {
66 if( !gs_dllCount )
67 {
68 gs_dllHandle = wxDllLoader::LoadLibrary( "hhctrl.ocx" );
69 if( !gs_dllHandle )
70 {
71 wxLogError(_("MS HTML Help functions are unavailable because the MS HTML Help library is not installed on this machine. Please install it."));
72 return FALSE;
73 }
74 else
75 {
76 gs_dllCount = 1;
77 gs_htmlHelp = (HTMLHELP)wxDllLoader::GetSymbol( gs_dllHandle, HTMLHELP_NAME );
78
79 if( !gs_htmlHelp )
80 {
81 wxLogError(_("Failed to initialize MS HTML Help."));
82
83 wxDllLoader::UnloadLibrary(gs_dllHandle);
84 gs_dllHandle = 0;
85 gs_dllCount = 0;
86
87 return FALSE ;
88 }
89 }
90 }
91 else
92 {
93 ++gs_dllCount;
94 }
95
96 return TRUE;
97 }
98
99 static void UnloadHtmlHelpLibrary()
100 {
101 if( gs_dllCount != 0 && !--gs_dllCount )
102 {
103 wxDllLoader::UnloadLibrary( gs_dllHandle );
104 gs_dllHandle = 0;
105 gs_htmlHelp = 0;
106 }
107 }
108
109 static HWND GetSuitableHWND()
110 {
111 if (wxTheApp->GetTopWindow())
112 return (HWND) wxTheApp->GetTopWindow()->GetHWND();
113 else
114 return GetDesktopWindow();
115 }
116
117 IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController, wxHelpControllerBase)
118
119 bool wxCHMHelpController::Initialize(const wxString& filename)
120 {
121 // warn on failure
122 if( !LoadHtmlHelpLibrary() )
123 return FALSE;
124
125 m_helpFile = filename;
126 return TRUE;
127 }
128
129 bool wxCHMHelpController::LoadFile(const wxString& file)
130 {
131 if (!file.IsEmpty())
132 m_helpFile = file;
133 return TRUE;
134 }
135
136 bool wxCHMHelpController::DisplayContents()
137 {
138 if (m_helpFile.IsEmpty()) return FALSE;
139
140 wxString str = GetValidFilename(m_helpFile);
141
142 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_HELP_FINDER, 0L);
143 return TRUE;
144 }
145
146 // Use topic or HTML filename
147 bool wxCHMHelpController::DisplaySection(const wxString& section)
148 {
149 if (m_helpFile.IsEmpty()) return FALSE;
150
151 wxString str = GetValidFilename(m_helpFile);
152
153 // Is this an HTML file or a keyword?
154 bool isFilename = (section.Find(wxT(".htm")) != -1);
155
156 if (isFilename)
157 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_DISPLAY_TOPIC, (DWORD) (const wxChar*) section);
158 else
159 KeywordSearch(section);
160 return TRUE;
161 }
162
163 // Use context number
164 bool wxCHMHelpController::DisplaySection(int section)
165 {
166 if (m_helpFile.IsEmpty()) return FALSE;
167
168 wxString str = GetValidFilename(m_helpFile);
169
170 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_HELP_CONTEXT, (DWORD)section);
171 return TRUE;
172 }
173
174 bool wxCHMHelpController::DisplayContextPopup(int contextId)
175 {
176 if (m_helpFile.IsEmpty()) return FALSE;
177
178 wxString str = GetValidFilename(m_helpFile);
179
180 // We also have to specify the popups file (default is cshelp.txt).
181 // str += wxT("::/cshelp.txt");
182
183 HH_POPUP popup;
184 popup.cbStruct = sizeof(popup);
185 popup.hinst = (HINSTANCE) wxGetInstance();
186 popup.idString = contextId ;
187
188 GetCursorPos(& popup.pt);
189 popup.clrForeground = (COLORREF)-1;
190 popup.clrBackground = (COLORREF)-1;
191 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
192 popup.pszFont = NULL;
193 popup.pszText = NULL;
194
195 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_DISPLAY_TEXT_POPUP, (DWORD) & popup);
196 return TRUE;
197 }
198
199 bool wxCHMHelpController::DisplayTextPopup(const wxString& text, const wxPoint& pos)
200 {
201 HH_POPUP popup;
202 popup.cbStruct = sizeof(popup);
203 popup.hinst = (HINSTANCE) wxGetInstance();
204 popup.idString = 0 ;
205 popup.pt.x = pos.x; popup.pt.y = pos.y;
206 popup.clrForeground = (COLORREF)-1;
207 popup.clrBackground = (COLORREF)-1;
208 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
209 popup.pszFont = NULL;
210 popup.pszText = (const wxChar*) text;
211
212 gs_htmlHelp(GetSuitableHWND(), NULL, HH_DISPLAY_TEXT_POPUP, (DWORD) & popup);
213 return TRUE;
214 }
215
216 bool wxCHMHelpController::DisplayBlock(long block)
217 {
218 return DisplaySection(block);
219 }
220
221 bool wxCHMHelpController::KeywordSearch(const wxString& k)
222 {
223 if (m_helpFile.IsEmpty()) return FALSE;
224
225 wxString str = GetValidFilename(m_helpFile);
226
227 HH_AKLINK link;
228 link.cbStruct = sizeof(HH_AKLINK) ;
229 link.fReserved = FALSE ;
230 link.pszKeywords = k.c_str() ;
231 link.pszUrl = NULL ;
232 link.pszMsgText = NULL ;
233 link.pszMsgTitle = NULL ;
234 link.pszWindow = NULL ;
235 link.fIndexOnFail = TRUE ;
236
237 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_KEYWORD_LOOKUP, (DWORD)& link);
238 return TRUE;
239 }
240
241 bool wxCHMHelpController::Quit()
242 {
243 gs_htmlHelp(GetSuitableHWND(), 0, HH_CLOSE_ALL, 0L);
244 UnloadHtmlHelpLibrary();
245
246 return TRUE;
247 }
248
249 // Append extension if necessary.
250 wxString wxCHMHelpController::GetValidFilename(const wxString& file) const
251 {
252 wxString path, name, ext;
253 wxSplitPath(file, & path, & name, & ext);
254
255 wxString fullName;
256 if (path.IsEmpty())
257 fullName = name + wxT(".chm");
258 else if (path.Last() == wxT('\\'))
259 fullName = path + name + wxT(".chm");
260 else
261 fullName = path + wxT("\\") + name + wxT(".chm");
262 return fullName;
263 }
264
265 #endif // wxUSE_HELP