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