]> git.saurik.com Git - wxWidgets.git/blob - utils/helpview/src/helpview.cpp
4b445e00651ae96803a0bb53071546aff7e96c0a
[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 // Copyright: (c) 2002 Vaclav Slavik, Julian Smart and others
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 // for all others, include the necessary headers (this file is usually all you
20 // need because it includes almost all "standard" wxWidgets headers
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #endif
24
25 #include "wx/filename.h"
26 #include "wx/image.h"
27 #include "wx/wxhtml.h"
28 #include "wx/fs_zip.h"
29 #include "wx/log.h"
30 #include "wx/artprov.h"
31 #include "wx/filedlg.h"
32
33 #include "helpview.h"
34
35 class AlternateArtProvider : public wxArtProvider
36 {
37 protected:
38 virtual wxBitmap CreateBitmap(const wxArtID& id, const wxArtClient& client,
39 const wxSize& size);
40 };
41
42 IMPLEMENT_APP(hvApp)
43
44 hvApp::hvApp()
45 {
46 #if wxUSE_IPC
47 m_server = NULL;
48 #endif
49 }
50
51 bool hvApp::OnInit()
52 {
53 #ifdef __WXMOTIF__
54 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
55 #endif
56
57 wxArtProvider::Push(new AlternateArtProvider);
58
59 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
60 wxApp::s_macAboutMenuItemId = wxID_ABOUT;
61 wxFileName::MacRegisterDefaultTypeAndCreator( wxT("htb") , 'HTBD' , 'HTBA' ) ;
62 #endif
63
64 int istyle = wxHF_DEFAULT_STYLE;
65
66 wxString service, windowName, titleFormat, argStr;
67 wxString book[10];
68 int bookCount = 0;
69 int i;
70 bool hasService = false;
71 bool hasWindowName = false;
72 bool createServer = false;
73
74 #if wxUSE_IPC
75 m_server = NULL;
76 #endif
77
78 // Help books are recognized by extension ".hhp" ".htb" or ".zip".
79 // Service and window_name can occur anywhere in arguments,
80 // but service must be first
81 // Other arguments (topic?) could be added
82
83 // modes of operation:
84 // 1) no arguments - stand alone, prompt user for book
85 // 2) books only - stand alone, open books
86 // 3) "--server" as (any) arg - start connection with default service;
87 // also open any books passed as other arguments
88 // 4) at least one argument which is not book, and not "--server" - take first
89 // such argument as service, second (if present) as window name,
90 // start service, open any books
91
92 for( i=1; i<argc; i++ )
93 {
94 argStr = argv[i];
95
96 if ( argStr.Find( wxT(".hhp") ) >= 0
97 || argStr.Find( wxT(".htb") ) >= 0
98 || argStr.Find( wxT(".zip") ) >= 0 )
99 {
100 book[bookCount] = argStr;
101 bookCount++;
102 }
103 else if ( argStr == wxT("--server") )
104 {
105 createServer = true;
106 #if defined(__WXMSW__)
107 service = wxT("generic_helpservice");
108 #elif defined(__UNIX__)
109 service = wxT("/tmp/") + wxString(wxT("generic_helpservice"));
110 #else
111 service = wxT("4242");
112 #endif
113 }
114 else if ( !hasService )
115 {
116 service = argStr;
117 hasService = true;
118 createServer = true;
119 }
120 else if ( !hasWindowName )
121 {
122 windowName = argStr;
123 hasWindowName = true;
124 }
125 else if ( argStr.Find( wxT("--Style") ) >= 0 )
126 {
127 long i;
128 wxString numb = argStr.AfterLast(wxT('e'));
129 if ( !(numb.ToLong(&i) ) )
130 {
131 wxLogError( wxT("Integer conversion failed for --Style") );
132 }
133 else
134 {
135 istyle = i;
136 }
137 }
138 else
139 {
140 //unknown - could be topic?
141 }
142 }
143
144 // No book - query user; but not on Mac, since there
145 // may be an AppleEvent to open a document on the way
146 #ifndef __WXMAC__
147 if ( bookCount < 1 )
148 {
149 wxString s = wxFileSelector( wxT("Open help file"),
150 wxGetCwd(),
151 wxEmptyString,
152 wxEmptyString,
153 wxT("Help books (*.htb)|*.htb|Help books (*.zip)|*.zip|HTML Help Project (*.hhp)|*.hhp"),
154 wxFD_OPEN | wxFD_FILE_MUST_EXIST,
155 NULL);
156
157 if (!s.empty())
158 {
159 book[0] = s;
160 bookCount = 1;
161 }
162 }
163 #endif
164
165 #if wxUSE_IPC
166
167 if ( createServer )
168 {
169 // Create a new server
170 m_server = new hvServer;
171
172 if ( !m_server->Create(service) )
173 {
174 wxString wxm = wxT("Server Create failed - service: ");
175 wxString xxm = wxm << service;
176 wxLogError( xxm );
177 //if MSW quits here, probably another copy already exists
178 return false;
179 }
180 createServer = false;
181 wxUnusedVar(createServer);
182 }
183
184 #endif // wxUSE_IPC
185
186 //now add help
187 wxInitAllImageHandlers();
188 wxFileSystem::AddHandler(new wxZipFSHandler);
189
190 SetVendorName(wxT("wxWidgets") );
191 SetAppName(wxT("wxHTMLHelpServer") );
192 wxConfig::Get(); // create an instance
193
194 m_helpController = new wxHtmlHelpController( istyle );
195
196 if ( !hasWindowName )
197 {
198 titleFormat = wxT("Help: %s") ;
199 }
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 wxUSE_IPC
228 wxObjectList::compatibility_iterator node = m_connections.GetFirst();
229 while (node)
230 {
231 wxObjectList::compatibility_iterator next = node->GetNext();
232 hvConnection* connection = (hvConnection*) node->GetData();
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 wxFD_OPEN | wxFD_FILE_MUST_EXIST,
262 NULL);
263
264 if ( !s.empty() )
265 {
266 wxString ext = s.Right(4).Lower();
267 if (ext == wxT(".zip") || ext == wxT(".htb") || ext == wxT(".hhp"))
268 {
269 wxBusyCursor bcur;
270 wxFileName fileName(s);
271 controller->AddBook(fileName);
272 return true;
273 }
274 }
275
276 return false;
277 }
278
279 #ifdef __WXMAC__
280 /// Respond to Apple Event for opening a document
281 void hvApp::MacOpenFiles(const wxArrayString& fileNames)
282 {
283 wxBusyCursor bcur;
284 wxFileName fileName(fileNames[0]);
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 #define GET_STD_ICON_FROM_APP(iconId)
304
305 // There are two ways of getting the standard icon: either via XPMs or via
306 // wxIcon ctor. This depends on the platform:
307 #if defined(__WXUNIVERSAL__)
308 #define CREATE_STD_ICON(iconId, xpmRc) return wxNullBitmap;
309 #elif defined(__WXGTK__) || defined(__WXMOTIF__)
310 #define CREATE_STD_ICON(iconId, xpmRc) return wxBitmap(xpmRc##_xpm);
311 #else
312 #define CREATE_STD_ICON(iconId, xpmRc) \
313 { \
314 wxIcon icon(wxT(iconId)); \
315 wxBitmap bmp; \
316 bmp.CopyFromIcon(icon); \
317 return bmp; \
318 }
319 #endif
320
321 // Macro used in CreateBitmap to get wxICON_FOO icons:
322 #define ART_MSGBOX(artId, iconId, xpmRc) \
323 if ( id == artId ) \
324 { \
325 GET_STD_ICON_FROM_APP(iconId) \
326 CREATE_STD_ICON(#iconId, xpmRc) \
327 }
328
329 // ---------------------------------------------------------------------
330 // XPMs with the art
331 // ---------------------------------------------------------------------
332
333 // XPM hack: make the arrays const
334 //#define static static const
335
336 #include "bitmaps/helpback.xpm"
337 #include "bitmaps/helpbook.xpm"
338 #include "bitmaps/helpdown.xpm"
339 #include "bitmaps/helpforward.xpm"
340 #include "bitmaps/helpoptions.xpm"
341 #include "bitmaps/helppage.xpm"
342 #include "bitmaps/helpsidepanel.xpm"
343 #include "bitmaps/helpup.xpm"
344 #include "bitmaps/helpuplevel.xpm"
345 #include "bitmaps/helpicon.xpm"
346 #include "bitmaps/helpopen.xpm"
347
348 //#undef static
349
350 // ---------------------------------------------------------------------
351 // CreateBitmap routine
352 // ---------------------------------------------------------------------
353
354 wxBitmap AlternateArtProvider::CreateBitmap(const wxArtID& id,
355 const wxArtClient& client,
356 const wxSize& WXUNUSED(size))
357 {
358 ART(wxART_HELP_SIDE_PANEL, helpsidepanel)
359 ART(wxART_HELP_SETTINGS, helpoptions)
360 ART(wxART_HELP_BOOK, helpbook)
361 ART(wxART_HELP_FOLDER, helpbook)
362 ART(wxART_HELP_PAGE, helppage)
363 //ART(wxART_ADD_BOOKMARK, addbookm)
364 //ART(wxART_DEL_BOOKMARK, delbookm)
365 ART(wxART_GO_BACK, helpback)
366 ART(wxART_GO_FORWARD, helpforward)
367 ART(wxART_GO_UP, helpup)
368 ART(wxART_GO_DOWN, helpdown)
369 ART(wxART_GO_TO_PARENT, helpuplevel)
370 ART(wxART_FILE_OPEN, helpopen)
371 if (client == wxART_HELP_BROWSER)
372 {
373 //ART(wxART_FRAME_ICON, helpicon)
374 ART(wxART_HELP, helpicon)
375 }
376
377 //ART(wxART_GO_HOME, home)
378
379 // Any wxWidgets icons not implemented here
380 // will be provided by the default art provider.
381 return wxNullBitmap;
382 }
383
384 #if wxUSE_IPC
385
386 wxConnectionBase *hvServer::OnAcceptConnection(const wxString& topic)
387 {
388 if (topic == wxT("HELP"))
389 return new hvConnection();
390 else
391 return NULL;
392 }
393
394 // ----------------------------------------------------------------------------
395 // hvConnection
396 // ----------------------------------------------------------------------------
397
398 hvConnection::hvConnection()
399 : wxConnection()
400 {
401 wxGetApp().GetConnections().Append(this);
402 }
403
404 hvConnection::~hvConnection()
405 {
406 wxGetApp().GetConnections().DeleteObject(this);
407 }
408
409 bool hvConnection::OnExec(const wxString& WXUNUSED(topic),
410 const wxString& data)
411 {
412 // wxLogStatus("Execute command: %s", data);
413
414 if ( data == "--intstring" )
415 {
416 long i;
417 wxString argStr = data;
418 wxString numb = argStr.AfterLast(wxT('g'));
419 if ( !(numb.ToLong(&i) ) )
420 {
421 wxLogError( wxT("Integer conversion failed for --intstring") );
422 }
423 else
424 {
425 wxGetApp().GetHelpController()->Display(int(i));
426 }
427 }
428 else
429 {
430 wxGetApp().GetHelpController()->Display(data);
431 }
432
433 return true;
434 }
435
436 bool hvConnection::OnPoke(const wxString& WXUNUSED(topic),
437 const wxString& item,
438 const void *buf,
439 size_t size,
440 wxIPCFormat format)
441 {
442 const wxString data = GetTextFromData(buf, size, format);
443
444 // wxLogStatus("Poke command: %s = %s", item.c_str(), data);
445 //topic is not tested
446
447 if ( wxGetApp().GetHelpController() )
448 {
449 if ( item == wxT("--AddBook") )
450 {
451 wxGetApp().GetHelpController()->AddBook(data);
452 }
453 else if ( item == wxT("--DisplayContents") )
454 {
455 wxGetApp().GetHelpController()->DisplayContents();
456 }
457 else if ( item == wxT("--DisplayIndex") )
458 {
459 wxGetApp().GetHelpController()->DisplayIndex();
460 }
461 else if ( item == wxT("--KeywordSearch") )
462 {
463 wxGetApp().GetHelpController()->KeywordSearch(data);
464 }
465 else if ( item == wxT("--SetTitleFormat") )
466 {
467 wxString newname = data;
468 newname.Replace( wxT("_"), wxT(" ") );
469 wxGetApp().GetHelpController()->SetTitleFormat(newname);
470 //does not redraw title bar?
471 //wxGetApp().GetHelpController()->ReFresh(); - or something
472 }
473 else if ( item == wxT("--SetTempDir") )
474 {
475 wxGetApp().GetHelpController()->SetTempDir(data);
476 }
477 else if ( item == wxT("--YouAreDead") )
478 {
479 // don't really know how to kill app from down here...
480 // use wxKill from client instead
481 //wxWindow *win = wxTheApp->GetTopWindow();
482 //if ( win )
483 // win->Destroy();
484 }
485 }
486
487 return true;
488 }
489
490 #endif // #if wxUSE_IPC