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