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