]> git.saurik.com Git - wxWidgets.git/blame - src/msw/helpchm.cpp
don't append anything back in DoDeleteOneItem() if the control becomes empty (fixes...
[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
e0a050e3
VS
91 return htmlHelp &&
92 htmlHelp(GetSuitableHWND(win), str, uint, dword);
f6bcfd97
BP
93}
94
95IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController, wxHelpControllerBase)
96
97bool wxCHMHelpController::Initialize(const wxString& filename)
98{
392c5133 99 if ( !GetHtmlHelpFunction() )
59af881e 100 return false;
638a0b2c 101
f6bcfd97 102 m_helpFile = filename;
59af881e 103 return true;
f6bcfd97
BP
104}
105
106bool wxCHMHelpController::LoadFile(const wxString& file)
107{
108 if (!file.IsEmpty())
109 m_helpFile = file;
59af881e 110 return true;
f6bcfd97
BP
111}
112
113bool wxCHMHelpController::DisplayContents()
114{
392c5133
VZ
115 if (m_helpFile.IsEmpty())
116 return false;
f6bcfd97
BP
117
118 wxString str = GetValidFilename(m_helpFile);
119
e0a050e3
VS
120 return CallHtmlHelpFunction(GetParentWindow(),
121 str.wx_str(), HH_DISPLAY_TOPIC, 0L);
f6bcfd97
BP
122}
123
124// Use topic or HTML filename
125bool wxCHMHelpController::DisplaySection(const wxString& section)
126{
392c5133
VZ
127 if (m_helpFile.IsEmpty())
128 return false;
f6bcfd97
BP
129
130 wxString str = GetValidFilename(m_helpFile);
131
132 // Is this an HTML file or a keyword?
392c5133
VZ
133 if ( section.Find(wxT(".htm")) != wxNOT_FOUND )
134 {
135 // interpret as a file name
e0a050e3
VS
136 return CallHtmlHelpFunction(GetParentWindow(),
137 str.wx_str(), HH_DISPLAY_TOPIC,
392c5133
VZ
138 wxPtrToUInt(section.c_str()));
139 }
f6bcfd97 140
392c5133 141 return KeywordSearch(section);
f6bcfd97
BP
142}
143
144// Use context number
145bool wxCHMHelpController::DisplaySection(int section)
146{
392c5133
VZ
147 if (m_helpFile.IsEmpty())
148 return false;
f6bcfd97
BP
149
150 wxString str = GetValidFilename(m_helpFile);
151
e0a050e3
VS
152 return CallHtmlHelpFunction(GetParentWindow(),
153 str.wx_str(), HH_HELP_CONTEXT, (DWORD)section);
f6bcfd97
BP
154}
155
5100cabf
JS
156bool wxCHMHelpController::DisplayContextPopup(int contextId)
157{
59af881e 158 if (m_helpFile.IsEmpty()) return false;
5100cabf
JS
159
160 wxString str = GetValidFilename(m_helpFile);
161
a1b8f548
JS
162 // We also have to specify the popups file (default is cshelp.txt).
163 // str += wxT("::/cshelp.txt");
164
5100cabf
JS
165 HH_POPUP popup;
166 popup.cbStruct = sizeof(popup);
167 popup.hinst = (HINSTANCE) wxGetInstance();
168 popup.idString = contextId ;
169
170 GetCursorPos(& popup.pt);
5438a566
VZ
171 popup.clrForeground = (COLORREF)-1;
172 popup.clrBackground = (COLORREF)-1;
5100cabf
JS
173 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
174 popup.pszFont = NULL;
175 popup.pszText = NULL;
176
e0a050e3
VS
177 return CallHtmlHelpFunction(GetParentWindow(),
178 str.wx_str(), HH_DISPLAY_TEXT_POPUP,
392c5133
VZ
179 wxPtrToUInt(&popup));
180}
181
182bool
183wxCHMHelpController::DisplayTextPopup(const wxString& text, const wxPoint& pos)
184{
185 return ShowContextHelpPopup(text, pos, GetParentWindow());
5100cabf
JS
186}
187
392c5133
VZ
188/* static */
189bool wxCHMHelpController::ShowContextHelpPopup(const wxString& text,
190 const wxPoint& pos,
191 wxWindow *window)
5100cabf
JS
192{
193 HH_POPUP popup;
194 popup.cbStruct = sizeof(popup);
195 popup.hinst = (HINSTANCE) wxGetInstance();
196 popup.idString = 0 ;
197 popup.pt.x = pos.x; popup.pt.y = pos.y;
5438a566
VZ
198 popup.clrForeground = (COLORREF)-1;
199 popup.clrBackground = (COLORREF)-1;
5100cabf
JS
200 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
201 popup.pszFont = NULL;
e0a050e3 202 popup.pszText = text.wx_str();
5100cabf 203
392c5133
VZ
204 return CallHtmlHelpFunction(window, NULL, HH_DISPLAY_TEXT_POPUP,
205 wxPtrToUInt(&popup));
5100cabf
JS
206}
207
f6bcfd97
BP
208bool wxCHMHelpController::DisplayBlock(long block)
209{
210 return DisplaySection(block);
211}
212
69b5cec2
VS
213bool wxCHMHelpController::KeywordSearch(const wxString& k,
214 wxHelpSearchMode WXUNUSED(mode))
f6bcfd97 215{
392c5133
VZ
216 if (m_helpFile.IsEmpty())
217 return false;
f6bcfd97
BP
218
219 wxString str = GetValidFilename(m_helpFile);
220
221 HH_AKLINK link;
222 link.cbStruct = sizeof(HH_AKLINK) ;
223 link.fReserved = FALSE ;
224 link.pszKeywords = k.c_str() ;
225 link.pszUrl = NULL ;
226 link.pszMsgText = NULL ;
227 link.pszMsgTitle = NULL ;
228 link.pszWindow = NULL ;
229 link.fIndexOnFail = TRUE ;
230
e0a050e3
VS
231 return CallHtmlHelpFunction(GetParentWindow(),
232 str.wx_str(), HH_KEYWORD_LOOKUP,
392c5133 233 wxPtrToUInt(&link));
f6bcfd97
BP
234}
235
236bool wxCHMHelpController::Quit()
237{
392c5133 238 return CallHtmlHelpFunction(GetParentWindow(), NULL, HH_CLOSE_ALL, 0L);
f6bcfd97
BP
239}
240
241// Append extension if necessary.
242wxString wxCHMHelpController::GetValidFilename(const wxString& file) const
243{
244 wxString path, name, ext;
245 wxSplitPath(file, & path, & name, & ext);
246
247 wxString fullName;
248 if (path.IsEmpty())
249 fullName = name + wxT(".chm");
250 else if (path.Last() == wxT('\\'))
251 fullName = path + name + wxT(".chm");
252 else
253 fullName = path + wxT("\\") + name + wxT(".chm");
254 return fullName;
255}
256
257#endif // wxUSE_HELP