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