]> git.saurik.com Git - wxWidgets.git/blob - samples/web/web.cpp
6f9955dbe1353bf6acefd079c214844464212911
[wxWidgets.git] / samples / web / web.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: web.cpp
3 // Purpose: wxWebView sample
4 // Author: Marianne Gagnon
5 // Id: $Id$
6 // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #endif
24
25 #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"
33
34 #if !defined(__WXMSW__) && !defined(__WXPM__)
35 #include "../sample.xpm"
36 #endif
37
38 #if wxUSE_STC
39 #include "wx/stc/stc.h"
40 #else
41 #error "wxStyledTextControl is needed by this sample"
42 #endif
43
44 #if defined(__WXMSW__) || defined(__WXOSX__)
45 #include "stop.xpm"
46 #include "refresh.xpm"
47 #endif
48
49 #include "wxlogo.xpm"
50
51 class WebApp : public wxApp
52 {
53 public:
54 virtual bool OnInit();
55 };
56
57 class WebFrame : public wxFrame
58 {
59 public:
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);
69 void OnClearHistory(wxCommandEvent& evt);
70 void OnEnableHistory(wxCommandEvent& evt);
71 void OnNavigationRequest(wxWebNavigationEvent& evt);
72 void OnNavigationComplete(wxWebNavigationEvent& evt);
73 void OnDocumentLoaded(wxWebNavigationEvent& evt);
74 void OnNewWindow(wxWebNavigationEvent& evt);
75 void OnTitleChanged(wxWebNavigationEvent& evt);
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);
81 void OnCut(wxCommandEvent& evt);
82 void OnCopy(wxCommandEvent& evt);
83 void OnPaste(wxCommandEvent& evt);
84 void OnUndo(wxCommandEvent& evt);
85 void OnRedo(wxCommandEvent& evt);
86 void OnMode(wxCommandEvent& evt);
87 void OnZoomLayout(wxCommandEvent& evt);
88
89 private:
90 wxTextCtrl* m_url;
91 wxWebView* m_browser;
92
93 wxToolBar* m_toolbar;
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;
101 wxMenuItem* m_tools_layout;
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;
107 wxMenuItem* m_tools_handle_navigation;
108 wxMenuItem* m_tools_handle_new_window;
109 wxMenuItem* m_tools_enable_history;
110 wxMenuItem* m_edit_cut;
111 wxMenuItem* m_edit_copy;
112 wxMenuItem* m_edit_paste;
113 wxMenuItem* m_edit_undo;
114 wxMenuItem* m_edit_redo;
115 wxMenuItem* m_edit_mode;
116
117 wxTimer* m_timer;
118 int m_animation_angle;
119
120 wxInfoBar *m_info;
121 wxStaticText* m_info_text;
122 };
123
124 class SourceViewDialog : public wxDialog
125 {
126 public:
127 SourceViewDialog(wxWindow* parent, wxString source);
128 };
129
130 IMPLEMENT_APP(WebApp)
131
132 // ============================================================================
133 // implementation
134 // ============================================================================
135
136 bool 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
147 WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample")
148 {
149 //Required from virtual file system archive support
150 wxFileSystem::AddHandler(new wxArchiveFSHandler);
151
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));
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);
183 m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER );
184 m_toolbar->AddControl(m_url, _("URL"));
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
194 m_browser = wxWebView::New(this, wxID_ANY, "http://www.wxwidgets.org");
195 topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
196
197 //We register the file:// protocol for testing purposes
198 m_browser->RegisterHandler(wxSharedPtr<wxWebHandler>(new wxWebFileHandler()));
199
200 SetSizer(topsizer);
201
202 //Set a more sensible size for web browsing
203 SetSize(wxSize(800, 600));
204
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();
213 m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom"));
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"));
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"));
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"));
225
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"));
231 editmenu->AppendSeparator();
232 m_edit_undo = editmenu->Append(wxID_ANY, _("Undo"));
233 m_edit_redo = editmenu->Append(wxID_ANY, _("Redo"));
234 editmenu->AppendSeparator();
235 m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode"));
236
237 m_tools_menu->AppendSeparator();
238 m_tools_menu->AppendSubMenu(editmenu, "Edit");
239
240 //By default we want to handle navigation and new windows
241 m_tools_handle_navigation->Check();
242 m_tools_handle_new_window->Check();
243 m_tools_enable_history->Check();
244 if(!m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT))
245 m_tools_layout->Enable(false);
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);
274 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
275 wxWebNavigationEventHandler(WebFrame::OnTitleChanged), NULL, this);
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 );
282 Connect(m_tools_layout->GetId(), wxEVT_COMMAND_MENU_SELECTED,
283 wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this );
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 );
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 );
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 );
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 );
308 Connect(m_edit_mode->GetId(), wxEVT_COMMAND_MENU_SELECTED,
309 wxCommandEventHandler(WebFrame::OnMode), NULL, this );
310 }
311
312 void WebFrame::OnAnimationTimer(wxTimerEvent& WXUNUSED(evt))
313 {
314 m_animation_angle += 15;
315 if (m_animation_angle > 360) m_animation_angle -= 360;
316
317 wxBitmap image(24, 24);
318 {
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),
329 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 );
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),
339 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 );
340 }
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 */
350 void 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() );
354
355 if (m_browser->IsBusy())
356 {
357 if (m_timer == NULL)
358 {
359 m_timer = new wxTimer(this);
360 this->Connect(wxEVT_TIMER, wxTimerEventHandler(WebFrame::OnAnimationTimer), NULL, this);
361 }
362 m_timer->Start(100); // start animation timer
363
364 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
365 }
366 else
367 {
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 );
371 }
372
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 */
380 void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt))
381 {
382 m_browser->LoadUrl( m_url->GetValue() );
383 UpdateState();
384 }
385
386 /**
387 * Callback invoked when user pressed the "back" button
388 */
389 void WebFrame::OnBack(wxCommandEvent& WXUNUSED(evt))
390 {
391 m_browser->GoBack();
392 UpdateState();
393 }
394
395 /**
396 * Callback invoked when user pressed the "forward" button
397 */
398 void WebFrame::OnForward(wxCommandEvent& WXUNUSED(evt))
399 {
400 m_browser->GoForward();
401 UpdateState();
402 }
403
404 /**
405 * Callback invoked when user pressed the "stop" button
406 */
407 void WebFrame::OnStop(wxCommandEvent& WXUNUSED(evt))
408 {
409 m_browser->Stop();
410 UpdateState();
411 }
412
413 /**
414 * Callback invoked when user pressed the "reload" button
415 */
416 void WebFrame::OnReload(wxCommandEvent& WXUNUSED(evt))
417 {
418 m_browser->Reload();
419 UpdateState();
420 }
421
422 void WebFrame::OnClearHistory(wxCommandEvent& WXUNUSED(evt))
423 {
424 m_browser->ClearHistory();
425 UpdateState();
426 }
427
428 void WebFrame::OnEnableHistory(wxCommandEvent& WXUNUSED(evt))
429 {
430 m_browser->EnableHistory(m_tools_enable_history->IsChecked());
431 UpdateState();
432 }
433
434 void WebFrame::OnCut(wxCommandEvent& WXUNUSED(evt))
435 {
436 m_browser->Cut();
437 }
438
439 void WebFrame::OnCopy(wxCommandEvent& WXUNUSED(evt))
440 {
441 m_browser->Copy();
442 }
443
444 void WebFrame::OnPaste(wxCommandEvent& WXUNUSED(evt))
445 {
446 m_browser->Paste();
447 }
448
449 void WebFrame::OnUndo(wxCommandEvent& WXUNUSED(evt))
450 {
451 m_browser->Undo();
452 }
453
454 void WebFrame::OnRedo(wxCommandEvent& WXUNUSED(evt))
455 {
456 m_browser->Redo();
457 }
458
459 void WebFrame::OnMode(wxCommandEvent& WXUNUSED(evt))
460 {
461 m_browser->SetEditable(m_edit_mode->IsChecked());
462 }
463
464
465 /**
466 * Callback invoked when there is a request to load a new page (for instance
467 * when the user clicks a link)
468 */
469 void WebFrame::OnNavigationRequest(wxWebNavigationEvent& evt)
470 {
471 if(m_info->IsShown())
472 {
473 m_info->Dismiss();
474 }
475
476 wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" +
477 evt.GetTarget() + "')");
478
479 wxASSERT(m_browser->IsBusy());
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
486 UpdateState();
487 }
488
489 /**
490 * Callback invoked when a navigation request was accepted
491 */
492 void WebFrame::OnNavigationComplete(wxWebNavigationEvent& evt)
493 {
494 wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'");
495 UpdateState();
496 }
497
498 /**
499 * Callback invoked when a page is finished loading
500 */
501 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent& evt)
502 {
503 //Only notify if the document is the main frame, not a subframe
504 if(evt.GetURL() == m_browser->GetCurrentURL())
505 {
506 wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'");
507 }
508 UpdateState();
509 }
510
511 /**
512 * On new window, we veto to stop extra windows appearing
513 */
514 void WebFrame::OnNewWindow(wxWebNavigationEvent& evt)
515 {
516 wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'");
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())
521 m_browser->LoadUrl(evt.GetURL());
522
523 UpdateState();
524 }
525
526 void WebFrame::OnTitleChanged(wxWebNavigationEvent& evt)
527 {
528 wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'");
529 UpdateState();
530 }
531
532 /**
533 * Invoked when user selects the "View Source" menu item
534 */
535 void WebFrame::OnViewSourceRequest(wxCommandEvent& WXUNUSED(evt))
536 {
537 SourceViewDialog dlg(this, m_browser->GetPageSource());
538 dlg.ShowModal();
539 }
540
541 /**
542 * Invoked when user selects the "Menu" item
543 */
544 void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
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)
557 {
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;
573 }
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());
578
579 m_edit_undo->Enable(m_browser->CanUndo());
580 m_edit_redo->Enable(m_browser->CanRedo());
581
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 */
589 void WebFrame::OnSetZoom(wxCommandEvent& evt)
590 {
591 if (evt.GetId() == m_tools_tiny->GetId())
592 {
593 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
594 }
595 else if (evt.GetId() == m_tools_small->GetId())
596 {
597 m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL);
598 }
599 else if (evt.GetId() == m_tools_medium->GetId())
600 {
601 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
602 }
603 else if (evt.GetId() == m_tools_large->GetId())
604 {
605 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE);
606 }
607 else if (evt.GetId() == m_tools_largest->GetId())
608 {
609 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST);
610 }
611 else
612 {
613 wxFAIL;
614 }
615 }
616
617 void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt))
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
625 /**
626 * Callback invoked when a loading error occurs
627 */
628 void WebFrame::OnError(wxWebNavigationEvent& evt)
629 {
630 wxString errorCategory;
631 switch (evt.GetInt())
632 {
633 case wxWEB_NAV_ERR_CONNECTION:
634 errorCategory = "wxWEB_NAV_ERR_CONNECTION";
635 break;
636
637 case wxWEB_NAV_ERR_CERTIFICATE:
638 errorCategory = "wxWEB_NAV_ERR_CERTIFICATE";
639 break;
640
641 case wxWEB_NAV_ERR_AUTH:
642 errorCategory = "wxWEB_NAV_ERR_AUTH";
643 break;
644
645 case wxWEB_NAV_ERR_SECURITY:
646 errorCategory = "wxWEB_NAV_ERR_SECURITY";
647 break;
648
649 case wxWEB_NAV_ERR_NOT_FOUND:
650 errorCategory = "wxWEB_NAV_ERR_NOT_FOUND";
651 break;
652
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;
664 }
665
666 wxLogMessage("Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")");
667
668 //Show the info bar with an error
669 m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +
670 "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR);
671
672 UpdateState();
673 }
674
675 /**
676 * Invoked when user selects "Print" from the menu
677 */
678 void WebFrame::OnPrint(wxCommandEvent& WXUNUSED(evt))
679 {
680 m_browser->Print();
681 }
682
683 SourceViewDialog::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);
692
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));
703
704 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
705 sizer->Add(text, 1, wxEXPAND);
706 SetSizer(sizer);
707 }