]> git.saurik.com Git - wxWidgets.git/blame - samples/web/web.cpp
Fix loading history items under GTK. The wrong function was being used.
[wxWidgets.git] / samples / web / web.cpp
CommitLineData
61b98a2d 1/////////////////////////////////////////////////////////////////////////////
ec4ac0f7
SL
2// Name: web.cpp
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/////////////////////////////////////////////////////////////////////////////
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"
29#include "wx/webviewfilehandler.h"
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
ec4ac0f7 51class WebApp : public wxApp
61b98a2d
SL
52{
53public:
ec4ac0f7 54 virtual bool OnInit();
61b98a2d
SL
55};
56
ec4ac0f7 57class WebFrame : public wxFrame
61b98a2d 58{
ec4ac0f7
SL
59public:
60 WebFrame();
61
62 void OnAnimationTimer(wxTimerEvent& evt);
63 void UpdateState();
64 void OnUrl(wxCommandEvent& evt);
65 void OnBack(wxCommandEvent& evt);
66 void OnForward(wxCommandEvent& evt);
67 void OnStop(wxCommandEvent& evt);
68 void OnReload(wxCommandEvent& evt);
152a5808
SL
69 void OnClearHistory(wxCommandEvent& evt);
70 void OnEnableHistory(wxCommandEvent& evt);
ec4ac0f7
SL
71 void OnNavigationRequest(wxWebNavigationEvent& evt);
72 void OnNavigationComplete(wxWebNavigationEvent& evt);
73 void OnDocumentLoaded(wxWebNavigationEvent& evt);
74 void OnNewWindow(wxWebNavigationEvent& evt);
153530af 75 void OnTitleChanged(wxWebNavigationEvent& evt);
ec4ac0f7
SL
76 void OnViewSourceRequest(wxCommandEvent& evt);
77 void OnToolsClicked(wxCommandEvent& evt);
78 void OnSetZoom(wxCommandEvent& evt);
79 void OnError(wxWebNavigationEvent& evt);
80 void OnPrint(wxCommandEvent& evt);
4681a3ea
SL
81 void OnCut(wxCommandEvent& evt);
82 void OnCopy(wxCommandEvent& evt);
83 void OnPaste(wxCommandEvent& evt);
97e49559
SL
84 void OnUndo(wxCommandEvent& evt);
85 void OnRedo(wxCommandEvent& evt);
c7cbe308 86 void OnMode(wxCommandEvent& evt);
87d482ec 87 void OnZoomLayout(wxCommandEvent& evt);
ec4ac0f7
SL
88
89private:
90 wxTextCtrl* m_url;
91 wxWebView* m_browser;
92
61b98a2d 93 wxToolBar* m_toolbar;
ec4ac0f7
SL
94 wxToolBarToolBase* m_toolbar_back;
95 wxToolBarToolBase* m_toolbar_forward;
96 wxToolBarToolBase* m_toolbar_stop;
97 wxToolBarToolBase* m_toolbar_reload;
98 wxToolBarToolBase* m_toolbar_tools;
99
100 wxMenu* m_tools_menu;
87d482ec 101 wxMenuItem* m_tools_layout;
ec4ac0f7
SL
102 wxMenuItem* m_tools_tiny;
103 wxMenuItem* m_tools_small;
104 wxMenuItem* m_tools_medium;
105 wxMenuItem* m_tools_large;
106 wxMenuItem* m_tools_largest;
62e8551f
SL
107 wxMenuItem* m_tools_handle_navigation;
108 wxMenuItem* m_tools_handle_new_window;
152a5808 109 wxMenuItem* m_tools_enable_history;
4681a3ea
SL
110 wxMenuItem* m_edit_cut;
111 wxMenuItem* m_edit_copy;
112 wxMenuItem* m_edit_paste;
97e49559
SL
113 wxMenuItem* m_edit_undo;
114 wxMenuItem* m_edit_redo;
c7cbe308 115 wxMenuItem* m_edit_mode;
ec4ac0f7 116
61b98a2d
SL
117 wxTimer* m_timer;
118 int m_animation_angle;
ec4ac0f7 119
ec4ac0f7
SL
120 wxInfoBar *m_info;
121 wxStaticText* m_info_text;
122};
123
124class SourceViewDialog : public wxDialog
125{
61b98a2d 126public:
ec4ac0f7
SL
127 SourceViewDialog(wxWindow* parent, wxString source);
128};
129
130IMPLEMENT_APP(WebApp)
131
132// ============================================================================
133// implementation
134// ============================================================================
135
136bool WebApp::OnInit()
137{
138 if ( !wxApp::OnInit() )
139 return false;
140
141 WebFrame *frame = new WebFrame();
142 frame->Show();
143
144 return true;
145}
146
147WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample")
148{
eff8f795
SL
149 //Required from virtual file system archive support
150 wxFileSystem::AddHandler(new wxArchiveFSHandler);
151
ec4ac0f7
SL
152 // set the frame icon
153 SetIcon(wxICON(sample));
154 SetTitle("wxWebView Sample");
155
156 m_timer = NULL;
157 m_animation_angle = 0;
158
159
160 wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
161
162 // Create the toolbar
163 m_toolbar = CreateToolBar(wxTB_TEXT);
164 m_toolbar->SetToolBitmapSize(wxSize(32, 32));
dffce0bd
SL
165
166 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR);
167 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR);
168 #ifdef __WXGTK__
169 wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR);
170 #else
171 wxBitmap stop = wxBitmap(stop_xpm);
172 #endif
173 #ifdef __WXGTK__
174 wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR);
175 #else
176 wxBitmap refresh = wxBitmap(refresh_xpm);
177 #endif
178
179 m_toolbar_back = m_toolbar->AddTool(wxID_ANY, _("Back"), back);
180 m_toolbar_forward = m_toolbar->AddTool(wxID_ANY, _("Forward"), forward);
181 m_toolbar_stop = m_toolbar->AddTool(wxID_ANY, _("Stop"), stop);
182 m_toolbar_reload = m_toolbar->AddTool(wxID_ANY, _("Reload"), refresh);
ec4ac0f7 183 m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER );
9a00d078 184 m_toolbar->AddControl(m_url, _("URL"));
ec4ac0f7
SL
185 m_toolbar_tools = m_toolbar->AddTool(wxID_ANY, _("Menu"), wxBitmap(wxlogo_xpm));
186
187 m_toolbar->Realize();
188
189 // Create the info panel
190 m_info = new wxInfoBar(this);
191 topsizer->Add(m_info, wxSizerFlags().Expand());
192
193 // Create the webview
9ef101cd 194 m_browser = wxWebView::New(this, wxID_ANY, "http://www.wxwidgets.org");
ec4ac0f7
SL
195 topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
196
1c29aaca 197 //We register the file:// protocol for testing purposes
3baf235f 198 m_browser->RegisterHandler(wxSharedPtr<wxWebHandler>(new wxWebFileHandler()));
eff8f795 199
ec4ac0f7
SL
200 SetSizer(topsizer);
201
9a00d078
SL
202 //Set a more sensible size for web browsing
203 SetSize(wxSize(800, 600));
204
ec4ac0f7
SL
205 // Create a log window
206 new wxLogWindow(this, _("Logging"));
207
208 // Create the Tools menu
209 m_tools_menu = new wxMenu();
210 wxMenuItem* print = m_tools_menu->Append(wxID_ANY , _("Print"));
211 wxMenuItem* viewSource = m_tools_menu->Append(wxID_ANY , _("View Source"));
212 m_tools_menu->AppendSeparator();
87d482ec 213 m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom"));
ec4ac0f7
SL
214 m_tools_tiny = m_tools_menu->AppendCheckItem(wxID_ANY, _("Tiny"));
215 m_tools_small = m_tools_menu->AppendCheckItem(wxID_ANY, _("Small"));
216 m_tools_medium = m_tools_menu->AppendCheckItem(wxID_ANY, _("Medium"));
217 m_tools_large = m_tools_menu->AppendCheckItem(wxID_ANY, _("Large"));
218 m_tools_largest = m_tools_menu->AppendCheckItem(wxID_ANY, _("Largest"));
62e8551f
SL
219 m_tools_menu->AppendSeparator();
220 m_tools_handle_navigation = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle Navigation"));
221 m_tools_handle_new_window = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle New Windows"));
152a5808
SL
222 m_tools_menu->AppendSeparator();
223 wxMenuItem* clearhist = m_tools_menu->Append(wxID_ANY, _("Clear History"));
224 m_tools_enable_history = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable History"));
62e8551f 225
4681a3ea
SL
226 //Create an editing menu
227 wxMenu* editmenu = new wxMenu();
228 m_edit_cut = editmenu->Append(wxID_ANY, _("Cut"));
229 m_edit_copy = editmenu->Append(wxID_ANY, _("Copy"));
230 m_edit_paste = editmenu->Append(wxID_ANY, _("Paste"));
97e49559
SL
231 editmenu->AppendSeparator();
232 m_edit_undo = editmenu->Append(wxID_ANY, _("Undo"));
233 m_edit_redo = editmenu->Append(wxID_ANY, _("Redo"));
c7cbe308
SL
234 editmenu->AppendSeparator();
235 m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode"));
4681a3ea
SL
236
237 m_tools_menu->AppendSeparator();
238 m_tools_menu->AppendSubMenu(editmenu, "Edit");
239
61635eed 240 //By default we want to handle navigation and new windows
62e8551f
SL
241 m_tools_handle_navigation->Check();
242 m_tools_handle_new_window->Check();
152a5808 243 m_tools_enable_history->Check();
87d482ec
SL
244 if(!m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT))
245 m_tools_layout->Enable(false);
ec4ac0f7
SL
246
247
248 // Connect the toolbar events
249 Connect(m_toolbar_back->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
250 wxCommandEventHandler(WebFrame::OnBack), NULL, this );
251 Connect(m_toolbar_forward->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
252 wxCommandEventHandler(WebFrame::OnForward), NULL, this );
253 Connect(m_toolbar_stop->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
254 wxCommandEventHandler(WebFrame::OnStop), NULL, this );
255 Connect(m_toolbar_reload->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
256 wxCommandEventHandler(WebFrame::OnReload),NULL, this );
257 Connect(m_toolbar_tools->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
258 wxCommandEventHandler(WebFrame::OnToolsClicked), NULL, this );
259
260 Connect(m_url->GetId(), wxEVT_COMMAND_TEXT_ENTER,
261 wxCommandEventHandler(WebFrame::OnUrl), NULL, this );
262
263 // Connect the webview events
264 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
265 wxWebNavigationEventHandler(WebFrame::OnNavigationRequest), NULL, this);
266 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
267 wxWebNavigationEventHandler(WebFrame::OnNavigationComplete), NULL, this);
268 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED,
269 wxWebNavigationEventHandler(WebFrame::OnDocumentLoaded), NULL, this);
270 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR,
271 wxWebNavigationEventHandler(WebFrame::OnError), NULL, this);
272 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
273 wxWebNavigationEventHandler(WebFrame::OnNewWindow), NULL, this);
153530af
SL
274 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
275 wxWebNavigationEventHandler(WebFrame::OnTitleChanged), NULL, this);
ec4ac0f7
SL
276
277 // Connect the menu events
278 Connect(viewSource->GetId(), wxEVT_COMMAND_MENU_SELECTED,
279 wxCommandEventHandler(WebFrame::OnViewSourceRequest), NULL, this );
280 Connect(print->GetId(), wxEVT_COMMAND_MENU_SELECTED,
281 wxCommandEventHandler(WebFrame::OnPrint), NULL, this );
87d482ec
SL
282 Connect(m_tools_layout->GetId(), wxEVT_COMMAND_MENU_SELECTED,
283 wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this );
ec4ac0f7
SL
284 Connect(m_tools_tiny->GetId(), wxEVT_COMMAND_MENU_SELECTED,
285 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
286 Connect(m_tools_small->GetId(), wxEVT_COMMAND_MENU_SELECTED,
287 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
288 Connect(m_tools_medium->GetId(), wxEVT_COMMAND_MENU_SELECTED,
289 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
290 Connect(m_tools_large->GetId(), wxEVT_COMMAND_MENU_SELECTED,
291 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
292 Connect(m_tools_largest->GetId(), wxEVT_COMMAND_MENU_SELECTED,
293 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
152a5808
SL
294 Connect(clearhist->GetId(), wxEVT_COMMAND_MENU_SELECTED,
295 wxCommandEventHandler(WebFrame::OnClearHistory), NULL, this );
296 Connect(m_tools_enable_history->GetId(), wxEVT_COMMAND_MENU_SELECTED,
297 wxCommandEventHandler(WebFrame::OnEnableHistory), NULL, this );
4681a3ea
SL
298 Connect(m_edit_cut->GetId(), wxEVT_COMMAND_MENU_SELECTED,
299 wxCommandEventHandler(WebFrame::OnCut), NULL, this );
300 Connect(m_edit_copy->GetId(), wxEVT_COMMAND_MENU_SELECTED,
301 wxCommandEventHandler(WebFrame::OnCopy), NULL, this );
302 Connect(m_edit_paste->GetId(), wxEVT_COMMAND_MENU_SELECTED,
303 wxCommandEventHandler(WebFrame::OnPaste), NULL, this );
97e49559
SL
304 Connect(m_edit_undo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
305 wxCommandEventHandler(WebFrame::OnUndo), NULL, this );
306 Connect(m_edit_redo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
307 wxCommandEventHandler(WebFrame::OnRedo), NULL, this );
c7cbe308
SL
308 Connect(m_edit_mode->GetId(), wxEVT_COMMAND_MENU_SELECTED,
309 wxCommandEventHandler(WebFrame::OnMode), NULL, this );
ec4ac0f7
SL
310}
311
8bccab8f 312void WebFrame::OnAnimationTimer(wxTimerEvent& WXUNUSED(evt))
ec4ac0f7
SL
313{
314 m_animation_angle += 15;
315 if (m_animation_angle > 360) m_animation_angle -= 360;
61b98a2d 316
61635eed 317 wxBitmap image(24, 24);
61b98a2d 318 {
61b98a2d
SL
319 wxMemoryDC dc;
320 dc.SelectObject(image);
321 dc.SetBackground(wxBrush(wxColour(255,0,255)));
322 dc.Clear();
323
324 if (m_animation_angle >= 0 && m_animation_angle <= 180)
325 {
326 dc.SetBrush(*wxYELLOW_BRUSH);
327 dc.SetPen(*wxYELLOW_PEN);
328 dc.DrawCircle(16 - int(sin(m_animation_angle*0.01745f /* convert to radians */)*14.0f),
ec4ac0f7 329 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 );
61b98a2d
SL
330 }
331
332 dc.DrawBitmap(wxBitmap(wxlogo_xpm), 0, 0, true);
333
334 if (m_animation_angle > 180)
335 {
336 dc.SetBrush(*wxYELLOW_BRUSH);
337 dc.SetPen(*wxYELLOW_PEN);
338 dc.DrawCircle(16 - int(sin(m_animation_angle*0.01745f /* convert to radians */)*14.0f),
ec4ac0f7 339 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 );
61b98a2d 340 }
ec4ac0f7
SL
341 }
342 image.SetMask(new wxMask(image, wxColour(255,0,255)));
343 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), image);
344}
345
346/**
347 * Method that retrieves the current state from the web control and updates the GUI
348 * the reflect this current state.
349 */
350void WebFrame::UpdateState()
351{
352 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
353 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
61b98a2d 354
ec4ac0f7 355 if (m_browser->IsBusy())
61b98a2d 356 {
ec4ac0f7 357 if (m_timer == NULL)
61b98a2d 358 {
ec4ac0f7
SL
359 m_timer = new wxTimer(this);
360 this->Connect(wxEVT_TIMER, wxTimerEventHandler(WebFrame::OnAnimationTimer), NULL, this);
61b98a2d 361 }
ec4ac0f7 362 m_timer->Start(100); // start animation timer
61b98a2d 363
ec4ac0f7 364 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
61b98a2d 365 }
ec4ac0f7 366 else
61b98a2d 367 {
ec4ac0f7
SL
368 if (m_timer != NULL) m_timer->Stop(); // stop animation timer
369 m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), wxBitmap(wxlogo_xpm));
370 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
61b98a2d
SL
371 }
372
ec4ac0f7
SL
373 SetTitle( m_browser->GetCurrentTitle() );
374 m_url->SetValue( m_browser->GetCurrentURL() );
375}
376
377/**
378 * Callback invoked when user entered an URL and pressed enter
379 */
8bccab8f 380void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
381{
382 m_browser->LoadUrl( m_url->GetValue() );
383 UpdateState();
384}
385
386/**
387 * Callback invoked when user pressed the "back" button
388 */
8bccab8f 389void WebFrame::OnBack(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
390{
391 m_browser->GoBack();
392 UpdateState();
393}
394
395/**
396 * Callback invoked when user pressed the "forward" button
397 */
8bccab8f 398void WebFrame::OnForward(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
399{
400 m_browser->GoForward();
401 UpdateState();
402}
403
404/**
405 * Callback invoked when user pressed the "stop" button
406 */
8bccab8f 407void WebFrame::OnStop(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
408{
409 m_browser->Stop();
410 UpdateState();
411}
412
413/**
414 * Callback invoked when user pressed the "reload" button
415 */
8bccab8f 416void WebFrame::OnReload(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
417{
418 m_browser->Reload();
419 UpdateState();
420}
421
8bccab8f 422void WebFrame::OnClearHistory(wxCommandEvent& WXUNUSED(evt))
152a5808
SL
423{
424 m_browser->ClearHistory();
425 UpdateState();
426}
427
8bccab8f 428void WebFrame::OnEnableHistory(wxCommandEvent& WXUNUSED(evt))
152a5808
SL
429{
430 m_browser->EnableHistory(m_tools_enable_history->IsChecked());
431 UpdateState();
432}
433
8bccab8f 434void WebFrame::OnCut(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
435{
436 m_browser->Cut();
437}
438
8bccab8f 439void WebFrame::OnCopy(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
440{
441 m_browser->Copy();
442}
443
8bccab8f 444void WebFrame::OnPaste(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
445{
446 m_browser->Paste();
447}
448
8bccab8f 449void WebFrame::OnUndo(wxCommandEvent& WXUNUSED(evt))
97e49559
SL
450{
451 m_browser->Undo();
452}
453
8bccab8f 454void WebFrame::OnRedo(wxCommandEvent& WXUNUSED(evt))
97e49559
SL
455{
456 m_browser->Redo();
457}
458
8bccab8f 459void WebFrame::OnMode(wxCommandEvent& WXUNUSED(evt))
c7cbe308
SL
460{
461 m_browser->SetEditable(m_edit_mode->IsChecked());
462}
463
464
ec4ac0f7
SL
465/**
466 * Callback invoked when there is a request to load a new page (for instance
467 * when the user clicks a link)
468 */
469void WebFrame::OnNavigationRequest(wxWebNavigationEvent& evt)
470{
75b0b0bc
SL
471 if(m_info->IsShown())
472 {
473 m_info->Show(false);
474 }
475
e40741b9 476 wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" +
ec4ac0f7
SL
477 evt.GetTarget() + "')");
478
479 wxASSERT(m_browser->IsBusy());
62e8551f
SL
480
481 //If we don't want to handle navigation then veto the event and navigation
482 //will not take place
483 if(!m_tools_handle_navigation->IsChecked())
484 evt.Veto();
485
ec4ac0f7
SL
486 UpdateState();
487}
488
489/**
490 * Callback invoked when a navigation request was accepted
491 */
492void WebFrame::OnNavigationComplete(wxWebNavigationEvent& evt)
493{
e40741b9 494 wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'");
ec4ac0f7
SL
495 UpdateState();
496}
497
498/**
499 * Callback invoked when a page is finished loading
500 */
501void WebFrame::OnDocumentLoaded(wxWebNavigationEvent& evt)
502{
113e0a92 503 //Only notify if the document is the main frame, not a subframe
e40741b9 504 if(evt.GetURL() == m_browser->GetCurrentURL())
8bccab8f 505 {
e40741b9 506 wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'");
8bccab8f 507 }
ec4ac0f7
SL
508 UpdateState();
509}
510
511/**
512 * On new window, we veto to stop extra windows appearing
513 */
514void WebFrame::OnNewWindow(wxWebNavigationEvent& evt)
515{
e40741b9 516 wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'");
62e8551f
SL
517
518 //If we handle new window events then just load them in this window as we
519 //are a single window browser
520 if(m_tools_handle_new_window->IsChecked())
e40741b9 521 m_browser->LoadUrl(evt.GetURL());
62e8551f 522
ec4ac0f7
SL
523 UpdateState();
524}
525
153530af
SL
526void WebFrame::OnTitleChanged(wxWebNavigationEvent& evt)
527{
528 wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'");
529 UpdateState();
530}
531
ec4ac0f7
SL
532/**
533 * Invoked when user selects the "View Source" menu item
534 */
8bccab8f 535void WebFrame::OnViewSourceRequest(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
536{
537 SourceViewDialog dlg(this, m_browser->GetPageSource());
538 dlg.ShowModal();
539}
540
541/**
542 * Invoked when user selects the "Menu" item
543 */
8bccab8f 544void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
545{
546 if(m_browser->GetCurrentURL() == "")
547 return;
548
549 m_tools_tiny->Check(false);
550 m_tools_small->Check(false);
551 m_tools_medium->Check(false);
552 m_tools_large->Check(false);
553 m_tools_largest->Check(false);
554
555 wxWebViewZoom zoom = m_browser->GetZoom();
556 switch (zoom)
61b98a2d 557 {
ec4ac0f7
SL
558 case wxWEB_VIEW_ZOOM_TINY:
559 m_tools_tiny->Check();
560 break;
561 case wxWEB_VIEW_ZOOM_SMALL:
562 m_tools_small->Check();
563 break;
564 case wxWEB_VIEW_ZOOM_MEDIUM:
565 m_tools_medium->Check();
566 break;
567 case wxWEB_VIEW_ZOOM_LARGE:
568 m_tools_large->Check();
569 break;
570 case wxWEB_VIEW_ZOOM_LARGEST:
571 m_tools_largest->Check();
572 break;
61b98a2d 573 }
4681a3ea
SL
574
575 m_edit_cut->Enable(m_browser->CanCut());
576 m_edit_copy->Enable(m_browser->CanCopy());
577 m_edit_paste->Enable(m_browser->CanPaste());
97e49559
SL
578
579 m_edit_undo->Enable(m_browser->CanUndo());
580 m_edit_redo->Enable(m_browser->CanRedo());
61b98a2d 581
ec4ac0f7
SL
582 wxPoint position = ScreenToClient( wxGetMousePosition() );
583 PopupMenu(m_tools_menu, position.x, position.y);
584}
585
586/**
587 * Invoked when user selects the zoom size in the menu
588 */
589void WebFrame::OnSetZoom(wxCommandEvent& evt)
590{
591 if (evt.GetId() == m_tools_tiny->GetId())
61b98a2d 592 {
ec4ac0f7 593 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
61b98a2d 594 }
ec4ac0f7 595 else if (evt.GetId() == m_tools_small->GetId())
61b98a2d 596 {
ec4ac0f7 597 m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL);
61b98a2d 598 }
ec4ac0f7 599 else if (evt.GetId() == m_tools_medium->GetId())
61b98a2d 600 {
ec4ac0f7 601 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
61b98a2d 602 }
ec4ac0f7 603 else if (evt.GetId() == m_tools_large->GetId())
61b98a2d 604 {
ec4ac0f7 605 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE);
61b98a2d 606 }
ec4ac0f7 607 else if (evt.GetId() == m_tools_largest->GetId())
61b98a2d 608 {
ec4ac0f7 609 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST);
61b98a2d 610 }
ec4ac0f7 611 else
61b98a2d 612 {
ec4ac0f7 613 wxFAIL;
61b98a2d 614 }
ec4ac0f7 615}
853b6cd0 616
8bccab8f 617void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt))
87d482ec
SL
618{
619 if(m_tools_layout->IsChecked())
620 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT);
621 else
622 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT);
623}
624
ec4ac0f7
SL
625/**
626 * Callback invoked when a loading error occurs
627 */
628void WebFrame::OnError(wxWebNavigationEvent& evt)
629{
630 wxString errorCategory;
631 switch (evt.GetInt())
61b98a2d 632 {
ec4ac0f7
SL
633 case wxWEB_NAV_ERR_CONNECTION:
634 errorCategory = "wxWEB_NAV_ERR_CONNECTION";
635 break;
61b98a2d 636
ec4ac0f7
SL
637 case wxWEB_NAV_ERR_CERTIFICATE:
638 errorCategory = "wxWEB_NAV_ERR_CERTIFICATE";
639 break;
61b98a2d 640
ec4ac0f7
SL
641 case wxWEB_NAV_ERR_AUTH:
642 errorCategory = "wxWEB_NAV_ERR_AUTH";
643 break;
61b98a2d 644
ec4ac0f7
SL
645 case wxWEB_NAV_ERR_SECURITY:
646 errorCategory = "wxWEB_NAV_ERR_SECURITY";
647 break;
61b98a2d 648
ec4ac0f7
SL
649 case wxWEB_NAV_ERR_NOT_FOUND:
650 errorCategory = "wxWEB_NAV_ERR_NOT_FOUND";
651 break;
61b98a2d 652
ec4ac0f7
SL
653 case wxWEB_NAV_ERR_REQUEST:
654 errorCategory = "wxWEB_NAV_ERR_REQUEST";
655 break;
656
657 case wxWEB_NAV_ERR_USER_CANCELLED:
658 errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED";
659 break;
660
661 case wxWEB_NAV_ERR_OTHER:
662 errorCategory = "wxWEB_NAV_ERR_OTHER";
663 break;
61b98a2d
SL
664 }
665
e40741b9 666 wxLogMessage("Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")");
61b98a2d 667
ec4ac0f7 668 //Show the info bar with an error
e40741b9 669 m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +
ec4ac0f7 670 "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR);
61b98a2d 671
ec4ac0f7
SL
672 UpdateState();
673}
61b98a2d 674
ec4ac0f7
SL
675/**
676 * Invoked when user selects "Print" from the menu
677 */
8bccab8f 678void WebFrame::OnPrint(wxCommandEvent& WXUNUSED(evt))
61b98a2d 679{
ec4ac0f7
SL
680 m_browser->Print();
681}
61b98a2d 682
ec4ac0f7
SL
683SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
684 wxDialog(parent, wxID_ANY, "Source Code",
685 wxDefaultPosition, wxSize(700,500),
686 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
687{
688 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
689 text->SetMarginWidth(1, 30);
690 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
691 text->SetText(source);
61b98a2d 692
ec4ac0f7
SL
693 text->StyleClearAll();
694 text->SetLexer(wxSTC_LEX_HTML);
695 text->StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(255,0,0));
696 text->StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(255,0,0));
697 text->StyleSetForeground(wxSTC_H_ENTITY, wxColour(255,0,0));
698 text->StyleSetForeground(wxSTC_H_TAG, wxColour(0,150,0));
699 text->StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(0,150,0));
700 text->StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,0,150));
701 text->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(0,0,150));
702 text->StyleSetForeground(wxSTC_H_COMMENT, wxColour(150,150,150));
61b98a2d 703
61b98a2d 704 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
ec4ac0f7
SL
705 sizer->Add(text, 1, wxEXPAND);
706 SetSizer(sizer);
61b98a2d 707}