]> git.saurik.com Git - wxWidgets.git/blame - src/msw/helpchm.cpp
GetSpacer returns a copy, not a reference to the size
[wxWidgets.git] / src / msw / helpchm.cpp
CommitLineData
f6bcfd97 1/////////////////////////////////////////////////////////////////////////////
a71d815b 2// Name: src/msw/helpchm.cpp
f6bcfd97
BP
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
a71d815b 19#if wxUSE_HELP && wxUSE_MS_HTML_HELP
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
392c5133 47HTMLHELP GetHtmlHelpFunction()
638a0b2c 48{
392c5133 49 static HTMLHELP s_htmlHelp = NULL;
638a0b2c 50
392c5133 51 if ( !s_htmlHelp )
638a0b2c 52 {
392c5133 53 static wxDynamicLibrary s_dllHtmlHelp(_T("HHCTRL.OCX"), wxDL_VERBATIM);
4f89dbc4 54
392c5133 55 if ( !s_dllHtmlHelp.IsLoaded() )
4f89dbc4 56 {
392c5133
VZ
57 wxLogError(_("MS HTML Help functions are unavailable because the MS HTML Help library is not installed on this machine. Please install it."));
58 }
59 else
60 {
61 s_htmlHelp = (HTMLHELP)s_dllHtmlHelp.GetSymbol(HTMLHELP_NAME);
62 if ( !s_htmlHelp )
63 {
64 wxLogError(_("Failed to initialize MS HTML Help."));
65 }
4f89dbc4 66 }
638a0b2c
VS
67 }
68
392c5133 69 return s_htmlHelp;
638a0b2c
VS
70}
71
392c5133
VZ
72// find the window to use in HtmlHelp() call: use the given one by default but
73// fall back to the top level app window and then the desktop if it's NULL
74static HWND GetSuitableHWND(wxWindow *win)
638a0b2c 75{
392c5133
VZ
76 if ( !win && wxTheApp )
77 win = wxTheApp->GetTopWindow();
78
79 return win ? GetHwndOf(win) : ::GetDesktopWindow();
638a0b2c
VS
80}
81
392c5133
VZ
82// wrap the real HtmlHelp() but just return false (and not crash) if it
83// couldn't be loaded
84//
85// it also takes a wxWindow instead of HWND
86static bool
87CallHtmlHelpFunction(wxWindow *win, const wxChar *str, UINT uint, DWORD dword)
f6bcfd97 88{
392c5133
VZ
89 HTMLHELP htmlHelp = GetHtmlHelpFunction();
90
91 return htmlHelp && htmlHelp(GetSuitableHWND(win), str, uint, dword);
f6bcfd97
BP
92}
93
94IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController, wxHelpControllerBase)
95
96bool wxCHMHelpController::Initialize(const wxString& filename)
97{
392c5133 98 if ( !GetHtmlHelpFunction() )
59af881e 99 return false;
638a0b2c 100
f6bcfd97 101 m_helpFile = filename;
59af881e 102 return true;
f6bcfd97
BP
103}
104
105bool wxCHMHelpController::LoadFile(const wxString& file)
106{
107 if (!file.IsEmpty())
108 m_helpFile = file;
59af881e 109 return true;
f6bcfd97
BP
110}
111
112bool wxCHMHelpController::DisplayContents()
113{
392c5133
VZ
114 if (m_helpFile.IsEmpty())
115 return false;
f6bcfd97
BP
116
117 wxString str = GetValidFilename(m_helpFile);
118
392c5133 119 return CallHtmlHelpFunction(GetParentWindow(), str, HH_DISPLAY_TOPIC, 0L);
f6bcfd97
BP
120}
121
122// Use topic or HTML filename
123bool wxCHMHelpController::DisplaySection(const wxString& section)
124{
392c5133
VZ
125 if (m_helpFile.IsEmpty())
126 return false;
f6bcfd97
BP
127
128 wxString str = GetValidFilename(m_helpFile);
129
130 // Is this an HTML file or a keyword?
392c5133
VZ
131 if ( section.Find(wxT(".htm")) != wxNOT_FOUND )
132 {
133 // interpret as a file name
134 return CallHtmlHelpFunction(GetParentWindow(), str, HH_DISPLAY_TOPIC,
135 wxPtrToUInt(section.c_str()));
136 }
f6bcfd97 137
392c5133 138 return KeywordSearch(section);
f6bcfd97
BP
139}
140
141// Use context number
142bool wxCHMHelpController::DisplaySection(int section)
143{
392c5133
VZ
144 if (m_helpFile.IsEmpty())
145 return false;
f6bcfd97
BP
146
147 wxString str = GetValidFilename(m_helpFile);
148
392c5133
VZ
149 return CallHtmlHelpFunction(GetParentWindow(), str, HH_HELP_CONTEXT,
150 (DWORD)section);
f6bcfd97
BP
151}
152
5100cabf
JS
153bool wxCHMHelpController::DisplayContextPopup(int contextId)
154{
59af881e 155 if (m_helpFile.IsEmpty()) return false;
5100cabf
JS
156
157 wxString str = GetValidFilename(m_helpFile);
158
a1b8f548
JS
159 // We also have to specify the popups file (default is cshelp.txt).
160 // str += wxT("::/cshelp.txt");
161
5100cabf
JS
162 HH_POPUP popup;
163 popup.cbStruct = sizeof(popup);
164 popup.hinst = (HINSTANCE) wxGetInstance();
165 popup.idString = contextId ;
166
167 GetCursorPos(& popup.pt);
5438a566
VZ
168 popup.clrForeground = (COLORREF)-1;
169 popup.clrBackground = (COLORREF)-1;
5100cabf
JS
170 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
171 popup.pszFont = NULL;
172 popup.pszText = NULL;
173
392c5133
VZ
174 return CallHtmlHelpFunction(GetParentWindow(), str, HH_DISPLAY_TEXT_POPUP,
175 wxPtrToUInt(&popup));
176}
177
178bool
179wxCHMHelpController::DisplayTextPopup(const wxString& text, const wxPoint& pos)
180{
181 return ShowContextHelpPopup(text, pos, GetParentWindow());
5100cabf
JS
182}
183
392c5133
VZ
184/* static */
185bool wxCHMHelpController::ShowContextHelpPopup(const wxString& text,
186 const wxPoint& pos,
187 wxWindow *window)
5100cabf
JS
188{
189 HH_POPUP popup;
190 popup.cbStruct = sizeof(popup);
191 popup.hinst = (HINSTANCE) wxGetInstance();
192 popup.idString = 0 ;
193 popup.pt.x = pos.x; popup.pt.y = pos.y;
5438a566
VZ
194 popup.clrForeground = (COLORREF)-1;
195 popup.clrBackground = (COLORREF)-1;
5100cabf
JS
196 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
197 popup.pszFont = NULL;
198 popup.pszText = (const wxChar*) text;
199
392c5133
VZ
200 return CallHtmlHelpFunction(window, NULL, HH_DISPLAY_TEXT_POPUP,
201 wxPtrToUInt(&popup));
5100cabf
JS
202}
203
f6bcfd97
BP
204bool wxCHMHelpController::DisplayBlock(long block)
205{
206 return DisplaySection(block);
207}
208
69b5cec2
VS
209bool wxCHMHelpController::KeywordSearch(const wxString& k,
210 wxHelpSearchMode WXUNUSED(mode))
f6bcfd97 211{
392c5133
VZ
212 if (m_helpFile.IsEmpty())
213 return false;
f6bcfd97
BP
214
215 wxString str = GetValidFilename(m_helpFile);
216
217 HH_AKLINK link;
218 link.cbStruct = sizeof(HH_AKLINK) ;
219 link.fReserved = FALSE ;
220 link.pszKeywords = k.c_str() ;
221 link.pszUrl = NULL ;
222 link.pszMsgText = NULL ;
223 link.pszMsgTitle = NULL ;
224 link.pszWindow = NULL ;
225 link.fIndexOnFail = TRUE ;
226
392c5133
VZ
227 return CallHtmlHelpFunction(GetParentWindow(), str, HH_KEYWORD_LOOKUP,
228 wxPtrToUInt(&link));
f6bcfd97
BP
229}
230
231bool wxCHMHelpController::Quit()
232{
392c5133 233 return CallHtmlHelpFunction(GetParentWindow(), NULL, HH_CLOSE_ALL, 0L);
f6bcfd97
BP
234}
235
236// Append extension if necessary.
237wxString wxCHMHelpController::GetValidFilename(const wxString& file) const
238{
239 wxString path, name, ext;
240 wxSplitPath(file, & path, & name, & ext);
241
242 wxString fullName;
243 if (path.IsEmpty())
244 fullName = name + wxT(".chm");
245 else if (path.Last() == wxT('\\'))
246 fullName = path + name + wxT(".chm");
247 else
248 fullName = path + wxT("\\") + name + wxT(".chm");
249 return fullName;
250}
251
252#endif // wxUSE_HELP