1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWebView sample
4 // Author: Marianne Gagnon
6 // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
25 #if !wxUSE_WEBVIEW_WEBKIT && !wxUSE_WEBVIEW_IE
26 #error "A wxWebView backend is required by this sample"
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"
38 #ifndef wxHAS_IMAGES_IN_RESOURCES
39 #include "../sample.xpm"
43 #include "wx/stc/stc.h"
45 #error "wxStyledTextControl is needed by this sample"
48 #if defined(__WXMSW__) || defined(__WXOSX__)
50 #include "refresh.xpm"
56 //We map menu items to their history items
57 WX_DECLARE_HASH_MAP(int, wxSharedPtr
<wxWebViewHistoryItem
>,
58 wxIntegerHash
, wxIntegerEqual
, wxMenuHistoryMap
);
60 class WebApp
: public wxApp
63 virtual bool OnInit();
66 class WebFrame
: public wxFrame
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
);
107 wxWebView
* m_browser
;
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
;
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
;
137 wxStaticText
* m_info_text
;
139 wxMenuHistoryMap m_histMenuItems
;
142 class SourceViewDialog
: public wxDialog
145 SourceViewDialog(wxWindow
* parent
, wxString source
);
148 IMPLEMENT_APP(WebApp
)
150 // ============================================================================
152 // ============================================================================
154 bool WebApp::OnInit()
156 if ( !wxApp::OnInit() )
159 WebFrame
*frame
= new WebFrame();
165 WebFrame::WebFrame() : wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
167 //Required from virtual file system archive support
168 wxFileSystem::AddHandler(new wxArchiveFSHandler
);
170 // set the frame icon
171 SetIcon(wxICON(sample
));
172 SetTitle("wxWebView Sample");
174 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
176 // Create the toolbar
177 m_toolbar
= CreateToolBar(wxTB_TEXT
);
178 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
180 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
181 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
183 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
185 wxBitmap stop
= wxBitmap(stop_xpm
);
188 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
190 wxBitmap refresh
= wxBitmap(refresh_xpm
);
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
));
201 m_toolbar
->Realize();
203 // Create the info panel
204 m_info
= new wxInfoBar(this);
205 topsizer
->Add(m_info
, wxSizerFlags().Expand());
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));
211 //We register the wxfs:// protocol for testing purposes
212 m_browser
->RegisterHandler(wxSharedPtr
<wxWebViewHandler
>(new wxWebViewArchiveHandler("wxfs")));
216 //Set a more sensible size for web browsing
217 SetSize(wxSize(800, 600));
219 // Create a log window
220 new wxLogWindow(this, _("Logging"));
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();
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();
244 m_tools_menu
->AppendSubMenu(m_tools_history_menu
, "History");
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"));
257 m_tools_menu
->AppendSeparator();
258 m_tools_menu
->AppendSubMenu(editmenu
, "Edit");
260 wxMenuItem
* script
= m_tools_menu
->Append(wxID_ANY
, _("Run Script"));
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"));
268 editmenu
->AppendSubMenu(selection
, "Selection");
270 wxMenuItem
* loadscheme
= m_tools_menu
->Append(wxID_ANY
, _("Custom Scheme Example"));
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);
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 );
292 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
293 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
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);
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 );
353 //Connect the idle events
354 Connect(wxID_ANY
, wxEVT_IDLE
, wxIdleEventHandler(WebFrame::OnIdle
), NULL
, this);
357 WebFrame::~WebFrame()
363 * Method that retrieves the current state from the web control and updates the GUI
364 * the reflect this current state.
366 void WebFrame::UpdateState()
368 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
369 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
371 if (m_browser
->IsBusy())
373 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
377 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
380 SetTitle( m_browser
->GetCurrentTitle() );
381 m_url
->SetValue( m_browser
->GetCurrentURL() );
384 void WebFrame::OnIdle(wxIdleEvent
& WXUNUSED(evt
))
386 if(m_browser
->IsBusy())
388 wxSetCursor(wxCURSOR_ARROWWAIT
);
389 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), true);
393 wxSetCursor(wxNullCursor
);
394 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), false);
399 * Callback invoked when user entered an URL and pressed enter
401 void WebFrame::OnUrl(wxCommandEvent
& WXUNUSED(evt
))
403 m_browser
->LoadURL( m_url
->GetValue() );
404 m_browser
->SetFocus();
409 * Callback invoked when user pressed the "back" button
411 void WebFrame::OnBack(wxCommandEvent
& WXUNUSED(evt
))
418 * Callback invoked when user pressed the "forward" button
420 void WebFrame::OnForward(wxCommandEvent
& WXUNUSED(evt
))
422 m_browser
->GoForward();
427 * Callback invoked when user pressed the "stop" button
429 void WebFrame::OnStop(wxCommandEvent
& WXUNUSED(evt
))
436 * Callback invoked when user pressed the "reload" button
438 void WebFrame::OnReload(wxCommandEvent
& WXUNUSED(evt
))
444 void WebFrame::OnClearHistory(wxCommandEvent
& WXUNUSED(evt
))
446 m_browser
->ClearHistory();
450 void WebFrame::OnEnableHistory(wxCommandEvent
& WXUNUSED(evt
))
452 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
456 void WebFrame::OnCut(wxCommandEvent
& WXUNUSED(evt
))
461 void WebFrame::OnCopy(wxCommandEvent
& WXUNUSED(evt
))
466 void WebFrame::OnPaste(wxCommandEvent
& WXUNUSED(evt
))
471 void WebFrame::OnUndo(wxCommandEvent
& WXUNUSED(evt
))
476 void WebFrame::OnRedo(wxCommandEvent
& WXUNUSED(evt
))
481 void WebFrame::OnMode(wxCommandEvent
& WXUNUSED(evt
))
483 m_browser
->SetEditable(m_edit_mode
->IsChecked());
486 void WebFrame::OnLoadScheme(wxCommandEvent
& WXUNUSED(evt
))
488 wxFileName
helpfile("../help/doc.zip");
489 helpfile
.MakeAbsolute();
490 wxString path
= helpfile
.GetFullPath();
491 //Under MSW we need to flip the slashes
492 path
.Replace("\\", "/");
493 path
= "wxfs:///" + path
+ ";protocol=zip/doc.htm";
494 m_browser
->LoadURL(path
);
498 * Callback invoked when there is a request to load a new page (for instance
499 * when the user clicks a link)
501 void WebFrame::OnNavigationRequest(wxWebViewEvent
& evt
)
503 if(m_info
->IsShown())
508 wxLogMessage("%s", "Navigation request to '" + evt
.GetURL() + "' (target='" +
509 evt
.GetTarget() + "')");
511 wxASSERT(m_browser
->IsBusy());
513 //If we don't want to handle navigation then veto the event and navigation
514 //will not take place, we also need to stop the loading animation
515 if(!m_tools_handle_navigation
->IsChecked())
518 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
527 * Callback invoked when a navigation request was accepted
529 void WebFrame::OnNavigationComplete(wxWebViewEvent
& evt
)
531 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetURL() + "'");
536 * Callback invoked when a page is finished loading
538 void WebFrame::OnDocumentLoaded(wxWebViewEvent
& evt
)
540 //Only notify if the document is the main frame, not a subframe
541 if(evt
.GetURL() == m_browser
->GetCurrentURL())
543 wxLogMessage("%s", "Document loaded; url='" + evt
.GetURL() + "'");
549 * On new window, we veto to stop extra windows appearing
551 void WebFrame::OnNewWindow(wxWebViewEvent
& evt
)
553 wxLogMessage("%s", "New window; url='" + evt
.GetURL() + "'");
555 //If we handle new window events then just load them in this window as we
556 //are a single window browser
557 if(m_tools_handle_new_window
->IsChecked())
558 m_browser
->LoadURL(evt
.GetURL());
563 void WebFrame::OnTitleChanged(wxWebViewEvent
& evt
)
565 SetTitle(evt
.GetString());
566 wxLogMessage("%s", "Title changed; title='" + evt
.GetString() + "'");
570 * Invoked when user selects the "View Source" menu item
572 void WebFrame::OnViewSourceRequest(wxCommandEvent
& WXUNUSED(evt
))
574 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
579 * Invoked when user selects the "Menu" item
581 void WebFrame::OnToolsClicked(wxCommandEvent
& WXUNUSED(evt
))
583 if(m_browser
->GetCurrentURL() == "")
586 m_tools_tiny
->Check(false);
587 m_tools_small
->Check(false);
588 m_tools_medium
->Check(false);
589 m_tools_large
->Check(false);
590 m_tools_largest
->Check(false);
592 wxWebViewZoom zoom
= m_browser
->GetZoom();
595 case wxWEB_VIEW_ZOOM_TINY
:
596 m_tools_tiny
->Check();
598 case wxWEB_VIEW_ZOOM_SMALL
:
599 m_tools_small
->Check();
601 case wxWEB_VIEW_ZOOM_MEDIUM
:
602 m_tools_medium
->Check();
604 case wxWEB_VIEW_ZOOM_LARGE
:
605 m_tools_large
->Check();
607 case wxWEB_VIEW_ZOOM_LARGEST
:
608 m_tools_largest
->Check();
612 m_edit_cut
->Enable(m_browser
->CanCut());
613 m_edit_copy
->Enable(m_browser
->CanCopy());
614 m_edit_paste
->Enable(m_browser
->CanPaste());
616 m_edit_undo
->Enable(m_browser
->CanUndo());
617 m_edit_redo
->Enable(m_browser
->CanRedo());
619 m_selection_clear
->Enable(m_browser
->HasSelection());
620 m_selection_delete
->Enable(m_browser
->HasSelection());
622 //Firstly we clear the existing menu items, then we add the current ones
623 wxMenuHistoryMap::const_iterator it
;
624 for( it
= m_histMenuItems
.begin(); it
!= m_histMenuItems
.end(); ++it
)
626 m_tools_history_menu
->Destroy(it
->first
);
628 m_histMenuItems
.clear();
630 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > back
= m_browser
->GetBackwardHistory();
631 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forward
= m_browser
->GetForwardHistory();
636 for(i
= 0; i
< back
.size(); i
++)
638 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, back
[i
]->GetTitle());
639 m_histMenuItems
[item
->GetId()] = back
[i
];
640 Connect(item
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
641 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
644 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, m_browser
->GetCurrentTitle());
647 //No need to connect the current item
648 m_histMenuItems
[item
->GetId()] = wxSharedPtr
<wxWebViewHistoryItem
>(new wxWebViewHistoryItem(m_browser
->GetCurrentURL(), m_browser
->GetCurrentTitle()));
650 for(i
= 0; i
< forward
.size(); i
++)
652 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, forward
[i
]->GetTitle());
653 m_histMenuItems
[item
->GetId()] = forward
[i
];
654 Connect(item
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
655 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
658 wxPoint position
= ScreenToClient( wxGetMousePosition() );
659 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
663 * Invoked when user selects the zoom size in the menu
665 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
667 if (evt
.GetId() == m_tools_tiny
->GetId())
669 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
671 else if (evt
.GetId() == m_tools_small
->GetId())
673 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
675 else if (evt
.GetId() == m_tools_medium
->GetId())
677 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
679 else if (evt
.GetId() == m_tools_large
->GetId())
681 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
683 else if (evt
.GetId() == m_tools_largest
->GetId())
685 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
693 void WebFrame::OnZoomLayout(wxCommandEvent
& WXUNUSED(evt
))
695 if(m_tools_layout
->IsChecked())
696 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
698 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
701 void WebFrame::OnHistory(wxCommandEvent
& evt
)
703 m_browser
->LoadHistoryItem(m_histMenuItems
[evt
.GetId()]);
706 void WebFrame::OnRunScript(wxCommandEvent
& WXUNUSED(evt
))
708 wxTextEntryDialog
dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr
, "", wxOK
|wxCANCEL
|wxCENTRE
|wxTE_MULTILINE
);
709 if(dialog
.ShowModal() == wxID_OK
)
711 m_browser
->RunScript(dialog
.GetValue());
715 void WebFrame::OnClearSelection(wxCommandEvent
& WXUNUSED(evt
))
717 m_browser
->ClearSelection();
720 void WebFrame::OnDeleteSelection(wxCommandEvent
& WXUNUSED(evt
))
722 m_browser
->DeleteSelection();
725 void WebFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(evt
))
727 m_browser
->SelectAll();
731 * Callback invoked when a loading error occurs
733 void WebFrame::OnError(wxWebViewEvent
& evt
)
735 wxString errorCategory
;
736 switch (evt
.GetInt())
738 case wxWEB_NAV_ERR_CONNECTION
:
739 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
742 case wxWEB_NAV_ERR_CERTIFICATE
:
743 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
746 case wxWEB_NAV_ERR_AUTH
:
747 errorCategory
= "wxWEB_NAV_ERR_AUTH";
750 case wxWEB_NAV_ERR_SECURITY
:
751 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
754 case wxWEB_NAV_ERR_NOT_FOUND
:
755 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
758 case wxWEB_NAV_ERR_REQUEST
:
759 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
762 case wxWEB_NAV_ERR_USER_CANCELLED
:
763 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
766 case wxWEB_NAV_ERR_OTHER
:
767 errorCategory
= "wxWEB_NAV_ERR_OTHER";
771 wxLogMessage("%s", "Error; url='" + evt
.GetURL() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
773 //Show the info bar with an error
774 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetURL() + "\n" +
775 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
781 * Invoked when user selects "Print" from the menu
783 void WebFrame::OnPrint(wxCommandEvent
& WXUNUSED(evt
))
788 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
789 wxDialog(parent
, wxID_ANY
, "Source Code",
790 wxDefaultPosition
, wxSize(700,500),
791 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
793 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
794 text
->SetMarginWidth(1, 30);
795 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
796 text
->SetText(source
);
798 text
->StyleClearAll();
799 text
->SetLexer(wxSTC_LEX_HTML
);
800 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
801 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
802 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
803 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
804 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
805 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
806 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
807 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
809 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
810 sizer
->Add(text
, 1, wxEXPAND
);