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
);
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
;
193 wxStaticText
* m_info_text
;
194 wxTextCtrl
* m_find_ctrl
;
195 wxToolBar
* m_find_toolbar
;
197 wxMenuHistoryMap m_histMenuItems
;
199 int m_findFlags
, m_findCount
;
202 class SourceViewDialog
: public wxDialog
205 SourceViewDialog(wxWindow
* parent
, wxString source
);
208 IMPLEMENT_APP(WebApp
)
210 // ============================================================================
212 // ============================================================================
214 bool WebApp::OnInit()
216 if ( !wxApp::OnInit() )
219 //Required for virtual file system archive and memory support
220 wxFileSystem::AddHandler(new wxArchiveFSHandler
);
221 wxFileSystem::AddHandler(new wxMemoryFSHandler
);
223 // Create the memory files
224 wxImage::AddHandler(new wxPNGHandler
);
225 wxMemoryFSHandler::AddFile("logo.png",
226 wxBitmap(wxlogo_xpm
), wxBITMAP_TYPE_PNG
);
227 wxMemoryFSHandler::AddFile("page1.htm",
228 "<html><head><title>File System Example</title>"
229 "<link rel='stylesheet' type='text/css' href='memory:test.css'>"
230 "</head><body><h1>Page 1</h1>"
231 "<p><img src='memory:logo.png'></p>"
232 "<p>Some text about <a href='memory:page2.htm'>Page 2</a>.</p></body>");
233 wxMemoryFSHandler::AddFile("page2.htm",
234 "<html><head><title>File System Example</title>"
235 "<link rel='stylesheet' type='text/css' href='memory:test.css'>"
236 "</head><body><h1>Page 2</h1>"
237 "<p><a href='memory:page1.htm'>Page 1</a> was better.</p></body>");
238 wxMemoryFSHandler::AddFile("test.css", "h1 {color: red;}");
240 WebFrame
*frame
= new WebFrame(m_url
);
246 WebFrame::WebFrame(const wxString
& url
) :
247 wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
249 // set the frame icon
250 SetIcon(wxICON(sample
));
251 SetTitle("wxWebView Sample");
253 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
255 // Create the toolbar
256 m_toolbar
= CreateToolBar(wxTB_TEXT
);
257 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
259 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
260 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
262 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
264 wxBitmap stop
= wxBitmap(stop_xpm
);
267 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
269 wxBitmap refresh
= wxBitmap(refresh_xpm
);
272 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), back
);
273 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), forward
);
274 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), stop
);
275 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), refresh
);
276 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
277 m_toolbar
->AddControl(m_url
, _("URL"));
278 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
280 m_toolbar
->Realize();
283 m_findFlags
= wxWEB_VIEW_FIND_DEFAULT
;
284 m_findText
= wxEmptyString
;
287 // Create panel for find toolbar.
288 wxPanel
* panel
= new wxPanel(this);
289 topsizer
->Add(panel
, wxSizerFlags().Expand());
291 // Create sizer for panel.
292 wxBoxSizer
* panel_sizer
= new wxBoxSizer(wxVERTICAL
);
293 panel
->SetSizer(panel_sizer
);
295 // Create the find toolbar.
296 m_find_toolbar
= new wxToolBar(panel
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTB_HORIZONTAL
|wxTB_TEXT
|wxTB_HORZ_LAYOUT
);
297 m_find_toolbar
->Hide();
298 panel_sizer
->Add(m_find_toolbar
, wxSizerFlags().Expand());
300 // Create find control.
301 m_find_ctrl
= new wxTextCtrl(m_find_toolbar
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxSize(140,-1), wxTE_PROCESS_ENTER
);
305 wxMenu
* findmenu
= new wxMenu
;
306 m_find_toolbar_wrap
= findmenu
->AppendCheckItem(wxID_ANY
,"Wrap");
307 m_find_toolbar_matchcase
= findmenu
->AppendCheckItem(wxID_ANY
,"Match Case");
308 m_find_toolbar_wholeword
= findmenu
->AppendCheckItem(wxID_ANY
,"Entire Word");
309 m_find_toolbar_highlight
= findmenu
->AppendCheckItem(wxID_ANY
,"Highlight");
310 // Add find toolbar tools.
311 m_find_toolbar
->SetToolSeparation(7);
312 m_find_toolbar_done
= m_find_toolbar
->AddTool(wxID_ANY
, "Close", wxArtProvider::GetBitmap(wxART_CROSS_MARK
));
313 m_find_toolbar
->AddSeparator();
314 m_find_toolbar
->AddControl(m_find_ctrl
, "Find");
315 m_find_toolbar
->AddSeparator();
316 m_find_toolbar_next
= m_find_toolbar
->AddTool(wxID_ANY
, "Next", wxArtProvider::GetBitmap(wxART_GO_DOWN
, wxART_TOOLBAR
, wxSize(16,16)));
317 m_find_toolbar_previous
= m_find_toolbar
->AddTool(wxID_ANY
, "Previous", wxArtProvider::GetBitmap(wxART_GO_UP
, wxART_TOOLBAR
, wxSize(16,16)));
318 m_find_toolbar
->AddSeparator();
319 m_find_toolbar_options
= m_find_toolbar
->AddTool(wxID_ANY
, "Options", wxArtProvider::GetBitmap(wxART_PLUS
, wxART_TOOLBAR
, wxSize(16,16)), "", wxITEM_DROPDOWN
);
320 m_find_toolbar_options
->SetDropdownMenu(findmenu
);
321 m_find_toolbar
->Realize();
323 // Create the info panel
324 m_info
= new wxInfoBar(this);
325 topsizer
->Add(m_info
, wxSizerFlags().Expand());
327 // Create the webview
328 m_browser
= wxWebView::New(this, wxID_ANY
, url
);
329 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
331 //We register the wxfs:// protocol for testing purposes
332 m_browser
->RegisterHandler(wxSharedPtr
<wxWebViewHandler
>(new wxWebViewArchiveHandler("wxfs")));
333 //And the memory: file system
334 m_browser
->RegisterHandler(wxSharedPtr
<wxWebViewHandler
>(new wxWebViewFSHandler("memory")));
338 //Set a more sensible size for web browsing
339 SetSize(wxSize(800, 600));
341 // Create a log window
342 new wxLogWindow(this, _("Logging"));
344 // Create the Tools menu
345 m_tools_menu
= new wxMenu();
346 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
347 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
348 m_tools_menu
->AppendSeparator();
349 m_tools_layout
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Use Layout Zoom"));
350 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
351 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
352 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
353 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
354 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
355 m_tools_menu
->AppendSeparator();
356 m_tools_handle_navigation
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle Navigation"));
357 m_tools_handle_new_window
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle New Windows"));
358 m_tools_menu
->AppendSeparator();
361 m_find
= m_tools_menu
->Append(wxID_ANY
, _("Find"));
362 m_tools_menu
->AppendSeparator();
365 m_tools_history_menu
= new wxMenu();
366 wxMenuItem
* clearhist
= m_tools_history_menu
->Append(wxID_ANY
, _("Clear History"));
367 m_tools_enable_history
= m_tools_history_menu
->AppendCheckItem(wxID_ANY
, _("Enable History"));
368 m_tools_history_menu
->AppendSeparator();
370 m_tools_menu
->AppendSubMenu(m_tools_history_menu
, "History");
372 //Create an editing menu
373 wxMenu
* editmenu
= new wxMenu();
374 m_edit_cut
= editmenu
->Append(wxID_ANY
, _("Cut"));
375 m_edit_copy
= editmenu
->Append(wxID_ANY
, _("Copy"));
376 m_edit_paste
= editmenu
->Append(wxID_ANY
, _("Paste"));
377 editmenu
->AppendSeparator();
378 m_edit_undo
= editmenu
->Append(wxID_ANY
, _("Undo"));
379 m_edit_redo
= editmenu
->Append(wxID_ANY
, _("Redo"));
380 editmenu
->AppendSeparator();
381 m_edit_mode
= editmenu
->AppendCheckItem(wxID_ANY
, _("Edit Mode"));
383 m_tools_menu
->AppendSeparator();
384 m_tools_menu
->AppendSubMenu(editmenu
, "Edit");
386 wxMenu
* scroll_menu
= new wxMenu
;
387 m_scroll_line_up
= scroll_menu
->Append(wxID_ANY
, "Line &up");
388 m_scroll_line_down
= scroll_menu
->Append(wxID_ANY
, "Line &down");
389 m_scroll_page_up
= scroll_menu
->Append(wxID_ANY
, "Page u&p");
390 m_scroll_page_down
= scroll_menu
->Append(wxID_ANY
, "Page d&own");
391 m_tools_menu
->AppendSubMenu(scroll_menu
, "Scroll");
393 wxMenuItem
* script
= m_tools_menu
->Append(wxID_ANY
, _("Run Script"));
396 wxMenu
* selection
= new wxMenu();
397 m_selection_clear
= selection
->Append(wxID_ANY
, _("Clear Selection"));
398 m_selection_delete
= selection
->Append(wxID_ANY
, _("Delete Selection"));
399 wxMenuItem
* selectall
= selection
->Append(wxID_ANY
, _("Select All"));
401 editmenu
->AppendSubMenu(selection
, "Selection");
403 wxMenuItem
* loadscheme
= m_tools_menu
->Append(wxID_ANY
, _("Custom Scheme Example"));
404 wxMenuItem
* usememoryfs
= m_tools_menu
->Append(wxID_ANY
, _("Memory File System Example"));
406 //By default we want to handle navigation and new windows
407 m_tools_handle_navigation
->Check();
408 m_tools_handle_new_window
->Check();
409 m_tools_enable_history
->Check();
410 if(!m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
))
411 m_tools_layout
->Enable(false);
414 // Connect the toolbar events
415 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
416 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
417 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
418 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
419 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
420 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
421 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
422 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
423 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
424 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
426 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
427 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
429 // Connect find toolbar events.
430 Connect(m_find_toolbar_done
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
431 wxCommandEventHandler(WebFrame::OnFindDone
), NULL
, this );
432 Connect(m_find_toolbar_next
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
433 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
434 Connect(m_find_toolbar_previous
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
435 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
437 // Connect find control events.
438 Connect(m_find_ctrl
->GetId(), wxEVT_COMMAND_TEXT_UPDATED
,
439 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
440 Connect(m_find_ctrl
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
441 wxCommandEventHandler(WebFrame::OnFindText
), NULL
, this );
443 // Connect the webview events
444 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
445 wxWebViewEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
446 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
447 wxWebViewEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
448 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
449 wxWebViewEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
450 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
451 wxWebViewEventHandler(WebFrame::OnError
), NULL
, this);
452 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
453 wxWebViewEventHandler(WebFrame::OnNewWindow
), NULL
, this);
454 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
455 wxWebViewEventHandler(WebFrame::OnTitleChanged
), NULL
, this);
457 // Connect the menu events
458 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
459 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
460 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
461 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
462 Connect(m_tools_layout
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
463 wxCommandEventHandler(WebFrame::OnZoomLayout
), NULL
, this );
464 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
465 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
466 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
467 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
468 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
469 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
470 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
471 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
472 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
473 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
474 Connect(clearhist
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
475 wxCommandEventHandler(WebFrame::OnClearHistory
), NULL
, this );
476 Connect(m_tools_enable_history
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
477 wxCommandEventHandler(WebFrame::OnEnableHistory
), NULL
, this );
478 Connect(m_edit_cut
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
479 wxCommandEventHandler(WebFrame::OnCut
), NULL
, this );
480 Connect(m_edit_copy
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
481 wxCommandEventHandler(WebFrame::OnCopy
), NULL
, this );
482 Connect(m_edit_paste
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
483 wxCommandEventHandler(WebFrame::OnPaste
), NULL
, this );
484 Connect(m_edit_undo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
485 wxCommandEventHandler(WebFrame::OnUndo
), NULL
, this );
486 Connect(m_edit_redo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
487 wxCommandEventHandler(WebFrame::OnRedo
), NULL
, this );
488 Connect(m_edit_mode
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
489 wxCommandEventHandler(WebFrame::OnMode
), NULL
, this );
490 Connect(m_scroll_line_up
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
491 wxCommandEventHandler(WebFrame::OnScrollLineUp
), NULL
, this );
492 Connect(m_scroll_line_down
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
493 wxCommandEventHandler(WebFrame::OnScrollLineDown
), NULL
, this );
494 Connect(m_scroll_page_up
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
495 wxCommandEventHandler(WebFrame::OnScrollPageUp
), NULL
, this );
496 Connect(m_scroll_page_down
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
497 wxCommandEventHandler(WebFrame::OnScrollPageDown
), NULL
, this );
498 Connect(script
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
499 wxCommandEventHandler(WebFrame::OnRunScript
), NULL
, this );
500 Connect(m_selection_clear
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
501 wxCommandEventHandler(WebFrame::OnClearSelection
), NULL
, this );
502 Connect(m_selection_delete
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
503 wxCommandEventHandler(WebFrame::OnDeleteSelection
), NULL
, this );
504 Connect(selectall
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
505 wxCommandEventHandler(WebFrame::OnSelectAll
), NULL
, this );
506 Connect(loadscheme
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
507 wxCommandEventHandler(WebFrame::OnLoadScheme
), NULL
, this );
508 Connect(usememoryfs
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
509 wxCommandEventHandler(WebFrame::OnUseMemoryFS
), NULL
, this );
510 Connect(m_find
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
511 wxCommandEventHandler(WebFrame::OnFind
), NULL
, this );
513 //Connect the idle events
514 Connect(wxID_ANY
, wxEVT_IDLE
, wxIdleEventHandler(WebFrame::OnIdle
), NULL
, this);
517 WebFrame::~WebFrame()
523 * Method that retrieves the current state from the web control and updates the GUI
524 * the reflect this current state.
526 void WebFrame::UpdateState()
528 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
529 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
531 if (m_browser
->IsBusy())
533 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
537 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
540 SetTitle( m_browser
->GetCurrentTitle() );
541 m_url
->SetValue( m_browser
->GetCurrentURL() );
544 void WebFrame::OnIdle(wxIdleEvent
& WXUNUSED(evt
))
546 if(m_browser
->IsBusy())
548 wxSetCursor(wxCURSOR_ARROWWAIT
);
549 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), true);
553 wxSetCursor(wxNullCursor
);
554 m_toolbar
->EnableTool(m_toolbar_stop
->GetId(), false);
559 * Callback invoked when user entered an URL and pressed enter
561 void WebFrame::OnUrl(wxCommandEvent
& WXUNUSED(evt
))
563 m_browser
->LoadURL( m_url
->GetValue() );
564 m_browser
->SetFocus();
569 * Callback invoked when user pressed the "back" button
571 void WebFrame::OnBack(wxCommandEvent
& WXUNUSED(evt
))
578 * Callback invoked when user pressed the "forward" button
580 void WebFrame::OnForward(wxCommandEvent
& WXUNUSED(evt
))
582 m_browser
->GoForward();
587 * Callback invoked when user pressed the "stop" button
589 void WebFrame::OnStop(wxCommandEvent
& WXUNUSED(evt
))
596 * Callback invoked when user pressed the "reload" button
598 void WebFrame::OnReload(wxCommandEvent
& WXUNUSED(evt
))
604 void WebFrame::OnClearHistory(wxCommandEvent
& WXUNUSED(evt
))
606 m_browser
->ClearHistory();
610 void WebFrame::OnEnableHistory(wxCommandEvent
& WXUNUSED(evt
))
612 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
616 void WebFrame::OnCut(wxCommandEvent
& WXUNUSED(evt
))
621 void WebFrame::OnCopy(wxCommandEvent
& WXUNUSED(evt
))
626 void WebFrame::OnPaste(wxCommandEvent
& WXUNUSED(evt
))
631 void WebFrame::OnUndo(wxCommandEvent
& WXUNUSED(evt
))
636 void WebFrame::OnRedo(wxCommandEvent
& WXUNUSED(evt
))
641 void WebFrame::OnMode(wxCommandEvent
& WXUNUSED(evt
))
643 m_browser
->SetEditable(m_edit_mode
->IsChecked());
646 void WebFrame::OnLoadScheme(wxCommandEvent
& WXUNUSED(evt
))
648 wxFileName
helpfile("../help/doc.zip");
649 helpfile
.MakeAbsolute();
650 wxString path
= helpfile
.GetFullPath();
651 //Under MSW we need to flip the slashes
652 path
.Replace("\\", "/");
653 path
= "wxfs:///" + path
+ ";protocol=zip/doc.htm";
654 m_browser
->LoadURL(path
);
657 void WebFrame::OnUseMemoryFS(wxCommandEvent
& WXUNUSED(evt
))
659 m_browser
->LoadURL("memory:page1.htm");
662 void WebFrame::OnFind(wxCommandEvent
& WXUNUSED(evt
))
664 wxString value
= m_browser
->GetSelectedText();
665 if(value
.Len() > 150)
669 m_find_ctrl
->SetValue(value
);
670 if(!m_find_toolbar
->IsShown()){
671 m_find_toolbar
->Show(true);
674 m_find_ctrl
->SelectAll();
677 void WebFrame::OnFindDone(wxCommandEvent
& WXUNUSED(evt
))
680 m_find_toolbar
->Show(false);
684 void WebFrame::OnFindText(wxCommandEvent
& evt
)
688 if(m_find_toolbar_wrap
->IsChecked())
689 flags
|= wxWEB_VIEW_FIND_WRAP
;
690 if(m_find_toolbar_wholeword
->IsChecked())
691 flags
|= wxWEB_VIEW_FIND_ENTIRE_WORD
;
692 if(m_find_toolbar_matchcase
->IsChecked())
693 flags
|= wxWEB_VIEW_FIND_MATCH_CASE
;
694 if(m_find_toolbar_highlight
->IsChecked())
695 flags
|= wxWEB_VIEW_FIND_HIGHLIGHT_RESULT
;
697 if(m_find_toolbar_previous
->GetId() == evt
.GetId())
698 flags
|= wxWEB_VIEW_FIND_BACKWARDS
;
700 wxString find_text
= m_find_ctrl
->GetValue();
701 long count
= m_browser
->Find(find_text
, flags
);
703 if(m_findText
!= find_text
)
706 m_findText
= find_text
;
709 if(count
!= wxNOT_FOUND
|| find_text
.IsEmpty())
711 m_find_ctrl
->SetBackgroundColour(*wxWHITE
);
715 m_find_ctrl
->SetBackgroundColour(wxColour(255, 101, 101));
718 m_find_ctrl
->Refresh();
720 //Log the result, note that count is zero indexed.
721 if(count
!= m_findCount
)
725 wxLogMessage("Searching for:%s current match:%i/%i", m_findText
.c_str(), count
, m_findCount
);
729 * Callback invoked when there is a request to load a new page (for instance
730 * when the user clicks a link)
732 void WebFrame::OnNavigationRequest(wxWebViewEvent
& evt
)
734 if(m_info
->IsShown())
739 wxLogMessage("%s", "Navigation request to '" + evt
.GetURL() + "' (target='" +
740 evt
.GetTarget() + "')");
742 wxASSERT(m_browser
->IsBusy());
744 //If we don't want to handle navigation then veto the event and navigation
745 //will not take place, we also need to stop the loading animation
746 if(!m_tools_handle_navigation
->IsChecked())
749 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
758 * Callback invoked when a navigation request was accepted
760 void WebFrame::OnNavigationComplete(wxWebViewEvent
& evt
)
762 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetURL() + "'");
767 * Callback invoked when a page is finished loading
769 void WebFrame::OnDocumentLoaded(wxWebViewEvent
& evt
)
771 //Only notify if the document is the main frame, not a subframe
772 if(evt
.GetURL() == m_browser
->GetCurrentURL())
774 wxLogMessage("%s", "Document loaded; url='" + evt
.GetURL() + "'");
780 * On new window, we veto to stop extra windows appearing
782 void WebFrame::OnNewWindow(wxWebViewEvent
& evt
)
784 wxLogMessage("%s", "New window; url='" + evt
.GetURL() + "'");
786 //If we handle new window events then just load them in this window as we
787 //are a single window browser
788 if(m_tools_handle_new_window
->IsChecked())
789 m_browser
->LoadURL(evt
.GetURL());
794 void WebFrame::OnTitleChanged(wxWebViewEvent
& evt
)
796 SetTitle(evt
.GetString());
797 wxLogMessage("%s", "Title changed; title='" + evt
.GetString() + "'");
801 * Invoked when user selects the "View Source" menu item
803 void WebFrame::OnViewSourceRequest(wxCommandEvent
& WXUNUSED(evt
))
805 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
810 * Invoked when user selects the "Menu" item
812 void WebFrame::OnToolsClicked(wxCommandEvent
& WXUNUSED(evt
))
814 if(m_browser
->GetCurrentURL() == "")
817 m_tools_tiny
->Check(false);
818 m_tools_small
->Check(false);
819 m_tools_medium
->Check(false);
820 m_tools_large
->Check(false);
821 m_tools_largest
->Check(false);
823 wxWebViewZoom zoom
= m_browser
->GetZoom();
826 case wxWEB_VIEW_ZOOM_TINY
:
827 m_tools_tiny
->Check();
829 case wxWEB_VIEW_ZOOM_SMALL
:
830 m_tools_small
->Check();
832 case wxWEB_VIEW_ZOOM_MEDIUM
:
833 m_tools_medium
->Check();
835 case wxWEB_VIEW_ZOOM_LARGE
:
836 m_tools_large
->Check();
838 case wxWEB_VIEW_ZOOM_LARGEST
:
839 m_tools_largest
->Check();
843 m_edit_cut
->Enable(m_browser
->CanCut());
844 m_edit_copy
->Enable(m_browser
->CanCopy());
845 m_edit_paste
->Enable(m_browser
->CanPaste());
847 m_edit_undo
->Enable(m_browser
->CanUndo());
848 m_edit_redo
->Enable(m_browser
->CanRedo());
850 m_selection_clear
->Enable(m_browser
->HasSelection());
851 m_selection_delete
->Enable(m_browser
->HasSelection());
853 //Firstly we clear the existing menu items, then we add the current ones
854 wxMenuHistoryMap::const_iterator it
;
855 for( it
= m_histMenuItems
.begin(); it
!= m_histMenuItems
.end(); ++it
)
857 m_tools_history_menu
->Destroy(it
->first
);
859 m_histMenuItems
.clear();
861 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > back
= m_browser
->GetBackwardHistory();
862 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forward
= m_browser
->GetForwardHistory();
867 for(i
= 0; i
< back
.size(); i
++)
869 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, back
[i
]->GetTitle());
870 m_histMenuItems
[item
->GetId()] = back
[i
];
871 Connect(item
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
872 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
875 wxString title
= m_browser
->GetCurrentTitle();
877 title
= "(untitled)";
878 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, title
);
881 //No need to connect the current item
882 m_histMenuItems
[item
->GetId()] = wxSharedPtr
<wxWebViewHistoryItem
>(new wxWebViewHistoryItem(m_browser
->GetCurrentURL(), m_browser
->GetCurrentTitle()));
884 for(i
= 0; i
< forward
.size(); i
++)
886 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, forward
[i
]->GetTitle());
887 m_histMenuItems
[item
->GetId()] = forward
[i
];
888 Connect(item
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
889 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
892 wxPoint position
= ScreenToClient( wxGetMousePosition() );
893 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
897 * Invoked when user selects the zoom size in the menu
899 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
901 if (evt
.GetId() == m_tools_tiny
->GetId())
903 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
905 else if (evt
.GetId() == m_tools_small
->GetId())
907 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
909 else if (evt
.GetId() == m_tools_medium
->GetId())
911 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
913 else if (evt
.GetId() == m_tools_large
->GetId())
915 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
917 else if (evt
.GetId() == m_tools_largest
->GetId())
919 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
927 void WebFrame::OnZoomLayout(wxCommandEvent
& WXUNUSED(evt
))
929 if(m_tools_layout
->IsChecked())
930 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
932 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
935 void WebFrame::OnHistory(wxCommandEvent
& evt
)
937 m_browser
->LoadHistoryItem(m_histMenuItems
[evt
.GetId()]);
940 void WebFrame::OnRunScript(wxCommandEvent
& WXUNUSED(evt
))
942 wxTextEntryDialog
dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr
, "", wxOK
|wxCANCEL
|wxCENTRE
|wxTE_MULTILINE
);
943 if(dialog
.ShowModal() == wxID_OK
)
945 m_browser
->RunScript(dialog
.GetValue());
949 void WebFrame::OnClearSelection(wxCommandEvent
& WXUNUSED(evt
))
951 m_browser
->ClearSelection();
954 void WebFrame::OnDeleteSelection(wxCommandEvent
& WXUNUSED(evt
))
956 m_browser
->DeleteSelection();
959 void WebFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(evt
))
961 m_browser
->SelectAll();
965 * Callback invoked when a loading error occurs
967 void WebFrame::OnError(wxWebViewEvent
& evt
)
969 wxString errorCategory
;
970 switch (evt
.GetInt())
972 case wxWEB_NAV_ERR_CONNECTION
:
973 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
976 case wxWEB_NAV_ERR_CERTIFICATE
:
977 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
980 case wxWEB_NAV_ERR_AUTH
:
981 errorCategory
= "wxWEB_NAV_ERR_AUTH";
984 case wxWEB_NAV_ERR_SECURITY
:
985 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
988 case wxWEB_NAV_ERR_NOT_FOUND
:
989 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
992 case wxWEB_NAV_ERR_REQUEST
:
993 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
996 case wxWEB_NAV_ERR_USER_CANCELLED
:
997 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
1000 case wxWEB_NAV_ERR_OTHER
:
1001 errorCategory
= "wxWEB_NAV_ERR_OTHER";
1005 wxLogMessage("%s", "Error; url='" + evt
.GetURL() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
1007 //Show the info bar with an error
1008 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetURL() + "\n" +
1009 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
1015 * Invoked when user selects "Print" from the menu
1017 void WebFrame::OnPrint(wxCommandEvent
& WXUNUSED(evt
))
1022 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
1023 wxDialog(parent
, wxID_ANY
, "Source Code",
1024 wxDefaultPosition
, wxSize(700,500),
1025 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
1027 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
1028 text
->SetMarginWidth(1, 30);
1029 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
1030 text
->SetText(source
);
1032 text
->StyleClearAll();
1033 text
->SetLexer(wxSTC_LEX_HTML
);
1034 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
1035 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
1036 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
1037 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
1038 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
1039 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
1040 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
1041 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
1043 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
1044 sizer
->Add(text
, 1, wxEXPAND
);