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