PageRenderTime 502ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/Dependencies/wxWidgets/samples/html/test/test.cpp

https://github.com/goofoo/Helium
C++ | 440 lines | 306 code | 78 blank | 56 comment | 15 complexity | 24816246f6f4b80468a3c826ad2e2518 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: test.cpp
  3. // Purpose: wxHtml testing example
  4. // Author: Vaclav Slavik
  5. // Created: 1999-07-07
  6. // RCS-ID: $Id$
  7. // Copyright: (c) Vaclav Slavik
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // For compilers that support precompilation, includes "wx/wx.h".
  11. #include "wx/wxprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. // For all others, include the necessary headers (this file is usually all you
  16. // need because it includes almost all "standard" wxWidgets headers
  17. #ifndef WX_PRECOMP
  18. #include "wx/wx.h"
  19. #endif
  20. #include "wx/image.h"
  21. #include "wx/sysopt.h"
  22. #include "wx/html/htmlwin.h"
  23. #include "wx/html/htmlproc.h"
  24. #include "wx/fs_inet.h"
  25. #include "wx/filedlg.h"
  26. #include "wx/utils.h"
  27. #include "wx/clipbrd.h"
  28. #include "wx/dataobj.h"
  29. #include "wx/stopwatch.h"
  30. #include "../../sample.xpm"
  31. // ----------------------------------------------------------------------------
  32. // private classes
  33. // ----------------------------------------------------------------------------
  34. // Define a new application type, each program should derive a class from wxApp
  35. class MyApp : public wxApp
  36. {
  37. public:
  38. virtual bool OnInit();
  39. };
  40. // Define a new html window type: this is a wrapper for handling wxHtmlWindow events
  41. class MyHtmlWindow : public wxHtmlWindow
  42. {
  43. public:
  44. MyHtmlWindow(wxWindow *parent) : wxHtmlWindow( parent )
  45. {
  46. // no custom background initially to avoid confusing people
  47. m_drawCustomBg = false;
  48. }
  49. virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType WXUNUSED(type),
  50. const wxString& WXUNUSED(url),
  51. wxString *WXUNUSED(redirect)) const;
  52. // toggle drawing of custom background
  53. void DrawCustomBg(bool draw)
  54. {
  55. m_drawCustomBg = draw;
  56. Refresh();
  57. }
  58. private:
  59. #if wxUSE_CLIPBOARD
  60. void OnClipboardEvent(wxClipboardTextEvent& event);
  61. #endif // wxUSE_CLIPBOARD
  62. void OnEraseBgEvent(wxEraseEvent& event);
  63. bool m_drawCustomBg;
  64. DECLARE_EVENT_TABLE()
  65. wxDECLARE_NO_COPY_CLASS(MyHtmlWindow);
  66. };
  67. // Define a new frame type: this is going to be our main frame
  68. class MyFrame : public wxFrame
  69. {
  70. public:
  71. // ctor(s)
  72. MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
  73. // event handlers (these functions should _not_ be virtual)
  74. void OnQuit(wxCommandEvent& event);
  75. void OnPageOpen(wxCommandEvent& event);
  76. void OnDefaultLocalBrowser(wxCommandEvent& event);
  77. void OnDefaultWebBrowser(wxCommandEvent& event);
  78. void OnBack(wxCommandEvent& event);
  79. void OnForward(wxCommandEvent& event);
  80. void OnProcessor(wxCommandEvent& event);
  81. void OnDrawCustomBg(wxCommandEvent& event);
  82. void OnHtmlLinkClicked(wxHtmlLinkEvent& event);
  83. void OnHtmlCellHover(wxHtmlCellEvent &event);
  84. void OnHtmlCellClicked(wxHtmlCellEvent &event);
  85. private:
  86. MyHtmlWindow *m_Html;
  87. wxHtmlProcessor *m_Processor;
  88. // Any class wishing to process wxWidgets events must use this macro
  89. DECLARE_EVENT_TABLE()
  90. };
  91. class BoldProcessor : public wxHtmlProcessor
  92. {
  93. public:
  94. virtual wxString Process(const wxString& s) const
  95. {
  96. wxString r(s);
  97. r.Replace(wxT("<b>"), wxEmptyString);
  98. r.Replace(wxT("<B>"), wxEmptyString);
  99. r.Replace(wxT("</b>"), wxEmptyString);
  100. r.Replace(wxT("</B>"), wxEmptyString);
  101. return r;
  102. }
  103. };
  104. // ----------------------------------------------------------------------------
  105. // constants
  106. // ----------------------------------------------------------------------------
  107. // IDs for the controls and the menu commands
  108. enum
  109. {
  110. // menu items
  111. ID_PageOpen = wxID_HIGHEST,
  112. ID_DefaultLocalBrowser,
  113. ID_DefaultWebBrowser,
  114. ID_Back,
  115. ID_Forward,
  116. ID_Processor,
  117. ID_DrawCustomBg
  118. };
  119. // ----------------------------------------------------------------------------
  120. // event tables and other macros for wxWidgets
  121. // ----------------------------------------------------------------------------
  122. BEGIN_EVENT_TABLE(MyFrame, wxFrame)
  123. EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
  124. EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen)
  125. EVT_MENU(ID_DefaultLocalBrowser, MyFrame::OnDefaultLocalBrowser)
  126. EVT_MENU(ID_DefaultWebBrowser, MyFrame::OnDefaultWebBrowser)
  127. EVT_MENU(ID_Back, MyFrame::OnBack)
  128. EVT_MENU(ID_Forward, MyFrame::OnForward)
  129. EVT_MENU(ID_Processor, MyFrame::OnProcessor)
  130. EVT_MENU(ID_DrawCustomBg, MyFrame::OnDrawCustomBg)
  131. EVT_HTML_LINK_CLICKED(wxID_ANY, MyFrame::OnHtmlLinkClicked)
  132. EVT_HTML_CELL_HOVER(wxID_ANY, MyFrame::OnHtmlCellHover)
  133. EVT_HTML_CELL_CLICKED(wxID_ANY, MyFrame::OnHtmlCellClicked)
  134. END_EVENT_TABLE()
  135. IMPLEMENT_APP(MyApp)
  136. // ============================================================================
  137. // implementation
  138. // ============================================================================
  139. // ----------------------------------------------------------------------------
  140. // the application class
  141. // ----------------------------------------------------------------------------
  142. // `Main program' equivalent: the program execution "starts" here
  143. bool MyApp::OnInit()
  144. {
  145. if ( !wxApp::OnInit() )
  146. return false;
  147. #if wxUSE_SYSTEM_OPTIONS
  148. wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
  149. #endif
  150. wxInitAllImageHandlers();
  151. #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
  152. wxFileSystem::AddHandler(new wxInternetFSHandler);
  153. #endif
  154. SetVendorName(wxT("wxWidgets"));
  155. SetAppName(wxT("wxHtmlTest"));
  156. // the following call to wxConfig::Get will use it to create an object...
  157. // Create the main application window
  158. MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"),
  159. wxDefaultPosition, wxSize(640, 480));
  160. frame->Show();
  161. return true /* continue running */;
  162. }
  163. // ----------------------------------------------------------------------------
  164. // main frame
  165. // ----------------------------------------------------------------------------
  166. // frame constructor
  167. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
  168. : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size,
  169. wxDEFAULT_FRAME_STYLE, wxT("html_test_app"))
  170. {
  171. // create a menu bar
  172. wxMenu *menuFile = new wxMenu;
  173. wxMenu *menuNav = new wxMenu;
  174. menuFile->Append(ID_PageOpen, _("&Open HTML page...\tCtrl-O"));
  175. menuFile->Append(ID_DefaultLocalBrowser, _("&Open current page with default browser"));
  176. menuFile->Append(ID_DefaultWebBrowser, _("Open a &web page with default browser"));
  177. menuFile->AppendSeparator();
  178. menuFile->Append(ID_Processor, _("&Remove bold attribute"),
  179. wxEmptyString, wxITEM_CHECK);
  180. menuFile->AppendSeparator();
  181. menuFile->AppendCheckItem(ID_DrawCustomBg, "&Draw custom background");
  182. menuFile->AppendSeparator();
  183. menuFile->Append(wxID_EXIT, _("&Close frame"));
  184. menuNav->Append(ID_Back, _("Go &BACK"));
  185. menuNav->Append(ID_Forward, _("Go &FORWARD"));
  186. // now append the freshly created menu to the menu bar...
  187. wxMenuBar *menuBar = new wxMenuBar;
  188. menuBar->Append(menuFile, _("&File"));
  189. menuBar->Append(menuNav, _("&Navigate"));
  190. // ... and attach this menu bar to the frame
  191. SetMenuBar(menuBar);
  192. SetIcon(wxIcon(sample_xpm));
  193. #if wxUSE_ACCEL
  194. // Create convenient accelerators for Back and Forward navigation
  195. wxAcceleratorEntry entries[2];
  196. entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back);
  197. entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward);
  198. wxAcceleratorTable accel(WXSIZEOF(entries), entries);
  199. SetAcceleratorTable(accel);
  200. #endif // wxUSE_ACCEL
  201. #if wxUSE_STATUSBAR
  202. CreateStatusBar(2);
  203. #endif // wxUSE_STATUSBAR
  204. m_Processor = new BoldProcessor;
  205. m_Processor->Enable(false);
  206. m_Html = new MyHtmlWindow(this);
  207. m_Html->SetRelatedFrame(this, _("HTML : %s"));
  208. #if wxUSE_STATUSBAR
  209. m_Html->SetRelatedStatusBar(1);
  210. #endif // wxUSE_STATUSBAR
  211. m_Html->ReadCustomization(wxConfig::Get());
  212. m_Html->LoadFile(wxFileName(wxT("test.htm")));
  213. m_Html->AddProcessor(m_Processor);
  214. wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, wxT(""),
  215. wxDefaultPosition, wxDefaultSize,
  216. wxTE_MULTILINE);
  217. delete wxLog::SetActiveTarget(new wxLogTextCtrl(text));
  218. wxSizer *sz = new wxBoxSizer(wxVERTICAL);
  219. sz->Add(m_Html, 3, wxGROW);
  220. sz->Add(text, 1, wxGROW);
  221. SetSizer(sz);
  222. }
  223. // event handlers
  224. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  225. {
  226. m_Html->WriteCustomization(wxConfig::Get());
  227. delete wxConfig::Set(NULL);
  228. // true is to force the frame to close
  229. Close(true);
  230. }
  231. void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event))
  232. {
  233. #if wxUSE_FILEDLG
  234. wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString,
  235. wxEmptyString, wxEmptyString, wxT("HTML files|*.htm;*.html"));
  236. if (!p.empty())
  237. {
  238. #if wxUSE_STOPWATCH
  239. wxStopWatch sw;
  240. #endif
  241. m_Html->LoadFile(wxFileName(p));
  242. #if wxUSE_STOPWATCH
  243. wxLogStatus("Loaded \"%s\" in %lums", p, sw.Time());
  244. #endif
  245. }
  246. #endif // wxUSE_FILEDLG
  247. }
  248. void MyFrame::OnDefaultLocalBrowser(wxCommandEvent& WXUNUSED(event))
  249. {
  250. wxString page = m_Html->GetOpenedPage();
  251. if (!page.empty())
  252. {
  253. wxLaunchDefaultBrowser(page);
  254. }
  255. }
  256. void MyFrame::OnDefaultWebBrowser(wxCommandEvent& WXUNUSED(event))
  257. {
  258. wxString page = m_Html->GetOpenedPage();
  259. if (!page.empty())
  260. {
  261. wxLaunchDefaultBrowser(wxT("http://www.google.com"));
  262. }
  263. }
  264. void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
  265. {
  266. if (!m_Html->HistoryBack())
  267. {
  268. wxMessageBox(_("You reached prehistory era!"));
  269. }
  270. }
  271. void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
  272. {
  273. if (!m_Html->HistoryForward())
  274. {
  275. wxMessageBox(_("No more items in history!"));
  276. }
  277. }
  278. void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event))
  279. {
  280. m_Processor->Enable(!m_Processor->IsEnabled());
  281. m_Html->LoadPage(m_Html->GetOpenedPage());
  282. }
  283. void MyFrame::OnDrawCustomBg(wxCommandEvent& event)
  284. {
  285. m_Html->DrawCustomBg(event.IsChecked());
  286. }
  287. void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent &event)
  288. {
  289. wxLogMessage(wxT("The url '%s' has been clicked!"), event.GetLinkInfo().GetHref().c_str());
  290. // skipping this event the default behaviour (load the clicked URL)
  291. // will happen...
  292. event.Skip();
  293. }
  294. void MyFrame::OnHtmlCellHover(wxHtmlCellEvent &event)
  295. {
  296. wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
  297. event.GetCell(), event.GetPoint().x, event.GetPoint().y);
  298. }
  299. void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent &event)
  300. {
  301. wxLogMessage(wxT("Click over cell %p at %d;%d"),
  302. event.GetCell(), event.GetPoint().x, event.GetPoint().y);
  303. // if we don't skip the event, OnHtmlLinkClicked won't be called!
  304. event.Skip();
  305. }
  306. wxHtmlOpeningStatus MyHtmlWindow::OnOpeningURL(wxHtmlURLType WXUNUSED(type),
  307. const wxString& url,
  308. wxString *WXUNUSED(redirect)) const
  309. {
  310. GetRelatedFrame()->SetStatusText(url + wxT(" lately opened"),1);
  311. return wxHTML_OPEN;
  312. }
  313. BEGIN_EVENT_TABLE(MyHtmlWindow, wxHtmlWindow)
  314. #if wxUSE_CLIPBOARD
  315. EVT_TEXT_COPY(wxID_ANY, MyHtmlWindow::OnClipboardEvent)
  316. #endif // wxUSE_CLIPBOARD
  317. EVT_ERASE_BACKGROUND(MyHtmlWindow::OnEraseBgEvent)
  318. END_EVENT_TABLE()
  319. #if wxUSE_CLIPBOARD
  320. void MyHtmlWindow::OnClipboardEvent(wxClipboardTextEvent& WXUNUSED(event))
  321. {
  322. // explicitly call wxHtmlWindow::CopySelection() method
  323. // and show the first 100 characters of the text copied in the status bar
  324. if ( CopySelection() )
  325. {
  326. wxTextDataObject data;
  327. if ( wxTheClipboard && wxTheClipboard->Open() && wxTheClipboard->GetData(data) )
  328. {
  329. const wxString text = data.GetText();
  330. const size_t maxTextLength = 100;
  331. wxLogStatus(wxString::Format(wxT("Clipboard: '%s%s'"),
  332. wxString(text, maxTextLength).c_str(),
  333. (text.length() > maxTextLength) ? wxT("...")
  334. : wxT("")));
  335. wxTheClipboard->Close();
  336. return;
  337. }
  338. }
  339. wxLogStatus(wxT("Clipboard: nothing"));
  340. }
  341. #endif // wxUSE_CLIPBOARD
  342. void MyHtmlWindow::OnEraseBgEvent(wxEraseEvent& event)
  343. {
  344. if ( !m_drawCustomBg )
  345. {
  346. event.Skip();
  347. return;
  348. }
  349. // draw a background grid to show that this handler is indeed executed
  350. wxDC& dc = *event.GetDC();
  351. dc.SetPen(*wxBLUE_PEN);
  352. dc.Clear();
  353. const wxSize size = GetVirtualSize();
  354. for ( int x = 0; x < size.x; x += 15 )
  355. {
  356. dc.DrawLine(x, 0, x, size.y);
  357. }
  358. for ( int y = 0; y < size.y; y += 15 )
  359. {
  360. dc.DrawLine(0, y, size.x, y);
  361. }
  362. }