Unreviewed, rolling out r96108, r96111, r96113, and r96116.
[WebKit-https.git] / Source / WebKit / wx / WebBrowserShell.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 "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 "WebBrowserShell.h"
39 #include "WebFrame.h"
40 #include "WebView.h"
41 #include "WebViewPrivate.h"
42
43 wxPageSourceViewFrame::wxPageSourceViewFrame(const wxString& source)
44         : wxFrame(NULL, wxID_ANY, _("Page Source View"), wxDefaultPosition, wxSize(600, 500))
45 {
46     wxTextCtrl* control = new wxTextCtrl(this, -1, source, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
47 }
48
49 enum {
50     ID_LOADFILE = wxID_HIGHEST + 1,
51     ID_TEXTCTRL = wxID_HIGHEST + 2,
52     ID_BACK = wxID_HIGHEST + 3,
53     ID_FORWARD = wxID_HIGHEST + 4,
54     ID_TOGGLE_BEFORE_LOAD = wxID_HIGHEST + 5,
55     ID_MAKE_TEXT_LARGER = wxID_HIGHEST + 6,
56     ID_MAKE_TEXT_SMALLER = wxID_HIGHEST + 7,
57     ID_STOP = wxID_HIGHEST + 8,
58     ID_RELOAD = wxID_HIGHEST + 9,
59     ID_GET_SOURCE = wxID_HIGHEST + 10,
60     ID_SET_SOURCE = wxID_HIGHEST + 11,
61     ID_SEARCHCTRL = wxID_HIGHEST + 12,
62     ID_LOADURL = wxID_HIGHEST + 13,
63     ID_NEW_WINDOW = wxID_HIGHEST + 14,
64     ID_BROWSE = wxID_HIGHEST + 15,
65     ID_EDIT = wxID_HIGHEST + 16,
66     ID_RUN_SCRIPT = wxID_HIGHEST + 17,
67     ID_WEBVIEW = wxID_HIGHEST + 18,
68     ID_EDIT_COMMAND = wxID_HIGHEST + 19,
69     ID_GET_EDIT_COMMAND_STATE = wxID_HIGHEST + 20
70 };
71
72 BEGIN_EVENT_TABLE(wxWebBrowserShell, wxFrame)
73     EVT_MENU(wxID_CUT, wxWebBrowserShell::OnCut)
74     EVT_MENU(wxID_COPY, wxWebBrowserShell::OnCopy)
75     EVT_MENU(wxID_PASTE, wxWebBrowserShell::OnPaste)
76     EVT_MENU(wxID_EXIT,  wxWebBrowserShell::OnQuit)
77     EVT_MENU(wxID_ABOUT, wxWebBrowserShell::OnAbout)
78     EVT_MENU(ID_LOADFILE, wxWebBrowserShell::OnLoadFile)
79     EVT_TEXT_ENTER(ID_TEXTCTRL, wxWebBrowserShell::OnAddressBarEnter)
80     EVT_TEXT_ENTER(ID_SEARCHCTRL, wxWebBrowserShell::OnSearchCtrlEnter)
81     EVT_WEBVIEW_LOAD(ID_WEBVIEW, wxWebBrowserShell::OnLoadEvent)
82     EVT_WEBVIEW_BEFORE_LOAD(ID_WEBVIEW, wxWebBrowserShell::OnBeforeLoad)
83     EVT_MENU(ID_BACK, wxWebBrowserShell::OnBack)
84     EVT_MENU(ID_FORWARD, wxWebBrowserShell::OnForward)
85     EVT_MENU(ID_STOP, wxWebBrowserShell::OnStop)
86     EVT_MENU(ID_RELOAD, wxWebBrowserShell::OnReload)
87     EVT_MENU(ID_MAKE_TEXT_LARGER, wxWebBrowserShell::OnMakeTextLarger)
88     EVT_MENU(ID_MAKE_TEXT_SMALLER, wxWebBrowserShell::OnMakeTextSmaller)
89     EVT_MENU(ID_GET_SOURCE, wxWebBrowserShell::OnGetSource)
90     EVT_MENU(ID_SET_SOURCE, wxWebBrowserShell::OnSetSource)
91     EVT_MENU(ID_BROWSE, wxWebBrowserShell::OnBrowse)
92     EVT_MENU(ID_EDIT, wxWebBrowserShell::OnEdit)
93     EVT_MENU(ID_RUN_SCRIPT, wxWebBrowserShell::OnRunScript)
94     EVT_MENU(ID_EDIT_COMMAND, wxWebBrowserShell::OnEditCommand)
95     EVT_MENU(ID_GET_EDIT_COMMAND_STATE, wxWebBrowserShell::OnGetEditCommandState)
96     EVT_MENU(wxID_PRINT, wxWebBrowserShell::OnPrint)
97 END_EVENT_TABLE()
98
99
100 wxWebBrowserShell::wxWebBrowserShell(const wxString& title) : 
101         wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(600, 500)),
102         m_checkBeforeLoad(false)
103 {
104     // create a menu bar
105     wxMenu *fileMenu = new wxMenu;
106     fileMenu->Append(ID_NEW_WINDOW, _T("New Window\tCTRL+N"));
107     fileMenu->Append(ID_LOADFILE, _T("Open File...\tCTRL+O"));
108     fileMenu->Append(ID_LOADURL, _("Open Location...\tCTRL+L"));
109     fileMenu->AppendSeparator();
110     fileMenu->Append(wxID_PRINT, _("Print..."));
111     fileMenu->Append(wxID_EXIT, _T("E&xit\tAlt-X"), _T("Quit this program"));
112     
113     wxMenu *editMenu = new wxMenu;
114     editMenu->Append(wxID_CUT, _T("Cut\tCTRL+X"));
115     editMenu->Append(wxID_COPY, _T("Copy\tCTRL+C"));
116     editMenu->Append(wxID_PASTE, _T("Paste\tCTRL+V"));
117
118     wxMenu* viewMenu = new wxMenu;
119     viewMenu->AppendRadioItem(ID_BROWSE, _("Browse"));
120     viewMenu->AppendRadioItem(ID_EDIT, _("Edit"));
121     viewMenu->AppendSeparator();
122     viewMenu->Append(ID_STOP, _("Stop"));
123     viewMenu->Append(ID_RELOAD, _("Reload Page"));
124     viewMenu->Append(ID_MAKE_TEXT_SMALLER, _("Make Text Smaller\tCTRL+-"));
125     viewMenu->Append(ID_MAKE_TEXT_LARGER, _("Make Text Bigger\tCTRL++"));
126     viewMenu->AppendSeparator();
127     viewMenu->Append(ID_GET_SOURCE, _("View Page Source"));
128     viewMenu->AppendSeparator();
129     
130     m_debugMenu = new wxMenu;
131     m_debugMenu->Append(ID_SET_SOURCE, _("Test SetPageSource"));
132     m_debugMenu->Append(ID_RUN_SCRIPT, _("Test RunScript"));
133     m_debugMenu->Append(ID_EDIT_COMMAND, _("Test EditCommand::Execute"));
134     m_debugMenu->Append(ID_GET_EDIT_COMMAND_STATE, _("Test EditCommand::GetState"));
135
136     // the "About" item should be in the help menu
137     wxMenu *helpMenu = new wxMenu;
138     helpMenu->Append(wxID_ABOUT, _T("&About...\tF1"), _T("Show about dialog"));
139
140     // now append the freshly created menu to the menu bar...
141     wxMenuBar *menuBar = new wxMenuBar();
142     menuBar->Append(fileMenu, _T("&File"));
143     menuBar->Append(editMenu, _T("&Edit"));
144     menuBar->Append(viewMenu, _T("&View"));
145     menuBar->Append(helpMenu, _T("&Help"));
146
147     // ... and attach this menu bar to the frame
148     SetMenuBar(menuBar);
149     
150     wxToolBar* toolbar = CreateToolBar();
151     toolbar->SetToolBitmapSize(wxSize(32, 32));
152     
153     wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK, wxART_TOOLBAR, wxSize(32,32));
154     toolbar->AddTool(ID_BACK, back, wxT("Back"));
155     
156     wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD, wxART_TOOLBAR, wxSize(32,32));
157     toolbar->AddTool(ID_FORWARD, forward, wxT("Next"));
158
159     addressBar = new wxTextCtrl(toolbar, ID_TEXTCTRL, _T(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER);
160     toolbar->AddControl(addressBar);
161     
162     searchCtrl = new wxSearchCtrl(toolbar, ID_SEARCHCTRL, _("Search"), wxDefaultPosition, wxSize(200, -1), wxTE_PROCESS_ENTER);
163     toolbar->AddControl(searchCtrl);
164     toolbar->Realize();
165     
166     SetToolBar(toolbar);
167
168     // Create the wxWebView Window
169     webview = new wxWebView((wxWindow*)this, ID_WEBVIEW, wxDefaultPosition, wxSize(200, 200));
170     webview->SetBackgroundColour(*wxWHITE);
171
172     // create a status bar just for fun (by default with 1 pane only)
173     CreateStatusBar(2);
174 }
175
176 wxWebBrowserShell::~wxWebBrowserShell()
177 {
178     if (m_debugMenu && GetMenuBar()->FindMenu(_("&Debug")) == wxNOT_FOUND)
179         delete m_debugMenu;
180 }
181
182 void wxWebBrowserShell::ShowDebugMenu(bool show)
183 {
184     int debugMenu = GetMenuBar()->FindMenu(_("&Debug"));
185     if (show && debugMenu == wxNOT_FOUND) {
186         int prevMenu = GetMenuBar()->FindMenu(_("&View"));
187         if (prevMenu != wxNOT_FOUND)
188             GetMenuBar()->Insert((size_t)prevMenu+1, m_debugMenu, _("&Debug"));
189     }
190     else if (!show && debugMenu != wxNOT_FOUND) {
191         GetMenuBar()->Remove(debugMenu);
192     }
193 }
194
195 // event handlers
196
197 void wxWebBrowserShell::OnQuit(wxCommandEvent& WXUNUSED(event))
198 {
199     // true is to force the frame to close
200     Close(true);
201 }
202
203 void wxWebBrowserShell::OnAbout(wxCommandEvent& WXUNUSED(event))
204 {
205     wxString msg;
206     msg.Printf(_T("This is the About dialog of the wxWebKit sample.\n")
207                _T("Welcome to %s"), wxVERSION_STRING);
208
209     wxMessageBox(msg, _T("About wxWebKit Sample"), wxOK | wxICON_INFORMATION, this);
210
211 }
212
213 void wxWebBrowserShell::OnLoadFile(wxCommandEvent& WXUNUSED(event))
214 {
215     wxFileDialog* dialog = new wxFileDialog(this, wxT("Choose a file"));
216     if (dialog->ShowModal() == wxID_OK) {  
217         wxString path = dialog->GetPath().Prepend(wxT("file://"));
218         
219         if (webview)
220             webview->LoadURL(path);
221     }
222 }
223
224 void wxWebBrowserShell::OnLoadEvent(wxWebViewLoadEvent& event)
225 {
226     if (GetStatusBar() != NULL){
227         if (event.GetState() == wxWEBVIEW_LOAD_NEGOTIATING) {
228             GetStatusBar()->SetStatusText(_("Contacting ") + event.GetURL());
229         }
230         else if (event.GetState() == wxWEBVIEW_LOAD_TRANSFERRING) {
231             GetStatusBar()->SetStatusText(_("Loading ") + event.GetURL());
232             if (event.GetFrame() == webview->GetMainFrame())
233                 addressBar->SetValue(event.GetURL());
234         }
235         else if (event.GetState() == wxWEBVIEW_LOAD_ONLOAD_HANDLED) {
236             GetStatusBar()->SetStatusText(_("Load complete."));
237             if (event.GetFrame() == webview->GetMainFrame())
238                 SetTitle(webview->GetPageTitle());
239         }
240         else if (event.GetState() == wxWEBVIEW_LOAD_FAILED) {
241             GetStatusBar()->SetStatusText(_("Failed to load ") + event.GetURL());
242         }
243     }
244 }
245
246 void wxWebBrowserShell::OnBeforeLoad(wxWebViewBeforeLoadEvent& myEvent)
247 {
248     if (m_checkBeforeLoad) {
249         int reply = wxMessageBox(_("Would you like to continue loading ") + myEvent.GetURL() + wxT("?"), _("Continue Loading?"), wxYES_NO); 
250         if (reply == wxNO) {
251             myEvent.Cancel();
252         }
253     }
254 }
255
256 void wxWebBrowserShell::OnAddressBarEnter(wxCommandEvent& event)
257 {
258     if (webview)
259         webview->LoadURL(addressBar->GetValue());
260 }
261
262 void wxWebBrowserShell::OnSearchCtrlEnter(wxCommandEvent& event)
263 {
264     if (webview) {
265         webview->LoadURL(wxString::Format(wxT("http://www.google.com/search?rls=en&q=%s&ie=UTF-8&oe=UTF-8"), searchCtrl->GetValue().wc_str()));
266     }
267 }
268
269 void wxWebBrowserShell::OnCut(wxCommandEvent& event)
270 {
271     if (webview && webview->CanCut())
272         webview->Cut();
273 }
274
275 void wxWebBrowserShell::OnCopy(wxCommandEvent& event)
276 {
277     if (webview && webview->CanCopy())
278         webview->Copy();
279 }
280
281 void wxWebBrowserShell::OnPaste(wxCommandEvent& event)
282 {
283     if (webview && webview->CanPaste())
284         webview->Paste();
285 }
286
287 void wxWebBrowserShell::OnBack(wxCommandEvent& event)
288 {
289     if (webview)
290         webview->GoBack();
291 }
292
293 void wxWebBrowserShell::OnForward(wxCommandEvent& event)
294 {
295     if (webview)
296         webview->GoForward();
297 }
298
299 void wxWebBrowserShell::OnStop(wxCommandEvent& myEvent)
300 {
301     if (webview)
302         webview->Stop();
303 }
304
305 void wxWebBrowserShell::OnReload(wxCommandEvent& myEvent)
306 {
307     if (webview)
308         webview->Reload();
309 }
310
311 void wxWebBrowserShell::OnMakeTextLarger(wxCommandEvent& myEvent)
312 {
313     if (webview) {
314         if (webview->CanIncreaseTextSize())
315             webview->IncreaseTextSize();
316     }
317 }
318
319 void wxWebBrowserShell::OnMakeTextSmaller(wxCommandEvent& myEvent)
320 {
321     if (webview) {
322         if (webview->CanDecreaseTextSize())
323             webview->DecreaseTextSize();
324     }
325 }
326
327 void wxWebBrowserShell::OnGetSource(wxCommandEvent& myEvent)
328 {
329     if (webview) {
330         wxPageSourceViewFrame* pageSourceFrame = new wxPageSourceViewFrame(webview->GetPageSource());
331         pageSourceFrame->Show();
332     }
333 }
334
335 void wxWebBrowserShell::OnSetSource(wxCommandEvent& event)
336 {
337     if (webview)
338         webview->SetPageSource(wxString(wxT("<p>Hello World!</p>")));
339 }
340
341 void wxWebBrowserShell::OnBrowse(wxCommandEvent& event)
342 {
343     if (webview)
344         webview->MakeEditable(!event.IsChecked());
345 }
346
347 void wxWebBrowserShell::OnEdit(wxCommandEvent& event)
348 {
349     if (webview)
350         webview->MakeEditable(event.IsChecked());
351 }
352
353 void wxWebBrowserShell::OnRunScript(wxCommandEvent& myEvent)
354 {
355     if (webview) {
356         wxTextEntryDialog* dialog = new wxTextEntryDialog(this, _("Type in a JavaScript to exectute."));
357         if (dialog->ShowModal() == wxID_OK)
358             wxMessageBox(wxT("Result is: ") + webview->RunScript(dialog->GetValue()));
359     
360         dialog->Destroy();
361     }
362 }
363
364 void wxWebBrowserShell::OnEditCommand(wxCommandEvent& myEvent)
365 {
366     if (webview) {
367         if (!webview->IsEditable()) {
368             wxMessageBox(wxT("Please enable editing before running editing commands."));
369             return;
370         }
371         
372         wxTextEntryDialog* dialog = new wxTextEntryDialog(this, _("Type in a editing command to exectute."));
373         if (dialog->ShowModal() == wxID_OK) {
374             bool result = webview->ExecuteEditCommand(dialog->GetValue());
375             if (!result)
376                 wxMessageBox(wxT("Editing command failed."));
377         }
378         dialog->Destroy();
379     }
380 }
381
382 void wxWebBrowserShell::OnGetEditCommandState(wxCommandEvent& myEvent)
383 {
384     if (webview) {
385         if (!webview->IsEditable()) {
386             wxMessageBox(wxT("Please enable editing before running editing commands."));
387             return;
388         }
389         
390         wxTextEntryDialog* dialog = new wxTextEntryDialog(this, _("Type in a editing command whose state you want to get."));
391         if (dialog->ShowModal() == wxID_OK) {
392             EditState result = webview->GetEditCommandState(dialog->GetValue());
393             if (result == EditStateTrue)
394                 wxMessageBox(wxT("State is true."));
395             else if (result == EditStateFalse)
396                 wxMessageBox(wxT("State is false."));
397             else if (result == EditStateMixed)
398                 wxMessageBox(wxT("State is mixed."));
399         }
400         dialog->Destroy();
401     }
402 }
403
404 void wxWebBrowserShell::OnPrint(wxCommandEvent& myEvent)
405 {
406     if (webview && webview->GetMainFrame())
407         webview->GetMainFrame()->Print();
408 }