]> git.saurik.com Git - wxWidgets.git/blame - samples/webview/webview.cpp
Document that wxDC::FloodFill() is not implemented under wxOSX.
[wxWidgets.git] / samples / webview / webview.cpp
CommitLineData
61b98a2d 1/////////////////////////////////////////////////////////////////////////////
467d261e 2// Name: webview.cpp
ec4ac0f7 3// Purpose: wxWebView sample
61b98a2d
SL
4// Author: Marianne Gagnon
5// Id: $Id$
ec4ac0f7 6// Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
61b98a2d
SL
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
abcb9c6e 9
ec4ac0f7
SL
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
14// For compilers that support precompilation, includes "wx/wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
21#ifndef WX_PRECOMP
22 #include "wx/wx.h"
23#endif
24
4794f127
SL
25#if !wxUSE_WEBVIEW_WEBKIT && !wxUSE_WEBVIEW_IE
26#error "A wxWebView backend is required by this sample"
27#endif
28
b8a14a17 29#include "wx/artprov.h"
0eb47013 30#include "wx/cmdline.h"
b8a14a17
SC
31#include "wx/notifmsg.h"
32#include "wx/settings.h"
33#include "wx/webview.h"
7d8d6163 34#include "wx/webviewarchivehandler.h"
0bfd90b3 35#include "wx/webviewfshandler.h"
b8a14a17
SC
36#include "wx/infobar.h"
37#include "wx/filesys.h"
38#include "wx/fs_arc.h"
0bfd90b3 39#include "wx/fs_mem.h"
ec4ac0f7 40
e7092398 41#ifndef wxHAS_IMAGES_IN_RESOURCES
ec4ac0f7
SL
42 #include "../sample.xpm"
43#endif
61b98a2d
SL
44
45#if wxUSE_STC
b8a14a17 46#include "wx/stc/stc.h"
61b98a2d
SL
47#else
48#error "wxStyledTextControl is needed by this sample"
49#endif
50
8bccab8f 51#if defined(__WXMSW__) || defined(__WXOSX__)
61b98a2d
SL
52#include "stop.xpm"
53#include "refresh.xpm"
8bccab8f
SL
54#endif
55
dffce0bd 56#include "wxlogo.xpm"
61b98a2d 57
10ad4ba6
SL
58
59//We map menu items to their history items
c13d6ac1 60WX_DECLARE_HASH_MAP(int, wxSharedPtr<wxWebViewHistoryItem>,
10ad4ba6
SL
61 wxIntegerHash, wxIntegerEqual, wxMenuHistoryMap);
62
ec4ac0f7 63class WebApp : public wxApp
61b98a2d
SL
64{
65public:
0eb47013
VZ
66 WebApp() :
67 m_url("http://www.wxwidgets.org")
68 {
69 }
70
ec4ac0f7 71 virtual bool OnInit();
0eb47013
VZ
72
73#if wxUSE_CMDLINE_PARSER
74 virtual void OnInitCmdLine(wxCmdLineParser& parser)
75 {
76 wxApp::OnInitCmdLine(parser);
77
78 parser.AddParam("URL to open",
79 wxCMD_LINE_VAL_STRING,
80 wxCMD_LINE_PARAM_OPTIONAL);
81 }
82
83 virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
84 {
85 if ( !wxApp::OnCmdLineParsed(parser) )
86 return false;
87
88 if ( parser.GetParamCount() )
89 m_url = parser.GetParam(0);
90
91 return true;
92 }
93#endif // wxUSE_CMDLINE_PARSER
94
95private:
96 wxString m_url;
61b98a2d
SL
97};
98
ec4ac0f7 99class WebFrame : public wxFrame
61b98a2d 100{
ec4ac0f7 101public:
0eb47013
VZ
102 WebFrame(const wxString& url);
103 virtual ~WebFrame();
ec4ac0f7 104
ec4ac0f7 105 void UpdateState();
a1788524 106 void OnIdle(wxIdleEvent& evt);
ec4ac0f7
SL
107 void OnUrl(wxCommandEvent& evt);
108 void OnBack(wxCommandEvent& evt);
109 void OnForward(wxCommandEvent& evt);
110 void OnStop(wxCommandEvent& evt);
111 void OnReload(wxCommandEvent& evt);
152a5808
SL
112 void OnClearHistory(wxCommandEvent& evt);
113 void OnEnableHistory(wxCommandEvent& evt);
04fa04d8
SL
114 void OnNavigationRequest(wxWebViewEvent& evt);
115 void OnNavigationComplete(wxWebViewEvent& evt);
116 void OnDocumentLoaded(wxWebViewEvent& evt);
117 void OnNewWindow(wxWebViewEvent& evt);
118 void OnTitleChanged(wxWebViewEvent& evt);
ec4ac0f7
SL
119 void OnViewSourceRequest(wxCommandEvent& evt);
120 void OnToolsClicked(wxCommandEvent& evt);
121 void OnSetZoom(wxCommandEvent& evt);
04fa04d8 122 void OnError(wxWebViewEvent& evt);
ec4ac0f7 123 void OnPrint(wxCommandEvent& evt);
4681a3ea
SL
124 void OnCut(wxCommandEvent& evt);
125 void OnCopy(wxCommandEvent& evt);
126 void OnPaste(wxCommandEvent& evt);
97e49559
SL
127 void OnUndo(wxCommandEvent& evt);
128 void OnRedo(wxCommandEvent& evt);
c7cbe308 129 void OnMode(wxCommandEvent& evt);
87d482ec 130 void OnZoomLayout(wxCommandEvent& evt);
10ad4ba6 131 void OnHistory(wxCommandEvent& evt);
1f7c17f4
VZ
132 void OnScrollLineUp(wxCommandEvent&) { m_browser->LineUp(); }
133 void OnScrollLineDown(wxCommandEvent&) { m_browser->LineDown(); }
134 void OnScrollPageUp(wxCommandEvent&) { m_browser->PageUp(); }
135 void OnScrollPageDown(wxCommandEvent&) { m_browser->PageDown(); }
54883129 136 void OnRunScript(wxCommandEvent& evt);
603cfe42
SL
137 void OnClearSelection(wxCommandEvent& evt);
138 void OnDeleteSelection(wxCommandEvent& evt);
139 void OnSelectAll(wxCommandEvent& evt);
49f07bec 140 void OnLoadScheme(wxCommandEvent& evt);
0bfd90b3 141 void OnUseMemoryFS(wxCommandEvent& evt);
66ac0400
SL
142 void OnFind(wxCommandEvent& evt);
143 void OnFindDone(wxCommandEvent& evt);
144 void OnFindText(wxCommandEvent& evt);
145 void OnFindOptions(wxCommandEvent& evt);
c420d57b 146 void OnEnableContextMenu(wxCommandEvent& evt);
ec4ac0f7
SL
147
148private:
149 wxTextCtrl* m_url;
150 wxWebView* m_browser;
151
61b98a2d 152 wxToolBar* m_toolbar;
ec4ac0f7
SL
153 wxToolBarToolBase* m_toolbar_back;
154 wxToolBarToolBase* m_toolbar_forward;
155 wxToolBarToolBase* m_toolbar_stop;
156 wxToolBarToolBase* m_toolbar_reload;
157 wxToolBarToolBase* m_toolbar_tools;
158
66ac0400
SL
159 wxToolBarToolBase* m_find_toolbar_done;
160 wxToolBarToolBase* m_find_toolbar_next;
161 wxToolBarToolBase* m_find_toolbar_previous;
162 wxToolBarToolBase* m_find_toolbar_options;
163 wxMenuItem* m_find_toolbar_wrap;
164 wxMenuItem* m_find_toolbar_highlight;
165 wxMenuItem* m_find_toolbar_matchcase;
166 wxMenuItem* m_find_toolbar_wholeword;
167
ec4ac0f7 168 wxMenu* m_tools_menu;
10ad4ba6 169 wxMenu* m_tools_history_menu;
87d482ec 170 wxMenuItem* m_tools_layout;
ec4ac0f7
SL
171 wxMenuItem* m_tools_tiny;
172 wxMenuItem* m_tools_small;
173 wxMenuItem* m_tools_medium;
174 wxMenuItem* m_tools_large;
175 wxMenuItem* m_tools_largest;
62e8551f
SL
176 wxMenuItem* m_tools_handle_navigation;
177 wxMenuItem* m_tools_handle_new_window;
152a5808 178 wxMenuItem* m_tools_enable_history;
4681a3ea
SL
179 wxMenuItem* m_edit_cut;
180 wxMenuItem* m_edit_copy;
181 wxMenuItem* m_edit_paste;
97e49559
SL
182 wxMenuItem* m_edit_undo;
183 wxMenuItem* m_edit_redo;
c7cbe308 184 wxMenuItem* m_edit_mode;
1f7c17f4
VZ
185 wxMenuItem* m_scroll_line_up;
186 wxMenuItem* m_scroll_line_down;
187 wxMenuItem* m_scroll_page_up;
188 wxMenuItem* m_scroll_page_down;
603cfe42
SL
189 wxMenuItem* m_selection_clear;
190 wxMenuItem* m_selection_delete;
66ac0400 191 wxMenuItem* m_find;
c420d57b 192 wxMenuItem* m_context_menu;
ec4ac0f7 193
ec4ac0f7
SL
194 wxInfoBar *m_info;
195 wxStaticText* m_info_text;
66ac0400
SL
196 wxTextCtrl* m_find_ctrl;
197 wxToolBar* m_find_toolbar;
10ad4ba6
SL
198
199 wxMenuHistoryMap m_histMenuItems;
66ac0400
SL
200 wxString m_findText;
201 int m_findFlags, m_findCount;
ec4ac0f7
SL
202};
203
204class SourceViewDialog : public wxDialog
205{
61b98a2d 206public:
ec4ac0f7
SL
207 SourceViewDialog(wxWindow* parent, wxString source);
208};
209
210IMPLEMENT_APP(WebApp)
211
212// ============================================================================
213// implementation
214// ============================================================================
215
216bool WebApp::OnInit()
217{
218 if ( !wxApp::OnInit() )
219 return false;
220
0bfd90b3
SL
221 //Required for virtual file system archive and memory support
222 wxFileSystem::AddHandler(new wxArchiveFSHandler);
223 wxFileSystem::AddHandler(new wxMemoryFSHandler);
224
225 // Create the memory files
226 wxImage::AddHandler(new wxPNGHandler);
227 wxMemoryFSHandler::AddFile("logo.png",
228 wxBitmap(wxlogo_xpm), wxBITMAP_TYPE_PNG);
229 wxMemoryFSHandler::AddFile("page1.htm",
230 "<html><head><title>File System Example</title>"
231 "<link rel='stylesheet' type='text/css' href='memory:test.css'>"
232 "</head><body><h1>Page 1</h1>"
233 "<p><img src='memory:logo.png'></p>"
234 "<p>Some text about <a href='memory:page2.htm'>Page 2</a>.</p></body>");
235 wxMemoryFSHandler::AddFile("page2.htm",
236 "<html><head><title>File System Example</title>"
237 "<link rel='stylesheet' type='text/css' href='memory:test.css'>"
238 "</head><body><h1>Page 2</h1>"
239 "<p><a href='memory:page1.htm'>Page 1</a> was better.</p></body>");
240 wxMemoryFSHandler::AddFile("test.css", "h1 {color: red;}");
241
0eb47013 242 WebFrame *frame = new WebFrame(m_url);
ec4ac0f7
SL
243 frame->Show();
244
245 return true;
246}
247
0eb47013
VZ
248WebFrame::WebFrame(const wxString& url) :
249 wxFrame(NULL, wxID_ANY, "wxWebView Sample")
ec4ac0f7
SL
250{
251 // set the frame icon
252 SetIcon(wxICON(sample));
253 SetTitle("wxWebView Sample");
254
ec4ac0f7
SL
255 wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
256
257 // Create the toolbar
258 m_toolbar = CreateToolBar(wxTB_TEXT);
259 m_toolbar->SetToolBitmapSize(wxSize(32, 32));
abcb9c6e 260
dffce0bd
SL
261 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR);
262 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR);
263 #ifdef __WXGTK__
264 wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR);
abcb9c6e 265 #else
dffce0bd
SL
266 wxBitmap stop = wxBitmap(stop_xpm);
267 #endif
268 #ifdef __WXGTK__
269 wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR);
abcb9c6e 270 #else
dffce0bd
SL
271 wxBitmap refresh = wxBitmap(refresh_xpm);
272 #endif
273
274 m_toolbar_back = m_toolbar->AddTool(wxID_ANY, _("Back"), back);
275 m_toolbar_forward = m_toolbar->AddTool(wxID_ANY, _("Forward"), forward);
276 m_toolbar_stop = m_toolbar->AddTool(wxID_ANY, _("Stop"), stop);
277 m_toolbar_reload = m_toolbar->AddTool(wxID_ANY, _("Reload"), refresh);
ec4ac0f7 278 m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER );
9a00d078 279 m_toolbar->AddControl(m_url, _("URL"));
ec4ac0f7
SL
280 m_toolbar_tools = m_toolbar->AddTool(wxID_ANY, _("Menu"), wxBitmap(wxlogo_xpm));
281
282 m_toolbar->Realize();
283
66ac0400 284 // Set find values.
236cff73 285 m_findFlags = wxWEBVIEW_FIND_DEFAULT;
66ac0400
SL
286 m_findText = wxEmptyString;
287 m_findCount = 0;
288
289 // Create panel for find toolbar.
290 wxPanel* panel = new wxPanel(this);
291 topsizer->Add(panel, wxSizerFlags().Expand());
292
293 // Create sizer for panel.
294 wxBoxSizer* panel_sizer = new wxBoxSizer(wxVERTICAL);
295 panel->SetSizer(panel_sizer);
296
297 // Create the find toolbar.
298 m_find_toolbar = new wxToolBar(panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL|wxTB_TEXT|wxTB_HORZ_LAYOUT);
299 m_find_toolbar->Hide();
300 panel_sizer->Add(m_find_toolbar, wxSizerFlags().Expand());
301
302 // Create find control.
303 m_find_ctrl = new wxTextCtrl(m_find_toolbar, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(140,-1), wxTE_PROCESS_ENTER);
304
305
306 //Find options menu
307 wxMenu* findmenu = new wxMenu;
308 m_find_toolbar_wrap = findmenu->AppendCheckItem(wxID_ANY,"Wrap");
309 m_find_toolbar_matchcase = findmenu->AppendCheckItem(wxID_ANY,"Match Case");
310 m_find_toolbar_wholeword = findmenu->AppendCheckItem(wxID_ANY,"Entire Word");
311 m_find_toolbar_highlight = findmenu->AppendCheckItem(wxID_ANY,"Highlight");
312 // Add find toolbar tools.
313 m_find_toolbar->SetToolSeparation(7);
314 m_find_toolbar_done = m_find_toolbar->AddTool(wxID_ANY, "Close", wxArtProvider::GetBitmap(wxART_CROSS_MARK));
315 m_find_toolbar->AddSeparator();
316 m_find_toolbar->AddControl(m_find_ctrl, "Find");
317 m_find_toolbar->AddSeparator();
318 m_find_toolbar_next = m_find_toolbar->AddTool(wxID_ANY, "Next", wxArtProvider::GetBitmap(wxART_GO_DOWN, wxART_TOOLBAR, wxSize(16,16)));
319 m_find_toolbar_previous = m_find_toolbar->AddTool(wxID_ANY, "Previous", wxArtProvider::GetBitmap(wxART_GO_UP, wxART_TOOLBAR, wxSize(16,16)));
320 m_find_toolbar->AddSeparator();
321 m_find_toolbar_options = m_find_toolbar->AddTool(wxID_ANY, "Options", wxArtProvider::GetBitmap(wxART_PLUS, wxART_TOOLBAR, wxSize(16,16)), "", wxITEM_DROPDOWN);
322 m_find_toolbar_options->SetDropdownMenu(findmenu);
323 m_find_toolbar->Realize();
324
ec4ac0f7
SL
325 // Create the info panel
326 m_info = new wxInfoBar(this);
327 topsizer->Add(m_info, wxSizerFlags().Expand());
328
329 // Create the webview
0eb47013 330 m_browser = wxWebView::New(this, wxID_ANY, url);
ec4ac0f7
SL
331 topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
332
7d8d6163
SL
333 //We register the wxfs:// protocol for testing purposes
334 m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
0bfd90b3
SL
335 //And the memory: file system
336 m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewFSHandler("memory")));
eff8f795 337
ec4ac0f7
SL
338 SetSizer(topsizer);
339
9a00d078
SL
340 //Set a more sensible size for web browsing
341 SetSize(wxSize(800, 600));
342
ec4ac0f7
SL
343 // Create a log window
344 new wxLogWindow(this, _("Logging"));
345
346 // Create the Tools menu
347 m_tools_menu = new wxMenu();
348 wxMenuItem* print = m_tools_menu->Append(wxID_ANY , _("Print"));
349 wxMenuItem* viewSource = m_tools_menu->Append(wxID_ANY , _("View Source"));
350 m_tools_menu->AppendSeparator();
87d482ec 351 m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom"));
ec4ac0f7
SL
352 m_tools_tiny = m_tools_menu->AppendCheckItem(wxID_ANY, _("Tiny"));
353 m_tools_small = m_tools_menu->AppendCheckItem(wxID_ANY, _("Small"));
354 m_tools_medium = m_tools_menu->AppendCheckItem(wxID_ANY, _("Medium"));
355 m_tools_large = m_tools_menu->AppendCheckItem(wxID_ANY, _("Large"));
356 m_tools_largest = m_tools_menu->AppendCheckItem(wxID_ANY, _("Largest"));
62e8551f
SL
357 m_tools_menu->AppendSeparator();
358 m_tools_handle_navigation = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle Navigation"));
359 m_tools_handle_new_window = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle New Windows"));
152a5808 360 m_tools_menu->AppendSeparator();
10ad4ba6 361
66ac0400
SL
362 //Find
363 m_find = m_tools_menu->Append(wxID_ANY, _("Find"));
364 m_tools_menu->AppendSeparator();
365
10ad4ba6
SL
366 //History menu
367 m_tools_history_menu = new wxMenu();
368 wxMenuItem* clearhist = m_tools_history_menu->Append(wxID_ANY, _("Clear History"));
369 m_tools_enable_history = m_tools_history_menu->AppendCheckItem(wxID_ANY, _("Enable History"));
370 m_tools_history_menu->AppendSeparator();
371
54883129 372 m_tools_menu->AppendSubMenu(m_tools_history_menu, "History");
62e8551f 373
4681a3ea
SL
374 //Create an editing menu
375 wxMenu* editmenu = new wxMenu();
376 m_edit_cut = editmenu->Append(wxID_ANY, _("Cut"));
377 m_edit_copy = editmenu->Append(wxID_ANY, _("Copy"));
378 m_edit_paste = editmenu->Append(wxID_ANY, _("Paste"));
97e49559
SL
379 editmenu->AppendSeparator();
380 m_edit_undo = editmenu->Append(wxID_ANY, _("Undo"));
381 m_edit_redo = editmenu->Append(wxID_ANY, _("Redo"));
c7cbe308
SL
382 editmenu->AppendSeparator();
383 m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode"));
4681a3ea
SL
384
385 m_tools_menu->AppendSeparator();
386 m_tools_menu->AppendSubMenu(editmenu, "Edit");
387
1f7c17f4
VZ
388 wxMenu* scroll_menu = new wxMenu;
389 m_scroll_line_up = scroll_menu->Append(wxID_ANY, "Line &up");
390 m_scroll_line_down = scroll_menu->Append(wxID_ANY, "Line &down");
391 m_scroll_page_up = scroll_menu->Append(wxID_ANY, "Page u&p");
392 m_scroll_page_down = scroll_menu->Append(wxID_ANY, "Page d&own");
393 m_tools_menu->AppendSubMenu(scroll_menu, "Scroll");
394
54883129
SL
395 wxMenuItem* script = m_tools_menu->Append(wxID_ANY, _("Run Script"));
396
603cfe42
SL
397 //Selection menu
398 wxMenu* selection = new wxMenu();
399 m_selection_clear = selection->Append(wxID_ANY, _("Clear Selection"));
400 m_selection_delete = selection->Append(wxID_ANY, _("Delete Selection"));
401 wxMenuItem* selectall = selection->Append(wxID_ANY, _("Select All"));
402
403 editmenu->AppendSubMenu(selection, "Selection");
404
49f07bec 405 wxMenuItem* loadscheme = m_tools_menu->Append(wxID_ANY, _("Custom Scheme Example"));
0bfd90b3 406 wxMenuItem* usememoryfs = m_tools_menu->Append(wxID_ANY, _("Memory File System Example"));
49f07bec 407
c420d57b
SL
408 m_context_menu = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable Context Menu"));
409
61635eed 410 //By default we want to handle navigation and new windows
62e8551f
SL
411 m_tools_handle_navigation->Check();
412 m_tools_handle_new_window->Check();
152a5808 413 m_tools_enable_history->Check();
236cff73 414 if(!m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT))
87d482ec 415 m_tools_layout->Enable(false);
ec4ac0f7
SL
416
417
418 // Connect the toolbar events
ce7fe42e 419 Connect(m_toolbar_back->GetId(), wxEVT_TOOL,
ec4ac0f7 420 wxCommandEventHandler(WebFrame::OnBack), NULL, this );
ce7fe42e 421 Connect(m_toolbar_forward->GetId(), wxEVT_TOOL,
ec4ac0f7 422 wxCommandEventHandler(WebFrame::OnForward), NULL, this );
ce7fe42e 423 Connect(m_toolbar_stop->GetId(), wxEVT_TOOL,
ec4ac0f7 424 wxCommandEventHandler(WebFrame::OnStop), NULL, this );
ce7fe42e 425 Connect(m_toolbar_reload->GetId(), wxEVT_TOOL,
ec4ac0f7 426 wxCommandEventHandler(WebFrame::OnReload),NULL, this );
ce7fe42e 427 Connect(m_toolbar_tools->GetId(), wxEVT_TOOL,
ec4ac0f7
SL
428 wxCommandEventHandler(WebFrame::OnToolsClicked), NULL, this );
429
ce7fe42e 430 Connect(m_url->GetId(), wxEVT_TEXT_ENTER,
ec4ac0f7
SL
431 wxCommandEventHandler(WebFrame::OnUrl), NULL, this );
432
66ac0400 433 // Connect find toolbar events.
ce7fe42e 434 Connect(m_find_toolbar_done->GetId(), wxEVT_TOOL,
66ac0400 435 wxCommandEventHandler(WebFrame::OnFindDone), NULL, this );
ce7fe42e 436 Connect(m_find_toolbar_next->GetId(), wxEVT_TOOL,
66ac0400 437 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
ce7fe42e 438 Connect(m_find_toolbar_previous->GetId(), wxEVT_TOOL,
66ac0400
SL
439 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
440
441 // Connect find control events.
ce7fe42e 442 Connect(m_find_ctrl->GetId(), wxEVT_TEXT,
66ac0400 443 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
ce7fe42e 444 Connect(m_find_ctrl->GetId(), wxEVT_TEXT_ENTER,
66ac0400
SL
445 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
446
ec4ac0f7 447 // Connect the webview events
ce7fe42e 448 Connect(m_browser->GetId(), wxEVT_WEBVIEW_NAVIGATING,
04fa04d8 449 wxWebViewEventHandler(WebFrame::OnNavigationRequest), NULL, this);
ce7fe42e 450 Connect(m_browser->GetId(), wxEVT_WEBVIEW_NAVIGATED,
04fa04d8 451 wxWebViewEventHandler(WebFrame::OnNavigationComplete), NULL, this);
ce7fe42e 452 Connect(m_browser->GetId(), wxEVT_WEBVIEW_LOADED,
abcb9c6e 453 wxWebViewEventHandler(WebFrame::OnDocumentLoaded), NULL, this);
ce7fe42e 454 Connect(m_browser->GetId(), wxEVT_WEBVIEW_ERROR,
04fa04d8 455 wxWebViewEventHandler(WebFrame::OnError), NULL, this);
ce7fe42e 456 Connect(m_browser->GetId(), wxEVT_WEBVIEW_NEWWINDOW,
04fa04d8 457 wxWebViewEventHandler(WebFrame::OnNewWindow), NULL, this);
ce7fe42e 458 Connect(m_browser->GetId(), wxEVT_WEBVIEW_TITLE_CHANGED,
04fa04d8 459 wxWebViewEventHandler(WebFrame::OnTitleChanged), NULL, this);
ec4ac0f7
SL
460
461 // Connect the menu events
ce7fe42e 462 Connect(viewSource->GetId(), wxEVT_MENU,
ec4ac0f7 463 wxCommandEventHandler(WebFrame::OnViewSourceRequest), NULL, this );
ce7fe42e 464 Connect(print->GetId(), wxEVT_MENU,
ec4ac0f7 465 wxCommandEventHandler(WebFrame::OnPrint), NULL, this );
ce7fe42e 466 Connect(m_tools_layout->GetId(), wxEVT_MENU,
87d482ec 467 wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this );
ce7fe42e 468 Connect(m_tools_tiny->GetId(), wxEVT_MENU,
ec4ac0f7 469 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
ce7fe42e 470 Connect(m_tools_small->GetId(), wxEVT_MENU,
ec4ac0f7 471 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
ce7fe42e 472 Connect(m_tools_medium->GetId(), wxEVT_MENU,
ec4ac0f7 473 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
ce7fe42e 474 Connect(m_tools_large->GetId(), wxEVT_MENU,
ec4ac0f7 475 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
ce7fe42e 476 Connect(m_tools_largest->GetId(), wxEVT_MENU,
ec4ac0f7 477 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
ce7fe42e 478 Connect(clearhist->GetId(), wxEVT_MENU,
152a5808 479 wxCommandEventHandler(WebFrame::OnClearHistory), NULL, this );
ce7fe42e 480 Connect(m_tools_enable_history->GetId(), wxEVT_MENU,
152a5808 481 wxCommandEventHandler(WebFrame::OnEnableHistory), NULL, this );
ce7fe42e 482 Connect(m_edit_cut->GetId(), wxEVT_MENU,
4681a3ea 483 wxCommandEventHandler(WebFrame::OnCut), NULL, this );
ce7fe42e 484 Connect(m_edit_copy->GetId(), wxEVT_MENU,
4681a3ea 485 wxCommandEventHandler(WebFrame::OnCopy), NULL, this );
ce7fe42e 486 Connect(m_edit_paste->GetId(), wxEVT_MENU,
4681a3ea 487 wxCommandEventHandler(WebFrame::OnPaste), NULL, this );
ce7fe42e 488 Connect(m_edit_undo->GetId(), wxEVT_MENU,
97e49559 489 wxCommandEventHandler(WebFrame::OnUndo), NULL, this );
ce7fe42e 490 Connect(m_edit_redo->GetId(), wxEVT_MENU,
97e49559 491 wxCommandEventHandler(WebFrame::OnRedo), NULL, this );
ce7fe42e 492 Connect(m_edit_mode->GetId(), wxEVT_MENU,
c7cbe308 493 wxCommandEventHandler(WebFrame::OnMode), NULL, this );
ce7fe42e 494 Connect(m_scroll_line_up->GetId(), wxEVT_MENU,
1f7c17f4 495 wxCommandEventHandler(WebFrame::OnScrollLineUp), NULL, this );
ce7fe42e 496 Connect(m_scroll_line_down->GetId(), wxEVT_MENU,
1f7c17f4 497 wxCommandEventHandler(WebFrame::OnScrollLineDown), NULL, this );
ce7fe42e 498 Connect(m_scroll_page_up->GetId(), wxEVT_MENU,
1f7c17f4 499 wxCommandEventHandler(WebFrame::OnScrollPageUp), NULL, this );
ce7fe42e 500 Connect(m_scroll_page_down->GetId(), wxEVT_MENU,
1f7c17f4 501 wxCommandEventHandler(WebFrame::OnScrollPageDown), NULL, this );
ce7fe42e 502 Connect(script->GetId(), wxEVT_MENU,
54883129 503 wxCommandEventHandler(WebFrame::OnRunScript), NULL, this );
ce7fe42e 504 Connect(m_selection_clear->GetId(), wxEVT_MENU,
603cfe42 505 wxCommandEventHandler(WebFrame::OnClearSelection), NULL, this );
ce7fe42e 506 Connect(m_selection_delete->GetId(), wxEVT_MENU,
603cfe42 507 wxCommandEventHandler(WebFrame::OnDeleteSelection), NULL, this );
ce7fe42e 508 Connect(selectall->GetId(), wxEVT_MENU,
603cfe42 509 wxCommandEventHandler(WebFrame::OnSelectAll), NULL, this );
ce7fe42e 510 Connect(loadscheme->GetId(), wxEVT_MENU,
49f07bec 511 wxCommandEventHandler(WebFrame::OnLoadScheme), NULL, this );
ce7fe42e 512 Connect(usememoryfs->GetId(), wxEVT_MENU,
0bfd90b3 513 wxCommandEventHandler(WebFrame::OnUseMemoryFS), NULL, this );
ce7fe42e 514 Connect(m_find->GetId(), wxEVT_MENU,
66ac0400 515 wxCommandEventHandler(WebFrame::OnFind), NULL, this );
ce7fe42e 516 Connect(m_context_menu->GetId(), wxEVT_MENU,
c420d57b 517 wxCommandEventHandler(WebFrame::OnEnableContextMenu), NULL, this );
a1788524
SL
518
519 //Connect the idle events
520 Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(WebFrame::OnIdle), NULL, this);
ec4ac0f7
SL
521}
522
7892e035
SL
523WebFrame::~WebFrame()
524{
525 delete m_tools_menu;
526}
527
ec4ac0f7
SL
528/**
529 * Method that retrieves the current state from the web control and updates the GUI
530 * the reflect this current state.
531 */
532void WebFrame::UpdateState()
533{
534 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
535 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
abcb9c6e 536
ec4ac0f7 537 if (m_browser->IsBusy())
61b98a2d 538 {
abcb9c6e 539 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
61b98a2d 540 }
ec4ac0f7 541 else
61b98a2d 542 {
abcb9c6e 543 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
61b98a2d 544 }
abcb9c6e 545
ec4ac0f7
SL
546 SetTitle( m_browser->GetCurrentTitle() );
547 m_url->SetValue( m_browser->GetCurrentURL() );
548}
549
c49d2434 550void WebFrame::OnIdle(wxIdleEvent& WXUNUSED(evt))
a1788524
SL
551{
552 if(m_browser->IsBusy())
553 {
554 wxSetCursor(wxCURSOR_ARROWWAIT);
555 m_toolbar->EnableTool(m_toolbar_stop->GetId(), true);
556 }
557 else
558 {
559 wxSetCursor(wxNullCursor);
560 m_toolbar->EnableTool(m_toolbar_stop->GetId(), false);
561 }
562}
563
ec4ac0f7
SL
564/**
565 * Callback invoked when user entered an URL and pressed enter
566 */
8bccab8f 567void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7 568{
4d0dddc7 569 m_browser->LoadURL( m_url->GetValue() );
c49d2434 570 m_browser->SetFocus();
ec4ac0f7
SL
571 UpdateState();
572}
573
574/**
575 * Callback invoked when user pressed the "back" button
576 */
8bccab8f 577void WebFrame::OnBack(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
578{
579 m_browser->GoBack();
580 UpdateState();
581}
582
583/**
584 * Callback invoked when user pressed the "forward" button
585 */
8bccab8f 586void WebFrame::OnForward(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
587{
588 m_browser->GoForward();
589 UpdateState();
590}
591
592/**
593 * Callback invoked when user pressed the "stop" button
594 */
8bccab8f 595void WebFrame::OnStop(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
596{
597 m_browser->Stop();
598 UpdateState();
599}
600
601/**
602 * Callback invoked when user pressed the "reload" button
603 */
8bccab8f 604void WebFrame::OnReload(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
605{
606 m_browser->Reload();
607 UpdateState();
608}
609
8bccab8f 610void WebFrame::OnClearHistory(wxCommandEvent& WXUNUSED(evt))
152a5808
SL
611{
612 m_browser->ClearHistory();
613 UpdateState();
614}
615
8bccab8f 616void WebFrame::OnEnableHistory(wxCommandEvent& WXUNUSED(evt))
152a5808
SL
617{
618 m_browser->EnableHistory(m_tools_enable_history->IsChecked());
619 UpdateState();
620}
621
8bccab8f 622void WebFrame::OnCut(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
623{
624 m_browser->Cut();
625}
626
8bccab8f 627void WebFrame::OnCopy(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
628{
629 m_browser->Copy();
630}
631
8bccab8f 632void WebFrame::OnPaste(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
633{
634 m_browser->Paste();
635}
636
8bccab8f 637void WebFrame::OnUndo(wxCommandEvent& WXUNUSED(evt))
97e49559
SL
638{
639 m_browser->Undo();
640}
641
8bccab8f 642void WebFrame::OnRedo(wxCommandEvent& WXUNUSED(evt))
97e49559
SL
643{
644 m_browser->Redo();
645}
646
8bccab8f 647void WebFrame::OnMode(wxCommandEvent& WXUNUSED(evt))
c7cbe308
SL
648{
649 m_browser->SetEditable(m_edit_mode->IsChecked());
650}
651
49f07bec
SL
652void WebFrame::OnLoadScheme(wxCommandEvent& WXUNUSED(evt))
653{
654 wxFileName helpfile("../help/doc.zip");
655 helpfile.MakeAbsolute();
656 wxString path = helpfile.GetFullPath();
657 //Under MSW we need to flip the slashes
658 path.Replace("\\", "/");
659 path = "wxfs:///" + path + ";protocol=zip/doc.htm";
4d0dddc7 660 m_browser->LoadURL(path);
66ac0400
SL
661}
662
0bfd90b3
SL
663void WebFrame::OnUseMemoryFS(wxCommandEvent& WXUNUSED(evt))
664{
665 m_browser->LoadURL("memory:page1.htm");
666}
667
c420d57b
SL
668void WebFrame::OnEnableContextMenu(wxCommandEvent& evt)
669{
670 m_browser->EnableContextMenu(evt.IsChecked());
671}
672
66ac0400
SL
673void WebFrame::OnFind(wxCommandEvent& WXUNUSED(evt))
674{
675 wxString value = m_browser->GetSelectedText();
676 if(value.Len() > 150)
677 {
678 value.Truncate(150);
679 }
680 m_find_ctrl->SetValue(value);
681 if(!m_find_toolbar->IsShown()){
682 m_find_toolbar->Show(true);
683 SendSizeEvent();
684 }
685 m_find_ctrl->SelectAll();
686}
687
688void WebFrame::OnFindDone(wxCommandEvent& WXUNUSED(evt))
689{
690 m_browser->Find("");
691 m_find_toolbar->Show(false);
692 SendSizeEvent();
693}
694
695void WebFrame::OnFindText(wxCommandEvent& evt)
696{
697 int flags = 0;
698
699 if(m_find_toolbar_wrap->IsChecked())
236cff73 700 flags |= wxWEBVIEW_FIND_WRAP;
66ac0400 701 if(m_find_toolbar_wholeword->IsChecked())
236cff73 702 flags |= wxWEBVIEW_FIND_ENTIRE_WORD;
66ac0400 703 if(m_find_toolbar_matchcase->IsChecked())
236cff73 704 flags |= wxWEBVIEW_FIND_MATCH_CASE;
66ac0400 705 if(m_find_toolbar_highlight->IsChecked())
236cff73 706 flags |= wxWEBVIEW_FIND_HIGHLIGHT_RESULT;
66ac0400
SL
707
708 if(m_find_toolbar_previous->GetId() == evt.GetId())
236cff73 709 flags |= wxWEBVIEW_FIND_BACKWARDS;
66ac0400
SL
710
711 wxString find_text = m_find_ctrl->GetValue();
712 long count = m_browser->Find(find_text, flags);
713
714 if(m_findText != find_text)
715 {
716 m_findCount = count;
717 m_findText = find_text;
718 }
719
720 if(count != wxNOT_FOUND || find_text.IsEmpty())
721 {
722 m_find_ctrl->SetBackgroundColour(*wxWHITE);
723 }
724 else
725 {
726 m_find_ctrl->SetBackgroundColour(wxColour(255, 101, 101));
727 }
728
729 m_find_ctrl->Refresh();
730
731 //Log the result, note that count is zero indexed.
732 if(count != m_findCount)
733 {
734 count++;
735 }
736 wxLogMessage("Searching for:%s current match:%i/%i", m_findText.c_str(), count, m_findCount);
49f07bec 737}
c7cbe308 738
ec4ac0f7
SL
739/**
740 * Callback invoked when there is a request to load a new page (for instance
741 * when the user clicks a link)
742 */
04fa04d8 743void WebFrame::OnNavigationRequest(wxWebViewEvent& evt)
ec4ac0f7 744{
75b0b0bc
SL
745 if(m_info->IsShown())
746 {
70544c1e 747 m_info->Dismiss();
75b0b0bc
SL
748 }
749
e40741b9 750 wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" +
ec4ac0f7 751 evt.GetTarget() + "')");
abcb9c6e 752
ec4ac0f7 753 wxASSERT(m_browser->IsBusy());
62e8551f
SL
754
755 //If we don't want to handle navigation then veto the event and navigation
0e830c7c 756 //will not take place, we also need to stop the loading animation
62e8551f 757 if(!m_tools_handle_navigation->IsChecked())
0e830c7c 758 {
62e8551f 759 evt.Veto();
abcb9c6e 760 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
0e830c7c
SL
761 }
762 else
763 {
764 UpdateState();
765 }
ec4ac0f7
SL
766}
767
768/**
769 * Callback invoked when a navigation request was accepted
770 */
04fa04d8 771void WebFrame::OnNavigationComplete(wxWebViewEvent& evt)
ec4ac0f7 772{
e40741b9 773 wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'");
ec4ac0f7
SL
774 UpdateState();
775}
776
777/**
778 * Callback invoked when a page is finished loading
779 */
04fa04d8 780void WebFrame::OnDocumentLoaded(wxWebViewEvent& evt)
ec4ac0f7 781{
113e0a92 782 //Only notify if the document is the main frame, not a subframe
e40741b9 783 if(evt.GetURL() == m_browser->GetCurrentURL())
8bccab8f 784 {
e40741b9 785 wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'");
8bccab8f 786 }
ec4ac0f7
SL
787 UpdateState();
788}
789
790/**
791 * On new window, we veto to stop extra windows appearing
792 */
04fa04d8 793void WebFrame::OnNewWindow(wxWebViewEvent& evt)
ec4ac0f7 794{
e40741b9 795 wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'");
62e8551f 796
abcb9c6e 797 //If we handle new window events then just load them in this window as we
62e8551f
SL
798 //are a single window browser
799 if(m_tools_handle_new_window->IsChecked())
4d0dddc7 800 m_browser->LoadURL(evt.GetURL());
62e8551f 801
ec4ac0f7
SL
802 UpdateState();
803}
804
04fa04d8 805void WebFrame::OnTitleChanged(wxWebViewEvent& evt)
153530af 806{
7cd03861 807 SetTitle(evt.GetString());
153530af 808 wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'");
153530af
SL
809}
810
ec4ac0f7
SL
811/**
812 * Invoked when user selects the "View Source" menu item
813 */
8bccab8f 814void WebFrame::OnViewSourceRequest(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
815{
816 SourceViewDialog dlg(this, m_browser->GetPageSource());
817 dlg.ShowModal();
818}
819
820/**
821 * Invoked when user selects the "Menu" item
822 */
8bccab8f 823void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
824{
825 if(m_browser->GetCurrentURL() == "")
826 return;
827
828 m_tools_tiny->Check(false);
abcb9c6e 829 m_tools_small->Check(false);
ec4ac0f7
SL
830 m_tools_medium->Check(false);
831 m_tools_large->Check(false);
832 m_tools_largest->Check(false);
833
834 wxWebViewZoom zoom = m_browser->GetZoom();
835 switch (zoom)
61b98a2d 836 {
236cff73 837 case wxWEBVIEW_ZOOM_TINY:
ec4ac0f7
SL
838 m_tools_tiny->Check();
839 break;
236cff73 840 case wxWEBVIEW_ZOOM_SMALL:
ec4ac0f7
SL
841 m_tools_small->Check();
842 break;
236cff73 843 case wxWEBVIEW_ZOOM_MEDIUM:
ec4ac0f7
SL
844 m_tools_medium->Check();
845 break;
236cff73 846 case wxWEBVIEW_ZOOM_LARGE:
ec4ac0f7
SL
847 m_tools_large->Check();
848 break;
236cff73 849 case wxWEBVIEW_ZOOM_LARGEST:
ec4ac0f7
SL
850 m_tools_largest->Check();
851 break;
61b98a2d 852 }
4681a3ea
SL
853
854 m_edit_cut->Enable(m_browser->CanCut());
855 m_edit_copy->Enable(m_browser->CanCopy());
856 m_edit_paste->Enable(m_browser->CanPaste());
97e49559
SL
857
858 m_edit_undo->Enable(m_browser->CanUndo());
859 m_edit_redo->Enable(m_browser->CanRedo());
10ad4ba6 860
603cfe42
SL
861 m_selection_clear->Enable(m_browser->HasSelection());
862 m_selection_delete->Enable(m_browser->HasSelection());
863
c420d57b
SL
864 m_context_menu->Check(m_browser->IsContextMenuEnabled());
865
10ad4ba6
SL
866 //Firstly we clear the existing menu items, then we add the current ones
867 wxMenuHistoryMap::const_iterator it;
868 for( it = m_histMenuItems.begin(); it != m_histMenuItems.end(); ++it )
869 {
870 m_tools_history_menu->Destroy(it->first);
871 }
872 m_histMenuItems.clear();
873
c13d6ac1
SL
874 wxVector<wxSharedPtr<wxWebViewHistoryItem> > back = m_browser->GetBackwardHistory();
875 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forward = m_browser->GetForwardHistory();
10ad4ba6
SL
876
877 wxMenuItem* item;
878
abcb9c6e
JS
879 unsigned int i;
880 for(i = 0; i < back.size(); i++)
10ad4ba6
SL
881 {
882 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, back[i]->GetTitle());
883 m_histMenuItems[item->GetId()] = back[i];
ce7fe42e 884 Connect(item->GetId(), wxEVT_MENU,
10ad4ba6
SL
885 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
886 }
887
14a0b47d
VZ
888 wxString title = m_browser->GetCurrentTitle();
889 if ( title.empty() )
890 title = "(untitled)";
891 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, title);
10ad4ba6
SL
892 item->Check();
893
894 //No need to connect the current item
c13d6ac1 895 m_histMenuItems[item->GetId()] = wxSharedPtr<wxWebViewHistoryItem>(new wxWebViewHistoryItem(m_browser->GetCurrentURL(), m_browser->GetCurrentTitle()));
10ad4ba6 896
abcb9c6e 897 for(i = 0; i < forward.size(); i++)
10ad4ba6
SL
898 {
899 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, forward[i]->GetTitle());
900 m_histMenuItems[item->GetId()] = forward[i];
ce7fe42e 901 Connect(item->GetId(), wxEVT_TOOL,
10ad4ba6
SL
902 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
903 }
abcb9c6e 904
ec4ac0f7
SL
905 wxPoint position = ScreenToClient( wxGetMousePosition() );
906 PopupMenu(m_tools_menu, position.x, position.y);
907}
908
909/**
910 * Invoked when user selects the zoom size in the menu
911 */
912void WebFrame::OnSetZoom(wxCommandEvent& evt)
913{
914 if (evt.GetId() == m_tools_tiny->GetId())
61b98a2d 915 {
236cff73 916 m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY);
61b98a2d 917 }
ec4ac0f7 918 else if (evt.GetId() == m_tools_small->GetId())
61b98a2d 919 {
236cff73 920 m_browser->SetZoom(wxWEBVIEW_ZOOM_SMALL);
61b98a2d 921 }
ec4ac0f7 922 else if (evt.GetId() == m_tools_medium->GetId())
61b98a2d 923 {
236cff73 924 m_browser->SetZoom(wxWEBVIEW_ZOOM_MEDIUM);
61b98a2d 925 }
ec4ac0f7 926 else if (evt.GetId() == m_tools_large->GetId())
61b98a2d 927 {
236cff73 928 m_browser->SetZoom(wxWEBVIEW_ZOOM_LARGE);
61b98a2d 929 }
ec4ac0f7 930 else if (evt.GetId() == m_tools_largest->GetId())
61b98a2d 931 {
236cff73 932 m_browser->SetZoom(wxWEBVIEW_ZOOM_LARGEST);
61b98a2d 933 }
ec4ac0f7 934 else
61b98a2d 935 {
ec4ac0f7 936 wxFAIL;
61b98a2d 937 }
ec4ac0f7 938}
853b6cd0 939
8bccab8f 940void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt))
87d482ec
SL
941{
942 if(m_tools_layout->IsChecked())
236cff73 943 m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT);
87d482ec 944 else
236cff73 945 m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT);
87d482ec
SL
946}
947
10ad4ba6
SL
948void WebFrame::OnHistory(wxCommandEvent& evt)
949{
950 m_browser->LoadHistoryItem(m_histMenuItems[evt.GetId()]);
951}
952
54883129
SL
953void WebFrame::OnRunScript(wxCommandEvent& WXUNUSED(evt))
954{
955 wxTextEntryDialog dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr, "", wxOK|wxCANCEL|wxCENTRE|wxTE_MULTILINE);
956 if(dialog.ShowModal() == wxID_OK)
957 {
958 m_browser->RunScript(dialog.GetValue());
959 }
960}
961
603cfe42
SL
962void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))
963{
964 m_browser->ClearSelection();
965}
966
967void WebFrame::OnDeleteSelection(wxCommandEvent& WXUNUSED(evt))
968{
969 m_browser->DeleteSelection();
970}
971
972void WebFrame::OnSelectAll(wxCommandEvent& WXUNUSED(evt))
973{
974 m_browser->SelectAll();
975}
976
ec4ac0f7
SL
977/**
978 * Callback invoked when a loading error occurs
979 */
04fa04d8 980void WebFrame::OnError(wxWebViewEvent& evt)
c1381581
SL
981{
982#define WX_ERROR_CASE(type) \
983 case type: \
984 category = #type; \
ec4ac0f7 985 break;
abcb9c6e 986
c1381581
SL
987 wxString category;
988 switch (evt.GetInt())
989 {
236cff73
SL
990 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CONNECTION);
991 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CERTIFICATE);
992 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_AUTH);
993 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_SECURITY);
994 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_NOT_FOUND);
995 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_REQUEST);
996 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_USER_CANCELLED);
997 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_OTHER);
61b98a2d 998 }
abcb9c6e 999
c1381581 1000 wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + category + " (" + evt.GetString() + ")'");
abcb9c6e 1001
ec4ac0f7 1002 //Show the info bar with an error
e40741b9 1003 m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +
c1381581 1004 "'" + category + "'", wxICON_ERROR);
abcb9c6e 1005
ec4ac0f7
SL
1006 UpdateState();
1007}
61b98a2d 1008
ec4ac0f7
SL
1009/**
1010 * Invoked when user selects "Print" from the menu
1011 */
8bccab8f 1012void WebFrame::OnPrint(wxCommandEvent& WXUNUSED(evt))
61b98a2d 1013{
ec4ac0f7
SL
1014 m_browser->Print();
1015}
61b98a2d 1016
ec4ac0f7
SL
1017SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
1018 wxDialog(parent, wxID_ANY, "Source Code",
1019 wxDefaultPosition, wxSize(700,500),
1020 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
1021{
1022 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
1023 text->SetMarginWidth(1, 30);
abcb9c6e 1024 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
ec4ac0f7 1025 text->SetText(source);
61b98a2d 1026
ec4ac0f7
SL
1027 text->StyleClearAll();
1028 text->SetLexer(wxSTC_LEX_HTML);
1029 text->StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(255,0,0));
1030 text->StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(255,0,0));
1031 text->StyleSetForeground(wxSTC_H_ENTITY, wxColour(255,0,0));
1032 text->StyleSetForeground(wxSTC_H_TAG, wxColour(0,150,0));
1033 text->StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(0,150,0));
1034 text->StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,0,150));
1035 text->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(0,0,150));
1036 text->StyleSetForeground(wxSTC_H_COMMENT, wxColour(150,150,150));
61b98a2d 1037
61b98a2d 1038 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
ec4ac0f7
SL
1039 sizer->Add(text, 1, wxEXPAND);
1040 SetSizer(sizer);
61b98a2d 1041}