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