]> git.saurik.com Git - wxWidgets.git/blob - samples/web/web.cpp
Add CombineURIs implementation for wxWebFileProtocolHandler. Update the IE backend...
[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 #include <wx/filesys.h>
31 #include <wx/fs_arc.h>
32
33 #if !defined(__WXMSW__) && !defined(__WXPM__)
34 #include "../sample.xpm"
35 #endif
36
37 #if wxUSE_STC
38 #include <wx/stc/stc.h>
39 #else
40 #error "wxStyledTextControl is needed by this sample"
41 #endif
42
43 #include "stop.xpm"
44 #include "refresh.xpm"
45 #include "wxlogo.xpm"
46
47 class WebApp : public wxApp
48 {
49 public:
50 virtual bool OnInit();
51 };
52
53 class WebFrame : public wxFrame
54 {
55 public:
56 WebFrame();
57
58 void OnAnimationTimer(wxTimerEvent& evt);
59 void UpdateState();
60 void OnUrl(wxCommandEvent& evt);
61 void OnBack(wxCommandEvent& evt);
62 void OnForward(wxCommandEvent& evt);
63 void OnStop(wxCommandEvent& evt);
64 void OnReload(wxCommandEvent& evt);
65 void OnClearHistory(wxCommandEvent& evt);
66 void OnEnableHistory(wxCommandEvent& evt);
67 void OnNavigationRequest(wxWebNavigationEvent& evt);
68 void OnNavigationComplete(wxWebNavigationEvent& evt);
69 void OnDocumentLoaded(wxWebNavigationEvent& evt);
70 void OnNewWindow(wxWebNavigationEvent& evt);
71 void OnViewSourceRequest(wxCommandEvent& evt);
72 void OnToolsClicked(wxCommandEvent& evt);
73 void OnSetZoom(wxCommandEvent& evt);
74 void OnError(wxWebNavigationEvent& evt);
75 void OnPrint(wxCommandEvent& evt);
76 void OnCut(wxCommandEvent& evt);
77 void OnCopy(wxCommandEvent& evt);
78 void OnPaste(wxCommandEvent& evt);
79 void OnUndo(wxCommandEvent& evt);
80 void OnRedo(wxCommandEvent& evt);
81 void OnMode(wxCommandEvent& evt);
82 void OnZoomLayout(wxCommandEvent& evt);
83
84 private:
85 wxTextCtrl* m_url;
86 wxWebView* m_browser;
87
88 wxToolBar* m_toolbar;
89 wxToolBarToolBase* m_toolbar_back;
90 wxToolBarToolBase* m_toolbar_forward;
91 wxToolBarToolBase* m_toolbar_stop;
92 wxToolBarToolBase* m_toolbar_reload;
93 wxToolBarToolBase* m_toolbar_tools;
94
95 wxMenu* m_tools_menu;
96 wxMenuItem* m_tools_layout;
97 wxMenuItem* m_tools_tiny;
98 wxMenuItem* m_tools_small;
99 wxMenuItem* m_tools_medium;
100 wxMenuItem* m_tools_large;
101 wxMenuItem* m_tools_largest;
102 wxMenuItem* m_tools_handle_navigation;
103 wxMenuItem* m_tools_handle_new_window;
104 wxMenuItem* m_tools_enable_history;
105 wxMenuItem* m_edit_cut;
106 wxMenuItem* m_edit_copy;
107 wxMenuItem* m_edit_paste;
108 wxMenuItem* m_edit_undo;
109 wxMenuItem* m_edit_redo;
110 wxMenuItem* m_edit_mode;
111
112 wxTimer* m_timer;
113 int m_animation_angle;
114
115 wxInfoBar *m_info;
116 wxStaticText* m_info_text;
117 };
118
119 class SourceViewDialog : public wxDialog
120 {
121 public:
122 SourceViewDialog(wxWindow* parent, wxString source);
123 };
124
125 IMPLEMENT_APP(WebApp)
126
127 // ============================================================================
128 // implementation
129 // ============================================================================
130
131 bool WebApp::OnInit()
132 {
133 if ( !wxApp::OnInit() )
134 return false;
135
136 WebFrame *frame = new WebFrame();
137 frame->Show();
138
139 return true;
140 }
141
142 WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample")
143 {
144 //Required from virtual file system archive support
145 wxFileSystem::AddHandler(new wxArchiveFSHandler);
146
147 // set the frame icon
148 SetIcon(wxICON(sample));
149 SetTitle("wxWebView Sample");
150
151 m_timer = NULL;
152 m_animation_angle = 0;
153
154
155 wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
156
157 // Create the toolbar
158 m_toolbar = CreateToolBar(wxTB_TEXT);
159 m_toolbar->SetToolBitmapSize(wxSize(32, 32));
160
161 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR);
162 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR);
163 #ifdef __WXGTK__
164 wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR);
165 #else
166 wxBitmap stop = wxBitmap(stop_xpm);
167 #endif
168 #ifdef __WXGTK__
169 wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR);
170 #else
171 wxBitmap refresh = wxBitmap(refresh_xpm);
172 #endif
173
174 m_toolbar_back = m_toolbar->AddTool(wxID_ANY, _("Back"), back);
175 m_toolbar_forward = m_toolbar->AddTool(wxID_ANY, _("Forward"), forward);
176 m_toolbar_stop = m_toolbar->AddTool(wxID_ANY, _("Stop"), stop);
177 m_toolbar_reload = m_toolbar->AddTool(wxID_ANY, _("Reload"), refresh);
178 m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER );
179 m_toolbar->AddControl(m_url, _("URL"));
180 m_toolbar_tools = m_toolbar->AddTool(wxID_ANY, _("Menu"), wxBitmap(wxlogo_xpm));
181
182 m_toolbar->Realize();
183
184 // Create the info panel
185 m_info = new wxInfoBar(this);
186 topsizer->Add(m_info, wxSizerFlags().Expand());
187
188 // Create the webview
189 m_browser = wxWebView::New(this, wxID_ANY, "http://www.wxwidgets.org");
190 topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
191
192 //We register the test:// protocol for testing purposes
193 m_browser->RegisterProtocol(new wxWebFileProtocolHandler());
194
195 SetSizer(topsizer);
196
197 //Set a more sensible size for web browsing
198 SetSize(wxSize(800, 600));
199
200 // Create a log window
201 new wxLogWindow(this, _("Logging"));
202
203 // Create the Tools menu
204 m_tools_menu = new wxMenu();
205 wxMenuItem* print = m_tools_menu->Append(wxID_ANY , _("Print"));
206 wxMenuItem* viewSource = m_tools_menu->Append(wxID_ANY , _("View Source"));
207 m_tools_menu->AppendSeparator();
208 m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom"));
209 m_tools_tiny = m_tools_menu->AppendCheckItem(wxID_ANY, _("Tiny"));
210 m_tools_small = m_tools_menu->AppendCheckItem(wxID_ANY, _("Small"));
211 m_tools_medium = m_tools_menu->AppendCheckItem(wxID_ANY, _("Medium"));
212 m_tools_large = m_tools_menu->AppendCheckItem(wxID_ANY, _("Large"));
213 m_tools_largest = m_tools_menu->AppendCheckItem(wxID_ANY, _("Largest"));
214 m_tools_menu->AppendSeparator();
215 m_tools_handle_navigation = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle Navigation"));
216 m_tools_handle_new_window = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle New Windows"));
217 m_tools_menu->AppendSeparator();
218 wxMenuItem* clearhist = m_tools_menu->Append(wxID_ANY, _("Clear History"));
219 m_tools_enable_history = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable History"));
220
221 //Create an editing menu
222 wxMenu* editmenu = new wxMenu();
223 m_edit_cut = editmenu->Append(wxID_ANY, _("Cut"));
224 m_edit_copy = editmenu->Append(wxID_ANY, _("Copy"));
225 m_edit_paste = editmenu->Append(wxID_ANY, _("Paste"));
226 editmenu->AppendSeparator();
227 m_edit_undo = editmenu->Append(wxID_ANY, _("Undo"));
228 m_edit_redo = editmenu->Append(wxID_ANY, _("Redo"));
229 editmenu->AppendSeparator();
230 m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode"));
231
232 m_tools_menu->AppendSeparator();
233 m_tools_menu->AppendSubMenu(editmenu, "Edit");
234
235 //By default we want to handle navigation and new windows
236 m_tools_handle_navigation->Check();
237 m_tools_handle_new_window->Check();
238 m_tools_enable_history->Check();
239 if(!m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT))
240 m_tools_layout->Enable(false);
241
242
243 // Connect the toolbar events
244 Connect(m_toolbar_back->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
245 wxCommandEventHandler(WebFrame::OnBack), NULL, this );
246 Connect(m_toolbar_forward->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
247 wxCommandEventHandler(WebFrame::OnForward), NULL, this );
248 Connect(m_toolbar_stop->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
249 wxCommandEventHandler(WebFrame::OnStop), NULL, this );
250 Connect(m_toolbar_reload->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
251 wxCommandEventHandler(WebFrame::OnReload),NULL, this );
252 Connect(m_toolbar_tools->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
253 wxCommandEventHandler(WebFrame::OnToolsClicked), NULL, this );
254
255 Connect(m_url->GetId(), wxEVT_COMMAND_TEXT_ENTER,
256 wxCommandEventHandler(WebFrame::OnUrl), NULL, this );
257
258 // Connect the webview events
259 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
260 wxWebNavigationEventHandler(WebFrame::OnNavigationRequest), NULL, this);
261 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
262 wxWebNavigationEventHandler(WebFrame::OnNavigationComplete), NULL, this);
263 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED,
264 wxWebNavigationEventHandler(WebFrame::OnDocumentLoaded), NULL, this);
265 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR,
266 wxWebNavigationEventHandler(WebFrame::OnError), NULL, this);
267 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
268 wxWebNavigationEventHandler(WebFrame::OnNewWindow), NULL, this);
269
270 // Connect the menu events
271 Connect(viewSource->GetId(), wxEVT_COMMAND_MENU_SELECTED,
272 wxCommandEventHandler(WebFrame::OnViewSourceRequest), NULL, this );
273 Connect(print->GetId(), wxEVT_COMMAND_MENU_SELECTED,
274 wxCommandEventHandler(WebFrame::OnPrint), NULL, this );
275 Connect(m_tools_layout->GetId(), wxEVT_COMMAND_MENU_SELECTED,
276 wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this );
277 Connect(m_tools_tiny->GetId(), wxEVT_COMMAND_MENU_SELECTED,
278 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
279 Connect(m_tools_small->GetId(), wxEVT_COMMAND_MENU_SELECTED,
280 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
281 Connect(m_tools_medium->GetId(), wxEVT_COMMAND_MENU_SELECTED,
282 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
283 Connect(m_tools_large->GetId(), wxEVT_COMMAND_MENU_SELECTED,
284 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
285 Connect(m_tools_largest->GetId(), wxEVT_COMMAND_MENU_SELECTED,
286 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
287 Connect(clearhist->GetId(), wxEVT_COMMAND_MENU_SELECTED,
288 wxCommandEventHandler(WebFrame::OnClearHistory), NULL, this );
289 Connect(m_tools_enable_history->GetId(), wxEVT_COMMAND_MENU_SELECTED,
290 wxCommandEventHandler(WebFrame::OnEnableHistory), NULL, this );
291 Connect(m_edit_cut->GetId(), wxEVT_COMMAND_MENU_SELECTED,
292 wxCommandEventHandler(WebFrame::OnCut), NULL, this );
293 Connect(m_edit_copy->GetId(), wxEVT_COMMAND_MENU_SELECTED,
294 wxCommandEventHandler(WebFrame::OnCopy), NULL, this );
295 Connect(m_edit_paste->GetId(), wxEVT_COMMAND_MENU_SELECTED,
296 wxCommandEventHandler(WebFrame::OnPaste), NULL, this );
297 Connect(m_edit_undo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
298 wxCommandEventHandler(WebFrame::OnUndo), NULL, this );
299 Connect(m_edit_redo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
300 wxCommandEventHandler(WebFrame::OnRedo), NULL, this );
301 Connect(m_edit_mode->GetId(), wxEVT_COMMAND_MENU_SELECTED,
302 wxCommandEventHandler(WebFrame::OnMode), NULL, this );
303 }
304
305 void WebFrame::OnAnimationTimer(wxTimerEvent& evt)
306 {
307 m_animation_angle += 15;
308 if (m_animation_angle > 360) m_animation_angle -= 360;
309
310 wxBitmap image(24, 24);
311 {
312 wxMemoryDC dc;
313 dc.SelectObject(image);
314 dc.SetBackground(wxBrush(wxColour(255,0,255)));
315 dc.Clear();
316
317 if (m_animation_angle >= 0 && m_animation_angle <= 180)
318 {
319 dc.SetBrush(*wxYELLOW_BRUSH);
320 dc.SetPen(*wxYELLOW_PEN);
321 dc.DrawCircle(16 - int(sin(m_animation_angle*0.01745f /* convert to radians */)*14.0f),
322 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 );
323 }
324
325 dc.DrawBitmap(wxBitmap(wxlogo_xpm), 0, 0, true);
326
327 if (m_animation_angle > 180)
328 {
329 dc.SetBrush(*wxYELLOW_BRUSH);
330 dc.SetPen(*wxYELLOW_PEN);
331 dc.DrawCircle(16 - int(sin(m_animation_angle*0.01745f /* convert to radians */)*14.0f),
332 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 );
333 }
334 }
335 image.SetMask(new wxMask(image, wxColour(255,0,255)));
336 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), image);
337 }
338
339 /**
340 * Method that retrieves the current state from the web control and updates the GUI
341 * the reflect this current state.
342 */
343 void WebFrame::UpdateState()
344 {
345 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
346 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
347
348 if (m_browser->IsBusy())
349 {
350 if (m_timer == NULL)
351 {
352 m_timer = new wxTimer(this);
353 this->Connect(wxEVT_TIMER, wxTimerEventHandler(WebFrame::OnAnimationTimer), NULL, this);
354 }
355 m_timer->Start(100); // start animation timer
356
357 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
358 }
359 else
360 {
361 if (m_timer != NULL) m_timer->Stop(); // stop animation timer
362 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), wxBitmap(wxlogo_xpm));
363 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
364 }
365
366 SetTitle( m_browser->GetCurrentTitle() );
367 m_url->SetValue( m_browser->GetCurrentURL() );
368 }
369
370 /**
371 * Callback invoked when user entered an URL and pressed enter
372 */
373 void WebFrame::OnUrl(wxCommandEvent& evt)
374 {
375 m_browser->LoadUrl( m_url->GetValue() );
376 UpdateState();
377 }
378
379 /**
380 * Callback invoked when user pressed the "back" button
381 */
382 void WebFrame::OnBack(wxCommandEvent& evt)
383 {
384 m_browser->GoBack();
385 UpdateState();
386 }
387
388 /**
389 * Callback invoked when user pressed the "forward" button
390 */
391 void WebFrame::OnForward(wxCommandEvent& evt)
392 {
393 m_browser->GoForward();
394 UpdateState();
395 }
396
397 /**
398 * Callback invoked when user pressed the "stop" button
399 */
400 void WebFrame::OnStop(wxCommandEvent& evt)
401 {
402 m_browser->Stop();
403 UpdateState();
404 }
405
406 /**
407 * Callback invoked when user pressed the "reload" button
408 */
409 void WebFrame::OnReload(wxCommandEvent& evt)
410 {
411 m_browser->Reload();
412 UpdateState();
413 }
414
415 void WebFrame::OnClearHistory(wxCommandEvent& evt)
416 {
417 m_browser->ClearHistory();
418 UpdateState();
419 }
420
421 void WebFrame::OnEnableHistory(wxCommandEvent& evt)
422 {
423 m_browser->EnableHistory(m_tools_enable_history->IsChecked());
424 UpdateState();
425 }
426
427 void WebFrame::OnCut(wxCommandEvent& evt)
428 {
429 m_browser->Cut();
430 }
431
432 void WebFrame::OnCopy(wxCommandEvent& evt)
433 {
434 m_browser->Copy();
435 }
436
437 void WebFrame::OnPaste(wxCommandEvent& evt)
438 {
439 m_browser->Paste();
440 }
441
442 void WebFrame::OnUndo(wxCommandEvent& evt)
443 {
444 m_browser->Undo();
445 }
446
447 void WebFrame::OnRedo(wxCommandEvent& evt)
448 {
449 m_browser->Redo();
450 }
451
452 void WebFrame::OnMode(wxCommandEvent& evt)
453 {
454 m_browser->SetEditable(m_edit_mode->IsChecked());
455 }
456
457
458 /**
459 * Callback invoked when there is a request to load a new page (for instance
460 * when the user clicks a link)
461 */
462 void WebFrame::OnNavigationRequest(wxWebNavigationEvent& evt)
463 {
464 wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" +
465 evt.GetTarget() + "')");
466
467 wxASSERT(m_browser->IsBusy());
468
469 //If we don't want to handle navigation then veto the event and navigation
470 //will not take place
471 if(!m_tools_handle_navigation->IsChecked())
472 evt.Veto();
473
474 UpdateState();
475 }
476
477 /**
478 * Callback invoked when a navigation request was accepted
479 */
480 void WebFrame::OnNavigationComplete(wxWebNavigationEvent& evt)
481 {
482 wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'");
483 UpdateState();
484 }
485
486 /**
487 * Callback invoked when a page is finished loading
488 */
489 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent& evt)
490 {
491 //Only notify if the document is the main frame, not a subframe
492 if(evt.GetURL() == m_browser->GetCurrentURL())
493 wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'");
494 UpdateState();
495 }
496
497 /**
498 * On new window, we veto to stop extra windows appearing
499 */
500 void WebFrame::OnNewWindow(wxWebNavigationEvent& evt)
501 {
502 wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'");
503
504 //If we handle new window events then just load them in this window as we
505 //are a single window browser
506 if(m_tools_handle_new_window->IsChecked())
507 m_browser->LoadUrl(evt.GetURL());
508
509 UpdateState();
510 }
511
512 /**
513 * Invoked when user selects the "View Source" menu item
514 */
515 void WebFrame::OnViewSourceRequest(wxCommandEvent& evt)
516 {
517 SourceViewDialog dlg(this, m_browser->GetPageSource());
518 dlg.ShowModal();
519 }
520
521 /**
522 * Invoked when user selects the "Menu" item
523 */
524 void WebFrame::OnToolsClicked(wxCommandEvent& evt)
525 {
526 if(m_browser->GetCurrentURL() == "")
527 return;
528
529 m_tools_tiny->Check(false);
530 m_tools_small->Check(false);
531 m_tools_medium->Check(false);
532 m_tools_large->Check(false);
533 m_tools_largest->Check(false);
534
535 wxWebViewZoom zoom = m_browser->GetZoom();
536 switch (zoom)
537 {
538 case wxWEB_VIEW_ZOOM_TINY:
539 m_tools_tiny->Check();
540 break;
541 case wxWEB_VIEW_ZOOM_SMALL:
542 m_tools_small->Check();
543 break;
544 case wxWEB_VIEW_ZOOM_MEDIUM:
545 m_tools_medium->Check();
546 break;
547 case wxWEB_VIEW_ZOOM_LARGE:
548 m_tools_large->Check();
549 break;
550 case wxWEB_VIEW_ZOOM_LARGEST:
551 m_tools_largest->Check();
552 break;
553 }
554
555 m_edit_cut->Enable(m_browser->CanCut());
556 m_edit_copy->Enable(m_browser->CanCopy());
557 m_edit_paste->Enable(m_browser->CanPaste());
558
559 m_edit_undo->Enable(m_browser->CanUndo());
560 m_edit_redo->Enable(m_browser->CanRedo());
561
562 wxPoint position = ScreenToClient( wxGetMousePosition() );
563 PopupMenu(m_tools_menu, position.x, position.y);
564 }
565
566 /**
567 * Invoked when user selects the zoom size in the menu
568 */
569 void WebFrame::OnSetZoom(wxCommandEvent& evt)
570 {
571 if (evt.GetId() == m_tools_tiny->GetId())
572 {
573 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
574 }
575 else if (evt.GetId() == m_tools_small->GetId())
576 {
577 m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL);
578 }
579 else if (evt.GetId() == m_tools_medium->GetId())
580 {
581 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
582 }
583 else if (evt.GetId() == m_tools_large->GetId())
584 {
585 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE);
586 }
587 else if (evt.GetId() == m_tools_largest->GetId())
588 {
589 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST);
590 }
591 else
592 {
593 wxFAIL;
594 }
595 }
596
597 void WebFrame::OnZoomLayout(wxCommandEvent& evt)
598 {
599 if(m_tools_layout->IsChecked())
600 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT);
601 else
602 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT);
603 }
604
605 /**
606 * Callback invoked when a loading error occurs
607 */
608 void WebFrame::OnError(wxWebNavigationEvent& evt)
609 {
610 wxString errorCategory;
611 switch (evt.GetInt())
612 {
613 case wxWEB_NAV_ERR_CONNECTION:
614 errorCategory = "wxWEB_NAV_ERR_CONNECTION";
615 break;
616
617 case wxWEB_NAV_ERR_CERTIFICATE:
618 errorCategory = "wxWEB_NAV_ERR_CERTIFICATE";
619 break;
620
621 case wxWEB_NAV_ERR_AUTH:
622 errorCategory = "wxWEB_NAV_ERR_AUTH";
623 break;
624
625 case wxWEB_NAV_ERR_SECURITY:
626 errorCategory = "wxWEB_NAV_ERR_SECURITY";
627 break;
628
629 case wxWEB_NAV_ERR_NOT_FOUND:
630 errorCategory = "wxWEB_NAV_ERR_NOT_FOUND";
631 break;
632
633 case wxWEB_NAV_ERR_REQUEST:
634 errorCategory = "wxWEB_NAV_ERR_REQUEST";
635 break;
636
637 case wxWEB_NAV_ERR_USER_CANCELLED:
638 errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED";
639 break;
640
641 case wxWEB_NAV_ERR_OTHER:
642 errorCategory = "wxWEB_NAV_ERR_OTHER";
643 break;
644 }
645
646 wxLogMessage("Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")");
647
648 //Show the info bar with an error
649 m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +
650 "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR);
651
652 UpdateState();
653 }
654
655 /**
656 * Invoked when user selects "Print" from the menu
657 */
658 void WebFrame::OnPrint(wxCommandEvent& evt)
659 {
660 m_browser->Print();
661 }
662
663 SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
664 wxDialog(parent, wxID_ANY, "Source Code",
665 wxDefaultPosition, wxSize(700,500),
666 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
667 {
668 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
669 text->SetMarginWidth(1, 30);
670 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
671 text->SetText(source);
672
673 text->StyleClearAll();
674 text->SetLexer(wxSTC_LEX_HTML);
675 text->StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(255,0,0));
676 text->StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(255,0,0));
677 text->StyleSetForeground(wxSTC_H_ENTITY, wxColour(255,0,0));
678 text->StyleSetForeground(wxSTC_H_TAG, wxColour(0,150,0));
679 text->StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(0,150,0));
680 text->StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,0,150));
681 text->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(0,0,150));
682 text->StyleSetForeground(wxSTC_H_COMMENT, wxColour(150,150,150));
683
684 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
685 sizer->Add(text, 1, wxEXPAND);
686 SetSizer(sizer);
687 }