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 #include "wx/artprov.h"
26 #include "wx/notifmsg.h"
27 #include "wx/settings.h"
28 #include "wx/webview.h"
29 #include "wx/webviewarchivehandler.h"
30 #include "wx/infobar.h"
31 #include "wx/filesys.h"
32 #include "wx/fs_arc.h"
34 #if !defined(__WXMSW__) && !defined(__WXPM__)
35 #include "../sample.xpm"
39 #include "wx/stc/stc.h"
41 #error "wxStyledTextControl is needed by this sample"
44 #if defined(__WXMSW__) || defined(__WXOSX__)
46 #include "refresh.xpm"
52 //We map menu items to their history items
53 WX_DECLARE_HASH_MAP(int, wxSharedPtr
<wxWebViewHistoryItem
>,
54 wxIntegerHash
, wxIntegerEqual
, wxMenuHistoryMap
);
56 class WebApp
: public wxApp
59 virtual bool OnInit();
62 class WebFrame
: public wxFrame
67 void OnAnimationTimer(wxTimerEvent
& evt
);
69 void OnUrl(wxCommandEvent
& evt
);
70 void OnBack(wxCommandEvent
& evt
);
71 void OnForward(wxCommandEvent
& evt
);
72 void OnStop(wxCommandEvent
& evt
);
73 void OnReload(wxCommandEvent
& evt
);
74 void OnClearHistory(wxCommandEvent
& evt
);
75 void OnEnableHistory(wxCommandEvent
& evt
);
76 void OnNavigationRequest(wxWebViewEvent
& evt
);
77 void OnNavigationComplete(wxWebViewEvent
& evt
);
78 void OnDocumentLoaded(wxWebViewEvent
& evt
);
79 void OnNewWindow(wxWebViewEvent
& evt
);
80 void OnTitleChanged(wxWebViewEvent
& evt
);
81 void OnViewSourceRequest(wxCommandEvent
& evt
);
82 void OnToolsClicked(wxCommandEvent
& evt
);
83 void OnSetZoom(wxCommandEvent
& evt
);
84 void OnError(wxWebViewEvent
& evt
);
85 void OnPrint(wxCommandEvent
& evt
);
86 void OnCut(wxCommandEvent
& evt
);
87 void OnCopy(wxCommandEvent
& evt
);
88 void OnPaste(wxCommandEvent
& evt
);
89 void OnUndo(wxCommandEvent
& evt
);
90 void OnRedo(wxCommandEvent
& evt
);
91 void OnMode(wxCommandEvent
& evt
);
92 void OnZoomLayout(wxCommandEvent
& evt
);
93 void OnHistory(wxCommandEvent
& evt
);
94 void OnRunScript(wxCommandEvent
& evt
);
95 void OnClearSelection(wxCommandEvent
& evt
);
96 void OnDeleteSelection(wxCommandEvent
& evt
);
97 void OnSelectAll(wxCommandEvent
& evt
);
98 void OnLoadScheme(wxCommandEvent
& evt
);
102 wxWebView
* m_browser
;
104 wxToolBar
* m_toolbar
;
105 wxToolBarToolBase
* m_toolbar_back
;
106 wxToolBarToolBase
* m_toolbar_forward
;
107 wxToolBarToolBase
* m_toolbar_stop
;
108 wxToolBarToolBase
* m_toolbar_reload
;
109 wxToolBarToolBase
* m_toolbar_tools
;
111 wxMenu
* m_tools_menu
;
112 wxMenu
* m_tools_history_menu
;
113 wxMenuItem
* m_tools_layout
;
114 wxMenuItem
* m_tools_tiny
;
115 wxMenuItem
* m_tools_small
;
116 wxMenuItem
* m_tools_medium
;
117 wxMenuItem
* m_tools_large
;
118 wxMenuItem
* m_tools_largest
;
119 wxMenuItem
* m_tools_handle_navigation
;
120 wxMenuItem
* m_tools_handle_new_window
;
121 wxMenuItem
* m_tools_enable_history
;
122 wxMenuItem
* m_edit_cut
;
123 wxMenuItem
* m_edit_copy
;
124 wxMenuItem
* m_edit_paste
;
125 wxMenuItem
* m_edit_undo
;
126 wxMenuItem
* m_edit_redo
;
127 wxMenuItem
* m_edit_mode
;
128 wxMenuItem
* m_selection_clear
;
129 wxMenuItem
* m_selection_delete
;
132 int m_animation_angle
;
135 wxStaticText
* m_info_text
;
137 wxMenuHistoryMap m_histMenuItems
;
140 class SourceViewDialog
: public wxDialog
143 SourceViewDialog(wxWindow
* parent
, wxString source
);
146 IMPLEMENT_APP(WebApp
)
148 // ============================================================================
150 // ============================================================================
152 bool WebApp::OnInit()
154 if ( !wxApp::OnInit() )
157 WebFrame
*frame
= new WebFrame();
163 WebFrame::WebFrame() : wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
165 //Required from virtual file system archive support
166 wxFileSystem::AddHandler(new wxArchiveFSHandler
);
168 // set the frame icon
169 SetIcon(wxICON(sample
));
170 SetTitle("wxWebView Sample");
173 m_animation_angle
= 0;
176 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
178 // Create the toolbar
179 m_toolbar
= CreateToolBar(wxTB_TEXT
);
180 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
182 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
183 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
185 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
187 wxBitmap stop
= wxBitmap(stop_xpm
);
190 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
192 wxBitmap refresh
= wxBitmap(refresh_xpm
);
195 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), back
);
196 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), forward
);
197 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), stop
);
198 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), refresh
);
199 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
200 m_toolbar
->AddControl(m_url
, _("URL"));
201 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
203 m_toolbar
->Realize();
205 // Create the info panel
206 m_info
= new wxInfoBar(this);
207 topsizer
->Add(m_info
, wxSizerFlags().Expand());
209 // Create the webview
210 m_browser
= wxWebView::New(this, wxID_ANY
, "http://www.wxwidgets.org");
211 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
213 //We register the wxfs:// protocol for testing purposes
214 m_browser
->RegisterHandler(wxSharedPtr
<wxWebViewHandler
>(new wxWebViewArchiveHandler("wxfs")));
218 //Set a more sensible size for web browsing
219 SetSize(wxSize(800, 600));
221 // Create a log window
222 new wxLogWindow(this, _("Logging"));
224 // Create the Tools menu
225 m_tools_menu
= new wxMenu();
226 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
227 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
228 m_tools_menu
->AppendSeparator();
229 m_tools_layout
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Use Layout Zoom"));
230 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
231 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
232 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
233 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
234 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
235 m_tools_menu
->AppendSeparator();
236 m_tools_handle_navigation
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle Navigation"));
237 m_tools_handle_new_window
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle New Windows"));
238 m_tools_menu
->AppendSeparator();
241 m_tools_history_menu
= new wxMenu();
242 wxMenuItem
* clearhist
= m_tools_history_menu
->Append(wxID_ANY
, _("Clear History"));
243 m_tools_enable_history
= m_tools_history_menu
->AppendCheckItem(wxID_ANY
, _("Enable History"));
244 m_tools_history_menu
->AppendSeparator();
246 m_tools_menu
->AppendSubMenu(m_tools_history_menu
, "History");
248 //Create an editing menu
249 wxMenu
* editmenu
= new wxMenu();
250 m_edit_cut
= editmenu
->Append(wxID_ANY
, _("Cut"));
251 m_edit_copy
= editmenu
->Append(wxID_ANY
, _("Copy"));
252 m_edit_paste
= editmenu
->Append(wxID_ANY
, _("Paste"));
253 editmenu
->AppendSeparator();
254 m_edit_undo
= editmenu
->Append(wxID_ANY
, _("Undo"));
255 m_edit_redo
= editmenu
->Append(wxID_ANY
, _("Redo"));
256 editmenu
->AppendSeparator();
257 m_edit_mode
= editmenu
->AppendCheckItem(wxID_ANY
, _("Edit Mode"));
259 m_tools_menu
->AppendSeparator();
260 m_tools_menu
->AppendSubMenu(editmenu
, "Edit");
262 wxMenuItem
* script
= m_tools_menu
->Append(wxID_ANY
, _("Run Script"));
265 wxMenu
* selection
= new wxMenu();
266 m_selection_clear
= selection
->Append(wxID_ANY
, _("Clear Selection"));
267 m_selection_delete
= selection
->Append(wxID_ANY
, _("Delete Selection"));
268 wxMenuItem
* selectall
= selection
->Append(wxID_ANY
, _("Select All"));
270 editmenu
->AppendSubMenu(selection
, "Selection");
272 wxMenuItem
* loadscheme
= m_tools_menu
->Append(wxID_ANY
, _("Custom Scheme Example"));
274 //By default we want to handle navigation and new windows
275 m_tools_handle_navigation
->Check();
276 m_tools_handle_new_window
->Check();
277 m_tools_enable_history
->Check();
278 if(!m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
))
279 m_tools_layout
->Enable(false);
282 // Connect the toolbar events
283 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
284 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
285 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
286 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
287 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
288 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
289 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
290 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
291 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
292 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
294 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
295 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
297 // Connect the webview events
298 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
299 wxWebViewEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
300 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
301 wxWebViewEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
302 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
303 wxWebViewEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
304 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
305 wxWebViewEventHandler(WebFrame::OnError
), NULL
, this);
306 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
307 wxWebViewEventHandler(WebFrame::OnNewWindow
), NULL
, this);
308 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
309 wxWebViewEventHandler(WebFrame::OnTitleChanged
), NULL
, this);
311 // Connect the menu events
312 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
313 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
314 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
315 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
316 Connect(m_tools_layout
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
317 wxCommandEventHandler(WebFrame::OnZoomLayout
), NULL
, this );
318 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
319 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
320 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
321 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
322 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
323 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
324 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
325 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
326 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
327 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
328 Connect(clearhist
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
329 wxCommandEventHandler(WebFrame::OnClearHistory
), NULL
, this );
330 Connect(m_tools_enable_history
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
331 wxCommandEventHandler(WebFrame::OnEnableHistory
), NULL
, this );
332 Connect(m_edit_cut
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
333 wxCommandEventHandler(WebFrame::OnCut
), NULL
, this );
334 Connect(m_edit_copy
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
335 wxCommandEventHandler(WebFrame::OnCopy
), NULL
, this );
336 Connect(m_edit_paste
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
337 wxCommandEventHandler(WebFrame::OnPaste
), NULL
, this );
338 Connect(m_edit_undo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
339 wxCommandEventHandler(WebFrame::OnUndo
), NULL
, this );
340 Connect(m_edit_redo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
341 wxCommandEventHandler(WebFrame::OnRedo
), NULL
, this );
342 Connect(m_edit_mode
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
343 wxCommandEventHandler(WebFrame::OnMode
), NULL
, this );
344 Connect(script
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
345 wxCommandEventHandler(WebFrame::OnRunScript
), NULL
, this );
346 Connect(m_selection_clear
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
347 wxCommandEventHandler(WebFrame::OnClearSelection
), NULL
, this );
348 Connect(m_selection_delete
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
349 wxCommandEventHandler(WebFrame::OnDeleteSelection
), NULL
, this );
350 Connect(selectall
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
351 wxCommandEventHandler(WebFrame::OnSelectAll
), NULL
, this );
352 Connect(loadscheme
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
353 wxCommandEventHandler(WebFrame::OnLoadScheme
), NULL
, this );
356 void WebFrame::OnAnimationTimer(wxTimerEvent
& WXUNUSED(evt
))
358 m_animation_angle
+= 15;
359 if (m_animation_angle
> 360) m_animation_angle
-= 360;
361 wxBitmap
image(24, 24);
364 dc
.SelectObject(image
);
365 dc
.SetBackground(wxBrush(wxColour(255,0,255)));
368 if (m_animation_angle
>= 0 && m_animation_angle
<= 180)
370 dc
.SetBrush(*wxYELLOW_BRUSH
);
371 dc
.SetPen(*wxYELLOW_PEN
);
372 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
373 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
376 dc
.DrawBitmap(wxBitmap(wxlogo_xpm
), 0, 0, true);
378 if (m_animation_angle
> 180)
380 dc
.SetBrush(*wxYELLOW_BRUSH
);
381 dc
.SetPen(*wxYELLOW_PEN
);
382 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
383 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
386 image
.SetMask(new wxMask(image
, wxColour(255,0,255)));
387 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), image
);
391 * Method that retrieves the current state from the web control and updates the GUI
392 * the reflect this current state.
394 void WebFrame::UpdateState()
396 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
397 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
399 if (m_browser
->IsBusy())
403 m_timer
= new wxTimer(this);
404 this->Connect(wxEVT_TIMER
, wxTimerEventHandler(WebFrame::OnAnimationTimer
), NULL
, this);
406 m_timer
->Start(100); // start animation timer
408 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
412 if (m_timer
!= NULL
) m_timer
->Stop(); // stop animation timer
413 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), wxBitmap(wxlogo_xpm
));
414 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
417 SetTitle( m_browser
->GetCurrentTitle() );
418 m_url
->SetValue( m_browser
->GetCurrentURL() );
422 * Callback invoked when user entered an URL and pressed enter
424 void WebFrame::OnUrl(wxCommandEvent
& WXUNUSED(evt
))
426 m_browser
->LoadURL( m_url
->GetValue() );
431 * Callback invoked when user pressed the "back" button
433 void WebFrame::OnBack(wxCommandEvent
& WXUNUSED(evt
))
440 * Callback invoked when user pressed the "forward" button
442 void WebFrame::OnForward(wxCommandEvent
& WXUNUSED(evt
))
444 m_browser
->GoForward();
449 * Callback invoked when user pressed the "stop" button
451 void WebFrame::OnStop(wxCommandEvent
& WXUNUSED(evt
))
458 * Callback invoked when user pressed the "reload" button
460 void WebFrame::OnReload(wxCommandEvent
& WXUNUSED(evt
))
466 void WebFrame::OnClearHistory(wxCommandEvent
& WXUNUSED(evt
))
468 m_browser
->ClearHistory();
472 void WebFrame::OnEnableHistory(wxCommandEvent
& WXUNUSED(evt
))
474 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
478 void WebFrame::OnCut(wxCommandEvent
& WXUNUSED(evt
))
483 void WebFrame::OnCopy(wxCommandEvent
& WXUNUSED(evt
))
488 void WebFrame::OnPaste(wxCommandEvent
& WXUNUSED(evt
))
493 void WebFrame::OnUndo(wxCommandEvent
& WXUNUSED(evt
))
498 void WebFrame::OnRedo(wxCommandEvent
& WXUNUSED(evt
))
503 void WebFrame::OnMode(wxCommandEvent
& WXUNUSED(evt
))
505 m_browser
->SetEditable(m_edit_mode
->IsChecked());
508 void WebFrame::OnLoadScheme(wxCommandEvent
& WXUNUSED(evt
))
510 wxFileName
helpfile("../help/doc.zip");
511 helpfile
.MakeAbsolute();
512 wxString path
= helpfile
.GetFullPath();
513 //Under MSW we need to flip the slashes
514 path
.Replace("\\", "/");
515 path
= "wxfs:///" + path
+ ";protocol=zip/doc.htm";
516 m_browser
->LoadURL(path
);
520 * Callback invoked when there is a request to load a new page (for instance
521 * when the user clicks a link)
523 void WebFrame::OnNavigationRequest(wxWebViewEvent
& evt
)
525 if(m_info
->IsShown())
530 wxLogMessage("%s", "Navigation request to '" + evt
.GetURL() + "' (target='" +
531 evt
.GetTarget() + "')");
533 wxASSERT(m_browser
->IsBusy());
535 //If we don't want to handle navigation then veto the event and navigation
536 //will not take place, we also need to stop the loading animation
537 if(!m_tools_handle_navigation
->IsChecked())
540 if (m_timer
!= NULL
) m_timer
->Stop(); // stop animation timer
541 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), wxBitmap(wxlogo_xpm
));
542 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
551 * Callback invoked when a navigation request was accepted
553 void WebFrame::OnNavigationComplete(wxWebViewEvent
& evt
)
555 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetURL() + "'");
560 * Callback invoked when a page is finished loading
562 void WebFrame::OnDocumentLoaded(wxWebViewEvent
& evt
)
564 //Only notify if the document is the main frame, not a subframe
565 if(evt
.GetURL() == m_browser
->GetCurrentURL())
567 wxLogMessage("%s", "Document loaded; url='" + evt
.GetURL() + "'");
573 * On new window, we veto to stop extra windows appearing
575 void WebFrame::OnNewWindow(wxWebViewEvent
& evt
)
577 wxLogMessage("%s", "New window; url='" + evt
.GetURL() + "'");
579 //If we handle new window events then just load them in this window as we
580 //are a single window browser
581 if(m_tools_handle_new_window
->IsChecked())
582 m_browser
->LoadURL(evt
.GetURL());
587 void WebFrame::OnTitleChanged(wxWebViewEvent
& evt
)
589 wxLogMessage("%s", "Title changed; title='" + evt
.GetString() + "'");
594 * Invoked when user selects the "View Source" menu item
596 void WebFrame::OnViewSourceRequest(wxCommandEvent
& WXUNUSED(evt
))
598 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
603 * Invoked when user selects the "Menu" item
605 void WebFrame::OnToolsClicked(wxCommandEvent
& WXUNUSED(evt
))
607 if(m_browser
->GetCurrentURL() == "")
610 m_tools_tiny
->Check(false);
611 m_tools_small
->Check(false);
612 m_tools_medium
->Check(false);
613 m_tools_large
->Check(false);
614 m_tools_largest
->Check(false);
616 wxWebViewZoom zoom
= m_browser
->GetZoom();
619 case wxWEB_VIEW_ZOOM_TINY
:
620 m_tools_tiny
->Check();
622 case wxWEB_VIEW_ZOOM_SMALL
:
623 m_tools_small
->Check();
625 case wxWEB_VIEW_ZOOM_MEDIUM
:
626 m_tools_medium
->Check();
628 case wxWEB_VIEW_ZOOM_LARGE
:
629 m_tools_large
->Check();
631 case wxWEB_VIEW_ZOOM_LARGEST
:
632 m_tools_largest
->Check();
636 m_edit_cut
->Enable(m_browser
->CanCut());
637 m_edit_copy
->Enable(m_browser
->CanCopy());
638 m_edit_paste
->Enable(m_browser
->CanPaste());
640 m_edit_undo
->Enable(m_browser
->CanUndo());
641 m_edit_redo
->Enable(m_browser
->CanRedo());
643 m_selection_clear
->Enable(m_browser
->HasSelection());
644 m_selection_delete
->Enable(m_browser
->HasSelection());
646 //Firstly we clear the existing menu items, then we add the current ones
647 wxMenuHistoryMap::const_iterator it
;
648 for( it
= m_histMenuItems
.begin(); it
!= m_histMenuItems
.end(); ++it
)
650 m_tools_history_menu
->Destroy(it
->first
);
652 m_histMenuItems
.clear();
654 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > back
= m_browser
->GetBackwardHistory();
655 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forward
= m_browser
->GetForwardHistory();
659 for(unsigned int i
= 0; i
< back
.size(); i
++)
661 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, back
[i
]->GetTitle());
662 m_histMenuItems
[item
->GetId()] = back
[i
];
663 Connect(item
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
664 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
667 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, m_browser
->GetCurrentTitle());
670 //No need to connect the current item
671 m_histMenuItems
[item
->GetId()] = wxSharedPtr
<wxWebViewHistoryItem
>(new wxWebViewHistoryItem(m_browser
->GetCurrentURL(), m_browser
->GetCurrentTitle()));
673 for(unsigned int i
= 0; i
< forward
.size(); i
++)
675 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, forward
[i
]->GetTitle());
676 m_histMenuItems
[item
->GetId()] = forward
[i
];
677 Connect(item
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
678 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
681 wxPoint position
= ScreenToClient( wxGetMousePosition() );
682 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
686 * Invoked when user selects the zoom size in the menu
688 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
690 if (evt
.GetId() == m_tools_tiny
->GetId())
692 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
694 else if (evt
.GetId() == m_tools_small
->GetId())
696 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
698 else if (evt
.GetId() == m_tools_medium
->GetId())
700 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
702 else if (evt
.GetId() == m_tools_large
->GetId())
704 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
706 else if (evt
.GetId() == m_tools_largest
->GetId())
708 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
716 void WebFrame::OnZoomLayout(wxCommandEvent
& WXUNUSED(evt
))
718 if(m_tools_layout
->IsChecked())
719 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
721 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
724 void WebFrame::OnHistory(wxCommandEvent
& evt
)
726 m_browser
->LoadHistoryItem(m_histMenuItems
[evt
.GetId()]);
729 void WebFrame::OnRunScript(wxCommandEvent
& WXUNUSED(evt
))
731 wxTextEntryDialog
dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr
, "", wxOK
|wxCANCEL
|wxCENTRE
|wxTE_MULTILINE
);
732 if(dialog
.ShowModal() == wxID_OK
)
734 m_browser
->RunScript(dialog
.GetValue());
738 void WebFrame::OnClearSelection(wxCommandEvent
& WXUNUSED(evt
))
740 m_browser
->ClearSelection();
743 void WebFrame::OnDeleteSelection(wxCommandEvent
& WXUNUSED(evt
))
745 m_browser
->DeleteSelection();
748 void WebFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(evt
))
750 m_browser
->SelectAll();
754 * Callback invoked when a loading error occurs
756 void WebFrame::OnError(wxWebViewEvent
& evt
)
758 wxString errorCategory
;
759 switch (evt
.GetInt())
761 case wxWEB_NAV_ERR_CONNECTION
:
762 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
765 case wxWEB_NAV_ERR_CERTIFICATE
:
766 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
769 case wxWEB_NAV_ERR_AUTH
:
770 errorCategory
= "wxWEB_NAV_ERR_AUTH";
773 case wxWEB_NAV_ERR_SECURITY
:
774 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
777 case wxWEB_NAV_ERR_NOT_FOUND
:
778 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
781 case wxWEB_NAV_ERR_REQUEST
:
782 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
785 case wxWEB_NAV_ERR_USER_CANCELLED
:
786 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
789 case wxWEB_NAV_ERR_OTHER
:
790 errorCategory
= "wxWEB_NAV_ERR_OTHER";
794 wxLogMessage("Error; url='" + evt
.GetURL() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
796 //Show the info bar with an error
797 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetURL() + "\n" +
798 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
804 * Invoked when user selects "Print" from the menu
806 void WebFrame::OnPrint(wxCommandEvent
& WXUNUSED(evt
))
811 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
812 wxDialog(parent
, wxID_ANY
, "Source Code",
813 wxDefaultPosition
, wxSize(700,500),
814 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
816 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
817 text
->SetMarginWidth(1, 30);
818 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
819 text
->SetText(source
);
821 text
->StyleClearAll();
822 text
->SetLexer(wxSTC_LEX_HTML
);
823 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
824 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
825 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
826 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
827 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
828 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
829 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
830 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
832 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
833 sizer
->Add(text
, 1, wxEXPAND
);