PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/Dependencies/wxWidgets/utils/helpview/src/helpview.cpp

https://github.com/goofoo/Helium
C++ | 491 lines | 349 code | 70 blank | 72 comment | 63 complexity | bb060ce380f2da5a662167d4eb27957c MD5 | raw file
  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. // For compilers that support precompilation, includes "wx/wx.h".
  13. #include "wx/wxprec.h"
  14. #ifdef __BORLANDC__
  15. #pragma hdrstop
  16. #endif
  17. // for all others, include the necessary headers (this file is usually all you
  18. // need because it includes almost all "standard" wxWidgets headers
  19. #ifndef WX_PRECOMP
  20. #include "wx/wx.h"
  21. #endif
  22. #include "wx/filename.h"
  23. #include "wx/image.h"
  24. #include "wx/wxhtml.h"
  25. #include "wx/fs_zip.h"
  26. #include "wx/log.h"
  27. #include "wx/artprov.h"
  28. #include "wx/filedlg.h"
  29. #include "helpview.h"
  30. class AlternateArtProvider : public wxArtProvider
  31. {
  32. protected:
  33. virtual wxBitmap CreateBitmap(const wxArtID& id, const wxArtClient& client,
  34. const wxSize& size);
  35. };
  36. IMPLEMENT_APP(hvApp)
  37. hvApp::hvApp()
  38. {
  39. #if wxUSE_IPC
  40. m_server = NULL;
  41. #endif
  42. }
  43. bool hvApp::OnInit()
  44. {
  45. #ifdef __WXMOTIF__
  46. delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
  47. #endif
  48. wxArtProvider::Push(new AlternateArtProvider);
  49. #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
  50. wxApp::s_macAboutMenuItemId = wxID_ABOUT;
  51. wxFileName::MacRegisterDefaultTypeAndCreator( wxT("htb") , 'HTBD' , 'HTBA' ) ;
  52. #endif
  53. int istyle = wxHF_DEFAULT_STYLE;
  54. wxString service, windowName, titleFormat, argStr;
  55. wxString book[10];
  56. int bookCount = 0;
  57. int i;
  58. bool hasService = false;
  59. bool hasWindowName = false;
  60. bool createServer = false;
  61. #if wxUSE_IPC
  62. m_server = NULL;
  63. #endif
  64. // Help books are recognized by extension ".hhp" ".htb" or ".zip".
  65. // Service and window_name can occur anywhere in arguments,
  66. // but service must be first
  67. // Other arguments (topic?) could be added
  68. // modes of operation:
  69. // 1) no arguments - stand alone, prompt user for book
  70. // 2) books only - stand alone, open books
  71. // 3) "--server" as (any) arg - start connection with default service;
  72. // also open any books passed as other arguments
  73. // 4) at least one argument which is not book, and not "--server" - take first
  74. // such argument as service, second (if present) as window name,
  75. // start service, open any books
  76. for( i=1; i<argc; i++ )
  77. {
  78. argStr = argv[i];
  79. if ( argStr.Find( wxT(".hhp") ) >= 0
  80. || argStr.Find( wxT(".htb") ) >= 0
  81. || argStr.Find( wxT(".zip") ) >= 0 )
  82. {
  83. book[bookCount] = argStr;
  84. bookCount++;
  85. }
  86. else if ( argStr == wxT("--server") )
  87. {
  88. createServer = true;
  89. #if defined(__WXMSW__)
  90. service = wxT("generic_helpservice");
  91. #elif defined(__UNIX__)
  92. service = wxT("/tmp/") + wxString(wxT("generic_helpservice"));
  93. #else
  94. service = wxT("4242");
  95. #endif
  96. }
  97. else if ( !hasService )
  98. {
  99. service = argStr;
  100. hasService = true;
  101. createServer = true;
  102. }
  103. else if ( !hasWindowName )
  104. {
  105. windowName = argStr;
  106. hasWindowName = true;
  107. }
  108. else if ( argStr.Find( wxT("--Style") ) >= 0 )
  109. {
  110. long i;
  111. wxString numb = argStr.AfterLast(wxT('e'));
  112. if ( !(numb.ToLong(&i) ) )
  113. {
  114. wxLogError( wxT("Integer conversion failed for --Style") );
  115. }
  116. else
  117. {
  118. istyle = i;
  119. }
  120. }
  121. else
  122. {
  123. //unknown - could be topic?
  124. }
  125. }
  126. // No book - query user; but not on Mac, since there
  127. // may be an AppleEvent to open a document on the way
  128. #ifndef __WXMAC__
  129. if ( bookCount < 1 )
  130. {
  131. wxString s = wxFileSelector( wxT("Open help file"),
  132. wxGetCwd(),
  133. wxEmptyString,
  134. wxEmptyString,
  135. wxT("Help books (*.htb)|*.htb|Help books (*.zip)|*.zip|HTML Help Project (*.hhp)|*.hhp"),
  136. wxFD_OPEN | wxFD_FILE_MUST_EXIST,
  137. NULL);
  138. if (!s.empty())
  139. {
  140. book[0] = s;
  141. bookCount = 1;
  142. }
  143. }
  144. #endif
  145. #if wxUSE_IPC
  146. if ( createServer )
  147. {
  148. // Create a new server
  149. m_server = new hvServer;
  150. if ( !m_server->Create(service) )
  151. {
  152. wxString wxm = wxT("Server Create failed - service: ");
  153. wxString xxm = wxm << service;
  154. wxLogError( xxm );
  155. //if MSW quits here, probably another copy already exists
  156. return false;
  157. }
  158. createServer = false;
  159. wxUnusedVar(createServer);
  160. }
  161. #endif // wxUSE_IPC
  162. //now add help
  163. wxInitAllImageHandlers();
  164. wxFileSystem::AddHandler(new wxZipFSHandler);
  165. SetVendorName(wxT("wxWidgets") );
  166. SetAppName(wxT("wxHTMLHelpServer") );
  167. wxConfig::Get(); // create an instance
  168. m_helpController = new wxHtmlHelpController( istyle );
  169. if ( !hasWindowName )
  170. {
  171. titleFormat = wxT("Help: %s") ;
  172. }
  173. else
  174. {
  175. //remove underscores
  176. windowName.Replace( wxT("_"), wxT(" ") );
  177. titleFormat = windowName;
  178. }
  179. m_helpController->SetTitleFormat( titleFormat );
  180. for( i=0; i<bookCount; i++ )
  181. {
  182. wxFileName fileName(book[i]);
  183. m_helpController->AddBook(fileName);
  184. }
  185. #ifdef __WXMOTIF__
  186. delete wxLog::SetActiveTarget(new wxLogGui);
  187. #endif
  188. m_helpController->DisplayContents();
  189. return true;
  190. }
  191. int hvApp::OnExit()
  192. {
  193. #if wxUSE_IPC
  194. wxObjectList::compatibility_iterator node = m_connections.GetFirst();
  195. while (node)
  196. {
  197. wxObjectList::compatibility_iterator next = node->GetNext();
  198. hvConnection* connection = (hvConnection*) node->GetData();
  199. connection->Disconnect();
  200. delete connection;
  201. node = next;
  202. }
  203. m_connections.Clear();
  204. if (m_server)
  205. {
  206. delete m_server;
  207. m_server = NULL;
  208. }
  209. #endif
  210. delete m_helpController;
  211. delete wxConfig::Set(NULL);
  212. return 0;
  213. }
  214. bool hvApp::OpenBook(wxHtmlHelpController* controller)
  215. {
  216. wxString s = wxFileSelector(_("Open help file"),
  217. wxGetCwd(),
  218. wxEmptyString,
  219. wxEmptyString,
  220. _(
  221. "Help books (*.htb)|*.htb|Help books (*.zip)|*.zip|\
  222. HTML Help Project (*.hhp)|*.hhp"),
  223. wxFD_OPEN | wxFD_FILE_MUST_EXIST,
  224. NULL);
  225. if ( !s.empty() )
  226. {
  227. wxString ext = s.Right(4).Lower();
  228. if (ext == wxT(".zip") || ext == wxT(".htb") || ext == wxT(".hhp"))
  229. {
  230. wxBusyCursor bcur;
  231. wxFileName fileName(s);
  232. controller->AddBook(fileName);
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. #ifdef __WXMAC__
  239. /// Respond to Apple Event for opening a document
  240. void hvApp::MacOpenFile(const wxString& filename)
  241. {
  242. wxBusyCursor bcur;
  243. wxFileName fileName(filename);
  244. m_helpController->AddBook(fileName);
  245. m_helpController->DisplayContents();
  246. }
  247. #endif
  248. /*
  249. * Art provider class
  250. */
  251. // ---------------------------------------------------------------------
  252. // helper macros
  253. // ---------------------------------------------------------------------
  254. // Standard macro for getting a resource from XPM file:
  255. #define ART(artId, xpmRc) \
  256. if ( id == artId ) return wxBitmap(xpmRc##_xpm);
  257. #define GET_STD_ICON_FROM_APP(iconId)
  258. // There are two ways of getting the standard icon: either via XPMs or via
  259. // wxIcon ctor. This depends on the platform:
  260. #if defined(__WXUNIVERSAL__)
  261. #define CREATE_STD_ICON(iconId, xpmRc) return wxNullBitmap;
  262. #elif defined(__WXGTK__) || defined(__WXMOTIF__)
  263. #define CREATE_STD_ICON(iconId, xpmRc) return wxBitmap(xpmRc##_xpm);
  264. #else
  265. #define CREATE_STD_ICON(iconId, xpmRc) \
  266. { \
  267. wxIcon icon(wxT(iconId)); \
  268. wxBitmap bmp; \
  269. bmp.CopyFromIcon(icon); \
  270. return bmp; \
  271. }
  272. #endif
  273. // Macro used in CreateBitmap to get wxICON_FOO icons:
  274. #define ART_MSGBOX(artId, iconId, xpmRc) \
  275. if ( id == artId ) \
  276. { \
  277. GET_STD_ICON_FROM_APP(iconId) \
  278. CREATE_STD_ICON(#iconId, xpmRc) \
  279. }
  280. // ---------------------------------------------------------------------
  281. // XPMs with the art
  282. // ---------------------------------------------------------------------
  283. // XPM hack: make the arrays const
  284. //#define static static const
  285. #include "bitmaps/helpback.xpm"
  286. #include "bitmaps/helpbook.xpm"
  287. #include "bitmaps/helpdown.xpm"
  288. #include "bitmaps/helpforward.xpm"
  289. #include "bitmaps/helpoptions.xpm"
  290. #include "bitmaps/helppage.xpm"
  291. #include "bitmaps/helpsidepanel.xpm"
  292. #include "bitmaps/helpup.xpm"
  293. #include "bitmaps/helpuplevel.xpm"
  294. #include "bitmaps/helpicon.xpm"
  295. #include "bitmaps/helpopen.xpm"
  296. //#undef static
  297. // ---------------------------------------------------------------------
  298. // CreateBitmap routine
  299. // ---------------------------------------------------------------------
  300. wxBitmap AlternateArtProvider::CreateBitmap(const wxArtID& id,
  301. const wxArtClient& client,
  302. const wxSize& WXUNUSED(size))
  303. {
  304. ART(wxART_HELP_SIDE_PANEL, helpsidepanel)
  305. ART(wxART_HELP_SETTINGS, helpoptions)
  306. ART(wxART_HELP_BOOK, helpbook)
  307. ART(wxART_HELP_FOLDER, helpbook)
  308. ART(wxART_HELP_PAGE, helppage)
  309. //ART(wxART_ADD_BOOKMARK, addbookm)
  310. //ART(wxART_DEL_BOOKMARK, delbookm)
  311. ART(wxART_GO_BACK, helpback)
  312. ART(wxART_GO_FORWARD, helpforward)
  313. ART(wxART_GO_UP, helpup)
  314. ART(wxART_GO_DOWN, helpdown)
  315. ART(wxART_GO_TO_PARENT, helpuplevel)
  316. ART(wxART_FILE_OPEN, helpopen)
  317. if (client == wxART_HELP_BROWSER)
  318. {
  319. //ART(wxART_FRAME_ICON, helpicon)
  320. ART(wxART_HELP, helpicon)
  321. }
  322. //ART(wxART_GO_HOME, home)
  323. // Any wxWidgets icons not implemented here
  324. // will be provided by the default art provider.
  325. return wxNullBitmap;
  326. }
  327. #if wxUSE_IPC
  328. wxConnectionBase *hvServer::OnAcceptConnection(const wxString& topic)
  329. {
  330. if (topic == wxT("HELP"))
  331. return new hvConnection();
  332. else
  333. return NULL;
  334. }
  335. // ----------------------------------------------------------------------------
  336. // hvConnection
  337. // ----------------------------------------------------------------------------
  338. hvConnection::hvConnection()
  339. : wxConnection()
  340. {
  341. wxGetApp().GetConnections().Append(this);
  342. }
  343. hvConnection::~hvConnection()
  344. {
  345. wxGetApp().GetConnections().DeleteObject(this);
  346. }
  347. bool hvConnection::OnExec(const wxString& WXUNUSED(topic),
  348. const wxString& data)
  349. {
  350. // wxLogStatus("Execute command: %s", data);
  351. if ( data == "--intstring" )
  352. {
  353. long i;
  354. wxString argStr = data;
  355. wxString numb = argStr.AfterLast(wxT('g'));
  356. if ( !(numb.ToLong(&i) ) )
  357. {
  358. wxLogError( wxT("Integer conversion failed for --intstring") );
  359. }
  360. else
  361. {
  362. wxGetApp().GetHelpController()->Display(int(i));
  363. }
  364. }
  365. else
  366. {
  367. wxGetApp().GetHelpController()->Display(data);
  368. }
  369. return true;
  370. }
  371. bool hvConnection::OnPoke(const wxString& WXUNUSED(topic),
  372. const wxString& item,
  373. const void *buf,
  374. size_t size,
  375. wxIPCFormat format)
  376. {
  377. const wxString data = GetTextFromData(buf, size, format);
  378. // wxLogStatus("Poke command: %s = %s", item.c_str(), data);
  379. //topic is not tested
  380. if ( wxGetApp().GetHelpController() )
  381. {
  382. if ( item == wxT("--AddBook") )
  383. {
  384. wxGetApp().GetHelpController()->AddBook(data);
  385. }
  386. else if ( item == wxT("--DisplayContents") )
  387. {
  388. wxGetApp().GetHelpController()->DisplayContents();
  389. }
  390. else if ( item == wxT("--DisplayIndex") )
  391. {
  392. wxGetApp().GetHelpController()->DisplayIndex();
  393. }
  394. else if ( item == wxT("--KeywordSearch") )
  395. {
  396. wxGetApp().GetHelpController()->KeywordSearch(data);
  397. }
  398. else if ( item == wxT("--SetTitleFormat") )
  399. {
  400. wxString newname = data;
  401. newname.Replace( wxT("_"), wxT(" ") );
  402. wxGetApp().GetHelpController()->SetTitleFormat(newname);
  403. //does not redraw title bar?
  404. //wxGetApp().GetHelpController()->ReFresh(); - or something
  405. }
  406. else if ( item == wxT("--SetTempDir") )
  407. {
  408. wxGetApp().GetHelpController()->SetTempDir(data);
  409. }
  410. else if ( item == wxT("--YouAreDead") )
  411. {
  412. // don't really know how to kill app from down here...
  413. // use wxKill from client instead
  414. //wxWindow *win = wxTheApp->GetTopWindow();
  415. //if ( win )
  416. // win->Destroy();
  417. }
  418. }
  419. return true;
  420. }
  421. #endif // #if wxUSE_IPC