]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/helpchm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/helpchm.cpp
3 // Purpose: Help system: MS HTML Help implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_HELP && wxUSE_MS_HTML_HELP
21 #include "wx/filefn.h"
22 #include "wx/msw/helpchm.h"
24 #include "wx/dynload.h"
31 #include "wx/msw/private.h"
32 #include "wx/msw/htmlhelp.h"
34 // ----------------------------------------------------------------------------
35 // utility functions to manage the loading/unloading
37 // ----------------------------------------------------------------------------
40 typedef HWND ( WINAPI
* HTMLHELP
)( HWND
, LPCSTR
, UINT
, DWORD
);
41 #define HTMLHELP_NAME wxT("HtmlHelpA")
43 typedef HWND ( WINAPI
* HTMLHELP
)( HWND
, LPCWSTR
, UINT
, DWORD
);
44 #define HTMLHELP_NAME wxT("HtmlHelpW")
47 HTMLHELP
GetHtmlHelpFunction()
49 static HTMLHELP s_htmlHelp
= NULL
;
53 static wxDynamicLibrary
s_dllHtmlHelp(_T("HHCTRL.OCX"), wxDL_VERBATIM
);
55 if ( !s_dllHtmlHelp
.IsLoaded() )
57 wxLogError(_("MS HTML Help functions are unavailable because the MS HTML Help library is not installed on this machine. Please install it."));
61 s_htmlHelp
= (HTMLHELP
)s_dllHtmlHelp
.GetSymbol(HTMLHELP_NAME
);
64 wxLogError(_("Failed to initialize MS HTML Help."));
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
)
76 if ( !win
&& wxTheApp
)
77 win
= wxTheApp
->GetTopWindow();
79 return win
? GetHwndOf(win
) : ::GetDesktopWindow();
82 // wrap the real HtmlHelp() but just return false (and not crash) if it
85 // it also takes a wxWindow instead of HWND
87 CallHtmlHelpFunction(wxWindow
*win
, const wxChar
*str
, UINT uint
, DWORD dword
)
89 HTMLHELP htmlHelp
= GetHtmlHelpFunction();
91 return htmlHelp
&& htmlHelp(GetSuitableHWND(win
), str
, uint
, dword
);
94 IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController
, wxHelpControllerBase
)
96 bool wxCHMHelpController::Initialize(const wxString
& filename
)
98 if ( !GetHtmlHelpFunction() )
101 m_helpFile
= filename
;
105 bool wxCHMHelpController::LoadFile(const wxString
& file
)
112 bool wxCHMHelpController::DisplayContents()
114 if (m_helpFile
.IsEmpty())
117 wxString str
= GetValidFilename(m_helpFile
);
119 return CallHtmlHelpFunction(GetParentWindow(), str
, HH_DISPLAY_TOPIC
, 0L);
122 // Use topic or HTML filename
123 bool wxCHMHelpController::DisplaySection(const wxString
& section
)
125 if (m_helpFile
.IsEmpty())
128 wxString str
= GetValidFilename(m_helpFile
);
130 // Is this an HTML file or a keyword?
131 if ( section
.Find(wxT(".htm")) != wxNOT_FOUND
)
133 // interpret as a file name
134 return CallHtmlHelpFunction(GetParentWindow(), str
, HH_DISPLAY_TOPIC
,
135 wxPtrToUInt(section
.c_str()));
138 return KeywordSearch(section
);
141 // Use context number
142 bool wxCHMHelpController::DisplaySection(int section
)
144 if (m_helpFile
.IsEmpty())
147 wxString str
= GetValidFilename(m_helpFile
);
149 return CallHtmlHelpFunction(GetParentWindow(), str
, HH_HELP_CONTEXT
,
153 bool wxCHMHelpController::DisplayContextPopup(int contextId
)
155 if (m_helpFile
.IsEmpty()) return false;
157 wxString str
= GetValidFilename(m_helpFile
);
159 // We also have to specify the popups file (default is cshelp.txt).
160 // str += wxT("::/cshelp.txt");
163 popup
.cbStruct
= sizeof(popup
);
164 popup
.hinst
= (HINSTANCE
) wxGetInstance();
165 popup
.idString
= contextId
;
167 GetCursorPos(& popup
.pt
);
168 popup
.clrForeground
= (COLORREF
)-1;
169 popup
.clrBackground
= (COLORREF
)-1;
170 popup
.rcMargins
.top
= popup
.rcMargins
.left
= popup
.rcMargins
.right
= popup
.rcMargins
.bottom
= -1;
171 popup
.pszFont
= NULL
;
172 popup
.pszText
= NULL
;
174 return CallHtmlHelpFunction(GetParentWindow(), str
, HH_DISPLAY_TEXT_POPUP
,
175 wxPtrToUInt(&popup
));
179 wxCHMHelpController::DisplayTextPopup(const wxString
& text
, const wxPoint
& pos
)
181 return ShowContextHelpPopup(text
, pos
, GetParentWindow());
185 bool wxCHMHelpController::ShowContextHelpPopup(const wxString
& text
,
190 popup
.cbStruct
= sizeof(popup
);
191 popup
.hinst
= (HINSTANCE
) wxGetInstance();
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
;
200 return CallHtmlHelpFunction(window
, NULL
, HH_DISPLAY_TEXT_POPUP
,
201 wxPtrToUInt(&popup
));
204 bool wxCHMHelpController::DisplayBlock(long block
)
206 return DisplaySection(block
);
209 bool wxCHMHelpController::KeywordSearch(const wxString
& k
,
210 wxHelpSearchMode
WXUNUSED(mode
))
212 if (m_helpFile
.IsEmpty())
215 wxString str
= GetValidFilename(m_helpFile
);
218 link
.cbStruct
= sizeof(HH_AKLINK
) ;
219 link
.fReserved
= FALSE
;
220 link
.pszKeywords
= k
.c_str() ;
222 link
.pszMsgText
= NULL
;
223 link
.pszMsgTitle
= NULL
;
224 link
.pszWindow
= NULL
;
225 link
.fIndexOnFail
= TRUE
;
227 return CallHtmlHelpFunction(GetParentWindow(), str
, HH_KEYWORD_LOOKUP
,
231 bool wxCHMHelpController::Quit()
233 return CallHtmlHelpFunction(GetParentWindow(), NULL
, HH_CLOSE_ALL
, 0L);
236 // Append extension if necessary.
237 wxString
wxCHMHelpController::GetValidFilename(const wxString
& file
) const
239 wxString path
, name
, ext
;
240 wxSplitPath(file
, & path
, & name
, & ext
);
244 fullName
= name
+ wxT(".chm");
245 else if (path
.Last() == wxT('\\'))
246 fullName
= path
+ name
+ wxT(".chm");
248 fullName
= path
+ wxT("\\") + name
+ wxT(".chm");