]> git.saurik.com Git - wxWidgets.git/blob - samples/web/web.cpp
Give the sample a more sensible initial size for web browsing
[wxWidgets.git] / samples / web / web.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: web.cpp
3 // Purpose: wxWebView sample
4 // Author: Marianne Gagnon
5 // Id: $Id$
6 // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #endif
24
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>
30
31 #if !defined(__WXMSW__) && !defined(__WXPM__)
32 #include "../sample.xpm"
33 #endif
34
35 #if wxUSE_STC
36 #include <wx/stc/stc.h>
37 #else
38 #error "wxStyledTextControl is needed by this sample"
39 #endif
40
41 #include "stop.xpm"
42 #include "refresh.xpm"
43 #include "wxlogo.xpm"
44
45 class WebApp : public wxApp
46 {
47 public:
48 virtual bool OnInit();
49 };
50
51 class WebFrame : public wxFrame
52 {
53 public:
54 WebFrame();
55
56 void OnAnimationTimer(wxTimerEvent& evt);
57 void UpdateState();
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);
72
73 private:
74 wxTextCtrl* m_url;
75 wxWebView* m_browser;
76
77 wxToolBar* m_toolbar;
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;
83
84 wxMenu* m_tools_menu;
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;
92
93 wxTimer* m_timer;
94 int m_animation_angle;
95
96 wxInfoBar *m_info;
97 wxStaticText* m_info_text;
98 };
99
100 class SourceViewDialog : public wxDialog
101 {
102 public:
103 SourceViewDialog(wxWindow* parent, wxString source);
104 };
105
106 IMPLEMENT_APP(WebApp)
107
108 // ============================================================================
109 // implementation
110 // ============================================================================
111
112 bool WebApp::OnInit()
113 {
114 if ( !wxApp::OnInit() )
115 return false;
116
117 WebFrame *frame = new WebFrame();
118 frame->Show();
119
120 return true;
121 }
122
123 WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample")
124 {
125 // set the frame icon
126 SetIcon(wxICON(sample));
127 SetTitle("wxWebView Sample");
128
129 m_timer = NULL;
130 m_animation_angle = 0;
131
132
133 wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
134
135 // Create the toolbar
136 m_toolbar = CreateToolBar(wxTB_TEXT);
137 m_toolbar->SetToolBitmapSize(wxSize(32, 32));
138
139 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR);
140 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR);
141 #ifdef __WXGTK__
142 wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR);
143 #else
144 wxBitmap stop = wxBitmap(stop_xpm);
145 #endif
146 #ifdef __WXGTK__
147 wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR);
148 #else
149 wxBitmap refresh = wxBitmap(refresh_xpm);
150 #endif
151
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));
159
160 m_toolbar->Realize();
161
162 // Create the info panel
163 m_info = new wxInfoBar(this);
164 topsizer->Add(m_info, wxSizerFlags().Expand());
165
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));
169
170 SetSizer(topsizer);
171
172 //Set a more sensible size for web browsing
173 SetSize(wxSize(800, 600));
174
175 // Create a log window
176 new wxLogWindow(this, _("Logging"));
177
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"));
191
192 //By default we want to handle navigation and new windows
193 m_tools_handle_navigation->Check();
194 m_tools_handle_new_window->Check();
195
196
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 );
208
209 Connect(m_url->GetId(), wxEVT_COMMAND_TEXT_ENTER,
210 wxCommandEventHandler(WebFrame::OnUrl), NULL, this );
211
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);
223
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 );
239 }
240
241 void WebFrame::OnAnimationTimer(wxTimerEvent& evt)
242 {
243 m_animation_angle += 15;
244 if (m_animation_angle > 360) m_animation_angle -= 360;
245
246 wxBitmap image(24, 24);
247 {
248 wxMemoryDC dc;
249 dc.SelectObject(image);
250 dc.SetBackground(wxBrush(wxColour(255,0,255)));
251 dc.Clear();
252
253 if (m_animation_angle >= 0 && m_animation_angle <= 180)
254 {
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 );
259 }
260
261 dc.DrawBitmap(wxBitmap(wxlogo_xpm), 0, 0, true);
262
263 if (m_animation_angle > 180)
264 {
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 );
269 }
270 }
271 image.SetMask(new wxMask(image, wxColour(255,0,255)));
272 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), image);
273 }
274
275 /**
276 * Method that retrieves the current state from the web control and updates the GUI
277 * the reflect this current state.
278 */
279 void WebFrame::UpdateState()
280 {
281 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
282 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
283
284 if (m_browser->IsBusy())
285 {
286 if (m_timer == NULL)
287 {
288 m_timer = new wxTimer(this);
289 this->Connect(wxEVT_TIMER, wxTimerEventHandler(WebFrame::OnAnimationTimer), NULL, this);
290 }
291 m_timer->Start(100); // start animation timer
292
293 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
294 }
295 else
296 {
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 );
300 }
301
302 SetTitle( m_browser->GetCurrentTitle() );
303 m_url->SetValue( m_browser->GetCurrentURL() );
304 }
305
306 /**
307 * Callback invoked when user entered an URL and pressed enter
308 */
309 void WebFrame::OnUrl(wxCommandEvent& evt)
310 {
311 m_browser->LoadUrl( m_url->GetValue() );
312 UpdateState();
313 }
314
315 /**
316 * Callback invoked when user pressed the "back" button
317 */
318 void WebFrame::OnBack(wxCommandEvent& evt)
319 {
320 m_browser->GoBack();
321 UpdateState();
322 }
323
324 /**
325 * Callback invoked when user pressed the "forward" button
326 */
327 void WebFrame::OnForward(wxCommandEvent& evt)
328 {
329 m_browser->GoForward();
330 UpdateState();
331 }
332
333 /**
334 * Callback invoked when user pressed the "stop" button
335 */
336 void WebFrame::OnStop(wxCommandEvent& evt)
337 {
338 m_browser->Stop();
339 UpdateState();
340 }
341
342 /**
343 * Callback invoked when user pressed the "reload" button
344 */
345 void WebFrame::OnReload(wxCommandEvent& evt)
346 {
347 m_browser->Reload();
348 UpdateState();
349 }
350
351 /**
352 * Callback invoked when there is a request to load a new page (for instance
353 * when the user clicks a link)
354 */
355 void WebFrame::OnNavigationRequest(wxWebNavigationEvent& evt)
356 {
357 wxLogMessage("%s", "Navigation request to '" + evt.GetHref() + "' (target='" +
358 evt.GetTarget() + "')");
359
360 wxASSERT(m_browser->IsBusy());
361
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())
365 evt.Veto();
366
367 UpdateState();
368 }
369
370 /**
371 * Callback invoked when a navigation request was accepted
372 */
373 void WebFrame::OnNavigationComplete(wxWebNavigationEvent& evt)
374 {
375 wxLogMessage("%s", "Navigation complete; url='" + evt.GetHref() + "'");
376 UpdateState();
377 }
378
379 /**
380 * Callback invoked when a page is finished loading
381 */
382 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent& evt)
383 {
384 wxLogMessage("%s", "Document loaded; url='" + evt.GetHref() + "'");
385 UpdateState();
386 }
387
388 /**
389 * On new window, we veto to stop extra windows appearing
390 */
391 void WebFrame::OnNewWindow(wxWebNavigationEvent& evt)
392 {
393 wxLogMessage("%s", "New window; url='" + evt.GetHref() + "'");
394
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());
399
400 //We always veto because we handle the event, otherwise under windows a new
401 //internet explorer windowis created
402 evt.Veto();
403
404 UpdateState();
405 }
406
407 /**
408 * Invoked when user selects the "View Source" menu item
409 */
410 void WebFrame::OnViewSourceRequest(wxCommandEvent& evt)
411 {
412 SourceViewDialog dlg(this, m_browser->GetPageSource());
413 dlg.ShowModal();
414 }
415
416 /**
417 * Invoked when user selects the "Menu" item
418 */
419 void WebFrame::OnToolsClicked(wxCommandEvent& evt)
420 {
421 if(m_browser->GetCurrentURL() == "")
422 return;
423
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);
429
430 wxWebViewZoom zoom = m_browser->GetZoom();
431 switch (zoom)
432 {
433 case wxWEB_VIEW_ZOOM_TINY:
434 m_tools_tiny->Check();
435 break;
436 case wxWEB_VIEW_ZOOM_SMALL:
437 m_tools_small->Check();
438 break;
439 case wxWEB_VIEW_ZOOM_MEDIUM:
440 m_tools_medium->Check();
441 break;
442 case wxWEB_VIEW_ZOOM_LARGE:
443 m_tools_large->Check();
444 break;
445 case wxWEB_VIEW_ZOOM_LARGEST:
446 m_tools_largest->Check();
447 break;
448 }
449
450 wxPoint position = ScreenToClient( wxGetMousePosition() );
451 PopupMenu(m_tools_menu, position.x, position.y);
452 }
453
454 /**
455 * Invoked when user selects the zoom size in the menu
456 */
457 void WebFrame::OnSetZoom(wxCommandEvent& evt)
458 {
459 if (evt.GetId() == m_tools_tiny->GetId())
460 {
461 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
462 }
463 else if (evt.GetId() == m_tools_small->GetId())
464 {
465 m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL);
466 }
467 else if (evt.GetId() == m_tools_medium->GetId())
468 {
469 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
470 }
471 else if (evt.GetId() == m_tools_large->GetId())
472 {
473 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE);
474 }
475 else if (evt.GetId() == m_tools_largest->GetId())
476 {
477 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST);
478 }
479 else
480 {
481 wxFAIL;
482 }
483 }
484
485 /**
486 * Callback invoked when a loading error occurs
487 */
488 void WebFrame::OnError(wxWebNavigationEvent& evt)
489 {
490 wxString errorCategory;
491 switch (evt.GetInt())
492 {
493 case wxWEB_NAV_ERR_CONNECTION:
494 errorCategory = "wxWEB_NAV_ERR_CONNECTION";
495 break;
496
497 case wxWEB_NAV_ERR_CERTIFICATE:
498 errorCategory = "wxWEB_NAV_ERR_CERTIFICATE";
499 break;
500
501 case wxWEB_NAV_ERR_AUTH:
502 errorCategory = "wxWEB_NAV_ERR_AUTH";
503 break;
504
505 case wxWEB_NAV_ERR_SECURITY:
506 errorCategory = "wxWEB_NAV_ERR_SECURITY";
507 break;
508
509 case wxWEB_NAV_ERR_NOT_FOUND:
510 errorCategory = "wxWEB_NAV_ERR_NOT_FOUND";
511 break;
512
513 case wxWEB_NAV_ERR_REQUEST:
514 errorCategory = "wxWEB_NAV_ERR_REQUEST";
515 break;
516
517 case wxWEB_NAV_ERR_USER_CANCELLED:
518 errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED";
519 break;
520
521 case wxWEB_NAV_ERR_OTHER:
522 errorCategory = "wxWEB_NAV_ERR_OTHER";
523 break;
524 }
525
526 wxLogMessage("Error; url='" + evt.GetHref() + "', error='" + errorCategory + "' (" + evt.GetString() + ")");
527
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);
531
532 UpdateState();
533 }
534
535 /**
536 * Invoked when user selects "Print" from the menu
537 */
538 void WebFrame::OnPrint(wxCommandEvent& evt)
539 {
540 m_browser->Print();
541 }
542
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)
547 {
548 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
549 text->SetMarginWidth(1, 30);
550 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
551 text->SetText(source);
552
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));
563
564 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
565 sizer->Add(text, 1, wxEXPAND);
566 SetSizer(sizer);
567 }