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