Ifdefed m_server
[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 m_helpController->AddBook(book[i]);
206 }
207
208 #ifdef __WXMOTIF__
209 delete wxLog::SetActiveTarget(new wxLogGui);
210 #endif
211
212 m_helpController -> DisplayContents();
213
214 return TRUE;
215 }
216
217
218 int hvApp::OnExit()
219 {
220 #if hvUSE_IPC
221 wxNode* node = m_connections.First();
222 while (node)
223 {
224 wxNode* next = node->Next();
225 hvConnection* connection = (hvConnection*) node->Data();
226 connection->Disconnect();
227 delete connection;
228 node = next;
229 }
230 m_connections.Clear();
231
232 if (m_server)
233 {
234 delete m_server;
235 m_server = NULL;
236 }
237 #endif
238
239 delete m_helpController;
240 delete wxConfig::Set(NULL);
241
242 return 0;
243 }
244
245 bool hvApp::OpenBook(wxHtmlHelpController* controller)
246 {
247 wxString s = wxFileSelector(_("Open help file"),
248 wxGetCwd(),
249 wxEmptyString,
250 wxEmptyString,
251 _(
252 "Help books (*.htb)|*.htb|Help books (*.zip)|*.zip|\
253 HTML Help Project (*.hhp)|*.hhp"),
254 wxOPEN | wxFILE_MUST_EXIST,
255 NULL);
256
257 if (!s.IsEmpty())
258 {
259 wxString ext = s.Right(4).Lower();
260 if (ext == _T(".zip") || ext == _T(".htb") || ext == _T(".hhp"))
261 {
262 wxBusyCursor bcur;
263 controller->AddBook(s);
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