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