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