1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWebView sample
4 // Author: Marianne Gagnon
6 // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
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"
34 #if !defined(__WXMSW__) && !defined(__WXPM__)
35 #include "../sample.xpm"
39 #include "wx/stc/stc.h"
41 #error "wxStyledTextControl is needed by this sample"
44 #if defined(__WXMSW__) || defined(__WXOSX__)
46 #include "refresh.xpm"
52 //We map menu items to their history items
53 WX_DECLARE_HASH_MAP(int, wxSharedPtr
<wxWebHistoryItem
>,
54 wxIntegerHash
, wxIntegerEqual
, wxMenuHistoryMap
);
56 class WebApp
: public wxApp
59 virtual bool OnInit();
62 class WebFrame
: public wxFrame
67 void OnAnimationTimer(wxTimerEvent
& evt
);
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
);
101 wxWebView
* m_browser
;
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
;
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
;
131 int m_animation_angle
;
134 wxStaticText
* m_info_text
;
136 wxMenuHistoryMap m_histMenuItems
;
139 class SourceViewDialog
: public wxDialog
142 SourceViewDialog(wxWindow
* parent
, wxString source
);
145 IMPLEMENT_APP(WebApp
)
147 // ============================================================================
149 // ============================================================================
151 bool WebApp::OnInit()
153 if ( !wxApp::OnInit() )
156 WebFrame
*frame
= new WebFrame();
162 WebFrame::WebFrame() : wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
164 //Required from virtual file system archive support
165 wxFileSystem::AddHandler(new wxArchiveFSHandler
);
167 // set the frame icon
168 SetIcon(wxICON(sample
));
169 SetTitle("wxWebView Sample");
172 m_animation_angle
= 0;
175 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
177 // Create the toolbar
178 m_toolbar
= CreateToolBar(wxTB_TEXT
);
179 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
181 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
182 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
184 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
186 wxBitmap stop
= wxBitmap(stop_xpm
);
189 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
191 wxBitmap refresh
= wxBitmap(refresh_xpm
);
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
));
202 m_toolbar
->Realize();
204 // Create the info panel
205 m_info
= new wxInfoBar(this);
206 topsizer
->Add(m_info
, wxSizerFlags().Expand());
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));
212 //We register the file:// protocol for testing purposes
213 m_browser
->RegisterHandler(wxSharedPtr
<wxWebHandler
>(new wxWebFileHandler()));
217 //Set a more sensible size for web browsing
218 SetSize(wxSize(800, 600));
220 // Create a log window
221 new wxLogWindow(this, _("Logging"));
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();
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();
245 m_tools_menu
->AppendSubMenu(m_tools_history_menu
, "History");
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"));
258 m_tools_menu
->AppendSeparator();
259 m_tools_menu
->AppendSubMenu(editmenu
, "Edit");
261 wxMenuItem
* script
= m_tools_menu
->Append(wxID_ANY
, _("Run Script"));
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"));
269 editmenu
->AppendSubMenu(selection
, "Selection");
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);
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 );
291 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
292 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
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);
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 );
351 void WebFrame::OnAnimationTimer(wxTimerEvent
& WXUNUSED(evt
))
353 m_animation_angle
+= 15;
354 if (m_animation_angle
> 360) m_animation_angle
-= 360;
356 wxBitmap
image(24, 24);
359 dc
.SelectObject(image
);
360 dc
.SetBackground(wxBrush(wxColour(255,0,255)));
363 if (m_animation_angle
>= 0 && m_animation_angle
<= 180)
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 );
371 dc
.DrawBitmap(wxBitmap(wxlogo_xpm
), 0, 0, true);
373 if (m_animation_angle
> 180)
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 );
381 image
.SetMask(new wxMask(image
, wxColour(255,0,255)));
382 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), image
);
386 * Method that retrieves the current state from the web control and updates the GUI
387 * the reflect this current state.
389 void WebFrame::UpdateState()
391 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
392 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
394 if (m_browser
->IsBusy())
398 m_timer
= new wxTimer(this);
399 this->Connect(wxEVT_TIMER
, wxTimerEventHandler(WebFrame::OnAnimationTimer
), NULL
, this);
401 m_timer
->Start(100); // start animation timer
403 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
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 );
412 SetTitle( m_browser
->GetCurrentTitle() );
413 m_url
->SetValue( m_browser
->GetCurrentURL() );
417 * Callback invoked when user entered an URL and pressed enter
419 void WebFrame::OnUrl(wxCommandEvent
& WXUNUSED(evt
))
421 m_browser
->LoadUrl( m_url
->GetValue() );
426 * Callback invoked when user pressed the "back" button
428 void WebFrame::OnBack(wxCommandEvent
& WXUNUSED(evt
))
435 * Callback invoked when user pressed the "forward" button
437 void WebFrame::OnForward(wxCommandEvent
& WXUNUSED(evt
))
439 m_browser
->GoForward();
444 * Callback invoked when user pressed the "stop" button
446 void WebFrame::OnStop(wxCommandEvent
& WXUNUSED(evt
))
453 * Callback invoked when user pressed the "reload" button
455 void WebFrame::OnReload(wxCommandEvent
& WXUNUSED(evt
))
461 void WebFrame::OnClearHistory(wxCommandEvent
& WXUNUSED(evt
))
463 m_browser
->ClearHistory();
467 void WebFrame::OnEnableHistory(wxCommandEvent
& WXUNUSED(evt
))
469 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
473 void WebFrame::OnCut(wxCommandEvent
& WXUNUSED(evt
))
478 void WebFrame::OnCopy(wxCommandEvent
& WXUNUSED(evt
))
483 void WebFrame::OnPaste(wxCommandEvent
& WXUNUSED(evt
))
488 void WebFrame::OnUndo(wxCommandEvent
& WXUNUSED(evt
))
493 void WebFrame::OnRedo(wxCommandEvent
& WXUNUSED(evt
))
498 void WebFrame::OnMode(wxCommandEvent
& WXUNUSED(evt
))
500 m_browser
->SetEditable(m_edit_mode
->IsChecked());
505 * Callback invoked when there is a request to load a new page (for instance
506 * when the user clicks a link)
508 void WebFrame::OnNavigationRequest(wxWebNavigationEvent
& evt
)
510 if(m_info
->IsShown())
515 wxLogMessage("%s", "Navigation request to '" + evt
.GetURL() + "' (target='" +
516 evt
.GetTarget() + "')");
518 wxASSERT(m_browser
->IsBusy());
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())
529 * Callback invoked when a navigation request was accepted
531 void WebFrame::OnNavigationComplete(wxWebNavigationEvent
& evt
)
533 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetURL() + "'");
538 * Callback invoked when a page is finished loading
540 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent
& evt
)
542 //Only notify if the document is the main frame, not a subframe
543 if(evt
.GetURL() == m_browser
->GetCurrentURL())
545 wxLogMessage("%s", "Document loaded; url='" + evt
.GetURL() + "'");
551 * On new window, we veto to stop extra windows appearing
553 void WebFrame::OnNewWindow(wxWebNavigationEvent
& evt
)
555 wxLogMessage("%s", "New window; url='" + evt
.GetURL() + "'");
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());
565 void WebFrame::OnTitleChanged(wxWebNavigationEvent
& evt
)
567 wxLogMessage("%s", "Title changed; title='" + evt
.GetString() + "'");
572 * Invoked when user selects the "View Source" menu item
574 void WebFrame::OnViewSourceRequest(wxCommandEvent
& WXUNUSED(evt
))
576 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
581 * Invoked when user selects the "Menu" item
583 void WebFrame::OnToolsClicked(wxCommandEvent
& WXUNUSED(evt
))
585 if(m_browser
->GetCurrentURL() == "")
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);
594 wxWebViewZoom zoom
= m_browser
->GetZoom();
597 case wxWEB_VIEW_ZOOM_TINY
:
598 m_tools_tiny
->Check();
600 case wxWEB_VIEW_ZOOM_SMALL
:
601 m_tools_small
->Check();
603 case wxWEB_VIEW_ZOOM_MEDIUM
:
604 m_tools_medium
->Check();
606 case wxWEB_VIEW_ZOOM_LARGE
:
607 m_tools_large
->Check();
609 case wxWEB_VIEW_ZOOM_LARGEST
:
610 m_tools_largest
->Check();
614 m_edit_cut
->Enable(m_browser
->CanCut());
615 m_edit_copy
->Enable(m_browser
->CanCopy());
616 m_edit_paste
->Enable(m_browser
->CanPaste());
618 m_edit_undo
->Enable(m_browser
->CanUndo());
619 m_edit_redo
->Enable(m_browser
->CanRedo());
621 m_selection_clear
->Enable(m_browser
->HasSelection());
622 m_selection_delete
->Enable(m_browser
->HasSelection());
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
)
628 m_tools_history_menu
->Destroy(it
->first
);
630 m_histMenuItems
.clear();
632 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > back
= m_browser
->GetBackwardHistory();
633 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > forward
= m_browser
->GetForwardHistory();
637 for(unsigned int i
= 0; i
< back
.size(); i
++)
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 );
645 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, m_browser
->GetCurrentTitle());
648 //No need to connect the current item
649 m_histMenuItems
[item
->GetId()] = wxSharedPtr
<wxWebHistoryItem
>(new wxWebHistoryItem(m_browser
->GetCurrentURL(), m_browser
->GetCurrentTitle()));
651 for(unsigned int i
= 0; i
< forward
.size(); i
++)
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 );
659 wxPoint position
= ScreenToClient( wxGetMousePosition() );
660 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
664 * Invoked when user selects the zoom size in the menu
666 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
668 if (evt
.GetId() == m_tools_tiny
->GetId())
670 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
672 else if (evt
.GetId() == m_tools_small
->GetId())
674 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
676 else if (evt
.GetId() == m_tools_medium
->GetId())
678 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
680 else if (evt
.GetId() == m_tools_large
->GetId())
682 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
684 else if (evt
.GetId() == m_tools_largest
->GetId())
686 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
694 void WebFrame::OnZoomLayout(wxCommandEvent
& WXUNUSED(evt
))
696 if(m_tools_layout
->IsChecked())
697 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
699 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
702 void WebFrame::OnHistory(wxCommandEvent
& evt
)
704 m_browser
->LoadHistoryItem(m_histMenuItems
[evt
.GetId()]);
707 void WebFrame::OnRunScript(wxCommandEvent
& WXUNUSED(evt
))
709 wxTextEntryDialog
dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr
, "", wxOK
|wxCANCEL
|wxCENTRE
|wxTE_MULTILINE
);
710 if(dialog
.ShowModal() == wxID_OK
)
712 m_browser
->RunScript(dialog
.GetValue());
716 void WebFrame::OnClearSelection(wxCommandEvent
& WXUNUSED(evt
))
718 m_browser
->ClearSelection();
721 void WebFrame::OnDeleteSelection(wxCommandEvent
& WXUNUSED(evt
))
723 m_browser
->DeleteSelection();
726 void WebFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(evt
))
728 m_browser
->SelectAll();
732 * Callback invoked when a loading error occurs
734 void WebFrame::OnError(wxWebNavigationEvent
& evt
)
736 wxString errorCategory
;
737 switch (evt
.GetInt())
739 case wxWEB_NAV_ERR_CONNECTION
:
740 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
743 case wxWEB_NAV_ERR_CERTIFICATE
:
744 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
747 case wxWEB_NAV_ERR_AUTH
:
748 errorCategory
= "wxWEB_NAV_ERR_AUTH";
751 case wxWEB_NAV_ERR_SECURITY
:
752 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
755 case wxWEB_NAV_ERR_NOT_FOUND
:
756 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
759 case wxWEB_NAV_ERR_REQUEST
:
760 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
763 case wxWEB_NAV_ERR_USER_CANCELLED
:
764 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
767 case wxWEB_NAV_ERR_OTHER
:
768 errorCategory
= "wxWEB_NAV_ERR_OTHER";
772 wxLogMessage("Error; url='" + evt
.GetURL() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
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
);
782 * Invoked when user selects "Print" from the menu
784 void WebFrame::OnPrint(wxCommandEvent
& WXUNUSED(evt
))
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
)
794 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
795 text
->SetMarginWidth(1, 30);
796 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
797 text
->SetText(source
);
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));
810 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
811 sizer
->Add(text
, 1, wxEXPAND
);