Allow passing the URL to open to webview sample on command line.
[wxWidgets.git] / samples / webview / webview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: webview.cpp
3 // Purpose: wxWebView sample
4 // Author: Marianne Gagnon
5 // Id: $Id$
6 // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
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
25 #if !wxUSE_WEBVIEW_WEBKIT && !wxUSE_WEBVIEW_IE
26 #error "A wxWebView backend is required by this sample"
27 #endif
28
29 #include "wx/artprov.h"
30 #include "wx/cmdline.h"
31 #include "wx/notifmsg.h"
32 #include "wx/settings.h"
33 #include "wx/webview.h"
34 #include "wx/webviewarchivehandler.h"
35 #include "wx/infobar.h"
36 #include "wx/filesys.h"
37 #include "wx/fs_arc.h"
38
39 #ifndef wxHAS_IMAGES_IN_RESOURCES
40 #include "../sample.xpm"
41 #endif
42
43 #if wxUSE_STC
44 #include "wx/stc/stc.h"
45 #else
46 #error "wxStyledTextControl is needed by this sample"
47 #endif
48
49 #if defined(__WXMSW__) || defined(__WXOSX__)
50 #include "stop.xpm"
51 #include "refresh.xpm"
52 #endif
53
54 #include "wxlogo.xpm"
55
56
57 //We map menu items to their history items
58 WX_DECLARE_HASH_MAP(int, wxSharedPtr<wxWebViewHistoryItem>,
59 wxIntegerHash, wxIntegerEqual, wxMenuHistoryMap);
60
61 class WebApp : public wxApp
62 {
63 public:
64 WebApp() :
65 m_url("http://www.wxwidgets.org")
66 {
67 }
68
69 virtual bool OnInit();
70
71 #if wxUSE_CMDLINE_PARSER
72 virtual void OnInitCmdLine(wxCmdLineParser& parser)
73 {
74 wxApp::OnInitCmdLine(parser);
75
76 parser.AddParam("URL to open",
77 wxCMD_LINE_VAL_STRING,
78 wxCMD_LINE_PARAM_OPTIONAL);
79 }
80
81 virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
82 {
83 if ( !wxApp::OnCmdLineParsed(parser) )
84 return false;
85
86 if ( parser.GetParamCount() )
87 m_url = parser.GetParam(0);
88
89 return true;
90 }
91 #endif // wxUSE_CMDLINE_PARSER
92
93 private:
94 wxString m_url;
95 };
96
97 class WebFrame : public wxFrame
98 {
99 public:
100 WebFrame(const wxString& url);
101 virtual ~WebFrame();
102
103 void UpdateState();
104 void OnIdle(wxIdleEvent& evt);
105 void OnUrl(wxCommandEvent& evt);
106 void OnBack(wxCommandEvent& evt);
107 void OnForward(wxCommandEvent& evt);
108 void OnStop(wxCommandEvent& evt);
109 void OnReload(wxCommandEvent& evt);
110 void OnClearHistory(wxCommandEvent& evt);
111 void OnEnableHistory(wxCommandEvent& evt);
112 void OnNavigationRequest(wxWebViewEvent& evt);
113 void OnNavigationComplete(wxWebViewEvent& evt);
114 void OnDocumentLoaded(wxWebViewEvent& evt);
115 void OnNewWindow(wxWebViewEvent& evt);
116 void OnTitleChanged(wxWebViewEvent& evt);
117 void OnViewSourceRequest(wxCommandEvent& evt);
118 void OnToolsClicked(wxCommandEvent& evt);
119 void OnSetZoom(wxCommandEvent& evt);
120 void OnError(wxWebViewEvent& evt);
121 void OnPrint(wxCommandEvent& evt);
122 void OnCut(wxCommandEvent& evt);
123 void OnCopy(wxCommandEvent& evt);
124 void OnPaste(wxCommandEvent& evt);
125 void OnUndo(wxCommandEvent& evt);
126 void OnRedo(wxCommandEvent& evt);
127 void OnMode(wxCommandEvent& evt);
128 void OnZoomLayout(wxCommandEvent& evt);
129 void OnHistory(wxCommandEvent& evt);
130 void OnRunScript(wxCommandEvent& evt);
131 void OnClearSelection(wxCommandEvent& evt);
132 void OnDeleteSelection(wxCommandEvent& evt);
133 void OnSelectAll(wxCommandEvent& evt);
134 void OnLoadScheme(wxCommandEvent& evt);
135
136 private:
137 wxTextCtrl* m_url;
138 wxWebView* m_browser;
139
140 wxToolBar* m_toolbar;
141 wxToolBarToolBase* m_toolbar_back;
142 wxToolBarToolBase* m_toolbar_forward;
143 wxToolBarToolBase* m_toolbar_stop;
144 wxToolBarToolBase* m_toolbar_reload;
145 wxToolBarToolBase* m_toolbar_tools;
146
147 wxMenu* m_tools_menu;
148 wxMenu* m_tools_history_menu;
149 wxMenuItem* m_tools_layout;
150 wxMenuItem* m_tools_tiny;
151 wxMenuItem* m_tools_small;
152 wxMenuItem* m_tools_medium;
153 wxMenuItem* m_tools_large;
154 wxMenuItem* m_tools_largest;
155 wxMenuItem* m_tools_handle_navigation;
156 wxMenuItem* m_tools_handle_new_window;
157 wxMenuItem* m_tools_enable_history;
158 wxMenuItem* m_edit_cut;
159 wxMenuItem* m_edit_copy;
160 wxMenuItem* m_edit_paste;
161 wxMenuItem* m_edit_undo;
162 wxMenuItem* m_edit_redo;
163 wxMenuItem* m_edit_mode;
164 wxMenuItem* m_selection_clear;
165 wxMenuItem* m_selection_delete;
166
167 wxInfoBar *m_info;
168 wxStaticText* m_info_text;
169
170 wxMenuHistoryMap m_histMenuItems;
171 };
172
173 class SourceViewDialog : public wxDialog
174 {
175 public:
176 SourceViewDialog(wxWindow* parent, wxString source);
177 };
178
179 IMPLEMENT_APP(WebApp)
180
181 // ============================================================================
182 // implementation
183 // ============================================================================
184
185 bool WebApp::OnInit()
186 {
187 if ( !wxApp::OnInit() )
188 return false;
189
190 WebFrame *frame = new WebFrame(m_url);
191 frame->Show();
192
193 return true;
194 }
195
196 WebFrame::WebFrame(const wxString& url) :
197 wxFrame(NULL, wxID_ANY, "wxWebView Sample")
198 {
199 //Required from virtual file system archive support
200 wxFileSystem::AddHandler(new wxArchiveFSHandler);
201
202 // set the frame icon
203 SetIcon(wxICON(sample));
204 SetTitle("wxWebView Sample");
205
206 wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
207
208 // Create the toolbar
209 m_toolbar = CreateToolBar(wxTB_TEXT);
210 m_toolbar->SetToolBitmapSize(wxSize(32, 32));
211
212 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR);
213 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR);
214 #ifdef __WXGTK__
215 wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR);
216 #else
217 wxBitmap stop = wxBitmap(stop_xpm);
218 #endif
219 #ifdef __WXGTK__
220 wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR);
221 #else
222 wxBitmap refresh = wxBitmap(refresh_xpm);
223 #endif
224
225 m_toolbar_back = m_toolbar->AddTool(wxID_ANY, _("Back"), back);
226 m_toolbar_forward = m_toolbar->AddTool(wxID_ANY, _("Forward"), forward);
227 m_toolbar_stop = m_toolbar->AddTool(wxID_ANY, _("Stop"), stop);
228 m_toolbar_reload = m_toolbar->AddTool(wxID_ANY, _("Reload"), refresh);
229 m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER );
230 m_toolbar->AddControl(m_url, _("URL"));
231 m_toolbar_tools = m_toolbar->AddTool(wxID_ANY, _("Menu"), wxBitmap(wxlogo_xpm));
232
233 m_toolbar->Realize();
234
235 // Create the info panel
236 m_info = new wxInfoBar(this);
237 topsizer->Add(m_info, wxSizerFlags().Expand());
238
239 // Create the webview
240 m_browser = wxWebView::New(this, wxID_ANY, url);
241 topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
242
243 //We register the wxfs:// protocol for testing purposes
244 m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
245
246 SetSizer(topsizer);
247
248 //Set a more sensible size for web browsing
249 SetSize(wxSize(800, 600));
250
251 // Create a log window
252 new wxLogWindow(this, _("Logging"));
253
254 // Create the Tools menu
255 m_tools_menu = new wxMenu();
256 wxMenuItem* print = m_tools_menu->Append(wxID_ANY , _("Print"));
257 wxMenuItem* viewSource = m_tools_menu->Append(wxID_ANY , _("View Source"));
258 m_tools_menu->AppendSeparator();
259 m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom"));
260 m_tools_tiny = m_tools_menu->AppendCheckItem(wxID_ANY, _("Tiny"));
261 m_tools_small = m_tools_menu->AppendCheckItem(wxID_ANY, _("Small"));
262 m_tools_medium = m_tools_menu->AppendCheckItem(wxID_ANY, _("Medium"));
263 m_tools_large = m_tools_menu->AppendCheckItem(wxID_ANY, _("Large"));
264 m_tools_largest = m_tools_menu->AppendCheckItem(wxID_ANY, _("Largest"));
265 m_tools_menu->AppendSeparator();
266 m_tools_handle_navigation = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle Navigation"));
267 m_tools_handle_new_window = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle New Windows"));
268 m_tools_menu->AppendSeparator();
269
270 //History menu
271 m_tools_history_menu = new wxMenu();
272 wxMenuItem* clearhist = m_tools_history_menu->Append(wxID_ANY, _("Clear History"));
273 m_tools_enable_history = m_tools_history_menu->AppendCheckItem(wxID_ANY, _("Enable History"));
274 m_tools_history_menu->AppendSeparator();
275
276 m_tools_menu->AppendSubMenu(m_tools_history_menu, "History");
277
278 //Create an editing menu
279 wxMenu* editmenu = new wxMenu();
280 m_edit_cut = editmenu->Append(wxID_ANY, _("Cut"));
281 m_edit_copy = editmenu->Append(wxID_ANY, _("Copy"));
282 m_edit_paste = editmenu->Append(wxID_ANY, _("Paste"));
283 editmenu->AppendSeparator();
284 m_edit_undo = editmenu->Append(wxID_ANY, _("Undo"));
285 m_edit_redo = editmenu->Append(wxID_ANY, _("Redo"));
286 editmenu->AppendSeparator();
287 m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode"));
288
289 m_tools_menu->AppendSeparator();
290 m_tools_menu->AppendSubMenu(editmenu, "Edit");
291
292 wxMenuItem* script = m_tools_menu->Append(wxID_ANY, _("Run Script"));
293
294 //Selection menu
295 wxMenu* selection = new wxMenu();
296 m_selection_clear = selection->Append(wxID_ANY, _("Clear Selection"));
297 m_selection_delete = selection->Append(wxID_ANY, _("Delete Selection"));
298 wxMenuItem* selectall = selection->Append(wxID_ANY, _("Select All"));
299
300 editmenu->AppendSubMenu(selection, "Selection");
301
302 wxMenuItem* loadscheme = m_tools_menu->Append(wxID_ANY, _("Custom Scheme Example"));
303
304 //By default we want to handle navigation and new windows
305 m_tools_handle_navigation->Check();
306 m_tools_handle_new_window->Check();
307 m_tools_enable_history->Check();
308 if(!m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT))
309 m_tools_layout->Enable(false);
310
311
312 // Connect the toolbar events
313 Connect(m_toolbar_back->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
314 wxCommandEventHandler(WebFrame::OnBack), NULL, this );
315 Connect(m_toolbar_forward->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
316 wxCommandEventHandler(WebFrame::OnForward), NULL, this );
317 Connect(m_toolbar_stop->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
318 wxCommandEventHandler(WebFrame::OnStop), NULL, this );
319 Connect(m_toolbar_reload->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
320 wxCommandEventHandler(WebFrame::OnReload),NULL, this );
321 Connect(m_toolbar_tools->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
322 wxCommandEventHandler(WebFrame::OnToolsClicked), NULL, this );
323
324 Connect(m_url->GetId(), wxEVT_COMMAND_TEXT_ENTER,
325 wxCommandEventHandler(WebFrame::OnUrl), NULL, this );
326
327 // Connect the webview events
328 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
329 wxWebViewEventHandler(WebFrame::OnNavigationRequest), NULL, this);
330 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
331 wxWebViewEventHandler(WebFrame::OnNavigationComplete), NULL, this);
332 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED,
333 wxWebViewEventHandler(WebFrame::OnDocumentLoaded), NULL, this);
334 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR,
335 wxWebViewEventHandler(WebFrame::OnError), NULL, this);
336 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
337 wxWebViewEventHandler(WebFrame::OnNewWindow), NULL, this);
338 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
339 wxWebViewEventHandler(WebFrame::OnTitleChanged), NULL, this);
340
341 // Connect the menu events
342 Connect(viewSource->GetId(), wxEVT_COMMAND_MENU_SELECTED,
343 wxCommandEventHandler(WebFrame::OnViewSourceRequest), NULL, this );
344 Connect(print->GetId(), wxEVT_COMMAND_MENU_SELECTED,
345 wxCommandEventHandler(WebFrame::OnPrint), NULL, this );
346 Connect(m_tools_layout->GetId(), wxEVT_COMMAND_MENU_SELECTED,
347 wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this );
348 Connect(m_tools_tiny->GetId(), wxEVT_COMMAND_MENU_SELECTED,
349 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
350 Connect(m_tools_small->GetId(), wxEVT_COMMAND_MENU_SELECTED,
351 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
352 Connect(m_tools_medium->GetId(), wxEVT_COMMAND_MENU_SELECTED,
353 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
354 Connect(m_tools_large->GetId(), wxEVT_COMMAND_MENU_SELECTED,
355 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
356 Connect(m_tools_largest->GetId(), wxEVT_COMMAND_MENU_SELECTED,
357 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
358 Connect(clearhist->GetId(), wxEVT_COMMAND_MENU_SELECTED,
359 wxCommandEventHandler(WebFrame::OnClearHistory), NULL, this );
360 Connect(m_tools_enable_history->GetId(), wxEVT_COMMAND_MENU_SELECTED,
361 wxCommandEventHandler(WebFrame::OnEnableHistory), NULL, this );
362 Connect(m_edit_cut->GetId(), wxEVT_COMMAND_MENU_SELECTED,
363 wxCommandEventHandler(WebFrame::OnCut), NULL, this );
364 Connect(m_edit_copy->GetId(), wxEVT_COMMAND_MENU_SELECTED,
365 wxCommandEventHandler(WebFrame::OnCopy), NULL, this );
366 Connect(m_edit_paste->GetId(), wxEVT_COMMAND_MENU_SELECTED,
367 wxCommandEventHandler(WebFrame::OnPaste), NULL, this );
368 Connect(m_edit_undo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
369 wxCommandEventHandler(WebFrame::OnUndo), NULL, this );
370 Connect(m_edit_redo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
371 wxCommandEventHandler(WebFrame::OnRedo), NULL, this );
372 Connect(m_edit_mode->GetId(), wxEVT_COMMAND_MENU_SELECTED,
373 wxCommandEventHandler(WebFrame::OnMode), NULL, this );
374 Connect(script->GetId(), wxEVT_COMMAND_MENU_SELECTED,
375 wxCommandEventHandler(WebFrame::OnRunScript), NULL, this );
376 Connect(m_selection_clear->GetId(), wxEVT_COMMAND_MENU_SELECTED,
377 wxCommandEventHandler(WebFrame::OnClearSelection), NULL, this );
378 Connect(m_selection_delete->GetId(), wxEVT_COMMAND_MENU_SELECTED,
379 wxCommandEventHandler(WebFrame::OnDeleteSelection), NULL, this );
380 Connect(selectall->GetId(), wxEVT_COMMAND_MENU_SELECTED,
381 wxCommandEventHandler(WebFrame::OnSelectAll), NULL, this );
382 Connect(loadscheme->GetId(), wxEVT_COMMAND_MENU_SELECTED,
383 wxCommandEventHandler(WebFrame::OnLoadScheme), NULL, this );
384
385 //Connect the idle events
386 Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(WebFrame::OnIdle), NULL, this);
387 }
388
389 WebFrame::~WebFrame()
390 {
391 delete m_tools_menu;
392 }
393
394 /**
395 * Method that retrieves the current state from the web control and updates the GUI
396 * the reflect this current state.
397 */
398 void WebFrame::UpdateState()
399 {
400 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
401 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
402
403 if (m_browser->IsBusy())
404 {
405 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
406 }
407 else
408 {
409 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
410 }
411
412 SetTitle( m_browser->GetCurrentTitle() );
413 m_url->SetValue( m_browser->GetCurrentURL() );
414 }
415
416 void WebFrame::OnIdle(wxIdleEvent& WXUNUSED(evt))
417 {
418 if(m_browser->IsBusy())
419 {
420 wxSetCursor(wxCURSOR_ARROWWAIT);
421 m_toolbar->EnableTool(m_toolbar_stop->GetId(), true);
422 }
423 else
424 {
425 wxSetCursor(wxNullCursor);
426 m_toolbar->EnableTool(m_toolbar_stop->GetId(), false);
427 }
428 }
429
430 /**
431 * Callback invoked when user entered an URL and pressed enter
432 */
433 void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt))
434 {
435 m_browser->LoadURL( m_url->GetValue() );
436 m_browser->SetFocus();
437 UpdateState();
438 }
439
440 /**
441 * Callback invoked when user pressed the "back" button
442 */
443 void WebFrame::OnBack(wxCommandEvent& WXUNUSED(evt))
444 {
445 m_browser->GoBack();
446 UpdateState();
447 }
448
449 /**
450 * Callback invoked when user pressed the "forward" button
451 */
452 void WebFrame::OnForward(wxCommandEvent& WXUNUSED(evt))
453 {
454 m_browser->GoForward();
455 UpdateState();
456 }
457
458 /**
459 * Callback invoked when user pressed the "stop" button
460 */
461 void WebFrame::OnStop(wxCommandEvent& WXUNUSED(evt))
462 {
463 m_browser->Stop();
464 UpdateState();
465 }
466
467 /**
468 * Callback invoked when user pressed the "reload" button
469 */
470 void WebFrame::OnReload(wxCommandEvent& WXUNUSED(evt))
471 {
472 m_browser->Reload();
473 UpdateState();
474 }
475
476 void WebFrame::OnClearHistory(wxCommandEvent& WXUNUSED(evt))
477 {
478 m_browser->ClearHistory();
479 UpdateState();
480 }
481
482 void WebFrame::OnEnableHistory(wxCommandEvent& WXUNUSED(evt))
483 {
484 m_browser->EnableHistory(m_tools_enable_history->IsChecked());
485 UpdateState();
486 }
487
488 void WebFrame::OnCut(wxCommandEvent& WXUNUSED(evt))
489 {
490 m_browser->Cut();
491 }
492
493 void WebFrame::OnCopy(wxCommandEvent& WXUNUSED(evt))
494 {
495 m_browser->Copy();
496 }
497
498 void WebFrame::OnPaste(wxCommandEvent& WXUNUSED(evt))
499 {
500 m_browser->Paste();
501 }
502
503 void WebFrame::OnUndo(wxCommandEvent& WXUNUSED(evt))
504 {
505 m_browser->Undo();
506 }
507
508 void WebFrame::OnRedo(wxCommandEvent& WXUNUSED(evt))
509 {
510 m_browser->Redo();
511 }
512
513 void WebFrame::OnMode(wxCommandEvent& WXUNUSED(evt))
514 {
515 m_browser->SetEditable(m_edit_mode->IsChecked());
516 }
517
518 void WebFrame::OnLoadScheme(wxCommandEvent& WXUNUSED(evt))
519 {
520 wxFileName helpfile("../help/doc.zip");
521 helpfile.MakeAbsolute();
522 wxString path = helpfile.GetFullPath();
523 //Under MSW we need to flip the slashes
524 path.Replace("\\", "/");
525 path = "wxfs:///" + path + ";protocol=zip/doc.htm";
526 m_browser->LoadURL(path);
527 }
528
529 /**
530 * Callback invoked when there is a request to load a new page (for instance
531 * when the user clicks a link)
532 */
533 void WebFrame::OnNavigationRequest(wxWebViewEvent& evt)
534 {
535 if(m_info->IsShown())
536 {
537 m_info->Dismiss();
538 }
539
540 wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" +
541 evt.GetTarget() + "')");
542
543 wxASSERT(m_browser->IsBusy());
544
545 //If we don't want to handle navigation then veto the event and navigation
546 //will not take place, we also need to stop the loading animation
547 if(!m_tools_handle_navigation->IsChecked())
548 {
549 evt.Veto();
550 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
551 }
552 else
553 {
554 UpdateState();
555 }
556 }
557
558 /**
559 * Callback invoked when a navigation request was accepted
560 */
561 void WebFrame::OnNavigationComplete(wxWebViewEvent& evt)
562 {
563 wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'");
564 UpdateState();
565 }
566
567 /**
568 * Callback invoked when a page is finished loading
569 */
570 void WebFrame::OnDocumentLoaded(wxWebViewEvent& evt)
571 {
572 //Only notify if the document is the main frame, not a subframe
573 if(evt.GetURL() == m_browser->GetCurrentURL())
574 {
575 wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'");
576 }
577 UpdateState();
578 }
579
580 /**
581 * On new window, we veto to stop extra windows appearing
582 */
583 void WebFrame::OnNewWindow(wxWebViewEvent& evt)
584 {
585 wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'");
586
587 //If we handle new window events then just load them in this window as we
588 //are a single window browser
589 if(m_tools_handle_new_window->IsChecked())
590 m_browser->LoadURL(evt.GetURL());
591
592 UpdateState();
593 }
594
595 void WebFrame::OnTitleChanged(wxWebViewEvent& evt)
596 {
597 SetTitle(evt.GetString());
598 wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'");
599 }
600
601 /**
602 * Invoked when user selects the "View Source" menu item
603 */
604 void WebFrame::OnViewSourceRequest(wxCommandEvent& WXUNUSED(evt))
605 {
606 SourceViewDialog dlg(this, m_browser->GetPageSource());
607 dlg.ShowModal();
608 }
609
610 /**
611 * Invoked when user selects the "Menu" item
612 */
613 void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
614 {
615 if(m_browser->GetCurrentURL() == "")
616 return;
617
618 m_tools_tiny->Check(false);
619 m_tools_small->Check(false);
620 m_tools_medium->Check(false);
621 m_tools_large->Check(false);
622 m_tools_largest->Check(false);
623
624 wxWebViewZoom zoom = m_browser->GetZoom();
625 switch (zoom)
626 {
627 case wxWEB_VIEW_ZOOM_TINY:
628 m_tools_tiny->Check();
629 break;
630 case wxWEB_VIEW_ZOOM_SMALL:
631 m_tools_small->Check();
632 break;
633 case wxWEB_VIEW_ZOOM_MEDIUM:
634 m_tools_medium->Check();
635 break;
636 case wxWEB_VIEW_ZOOM_LARGE:
637 m_tools_large->Check();
638 break;
639 case wxWEB_VIEW_ZOOM_LARGEST:
640 m_tools_largest->Check();
641 break;
642 }
643
644 m_edit_cut->Enable(m_browser->CanCut());
645 m_edit_copy->Enable(m_browser->CanCopy());
646 m_edit_paste->Enable(m_browser->CanPaste());
647
648 m_edit_undo->Enable(m_browser->CanUndo());
649 m_edit_redo->Enable(m_browser->CanRedo());
650
651 m_selection_clear->Enable(m_browser->HasSelection());
652 m_selection_delete->Enable(m_browser->HasSelection());
653
654 //Firstly we clear the existing menu items, then we add the current ones
655 wxMenuHistoryMap::const_iterator it;
656 for( it = m_histMenuItems.begin(); it != m_histMenuItems.end(); ++it )
657 {
658 m_tools_history_menu->Destroy(it->first);
659 }
660 m_histMenuItems.clear();
661
662 wxVector<wxSharedPtr<wxWebViewHistoryItem> > back = m_browser->GetBackwardHistory();
663 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forward = m_browser->GetForwardHistory();
664
665 wxMenuItem* item;
666
667 unsigned int i;
668 for(i = 0; i < back.size(); i++)
669 {
670 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, back[i]->GetTitle());
671 m_histMenuItems[item->GetId()] = back[i];
672 Connect(item->GetId(), wxEVT_COMMAND_MENU_SELECTED,
673 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
674 }
675
676 wxString title = m_browser->GetCurrentTitle();
677 if ( title.empty() )
678 title = "(untitled)";
679 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, title);
680 item->Check();
681
682 //No need to connect the current item
683 m_histMenuItems[item->GetId()] = wxSharedPtr<wxWebViewHistoryItem>(new wxWebViewHistoryItem(m_browser->GetCurrentURL(), m_browser->GetCurrentTitle()));
684
685 for(i = 0; i < forward.size(); i++)
686 {
687 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, forward[i]->GetTitle());
688 m_histMenuItems[item->GetId()] = forward[i];
689 Connect(item->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
690 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
691 }
692
693 wxPoint position = ScreenToClient( wxGetMousePosition() );
694 PopupMenu(m_tools_menu, position.x, position.y);
695 }
696
697 /**
698 * Invoked when user selects the zoom size in the menu
699 */
700 void WebFrame::OnSetZoom(wxCommandEvent& evt)
701 {
702 if (evt.GetId() == m_tools_tiny->GetId())
703 {
704 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
705 }
706 else if (evt.GetId() == m_tools_small->GetId())
707 {
708 m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL);
709 }
710 else if (evt.GetId() == m_tools_medium->GetId())
711 {
712 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
713 }
714 else if (evt.GetId() == m_tools_large->GetId())
715 {
716 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE);
717 }
718 else if (evt.GetId() == m_tools_largest->GetId())
719 {
720 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST);
721 }
722 else
723 {
724 wxFAIL;
725 }
726 }
727
728 void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt))
729 {
730 if(m_tools_layout->IsChecked())
731 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT);
732 else
733 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT);
734 }
735
736 void WebFrame::OnHistory(wxCommandEvent& evt)
737 {
738 m_browser->LoadHistoryItem(m_histMenuItems[evt.GetId()]);
739 }
740
741 void WebFrame::OnRunScript(wxCommandEvent& WXUNUSED(evt))
742 {
743 wxTextEntryDialog dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr, "", wxOK|wxCANCEL|wxCENTRE|wxTE_MULTILINE);
744 if(dialog.ShowModal() == wxID_OK)
745 {
746 m_browser->RunScript(dialog.GetValue());
747 }
748 }
749
750 void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))
751 {
752 m_browser->ClearSelection();
753 }
754
755 void WebFrame::OnDeleteSelection(wxCommandEvent& WXUNUSED(evt))
756 {
757 m_browser->DeleteSelection();
758 }
759
760 void WebFrame::OnSelectAll(wxCommandEvent& WXUNUSED(evt))
761 {
762 m_browser->SelectAll();
763 }
764
765 /**
766 * Callback invoked when a loading error occurs
767 */
768 void WebFrame::OnError(wxWebViewEvent& evt)
769 {
770 wxString errorCategory;
771 switch (evt.GetInt())
772 {
773 case wxWEB_NAV_ERR_CONNECTION:
774 errorCategory = "wxWEB_NAV_ERR_CONNECTION";
775 break;
776
777 case wxWEB_NAV_ERR_CERTIFICATE:
778 errorCategory = "wxWEB_NAV_ERR_CERTIFICATE";
779 break;
780
781 case wxWEB_NAV_ERR_AUTH:
782 errorCategory = "wxWEB_NAV_ERR_AUTH";
783 break;
784
785 case wxWEB_NAV_ERR_SECURITY:
786 errorCategory = "wxWEB_NAV_ERR_SECURITY";
787 break;
788
789 case wxWEB_NAV_ERR_NOT_FOUND:
790 errorCategory = "wxWEB_NAV_ERR_NOT_FOUND";
791 break;
792
793 case wxWEB_NAV_ERR_REQUEST:
794 errorCategory = "wxWEB_NAV_ERR_REQUEST";
795 break;
796
797 case wxWEB_NAV_ERR_USER_CANCELLED:
798 errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED";
799 break;
800
801 case wxWEB_NAV_ERR_OTHER:
802 errorCategory = "wxWEB_NAV_ERR_OTHER";
803 break;
804 }
805
806 wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")");
807
808 //Show the info bar with an error
809 m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +
810 "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR);
811
812 UpdateState();
813 }
814
815 /**
816 * Invoked when user selects "Print" from the menu
817 */
818 void WebFrame::OnPrint(wxCommandEvent& WXUNUSED(evt))
819 {
820 m_browser->Print();
821 }
822
823 SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
824 wxDialog(parent, wxID_ANY, "Source Code",
825 wxDefaultPosition, wxSize(700,500),
826 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
827 {
828 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
829 text->SetMarginWidth(1, 30);
830 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
831 text->SetText(source);
832
833 text->StyleClearAll();
834 text->SetLexer(wxSTC_LEX_HTML);
835 text->StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(255,0,0));
836 text->StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(255,0,0));
837 text->StyleSetForeground(wxSTC_H_ENTITY, wxColour(255,0,0));
838 text->StyleSetForeground(wxSTC_H_TAG, wxColour(0,150,0));
839 text->StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(0,150,0));
840 text->StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,0,150));
841 text->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(0,0,150));
842 text->StyleSetForeground(wxSTC_H_COMMENT, wxColour(150,150,150));
843
844 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
845 sizer->Add(text, 1, wxEXPAND);
846 SetSizer(sizer);
847 }