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