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 OnNavigationRequest(wxWebNavigationEvent
& evt
);
64 void OnNavigationComplete(wxWebNavigationEvent
& evt
);
65 void OnDocumentLoaded(wxWebNavigationEvent
& evt
);
66 void OnNewWindow(wxWebNavigationEvent
& evt
);
67 void OnViewSourceRequest(wxCommandEvent
& evt
);
68 void OnToolsClicked(wxCommandEvent
& evt
);
69 void OnSetZoom(wxCommandEvent
& evt
);
70 void OnError(wxWebNavigationEvent
& evt
);
71 void OnPrint(wxCommandEvent
& evt
);
78 wxToolBarToolBase
* m_toolbar_back
;
79 wxToolBarToolBase
* m_toolbar_forward
;
80 wxToolBarToolBase
* m_toolbar_stop
;
81 wxToolBarToolBase
* m_toolbar_reload
;
82 wxToolBarToolBase
* m_toolbar_tools
;
85 wxMenuItem
* m_tools_tiny
;
86 wxMenuItem
* m_tools_small
;
87 wxMenuItem
* m_tools_medium
;
88 wxMenuItem
* m_tools_large
;
89 wxMenuItem
* m_tools_largest
;
90 wxMenuItem
* m_tools_handle_navigation
;
91 wxMenuItem
* m_tools_handle_new_window
;
94 int m_animation_angle
;
97 wxStaticText
* m_info_text
;
100 class SourceViewDialog
: public wxDialog
103 SourceViewDialog(wxWindow
* parent
, wxString source
);
106 IMPLEMENT_APP(WebApp
)
108 // ============================================================================
110 // ============================================================================
112 bool WebApp::OnInit()
114 if ( !wxApp::OnInit() )
117 WebFrame
*frame
= new WebFrame();
123 WebFrame::WebFrame() : wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
125 // set the frame icon
126 SetIcon(wxICON(sample
));
127 SetTitle("wxWebView Sample");
130 m_animation_angle
= 0;
133 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
135 // Create the toolbar
136 m_toolbar
= CreateToolBar(wxTB_TEXT
);
137 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
139 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
140 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
142 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
144 wxBitmap stop
= wxBitmap(stop_xpm
);
147 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
149 wxBitmap refresh
= wxBitmap(refresh_xpm
);
152 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), back
);
153 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), forward
);
154 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), stop
);
155 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), refresh
);
156 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
157 m_toolbar
->AddControl(m_url
, _("URL"));
158 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
160 m_toolbar
->Realize();
162 // Create the info panel
163 m_info
= new wxInfoBar(this);
164 topsizer
->Add(m_info
, wxSizerFlags().Expand());
166 // Create the webview
167 m_browser
= wxWebView::New(this, wxID_ANY
, "http://www.wxwidgets.org");
168 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
172 //Set a more sensible size for web browsing
173 SetSize(wxSize(800, 600));
175 // Create a log window
176 new wxLogWindow(this, _("Logging"));
178 // Create the Tools menu
179 m_tools_menu
= new wxMenu();
180 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
181 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
182 m_tools_menu
->AppendSeparator();
183 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
184 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
185 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
186 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
187 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
188 m_tools_menu
->AppendSeparator();
189 m_tools_handle_navigation
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle Navigation"));
190 m_tools_handle_new_window
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle New Windows"));
192 //By default we want to handle navigation and new windows
193 m_tools_handle_navigation
->Check();
194 m_tools_handle_new_window
->Check();
197 // Connect the toolbar events
198 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
199 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
200 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
201 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
202 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
203 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
204 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
205 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
206 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
207 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
209 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
210 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
212 // Connect the webview events
213 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
214 wxWebNavigationEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
215 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
216 wxWebNavigationEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
217 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
218 wxWebNavigationEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
219 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
220 wxWebNavigationEventHandler(WebFrame::OnError
), NULL
, this);
221 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
222 wxWebNavigationEventHandler(WebFrame::OnNewWindow
), NULL
, this);
224 // Connect the menu events
225 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
226 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
227 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
228 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
229 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
230 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
231 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
232 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
233 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
234 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
235 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
236 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
237 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
238 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
241 void WebFrame::OnAnimationTimer(wxTimerEvent
& evt
)
243 m_animation_angle
+= 15;
244 if (m_animation_angle
> 360) m_animation_angle
-= 360;
246 wxBitmap
image(24, 24);
249 dc
.SelectObject(image
);
250 dc
.SetBackground(wxBrush(wxColour(255,0,255)));
253 if (m_animation_angle
>= 0 && m_animation_angle
<= 180)
255 dc
.SetBrush(*wxYELLOW_BRUSH
);
256 dc
.SetPen(*wxYELLOW_PEN
);
257 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
258 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
261 dc
.DrawBitmap(wxBitmap(wxlogo_xpm
), 0, 0, true);
263 if (m_animation_angle
> 180)
265 dc
.SetBrush(*wxYELLOW_BRUSH
);
266 dc
.SetPen(*wxYELLOW_PEN
);
267 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
268 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
271 image
.SetMask(new wxMask(image
, wxColour(255,0,255)));
272 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), image
);
276 * Method that retrieves the current state from the web control and updates the GUI
277 * the reflect this current state.
279 void WebFrame::UpdateState()
281 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
282 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
284 if (m_browser
->IsBusy())
288 m_timer
= new wxTimer(this);
289 this->Connect(wxEVT_TIMER
, wxTimerEventHandler(WebFrame::OnAnimationTimer
), NULL
, this);
291 m_timer
->Start(100); // start animation timer
293 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
297 if (m_timer
!= NULL
) m_timer
->Stop(); // stop animation timer
298 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), wxBitmap(wxlogo_xpm
));
299 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
302 SetTitle( m_browser
->GetCurrentTitle() );
303 m_url
->SetValue( m_browser
->GetCurrentURL() );
307 * Callback invoked when user entered an URL and pressed enter
309 void WebFrame::OnUrl(wxCommandEvent
& evt
)
311 m_browser
->LoadUrl( m_url
->GetValue() );
316 * Callback invoked when user pressed the "back" button
318 void WebFrame::OnBack(wxCommandEvent
& evt
)
325 * Callback invoked when user pressed the "forward" button
327 void WebFrame::OnForward(wxCommandEvent
& evt
)
329 m_browser
->GoForward();
334 * Callback invoked when user pressed the "stop" button
336 void WebFrame::OnStop(wxCommandEvent
& evt
)
343 * Callback invoked when user pressed the "reload" button
345 void WebFrame::OnReload(wxCommandEvent
& evt
)
352 * Callback invoked when there is a request to load a new page (for instance
353 * when the user clicks a link)
355 void WebFrame::OnNavigationRequest(wxWebNavigationEvent
& evt
)
357 wxLogMessage("%s", "Navigation request to '" + evt
.GetHref() + "' (target='" +
358 evt
.GetTarget() + "')");
360 wxASSERT(m_browser
->IsBusy());
362 //If we don't want to handle navigation then veto the event and navigation
363 //will not take place
364 if(!m_tools_handle_navigation
->IsChecked())
371 * Callback invoked when a navigation request was accepted
373 void WebFrame::OnNavigationComplete(wxWebNavigationEvent
& evt
)
375 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetHref() + "'");
380 * Callback invoked when a page is finished loading
382 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent
& evt
)
384 wxLogMessage("%s", "Document loaded; url='" + evt
.GetHref() + "'");
389 * On new window, we veto to stop extra windows appearing
391 void WebFrame::OnNewWindow(wxWebNavigationEvent
& evt
)
393 wxLogMessage("%s", "New window; url='" + evt
.GetHref() + "'");
395 //If we handle new window events then just load them in this window as we
396 //are a single window browser
397 if(m_tools_handle_new_window
->IsChecked())
398 m_browser
->LoadUrl(evt
.GetHref());
400 //We always veto because we handle the event, otherwise under windows a new
401 //internet explorer windowis created
408 * Invoked when user selects the "View Source" menu item
410 void WebFrame::OnViewSourceRequest(wxCommandEvent
& evt
)
412 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
417 * Invoked when user selects the "Menu" item
419 void WebFrame::OnToolsClicked(wxCommandEvent
& evt
)
421 if(m_browser
->GetCurrentURL() == "")
424 m_tools_tiny
->Check(false);
425 m_tools_small
->Check(false);
426 m_tools_medium
->Check(false);
427 m_tools_large
->Check(false);
428 m_tools_largest
->Check(false);
430 wxWebViewZoom zoom
= m_browser
->GetZoom();
433 case wxWEB_VIEW_ZOOM_TINY
:
434 m_tools_tiny
->Check();
436 case wxWEB_VIEW_ZOOM_SMALL
:
437 m_tools_small
->Check();
439 case wxWEB_VIEW_ZOOM_MEDIUM
:
440 m_tools_medium
->Check();
442 case wxWEB_VIEW_ZOOM_LARGE
:
443 m_tools_large
->Check();
445 case wxWEB_VIEW_ZOOM_LARGEST
:
446 m_tools_largest
->Check();
450 wxPoint position
= ScreenToClient( wxGetMousePosition() );
451 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
455 * Invoked when user selects the zoom size in the menu
457 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
459 if (evt
.GetId() == m_tools_tiny
->GetId())
461 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
463 else if (evt
.GetId() == m_tools_small
->GetId())
465 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
467 else if (evt
.GetId() == m_tools_medium
->GetId())
469 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
471 else if (evt
.GetId() == m_tools_large
->GetId())
473 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
475 else if (evt
.GetId() == m_tools_largest
->GetId())
477 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
486 * Callback invoked when a loading error occurs
488 void WebFrame::OnError(wxWebNavigationEvent
& evt
)
490 wxString errorCategory
;
491 switch (evt
.GetInt())
493 case wxWEB_NAV_ERR_CONNECTION
:
494 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
497 case wxWEB_NAV_ERR_CERTIFICATE
:
498 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
501 case wxWEB_NAV_ERR_AUTH
:
502 errorCategory
= "wxWEB_NAV_ERR_AUTH";
505 case wxWEB_NAV_ERR_SECURITY
:
506 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
509 case wxWEB_NAV_ERR_NOT_FOUND
:
510 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
513 case wxWEB_NAV_ERR_REQUEST
:
514 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
517 case wxWEB_NAV_ERR_USER_CANCELLED
:
518 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
521 case wxWEB_NAV_ERR_OTHER
:
522 errorCategory
= "wxWEB_NAV_ERR_OTHER";
526 wxLogMessage("Error; url='" + evt
.GetHref() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
528 //Show the info bar with an error
529 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetHref() + "\n" +
530 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
536 * Invoked when user selects "Print" from the menu
538 void WebFrame::OnPrint(wxCommandEvent
& evt
)
543 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
544 wxDialog(parent
, wxID_ANY
, "Source Code",
545 wxDefaultPosition
, wxSize(700,500),
546 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
548 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
549 text
->SetMarginWidth(1, 30);
550 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
551 text
->SetText(source
);
553 text
->StyleClearAll();
554 text
->SetLexer(wxSTC_LEX_HTML
);
555 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
556 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
557 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
558 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
559 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
560 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
561 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
562 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
564 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
565 sizer
->Add(text
, 1, wxEXPAND
);