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
;
92 int m_animation_angle
;
95 wxStaticText
* m_info_text
;
98 class SourceViewDialog
: public wxDialog
101 SourceViewDialog(wxWindow
* parent
, wxString source
);
104 IMPLEMENT_APP(WebApp
)
106 // ============================================================================
108 // ============================================================================
110 bool WebApp::OnInit()
112 if ( !wxApp::OnInit() )
115 WebFrame
*frame
= new WebFrame();
121 WebFrame::WebFrame() : wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
123 // set the frame icon
124 SetIcon(wxICON(sample
));
125 SetTitle("wxWebView Sample");
128 m_animation_angle
= 0;
131 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
133 // Create the toolbar
134 m_toolbar
= CreateToolBar(wxTB_TEXT
);
135 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
137 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
138 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
140 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
142 wxBitmap stop
= wxBitmap(stop_xpm
);
145 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
147 wxBitmap refresh
= wxBitmap(refresh_xpm
);
150 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), back
);
151 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), forward
);
152 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), stop
);
153 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), refresh
);
154 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
155 m_toolbar
->AddControl(m_url
, _("URL"));
156 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
158 m_toolbar
->Realize();
160 // Create the info panel
161 m_info
= new wxInfoBar(this);
162 topsizer
->Add(m_info
, wxSizerFlags().Expand());
164 // Create the webview
165 m_browser
= wxWebView::New(this, wxID_ANY
, "http://www.wxwidgets.org");
166 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
170 // Create a log window
171 new wxLogWindow(this, _("Logging"));
173 // Create the Tools menu
174 m_tools_menu
= new wxMenu();
175 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
176 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
177 m_tools_menu
->AppendSeparator();
178 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
179 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
180 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
181 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
182 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
185 // Connect the toolbar events
186 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
187 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
188 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
189 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
190 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
191 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
192 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
193 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
194 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
195 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
197 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
198 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
200 // Connect the webview events
201 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
202 wxWebNavigationEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
203 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
204 wxWebNavigationEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
205 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
206 wxWebNavigationEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
207 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
208 wxWebNavigationEventHandler(WebFrame::OnError
), NULL
, this);
209 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
210 wxWebNavigationEventHandler(WebFrame::OnNewWindow
), NULL
, this);
212 // Connect the menu events
213 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
214 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
215 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
216 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
217 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
218 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
219 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
220 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
221 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
222 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
223 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
224 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
225 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
226 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
229 void WebFrame::OnAnimationTimer(wxTimerEvent
& evt
)
231 m_animation_angle
+= 15;
232 if (m_animation_angle
> 360) m_animation_angle
-= 360;
234 wxBitmap
image(32, 32);
237 dc
.SelectObject(image
);
238 dc
.SetBackground(wxBrush(wxColour(255,0,255)));
241 if (m_animation_angle
>= 0 && m_animation_angle
<= 180)
243 dc
.SetBrush(*wxYELLOW_BRUSH
);
244 dc
.SetPen(*wxYELLOW_PEN
);
245 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
246 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
249 dc
.DrawBitmap(wxBitmap(wxlogo_xpm
), 0, 0, true);
251 if (m_animation_angle
> 180)
253 dc
.SetBrush(*wxYELLOW_BRUSH
);
254 dc
.SetPen(*wxYELLOW_PEN
);
255 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
256 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
259 image
.SetMask(new wxMask(image
, wxColour(255,0,255)));
260 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), image
);
264 * Method that retrieves the current state from the web control and updates the GUI
265 * the reflect this current state.
267 void WebFrame::UpdateState()
269 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
270 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
272 if (m_browser
->IsBusy())
276 m_timer
= new wxTimer(this);
277 this->Connect(wxEVT_TIMER
, wxTimerEventHandler(WebFrame::OnAnimationTimer
), NULL
, this);
279 m_timer
->Start(100); // start animation timer
281 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
285 if (m_timer
!= NULL
) m_timer
->Stop(); // stop animation timer
286 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), wxBitmap(wxlogo_xpm
));
287 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
290 SetTitle( m_browser
->GetCurrentTitle() );
291 m_url
->SetValue( m_browser
->GetCurrentURL() );
295 * Callback invoked when user entered an URL and pressed enter
297 void WebFrame::OnUrl(wxCommandEvent
& evt
)
299 m_browser
->LoadUrl( m_url
->GetValue() );
304 * Callback invoked when user pressed the "back" button
306 void WebFrame::OnBack(wxCommandEvent
& evt
)
313 * Callback invoked when user pressed the "forward" button
315 void WebFrame::OnForward(wxCommandEvent
& evt
)
317 m_browser
->GoForward();
322 * Callback invoked when user pressed the "stop" button
324 void WebFrame::OnStop(wxCommandEvent
& evt
)
331 * Callback invoked when user pressed the "reload" button
333 void WebFrame::OnReload(wxCommandEvent
& evt
)
340 * Callback invoked when there is a request to load a new page (for instance
341 * when the user clicks a link)
343 void WebFrame::OnNavigationRequest(wxWebNavigationEvent
& evt
)
345 wxLogMessage("%s", "Navigation request to '" + evt
.GetHref() + "' (target='" +
346 evt
.GetTarget() + "')");
348 wxASSERT(m_browser
->IsBusy());
350 // Uncomment this to see how to block navigation requests
351 //int answer = wxMessageBox("Proceed with navigation to '" + evt.GetHref() + "'?",
352 // "Proceed with navigation?", wxYES_NO );
353 //if (answer != wxYES)
361 * Callback invoked when a navigation request was accepted
363 void WebFrame::OnNavigationComplete(wxWebNavigationEvent
& evt
)
365 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetHref() + "'");
370 * Callback invoked when a page is finished loading
372 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent
& evt
)
374 wxLogMessage("%s", "Document loaded; url='" + evt
.GetHref() + "'");
379 * On new window, we veto to stop extra windows appearing
381 void WebFrame::OnNewWindow(wxWebNavigationEvent
& evt
)
383 wxLogMessage("%s", "New window; url='" + evt
.GetHref() + "'");
390 * Invoked when user selects the "View Source" menu item
392 void WebFrame::OnViewSourceRequest(wxCommandEvent
& evt
)
394 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
399 * Invoked when user selects the "Menu" item
401 void WebFrame::OnToolsClicked(wxCommandEvent
& evt
)
403 if(m_browser
->GetCurrentURL() == "")
406 m_tools_tiny
->Check(false);
407 m_tools_small
->Check(false);
408 m_tools_medium
->Check(false);
409 m_tools_large
->Check(false);
410 m_tools_largest
->Check(false);
412 wxWebViewZoom zoom
= m_browser
->GetZoom();
415 case wxWEB_VIEW_ZOOM_TINY
:
416 m_tools_tiny
->Check();
418 case wxWEB_VIEW_ZOOM_SMALL
:
419 m_tools_small
->Check();
421 case wxWEB_VIEW_ZOOM_MEDIUM
:
422 m_tools_medium
->Check();
424 case wxWEB_VIEW_ZOOM_LARGE
:
425 m_tools_large
->Check();
427 case wxWEB_VIEW_ZOOM_LARGEST
:
428 m_tools_largest
->Check();
432 wxPoint position
= ScreenToClient( wxGetMousePosition() );
433 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
437 * Invoked when user selects the zoom size in the menu
439 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
441 if (evt
.GetId() == m_tools_tiny
->GetId())
443 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
445 else if (evt
.GetId() == m_tools_small
->GetId())
447 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
449 else if (evt
.GetId() == m_tools_medium
->GetId())
451 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
453 else if (evt
.GetId() == m_tools_large
->GetId())
455 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
457 else if (evt
.GetId() == m_tools_largest
->GetId())
459 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
468 * Callback invoked when a loading error occurs
470 void WebFrame::OnError(wxWebNavigationEvent
& evt
)
472 wxString errorCategory
;
473 switch (evt
.GetInt())
475 case wxWEB_NAV_ERR_CONNECTION
:
476 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
479 case wxWEB_NAV_ERR_CERTIFICATE
:
480 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
483 case wxWEB_NAV_ERR_AUTH
:
484 errorCategory
= "wxWEB_NAV_ERR_AUTH";
487 case wxWEB_NAV_ERR_SECURITY
:
488 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
491 case wxWEB_NAV_ERR_NOT_FOUND
:
492 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
495 case wxWEB_NAV_ERR_REQUEST
:
496 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
499 case wxWEB_NAV_ERR_USER_CANCELLED
:
500 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
503 case wxWEB_NAV_ERR_OTHER
:
504 errorCategory
= "wxWEB_NAV_ERR_OTHER";
508 wxLogMessage("Error; url='" + evt
.GetHref() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
510 //Show the info bar with an error
511 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetHref() + "\n" +
512 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
518 * Invoked when user selects "Print" from the menu
520 void WebFrame::OnPrint(wxCommandEvent
& evt
)
525 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
526 wxDialog(parent
, wxID_ANY
, "Source Code",
527 wxDefaultPosition
, wxSize(700,500),
528 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
530 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
531 text
->SetMarginWidth(1, 30);
532 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
533 text
->SetText(source
);
535 text
->StyleClearAll();
536 text
->SetLexer(wxSTC_LEX_HTML
);
537 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
538 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
539 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
540 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
541 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
542 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
543 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
544 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
546 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
547 sizer
->Add(text
, 1, wxEXPAND
);