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
72 void OnAnimationTimer(wxTimerEvent
& 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 int m_animation_angle
;
140 wxStaticText
* m_info_text
;
142 wxMenuHistoryMap m_histMenuItems
;
145 class SourceViewDialog
: public wxDialog
148 SourceViewDialog(wxWindow
* parent
, wxString source
);
151 IMPLEMENT_APP(WebApp
)
153 // ============================================================================
155 // ============================================================================
157 bool WebApp::OnInit()
159 if ( !wxApp::OnInit() )
162 WebFrame
*frame
= new WebFrame();
168 WebFrame::WebFrame() : wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
170 //Required from virtual file system archive support
171 wxFileSystem::AddHandler(new wxArchiveFSHandler
);
173 // set the frame icon
174 SetIcon(wxICON(sample
));
175 SetTitle("wxWebView Sample");
178 m_animation_angle
= 0;
181 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
183 // Create the toolbar
184 m_toolbar
= CreateToolBar(wxTB_TEXT
);
185 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
187 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
188 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
190 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
192 wxBitmap stop
= wxBitmap(stop_xpm
);
195 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
197 wxBitmap refresh
= wxBitmap(refresh_xpm
);
200 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), back
);
201 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), forward
);
202 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), stop
);
203 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), refresh
);
204 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
205 m_toolbar
->AddControl(m_url
, _("URL"));
206 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
208 m_toolbar
->Realize();
210 // Create the info panel
211 m_info
= new wxInfoBar(this);
212 topsizer
->Add(m_info
, wxSizerFlags().Expand());
214 // Create the webview
215 m_browser
= wxWebView::New(this, wxID_ANY
, "http://www.wxwidgets.org");
216 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
218 //We register the wxfs:// protocol for testing purposes
219 m_browser
->RegisterHandler(wxSharedPtr
<wxWebViewHandler
>(new wxWebViewArchiveHandler("wxfs")));
223 //Set a more sensible size for web browsing
224 SetSize(wxSize(800, 600));
226 // Create a log window
227 new wxLogWindow(this, _("Logging"));
229 // Create the Tools menu
230 m_tools_menu
= new wxMenu();
231 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
232 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
233 m_tools_menu
->AppendSeparator();
234 m_tools_layout
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Use Layout Zoom"));
235 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
236 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
237 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
238 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
239 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
240 m_tools_menu
->AppendSeparator();
241 m_tools_handle_navigation
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle Navigation"));
242 m_tools_handle_new_window
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle New Windows"));
243 m_tools_menu
->AppendSeparator();
246 m_tools_history_menu
= new wxMenu();
247 wxMenuItem
* clearhist
= m_tools_history_menu
->Append(wxID_ANY
, _("Clear History"));
248 m_tools_enable_history
= m_tools_history_menu
->AppendCheckItem(wxID_ANY
, _("Enable History"));
249 m_tools_history_menu
->AppendSeparator();
251 m_tools_menu
->AppendSubMenu(m_tools_history_menu
, "History");
253 //Create an editing menu
254 wxMenu
* editmenu
= new wxMenu();
255 m_edit_cut
= editmenu
->Append(wxID_ANY
, _("Cut"));
256 m_edit_copy
= editmenu
->Append(wxID_ANY
, _("Copy"));
257 m_edit_paste
= editmenu
->Append(wxID_ANY
, _("Paste"));
258 editmenu
->AppendSeparator();
259 m_edit_undo
= editmenu
->Append(wxID_ANY
, _("Undo"));
260 m_edit_redo
= editmenu
->Append(wxID_ANY
, _("Redo"));
261 editmenu
->AppendSeparator();
262 m_edit_mode
= editmenu
->AppendCheckItem(wxID_ANY
, _("Edit Mode"));
264 m_tools_menu
->AppendSeparator();
265 m_tools_menu
->AppendSubMenu(editmenu
, "Edit");
267 wxMenuItem
* script
= m_tools_menu
->Append(wxID_ANY
, _("Run Script"));
270 wxMenu
* selection
= new wxMenu();
271 m_selection_clear
= selection
->Append(wxID_ANY
, _("Clear Selection"));
272 m_selection_delete
= selection
->Append(wxID_ANY
, _("Delete Selection"));
273 wxMenuItem
* selectall
= selection
->Append(wxID_ANY
, _("Select All"));
275 editmenu
->AppendSubMenu(selection
, "Selection");
277 wxMenuItem
* loadscheme
= m_tools_menu
->Append(wxID_ANY
, _("Custom Scheme Example"));
279 //By default we want to handle navigation and new windows
280 m_tools_handle_navigation
->Check();
281 m_tools_handle_new_window
->Check();
282 m_tools_enable_history
->Check();
283 if(!m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
))
284 m_tools_layout
->Enable(false);
287 // Connect the toolbar events
288 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
289 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
290 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
291 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
292 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
293 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
294 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
295 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
296 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
297 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
299 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
300 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
302 // Connect the webview events
303 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
304 wxWebViewEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
305 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
306 wxWebViewEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
307 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
308 wxWebViewEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
309 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
310 wxWebViewEventHandler(WebFrame::OnError
), NULL
, this);
311 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
312 wxWebViewEventHandler(WebFrame::OnNewWindow
), NULL
, this);
313 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
314 wxWebViewEventHandler(WebFrame::OnTitleChanged
), NULL
, this);
316 // Connect the menu events
317 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
318 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
319 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
320 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
321 Connect(m_tools_layout
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
322 wxCommandEventHandler(WebFrame::OnZoomLayout
), NULL
, this );
323 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
324 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
325 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
326 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
327 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
328 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
329 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
330 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
331 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
332 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
333 Connect(clearhist
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
334 wxCommandEventHandler(WebFrame::OnClearHistory
), NULL
, this );
335 Connect(m_tools_enable_history
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
336 wxCommandEventHandler(WebFrame::OnEnableHistory
), NULL
, this );
337 Connect(m_edit_cut
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
338 wxCommandEventHandler(WebFrame::OnCut
), NULL
, this );
339 Connect(m_edit_copy
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
340 wxCommandEventHandler(WebFrame::OnCopy
), NULL
, this );
341 Connect(m_edit_paste
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
342 wxCommandEventHandler(WebFrame::OnPaste
), NULL
, this );
343 Connect(m_edit_undo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
344 wxCommandEventHandler(WebFrame::OnUndo
), NULL
, this );
345 Connect(m_edit_redo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
346 wxCommandEventHandler(WebFrame::OnRedo
), NULL
, this );
347 Connect(m_edit_mode
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
348 wxCommandEventHandler(WebFrame::OnMode
), NULL
, this );
349 Connect(script
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
350 wxCommandEventHandler(WebFrame::OnRunScript
), NULL
, this );
351 Connect(m_selection_clear
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
352 wxCommandEventHandler(WebFrame::OnClearSelection
), NULL
, this );
353 Connect(m_selection_delete
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
354 wxCommandEventHandler(WebFrame::OnDeleteSelection
), NULL
, this );
355 Connect(selectall
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
356 wxCommandEventHandler(WebFrame::OnSelectAll
), NULL
, this );
357 Connect(loadscheme
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
358 wxCommandEventHandler(WebFrame::OnLoadScheme
), NULL
, this );
361 WebFrame::~WebFrame()
367 void WebFrame::OnAnimationTimer(wxTimerEvent
& WXUNUSED(evt
))
369 m_animation_angle
+= 15;
370 if (m_animation_angle
> 360) m_animation_angle
-= 360;
372 wxBitmap
image(24, 24);
375 dc
.SelectObject(image
);
376 dc
.SetBackground(wxBrush(wxColour(255,0,255)));
379 if (m_animation_angle
>= 0 && m_animation_angle
<= 180)
381 dc
.SetBrush(*wxYELLOW_BRUSH
);
382 dc
.SetPen(*wxYELLOW_PEN
);
383 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
384 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
387 dc
.DrawBitmap(wxBitmap(wxlogo_xpm
), 0, 0, true);
389 if (m_animation_angle
> 180)
391 dc
.SetBrush(*wxYELLOW_BRUSH
);
392 dc
.SetPen(*wxYELLOW_PEN
);
393 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
394 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
397 image
.SetMask(new wxMask(image
, wxColour(255,0,255)));
398 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), image
);
402 * Method that retrieves the current state from the web control and updates the GUI
403 * the reflect this current state.
405 void WebFrame::UpdateState()
407 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
408 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
410 if (m_browser
->IsBusy())
414 m_timer
= new wxTimer(this);
415 this->Connect(wxEVT_TIMER
, wxTimerEventHandler(WebFrame::OnAnimationTimer
), NULL
, this);
417 m_timer
->Start(100); // start animation timer
419 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
423 if (m_timer
!= NULL
) m_timer
->Stop(); // stop animation timer
424 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), wxBitmap(wxlogo_xpm
));
425 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
428 SetTitle( m_browser
->GetCurrentTitle() );
429 m_url
->SetValue( m_browser
->GetCurrentURL() );
433 * Callback invoked when user entered an URL and pressed enter
435 void WebFrame::OnUrl(wxCommandEvent
& WXUNUSED(evt
))
437 m_browser
->LoadURL( m_url
->GetValue() );
442 * Callback invoked when user pressed the "back" button
444 void WebFrame::OnBack(wxCommandEvent
& WXUNUSED(evt
))
451 * Callback invoked when user pressed the "forward" button
453 void WebFrame::OnForward(wxCommandEvent
& WXUNUSED(evt
))
455 m_browser
->GoForward();
460 * Callback invoked when user pressed the "stop" button
462 void WebFrame::OnStop(wxCommandEvent
& WXUNUSED(evt
))
469 * Callback invoked when user pressed the "reload" button
471 void WebFrame::OnReload(wxCommandEvent
& WXUNUSED(evt
))
477 void WebFrame::OnClearHistory(wxCommandEvent
& WXUNUSED(evt
))
479 m_browser
->ClearHistory();
483 void WebFrame::OnEnableHistory(wxCommandEvent
& WXUNUSED(evt
))
485 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
489 void WebFrame::OnCut(wxCommandEvent
& WXUNUSED(evt
))
494 void WebFrame::OnCopy(wxCommandEvent
& WXUNUSED(evt
))
499 void WebFrame::OnPaste(wxCommandEvent
& WXUNUSED(evt
))
504 void WebFrame::OnUndo(wxCommandEvent
& WXUNUSED(evt
))
509 void WebFrame::OnRedo(wxCommandEvent
& WXUNUSED(evt
))
514 void WebFrame::OnMode(wxCommandEvent
& WXUNUSED(evt
))
516 m_browser
->SetEditable(m_edit_mode
->IsChecked());
519 void WebFrame::OnLoadScheme(wxCommandEvent
& WXUNUSED(evt
))
521 wxFileName
helpfile("../help/doc.zip");
522 helpfile
.MakeAbsolute();
523 wxString path
= helpfile
.GetFullPath();
524 //Under MSW we need to flip the slashes
525 path
.Replace("\\", "/");
526 path
= "wxfs:///" + path
+ ";protocol=zip/doc.htm";
527 m_browser
->LoadURL(path
);
531 * Callback invoked when there is a request to load a new page (for instance
532 * when the user clicks a link)
534 void WebFrame::OnNavigationRequest(wxWebViewEvent
& evt
)
536 if(m_info
->IsShown())
541 wxLogMessage("%s", "Navigation request to '" + evt
.GetURL() + "' (target='" +
542 evt
.GetTarget() + "')");
544 wxASSERT(m_browser
->IsBusy());
546 //If we don't want to handle navigation then veto the event and navigation
547 //will not take place, we also need to stop the loading animation
548 if(!m_tools_handle_navigation
->IsChecked())
551 if (m_timer
!= NULL
) m_timer
->Stop(); // stop animation timer
552 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), wxBitmap(wxlogo_xpm
));
553 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
562 * Callback invoked when a navigation request was accepted
564 void WebFrame::OnNavigationComplete(wxWebViewEvent
& evt
)
566 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetURL() + "'");
571 * Callback invoked when a page is finished loading
573 void WebFrame::OnDocumentLoaded(wxWebViewEvent
& evt
)
575 //Only notify if the document is the main frame, not a subframe
576 if(evt
.GetURL() == m_browser
->GetCurrentURL())
578 wxLogMessage("%s", "Document loaded; url='" + evt
.GetURL() + "'");
584 * On new window, we veto to stop extra windows appearing
586 void WebFrame::OnNewWindow(wxWebViewEvent
& evt
)
588 wxLogMessage("%s", "New window; url='" + evt
.GetURL() + "'");
590 //If we handle new window events then just load them in this window as we
591 //are a single window browser
592 if(m_tools_handle_new_window
->IsChecked())
593 m_browser
->LoadURL(evt
.GetURL());
598 void WebFrame::OnTitleChanged(wxWebViewEvent
& evt
)
600 SetTitle(evt
.GetString());
601 wxLogMessage("%s", "Title changed; title='" + evt
.GetString() + "'");
605 * Invoked when user selects the "View Source" menu item
607 void WebFrame::OnViewSourceRequest(wxCommandEvent
& WXUNUSED(evt
))
609 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
614 * Invoked when user selects the "Menu" item
616 void WebFrame::OnToolsClicked(wxCommandEvent
& WXUNUSED(evt
))
618 if(m_browser
->GetCurrentURL() == "")
621 m_tools_tiny
->Check(false);
622 m_tools_small
->Check(false);
623 m_tools_medium
->Check(false);
624 m_tools_large
->Check(false);
625 m_tools_largest
->Check(false);
627 wxWebViewZoom zoom
= m_browser
->GetZoom();
630 case wxWEB_VIEW_ZOOM_TINY
:
631 m_tools_tiny
->Check();
633 case wxWEB_VIEW_ZOOM_SMALL
:
634 m_tools_small
->Check();
636 case wxWEB_VIEW_ZOOM_MEDIUM
:
637 m_tools_medium
->Check();
639 case wxWEB_VIEW_ZOOM_LARGE
:
640 m_tools_large
->Check();
642 case wxWEB_VIEW_ZOOM_LARGEST
:
643 m_tools_largest
->Check();
647 m_edit_cut
->Enable(m_browser
->CanCut());
648 m_edit_copy
->Enable(m_browser
->CanCopy());
649 m_edit_paste
->Enable(m_browser
->CanPaste());
651 m_edit_undo
->Enable(m_browser
->CanUndo());
652 m_edit_redo
->Enable(m_browser
->CanRedo());
654 m_selection_clear
->Enable(m_browser
->HasSelection());
655 m_selection_delete
->Enable(m_browser
->HasSelection());
657 //Firstly we clear the existing menu items, then we add the current ones
658 wxMenuHistoryMap::const_iterator it
;
659 for( it
= m_histMenuItems
.begin(); it
!= m_histMenuItems
.end(); ++it
)
661 m_tools_history_menu
->Destroy(it
->first
);
663 m_histMenuItems
.clear();
665 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > back
= m_browser
->GetBackwardHistory();
666 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forward
= m_browser
->GetForwardHistory();
671 for(i
= 0; i
< back
.size(); i
++)
673 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, back
[i
]->GetTitle());
674 m_histMenuItems
[item
->GetId()] = back
[i
];
675 Connect(item
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
676 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
679 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, m_browser
->GetCurrentTitle());
682 //No need to connect the current item
683 m_histMenuItems
[item
->GetId()] = wxSharedPtr
<wxWebViewHistoryItem
>(new wxWebViewHistoryItem(m_browser
->GetCurrentURL(), m_browser
->GetCurrentTitle()));
685 for(i
= 0; i
< forward
.size(); i
++)
687 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, forward
[i
]->GetTitle());
688 m_histMenuItems
[item
->GetId()] = forward
[i
];
689 Connect(item
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
690 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
693 wxPoint position
= ScreenToClient( wxGetMousePosition() );
694 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
698 * Invoked when user selects the zoom size in the menu
700 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
702 if (evt
.GetId() == m_tools_tiny
->GetId())
704 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
706 else if (evt
.GetId() == m_tools_small
->GetId())
708 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
710 else if (evt
.GetId() == m_tools_medium
->GetId())
712 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
714 else if (evt
.GetId() == m_tools_large
->GetId())
716 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
718 else if (evt
.GetId() == m_tools_largest
->GetId())
720 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
728 void WebFrame::OnZoomLayout(wxCommandEvent
& WXUNUSED(evt
))
730 if(m_tools_layout
->IsChecked())
731 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
733 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
736 void WebFrame::OnHistory(wxCommandEvent
& evt
)
738 m_browser
->LoadHistoryItem(m_histMenuItems
[evt
.GetId()]);
741 void WebFrame::OnRunScript(wxCommandEvent
& WXUNUSED(evt
))
743 wxTextEntryDialog
dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr
, "", wxOK
|wxCANCEL
|wxCENTRE
|wxTE_MULTILINE
);
744 if(dialog
.ShowModal() == wxID_OK
)
746 m_browser
->RunScript(dialog
.GetValue());
750 void WebFrame::OnClearSelection(wxCommandEvent
& WXUNUSED(evt
))
752 m_browser
->ClearSelection();
755 void WebFrame::OnDeleteSelection(wxCommandEvent
& WXUNUSED(evt
))
757 m_browser
->DeleteSelection();
760 void WebFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(evt
))
762 m_browser
->SelectAll();
766 * Callback invoked when a loading error occurs
768 void WebFrame::OnError(wxWebViewEvent
& evt
)
770 wxString errorCategory
;
771 switch (evt
.GetInt())
773 case wxWEB_NAV_ERR_CONNECTION
:
774 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
777 case wxWEB_NAV_ERR_CERTIFICATE
:
778 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
781 case wxWEB_NAV_ERR_AUTH
:
782 errorCategory
= "wxWEB_NAV_ERR_AUTH";
785 case wxWEB_NAV_ERR_SECURITY
:
786 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
789 case wxWEB_NAV_ERR_NOT_FOUND
:
790 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
793 case wxWEB_NAV_ERR_REQUEST
:
794 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
797 case wxWEB_NAV_ERR_USER_CANCELLED
:
798 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
801 case wxWEB_NAV_ERR_OTHER
:
802 errorCategory
= "wxWEB_NAV_ERR_OTHER";
806 wxLogMessage("Error; url='" + evt
.GetURL() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
808 //Show the info bar with an error
809 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetURL() + "\n" +
810 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
816 * Invoked when user selects "Print" from the menu
818 void WebFrame::OnPrint(wxCommandEvent
& WXUNUSED(evt
))
823 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
824 wxDialog(parent
, wxID_ANY
, "Source Code",
825 wxDefaultPosition
, wxSize(700,500),
826 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
828 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
829 text
->SetMarginWidth(1, 30);
830 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
831 text
->SetText(source
);
833 text
->StyleClearAll();
834 text
->SetLexer(wxSTC_LEX_HTML
);
835 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
836 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
837 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
838 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
839 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
840 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
841 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
842 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
844 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
845 sizer
->Add(text
, 1, wxEXPAND
);