1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWebView sample
4 // Author: Marianne Gagnon
6 // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
25 #if !wxUSE_WEBVIEW_WEBKIT && !wxUSE_WEBVIEW_IE
26 #error "A wxWebView backend is required by this sample"
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"
39 #ifndef wxHAS_IMAGES_IN_RESOURCES
40 #include "../sample.xpm"
44 #include "wx/stc/stc.h"
46 #error "wxStyledTextControl is needed by this sample"
49 #if defined(__WXMSW__) || defined(__WXOSX__)
51 #include "refresh.xpm"
57 //We map menu items to their history items
58 WX_DECLARE_HASH_MAP(int, wxSharedPtr
<wxWebViewHistoryItem
>,
59 wxIntegerHash
, wxIntegerEqual
, wxMenuHistoryMap
);
61 class WebApp
: public wxApp
65 m_url("http://www.wxwidgets.org")
69 virtual bool OnInit();
71 #if wxUSE_CMDLINE_PARSER
72 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
74 wxApp::OnInitCmdLine(parser
);
76 parser
.AddParam("URL to open",
77 wxCMD_LINE_VAL_STRING
,
78 wxCMD_LINE_PARAM_OPTIONAL
);
81 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
83 if ( !wxApp::OnCmdLineParsed(parser
) )
86 if ( parser
.GetParamCount() )
87 m_url
= parser
.GetParam(0);
91 #endif // wxUSE_CMDLINE_PARSER
97 class WebFrame
: public wxFrame
100 WebFrame(const wxString
& url
);
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 OnScrollLineUp(wxCommandEvent
&) { m_browser
->LineUp(); }
131 void OnScrollLineDown(wxCommandEvent
&) { m_browser
->LineDown(); }
132 void OnScrollPageUp(wxCommandEvent
&) { m_browser
->PageUp(); }
133 void OnScrollPageDown(wxCommandEvent
&) { m_browser
->PageDown(); }
134 void OnRunScript(wxCommandEvent
& evt
);
135 void OnClearSelection(wxCommandEvent
& evt
);
136 void OnDeleteSelection(wxCommandEvent
& evt
);
137 void OnSelectAll(wxCommandEvent
& evt
);
138 void OnLoadScheme(wxCommandEvent
& evt
);
142 wxWebView
* m_browser
;
144 wxToolBar
* m_toolbar
;
145 wxToolBarToolBase
* m_toolbar_back
;
146 wxToolBarToolBase
* m_toolbar_forward
;
147 wxToolBarToolBase
* m_toolbar_stop
;
148 wxToolBarToolBase
* m_toolbar_reload
;
149 wxToolBarToolBase
* m_toolbar_tools
;
151 wxMenu
* m_tools_menu
;
152 wxMenu
* m_tools_history_menu
;
153 wxMenuItem
* m_tools_layout
;
154 wxMenuItem
* m_tools_tiny
;
155 wxMenuItem
* m_tools_small
;
156 wxMenuItem
* m_tools_medium
;
157 wxMenuItem
* m_tools_large
;
158 wxMenuItem
* m_tools_largest
;
159 wxMenuItem
* m_tools_handle_navigation
;
160 wxMenuItem
* m_tools_handle_new_window
;
161 wxMenuItem
* m_tools_enable_history
;
162 wxMenuItem
* m_edit_cut
;
163 wxMenuItem
* m_edit_copy
;
164 wxMenuItem
* m_edit_paste
;
165 wxMenuItem
* m_edit_undo
;
166 wxMenuItem
* m_edit_redo
;
167 wxMenuItem
* m_edit_mode
;
168 wxMenuItem
* m_scroll_line_up
;
169 wxMenuItem
* m_scroll_line_down
;
170 wxMenuItem
* m_scroll_page_up
;
171 wxMenuItem
* m_scroll_page_down
;
172 wxMenuItem
* m_selection_clear
;
173 wxMenuItem
* m_selection_delete
;
176 wxStaticText
* m_info_text
;
178 wxMenuHistoryMap m_histMenuItems
;
181 class SourceViewDialog
: public wxDialog
184 SourceViewDialog(wxWindow
* parent
, wxString source
);
187 IMPLEMENT_APP(WebApp
)
189 // ============================================================================
191 // ============================================================================
193 bool WebApp::OnInit()
195 if ( !wxApp::OnInit() )
198 WebFrame
*frame
= new WebFrame(m_url
);
204 WebFrame::WebFrame(const wxString
& url
) :
205 wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
207 //Required from virtual file system archive support
208 wxFileSystem::AddHandler(new wxArchiveFSHandler
);
210 // set the frame icon
211 SetIcon(wxICON(sample
));
212 SetTitle("wxWebView Sample");
214 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
216 // Create the toolbar
217 m_toolbar
= CreateToolBar(wxTB_TEXT
);
218 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
220 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
221 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
223 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
225 wxBitmap stop
= wxBitmap(stop_xpm
);
228 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
230 wxBitmap refresh
= wxBitmap(refresh_xpm
);
233 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), back
);
234 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), forward
);
235 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), stop
);
236 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), refresh
);
237 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
238 m_toolbar
->AddControl(m_url
, _("URL"));
239 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
241 m_toolbar
->Realize();
243 // Create the info panel
244 m_info
= new wxInfoBar(this);
245 topsizer
->Add(m_info
, wxSizerFlags().Expand());
247 // Create the webview
248 m_browser
= wxWebView::New(this, wxID_ANY
, url
);
249 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
251 //We register the wxfs:// protocol for testing purposes
252 m_browser
->RegisterHandler(wxSharedPtr
<wxWebViewHandler
>(new wxWebViewArchiveHandler("wxfs")));
256 //Set a more sensible size for web browsing
257 SetSize(wxSize(800, 600));
259 // Create a log window
260 new wxLogWindow(this, _("Logging"));
262 // Create the Tools menu
263 m_tools_menu
= new wxMenu();
264 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
265 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
266 m_tools_menu
->AppendSeparator();
267 m_tools_layout
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Use Layout Zoom"));
268 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
269 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
270 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
271 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
272 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
273 m_tools_menu
->AppendSeparator();
274 m_tools_handle_navigation
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle Navigation"));
275 m_tools_handle_new_window
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle New Windows"));
276 m_tools_menu
->AppendSeparator();
279 m_tools_history_menu
= new wxMenu();
280 wxMenuItem
* clearhist
= m_tools_history_menu
->Append(wxID_ANY
, _("Clear History"));
281 m_tools_enable_history
= m_tools_history_menu
->AppendCheckItem(wxID_ANY
, _("Enable History"));
282 m_tools_history_menu
->AppendSeparator();
284 m_tools_menu
->AppendSubMenu(m_tools_history_menu
, "History");
286 //Create an editing menu
287 wxMenu
* editmenu
= new wxMenu();
288 m_edit_cut
= editmenu
->Append(wxID_ANY
, _("Cut"));
289 m_edit_copy
= editmenu
->Append(wxID_ANY
, _("Copy"));
290 m_edit_paste
= editmenu
->Append(wxID_ANY
, _("Paste"));
291 editmenu
->AppendSeparator();
292 m_edit_undo
= editmenu
->Append(wxID_ANY
, _("Undo"));
293 m_edit_redo
= editmenu
->Append(wxID_ANY
, _("Redo"));
294 editmenu
->AppendSeparator();
295 m_edit_mode
= editmenu
->AppendCheckItem(wxID_ANY
, _("Edit Mode"));
297 m_tools_menu
->AppendSeparator();
298 m_tools_menu
->AppendSubMenu(editmenu
, "Edit");
300 wxMenu
* scroll_menu
= new wxMenu
;
301 m_scroll_line_up
= scroll_menu
->Append(wxID_ANY
, "Line &up");
302 m_scroll_line_down
= scroll_menu
->Append(wxID_ANY
, "Line &down");
303 m_scroll_page_up
= scroll_menu
->Append(wxID_ANY
, "Page u&p");
304 m_scroll_page_down
= scroll_menu
->Append(wxID_ANY
, "Page d&own");
305 m_tools_menu
->AppendSubMenu(scroll_menu
, "Scroll");
307 wxMenuItem
* script
= m_tools_menu
->Append(wxID_ANY
, _("Run Script"));
310 wxMenu
* selection
= new wxMenu();
311 m_selection_clear
= selection
->Append(wxID_ANY
, _("Clear Selection"));
312 m_selection_delete
= selection
->Append(wxID_ANY
, _("Delete Selection"));
313 wxMenuItem
* selectall
= selection
->Append(wxID_ANY
, _("Select All"));
315 editmenu
->AppendSubMenu(selection
, "Selection");
317 wxMenuItem
* loadscheme
= m_tools_menu
->Append(wxID_ANY
, _("Custom Scheme Example"));
319 //By default we want to handle navigation and new windows
320 m_tools_handle_navigation
->Check();
321 m_tools_handle_new_window
->Check();
322 m_tools_enable_history
->Check();
323 if(!m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
))
324 m_tools_layout
->Enable(false);
327 // Connect the toolbar events
328 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
329 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
330 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
331 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
332 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
333 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
334 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
335 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
336 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
337 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
339 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
340 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
342 // Connect the webview events
343 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
344 wxWebViewEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
345 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
346 wxWebViewEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
347 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
348 wxWebViewEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
349 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
350 wxWebViewEventHandler(WebFrame::OnError
), NULL
, this);
351 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
352 wxWebViewEventHandler(WebFrame::OnNewWindow
), NULL
, this);
353 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
354 wxWebViewEventHandler(WebFrame::OnTitleChanged
), NULL
, this);
356 // Connect the menu events
357 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
358 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
359 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
360 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
361 Connect(m_tools_layout
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
362 wxCommandEventHandler(WebFrame::OnZoomLayout
), NULL
, this );
363 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
364 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
365 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
366 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
367 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
368 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
369 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
370 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
371 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
372 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
373 Connect(clearhist
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
374 wxCommandEventHandler(WebFrame::OnClearHistory
), NULL
, this );
375 Connect(m_tools_enable_history
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
376 wxCommandEventHandler(WebFrame::OnEnableHistory
), NULL
, this );
377 Connect(m_edit_cut
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
378 wxCommandEventHandler(WebFrame::OnCut
), NULL
, this );
379 Connect(m_edit_copy
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
380 wxCommandEventHandler(WebFrame::OnCopy
), NULL
, this );
381 Connect(m_edit_paste
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
382 wxCommandEventHandler(WebFrame::OnPaste
), NULL
, this );
383 Connect(m_edit_undo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
384 wxCommandEventHandler(WebFrame::OnUndo
), NULL
, this );
385 Connect(m_edit_redo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
386 wxCommandEventHandler(WebFrame::OnRedo
), NULL
, this );
387 Connect(m_edit_mode
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
388 wxCommandEventHandler(WebFrame::OnMode
), NULL
, this );
389 Connect(m_scroll_line_up
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
390 wxCommandEventHandler(WebFrame::OnScrollLineUp
), NULL
, this );
391 Connect(m_scroll_line_down
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
392 wxCommandEventHandler(WebFrame::OnScrollLineDown
), NULL
, this );
393 Connect(m_scroll_page_up
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
394 wxCommandEventHandler(WebFrame::OnScrollPageUp
), NULL
, this );
395 Connect(m_scroll_page_down
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
396 wxCommandEventHandler(WebFrame::OnScrollPageDown
), NULL
, this );
397 Connect(script
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
398 wxCommandEventHandler(WebFrame::OnRunScript
), NULL
, this );
399 Connect(m_selection_clear
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
400 wxCommandEventHandler(WebFrame::OnClearSelection
), NULL
, this );
401 Connect(m_selection_delete
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
402 wxCommandEventHandler(WebFrame::OnDeleteSelection
), NULL
, this );
403 Connect(selectall
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
404 wxCommandEventHandler(WebFrame::OnSelectAll
), NULL
, this );
405 Connect(loadscheme
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
406 wxCommandEventHandler(WebFrame::OnLoadScheme
), NULL
, this );
408 //Connect the idle events
409 Connect(wxID_ANY
, wxEVT_IDLE
, wxIdleEventHandler(WebFrame::OnIdle
), NULL
, this);
412 WebFrame::~WebFrame()
418 * Method that retrieves the current state from the web control and updates the GUI
419 * the reflect this current state.
421 void WebFrame::UpdateState()
423 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
424 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
426 if (m_browser
->IsBusy())
428 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
432 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
435 SetTitle( m_browser
->GetCurrentTitle() );
436 m_url
->SetValue( m_browser
->GetCurrentURL() );
439 void WebFrame::OnIdle(wxIdleEvent
& WXUNUSED(evt
))
441 if(m_browser
->IsBusy())
443 wxSetCursor(wxCURSOR_ARROWWAIT
);
444 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), true);
448 wxSetCursor(wxNullCursor
);
449 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), false);
454 * Callback invoked when user entered an URL and pressed enter
456 void WebFrame::OnUrl(wxCommandEvent
& WXUNUSED(evt
))
458 m_browser
->LoadURL( m_url
->GetValue() );
459 m_browser
->SetFocus();
464 * Callback invoked when user pressed the "back" button
466 void WebFrame::OnBack(wxCommandEvent
& WXUNUSED(evt
))
473 * Callback invoked when user pressed the "forward" button
475 void WebFrame::OnForward(wxCommandEvent
& WXUNUSED(evt
))
477 m_browser
->GoForward();
482 * Callback invoked when user pressed the "stop" button
484 void WebFrame::OnStop(wxCommandEvent
& WXUNUSED(evt
))
491 * Callback invoked when user pressed the "reload" button
493 void WebFrame::OnReload(wxCommandEvent
& WXUNUSED(evt
))
499 void WebFrame::OnClearHistory(wxCommandEvent
& WXUNUSED(evt
))
501 m_browser
->ClearHistory();
505 void WebFrame::OnEnableHistory(wxCommandEvent
& WXUNUSED(evt
))
507 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
511 void WebFrame::OnCut(wxCommandEvent
& WXUNUSED(evt
))
516 void WebFrame::OnCopy(wxCommandEvent
& WXUNUSED(evt
))
521 void WebFrame::OnPaste(wxCommandEvent
& WXUNUSED(evt
))
526 void WebFrame::OnUndo(wxCommandEvent
& WXUNUSED(evt
))
531 void WebFrame::OnRedo(wxCommandEvent
& WXUNUSED(evt
))
536 void WebFrame::OnMode(wxCommandEvent
& WXUNUSED(evt
))
538 m_browser
->SetEditable(m_edit_mode
->IsChecked());
541 void WebFrame::OnLoadScheme(wxCommandEvent
& WXUNUSED(evt
))
543 wxFileName
helpfile("../help/doc.zip");
544 helpfile
.MakeAbsolute();
545 wxString path
= helpfile
.GetFullPath();
546 //Under MSW we need to flip the slashes
547 path
.Replace("\\", "/");
548 path
= "wxfs:///" + path
+ ";protocol=zip/doc.htm";
549 m_browser
->LoadURL(path
);
553 * Callback invoked when there is a request to load a new page (for instance
554 * when the user clicks a link)
556 void WebFrame::OnNavigationRequest(wxWebViewEvent
& evt
)
558 if(m_info
->IsShown())
563 wxLogMessage("%s", "Navigation request to '" + evt
.GetURL() + "' (target='" +
564 evt
.GetTarget() + "')");
566 wxASSERT(m_browser
->IsBusy());
568 //If we don't want to handle navigation then veto the event and navigation
569 //will not take place, we also need to stop the loading animation
570 if(!m_tools_handle_navigation
->IsChecked())
573 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
582 * Callback invoked when a navigation request was accepted
584 void WebFrame::OnNavigationComplete(wxWebViewEvent
& evt
)
586 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetURL() + "'");
591 * Callback invoked when a page is finished loading
593 void WebFrame::OnDocumentLoaded(wxWebViewEvent
& evt
)
595 //Only notify if the document is the main frame, not a subframe
596 if(evt
.GetURL() == m_browser
->GetCurrentURL())
598 wxLogMessage("%s", "Document loaded; url='" + evt
.GetURL() + "'");
604 * On new window, we veto to stop extra windows appearing
606 void WebFrame::OnNewWindow(wxWebViewEvent
& evt
)
608 wxLogMessage("%s", "New window; url='" + evt
.GetURL() + "'");
610 //If we handle new window events then just load them in this window as we
611 //are a single window browser
612 if(m_tools_handle_new_window
->IsChecked())
613 m_browser
->LoadURL(evt
.GetURL());
618 void WebFrame::OnTitleChanged(wxWebViewEvent
& evt
)
620 SetTitle(evt
.GetString());
621 wxLogMessage("%s", "Title changed; title='" + evt
.GetString() + "'");
625 * Invoked when user selects the "View Source" menu item
627 void WebFrame::OnViewSourceRequest(wxCommandEvent
& WXUNUSED(evt
))
629 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
634 * Invoked when user selects the "Menu" item
636 void WebFrame::OnToolsClicked(wxCommandEvent
& WXUNUSED(evt
))
638 if(m_browser
->GetCurrentURL() == "")
641 m_tools_tiny
->Check(false);
642 m_tools_small
->Check(false);
643 m_tools_medium
->Check(false);
644 m_tools_large
->Check(false);
645 m_tools_largest
->Check(false);
647 wxWebViewZoom zoom
= m_browser
->GetZoom();
650 case wxWEB_VIEW_ZOOM_TINY
:
651 m_tools_tiny
->Check();
653 case wxWEB_VIEW_ZOOM_SMALL
:
654 m_tools_small
->Check();
656 case wxWEB_VIEW_ZOOM_MEDIUM
:
657 m_tools_medium
->Check();
659 case wxWEB_VIEW_ZOOM_LARGE
:
660 m_tools_large
->Check();
662 case wxWEB_VIEW_ZOOM_LARGEST
:
663 m_tools_largest
->Check();
667 m_edit_cut
->Enable(m_browser
->CanCut());
668 m_edit_copy
->Enable(m_browser
->CanCopy());
669 m_edit_paste
->Enable(m_browser
->CanPaste());
671 m_edit_undo
->Enable(m_browser
->CanUndo());
672 m_edit_redo
->Enable(m_browser
->CanRedo());
674 m_selection_clear
->Enable(m_browser
->HasSelection());
675 m_selection_delete
->Enable(m_browser
->HasSelection());
677 //Firstly we clear the existing menu items, then we add the current ones
678 wxMenuHistoryMap::const_iterator it
;
679 for( it
= m_histMenuItems
.begin(); it
!= m_histMenuItems
.end(); ++it
)
681 m_tools_history_menu
->Destroy(it
->first
);
683 m_histMenuItems
.clear();
685 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > back
= m_browser
->GetBackwardHistory();
686 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forward
= m_browser
->GetForwardHistory();
691 for(i
= 0; i
< back
.size(); i
++)
693 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, back
[i
]->GetTitle());
694 m_histMenuItems
[item
->GetId()] = back
[i
];
695 Connect(item
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
696 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
699 wxString title
= m_browser
->GetCurrentTitle();
701 title
= "(untitled)";
702 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, title
);
705 //No need to connect the current item
706 m_histMenuItems
[item
->GetId()] = wxSharedPtr
<wxWebViewHistoryItem
>(new wxWebViewHistoryItem(m_browser
->GetCurrentURL(), m_browser
->GetCurrentTitle()));
708 for(i
= 0; i
< forward
.size(); i
++)
710 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, forward
[i
]->GetTitle());
711 m_histMenuItems
[item
->GetId()] = forward
[i
];
712 Connect(item
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
713 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
716 wxPoint position
= ScreenToClient( wxGetMousePosition() );
717 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
721 * Invoked when user selects the zoom size in the menu
723 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
725 if (evt
.GetId() == m_tools_tiny
->GetId())
727 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
729 else if (evt
.GetId() == m_tools_small
->GetId())
731 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
733 else if (evt
.GetId() == m_tools_medium
->GetId())
735 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
737 else if (evt
.GetId() == m_tools_large
->GetId())
739 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
741 else if (evt
.GetId() == m_tools_largest
->GetId())
743 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
751 void WebFrame::OnZoomLayout(wxCommandEvent
& WXUNUSED(evt
))
753 if(m_tools_layout
->IsChecked())
754 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
756 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
759 void WebFrame::OnHistory(wxCommandEvent
& evt
)
761 m_browser
->LoadHistoryItem(m_histMenuItems
[evt
.GetId()]);
764 void WebFrame::OnRunScript(wxCommandEvent
& WXUNUSED(evt
))
766 wxTextEntryDialog
dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr
, "", wxOK
|wxCANCEL
|wxCENTRE
|wxTE_MULTILINE
);
767 if(dialog
.ShowModal() == wxID_OK
)
769 m_browser
->RunScript(dialog
.GetValue());
773 void WebFrame::OnClearSelection(wxCommandEvent
& WXUNUSED(evt
))
775 m_browser
->ClearSelection();
778 void WebFrame::OnDeleteSelection(wxCommandEvent
& WXUNUSED(evt
))
780 m_browser
->DeleteSelection();
783 void WebFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(evt
))
785 m_browser
->SelectAll();
789 * Callback invoked when a loading error occurs
791 void WebFrame::OnError(wxWebViewEvent
& evt
)
793 wxString errorCategory
;
794 switch (evt
.GetInt())
796 case wxWEB_NAV_ERR_CONNECTION
:
797 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
800 case wxWEB_NAV_ERR_CERTIFICATE
:
801 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
804 case wxWEB_NAV_ERR_AUTH
:
805 errorCategory
= "wxWEB_NAV_ERR_AUTH";
808 case wxWEB_NAV_ERR_SECURITY
:
809 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
812 case wxWEB_NAV_ERR_NOT_FOUND
:
813 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
816 case wxWEB_NAV_ERR_REQUEST
:
817 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
820 case wxWEB_NAV_ERR_USER_CANCELLED
:
821 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
824 case wxWEB_NAV_ERR_OTHER
:
825 errorCategory
= "wxWEB_NAV_ERR_OTHER";
829 wxLogMessage("%s", "Error; url='" + evt
.GetURL() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
831 //Show the info bar with an error
832 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetURL() + "\n" +
833 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
839 * Invoked when user selects "Print" from the menu
841 void WebFrame::OnPrint(wxCommandEvent
& WXUNUSED(evt
))
846 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
847 wxDialog(parent
, wxID_ANY
, "Source Code",
848 wxDefaultPosition
, wxSize(700,500),
849 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
851 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
852 text
->SetMarginWidth(1, 30);
853 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
854 text
->SetText(source
);
856 text
->StyleClearAll();
857 text
->SetLexer(wxSTC_LEX_HTML
);
858 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
859 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
860 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
861 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
862 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
863 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
864 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
865 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
867 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
868 sizer
->Add(text
, 1, wxEXPAND
);