]> git.saurik.com Git - wxWidgets.git/blob - src/msw/helpchm.cpp
Fixes and XCode 2.3 update
[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:
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
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(wxCHMHelpController* controller)
85 {
86 if (controller->GetParentWindow())
87 return (HWND) controller->GetParentWindow()->GetHWND();
88 else if (wxTheApp->GetTopWindow())
89 return (HWND) wxTheApp->GetTopWindow()->GetHWND();
90 else
91 return GetDesktopWindow();
92 }
93
94 IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController, wxHelpControllerBase)
95
96 bool wxCHMHelpController::Initialize(const wxString& filename)
97 {
98 // warn on failure
99 if( !LoadHtmlHelpLibrary() )
100 return false;
101
102 m_helpFile = filename;
103 return true;
104 }
105
106 bool wxCHMHelpController::LoadFile(const wxString& file)
107 {
108 if (!file.IsEmpty())
109 m_helpFile = file;
110 return true;
111 }
112
113 bool wxCHMHelpController::DisplayContents()
114 {
115 if (m_helpFile.IsEmpty()) return false;
116
117 wxString str = GetValidFilename(m_helpFile);
118
119 gs_htmlHelp(GetSuitableHWND(this), (const wxChar*) str, HH_DISPLAY_TOPIC, 0L);
120 return true;
121 }
122
123 // Use topic or HTML filename
124 bool wxCHMHelpController::DisplaySection(const wxString& section)
125 {
126 if (m_helpFile.IsEmpty()) return false;
127
128 wxString str = GetValidFilename(m_helpFile);
129
130 // Is this an HTML file or a keyword?
131 bool isFilename = (section.Find(wxT(".htm")) != wxNOT_FOUND);
132
133 if (isFilename)
134 gs_htmlHelp(GetSuitableHWND(this), (const wxChar*) str, HH_DISPLAY_TOPIC, (DWORD) (const wxChar*) section);
135 else
136 KeywordSearch(section);
137 return true;
138 }
139
140 // Use context number
141 bool wxCHMHelpController::DisplaySection(int section)
142 {
143 if (m_helpFile.IsEmpty()) return false;
144
145 wxString str = GetValidFilename(m_helpFile);
146
147 gs_htmlHelp(GetSuitableHWND(this), (const wxChar*) str, HH_HELP_CONTEXT, (DWORD)section);
148 return true;
149 }
150
151 bool wxCHMHelpController::DisplayContextPopup(int contextId)
152 {
153 if (m_helpFile.IsEmpty()) return false;
154
155 wxString str = GetValidFilename(m_helpFile);
156
157 // We also have to specify the popups file (default is cshelp.txt).
158 // str += wxT("::/cshelp.txt");
159
160 HH_POPUP popup;
161 popup.cbStruct = sizeof(popup);
162 popup.hinst = (HINSTANCE) wxGetInstance();
163 popup.idString = contextId ;
164
165 GetCursorPos(& popup.pt);
166 popup.clrForeground = (COLORREF)-1;
167 popup.clrBackground = (COLORREF)-1;
168 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
169 popup.pszFont = NULL;
170 popup.pszText = NULL;
171
172 gs_htmlHelp(GetSuitableHWND(this), (const wxChar*) str, HH_DISPLAY_TEXT_POPUP, (DWORD) & popup);
173 return true;
174 }
175
176 bool wxCHMHelpController::DisplayTextPopup(const wxString& text, const wxPoint& pos)
177 {
178 HH_POPUP popup;
179 popup.cbStruct = sizeof(popup);
180 popup.hinst = (HINSTANCE) wxGetInstance();
181 popup.idString = 0 ;
182 popup.pt.x = pos.x; popup.pt.y = pos.y;
183 popup.clrForeground = (COLORREF)-1;
184 popup.clrBackground = (COLORREF)-1;
185 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
186 popup.pszFont = NULL;
187 popup.pszText = (const wxChar*) text;
188
189 gs_htmlHelp(GetSuitableHWND(this), NULL, HH_DISPLAY_TEXT_POPUP, (DWORD) & popup);
190 return true;
191 }
192
193 bool wxCHMHelpController::DisplayBlock(long block)
194 {
195 return DisplaySection(block);
196 }
197
198 bool wxCHMHelpController::KeywordSearch(const wxString& k,
199 wxHelpSearchMode WXUNUSED(mode))
200 {
201 if (m_helpFile.IsEmpty()) return false;
202
203 wxString str = GetValidFilename(m_helpFile);
204
205 HH_AKLINK link;
206 link.cbStruct = sizeof(HH_AKLINK) ;
207 link.fReserved = FALSE ;
208 link.pszKeywords = k.c_str() ;
209 link.pszUrl = NULL ;
210 link.pszMsgText = NULL ;
211 link.pszMsgTitle = NULL ;
212 link.pszWindow = NULL ;
213 link.fIndexOnFail = TRUE ;
214
215 gs_htmlHelp(GetSuitableHWND(this), (const wxChar*) str, HH_KEYWORD_LOOKUP, (DWORD)& link);
216 return true;
217 }
218
219 bool wxCHMHelpController::Quit()
220 {
221 gs_htmlHelp(GetSuitableHWND(this), 0, HH_CLOSE_ALL, 0L);
222
223 return true;
224 }
225
226 // Append extension if necessary.
227 wxString wxCHMHelpController::GetValidFilename(const wxString& file) const
228 {
229 wxString path, name, ext;
230 wxSplitPath(file, & path, & name, & ext);
231
232 wxString fullName;
233 if (path.IsEmpty())
234 fullName = name + wxT(".chm");
235 else if (path.Last() == wxT('\\'))
236 fullName = path + name + wxT(".chm");
237 else
238 fullName = path + wxT("\\") + name + wxT(".chm");
239 return fullName;
240 }
241
242 wxCHMHelpController::~wxCHMHelpController()
243 {
244 UnloadHtmlHelpLibrary();
245 }
246
247 #endif // wxUSE_HELP