2 * Copyright (C) 2007 Kevin Ollivier All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
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.
29 #include "wx/wxprec.h"
36 #include "WebViewPrivate.h"
38 #include "wx/artprov.h"
40 wxPageSourceViewFrame::wxPageSourceViewFrame(const wxString& source)
41 : wxFrame(NULL, wxID_ANY, _("Page Source View"), wxDefaultPosition, wxSize(600, 500))
43 wxTextCtrl* control = new wxTextCtrl(this, -1, source, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
47 ID_LOADFILE = wxID_HIGHEST + 1,
48 ID_TEXTCTRL = wxID_HIGHEST + 2,
49 ID_BACK = wxID_HIGHEST + 3,
50 ID_FORWARD = wxID_HIGHEST + 4,
51 ID_TOGGLE_BEFORE_LOAD = wxID_HIGHEST + 5,
52 ID_MAKE_TEXT_LARGER = wxID_HIGHEST + 6,
53 ID_MAKE_TEXT_SMALLER = wxID_HIGHEST + 7,
54 ID_STOP = wxID_HIGHEST + 8,
55 ID_RELOAD = wxID_HIGHEST + 9,
56 ID_GET_SOURCE = wxID_HIGHEST + 10,
57 ID_SET_SOURCE = wxID_HIGHEST + 11,
58 ID_SEARCHCTRL = wxID_HIGHEST + 12,
59 ID_LOADURL = wxID_HIGHEST + 13,
60 ID_NEW_WINDOW = wxID_HIGHEST + 14,
61 ID_BROWSE = wxID_HIGHEST + 15,
62 ID_EDIT = wxID_HIGHEST + 16,
63 ID_RUN_SCRIPT = wxID_HIGHEST + 17
66 BEGIN_EVENT_TABLE(wxWebFrame, wxFrame)
67 EVT_MENU(wxID_EXIT, wxWebFrame::OnQuit)
68 EVT_MENU(wxID_ABOUT, wxWebFrame::OnAbout)
69 EVT_MENU(ID_LOADFILE, wxWebFrame::OnLoadFile)
70 EVT_TEXT_ENTER(ID_TEXTCTRL, wxWebFrame::OnAddressBarEnter)
71 EVT_TEXT_ENTER(ID_SEARCHCTRL, wxWebFrame::OnSearchCtrlEnter)
72 EVT_WEBVIEW_STATE_CHANGED(wxWebFrame::OnStateChangedEvent)
73 EVT_WEBVIEW_BEFORE_LOAD(wxWebFrame::OnBeforeLoad)
74 EVT_MENU(ID_BACK, wxWebFrame::OnBack)
75 EVT_MENU(ID_FORWARD, wxWebFrame::OnForward)
76 EVT_MENU(ID_STOP, wxWebFrame::OnStop)
77 EVT_MENU(ID_RELOAD, wxWebFrame::OnReload)
78 EVT_MENU(ID_MAKE_TEXT_LARGER, wxWebFrame::OnMakeTextLarger)
79 EVT_MENU(ID_MAKE_TEXT_SMALLER, wxWebFrame::OnMakeTextSmaller)
80 EVT_MENU(ID_GET_SOURCE, wxWebFrame::OnGetSource)
81 EVT_MENU(ID_SET_SOURCE, wxWebFrame::OnSetSource)
82 EVT_MENU(ID_BROWSE, wxWebFrame::OnBrowse)
83 EVT_MENU(ID_EDIT, wxWebFrame::OnEdit)
84 EVT_MENU(ID_RUN_SCRIPT, wxWebFrame::OnRunScript)
88 wxWebFrame::wxWebFrame(const wxString& title) :
89 wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(600, 500)),
90 m_checkBeforeLoad(false)
94 wxMenu *fileMenu = new wxMenu;
95 fileMenu->Append(ID_NEW_WINDOW, _T("New Window\tCTRL+N"));
96 fileMenu->Append(ID_LOADFILE, _T("Open File...\tCTRL+O"));
97 fileMenu->Append(ID_LOADURL, _("Open Location...\tCTRL+L"));
98 fileMenu->Append(wxID_EXIT, _T("E&xit\tAlt-X"), _T("Quit this program"));
100 wxMenu *editMenu = new wxMenu;
101 editMenu->Append(wxID_CUT, _T("Cut\tCTRL+X"));
102 editMenu->Append(wxID_COPY, _T("Copy\tCTRL+C"));
103 editMenu->Append(wxID_PASTE, _T("Paste\tCTRL+V"));
105 wxMenu* viewMenu = new wxMenu;
106 viewMenu->AppendRadioItem(ID_BROWSE, _("Browse"));
107 viewMenu->AppendRadioItem(ID_EDIT, _("Edit"));
108 viewMenu->AppendSeparator();
109 viewMenu->Append(ID_STOP, _("Stop"));
110 viewMenu->Append(ID_RELOAD, _("Reload Page"));
111 viewMenu->Append(ID_MAKE_TEXT_SMALLER, _("Make Text Smaller\tCTRL+-"));
112 viewMenu->Append(ID_MAKE_TEXT_LARGER, _("Make Text Bigger\tCTRL++"));
113 viewMenu->AppendSeparator();
114 viewMenu->Append(ID_GET_SOURCE, _("View Page Source"));
115 viewMenu->AppendSeparator();
117 m_debugMenu = new wxMenu;
118 m_debugMenu->Append(ID_SET_SOURCE, _("Test SetPageSource"));
119 m_debugMenu->Append(ID_RUN_SCRIPT, _("Test RunScript"));
121 // the "About" item should be in the help menu
122 wxMenu *helpMenu = new wxMenu;
123 helpMenu->Append(wxID_ABOUT, _T("&About...\tF1"), _T("Show about dialog"));
125 // now append the freshly created menu to the menu bar...
126 wxMenuBar *menuBar = new wxMenuBar();
127 menuBar->Append(fileMenu, _T("&File"));
128 menuBar->Append(editMenu, _T("&Edit"));
129 menuBar->Append(viewMenu, _T("&View"));
130 menuBar->Append(helpMenu, _T("&Help"));
132 // ... and attach this menu bar to the frame
135 wxToolBar* toolbar = CreateToolBar();
136 toolbar->SetToolBitmapSize(wxSize(32, 32));
138 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK, wxART_TOOLBAR, wxSize(32,32));
139 toolbar->AddTool(ID_BACK, back, wxT("Back"));
141 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD, wxART_TOOLBAR, wxSize(32,32));
142 toolbar->AddTool(ID_FORWARD, forward, wxT("Next"));
144 addressBar = new wxTextCtrl(toolbar, ID_TEXTCTRL, _T(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER);
145 toolbar->AddControl(addressBar);
147 searchCtrl = new wxSearchCtrl(toolbar, ID_SEARCHCTRL, _("Search"), wxDefaultPosition, wxSize(200, -1), wxTE_PROCESS_ENTER);
148 toolbar->AddControl(searchCtrl);
153 // Create the wxWebView Window
154 webview = new wxWebView((wxWindow*)this, 1001, wxDefaultPosition, wxSize(200, 200));
155 webview->SetBackgroundColour(*wxWHITE);
157 // create a status bar just for fun (by default with 1 pane only)
161 wxWebFrame::~wxWebFrame()
163 if (m_debugMenu && GetMenuBar()->FindMenu(_("&Debug")) == wxNOT_FOUND)
167 void wxWebFrame::ShowDebugMenu(bool show)
169 int debugMenu = GetMenuBar()->FindMenu(_("&Debug"));
170 if (show && debugMenu == wxNOT_FOUND) {
171 int prevMenu = GetMenuBar()->FindMenu(_("&View"));
172 if (prevMenu != wxNOT_FOUND)
173 GetMenuBar()->Insert((size_t)prevMenu+1, m_debugMenu, _("&Debug"));
175 else if (!show && debugMenu != wxNOT_FOUND) {
176 GetMenuBar()->Remove(debugMenu);
182 void wxWebFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
184 // true is to force the frame to close
188 void wxWebFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
191 msg.Printf(_T("This is the About dialog of the wxWebKit sample.\n")
192 _T("Welcome to %s"), wxVERSION_STRING);
194 wxMessageBox(msg, _T("About wxWebKit Sample"), wxOK | wxICON_INFORMATION, this);
198 void wxWebFrame::OnLoadFile(wxCommandEvent& WXUNUSED(event))
200 wxFileDialog* dialog = new wxFileDialog(this, wxT("Choose a file"));
201 if (dialog->ShowModal() == wxID_OK) {
202 wxString path = dialog->GetPath().Prepend(wxT("file://"));
205 webview->LoadURL(path);
209 void wxWebFrame::OnStateChangedEvent(wxWebViewStateChangedEvent& event)
211 if (GetStatusBar() != NULL){
212 if (event.GetState() == wxWEBVIEW_STATE_NEGOTIATING) {
213 GetStatusBar()->SetStatusText(_("Contacting ") + event.GetURL());
215 else if (event.GetState() == wxWEBVIEW_STATE_TRANSFERRING) {
216 GetStatusBar()->SetStatusText(_("Loading ") + event.GetURL());
218 else if (event.GetState() == wxWEBVIEW_STATE_STOP) {
219 GetStatusBar()->SetStatusText(_("Load complete."));
220 addressBar->SetValue(event.GetURL());
221 SetTitle(webview->GetPageTitle());
223 else if (event.GetState() == wxWEBVIEW_STATE_FAILED) {
224 GetStatusBar()->SetStatusText(_("Failed to load ") + event.GetURL());
229 void wxWebFrame::OnBeforeLoad(wxWebViewBeforeLoadEvent& myEvent)
231 if (m_checkBeforeLoad) {
232 int reply = wxMessageBox(_("Would you like to continue loading ") + myEvent.GetURL() + wxT("?"), _("Continue Loading?"), wxYES_NO);
239 void wxWebFrame::OnAddressBarEnter(wxCommandEvent& event)
242 webview->LoadURL(addressBar->GetValue());
245 void wxWebFrame::OnSearchCtrlEnter(wxCommandEvent& event)
248 webview->LoadURL(wxString::Format(wxT("http://www.google.com/search?rls=en&q=%s&ie=UTF-8&oe=UTF-8"), searchCtrl->GetValue().wc_str()));
252 void wxWebFrame::OnBack(wxCommandEvent& event)
258 void wxWebFrame::OnForward(wxCommandEvent& event)
261 webview->GoForward();
264 void wxWebFrame::OnStop(wxCommandEvent& myEvent)
270 void wxWebFrame::OnReload(wxCommandEvent& myEvent)
276 void wxWebFrame::OnMakeTextLarger(wxCommandEvent& myEvent)
279 if (webview->CanIncreaseTextSize())
280 webview->IncreaseTextSize();
284 void wxWebFrame::OnMakeTextSmaller(wxCommandEvent& myEvent)
287 if (webview->CanDecreaseTextSize())
288 webview->DecreaseTextSize();
292 void wxWebFrame::OnGetSource(wxCommandEvent& myEvent)
295 wxPageSourceViewFrame* wxWebFrame = new wxPageSourceViewFrame(webview->GetPageSource());
300 void wxWebFrame::OnSetSource(wxCommandEvent& event)
303 webview->SetPageSource(wxString(wxT("<p>Hello World!</p>")));
306 void wxWebFrame::OnBrowse(wxCommandEvent& event)
309 webview->MakeEditable(!event.IsChecked());
312 void wxWebFrame::OnEdit(wxCommandEvent& event)
315 webview->MakeEditable(event.IsChecked());
318 void wxWebFrame::OnRunScript(wxCommandEvent& myEvent){
320 wxTextEntryDialog* dialog = new wxTextEntryDialog(this, _("Type in a JavaScript to exectute."));
321 if (dialog->ShowModal() == wxID_OK)
322 wxMessageBox(wxT("Result is: ") + webview->RunScript(dialog->GetValue()));