]> git.saurik.com Git - wxWidgets.git/blame - samples/webview/webview.cpp
Rebake trunk after wxWebView merge and add missing project files for the wxWebView...
[wxWidgets.git] / samples / webview / webview.cpp
CommitLineData
61b98a2d 1/////////////////////////////////////////////////////////////////////////////
467d261e 2// Name: webview.cpp
ec4ac0f7 3// Purpose: wxWebView sample
61b98a2d
SL
4// Author: Marianne Gagnon
5// Id: $Id$
ec4ac0f7 6// Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
61b98a2d
SL
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
abcb9c6e 9
ec4ac0f7
SL
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
b8a14a17
SC
25#include "wx/artprov.h"
26#include "wx/notifmsg.h"
27#include "wx/settings.h"
28#include "wx/webview.h"
7d8d6163 29#include "wx/webviewarchivehandler.h"
b8a14a17
SC
30#include "wx/infobar.h"
31#include "wx/filesys.h"
32#include "wx/fs_arc.h"
ec4ac0f7
SL
33
34#if !defined(__WXMSW__) && !defined(__WXPM__)
35 #include "../sample.xpm"
36#endif
61b98a2d
SL
37
38#if wxUSE_STC
b8a14a17 39#include "wx/stc/stc.h"
61b98a2d
SL
40#else
41#error "wxStyledTextControl is needed by this sample"
42#endif
43
8bccab8f 44#if defined(__WXMSW__) || defined(__WXOSX__)
61b98a2d
SL
45#include "stop.xpm"
46#include "refresh.xpm"
8bccab8f
SL
47#endif
48
dffce0bd 49#include "wxlogo.xpm"
61b98a2d 50
10ad4ba6
SL
51
52//We map menu items to their history items
c13d6ac1 53WX_DECLARE_HASH_MAP(int, wxSharedPtr<wxWebViewHistoryItem>,
10ad4ba6
SL
54 wxIntegerHash, wxIntegerEqual, wxMenuHistoryMap);
55
ec4ac0f7 56class WebApp : public wxApp
61b98a2d
SL
57{
58public:
ec4ac0f7 59 virtual bool OnInit();
61b98a2d
SL
60};
61
ec4ac0f7 62class WebFrame : public wxFrame
61b98a2d 63{
ec4ac0f7
SL
64public:
65 WebFrame();
66
67 void OnAnimationTimer(wxTimerEvent& evt);
68 void UpdateState();
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);
152a5808
SL
74 void OnClearHistory(wxCommandEvent& evt);
75 void OnEnableHistory(wxCommandEvent& evt);
04fa04d8
SL
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);
ec4ac0f7
SL
81 void OnViewSourceRequest(wxCommandEvent& evt);
82 void OnToolsClicked(wxCommandEvent& evt);
83 void OnSetZoom(wxCommandEvent& evt);
04fa04d8 84 void OnError(wxWebViewEvent& evt);
ec4ac0f7 85 void OnPrint(wxCommandEvent& evt);
4681a3ea
SL
86 void OnCut(wxCommandEvent& evt);
87 void OnCopy(wxCommandEvent& evt);
88 void OnPaste(wxCommandEvent& evt);
97e49559
SL
89 void OnUndo(wxCommandEvent& evt);
90 void OnRedo(wxCommandEvent& evt);
c7cbe308 91 void OnMode(wxCommandEvent& evt);
87d482ec 92 void OnZoomLayout(wxCommandEvent& evt);
10ad4ba6 93 void OnHistory(wxCommandEvent& evt);
54883129 94 void OnRunScript(wxCommandEvent& evt);
603cfe42
SL
95 void OnClearSelection(wxCommandEvent& evt);
96 void OnDeleteSelection(wxCommandEvent& evt);
97 void OnSelectAll(wxCommandEvent& evt);
49f07bec 98 void OnLoadScheme(wxCommandEvent& evt);
ec4ac0f7
SL
99
100private:
101 wxTextCtrl* m_url;
102 wxWebView* m_browser;
103
61b98a2d 104 wxToolBar* m_toolbar;
ec4ac0f7
SL
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;
110
111 wxMenu* m_tools_menu;
10ad4ba6 112 wxMenu* m_tools_history_menu;
87d482ec 113 wxMenuItem* m_tools_layout;
ec4ac0f7
SL
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;
62e8551f
SL
119 wxMenuItem* m_tools_handle_navigation;
120 wxMenuItem* m_tools_handle_new_window;
152a5808 121 wxMenuItem* m_tools_enable_history;
4681a3ea
SL
122 wxMenuItem* m_edit_cut;
123 wxMenuItem* m_edit_copy;
124 wxMenuItem* m_edit_paste;
97e49559
SL
125 wxMenuItem* m_edit_undo;
126 wxMenuItem* m_edit_redo;
c7cbe308 127 wxMenuItem* m_edit_mode;
603cfe42
SL
128 wxMenuItem* m_selection_clear;
129 wxMenuItem* m_selection_delete;
ec4ac0f7 130
61b98a2d
SL
131 wxTimer* m_timer;
132 int m_animation_angle;
ec4ac0f7 133
ec4ac0f7
SL
134 wxInfoBar *m_info;
135 wxStaticText* m_info_text;
10ad4ba6
SL
136
137 wxMenuHistoryMap m_histMenuItems;
ec4ac0f7
SL
138};
139
140class SourceViewDialog : public wxDialog
141{
61b98a2d 142public:
ec4ac0f7
SL
143 SourceViewDialog(wxWindow* parent, wxString source);
144};
145
146IMPLEMENT_APP(WebApp)
147
148// ============================================================================
149// implementation
150// ============================================================================
151
152bool WebApp::OnInit()
153{
154 if ( !wxApp::OnInit() )
155 return false;
156
157 WebFrame *frame = new WebFrame();
158 frame->Show();
159
160 return true;
161}
162
163WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample")
164{
eff8f795
SL
165 //Required from virtual file system archive support
166 wxFileSystem::AddHandler(new wxArchiveFSHandler);
167
ec4ac0f7
SL
168 // set the frame icon
169 SetIcon(wxICON(sample));
170 SetTitle("wxWebView Sample");
171
172 m_timer = NULL;
173 m_animation_angle = 0;
174
175
176 wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
177
178 // Create the toolbar
179 m_toolbar = CreateToolBar(wxTB_TEXT);
180 m_toolbar->SetToolBitmapSize(wxSize(32, 32));
abcb9c6e 181
dffce0bd
SL
182 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR);
183 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR);
184 #ifdef __WXGTK__
185 wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR);
abcb9c6e 186 #else
dffce0bd
SL
187 wxBitmap stop = wxBitmap(stop_xpm);
188 #endif
189 #ifdef __WXGTK__
190 wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR);
abcb9c6e 191 #else
dffce0bd
SL
192 wxBitmap refresh = wxBitmap(refresh_xpm);
193 #endif
194
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);
ec4ac0f7 199 m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER );
9a00d078 200 m_toolbar->AddControl(m_url, _("URL"));
ec4ac0f7
SL
201 m_toolbar_tools = m_toolbar->AddTool(wxID_ANY, _("Menu"), wxBitmap(wxlogo_xpm));
202
203 m_toolbar->Realize();
204
205 // Create the info panel
206 m_info = new wxInfoBar(this);
207 topsizer->Add(m_info, wxSizerFlags().Expand());
208
209 // Create the webview
9ef101cd 210 m_browser = wxWebView::New(this, wxID_ANY, "http://www.wxwidgets.org");
ec4ac0f7
SL
211 topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
212
7d8d6163
SL
213 //We register the wxfs:// protocol for testing purposes
214 m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
eff8f795 215
ec4ac0f7
SL
216 SetSizer(topsizer);
217
9a00d078
SL
218 //Set a more sensible size for web browsing
219 SetSize(wxSize(800, 600));
220
ec4ac0f7
SL
221 // Create a log window
222 new wxLogWindow(this, _("Logging"));
223
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();
87d482ec 229 m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom"));
ec4ac0f7
SL
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"));
62e8551f
SL
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"));
152a5808 238 m_tools_menu->AppendSeparator();
10ad4ba6
SL
239
240 //History menu
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();
245
54883129 246 m_tools_menu->AppendSubMenu(m_tools_history_menu, "History");
62e8551f 247
4681a3ea
SL
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"));
97e49559
SL
253 editmenu->AppendSeparator();
254 m_edit_undo = editmenu->Append(wxID_ANY, _("Undo"));
255 m_edit_redo = editmenu->Append(wxID_ANY, _("Redo"));
c7cbe308
SL
256 editmenu->AppendSeparator();
257 m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode"));
4681a3ea
SL
258
259 m_tools_menu->AppendSeparator();
260 m_tools_menu->AppendSubMenu(editmenu, "Edit");
261
54883129
SL
262 wxMenuItem* script = m_tools_menu->Append(wxID_ANY, _("Run Script"));
263
603cfe42
SL
264 //Selection menu
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"));
269
270 editmenu->AppendSubMenu(selection, "Selection");
271
49f07bec
SL
272 wxMenuItem* loadscheme = m_tools_menu->Append(wxID_ANY, _("Custom Scheme Example"));
273
61635eed 274 //By default we want to handle navigation and new windows
62e8551f
SL
275 m_tools_handle_navigation->Check();
276 m_tools_handle_new_window->Check();
152a5808 277 m_tools_enable_history->Check();
87d482ec
SL
278 if(!m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT))
279 m_tools_layout->Enable(false);
ec4ac0f7
SL
280
281
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 );
293
abcb9c6e 294 Connect(m_url->GetId(), wxEVT_COMMAND_TEXT_ENTER,
ec4ac0f7
SL
295 wxCommandEventHandler(WebFrame::OnUrl), NULL, this );
296
297 // Connect the webview events
298 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
04fa04d8 299 wxWebViewEventHandler(WebFrame::OnNavigationRequest), NULL, this);
ec4ac0f7 300 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
04fa04d8 301 wxWebViewEventHandler(WebFrame::OnNavigationComplete), NULL, this);
ec4ac0f7 302 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED,
abcb9c6e 303 wxWebViewEventHandler(WebFrame::OnDocumentLoaded), NULL, this);
ec4ac0f7 304 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR,
04fa04d8 305 wxWebViewEventHandler(WebFrame::OnError), NULL, this);
ec4ac0f7 306 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
04fa04d8 307 wxWebViewEventHandler(WebFrame::OnNewWindow), NULL, this);
153530af 308 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
04fa04d8 309 wxWebViewEventHandler(WebFrame::OnTitleChanged), NULL, this);
ec4ac0f7
SL
310
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 );
87d482ec
SL
316 Connect(m_tools_layout->GetId(), wxEVT_COMMAND_MENU_SELECTED,
317 wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this );
ec4ac0f7
SL
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 );
152a5808
SL
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 );
4681a3ea
SL
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 );
97e49559
SL
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 );
c7cbe308
SL
342 Connect(m_edit_mode->GetId(), wxEVT_COMMAND_MENU_SELECTED,
343 wxCommandEventHandler(WebFrame::OnMode), NULL, this );
54883129
SL
344 Connect(script->GetId(), wxEVT_COMMAND_MENU_SELECTED,
345 wxCommandEventHandler(WebFrame::OnRunScript), NULL, this );
603cfe42
SL
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 );
49f07bec
SL
352 Connect(loadscheme->GetId(), wxEVT_COMMAND_MENU_SELECTED,
353 wxCommandEventHandler(WebFrame::OnLoadScheme), NULL, this );
ec4ac0f7
SL
354}
355
8bccab8f 356void WebFrame::OnAnimationTimer(wxTimerEvent& WXUNUSED(evt))
ec4ac0f7
SL
357{
358 m_animation_angle += 15;
359 if (m_animation_angle > 360) m_animation_angle -= 360;
abcb9c6e
JS
360
361 wxBitmap image(24, 24);
61b98a2d 362 {
61b98a2d
SL
363 wxMemoryDC dc;
364 dc.SelectObject(image);
365 dc.SetBackground(wxBrush(wxColour(255,0,255)));
366 dc.Clear();
abcb9c6e 367
61b98a2d
SL
368 if (m_animation_angle >= 0 && m_animation_angle <= 180)
369 {
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),
ec4ac0f7 373 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 );
61b98a2d 374 }
abcb9c6e 375
61b98a2d 376 dc.DrawBitmap(wxBitmap(wxlogo_xpm), 0, 0, true);
abcb9c6e 377
61b98a2d
SL
378 if (m_animation_angle > 180)
379 {
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),
ec4ac0f7 383 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 );
61b98a2d 384 }
abcb9c6e 385 }
ec4ac0f7
SL
386 image.SetMask(new wxMask(image, wxColour(255,0,255)));
387 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), image);
388}
389
390/**
391 * Method that retrieves the current state from the web control and updates the GUI
392 * the reflect this current state.
393 */
394void WebFrame::UpdateState()
395{
396 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
397 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
abcb9c6e 398
ec4ac0f7 399 if (m_browser->IsBusy())
61b98a2d 400 {
ec4ac0f7 401 if (m_timer == NULL)
61b98a2d 402 {
ec4ac0f7
SL
403 m_timer = new wxTimer(this);
404 this->Connect(wxEVT_TIMER, wxTimerEventHandler(WebFrame::OnAnimationTimer), NULL, this);
61b98a2d 405 }
ec4ac0f7 406 m_timer->Start(100); // start animation timer
abcb9c6e
JS
407
408 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
61b98a2d 409 }
ec4ac0f7 410 else
61b98a2d 411 {
ec4ac0f7
SL
412 if (m_timer != NULL) m_timer->Stop(); // stop animation timer
413 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), wxBitmap(wxlogo_xpm));
abcb9c6e 414 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
61b98a2d 415 }
abcb9c6e 416
ec4ac0f7
SL
417 SetTitle( m_browser->GetCurrentTitle() );
418 m_url->SetValue( m_browser->GetCurrentURL() );
419}
420
421/**
422 * Callback invoked when user entered an URL and pressed enter
423 */
8bccab8f 424void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7 425{
4d0dddc7 426 m_browser->LoadURL( m_url->GetValue() );
ec4ac0f7
SL
427 UpdateState();
428}
429
430/**
431 * Callback invoked when user pressed the "back" button
432 */
8bccab8f 433void WebFrame::OnBack(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
434{
435 m_browser->GoBack();
436 UpdateState();
437}
438
439/**
440 * Callback invoked when user pressed the "forward" button
441 */
8bccab8f 442void WebFrame::OnForward(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
443{
444 m_browser->GoForward();
445 UpdateState();
446}
447
448/**
449 * Callback invoked when user pressed the "stop" button
450 */
8bccab8f 451void WebFrame::OnStop(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
452{
453 m_browser->Stop();
454 UpdateState();
455}
456
457/**
458 * Callback invoked when user pressed the "reload" button
459 */
8bccab8f 460void WebFrame::OnReload(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
461{
462 m_browser->Reload();
463 UpdateState();
464}
465
8bccab8f 466void WebFrame::OnClearHistory(wxCommandEvent& WXUNUSED(evt))
152a5808
SL
467{
468 m_browser->ClearHistory();
469 UpdateState();
470}
471
8bccab8f 472void WebFrame::OnEnableHistory(wxCommandEvent& WXUNUSED(evt))
152a5808
SL
473{
474 m_browser->EnableHistory(m_tools_enable_history->IsChecked());
475 UpdateState();
476}
477
8bccab8f 478void WebFrame::OnCut(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
479{
480 m_browser->Cut();
481}
482
8bccab8f 483void WebFrame::OnCopy(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
484{
485 m_browser->Copy();
486}
487
8bccab8f 488void WebFrame::OnPaste(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
489{
490 m_browser->Paste();
491}
492
8bccab8f 493void WebFrame::OnUndo(wxCommandEvent& WXUNUSED(evt))
97e49559
SL
494{
495 m_browser->Undo();
496}
497
8bccab8f 498void WebFrame::OnRedo(wxCommandEvent& WXUNUSED(evt))
97e49559
SL
499{
500 m_browser->Redo();
501}
502
8bccab8f 503void WebFrame::OnMode(wxCommandEvent& WXUNUSED(evt))
c7cbe308
SL
504{
505 m_browser->SetEditable(m_edit_mode->IsChecked());
506}
507
49f07bec
SL
508void WebFrame::OnLoadScheme(wxCommandEvent& WXUNUSED(evt))
509{
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";
4d0dddc7 516 m_browser->LoadURL(path);
49f07bec 517}
c7cbe308 518
ec4ac0f7
SL
519/**
520 * Callback invoked when there is a request to load a new page (for instance
521 * when the user clicks a link)
522 */
04fa04d8 523void WebFrame::OnNavigationRequest(wxWebViewEvent& evt)
ec4ac0f7 524{
75b0b0bc
SL
525 if(m_info->IsShown())
526 {
70544c1e 527 m_info->Dismiss();
75b0b0bc
SL
528 }
529
e40741b9 530 wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" +
ec4ac0f7 531 evt.GetTarget() + "')");
abcb9c6e 532
ec4ac0f7 533 wxASSERT(m_browser->IsBusy());
62e8551f
SL
534
535 //If we don't want to handle navigation then veto the event and navigation
0e830c7c 536 //will not take place, we also need to stop the loading animation
62e8551f 537 if(!m_tools_handle_navigation->IsChecked())
0e830c7c 538 {
62e8551f 539 evt.Veto();
0e830c7c
SL
540 if (m_timer != NULL) m_timer->Stop(); // stop animation timer
541 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), wxBitmap(wxlogo_xpm));
abcb9c6e 542 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
0e830c7c
SL
543 }
544 else
545 {
546 UpdateState();
547 }
ec4ac0f7
SL
548}
549
550/**
551 * Callback invoked when a navigation request was accepted
552 */
04fa04d8 553void WebFrame::OnNavigationComplete(wxWebViewEvent& evt)
ec4ac0f7 554{
e40741b9 555 wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'");
ec4ac0f7
SL
556 UpdateState();
557}
558
559/**
560 * Callback invoked when a page is finished loading
561 */
04fa04d8 562void WebFrame::OnDocumentLoaded(wxWebViewEvent& evt)
ec4ac0f7 563{
113e0a92 564 //Only notify if the document is the main frame, not a subframe
e40741b9 565 if(evt.GetURL() == m_browser->GetCurrentURL())
8bccab8f 566 {
e40741b9 567 wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'");
8bccab8f 568 }
ec4ac0f7
SL
569 UpdateState();
570}
571
572/**
573 * On new window, we veto to stop extra windows appearing
574 */
04fa04d8 575void WebFrame::OnNewWindow(wxWebViewEvent& evt)
ec4ac0f7 576{
e40741b9 577 wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'");
62e8551f 578
abcb9c6e 579 //If we handle new window events then just load them in this window as we
62e8551f
SL
580 //are a single window browser
581 if(m_tools_handle_new_window->IsChecked())
4d0dddc7 582 m_browser->LoadURL(evt.GetURL());
62e8551f 583
ec4ac0f7
SL
584 UpdateState();
585}
586
04fa04d8 587void WebFrame::OnTitleChanged(wxWebViewEvent& evt)
153530af
SL
588{
589 wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'");
590 UpdateState();
591}
592
ec4ac0f7
SL
593/**
594 * Invoked when user selects the "View Source" menu item
595 */
8bccab8f 596void WebFrame::OnViewSourceRequest(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
597{
598 SourceViewDialog dlg(this, m_browser->GetPageSource());
599 dlg.ShowModal();
600}
601
602/**
603 * Invoked when user selects the "Menu" item
604 */
8bccab8f 605void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
606{
607 if(m_browser->GetCurrentURL() == "")
608 return;
609
610 m_tools_tiny->Check(false);
abcb9c6e 611 m_tools_small->Check(false);
ec4ac0f7
SL
612 m_tools_medium->Check(false);
613 m_tools_large->Check(false);
614 m_tools_largest->Check(false);
615
616 wxWebViewZoom zoom = m_browser->GetZoom();
617 switch (zoom)
61b98a2d 618 {
ec4ac0f7
SL
619 case wxWEB_VIEW_ZOOM_TINY:
620 m_tools_tiny->Check();
621 break;
622 case wxWEB_VIEW_ZOOM_SMALL:
623 m_tools_small->Check();
624 break;
625 case wxWEB_VIEW_ZOOM_MEDIUM:
626 m_tools_medium->Check();
627 break;
628 case wxWEB_VIEW_ZOOM_LARGE:
629 m_tools_large->Check();
630 break;
631 case wxWEB_VIEW_ZOOM_LARGEST:
632 m_tools_largest->Check();
633 break;
61b98a2d 634 }
4681a3ea
SL
635
636 m_edit_cut->Enable(m_browser->CanCut());
637 m_edit_copy->Enable(m_browser->CanCopy());
638 m_edit_paste->Enable(m_browser->CanPaste());
97e49559
SL
639
640 m_edit_undo->Enable(m_browser->CanUndo());
641 m_edit_redo->Enable(m_browser->CanRedo());
10ad4ba6 642
603cfe42
SL
643 m_selection_clear->Enable(m_browser->HasSelection());
644 m_selection_delete->Enable(m_browser->HasSelection());
645
10ad4ba6
SL
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 )
649 {
650 m_tools_history_menu->Destroy(it->first);
651 }
652 m_histMenuItems.clear();
653
c13d6ac1
SL
654 wxVector<wxSharedPtr<wxWebViewHistoryItem> > back = m_browser->GetBackwardHistory();
655 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forward = m_browser->GetForwardHistory();
10ad4ba6
SL
656
657 wxMenuItem* item;
658
abcb9c6e
JS
659 unsigned int i;
660 for(i = 0; i < back.size(); i++)
10ad4ba6
SL
661 {
662 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, back[i]->GetTitle());
663 m_histMenuItems[item->GetId()] = back[i];
664 Connect(item->GetId(), wxEVT_COMMAND_MENU_SELECTED,
665 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
666 }
667
668 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, m_browser->GetCurrentTitle());
669 item->Check();
670
671 //No need to connect the current item
c13d6ac1 672 m_histMenuItems[item->GetId()] = wxSharedPtr<wxWebViewHistoryItem>(new wxWebViewHistoryItem(m_browser->GetCurrentURL(), m_browser->GetCurrentTitle()));
10ad4ba6 673
abcb9c6e 674 for(i = 0; i < forward.size(); i++)
10ad4ba6
SL
675 {
676 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, forward[i]->GetTitle());
677 m_histMenuItems[item->GetId()] = forward[i];
678 Connect(item->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
679 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
680 }
abcb9c6e 681
ec4ac0f7
SL
682 wxPoint position = ScreenToClient( wxGetMousePosition() );
683 PopupMenu(m_tools_menu, position.x, position.y);
684}
685
686/**
687 * Invoked when user selects the zoom size in the menu
688 */
689void WebFrame::OnSetZoom(wxCommandEvent& evt)
690{
691 if (evt.GetId() == m_tools_tiny->GetId())
61b98a2d 692 {
ec4ac0f7 693 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
61b98a2d 694 }
ec4ac0f7 695 else if (evt.GetId() == m_tools_small->GetId())
61b98a2d 696 {
ec4ac0f7 697 m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL);
61b98a2d 698 }
ec4ac0f7 699 else if (evt.GetId() == m_tools_medium->GetId())
61b98a2d 700 {
ec4ac0f7 701 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
61b98a2d 702 }
ec4ac0f7 703 else if (evt.GetId() == m_tools_large->GetId())
61b98a2d 704 {
ec4ac0f7 705 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE);
61b98a2d 706 }
ec4ac0f7 707 else if (evt.GetId() == m_tools_largest->GetId())
61b98a2d 708 {
ec4ac0f7 709 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST);
61b98a2d 710 }
ec4ac0f7 711 else
61b98a2d 712 {
ec4ac0f7 713 wxFAIL;
61b98a2d 714 }
ec4ac0f7 715}
853b6cd0 716
8bccab8f 717void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt))
87d482ec
SL
718{
719 if(m_tools_layout->IsChecked())
720 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT);
721 else
722 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT);
723}
724
10ad4ba6
SL
725void WebFrame::OnHistory(wxCommandEvent& evt)
726{
727 m_browser->LoadHistoryItem(m_histMenuItems[evt.GetId()]);
728}
729
54883129
SL
730void WebFrame::OnRunScript(wxCommandEvent& WXUNUSED(evt))
731{
732 wxTextEntryDialog dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr, "", wxOK|wxCANCEL|wxCENTRE|wxTE_MULTILINE);
733 if(dialog.ShowModal() == wxID_OK)
734 {
735 m_browser->RunScript(dialog.GetValue());
736 }
737}
738
603cfe42
SL
739void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))
740{
741 m_browser->ClearSelection();
742}
743
744void WebFrame::OnDeleteSelection(wxCommandEvent& WXUNUSED(evt))
745{
746 m_browser->DeleteSelection();
747}
748
749void WebFrame::OnSelectAll(wxCommandEvent& WXUNUSED(evt))
750{
751 m_browser->SelectAll();
752}
753
ec4ac0f7
SL
754/**
755 * Callback invoked when a loading error occurs
756 */
04fa04d8 757void WebFrame::OnError(wxWebViewEvent& evt)
ec4ac0f7
SL
758{
759 wxString errorCategory;
760 switch (evt.GetInt())
61b98a2d 761 {
ec4ac0f7
SL
762 case wxWEB_NAV_ERR_CONNECTION:
763 errorCategory = "wxWEB_NAV_ERR_CONNECTION";
764 break;
abcb9c6e 765
ec4ac0f7
SL
766 case wxWEB_NAV_ERR_CERTIFICATE:
767 errorCategory = "wxWEB_NAV_ERR_CERTIFICATE";
768 break;
abcb9c6e 769
ec4ac0f7
SL
770 case wxWEB_NAV_ERR_AUTH:
771 errorCategory = "wxWEB_NAV_ERR_AUTH";
772 break;
abcb9c6e 773
ec4ac0f7
SL
774 case wxWEB_NAV_ERR_SECURITY:
775 errorCategory = "wxWEB_NAV_ERR_SECURITY";
776 break;
abcb9c6e 777
ec4ac0f7
SL
778 case wxWEB_NAV_ERR_NOT_FOUND:
779 errorCategory = "wxWEB_NAV_ERR_NOT_FOUND";
780 break;
abcb9c6e 781
ec4ac0f7
SL
782 case wxWEB_NAV_ERR_REQUEST:
783 errorCategory = "wxWEB_NAV_ERR_REQUEST";
784 break;
abcb9c6e 785
ec4ac0f7
SL
786 case wxWEB_NAV_ERR_USER_CANCELLED:
787 errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED";
788 break;
abcb9c6e 789
ec4ac0f7
SL
790 case wxWEB_NAV_ERR_OTHER:
791 errorCategory = "wxWEB_NAV_ERR_OTHER";
792 break;
61b98a2d 793 }
abcb9c6e 794
e40741b9 795 wxLogMessage("Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")");
abcb9c6e 796
ec4ac0f7 797 //Show the info bar with an error
e40741b9 798 m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +
ec4ac0f7 799 "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR);
abcb9c6e 800
ec4ac0f7
SL
801 UpdateState();
802}
61b98a2d 803
ec4ac0f7
SL
804/**
805 * Invoked when user selects "Print" from the menu
806 */
8bccab8f 807void WebFrame::OnPrint(wxCommandEvent& WXUNUSED(evt))
61b98a2d 808{
ec4ac0f7
SL
809 m_browser->Print();
810}
61b98a2d 811
ec4ac0f7
SL
812SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
813 wxDialog(parent, wxID_ANY, "Source Code",
814 wxDefaultPosition, wxSize(700,500),
815 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
816{
817 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
818 text->SetMarginWidth(1, 30);
abcb9c6e 819 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
ec4ac0f7 820 text->SetText(source);
61b98a2d 821
ec4ac0f7
SL
822 text->StyleClearAll();
823 text->SetLexer(wxSTC_LEX_HTML);
824 text->StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(255,0,0));
825 text->StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(255,0,0));
826 text->StyleSetForeground(wxSTC_H_ENTITY, wxColour(255,0,0));
827 text->StyleSetForeground(wxSTC_H_TAG, wxColour(0,150,0));
828 text->StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(0,150,0));
829 text->StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,0,150));
830 text->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(0,0,150));
831 text->StyleSetForeground(wxSTC_H_COMMENT, wxColour(150,150,150));
61b98a2d 832
61b98a2d 833 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
ec4ac0f7
SL
834 sizer->Add(text, 1, wxEXPAND);
835 SetSizer(sizer);
61b98a2d 836}