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: helpchm.cpp 60697 2009-05-20 13:15:24Z JS $
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 
GetHtmlHelpFunction()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
GetSuitableHWND(wxWindow * win)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
CallHtmlHelpFunction(wxWindow * win,const wxChar * str,UINT uint,DWORD dword)87 CallHtmlHelpFunction(wxWindow *win, const wxChar *str, UINT uint, DWORD dword)
88 {
89     HTMLHELP htmlHelp = GetHtmlHelpFunction();
90 
91     return htmlHelp && htmlHelp(GetSuitableHWND(win), str, uint, dword);
92 }
93 
IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController,wxHelpControllerBase)94 IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController, wxHelpControllerBase)
95 
96 bool wxCHMHelpController::Initialize(const wxString& filename)
97 {
98     if ( !GetHtmlHelpFunction() )
99         return false;
100 
101     m_helpFile = filename;
102     return true;
103 }
104 
LoadFile(const wxString & file)105 bool wxCHMHelpController::LoadFile(const wxString& file)
106 {
107     if (!file.IsEmpty())
108         m_helpFile = file;
109     return true;
110 }
111 
DisplayContents()112 bool wxCHMHelpController::DisplayContents()
113 {
114     if (m_helpFile.IsEmpty())
115         return false;
116 
117     wxString str = GetValidFilename(m_helpFile);
118 
119     return CallHtmlHelpFunction(GetParentWindow(), str, HH_DISPLAY_TOPIC, 0L);
120 }
121 
122 // Use topic or HTML filename
DisplaySection(const wxString & section)123 bool wxCHMHelpController::DisplaySection(const wxString& section)
124 {
125     if (m_helpFile.IsEmpty())
126         return false;
127 
128     wxString str = GetValidFilename(m_helpFile);
129 
130     // Is this an HTML file or a keyword?
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     }
137 
138     return KeywordSearch(section);
139 }
140 
141 // Use context number
DisplaySection(int section)142 bool wxCHMHelpController::DisplaySection(int section)
143 {
144     if (m_helpFile.IsEmpty())
145         return false;
146 
147     wxString str = GetValidFilename(m_helpFile);
148 
149     return CallHtmlHelpFunction(GetParentWindow(), str, HH_HELP_CONTEXT,
150                                 (DWORD)section);
151 }
152 
DisplayContextPopup(int contextId)153 bool wxCHMHelpController::DisplayContextPopup(int contextId)
154 {
155     if (m_helpFile.IsEmpty()) return false;
156 
157     wxString str = GetValidFilename(m_helpFile);
158 
159     // We also have to specify the popups file (default is cshelp.txt).
160     // str += wxT("::/cshelp.txt");
161 
162     HH_POPUP popup;
163     popup.cbStruct = sizeof(popup);
164     popup.hinst = (HINSTANCE) wxGetInstance();
165     popup.idString = contextId ;
166 
167     GetCursorPos(& popup.pt);
168     popup.clrForeground = ::GetSysColor(COLOR_INFOTEXT);
169     popup.clrBackground = ::GetSysColor(COLOR_INFOBK);
170     popup.rcMargins.top = popup.rcMargins.left = popup.rcMargins.right = popup.rcMargins.bottom = -1;
171     popup.pszFont = NULL;
172     popup.pszText = NULL;
173 
174     return CallHtmlHelpFunction(GetParentWindow(), str, HH_DISPLAY_TEXT_POPUP,
175                                 wxPtrToUInt(&popup));
176 }
177 
178 bool
DisplayTextPopup(const wxString & text,const wxPoint & pos)179 wxCHMHelpController::DisplayTextPopup(const wxString& text, const wxPoint& pos)
180 {
181     return ShowContextHelpPopup(text, pos, GetParentWindow());
182 }
183 
184 /* static */
ShowContextHelpPopup(const wxString & text,const wxPoint & pos,wxWindow * window)185 bool wxCHMHelpController::ShowContextHelpPopup(const wxString& text,
186                                                const wxPoint& pos,
187                                                wxWindow *window)
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;
194     popup.clrForeground = (COLORREF)-1;
195     popup.clrBackground = (COLORREF)-1;
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 
200     return CallHtmlHelpFunction(window, NULL, HH_DISPLAY_TEXT_POPUP,
201                                 wxPtrToUInt(&popup));
202 }
203 
DisplayBlock(long block)204 bool wxCHMHelpController::DisplayBlock(long block)
205 {
206     return DisplaySection(block);
207 }
208 
KeywordSearch(const wxString & k,wxHelpSearchMode WXUNUSED (mode))209 bool wxCHMHelpController::KeywordSearch(const wxString& k,
210                                         wxHelpSearchMode WXUNUSED(mode))
211 {
212     if (m_helpFile.IsEmpty())
213         return false;
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 
227     return CallHtmlHelpFunction(GetParentWindow(), str, HH_KEYWORD_LOOKUP,
228                                 wxPtrToUInt(&link));
229 }
230 
Quit()231 bool wxCHMHelpController::Quit()
232 {
233     return CallHtmlHelpFunction(GetParentWindow(), NULL, HH_CLOSE_ALL, 0L);
234 }
235 
236 // Append extension if necessary.
GetValidFilename(const wxString & file) const237 wxString 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
253