htmlctrl.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: htmlctrl.cpp
  3. // Purpose: HtmlCtrl sample
  4. // Author: Julian Smart / Kevin Ollivier
  5. // Modified by:
  6. // Created: 04/16/2004
  7. // Copyright: (c) Julian Smart / Kevin Ollivier
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers (this file is usually all you
  22. // need because it includes almost all "standard" wxWidgets headers)
  23. #ifndef WX_PRECOMP
  24. #include "wx/wx.h"
  25. #endif
  26. #include "wx/html/webkit.h"
  27. #ifndef wxHAS_IMAGES_IN_RESOURCES
  28. #include "../../sample.xpm"
  29. #endif
  30. // ----------------------------------------------------------------------------
  31. // resources
  32. // ----------------------------------------------------------------------------
  33. enum {
  34. ID_BACK = wxID_HIGHEST + 1,
  35. ID_NEXT = wxID_HIGHEST + 2,
  36. ID_RELOAD = wxID_HIGHEST + 3,
  37. ID_URLLIST = wxID_HIGHEST + 4,
  38. ID_STOP = wxID_HIGHEST + 5,
  39. ID_WEBKIT = wxID_HIGHEST + 6,
  40. ID_VIEW_SOURCE = wxID_HIGHEST + 7,
  41. ID_OPEN = wxID_HIGHEST + 8,
  42. ID_SAVE = wxID_HIGHEST + 9,
  43. ID_SET_SOURCE = wxID_HIGHEST + 10
  44. };
  45. // ----------------------------------------------------------------------------
  46. // private classes
  47. // ----------------------------------------------------------------------------
  48. // Define a new application type, each program should derive a class from wxApp
  49. class MyApp : public wxApp
  50. {
  51. public:
  52. // override base class virtuals
  53. // ----------------------------
  54. // this one is called on application startup and is a good place for the app
  55. // initialization (doing it here and not in the ctor allows to have an error
  56. // return: if OnInit() returns false, the application terminates)
  57. virtual bool OnInit();
  58. };
  59. // Define a new frame type: this is going to be our main frame
  60. class MyFrame : public wxFrame
  61. {
  62. public:
  63. // ctor(s)
  64. MyFrame(const wxString& title);
  65. void OnBackButton(wxCommandEvent& myEvent);
  66. void OnNextButton(wxCommandEvent& myEvent);
  67. void OnURLEnter(wxCommandEvent& myEvent);
  68. void OnStopButton(wxCommandEvent& myEvent);
  69. void OnReloadButton(wxCommandEvent& myEvent);
  70. void OnViewSource(wxCommandEvent& myEvent);
  71. void OnSetSource(wxCommandEvent& myEvent);
  72. void OnStateChanged(wxWebKitStateChangedEvent& myEvent);
  73. wxWebKitCtrl* mySafari;
  74. wxTextCtrl* urlText;
  75. private:
  76. // any class wishing to process wxWidgets events must use this macro
  77. wxDECLARE_EVENT_TABLE();
  78. };
  79. // ----------------------------------------------------------------------------
  80. // event tables and other macros for wxWidgets
  81. // ----------------------------------------------------------------------------
  82. // the event tables connect the wxWidgets events with the functions (event
  83. // handlers) which process them. It can be also done at run-time, but for the
  84. // simple menu events like this the static method is much simpler.
  85. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  86. EVT_BUTTON(ID_BACK, MyFrame::OnBackButton)
  87. EVT_BUTTON(ID_NEXT, MyFrame::OnNextButton)
  88. EVT_BUTTON(ID_STOP, MyFrame::OnStopButton)
  89. EVT_BUTTON(ID_RELOAD, MyFrame::OnReloadButton)
  90. EVT_MENU(ID_VIEW_SOURCE, MyFrame::OnViewSource)
  91. EVT_MENU(ID_SET_SOURCE, MyFrame::OnSetSource)
  92. EVT_TEXT_ENTER(ID_URLLIST, MyFrame::OnURLEnter)
  93. EVT_WEBKIT_STATE_CHANGED(MyFrame::OnStateChanged)
  94. //EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
  95. //EVT_MENU(Minimal_About, MyFrame::OnAbout)
  96. wxEND_EVENT_TABLE()
  97. // Create a new application object: this macro will allow wxWidgets to create
  98. // the application object during program execution (it's better than using a
  99. // static object for many reasons) and also implements the accessor function
  100. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  101. // not wxApp)
  102. IMPLEMENT_APP(MyApp)
  103. // ============================================================================
  104. // implementation
  105. // ============================================================================
  106. // ----------------------------------------------------------------------------
  107. // the application class
  108. // ----------------------------------------------------------------------------
  109. // 'Main program' equivalent: the program execution "starts" here
  110. bool MyApp::OnInit()
  111. {
  112. if ( !wxApp::OnInit() )
  113. return false;
  114. // create the main application window
  115. MyFrame *frame = new MyFrame(wxT("wxWebKit Sample"));
  116. // and show it (the frames, unlike simple controls, are not shown when
  117. // created initially)
  118. frame->Show(true);
  119. // success: wxApp::OnRun() will be called which will enter the main message
  120. // loop and the application will run. If we returned false here, the
  121. // application would exit immediately.
  122. return true;
  123. }
  124. // ----------------------------------------------------------------------------
  125. // main frame
  126. // ----------------------------------------------------------------------------
  127. // frame constructor
  128. MyFrame::MyFrame(const wxString& title)
  129. : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(500,500))
  130. {
  131. SetIcon(wxICON(sample));
  132. wxMenuBar* myBar = new wxMenuBar();
  133. wxMenu* fileMenu = new wxMenu;
  134. fileMenu->Append(ID_OPEN, _("&Open"));
  135. fileMenu->Append(ID_SAVE, _("&Save"));
  136. myBar->Append(fileMenu, _("&File"));
  137. wxMenu* editMenu = new wxMenu;
  138. editMenu->Append(ID_SET_SOURCE, _("Set Page Source"));
  139. myBar->Append(editMenu, _("&Edit"));
  140. //wxMenu* viewMenu = new wxMenu(_("View"));
  141. //viewMenu->Append(ID_VIEW_SOURCE, _("View Source"));
  142. //myBar->Append(viewMenu, _("View"));
  143. SetMenuBar(myBar);
  144. wxToolBar* myToolbar = CreateToolBar();
  145. wxButton* btnBack = new wxButton(myToolbar, ID_BACK, _("Back"));
  146. myToolbar->AddControl(btnBack);
  147. myToolbar->AddSeparator();
  148. wxButton* btnNext = new wxButton(myToolbar, ID_NEXT, _("Next"));
  149. myToolbar->AddControl(btnNext);
  150. myToolbar->AddSeparator();
  151. wxButton* btnStop = new wxButton(myToolbar, ID_STOP, _("Stop"));
  152. myToolbar->AddControl(btnStop);
  153. myToolbar->AddSeparator();
  154. wxButton* btnReload = new wxButton(myToolbar, ID_RELOAD, _("Reload"));
  155. myToolbar->AddControl(btnReload);
  156. myToolbar->AddSeparator();
  157. urlText = new wxTextCtrl(myToolbar, ID_URLLIST, wxT("http://www.wxwidgets.org"), wxDefaultPosition, wxSize(220, -1), wxTE_PROCESS_ENTER);
  158. myToolbar->AddControl(urlText);
  159. myToolbar->AddSeparator();
  160. myToolbar->Realize();
  161. // Testing wxWebKitCtrl inside a panel
  162. #if 1
  163. wxPanel* panel = new wxPanel(this, wxID_ANY);
  164. wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL);
  165. panel->SetSizer(boxSizer);
  166. mySafari = new wxWebKitCtrl(panel, ID_WEBKIT, wxT("http://www.wxwidgets.org"), wxDefaultPosition, wxSize(200, 200));
  167. boxSizer->Add(mySafari, 1, wxEXPAND);
  168. wxBoxSizer* frameSizer = new wxBoxSizer(wxVERTICAL);
  169. SetSizer(frameSizer);
  170. frameSizer->Add(panel, 1, wxEXPAND);
  171. #else
  172. mySafari = new wxWebKitCtrl(this, ID_WEBKIT, wxT("http://www.wxwidgets.org"), wxDefaultPosition, wxSize(200, 200));
  173. #endif
  174. #if wxUSE_STATUSBAR
  175. CreateStatusBar(2);
  176. #endif // wxUSE_STATUSBAR
  177. }
  178. void MyFrame::OnBackButton(wxCommandEvent& WXUNUSED(myEvent))
  179. {
  180. if (mySafari->CanGoBack())
  181. mySafari->GoBack();
  182. }
  183. void MyFrame::OnNextButton(wxCommandEvent& WXUNUSED(myEvent))
  184. {
  185. if (mySafari->CanGoForward())
  186. mySafari->GoForward();
  187. }
  188. void MyFrame::OnStopButton(wxCommandEvent& WXUNUSED(myEvent))
  189. {
  190. mySafari->Stop();
  191. }
  192. void MyFrame::OnReloadButton(wxCommandEvent& WXUNUSED(myEvent))
  193. {
  194. mySafari->Reload();
  195. }
  196. void MyFrame::OnURLEnter(wxCommandEvent& WXUNUSED(myEvent))
  197. {
  198. mySafari->LoadURL(urlText->GetValue());
  199. }
  200. void MyFrame::OnStateChanged(wxWebKitStateChangedEvent& myEvent)
  201. {
  202. if (GetStatusBar() != NULL)
  203. {
  204. if (myEvent.GetState() == wxWEBKIT_STATE_NEGOTIATING)
  205. {
  206. GetStatusBar()->SetStatusText(_("Contacting ") + myEvent.GetURL());
  207. urlText->SetValue(myEvent.GetURL());
  208. }
  209. else if (myEvent.GetState() == wxWEBKIT_STATE_TRANSFERRING)
  210. {
  211. GetStatusBar()->SetStatusText(_("Loading ") + myEvent.GetURL());
  212. }
  213. else if (myEvent.GetState() == wxWEBKIT_STATE_STOP)
  214. {
  215. GetStatusBar()->SetStatusText(_("Load complete."));
  216. SetTitle(mySafari->GetTitle());
  217. }
  218. else if (myEvent.GetState() == wxWEBKIT_STATE_FAILED)
  219. {
  220. GetStatusBar()->SetStatusText(_("Failed to load ") + myEvent.GetURL());
  221. }
  222. }
  223. }
  224. void MyFrame::OnViewSource(wxCommandEvent& WXUNUSED(myEvent))
  225. {
  226. if (mySafari->CanGetPageSource())
  227. wxMessageBox(mySafari->GetPageSource());
  228. }
  229. void MyFrame::OnSetSource(wxCommandEvent& WXUNUSED(myEvent))
  230. {
  231. if (mySafari)
  232. {
  233. wxString myText = wxT("<HTML><HEAD></HEAD><BODY><P>Hello world!</P></BODY></HTML>");
  234. mySafari->SetPageSource(myText);
  235. }
  236. }