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
);
157 m_browser
->LoadUrl("http://www.wxwidgets.org");
158 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
162 // Create a log window
163 new wxLogWindow(this, _("Logging"));
165 // Create the Tools menu
166 m_tools_menu
= new wxMenu();
167 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
168 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
169 m_tools_menu
->AppendSeparator();
170 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
171 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
172 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
173 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
174 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
177 // Connect the toolbar events
178 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
179 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
180 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
181 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
182 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
183 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
184 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
185 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
186 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
187 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
189 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
190 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
192 // Connect the webview events
193 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
194 wxWebNavigationEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
195 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
196 wxWebNavigationEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
197 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
198 wxWebNavigationEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
199 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
200 wxWebNavigationEventHandler(WebFrame::OnError
), NULL
, this);
201 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
202 wxWebNavigationEventHandler(WebFrame::OnNewWindow
), NULL
, this);
204 // Connect the menu events
205 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
206 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
207 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
208 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
209 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
210 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
211 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
212 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
213 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
214 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
215 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
216 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
217 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
218 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
221 void WebFrame::OnAnimationTimer(wxTimerEvent
& evt
)
223 m_animation_angle
+= 15;
224 if (m_animation_angle
> 360) m_animation_angle
-= 360;
226 wxBitmap
image(32, 32);
229 dc
.SelectObject(image
);
230 dc
.SetBackground(wxBrush(wxColour(255,0,255)));
233 if (m_animation_angle
>= 0 && m_animation_angle
<= 180)
235 dc
.SetBrush(*wxYELLOW_BRUSH
);
236 dc
.SetPen(*wxYELLOW_PEN
);
237 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
238 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
241 dc
.DrawBitmap(wxBitmap(wxlogo_xpm
), 0, 0, true);
243 if (m_animation_angle
> 180)
245 dc
.SetBrush(*wxYELLOW_BRUSH
);
246 dc
.SetPen(*wxYELLOW_PEN
);
247 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
248 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
251 image
.SetMask(new wxMask(image
, wxColour(255,0,255)));
252 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), image
);
256 * Method that retrieves the current state from the web control and updates the GUI
257 * the reflect this current state.
259 void WebFrame::UpdateState()
261 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
262 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
264 if (m_browser
->IsBusy())
268 m_timer
= new wxTimer(this);
269 this->Connect(wxEVT_TIMER
, wxTimerEventHandler(WebFrame::OnAnimationTimer
), NULL
, this);
271 m_timer
->Start(100); // start animation timer
273 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
277 if (m_timer
!= NULL
) m_timer
->Stop(); // stop animation timer
278 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), wxBitmap(wxlogo_xpm
));
279 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
282 SetTitle( m_browser
->GetCurrentTitle() );
283 m_url
->SetValue( m_browser
->GetCurrentURL() );
287 * Callback invoked when user entered an URL and pressed enter
289 void WebFrame::OnUrl(wxCommandEvent
& evt
)
291 m_browser
->LoadUrl( m_url
->GetValue() );
296 * Callback invoked when user pressed the "back" button
298 void WebFrame::OnBack(wxCommandEvent
& evt
)
305 * Callback invoked when user pressed the "forward" button
307 void WebFrame::OnForward(wxCommandEvent
& evt
)
309 m_browser
->GoForward();
314 * Callback invoked when user pressed the "stop" button
316 void WebFrame::OnStop(wxCommandEvent
& evt
)
323 * Callback invoked when user pressed the "reload" button
325 void WebFrame::OnReload(wxCommandEvent
& evt
)
332 * Callback invoked when there is a request to load a new page (for instance
333 * when the user clicks a link)
335 void WebFrame::OnNavigationRequest(wxWebNavigationEvent
& evt
)
337 wxLogMessage("%s", "Navigation request to '" + evt
.GetHref() + "' (target='" +
338 evt
.GetTarget() + "')");
340 wxASSERT(m_browser
->IsBusy());
342 // Uncomment this to see how to block navigation requests
343 //int answer = wxMessageBox("Proceed with navigation to '" + evt.GetHref() + "'?",
344 // "Proceed with navigation?", wxYES_NO );
345 //if (answer != wxYES)
353 * Callback invoked when a navigation request was accepted
355 void WebFrame::OnNavigationComplete(wxWebNavigationEvent
& evt
)
357 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetHref() + "'");
362 * Callback invoked when a page is finished loading
364 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent
& evt
)
366 wxLogMessage("%s", "Document loaded; url='" + evt
.GetHref() + "'");
371 * On new window, we veto to stop extra windows appearing
373 void WebFrame::OnNewWindow(wxWebNavigationEvent
& evt
)
375 wxLogMessage("%s", "New window; url='" + evt
.GetHref() + "'");
382 * Invoked when user selects the "View Source" menu item
384 void WebFrame::OnViewSourceRequest(wxCommandEvent
& evt
)
386 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
391 * Invoked when user selects the "Menu" item
393 void WebFrame::OnToolsClicked(wxCommandEvent
& evt
)
395 if(m_browser
->GetCurrentURL() == "")
398 m_tools_tiny
->Check(false);
399 m_tools_small
->Check(false);
400 m_tools_medium
->Check(false);
401 m_tools_large
->Check(false);
402 m_tools_largest
->Check(false);
404 wxWebViewZoom zoom
= m_browser
->GetZoom();
407 case wxWEB_VIEW_ZOOM_TINY
:
408 m_tools_tiny
->Check();
410 case wxWEB_VIEW_ZOOM_SMALL
:
411 m_tools_small
->Check();
413 case wxWEB_VIEW_ZOOM_MEDIUM
:
414 m_tools_medium
->Check();
416 case wxWEB_VIEW_ZOOM_LARGE
:
417 m_tools_large
->Check();
419 case wxWEB_VIEW_ZOOM_LARGEST
:
420 m_tools_largest
->Check();
424 wxPoint position
= ScreenToClient( wxGetMousePosition() );
425 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
429 * Invoked when user selects the zoom size in the menu
431 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
433 if (evt
.GetId() == m_tools_tiny
->GetId())
435 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
437 else if (evt
.GetId() == m_tools_small
->GetId())
439 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
441 else if (evt
.GetId() == m_tools_medium
->GetId())
443 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
445 else if (evt
.GetId() == m_tools_large
->GetId())
447 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
449 else if (evt
.GetId() == m_tools_largest
->GetId())
451 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
460 * Callback invoked when a loading error occurs
462 void WebFrame::OnError(wxWebNavigationEvent
& evt
)
464 wxString errorCategory
;
465 switch (evt
.GetInt())
467 case wxWEB_NAV_ERR_CONNECTION
:
468 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
471 case wxWEB_NAV_ERR_CERTIFICATE
:
472 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
475 case wxWEB_NAV_ERR_AUTH
:
476 errorCategory
= "wxWEB_NAV_ERR_AUTH";
479 case wxWEB_NAV_ERR_SECURITY
:
480 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
483 case wxWEB_NAV_ERR_NOT_FOUND
:
484 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
487 case wxWEB_NAV_ERR_REQUEST
:
488 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
491 case wxWEB_NAV_ERR_USER_CANCELLED
:
492 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
495 case wxWEB_NAV_ERR_OTHER
:
496 errorCategory
= "wxWEB_NAV_ERR_OTHER";
500 wxLogMessage("Error; url='" + evt
.GetHref() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
502 //Show the info bar with an error
503 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetHref() + "\n" +
504 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
510 * Invoked when user selects "Print" from the menu
512 void WebFrame::OnPrint(wxCommandEvent
& evt
)
517 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
518 wxDialog(parent
, wxID_ANY
, "Source Code",
519 wxDefaultPosition
, wxSize(700,500),
520 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
522 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
523 text
->SetMarginWidth(1, 30);
524 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
525 text
->SetText(source
);
527 text
->StyleClearAll();
528 text
->SetLexer(wxSTC_LEX_HTML
);
529 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
530 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
531 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
532 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
533 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
534 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
535 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
536 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
538 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
539 sizer
->Add(text
, 1, wxEXPAND
);