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 #include <wx/artprov.h>
26 #include <wx/notifmsg.h>
27 #include <wx/settings.h>
28 #include <wx/webview.h>
29 #include <wx/infobar.h>
31 #if !defined(__WXMSW__) && !defined(__WXPM__)
32 #include "../sample.xpm"
36 #include <wx/stc/stc.h>
38 #error "wxStyledTextControl is needed by this sample"
42 #include "refresh.xpm"
45 class WebApp
: public wxApp
48 virtual bool OnInit();
51 class WebFrame
: public wxFrame
56 void OnAnimationTimer(wxTimerEvent
& evt
);
58 void OnUrl(wxCommandEvent
& evt
);
59 void OnBack(wxCommandEvent
& evt
);
60 void OnForward(wxCommandEvent
& evt
);
61 void OnStop(wxCommandEvent
& evt
);
62 void OnReload(wxCommandEvent
& evt
);
63 void OnClearHistory(wxCommandEvent
& evt
);
64 void OnEnableHistory(wxCommandEvent
& evt
);
65 void OnNavigationRequest(wxWebNavigationEvent
& evt
);
66 void OnNavigationComplete(wxWebNavigationEvent
& evt
);
67 void OnDocumentLoaded(wxWebNavigationEvent
& evt
);
68 void OnNewWindow(wxWebNavigationEvent
& evt
);
69 void OnViewSourceRequest(wxCommandEvent
& evt
);
70 void OnToolsClicked(wxCommandEvent
& evt
);
71 void OnSetZoom(wxCommandEvent
& evt
);
72 void OnError(wxWebNavigationEvent
& evt
);
73 void OnPrint(wxCommandEvent
& evt
);
74 void OnCut(wxCommandEvent
& evt
);
75 void OnCopy(wxCommandEvent
& evt
);
76 void OnPaste(wxCommandEvent
& evt
);
83 wxToolBarToolBase
* m_toolbar_back
;
84 wxToolBarToolBase
* m_toolbar_forward
;
85 wxToolBarToolBase
* m_toolbar_stop
;
86 wxToolBarToolBase
* m_toolbar_reload
;
87 wxToolBarToolBase
* m_toolbar_tools
;
90 wxMenuItem
* m_tools_tiny
;
91 wxMenuItem
* m_tools_small
;
92 wxMenuItem
* m_tools_medium
;
93 wxMenuItem
* m_tools_large
;
94 wxMenuItem
* m_tools_largest
;
95 wxMenuItem
* m_tools_handle_navigation
;
96 wxMenuItem
* m_tools_handle_new_window
;
97 wxMenuItem
* m_tools_enable_history
;
98 wxMenuItem
* m_edit_cut
;
99 wxMenuItem
* m_edit_copy
;
100 wxMenuItem
* m_edit_paste
;
103 int m_animation_angle
;
106 wxStaticText
* m_info_text
;
109 class SourceViewDialog
: public wxDialog
112 SourceViewDialog(wxWindow
* parent
, wxString source
);
115 IMPLEMENT_APP(WebApp
)
117 // ============================================================================
119 // ============================================================================
121 bool WebApp::OnInit()
123 if ( !wxApp::OnInit() )
126 WebFrame
*frame
= new WebFrame();
132 WebFrame::WebFrame() : wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
134 // set the frame icon
135 SetIcon(wxICON(sample
));
136 SetTitle("wxWebView Sample");
139 m_animation_angle
= 0;
142 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
144 // Create the toolbar
145 m_toolbar
= CreateToolBar(wxTB_TEXT
);
146 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
148 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
149 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
151 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
153 wxBitmap stop
= wxBitmap(stop_xpm
);
156 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
158 wxBitmap refresh
= wxBitmap(refresh_xpm
);
161 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), back
);
162 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), forward
);
163 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), stop
);
164 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), refresh
);
165 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
166 m_toolbar
->AddControl(m_url
, _("URL"));
167 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
169 m_toolbar
->Realize();
171 // Create the info panel
172 m_info
= new wxInfoBar(this);
173 topsizer
->Add(m_info
, wxSizerFlags().Expand());
175 // Create the webview
176 m_browser
= wxWebView::New(this, wxID_ANY
, "http://www.wxwidgets.org");
177 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
181 //Set a more sensible size for web browsing
182 SetSize(wxSize(800, 600));
184 // Create a log window
185 new wxLogWindow(this, _("Logging"));
187 // Create the Tools menu
188 m_tools_menu
= new wxMenu();
189 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
190 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
191 m_tools_menu
->AppendSeparator();
192 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
193 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
194 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
195 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
196 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
197 m_tools_menu
->AppendSeparator();
198 m_tools_handle_navigation
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle Navigation"));
199 m_tools_handle_new_window
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle New Windows"));
200 m_tools_menu
->AppendSeparator();
201 wxMenuItem
* clearhist
= m_tools_menu
->Append(wxID_ANY
, _("Clear History"));
202 m_tools_enable_history
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Enable History"));
204 //Create an editing menu
205 wxMenu
* editmenu
= new wxMenu();
206 m_edit_cut
= editmenu
->Append(wxID_ANY
, _("Cut"));
207 m_edit_copy
= editmenu
->Append(wxID_ANY
, _("Copy"));
208 m_edit_paste
= editmenu
->Append(wxID_ANY
, _("Paste"));
210 m_tools_menu
->AppendSeparator();
211 m_tools_menu
->AppendSubMenu(editmenu
, "Edit");
213 //By default we want to handle navigation and new windows
214 m_tools_handle_navigation
->Check();
215 m_tools_handle_new_window
->Check();
216 m_tools_enable_history
->Check();
219 // Connect the toolbar events
220 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
221 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
222 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
223 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
224 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
225 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
226 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
227 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
228 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
229 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
231 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
232 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
234 // Connect the webview events
235 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
236 wxWebNavigationEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
237 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
238 wxWebNavigationEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
239 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
240 wxWebNavigationEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
241 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
242 wxWebNavigationEventHandler(WebFrame::OnError
), NULL
, this);
243 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
244 wxWebNavigationEventHandler(WebFrame::OnNewWindow
), NULL
, this);
246 // Connect the menu events
247 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
248 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
249 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
250 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
251 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
252 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
253 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
254 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
255 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
256 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
257 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
258 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
259 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
260 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
261 Connect(clearhist
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
262 wxCommandEventHandler(WebFrame::OnClearHistory
), NULL
, this );
263 Connect(m_tools_enable_history
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
264 wxCommandEventHandler(WebFrame::OnEnableHistory
), NULL
, this );
265 Connect(m_edit_cut
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
266 wxCommandEventHandler(WebFrame::OnCut
), NULL
, this );
267 Connect(m_edit_copy
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
268 wxCommandEventHandler(WebFrame::OnCopy
), NULL
, this );
269 Connect(m_edit_paste
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
270 wxCommandEventHandler(WebFrame::OnPaste
), NULL
, this );
273 void WebFrame::OnAnimationTimer(wxTimerEvent
& evt
)
275 m_animation_angle
+= 15;
276 if (m_animation_angle
> 360) m_animation_angle
-= 360;
278 wxBitmap
image(24, 24);
281 dc
.SelectObject(image
);
282 dc
.SetBackground(wxBrush(wxColour(255,0,255)));
285 if (m_animation_angle
>= 0 && m_animation_angle
<= 180)
287 dc
.SetBrush(*wxYELLOW_BRUSH
);
288 dc
.SetPen(*wxYELLOW_PEN
);
289 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
290 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
293 dc
.DrawBitmap(wxBitmap(wxlogo_xpm
), 0, 0, true);
295 if (m_animation_angle
> 180)
297 dc
.SetBrush(*wxYELLOW_BRUSH
);
298 dc
.SetPen(*wxYELLOW_PEN
);
299 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
300 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
303 image
.SetMask(new wxMask(image
, wxColour(255,0,255)));
304 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), image
);
308 * Method that retrieves the current state from the web control and updates the GUI
309 * the reflect this current state.
311 void WebFrame::UpdateState()
313 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
314 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
316 if (m_browser
->IsBusy())
320 m_timer
= new wxTimer(this);
321 this->Connect(wxEVT_TIMER
, wxTimerEventHandler(WebFrame::OnAnimationTimer
), NULL
, this);
323 m_timer
->Start(100); // start animation timer
325 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
329 if (m_timer
!= NULL
) m_timer
->Stop(); // stop animation timer
330 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), wxBitmap(wxlogo_xpm
));
331 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
334 SetTitle( m_browser
->GetCurrentTitle() );
335 m_url
->SetValue( m_browser
->GetCurrentURL() );
339 * Callback invoked when user entered an URL and pressed enter
341 void WebFrame::OnUrl(wxCommandEvent
& evt
)
343 m_browser
->LoadUrl( m_url
->GetValue() );
348 * Callback invoked when user pressed the "back" button
350 void WebFrame::OnBack(wxCommandEvent
& evt
)
357 * Callback invoked when user pressed the "forward" button
359 void WebFrame::OnForward(wxCommandEvent
& evt
)
361 m_browser
->GoForward();
366 * Callback invoked when user pressed the "stop" button
368 void WebFrame::OnStop(wxCommandEvent
& evt
)
375 * Callback invoked when user pressed the "reload" button
377 void WebFrame::OnReload(wxCommandEvent
& evt
)
383 void WebFrame::OnClearHistory(wxCommandEvent
& evt
)
385 m_browser
->ClearHistory();
389 void WebFrame::OnEnableHistory(wxCommandEvent
& evt
)
391 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
395 void WebFrame::OnCut(wxCommandEvent
& evt
)
400 void WebFrame::OnCopy(wxCommandEvent
& evt
)
405 void WebFrame::OnPaste(wxCommandEvent
& evt
)
411 * Callback invoked when there is a request to load a new page (for instance
412 * when the user clicks a link)
414 void WebFrame::OnNavigationRequest(wxWebNavigationEvent
& evt
)
416 wxLogMessage("%s", "Navigation request to '" + evt
.GetHref() + "' (target='" +
417 evt
.GetTarget() + "')");
419 wxASSERT(m_browser
->IsBusy());
421 //If we don't want to handle navigation then veto the event and navigation
422 //will not take place
423 if(!m_tools_handle_navigation
->IsChecked())
430 * Callback invoked when a navigation request was accepted
432 void WebFrame::OnNavigationComplete(wxWebNavigationEvent
& evt
)
434 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetHref() + "'");
439 * Callback invoked when a page is finished loading
441 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent
& evt
)
443 wxLogMessage("%s", "Document loaded; url='" + evt
.GetHref() + "'");
448 * On new window, we veto to stop extra windows appearing
450 void WebFrame::OnNewWindow(wxWebNavigationEvent
& evt
)
452 wxLogMessage("%s", "New window; url='" + evt
.GetHref() + "'");
454 //If we handle new window events then just load them in this window as we
455 //are a single window browser
456 if(m_tools_handle_new_window
->IsChecked())
457 m_browser
->LoadUrl(evt
.GetHref());
459 //We always veto because we handle the event, otherwise under windows a new
460 //internet explorer windowis created
467 * Invoked when user selects the "View Source" menu item
469 void WebFrame::OnViewSourceRequest(wxCommandEvent
& evt
)
471 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
476 * Invoked when user selects the "Menu" item
478 void WebFrame::OnToolsClicked(wxCommandEvent
& evt
)
480 if(m_browser
->GetCurrentURL() == "")
483 m_tools_tiny
->Check(false);
484 m_tools_small
->Check(false);
485 m_tools_medium
->Check(false);
486 m_tools_large
->Check(false);
487 m_tools_largest
->Check(false);
489 wxWebViewZoom zoom
= m_browser
->GetZoom();
492 case wxWEB_VIEW_ZOOM_TINY
:
493 m_tools_tiny
->Check();
495 case wxWEB_VIEW_ZOOM_SMALL
:
496 m_tools_small
->Check();
498 case wxWEB_VIEW_ZOOM_MEDIUM
:
499 m_tools_medium
->Check();
501 case wxWEB_VIEW_ZOOM_LARGE
:
502 m_tools_large
->Check();
504 case wxWEB_VIEW_ZOOM_LARGEST
:
505 m_tools_largest
->Check();
509 m_edit_cut
->Enable(m_browser
->CanCut());
510 m_edit_copy
->Enable(m_browser
->CanCopy());
511 m_edit_paste
->Enable(m_browser
->CanPaste());
513 wxPoint position
= ScreenToClient( wxGetMousePosition() );
514 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
518 * Invoked when user selects the zoom size in the menu
520 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
522 if (evt
.GetId() == m_tools_tiny
->GetId())
524 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
526 else if (evt
.GetId() == m_tools_small
->GetId())
528 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
530 else if (evt
.GetId() == m_tools_medium
->GetId())
532 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
534 else if (evt
.GetId() == m_tools_large
->GetId())
536 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
538 else if (evt
.GetId() == m_tools_largest
->GetId())
540 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
549 * Callback invoked when a loading error occurs
551 void WebFrame::OnError(wxWebNavigationEvent
& evt
)
553 wxString errorCategory
;
554 switch (evt
.GetInt())
556 case wxWEB_NAV_ERR_CONNECTION
:
557 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
560 case wxWEB_NAV_ERR_CERTIFICATE
:
561 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
564 case wxWEB_NAV_ERR_AUTH
:
565 errorCategory
= "wxWEB_NAV_ERR_AUTH";
568 case wxWEB_NAV_ERR_SECURITY
:
569 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
572 case wxWEB_NAV_ERR_NOT_FOUND
:
573 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
576 case wxWEB_NAV_ERR_REQUEST
:
577 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
580 case wxWEB_NAV_ERR_USER_CANCELLED
:
581 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
584 case wxWEB_NAV_ERR_OTHER
:
585 errorCategory
= "wxWEB_NAV_ERR_OTHER";
589 wxLogMessage("Error; url='" + evt
.GetHref() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
591 //Show the info bar with an error
592 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetHref() + "\n" +
593 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
599 * Invoked when user selects "Print" from the menu
601 void WebFrame::OnPrint(wxCommandEvent
& evt
)
606 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
607 wxDialog(parent
, wxID_ANY
, "Source Code",
608 wxDefaultPosition
, wxSize(700,500),
609 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
611 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
612 text
->SetMarginWidth(1, 30);
613 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
614 text
->SetText(source
);
616 text
->StyleClearAll();
617 text
->SetLexer(wxSTC_LEX_HTML
);
618 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
619 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
620 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
621 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
622 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
623 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
624 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
625 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
627 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
628 sizer
->Add(text
, 1, wxEXPAND
);