Delete the menu on close in the wxWebView sample. Fixes most of the memory leaks.
[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_tools_menu;
364 }
365
366 void WebFrame::OnAnimationTimer(wxTimerEvent& WXUNUSED(evt))
367 {
368 m_animation_angle += 15;
369 if (m_animation_angle > 360) m_animation_angle -= 360;
370
371 wxBitmap image(24, 24);
372 {
373 wxMemoryDC dc;
374 dc.SelectObject(image);
375 dc.SetBackground(wxBrush(wxColour(255,0,255)));
376 dc.Clear();
377
378 if (m_animation_angle >= 0 && m_animation_angle <= 180)
379 {
380 dc.SetBrush(*wxYELLOW_BRUSH);
381 dc.SetPen(*wxYELLOW_PEN);
382 dc.DrawCircle(16 - int(sin(m_animation_angle*0.01745f /* convert to radians */)*14.0f),
383 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 );
384 }
385
386 dc.DrawBitmap(wxBitmap(wxlogo_xpm), 0, 0, true);
387
388 if (m_animation_angle > 180)
389 {
390 dc.SetBrush(*wxYELLOW_BRUSH);
391 dc.SetPen(*wxYELLOW_PEN);
392 dc.DrawCircle(16 - int(sin(m_animation_angle*0.01745f /* convert to radians */)*14.0f),
393 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 );
394 }
395 }
396 image.SetMask(new wxMask(image, wxColour(255,0,255)));
397 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), image);
398 }
399
400 /**
401 * Method that retrieves the current state from the web control and updates the GUI
402 * the reflect this current state.
403 */
404 void WebFrame::UpdateState()
405 {
406 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
407 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
408
409 if (m_browser->IsBusy())
410 {
411 if (m_timer == NULL)
412 {
413 m_timer = new wxTimer(this);
414 this->Connect(wxEVT_TIMER, wxTimerEventHandler(WebFrame::OnAnimationTimer), NULL, this);
415 }
416 m_timer->Start(100); // start animation timer
417
418 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
419 }
420 else
421 {
422 if (m_timer != NULL) m_timer->Stop(); // stop animation timer
423 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), wxBitmap(wxlogo_xpm));
424 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
425 }
426
427 SetTitle( m_browser->GetCurrentTitle() );
428 m_url->SetValue( m_browser->GetCurrentURL() );
429 }
430
431 /**
432 * Callback invoked when user entered an URL and pressed enter
433 */
434 void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt))
435 {
436 m_browser->LoadURL( m_url->GetValue() );
437 UpdateState();
438 }
439
440 /**
441 * Callback invoked when user pressed the "back" button
442 */
443 void WebFrame::OnBack(wxCommandEvent& WXUNUSED(evt))
444 {
445 m_browser->GoBack();
446 UpdateState();
447 }
448
449 /**
450 * Callback invoked when user pressed the "forward" button
451 */
452 void WebFrame::OnForward(wxCommandEvent& WXUNUSED(evt))
453 {
454 m_browser->GoForward();
455 UpdateState();
456 }
457
458 /**
459 * Callback invoked when user pressed the "stop" button
460 */
461 void WebFrame::OnStop(wxCommandEvent& WXUNUSED(evt))
462 {
463 m_browser->Stop();
464 UpdateState();
465 }
466
467 /**
468 * Callback invoked when user pressed the "reload" button
469 */
470 void WebFrame::OnReload(wxCommandEvent& WXUNUSED(evt))
471 {
472 m_browser->Reload();
473 UpdateState();
474 }
475
476 void WebFrame::OnClearHistory(wxCommandEvent& WXUNUSED(evt))
477 {
478 m_browser->ClearHistory();
479 UpdateState();
480 }
481
482 void WebFrame::OnEnableHistory(wxCommandEvent& WXUNUSED(evt))
483 {
484 m_browser->EnableHistory(m_tools_enable_history->IsChecked());
485 UpdateState();
486 }
487
488 void WebFrame::OnCut(wxCommandEvent& WXUNUSED(evt))
489 {
490 m_browser->Cut();
491 }
492
493 void WebFrame::OnCopy(wxCommandEvent& WXUNUSED(evt))
494 {
495 m_browser->Copy();
496 }
497
498 void WebFrame::OnPaste(wxCommandEvent& WXUNUSED(evt))
499 {
500 m_browser->Paste();
501 }
502
503 void WebFrame::OnUndo(wxCommandEvent& WXUNUSED(evt))
504 {
505 m_browser->Undo();
506 }
507
508 void WebFrame::OnRedo(wxCommandEvent& WXUNUSED(evt))
509 {
510 m_browser->Redo();
511 }
512
513 void WebFrame::OnMode(wxCommandEvent& WXUNUSED(evt))
514 {
515 m_browser->SetEditable(m_edit_mode->IsChecked());
516 }
517
518 void WebFrame::OnLoadScheme(wxCommandEvent& WXUNUSED(evt))
519 {
520 wxFileName helpfile("../help/doc.zip");
521 helpfile.MakeAbsolute();
522 wxString path = helpfile.GetFullPath();
523 //Under MSW we need to flip the slashes
524 path.Replace("\\", "/");
525 path = "wxfs:///" + path + ";protocol=zip/doc.htm";
526 m_browser->LoadURL(path);
527 }
528
529 /**
530 * Callback invoked when there is a request to load a new page (for instance
531 * when the user clicks a link)
532 */
533 void WebFrame::OnNavigationRequest(wxWebViewEvent& evt)
534 {
535 if(m_info->IsShown())
536 {
537 m_info->Dismiss();
538 }
539
540 wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" +
541 evt.GetTarget() + "')");
542
543 wxASSERT(m_browser->IsBusy());
544
545 //If we don't want to handle navigation then veto the event and navigation
546 //will not take place, we also need to stop the loading animation
547 if(!m_tools_handle_navigation->IsChecked())
548 {
549 evt.Veto();
550 if (m_timer != NULL) m_timer->Stop(); // stop animation timer
551 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), wxBitmap(wxlogo_xpm));
552 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
553 }
554 else
555 {
556 UpdateState();
557 }
558 }
559
560 /**
561 * Callback invoked when a navigation request was accepted
562 */
563 void WebFrame::OnNavigationComplete(wxWebViewEvent& evt)
564 {
565 wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'");
566 UpdateState();
567 }
568
569 /**
570 * Callback invoked when a page is finished loading
571 */
572 void WebFrame::OnDocumentLoaded(wxWebViewEvent& evt)
573 {
574 //Only notify if the document is the main frame, not a subframe
575 if(evt.GetURL() == m_browser->GetCurrentURL())
576 {
577 wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'");
578 }
579 UpdateState();
580 }
581
582 /**
583 * On new window, we veto to stop extra windows appearing
584 */
585 void WebFrame::OnNewWindow(wxWebViewEvent& evt)
586 {
587 wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'");
588
589 //If we handle new window events then just load them in this window as we
590 //are a single window browser
591 if(m_tools_handle_new_window->IsChecked())
592 m_browser->LoadURL(evt.GetURL());
593
594 UpdateState();
595 }
596
597 void WebFrame::OnTitleChanged(wxWebViewEvent& evt)
598 {
599 wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'");
600 UpdateState();
601 }
602
603 /**
604 * Invoked when user selects the "View Source" menu item
605 */
606 void WebFrame::OnViewSourceRequest(wxCommandEvent& WXUNUSED(evt))
607 {
608 SourceViewDialog dlg(this, m_browser->GetPageSource());
609 dlg.ShowModal();
610 }
611
612 /**
613 * Invoked when user selects the "Menu" item
614 */
615 void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
616 {
617 if(m_browser->GetCurrentURL() == "")
618 return;
619
620 m_tools_tiny->Check(false);
621 m_tools_small->Check(false);
622 m_tools_medium->Check(false);
623 m_tools_large->Check(false);
624 m_tools_largest->Check(false);
625
626 wxWebViewZoom zoom = m_browser->GetZoom();
627 switch (zoom)
628 {
629 case wxWEB_VIEW_ZOOM_TINY:
630 m_tools_tiny->Check();
631 break;
632 case wxWEB_VIEW_ZOOM_SMALL:
633 m_tools_small->Check();
634 break;
635 case wxWEB_VIEW_ZOOM_MEDIUM:
636 m_tools_medium->Check();
637 break;
638 case wxWEB_VIEW_ZOOM_LARGE:
639 m_tools_large->Check();
640 break;
641 case wxWEB_VIEW_ZOOM_LARGEST:
642 m_tools_largest->Check();
643 break;
644 }
645
646 m_edit_cut->Enable(m_browser->CanCut());
647 m_edit_copy->Enable(m_browser->CanCopy());
648 m_edit_paste->Enable(m_browser->CanPaste());
649
650 m_edit_undo->Enable(m_browser->CanUndo());
651 m_edit_redo->Enable(m_browser->CanRedo());
652
653 m_selection_clear->Enable(m_browser->HasSelection());
654 m_selection_delete->Enable(m_browser->HasSelection());
655
656 //Firstly we clear the existing menu items, then we add the current ones
657 wxMenuHistoryMap::const_iterator it;
658 for( it = m_histMenuItems.begin(); it != m_histMenuItems.end(); ++it )
659 {
660 m_tools_history_menu->Destroy(it->first);
661 }
662 m_histMenuItems.clear();
663
664 wxVector<wxSharedPtr<wxWebViewHistoryItem> > back = m_browser->GetBackwardHistory();
665 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forward = m_browser->GetForwardHistory();
666
667 wxMenuItem* item;
668
669 unsigned int i;
670 for(i = 0; i < back.size(); i++)
671 {
672 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, back[i]->GetTitle());
673 m_histMenuItems[item->GetId()] = back[i];
674 Connect(item->GetId(), wxEVT_COMMAND_MENU_SELECTED,
675 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
676 }
677
678 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, m_browser->GetCurrentTitle());
679 item->Check();
680
681 //No need to connect the current item
682 m_histMenuItems[item->GetId()] = wxSharedPtr<wxWebViewHistoryItem>(new wxWebViewHistoryItem(m_browser->GetCurrentURL(), m_browser->GetCurrentTitle()));
683
684 for(i = 0; i < forward.size(); i++)
685 {
686 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, forward[i]->GetTitle());
687 m_histMenuItems[item->GetId()] = forward[i];
688 Connect(item->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
689 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
690 }
691
692 wxPoint position = ScreenToClient( wxGetMousePosition() );
693 PopupMenu(m_tools_menu, position.x, position.y);
694 }
695
696 /**
697 * Invoked when user selects the zoom size in the menu
698 */
699 void WebFrame::OnSetZoom(wxCommandEvent& evt)
700 {
701 if (evt.GetId() == m_tools_tiny->GetId())
702 {
703 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
704 }
705 else if (evt.GetId() == m_tools_small->GetId())
706 {
707 m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL);
708 }
709 else if (evt.GetId() == m_tools_medium->GetId())
710 {
711 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
712 }
713 else if (evt.GetId() == m_tools_large->GetId())
714 {
715 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE);
716 }
717 else if (evt.GetId() == m_tools_largest->GetId())
718 {
719 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST);
720 }
721 else
722 {
723 wxFAIL;
724 }
725 }
726
727 void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt))
728 {
729 if(m_tools_layout->IsChecked())
730 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT);
731 else
732 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT);
733 }
734
735 void WebFrame::OnHistory(wxCommandEvent& evt)
736 {
737 m_browser->LoadHistoryItem(m_histMenuItems[evt.GetId()]);
738 }
739
740 void WebFrame::OnRunScript(wxCommandEvent& WXUNUSED(evt))
741 {
742 wxTextEntryDialog dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr, "", wxOK|wxCANCEL|wxCENTRE|wxTE_MULTILINE);
743 if(dialog.ShowModal() == wxID_OK)
744 {
745 m_browser->RunScript(dialog.GetValue());
746 }
747 }
748
749 void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))
750 {
751 m_browser->ClearSelection();
752 }
753
754 void WebFrame::OnDeleteSelection(wxCommandEvent& WXUNUSED(evt))
755 {
756 m_browser->DeleteSelection();
757 }
758
759 void WebFrame::OnSelectAll(wxCommandEvent& WXUNUSED(evt))
760 {
761 m_browser->SelectAll();
762 }
763
764 /**
765 * Callback invoked when a loading error occurs
766 */
767 void WebFrame::OnError(wxWebViewEvent& evt)
768 {
769 wxString errorCategory;
770 switch (evt.GetInt())
771 {
772 case wxWEB_NAV_ERR_CONNECTION:
773 errorCategory = "wxWEB_NAV_ERR_CONNECTION";
774 break;
775
776 case wxWEB_NAV_ERR_CERTIFICATE:
777 errorCategory = "wxWEB_NAV_ERR_CERTIFICATE";
778 break;
779
780 case wxWEB_NAV_ERR_AUTH:
781 errorCategory = "wxWEB_NAV_ERR_AUTH";
782 break;
783
784 case wxWEB_NAV_ERR_SECURITY:
785 errorCategory = "wxWEB_NAV_ERR_SECURITY";
786 break;
787
788 case wxWEB_NAV_ERR_NOT_FOUND:
789 errorCategory = "wxWEB_NAV_ERR_NOT_FOUND";
790 break;
791
792 case wxWEB_NAV_ERR_REQUEST:
793 errorCategory = "wxWEB_NAV_ERR_REQUEST";
794 break;
795
796 case wxWEB_NAV_ERR_USER_CANCELLED:
797 errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED";
798 break;
799
800 case wxWEB_NAV_ERR_OTHER:
801 errorCategory = "wxWEB_NAV_ERR_OTHER";
802 break;
803 }
804
805 wxLogMessage("Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")");
806
807 //Show the info bar with an error
808 m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +
809 "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR);
810
811 UpdateState();
812 }
813
814 /**
815 * Invoked when user selects "Print" from the menu
816 */
817 void WebFrame::OnPrint(wxCommandEvent& WXUNUSED(evt))
818 {
819 m_browser->Print();
820 }
821
822 SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
823 wxDialog(parent, wxID_ANY, "Source Code",
824 wxDefaultPosition, wxSize(700,500),
825 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
826 {
827 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
828 text->SetMarginWidth(1, 30);
829 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
830 text->SetText(source);
831
832 text->StyleClearAll();
833 text->SetLexer(wxSTC_LEX_HTML);
834 text->StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(255,0,0));
835 text->StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(255,0,0));
836 text->StyleSetForeground(wxSTC_H_ENTITY, wxColour(255,0,0));
837 text->StyleSetForeground(wxSTC_H_TAG, wxColour(0,150,0));
838 text->StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(0,150,0));
839 text->StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,0,150));
840 text->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(0,0,150));
841 text->StyleSetForeground(wxSTC_H_COMMENT, wxColour(150,150,150));
842
843 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
844 sizer->Add(text, 1, wxEXPAND);
845 SetSizer(sizer);
846 }