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