Add another feed to Planet WebKit.
[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 "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 "WebFrame.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 };
67
68 BEGIN_EVENT_TABLE(wxWebFrame, wxFrame)
69     EVT_MENU(wxID_EXIT,  wxWebFrame::OnQuit)
70     EVT_MENU(wxID_ABOUT, wxWebFrame::OnAbout)
71     EVT_MENU(ID_LOADFILE, wxWebFrame::OnLoadFile)
72     EVT_TEXT_ENTER(ID_TEXTCTRL, wxWebFrame::OnAddressBarEnter)
73     EVT_TEXT_ENTER(ID_SEARCHCTRL, wxWebFrame::OnSearchCtrlEnter)
74     EVT_WEBVIEW_STATE_CHANGED(wxWebFrame::OnStateChangedEvent)
75     EVT_WEBVIEW_BEFORE_LOAD(wxWebFrame::OnBeforeLoad)
76     EVT_MENU(ID_BACK, wxWebFrame::OnBack)
77     EVT_MENU(ID_FORWARD, wxWebFrame::OnForward)
78     EVT_MENU(ID_STOP, wxWebFrame::OnStop)
79     EVT_MENU(ID_RELOAD, wxWebFrame::OnReload)
80     EVT_MENU(ID_MAKE_TEXT_LARGER, wxWebFrame::OnMakeTextLarger)
81     EVT_MENU(ID_MAKE_TEXT_SMALLER, wxWebFrame::OnMakeTextSmaller)
82     EVT_MENU(ID_GET_SOURCE, wxWebFrame::OnGetSource)
83     EVT_MENU(ID_SET_SOURCE, wxWebFrame::OnSetSource)
84     EVT_MENU(ID_BROWSE, wxWebFrame::OnBrowse)
85     EVT_MENU(ID_EDIT, wxWebFrame::OnEdit)
86     EVT_MENU(ID_RUN_SCRIPT, wxWebFrame::OnRunScript)
87 END_EVENT_TABLE()
88
89
90 wxWebFrame::wxWebFrame(const wxString& title) : 
91         wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(600, 500)),
92         m_checkBeforeLoad(false)
93 {
94
95     // create a menu bar
96     wxMenu *fileMenu = new wxMenu;
97     fileMenu->Append(ID_NEW_WINDOW, _T("New Window\tCTRL+N"));
98     fileMenu->Append(ID_LOADFILE, _T("Open File...\tCTRL+O"));
99     fileMenu->Append(ID_LOADURL, _("Open Location...\tCTRL+L"));
100     fileMenu->Append(wxID_EXIT, _T("E&xit\tAlt-X"), _T("Quit this program"));
101     
102     wxMenu *editMenu = new wxMenu;
103     editMenu->Append(wxID_CUT, _T("Cut\tCTRL+X"));
104     editMenu->Append(wxID_COPY, _T("Copy\tCTRL+C"));
105     editMenu->Append(wxID_PASTE, _T("Paste\tCTRL+V"));
106     
107     wxMenu* viewMenu = new wxMenu;
108     viewMenu->AppendRadioItem(ID_BROWSE, _("Browse"));
109     viewMenu->AppendRadioItem(ID_EDIT, _("Edit"));
110     viewMenu->AppendSeparator();
111     viewMenu->Append(ID_STOP, _("Stop"));
112     viewMenu->Append(ID_RELOAD, _("Reload Page"));
113     viewMenu->Append(ID_MAKE_TEXT_SMALLER, _("Make Text Smaller\tCTRL+-"));
114     viewMenu->Append(ID_MAKE_TEXT_LARGER, _("Make Text Bigger\tCTRL++"));
115     viewMenu->AppendSeparator();
116     viewMenu->Append(ID_GET_SOURCE, _("View Page Source"));
117     viewMenu->AppendSeparator();
118     
119     m_debugMenu = new wxMenu;
120     m_debugMenu->Append(ID_SET_SOURCE, _("Test SetPageSource"));
121     m_debugMenu->Append(ID_RUN_SCRIPT, _("Test RunScript"));
122
123     // the "About" item should be in the help menu
124     wxMenu *helpMenu = new wxMenu;
125     helpMenu->Append(wxID_ABOUT, _T("&About...\tF1"), _T("Show about dialog"));
126
127     // now append the freshly created menu to the menu bar...
128     wxMenuBar *menuBar = new wxMenuBar();
129     menuBar->Append(fileMenu, _T("&File"));
130     menuBar->Append(editMenu, _T("&Edit"));
131     menuBar->Append(viewMenu, _T("&View"));
132     menuBar->Append(helpMenu, _T("&Help"));
133
134     // ... and attach this menu bar to the frame
135     SetMenuBar(menuBar);
136     
137     wxToolBar* toolbar = CreateToolBar();
138     toolbar->SetToolBitmapSize(wxSize(32, 32));
139     
140     wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK, wxART_TOOLBAR, wxSize(32,32));
141     toolbar->AddTool(ID_BACK, back, wxT("Back"));
142     
143     wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD, wxART_TOOLBAR, wxSize(32,32));
144     toolbar->AddTool(ID_FORWARD, forward, wxT("Next"));
145
146     addressBar = new wxTextCtrl(toolbar, ID_TEXTCTRL, _T(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER);
147     toolbar->AddControl(addressBar);
148     
149     searchCtrl = new wxSearchCtrl(toolbar, ID_SEARCHCTRL, _("Search"), wxDefaultPosition, wxSize(200, -1), wxTE_PROCESS_ENTER);
150     toolbar->AddControl(searchCtrl);
151     toolbar->Realize();
152     
153     SetToolBar(toolbar);
154
155     // Create the wxWebView Window
156     webview = new wxWebView((wxWindow*)this, 1001, wxDefaultPosition, wxSize(200, 200));
157     webview->SetBackgroundColour(*wxWHITE);
158
159     // create a status bar just for fun (by default with 1 pane only)
160     CreateStatusBar(2);
161 }
162
163 wxWebFrame::~wxWebFrame()
164 {
165     if (m_debugMenu && GetMenuBar()->FindMenu(_("&Debug")) == wxNOT_FOUND)
166         delete m_debugMenu;
167 }
168
169 void wxWebFrame::ShowDebugMenu(bool show)
170 {
171     int debugMenu = GetMenuBar()->FindMenu(_("&Debug"));
172     if (show && debugMenu == wxNOT_FOUND) {
173         int prevMenu = GetMenuBar()->FindMenu(_("&View"));
174         if (prevMenu != wxNOT_FOUND)
175             GetMenuBar()->Insert((size_t)prevMenu+1, m_debugMenu, _("&Debug"));
176     }
177     else if (!show && debugMenu != wxNOT_FOUND) {
178         GetMenuBar()->Remove(debugMenu);
179     }
180 }
181
182 // event handlers
183
184 void wxWebFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
185 {
186     // true is to force the frame to close
187     Close(true);
188 }
189
190 void wxWebFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
191 {
192     wxString msg;
193     msg.Printf(_T("This is the About dialog of the wxWebKit sample.\n")
194                _T("Welcome to %s"), wxVERSION_STRING);
195
196     wxMessageBox(msg, _T("About wxWebKit Sample"), wxOK | wxICON_INFORMATION, this);
197
198 }
199
200 void wxWebFrame::OnLoadFile(wxCommandEvent& WXUNUSED(event))
201 {
202     wxFileDialog* dialog = new wxFileDialog(this, wxT("Choose a file"));
203     if (dialog->ShowModal() == wxID_OK) {  
204         wxString path = dialog->GetPath().Prepend(wxT("file://"));
205         
206         if (webview)
207             webview->LoadURL(path);
208     }
209 }
210
211 void wxWebFrame::OnStateChangedEvent(wxWebViewStateChangedEvent& event)
212 {
213     if (GetStatusBar() != NULL){
214         if (event.GetState() == wxWEBVIEW_STATE_NEGOTIATING) {
215             GetStatusBar()->SetStatusText(_("Contacting ") + event.GetURL());
216         }
217         else if (event.GetState() == wxWEBVIEW_STATE_TRANSFERRING) {
218             GetStatusBar()->SetStatusText(_("Loading ") + event.GetURL());
219         }
220         else if (event.GetState() == wxWEBVIEW_STATE_STOP) {
221             GetStatusBar()->SetStatusText(_("Load complete."));
222             addressBar->SetValue(event.GetURL());
223             SetTitle(webview->GetPageTitle());
224         }
225         else if (event.GetState() == wxWEBVIEW_STATE_FAILED) {
226             GetStatusBar()->SetStatusText(_("Failed to load ") + event.GetURL());
227         }
228     }
229 }
230
231 void wxWebFrame::OnBeforeLoad(wxWebViewBeforeLoadEvent& myEvent)
232 {
233     if (m_checkBeforeLoad) {
234         int reply = wxMessageBox(_("Would you like to continue loading ") + myEvent.GetURL() + wxT("?"), _("Continue Loading?"), wxYES_NO); 
235         if (reply == wxNO) {
236             myEvent.Cancel();
237         }
238     }
239 }
240
241 void wxWebFrame::OnAddressBarEnter(wxCommandEvent& event)
242 {
243     if (webview)
244         webview->LoadURL(addressBar->GetValue());
245 }
246
247 void wxWebFrame::OnSearchCtrlEnter(wxCommandEvent& event)
248 {
249     if (webview) {
250         webview->LoadURL(wxString::Format(wxT("http://www.google.com/search?rls=en&q=%s&ie=UTF-8&oe=UTF-8"), searchCtrl->GetValue().wc_str()));
251     }
252 }
253
254 void wxWebFrame::OnBack(wxCommandEvent& event)
255 {
256     if (webview)
257         webview->GoBack();
258 }
259
260 void wxWebFrame::OnForward(wxCommandEvent& event)
261 {
262     if (webview)
263         webview->GoForward();
264 }
265
266 void wxWebFrame::OnStop(wxCommandEvent& myEvent)
267 {
268     if (webview)
269         webview->Stop();
270 }
271
272 void wxWebFrame::OnReload(wxCommandEvent& myEvent)
273 {
274     if (webview)
275         webview->Reload();
276 }
277
278 void wxWebFrame::OnMakeTextLarger(wxCommandEvent& myEvent)
279 {
280     if (webview) {
281         if (webview->CanIncreaseTextSize())
282             webview->IncreaseTextSize();
283     }
284 }
285
286 void wxWebFrame::OnMakeTextSmaller(wxCommandEvent& myEvent)
287 {
288     if (webview) {
289         if (webview->CanDecreaseTextSize())
290             webview->DecreaseTextSize();
291     }
292 }
293
294 void wxWebFrame::OnGetSource(wxCommandEvent& myEvent)
295 {
296     if (webview) {
297         wxPageSourceViewFrame* wxWebFrame = new wxPageSourceViewFrame(webview->GetPageSource());
298         wxWebFrame->Show();
299     }
300 }
301
302 void wxWebFrame::OnSetSource(wxCommandEvent& event)
303 {
304     if (webview)
305         webview->SetPageSource(wxString(wxT("<p>Hello World!</p>")));
306 }
307
308 void wxWebFrame::OnBrowse(wxCommandEvent& event)
309 {
310     if (webview)
311         webview->MakeEditable(!event.IsChecked());
312 }
313
314 void wxWebFrame::OnEdit(wxCommandEvent& event)
315 {
316     if (webview)
317         webview->MakeEditable(event.IsChecked());
318 }
319
320 void wxWebFrame::OnRunScript(wxCommandEvent& myEvent){
321     if (webview) {
322         wxTextEntryDialog* dialog = new wxTextEntryDialog(this, _("Type in a JavaScript to exectute."));
323         if (dialog->ShowModal() == wxID_OK)
324             wxMessageBox(wxT("Result is: ") + webview->RunScript(dialog->GetValue()));
325     
326         dialog->Destroy();
327     }
328 }