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