]> git.saurik.com Git - wxWidgets.git/blame - utils/helpview/src/helpview.cpp
fix vertical mouse wheel event rotation value, sign was reversed in r74805
[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
4e4152e4
JS
8// Copyright: (c) 2002 Vaclav Slavik, Julian Smart and others
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
4e4152e4
JS
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
be5a51fb 20// need because it includes almost all "standard" wxWidgets headers
4e4152e4
JS
21#ifndef WX_PRECOMP
22#include "wx/wx.h"
23#endif
24
d9b21c9f 25#include "wx/filename.h"
4e4152e4
JS
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
877c86a2
JS
33#include "helpview.h"
34
4e4152e4
JS
35class AlternateArtProvider : public wxArtProvider
36{
37protected:
38 virtual wxBitmap CreateBitmap(const wxArtID& id, const wxArtClient& client,
39 const wxSize& size);
40};
41
4e4152e4
JS
42IMPLEMENT_APP(hvApp)
43
2b5f62a0
VZ
44hvApp::hvApp()
45{
62d1f48b 46#if wxUSE_IPC
2b5f62a0
VZ
47 m_server = NULL;
48#endif
49}
4e4152e4
JS
50
51bool hvApp::OnInit()
52{
53#ifdef __WXMOTIF__
54 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
55#endif
03a90749 56
3fafce67 57 wxArtProvider::Push(new AlternateArtProvider);
03a90749 58
97c0c88b 59#if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
476d3114 60 wxApp::s_macAboutMenuItemId = wxID_ABOUT;
63fa3f89 61 wxFileName::MacRegisterDefaultTypeAndCreator( wxT("htb") , 'HTBD' , 'HTBA' ) ;
d9b21c9f
JS
62#endif
63
9d683b44 64 int istyle = wxHF_DEFAULT_STYLE;
03a90749 65
254a2129
WS
66 wxString service, windowName, titleFormat, argStr;
67 wxString book[10];
2b5f62a0
VZ
68 int bookCount = 0;
69 int i;
03a90749
DS
70 bool hasService = false;
71 bool hasWindowName = false;
72 bool createServer = false;
73
62d1f48b 74#if wxUSE_IPC
2b5f62a0 75 m_server = NULL;
43b8a532 76#endif
03a90749 77
2b5f62a0
VZ
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
03a90749 82
2b5f62a0
VZ
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
03a90749
DS
91
92 for( i=1; i<argc; i++ )
2b5f62a0 93 {
03a90749
DS
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;
2b5f62a0 106#if defined(__WXMSW__)
03a90749 107 service = wxT("generic_helpservice");
2b5f62a0 108#elif defined(__UNIX__)
03a90749 109 service = wxT("/tmp/") + wxString(wxT("generic_helpservice"));
2b5f62a0 110#else
03a90749 111 service = wxT("4242");
2b5f62a0 112#endif
03a90749
DS
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 }
2b5f62a0 142 }
03a90749 143
d9b21c9f
JS
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__
2b5f62a0
VZ
147 if ( bookCount < 1 )
148 {
03a90749
DS
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"),
55325d01 154 wxFD_OPEN | wxFD_FILE_MUST_EXIST,
03a90749
DS
155 NULL);
156
62d1f48b 157 if (!s.empty())
03a90749
DS
158 {
159 book[0] = s;
160 bookCount = 1;
161 }
162 }
d9b21c9f 163#endif
03a90749 164
62d1f48b 165#if wxUSE_IPC
03a90749
DS
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);
2b5f62a0 182 }
03a90749 183
62d1f48b 184#endif // wxUSE_IPC
03a90749 185
2b5f62a0 186 //now add help
4e4152e4 187 wxInitAllImageHandlers();
03a90749
DS
188 wxFileSystem::AddHandler(new wxZipFSHandler);
189
be5a51fb 190 SetVendorName(wxT("wxWidgets") );
03a90749 191 SetAppName(wxT("wxHTMLHelpServer") );
4e4152e4 192 wxConfig::Get(); // create an instance
03a90749 193
2b5f62a0 194 m_helpController = new wxHtmlHelpController( istyle );
03a90749 195
2b5f62a0 196 if ( !hasWindowName )
03a90749 197 {
2b5f62a0 198 titleFormat = wxT("Help: %s") ;
03a90749 199 }
2b5f62a0
VZ
200 else
201 {
03a90749
DS
202 //remove underscores
203 windowName.Replace( wxT("_"), wxT(" ") );
204 titleFormat = windowName;
4e4152e4 205 }
03a90749 206
2b5f62a0 207 m_helpController->SetTitleFormat( titleFormat );
03a90749
DS
208
209 for( i=0; i<bookCount; i++ )
2b5f62a0 210 {
dba06cd0 211 wxFileName fileName(book[i]);
03a90749 212 m_helpController->AddBook(fileName);
2b5f62a0 213 }
03a90749 214
4e4152e4
JS
215#ifdef __WXMOTIF__
216 delete wxLog::SetActiveTarget(new wxLogGui);
217#endif
03a90749
DS
218
219 m_helpController->DisplayContents();
220
221 return true;
4e4152e4
JS
222}
223
224
225int hvApp::OnExit()
226{
62d1f48b 227#if wxUSE_IPC
8d0f1c1c 228 wxObjectList::compatibility_iterator node = m_connections.GetFirst();
2b5f62a0
VZ
229 while (node)
230 {
8d0f1c1c 231 wxObjectList::compatibility_iterator next = node->GetNext();
ddc4f3b5 232 hvConnection* connection = (hvConnection*) node->GetData();
2b5f62a0
VZ
233 connection->Disconnect();
234 delete connection;
235 node = next;
236 }
237 m_connections.Clear();
03a90749 238
2b5f62a0
VZ
239 if (m_server)
240 {
241 delete m_server;
242 m_server = NULL;
243 }
244#endif
03a90749 245
2b5f62a0 246 delete m_helpController;
4e4152e4 247 delete wxConfig::Set(NULL);
03a90749 248
4e4152e4
JS
249 return 0;
250}
251
252bool hvApp::OpenBook(wxHtmlHelpController* controller)
253{
254 wxString s = wxFileSelector(_("Open help file"),
255 wxGetCwd(),
256 wxEmptyString,
257 wxEmptyString,
258 _(
03a90749
DS
259 "Help books (*.htb)|*.htb|Help books (*.zip)|*.zip|\
260 HTML Help Project (*.hhp)|*.hhp"),
3fafce67 261 wxFD_OPEN | wxFD_FILE_MUST_EXIST,
03a90749
DS
262 NULL);
263
264 if ( !s.empty() )
4e4152e4
JS
265 {
266 wxString ext = s.Right(4).Lower();
9a83f860 267 if (ext == wxT(".zip") || ext == wxT(".htb") || ext == wxT(".hhp"))
4e4152e4
JS
268 {
269 wxBusyCursor bcur;
dba06cd0
JS
270 wxFileName fileName(s);
271 controller->AddBook(fileName);
03a90749 272 return true;
4e4152e4
JS
273 }
274 }
03a90749
DS
275
276 return false;
4e4152e4
JS
277}
278
d9b21c9f
JS
279#ifdef __WXMAC__
280/// Respond to Apple Event for opening a document
ee7553e9 281void hvApp::MacOpenFiles(const wxArrayString& fileNames)
d9b21c9f
JS
282{
283 wxBusyCursor bcur;
ee7553e9 284 wxFileName fileName(fileNames[0]);
d9b21c9f
JS
285 m_helpController->AddBook(fileName);
286 m_helpController->DisplayContents();
287}
288#endif
289
290
4e4152e4 291/*
2b5f62a0
VZ
292* Art provider class
293*/
4e4152e4
JS
294
295// ---------------------------------------------------------------------
296// helper macros
297// ---------------------------------------------------------------------
298
299// Standard macro for getting a resource from XPM file:
300#define ART(artId, xpmRc) \
2b5f62a0 301if ( id == artId ) return wxBitmap(xpmRc##_xpm);
4e4152e4 302
2b5f62a0 303#define GET_STD_ICON_FROM_APP(iconId)
4e4152e4
JS
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__)
2b5f62a0 308#define CREATE_STD_ICON(iconId, xpmRc) return wxNullBitmap;
4e4152e4 309#elif defined(__WXGTK__) || defined(__WXMOTIF__)
2b5f62a0 310#define CREATE_STD_ICON(iconId, xpmRc) return wxBitmap(xpmRc##_xpm);
4e4152e4 311#else
2b5f62a0
VZ
312#define CREATE_STD_ICON(iconId, xpmRc) \
313{ \
9a83f860 314 wxIcon icon(wxT(iconId)); \
03a90749
DS
315 wxBitmap bmp; \
316 bmp.CopyFromIcon(icon); \
317 return bmp; \
2b5f62a0 318}
4e4152e4
JS
319#endif
320
321// Macro used in CreateBitmap to get wxICON_FOO icons:
322#define ART_MSGBOX(artId, iconId, xpmRc) \
323 if ( id == artId ) \
2b5f62a0 324{ \
03a90749
DS
325 GET_STD_ICON_FROM_APP(iconId) \
326 CREATE_STD_ICON(#iconId, xpmRc) \
2b5f62a0 327}
4e4152e4
JS
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
354wxBitmap AlternateArtProvider::CreateBitmap(const wxArtID& id,
355 const wxArtClient& client,
356 const wxSize& WXUNUSED(size))
357{
358 ART(wxART_HELP_SIDE_PANEL, helpsidepanel)
03a90749
DS
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
be5a51fb 379 // Any wxWidgets icons not implemented here
03a90749
DS
380 // will be provided by the default art provider.
381 return wxNullBitmap;
2b5f62a0
VZ
382}
383
62d1f48b 384#if wxUSE_IPC
2b5f62a0
VZ
385
386wxConnectionBase *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
398hvConnection::hvConnection()
399: wxConnection()
400{
401 wxGetApp().GetConnections().Append(this);
402}
403
404hvConnection::~hvConnection()
405{
406 wxGetApp().GetConnections().DeleteObject(this);
407}
4e4152e4 408
587b4c84
VZ
409bool hvConnection::OnExec(const wxString& WXUNUSED(topic),
410 const wxString& data)
2b5f62a0 411{
03a90749
DS
412 // wxLogStatus("Execute command: %s", data);
413
587b4c84 414 if ( data == "--intstring" )
03a90749 415 {
2b5f62a0 416 long i;
03a90749
DS
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;
2b5f62a0 434}
4e4152e4 435
2b5f62a0
VZ
436bool hvConnection::OnPoke(const wxString& WXUNUSED(topic),
437 const wxString& item,
bcb6619f
VZ
438 const void *buf,
439 size_t size,
440 wxIPCFormat format)
2b5f62a0 441{
bcb6619f
VZ
442 const wxString data = GetTextFromData(buf, size, format);
443
03a90749
DS
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;
4e4152e4 488}
2b5f62a0 489
62d1f48b 490#endif // #if wxUSE_IPC