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