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