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