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