Reviewed by Mark Rowe.
[WebKit-https.git] / WebKit / wx / WebFrame.cpp
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 "wx/wxprec.h"
30 #ifndef WX_PRECOMP
31     #include "wx/wx.h"
32 #endif
33
34 #include "WebView.h"
35 #include "WebFrame.h"
36 #include "WebViewPrivate.h"
37
38 #include "wx/artprov.h"
39
40 wxPageSourceViewFrame::wxPageSourceViewFrame(const wxString& source)
41         : wxFrame(NULL, wxID_ANY, _("Page Source View"), wxDefaultPosition, wxSize(600, 500))
42 {
43     wxTextCtrl* control = new wxTextCtrl(this, -1, source, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
44 }
45
46 enum {
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
64 };
65
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)
85 END_EVENT_TABLE()
86
87
88 wxWebFrame::wxWebFrame(const wxString& title) : 
89         wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(600, 500)),
90         m_checkBeforeLoad(false)
91 {
92
93     // create a menu bar
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"));
99     
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"));
104     
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();
116     
117     m_debugMenu = new wxMenu;
118     m_debugMenu->Append(ID_SET_SOURCE, _("Test SetPageSource"));
119     m_debugMenu->Append(ID_RUN_SCRIPT, _("Test RunScript"));
120
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"));
124
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"));
131
132     // ... and attach this menu bar to the frame
133     SetMenuBar(menuBar);
134 /*
135     wxToolBar* toolbar = CreateToolBar();
136     toolbar->SetToolBitmapSize(wxSize(32, 32));
137     
138     wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK, wxART_TOOLBAR, wxSize(32,32));
139     toolbar->AddTool(ID_BACK, back, wxT("Back"));
140     
141     wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD, wxART_TOOLBAR, wxSize(32,32));
142     toolbar->AddTool(ID_FORWARD, forward, wxT("Next"));
143
144     addressBar = new wxTextCtrl(toolbar, ID_TEXTCTRL, _T(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER);
145     toolbar->AddControl(addressBar);
146     
147     searchCtrl = new wxSearchCtrl(toolbar, ID_SEARCHCTRL, _("Search"), wxDefaultPosition, wxSize(200, -1), wxTE_PROCESS_ENTER);
148     toolbar->AddControl(searchCtrl);
149     toolbar->Realize();
150     
151     SetToolBar(toolbar);
152 */
153     // Create the wxWebView Window
154     webview = new wxWebView((wxWindow*)this, 1001, wxDefaultPosition, wxSize(200, 200));
155     webview->SetBackgroundColour(*wxWHITE);
156
157     // create a status bar just for fun (by default with 1 pane only)
158     CreateStatusBar(2);
159 }
160
161 wxWebFrame::~wxWebFrame()
162 {
163     if (m_debugMenu && GetMenuBar()->FindMenu(_("&Debug")) == wxNOT_FOUND)
164         delete m_debugMenu;
165 }
166
167 void wxWebFrame::ShowDebugMenu(bool show)
168 {
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"));
174     }
175     else if (!show && debugMenu != wxNOT_FOUND) {
176         GetMenuBar()->Remove(debugMenu);
177     }
178 }
179
180 // event handlers
181
182 void wxWebFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
183 {
184     // true is to force the frame to close
185     Close(true);
186 }
187
188 void wxWebFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
189 {
190     wxString msg;
191     msg.Printf(_T("This is the About dialog of the wxWebKit sample.\n")
192                _T("Welcome to %s"), wxVERSION_STRING);
193
194     wxMessageBox(msg, _T("About wxWebKit Sample"), wxOK | wxICON_INFORMATION, this);
195
196 }
197
198 void wxWebFrame::OnLoadFile(wxCommandEvent& WXUNUSED(event))
199 {
200     wxFileDialog* dialog = new wxFileDialog(this, wxT("Choose a file"));
201     if (dialog->ShowModal() == wxID_OK) {  
202         wxString path = dialog->GetPath().Prepend(wxT("file://"));
203         
204         if (webview)
205             webview->LoadURL(path);
206     }
207 }
208
209 void wxWebFrame::OnStateChangedEvent(wxWebViewStateChangedEvent& event)
210 {
211     if (GetStatusBar() != NULL){
212         if (event.GetState() == wxWEBVIEW_STATE_NEGOTIATING) {
213             GetStatusBar()->SetStatusText(_("Contacting ") + event.GetURL());
214         }
215         else if (event.GetState() == wxWEBVIEW_STATE_TRANSFERRING) {
216             GetStatusBar()->SetStatusText(_("Loading ") + event.GetURL());
217         }
218         else if (event.GetState() == wxWEBVIEW_STATE_STOP) {
219             GetStatusBar()->SetStatusText(_("Load complete."));
220             addressBar->SetValue(event.GetURL());
221             SetTitle(webview->GetPageTitle());
222         }
223         else if (event.GetState() == wxWEBVIEW_STATE_FAILED) {
224             GetStatusBar()->SetStatusText(_("Failed to load ") + event.GetURL());
225         }
226     }
227 }
228
229 void wxWebFrame::OnBeforeLoad(wxWebViewBeforeLoadEvent& myEvent)
230 {
231     if (m_checkBeforeLoad) {
232         int reply = wxMessageBox(_("Would you like to continue loading ") + myEvent.GetURL() + wxT("?"), _("Continue Loading?"), wxYES_NO); 
233         if (reply == wxNO) {
234             myEvent.Cancel();
235         }
236     }
237 }
238
239 void wxWebFrame::OnAddressBarEnter(wxCommandEvent& event)
240 {
241     if (webview)
242         webview->LoadURL(addressBar->GetValue());
243 }
244
245 void wxWebFrame::OnSearchCtrlEnter(wxCommandEvent& event)
246 {
247     if (webview) {
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()));
249     }
250 }
251
252 void wxWebFrame::OnBack(wxCommandEvent& event)
253 {
254     if (webview)
255         webview->GoBack();
256 }
257
258 void wxWebFrame::OnForward(wxCommandEvent& event)
259 {
260     if (webview)
261         webview->GoForward();
262 }
263
264 void wxWebFrame::OnStop(wxCommandEvent& myEvent)
265 {
266     if (webview)
267         webview->Stop();
268 }
269
270 void wxWebFrame::OnReload(wxCommandEvent& myEvent)
271 {
272     if (webview)
273         webview->Reload();
274 }
275
276 void wxWebFrame::OnMakeTextLarger(wxCommandEvent& myEvent)
277 {
278     if (webview) {
279         if (webview->CanIncreaseTextSize())
280             webview->IncreaseTextSize();
281     }
282 }
283
284 void wxWebFrame::OnMakeTextSmaller(wxCommandEvent& myEvent)
285 {
286     if (webview) {
287         if (webview->CanDecreaseTextSize())
288             webview->DecreaseTextSize();
289     }
290 }
291
292 void wxWebFrame::OnGetSource(wxCommandEvent& myEvent)
293 {
294     if (webview) {
295         wxPageSourceViewFrame* wxWebFrame = new wxPageSourceViewFrame(webview->GetPageSource());
296         wxWebFrame->Show();
297     }
298 }
299
300 void wxWebFrame::OnSetSource(wxCommandEvent& event)
301 {
302     if (webview)
303         webview->SetPageSource(wxString(wxT("<p>Hello World!</p>")));
304 }
305
306 void wxWebFrame::OnBrowse(wxCommandEvent& event)
307 {
308     if (webview)
309         webview->MakeEditable(!event.IsChecked());
310 }
311
312 void wxWebFrame::OnEdit(wxCommandEvent& event)
313 {
314     if (webview)
315         webview->MakeEditable(event.IsChecked());
316 }
317
318 void wxWebFrame::OnRunScript(wxCommandEvent& myEvent){
319     if (webview) {
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()));
323     
324         dialog->Destroy();
325     }
326 }