test.cpp 13 KB

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