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/cmdline.h"
31 #include "wx/notifmsg.h"
32 #include "wx/settings.h"
33 #include "wx/webview.h"
34 #include "wx/webviewarchivehandler.h"
35 #include "wx/infobar.h"
36 #include "wx/filesys.h"
37 #include "wx/fs_arc.h"
39 #ifndef wxHAS_IMAGES_IN_RESOURCES
40 #include "../sample.xpm"
44 #include "wx/stc/stc.h"
46 #error "wxStyledTextControl is needed by this sample"
49 #if defined(__WXMSW__) || defined(__WXOSX__)
51 #include "refresh.xpm"
57 //We map menu items to their history items
58 WX_DECLARE_HASH_MAP(int, wxSharedPtr
<wxWebViewHistoryItem
>,
59 wxIntegerHash
, wxIntegerEqual
, wxMenuHistoryMap
);
61 class WebApp
: public wxApp
65 m_url("http://www.wxwidgets.org")
69 virtual bool OnInit();
71 #if wxUSE_CMDLINE_PARSER
72 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
74 wxApp
::OnInitCmdLine(parser
);
76 parser
.AddParam("URL to open",
77 wxCMD_LINE_VAL_STRING
,
78 wxCMD_LINE_PARAM_OPTIONAL
);
81 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
83 if ( !wxApp
::OnCmdLineParsed(parser
) )
86 if ( parser
.GetParamCount() )
87 m_url
= parser
.GetParam(0);
91 #endif // wxUSE_CMDLINE_PARSER
97 class WebFrame
: public wxFrame
100 WebFrame(const wxString
& url
);
104 void OnIdle(wxIdleEvent
& evt
);
105 void OnUrl(wxCommandEvent
& evt
);
106 void OnBack(wxCommandEvent
& evt
);
107 void OnForward(wxCommandEvent
& evt
);
108 void OnStop(wxCommandEvent
& evt
);
109 void OnReload(wxCommandEvent
& evt
);
110 void OnClearHistory(wxCommandEvent
& evt
);
111 void OnEnableHistory(wxCommandEvent
& evt
);
112 void OnNavigationRequest(wxWebViewEvent
& evt
);
113 void OnNavigationComplete(wxWebViewEvent
& evt
);
114 void OnDocumentLoaded(wxWebViewEvent
& evt
);
115 void OnNewWindow(wxWebViewEvent
& evt
);
116 void OnTitleChanged(wxWebViewEvent
& evt
);
117 void OnViewSourceRequest(wxCommandEvent
& evt
);
118 void OnToolsClicked(wxCommandEvent
& evt
);
119 void OnSetZoom(wxCommandEvent
& evt
);
120 void OnError(wxWebViewEvent
& evt
);
121 void OnPrint(wxCommandEvent
& evt
);
122 void OnCut(wxCommandEvent
& evt
);
123 void OnCopy(wxCommandEvent
& evt
);
124 void OnPaste(wxCommandEvent
& evt
);
125 void OnUndo(wxCommandEvent
& evt
);
126 void OnRedo(wxCommandEvent
& evt
);
127 void OnMode(wxCommandEvent
& evt
);
128 void OnZoomLayout(wxCommandEvent
& evt
);
129 void OnHistory(wxCommandEvent
& evt
);
130 void OnRunScript(wxCommandEvent
& evt
);
131 void OnClearSelection(wxCommandEvent
& evt
);
132 void OnDeleteSelection(wxCommandEvent
& evt
);
133 void OnSelectAll(wxCommandEvent
& evt
);
134 void OnLoadScheme(wxCommandEvent
& evt
);
138 wxWebView
* m_browser
;
140 wxToolBar
* m_toolbar
;
141 wxToolBarToolBase
* m_toolbar_back
;
142 wxToolBarToolBase
* m_toolbar_forward
;
143 wxToolBarToolBase
* m_toolbar_stop
;
144 wxToolBarToolBase
* m_toolbar_reload
;
145 wxToolBarToolBase
* m_toolbar_tools
;
147 wxMenu
* m_tools_menu
;
148 wxMenu
* m_tools_history_menu
;
149 wxMenuItem
* m_tools_layout
;
150 wxMenuItem
* m_tools_tiny
;
151 wxMenuItem
* m_tools_small
;
152 wxMenuItem
* m_tools_medium
;
153 wxMenuItem
* m_tools_large
;
154 wxMenuItem
* m_tools_largest
;
155 wxMenuItem
* m_tools_handle_navigation
;
156 wxMenuItem
* m_tools_handle_new_window
;
157 wxMenuItem
* m_tools_enable_history
;
158 wxMenuItem
* m_edit_cut
;
159 wxMenuItem
* m_edit_copy
;
160 wxMenuItem
* m_edit_paste
;
161 wxMenuItem
* m_edit_undo
;
162 wxMenuItem
* m_edit_redo
;
163 wxMenuItem
* m_edit_mode
;
164 wxMenuItem
* m_selection_clear
;
165 wxMenuItem
* m_selection_delete
;
168 wxStaticText
* m_info_text
;
170 wxMenuHistoryMap m_histMenuItems
;
173 class SourceViewDialog
: public wxDialog
176 SourceViewDialog(wxWindow
* parent
, wxString source
);
179 IMPLEMENT_APP(WebApp
)
181 // ============================================================================
183 // ============================================================================
185 bool WebApp
::OnInit()
187 if ( !wxApp
::OnInit() )
190 WebFrame
*frame
= new WebFrame(m_url
);
196 WebFrame
::WebFrame(const wxString
& url
) :
197 wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
199 //Required from virtual file system archive support
200 wxFileSystem
::AddHandler(new wxArchiveFSHandler
);
202 // set the frame icon
203 SetIcon(wxICON(sample
));
204 SetTitle("wxWebView Sample");
206 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
208 // Create the toolbar
209 m_toolbar
= CreateToolBar(wxTB_TEXT
);
210 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
212 wxBitmap back
= wxArtProvider
::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
213 wxBitmap forward
= wxArtProvider
::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
215 wxBitmap stop
= wxArtProvider
::GetBitmap("gtk-stop", wxART_TOOLBAR
);
217 wxBitmap stop
= wxBitmap(stop_xpm
);
220 wxBitmap refresh
= wxArtProvider
::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
222 wxBitmap refresh
= wxBitmap(refresh_xpm
);
225 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), back
);
226 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), forward
);
227 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), stop
);
228 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), refresh
);
229 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
230 m_toolbar
->AddControl(m_url
, _("URL"));
231 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
233 m_toolbar
->Realize();
235 // Create the info panel
236 m_info
= new wxInfoBar(this);
237 topsizer
->Add(m_info
, wxSizerFlags().Expand());
239 // Create the webview
240 m_browser
= wxWebView
::New(this, wxID_ANY
, url
);
241 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
243 //We register the wxfs:// protocol for testing purposes
244 m_browser
->RegisterHandler(wxSharedPtr
<wxWebViewHandler
>(new wxWebViewArchiveHandler("wxfs")));
248 //Set a more sensible size for web browsing
249 SetSize(wxSize(800, 600));
251 // Create a log window
252 new wxLogWindow(this, _("Logging"));
254 // Create the Tools menu
255 m_tools_menu
= new wxMenu();
256 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
257 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
258 m_tools_menu
->AppendSeparator();
259 m_tools_layout
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Use Layout Zoom"));
260 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
261 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
262 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
263 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
264 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
265 m_tools_menu
->AppendSeparator();
266 m_tools_handle_navigation
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle Navigation"));
267 m_tools_handle_new_window
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle New Windows"));
268 m_tools_menu
->AppendSeparator();
271 m_tools_history_menu
= new wxMenu();
272 wxMenuItem
* clearhist
= m_tools_history_menu
->Append(wxID_ANY
, _("Clear History"));
273 m_tools_enable_history
= m_tools_history_menu
->AppendCheckItem(wxID_ANY
, _("Enable History"));
274 m_tools_history_menu
->AppendSeparator();
276 m_tools_menu
->AppendSubMenu(m_tools_history_menu
, "History");
278 //Create an editing menu
279 wxMenu
* editmenu
= new wxMenu();
280 m_edit_cut
= editmenu
->Append(wxID_ANY
, _("Cut"));
281 m_edit_copy
= editmenu
->Append(wxID_ANY
, _("Copy"));
282 m_edit_paste
= editmenu
->Append(wxID_ANY
, _("Paste"));
283 editmenu
->AppendSeparator();
284 m_edit_undo
= editmenu
->Append(wxID_ANY
, _("Undo"));
285 m_edit_redo
= editmenu
->Append(wxID_ANY
, _("Redo"));
286 editmenu
->AppendSeparator();
287 m_edit_mode
= editmenu
->AppendCheckItem(wxID_ANY
, _("Edit Mode"));
289 m_tools_menu
->AppendSeparator();
290 m_tools_menu
->AppendSubMenu(editmenu
, "Edit");
292 wxMenuItem
* script
= m_tools_menu
->Append(wxID_ANY
, _("Run Script"));
295 wxMenu
* selection
= new wxMenu();
296 m_selection_clear
= selection
->Append(wxID_ANY
, _("Clear Selection"));
297 m_selection_delete
= selection
->Append(wxID_ANY
, _("Delete Selection"));
298 wxMenuItem
* selectall
= selection
->Append(wxID_ANY
, _("Select All"));
300 editmenu
->AppendSubMenu(selection
, "Selection");
302 wxMenuItem
* loadscheme
= m_tools_menu
->Append(wxID_ANY
, _("Custom Scheme Example"));
304 //By default we want to handle navigation and new windows
305 m_tools_handle_navigation
->Check();
306 m_tools_handle_new_window
->Check();
307 m_tools_enable_history
->Check();
308 if(!m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
))
309 m_tools_layout
->Enable(false);
312 // Connect the toolbar events
313 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
314 wxCommandEventHandler(WebFrame
::OnBack
), NULL
, this );
315 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
316 wxCommandEventHandler(WebFrame
::OnForward
), NULL
, this );
317 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
318 wxCommandEventHandler(WebFrame
::OnStop
), NULL
, this );
319 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
320 wxCommandEventHandler(WebFrame
::OnReload
),NULL
, this );
321 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
322 wxCommandEventHandler(WebFrame
::OnToolsClicked
), NULL
, this );
324 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
325 wxCommandEventHandler(WebFrame
::OnUrl
), NULL
, this );
327 // Connect the webview events
328 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
329 wxWebViewEventHandler(WebFrame
::OnNavigationRequest
), NULL
, this);
330 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
331 wxWebViewEventHandler(WebFrame
::OnNavigationComplete
), NULL
, this);
332 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
333 wxWebViewEventHandler(WebFrame
::OnDocumentLoaded
), NULL
, this);
334 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
335 wxWebViewEventHandler(WebFrame
::OnError
), NULL
, this);
336 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
337 wxWebViewEventHandler(WebFrame
::OnNewWindow
), NULL
, this);
338 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
339 wxWebViewEventHandler(WebFrame
::OnTitleChanged
), NULL
, this);
341 // Connect the menu events
342 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
343 wxCommandEventHandler(WebFrame
::OnViewSourceRequest
), NULL
, this );
344 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
345 wxCommandEventHandler(WebFrame
::OnPrint
), NULL
, this );
346 Connect(m_tools_layout
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
347 wxCommandEventHandler(WebFrame
::OnZoomLayout
), NULL
, this );
348 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
349 wxCommandEventHandler(WebFrame
::OnSetZoom
), NULL
, this );
350 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
351 wxCommandEventHandler(WebFrame
::OnSetZoom
), NULL
, this );
352 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
353 wxCommandEventHandler(WebFrame
::OnSetZoom
), NULL
, this );
354 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
355 wxCommandEventHandler(WebFrame
::OnSetZoom
), NULL
, this );
356 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
357 wxCommandEventHandler(WebFrame
::OnSetZoom
), NULL
, this );
358 Connect(clearhist
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
359 wxCommandEventHandler(WebFrame
::OnClearHistory
), NULL
, this );
360 Connect(m_tools_enable_history
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
361 wxCommandEventHandler(WebFrame
::OnEnableHistory
), NULL
, this );
362 Connect(m_edit_cut
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
363 wxCommandEventHandler(WebFrame
::OnCut
), NULL
, this );
364 Connect(m_edit_copy
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
365 wxCommandEventHandler(WebFrame
::OnCopy
), NULL
, this );
366 Connect(m_edit_paste
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
367 wxCommandEventHandler(WebFrame
::OnPaste
), NULL
, this );
368 Connect(m_edit_undo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
369 wxCommandEventHandler(WebFrame
::OnUndo
), NULL
, this );
370 Connect(m_edit_redo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
371 wxCommandEventHandler(WebFrame
::OnRedo
), NULL
, this );
372 Connect(m_edit_mode
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
373 wxCommandEventHandler(WebFrame
::OnMode
), NULL
, this );
374 Connect(script
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
375 wxCommandEventHandler(WebFrame
::OnRunScript
), NULL
, this );
376 Connect(m_selection_clear
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
377 wxCommandEventHandler(WebFrame
::OnClearSelection
), NULL
, this );
378 Connect(m_selection_delete
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
379 wxCommandEventHandler(WebFrame
::OnDeleteSelection
), NULL
, this );
380 Connect(selectall
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
381 wxCommandEventHandler(WebFrame
::OnSelectAll
), NULL
, this );
382 Connect(loadscheme
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
383 wxCommandEventHandler(WebFrame
::OnLoadScheme
), NULL
, this );
385 //Connect the idle events
386 Connect(wxID_ANY
, wxEVT_IDLE
, wxIdleEventHandler(WebFrame
::OnIdle
), NULL
, this);
389 WebFrame
::~WebFrame()
395 * Method that retrieves the current state from the web control and updates the GUI
396 * the reflect this current state.
398 void WebFrame
::UpdateState()
400 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
401 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
403 if (m_browser
->IsBusy())
405 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
409 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
412 SetTitle( m_browser
->GetCurrentTitle() );
413 m_url
->SetValue( m_browser
->GetCurrentURL() );
416 void WebFrame
::OnIdle(wxIdleEvent
& WXUNUSED(evt
))
418 if(m_browser
->IsBusy())
420 wxSetCursor(wxCURSOR_ARROWWAIT
);
421 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), true);
425 wxSetCursor(wxNullCursor
);
426 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), false);
431 * Callback invoked when user entered an URL and pressed enter
433 void WebFrame
::OnUrl(wxCommandEvent
& WXUNUSED(evt
))
435 m_browser
->LoadURL( m_url
->GetValue() );
436 m_browser
->SetFocus();
441 * Callback invoked when user pressed the "back" button
443 void WebFrame
::OnBack(wxCommandEvent
& WXUNUSED(evt
))
450 * Callback invoked when user pressed the "forward" button
452 void WebFrame
::OnForward(wxCommandEvent
& WXUNUSED(evt
))
454 m_browser
->GoForward();
459 * Callback invoked when user pressed the "stop" button
461 void WebFrame
::OnStop(wxCommandEvent
& WXUNUSED(evt
))
468 * Callback invoked when user pressed the "reload" button
470 void WebFrame
::OnReload(wxCommandEvent
& WXUNUSED(evt
))
476 void WebFrame
::OnClearHistory(wxCommandEvent
& WXUNUSED(evt
))
478 m_browser
->ClearHistory();
482 void WebFrame
::OnEnableHistory(wxCommandEvent
& WXUNUSED(evt
))
484 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
488 void WebFrame
::OnCut(wxCommandEvent
& WXUNUSED(evt
))
493 void WebFrame
::OnCopy(wxCommandEvent
& WXUNUSED(evt
))
498 void WebFrame
::OnPaste(wxCommandEvent
& WXUNUSED(evt
))
503 void WebFrame
::OnUndo(wxCommandEvent
& WXUNUSED(evt
))
508 void WebFrame
::OnRedo(wxCommandEvent
& WXUNUSED(evt
))
513 void WebFrame
::OnMode(wxCommandEvent
& WXUNUSED(evt
))
515 m_browser
->SetEditable(m_edit_mode
->IsChecked());
518 void WebFrame
::OnLoadScheme(wxCommandEvent
& WXUNUSED(evt
))
520 wxFileName
helpfile("../help/doc.zip");
521 helpfile
.MakeAbsolute();
522 wxString path
= helpfile
.GetFullPath();
523 //Under MSW we need to flip the slashes
524 path
.Replace("\\", "/");
525 path
= "wxfs:///" + path
+ ";protocol=zip/doc.htm";
526 m_browser
->LoadURL(path
);
530 * Callback invoked when there is a request to load a new page (for instance
531 * when the user clicks a link)
533 void WebFrame
::OnNavigationRequest(wxWebViewEvent
& evt
)
535 if(m_info
->IsShown())
540 wxLogMessage("%s", "Navigation request to '" + evt
.GetURL() + "' (target='" +
541 evt
.GetTarget() + "')");
543 wxASSERT(m_browser
->IsBusy());
545 //If we don't want to handle navigation then veto the event and navigation
546 //will not take place, we also need to stop the loading animation
547 if(!m_tools_handle_navigation
->IsChecked())
550 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
559 * Callback invoked when a navigation request was accepted
561 void WebFrame
::OnNavigationComplete(wxWebViewEvent
& evt
)
563 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetURL() + "'");
568 * Callback invoked when a page is finished loading
570 void WebFrame
::OnDocumentLoaded(wxWebViewEvent
& evt
)
572 //Only notify if the document is the main frame, not a subframe
573 if(evt
.GetURL() == m_browser
->GetCurrentURL())
575 wxLogMessage("%s", "Document loaded; url='" + evt
.GetURL() + "'");
581 * On new window, we veto to stop extra windows appearing
583 void WebFrame
::OnNewWindow(wxWebViewEvent
& evt
)
585 wxLogMessage("%s", "New window; url='" + evt
.GetURL() + "'");
587 //If we handle new window events then just load them in this window as we
588 //are a single window browser
589 if(m_tools_handle_new_window
->IsChecked())
590 m_browser
->LoadURL(evt
.GetURL());
595 void WebFrame
::OnTitleChanged(wxWebViewEvent
& evt
)
597 SetTitle(evt
.GetString());
598 wxLogMessage("%s", "Title changed; title='" + evt
.GetString() + "'");
602 * Invoked when user selects the "View Source" menu item
604 void WebFrame
::OnViewSourceRequest(wxCommandEvent
& WXUNUSED(evt
))
606 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
611 * Invoked when user selects the "Menu" item
613 void WebFrame
::OnToolsClicked(wxCommandEvent
& WXUNUSED(evt
))
615 if(m_browser
->GetCurrentURL() == "")
618 m_tools_tiny
->Check(false);
619 m_tools_small
->Check(false);
620 m_tools_medium
->Check(false);
621 m_tools_large
->Check(false);
622 m_tools_largest
->Check(false);
624 wxWebViewZoom zoom
= m_browser
->GetZoom();
627 case wxWEB_VIEW_ZOOM_TINY
:
628 m_tools_tiny
->Check();
630 case wxWEB_VIEW_ZOOM_SMALL
:
631 m_tools_small
->Check();
633 case wxWEB_VIEW_ZOOM_MEDIUM
:
634 m_tools_medium
->Check();
636 case wxWEB_VIEW_ZOOM_LARGE
:
637 m_tools_large
->Check();
639 case wxWEB_VIEW_ZOOM_LARGEST
:
640 m_tools_largest
->Check();
644 m_edit_cut
->Enable(m_browser
->CanCut());
645 m_edit_copy
->Enable(m_browser
->CanCopy());
646 m_edit_paste
->Enable(m_browser
->CanPaste());
648 m_edit_undo
->Enable(m_browser
->CanUndo());
649 m_edit_redo
->Enable(m_browser
->CanRedo());
651 m_selection_clear
->Enable(m_browser
->HasSelection());
652 m_selection_delete
->Enable(m_browser
->HasSelection());
654 //Firstly we clear the existing menu items, then we add the current ones
655 wxMenuHistoryMap
::const_iterator it
;
656 for( it
= m_histMenuItems
.begin(); it
!= m_histMenuItems
.end(); ++it
)
658 m_tools_history_menu
->Destroy(it
->first
);
660 m_histMenuItems
.clear();
662 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > back
= m_browser
->GetBackwardHistory();
663 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forward
= m_browser
->GetForwardHistory();
668 for(i
= 0; i
< back
.size(); i
++)
670 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, back
[i
]->GetTitle());
671 m_histMenuItems
[item
->GetId()] = back
[i
];
672 Connect(item
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
673 wxCommandEventHandler(WebFrame
::OnHistory
), NULL
, this );
676 wxString title
= m_browser
->GetCurrentTitle();
678 title
= "(untitled)";
679 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, title
);
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("%s", "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
);