1 /* 2 * Copyright (C) 2007 Kevin Ollivier All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * This class provides a default new window implementation for wxWebView clients 26 * who don't want/need to roll their own browser frame UI. 27 */ 28 29 #include "config.h" 30 31 #include "wx/wxprec.h" 32 #ifndef WX_PRECOMP 33 #include "wx/wx.h" 34 #endif 35 36 #include "wx/artprov.h" 37 38 #include "WebView.h" 39 #include "WebBrowserShell.h" 40 #include "WebViewPrivate.h" 41 42 wxPageSourceViewFrame::wxPageSourceViewFrame(const wxString& source) 43 : wxFrame(NULL, wxID_ANY, _("Page Source View"), wxDefaultPosition, wxSize(600, 500)) 44 { 45 wxTextCtrl* control = new wxTextCtrl(this, -1, source, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE); 46 } 47 48 enum { 49 ID_LOADFILE = wxID_HIGHEST + 1, 50 ID_TEXTCTRL = wxID_HIGHEST + 2, 51 ID_BACK = wxID_HIGHEST + 3, 52 ID_FORWARD = wxID_HIGHEST + 4, 53 ID_TOGGLE_BEFORE_LOAD = wxID_HIGHEST + 5, 54 ID_MAKE_TEXT_LARGER = wxID_HIGHEST + 6, 55 ID_MAKE_TEXT_SMALLER = wxID_HIGHEST + 7, 56 ID_STOP = wxID_HIGHEST + 8, 57 ID_RELOAD = wxID_HIGHEST + 9, 58 ID_GET_SOURCE = wxID_HIGHEST + 10, 59 ID_SET_SOURCE = wxID_HIGHEST + 11, 60 ID_SEARCHCTRL = wxID_HIGHEST + 12, 61 ID_LOADURL = wxID_HIGHEST + 13, 62 ID_NEW_WINDOW = wxID_HIGHEST + 14, 63 ID_BROWSE = wxID_HIGHEST + 15, 64 ID_EDIT = wxID_HIGHEST + 16, 65 ID_RUN_SCRIPT = wxID_HIGHEST + 17, 66 ID_WEBVIEW = wxID_HIGHEST + 18 67 }; 68 69 BEGIN_EVENT_TABLE(wxWebBrowserShell, wxFrame) 70 EVT_MENU(wxID_CUT, wxWebBrowserShell::OnCut) 71 EVT_MENU(wxID_COPY, wxWebBrowserShell::OnCopy) 72 EVT_MENU(wxID_PASTE, wxWebBrowserShell::OnPaste) 73 EVT_MENU(wxID_EXIT, wxWebBrowserShell::OnQuit) 74 EVT_MENU(wxID_ABOUT, wxWebBrowserShell::OnAbout) 75 EVT_MENU(ID_LOADFILE, wxWebBrowserShell::OnLoadFile) 76 EVT_TEXT_ENTER(ID_TEXTCTRL, wxWebBrowserShell::OnAddressBarEnter) 77 EVT_TEXT_ENTER(ID_SEARCHCTRL, wxWebBrowserShell::OnSearchCtrlEnter) 78 EVT_WEBVIEW_LOAD(ID_WEBVIEW, wxWebBrowserShell::OnLoadEvent) 79 EVT_WEBVIEW_BEFORE_LOAD(ID_WEBVIEW, wxWebBrowserShell::OnBeforeLoad) 80 EVT_MENU(ID_BACK, wxWebBrowserShell::OnBack) 81 EVT_MENU(ID_FORWARD, wxWebBrowserShell::OnForward) 82 EVT_MENU(ID_STOP, wxWebBrowserShell::OnStop) 83 EVT_MENU(ID_RELOAD, wxWebBrowserShell::OnReload) 84 EVT_MENU(ID_MAKE_TEXT_LARGER, wxWebBrowserShell::OnMakeTextLarger) 85 EVT_MENU(ID_MAKE_TEXT_SMALLER, wxWebBrowserShell::OnMakeTextSmaller) 86 EVT_MENU(ID_GET_SOURCE, wxWebBrowserShell::OnGetSource) 87 EVT_MENU(ID_SET_SOURCE, wxWebBrowserShell::OnSetSource) 88 EVT_MENU(ID_BROWSE, wxWebBrowserShell::OnBrowse) 89 EVT_MENU(ID_EDIT, wxWebBrowserShell::OnEdit) 90 EVT_MENU(ID_RUN_SCRIPT, wxWebBrowserShell::OnRunScript) 91 END_EVENT_TABLE() 92 93 94 wxWebBrowserShell::wxWebBrowserShell(const wxString& title) : 95 wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(600, 500)), 96 m_checkBeforeLoad(false) 97 { 98 99 // create a menu bar 100 wxMenu *fileMenu = new wxMenu; 101 fileMenu->Append(ID_NEW_WINDOW, _T("New Window\tCTRL+N")); 102 fileMenu->Append(ID_LOADFILE, _T("Open File...\tCTRL+O")); 103 fileMenu->Append(ID_LOADURL, _("Open Location...\tCTRL+L")); 104 fileMenu->Append(wxID_EXIT, _T("E&xit\tAlt-X"), _T("Quit this program")); 105 106 wxMenu *editMenu = new wxMenu; 107 editMenu->Append(wxID_CUT, _T("Cut\tCTRL+X")); 108 editMenu->Append(wxID_COPY, _T("Copy\tCTRL+C")); 109 editMenu->Append(wxID_PASTE, _T("Paste\tCTRL+V")); 110 111 wxMenu* viewMenu = new wxMenu; 112 viewMenu->AppendRadioItem(ID_BROWSE, _("Browse")); 113 viewMenu->AppendRadioItem(ID_EDIT, _("Edit")); 114 viewMenu->AppendSeparator(); 115 viewMenu->Append(ID_STOP, _("Stop")); 116 viewMenu->Append(ID_RELOAD, _("Reload Page")); 117 viewMenu->Append(ID_MAKE_TEXT_SMALLER, _("Make Text Smaller\tCTRL+-")); 118 viewMenu->Append(ID_MAKE_TEXT_LARGER, _("Make Text Bigger\tCTRL++")); 119 viewMenu->AppendSeparator(); 120 viewMenu->Append(ID_GET_SOURCE, _("View Page Source")); 121 viewMenu->AppendSeparator(); 122 123 m_debugMenu = new wxMenu; 124 m_debugMenu->Append(ID_SET_SOURCE, _("Test SetPageSource")); 125 m_debugMenu->Append(ID_RUN_SCRIPT, _("Test RunScript")); 126 127 // the "About" item should be in the help menu 128 wxMenu *helpMenu = new wxMenu; 129 helpMenu->Append(wxID_ABOUT, _T("&About...\tF1"), _T("Show about dialog")); 130 131 // now append the freshly created menu to the menu bar... 132 wxMenuBar *menuBar = new wxMenuBar(); 133 menuBar->Append(fileMenu, _T("&File")); 134 menuBar->Append(editMenu, _T("&Edit")); 135 menuBar->Append(viewMenu, _T("&View")); 136 menuBar->Append(helpMenu, _T("&Help")); 137 138 // ... and attach this menu bar to the frame 139 SetMenuBar(menuBar); 140 141 wxToolBar* toolbar = CreateToolBar(); 142 toolbar->SetToolBitmapSize(wxSize(32, 32)); 143 144 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK, wxART_TOOLBAR, wxSize(32,32)); 145 toolbar->AddTool(ID_BACK, back, wxT("Back")); 146 147 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD, wxART_TOOLBAR, wxSize(32,32)); 148 toolbar->AddTool(ID_FORWARD, forward, wxT("Next")); 149 150 addressBar = new wxTextCtrl(toolbar, ID_TEXTCTRL, _T(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER); 151 toolbar->AddControl(addressBar); 152 153 searchCtrl = new wxSearchCtrl(toolbar, ID_SEARCHCTRL, _("Search"), wxDefaultPosition, wxSize(200, -1), wxTE_PROCESS_ENTER); 154 toolbar->AddControl(searchCtrl); 155 toolbar->Realize(); 156 157 SetToolBar(toolbar); 158 159 // Create the wxWebView Window 160 webview = new wxWebView((wxWindow*)this, ID_WEBVIEW, wxDefaultPosition, wxSize(200, 200)); 161 webview->SetBackgroundColour(*wxWHITE); 162 163 // create a status bar just for fun (by default with 1 pane only) 164 CreateStatusBar(2); 165 } 166 167 wxWebBrowserShell::~wxWebBrowserShell() 168 { 169 if (m_debugMenu && GetMenuBar()->FindMenu(_("&Debug")) == wxNOT_FOUND) 170 delete m_debugMenu; 171 } 172 173 void wxWebBrowserShell::ShowDebugMenu(bool show) 174 { 175 int debugMenu = GetMenuBar()->FindMenu(_("&Debug")); 176 if (show && debugMenu == wxNOT_FOUND) { 177 int prevMenu = GetMenuBar()->FindMenu(_("&View")); 178 if (prevMenu != wxNOT_FOUND) 179 GetMenuBar()->Insert((size_t)prevMenu+1, m_debugMenu, _("&Debug")); 180 } 181 else if (!show && debugMenu != wxNOT_FOUND) { 182 GetMenuBar()->Remove(debugMenu); 183 } 184 } 185 186 // event handlers 187 188 void wxWebBrowserShell::OnQuit(wxCommandEvent& WXUNUSED(event)) 189 { 190 // true is to force the frame to close 191 Close(true); 192 } 193 194 void wxWebBrowserShell::OnAbout(wxCommandEvent& WXUNUSED(event)) 195 { 196 wxString msg; 197 msg.Printf(_T("This is the About dialog of the wxWebKit sample.\n") 198 _T("Welcome to %s"), wxVERSION_STRING); 199 200 wxMessageBox(msg, _T("About wxWebKit Sample"), wxOK | wxICON_INFORMATION, this); 201 202 } 203 204 void wxWebBrowserShell::OnLoadFile(wxCommandEvent& WXUNUSED(event)) 205 { 206 wxFileDialog* dialog = new wxFileDialog(this, wxT("Choose a file")); 207 if (dialog->ShowModal() == wxID_OK) { 208 wxString path = dialog->GetPath().Prepend(wxT("file://")); 209 210 if (webview) 211 webview->LoadURL(path); 212 } 213 } 214 215 void wxWebBrowserShell::OnLoadEvent(wxWebViewLoadEvent& event) 216 { 217 if (GetStatusBar() != NULL){ 218 if (event.GetState() == wxWEBVIEW_LOAD_NEGOTIATING) { 219 GetStatusBar()->SetStatusText(_("Contacting ") + event.GetURL()); 220 } 221 else if (event.GetState() == wxWEBVIEW_LOAD_TRANSFERRING) { 222 GetStatusBar()->SetStatusText(_("Loading ") + event.GetURL()); 223 } 224 else if (event.GetState() == wxWEBVIEW_LOAD_ONLOAD_HANDLED) { 225 GetStatusBar()->SetStatusText(_("Load complete.")); 226 addressBar->SetValue(event.GetURL()); 227 SetTitle(webview->GetPageTitle()); 228 } 229 else if (event.GetState() == wxWEBVIEW_LOAD_FAILED) { 230 GetStatusBar()->SetStatusText(_("Failed to load ") + event.GetURL()); 231 } 232 } 233 } 234 235 void wxWebBrowserShell::OnBeforeLoad(wxWebViewBeforeLoadEvent& myEvent) 236 { 237 if (m_checkBeforeLoad) { 238 int reply = wxMessageBox(_("Would you like to continue loading ") + myEvent.GetURL() + wxT("?"), _("Continue Loading?"), wxYES_NO); 239 if (reply == wxNO) { 240 myEvent.Cancel(); 241 } 242 } 243 } 244 245 void wxWebBrowserShell::OnAddressBarEnter(wxCommandEvent& event) 246 { 247 if (webview) 248 webview->LoadURL(addressBar->GetValue()); 249 } 250 251 void wxWebBrowserShell::OnSearchCtrlEnter(wxCommandEvent& event) 252 { 253 if (webview) { 254 webview->LoadURL(wxString::Format(wxT("http://www.google.com/search?rls=en&q=%s&ie=UTF-8&oe=UTF-8"), searchCtrl->GetValue().wc_str())); 255 } 256 } 257 258 void wxWebBrowserShell::OnCut(wxCommandEvent& event) 259 { 260 if (webview && webview->CanCut()) 261 webview->Cut(); 262 } 263 264 void wxWebBrowserShell::OnCopy(wxCommandEvent& event) 265 { 266 if (webview && webview->CanCopy()) 267 webview->Copy(); 268 } 269 270 void wxWebBrowserShell::OnPaste(wxCommandEvent& event) 271 { 272 if (webview && webview->CanPaste()) 273 webview->Paste(); 274 } 275 276 void wxWebBrowserShell::OnBack(wxCommandEvent& event) 277 { 278 if (webview) 279 webview->GoBack(); 280 } 281 282 void wxWebBrowserShell::OnForward(wxCommandEvent& event) 283 { 284 if (webview) 285 webview->GoForward(); 286 } 287 288 void wxWebBrowserShell::OnStop(wxCommandEvent& myEvent) 289 { 290 if (webview) 291 webview->Stop(); 292 } 293 294 void wxWebBrowserShell::OnReload(wxCommandEvent& myEvent) 295 { 296 if (webview) 297 webview->Reload(); 298 } 299 300 void wxWebBrowserShell::OnMakeTextLarger(wxCommandEvent& myEvent) 301 { 302 if (webview) { 303 if (webview->CanIncreaseTextSize()) 304 webview->IncreaseTextSize(); 305 } 306 } 307 308 void wxWebBrowserShell::OnMakeTextSmaller(wxCommandEvent& myEvent) 309 { 310 if (webview) { 311 if (webview->CanDecreaseTextSize()) 312 webview->DecreaseTextSize(); 313 } 314 } 315 316 void wxWebBrowserShell::OnGetSource(wxCommandEvent& myEvent) 317 { 318 if (webview) { 319 wxPageSourceViewFrame* pageSourceFrame = new wxPageSourceViewFrame(webview->GetPageSource()); 320 pageSourceFrame->Show(); 321 } 322 } 323 324 void wxWebBrowserShell::OnSetSource(wxCommandEvent& event) 325 { 326 if (webview) 327 webview->SetPageSource(wxString(wxT("<p>Hello World!</p>"))); 328 } 329 330 void wxWebBrowserShell::OnBrowse(wxCommandEvent& event) 331 { 332 if (webview) 333 webview->MakeEditable(!event.IsChecked()); 334 } 335 336 void wxWebBrowserShell::OnEdit(wxCommandEvent& event) 337 { 338 if (webview) 339 webview->MakeEditable(event.IsChecked()); 340 } 341 342 void wxWebBrowserShell::OnRunScript(wxCommandEvent& myEvent){ 343 if (webview) { 344 wxTextEntryDialog* dialog = new wxTextEntryDialog(this, _("Type in a JavaScript to exectute.")); 345 if (dialog->ShowModal() == wxID_OK) 346 wxMessageBox(wxT("Result is: ") + webview->RunScript(dialog->GetValue())); 347 348 dialog->Destroy(); 349 } 350 } 351