]> git.saurik.com Git - wxWidgets.git/blob - samples/web/web.cpp
Update the web sample to use images from the art provider if possible. Scale existing...
[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
91 wxTimer* m_timer;
92 int m_animation_angle;
93
94 wxInfoBar *m_info;
95 wxStaticText* m_info_text;
96 };
97
98 class SourceViewDialog : public wxDialog
99 {
100 public:
101 SourceViewDialog(wxWindow* parent, wxString source);
102 };
103
104 IMPLEMENT_APP(WebApp)
105
106 // ============================================================================
107 // implementation
108 // ============================================================================
109
110 bool WebApp::OnInit()
111 {
112 if ( !wxApp::OnInit() )
113 return false;
114
115 WebFrame *frame = new WebFrame();
116 frame->Show();
117
118 return true;
119 }
120
121 WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample")
122 {
123 // set the frame icon
124 SetIcon(wxICON(sample));
125 SetTitle("wxWebView Sample");
126
127 m_timer = NULL;
128 m_animation_angle = 0;
129
130
131 wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
132
133 // Create the toolbar
134 m_toolbar = CreateToolBar(wxTB_TEXT);
135 m_toolbar->SetToolBitmapSize(wxSize(32, 32));
136
137 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR);
138 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR);
139 #ifdef __WXGTK__
140 wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR);
141 #else
142 wxBitmap stop = wxBitmap(stop_xpm);
143 #endif
144 #ifdef __WXGTK__
145 wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR);
146 #else
147 wxBitmap refresh = wxBitmap(refresh_xpm);
148 #endif
149
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));
157
158 m_toolbar->Realize();
159
160 // Create the info panel
161 m_info = new wxInfoBar(this);
162 topsizer->Add(m_info, wxSizerFlags().Expand());
163
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));
167
168 SetSizer(topsizer);
169
170 // Create a log window
171 new wxLogWindow(this, _("Logging"));
172
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"));
183
184
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 );
196
197 Connect(m_url->GetId(), wxEVT_COMMAND_TEXT_ENTER,
198 wxCommandEventHandler(WebFrame::OnUrl), NULL, this );
199
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);
211
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 );
227 }
228
229 void WebFrame::OnAnimationTimer(wxTimerEvent& evt)
230 {
231 m_animation_angle += 15;
232 if (m_animation_angle > 360) m_animation_angle -= 360;
233
234 wxBitmap image(32, 32);
235 {
236 wxMemoryDC dc;
237 dc.SelectObject(image);
238 dc.SetBackground(wxBrush(wxColour(255,0,255)));
239 dc.Clear();
240
241 if (m_animation_angle >= 0 && m_animation_angle <= 180)
242 {
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 );
247 }
248
249 dc.DrawBitmap(wxBitmap(wxlogo_xpm), 0, 0, true);
250
251 if (m_animation_angle > 180)
252 {
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 );
257 }
258 }
259 image.SetMask(new wxMask(image, wxColour(255,0,255)));
260 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), image);
261 }
262
263 /**
264 * Method that retrieves the current state from the web control and updates the GUI
265 * the reflect this current state.
266 */
267 void WebFrame::UpdateState()
268 {
269 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
270 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
271
272 if (m_browser->IsBusy())
273 {
274 if (m_timer == NULL)
275 {
276 m_timer = new wxTimer(this);
277 this->Connect(wxEVT_TIMER, wxTimerEventHandler(WebFrame::OnAnimationTimer), NULL, this);
278 }
279 m_timer->Start(100); // start animation timer
280
281 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
282 }
283 else
284 {
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 );
288 }
289
290 SetTitle( m_browser->GetCurrentTitle() );
291 m_url->SetValue( m_browser->GetCurrentURL() );
292 }
293
294 /**
295 * Callback invoked when user entered an URL and pressed enter
296 */
297 void WebFrame::OnUrl(wxCommandEvent& evt)
298 {
299 m_browser->LoadUrl( m_url->GetValue() );
300 UpdateState();
301 }
302
303 /**
304 * Callback invoked when user pressed the "back" button
305 */
306 void WebFrame::OnBack(wxCommandEvent& evt)
307 {
308 m_browser->GoBack();
309 UpdateState();
310 }
311
312 /**
313 * Callback invoked when user pressed the "forward" button
314 */
315 void WebFrame::OnForward(wxCommandEvent& evt)
316 {
317 m_browser->GoForward();
318 UpdateState();
319 }
320
321 /**
322 * Callback invoked when user pressed the "stop" button
323 */
324 void WebFrame::OnStop(wxCommandEvent& evt)
325 {
326 m_browser->Stop();
327 UpdateState();
328 }
329
330 /**
331 * Callback invoked when user pressed the "reload" button
332 */
333 void WebFrame::OnReload(wxCommandEvent& evt)
334 {
335 m_browser->Reload();
336 UpdateState();
337 }
338
339 /**
340 * Callback invoked when there is a request to load a new page (for instance
341 * when the user clicks a link)
342 */
343 void WebFrame::OnNavigationRequest(wxWebNavigationEvent& evt)
344 {
345 wxLogMessage("%s", "Navigation request to '" + evt.GetHref() + "' (target='" +
346 evt.GetTarget() + "')");
347
348 wxASSERT(m_browser->IsBusy());
349
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)
354 //{
355 // evt.Veto();
356 //}
357 UpdateState();
358 }
359
360 /**
361 * Callback invoked when a navigation request was accepted
362 */
363 void WebFrame::OnNavigationComplete(wxWebNavigationEvent& evt)
364 {
365 wxLogMessage("%s", "Navigation complete; url='" + evt.GetHref() + "'");
366 UpdateState();
367 }
368
369 /**
370 * Callback invoked when a page is finished loading
371 */
372 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent& evt)
373 {
374 wxLogMessage("%s", "Document loaded; url='" + evt.GetHref() + "'");
375 UpdateState();
376 }
377
378 /**
379 * On new window, we veto to stop extra windows appearing
380 */
381 void WebFrame::OnNewWindow(wxWebNavigationEvent& evt)
382 {
383 wxLogMessage("%s", "New window; url='" + evt.GetHref() + "'");
384 evt.Veto();
385
386 UpdateState();
387 }
388
389 /**
390 * Invoked when user selects the "View Source" menu item
391 */
392 void WebFrame::OnViewSourceRequest(wxCommandEvent& evt)
393 {
394 SourceViewDialog dlg(this, m_browser->GetPageSource());
395 dlg.ShowModal();
396 }
397
398 /**
399 * Invoked when user selects the "Menu" item
400 */
401 void WebFrame::OnToolsClicked(wxCommandEvent& evt)
402 {
403 if(m_browser->GetCurrentURL() == "")
404 return;
405
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);
411
412 wxWebViewZoom zoom = m_browser->GetZoom();
413 switch (zoom)
414 {
415 case wxWEB_VIEW_ZOOM_TINY:
416 m_tools_tiny->Check();
417 break;
418 case wxWEB_VIEW_ZOOM_SMALL:
419 m_tools_small->Check();
420 break;
421 case wxWEB_VIEW_ZOOM_MEDIUM:
422 m_tools_medium->Check();
423 break;
424 case wxWEB_VIEW_ZOOM_LARGE:
425 m_tools_large->Check();
426 break;
427 case wxWEB_VIEW_ZOOM_LARGEST:
428 m_tools_largest->Check();
429 break;
430 }
431
432 wxPoint position = ScreenToClient( wxGetMousePosition() );
433 PopupMenu(m_tools_menu, position.x, position.y);
434 }
435
436 /**
437 * Invoked when user selects the zoom size in the menu
438 */
439 void WebFrame::OnSetZoom(wxCommandEvent& evt)
440 {
441 if (evt.GetId() == m_tools_tiny->GetId())
442 {
443 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
444 }
445 else if (evt.GetId() == m_tools_small->GetId())
446 {
447 m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL);
448 }
449 else if (evt.GetId() == m_tools_medium->GetId())
450 {
451 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
452 }
453 else if (evt.GetId() == m_tools_large->GetId())
454 {
455 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE);
456 }
457 else if (evt.GetId() == m_tools_largest->GetId())
458 {
459 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST);
460 }
461 else
462 {
463 wxFAIL;
464 }
465 }
466
467 /**
468 * Callback invoked when a loading error occurs
469 */
470 void WebFrame::OnError(wxWebNavigationEvent& evt)
471 {
472 wxString errorCategory;
473 switch (evt.GetInt())
474 {
475 case wxWEB_NAV_ERR_CONNECTION:
476 errorCategory = "wxWEB_NAV_ERR_CONNECTION";
477 break;
478
479 case wxWEB_NAV_ERR_CERTIFICATE:
480 errorCategory = "wxWEB_NAV_ERR_CERTIFICATE";
481 break;
482
483 case wxWEB_NAV_ERR_AUTH:
484 errorCategory = "wxWEB_NAV_ERR_AUTH";
485 break;
486
487 case wxWEB_NAV_ERR_SECURITY:
488 errorCategory = "wxWEB_NAV_ERR_SECURITY";
489 break;
490
491 case wxWEB_NAV_ERR_NOT_FOUND:
492 errorCategory = "wxWEB_NAV_ERR_NOT_FOUND";
493 break;
494
495 case wxWEB_NAV_ERR_REQUEST:
496 errorCategory = "wxWEB_NAV_ERR_REQUEST";
497 break;
498
499 case wxWEB_NAV_ERR_USER_CANCELLED:
500 errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED";
501 break;
502
503 case wxWEB_NAV_ERR_OTHER:
504 errorCategory = "wxWEB_NAV_ERR_OTHER";
505 break;
506 }
507
508 wxLogMessage("Error; url='" + evt.GetHref() + "', error='" + errorCategory + "' (" + evt.GetString() + ")");
509
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);
513
514 UpdateState();
515 }
516
517 /**
518 * Invoked when user selects "Print" from the menu
519 */
520 void WebFrame::OnPrint(wxCommandEvent& evt)
521 {
522 m_browser->Print();
523 }
524
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)
529 {
530 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
531 text->SetMarginWidth(1, 30);
532 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
533 text->SetText(source);
534
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));
545
546 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
547 sizer->Add(text, 1, wxEXPAND);
548 SetSizer(sizer);
549 }