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