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"
43 #include "forward.xpm"
45 #include "refresh.xpm"
48 class WebApp
: public wxApp
51 virtual bool OnInit();
54 class WebFrame
: public wxFrame
59 void OnAnimationTimer(wxTimerEvent
& evt
);
61 void OnUrl(wxCommandEvent
& evt
);
62 void OnBack(wxCommandEvent
& evt
);
63 void OnForward(wxCommandEvent
& evt
);
64 void OnStop(wxCommandEvent
& evt
);
65 void OnReload(wxCommandEvent
& evt
);
66 void OnNavigationRequest(wxWebNavigationEvent
& evt
);
67 void OnNavigationComplete(wxWebNavigationEvent
& evt
);
68 void OnDocumentLoaded(wxWebNavigationEvent
& evt
);
69 void OnNewWindow(wxWebNavigationEvent
& evt
);
70 void OnViewSourceRequest(wxCommandEvent
& evt
);
71 void OnToolsClicked(wxCommandEvent
& evt
);
72 void OnSetZoom(wxCommandEvent
& evt
);
73 void OnError(wxWebNavigationEvent
& evt
);
74 void OnPrint(wxCommandEvent
& evt
);
81 wxToolBarToolBase
* m_toolbar_back
;
82 wxToolBarToolBase
* m_toolbar_forward
;
83 wxToolBarToolBase
* m_toolbar_stop
;
84 wxToolBarToolBase
* m_toolbar_reload
;
85 wxToolBarToolBase
* m_toolbar_tools
;
88 wxMenuItem
* m_tools_tiny
;
89 wxMenuItem
* m_tools_small
;
90 wxMenuItem
* m_tools_medium
;
91 wxMenuItem
* m_tools_large
;
92 wxMenuItem
* m_tools_largest
;
95 int m_animation_angle
;
99 wxStaticText
* m_info_text
;
102 class SourceViewDialog
: public wxDialog
105 SourceViewDialog(wxWindow
* parent
, wxString source
);
108 IMPLEMENT_APP(WebApp
)
110 // ============================================================================
112 // ============================================================================
114 bool WebApp::OnInit()
116 if ( !wxApp::OnInit() )
119 WebFrame
*frame
= new WebFrame();
125 WebFrame::WebFrame() : wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
127 // set the frame icon
128 SetIcon(wxICON(sample
));
129 SetTitle("wxWebView Sample");
132 m_animation_angle
= 0;
135 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
137 // Create the toolbar
138 m_toolbar
= CreateToolBar(wxTB_TEXT
);
139 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
141 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), wxBitmap(back_xpm
));
142 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), wxBitmap(forward_xpm
));
143 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), wxBitmap(stop_xpm
));
144 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), wxBitmap(refresh_xpm
));
145 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
146 m_toolbar
->AddControl(m_url
, _("URL"));
147 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
149 m_toolbar
->Realize();
151 // Create the info panel
152 m_info
= new wxInfoBar(this);
153 topsizer
->Add(m_info
, wxSizerFlags().Expand());
155 // Create the webview
156 m_browser
= wxWebView::New(this, wxID_ANY
, "http://www.wxwidgets.org");
157 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
161 // Create a log window
162 new wxLogWindow(this, _("Logging"));
164 // Create the Tools menu
165 m_tools_menu
= new wxMenu();
166 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
167 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
168 m_tools_menu
->AppendSeparator();
169 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
170 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
171 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
172 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
173 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
176 // Connect the toolbar events
177 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
178 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
179 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
180 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
181 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
182 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
183 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
184 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
185 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
186 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
188 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
189 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
191 // Connect the webview events
192 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
193 wxWebNavigationEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
194 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
195 wxWebNavigationEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
196 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
197 wxWebNavigationEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
198 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
199 wxWebNavigationEventHandler(WebFrame::OnError
), NULL
, this);
200 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
201 wxWebNavigationEventHandler(WebFrame::OnNewWindow
), NULL
, this);
203 // Connect the menu events
204 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
205 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
206 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
207 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
208 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
209 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
210 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
211 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
212 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
213 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
214 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
215 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
216 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
217 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
220 void WebFrame::OnAnimationTimer(wxTimerEvent
& evt
)
222 m_animation_angle
+= 15;
223 if (m_animation_angle
> 360) m_animation_angle
-= 360;
225 wxBitmap
image(32, 32);
228 dc
.SelectObject(image
);
229 dc
.SetBackground(wxBrush(wxColour(255,0,255)));
232 if (m_animation_angle
>= 0 && m_animation_angle
<= 180)
234 dc
.SetBrush(*wxYELLOW_BRUSH
);
235 dc
.SetPen(*wxYELLOW_PEN
);
236 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
237 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
240 dc
.DrawBitmap(wxBitmap(wxlogo_xpm
), 0, 0, true);
242 if (m_animation_angle
> 180)
244 dc
.SetBrush(*wxYELLOW_BRUSH
);
245 dc
.SetPen(*wxYELLOW_PEN
);
246 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
247 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
250 image
.SetMask(new wxMask(image
, wxColour(255,0,255)));
251 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), image
);
255 * Method that retrieves the current state from the web control and updates the GUI
256 * the reflect this current state.
258 void WebFrame::UpdateState()
260 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
261 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
263 if (m_browser
->IsBusy())
267 m_timer
= new wxTimer(this);
268 this->Connect(wxEVT_TIMER
, wxTimerEventHandler(WebFrame::OnAnimationTimer
), NULL
, this);
270 m_timer
->Start(100); // start animation timer
272 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
276 if (m_timer
!= NULL
) m_timer
->Stop(); // stop animation timer
277 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), wxBitmap(wxlogo_xpm
));
278 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
281 SetTitle( m_browser
->GetCurrentTitle() );
282 m_url
->SetValue( m_browser
->GetCurrentURL() );
286 * Callback invoked when user entered an URL and pressed enter
288 void WebFrame::OnUrl(wxCommandEvent
& evt
)
290 m_browser
->LoadUrl( m_url
->GetValue() );
295 * Callback invoked when user pressed the "back" button
297 void WebFrame::OnBack(wxCommandEvent
& evt
)
304 * Callback invoked when user pressed the "forward" button
306 void WebFrame::OnForward(wxCommandEvent
& evt
)
308 m_browser
->GoForward();
313 * Callback invoked when user pressed the "stop" button
315 void WebFrame::OnStop(wxCommandEvent
& evt
)
322 * Callback invoked when user pressed the "reload" button
324 void WebFrame::OnReload(wxCommandEvent
& evt
)
331 * Callback invoked when there is a request to load a new page (for instance
332 * when the user clicks a link)
334 void WebFrame::OnNavigationRequest(wxWebNavigationEvent
& evt
)
336 wxLogMessage("%s", "Navigation request to '" + evt
.GetHref() + "' (target='" +
337 evt
.GetTarget() + "')");
339 wxASSERT(m_browser
->IsBusy());
341 // Uncomment this to see how to block navigation requests
342 //int answer = wxMessageBox("Proceed with navigation to '" + evt.GetHref() + "'?",
343 // "Proceed with navigation?", wxYES_NO );
344 //if (answer != wxYES)
352 * Callback invoked when a navigation request was accepted
354 void WebFrame::OnNavigationComplete(wxWebNavigationEvent
& evt
)
356 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetHref() + "'");
361 * Callback invoked when a page is finished loading
363 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent
& evt
)
365 wxLogMessage("%s", "Document loaded; url='" + evt
.GetHref() + "'");
370 * On new window, we veto to stop extra windows appearing
372 void WebFrame::OnNewWindow(wxWebNavigationEvent
& evt
)
374 wxLogMessage("%s", "New window; url='" + evt
.GetHref() + "'");
381 * Invoked when user selects the "View Source" menu item
383 void WebFrame::OnViewSourceRequest(wxCommandEvent
& evt
)
385 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
390 * Invoked when user selects the "Menu" item
392 void WebFrame::OnToolsClicked(wxCommandEvent
& evt
)
394 if(m_browser
->GetCurrentURL() == "")
397 m_tools_tiny
->Check(false);
398 m_tools_small
->Check(false);
399 m_tools_medium
->Check(false);
400 m_tools_large
->Check(false);
401 m_tools_largest
->Check(false);
403 wxWebViewZoom zoom
= m_browser
->GetZoom();
406 case wxWEB_VIEW_ZOOM_TINY
:
407 m_tools_tiny
->Check();
409 case wxWEB_VIEW_ZOOM_SMALL
:
410 m_tools_small
->Check();
412 case wxWEB_VIEW_ZOOM_MEDIUM
:
413 m_tools_medium
->Check();
415 case wxWEB_VIEW_ZOOM_LARGE
:
416 m_tools_large
->Check();
418 case wxWEB_VIEW_ZOOM_LARGEST
:
419 m_tools_largest
->Check();
423 wxPoint position
= ScreenToClient( wxGetMousePosition() );
424 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
428 * Invoked when user selects the zoom size in the menu
430 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
432 if (evt
.GetId() == m_tools_tiny
->GetId())
434 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
436 else if (evt
.GetId() == m_tools_small
->GetId())
438 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
440 else if (evt
.GetId() == m_tools_medium
->GetId())
442 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
444 else if (evt
.GetId() == m_tools_large
->GetId())
446 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
448 else if (evt
.GetId() == m_tools_largest
->GetId())
450 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
459 * Callback invoked when a loading error occurs
461 void WebFrame::OnError(wxWebNavigationEvent
& evt
)
463 wxString errorCategory
;
464 switch (evt
.GetInt())
466 case wxWEB_NAV_ERR_CONNECTION
:
467 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
470 case wxWEB_NAV_ERR_CERTIFICATE
:
471 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
474 case wxWEB_NAV_ERR_AUTH
:
475 errorCategory
= "wxWEB_NAV_ERR_AUTH";
478 case wxWEB_NAV_ERR_SECURITY
:
479 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
482 case wxWEB_NAV_ERR_NOT_FOUND
:
483 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
486 case wxWEB_NAV_ERR_REQUEST
:
487 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
490 case wxWEB_NAV_ERR_USER_CANCELLED
:
491 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
494 case wxWEB_NAV_ERR_OTHER
:
495 errorCategory
= "wxWEB_NAV_ERR_OTHER";
499 wxLogMessage("Error; url='" + evt
.GetHref() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
501 //Show the info bar with an error
502 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetHref() + "\n" +
503 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
509 * Invoked when user selects "Print" from the menu
511 void WebFrame::OnPrint(wxCommandEvent
& evt
)
516 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
517 wxDialog(parent
, wxID_ANY
, "Source Code",
518 wxDefaultPosition
, wxSize(700,500),
519 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
521 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
522 text
->SetMarginWidth(1, 30);
523 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
524 text
->SetText(source
);
526 text
->StyleClearAll();
527 text
->SetLexer(wxSTC_LEX_HTML
);
528 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
529 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
530 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
531 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
532 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
533 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
534 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
535 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
537 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
538 sizer
->Add(text
, 1, wxEXPAND
);