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 #if !defined(__WXMSW__) && !defined(__WXPM__)
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
& 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() );
408 * Callback invoked when user pressed the "back" button
410 void WebFrame::OnBack(wxCommandEvent
& WXUNUSED(evt
))
417 * Callback invoked when user pressed the "forward" button
419 void WebFrame::OnForward(wxCommandEvent
& WXUNUSED(evt
))
421 m_browser
->GoForward();
426 * Callback invoked when user pressed the "stop" button
428 void WebFrame::OnStop(wxCommandEvent
& WXUNUSED(evt
))
435 * Callback invoked when user pressed the "reload" button
437 void WebFrame::OnReload(wxCommandEvent
& WXUNUSED(evt
))
443 void WebFrame::OnClearHistory(wxCommandEvent
& WXUNUSED(evt
))
445 m_browser
->ClearHistory();
449 void WebFrame::OnEnableHistory(wxCommandEvent
& WXUNUSED(evt
))
451 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
455 void WebFrame::OnCut(wxCommandEvent
& WXUNUSED(evt
))
460 void WebFrame::OnCopy(wxCommandEvent
& WXUNUSED(evt
))
465 void WebFrame::OnPaste(wxCommandEvent
& WXUNUSED(evt
))
470 void WebFrame::OnUndo(wxCommandEvent
& WXUNUSED(evt
))
475 void WebFrame::OnRedo(wxCommandEvent
& WXUNUSED(evt
))
480 void WebFrame::OnMode(wxCommandEvent
& WXUNUSED(evt
))
482 m_browser
->SetEditable(m_edit_mode
->IsChecked());
485 void WebFrame::OnLoadScheme(wxCommandEvent
& WXUNUSED(evt
))
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
);
497 * Callback invoked when there is a request to load a new page (for instance
498 * when the user clicks a link)
500 void WebFrame::OnNavigationRequest(wxWebViewEvent
& evt
)
502 if(m_info
->IsShown())
507 wxLogMessage("%s", "Navigation request to '" + evt
.GetURL() + "' (target='" +
508 evt
.GetTarget() + "')");
510 wxASSERT(m_browser
->IsBusy());
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())
517 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
526 * Callback invoked when a navigation request was accepted
528 void WebFrame::OnNavigationComplete(wxWebViewEvent
& evt
)
530 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetURL() + "'");
535 * Callback invoked when a page is finished loading
537 void WebFrame::OnDocumentLoaded(wxWebViewEvent
& evt
)
539 //Only notify if the document is the main frame, not a subframe
540 if(evt
.GetURL() == m_browser
->GetCurrentURL())
542 wxLogMessage("%s", "Document loaded; url='" + evt
.GetURL() + "'");
548 * On new window, we veto to stop extra windows appearing
550 void WebFrame::OnNewWindow(wxWebViewEvent
& evt
)
552 wxLogMessage("%s", "New window; url='" + evt
.GetURL() + "'");
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());
562 void WebFrame::OnTitleChanged(wxWebViewEvent
& evt
)
564 SetTitle(evt
.GetString());
565 wxLogMessage("%s", "Title changed; title='" + evt
.GetString() + "'");
569 * Invoked when user selects the "View Source" menu item
571 void WebFrame::OnViewSourceRequest(wxCommandEvent
& WXUNUSED(evt
))
573 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
578 * Invoked when user selects the "Menu" item
580 void WebFrame::OnToolsClicked(wxCommandEvent
& WXUNUSED(evt
))
582 if(m_browser
->GetCurrentURL() == "")
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);
591 wxWebViewZoom zoom
= m_browser
->GetZoom();
594 case wxWEB_VIEW_ZOOM_TINY
:
595 m_tools_tiny
->Check();
597 case wxWEB_VIEW_ZOOM_SMALL
:
598 m_tools_small
->Check();
600 case wxWEB_VIEW_ZOOM_MEDIUM
:
601 m_tools_medium
->Check();
603 case wxWEB_VIEW_ZOOM_LARGE
:
604 m_tools_large
->Check();
606 case wxWEB_VIEW_ZOOM_LARGEST
:
607 m_tools_largest
->Check();
611 m_edit_cut
->Enable(m_browser
->CanCut());
612 m_edit_copy
->Enable(m_browser
->CanCopy());
613 m_edit_paste
->Enable(m_browser
->CanPaste());
615 m_edit_undo
->Enable(m_browser
->CanUndo());
616 m_edit_redo
->Enable(m_browser
->CanRedo());
618 m_selection_clear
->Enable(m_browser
->HasSelection());
619 m_selection_delete
->Enable(m_browser
->HasSelection());
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
)
625 m_tools_history_menu
->Destroy(it
->first
);
627 m_histMenuItems
.clear();
629 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > back
= m_browser
->GetBackwardHistory();
630 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forward
= m_browser
->GetForwardHistory();
635 for(i
= 0; i
< back
.size(); i
++)
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 );
643 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, m_browser
->GetCurrentTitle());
646 //No need to connect the current item
647 m_histMenuItems
[item
->GetId()] = wxSharedPtr
<wxWebViewHistoryItem
>(new wxWebViewHistoryItem(m_browser
->GetCurrentURL(), m_browser
->GetCurrentTitle()));
649 for(i
= 0; i
< forward
.size(); i
++)
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 );
657 wxPoint position
= ScreenToClient( wxGetMousePosition() );
658 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
662 * Invoked when user selects the zoom size in the menu
664 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
666 if (evt
.GetId() == m_tools_tiny
->GetId())
668 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
670 else if (evt
.GetId() == m_tools_small
->GetId())
672 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
674 else if (evt
.GetId() == m_tools_medium
->GetId())
676 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
678 else if (evt
.GetId() == m_tools_large
->GetId())
680 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
682 else if (evt
.GetId() == m_tools_largest
->GetId())
684 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
692 void WebFrame::OnZoomLayout(wxCommandEvent
& WXUNUSED(evt
))
694 if(m_tools_layout
->IsChecked())
695 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
697 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
700 void WebFrame::OnHistory(wxCommandEvent
& evt
)
702 m_browser
->LoadHistoryItem(m_histMenuItems
[evt
.GetId()]);
705 void WebFrame::OnRunScript(wxCommandEvent
& WXUNUSED(evt
))
707 wxTextEntryDialog
dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr
, "", wxOK
|wxCANCEL
|wxCENTRE
|wxTE_MULTILINE
);
708 if(dialog
.ShowModal() == wxID_OK
)
710 m_browser
->RunScript(dialog
.GetValue());
714 void WebFrame::OnClearSelection(wxCommandEvent
& WXUNUSED(evt
))
716 m_browser
->ClearSelection();
719 void WebFrame::OnDeleteSelection(wxCommandEvent
& WXUNUSED(evt
))
721 m_browser
->DeleteSelection();
724 void WebFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(evt
))
726 m_browser
->SelectAll();
730 * Callback invoked when a loading error occurs
732 void WebFrame::OnError(wxWebViewEvent
& evt
)
734 wxString errorCategory
;
735 switch (evt
.GetInt())
737 case wxWEB_NAV_ERR_CONNECTION
:
738 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
741 case wxWEB_NAV_ERR_CERTIFICATE
:
742 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
745 case wxWEB_NAV_ERR_AUTH
:
746 errorCategory
= "wxWEB_NAV_ERR_AUTH";
749 case wxWEB_NAV_ERR_SECURITY
:
750 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
753 case wxWEB_NAV_ERR_NOT_FOUND
:
754 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
757 case wxWEB_NAV_ERR_REQUEST
:
758 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
761 case wxWEB_NAV_ERR_USER_CANCELLED
:
762 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
765 case wxWEB_NAV_ERR_OTHER
:
766 errorCategory
= "wxWEB_NAV_ERR_OTHER";
770 wxLogMessage("%s", "Error; url='" + evt
.GetURL() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
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
);
780 * Invoked when user selects "Print" from the menu
782 void WebFrame::OnPrint(wxCommandEvent
& WXUNUSED(evt
))
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
)
792 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
793 text
->SetMarginWidth(1, 30);
794 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
795 text
->SetText(source
);
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));
808 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
809 sizer
->Add(text
, 1, wxEXPAND
);