]>
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();
92 htmlHelp(GetSuitableHWND(win
), str
, uint
, dword
);
95 IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController
, wxHelpControllerBase
)
97 bool wxCHMHelpController::Initialize(const wxString
& filename
)
99 if ( !GetHtmlHelpFunction() )
102 m_helpFile
= filename
;
106 bool wxCHMHelpController::LoadFile(const wxString
& file
)
113 bool wxCHMHelpController::DisplayContents()
115 if (m_helpFile
.IsEmpty())
118 wxString str
= GetValidFilename(m_helpFile
);
120 return CallHtmlHelpFunction(GetParentWindow(),
121 str
.wx_str(), HH_DISPLAY_TOPIC
, 0L);
124 // Use topic or HTML filename
125 bool wxCHMHelpController::DisplaySection(const wxString
& section
)
127 if (m_helpFile
.IsEmpty())
130 wxString str
= GetValidFilename(m_helpFile
);
132 // Is this an HTML file or a keyword?
133 if ( section
.Find(wxT(".htm")) != wxNOT_FOUND
)
135 // interpret as a file name
136 return CallHtmlHelpFunction(GetParentWindow(),
137 str
.wx_str(), HH_DISPLAY_TOPIC
,
138 wxPtrToUInt(section
.c_str()));
141 return KeywordSearch(section
);
144 // Use context number
145 bool wxCHMHelpController::DisplaySection(int section
)
147 if (m_helpFile
.IsEmpty())
150 wxString str
= GetValidFilename(m_helpFile
);
152 return CallHtmlHelpFunction(GetParentWindow(),
153 str
.wx_str(), HH_HELP_CONTEXT
, (DWORD
)section
);
156 bool wxCHMHelpController::DisplayContextPopup(int contextId
)
158 if (m_helpFile
.IsEmpty()) return false;
160 wxString str
= GetValidFilename(m_helpFile
);
162 // We also have to specify the popups file (default is cshelp.txt).
163 // str += wxT("::/cshelp.txt");
166 popup
.cbStruct
= sizeof(popup
);
167 popup
.hinst
= (HINSTANCE
) wxGetInstance();
168 popup
.idString
= contextId
;
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
;
177 return CallHtmlHelpFunction(GetParentWindow(),
178 str
.wx_str(), HH_DISPLAY_TEXT_POPUP
,
179 wxPtrToUInt(&popup
));
183 wxCHMHelpController::DisplayTextPopup(const wxString
& text
, const wxPoint
& pos
)
185 return ShowContextHelpPopup(text
, pos
, GetParentWindow());
189 bool wxCHMHelpController::ShowContextHelpPopup(const wxString
& text
,
194 popup
.cbStruct
= sizeof(popup
);
195 popup
.hinst
= (HINSTANCE
) wxGetInstance();
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();
204 return CallHtmlHelpFunction(window
, NULL
, HH_DISPLAY_TEXT_POPUP
,
205 wxPtrToUInt(&popup
));
208 bool wxCHMHelpController::DisplayBlock(long block
)
210 return DisplaySection(block
);
213 bool wxCHMHelpController::KeywordSearch(const wxString
& k
,
214 wxHelpSearchMode
WXUNUSED(mode
))
216 if (m_helpFile
.IsEmpty())
219 wxString str
= GetValidFilename(m_helpFile
);
222 link
.cbStruct
= sizeof(HH_AKLINK
) ;
223 link
.fReserved
= FALSE
;
224 link
.pszKeywords
= k
.c_str() ;
226 link
.pszMsgText
= NULL
;
227 link
.pszMsgTitle
= NULL
;
228 link
.pszWindow
= NULL
;
229 link
.fIndexOnFail
= TRUE
;
231 return CallHtmlHelpFunction(GetParentWindow(),
232 str
.wx_str(), HH_KEYWORD_LOOKUP
,
236 bool wxCHMHelpController::Quit()
238 return CallHtmlHelpFunction(GetParentWindow(), NULL
, HH_CLOSE_ALL
, 0L);
241 // Append extension if necessary.
242 wxString
wxCHMHelpController::GetValidFilename(const wxString
& file
) const
244 wxString path
, name
, ext
;
245 wxSplitPath(file
, & path
, & name
, & ext
);
249 fullName
= name
+ wxT(".chm");
250 else if (path
.Last() == wxT('\\'))
251 fullName
= path
+ name
+ wxT(".chm");
253 fullName
= path
+ wxT("\\") + name
+ wxT(".chm");