]> git.saurik.com Git - wxWidgets.git/blob - utils/helpview/src/helpview.cpp
added XRC contrib makefile
[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(
157 "Help books (*.htb)|*.htb|Help books (*.zip)|*.zip|\
158 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 // Create a new server
174 m_server = new hvServer;
175
176 if ( !m_server->Create(service) ) {
177 wxString wxm = wxT("Server Create failed - service: ");
178 wxString xxm = wxm << service;
179 wxLogError( xxm );
180 //if MSW quits here, probably another copy already exists
181 return FALSE;
182
183 }
184 createServer = FALSE;
185 }
186
187 #endif // hvUSE_IPC
188
189 //now add help
190 wxInitAllImageHandlers();
191 wxFileSystem::AddHandler(new wxZipFSHandler);
192
193 SetVendorName(wxT("wxWindows") );
194 SetAppName(wxT("wxHTMLHelpServer") );
195 wxConfig::Get(); // create an instance
196
197 m_helpController = new wxHtmlHelpController( istyle );
198
199 if ( !hasWindowName )
200 titleFormat = wxT("Help: %s") ;
201 else
202 {
203 //remove underscores
204 windowName.Replace( wxT("_"), wxT(" ") );
205 titleFormat = windowName;
206 }
207
208 m_helpController->SetTitleFormat( titleFormat );
209
210 for( i=0; i < bookCount; i++ )
211 {
212 wxFileName fileName(book[i]);
213 m_helpController->AddBook(fileName);
214 }
215
216 #ifdef __WXMOTIF__
217 delete wxLog::SetActiveTarget(new wxLogGui);
218 #endif
219
220 m_helpController -> DisplayContents();
221
222 return TRUE;
223 }
224
225
226 int hvApp::OnExit()
227 {
228 #if hvUSE_IPC
229 wxNode* node = m_connections.First();
230 while (node)
231 {
232 wxNode* next = node->Next();
233 hvConnection* connection = (hvConnection*) node->Data();
234 connection->Disconnect();
235 delete connection;
236 node = next;
237 }
238 m_connections.Clear();
239
240 if (m_server)
241 {
242 delete m_server;
243 m_server = NULL;
244 }
245 #endif
246
247 delete m_helpController;
248 delete wxConfig::Set(NULL);
249
250 return 0;
251 }
252
253 bool hvApp::OpenBook(wxHtmlHelpController* controller)
254 {
255 wxString s = wxFileSelector(_("Open help file"),
256 wxGetCwd(),
257 wxEmptyString,
258 wxEmptyString,
259 _(
260 "Help books (*.htb)|*.htb|Help books (*.zip)|*.zip|\
261 HTML Help Project (*.hhp)|*.hhp"),
262 wxOPEN | wxFILE_MUST_EXIST,
263 NULL);
264
265 if (!s.IsEmpty())
266 {
267 wxString ext = s.Right(4).Lower();
268 if (ext == _T(".zip") || ext == _T(".htb") || ext == _T(".hhp"))
269 {
270 wxBusyCursor bcur;
271 wxFileName fileName(s);
272 controller->AddBook(fileName);
273 return TRUE;
274 }
275 }
276 return FALSE;
277 }
278
279 #ifdef __WXMAC__
280 /// Respond to Apple Event for opening a document
281 void hvApp::MacOpenFile(const wxString& filename)
282 {
283 wxBusyCursor bcur;
284 wxFileName fileName(filename);
285 m_helpController->AddBook(fileName);
286 m_helpController->DisplayContents();
287 }
288 #endif
289
290
291 /*
292 * Art provider class
293 */
294
295 // ---------------------------------------------------------------------
296 // helper macros
297 // ---------------------------------------------------------------------
298
299 // Standard macro for getting a resource from XPM file:
300 #define ART(artId, xpmRc) \
301 if ( id == artId ) return wxBitmap(xpmRc##_xpm);
302
303 // Compatibility hack to use wxApp::GetStdIcon of overriden by the user
304 #if WXWIN_COMPATIBILITY_2_2
305 #define GET_STD_ICON_FROM_APP(iconId) \
306 if ( client == wxART_MESSAGE_BOX ) \
307 { \
308 wxIcon icon = wxTheApp->GetStdIcon(iconId); \
309 if ( icon.Ok() ) \
310 { \
311 wxBitmap bmp; \
312 bmp.CopyFromIcon(icon); \
313 return bmp; \
314 } \
315 }
316 #else
317 #define GET_STD_ICON_FROM_APP(iconId)
318 #endif
319
320 // There are two ways of getting the standard icon: either via XPMs or via
321 // wxIcon ctor. This depends on the platform:
322 #if defined(__WXUNIVERSAL__)
323 #define CREATE_STD_ICON(iconId, xpmRc) return wxNullBitmap;
324 #elif defined(__WXGTK__) || defined(__WXMOTIF__)
325 #define CREATE_STD_ICON(iconId, xpmRc) return wxBitmap(xpmRc##_xpm);
326 #else
327 #define CREATE_STD_ICON(iconId, xpmRc) \
328 { \
329 wxIcon icon(_T(iconId)); \
330 wxBitmap bmp; \
331 bmp.CopyFromIcon(icon); \
332 return bmp; \
333 }
334 #endif
335
336 // Macro used in CreateBitmap to get wxICON_FOO icons:
337 #define ART_MSGBOX(artId, iconId, xpmRc) \
338 if ( id == artId ) \
339 { \
340 GET_STD_ICON_FROM_APP(iconId) \
341 CREATE_STD_ICON(#iconId, xpmRc) \
342 }
343
344 // ---------------------------------------------------------------------
345 // XPMs with the art
346 // ---------------------------------------------------------------------
347
348 // XPM hack: make the arrays const
349 //#define static static const
350
351 #include "bitmaps/helpback.xpm"
352 #include "bitmaps/helpbook.xpm"
353 #include "bitmaps/helpdown.xpm"
354 #include "bitmaps/helpforward.xpm"
355 #include "bitmaps/helpoptions.xpm"
356 #include "bitmaps/helppage.xpm"
357 #include "bitmaps/helpsidepanel.xpm"
358 #include "bitmaps/helpup.xpm"
359 #include "bitmaps/helpuplevel.xpm"
360 #include "bitmaps/helpicon.xpm"
361 #include "bitmaps/helpopen.xpm"
362
363 //#undef static
364
365 // ---------------------------------------------------------------------
366 // CreateBitmap routine
367 // ---------------------------------------------------------------------
368
369 wxBitmap AlternateArtProvider::CreateBitmap(const wxArtID& id,
370 const wxArtClient& client,
371 const wxSize& WXUNUSED(size))
372 {
373 ART(wxART_HELP_SIDE_PANEL, helpsidepanel)
374 ART(wxART_HELP_SETTINGS, helpoptions)
375 ART(wxART_HELP_BOOK, helpbook)
376 ART(wxART_HELP_FOLDER, helpbook)
377 ART(wxART_HELP_PAGE, helppage)
378 //ART(wxART_ADD_BOOKMARK, addbookm)
379 //ART(wxART_DEL_BOOKMARK, delbookm)
380 ART(wxART_GO_BACK, helpback)
381 ART(wxART_GO_FORWARD, helpforward)
382 ART(wxART_GO_UP, helpup)
383 ART(wxART_GO_DOWN, helpdown)
384 ART(wxART_GO_TO_PARENT, helpuplevel)
385 ART(wxART_FILE_OPEN, helpopen)
386 if (client == wxART_HELP_BROWSER)
387 {
388 //ART(wxART_FRAME_ICON, helpicon)
389 ART(wxART_HELP, helpicon)
390 }
391
392 //ART(wxART_GO_HOME, home)
393
394 // Any wxWindows icons not implemented here
395 // will be provided by the default art provider.
396 return wxNullBitmap;
397 }
398
399 #if hvUSE_IPC
400
401 wxConnectionBase *hvServer::OnAcceptConnection(const wxString& topic)
402 {
403 if (topic == wxT("HELP"))
404 return new hvConnection();
405 else
406 return NULL;
407 }
408
409 // ----------------------------------------------------------------------------
410 // hvConnection
411 // ----------------------------------------------------------------------------
412
413 hvConnection::hvConnection()
414 : wxConnection()
415 {
416 wxGetApp().GetConnections().Append(this);
417 }
418
419 hvConnection::~hvConnection()
420 {
421 wxGetApp().GetConnections().DeleteObject(this);
422 }
423
424 bool hvConnection::OnExecute(const wxString& WXUNUSED(topic),
425 wxChar *data,
426 int WXUNUSED(size),
427 wxIPCFormat WXUNUSED(format))
428 {
429 // wxLogStatus("Execute command: %s", data);
430
431 if ( !wxStrncmp( data, wxT("--intstring"), 11 ) )
432 {
433 long i;
434 wxString argStr = data;
435 wxString numb = argStr.AfterLast(wxT('g'));
436 if ( !(numb.ToLong(&i) ) ) {
437 wxLogError( wxT("Integer conversion failed for --intstring") );
438 }
439 else {
440 wxGetApp().GetHelpController()->Display(int(i));
441 }
442 }
443 else
444 {
445 wxGetApp().GetHelpController()->Display(data);
446 }
447
448 return TRUE;
449 }
450
451 bool hvConnection::OnPoke(const wxString& WXUNUSED(topic),
452 const wxString& item,
453 wxChar *data,
454 int WXUNUSED(size),
455 wxIPCFormat WXUNUSED(format))
456 {
457 // wxLogStatus("Poke command: %s = %s", item.c_str(), data);
458 //topic is not tested
459
460 if ( wxGetApp().GetHelpController() )
461 {
462 if ( item == wxT("--AddBook") )
463 {
464 wxGetApp().GetHelpController()->AddBook(data);
465 }
466 else if ( item == wxT("--DisplayContents") )
467 {
468 wxGetApp().GetHelpController()->DisplayContents();
469 }
470 else if ( item == wxT("--DisplayIndex") )
471 {
472 wxGetApp().GetHelpController()->DisplayIndex();
473 }
474 else if ( item == wxT("--KeywordSearch") )
475 {
476 wxGetApp().GetHelpController()->KeywordSearch(data);
477 }
478 else if ( item == wxT("--SetTitleFormat") )
479 {
480 wxString newname = data;
481 newname.Replace( wxT("_"), wxT(" ") );
482 wxGetApp().GetHelpController()->SetTitleFormat(newname);
483 //does not redraw title bar?
484 //wxGetApp().GetHelpController()->ReFresh(); - or something
485 }
486 else if ( item == wxT("--SetTempDir") )
487 {
488 wxGetApp().GetHelpController()->SetTempDir(data);
489 }
490 else if ( item == wxT("--YouAreDead") )
491 {
492 // don't really know how to kill app from down here...
493 // use wxKill from client instead
494 //wxWindow *win = wxTheApp->GetTopWindow();
495 //if ( win )
496 // win->Destroy();
497 }
498 }
499
500 return TRUE;
501 }
502
503 wxChar *hvConnection::OnRequest(const wxString& WXUNUSED(topic),
504 const wxString& WXUNUSED(item),
505 int * WXUNUSED(size),
506 wxIPCFormat WXUNUSED(format))
507 {
508 return NULL;
509 }
510
511 bool hvConnection::OnStartAdvise(const wxString& WXUNUSED(topic),
512 const wxString& WXUNUSED(item))
513 {
514 return TRUE;
515 }
516
517 #endif
518 // hvUSE_IPC