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