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