]>
Commit | Line | Data |
---|---|---|
4e4152e4 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: helpview.h | |
3 | // Purpose: HelpView application | |
4 | // A standalone viewer for wxHTML Help (.htb) files | |
5 | // Author: Vaclav Slavik, Julian Smart | |
6 | // Modified by: | |
7 | // Created: 2002-07-09 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 2002 Vaclav Slavik, Julian Smart and others | |
10 | // Licence: wxWindows licence | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | #ifdef __GNUG__ | |
14 | #pragma implementation "help.cpp" | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | // for all others, include the necessary headers (this file is usually all you | |
25 | // need because it includes almost all "standard" wxWindows headers | |
26 | #ifndef WX_PRECOMP | |
27 | #include "wx/wx.h" | |
28 | #endif | |
29 | ||
30 | #include "wx/image.h" | |
31 | #include "wx/wxhtml.h" | |
32 | #include "wx/fs_zip.h" | |
33 | #include "wx/log.h" | |
34 | #include "wx/artprov.h" | |
35 | #include "wx/filedlg.h" | |
36 | ||
877c86a2 JS |
37 | #include "helpview.h" |
38 | ||
4e4152e4 JS |
39 | class AlternateArtProvider : public wxArtProvider |
40 | { | |
41 | protected: | |
42 | virtual wxBitmap CreateBitmap(const wxArtID& id, const wxArtClient& client, | |
43 | const wxSize& size); | |
44 | }; | |
45 | ||
4e4152e4 JS |
46 | IMPLEMENT_APP(hvApp) |
47 | ||
2b5f62a0 VZ |
48 | hvApp::hvApp() |
49 | { | |
50 | #if hvUSE_IPC | |
51 | m_server = NULL; | |
52 | #endif | |
53 | } | |
4e4152e4 JS |
54 | |
55 | bool hvApp::OnInit() | |
56 | { | |
57 | #ifdef __WXMOTIF__ | |
58 | delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used | |
59 | #endif | |
2b5f62a0 VZ |
60 | |
61 | wxArtProvider::PushProvider(new AlternateArtProvider); | |
62 | ||
63 | int istyle = wxHF_DEFAULT_STYLE; | |
64 | ||
4e4152e4 | 65 | wxArtProvider::PushProvider(new AlternateArtProvider); |
2b5f62a0 VZ |
66 | |
67 | wxString service, windowName, book[10], titleFormat, argStr; | |
68 | int bookCount = 0; | |
69 | int i; | |
70 | bool hasService = FALSE; | |
71 | bool hasWindowName = FALSE; | |
72 | bool createServer = FALSE; | |
73 | ||
74 | m_server = NULL; | |
75 | ||
76 | // Help books are recognized by extension ".hhp" ".htb" or ".zip". | |
77 | // Service and window_name can occur anywhere in arguments, | |
78 | // but service must be first | |
79 | // Other arguments (topic?) could be added | |
80 | ||
81 | // modes of operation: | |
82 | // 1) no arguments - stand alone, prompt user for book | |
83 | // 2) books only - stand alone, open books | |
84 | // 3) "--server" as (any) arg - start connection with default service; | |
85 | // also open any books passed as other arguments | |
86 | // 4) at least one argument which is not book, and not "--server" - take first | |
87 | // such argument as service, second (if present) as window name, | |
88 | // start service, open any books | |
89 | ||
90 | for( i=1; i < argc; i++ ) | |
91 | { | |
92 | argStr = argv[i]; | |
93 | ||
94 | if ( argStr.Find( wxT(".hhp") ) >= 0 || | |
95 | argStr.Find( wxT(".htb") ) >= 0 || | |
96 | argStr.Find( wxT(".zip") ) >= 0 ) { | |
97 | book[bookCount] = argStr; | |
98 | bookCount++; | |
99 | } | |
100 | else if ( argStr == wxT("--server") ) | |
101 | { | |
102 | createServer = TRUE; | |
103 | #if defined(__WXMSW__) | |
104 | service = wxT("generic_helpservice"); | |
105 | #elif defined(__UNIX__) | |
106 | service = wxT("/tmp/") + wxString(wxT("generic_helpservice")); | |
107 | #else | |
108 | service = wxT("4242"); | |
109 | #endif | |
110 | } | |
111 | else if ( !hasService ) | |
112 | { | |
113 | service = argStr; | |
114 | hasService = TRUE; | |
115 | createServer = TRUE; | |
116 | } | |
117 | else if ( !hasWindowName ) | |
118 | { | |
119 | windowName = argStr; | |
120 | hasWindowName = TRUE; | |
121 | } | |
122 | else if ( argStr.Find( wxT("--Style") ) >= 0 ) | |
123 | { | |
124 | long i; | |
125 | wxString numb = argStr.AfterLast(wxT('e')); | |
126 | if ( !(numb.ToLong(&i) ) ) | |
127 | { | |
128 | wxLogError( wxT("Integer conversion failed for --Style") ); | |
129 | } | |
130 | else | |
131 | { | |
132 | istyle = i; | |
133 | } | |
134 | } | |
135 | else | |
136 | { | |
137 | //unknown - could be topic? | |
138 | } | |
139 | } | |
140 | ||
141 | //no book - query user | |
142 | if ( bookCount < 1 ) | |
143 | { | |
144 | wxString s = wxFileSelector( wxT("Open help file"), | |
145 | wxGetCwd(), | |
146 | wxEmptyString, | |
147 | wxEmptyString, | |
148 | wxT( | |
149 | "Help books (*.htb)|*.htb|Help books (*.zip)|*.zip|\ | |
150 | HTML Help Project (*.hhp)|*.hhp"), | |
151 | wxOPEN | wxFILE_MUST_EXIST, | |
152 | NULL); | |
153 | ||
154 | if (!s.IsEmpty()) | |
155 | { | |
156 | book[0] = s; | |
157 | bookCount = 1; | |
158 | } | |
159 | } | |
160 | ||
161 | #if hvUSE_IPC | |
162 | ||
163 | if ( createServer ) { | |
164 | // Create a new server | |
165 | m_server = new hvServer; | |
166 | ||
167 | if ( !m_server->Create(service) ) { | |
168 | wxString wxm = wxT("Server Create failed - service: "); | |
169 | wxString xxm = wxm << service; | |
170 | wxLogError( xxm ); | |
171 | //if MSW quits here, probably another copy already exists | |
172 | return FALSE; | |
173 | ||
174 | } | |
175 | createServer = FALSE; | |
176 | } | |
177 | ||
178 | #endif // hvUSE_IPC | |
179 | ||
180 | //now add help | |
4e4152e4 | 181 | wxInitAllImageHandlers(); |
2b5f62a0 VZ |
182 | wxFileSystem::AddHandler(new wxZipFSHandler); |
183 | ||
184 | SetVendorName(wxT("wxWindows") ); | |
185 | SetAppName(wxT("wxHTMLHelpServer") ); | |
4e4152e4 | 186 | wxConfig::Get(); // create an instance |
2b5f62a0 VZ |
187 | |
188 | m_helpController = new wxHtmlHelpController( istyle ); | |
189 | ||
190 | if ( !hasWindowName ) | |
191 | titleFormat = wxT("Help: %s") ; | |
192 | else | |
193 | { | |
194 | //remove underscores | |
195 | windowName.Replace( wxT("_"), wxT(" ") ); | |
196 | titleFormat = windowName; | |
4e4152e4 | 197 | } |
2b5f62a0 VZ |
198 | |
199 | m_helpController->SetTitleFormat( titleFormat ); | |
200 | ||
201 | for( i=0; i < bookCount; i++ ) | |
202 | { | |
203 | m_helpController->AddBook(book[i]); | |
204 | } | |
205 | ||
4e4152e4 JS |
206 | #ifdef __WXMOTIF__ |
207 | delete wxLog::SetActiveTarget(new wxLogGui); | |
208 | #endif | |
2b5f62a0 VZ |
209 | |
210 | m_helpController -> DisplayContents(); | |
211 | ||
4e4152e4 JS |
212 | return TRUE; |
213 | } | |
214 | ||
215 | ||
216 | int hvApp::OnExit() | |
217 | { | |
2b5f62a0 VZ |
218 | #if hvUSE_IPC |
219 | wxNode* node = m_connections.First(); | |
220 | while (node) | |
221 | { | |
222 | wxNode* next = node->Next(); | |
223 | hvConnection* connection = (hvConnection*) node->Data(); | |
224 | connection->Disconnect(); | |
225 | delete connection; | |
226 | node = next; | |
227 | } | |
228 | m_connections.Clear(); | |
229 | ||
230 | if (m_server) | |
231 | { | |
232 | delete m_server; | |
233 | m_server = NULL; | |
234 | } | |
235 | #endif | |
236 | ||
237 | delete m_helpController; | |
4e4152e4 | 238 | delete wxConfig::Set(NULL); |
2b5f62a0 | 239 | |
4e4152e4 JS |
240 | return 0; |
241 | } | |
242 | ||
243 | bool hvApp::OpenBook(wxHtmlHelpController* controller) | |
244 | { | |
245 | wxString s = wxFileSelector(_("Open help file"), | |
246 | wxGetCwd(), | |
247 | wxEmptyString, | |
248 | wxEmptyString, | |
249 | _( | |
2b5f62a0 VZ |
250 | "Help books (*.htb)|*.htb|Help books (*.zip)|*.zip|\ |
251 | HTML Help Project (*.hhp)|*.hhp"), | |
252 | wxOPEN | wxFILE_MUST_EXIST, | |
253 | NULL); | |
254 | ||
4e4152e4 JS |
255 | if (!s.IsEmpty()) |
256 | { | |
257 | wxString ext = s.Right(4).Lower(); | |
258 | if (ext == _T(".zip") || ext == _T(".htb") || ext == _T(".hhp")) | |
259 | { | |
260 | wxBusyCursor bcur; | |
261 | controller->AddBook(s); | |
262 | return TRUE; | |
263 | } | |
264 | } | |
265 | return FALSE; | |
266 | } | |
267 | ||
268 | /* | |
2b5f62a0 VZ |
269 | * Art provider class |
270 | */ | |
4e4152e4 JS |
271 | |
272 | // --------------------------------------------------------------------- | |
273 | // helper macros | |
274 | // --------------------------------------------------------------------- | |
275 | ||
276 | // Standard macro for getting a resource from XPM file: | |
277 | #define ART(artId, xpmRc) \ | |
2b5f62a0 | 278 | if ( id == artId ) return wxBitmap(xpmRc##_xpm); |
4e4152e4 JS |
279 | |
280 | // Compatibility hack to use wxApp::GetStdIcon of overriden by the user | |
281 | #if WXWIN_COMPATIBILITY_2_2 | |
2b5f62a0 VZ |
282 | #define GET_STD_ICON_FROM_APP(iconId) \ |
283 | if ( client == wxART_MESSAGE_BOX ) \ | |
284 | { \ | |
285 | wxIcon icon = wxTheApp->GetStdIcon(iconId); \ | |
286 | if ( icon.Ok() ) \ | |
287 | { \ | |
288 | wxBitmap bmp; \ | |
289 | bmp.CopyFromIcon(icon); \ | |
290 | return bmp; \ | |
291 | } \ | |
292 | } | |
4e4152e4 | 293 | #else |
2b5f62a0 | 294 | #define GET_STD_ICON_FROM_APP(iconId) |
4e4152e4 JS |
295 | #endif |
296 | ||
297 | // There are two ways of getting the standard icon: either via XPMs or via | |
298 | // wxIcon ctor. This depends on the platform: | |
299 | #if defined(__WXUNIVERSAL__) | |
2b5f62a0 | 300 | #define CREATE_STD_ICON(iconId, xpmRc) return wxNullBitmap; |
4e4152e4 | 301 | #elif defined(__WXGTK__) || defined(__WXMOTIF__) |
2b5f62a0 | 302 | #define CREATE_STD_ICON(iconId, xpmRc) return wxBitmap(xpmRc##_xpm); |
4e4152e4 | 303 | #else |
2b5f62a0 VZ |
304 | #define CREATE_STD_ICON(iconId, xpmRc) \ |
305 | { \ | |
306 | wxIcon icon(_T(iconId)); \ | |
307 | wxBitmap bmp; \ | |
308 | bmp.CopyFromIcon(icon); \ | |
309 | return bmp; \ | |
310 | } | |
4e4152e4 JS |
311 | #endif |
312 | ||
313 | // Macro used in CreateBitmap to get wxICON_FOO icons: | |
314 | #define ART_MSGBOX(artId, iconId, xpmRc) \ | |
315 | if ( id == artId ) \ | |
2b5f62a0 VZ |
316 | { \ |
317 | GET_STD_ICON_FROM_APP(iconId) \ | |
318 | CREATE_STD_ICON(#iconId, xpmRc) \ | |
319 | } | |
4e4152e4 JS |
320 | |
321 | // --------------------------------------------------------------------- | |
322 | // XPMs with the art | |
323 | // --------------------------------------------------------------------- | |
324 | ||
325 | // XPM hack: make the arrays const | |
326 | //#define static static const | |
327 | ||
328 | #include "bitmaps/helpback.xpm" | |
329 | #include "bitmaps/helpbook.xpm" | |
330 | #include "bitmaps/helpdown.xpm" | |
331 | #include "bitmaps/helpforward.xpm" | |
332 | #include "bitmaps/helpoptions.xpm" | |
333 | #include "bitmaps/helppage.xpm" | |
334 | #include "bitmaps/helpsidepanel.xpm" | |
335 | #include "bitmaps/helpup.xpm" | |
336 | #include "bitmaps/helpuplevel.xpm" | |
337 | #include "bitmaps/helpicon.xpm" | |
338 | #include "bitmaps/helpopen.xpm" | |
339 | ||
340 | //#undef static | |
341 | ||
342 | // --------------------------------------------------------------------- | |
343 | // CreateBitmap routine | |
344 | // --------------------------------------------------------------------- | |
345 | ||
346 | wxBitmap AlternateArtProvider::CreateBitmap(const wxArtID& id, | |
347 | const wxArtClient& client, | |
348 | const wxSize& WXUNUSED(size)) | |
349 | { | |
350 | ART(wxART_HELP_SIDE_PANEL, helpsidepanel) | |
2b5f62a0 VZ |
351 | ART(wxART_HELP_SETTINGS, helpoptions) |
352 | ART(wxART_HELP_BOOK, helpbook) | |
353 | ART(wxART_HELP_FOLDER, helpbook) | |
354 | ART(wxART_HELP_PAGE, helppage) | |
355 | //ART(wxART_ADD_BOOKMARK, addbookm) | |
356 | //ART(wxART_DEL_BOOKMARK, delbookm) | |
357 | ART(wxART_GO_BACK, helpback) | |
358 | ART(wxART_GO_FORWARD, helpforward) | |
359 | ART(wxART_GO_UP, helpup) | |
360 | ART(wxART_GO_DOWN, helpdown) | |
361 | ART(wxART_GO_TO_PARENT, helpuplevel) | |
362 | ART(wxART_FILE_OPEN, helpopen) | |
363 | if (client == wxART_HELP_BROWSER) | |
364 | { | |
365 | //ART(wxART_FRAME_ICON, helpicon) | |
366 | ART(wxART_HELP, helpicon) | |
367 | } | |
368 | ||
369 | //ART(wxART_GO_HOME, home) | |
370 | ||
371 | // Any wxWindows icons not implemented here | |
372 | // will be provided by the default art provider. | |
373 | return wxNullBitmap; | |
374 | } | |
375 | ||
376 | #if hvUSE_IPC | |
377 | ||
378 | wxConnectionBase *hvServer::OnAcceptConnection(const wxString& topic) | |
379 | { | |
380 | if (topic == wxT("HELP")) | |
381 | return new hvConnection(); | |
382 | else | |
383 | return NULL; | |
384 | } | |
385 | ||
386 | // ---------------------------------------------------------------------------- | |
387 | // hvConnection | |
388 | // ---------------------------------------------------------------------------- | |
389 | ||
390 | hvConnection::hvConnection() | |
391 | : wxConnection() | |
392 | { | |
393 | wxGetApp().GetConnections().Append(this); | |
394 | } | |
395 | ||
396 | hvConnection::~hvConnection() | |
397 | { | |
398 | wxGetApp().GetConnections().DeleteObject(this); | |
399 | } | |
4e4152e4 | 400 | |
2b5f62a0 VZ |
401 | bool hvConnection::OnExecute(const wxString& WXUNUSED(topic), |
402 | wxChar *data, | |
403 | int WXUNUSED(size), | |
404 | wxIPCFormat WXUNUSED(format)) | |
405 | { | |
406 | // wxLogStatus("Execute command: %s", data); | |
407 | ||
408 | if ( !wxStrncmp( data, wxT("--intstring"), 11 ) ) | |
409 | { | |
410 | long i; | |
411 | wxString argStr = data; | |
412 | wxString numb = argStr.AfterLast(wxT('g')); | |
413 | if ( !(numb.ToLong(&i) ) ) { | |
414 | wxLogError( wxT("Integer conversion failed for --intstring") ); | |
415 | } | |
416 | else { | |
417 | wxGetApp().GetHelpController()->Display(int(i)); | |
418 | } | |
419 | } | |
420 | else | |
421 | { | |
422 | wxGetApp().GetHelpController()->Display(data); | |
423 | } | |
424 | ||
425 | return TRUE; | |
426 | } | |
4e4152e4 | 427 | |
2b5f62a0 VZ |
428 | bool hvConnection::OnPoke(const wxString& WXUNUSED(topic), |
429 | const wxString& item, | |
430 | wxChar *data, | |
431 | int WXUNUSED(size), | |
432 | wxIPCFormat WXUNUSED(format)) | |
433 | { | |
434 | // wxLogStatus("Poke command: %s = %s", item.c_str(), data); | |
435 | //topic is not tested | |
436 | ||
437 | if ( wxGetApp().GetHelpController() ) | |
438 | { | |
439 | if ( item == wxT("--AddBook") ) | |
440 | { | |
441 | wxGetApp().GetHelpController()->AddBook(data); | |
442 | } | |
443 | else if ( item == wxT("--DisplayContents") ) | |
444 | { | |
445 | wxGetApp().GetHelpController()->DisplayContents(); | |
446 | } | |
447 | else if ( item == wxT("--DisplayIndex") ) | |
448 | { | |
449 | wxGetApp().GetHelpController()->DisplayIndex(); | |
450 | } | |
451 | else if ( item == wxT("--KeywordSearch") ) | |
452 | { | |
453 | wxGetApp().GetHelpController()->KeywordSearch(data); | |
454 | } | |
455 | else if ( item == wxT("--SetTitleFormat") ) | |
456 | { | |
457 | wxString newname = data; | |
458 | newname.Replace( wxT("_"), wxT(" ") ); | |
459 | wxGetApp().GetHelpController()->SetTitleFormat(newname); | |
460 | //does not redraw title bar? | |
461 | //wxGetApp().GetHelpController()->ReFresh(); - or something | |
462 | } | |
463 | else if ( item == wxT("--SetTempDir") ) | |
464 | { | |
465 | wxGetApp().GetHelpController()->SetTempDir(data); | |
466 | } | |
467 | else if ( item == wxT("--YouAreDead") ) | |
468 | { | |
469 | // don't really know how to kill app from down here... | |
470 | // use wxKill from client instead | |
471 | //wxWindow *win = wxTheApp->GetTopWindow(); | |
472 | //if ( win ) | |
473 | // win->Destroy(); | |
474 | } | |
475 | } | |
476 | ||
477 | return TRUE; | |
4e4152e4 | 478 | } |
2b5f62a0 VZ |
479 | |
480 | wxChar *hvConnection::OnRequest(const wxString& WXUNUSED(topic), | |
481 | const wxString& WXUNUSED(item), | |
482 | int * WXUNUSED(size), | |
483 | wxIPCFormat WXUNUSED(format)) | |
484 | { | |
485 | return NULL; | |
486 | } | |
487 | ||
488 | bool hvConnection::OnStartAdvise(const wxString& WXUNUSED(topic), | |
489 | const wxString& WXUNUSED(item)) | |
490 | { | |
491 | return TRUE; | |
492 | } | |
493 | ||
494 | #endif | |
495 | // hvUSE_IPC |