]> git.saurik.com Git - wxWidgets.git/blame - samples/webview/webview.cpp
Generate clipboard events for wxComboBox in wxGTK too.
[wxWidgets.git] / samples / webview / webview.cpp
CommitLineData
61b98a2d 1/////////////////////////////////////////////////////////////////////////////
467d261e 2// Name: webview.cpp
ec4ac0f7 3// Purpose: wxWebView sample
61b98a2d
SL
4// Author: Marianne Gagnon
5// Id: $Id$
ec4ac0f7 6// Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
61b98a2d
SL
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
abcb9c6e 9
ec4ac0f7
SL
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
14// For compilers that support precompilation, includes "wx/wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
21#ifndef WX_PRECOMP
22 #include "wx/wx.h"
23#endif
24
4794f127
SL
25#if !wxUSE_WEBVIEW_WEBKIT && !wxUSE_WEBVIEW_IE
26#error "A wxWebView backend is required by this sample"
27#endif
28
b8a14a17 29#include "wx/artprov.h"
0eb47013 30#include "wx/cmdline.h"
b8a14a17
SC
31#include "wx/notifmsg.h"
32#include "wx/settings.h"
33#include "wx/webview.h"
7d8d6163 34#include "wx/webviewarchivehandler.h"
b8a14a17
SC
35#include "wx/infobar.h"
36#include "wx/filesys.h"
37#include "wx/fs_arc.h"
ec4ac0f7 38
e7092398 39#ifndef wxHAS_IMAGES_IN_RESOURCES
ec4ac0f7
SL
40 #include "../sample.xpm"
41#endif
61b98a2d
SL
42
43#if wxUSE_STC
b8a14a17 44#include "wx/stc/stc.h"
61b98a2d
SL
45#else
46#error "wxStyledTextControl is needed by this sample"
47#endif
48
8bccab8f 49#if defined(__WXMSW__) || defined(__WXOSX__)
61b98a2d
SL
50#include "stop.xpm"
51#include "refresh.xpm"
8bccab8f
SL
52#endif
53
dffce0bd 54#include "wxlogo.xpm"
61b98a2d 55
10ad4ba6
SL
56
57//We map menu items to their history items
c13d6ac1 58WX_DECLARE_HASH_MAP(int, wxSharedPtr<wxWebViewHistoryItem>,
10ad4ba6
SL
59 wxIntegerHash, wxIntegerEqual, wxMenuHistoryMap);
60
ec4ac0f7 61class WebApp : public wxApp
61b98a2d
SL
62{
63public:
0eb47013
VZ
64 WebApp() :
65 m_url("http://www.wxwidgets.org")
66 {
67 }
68
ec4ac0f7 69 virtual bool OnInit();
0eb47013
VZ
70
71#if wxUSE_CMDLINE_PARSER
72 virtual void OnInitCmdLine(wxCmdLineParser& parser)
73 {
74 wxApp::OnInitCmdLine(parser);
75
76 parser.AddParam("URL to open",
77 wxCMD_LINE_VAL_STRING,
78 wxCMD_LINE_PARAM_OPTIONAL);
79 }
80
81 virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
82 {
83 if ( !wxApp::OnCmdLineParsed(parser) )
84 return false;
85
86 if ( parser.GetParamCount() )
87 m_url = parser.GetParam(0);
88
89 return true;
90 }
91#endif // wxUSE_CMDLINE_PARSER
92
93private:
94 wxString m_url;
61b98a2d
SL
95};
96
ec4ac0f7 97class WebFrame : public wxFrame
61b98a2d 98{
ec4ac0f7 99public:
0eb47013
VZ
100 WebFrame(const wxString& url);
101 virtual ~WebFrame();
ec4ac0f7 102
ec4ac0f7 103 void UpdateState();
a1788524 104 void OnIdle(wxIdleEvent& evt);
ec4ac0f7
SL
105 void OnUrl(wxCommandEvent& evt);
106 void OnBack(wxCommandEvent& evt);
107 void OnForward(wxCommandEvent& evt);
108 void OnStop(wxCommandEvent& evt);
109 void OnReload(wxCommandEvent& evt);
152a5808
SL
110 void OnClearHistory(wxCommandEvent& evt);
111 void OnEnableHistory(wxCommandEvent& evt);
04fa04d8
SL
112 void OnNavigationRequest(wxWebViewEvent& evt);
113 void OnNavigationComplete(wxWebViewEvent& evt);
114 void OnDocumentLoaded(wxWebViewEvent& evt);
115 void OnNewWindow(wxWebViewEvent& evt);
116 void OnTitleChanged(wxWebViewEvent& evt);
ec4ac0f7
SL
117 void OnViewSourceRequest(wxCommandEvent& evt);
118 void OnToolsClicked(wxCommandEvent& evt);
119 void OnSetZoom(wxCommandEvent& evt);
04fa04d8 120 void OnError(wxWebViewEvent& evt);
ec4ac0f7 121 void OnPrint(wxCommandEvent& evt);
4681a3ea
SL
122 void OnCut(wxCommandEvent& evt);
123 void OnCopy(wxCommandEvent& evt);
124 void OnPaste(wxCommandEvent& evt);
97e49559
SL
125 void OnUndo(wxCommandEvent& evt);
126 void OnRedo(wxCommandEvent& evt);
c7cbe308 127 void OnMode(wxCommandEvent& evt);
87d482ec 128 void OnZoomLayout(wxCommandEvent& evt);
10ad4ba6 129 void OnHistory(wxCommandEvent& evt);
1f7c17f4
VZ
130 void OnScrollLineUp(wxCommandEvent&) { m_browser->LineUp(); }
131 void OnScrollLineDown(wxCommandEvent&) { m_browser->LineDown(); }
132 void OnScrollPageUp(wxCommandEvent&) { m_browser->PageUp(); }
133 void OnScrollPageDown(wxCommandEvent&) { m_browser->PageDown(); }
54883129 134 void OnRunScript(wxCommandEvent& evt);
603cfe42
SL
135 void OnClearSelection(wxCommandEvent& evt);
136 void OnDeleteSelection(wxCommandEvent& evt);
137 void OnSelectAll(wxCommandEvent& evt);
49f07bec 138 void OnLoadScheme(wxCommandEvent& evt);
ec4ac0f7
SL
139
140private:
141 wxTextCtrl* m_url;
142 wxWebView* m_browser;
143
61b98a2d 144 wxToolBar* m_toolbar;
ec4ac0f7
SL
145 wxToolBarToolBase* m_toolbar_back;
146 wxToolBarToolBase* m_toolbar_forward;
147 wxToolBarToolBase* m_toolbar_stop;
148 wxToolBarToolBase* m_toolbar_reload;
149 wxToolBarToolBase* m_toolbar_tools;
150
151 wxMenu* m_tools_menu;
10ad4ba6 152 wxMenu* m_tools_history_menu;
87d482ec 153 wxMenuItem* m_tools_layout;
ec4ac0f7
SL
154 wxMenuItem* m_tools_tiny;
155 wxMenuItem* m_tools_small;
156 wxMenuItem* m_tools_medium;
157 wxMenuItem* m_tools_large;
158 wxMenuItem* m_tools_largest;
62e8551f
SL
159 wxMenuItem* m_tools_handle_navigation;
160 wxMenuItem* m_tools_handle_new_window;
152a5808 161 wxMenuItem* m_tools_enable_history;
4681a3ea
SL
162 wxMenuItem* m_edit_cut;
163 wxMenuItem* m_edit_copy;
164 wxMenuItem* m_edit_paste;
97e49559
SL
165 wxMenuItem* m_edit_undo;
166 wxMenuItem* m_edit_redo;
c7cbe308 167 wxMenuItem* m_edit_mode;
1f7c17f4
VZ
168 wxMenuItem* m_scroll_line_up;
169 wxMenuItem* m_scroll_line_down;
170 wxMenuItem* m_scroll_page_up;
171 wxMenuItem* m_scroll_page_down;
603cfe42
SL
172 wxMenuItem* m_selection_clear;
173 wxMenuItem* m_selection_delete;
ec4ac0f7 174
ec4ac0f7
SL
175 wxInfoBar *m_info;
176 wxStaticText* m_info_text;
10ad4ba6
SL
177
178 wxMenuHistoryMap m_histMenuItems;
ec4ac0f7
SL
179};
180
181class SourceViewDialog : public wxDialog
182{
61b98a2d 183public:
ec4ac0f7
SL
184 SourceViewDialog(wxWindow* parent, wxString source);
185};
186
187IMPLEMENT_APP(WebApp)
188
189// ============================================================================
190// implementation
191// ============================================================================
192
193bool WebApp::OnInit()
194{
195 if ( !wxApp::OnInit() )
196 return false;
197
0eb47013 198 WebFrame *frame = new WebFrame(m_url);
ec4ac0f7
SL
199 frame->Show();
200
201 return true;
202}
203
0eb47013
VZ
204WebFrame::WebFrame(const wxString& url) :
205 wxFrame(NULL, wxID_ANY, "wxWebView Sample")
ec4ac0f7 206{
eff8f795
SL
207 //Required from virtual file system archive support
208 wxFileSystem::AddHandler(new wxArchiveFSHandler);
209
ec4ac0f7
SL
210 // set the frame icon
211 SetIcon(wxICON(sample));
212 SetTitle("wxWebView Sample");
213
ec4ac0f7
SL
214 wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
215
216 // Create the toolbar
217 m_toolbar = CreateToolBar(wxTB_TEXT);
218 m_toolbar->SetToolBitmapSize(wxSize(32, 32));
abcb9c6e 219
dffce0bd
SL
220 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR);
221 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR);
222 #ifdef __WXGTK__
223 wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR);
abcb9c6e 224 #else
dffce0bd
SL
225 wxBitmap stop = wxBitmap(stop_xpm);
226 #endif
227 #ifdef __WXGTK__
228 wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR);
abcb9c6e 229 #else
dffce0bd
SL
230 wxBitmap refresh = wxBitmap(refresh_xpm);
231 #endif
232
233 m_toolbar_back = m_toolbar->AddTool(wxID_ANY, _("Back"), back);
234 m_toolbar_forward = m_toolbar->AddTool(wxID_ANY, _("Forward"), forward);
235 m_toolbar_stop = m_toolbar->AddTool(wxID_ANY, _("Stop"), stop);
236 m_toolbar_reload = m_toolbar->AddTool(wxID_ANY, _("Reload"), refresh);
ec4ac0f7 237 m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER );
9a00d078 238 m_toolbar->AddControl(m_url, _("URL"));
ec4ac0f7
SL
239 m_toolbar_tools = m_toolbar->AddTool(wxID_ANY, _("Menu"), wxBitmap(wxlogo_xpm));
240
241 m_toolbar->Realize();
242
243 // Create the info panel
244 m_info = new wxInfoBar(this);
245 topsizer->Add(m_info, wxSizerFlags().Expand());
246
247 // Create the webview
0eb47013 248 m_browser = wxWebView::New(this, wxID_ANY, url);
ec4ac0f7
SL
249 topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
250
7d8d6163
SL
251 //We register the wxfs:// protocol for testing purposes
252 m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
eff8f795 253
ec4ac0f7
SL
254 SetSizer(topsizer);
255
9a00d078
SL
256 //Set a more sensible size for web browsing
257 SetSize(wxSize(800, 600));
258
ec4ac0f7
SL
259 // Create a log window
260 new wxLogWindow(this, _("Logging"));
261
262 // Create the Tools menu
263 m_tools_menu = new wxMenu();
264 wxMenuItem* print = m_tools_menu->Append(wxID_ANY , _("Print"));
265 wxMenuItem* viewSource = m_tools_menu->Append(wxID_ANY , _("View Source"));
266 m_tools_menu->AppendSeparator();
87d482ec 267 m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom"));
ec4ac0f7
SL
268 m_tools_tiny = m_tools_menu->AppendCheckItem(wxID_ANY, _("Tiny"));
269 m_tools_small = m_tools_menu->AppendCheckItem(wxID_ANY, _("Small"));
270 m_tools_medium = m_tools_menu->AppendCheckItem(wxID_ANY, _("Medium"));
271 m_tools_large = m_tools_menu->AppendCheckItem(wxID_ANY, _("Large"));
272 m_tools_largest = m_tools_menu->AppendCheckItem(wxID_ANY, _("Largest"));
62e8551f
SL
273 m_tools_menu->AppendSeparator();
274 m_tools_handle_navigation = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle Navigation"));
275 m_tools_handle_new_window = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle New Windows"));
152a5808 276 m_tools_menu->AppendSeparator();
10ad4ba6
SL
277
278 //History menu
279 m_tools_history_menu = new wxMenu();
280 wxMenuItem* clearhist = m_tools_history_menu->Append(wxID_ANY, _("Clear History"));
281 m_tools_enable_history = m_tools_history_menu->AppendCheckItem(wxID_ANY, _("Enable History"));
282 m_tools_history_menu->AppendSeparator();
283
54883129 284 m_tools_menu->AppendSubMenu(m_tools_history_menu, "History");
62e8551f 285
4681a3ea
SL
286 //Create an editing menu
287 wxMenu* editmenu = new wxMenu();
288 m_edit_cut = editmenu->Append(wxID_ANY, _("Cut"));
289 m_edit_copy = editmenu->Append(wxID_ANY, _("Copy"));
290 m_edit_paste = editmenu->Append(wxID_ANY, _("Paste"));
97e49559
SL
291 editmenu->AppendSeparator();
292 m_edit_undo = editmenu->Append(wxID_ANY, _("Undo"));
293 m_edit_redo = editmenu->Append(wxID_ANY, _("Redo"));
c7cbe308
SL
294 editmenu->AppendSeparator();
295 m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode"));
4681a3ea
SL
296
297 m_tools_menu->AppendSeparator();
298 m_tools_menu->AppendSubMenu(editmenu, "Edit");
299
1f7c17f4
VZ
300 wxMenu* scroll_menu = new wxMenu;
301 m_scroll_line_up = scroll_menu->Append(wxID_ANY, "Line &up");
302 m_scroll_line_down = scroll_menu->Append(wxID_ANY, "Line &down");
303 m_scroll_page_up = scroll_menu->Append(wxID_ANY, "Page u&p");
304 m_scroll_page_down = scroll_menu->Append(wxID_ANY, "Page d&own");
305 m_tools_menu->AppendSubMenu(scroll_menu, "Scroll");
306
54883129
SL
307 wxMenuItem* script = m_tools_menu->Append(wxID_ANY, _("Run Script"));
308
603cfe42
SL
309 //Selection menu
310 wxMenu* selection = new wxMenu();
311 m_selection_clear = selection->Append(wxID_ANY, _("Clear Selection"));
312 m_selection_delete = selection->Append(wxID_ANY, _("Delete Selection"));
313 wxMenuItem* selectall = selection->Append(wxID_ANY, _("Select All"));
314
315 editmenu->AppendSubMenu(selection, "Selection");
316
49f07bec
SL
317 wxMenuItem* loadscheme = m_tools_menu->Append(wxID_ANY, _("Custom Scheme Example"));
318
61635eed 319 //By default we want to handle navigation and new windows
62e8551f
SL
320 m_tools_handle_navigation->Check();
321 m_tools_handle_new_window->Check();
152a5808 322 m_tools_enable_history->Check();
87d482ec
SL
323 if(!m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT))
324 m_tools_layout->Enable(false);
ec4ac0f7
SL
325
326
327 // Connect the toolbar events
328 Connect(m_toolbar_back->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
329 wxCommandEventHandler(WebFrame::OnBack), NULL, this );
330 Connect(m_toolbar_forward->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
331 wxCommandEventHandler(WebFrame::OnForward), NULL, this );
332 Connect(m_toolbar_stop->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
333 wxCommandEventHandler(WebFrame::OnStop), NULL, this );
334 Connect(m_toolbar_reload->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
335 wxCommandEventHandler(WebFrame::OnReload),NULL, this );
336 Connect(m_toolbar_tools->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
337 wxCommandEventHandler(WebFrame::OnToolsClicked), NULL, this );
338
abcb9c6e 339 Connect(m_url->GetId(), wxEVT_COMMAND_TEXT_ENTER,
ec4ac0f7
SL
340 wxCommandEventHandler(WebFrame::OnUrl), NULL, this );
341
342 // Connect the webview events
343 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
04fa04d8 344 wxWebViewEventHandler(WebFrame::OnNavigationRequest), NULL, this);
ec4ac0f7 345 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
04fa04d8 346 wxWebViewEventHandler(WebFrame::OnNavigationComplete), NULL, this);
ec4ac0f7 347 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED,
abcb9c6e 348 wxWebViewEventHandler(WebFrame::OnDocumentLoaded), NULL, this);
ec4ac0f7 349 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR,
04fa04d8 350 wxWebViewEventHandler(WebFrame::OnError), NULL, this);
ec4ac0f7 351 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
04fa04d8 352 wxWebViewEventHandler(WebFrame::OnNewWindow), NULL, this);
153530af 353 Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
04fa04d8 354 wxWebViewEventHandler(WebFrame::OnTitleChanged), NULL, this);
ec4ac0f7
SL
355
356 // Connect the menu events
357 Connect(viewSource->GetId(), wxEVT_COMMAND_MENU_SELECTED,
358 wxCommandEventHandler(WebFrame::OnViewSourceRequest), NULL, this );
359 Connect(print->GetId(), wxEVT_COMMAND_MENU_SELECTED,
360 wxCommandEventHandler(WebFrame::OnPrint), NULL, this );
87d482ec
SL
361 Connect(m_tools_layout->GetId(), wxEVT_COMMAND_MENU_SELECTED,
362 wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this );
ec4ac0f7
SL
363 Connect(m_tools_tiny->GetId(), wxEVT_COMMAND_MENU_SELECTED,
364 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
365 Connect(m_tools_small->GetId(), wxEVT_COMMAND_MENU_SELECTED,
366 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
367 Connect(m_tools_medium->GetId(), wxEVT_COMMAND_MENU_SELECTED,
368 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
369 Connect(m_tools_large->GetId(), wxEVT_COMMAND_MENU_SELECTED,
370 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
371 Connect(m_tools_largest->GetId(), wxEVT_COMMAND_MENU_SELECTED,
372 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
152a5808
SL
373 Connect(clearhist->GetId(), wxEVT_COMMAND_MENU_SELECTED,
374 wxCommandEventHandler(WebFrame::OnClearHistory), NULL, this );
375 Connect(m_tools_enable_history->GetId(), wxEVT_COMMAND_MENU_SELECTED,
376 wxCommandEventHandler(WebFrame::OnEnableHistory), NULL, this );
4681a3ea
SL
377 Connect(m_edit_cut->GetId(), wxEVT_COMMAND_MENU_SELECTED,
378 wxCommandEventHandler(WebFrame::OnCut), NULL, this );
379 Connect(m_edit_copy->GetId(), wxEVT_COMMAND_MENU_SELECTED,
380 wxCommandEventHandler(WebFrame::OnCopy), NULL, this );
381 Connect(m_edit_paste->GetId(), wxEVT_COMMAND_MENU_SELECTED,
382 wxCommandEventHandler(WebFrame::OnPaste), NULL, this );
97e49559
SL
383 Connect(m_edit_undo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
384 wxCommandEventHandler(WebFrame::OnUndo), NULL, this );
385 Connect(m_edit_redo->GetId(), wxEVT_COMMAND_MENU_SELECTED,
386 wxCommandEventHandler(WebFrame::OnRedo), NULL, this );
c7cbe308
SL
387 Connect(m_edit_mode->GetId(), wxEVT_COMMAND_MENU_SELECTED,
388 wxCommandEventHandler(WebFrame::OnMode), NULL, this );
1f7c17f4
VZ
389 Connect(m_scroll_line_up->GetId(), wxEVT_COMMAND_MENU_SELECTED,
390 wxCommandEventHandler(WebFrame::OnScrollLineUp), NULL, this );
391 Connect(m_scroll_line_down->GetId(), wxEVT_COMMAND_MENU_SELECTED,
392 wxCommandEventHandler(WebFrame::OnScrollLineDown), NULL, this );
393 Connect(m_scroll_page_up->GetId(), wxEVT_COMMAND_MENU_SELECTED,
394 wxCommandEventHandler(WebFrame::OnScrollPageUp), NULL, this );
395 Connect(m_scroll_page_down->GetId(), wxEVT_COMMAND_MENU_SELECTED,
396 wxCommandEventHandler(WebFrame::OnScrollPageDown), NULL, this );
54883129
SL
397 Connect(script->GetId(), wxEVT_COMMAND_MENU_SELECTED,
398 wxCommandEventHandler(WebFrame::OnRunScript), NULL, this );
603cfe42
SL
399 Connect(m_selection_clear->GetId(), wxEVT_COMMAND_MENU_SELECTED,
400 wxCommandEventHandler(WebFrame::OnClearSelection), NULL, this );
401 Connect(m_selection_delete->GetId(), wxEVT_COMMAND_MENU_SELECTED,
402 wxCommandEventHandler(WebFrame::OnDeleteSelection), NULL, this );
403 Connect(selectall->GetId(), wxEVT_COMMAND_MENU_SELECTED,
404 wxCommandEventHandler(WebFrame::OnSelectAll), NULL, this );
49f07bec
SL
405 Connect(loadscheme->GetId(), wxEVT_COMMAND_MENU_SELECTED,
406 wxCommandEventHandler(WebFrame::OnLoadScheme), NULL, this );
a1788524
SL
407
408 //Connect the idle events
409 Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(WebFrame::OnIdle), NULL, this);
ec4ac0f7
SL
410}
411
7892e035
SL
412WebFrame::~WebFrame()
413{
414 delete m_tools_menu;
415}
416
ec4ac0f7
SL
417/**
418 * Method that retrieves the current state from the web control and updates the GUI
419 * the reflect this current state.
420 */
421void WebFrame::UpdateState()
422{
423 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
424 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
abcb9c6e 425
ec4ac0f7 426 if (m_browser->IsBusy())
61b98a2d 427 {
abcb9c6e 428 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
61b98a2d 429 }
ec4ac0f7 430 else
61b98a2d 431 {
abcb9c6e 432 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
61b98a2d 433 }
abcb9c6e 434
ec4ac0f7
SL
435 SetTitle( m_browser->GetCurrentTitle() );
436 m_url->SetValue( m_browser->GetCurrentURL() );
437}
438
c49d2434 439void WebFrame::OnIdle(wxIdleEvent& WXUNUSED(evt))
a1788524
SL
440{
441 if(m_browser->IsBusy())
442 {
443 wxSetCursor(wxCURSOR_ARROWWAIT);
444 m_toolbar->EnableTool(m_toolbar_stop->GetId(), true);
445 }
446 else
447 {
448 wxSetCursor(wxNullCursor);
449 m_toolbar->EnableTool(m_toolbar_stop->GetId(), false);
450 }
451}
452
ec4ac0f7
SL
453/**
454 * Callback invoked when user entered an URL and pressed enter
455 */
8bccab8f 456void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7 457{
4d0dddc7 458 m_browser->LoadURL( m_url->GetValue() );
c49d2434 459 m_browser->SetFocus();
ec4ac0f7
SL
460 UpdateState();
461}
462
463/**
464 * Callback invoked when user pressed the "back" button
465 */
8bccab8f 466void WebFrame::OnBack(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
467{
468 m_browser->GoBack();
469 UpdateState();
470}
471
472/**
473 * Callback invoked when user pressed the "forward" button
474 */
8bccab8f 475void WebFrame::OnForward(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
476{
477 m_browser->GoForward();
478 UpdateState();
479}
480
481/**
482 * Callback invoked when user pressed the "stop" button
483 */
8bccab8f 484void WebFrame::OnStop(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
485{
486 m_browser->Stop();
487 UpdateState();
488}
489
490/**
491 * Callback invoked when user pressed the "reload" button
492 */
8bccab8f 493void WebFrame::OnReload(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
494{
495 m_browser->Reload();
496 UpdateState();
497}
498
8bccab8f 499void WebFrame::OnClearHistory(wxCommandEvent& WXUNUSED(evt))
152a5808
SL
500{
501 m_browser->ClearHistory();
502 UpdateState();
503}
504
8bccab8f 505void WebFrame::OnEnableHistory(wxCommandEvent& WXUNUSED(evt))
152a5808
SL
506{
507 m_browser->EnableHistory(m_tools_enable_history->IsChecked());
508 UpdateState();
509}
510
8bccab8f 511void WebFrame::OnCut(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
512{
513 m_browser->Cut();
514}
515
8bccab8f 516void WebFrame::OnCopy(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
517{
518 m_browser->Copy();
519}
520
8bccab8f 521void WebFrame::OnPaste(wxCommandEvent& WXUNUSED(evt))
4681a3ea
SL
522{
523 m_browser->Paste();
524}
525
8bccab8f 526void WebFrame::OnUndo(wxCommandEvent& WXUNUSED(evt))
97e49559
SL
527{
528 m_browser->Undo();
529}
530
8bccab8f 531void WebFrame::OnRedo(wxCommandEvent& WXUNUSED(evt))
97e49559
SL
532{
533 m_browser->Redo();
534}
535
8bccab8f 536void WebFrame::OnMode(wxCommandEvent& WXUNUSED(evt))
c7cbe308
SL
537{
538 m_browser->SetEditable(m_edit_mode->IsChecked());
539}
540
49f07bec
SL
541void WebFrame::OnLoadScheme(wxCommandEvent& WXUNUSED(evt))
542{
543 wxFileName helpfile("../help/doc.zip");
544 helpfile.MakeAbsolute();
545 wxString path = helpfile.GetFullPath();
546 //Under MSW we need to flip the slashes
547 path.Replace("\\", "/");
548 path = "wxfs:///" + path + ";protocol=zip/doc.htm";
4d0dddc7 549 m_browser->LoadURL(path);
49f07bec 550}
c7cbe308 551
ec4ac0f7
SL
552/**
553 * Callback invoked when there is a request to load a new page (for instance
554 * when the user clicks a link)
555 */
04fa04d8 556void WebFrame::OnNavigationRequest(wxWebViewEvent& evt)
ec4ac0f7 557{
75b0b0bc
SL
558 if(m_info->IsShown())
559 {
70544c1e 560 m_info->Dismiss();
75b0b0bc
SL
561 }
562
e40741b9 563 wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" +
ec4ac0f7 564 evt.GetTarget() + "')");
abcb9c6e 565
ec4ac0f7 566 wxASSERT(m_browser->IsBusy());
62e8551f
SL
567
568 //If we don't want to handle navigation then veto the event and navigation
0e830c7c 569 //will not take place, we also need to stop the loading animation
62e8551f 570 if(!m_tools_handle_navigation->IsChecked())
0e830c7c 571 {
62e8551f 572 evt.Veto();
abcb9c6e 573 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
0e830c7c
SL
574 }
575 else
576 {
577 UpdateState();
578 }
ec4ac0f7
SL
579}
580
581/**
582 * Callback invoked when a navigation request was accepted
583 */
04fa04d8 584void WebFrame::OnNavigationComplete(wxWebViewEvent& evt)
ec4ac0f7 585{
e40741b9 586 wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'");
ec4ac0f7
SL
587 UpdateState();
588}
589
590/**
591 * Callback invoked when a page is finished loading
592 */
04fa04d8 593void WebFrame::OnDocumentLoaded(wxWebViewEvent& evt)
ec4ac0f7 594{
113e0a92 595 //Only notify if the document is the main frame, not a subframe
e40741b9 596 if(evt.GetURL() == m_browser->GetCurrentURL())
8bccab8f 597 {
e40741b9 598 wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'");
8bccab8f 599 }
ec4ac0f7
SL
600 UpdateState();
601}
602
603/**
604 * On new window, we veto to stop extra windows appearing
605 */
04fa04d8 606void WebFrame::OnNewWindow(wxWebViewEvent& evt)
ec4ac0f7 607{
e40741b9 608 wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'");
62e8551f 609
abcb9c6e 610 //If we handle new window events then just load them in this window as we
62e8551f
SL
611 //are a single window browser
612 if(m_tools_handle_new_window->IsChecked())
4d0dddc7 613 m_browser->LoadURL(evt.GetURL());
62e8551f 614
ec4ac0f7
SL
615 UpdateState();
616}
617
04fa04d8 618void WebFrame::OnTitleChanged(wxWebViewEvent& evt)
153530af 619{
7cd03861 620 SetTitle(evt.GetString());
153530af 621 wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'");
153530af
SL
622}
623
ec4ac0f7
SL
624/**
625 * Invoked when user selects the "View Source" menu item
626 */
8bccab8f 627void WebFrame::OnViewSourceRequest(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
628{
629 SourceViewDialog dlg(this, m_browser->GetPageSource());
630 dlg.ShowModal();
631}
632
633/**
634 * Invoked when user selects the "Menu" item
635 */
8bccab8f 636void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
ec4ac0f7
SL
637{
638 if(m_browser->GetCurrentURL() == "")
639 return;
640
641 m_tools_tiny->Check(false);
abcb9c6e 642 m_tools_small->Check(false);
ec4ac0f7
SL
643 m_tools_medium->Check(false);
644 m_tools_large->Check(false);
645 m_tools_largest->Check(false);
646
647 wxWebViewZoom zoom = m_browser->GetZoom();
648 switch (zoom)
61b98a2d 649 {
ec4ac0f7
SL
650 case wxWEB_VIEW_ZOOM_TINY:
651 m_tools_tiny->Check();
652 break;
653 case wxWEB_VIEW_ZOOM_SMALL:
654 m_tools_small->Check();
655 break;
656 case wxWEB_VIEW_ZOOM_MEDIUM:
657 m_tools_medium->Check();
658 break;
659 case wxWEB_VIEW_ZOOM_LARGE:
660 m_tools_large->Check();
661 break;
662 case wxWEB_VIEW_ZOOM_LARGEST:
663 m_tools_largest->Check();
664 break;
61b98a2d 665 }
4681a3ea
SL
666
667 m_edit_cut->Enable(m_browser->CanCut());
668 m_edit_copy->Enable(m_browser->CanCopy());
669 m_edit_paste->Enable(m_browser->CanPaste());
97e49559
SL
670
671 m_edit_undo->Enable(m_browser->CanUndo());
672 m_edit_redo->Enable(m_browser->CanRedo());
10ad4ba6 673
603cfe42
SL
674 m_selection_clear->Enable(m_browser->HasSelection());
675 m_selection_delete->Enable(m_browser->HasSelection());
676
10ad4ba6
SL
677 //Firstly we clear the existing menu items, then we add the current ones
678 wxMenuHistoryMap::const_iterator it;
679 for( it = m_histMenuItems.begin(); it != m_histMenuItems.end(); ++it )
680 {
681 m_tools_history_menu->Destroy(it->first);
682 }
683 m_histMenuItems.clear();
684
c13d6ac1
SL
685 wxVector<wxSharedPtr<wxWebViewHistoryItem> > back = m_browser->GetBackwardHistory();
686 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forward = m_browser->GetForwardHistory();
10ad4ba6
SL
687
688 wxMenuItem* item;
689
abcb9c6e
JS
690 unsigned int i;
691 for(i = 0; i < back.size(); i++)
10ad4ba6
SL
692 {
693 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, back[i]->GetTitle());
694 m_histMenuItems[item->GetId()] = back[i];
695 Connect(item->GetId(), wxEVT_COMMAND_MENU_SELECTED,
696 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
697 }
698
14a0b47d
VZ
699 wxString title = m_browser->GetCurrentTitle();
700 if ( title.empty() )
701 title = "(untitled)";
702 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, title);
10ad4ba6
SL
703 item->Check();
704
705 //No need to connect the current item
c13d6ac1 706 m_histMenuItems[item->GetId()] = wxSharedPtr<wxWebViewHistoryItem>(new wxWebViewHistoryItem(m_browser->GetCurrentURL(), m_browser->GetCurrentTitle()));
10ad4ba6 707
abcb9c6e 708 for(i = 0; i < forward.size(); i++)
10ad4ba6
SL
709 {
710 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, forward[i]->GetTitle());
711 m_histMenuItems[item->GetId()] = forward[i];
712 Connect(item->GetId(), wxEVT_COMMAND_TOOL_CLICKED,
713 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
714 }
abcb9c6e 715
ec4ac0f7
SL
716 wxPoint position = ScreenToClient( wxGetMousePosition() );
717 PopupMenu(m_tools_menu, position.x, position.y);
718}
719
720/**
721 * Invoked when user selects the zoom size in the menu
722 */
723void WebFrame::OnSetZoom(wxCommandEvent& evt)
724{
725 if (evt.GetId() == m_tools_tiny->GetId())
61b98a2d 726 {
ec4ac0f7 727 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
61b98a2d 728 }
ec4ac0f7 729 else if (evt.GetId() == m_tools_small->GetId())
61b98a2d 730 {
ec4ac0f7 731 m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL);
61b98a2d 732 }
ec4ac0f7 733 else if (evt.GetId() == m_tools_medium->GetId())
61b98a2d 734 {
ec4ac0f7 735 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
61b98a2d 736 }
ec4ac0f7 737 else if (evt.GetId() == m_tools_large->GetId())
61b98a2d 738 {
ec4ac0f7 739 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE);
61b98a2d 740 }
ec4ac0f7 741 else if (evt.GetId() == m_tools_largest->GetId())
61b98a2d 742 {
ec4ac0f7 743 m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST);
61b98a2d 744 }
ec4ac0f7 745 else
61b98a2d 746 {
ec4ac0f7 747 wxFAIL;
61b98a2d 748 }
ec4ac0f7 749}
853b6cd0 750
8bccab8f 751void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt))
87d482ec
SL
752{
753 if(m_tools_layout->IsChecked())
754 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT);
755 else
756 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT);
757}
758
10ad4ba6
SL
759void WebFrame::OnHistory(wxCommandEvent& evt)
760{
761 m_browser->LoadHistoryItem(m_histMenuItems[evt.GetId()]);
762}
763
54883129
SL
764void WebFrame::OnRunScript(wxCommandEvent& WXUNUSED(evt))
765{
766 wxTextEntryDialog dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr, "", wxOK|wxCANCEL|wxCENTRE|wxTE_MULTILINE);
767 if(dialog.ShowModal() == wxID_OK)
768 {
769 m_browser->RunScript(dialog.GetValue());
770 }
771}
772
603cfe42
SL
773void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))
774{
775 m_browser->ClearSelection();
776}
777
778void WebFrame::OnDeleteSelection(wxCommandEvent& WXUNUSED(evt))
779{
780 m_browser->DeleteSelection();
781}
782
783void WebFrame::OnSelectAll(wxCommandEvent& WXUNUSED(evt))
784{
785 m_browser->SelectAll();
786}
787
ec4ac0f7
SL
788/**
789 * Callback invoked when a loading error occurs
790 */
04fa04d8 791void WebFrame::OnError(wxWebViewEvent& evt)
ec4ac0f7
SL
792{
793 wxString errorCategory;
794 switch (evt.GetInt())
61b98a2d 795 {
ec4ac0f7
SL
796 case wxWEB_NAV_ERR_CONNECTION:
797 errorCategory = "wxWEB_NAV_ERR_CONNECTION";
798 break;
abcb9c6e 799
ec4ac0f7
SL
800 case wxWEB_NAV_ERR_CERTIFICATE:
801 errorCategory = "wxWEB_NAV_ERR_CERTIFICATE";
802 break;
abcb9c6e 803
ec4ac0f7
SL
804 case wxWEB_NAV_ERR_AUTH:
805 errorCategory = "wxWEB_NAV_ERR_AUTH";
806 break;
abcb9c6e 807
ec4ac0f7
SL
808 case wxWEB_NAV_ERR_SECURITY:
809 errorCategory = "wxWEB_NAV_ERR_SECURITY";
810 break;
abcb9c6e 811
ec4ac0f7
SL
812 case wxWEB_NAV_ERR_NOT_FOUND:
813 errorCategory = "wxWEB_NAV_ERR_NOT_FOUND";
814 break;
abcb9c6e 815
ec4ac0f7
SL
816 case wxWEB_NAV_ERR_REQUEST:
817 errorCategory = "wxWEB_NAV_ERR_REQUEST";
818 break;
abcb9c6e 819
ec4ac0f7
SL
820 case wxWEB_NAV_ERR_USER_CANCELLED:
821 errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED";
822 break;
abcb9c6e 823
ec4ac0f7
SL
824 case wxWEB_NAV_ERR_OTHER:
825 errorCategory = "wxWEB_NAV_ERR_OTHER";
826 break;
61b98a2d 827 }
abcb9c6e 828
f9a786f8 829 wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")");
abcb9c6e 830
ec4ac0f7 831 //Show the info bar with an error
e40741b9 832 m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +
ec4ac0f7 833 "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR);
abcb9c6e 834
ec4ac0f7
SL
835 UpdateState();
836}
61b98a2d 837
ec4ac0f7
SL
838/**
839 * Invoked when user selects "Print" from the menu
840 */
8bccab8f 841void WebFrame::OnPrint(wxCommandEvent& WXUNUSED(evt))
61b98a2d 842{
ec4ac0f7
SL
843 m_browser->Print();
844}
61b98a2d 845
ec4ac0f7
SL
846SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
847 wxDialog(parent, wxID_ANY, "Source Code",
848 wxDefaultPosition, wxSize(700,500),
849 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
850{
851 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
852 text->SetMarginWidth(1, 30);
abcb9c6e 853 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
ec4ac0f7 854 text->SetText(source);
61b98a2d 855
ec4ac0f7
SL
856 text->StyleClearAll();
857 text->SetLexer(wxSTC_LEX_HTML);
858 text->StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(255,0,0));
859 text->StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(255,0,0));
860 text->StyleSetForeground(wxSTC_H_ENTITY, wxColour(255,0,0));
861 text->StyleSetForeground(wxSTC_H_TAG, wxColour(0,150,0));
862 text->StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(0,150,0));
863 text->StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,0,150));
864 text->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(0,0,150));
865 text->StyleSetForeground(wxSTC_H_COMMENT, wxColour(150,150,150));
61b98a2d 866
61b98a2d 867 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
ec4ac0f7
SL
868 sizer->Add(text, 1, wxEXPAND);
869 SetSizer(sizer);
61b98a2d 870}