1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWebView sample
4 // Author: Marianne Gagnon
5 // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
13 // For compilers that support precompilation, includes "wx/wx.h".
14 #include "wx/wxprec.h"
24 #if !wxUSE_WEBVIEW_WEBKIT && !wxUSE_WEBVIEW_IE
25 #error "A wxWebView backend is required by this sample"
28 #include "wx/artprov.h"
29 #include "wx/cmdline.h"
30 #include "wx/notifmsg.h"
31 #include "wx/settings.h"
32 #include "wx/webview.h"
33 #include "wx/webviewarchivehandler.h"
34 #include "wx/webviewfshandler.h"
35 #include "wx/infobar.h"
36 #include "wx/filesys.h"
37 #include "wx/fs_arc.h"
38 #include "wx/fs_mem.h"
40 #ifndef wxHAS_IMAGES_IN_RESOURCES
41 #include "../sample.xpm"
45 #include "wx/stc/stc.h"
47 #error "wxStyledTextControl is needed by this sample"
50 #if defined(__WXMSW__) || defined(__WXOSX__)
52 #include "refresh.xpm"
58 //We map menu items to their history items
59 WX_DECLARE_HASH_MAP(int, wxSharedPtr
<wxWebViewHistoryItem
>,
60 wxIntegerHash
, wxIntegerEqual
, wxMenuHistoryMap
);
62 class WebApp
: public wxApp
66 m_url("http://www.wxwidgets.org")
70 virtual bool OnInit();
72 #if wxUSE_CMDLINE_PARSER
73 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
75 wxApp::OnInitCmdLine(parser
);
77 parser
.AddParam("URL to open",
78 wxCMD_LINE_VAL_STRING
,
79 wxCMD_LINE_PARAM_OPTIONAL
);
82 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
84 if ( !wxApp::OnCmdLineParsed(parser
) )
87 if ( parser
.GetParamCount() )
88 m_url
= parser
.GetParam(0);
92 #endif // wxUSE_CMDLINE_PARSER
98 class WebFrame
: public wxFrame
101 WebFrame(const wxString
& url
);
105 void OnIdle(wxIdleEvent
& evt
);
106 void OnUrl(wxCommandEvent
& evt
);
107 void OnBack(wxCommandEvent
& evt
);
108 void OnForward(wxCommandEvent
& evt
);
109 void OnStop(wxCommandEvent
& evt
);
110 void OnReload(wxCommandEvent
& evt
);
111 void OnClearHistory(wxCommandEvent
& evt
);
112 void OnEnableHistory(wxCommandEvent
& evt
);
113 void OnNavigationRequest(wxWebViewEvent
& evt
);
114 void OnNavigationComplete(wxWebViewEvent
& evt
);
115 void OnDocumentLoaded(wxWebViewEvent
& evt
);
116 void OnNewWindow(wxWebViewEvent
& evt
);
117 void OnTitleChanged(wxWebViewEvent
& evt
);
118 void OnViewSourceRequest(wxCommandEvent
& evt
);
119 void OnToolsClicked(wxCommandEvent
& evt
);
120 void OnSetZoom(wxCommandEvent
& evt
);
121 void OnError(wxWebViewEvent
& evt
);
122 void OnPrint(wxCommandEvent
& evt
);
123 void OnCut(wxCommandEvent
& evt
);
124 void OnCopy(wxCommandEvent
& evt
);
125 void OnPaste(wxCommandEvent
& evt
);
126 void OnUndo(wxCommandEvent
& evt
);
127 void OnRedo(wxCommandEvent
& evt
);
128 void OnMode(wxCommandEvent
& evt
);
129 void OnZoomLayout(wxCommandEvent
& evt
);
130 void OnHistory(wxCommandEvent
& evt
);
131 void OnScrollLineUp(wxCommandEvent
&) { m_browser
->LineUp(); }
132 void OnScrollLineDown(wxCommandEvent
&) { m_browser
->LineDown(); }
133 void OnScrollPageUp(wxCommandEvent
&) { m_browser
->PageUp(); }
134 void OnScrollPageDown(wxCommandEvent
&) { m_browser
->PageDown(); }
135 void OnRunScript(wxCommandEvent
& evt
);
136 void OnClearSelection(wxCommandEvent
& evt
);
137 void OnDeleteSelection(wxCommandEvent
& evt
);
138 void OnSelectAll(wxCommandEvent
& evt
);
139 void OnLoadScheme(wxCommandEvent
& evt
);
140 void OnUseMemoryFS(wxCommandEvent
& evt
);
141 void OnFind(wxCommandEvent
& evt
);
142 void OnFindDone(wxCommandEvent
& evt
);
143 void OnFindText(wxCommandEvent
& evt
);
144 void OnFindOptions(wxCommandEvent
& evt
);
145 void OnEnableContextMenu(wxCommandEvent
& evt
);
149 wxWebView
* m_browser
;
151 wxToolBar
* m_toolbar
;
152 wxToolBarToolBase
* m_toolbar_back
;
153 wxToolBarToolBase
* m_toolbar_forward
;
154 wxToolBarToolBase
* m_toolbar_stop
;
155 wxToolBarToolBase
* m_toolbar_reload
;
156 wxToolBarToolBase
* m_toolbar_tools
;
158 wxToolBarToolBase
* m_find_toolbar_done
;
159 wxToolBarToolBase
* m_find_toolbar_next
;
160 wxToolBarToolBase
* m_find_toolbar_previous
;
161 wxToolBarToolBase
* m_find_toolbar_options
;
162 wxMenuItem
* m_find_toolbar_wrap
;
163 wxMenuItem
* m_find_toolbar_highlight
;
164 wxMenuItem
* m_find_toolbar_matchcase
;
165 wxMenuItem
* m_find_toolbar_wholeword
;
167 wxMenu
* m_tools_menu
;
168 wxMenu
* m_tools_history_menu
;
169 wxMenuItem
* m_tools_layout
;
170 wxMenuItem
* m_tools_tiny
;
171 wxMenuItem
* m_tools_small
;
172 wxMenuItem
* m_tools_medium
;
173 wxMenuItem
* m_tools_large
;
174 wxMenuItem
* m_tools_largest
;
175 wxMenuItem
* m_tools_handle_navigation
;
176 wxMenuItem
* m_tools_handle_new_window
;
177 wxMenuItem
* m_tools_enable_history
;
178 wxMenuItem
* m_edit_cut
;
179 wxMenuItem
* m_edit_copy
;
180 wxMenuItem
* m_edit_paste
;
181 wxMenuItem
* m_edit_undo
;
182 wxMenuItem
* m_edit_redo
;
183 wxMenuItem
* m_edit_mode
;
184 wxMenuItem
* m_scroll_line_up
;
185 wxMenuItem
* m_scroll_line_down
;
186 wxMenuItem
* m_scroll_page_up
;
187 wxMenuItem
* m_scroll_page_down
;
188 wxMenuItem
* m_selection_clear
;
189 wxMenuItem
* m_selection_delete
;
191 wxMenuItem
* m_context_menu
;
194 wxStaticText
* m_info_text
;
195 wxTextCtrl
* m_find_ctrl
;
196 wxToolBar
* m_find_toolbar
;
198 wxMenuHistoryMap m_histMenuItems
;
200 int m_findFlags
, m_findCount
;
203 class SourceViewDialog
: public wxDialog
206 SourceViewDialog(wxWindow
* parent
, wxString source
);
209 IMPLEMENT_APP(WebApp
)
211 // ============================================================================
213 // ============================================================================
215 bool WebApp::OnInit()
217 if ( !wxApp::OnInit() )
220 //Required for virtual file system archive and memory support
221 wxFileSystem::AddHandler(new wxArchiveFSHandler
);
222 wxFileSystem::AddHandler(new wxMemoryFSHandler
);
224 // Create the memory files
225 wxImage::AddHandler(new wxPNGHandler
);
226 wxMemoryFSHandler::AddFile("logo.png",
227 wxBitmap(wxlogo_xpm
), wxBITMAP_TYPE_PNG
);
228 wxMemoryFSHandler::AddFile("page1.htm",
229 "<html><head><title>File System Example</title>"
230 "<link rel='stylesheet' type='text/css' href='memory:test.css'>"
231 "</head><body><h1>Page 1</h1>"
232 "<p><img src='memory:logo.png'></p>"
233 "<p>Some text about <a href='memory:page2.htm'>Page 2</a>.</p></body>");
234 wxMemoryFSHandler::AddFile("page2.htm",
235 "<html><head><title>File System Example</title>"
236 "<link rel='stylesheet' type='text/css' href='memory:test.css'>"
237 "</head><body><h1>Page 2</h1>"
238 "<p><a href='memory:page1.htm'>Page 1</a> was better.</p></body>");
239 wxMemoryFSHandler::AddFile("test.css", "h1 {color: red;}");
241 WebFrame
*frame
= new WebFrame(m_url
);
247 WebFrame::WebFrame(const wxString
& url
) :
248 wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
250 // set the frame icon
251 SetIcon(wxICON(sample
));
252 SetTitle("wxWebView Sample");
254 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
256 // Create the toolbar
257 m_toolbar
= CreateToolBar(wxTB_TEXT
);
258 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
260 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
261 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
263 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
265 wxBitmap stop
= wxBitmap(stop_xpm
);
268 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
270 wxBitmap refresh
= wxBitmap(refresh_xpm
);
273 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), back
);
274 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), forward
);
275 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), stop
);
276 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), refresh
);
277 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
278 m_toolbar
->AddControl(m_url
, _("URL"));
279 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
281 m_toolbar
->Realize();
284 m_findFlags
= wxWEBVIEW_FIND_DEFAULT
;
285 m_findText
= wxEmptyString
;
288 // Create panel for find toolbar.
289 wxPanel
* panel
= new wxPanel(this);
290 topsizer
->Add(panel
, wxSizerFlags().Expand());
292 // Create sizer for panel.
293 wxBoxSizer
* panel_sizer
= new wxBoxSizer(wxVERTICAL
);
294 panel
->SetSizer(panel_sizer
);
296 // Create the find toolbar.
297 m_find_toolbar
= new wxToolBar(panel
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTB_HORIZONTAL
|wxTB_TEXT
|wxTB_HORZ_LAYOUT
);
298 m_find_toolbar
->Hide();
299 panel_sizer
->Add(m_find_toolbar
, wxSizerFlags().Expand());
301 // Create find control.
302 m_find_ctrl
= new wxTextCtrl(m_find_toolbar
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxSize(140,-1), wxTE_PROCESS_ENTER
);
306 wxMenu
* findmenu
= new wxMenu
;
307 m_find_toolbar_wrap
= findmenu
->AppendCheckItem(wxID_ANY
,"Wrap");
308 m_find_toolbar_matchcase
= findmenu
->AppendCheckItem(wxID_ANY
,"Match Case");
309 m_find_toolbar_wholeword
= findmenu
->AppendCheckItem(wxID_ANY
,"Entire Word");
310 m_find_toolbar_highlight
= findmenu
->AppendCheckItem(wxID_ANY
,"Highlight");
311 // Add find toolbar tools.
312 m_find_toolbar
->SetToolSeparation(7);
313 m_find_toolbar_done
= m_find_toolbar
->AddTool(wxID_ANY
, "Close", wxArtProvider::GetBitmap(wxART_CROSS_MARK
));
314 m_find_toolbar
->AddSeparator();
315 m_find_toolbar
->AddControl(m_find_ctrl
, "Find");
316 m_find_toolbar
->AddSeparator();
317 m_find_toolbar_next
= m_find_toolbar
->AddTool(wxID_ANY
, "Next", wxArtProvider::GetBitmap(wxART_GO_DOWN
, wxART_TOOLBAR
, wxSize(16,16)));
318 m_find_toolbar_previous
= m_find_toolbar
->AddTool(wxID_ANY
, "Previous", wxArtProvider::GetBitmap(wxART_GO_UP
, wxART_TOOLBAR
, wxSize(16,16)));
319 m_find_toolbar
->AddSeparator();
320 m_find_toolbar_options
= m_find_toolbar
->AddTool(wxID_ANY
, "Options", wxArtProvider::GetBitmap(wxART_PLUS
, wxART_TOOLBAR
, wxSize(16,16)), "", wxITEM_DROPDOWN
);
321 m_find_toolbar_options
->SetDropdownMenu(findmenu
);
322 m_find_toolbar
->Realize();
324 // Create the info panel
325 m_info
= new wxInfoBar(this);
326 topsizer
->Add(m_info
, wxSizerFlags().Expand());
328 // Create the webview
329 m_browser
= wxWebView::New(this, wxID_ANY
, url
);
330 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
332 //We register the wxfs:// protocol for testing purposes
333 m_browser
->RegisterHandler(wxSharedPtr
<wxWebViewHandler
>(new wxWebViewArchiveHandler("wxfs")));
334 //And the memory: file system
335 m_browser
->RegisterHandler(wxSharedPtr
<wxWebViewHandler
>(new wxWebViewFSHandler("memory")));
339 //Set a more sensible size for web browsing
340 SetSize(wxSize(800, 600));
342 // Create a log window
343 new wxLogWindow(this, _("Logging"), true, false);
345 // Create the Tools menu
346 m_tools_menu
= new wxMenu();
347 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
348 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
349 m_tools_menu
->AppendSeparator();
350 m_tools_layout
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Use Layout Zoom"));
351 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
352 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
353 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
354 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
355 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
356 m_tools_menu
->AppendSeparator();
357 m_tools_handle_navigation
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle Navigation"));
358 m_tools_handle_new_window
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle New Windows"));
359 m_tools_menu
->AppendSeparator();
362 m_find
= m_tools_menu
->Append(wxID_ANY
, _("Find"));
363 m_tools_menu
->AppendSeparator();
366 m_tools_history_menu
= new wxMenu();
367 wxMenuItem
* clearhist
= m_tools_history_menu
->Append(wxID_ANY
, _("Clear History"));
368 m_tools_enable_history
= m_tools_history_menu
->AppendCheckItem(wxID_ANY
, _("Enable History"));
369 m_tools_history_menu
->AppendSeparator();
371 m_tools_menu
->AppendSubMenu(m_tools_history_menu
, "History");
373 //Create an editing menu
374 wxMenu
* editmenu
= new wxMenu();
375 m_edit_cut
= editmenu
->Append(wxID_ANY
, _("Cut"));
376 m_edit_copy
= editmenu
->Append(wxID_ANY
, _("Copy"));
377 m_edit_paste
= editmenu
->Append(wxID_ANY
, _("Paste"));
378 editmenu
->AppendSeparator();
379 m_edit_undo
= editmenu
->Append(wxID_ANY
, _("Undo"));
380 m_edit_redo
= editmenu
->Append(wxID_ANY
, _("Redo"));
381 editmenu
->AppendSeparator();
382 m_edit_mode
= editmenu
->AppendCheckItem(wxID_ANY
, _("Edit Mode"));
384 m_tools_menu
->AppendSeparator();
385 m_tools_menu
->AppendSubMenu(editmenu
, "Edit");
387 wxMenu
* scroll_menu
= new wxMenu
;
388 m_scroll_line_up
= scroll_menu
->Append(wxID_ANY
, "Line &up");
389 m_scroll_line_down
= scroll_menu
->Append(wxID_ANY
, "Line &down");
390 m_scroll_page_up
= scroll_menu
->Append(wxID_ANY
, "Page u&p");
391 m_scroll_page_down
= scroll_menu
->Append(wxID_ANY
, "Page d&own");
392 m_tools_menu
->AppendSubMenu(scroll_menu
, "Scroll");
394 wxMenuItem
* script
= m_tools_menu
->Append(wxID_ANY
, _("Run Script"));
397 wxMenu
* selection
= new wxMenu();
398 m_selection_clear
= selection
->Append(wxID_ANY
, _("Clear Selection"));
399 m_selection_delete
= selection
->Append(wxID_ANY
, _("Delete Selection"));
400 wxMenuItem
* selectall
= selection
->Append(wxID_ANY
, _("Select All"));
402 editmenu
->AppendSubMenu(selection
, "Selection");
404 wxMenuItem
* loadscheme
= m_tools_menu
->Append(wxID_ANY
, _("Custom Scheme Example"));
405 wxMenuItem
* usememoryfs
= m_tools_menu
->Append(wxID_ANY
, _("Memory File System Example"));
407 m_context_menu
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Enable Context Menu"));
409 //By default we want to handle navigation and new windows
410 m_tools_handle_navigation
->Check();
411 m_tools_handle_new_window
->Check();
412 m_tools_enable_history
->Check();
413 if(!m_browser
->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT
))
414 m_tools_layout
->Enable(false);
417 // Connect the toolbar events
418 Connect(m_toolbar_back
->GetId(), wxEVT_TOOL
,
419 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
420 Connect(m_toolbar_forward
->GetId(), wxEVT_TOOL
,
421 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
422 Connect(m_toolbar_stop
->GetId(), wxEVT_TOOL
,
423 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
424 Connect(m_toolbar_reload
->GetId(), wxEVT_TOOL
,
425 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
426 Connect(m_toolbar_tools
->GetId(), wxEVT_TOOL
,
427 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
429 Connect(m_url
->GetId(), wxEVT_TEXT_ENTER
,
430 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
432 // Connect find toolbar events.
433 Connect(m_find_toolbar_done
->GetId(), wxEVT_TOOL
,
434 wxCommandEventHandler(WebFrame::OnFindDone
), NULL
, this );
435 Connect(m_find_toolbar_next
->GetId(), wxEVT_TOOL
,
436 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
437 Connect(m_find_toolbar_previous
->GetId(), wxEVT_TOOL
,
438 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
440 // Connect find control events.
441 Connect(m_find_ctrl
->GetId(), wxEVT_TEXT
,
442 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
443 Connect(m_find_ctrl
->GetId(), wxEVT_TEXT_ENTER
,
444 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
446 // Connect the webview events
447 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_NAVIGATING
,
448 wxWebViewEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
449 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_NAVIGATED
,
450 wxWebViewEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
451 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_LOADED
,
452 wxWebViewEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
453 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_ERROR
,
454 wxWebViewEventHandler(WebFrame::OnError
), NULL
, this);
455 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_NEWWINDOW
,
456 wxWebViewEventHandler(WebFrame::OnNewWindow
), NULL
, this);
457 Connect(m_browser
->GetId(), wxEVT_WEBVIEW_TITLE_CHANGED
,
458 wxWebViewEventHandler(WebFrame::OnTitleChanged
), NULL
, this);
460 // Connect the menu events
461 Connect(viewSource
->GetId(), wxEVT_MENU
,
462 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
463 Connect(print
->GetId(), wxEVT_MENU
,
464 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
465 Connect(m_tools_layout
->GetId(), wxEVT_MENU
,
466 wxCommandEventHandler(WebFrame::OnZoomLayout
), NULL
, this );
467 Connect(m_tools_tiny
->GetId(), wxEVT_MENU
,
468 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
469 Connect(m_tools_small
->GetId(), wxEVT_MENU
,
470 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
471 Connect(m_tools_medium
->GetId(), wxEVT_MENU
,
472 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
473 Connect(m_tools_large
->GetId(), wxEVT_MENU
,
474 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
475 Connect(m_tools_largest
->GetId(), wxEVT_MENU
,
476 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
477 Connect(clearhist
->GetId(), wxEVT_MENU
,
478 wxCommandEventHandler(WebFrame::OnClearHistory
), NULL
, this );
479 Connect(m_tools_enable_history
->GetId(), wxEVT_MENU
,
480 wxCommandEventHandler(WebFrame::OnEnableHistory
), NULL
, this );
481 Connect(m_edit_cut
->GetId(), wxEVT_MENU
,
482 wxCommandEventHandler(WebFrame::OnCut
), NULL
, this );
483 Connect(m_edit_copy
->GetId(), wxEVT_MENU
,
484 wxCommandEventHandler(WebFrame::OnCopy
), NULL
, this );
485 Connect(m_edit_paste
->GetId(), wxEVT_MENU
,
486 wxCommandEventHandler(WebFrame::OnPaste
), NULL
, this );
487 Connect(m_edit_undo
->GetId(), wxEVT_MENU
,
488 wxCommandEventHandler(WebFrame::OnUndo
), NULL
, this );
489 Connect(m_edit_redo
->GetId(), wxEVT_MENU
,
490 wxCommandEventHandler(WebFrame::OnRedo
), NULL
, this );
491 Connect(m_edit_mode
->GetId(), wxEVT_MENU
,
492 wxCommandEventHandler(WebFrame::OnMode
), NULL
, this );
493 Connect(m_scroll_line_up
->GetId(), wxEVT_MENU
,
494 wxCommandEventHandler(WebFrame::OnScrollLineUp
), NULL
, this );
495 Connect(m_scroll_line_down
->GetId(), wxEVT_MENU
,
496 wxCommandEventHandler(WebFrame::OnScrollLineDown
), NULL
, this );
497 Connect(m_scroll_page_up
->GetId(), wxEVT_MENU
,
498 wxCommandEventHandler(WebFrame::OnScrollPageUp
), NULL
, this );
499 Connect(m_scroll_page_down
->GetId(), wxEVT_MENU
,
500 wxCommandEventHandler(WebFrame::OnScrollPageDown
), NULL
, this );
501 Connect(script
->GetId(), wxEVT_MENU
,
502 wxCommandEventHandler(WebFrame::OnRunScript
), NULL
, this );
503 Connect(m_selection_clear
->GetId(), wxEVT_MENU
,
504 wxCommandEventHandler(WebFrame::OnClearSelection
), NULL
, this );
505 Connect(m_selection_delete
->GetId(), wxEVT_MENU
,
506 wxCommandEventHandler(WebFrame::OnDeleteSelection
), NULL
, this );
507 Connect(selectall
->GetId(), wxEVT_MENU
,
508 wxCommandEventHandler(WebFrame::OnSelectAll
), NULL
, this );
509 Connect(loadscheme
->GetId(), wxEVT_MENU
,
510 wxCommandEventHandler(WebFrame::OnLoadScheme
), NULL
, this );
511 Connect(usememoryfs
->GetId(), wxEVT_MENU
,
512 wxCommandEventHandler(WebFrame::OnUseMemoryFS
), NULL
, this );
513 Connect(m_find
->GetId(), wxEVT_MENU
,
514 wxCommandEventHandler(WebFrame::OnFind
), NULL
, this );
515 Connect(m_context_menu
->GetId(), wxEVT_MENU
,
516 wxCommandEventHandler(WebFrame::OnEnableContextMenu
), NULL
, this );
518 //Connect the idle events
519 Connect(wxID_ANY
, wxEVT_IDLE
, wxIdleEventHandler(WebFrame::OnIdle
), NULL
, this);
522 WebFrame::~WebFrame()
528 * Method that retrieves the current state from the web control and updates the GUI
529 * the reflect this current state.
531 void WebFrame::UpdateState()
533 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
534 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
536 if (m_browser
->IsBusy())
538 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
542 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
545 SetTitle( m_browser
->GetCurrentTitle() );
546 m_url
->SetValue( m_browser
->GetCurrentURL() );
549 void WebFrame::OnIdle(wxIdleEvent
& WXUNUSED(evt
))
551 if(m_browser
->IsBusy())
553 wxSetCursor(wxCURSOR_ARROWWAIT
);
554 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), true);
558 wxSetCursor(wxNullCursor
);
559 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), false);
564 * Callback invoked when user entered an URL and pressed enter
566 void WebFrame::OnUrl(wxCommandEvent
& WXUNUSED(evt
))
568 m_browser
->LoadURL( m_url
->GetValue() );
569 m_browser
->SetFocus();
574 * Callback invoked when user pressed the "back" button
576 void WebFrame::OnBack(wxCommandEvent
& WXUNUSED(evt
))
583 * Callback invoked when user pressed the "forward" button
585 void WebFrame::OnForward(wxCommandEvent
& WXUNUSED(evt
))
587 m_browser
->GoForward();
592 * Callback invoked when user pressed the "stop" button
594 void WebFrame::OnStop(wxCommandEvent
& WXUNUSED(evt
))
601 * Callback invoked when user pressed the "reload" button
603 void WebFrame::OnReload(wxCommandEvent
& WXUNUSED(evt
))
609 void WebFrame::OnClearHistory(wxCommandEvent
& WXUNUSED(evt
))
611 m_browser
->ClearHistory();
615 void WebFrame::OnEnableHistory(wxCommandEvent
& WXUNUSED(evt
))
617 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
621 void WebFrame::OnCut(wxCommandEvent
& WXUNUSED(evt
))
626 void WebFrame::OnCopy(wxCommandEvent
& WXUNUSED(evt
))
631 void WebFrame::OnPaste(wxCommandEvent
& WXUNUSED(evt
))
636 void WebFrame::OnUndo(wxCommandEvent
& WXUNUSED(evt
))
641 void WebFrame::OnRedo(wxCommandEvent
& WXUNUSED(evt
))
646 void WebFrame::OnMode(wxCommandEvent
& WXUNUSED(evt
))
648 m_browser
->SetEditable(m_edit_mode
->IsChecked());
651 void WebFrame::OnLoadScheme(wxCommandEvent
& WXUNUSED(evt
))
653 wxFileName
helpfile("../help/doc.zip");
654 helpfile
.MakeAbsolute();
655 wxString path
= helpfile
.GetFullPath();
656 //Under MSW we need to flip the slashes
657 path
.Replace("\\", "/");
658 path
= "wxfs:///" + path
+ ";protocol=zip/doc.htm";
659 m_browser
->LoadURL(path
);
662 void WebFrame::OnUseMemoryFS(wxCommandEvent
& WXUNUSED(evt
))
664 m_browser
->LoadURL("memory:page1.htm");
667 void WebFrame::OnEnableContextMenu(wxCommandEvent
& evt
)
669 m_browser
->EnableContextMenu(evt
.IsChecked());
672 void WebFrame::OnFind(wxCommandEvent
& WXUNUSED(evt
))
674 wxString value
= m_browser
->GetSelectedText();
675 if(value
.Len() > 150)
679 m_find_ctrl
->SetValue(value
);
680 if(!m_find_toolbar
->IsShown()){
681 m_find_toolbar
->Show(true);
684 m_find_ctrl
->SelectAll();
687 void WebFrame::OnFindDone(wxCommandEvent
& WXUNUSED(evt
))
690 m_find_toolbar
->Show(false);
694 void WebFrame::OnFindText(wxCommandEvent
& evt
)
698 if(m_find_toolbar_wrap
->IsChecked())
699 flags
|= wxWEBVIEW_FIND_WRAP
;
700 if(m_find_toolbar_wholeword
->IsChecked())
701 flags
|= wxWEBVIEW_FIND_ENTIRE_WORD
;
702 if(m_find_toolbar_matchcase
->IsChecked())
703 flags
|= wxWEBVIEW_FIND_MATCH_CASE
;
704 if(m_find_toolbar_highlight
->IsChecked())
705 flags
|= wxWEBVIEW_FIND_HIGHLIGHT_RESULT
;
707 if(m_find_toolbar_previous
->GetId() == evt
.GetId())
708 flags
|= wxWEBVIEW_FIND_BACKWARDS
;
710 wxString find_text
= m_find_ctrl
->GetValue();
711 long count
= m_browser
->Find(find_text
, flags
);
713 if(m_findText
!= find_text
)
716 m_findText
= find_text
;
719 if(count
!= wxNOT_FOUND
|| find_text
.IsEmpty())
721 m_find_ctrl
->SetBackgroundColour(*wxWHITE
);
725 m_find_ctrl
->SetBackgroundColour(wxColour(255, 101, 101));
728 m_find_ctrl
->Refresh();
730 //Log the result, note that count is zero indexed.
731 if(count
!= m_findCount
)
735 wxLogMessage("Searching for:%s current match:%i/%i", m_findText
.c_str(), count
, m_findCount
);
739 * Callback invoked when there is a request to load a new page (for instance
740 * when the user clicks a link)
742 void WebFrame::OnNavigationRequest(wxWebViewEvent
& evt
)
744 if(m_info
->IsShown())
749 wxLogMessage("%s", "Navigation request to '" + evt
.GetURL() + "' (target='" +
750 evt
.GetTarget() + "')");
752 wxASSERT(m_browser
->IsBusy());
754 //If we don't want to handle navigation then veto the event and navigation
755 //will not take place, we also need to stop the loading animation
756 if(!m_tools_handle_navigation
->IsChecked())
759 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
768 * Callback invoked when a navigation request was accepted
770 void WebFrame::OnNavigationComplete(wxWebViewEvent
& evt
)
772 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetURL() + "'");
777 * Callback invoked when a page is finished loading
779 void WebFrame::OnDocumentLoaded(wxWebViewEvent
& evt
)
781 //Only notify if the document is the main frame, not a subframe
782 if(evt
.GetURL() == m_browser
->GetCurrentURL())
784 wxLogMessage("%s", "Document loaded; url='" + evt
.GetURL() + "'");
790 * On new window, we veto to stop extra windows appearing
792 void WebFrame::OnNewWindow(wxWebViewEvent
& evt
)
794 wxLogMessage("%s", "New window; url='" + evt
.GetURL() + "'");
796 //If we handle new window events then just load them in this window as we
797 //are a single window browser
798 if(m_tools_handle_new_window
->IsChecked())
799 m_browser
->LoadURL(evt
.GetURL());
804 void WebFrame::OnTitleChanged(wxWebViewEvent
& evt
)
806 SetTitle(evt
.GetString());
807 wxLogMessage("%s", "Title changed; title='" + evt
.GetString() + "'");
811 * Invoked when user selects the "View Source" menu item
813 void WebFrame::OnViewSourceRequest(wxCommandEvent
& WXUNUSED(evt
))
815 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
820 * Invoked when user selects the "Menu" item
822 void WebFrame::OnToolsClicked(wxCommandEvent
& WXUNUSED(evt
))
824 if(m_browser
->GetCurrentURL() == "")
827 m_tools_tiny
->Check(false);
828 m_tools_small
->Check(false);
829 m_tools_medium
->Check(false);
830 m_tools_large
->Check(false);
831 m_tools_largest
->Check(false);
833 wxWebViewZoom zoom
= m_browser
->GetZoom();
836 case wxWEBVIEW_ZOOM_TINY
:
837 m_tools_tiny
->Check();
839 case wxWEBVIEW_ZOOM_SMALL
:
840 m_tools_small
->Check();
842 case wxWEBVIEW_ZOOM_MEDIUM
:
843 m_tools_medium
->Check();
845 case wxWEBVIEW_ZOOM_LARGE
:
846 m_tools_large
->Check();
848 case wxWEBVIEW_ZOOM_LARGEST
:
849 m_tools_largest
->Check();
853 m_edit_cut
->Enable(m_browser
->CanCut());
854 m_edit_copy
->Enable(m_browser
->CanCopy());
855 m_edit_paste
->Enable(m_browser
->CanPaste());
857 m_edit_undo
->Enable(m_browser
->CanUndo());
858 m_edit_redo
->Enable(m_browser
->CanRedo());
860 m_selection_clear
->Enable(m_browser
->HasSelection());
861 m_selection_delete
->Enable(m_browser
->HasSelection());
863 m_context_menu
->Check(m_browser
->IsContextMenuEnabled());
865 //Firstly we clear the existing menu items, then we add the current ones
866 wxMenuHistoryMap::const_iterator it
;
867 for( it
= m_histMenuItems
.begin(); it
!= m_histMenuItems
.end(); ++it
)
869 m_tools_history_menu
->Destroy(it
->first
);
871 m_histMenuItems
.clear();
873 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > back
= m_browser
->GetBackwardHistory();
874 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forward
= m_browser
->GetForwardHistory();
879 for(i
= 0; i
< back
.size(); i
++)
881 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, back
[i
]->GetTitle());
882 m_histMenuItems
[item
->GetId()] = back
[i
];
883 Connect(item
->GetId(), wxEVT_MENU
,
884 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
887 wxString title
= m_browser
->GetCurrentTitle();
889 title
= "(untitled)";
890 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, title
);
893 //No need to connect the current item
894 m_histMenuItems
[item
->GetId()] = wxSharedPtr
<wxWebViewHistoryItem
>(new wxWebViewHistoryItem(m_browser
->GetCurrentURL(), m_browser
->GetCurrentTitle()));
896 for(i
= 0; i
< forward
.size(); i
++)
898 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, forward
[i
]->GetTitle());
899 m_histMenuItems
[item
->GetId()] = forward
[i
];
900 Connect(item
->GetId(), wxEVT_TOOL
,
901 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
904 wxPoint position
= ScreenToClient( wxGetMousePosition() );
905 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
909 * Invoked when user selects the zoom size in the menu
911 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
913 if (evt
.GetId() == m_tools_tiny
->GetId())
915 m_browser
->SetZoom(wxWEBVIEW_ZOOM_TINY
);
917 else if (evt
.GetId() == m_tools_small
->GetId())
919 m_browser
->SetZoom(wxWEBVIEW_ZOOM_SMALL
);
921 else if (evt
.GetId() == m_tools_medium
->GetId())
923 m_browser
->SetZoom(wxWEBVIEW_ZOOM_MEDIUM
);
925 else if (evt
.GetId() == m_tools_large
->GetId())
927 m_browser
->SetZoom(wxWEBVIEW_ZOOM_LARGE
);
929 else if (evt
.GetId() == m_tools_largest
->GetId())
931 m_browser
->SetZoom(wxWEBVIEW_ZOOM_LARGEST
);
939 void WebFrame::OnZoomLayout(wxCommandEvent
& WXUNUSED(evt
))
941 if(m_tools_layout
->IsChecked())
942 m_browser
->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT
);
944 m_browser
->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT
);
947 void WebFrame::OnHistory(wxCommandEvent
& evt
)
949 m_browser
->LoadHistoryItem(m_histMenuItems
[evt
.GetId()]);
952 void WebFrame::OnRunScript(wxCommandEvent
& WXUNUSED(evt
))
954 wxTextEntryDialog
dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr
, "", wxOK
|wxCANCEL
|wxCENTRE
|wxTE_MULTILINE
);
955 if(dialog
.ShowModal() == wxID_OK
)
957 m_browser
->RunScript(dialog
.GetValue());
961 void WebFrame::OnClearSelection(wxCommandEvent
& WXUNUSED(evt
))
963 m_browser
->ClearSelection();
966 void WebFrame::OnDeleteSelection(wxCommandEvent
& WXUNUSED(evt
))
968 m_browser
->DeleteSelection();
971 void WebFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(evt
))
973 m_browser
->SelectAll();
977 * Callback invoked when a loading error occurs
979 void WebFrame::OnError(wxWebViewEvent
& evt
)
981 #define WX_ERROR_CASE(type) \
987 switch (evt
.GetInt())
989 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CONNECTION
);
990 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CERTIFICATE
);
991 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_AUTH
);
992 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_SECURITY
);
993 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_NOT_FOUND
);
994 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_REQUEST
);
995 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_USER_CANCELLED
);
996 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_OTHER
);
999 wxLogMessage("%s", "Error; url='" + evt
.GetURL() + "', error='" + category
+ " (" + evt
.GetString() + ")'");
1001 //Show the info bar with an error
1002 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetURL() + "\n" +
1003 "'" + category
+ "'", wxICON_ERROR
);
1009 * Invoked when user selects "Print" from the menu
1011 void WebFrame::OnPrint(wxCommandEvent
& WXUNUSED(evt
))
1016 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
1017 wxDialog(parent
, wxID_ANY
, "Source Code",
1018 wxDefaultPosition
, wxSize(700,500),
1019 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
1021 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
1022 text
->SetMarginWidth(1, 30);
1023 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
1024 text
->SetText(source
);
1026 text
->StyleClearAll();
1027 text
->SetLexer(wxSTC_LEX_HTML
);
1028 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
1029 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
1030 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
1031 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
1032 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
1033 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
1034 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
1035 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
1037 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
1038 sizer
->Add(text
, 1, wxEXPAND
);