Add support for searching and highlighting a wxWebView.
[wxWidgets.git] / samples / webview / webview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: webview.cpp
3 // Purpose: wxWebView sample
4 // Author: Marianne Gagnon
5 // Id: $Id$
6 // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #endif
24
25 #if !wxUSE_WEBVIEW_WEBKIT && !wxUSE_WEBVIEW_IE
26 #error "A wxWebView backend is required by this sample"
27 #endif
28
29 #include "wx/artprov.h"
30 #include "wx/cmdline.h"
31 #include "wx/notifmsg.h"
32 #include "wx/settings.h"
33 #include "wx/webview.h"
34 #include "wx/webviewarchivehandler.h"
35 #include "wx/infobar.h"
36 #include "wx/filesys.h"
37 #include "wx/fs_arc.h"
38
39 #ifndef wxHAS_IMAGES_IN_RESOURCES
40 #include "../sample.xpm"
41 #endif
42
43 #if wxUSE_STC
44 #include "wx/stc/stc.h"
45 #else
46 #error "wxStyledTextControl is needed by this sample"
47 #endif
48
49 #if defined(__WXMSW__) || defined(__WXOSX__)
50 #include "stop.xpm"
51 #include "refresh.xpm"
52 #endif
53
54 #include "wxlogo.xpm"
55
56
57 //We map menu items to their history items
58 WX_DECLARE_HASH_MAP(int, wxSharedPtr<wxWebViewHistoryItem>,
59 wxIntegerHash, wxIntegerEqual, wxMenuHistoryMap);
60
61 class WebApp : public wxApp
62 {
63 public:
64 WebApp() :
65 m_url("http://www.wxwidgets.org")
66 {
67 }
68
69 virtual bool OnInit();
70
71 #if wxUSE_CMDLINE_PARSER
72 virtual void OnInitCmdLine(wxCmdLineParser& parser)
73 {
74 wxApp::OnInitCmdLine(parser);
75
76 parser.AddParam("URL to open",
77 wxCMD_LINE_VAL_STRING,
78 wxCMD_LINE_PARAM_OPTIONAL);
79 }
80
81 virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
82 {
83 if ( !wxApp::OnCmdLineParsed(parser) )
84 return false;
85
86 if ( parser.GetParamCount() )
87 m_url = parser.GetParam(0);
88
89 return true;
90 }
91 #endif // wxUSE_CMDLINE_PARSER
92
93 private:
94 wxString m_url;
95 };
96
97 class WebFrame : public wxFrame
98 {
99 public:
100 WebFrame(const wxString& url);
101 virtual ~WebFrame();
102
103 void UpdateState();
104 void OnIdle(wxIdleEvent& evt);
105 void OnUrl(wxCommandEvent& evt);
106 void OnBack(wxCommandEvent& evt);
107 void OnForward(wxCommandEvent& evt);
108 void OnStop(wxCommandEvent& evt);
109 void OnReload(wxCommandEvent& evt);
110 void OnClearHistory(wxCommandEvent& evt);
111 void OnEnableHistory(wxCommandEvent& evt);
112 void OnNavigationRequest(wxWebViewEvent& evt);
113 void OnNavigationComplete(wxWebViewEvent& evt);
114 void OnDocumentLoaded(wxWebViewEvent& evt);
115 void OnNewWindow(wxWebViewEvent& evt);
116 void OnTitleChanged(wxWebViewEvent& evt);
117 void OnViewSourceRequest(wxCommandEvent& evt);
118 void OnToolsClicked(wxCommandEvent& evt);
119 void OnSetZoom(wxCommandEvent& evt);
120 void OnError(wxWebViewEvent& evt);
121 void OnPrint(wxCommandEvent& evt);
122 void OnCut(wxCommandEvent& evt);
123 void OnCopy(wxCommandEvent& evt);
124 void OnPaste(wxCommandEvent& evt);
125 void OnUndo(wxCommandEvent& evt);
126 void OnRedo(wxCommandEvent& evt);
127 void OnMode(wxCommandEvent& evt);
128 void OnZoomLayout(wxCommandEvent& evt);
129 void OnHistory(wxCommandEvent& evt);
130 void OnScrollLineUp(wxCommandEvent&) { m_browser->LineUp(); }
131 void OnScrollLineDown(wxCommandEvent&) { m_browser->LineDown(); }
132 void OnScrollPageUp(wxCommandEvent&) { m_browser->PageUp(); }
133 void OnScrollPageDown(wxCommandEvent&) { m_browser->PageDown(); }
134 void OnRunScript(wxCommandEvent& evt);
135 void OnClearSelection(wxCommandEvent& evt);
136 void OnDeleteSelection(wxCommandEvent& evt);
137 void OnSelectAll(wxCommandEvent& evt);
138 void OnLoadScheme(wxCommandEvent& evt);
139 void OnFind(wxCommandEvent& evt);
140 void OnFindDone(wxCommandEvent& evt);
141 void OnFindText(wxCommandEvent& evt);
142 void OnFindOptions(wxCommandEvent& evt);
143
144 private:
145 wxTextCtrl* m_url;
146 wxWebView* m_browser;
147
148 wxToolBar* m_toolbar;
149 wxToolBarToolBase* m_toolbar_back;
150 wxToolBarToolBase* m_toolbar_forward;
151 wxToolBarToolBase* m_toolbar_stop;
152 wxToolBarToolBase* m_toolbar_reload;
153 wxToolBarToolBase* m_toolbar_tools;
154
155 wxToolBarToolBase* m_find_toolbar_done;
156 wxToolBarToolBase* m_find_toolbar_next;
157 wxToolBarToolBase* m_find_toolbar_previous;
158 wxToolBarToolBase* m_find_toolbar_options;
159 wxMenuItem* m_find_toolbar_wrap;
160 wxMenuItem* m_find_toolbar_highlight;
161 wxMenuItem* m_find_toolbar_matchcase;
162 wxMenuItem* m_find_toolbar_wholeword;
163
164 wxMenu* m_tools_menu;
165 wxMenu* m_tools_history_menu;
166 wxMenuItem* m_tools_layout;
167 wxMenuItem* m_tools_tiny;
168 wxMenuItem* m_tools_small;
169 wxMenuItem* m_tools_medium;
170 wxMenuItem* m_tools_large;
171 wxMenuItem* m_tools_largest;
172 wxMenuItem* m_tools_handle_navigation;
173 wxMenuItem* m_tools_handle_new_window;
174 wxMenuItem* m_tools_enable_history;
175 wxMenuItem* m_edit_cut;
176 wxMenuItem* m_edit_copy;
177 wxMenuItem* m_edit_paste;
178 wxMenuItem* m_edit_undo;
179 wxMenuItem* m_edit_redo;
180 wxMenuItem* m_edit_mode;
181 wxMenuItem* m_scroll_line_up;
182 wxMenuItem* m_scroll_line_down;
183 wxMenuItem* m_scroll_page_up;
184 wxMenuItem* m_scroll_page_down;
185 wxMenuItem* m_selection_clear;
186 wxMenuItem* m_selection_delete;
187 wxMenuItem* m_find;
188
189 wxInfoBar *m_info;
190 wxStaticText* m_info_text;
191 wxTextCtrl* m_find_ctrl;
192 wxToolBar* m_find_toolbar;
193
194 wxMenuHistoryMap m_histMenuItems;
195 wxString m_findText;
196 int m_findFlags, m_findCount;
197 };
198
199 class SourceViewDialog : public wxDialog
200 {
201 public:
202 SourceViewDialog(wxWindow* parent, wxString source);
203 };
204
205 IMPLEMENT_APP(WebApp)
206
207 // ============================================================================
208 // implementation
209 // ============================================================================
210
211 bool WebApp::OnInit()
212 {
213 if ( !wxApp::OnInit() )
214 return false;
215
216 WebFrame *frame = new WebFrame(m_url);
217 frame->Show();
218
219 return true;
220 }
221
222 WebFrame::WebFrame(const wxString& url) :
223 wxFrame(NULL, wxID_ANY, "wxWebView Sample")
224 {
225 //Required from virtual file system archive support
226 wxFileSystem::AddHandler(new wxArchiveFSHandler);
227
228 // set the frame icon
229 SetIcon(wxICON(sample));
230 SetTitle("wxWebView Sample");
231
232 wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
233
234 // Create the toolbar
235 m_toolbar = CreateToolBar(wxTB_TEXT);
236 m_toolbar->SetToolBitmapSize(wxSize(32, 32));
237
238 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR);
239 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR);
240 #ifdef __WXGTK__
241 wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR);
242 #else
243 wxBitmap stop = wxBitmap(stop_xpm);
244 #endif
245 #ifdef __WXGTK__
246 wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR);
247 #else
248 wxBitmap refresh = wxBitmap(refresh_xpm);
249 #endif
250
251 m_toolbar_back = m_toolbar->AddTool(wxID_ANY, _("Back"), back);
252 m_toolbar_forward = m_toolbar->AddTool(wxID_ANY, _("Forward"), forward);
253 m_toolbar_stop = m_toolbar->AddTool(wxID_ANY, _("Stop"), stop);
254 m_toolbar_reload = m_toolbar->AddTool(wxID_ANY, _("Reload"), refresh);
255 m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER );
256 m_toolbar->AddControl(m_url, _("URL"));
257 m_toolbar_tools = m_toolbar->AddTool(wxID_ANY, _("Menu"), wxBitmap(wxlogo_xpm));
258
259 m_toolbar->Realize();
260
261 // Set find values.
262 m_findFlags = wxWEB_VIEW_FIND_DEFAULT;
263 m_findText = wxEmptyString;
264 m_findCount = 0;
265
266 // Create panel for find toolbar.
267 wxPanel* panel = new wxPanel(this);
268 topsizer->Add(panel, wxSizerFlags().Expand());
269
270 // Create sizer for panel.
271 wxBoxSizer* panel_sizer = new wxBoxSizer(wxVERTICAL);
272 panel->SetSizer(panel_sizer);
273
274 // Create the find toolbar.
275 m_find_toolbar = new wxToolBar(panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL|wxTB_TEXT|wxTB_HORZ_LAYOUT);
276 m_find_toolbar->Hide();
277 panel_sizer->Add(m_find_toolbar, wxSizerFlags().Expand());
278
279 // Create find control.
280 m_find_ctrl = new wxTextCtrl(m_find_toolbar, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(140,-1), wxTE_PROCESS_ENTER);
281
282
283 //Find options menu
284 wxMenu* findmenu = new wxMenu;
285 m_find_toolbar_wrap = findmenu->AppendCheckItem(wxID_ANY,"Wrap");
286 m_find_toolbar_matchcase = findmenu->AppendCheckItem(wxID_ANY,"Match Case");
287 m_find_toolbar_wholeword = findmenu->AppendCheckItem(wxID_ANY,"Entire Word");
288 m_find_toolbar_highlight = findmenu->AppendCheckItem(wxID_ANY,"Highlight");
289 // Add find toolbar tools.
290 m_find_toolbar->SetToolSeparation(7);
291 m_find_toolbar_done = m_find_toolbar->AddTool(wxID_ANY, "Close", wxArtProvider::GetBitmap(wxART_CROSS_MARK));
292 m_find_toolbar->AddSeparator();
293 m_find_toolbar->AddControl(m_find_ctrl, "Find");
294 m_find_toolbar->AddSeparator();
295 m_find_toolbar_next = m_find_toolbar->AddTool(wxID_ANY, "Next", wxArtProvider::GetBitmap(wxART_GO_DOWN, wxART_TOOLBAR, wxSize(16,16)));
296 m_find_toolbar_previous = m_find_toolbar->AddTool(wxID_ANY, "Previous", wxArtProvider::GetBitmap(wxART_GO_UP, wxART_TOOLBAR, wxSize(16,16)));
297 m_find_toolbar->AddSeparator();
298 m_find_toolbar_options = m_find_toolbar->AddTool(wxID_ANY, "Options", wxArtProvider::GetBitmap(wxART_PLUS, wxART_TOOLBAR, wxSize(16,16)), "", wxITEM_DROPDOWN);
299 m_find_toolbar_options->SetDropdownMenu(findmenu);
300 m_find_toolbar->Realize();
301
302 // Create the info panel
303 m_info = new wxInfoBar(this);
304 topsizer->Add(m_info, wxSizerFlags().Expand());
305
306 // Create the webview
307 m_browser = wxWebView::New(this, wxID_ANY, url);
308 topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
309
310 //We register the wxfs:// protocol for testing purposes
311 m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
312
313 SetSizer(topsizer);
314
315 //Set a more sensible size for web browsing
316 SetSize(wxSize(800, 600));
317
318 // Create a log window
319 new wxLogWindow(this, _("Logging"));
320
321 // Create the Tools menu
322 m_tools_menu = new wxMenu();
323 wxMenuItem* print = m_tools_menu->Append(wxID_ANY , _("Print"));
324 wxMenuItem* viewSource = m_tools_menu->Append(wxID_ANY , _("View Source"));
325 m_tools_menu->AppendSeparator();
326 m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom"));
327 m_tools_tiny = m_tools_menu->AppendCheckItem(wxID_ANY, _("Tiny"));
328 m_tools_small = m_tools_menu->AppendCheckItem(wxID_ANY, _("Small"));
329 m_tools_medium = m_tools_menu->AppendCheckItem(wxID_ANY, _("Medium"));
330 m_tools_large = m_tools_menu->AppendCheckItem(wxID_ANY, _("Large"));
331 m_tools_largest = m_tools_menu->AppendCheckItem(wxID_ANY, _("Largest"));
332 m_tools_menu->AppendSeparator();
333 m_tools_handle_navigation = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle Navigation"));
334 m_tools_handle_new_window = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle New Windows"));
335 m_tools_menu->AppendSeparator();
336
337 //Find
338 m_find = m_tools_menu->Append(wxID_ANY, _("Find"));
339 m_tools_menu->AppendSeparator();
340
341 //History menu
342 m_tools_history_menu = new wxMenu();
343 wxMenuItem* clearhist = m_tools_history_menu->Append(wxID_ANY, _("Clear History"));
344 m_tools_enable_history = m_tools_history_menu->AppendCheckItem(wxID_ANY, _("Enable History"));
345 m_tools_history_menu->AppendSeparator();
346
347 m_tools_menu->AppendSubMenu(m_tools_history_menu, "History");
348
349 //Create an editing menu
350 wxMenu* editmenu = new wxMenu();
351 m_edit_cut = editmenu->Append(wxID_ANY, _("Cut"));
352 m_edit_copy = editmenu->Append(wxID_ANY, _("Copy"));
353 m_edit_paste = editmenu->Append(wxID_ANY, _("Paste"));
354 editmenu->AppendSeparator();
355 m_edit_undo = editmenu->Append(wxID_ANY, _("Undo"));
356 m_edit_redo = editmenu->Append(wxID_ANY, _("Redo"));
357 editmenu->AppendSeparator();
358 m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode"));
359
360 m_tools_menu->AppendSeparator();
361 m_tools_menu->AppendSubMenu(editmenu, "Edit");
362
363 wxMenu* scroll_menu = new wxMenu;
364 m_scroll_line_up = scroll_menu->Append(wxID_ANY, "Line &up");
365 m_scroll_line_down = scroll_menu->Append(wxID_ANY, "Line &down");
366 m_scroll_page_up = scroll_menu->Append(wxID_ANY, "Page u&p");
367 m_scroll_page_down = scroll_menu->Append(wxID_ANY, "Page d&own");
368 m_tools_menu->AppendSubMenu(scroll_menu, "Scroll");
369
370 wxMenuItem* script = m_tools_menu->Append(wxID_ANY, _("Run Script"));
371
372 //Selection menu
373 wxMenu* selection = new wxMenu();
374 m_selection_clear = selection->Append(wxID_ANY, _("Clear Selection"));
375 m_selection_delete = selection->Append(wxID_ANY, _("Delete Selection"));
376 wxMenuItem* selectall = selection->Append(wxID_ANY, _("Select All"));
377
378 editmenu->AppendSubMenu(selection, "Selection");
379
380 wxMenuItem* loadscheme = m_tools_menu->Append(wxID_ANY, _("Custom Scheme Example"));
381
382 //By default we want to handle navigation and new windows
383 m_tools_handle_navigation->Check();
384 m_tools_handle_new_window->Check();
385 m_tools_enable_history->Check();
386 if(!m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT))
387 m_tools_layout->Enable(false);
388
389
390 // Connect the toolbar events
391 Connect(m_toolbar_back->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
392 wxCommandEventHandler(WebFrame::OnBack), NULL, this );
393 Connect(m_toolbar_forward->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
394 wxCommandEventHandler(WebFrame::OnForward), NULL, this );
395 Connect(m_toolbar_stop->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
396 wxCommandEventHandler(WebFrame::OnStop), NULL, this );
397 Connect(m_toolbar_reload->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
398 wxCommandEventHandler(WebFrame::OnReload),NULL, this );
399 Connect(m_toolbar_tools->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
400 wxCommandEventHandler(WebFrame::OnToolsClicked), NULL, this );
401
402 Connect(m_url->GetId(), wxEVT_COMMAND_TEXT_ENTER,
403 wxCommandEventHandler(WebFrame::OnUrl), NULL, this );
404
405 // Connect find toolbar events.
406 Connect(m_find_toolbar_done->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
407 wxCommandEventHandler(WebFrame::OnFindDone), NULL, this );
408 Connect(m_find_toolbar_next->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
409 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
410 Connect(m_find_toolbar_previous->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
411 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
412
413 // Connect find control events.
414 Connect(m_find_ctrl->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
415 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
416 Connect(m_find_ctrl->GetId(), wxEVT_COMMAND_TEXT_ENTER,
417 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
418
419 // Connect the webview events
420 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
421 wxWebViewEventHandler(WebFrame::OnNavigationRequest), NULL, this);
422 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
423 wxWebViewEventHandler(WebFrame::OnNavigationComplete), NULL, this);
424 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED,
425 wxWebViewEventHandler(WebFrame::OnDocumentLoaded), NULL, this);
426 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR,
427 wxWebViewEventHandler(WebFrame::OnError), NULL, this);
428 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
429 wxWebViewEventHandler(WebFrame::OnNewWindow), NULL, this);
430 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
431 wxWebViewEventHandler(WebFrame::OnTitleChanged), NULL, this);
432
433 // Connect the menu events
434 Connect(viewSource->GetId(), wxEVT_COMMAND_MENU_SELECTED,
435 wxCommandEventHandler(WebFrame::OnViewSourceRequest), NULL, this );
436 Connect(print->GetId(), wxEVT_COMMAND_MENU_SELECTED,
437 wxCommandEventHandler(WebFrame::OnPrint), NULL, this );
438 Connect(m_tools_layout->GetId(), wxEVT_COMMAND_MENU_SELECTED,
439 wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this );
440 Connect(m_tools_tiny->GetId(), wxEVT_COMMAND_MENU_SELECTED,
441 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
442 Connect(m_tools_small->GetId(), wxEVT_COMMAND_MENU_SELECTED,
443 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
444 Connect(m_tools_medium->GetId(), wxEVT_COMMAND_MENU_SELECTED,
445 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
446 Connect(m_tools_large->GetId(), wxEVT_COMMAND_MENU_SELECTED,
447 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
448 Connect(m_tools_largest->GetId(), wxEVT_COMMAND_MENU_SELECTED,
449 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
450 Connect(clearhist->GetId(), wxEVT_COMMAND_MENU_SELECTED,
451 wxCommandEventHandler(WebFrame::OnClearHistory), NULL, this );
452 Connect(m_tools_enable_history->GetId(), wxEVT_COMMAND_MENU_SELECTED,
453 wxCommandEventHandler(WebFrame::OnEnableHistory), NULL, this );
454 Connect(m_edit_cut->GetId(), wxEVT_COMMAND_MENU_SELECTED,
455 wxCommandEventHandler(WebFrame::OnCut), NULL, this );
456 Connect(m_edit_copy->GetId(), wxEVT_COMMAND_MENU_SELECTED,
457 wxCommandEventHandler(WebFrame::OnCopy), NULL, this );
458 Connect(m_edit_paste->GetId(), wxEVT_COMMAND_MENU_SELECTED,
459 wxCommandEventHandler(WebFrame::OnPaste), NULL, this );
460 Connect(m_edit_undo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
461 wxCommandEventHandler(WebFrame::OnUndo), NULL, this );
462 Connect(m_edit_redo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
463 wxCommandEventHandler(WebFrame::OnRedo), NULL, this );
464 Connect(m_edit_mode->GetId(), wxEVT_COMMAND_MENU_SELECTED,
465 wxCommandEventHandler(WebFrame::OnMode), NULL, this );
466 Connect(m_scroll_line_up->GetId(), wxEVT_COMMAND_MENU_SELECTED,
467 wxCommandEventHandler(WebFrame::OnScrollLineUp), NULL, this );
468 Connect(m_scroll_line_down->GetId(), wxEVT_COMMAND_MENU_SELECTED,
469 wxCommandEventHandler(WebFrame::OnScrollLineDown), NULL, this );
470 Connect(m_scroll_page_up->GetId(), wxEVT_COMMAND_MENU_SELECTED,
471 wxCommandEventHandler(WebFrame::OnScrollPageUp), NULL, this );
472 Connect(m_scroll_page_down->GetId(), wxEVT_COMMAND_MENU_SELECTED,
473 wxCommandEventHandler(WebFrame::OnScrollPageDown), NULL, this );
474 Connect(script->GetId(), wxEVT_COMMAND_MENU_SELECTED,
475 wxCommandEventHandler(WebFrame::OnRunScript), NULL, this );
476 Connect(m_selection_clear->GetId(), wxEVT_COMMAND_MENU_SELECTED,
477 wxCommandEventHandler(WebFrame::OnClearSelection), NULL, this );
478 Connect(m_selection_delete->GetId(), wxEVT_COMMAND_MENU_SELECTED,
479 wxCommandEventHandler(WebFrame::OnDeleteSelection), NULL, this );
480 Connect(selectall->GetId(), wxEVT_COMMAND_MENU_SELECTED,
481 wxCommandEventHandler(WebFrame::OnSelectAll), NULL, this );
482 Connect(loadscheme->GetId(), wxEVT_COMMAND_MENU_SELECTED,
483 wxCommandEventHandler(WebFrame::OnLoadScheme), NULL, this );
484 Connect(m_find->GetId(), wxEVT_COMMAND_MENU_SELECTED,
485 wxCommandEventHandler(WebFrame::OnFind), NULL, this );
486
487 //Connect the idle events
488 Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(WebFrame::OnIdle), NULL, this);
489 }
490
491 WebFrame::~WebFrame()
492 {
493 delete m_tools_menu;
494 }
495
496 /**
497 * Method that retrieves the current state from the web control and updates the GUI
498 * the reflect this current state.
499 */
500 void WebFrame::UpdateState()
501 {
502 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
503 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
504
505 if (m_browser->IsBusy())
506 {
507 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
508 }
509 else
510 {
511 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
512 }
513
514 SetTitle( m_browser->GetCurrentTitle() );
515 m_url->SetValue( m_browser->GetCurrentURL() );
516 }
517
518 void WebFrame::OnIdle(wxIdleEvent& WXUNUSED(evt))
519 {
520 if(m_browser->IsBusy())
521 {
522 wxSetCursor(wxCURSOR_ARROWWAIT);
523 m_toolbar->EnableTool(m_toolbar_stop->GetId(), true);
524 }
525 else
526 {
527 wxSetCursor(wxNullCursor);
528 m_toolbar->EnableTool(m_toolbar_stop->GetId(), false);
529 }
530 }
531
532 /**
533 * Callback invoked when user entered an URL and pressed enter
534 */
535 void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt))
536 {
537 m_browser->LoadURL( m_url->GetValue() );
538 m_browser->SetFocus();
539 UpdateState();
540 }
541
542 /**
543 * Callback invoked when user pressed the "back" button
544 */
545 void WebFrame::OnBack(wxCommandEvent& WXUNUSED(evt))
546 {
547 m_browser->GoBack();
548 UpdateState();
549 }
550
551 /**
552 * Callback invoked when user pressed the "forward" button
553 */
554 void WebFrame::OnForward(wxCommandEvent& WXUNUSED(evt))
555 {
556 m_browser->GoForward();
557 UpdateState();
558 }
559
560 /**
561 * Callback invoked when user pressed the "stop" button
562 */
563 void WebFrame::OnStop(wxCommandEvent& WXUNUSED(evt))
564 {
565 m_browser->Stop();
566 UpdateState();
567 }
568
569 /**
570 * Callback invoked when user pressed the "reload" button
571 */
572 void WebFrame::OnReload(wxCommandEvent& WXUNUSED(evt))
573 {
574 m_browser->Reload();
575 UpdateState();
576 }
577
578 void WebFrame::OnClearHistory(wxCommandEvent& WXUNUSED(evt))
579 {
580 m_browser->ClearHistory();
581 UpdateState();
582 }
583
584 void WebFrame::OnEnableHistory(wxCommandEvent& WXUNUSED(evt))
585 {
586 m_browser->EnableHistory(m_tools_enable_history->IsChecked());
587 UpdateState();
588 }
589
590 void WebFrame::OnCut(wxCommandEvent& WXUNUSED(evt))
591 {
592 m_browser->Cut();
593 }
594
595 void WebFrame::OnCopy(wxCommandEvent& WXUNUSED(evt))
596 {
597 m_browser->Copy();
598 }
599
600 void WebFrame::OnPaste(wxCommandEvent& WXUNUSED(evt))
601 {
602 m_browser->Paste();
603 }
604
605 void WebFrame::OnUndo(wxCommandEvent& WXUNUSED(evt))
606 {
607 m_browser->Undo();
608 }
609
610 void WebFrame::OnRedo(wxCommandEvent& WXUNUSED(evt))
611 {
612 m_browser->Redo();
613 }
614
615 void WebFrame::OnMode(wxCommandEvent& WXUNUSED(evt))
616 {
617 m_browser->SetEditable(m_edit_mode->IsChecked());
618 }
619
620 void WebFrame::OnLoadScheme(wxCommandEvent& WXUNUSED(evt))
621 {
622 wxFileName helpfile("../help/doc.zip");
623 helpfile.MakeAbsolute();
624 wxString path = helpfile.GetFullPath();
625 //Under MSW we need to flip the slashes
626 path.Replace("\\", "/");
627 path = "wxfs:///" + path + ";protocol=zip/doc.htm";
628 m_browser->LoadURL(path);
629 }
630
631 void WebFrame::OnFind(wxCommandEvent& WXUNUSED(evt))
632 {
633 wxString value = m_browser->GetSelectedText();
634 if(value.Len() > 150)
635 {
636 value.Truncate(150);
637 }
638 m_find_ctrl->SetValue(value);
639 if(!m_find_toolbar->IsShown()){
640 m_find_toolbar->Show(true);
641 SendSizeEvent();
642 }
643 m_find_ctrl->SelectAll();
644 }
645
646 void WebFrame::OnFindDone(wxCommandEvent& WXUNUSED(evt))
647 {
648 m_browser->Find("");
649 m_find_toolbar->Show(false);
650 SendSizeEvent();
651 }
652
653 void WebFrame::OnFindText(wxCommandEvent& evt)
654 {
655 int flags = 0;
656
657 if(m_find_toolbar_wrap->IsChecked())
658 flags |= wxWEB_VIEW_FIND_WRAP;
659 if(m_find_toolbar_wholeword->IsChecked())
660 flags |= wxWEB_VIEW_FIND_ENTIRE_WORD;
661 if(m_find_toolbar_matchcase->IsChecked())
662 flags |= wxWEB_VIEW_FIND_MATCH_CASE;
663 if(m_find_toolbar_highlight->IsChecked())
664 flags |= wxWEB_VIEW_FIND_HIGHLIGHT_RESULT;
665
666 if(m_find_toolbar_previous->GetId() == evt.GetId())
667 flags |= wxWEB_VIEW_FIND_BACKWARDS;
668
669 wxString find_text = m_find_ctrl->GetValue();
670 long count = m_browser->Find(find_text, flags);
671
672 if(m_findText != find_text)
673 {
674 m_findCount = count;
675 m_findText = find_text;
676 }
677
678 if(count != wxNOT_FOUND || find_text.IsEmpty())
679 {
680 m_find_ctrl->SetBackgroundColour(*wxWHITE);
681 }
682 else
683 {
684 m_find_ctrl->SetBackgroundColour(wxColour(255, 101, 101));
685 }
686
687 m_find_ctrl->Refresh();
688
689 //Log the result, note that count is zero indexed.
690 if(count != m_findCount)
691 {
692 count++;
693 }
694 wxLogMessage("Searching for:%s current match:%i/%i", m_findText.c_str(), count, m_findCount);
695 }
696
697 /**
698 * Callback invoked when there is a request to load a new page (for instance
699 * when the user clicks a link)
700 */
701 void WebFrame::OnNavigationRequest(wxWebViewEvent& evt)
702 {
703 if(m_info->IsShown())
704 {
705 m_info->Dismiss();
706 }
707
708 wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" +
709 evt.GetTarget() + "')");
710
711 wxASSERT(m_browser->IsBusy());
712
713 //If we don't want to handle navigation then veto the event and navigation
714 //will not take place, we also need to stop the loading animation
715 if(!m_tools_handle_navigation->IsChecked())
716 {
717 evt.Veto();
718 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
719 }
720 else
721 {
722 UpdateState();
723 }
724 }
725
726 /**
727 * Callback invoked when a navigation request was accepted
728 */
729 void WebFrame::OnNavigationComplete(wxWebViewEvent& evt)
730 {
731 wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'");
732 UpdateState();
733 }
734
735 /**
736 * Callback invoked when a page is finished loading
737 */
738 void WebFrame::OnDocumentLoaded(wxWebViewEvent& evt)
739 {
740 //Only notify if the document is the main frame, not a subframe
741 if(evt.GetURL() == m_browser->GetCurrentURL())
742 {
743 wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'");
744 }
745 UpdateState();
746 }
747
748 /**
749 * On new window, we veto to stop extra windows appearing
750 */
751 void WebFrame::OnNewWindow(wxWebViewEvent& evt)
752 {
753 wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'");
754
755 //If we handle new window events then just load them in this window as we
756 //are a single window browser
757 if(m_tools_handle_new_window->IsChecked())
758 m_browser->LoadURL(evt.GetURL());
759
760 UpdateState();
761 }
762
763 void WebFrame::OnTitleChanged(wxWebViewEvent& evt)
764 {
765 SetTitle(evt.GetString());
766 wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'");
767 }
768
769 /**
770 * Invoked when user selects the "View Source" menu item
771 */
772 void WebFrame::OnViewSourceRequest(wxCommandEvent& WXUNUSED(evt))
773 {
774 SourceViewDialog dlg(this, m_browser->GetPageSource());
775 dlg.ShowModal();
776 }
777
778 /**
779 * Invoked when user selects the "Menu" item
780 */
781 void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
782 {
783 if(m_browser->GetCurrentURL() == "")
784 return;
785
786 m_tools_tiny->Check(false);
787 m_tools_small->Check(false);
788 m_tools_medium->Check(false);
789 m_tools_large->Check(false);
790 m_tools_largest->Check(false);
791
792 wxWebViewZoom zoom = m_browser->GetZoom();
793 switch (zoom)
794 {
795 case wxWEB_VIEW_ZOOM_TINY:
796 m_tools_tiny->Check();
797 break;
798 case wxWEB_VIEW_ZOOM_SMALL:
799 m_tools_small->Check();
800 break;
801 case wxWEB_VIEW_ZOOM_MEDIUM:
802 m_tools_medium->Check();
803 break;
804 case wxWEB_VIEW_ZOOM_LARGE:
805 m_tools_large->Check();
806 break;
807 case wxWEB_VIEW_ZOOM_LARGEST:
808 m_tools_largest->Check();
809 break;
810 }
811
812 m_edit_cut->Enable(m_browser->CanCut());
813 m_edit_copy->Enable(m_browser->CanCopy());
814 m_edit_paste->Enable(m_browser->CanPaste());
815
816 m_edit_undo->Enable(m_browser->CanUndo());
817 m_edit_redo->Enable(m_browser->CanRedo());
818
819 m_selection_clear->Enable(m_browser->HasSelection());
820 m_selection_delete->Enable(m_browser->HasSelection());
821
822 //Firstly we clear the existing menu items, then we add the current ones
823 wxMenuHistoryMap::const_iterator it;
824 for( it = m_histMenuItems.begin(); it != m_histMenuItems.end(); ++it )
825 {
826 m_tools_history_menu->Destroy(it->first);
827 }
828 m_histMenuItems.clear();
829
830 wxVector<wxSharedPtr<wxWebViewHistoryItem> > back = m_browser->GetBackwardHistory();
831 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forward = m_browser->GetForwardHistory();
832
833 wxMenuItem* item;
834
835 unsigned int i;
836 for(i = 0; i < back.size(); i++)
837 {
838 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, back[i]->GetTitle());
839 m_histMenuItems[item->GetId()] = back[i];
840 Connect(item->GetId(), wxEVT_COMMAND_MENU_SELECTED,
841 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
842 }
843
844 wxString title = m_browser->GetCurrentTitle();
845 if ( title.empty() )
846 title = "(untitled)";
847 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, title);
848 item->Check();
849
850 //No need to connect the current item
851 m_histMenuItems[item->GetId()] = wxSharedPtr<wxWebViewHistoryItem>(new wxWebViewHistoryItem(m_browser->GetCurrentURL(), m_browser->GetCurrentTitle()));
852
853 for(i = 0; i < forward.size(); i++)
854 {
855 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, forward[i]->GetTitle());
856 m_histMenuItems[item->GetId()] = forward[i];
857 Connect(item->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
858 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
859 }
860
861 wxPoint position = ScreenToClient( wxGetMousePosition() );
862 PopupMenu(m_tools_menu, position.x, position.y);
863 }
864
865 /**
866 * Invoked when user selects the zoom size in the menu
867 */
868 void WebFrame::OnSetZoom(wxCommandEvent& evt)
869 {
870 if (evt.GetId() == m_tools_tiny->GetId())
871 {
872 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
873 }
874 else if (evt.GetId() == m_tools_small->GetId())
875 {
876 m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL);
877 }
878 else if (evt.GetId() == m_tools_medium->GetId())
879 {
880 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
881 }
882 else if (evt.GetId() == m_tools_large->GetId())
883 {
884 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE);
885 }
886 else if (evt.GetId() == m_tools_largest->GetId())
887 {
888 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST);
889 }
890 else
891 {
892 wxFAIL;
893 }
894 }
895
896 void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt))
897 {
898 if(m_tools_layout->IsChecked())
899 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT);
900 else
901 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT);
902 }
903
904 void WebFrame::OnHistory(wxCommandEvent& evt)
905 {
906 m_browser->LoadHistoryItem(m_histMenuItems[evt.GetId()]);
907 }
908
909 void WebFrame::OnRunScript(wxCommandEvent& WXUNUSED(evt))
910 {
911 wxTextEntryDialog dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr, "", wxOK|wxCANCEL|wxCENTRE|wxTE_MULTILINE);
912 if(dialog.ShowModal() == wxID_OK)
913 {
914 m_browser->RunScript(dialog.GetValue());
915 }
916 }
917
918 void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))
919 {
920 m_browser->ClearSelection();
921 }
922
923 void WebFrame::OnDeleteSelection(wxCommandEvent& WXUNUSED(evt))
924 {
925 m_browser->DeleteSelection();
926 }
927
928 void WebFrame::OnSelectAll(wxCommandEvent& WXUNUSED(evt))
929 {
930 m_browser->SelectAll();
931 }
932
933 /**
934 * Callback invoked when a loading error occurs
935 */
936 void WebFrame::OnError(wxWebViewEvent& evt)
937 {
938 wxString errorCategory;
939 switch (evt.GetInt())
940 {
941 case wxWEB_NAV_ERR_CONNECTION:
942 errorCategory = "wxWEB_NAV_ERR_CONNECTION";
943 break;
944
945 case wxWEB_NAV_ERR_CERTIFICATE:
946 errorCategory = "wxWEB_NAV_ERR_CERTIFICATE";
947 break;
948
949 case wxWEB_NAV_ERR_AUTH:
950 errorCategory = "wxWEB_NAV_ERR_AUTH";
951 break;
952
953 case wxWEB_NAV_ERR_SECURITY:
954 errorCategory = "wxWEB_NAV_ERR_SECURITY";
955 break;
956
957 case wxWEB_NAV_ERR_NOT_FOUND:
958 errorCategory = "wxWEB_NAV_ERR_NOT_FOUND";
959 break;
960
961 case wxWEB_NAV_ERR_REQUEST:
962 errorCategory = "wxWEB_NAV_ERR_REQUEST";
963 break;
964
965 case wxWEB_NAV_ERR_USER_CANCELLED:
966 errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED";
967 break;
968
969 case wxWEB_NAV_ERR_OTHER:
970 errorCategory = "wxWEB_NAV_ERR_OTHER";
971 break;
972 }
973
974 wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")");
975
976 //Show the info bar with an error
977 m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +
978 "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR);
979
980 UpdateState();
981 }
982
983 /**
984 * Invoked when user selects "Print" from the menu
985 */
986 void WebFrame::OnPrint(wxCommandEvent& WXUNUSED(evt))
987 {
988 m_browser->Print();
989 }
990
991 SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
992 wxDialog(parent, wxID_ANY, "Source Code",
993 wxDefaultPosition, wxSize(700,500),
994 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
995 {
996 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
997 text->SetMarginWidth(1, 30);
998 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
999 text->SetText(source);
1000
1001 text->StyleClearAll();
1002 text->SetLexer(wxSTC_LEX_HTML);
1003 text->StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(255,0,0));
1004 text->StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(255,0,0));
1005 text->StyleSetForeground(wxSTC_H_ENTITY, wxColour(255,0,0));
1006 text->StyleSetForeground(wxSTC_H_TAG, wxColour(0,150,0));
1007 text->StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(0,150,0));
1008 text->StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,0,150));
1009 text->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(0,0,150));
1010 text->StyleSetForeground(wxSTC_H_COMMENT, wxColour(150,150,150));
1011
1012 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
1013 sizer->Add(text, 1, wxEXPAND);
1014 SetSizer(sizer);
1015 }