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 // Create a log window
173 new wxLogWindow(this, _("Logging"));
175 // Create the Tools menu
176 m_tools_menu
= new wxMenu();
177 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
178 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
179 m_tools_menu
->AppendSeparator();
180 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
181 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
182 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
183 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
184 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
185 m_tools_menu
->AppendSeparator();
186 m_tools_handle_navigation
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle Navigation"));
187 m_tools_handle_new_window
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle New Windows"));
189 //By default we want to handle navigation and new windows
190 m_tools_handle_navigation
->Check();
191 m_tools_handle_new_window
->Check();
194 // Connect the toolbar events
195 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
196 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
197 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
198 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
199 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
200 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
201 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
202 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
203 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
204 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
206 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
207 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
209 // Connect the webview events
210 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
211 wxWebNavigationEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
212 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
213 wxWebNavigationEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
214 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
215 wxWebNavigationEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
216 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
217 wxWebNavigationEventHandler(WebFrame::OnError
), NULL
, this);
218 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
219 wxWebNavigationEventHandler(WebFrame::OnNewWindow
), NULL
, this);
221 // Connect the menu events
222 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
223 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
224 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
225 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
226 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
227 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
228 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
229 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
230 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
231 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
232 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
233 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
234 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
235 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
238 void WebFrame::OnAnimationTimer(wxTimerEvent
& evt
)
240 m_animation_angle
+= 15;
241 if (m_animation_angle
> 360) m_animation_angle
-= 360;
243 wxBitmap
image(24, 24);
246 dc
.SelectObject(image
);
247 dc
.SetBackground(wxBrush(wxColour(255,0,255)));
250 if (m_animation_angle
>= 0 && m_animation_angle
<= 180)
252 dc
.SetBrush(*wxYELLOW_BRUSH
);
253 dc
.SetPen(*wxYELLOW_PEN
);
254 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
255 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
258 dc
.DrawBitmap(wxBitmap(wxlogo_xpm
), 0, 0, true);
260 if (m_animation_angle
> 180)
262 dc
.SetBrush(*wxYELLOW_BRUSH
);
263 dc
.SetPen(*wxYELLOW_PEN
);
264 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
265 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
268 image
.SetMask(new wxMask(image
, wxColour(255,0,255)));
269 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), image
);
273 * Method that retrieves the current state from the web control and updates the GUI
274 * the reflect this current state.
276 void WebFrame::UpdateState()
278 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
279 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
281 if (m_browser
->IsBusy())
285 m_timer
= new wxTimer(this);
286 this->Connect(wxEVT_TIMER
, wxTimerEventHandler(WebFrame::OnAnimationTimer
), NULL
, this);
288 m_timer
->Start(100); // start animation timer
290 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
294 if (m_timer
!= NULL
) m_timer
->Stop(); // stop animation timer
295 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), wxBitmap(wxlogo_xpm
));
296 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
299 SetTitle( m_browser
->GetCurrentTitle() );
300 m_url
->SetValue( m_browser
->GetCurrentURL() );
304 * Callback invoked when user entered an URL and pressed enter
306 void WebFrame::OnUrl(wxCommandEvent
& evt
)
308 m_browser
->LoadUrl( m_url
->GetValue() );
313 * Callback invoked when user pressed the "back" button
315 void WebFrame::OnBack(wxCommandEvent
& evt
)
322 * Callback invoked when user pressed the "forward" button
324 void WebFrame::OnForward(wxCommandEvent
& evt
)
326 m_browser
->GoForward();
331 * Callback invoked when user pressed the "stop" button
333 void WebFrame::OnStop(wxCommandEvent
& evt
)
340 * Callback invoked when user pressed the "reload" button
342 void WebFrame::OnReload(wxCommandEvent
& evt
)
349 * Callback invoked when there is a request to load a new page (for instance
350 * when the user clicks a link)
352 void WebFrame::OnNavigationRequest(wxWebNavigationEvent
& evt
)
354 wxLogMessage("%s", "Navigation request to '" + evt
.GetHref() + "' (target='" +
355 evt
.GetTarget() + "')");
357 wxASSERT(m_browser
->IsBusy());
359 //If we don't want to handle navigation then veto the event and navigation
360 //will not take place
361 if(!m_tools_handle_navigation
->IsChecked())
368 * Callback invoked when a navigation request was accepted
370 void WebFrame::OnNavigationComplete(wxWebNavigationEvent
& evt
)
372 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetHref() + "'");
377 * Callback invoked when a page is finished loading
379 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent
& evt
)
381 wxLogMessage("%s", "Document loaded; url='" + evt
.GetHref() + "'");
386 * On new window, we veto to stop extra windows appearing
388 void WebFrame::OnNewWindow(wxWebNavigationEvent
& evt
)
390 wxLogMessage("%s", "New window; url='" + evt
.GetHref() + "'");
392 //If we handle new window events then just load them in this window as we
393 //are a single window browser
394 if(m_tools_handle_new_window
->IsChecked())
395 m_browser
->LoadUrl(evt
.GetHref());
397 //We always veto because we handle the event, otherwise under windows a new
398 //internet explorer windowis created
405 * Invoked when user selects the "View Source" menu item
407 void WebFrame::OnViewSourceRequest(wxCommandEvent
& evt
)
409 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
414 * Invoked when user selects the "Menu" item
416 void WebFrame::OnToolsClicked(wxCommandEvent
& evt
)
418 if(m_browser
->GetCurrentURL() == "")
421 m_tools_tiny
->Check(false);
422 m_tools_small
->Check(false);
423 m_tools_medium
->Check(false);
424 m_tools_large
->Check(false);
425 m_tools_largest
->Check(false);
427 wxWebViewZoom zoom
= m_browser
->GetZoom();
430 case wxWEB_VIEW_ZOOM_TINY
:
431 m_tools_tiny
->Check();
433 case wxWEB_VIEW_ZOOM_SMALL
:
434 m_tools_small
->Check();
436 case wxWEB_VIEW_ZOOM_MEDIUM
:
437 m_tools_medium
->Check();
439 case wxWEB_VIEW_ZOOM_LARGE
:
440 m_tools_large
->Check();
442 case wxWEB_VIEW_ZOOM_LARGEST
:
443 m_tools_largest
->Check();
447 wxPoint position
= ScreenToClient( wxGetMousePosition() );
448 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
452 * Invoked when user selects the zoom size in the menu
454 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
456 if (evt
.GetId() == m_tools_tiny
->GetId())
458 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
460 else if (evt
.GetId() == m_tools_small
->GetId())
462 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
464 else if (evt
.GetId() == m_tools_medium
->GetId())
466 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
468 else if (evt
.GetId() == m_tools_large
->GetId())
470 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
472 else if (evt
.GetId() == m_tools_largest
->GetId())
474 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
483 * Callback invoked when a loading error occurs
485 void WebFrame::OnError(wxWebNavigationEvent
& evt
)
487 wxString errorCategory
;
488 switch (evt
.GetInt())
490 case wxWEB_NAV_ERR_CONNECTION
:
491 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
494 case wxWEB_NAV_ERR_CERTIFICATE
:
495 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
498 case wxWEB_NAV_ERR_AUTH
:
499 errorCategory
= "wxWEB_NAV_ERR_AUTH";
502 case wxWEB_NAV_ERR_SECURITY
:
503 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
506 case wxWEB_NAV_ERR_NOT_FOUND
:
507 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
510 case wxWEB_NAV_ERR_REQUEST
:
511 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
514 case wxWEB_NAV_ERR_USER_CANCELLED
:
515 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
518 case wxWEB_NAV_ERR_OTHER
:
519 errorCategory
= "wxWEB_NAV_ERR_OTHER";
523 wxLogMessage("Error; url='" + evt
.GetHref() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
525 //Show the info bar with an error
526 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetHref() + "\n" +
527 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
533 * Invoked when user selects "Print" from the menu
535 void WebFrame::OnPrint(wxCommandEvent
& evt
)
540 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
541 wxDialog(parent
, wxID_ANY
, "Source Code",
542 wxDefaultPosition
, wxSize(700,500),
543 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
545 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
546 text
->SetMarginWidth(1, 30);
547 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
548 text
->SetText(source
);
550 text
->StyleClearAll();
551 text
->SetLexer(wxSTC_LEX_HTML
);
552 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
553 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
554 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
555 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
556 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
557 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
558 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
559 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
561 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
562 sizer
->Add(text
, 1, wxEXPAND
);