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
);
100 wxToolBar
* m_toolbar
;
101 wxToolBarToolBase
* m_toolbar_back
;
102 wxToolBarToolBase
* m_toolbar_forward
;
103 wxToolBarToolBase
* m_toolbar_stop
;
104 wxToolBarToolBase
* m_toolbar_reload
;
105 wxToolBarToolBase
* m_toolbar_tools
;
107 wxMenu
* m_tools_menu
;
108 wxMenu
* m_tools_history_menu
;
109 wxMenuItem
* m_tools_layout
;
110 wxMenuItem
* m_tools_tiny
;
111 wxMenuItem
* m_tools_small
;
112 wxMenuItem
* m_tools_medium
;
113 wxMenuItem
* m_tools_large
;
114 wxMenuItem
* m_tools_largest
;
115 wxMenuItem
* m_tools_handle_navigation
;
116 wxMenuItem
* m_tools_handle_new_window
;
117 wxMenuItem
* m_tools_enable_history
;
118 wxMenuItem
* m_edit_cut
;
119 wxMenuItem
* m_edit_copy
;
120 wxMenuItem
* m_edit_paste
;
121 wxMenuItem
* m_edit_undo
;
122 wxMenuItem
* m_edit_redo
;
123 wxMenuItem
* m_edit_mode
;
126 int m_animation_angle
;
129 wxStaticText
* m_info_text
;
131 wxMenuHistoryMap m_histMenuItems
;
134 class SourceViewDialog
: public wxDialog
137 SourceViewDialog(wxWindow
* parent
, wxString source
);
140 IMPLEMENT_APP(WebApp
)
142 // ============================================================================
144 // ============================================================================
146 bool WebApp::OnInit()
148 if ( !wxApp::OnInit() )
151 WebFrame
*frame
= new WebFrame();
157 WebFrame::WebFrame() : wxFrame(NULL
, wxID_ANY
, "wxWebView Sample")
159 //Required from virtual file system archive support
160 wxFileSystem::AddHandler(new wxArchiveFSHandler
);
162 // set the frame icon
163 SetIcon(wxICON(sample
));
164 SetTitle("wxWebView Sample");
167 m_animation_angle
= 0;
170 wxBoxSizer
* topsizer
= new wxBoxSizer(wxVERTICAL
);
172 // Create the toolbar
173 m_toolbar
= CreateToolBar(wxTB_TEXT
);
174 m_toolbar
->SetToolBitmapSize(wxSize(32, 32));
176 wxBitmap back
= wxArtProvider::GetBitmap(wxART_GO_BACK
, wxART_TOOLBAR
);
177 wxBitmap forward
= wxArtProvider::GetBitmap(wxART_GO_FORWARD
, wxART_TOOLBAR
);
179 wxBitmap stop
= wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR
);
181 wxBitmap stop
= wxBitmap(stop_xpm
);
184 wxBitmap refresh
= wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR
);
186 wxBitmap refresh
= wxBitmap(refresh_xpm
);
189 m_toolbar_back
= m_toolbar
->AddTool(wxID_ANY
, _("Back"), back
);
190 m_toolbar_forward
= m_toolbar
->AddTool(wxID_ANY
, _("Forward"), forward
);
191 m_toolbar_stop
= m_toolbar
->AddTool(wxID_ANY
, _("Stop"), stop
);
192 m_toolbar_reload
= m_toolbar
->AddTool(wxID_ANY
, _("Reload"), refresh
);
193 m_url
= new wxTextCtrl(m_toolbar
, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1), wxTE_PROCESS_ENTER
);
194 m_toolbar
->AddControl(m_url
, _("URL"));
195 m_toolbar_tools
= m_toolbar
->AddTool(wxID_ANY
, _("Menu"), wxBitmap(wxlogo_xpm
));
197 m_toolbar
->Realize();
199 // Create the info panel
200 m_info
= new wxInfoBar(this);
201 topsizer
->Add(m_info
, wxSizerFlags().Expand());
203 // Create the webview
204 m_browser
= wxWebView::New(this, wxID_ANY
, "http://www.wxwidgets.org");
205 topsizer
->Add(m_browser
, wxSizerFlags().Expand().Proportion(1));
207 //We register the file:// protocol for testing purposes
208 m_browser
->RegisterHandler(wxSharedPtr
<wxWebHandler
>(new wxWebFileHandler()));
212 //Set a more sensible size for web browsing
213 SetSize(wxSize(800, 600));
215 // Create a log window
216 new wxLogWindow(this, _("Logging"));
218 // Create the Tools menu
219 m_tools_menu
= new wxMenu();
220 wxMenuItem
* print
= m_tools_menu
->Append(wxID_ANY
, _("Print"));
221 wxMenuItem
* viewSource
= m_tools_menu
->Append(wxID_ANY
, _("View Source"));
222 m_tools_menu
->AppendSeparator();
223 m_tools_layout
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Use Layout Zoom"));
224 m_tools_tiny
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Tiny"));
225 m_tools_small
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Small"));
226 m_tools_medium
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Medium"));
227 m_tools_large
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Large"));
228 m_tools_largest
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Largest"));
229 m_tools_menu
->AppendSeparator();
230 m_tools_handle_navigation
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle Navigation"));
231 m_tools_handle_new_window
= m_tools_menu
->AppendCheckItem(wxID_ANY
, _("Handle New Windows"));
232 m_tools_menu
->AppendSeparator();
235 m_tools_history_menu
= new wxMenu();
236 wxMenuItem
* clearhist
= m_tools_history_menu
->Append(wxID_ANY
, _("Clear History"));
237 m_tools_enable_history
= m_tools_history_menu
->AppendCheckItem(wxID_ANY
, _("Enable History"));
238 m_tools_history_menu
->AppendSeparator();
240 m_tools_menu
->AppendSubMenu(m_tools_history_menu
, "History");
242 //Create an editing menu
243 wxMenu
* editmenu
= new wxMenu();
244 m_edit_cut
= editmenu
->Append(wxID_ANY
, _("Cut"));
245 m_edit_copy
= editmenu
->Append(wxID_ANY
, _("Copy"));
246 m_edit_paste
= editmenu
->Append(wxID_ANY
, _("Paste"));
247 editmenu
->AppendSeparator();
248 m_edit_undo
= editmenu
->Append(wxID_ANY
, _("Undo"));
249 m_edit_redo
= editmenu
->Append(wxID_ANY
, _("Redo"));
250 editmenu
->AppendSeparator();
251 m_edit_mode
= editmenu
->AppendCheckItem(wxID_ANY
, _("Edit Mode"));
253 m_tools_menu
->AppendSeparator();
254 m_tools_menu
->AppendSubMenu(editmenu
, "Edit");
256 wxMenuItem
* script
= m_tools_menu
->Append(wxID_ANY
, _("Run Script"));
258 //By default we want to handle navigation and new windows
259 m_tools_handle_navigation
->Check();
260 m_tools_handle_new_window
->Check();
261 m_tools_enable_history
->Check();
262 if(!m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
))
263 m_tools_layout
->Enable(false);
266 // Connect the toolbar events
267 Connect(m_toolbar_back
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
268 wxCommandEventHandler(WebFrame::OnBack
), NULL
, this );
269 Connect(m_toolbar_forward
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
270 wxCommandEventHandler(WebFrame::OnForward
), NULL
, this );
271 Connect(m_toolbar_stop
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
272 wxCommandEventHandler(WebFrame::OnStop
), NULL
, this );
273 Connect(m_toolbar_reload
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
274 wxCommandEventHandler(WebFrame::OnReload
),NULL
, this );
275 Connect(m_toolbar_tools
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
276 wxCommandEventHandler(WebFrame::OnToolsClicked
), NULL
, this );
278 Connect(m_url
->GetId(), wxEVT_COMMAND_TEXT_ENTER
,
279 wxCommandEventHandler(WebFrame::OnUrl
), NULL
, this );
281 // Connect the webview events
282 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
283 wxWebNavigationEventHandler(WebFrame::OnNavigationRequest
), NULL
, this);
284 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
285 wxWebNavigationEventHandler(WebFrame::OnNavigationComplete
), NULL
, this);
286 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED
,
287 wxWebNavigationEventHandler(WebFrame::OnDocumentLoaded
), NULL
, this);
288 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR
,
289 wxWebNavigationEventHandler(WebFrame::OnError
), NULL
, this);
290 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
291 wxWebNavigationEventHandler(WebFrame::OnNewWindow
), NULL
, this);
292 Connect(m_browser
->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
293 wxWebNavigationEventHandler(WebFrame::OnTitleChanged
), NULL
, this);
295 // Connect the menu events
296 Connect(viewSource
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
297 wxCommandEventHandler(WebFrame::OnViewSourceRequest
), NULL
, this );
298 Connect(print
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
299 wxCommandEventHandler(WebFrame::OnPrint
), NULL
, this );
300 Connect(m_tools_layout
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
301 wxCommandEventHandler(WebFrame::OnZoomLayout
), NULL
, this );
302 Connect(m_tools_tiny
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
303 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
304 Connect(m_tools_small
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
305 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
306 Connect(m_tools_medium
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
307 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
308 Connect(m_tools_large
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
309 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
310 Connect(m_tools_largest
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
311 wxCommandEventHandler(WebFrame::OnSetZoom
), NULL
, this );
312 Connect(clearhist
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
313 wxCommandEventHandler(WebFrame::OnClearHistory
), NULL
, this );
314 Connect(m_tools_enable_history
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
315 wxCommandEventHandler(WebFrame::OnEnableHistory
), NULL
, this );
316 Connect(m_edit_cut
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
317 wxCommandEventHandler(WebFrame::OnCut
), NULL
, this );
318 Connect(m_edit_copy
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
319 wxCommandEventHandler(WebFrame::OnCopy
), NULL
, this );
320 Connect(m_edit_paste
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
321 wxCommandEventHandler(WebFrame::OnPaste
), NULL
, this );
322 Connect(m_edit_undo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
323 wxCommandEventHandler(WebFrame::OnUndo
), NULL
, this );
324 Connect(m_edit_redo
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
325 wxCommandEventHandler(WebFrame::OnRedo
), NULL
, this );
326 Connect(m_edit_mode
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
327 wxCommandEventHandler(WebFrame::OnMode
), NULL
, this );
328 Connect(script
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
329 wxCommandEventHandler(WebFrame::OnRunScript
), NULL
, this );
332 void WebFrame::OnAnimationTimer(wxTimerEvent
& WXUNUSED(evt
))
334 m_animation_angle
+= 15;
335 if (m_animation_angle
> 360) m_animation_angle
-= 360;
337 wxBitmap
image(24, 24);
340 dc
.SelectObject(image
);
341 dc
.SetBackground(wxBrush(wxColour(255,0,255)));
344 if (m_animation_angle
>= 0 && m_animation_angle
<= 180)
346 dc
.SetBrush(*wxYELLOW_BRUSH
);
347 dc
.SetPen(*wxYELLOW_PEN
);
348 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
349 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
352 dc
.DrawBitmap(wxBitmap(wxlogo_xpm
), 0, 0, true);
354 if (m_animation_angle
> 180)
356 dc
.SetBrush(*wxYELLOW_BRUSH
);
357 dc
.SetPen(*wxYELLOW_PEN
);
358 dc
.DrawCircle(16 - int(sin(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
),
359 16 + int(cos(m_animation_angle
*0.01745f
/* convert to radians */)*14.0f
), 3 );
362 image
.SetMask(new wxMask(image
, wxColour(255,0,255)));
363 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), image
);
367 * Method that retrieves the current state from the web control and updates the GUI
368 * the reflect this current state.
370 void WebFrame::UpdateState()
372 m_toolbar
->EnableTool( m_toolbar_back
->GetId(), m_browser
->CanGoBack() );
373 m_toolbar
->EnableTool( m_toolbar_forward
->GetId(), m_browser
->CanGoForward() );
375 if (m_browser
->IsBusy())
379 m_timer
= new wxTimer(this);
380 this->Connect(wxEVT_TIMER
, wxTimerEventHandler(WebFrame::OnAnimationTimer
), NULL
, this);
382 m_timer
->Start(100); // start animation timer
384 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), true );
388 if (m_timer
!= NULL
) m_timer
->Stop(); // stop animation timer
389 m_toolbar
->SetToolNormalBitmap(m_toolbar_tools
->GetId(), wxBitmap(wxlogo_xpm
));
390 m_toolbar
->EnableTool( m_toolbar_stop
->GetId(), false );
393 SetTitle( m_browser
->GetCurrentTitle() );
394 m_url
->SetValue( m_browser
->GetCurrentURL() );
398 * Callback invoked when user entered an URL and pressed enter
400 void WebFrame::OnUrl(wxCommandEvent
& WXUNUSED(evt
))
402 m_browser
->LoadUrl( m_url
->GetValue() );
407 * Callback invoked when user pressed the "back" button
409 void WebFrame::OnBack(wxCommandEvent
& WXUNUSED(evt
))
416 * Callback invoked when user pressed the "forward" button
418 void WebFrame::OnForward(wxCommandEvent
& WXUNUSED(evt
))
420 m_browser
->GoForward();
425 * Callback invoked when user pressed the "stop" button
427 void WebFrame::OnStop(wxCommandEvent
& WXUNUSED(evt
))
434 * Callback invoked when user pressed the "reload" button
436 void WebFrame::OnReload(wxCommandEvent
& WXUNUSED(evt
))
442 void WebFrame::OnClearHistory(wxCommandEvent
& WXUNUSED(evt
))
444 m_browser
->ClearHistory();
448 void WebFrame::OnEnableHistory(wxCommandEvent
& WXUNUSED(evt
))
450 m_browser
->EnableHistory(m_tools_enable_history
->IsChecked());
454 void WebFrame::OnCut(wxCommandEvent
& WXUNUSED(evt
))
459 void WebFrame::OnCopy(wxCommandEvent
& WXUNUSED(evt
))
464 void WebFrame::OnPaste(wxCommandEvent
& WXUNUSED(evt
))
469 void WebFrame::OnUndo(wxCommandEvent
& WXUNUSED(evt
))
474 void WebFrame::OnRedo(wxCommandEvent
& WXUNUSED(evt
))
479 void WebFrame::OnMode(wxCommandEvent
& WXUNUSED(evt
))
481 m_browser
->SetEditable(m_edit_mode
->IsChecked());
486 * Callback invoked when there is a request to load a new page (for instance
487 * when the user clicks a link)
489 void WebFrame::OnNavigationRequest(wxWebNavigationEvent
& evt
)
491 if(m_info
->IsShown())
496 wxLogMessage("%s", "Navigation request to '" + evt
.GetURL() + "' (target='" +
497 evt
.GetTarget() + "')");
499 wxASSERT(m_browser
->IsBusy());
501 //If we don't want to handle navigation then veto the event and navigation
502 //will not take place
503 if(!m_tools_handle_navigation
->IsChecked())
510 * Callback invoked when a navigation request was accepted
512 void WebFrame::OnNavigationComplete(wxWebNavigationEvent
& evt
)
514 wxLogMessage("%s", "Navigation complete; url='" + evt
.GetURL() + "'");
519 * Callback invoked when a page is finished loading
521 void WebFrame::OnDocumentLoaded(wxWebNavigationEvent
& evt
)
523 //Only notify if the document is the main frame, not a subframe
524 if(evt
.GetURL() == m_browser
->GetCurrentURL())
526 wxLogMessage("%s", "Document loaded; url='" + evt
.GetURL() + "'");
532 * On new window, we veto to stop extra windows appearing
534 void WebFrame::OnNewWindow(wxWebNavigationEvent
& evt
)
536 wxLogMessage("%s", "New window; url='" + evt
.GetURL() + "'");
538 //If we handle new window events then just load them in this window as we
539 //are a single window browser
540 if(m_tools_handle_new_window
->IsChecked())
541 m_browser
->LoadUrl(evt
.GetURL());
546 void WebFrame::OnTitleChanged(wxWebNavigationEvent
& evt
)
548 wxLogMessage("%s", "Title changed; title='" + evt
.GetString() + "'");
553 * Invoked when user selects the "View Source" menu item
555 void WebFrame::OnViewSourceRequest(wxCommandEvent
& WXUNUSED(evt
))
557 SourceViewDialog
dlg(this, m_browser
->GetPageSource());
562 * Invoked when user selects the "Menu" item
564 void WebFrame::OnToolsClicked(wxCommandEvent
& WXUNUSED(evt
))
566 if(m_browser
->GetCurrentURL() == "")
569 m_tools_tiny
->Check(false);
570 m_tools_small
->Check(false);
571 m_tools_medium
->Check(false);
572 m_tools_large
->Check(false);
573 m_tools_largest
->Check(false);
575 wxWebViewZoom zoom
= m_browser
->GetZoom();
578 case wxWEB_VIEW_ZOOM_TINY
:
579 m_tools_tiny
->Check();
581 case wxWEB_VIEW_ZOOM_SMALL
:
582 m_tools_small
->Check();
584 case wxWEB_VIEW_ZOOM_MEDIUM
:
585 m_tools_medium
->Check();
587 case wxWEB_VIEW_ZOOM_LARGE
:
588 m_tools_large
->Check();
590 case wxWEB_VIEW_ZOOM_LARGEST
:
591 m_tools_largest
->Check();
595 m_edit_cut
->Enable(m_browser
->CanCut());
596 m_edit_copy
->Enable(m_browser
->CanCopy());
597 m_edit_paste
->Enable(m_browser
->CanPaste());
599 m_edit_undo
->Enable(m_browser
->CanUndo());
600 m_edit_redo
->Enable(m_browser
->CanRedo());
602 //Firstly we clear the existing menu items, then we add the current ones
603 wxMenuHistoryMap::const_iterator it
;
604 for( it
= m_histMenuItems
.begin(); it
!= m_histMenuItems
.end(); ++it
)
606 m_tools_history_menu
->Destroy(it
->first
);
608 m_histMenuItems
.clear();
610 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > back
= m_browser
->GetBackwardHistory();
611 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > forward
= m_browser
->GetForwardHistory();
615 for(unsigned int i
= 0; i
< back
.size(); i
++)
617 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, back
[i
]->GetTitle());
618 m_histMenuItems
[item
->GetId()] = back
[i
];
619 Connect(item
->GetId(), wxEVT_COMMAND_MENU_SELECTED
,
620 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
623 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, m_browser
->GetCurrentTitle());
626 //No need to connect the current item
627 m_histMenuItems
[item
->GetId()] = wxSharedPtr
<wxWebHistoryItem
>(new wxWebHistoryItem(m_browser
->GetCurrentURL(), m_browser
->GetCurrentTitle()));
629 for(unsigned int i
= 0; i
< forward
.size(); i
++)
631 item
= m_tools_history_menu
->AppendRadioItem(wxID_ANY
, forward
[i
]->GetTitle());
632 m_histMenuItems
[item
->GetId()] = forward
[i
];
633 Connect(item
->GetId(), wxEVT_COMMAND_TOOL_CLICKED
,
634 wxCommandEventHandler(WebFrame::OnHistory
), NULL
, this );
637 wxPoint position
= ScreenToClient( wxGetMousePosition() );
638 PopupMenu(m_tools_menu
, position
.x
, position
.y
);
642 * Invoked when user selects the zoom size in the menu
644 void WebFrame::OnSetZoom(wxCommandEvent
& evt
)
646 if (evt
.GetId() == m_tools_tiny
->GetId())
648 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
650 else if (evt
.GetId() == m_tools_small
->GetId())
652 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_SMALL
);
654 else if (evt
.GetId() == m_tools_medium
->GetId())
656 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
658 else if (evt
.GetId() == m_tools_large
->GetId())
660 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGE
);
662 else if (evt
.GetId() == m_tools_largest
->GetId())
664 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_LARGEST
);
672 void WebFrame::OnZoomLayout(wxCommandEvent
& WXUNUSED(evt
))
674 if(m_tools_layout
->IsChecked())
675 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
677 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
680 void WebFrame::OnHistory(wxCommandEvent
& evt
)
682 m_browser
->LoadHistoryItem(m_histMenuItems
[evt
.GetId()]);
685 void WebFrame::OnRunScript(wxCommandEvent
& WXUNUSED(evt
))
687 wxTextEntryDialog
dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr
, "", wxOK
|wxCANCEL
|wxCENTRE
|wxTE_MULTILINE
);
688 if(dialog
.ShowModal() == wxID_OK
)
690 m_browser
->RunScript(dialog
.GetValue());
695 * Callback invoked when a loading error occurs
697 void WebFrame::OnError(wxWebNavigationEvent
& evt
)
699 wxString errorCategory
;
700 switch (evt
.GetInt())
702 case wxWEB_NAV_ERR_CONNECTION
:
703 errorCategory
= "wxWEB_NAV_ERR_CONNECTION";
706 case wxWEB_NAV_ERR_CERTIFICATE
:
707 errorCategory
= "wxWEB_NAV_ERR_CERTIFICATE";
710 case wxWEB_NAV_ERR_AUTH
:
711 errorCategory
= "wxWEB_NAV_ERR_AUTH";
714 case wxWEB_NAV_ERR_SECURITY
:
715 errorCategory
= "wxWEB_NAV_ERR_SECURITY";
718 case wxWEB_NAV_ERR_NOT_FOUND
:
719 errorCategory
= "wxWEB_NAV_ERR_NOT_FOUND";
722 case wxWEB_NAV_ERR_REQUEST
:
723 errorCategory
= "wxWEB_NAV_ERR_REQUEST";
726 case wxWEB_NAV_ERR_USER_CANCELLED
:
727 errorCategory
= "wxWEB_NAV_ERR_USER_CANCELLED";
730 case wxWEB_NAV_ERR_OTHER
:
731 errorCategory
= "wxWEB_NAV_ERR_OTHER";
735 wxLogMessage("Error; url='" + evt
.GetURL() + "', error='" + errorCategory
+ "' (" + evt
.GetString() + ")");
737 //Show the info bar with an error
738 m_info
->ShowMessage(_("An error occurred loading ") + evt
.GetURL() + "\n" +
739 "'" + errorCategory
+ "' (" + evt
.GetString() + ")", wxICON_ERROR
);
745 * Invoked when user selects "Print" from the menu
747 void WebFrame::OnPrint(wxCommandEvent
& WXUNUSED(evt
))
752 SourceViewDialog::SourceViewDialog(wxWindow
* parent
, wxString source
) :
753 wxDialog(parent
, wxID_ANY
, "Source Code",
754 wxDefaultPosition
, wxSize(700,500),
755 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
757 wxStyledTextCtrl
* text
= new wxStyledTextCtrl(this, wxID_ANY
);
758 text
->SetMarginWidth(1, 30);
759 text
->SetMarginType(1, wxSTC_MARGIN_NUMBER
);
760 text
->SetText(source
);
762 text
->StyleClearAll();
763 text
->SetLexer(wxSTC_LEX_HTML
);
764 text
->StyleSetForeground(wxSTC_H_DOUBLESTRING
, wxColour(255,0,0));
765 text
->StyleSetForeground(wxSTC_H_SINGLESTRING
, wxColour(255,0,0));
766 text
->StyleSetForeground(wxSTC_H_ENTITY
, wxColour(255,0,0));
767 text
->StyleSetForeground(wxSTC_H_TAG
, wxColour(0,150,0));
768 text
->StyleSetForeground(wxSTC_H_TAGUNKNOWN
, wxColour(0,150,0));
769 text
->StyleSetForeground(wxSTC_H_ATTRIBUTE
, wxColour(0,0,150));
770 text
->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN
, wxColour(0,0,150));
771 text
->StyleSetForeground(wxSTC_H_COMMENT
, wxColour(150,150,150));
773 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
774 sizer
->Add(text
, 1, wxEXPAND
);