]>
Commit | Line | Data |
---|---|---|
f6bcfd97 | 1 | ///////////////////////////////////////////////////////////////////////////// |
a71d815b | 2 | // Name: src/msw/helpchm.cpp |
f6bcfd97 BP |
3 | // Purpose: Help system: MS HTML Help implementation |
4 | // Author: Julian Smart | |
0016bb3b | 5 | // Modified by: Vadim Zeitlin at 2008-03-01: refactoring, simplification |
f6bcfd97 | 6 | // Created: 16/04/2000 |
f6bcfd97 | 7 | // Copyright: (c) Julian Smart |
65571936 | 8 | // Licence: wxWindows licence |
f6bcfd97 BP |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
f6bcfd97 BP |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
3295e238 | 15 | #pragma hdrstop |
f6bcfd97 BP |
16 | #endif |
17 | ||
a71d815b | 18 | #if wxUSE_HELP && wxUSE_MS_HTML_HELP |
f6bcfd97 | 19 | |
730078f8 | 20 | #include "wx/filename.h" |
f6bcfd97 | 21 | #include "wx/msw/helpchm.h" |
0b9ab0bd | 22 | |
397cfa3a | 23 | #include "wx/dynload.h" |
0b9ab0bd | 24 | |
9fcaaedd VS |
25 | #ifndef WX_PRECOMP |
26 | #include "wx/intl.h" | |
27 | #include "wx/app.h" | |
28 | #endif | |
29 | ||
f6bcfd97 | 30 | #include "wx/msw/private.h" |
92199f4c | 31 | #include "wx/msw/htmlhelp.h" |
3295e238 VZ |
32 | |
33 | // ---------------------------------------------------------------------------- | |
638a0b2c VS |
34 | // utility functions to manage the loading/unloading |
35 | // of hhctrl.ocx | |
3295e238 VZ |
36 | // ---------------------------------------------------------------------------- |
37 | ||
638a0b2c | 38 | #ifndef UNICODE |
3295e238 | 39 | typedef HWND ( WINAPI * HTMLHELP )( HWND, LPCSTR, UINT, DWORD ); |
2b5f62a0 | 40 | #define HTMLHELP_NAME wxT("HtmlHelpA") |
3295e238 VZ |
41 | #else // ANSI |
42 | typedef HWND ( WINAPI * HTMLHELP )( HWND, LPCWSTR, UINT, DWORD ); | |
2b5f62a0 | 43 | #define HTMLHELP_NAME wxT("HtmlHelpW") |
638a0b2c | 44 | #endif |
3295e238 | 45 | |
392c5133 | 46 | HTMLHELP GetHtmlHelpFunction() |
638a0b2c | 47 | { |
392c5133 | 48 | static HTMLHELP s_htmlHelp = NULL; |
638a0b2c | 49 | |
392c5133 | 50 | if ( !s_htmlHelp ) |
638a0b2c | 51 | { |
9a83f860 | 52 | static wxDynamicLibrary s_dllHtmlHelp(wxT("HHCTRL.OCX"), wxDL_VERBATIM); |
4f89dbc4 | 53 | |
392c5133 | 54 | if ( !s_dllHtmlHelp.IsLoaded() ) |
4f89dbc4 | 55 | { |
392c5133 VZ |
56 | wxLogError(_("MS HTML Help functions are unavailable because the MS HTML Help library is not installed on this machine. Please install it.")); |
57 | } | |
58 | else | |
59 | { | |
60 | s_htmlHelp = (HTMLHELP)s_dllHtmlHelp.GetSymbol(HTMLHELP_NAME); | |
61 | if ( !s_htmlHelp ) | |
62 | { | |
63 | wxLogError(_("Failed to initialize MS HTML Help.")); | |
64 | } | |
4f89dbc4 | 65 | } |
638a0b2c VS |
66 | } |
67 | ||
392c5133 | 68 | return s_htmlHelp; |
638a0b2c VS |
69 | } |
70 | ||
392c5133 VZ |
71 | // find the window to use in HtmlHelp() call: use the given one by default but |
72 | // fall back to the top level app window and then the desktop if it's NULL | |
73 | static HWND GetSuitableHWND(wxWindow *win) | |
638a0b2c | 74 | { |
392c5133 VZ |
75 | if ( !win && wxTheApp ) |
76 | win = wxTheApp->GetTopWindow(); | |
77 | ||
78 | return win ? GetHwndOf(win) : ::GetDesktopWindow(); | |
638a0b2c VS |
79 | } |
80 | ||
f6bcfd97 BP |
81 | |
82 | IMPLEMENT_DYNAMIC_CLASS(wxCHMHelpController, wxHelpControllerBase) | |
83 | ||
84 | bool wxCHMHelpController::Initialize(const wxString& filename) | |
85 | { | |
392c5133 | 86 | if ( !GetHtmlHelpFunction() ) |
59af881e | 87 | return false; |
638a0b2c | 88 | |
f6bcfd97 | 89 | m_helpFile = filename; |
59af881e | 90 | return true; |
f6bcfd97 BP |
91 | } |
92 | ||
93 | bool wxCHMHelpController::LoadFile(const wxString& file) | |
94 | { | |
95 | if (!file.IsEmpty()) | |
96 | m_helpFile = file; | |
59af881e | 97 | return true; |
f6bcfd97 BP |
98 | } |
99 | ||
0016bb3b VZ |
100 | /* static */ bool |
101 | wxCHMHelpController::CallHtmlHelp(wxWindow *win, | |
102 | const wxChar *str, | |
103 | unsigned cmd, | |
104 | WXWPARAM param) | |
105 | { | |
106 | HTMLHELP htmlHelp = GetHtmlHelpFunction(); | |
107 | ||
108 | return htmlHelp && htmlHelp(GetSuitableHWND(win), str, cmd, param); | |
109 | } | |
110 | ||
f6bcfd97 BP |
111 | bool wxCHMHelpController::DisplayContents() |
112 | { | |
392c5133 VZ |
113 | if (m_helpFile.IsEmpty()) |
114 | return false; | |
f6bcfd97 | 115 | |
0016bb3b | 116 | return CallHtmlHelp(HH_DISPLAY_TOPIC); |
f6bcfd97 BP |
117 | } |
118 | ||
119 | // Use topic or HTML filename | |
120 | bool wxCHMHelpController::DisplaySection(const wxString& section) | |
121 | { | |
392c5133 VZ |
122 | if (m_helpFile.IsEmpty()) |
123 | return false; | |
f6bcfd97 | 124 | |
f6bcfd97 | 125 | // Is this an HTML file or a keyword? |
392c5133 VZ |
126 | if ( section.Find(wxT(".htm")) != wxNOT_FOUND ) |
127 | { | |
128 | // interpret as a file name | |
017dc06b | 129 | return CallHtmlHelp(HH_DISPLAY_TOPIC, wxMSW_CONV_LPCTSTR(section)); |
392c5133 | 130 | } |
f6bcfd97 | 131 | |
392c5133 | 132 | return KeywordSearch(section); |
f6bcfd97 BP |
133 | } |
134 | ||
135 | // Use context number | |
136 | bool wxCHMHelpController::DisplaySection(int section) | |
137 | { | |
392c5133 VZ |
138 | if (m_helpFile.IsEmpty()) |
139 | return false; | |
f6bcfd97 | 140 | |
0016bb3b | 141 | return CallHtmlHelp(HH_HELP_CONTEXT, section); |
f6bcfd97 BP |
142 | } |
143 | ||
0016bb3b VZ |
144 | /* static */ |
145 | bool | |
146 | wxCHMHelpController::DoDisplayTextPopup(const wxChar *text, | |
147 | const wxPoint& pos, | |
148 | int contextId, | |
149 | wxWindow *window) | |
5100cabf | 150 | { |
5100cabf JS |
151 | HH_POPUP popup; |
152 | popup.cbStruct = sizeof(popup); | |
153 | popup.hinst = (HINSTANCE) wxGetInstance(); | |
0016bb3b VZ |
154 | popup.idString = contextId; |
155 | popup.pszText = text; | |
156 | popup.pt.x = pos.x; | |
157 | popup.pt.y = pos.y; | |
09e02fc7 JS |
158 | popup.clrForeground = ::GetSysColor(COLOR_INFOTEXT); |
159 | popup.clrBackground = ::GetSysColor(COLOR_INFOBK); | |
0016bb3b VZ |
160 | popup.rcMargins.top = |
161 | popup.rcMargins.left = | |
162 | popup.rcMargins.right = | |
163 | popup.rcMargins.bottom = -1; | |
5100cabf | 164 | popup.pszFont = NULL; |
5100cabf | 165 | |
0016bb3b VZ |
166 | return CallHtmlHelp(window, NULL, HH_DISPLAY_TEXT_POPUP, &popup); |
167 | } | |
168 | ||
169 | bool wxCHMHelpController::DisplayContextPopup(int contextId) | |
170 | { | |
171 | return DoDisplayTextPopup(NULL, wxGetMousePosition(), contextId, | |
172 | GetParentWindow()); | |
392c5133 VZ |
173 | } |
174 | ||
175 | bool | |
176 | wxCHMHelpController::DisplayTextPopup(const wxString& text, const wxPoint& pos) | |
177 | { | |
178 | return ShowContextHelpPopup(text, pos, GetParentWindow()); | |
5100cabf JS |
179 | } |
180 | ||
392c5133 VZ |
181 | /* static */ |
182 | bool wxCHMHelpController::ShowContextHelpPopup(const wxString& text, | |
183 | const wxPoint& pos, | |
184 | wxWindow *window) | |
5100cabf | 185 | { |
017dc06b | 186 | return DoDisplayTextPopup(text.t_str(), pos, 0, window); |
5100cabf JS |
187 | } |
188 | ||
f6bcfd97 BP |
189 | bool wxCHMHelpController::DisplayBlock(long block) |
190 | { | |
191 | return DisplaySection(block); | |
192 | } | |
193 | ||
69b5cec2 VS |
194 | bool wxCHMHelpController::KeywordSearch(const wxString& k, |
195 | wxHelpSearchMode WXUNUSED(mode)) | |
f6bcfd97 | 196 | { |
392c5133 VZ |
197 | if (m_helpFile.IsEmpty()) |
198 | return false; | |
f6bcfd97 | 199 | |
f6bcfd97 | 200 | HH_AKLINK link; |
0016bb3b VZ |
201 | link.cbStruct = sizeof(HH_AKLINK); |
202 | link.fReserved = FALSE; | |
017dc06b | 203 | link.pszKeywords = k.t_str(); |
0016bb3b VZ |
204 | link.pszUrl = NULL; |
205 | link.pszMsgText = NULL; | |
206 | link.pszMsgTitle = NULL; | |
207 | link.pszWindow = NULL; | |
208 | link.fIndexOnFail = TRUE; | |
209 | ||
210 | return CallHtmlHelp(HH_KEYWORD_LOOKUP, &link); | |
f6bcfd97 BP |
211 | } |
212 | ||
213 | bool wxCHMHelpController::Quit() | |
214 | { | |
40ec64ae | 215 | return CallHtmlHelp(NULL, NULL, HH_CLOSE_ALL); |
f6bcfd97 BP |
216 | } |
217 | ||
0016bb3b | 218 | wxString wxCHMHelpController::GetValidFilename() const |
f6bcfd97 BP |
219 | { |
220 | wxString path, name, ext; | |
bd365871 | 221 | wxFileName::SplitPath(m_helpFile, &path, &name, &ext); |
f6bcfd97 BP |
222 | |
223 | wxString fullName; | |
224 | if (path.IsEmpty()) | |
225 | fullName = name + wxT(".chm"); | |
226 | else if (path.Last() == wxT('\\')) | |
227 | fullName = path + name + wxT(".chm"); | |
228 | else | |
229 | fullName = path + wxT("\\") + name + wxT(".chm"); | |
230 | return fullName; | |
231 | } | |
232 | ||
233 | #endif // wxUSE_HELP |