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