]> git.saurik.com Git - wxWidgets.git/blob - samples/webview/webview.cpp
Fix wxKeyEvent::m_uniChar for EVT_CHAR for native controls in wxGTK.
[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
140 private:
141 wxTextCtrl* m_url;
142 wxWebView* m_browser;
143
144 wxToolBar* m_toolbar;
145 wxToolBarToolBase* m_toolbar_back;
146 wxToolBarToolBase* m_toolbar_forward;
147 wxToolBarToolBase* m_toolbar_stop;
148 wxToolBarToolBase* m_toolbar_reload;
149 wxToolBarToolBase* m_toolbar_tools;
150
151 wxMenu* m_tools_menu;
152 wxMenu* m_tools_history_menu;
153 wxMenuItem* m_tools_layout;
154 wxMenuItem* m_tools_tiny;
155 wxMenuItem* m_tools_small;
156 wxMenuItem* m_tools_medium;
157 wxMenuItem* m_tools_large;
158 wxMenuItem* m_tools_largest;
159 wxMenuItem* m_tools_handle_navigation;
160 wxMenuItem* m_tools_handle_new_window;
161 wxMenuItem* m_tools_enable_history;
162 wxMenuItem* m_edit_cut;
163 wxMenuItem* m_edit_copy;
164 wxMenuItem* m_edit_paste;
165 wxMenuItem* m_edit_undo;
166 wxMenuItem* m_edit_redo;
167 wxMenuItem* m_edit_mode;
168 wxMenuItem* m_scroll_line_up;
169 wxMenuItem* m_scroll_line_down;
170 wxMenuItem* m_scroll_page_up;
171 wxMenuItem* m_scroll_page_down;
172 wxMenuItem* m_selection_clear;
173 wxMenuItem* m_selection_delete;
174
175 wxInfoBar *m_info;
176 wxStaticText* m_info_text;
177
178 wxMenuHistoryMap m_histMenuItems;
179 };
180
181 class SourceViewDialog : public wxDialog
182 {
183 public:
184 SourceViewDialog(wxWindow* parent, wxString source);
185 };
186
187 IMPLEMENT_APP(WebApp)
188
189 // ============================================================================
190 // implementation
191 // ============================================================================
192
193 bool WebApp::OnInit()
194 {
195 if ( !wxApp::OnInit() )
196 return false;
197
198 WebFrame *frame = new WebFrame(m_url);
199 frame->Show();
200
201 return true;
202 }
203
204 WebFrame::WebFrame(const wxString& url) :
205 wxFrame(NULL, wxID_ANY, "wxWebView Sample")
206 {
207 //Required from virtual file system archive support
208 wxFileSystem::AddHandler(new wxArchiveFSHandler);
209
210 // set the frame icon
211 SetIcon(wxICON(sample));
212 SetTitle("wxWebView Sample");
213
214 wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
215
216 // Create the toolbar
217 m_toolbar = CreateToolBar(wxTB_TEXT);
218 m_toolbar->SetToolBitmapSize(wxSize(32, 32));
219
220 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR);
221 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR);
222 #ifdef __WXGTK__
223 wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR);
224 #else
225 wxBitmap stop = wxBitmap(stop_xpm);
226 #endif
227 #ifdef __WXGTK__
228 wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR);
229 #else
230 wxBitmap refresh = wxBitmap(refresh_xpm);
231 #endif
232
233 m_toolbar_back = m_toolbar->AddTool(wxID_ANY, _("Back"), back);
234 m_toolbar_forward = m_toolbar->AddTool(wxID_ANY, _("Forward"), forward);
235 m_toolbar_stop = m_toolbar->AddTool(wxID_ANY, _("Stop"), stop);
236 m_toolbar_reload = m_toolbar->AddTool(wxID_ANY, _("Reload"), refresh);
237 m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER );
238 m_toolbar->AddControl(m_url, _("URL"));
239 m_toolbar_tools = m_toolbar->AddTool(wxID_ANY, _("Menu"), wxBitmap(wxlogo_xpm));
240
241 m_toolbar->Realize();
242
243 // Create the info panel
244 m_info = new wxInfoBar(this);
245 topsizer->Add(m_info, wxSizerFlags().Expand());
246
247 // Create the webview
248 m_browser = wxWebView::New(this, wxID_ANY, url);
249 topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
250
251 //We register the wxfs:// protocol for testing purposes
252 m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
253
254 SetSizer(topsizer);
255
256 //Set a more sensible size for web browsing
257 SetSize(wxSize(800, 600));
258
259 // Create a log window
260 new wxLogWindow(this, _("Logging"));
261
262 // Create the Tools menu
263 m_tools_menu = new wxMenu();
264 wxMenuItem* print = m_tools_menu->Append(wxID_ANY , _("Print"));
265 wxMenuItem* viewSource = m_tools_menu->Append(wxID_ANY , _("View Source"));
266 m_tools_menu->AppendSeparator();
267 m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom"));
268 m_tools_tiny = m_tools_menu->AppendCheckItem(wxID_ANY, _("Tiny"));
269 m_tools_small = m_tools_menu->AppendCheckItem(wxID_ANY, _("Small"));
270 m_tools_medium = m_tools_menu->AppendCheckItem(wxID_ANY, _("Medium"));
271 m_tools_large = m_tools_menu->AppendCheckItem(wxID_ANY, _("Large"));
272 m_tools_largest = m_tools_menu->AppendCheckItem(wxID_ANY, _("Largest"));
273 m_tools_menu->AppendSeparator();
274 m_tools_handle_navigation = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle Navigation"));
275 m_tools_handle_new_window = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle New Windows"));
276 m_tools_menu->AppendSeparator();
277
278 //History menu
279 m_tools_history_menu = new wxMenu();
280 wxMenuItem* clearhist = m_tools_history_menu->Append(wxID_ANY, _("Clear History"));
281 m_tools_enable_history = m_tools_history_menu->AppendCheckItem(wxID_ANY, _("Enable History"));
282 m_tools_history_menu->AppendSeparator();
283
284 m_tools_menu->AppendSubMenu(m_tools_history_menu, "History");
285
286 //Create an editing menu
287 wxMenu* editmenu = new wxMenu();
288 m_edit_cut = editmenu->Append(wxID_ANY, _("Cut"));
289 m_edit_copy = editmenu->Append(wxID_ANY, _("Copy"));
290 m_edit_paste = editmenu->Append(wxID_ANY, _("Paste"));
291 editmenu->AppendSeparator();
292 m_edit_undo = editmenu->Append(wxID_ANY, _("Undo"));
293 m_edit_redo = editmenu->Append(wxID_ANY, _("Redo"));
294 editmenu->AppendSeparator();
295 m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode"));
296
297 m_tools_menu->AppendSeparator();
298 m_tools_menu->AppendSubMenu(editmenu, "Edit");
299
300 wxMenu* scroll_menu = new wxMenu;
301 m_scroll_line_up = scroll_menu->Append(wxID_ANY, "Line &up");
302 m_scroll_line_down = scroll_menu->Append(wxID_ANY, "Line &down");
303 m_scroll_page_up = scroll_menu->Append(wxID_ANY, "Page u&p");
304 m_scroll_page_down = scroll_menu->Append(wxID_ANY, "Page d&own");
305 m_tools_menu->AppendSubMenu(scroll_menu, "Scroll");
306
307 wxMenuItem* script = m_tools_menu->Append(wxID_ANY, _("Run Script"));
308
309 //Selection menu
310 wxMenu* selection = new wxMenu();
311 m_selection_clear = selection->Append(wxID_ANY, _("Clear Selection"));
312 m_selection_delete = selection->Append(wxID_ANY, _("Delete Selection"));
313 wxMenuItem* selectall = selection->Append(wxID_ANY, _("Select All"));
314
315 editmenu->AppendSubMenu(selection, "Selection");
316
317 wxMenuItem* loadscheme = m_tools_menu->Append(wxID_ANY, _("Custom Scheme Example"));
318
319 //By default we want to handle navigation and new windows
320 m_tools_handle_navigation->Check();
321 m_tools_handle_new_window->Check();
322 m_tools_enable_history->Check();
323 if(!m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT))
324 m_tools_layout->Enable(false);
325
326
327 // Connect the toolbar events
328 Connect(m_toolbar_back->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
329 wxCommandEventHandler(WebFrame::OnBack), NULL, this );
330 Connect(m_toolbar_forward->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
331 wxCommandEventHandler(WebFrame::OnForward), NULL, this );
332 Connect(m_toolbar_stop->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
333 wxCommandEventHandler(WebFrame::OnStop), NULL, this );
334 Connect(m_toolbar_reload->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
335 wxCommandEventHandler(WebFrame::OnReload),NULL, this );
336 Connect(m_toolbar_tools->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
337 wxCommandEventHandler(WebFrame::OnToolsClicked), NULL, this );
338
339 Connect(m_url->GetId(), wxEVT_COMMAND_TEXT_ENTER,
340 wxCommandEventHandler(WebFrame::OnUrl), NULL, this );
341
342 // Connect the webview events
343 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
344 wxWebViewEventHandler(WebFrame::OnNavigationRequest), NULL, this);
345 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
346 wxWebViewEventHandler(WebFrame::OnNavigationComplete), NULL, this);
347 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED,
348 wxWebViewEventHandler(WebFrame::OnDocumentLoaded), NULL, this);
349 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR,
350 wxWebViewEventHandler(WebFrame::OnError), NULL, this);
351 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
352 wxWebViewEventHandler(WebFrame::OnNewWindow), NULL, this);
353 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
354 wxWebViewEventHandler(WebFrame::OnTitleChanged), NULL, this);
355
356 // Connect the menu events
357 Connect(viewSource->GetId(), wxEVT_COMMAND_MENU_SELECTED,
358 wxCommandEventHandler(WebFrame::OnViewSourceRequest), NULL, this );
359 Connect(print->GetId(), wxEVT_COMMAND_MENU_SELECTED,
360 wxCommandEventHandler(WebFrame::OnPrint), NULL, this );
361 Connect(m_tools_layout->GetId(), wxEVT_COMMAND_MENU_SELECTED,
362 wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this );
363 Connect(m_tools_tiny->GetId(), wxEVT_COMMAND_MENU_SELECTED,
364 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
365 Connect(m_tools_small->GetId(), wxEVT_COMMAND_MENU_SELECTED,
366 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
367 Connect(m_tools_medium->GetId(), wxEVT_COMMAND_MENU_SELECTED,
368 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
369 Connect(m_tools_large->GetId(), wxEVT_COMMAND_MENU_SELECTED,
370 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
371 Connect(m_tools_largest->GetId(), wxEVT_COMMAND_MENU_SELECTED,
372 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
373 Connect(clearhist->GetId(), wxEVT_COMMAND_MENU_SELECTED,
374 wxCommandEventHandler(WebFrame::OnClearHistory), NULL, this );
375 Connect(m_tools_enable_history->GetId(), wxEVT_COMMAND_MENU_SELECTED,
376 wxCommandEventHandler(WebFrame::OnEnableHistory), NULL, this );
377 Connect(m_edit_cut->GetId(), wxEVT_COMMAND_MENU_SELECTED,
378 wxCommandEventHandler(WebFrame::OnCut), NULL, this );
379 Connect(m_edit_copy->GetId(), wxEVT_COMMAND_MENU_SELECTED,
380 wxCommandEventHandler(WebFrame::OnCopy), NULL, this );
381 Connect(m_edit_paste->GetId(), wxEVT_COMMAND_MENU_SELECTED,
382 wxCommandEventHandler(WebFrame::OnPaste), NULL, this );
383 Connect(m_edit_undo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
384 wxCommandEventHandler(WebFrame::OnUndo), NULL, this );
385 Connect(m_edit_redo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
386 wxCommandEventHandler(WebFrame::OnRedo), NULL, this );
387 Connect(m_edit_mode->GetId(), wxEVT_COMMAND_MENU_SELECTED,
388 wxCommandEventHandler(WebFrame::OnMode), NULL, this );
389 Connect(m_scroll_line_up->GetId(), wxEVT_COMMAND_MENU_SELECTED,
390 wxCommandEventHandler(WebFrame::OnScrollLineUp), NULL, this );
391 Connect(m_scroll_line_down->GetId(), wxEVT_COMMAND_MENU_SELECTED,
392 wxCommandEventHandler(WebFrame::OnScrollLineDown), NULL, this );
393 Connect(m_scroll_page_up->GetId(), wxEVT_COMMAND_MENU_SELECTED,
394 wxCommandEventHandler(WebFrame::OnScrollPageUp), NULL, this );
395 Connect(m_scroll_page_down->GetId(), wxEVT_COMMAND_MENU_SELECTED,
396 wxCommandEventHandler(WebFrame::OnScrollPageDown), NULL, this );
397 Connect(script->GetId(), wxEVT_COMMAND_MENU_SELECTED,
398 wxCommandEventHandler(WebFrame::OnRunScript), NULL, this );
399 Connect(m_selection_clear->GetId(), wxEVT_COMMAND_MENU_SELECTED,
400 wxCommandEventHandler(WebFrame::OnClearSelection), NULL, this );
401 Connect(m_selection_delete->GetId(), wxEVT_COMMAND_MENU_SELECTED,
402 wxCommandEventHandler(WebFrame::OnDeleteSelection), NULL, this );
403 Connect(selectall->GetId(), wxEVT_COMMAND_MENU_SELECTED,
404 wxCommandEventHandler(WebFrame::OnSelectAll), NULL, this );
405 Connect(loadscheme->GetId(), wxEVT_COMMAND_MENU_SELECTED,
406 wxCommandEventHandler(WebFrame::OnLoadScheme), NULL, this );
407
408 //Connect the idle events
409 Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(WebFrame::OnIdle), NULL, this);
410 }
411
412 WebFrame::~WebFrame()
413 {
414 delete m_tools_menu;
415 }
416
417 /**
418 * Method that retrieves the current state from the web control and updates the GUI
419 * the reflect this current state.
420 */
421 void WebFrame::UpdateState()
422 {
423 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
424 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
425
426 if (m_browser->IsBusy())
427 {
428 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
429 }
430 else
431 {
432 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
433 }
434
435 SetTitle( m_browser->GetCurrentTitle() );
436 m_url->SetValue( m_browser->GetCurrentURL() );
437 }
438
439 void WebFrame::OnIdle(wxIdleEvent& WXUNUSED(evt))
440 {
441 if(m_browser->IsBusy())
442 {
443 wxSetCursor(wxCURSOR_ARROWWAIT);
444 m_toolbar->EnableTool(m_toolbar_stop->GetId(), true);
445 }
446 else
447 {
448 wxSetCursor(wxNullCursor);
449 m_toolbar->EnableTool(m_toolbar_stop->GetId(), false);
450 }
451 }
452
453 /**
454 * Callback invoked when user entered an URL and pressed enter
455 */
456 void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt))
457 {
458 m_browser->LoadURL( m_url->GetValue() );
459 m_browser->SetFocus();
460 UpdateState();
461 }
462
463 /**
464 * Callback invoked when user pressed the "back" button
465 */
466 void WebFrame::OnBack(wxCommandEvent& WXUNUSED(evt))
467 {
468 m_browser->GoBack();
469 UpdateState();
470 }
471
472 /**
473 * Callback invoked when user pressed the "forward" button
474 */
475 void WebFrame::OnForward(wxCommandEvent& WXUNUSED(evt))
476 {
477 m_browser->GoForward();
478 UpdateState();
479 }
480
481 /**
482 * Callback invoked when user pressed the "stop" button
483 */
484 void WebFrame::OnStop(wxCommandEvent& WXUNUSED(evt))
485 {
486 m_browser->Stop();
487 UpdateState();
488 }
489
490 /**
491 * Callback invoked when user pressed the "reload" button
492 */
493 void WebFrame::OnReload(wxCommandEvent& WXUNUSED(evt))
494 {
495 m_browser->Reload();
496 UpdateState();
497 }
498
499 void WebFrame::OnClearHistory(wxCommandEvent& WXUNUSED(evt))
500 {
501 m_browser->ClearHistory();
502 UpdateState();
503 }
504
505 void WebFrame::OnEnableHistory(wxCommandEvent& WXUNUSED(evt))
506 {
507 m_browser->EnableHistory(m_tools_enable_history->IsChecked());
508 UpdateState();
509 }
510
511 void WebFrame::OnCut(wxCommandEvent& WXUNUSED(evt))
512 {
513 m_browser->Cut();
514 }
515
516 void WebFrame::OnCopy(wxCommandEvent& WXUNUSED(evt))
517 {
518 m_browser->Copy();
519 }
520
521 void WebFrame::OnPaste(wxCommandEvent& WXUNUSED(evt))
522 {
523 m_browser->Paste();
524 }
525
526 void WebFrame::OnUndo(wxCommandEvent& WXUNUSED(evt))
527 {
528 m_browser->Undo();
529 }
530
531 void WebFrame::OnRedo(wxCommandEvent& WXUNUSED(evt))
532 {
533 m_browser->Redo();
534 }
535
536 void WebFrame::OnMode(wxCommandEvent& WXUNUSED(evt))
537 {
538 m_browser->SetEditable(m_edit_mode->IsChecked());
539 }
540
541 void WebFrame::OnLoadScheme(wxCommandEvent& WXUNUSED(evt))
542 {
543 wxFileName helpfile("../help/doc.zip");
544 helpfile.MakeAbsolute();
545 wxString path = helpfile.GetFullPath();
546 //Under MSW we need to flip the slashes
547 path.Replace("\\", "/");
548 path = "wxfs:///" + path + ";protocol=zip/doc.htm";
549 m_browser->LoadURL(path);
550 }
551
552 /**
553 * Callback invoked when there is a request to load a new page (for instance
554 * when the user clicks a link)
555 */
556 void WebFrame::OnNavigationRequest(wxWebViewEvent& evt)
557 {
558 if(m_info->IsShown())
559 {
560 m_info->Dismiss();
561 }
562
563 wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" +
564 evt.GetTarget() + "')");
565
566 wxASSERT(m_browser->IsBusy());
567
568 //If we don't want to handle navigation then veto the event and navigation
569 //will not take place, we also need to stop the loading animation
570 if(!m_tools_handle_navigation->IsChecked())
571 {
572 evt.Veto();
573 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
574 }
575 else
576 {
577 UpdateState();
578 }
579 }
580
581 /**
582 * Callback invoked when a navigation request was accepted
583 */
584 void WebFrame::OnNavigationComplete(wxWebViewEvent& evt)
585 {
586 wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'");
587 UpdateState();
588 }
589
590 /**
591 * Callback invoked when a page is finished loading
592 */
593 void WebFrame::OnDocumentLoaded(wxWebViewEvent& evt)
594 {
595 //Only notify if the document is the main frame, not a subframe
596 if(evt.GetURL() == m_browser->GetCurrentURL())
597 {
598 wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'");
599 }
600 UpdateState();
601 }
602
603 /**
604 * On new window, we veto to stop extra windows appearing
605 */
606 void WebFrame::OnNewWindow(wxWebViewEvent& evt)
607 {
608 wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'");
609
610 //If we handle new window events then just load them in this window as we
611 //are a single window browser
612 if(m_tools_handle_new_window->IsChecked())
613 m_browser->LoadURL(evt.GetURL());
614
615 UpdateState();
616 }
617
618 void WebFrame::OnTitleChanged(wxWebViewEvent& evt)
619 {
620 SetTitle(evt.GetString());
621 wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'");
622 }
623
624 /**
625 * Invoked when user selects the "View Source" menu item
626 */
627 void WebFrame::OnViewSourceRequest(wxCommandEvent& WXUNUSED(evt))
628 {
629 SourceViewDialog dlg(this, m_browser->GetPageSource());
630 dlg.ShowModal();
631 }
632
633 /**
634 * Invoked when user selects the "Menu" item
635 */
636 void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
637 {
638 if(m_browser->GetCurrentURL() == "")
639 return;
640
641 m_tools_tiny->Check(false);
642 m_tools_small->Check(false);
643 m_tools_medium->Check(false);
644 m_tools_large->Check(false);
645 m_tools_largest->Check(false);
646
647 wxWebViewZoom zoom = m_browser->GetZoom();
648 switch (zoom)
649 {
650 case wxWEB_VIEW_ZOOM_TINY:
651 m_tools_tiny->Check();
652 break;
653 case wxWEB_VIEW_ZOOM_SMALL:
654 m_tools_small->Check();
655 break;
656 case wxWEB_VIEW_ZOOM_MEDIUM:
657 m_tools_medium->Check();
658 break;
659 case wxWEB_VIEW_ZOOM_LARGE:
660 m_tools_large->Check();
661 break;
662 case wxWEB_VIEW_ZOOM_LARGEST:
663 m_tools_largest->Check();
664 break;
665 }
666
667 m_edit_cut->Enable(m_browser->CanCut());
668 m_edit_copy->Enable(m_browser->CanCopy());
669 m_edit_paste->Enable(m_browser->CanPaste());
670
671 m_edit_undo->Enable(m_browser->CanUndo());
672 m_edit_redo->Enable(m_browser->CanRedo());
673
674 m_selection_clear->Enable(m_browser->HasSelection());
675 m_selection_delete->Enable(m_browser->HasSelection());
676
677 //Firstly we clear the existing menu items, then we add the current ones
678 wxMenuHistoryMap::const_iterator it;
679 for( it = m_histMenuItems.begin(); it != m_histMenuItems.end(); ++it )
680 {
681 m_tools_history_menu->Destroy(it->first);
682 }
683 m_histMenuItems.clear();
684
685 wxVector<wxSharedPtr<wxWebViewHistoryItem> > back = m_browser->GetBackwardHistory();
686 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forward = m_browser->GetForwardHistory();
687
688 wxMenuItem* item;
689
690 unsigned int i;
691 for(i = 0; i < back.size(); i++)
692 {
693 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, back[i]->GetTitle());
694 m_histMenuItems[item->GetId()] = back[i];
695 Connect(item->GetId(), wxEVT_COMMAND_MENU_SELECTED,
696 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
697 }
698
699 wxString title = m_browser->GetCurrentTitle();
700 if ( title.empty() )
701 title = "(untitled)";
702 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, title);
703 item->Check();
704
705 //No need to connect the current item
706 m_histMenuItems[item->GetId()] = wxSharedPtr<wxWebViewHistoryItem>(new wxWebViewHistoryItem(m_browser->GetCurrentURL(), m_browser->GetCurrentTitle()));
707
708 for(i = 0; i < forward.size(); i++)
709 {
710 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, forward[i]->GetTitle());
711 m_histMenuItems[item->GetId()] = forward[i];
712 Connect(item->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
713 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
714 }
715
716 wxPoint position = ScreenToClient( wxGetMousePosition() );
717 PopupMenu(m_tools_menu, position.x, position.y);
718 }
719
720 /**
721 * Invoked when user selects the zoom size in the menu
722 */
723 void WebFrame::OnSetZoom(wxCommandEvent& evt)
724 {
725 if (evt.GetId() == m_tools_tiny->GetId())
726 {
727 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
728 }
729 else if (evt.GetId() == m_tools_small->GetId())
730 {
731 m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL);
732 }
733 else if (evt.GetId() == m_tools_medium->GetId())
734 {
735 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
736 }
737 else if (evt.GetId() == m_tools_large->GetId())
738 {
739 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE);
740 }
741 else if (evt.GetId() == m_tools_largest->GetId())
742 {
743 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST);
744 }
745 else
746 {
747 wxFAIL;
748 }
749 }
750
751 void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt))
752 {
753 if(m_tools_layout->IsChecked())
754 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT);
755 else
756 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT);
757 }
758
759 void WebFrame::OnHistory(wxCommandEvent& evt)
760 {
761 m_browser->LoadHistoryItem(m_histMenuItems[evt.GetId()]);
762 }
763
764 void WebFrame::OnRunScript(wxCommandEvent& WXUNUSED(evt))
765 {
766 wxTextEntryDialog dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr, "", wxOK|wxCANCEL|wxCENTRE|wxTE_MULTILINE);
767 if(dialog.ShowModal() == wxID_OK)
768 {
769 m_browser->RunScript(dialog.GetValue());
770 }
771 }
772
773 void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))
774 {
775 m_browser->ClearSelection();
776 }
777
778 void WebFrame::OnDeleteSelection(wxCommandEvent& WXUNUSED(evt))
779 {
780 m_browser->DeleteSelection();
781 }
782
783 void WebFrame::OnSelectAll(wxCommandEvent& WXUNUSED(evt))
784 {
785 m_browser->SelectAll();
786 }
787
788 /**
789 * Callback invoked when a loading error occurs
790 */
791 void WebFrame::OnError(wxWebViewEvent& evt)
792 {
793 wxString errorCategory;
794 switch (evt.GetInt())
795 {
796 case wxWEB_NAV_ERR_CONNECTION:
797 errorCategory = "wxWEB_NAV_ERR_CONNECTION";
798 break;
799
800 case wxWEB_NAV_ERR_CERTIFICATE:
801 errorCategory = "wxWEB_NAV_ERR_CERTIFICATE";
802 break;
803
804 case wxWEB_NAV_ERR_AUTH:
805 errorCategory = "wxWEB_NAV_ERR_AUTH";
806 break;
807
808 case wxWEB_NAV_ERR_SECURITY:
809 errorCategory = "wxWEB_NAV_ERR_SECURITY";
810 break;
811
812 case wxWEB_NAV_ERR_NOT_FOUND:
813 errorCategory = "wxWEB_NAV_ERR_NOT_FOUND";
814 break;
815
816 case wxWEB_NAV_ERR_REQUEST:
817 errorCategory = "wxWEB_NAV_ERR_REQUEST";
818 break;
819
820 case wxWEB_NAV_ERR_USER_CANCELLED:
821 errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED";
822 break;
823
824 case wxWEB_NAV_ERR_OTHER:
825 errorCategory = "wxWEB_NAV_ERR_OTHER";
826 break;
827 }
828
829 wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")");
830
831 //Show the info bar with an error
832 m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +
833 "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR);
834
835 UpdateState();
836 }
837
838 /**
839 * Invoked when user selects "Print" from the menu
840 */
841 void WebFrame::OnPrint(wxCommandEvent& WXUNUSED(evt))
842 {
843 m_browser->Print();
844 }
845
846 SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
847 wxDialog(parent, wxID_ANY, "Source Code",
848 wxDefaultPosition, wxSize(700,500),
849 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
850 {
851 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
852 text->SetMarginWidth(1, 30);
853 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
854 text->SetText(source);
855
856 text->StyleClearAll();
857 text->SetLexer(wxSTC_LEX_HTML);
858 text->StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(255,0,0));
859 text->StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(255,0,0));
860 text->StyleSetForeground(wxSTC_H_ENTITY, wxColour(255,0,0));
861 text->StyleSetForeground(wxSTC_H_TAG, wxColour(0,150,0));
862 text->StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(0,150,0));
863 text->StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,0,150));
864 text->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(0,0,150));
865 text->StyleSetForeground(wxSTC_H_COMMENT, wxColour(150,150,150));
866
867 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
868 sizer->Add(text, 1, wxEXPAND);
869 SetSizer(sizer);
870 }