]> git.saurik.com Git - wxWidgets.git/blame - src/msw/helpchm.cpp
fixed wide char codeset detection for systems which do support LE/BE variants (broken...
[wxWidgets.git] / src / msw / helpchm.cpp
CommitLineData
f6bcfd97
BP
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
65571936 9// Licence: wxWindows licence
f6bcfd97
BP
10/////////////////////////////////////////////////////////////////////////////
11
f6bcfd97
BP
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
3295e238 16 #pragma hdrstop
f6bcfd97
BP
17#endif
18
3295e238 19#if wxUSE_HELP && wxUSE_MS_HTML_HELP && defined(__WIN95__)
f6bcfd97
BP
20
21#include "wx/filefn.h"
f6bcfd97 22#include "wx/msw/helpchm.h"
0b9ab0bd 23
397cfa3a 24#include "wx/dynload.h"
0b9ab0bd 25
9fcaaedd
VS
26#ifndef WX_PRECOMP
27 #include "wx/intl.h"
28 #include "wx/app.h"
29#endif
30
f6bcfd97 31#include "wx/msw/private.h"
92199f4c 32#include "wx/msw/htmlhelp.h"
3295e238
VZ
33
34// ----------------------------------------------------------------------------
638a0b2c
VS
35// utility functions to manage the loading/unloading
36// of hhctrl.ocx
3295e238
VZ
37// ----------------------------------------------------------------------------
38
638a0b2c 39#ifndef UNICODE
3295e238 40 typedef HWND ( WINAPI * HTMLHELP )( HWND, LPCSTR, UINT, DWORD );
2b5f62a0 41 #define HTMLHELP_NAME wxT("HtmlHelpA")
3295e238
VZ
42#else // ANSI
43 typedef HWND ( WINAPI * HTMLHELP )( HWND, LPCWSTR, UINT, DWORD );
2b5f62a0 44 #define HTMLHELP_NAME wxT("HtmlHelpW")
638a0b2c 45#endif
3295e238 46
4f89dbc4 47// dll symbol handle
638a0b2c 48static HTMLHELP gs_htmlHelp = 0;
638a0b2c
VS
49
50static bool LoadHtmlHelpLibrary()
51{
cd560c96 52 wxPluginLibrary *lib = wxPluginManager::LoadLibrary( _T("HHCTRL.OCX"), wxDL_DEFAULT | wxDL_VERBATIM );
638a0b2c 53
cd560c96 54 if( !lib )
4f89dbc4
RL
55 {
56 wxLogError(_("MS HTML Help functions are unavailable because the MS HTML Help library is not installed on this machine. Please install it."));
59af881e 57 return false;
638a0b2c
VS
58 }
59 else
60 {
cd560c96 61 gs_htmlHelp = (HTMLHELP)lib->GetSymbol( HTMLHELP_NAME );
4f89dbc4
RL
62
63 if( !gs_htmlHelp )
64 {
65 wxLogError(_("Failed to initialize MS HTML Help."));
66
cd560c96 67 lib->UnrefLib();
59af881e 68 return false ;
4f89dbc4 69 }
638a0b2c
VS
70 }
71
59af881e 72 return true;
638a0b2c
VS
73}
74
75static void UnloadHtmlHelpLibrary()
76{
cd560c96 77 if ( gs_htmlHelp )
c8200b5a 78 {
1be9536a
JS
79 if (wxPluginManager::UnloadLibrary( _T("HHCTRL.OCX") ))
80 gs_htmlHelp = 0;
638a0b2c
VS
81 }
82}
83
f6bcfd97
BP
84static HWND GetSuitableHWND()
85{
86 if (wxTheApp->GetTopWindow())
87 return (HWND) wxTheApp->GetTopWindow()->GetHWND();
88 else
89 return GetDesktopWindow();
90}
91
92IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController, wxHelpControllerBase)
93
94bool wxCHMHelpController::Initialize(const wxString& filename)
95{
638a0b2c
VS
96 // warn on failure
97 if( !LoadHtmlHelpLibrary() )
59af881e 98 return false;
638a0b2c 99
f6bcfd97 100 m_helpFile = filename;
59af881e 101 return true;
f6bcfd97
BP
102}
103
104bool wxCHMHelpController::LoadFile(const wxString& file)
105{
106 if (!file.IsEmpty())
107 m_helpFile = file;
59af881e 108 return true;
f6bcfd97
BP
109}
110
111bool wxCHMHelpController::DisplayContents()
112{
59af881e 113 if (m_helpFile.IsEmpty()) return false;
f6bcfd97
BP
114
115 wxString str = GetValidFilename(m_helpFile);
116
3295e238 117 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_DISPLAY_TOPIC, 0L);
59af881e 118 return true;
f6bcfd97
BP
119}
120
121// Use topic or HTML filename
122bool wxCHMHelpController::DisplaySection(const wxString& section)
123{
59af881e 124 if (m_helpFile.IsEmpty()) return false;
f6bcfd97
BP
125
126 wxString str = GetValidFilename(m_helpFile);
127
128 // Is this an HTML file or a keyword?
59af881e 129 bool isFilename = (section.Find(wxT(".htm")) != wxNOT_FOUND);
f6bcfd97
BP
130
131 if (isFilename)
638a0b2c 132 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_DISPLAY_TOPIC, (DWORD) (const wxChar*) section);
f6bcfd97
BP
133 else
134 KeywordSearch(section);
59af881e 135 return true;
f6bcfd97
BP
136}
137
138// Use context number
139bool wxCHMHelpController::DisplaySection(int section)
140{
59af881e 141 if (m_helpFile.IsEmpty()) return false;
f6bcfd97
BP
142
143 wxString str = GetValidFilename(m_helpFile);
144
638a0b2c 145 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_HELP_CONTEXT, (DWORD)section);
59af881e 146 return true;
f6bcfd97
BP
147}
148
5100cabf
JS
149bool wxCHMHelpController::DisplayContextPopup(int contextId)
150{
59af881e 151 if (m_helpFile.IsEmpty()) return false;
5100cabf
JS
152
153 wxString str = GetValidFilename(m_helpFile);
154
a1b8f548
JS
155 // We also have to specify the popups file (default is cshelp.txt).
156 // str += wxT("::/cshelp.txt");
157
5100cabf
JS
158 HH_POPUP popup;
159 popup.cbStruct = sizeof(popup);
160 popup.hinst = (HINSTANCE) wxGetInstance();
161 popup.idString = contextId ;
162
163 GetCursorPos(& popup.pt);
5438a566
VZ
164 popup.clrForeground = (COLORREF)-1;
165 popup.clrBackground = (COLORREF)-1;
5100cabf
JS
166 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
167 popup.pszFont = NULL;
168 popup.pszText = NULL;
169
638a0b2c 170 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_DISPLAY_TEXT_POPUP, (DWORD) & popup);
59af881e 171 return true;
5100cabf
JS
172}
173
174bool 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;
5438a566
VZ
181 popup.clrForeground = (COLORREF)-1;
182 popup.clrBackground = (COLORREF)-1;
5100cabf
JS
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
638a0b2c 187 gs_htmlHelp(GetSuitableHWND(), NULL, HH_DISPLAY_TEXT_POPUP, (DWORD) & popup);
59af881e 188 return true;
5100cabf
JS
189}
190
f6bcfd97
BP
191bool wxCHMHelpController::DisplayBlock(long block)
192{
193 return DisplaySection(block);
194}
195
69b5cec2
VS
196bool wxCHMHelpController::KeywordSearch(const wxString& k,
197 wxHelpSearchMode WXUNUSED(mode))
f6bcfd97 198{
59af881e 199 if (m_helpFile.IsEmpty()) return false;
f6bcfd97
BP
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
638a0b2c 213 gs_htmlHelp(GetSuitableHWND(), (const wxChar*) str, HH_KEYWORD_LOOKUP, (DWORD)& link);
59af881e 214 return true;
f6bcfd97
BP
215}
216
217bool wxCHMHelpController::Quit()
218{
638a0b2c 219 gs_htmlHelp(GetSuitableHWND(), 0, HH_CLOSE_ALL, 0L);
638a0b2c 220
59af881e 221 return true;
f6bcfd97
BP
222}
223
224// Append extension if necessary.
225wxString 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
3295e238
VZ
240wxCHMHelpController::~wxCHMHelpController()
241{
242 UnloadHtmlHelpLibrary();
243}
244
f6bcfd97 245#endif // wxUSE_HELP
4f89dbc4 246