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