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/webviewfshandler.h"
36 #include "wx/infobar.h"
37 #include "wx/filesys.h"
38 #include "wx/fs_arc.h"
39 #include "wx/fs_mem.h"
41 #ifndef wxHAS_IMAGES_IN_RESOURCES
42 #include "../sample.xpm"
46 #include "wx/stc/stc.h"
48 #error "wxStyledTextControl is needed by this sample"
51 #if defined(__WXMSW__) || defined(__WXOSX__)
53 #include "refresh.xpm"
59 //We map menu items to their history items
60 WX_DECLARE_HASH_MAP(int, wxSharedPtr
<wxWebViewHistoryItem
>,
61 wxIntegerHash
, wxIntegerEqual
, wxMenuHistoryMap
);
63 class WebApp
: public wxApp
67 m_url("http://www.wxwidgets.org")
71 virtual bool OnInit();
73 #if wxUSE_CMDLINE_PARSER
74 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
76 wxApp::OnInitCmdLine(parser
);
78 parser
.AddParam("URL to open",
79 wxCMD_LINE_VAL_STRING
,
80 wxCMD_LINE_PARAM_OPTIONAL
);
83 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
85 if ( !wxApp::OnCmdLineParsed(parser
) )
88 if ( parser
.GetParamCount() )
89 m_url
= parser
.GetParam(0);
93 #endif // wxUSE_CMDLINE_PARSER
99 class WebFrame
: public wxFrame
102 WebFrame(const wxString
& url
);
106 void OnIdle(wxIdleEvent
& evt
);
107 void OnUrl(wxCommandEvent
& evt
);
108 void OnBack(wxCommandEvent
& evt
);
109 void OnForward(wxCommandEvent
& evt
);
110 void OnStop(wxCommandEvent
& evt
);
111 void OnReload(wxCommandEvent
& evt
);
112 void OnClearHistory(wxCommandEvent
& evt
);
113 void OnEnableHistory(wxCommandEvent
& evt
);
114 void OnNavigationRequest(wxWebViewEvent
& evt
);
115 void OnNavigationComplete(wxWebViewEvent
& evt
);
116 void OnDocumentLoaded(wxWebViewEvent
& evt
);
117 void OnNewWindow(wxWebViewEvent
& evt
);
118 void OnTitleChanged(wxWebViewEvent
& evt
);
119 void OnViewSourceRequest(wxCommandEvent
& evt
);
120 void OnToolsClicked(wxCommandEvent
& evt
);
121 void OnSetZoom(wxCommandEvent
& evt
);
122 void OnError(wxWebViewEvent
& evt
);
123 void OnPrint(wxCommandEvent
& evt
);
124 void OnCut(wxCommandEvent
& evt
);
125 void OnCopy(wxCommandEvent
& evt
);
126 void OnPaste(wxCommandEvent
& evt
);
127 void OnUndo(wxCommandEvent
& evt
);
128 void OnRedo(wxCommandEvent
& evt
);
129 void OnMode(wxCommandEvent
& evt
);
130 void OnZoomLayout(wxCommandEvent
& evt
);
131 void OnHistory(wxCommandEvent
& evt
);
132 void OnScrollLineUp(wxCommandEvent
&) { m_browser
->LineUp(); }
133 void OnScrollLineDown(wxCommandEvent
&) { m_browser
->LineDown(); }
134 void OnScrollPageUp(wxCommandEvent
&) { m_browser
->PageUp(); }
135 void OnScrollPageDown(wxCommandEvent
&) { m_browser
->PageDown(); }
136 void OnRunScript(wxCommandEvent
& evt
);
137 void OnClearSelection(wxCommandEvent
& evt
);
138 void OnDeleteSelection(wxCommandEvent
& evt
);
139 void OnSelectAll(wxCommandEvent
& evt
);
140 void OnLoadScheme(wxCommandEvent
& evt
);
141 void OnUseMemoryFS(wxCommandEvent
& evt
);
142 void OnFind(wxCommandEvent
& evt
);
143 void OnFindDone(wxCommandEvent
& evt
);
144 void OnFindText(wxCommandEvent
& evt
);
145 void OnFindOptions(wxCommandEvent
& evt
);
146 void OnEnableContextMenu(wxCommandEvent
& evt
);
150 wxWebView
* m_browser
;
152 wxToolBar
* m_toolbar
;
153 wxToolBarToolBase
* m_toolbar_back
;
154 wxToolBarToolBase
* m_toolbar_forward
;
155 wxToolBarToolBase
* m_toolbar_stop
;
156 wxToolBarToolBase
* m_toolbar_reload
;
157 wxToolBarToolBase
* m_toolbar_tools
;
159 wxToolBarToolBase
* m_find_toolbar_done
;
160 wxToolBarToolBase
* m_find_toolbar_next
;
161 wxToolBarToolBase
* m_find_toolbar_previous
;
162 wxToolBarToolBase
* m_find_toolbar_options
;
163 wxMenuItem
* m_find_toolbar_wrap
;
164 wxMenuItem
* m_find_toolbar_highlight
;
165 wxMenuItem
* m_find_toolbar_matchcase
;
166 wxMenuItem
* m_find_toolbar_wholeword
;
168 wxMenu
* m_tools_menu
;
169 wxMenu
* m_tools_history_menu
;
170 wxMenuItem
* m_tools_layout
;
171 wxMenuItem
* m_tools_tiny
;
172 wxMenuItem
* m_tools_small
;
173 wxMenuItem
* m_tools_medium
;
174 wxMenuItem
* m_tools_large
;
175 wxMenuItem
* m_tools_largest
;
176 wxMenuItem
* m_tools_handle_navigation
;
177 wxMenuItem
* m_tools_handle_new_window
;
178 wxMenuItem
* m_tools_enable_history
;
179 wxMenuItem
* m_edit_cut
;
180 wxMenuItem
* m_edit_copy
;
181 wxMenuItem
* m_edit_paste
;
182 wxMenuItem
* m_edit_undo
;
183 wxMenuItem
* m_edit_redo
;
184 wxMenuItem
* m_edit_mode
;
185 wxMenuItem
* m_scroll_line_up
;
186 wxMenuItem
* m_scroll_line_down
;
187 wxMenuItem
* m_scroll_page_up
;
188 wxMenuItem
* m_scroll_page_down
;
189 wxMenuItem
* m_selection_clear
;
190 wxMenuItem
* m_selection_delete
;
192 wxMenuItem
* m_context_menu
;
195 wxStaticText
* m_info_text
;
196 wxTextCtrl
* m_find_ctrl
;
197 wxToolBar
* m_find_toolbar
;
199 wxMenuHistoryMap m_histMenuItems
;
201 int m_findFlags
, m_findCount
;
204 class SourceViewDialog
: public wxDialog
207 SourceViewDialog(wxWindow
* parent
, wxString source
);
210 IMPLEMENT_APP(WebApp
)
212 // ============================================================================
214 // ============================================================================
216 bool WebApp::OnInit()
218 if ( !wxApp::OnInit() )
221 //Required for virtual file system archive and memory support
222 wxFileSystem::AddHandler(new wxArchiveFSHandler
);
223 wxFileSystem::AddHandler(new wxMemoryFSHandler
);
225 // Create the memory files
226 wxImage::AddHandler(new wxPNGHandler
);
227 wxMemoryFSHandler::AddFile("logo.png",
228 wxBitmap(wxlogo_xpm
), wxBITMAP_TYPE_PNG
);
229 wxMemoryFSHandler::AddFile("page1.htm",
230 "<html><head><title>File System Example</title>"
231 "<link rel='stylesheet' type='text/css' href='memory:test.css'>"
232 "</head><body><h1>Page 1</h1>"
233 "<p><img src='memory:logo.png'></p>"
234 "<p>Some text about <a href='memory:page2.htm'>Page 2</a>.</p></body>");
235 wxMemoryFSHandler::AddFile("page2.htm",
236 "<html><head><title>File System Example</title>"
237 "<link rel='stylesheet' type='text/css' href='memory:test.css'>"
238 "</head><body><h1>Page 2</h1>"
239 "<p><a href='memory:page1.htm'>Page 1</a> was better.</p></body>");
240 wxMemoryFSHandler::AddFile("test.css", "h1 {color: red;}");
242 WebFrame
*frame
= new WebFrame(m_url
);
248 WebFrame::WebFrame(const wxString
& url
) :
249 wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
251 // set the frame icon
252 SetIcon(wxICON(sample
));
253 SetTitle("wxWebView Sample");
255 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
257 // Create the toolbar
258 m_toolbar
= CreateToolBar(wxTB_TEXT
);
259 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
261 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
262 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
264 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
266 wxBitmap stop
= wxBitmap(stop_xpm
);
269 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
271 wxBitmap refresh
= wxBitmap(refresh_xpm
);
274 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), back
);
275 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), forward
);
276 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), stop
);
277 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), refresh
);
278 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
279 m_toolbar
->AddControl(m_url
, _("URL"));
280 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
282 m_toolbar
->Realize();
285 m_findFlags
= wxWEBVIEW_FIND_DEFAULT
;
286 m_findText
= wxEmptyString
;
289 // Create panel for find toolbar.
290 wxPanel
* panel
= new wxPanel(this);
291 topsizer
->Add(panel
, wxSizerFlags().Expand());
293 // Create sizer for panel.
294 wxBoxSizer
* panel_sizer
= new wxBoxSizer(wxVERTICAL
);
295 panel
->SetSizer(panel_sizer
);
297 // Create the find toolbar.
298 m_find_toolbar
= new wxToolBar(panel
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTB_HORIZONTAL
|wxTB_TEXT
|wxTB_HORZ_LAYOUT
);
299 m_find_toolbar
->Hide();
300 panel_sizer
->Add(m_find_toolbar
, wxSizerFlags().Expand());
302 // Create find control.
303 m_find_ctrl
= new wxTextCtrl(m_find_toolbar
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxSize(140,-1), wxTE_PROCESS_ENTER
);
307 wxMenu
* findmenu
= new wxMenu
;
308 m_find_toolbar_wrap
= findmenu
->AppendCheckItem(wxID_ANY
,"Wrap");
309 m_find_toolbar_matchcase
= findmenu
->AppendCheckItem(wxID_ANY
,"Match Case");
310 m_find_toolbar_wholeword
= findmenu
->AppendCheckItem(wxID_ANY
,"Entire Word");
311 m_find_toolbar_highlight
= findmenu
->AppendCheckItem(wxID_ANY
,"Highlight");
312 // Add find toolbar tools.
313 m_find_toolbar
->SetToolSeparation(7);
314 m_find_toolbar_done
= m_find_toolbar
->AddTool(wxID_ANY
, "Close", wxArtProvider::GetBitmap(wxART_CROSS_MARK
));
315 m_find_toolbar
->AddSeparator();
316 m_find_toolbar
->AddControl(m_find_ctrl
, "Find");
317 m_find_toolbar
->AddSeparator();
318 m_find_toolbar_next
= m_find_toolbar
->AddTool(wxID_ANY
, "Next", wxArtProvider::GetBitmap(wxART_GO_DOWN
, wxART_TOOLBAR
, wxSize(16,16)));
319 m_find_toolbar_previous
= m_find_toolbar
->AddTool(wxID_ANY
, "Previous", wxArtProvider::GetBitmap(wxART_GO_UP
, wxART_TOOLBAR
, wxSize(16,16)));
320 m_find_toolbar
->AddSeparator();
321 m_find_toolbar_options
= m_find_toolbar
->AddTool(wxID_ANY
, "Options", wxArtProvider::GetBitmap(wxART_PLUS
, wxART_TOOLBAR
, wxSize(16,16)), "", wxITEM_DROPDOWN
);
322 m_find_toolbar_options
->SetDropdownMenu(findmenu
);
323 m_find_toolbar
->Realize();
325 // Create the info panel
326 m_info
= new wxInfoBar(this);
327 topsizer
->Add(m_info
, wxSizerFlags().Expand());
329 // Create the webview
330 m_browser
= wxWebView::New(this, wxID_ANY
, url
);
331 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
333 //We register the wxfs:// protocol for testing purposes
334 m_browser
->RegisterHandler(wxSharedPtr
<wxWebViewHandler
>(new wxWebViewArchiveHandler("wxfs")));
335 //And the memory: file system
336 m_browser
->RegisterHandler(wxSharedPtr
<wxWebViewHandler
>(new wxWebViewFSHandler("memory")));
340 //Set a more sensible size for web browsing
341 SetSize(wxSize(800, 600));
343 // Create a log window
344 new wxLogWindow(this, _("Logging"));
346 // Create the Tools menu
347 m_tools_menu
= new wxMenu();
348 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
349 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
350 m_tools_menu
->AppendSeparator();
351 m_tools_layout
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Use Layout Zoom"));
352 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
353 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
354 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
355 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
356 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
357 m_tools_menu
->AppendSeparator();
358 m_tools_handle_navigation
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle Navigation"));
359 m_tools_handle_new_window
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle New Windows"));
360 m_tools_menu
->AppendSeparator();
363 m_find
= m_tools_menu
->Append(wxID_ANY
, _("Find"));
364 m_tools_menu
->AppendSeparator();
367 m_tools_history_menu
= new wxMenu();
368 wxMenuItem
* clearhist
= m_tools_history_menu
->Append(wxID_ANY
, _("Clear History"));
369 m_tools_enable_history
= m_tools_history_menu
->AppendCheckItem(wxID_ANY
, _("Enable History"));
370 m_tools_history_menu
->AppendSeparator();
372 m_tools_menu
->AppendSubMenu(m_tools_history_menu
, "History");
374 //Create an editing menu
375 wxMenu
* editmenu
= new wxMenu();
376 m_edit_cut
= editmenu
->Append(wxID_ANY
, _("Cut"));
377 m_edit_copy
= editmenu
->Append(wxID_ANY
, _("Copy"));
378 m_edit_paste
= editmenu
->Append(wxID_ANY
, _("Paste"));
379 editmenu
->AppendSeparator();
380 m_edit_undo
= editmenu
->Append(wxID_ANY
, _("Undo"));
381 m_edit_redo
= editmenu
->Append(wxID_ANY
, _("Redo"));
382 editmenu
->AppendSeparator();
383 m_edit_mode
= editmenu
->AppendCheckItem(wxID_ANY
, _("Edit Mode"));
385 m_tools_menu
->AppendSeparator();
386 m_tools_menu
->AppendSubMenu(editmenu
, "Edit");
388 wxMenu
* scroll_menu
= new wxMenu
;
389 m_scroll_line_up
= scroll_menu
->Append(wxID_ANY
, "Line &up");
390 m_scroll_line_down
= scroll_menu
->Append(wxID_ANY
, "Line &down");
391 m_scroll_page_up
= scroll_menu
->Append(wxID_ANY
, "Page u&p");
392 m_scroll_page_down
= scroll_menu
->Append(wxID_ANY
, "Page d&own");
393 m_tools_menu
->AppendSubMenu(scroll_menu
, "Scroll");
395 wxMenuItem
* script
= m_tools_menu
->Append(wxID_ANY
, _("Run Script"));
398 wxMenu
* selection
= new wxMenu();
399 m_selection_clear
= selection
->Append(wxID_ANY
, _("Clear Selection"));
400 m_selection_delete
= selection
->Append(wxID_ANY
, _("Delete Selection"));
401 wxMenuItem
* selectall
= selection
->Append(wxID_ANY
, _("Select All"));
403 editmenu
->AppendSubMenu(selection
, "Selection");
405 wxMenuItem
* loadscheme
= m_tools_menu
->Append(wxID_ANY
, _("Custom Scheme Example"));
406 wxMenuItem
* usememoryfs
= m_tools_menu
->Append(wxID_ANY
, _("Memory File System Example"));
408 m_context_menu
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Enable Context Menu"));
410 //By default we want to handle navigation and new windows
411 m_tools_handle_navigation
->Check();
412 m_tools_handle_new_window
->Check();
413 m_tools_enable_history
->Check();
414 if(!m_browser
->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT
))
415 m_tools_layout
->Enable(false);
418 // Connect the toolbar events
419 Connect(m_toolbar_back
->GetId(), wxEVT_TOOL
,
420 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
421 Connect(m_toolbar_forward
->GetId(), wxEVT_TOOL
,
422 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
423 Connect(m_toolbar_stop
->GetId(), wxEVT_TOOL
,
424 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
425 Connect(m_toolbar_reload
->GetId(), wxEVT_TOOL
,
426 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
427 Connect(m_toolbar_tools
->GetId(), wxEVT_TOOL
,
428 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
430 Connect(m_url
->GetId(), wxEVT_TEXT_ENTER
,
431 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
433 // Connect find toolbar events.
434 Connect(m_find_toolbar_done
->GetId(), wxEVT_TOOL
,
435 wxCommandEventHandler(WebFrame::OnFindDone
), NULL
, this );
436 Connect(m_find_toolbar_next
->GetId(), wxEVT_TOOL
,
437 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
438 Connect(m_find_toolbar_previous
->GetId(), wxEVT_TOOL
,
439 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
441 // Connect find control events.
442 Connect(m_find_ctrl
->GetId(), wxEVT_TEXT
,
443 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
444 Connect(m_find_ctrl
->GetId(), wxEVT_TEXT_ENTER
,
445 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
447 // Connect the webview events
448 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_NAVIGATING
,
449 wxWebViewEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
450 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_NAVIGATED
,
451 wxWebViewEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
452 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_LOADED
,
453 wxWebViewEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
454 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_ERROR
,
455 wxWebViewEventHandler(WebFrame::OnError
), NULL
, this);
456 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_NEWWINDOW
,
457 wxWebViewEventHandler(WebFrame::OnNewWindow
), NULL
, this);
458 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_TITLE_CHANGED
,
459 wxWebViewEventHandler(WebFrame::OnTitleChanged
), NULL
, this);
461 // Connect the menu events
462 Connect(viewSource
->GetId(), wxEVT_MENU
,
463 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
464 Connect(print
->GetId(), wxEVT_MENU
,
465 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
466 Connect(m_tools_layout
->GetId(), wxEVT_MENU
,
467 wxCommandEventHandler(WebFrame::OnZoomLayout
), NULL
, this );
468 Connect(m_tools_tiny
->GetId(), wxEVT_MENU
,
469 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
470 Connect(m_tools_small
->GetId(), wxEVT_MENU
,
471 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
472 Connect(m_tools_medium
->GetId(), wxEVT_MENU
,
473 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
474 Connect(m_tools_large
->GetId(), wxEVT_MENU
,
475 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
476 Connect(m_tools_largest
->GetId(), wxEVT_MENU
,
477 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
478 Connect(clearhist
->GetId(), wxEVT_MENU
,
479 wxCommandEventHandler(WebFrame::OnClearHistory
), NULL
, this );
480 Connect(m_tools_enable_history
->GetId(), wxEVT_MENU
,
481 wxCommandEventHandler(WebFrame::OnEnableHistory
), NULL
, this );
482 Connect(m_edit_cut
->GetId(), wxEVT_MENU
,
483 wxCommandEventHandler(WebFrame::OnCut
), NULL
, this );
484 Connect(m_edit_copy
->GetId(), wxEVT_MENU
,
485 wxCommandEventHandler(WebFrame::OnCopy
), NULL
, this );
486 Connect(m_edit_paste
->GetId(), wxEVT_MENU
,
487 wxCommandEventHandler(WebFrame::OnPaste
), NULL
, this );
488 Connect(m_edit_undo
->GetId(), wxEVT_MENU
,
489 wxCommandEventHandler(WebFrame::OnUndo
), NULL
, this );
490 Connect(m_edit_redo
->GetId(), wxEVT_MENU
,
491 wxCommandEventHandler(WebFrame::OnRedo
), NULL
, this );
492 Connect(m_edit_mode
->GetId(), wxEVT_MENU
,
493 wxCommandEventHandler(WebFrame::OnMode
), NULL
, this );
494 Connect(m_scroll_line_up
->GetId(), wxEVT_MENU
,
495 wxCommandEventHandler(WebFrame::OnScrollLineUp
), NULL
, this );
496 Connect(m_scroll_line_down
->GetId(), wxEVT_MENU
,
497 wxCommandEventHandler(WebFrame::OnScrollLineDown
), NULL
, this );
498 Connect(m_scroll_page_up
->GetId(), wxEVT_MENU
,
499 wxCommandEventHandler(WebFrame::OnScrollPageUp
), NULL
, this );
500 Connect(m_scroll_page_down
->GetId(), wxEVT_MENU
,
501 wxCommandEventHandler(WebFrame::OnScrollPageDown
), NULL
, this );
502 Connect(script
->GetId(), wxEVT_MENU
,
503 wxCommandEventHandler(WebFrame::OnRunScript
), NULL
, this );
504 Connect(m_selection_clear
->GetId(), wxEVT_MENU
,
505 wxCommandEventHandler(WebFrame::OnClearSelection
), NULL
, this );
506 Connect(m_selection_delete
->GetId(), wxEVT_MENU
,
507 wxCommandEventHandler(WebFrame::OnDeleteSelection
), NULL
, this );
508 Connect(selectall
->GetId(), wxEVT_MENU
,
509 wxCommandEventHandler(WebFrame::OnSelectAll
), NULL
, this );
510 Connect(loadscheme
->GetId(), wxEVT_MENU
,
511 wxCommandEventHandler(WebFrame::OnLoadScheme
), NULL
, this );
512 Connect(usememoryfs
->GetId(), wxEVT_MENU
,
513 wxCommandEventHandler(WebFrame::OnUseMemoryFS
), NULL
, this );
514 Connect(m_find
->GetId(), wxEVT_MENU
,
515 wxCommandEventHandler(WebFrame::OnFind
), NULL
, this );
516 Connect(m_context_menu
->GetId(), wxEVT_MENU
,
517 wxCommandEventHandler(WebFrame::OnEnableContextMenu
), NULL
, this );
519 //Connect the idle events
520 Connect(wxID_ANY
, wxEVT_IDLE
, wxIdleEventHandler(WebFrame::OnIdle
), NULL
, this);
523 WebFrame::~WebFrame()
529 * Method that retrieves the current state from the web control and updates the GUI
530 * the reflect this current state.
532 void WebFrame::UpdateState()
534 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
535 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
537 if (m_browser
->IsBusy())
539 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
543 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
546 SetTitle( m_browser
->GetCurrentTitle() );
547 m_url
->SetValue( m_browser
->GetCurrentURL() );
550 void WebFrame::OnIdle(wxIdleEvent
& WXUNUSED(evt
))
552 if(m_browser
->IsBusy())
554 wxSetCursor(wxCURSOR_ARROWWAIT
);
555 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), true);
559 wxSetCursor(wxNullCursor
);
560 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), false);
565 * Callback invoked when user entered an URL and pressed enter
567 void WebFrame::OnUrl(wxCommandEvent
& WXUNUSED(evt
))
569 m_browser
->LoadURL( m_url
->GetValue() );
570 m_browser
->SetFocus();
575 * Callback invoked when user pressed the "back" button
577 void WebFrame::OnBack(wxCommandEvent
& WXUNUSED(evt
))
584 * Callback invoked when user pressed the "forward" button
586 void WebFrame::OnForward(wxCommandEvent
& WXUNUSED(evt
))
588 m_browser
->GoForward();
593 * Callback invoked when user pressed the "stop" button
595 void WebFrame::OnStop(wxCommandEvent
& WXUNUSED(evt
))
602 * Callback invoked when user pressed the "reload" button
604 void WebFrame::OnReload(wxCommandEvent
& WXUNUSED(evt
))
610 void WebFrame::OnClearHistory(wxCommandEvent
& WXUNUSED(evt
))
612 m_browser
->ClearHistory();
616 void WebFrame::OnEnableHistory(wxCommandEvent
& WXUNUSED(evt
))
618 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
622 void WebFrame::OnCut(wxCommandEvent
& WXUNUSED(evt
))
627 void WebFrame::OnCopy(wxCommandEvent
& WXUNUSED(evt
))
632 void WebFrame::OnPaste(wxCommandEvent
& WXUNUSED(evt
))
637 void WebFrame::OnUndo(wxCommandEvent
& WXUNUSED(evt
))
642 void WebFrame::OnRedo(wxCommandEvent
& WXUNUSED(evt
))
647 void WebFrame::OnMode(wxCommandEvent
& WXUNUSED(evt
))
649 m_browser
->SetEditable(m_edit_mode
->IsChecked());
652 void WebFrame::OnLoadScheme(wxCommandEvent
& WXUNUSED(evt
))
654 wxFileName
helpfile("../help/doc.zip");
655 helpfile
.MakeAbsolute();
656 wxString path
= helpfile
.GetFullPath();
657 //Under MSW we need to flip the slashes
658 path
.Replace("\\", "/");
659 path
= "wxfs:///" + path
+ ";protocol=zip/doc.htm";
660 m_browser
->LoadURL(path
);
663 void WebFrame::OnUseMemoryFS(wxCommandEvent
& WXUNUSED(evt
))
665 m_browser
->LoadURL("memory:page1.htm");
668 void WebFrame::OnEnableContextMenu(wxCommandEvent
& evt
)
670 m_browser
->EnableContextMenu(evt
.IsChecked());
673 void WebFrame::OnFind(wxCommandEvent
& WXUNUSED(evt
))
675 wxString value
= m_browser
->GetSelectedText();
676 if(value
.Len() > 150)
680 m_find_ctrl
->SetValue(value
);
681 if(!m_find_toolbar
->IsShown()){
682 m_find_toolbar
->Show(true);
685 m_find_ctrl
->SelectAll();
688 void WebFrame::OnFindDone(wxCommandEvent
& WXUNUSED(evt
))
691 m_find_toolbar
->Show(false);
695 void WebFrame::OnFindText(wxCommandEvent
& evt
)
699 if(m_find_toolbar_wrap
->IsChecked())
700 flags
|= wxWEBVIEW_FIND_WRAP
;
701 if(m_find_toolbar_wholeword
->IsChecked())
702 flags
|= wxWEBVIEW_FIND_ENTIRE_WORD
;
703 if(m_find_toolbar_matchcase
->IsChecked())
704 flags
|= wxWEBVIEW_FIND_MATCH_CASE
;
705 if(m_find_toolbar_highlight
->IsChecked())
706 flags
|= wxWEBVIEW_FIND_HIGHLIGHT_RESULT
;
708 if(m_find_toolbar_previous
->GetId() == evt
.GetId())
709 flags
|= wxWEBVIEW_FIND_BACKWARDS
;
711 wxString find_text
= m_find_ctrl
->GetValue();
712 long count
= m_browser
->Find(find_text
, flags
);
714 if(m_findText
!= find_text
)
717 m_findText
= find_text
;
720 if(count
!= wxNOT_FOUND
|| find_text
.IsEmpty())
722 m_find_ctrl
->SetBackgroundColour(*wxWHITE
);
726 m_find_ctrl
->SetBackgroundColour(wxColour(255, 101, 101));
729 m_find_ctrl
->Refresh();
731 //Log the result, note that count is zero indexed.
732 if(count
!= m_findCount
)
736 wxLogMessage("Searching for:%s current match:%i/%i", m_findText
.c_str(), count
, m_findCount
);
740 * Callback invoked when there is a request to load a new page (for instance
741 * when the user clicks a link)
743 void WebFrame::OnNavigationRequest(wxWebViewEvent
& evt
)
745 if(m_info
->IsShown())
750 wxLogMessage("%s", "Navigation request to '" + evt
.GetURL() + "' (target='" +
751 evt
.GetTarget() + "')");
753 wxASSERT(m_browser
->IsBusy());
755 //If we don't want to handle navigation then veto the event and navigation
756 //will not take place, we also need to stop the loading animation
757 if(!m_tools_handle_navigation
->IsChecked())
760 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
769 * Callback invoked when a navigation request was accepted
771 void WebFrame::OnNavigationComplete(wxWebViewEvent
& evt
)
773 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetURL() + "'");
778 * Callback invoked when a page is finished loading
780 void WebFrame::OnDocumentLoaded(wxWebViewEvent
& evt
)
782 //Only notify if the document is the main frame, not a subframe
783 if(evt
.GetURL() == m_browser
->GetCurrentURL())
785 wxLogMessage("%s", "Document loaded; url='" + evt
.GetURL() + "'");
791 * On new window, we veto to stop extra windows appearing
793 void WebFrame::OnNewWindow(wxWebViewEvent
& evt
)
795 wxLogMessage("%s", "New window; url='" + evt
.GetURL() + "'");
797 //If we handle new window events then just load them in this window as we
798 //are a single window browser
799 if(m_tools_handle_new_window
->IsChecked())
800 m_browser
->LoadURL(evt
.GetURL());
805 void WebFrame::OnTitleChanged(wxWebViewEvent
& evt
)
807 SetTitle(evt
.GetString());
808 wxLogMessage("%s", "Title changed; title='" + evt
.GetString() + "'");
812 * Invoked when user selects the "View Source" menu item
814 void WebFrame::OnViewSourceRequest(wxCommandEvent
& WXUNUSED(evt
))
816 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
821 * Invoked when user selects the "Menu" item
823 void WebFrame::OnToolsClicked(wxCommandEvent
& WXUNUSED(evt
))
825 if(m_browser
->GetCurrentURL() == "")
828 m_tools_tiny
->Check(false);
829 m_tools_small
->Check(false);
830 m_tools_medium
->Check(false);
831 m_tools_large
->Check(false);
832 m_tools_largest
->Check(false);
834 wxWebViewZoom zoom
= m_browser
->GetZoom();
837 case wxWEBVIEW_ZOOM_TINY
:
838 m_tools_tiny
->Check();
840 case wxWEBVIEW_ZOOM_SMALL
:
841 m_tools_small
->Check();
843 case wxWEBVIEW_ZOOM_MEDIUM
:
844 m_tools_medium
->Check();
846 case wxWEBVIEW_ZOOM_LARGE
:
847 m_tools_large
->Check();
849 case wxWEBVIEW_ZOOM_LARGEST
:
850 m_tools_largest
->Check();
854 m_edit_cut
->Enable(m_browser
->CanCut());
855 m_edit_copy
->Enable(m_browser
->CanCopy());
856 m_edit_paste
->Enable(m_browser
->CanPaste());
858 m_edit_undo
->Enable(m_browser
->CanUndo());
859 m_edit_redo
->Enable(m_browser
->CanRedo());
861 m_selection_clear
->Enable(m_browser
->HasSelection());
862 m_selection_delete
->Enable(m_browser
->HasSelection());
864 m_context_menu
->Check(m_browser
->IsContextMenuEnabled());
866 //Firstly we clear the existing menu items, then we add the current ones
867 wxMenuHistoryMap::const_iterator it
;
868 for( it
= m_histMenuItems
.begin(); it
!= m_histMenuItems
.end(); ++it
)
870 m_tools_history_menu
->Destroy(it
->first
);
872 m_histMenuItems
.clear();
874 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > back
= m_browser
->GetBackwardHistory();
875 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forward
= m_browser
->GetForwardHistory();
880 for(i
= 0; i
< back
.size(); i
++)
882 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, back
[i
]->GetTitle());
883 m_histMenuItems
[item
->GetId()] = back
[i
];
884 Connect(item
->GetId(), wxEVT_MENU
,
885 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
888 wxString title
= m_browser
->GetCurrentTitle();
890 title
= "(untitled)";
891 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, title
);
894 //No need to connect the current item
895 m_histMenuItems
[item
->GetId()] = wxSharedPtr
<wxWebViewHistoryItem
>(new wxWebViewHistoryItem(m_browser
->GetCurrentURL(), m_browser
->GetCurrentTitle()));
897 for(i
= 0; i
< forward
.size(); i
++)
899 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, forward
[i
]->GetTitle());
900 m_histMenuItems
[item
->GetId()] = forward
[i
];
901 Connect(item
->GetId(), wxEVT_TOOL
,
902 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
905 wxPoint position
= ScreenToClient( wxGetMousePosition() );
906 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
910 * Invoked when user selects the zoom size in the menu
912 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
914 if (evt
.GetId() == m_tools_tiny
->GetId())
916 m_browser
->SetZoom(wxWEBVIEW_ZOOM_TINY
);
918 else if (evt
.GetId() == m_tools_small
->GetId())
920 m_browser
->SetZoom(wxWEBVIEW_ZOOM_SMALL
);
922 else if (evt
.GetId() == m_tools_medium
->GetId())
924 m_browser
->SetZoom(wxWEBVIEW_ZOOM_MEDIUM
);
926 else if (evt
.GetId() == m_tools_large
->GetId())
928 m_browser
->SetZoom(wxWEBVIEW_ZOOM_LARGE
);
930 else if (evt
.GetId() == m_tools_largest
->GetId())
932 m_browser
->SetZoom(wxWEBVIEW_ZOOM_LARGEST
);
940 void WebFrame::OnZoomLayout(wxCommandEvent
& WXUNUSED(evt
))
942 if(m_tools_layout
->IsChecked())
943 m_browser
->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT
);
945 m_browser
->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT
);
948 void WebFrame::OnHistory(wxCommandEvent
& evt
)
950 m_browser
->LoadHistoryItem(m_histMenuItems
[evt
.GetId()]);
953 void WebFrame::OnRunScript(wxCommandEvent
& WXUNUSED(evt
))
955 wxTextEntryDialog
dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr
, "", wxOK
|wxCANCEL
|wxCENTRE
|wxTE_MULTILINE
);
956 if(dialog
.ShowModal() == wxID_OK
)
958 m_browser
->RunScript(dialog
.GetValue());
962 void WebFrame::OnClearSelection(wxCommandEvent
& WXUNUSED(evt
))
964 m_browser
->ClearSelection();
967 void WebFrame::OnDeleteSelection(wxCommandEvent
& WXUNUSED(evt
))
969 m_browser
->DeleteSelection();
972 void WebFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(evt
))
974 m_browser
->SelectAll();
978 * Callback invoked when a loading error occurs
980 void WebFrame::OnError(wxWebViewEvent
& evt
)
982 #define WX_ERROR_CASE(type) \
988 switch (evt
.GetInt())
990 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CONNECTION
);
991 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CERTIFICATE
);
992 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_AUTH
);
993 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_SECURITY
);
994 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_NOT_FOUND
);
995 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_REQUEST
);
996 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_USER_CANCELLED
);
997 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_OTHER
);
1000 wxLogMessage("%s", "Error; url='" + evt
.GetURL() + "', error='" + category
+ " (" + evt
.GetString() + ")'");
1002 //Show the info bar with an error
1003 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetURL() + "\n" +
1004 "'" + category
+ "'", wxICON_ERROR
);
1010 * Invoked when user selects "Print" from the menu
1012 void WebFrame::OnPrint(wxCommandEvent
& WXUNUSED(evt
))
1017 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
1018 wxDialog(parent
, wxID_ANY
, "Source Code",
1019 wxDefaultPosition
, wxSize(700,500),
1020 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
1022 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
1023 text
->SetMarginWidth(1, 30);
1024 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
1025 text
->SetText(source
);
1027 text
->StyleClearAll();
1028 text
->SetLexer(wxSTC_LEX_HTML
);
1029 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
1030 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
1031 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
1032 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
1033 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
1034 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
1035 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
1036 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
1038 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
1039 sizer
->Add(text
, 1, wxEXPAND
);