]> git.saurik.com Git - wxWidgets.git/blob - src/msw/helpchm.cpp
handle NULL BSTRs as empty ones per Microsoft convention
[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 HTMLHELP GetHtmlHelpFunction()
48 {
49 static HTMLHELP s_htmlHelp = NULL;
50
51 if ( !s_htmlHelp )
52 {
53 static wxDynamicLibrary s_dllHtmlHelp(_T("HHCTRL.OCX"), wxDL_VERBATIM);
54
55 if ( !s_dllHtmlHelp.IsLoaded() )
56 {
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 }
66 }
67 }
68
69 return s_htmlHelp;
70 }
71
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
74 static HWND GetSuitableHWND(wxWindow *win)
75 {
76 if ( !win && wxTheApp )
77 win = wxTheApp->GetTopWindow();
78
79 return win ? GetHwndOf(win) : ::GetDesktopWindow();
80 }
81
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
86 static bool
87 CallHtmlHelpFunction(wxWindow *win, const wxChar *str, UINT uint, DWORD dword)
88 {
89 HTMLHELP htmlHelp = GetHtmlHelpFunction();
90
91 return htmlHelp &&
92 htmlHelp(GetSuitableHWND(win), str, uint, dword);
93 }
94
95 IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController, wxHelpControllerBase)
96
97 bool wxCHMHelpController::Initialize(const wxString& filename)
98 {
99 if ( !GetHtmlHelpFunction() )
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())
116 return false;
117
118 wxString str = GetValidFilename(m_helpFile);
119
120 return CallHtmlHelpFunction(GetParentWindow(),
121 str.wx_str(), HH_DISPLAY_TOPIC, 0L);
122 }
123
124 // Use topic or HTML filename
125 bool wxCHMHelpController::DisplaySection(const wxString& section)
126 {
127 if (m_helpFile.IsEmpty())
128 return false;
129
130 wxString str = GetValidFilename(m_helpFile);
131
132 // Is this an HTML file or a keyword?
133 if ( section.Find(wxT(".htm")) != wxNOT_FOUND )
134 {
135 // interpret as a file name
136 return CallHtmlHelpFunction(GetParentWindow(),
137 str.wx_str(), HH_DISPLAY_TOPIC,
138 wxPtrToUInt(section.c_str()));
139 }
140
141 return KeywordSearch(section);
142 }
143
144 // Use context number
145 bool wxCHMHelpController::DisplaySection(int section)
146 {
147 if (m_helpFile.IsEmpty())
148 return false;
149
150 wxString str = GetValidFilename(m_helpFile);
151
152 return CallHtmlHelpFunction(GetParentWindow(),
153 str.wx_str(), HH_HELP_CONTEXT, (DWORD)section);
154 }
155
156 bool wxCHMHelpController::DisplayContextPopup(int contextId)
157 {
158 if (m_helpFile.IsEmpty()) return false;
159
160 wxString str = GetValidFilename(m_helpFile);
161
162 // We also have to specify the popups file (default is cshelp.txt).
163 // str += wxT("::/cshelp.txt");
164
165 HH_POPUP popup;
166 popup.cbStruct = sizeof(popup);
167 popup.hinst = (HINSTANCE) wxGetInstance();
168 popup.idString = contextId ;
169
170 GetCursorPos(& popup.pt);
171 popup.clrForeground = (COLORREF)-1;
172 popup.clrBackground = (COLORREF)-1;
173 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
174 popup.pszFont = NULL;
175 popup.pszText = NULL;
176
177 return CallHtmlHelpFunction(GetParentWindow(),
178 str.wx_str(), HH_DISPLAY_TEXT_POPUP,
179 wxPtrToUInt(&popup));
180 }
181
182 bool
183 wxCHMHelpController::DisplayTextPopup(const wxString& text, const wxPoint& pos)
184 {
185 return ShowContextHelpPopup(text, pos, GetParentWindow());
186 }
187
188 /* static */
189 bool wxCHMHelpController::ShowContextHelpPopup(const wxString& text,
190 const wxPoint& pos,
191 wxWindow *window)
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;
198 popup.clrForeground = (COLORREF)-1;
199 popup.clrBackground = (COLORREF)-1;
200 popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
201 popup.pszFont = NULL;
202 popup.pszText = text.wx_str();
203
204 return CallHtmlHelpFunction(window, NULL, HH_DISPLAY_TEXT_POPUP,
205 wxPtrToUInt(&popup));
206 }
207
208 bool wxCHMHelpController::DisplayBlock(long block)
209 {
210 return DisplaySection(block);
211 }
212
213 bool wxCHMHelpController::KeywordSearch(const wxString& k,
214 wxHelpSearchMode WXUNUSED(mode))
215 {
216 if (m_helpFile.IsEmpty())
217 return false;
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
231 return CallHtmlHelpFunction(GetParentWindow(),
232 str.wx_str(), HH_KEYWORD_LOOKUP,
233 wxPtrToUInt(&link));
234 }
235
236 bool wxCHMHelpController::Quit()
237 {
238 return CallHtmlHelpFunction(GetParentWindow(), NULL, HH_CLOSE_ALL, 0L);
239 }
240
241 // Append extension if necessary.
242 wxString 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