]> git.saurik.com Git - wxWidgets.git/blob - samples/webview/webview.cpp
5ec26471277c5dc3e4de822768b814f24f51756e
[wxWidgets.git] / samples / webview / webview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: webview.cpp
3 // Purpose: wxWebView sample
4 // Author: Marianne Gagnon
5 // Id: $Id$
6 // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #endif
24
25 #if !wxUSE_WEBVIEW_WEBKIT && !wxUSE_WEBVIEW_IE
26 #error "A wxWebView backend is required by this sample"
27 #endif
28
29 #include "wx/artprov.h"
30 #include "wx/cmdline.h"
31 #include "wx/notifmsg.h"
32 #include "wx/settings.h"
33 #include "wx/webview.h"
34 #include "wx/webviewarchivehandler.h"
35 #include "wx/webviewfshandler.h"
36 #include "wx/infobar.h"
37 #include "wx/filesys.h"
38 #include "wx/fs_arc.h"
39 #include "wx/fs_mem.h"
40
41 #ifndef wxHAS_IMAGES_IN_RESOURCES
42 #include "../sample.xpm"
43 #endif
44
45 #if wxUSE_STC
46 #include "wx/stc/stc.h"
47 #else
48 #error "wxStyledTextControl is needed by this sample"
49 #endif
50
51 #if defined(__WXMSW__) || defined(__WXOSX__)
52 #include "stop.xpm"
53 #include "refresh.xpm"
54 #endif
55
56 #include "wxlogo.xpm"
57
58
59 //We map menu items to their history items
60 WX_DECLARE_HASH_MAP(int, wxSharedPtr<wxWebViewHistoryItem>,
61 wxIntegerHash, wxIntegerEqual, wxMenuHistoryMap);
62
63 class WebApp : public wxApp
64 {
65 public:
66 WebApp() :
67 m_url("http://www.wxwidgets.org")
68 {
69 }
70
71 virtual bool OnInit();
72
73 #if wxUSE_CMDLINE_PARSER
74 virtual void OnInitCmdLine(wxCmdLineParser& parser)
75 {
76 wxApp::OnInitCmdLine(parser);
77
78 parser.AddParam("URL to open",
79 wxCMD_LINE_VAL_STRING,
80 wxCMD_LINE_PARAM_OPTIONAL);
81 }
82
83 virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
84 {
85 if ( !wxApp::OnCmdLineParsed(parser) )
86 return false;
87
88 if ( parser.GetParamCount() )
89 m_url = parser.GetParam(0);
90
91 return true;
92 }
93 #endif // wxUSE_CMDLINE_PARSER
94
95 private:
96 wxString m_url;
97 };
98
99 class WebFrame : public wxFrame
100 {
101 public:
102 WebFrame(const wxString& url);
103 virtual ~WebFrame();
104
105 void UpdateState();
106 void OnIdle(wxIdleEvent& evt);
107 void OnUrl(wxCommandEvent& evt);
108 void OnBack(wxCommandEvent& evt);
109 void OnForward(wxCommandEvent& evt);
110 void OnStop(wxCommandEvent& evt);
111 void OnReload(wxCommandEvent& evt);
112 void OnClearHistory(wxCommandEvent& evt);
113 void OnEnableHistory(wxCommandEvent& evt);
114 void OnNavigationRequest(wxWebViewEvent& evt);
115 void OnNavigationComplete(wxWebViewEvent& evt);
116 void OnDocumentLoaded(wxWebViewEvent& evt);
117 void OnNewWindow(wxWebViewEvent& evt);
118 void OnTitleChanged(wxWebViewEvent& evt);
119 void OnViewSourceRequest(wxCommandEvent& evt);
120 void OnToolsClicked(wxCommandEvent& evt);
121 void OnSetZoom(wxCommandEvent& evt);
122 void OnError(wxWebViewEvent& evt);
123 void OnPrint(wxCommandEvent& evt);
124 void OnCut(wxCommandEvent& evt);
125 void OnCopy(wxCommandEvent& evt);
126 void OnPaste(wxCommandEvent& evt);
127 void OnUndo(wxCommandEvent& evt);
128 void OnRedo(wxCommandEvent& evt);
129 void OnMode(wxCommandEvent& evt);
130 void OnZoomLayout(wxCommandEvent& evt);
131 void OnHistory(wxCommandEvent& evt);
132 void OnScrollLineUp(wxCommandEvent&) { m_browser->LineUp(); }
133 void OnScrollLineDown(wxCommandEvent&) { m_browser->LineDown(); }
134 void OnScrollPageUp(wxCommandEvent&) { m_browser->PageUp(); }
135 void OnScrollPageDown(wxCommandEvent&) { m_browser->PageDown(); }
136 void OnRunScript(wxCommandEvent& evt);
137 void OnClearSelection(wxCommandEvent& evt);
138 void OnDeleteSelection(wxCommandEvent& evt);
139 void OnSelectAll(wxCommandEvent& evt);
140 void OnLoadScheme(wxCommandEvent& evt);
141 void OnUseMemoryFS(wxCommandEvent& evt);
142 void OnFind(wxCommandEvent& evt);
143 void OnFindDone(wxCommandEvent& evt);
144 void OnFindText(wxCommandEvent& evt);
145 void OnFindOptions(wxCommandEvent& evt);
146 void OnEnableContextMenu(wxCommandEvent& evt);
147
148 private:
149 wxTextCtrl* m_url;
150 wxWebView* m_browser;
151
152 wxToolBar* m_toolbar;
153 wxToolBarToolBase* m_toolbar_back;
154 wxToolBarToolBase* m_toolbar_forward;
155 wxToolBarToolBase* m_toolbar_stop;
156 wxToolBarToolBase* m_toolbar_reload;
157 wxToolBarToolBase* m_toolbar_tools;
158
159 wxToolBarToolBase* m_find_toolbar_done;
160 wxToolBarToolBase* m_find_toolbar_next;
161 wxToolBarToolBase* m_find_toolbar_previous;
162 wxToolBarToolBase* m_find_toolbar_options;
163 wxMenuItem* m_find_toolbar_wrap;
164 wxMenuItem* m_find_toolbar_highlight;
165 wxMenuItem* m_find_toolbar_matchcase;
166 wxMenuItem* m_find_toolbar_wholeword;
167
168 wxMenu* m_tools_menu;
169 wxMenu* m_tools_history_menu;
170 wxMenuItem* m_tools_layout;
171 wxMenuItem* m_tools_tiny;
172 wxMenuItem* m_tools_small;
173 wxMenuItem* m_tools_medium;
174 wxMenuItem* m_tools_large;
175 wxMenuItem* m_tools_largest;
176 wxMenuItem* m_tools_handle_navigation;
177 wxMenuItem* m_tools_handle_new_window;
178 wxMenuItem* m_tools_enable_history;
179 wxMenuItem* m_edit_cut;
180 wxMenuItem* m_edit_copy;
181 wxMenuItem* m_edit_paste;
182 wxMenuItem* m_edit_undo;
183 wxMenuItem* m_edit_redo;
184 wxMenuItem* m_edit_mode;
185 wxMenuItem* m_scroll_line_up;
186 wxMenuItem* m_scroll_line_down;
187 wxMenuItem* m_scroll_page_up;
188 wxMenuItem* m_scroll_page_down;
189 wxMenuItem* m_selection_clear;
190 wxMenuItem* m_selection_delete;
191 wxMenuItem* m_find;
192 wxMenuItem* m_context_menu;
193
194 wxInfoBar *m_info;
195 wxStaticText* m_info_text;
196 wxTextCtrl* m_find_ctrl;
197 wxToolBar* m_find_toolbar;
198
199 wxMenuHistoryMap m_histMenuItems;
200 wxString m_findText;
201 int m_findFlags, m_findCount;
202 };
203
204 class SourceViewDialog : public wxDialog
205 {
206 public:
207 SourceViewDialog(wxWindow* parent, wxString source);
208 };
209
210 IMPLEMENT_APP(WebApp)
211
212 // ============================================================================
213 // implementation
214 // ============================================================================
215
216 bool WebApp::OnInit()
217 {
218 if ( !wxApp::OnInit() )
219 return false;
220
221 //Required for virtual file system archive and memory support
222 wxFileSystem::AddHandler(new wxArchiveFSHandler);
223 wxFileSystem::AddHandler(new wxMemoryFSHandler);
224
225 // Create the memory files
226 wxImage::AddHandler(new wxPNGHandler);
227 wxMemoryFSHandler::AddFile("logo.png",
228 wxBitmap(wxlogo_xpm), wxBITMAP_TYPE_PNG);
229 wxMemoryFSHandler::AddFile("page1.htm",
230 "<html><head><title>File System Example</title>"
231 "<link rel='stylesheet' type='text/css' href='memory:test.css'>"
232 "</head><body><h1>Page 1</h1>"
233 "<p><img src='memory:logo.png'></p>"
234 "<p>Some text about <a href='memory:page2.htm'>Page 2</a>.</p></body>");
235 wxMemoryFSHandler::AddFile("page2.htm",
236 "<html><head><title>File System Example</title>"
237 "<link rel='stylesheet' type='text/css' href='memory:test.css'>"
238 "</head><body><h1>Page 2</h1>"
239 "<p><a href='memory:page1.htm'>Page 1</a> was better.</p></body>");
240 wxMemoryFSHandler::AddFile("test.css", "h1 {color: red;}");
241
242 WebFrame *frame = new WebFrame(m_url);
243 frame->Show();
244
245 return true;
246 }
247
248 WebFrame::WebFrame(const wxString& url) :
249 wxFrame(NULL, wxID_ANY, "wxWebView Sample")
250 {
251 // set the frame icon
252 SetIcon(wxICON(sample));
253 SetTitle("wxWebView Sample");
254
255 wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
256
257 // Create the toolbar
258 m_toolbar = CreateToolBar(wxTB_TEXT);
259 m_toolbar->SetToolBitmapSize(wxSize(32, 32));
260
261 wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR);
262 wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR);
263 #ifdef __WXGTK__
264 wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR);
265 #else
266 wxBitmap stop = wxBitmap(stop_xpm);
267 #endif
268 #ifdef __WXGTK__
269 wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR);
270 #else
271 wxBitmap refresh = wxBitmap(refresh_xpm);
272 #endif
273
274 m_toolbar_back = m_toolbar->AddTool(wxID_ANY, _("Back"), back);
275 m_toolbar_forward = m_toolbar->AddTool(wxID_ANY, _("Forward"), forward);
276 m_toolbar_stop = m_toolbar->AddTool(wxID_ANY, _("Stop"), stop);
277 m_toolbar_reload = m_toolbar->AddTool(wxID_ANY, _("Reload"), refresh);
278 m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER );
279 m_toolbar->AddControl(m_url, _("URL"));
280 m_toolbar_tools = m_toolbar->AddTool(wxID_ANY, _("Menu"), wxBitmap(wxlogo_xpm));
281
282 m_toolbar->Realize();
283
284 // Set find values.
285 m_findFlags = wxWEBVIEW_FIND_DEFAULT;
286 m_findText = wxEmptyString;
287 m_findCount = 0;
288
289 // Create panel for find toolbar.
290 wxPanel* panel = new wxPanel(this);
291 topsizer->Add(panel, wxSizerFlags().Expand());
292
293 // Create sizer for panel.
294 wxBoxSizer* panel_sizer = new wxBoxSizer(wxVERTICAL);
295 panel->SetSizer(panel_sizer);
296
297 // Create the find toolbar.
298 m_find_toolbar = new wxToolBar(panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL|wxTB_TEXT|wxTB_HORZ_LAYOUT);
299 m_find_toolbar->Hide();
300 panel_sizer->Add(m_find_toolbar, wxSizerFlags().Expand());
301
302 // Create find control.
303 m_find_ctrl = new wxTextCtrl(m_find_toolbar, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(140,-1), wxTE_PROCESS_ENTER);
304
305
306 //Find options menu
307 wxMenu* findmenu = new wxMenu;
308 m_find_toolbar_wrap = findmenu->AppendCheckItem(wxID_ANY,"Wrap");
309 m_find_toolbar_matchcase = findmenu->AppendCheckItem(wxID_ANY,"Match Case");
310 m_find_toolbar_wholeword = findmenu->AppendCheckItem(wxID_ANY,"Entire Word");
311 m_find_toolbar_highlight = findmenu->AppendCheckItem(wxID_ANY,"Highlight");
312 // Add find toolbar tools.
313 m_find_toolbar->SetToolSeparation(7);
314 m_find_toolbar_done = m_find_toolbar->AddTool(wxID_ANY, "Close", wxArtProvider::GetBitmap(wxART_CROSS_MARK));
315 m_find_toolbar->AddSeparator();
316 m_find_toolbar->AddControl(m_find_ctrl, "Find");
317 m_find_toolbar->AddSeparator();
318 m_find_toolbar_next = m_find_toolbar->AddTool(wxID_ANY, "Next", wxArtProvider::GetBitmap(wxART_GO_DOWN, wxART_TOOLBAR, wxSize(16,16)));
319 m_find_toolbar_previous = m_find_toolbar->AddTool(wxID_ANY, "Previous", wxArtProvider::GetBitmap(wxART_GO_UP, wxART_TOOLBAR, wxSize(16,16)));
320 m_find_toolbar->AddSeparator();
321 m_find_toolbar_options = m_find_toolbar->AddTool(wxID_ANY, "Options", wxArtProvider::GetBitmap(wxART_PLUS, wxART_TOOLBAR, wxSize(16,16)), "", wxITEM_DROPDOWN);
322 m_find_toolbar_options->SetDropdownMenu(findmenu);
323 m_find_toolbar->Realize();
324
325 // Create the info panel
326 m_info = new wxInfoBar(this);
327 topsizer->Add(m_info, wxSizerFlags().Expand());
328
329 // Create the webview
330 m_browser = wxWebView::New(this, wxID_ANY, url);
331 topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
332
333 //We register the wxfs:// protocol for testing purposes
334 m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
335 //And the memory: file system
336 m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewFSHandler("memory")));
337
338 SetSizer(topsizer);
339
340 //Set a more sensible size for web browsing
341 SetSize(wxSize(800, 600));
342
343 // Create a log window
344 new wxLogWindow(this, _("Logging"), true, false);
345
346 // Create the Tools menu
347 m_tools_menu = new wxMenu();
348 wxMenuItem* print = m_tools_menu->Append(wxID_ANY , _("Print"));
349 wxMenuItem* viewSource = m_tools_menu->Append(wxID_ANY , _("View Source"));
350 m_tools_menu->AppendSeparator();
351 m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom"));
352 m_tools_tiny = m_tools_menu->AppendCheckItem(wxID_ANY, _("Tiny"));
353 m_tools_small = m_tools_menu->AppendCheckItem(wxID_ANY, _("Small"));
354 m_tools_medium = m_tools_menu->AppendCheckItem(wxID_ANY, _("Medium"));
355 m_tools_large = m_tools_menu->AppendCheckItem(wxID_ANY, _("Large"));
356 m_tools_largest = m_tools_menu->AppendCheckItem(wxID_ANY, _("Largest"));
357 m_tools_menu->AppendSeparator();
358 m_tools_handle_navigation = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle Navigation"));
359 m_tools_handle_new_window = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle New Windows"));
360 m_tools_menu->AppendSeparator();
361
362 //Find
363 m_find = m_tools_menu->Append(wxID_ANY, _("Find"));
364 m_tools_menu->AppendSeparator();
365
366 //History menu
367 m_tools_history_menu = new wxMenu();
368 wxMenuItem* clearhist = m_tools_history_menu->Append(wxID_ANY, _("Clear History"));
369 m_tools_enable_history = m_tools_history_menu->AppendCheckItem(wxID_ANY, _("Enable History"));
370 m_tools_history_menu->AppendSeparator();
371
372 m_tools_menu->AppendSubMenu(m_tools_history_menu, "History");
373
374 //Create an editing menu
375 wxMenu* editmenu = new wxMenu();
376 m_edit_cut = editmenu->Append(wxID_ANY, _("Cut"));
377 m_edit_copy = editmenu->Append(wxID_ANY, _("Copy"));
378 m_edit_paste = editmenu->Append(wxID_ANY, _("Paste"));
379 editmenu->AppendSeparator();
380 m_edit_undo = editmenu->Append(wxID_ANY, _("Undo"));
381 m_edit_redo = editmenu->Append(wxID_ANY, _("Redo"));
382 editmenu->AppendSeparator();
383 m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode"));
384
385 m_tools_menu->AppendSeparator();
386 m_tools_menu->AppendSubMenu(editmenu, "Edit");
387
388 wxMenu* scroll_menu = new wxMenu;
389 m_scroll_line_up = scroll_menu->Append(wxID_ANY, "Line &up");
390 m_scroll_line_down = scroll_menu->Append(wxID_ANY, "Line &down");
391 m_scroll_page_up = scroll_menu->Append(wxID_ANY, "Page u&p");
392 m_scroll_page_down = scroll_menu->Append(wxID_ANY, "Page d&own");
393 m_tools_menu->AppendSubMenu(scroll_menu, "Scroll");
394
395 wxMenuItem* script = m_tools_menu->Append(wxID_ANY, _("Run Script"));
396
397 //Selection menu
398 wxMenu* selection = new wxMenu();
399 m_selection_clear = selection->Append(wxID_ANY, _("Clear Selection"));
400 m_selection_delete = selection->Append(wxID_ANY, _("Delete Selection"));
401 wxMenuItem* selectall = selection->Append(wxID_ANY, _("Select All"));
402
403 editmenu->AppendSubMenu(selection, "Selection");
404
405 wxMenuItem* loadscheme = m_tools_menu->Append(wxID_ANY, _("Custom Scheme Example"));
406 wxMenuItem* usememoryfs = m_tools_menu->Append(wxID_ANY, _("Memory File System Example"));
407
408 m_context_menu = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable Context Menu"));
409
410 //By default we want to handle navigation and new windows
411 m_tools_handle_navigation->Check();
412 m_tools_handle_new_window->Check();
413 m_tools_enable_history->Check();
414 if(!m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT))
415 m_tools_layout->Enable(false);
416
417
418 // Connect the toolbar events
419 Connect(m_toolbar_back->GetId(), wxEVT_TOOL,
420 wxCommandEventHandler(WebFrame::OnBack), NULL, this );
421 Connect(m_toolbar_forward->GetId(), wxEVT_TOOL,
422 wxCommandEventHandler(WebFrame::OnForward), NULL, this );
423 Connect(m_toolbar_stop->GetId(), wxEVT_TOOL,
424 wxCommandEventHandler(WebFrame::OnStop), NULL, this );
425 Connect(m_toolbar_reload->GetId(), wxEVT_TOOL,
426 wxCommandEventHandler(WebFrame::OnReload),NULL, this );
427 Connect(m_toolbar_tools->GetId(), wxEVT_TOOL,
428 wxCommandEventHandler(WebFrame::OnToolsClicked), NULL, this );
429
430 Connect(m_url->GetId(), wxEVT_TEXT_ENTER,
431 wxCommandEventHandler(WebFrame::OnUrl), NULL, this );
432
433 // Connect find toolbar events.
434 Connect(m_find_toolbar_done->GetId(), wxEVT_TOOL,
435 wxCommandEventHandler(WebFrame::OnFindDone), NULL, this );
436 Connect(m_find_toolbar_next->GetId(), wxEVT_TOOL,
437 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
438 Connect(m_find_toolbar_previous->GetId(), wxEVT_TOOL,
439 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
440
441 // Connect find control events.
442 Connect(m_find_ctrl->GetId(), wxEVT_TEXT,
443 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
444 Connect(m_find_ctrl->GetId(), wxEVT_TEXT_ENTER,
445 wxCommandEventHandler(WebFrame::OnFindText), NULL, this );
446
447 // Connect the webview events
448 Connect(m_browser->GetId(), wxEVT_WEBVIEW_NAVIGATING,
449 wxWebViewEventHandler(WebFrame::OnNavigationRequest), NULL, this);
450 Connect(m_browser->GetId(), wxEVT_WEBVIEW_NAVIGATED,
451 wxWebViewEventHandler(WebFrame::OnNavigationComplete), NULL, this);
452 Connect(m_browser->GetId(), wxEVT_WEBVIEW_LOADED,
453 wxWebViewEventHandler(WebFrame::OnDocumentLoaded), NULL, this);
454 Connect(m_browser->GetId(), wxEVT_WEBVIEW_ERROR,
455 wxWebViewEventHandler(WebFrame::OnError), NULL, this);
456 Connect(m_browser->GetId(), wxEVT_WEBVIEW_NEWWINDOW,
457 wxWebViewEventHandler(WebFrame::OnNewWindow), NULL, this);
458 Connect(m_browser->GetId(), wxEVT_WEBVIEW_TITLE_CHANGED,
459 wxWebViewEventHandler(WebFrame::OnTitleChanged), NULL, this);
460
461 // Connect the menu events
462 Connect(viewSource->GetId(), wxEVT_MENU,
463 wxCommandEventHandler(WebFrame::OnViewSourceRequest), NULL, this );
464 Connect(print->GetId(), wxEVT_MENU,
465 wxCommandEventHandler(WebFrame::OnPrint), NULL, this );
466 Connect(m_tools_layout->GetId(), wxEVT_MENU,
467 wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this );
468 Connect(m_tools_tiny->GetId(), wxEVT_MENU,
469 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
470 Connect(m_tools_small->GetId(), wxEVT_MENU,
471 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
472 Connect(m_tools_medium->GetId(), wxEVT_MENU,
473 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
474 Connect(m_tools_large->GetId(), wxEVT_MENU,
475 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
476 Connect(m_tools_largest->GetId(), wxEVT_MENU,
477 wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this );
478 Connect(clearhist->GetId(), wxEVT_MENU,
479 wxCommandEventHandler(WebFrame::OnClearHistory), NULL, this );
480 Connect(m_tools_enable_history->GetId(), wxEVT_MENU,
481 wxCommandEventHandler(WebFrame::OnEnableHistory), NULL, this );
482 Connect(m_edit_cut->GetId(), wxEVT_MENU,
483 wxCommandEventHandler(WebFrame::OnCut), NULL, this );
484 Connect(m_edit_copy->GetId(), wxEVT_MENU,
485 wxCommandEventHandler(WebFrame::OnCopy), NULL, this );
486 Connect(m_edit_paste->GetId(), wxEVT_MENU,
487 wxCommandEventHandler(WebFrame::OnPaste), NULL, this );
488 Connect(m_edit_undo->GetId(), wxEVT_MENU,
489 wxCommandEventHandler(WebFrame::OnUndo), NULL, this );
490 Connect(m_edit_redo->GetId(), wxEVT_MENU,
491 wxCommandEventHandler(WebFrame::OnRedo), NULL, this );
492 Connect(m_edit_mode->GetId(), wxEVT_MENU,
493 wxCommandEventHandler(WebFrame::OnMode), NULL, this );
494 Connect(m_scroll_line_up->GetId(), wxEVT_MENU,
495 wxCommandEventHandler(WebFrame::OnScrollLineUp), NULL, this );
496 Connect(m_scroll_line_down->GetId(), wxEVT_MENU,
497 wxCommandEventHandler(WebFrame::OnScrollLineDown), NULL, this );
498 Connect(m_scroll_page_up->GetId(), wxEVT_MENU,
499 wxCommandEventHandler(WebFrame::OnScrollPageUp), NULL, this );
500 Connect(m_scroll_page_down->GetId(), wxEVT_MENU,
501 wxCommandEventHandler(WebFrame::OnScrollPageDown), NULL, this );
502 Connect(script->GetId(), wxEVT_MENU,
503 wxCommandEventHandler(WebFrame::OnRunScript), NULL, this );
504 Connect(m_selection_clear->GetId(), wxEVT_MENU,
505 wxCommandEventHandler(WebFrame::OnClearSelection), NULL, this );
506 Connect(m_selection_delete->GetId(), wxEVT_MENU,
507 wxCommandEventHandler(WebFrame::OnDeleteSelection), NULL, this );
508 Connect(selectall->GetId(), wxEVT_MENU,
509 wxCommandEventHandler(WebFrame::OnSelectAll), NULL, this );
510 Connect(loadscheme->GetId(), wxEVT_MENU,
511 wxCommandEventHandler(WebFrame::OnLoadScheme), NULL, this );
512 Connect(usememoryfs->GetId(), wxEVT_MENU,
513 wxCommandEventHandler(WebFrame::OnUseMemoryFS), NULL, this );
514 Connect(m_find->GetId(), wxEVT_MENU,
515 wxCommandEventHandler(WebFrame::OnFind), NULL, this );
516 Connect(m_context_menu->GetId(), wxEVT_MENU,
517 wxCommandEventHandler(WebFrame::OnEnableContextMenu), NULL, this );
518
519 //Connect the idle events
520 Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(WebFrame::OnIdle), NULL, this);
521 }
522
523 WebFrame::~WebFrame()
524 {
525 delete m_tools_menu;
526 }
527
528 /**
529 * Method that retrieves the current state from the web control and updates the GUI
530 * the reflect this current state.
531 */
532 void WebFrame::UpdateState()
533 {
534 m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() );
535 m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() );
536
537 if (m_browser->IsBusy())
538 {
539 m_toolbar->EnableTool( m_toolbar_stop->GetId(), true );
540 }
541 else
542 {
543 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
544 }
545
546 SetTitle( m_browser->GetCurrentTitle() );
547 m_url->SetValue( m_browser->GetCurrentURL() );
548 }
549
550 void WebFrame::OnIdle(wxIdleEvent& WXUNUSED(evt))
551 {
552 if(m_browser->IsBusy())
553 {
554 wxSetCursor(wxCURSOR_ARROWWAIT);
555 m_toolbar->EnableTool(m_toolbar_stop->GetId(), true);
556 }
557 else
558 {
559 wxSetCursor(wxNullCursor);
560 m_toolbar->EnableTool(m_toolbar_stop->GetId(), false);
561 }
562 }
563
564 /**
565 * Callback invoked when user entered an URL and pressed enter
566 */
567 void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt))
568 {
569 m_browser->LoadURL( m_url->GetValue() );
570 m_browser->SetFocus();
571 UpdateState();
572 }
573
574 /**
575 * Callback invoked when user pressed the "back" button
576 */
577 void WebFrame::OnBack(wxCommandEvent& WXUNUSED(evt))
578 {
579 m_browser->GoBack();
580 UpdateState();
581 }
582
583 /**
584 * Callback invoked when user pressed the "forward" button
585 */
586 void WebFrame::OnForward(wxCommandEvent& WXUNUSED(evt))
587 {
588 m_browser->GoForward();
589 UpdateState();
590 }
591
592 /**
593 * Callback invoked when user pressed the "stop" button
594 */
595 void WebFrame::OnStop(wxCommandEvent& WXUNUSED(evt))
596 {
597 m_browser->Stop();
598 UpdateState();
599 }
600
601 /**
602 * Callback invoked when user pressed the "reload" button
603 */
604 void WebFrame::OnReload(wxCommandEvent& WXUNUSED(evt))
605 {
606 m_browser->Reload();
607 UpdateState();
608 }
609
610 void WebFrame::OnClearHistory(wxCommandEvent& WXUNUSED(evt))
611 {
612 m_browser->ClearHistory();
613 UpdateState();
614 }
615
616 void WebFrame::OnEnableHistory(wxCommandEvent& WXUNUSED(evt))
617 {
618 m_browser->EnableHistory(m_tools_enable_history->IsChecked());
619 UpdateState();
620 }
621
622 void WebFrame::OnCut(wxCommandEvent& WXUNUSED(evt))
623 {
624 m_browser->Cut();
625 }
626
627 void WebFrame::OnCopy(wxCommandEvent& WXUNUSED(evt))
628 {
629 m_browser->Copy();
630 }
631
632 void WebFrame::OnPaste(wxCommandEvent& WXUNUSED(evt))
633 {
634 m_browser->Paste();
635 }
636
637 void WebFrame::OnUndo(wxCommandEvent& WXUNUSED(evt))
638 {
639 m_browser->Undo();
640 }
641
642 void WebFrame::OnRedo(wxCommandEvent& WXUNUSED(evt))
643 {
644 m_browser->Redo();
645 }
646
647 void WebFrame::OnMode(wxCommandEvent& WXUNUSED(evt))
648 {
649 m_browser->SetEditable(m_edit_mode->IsChecked());
650 }
651
652 void WebFrame::OnLoadScheme(wxCommandEvent& WXUNUSED(evt))
653 {
654 wxFileName helpfile("../help/doc.zip");
655 helpfile.MakeAbsolute();
656 wxString path = helpfile.GetFullPath();
657 //Under MSW we need to flip the slashes
658 path.Replace("\\", "/");
659 path = "wxfs:///" + path + ";protocol=zip/doc.htm";
660 m_browser->LoadURL(path);
661 }
662
663 void WebFrame::OnUseMemoryFS(wxCommandEvent& WXUNUSED(evt))
664 {
665 m_browser->LoadURL("memory:page1.htm");
666 }
667
668 void WebFrame::OnEnableContextMenu(wxCommandEvent& evt)
669 {
670 m_browser->EnableContextMenu(evt.IsChecked());
671 }
672
673 void WebFrame::OnFind(wxCommandEvent& WXUNUSED(evt))
674 {
675 wxString value = m_browser->GetSelectedText();
676 if(value.Len() > 150)
677 {
678 value.Truncate(150);
679 }
680 m_find_ctrl->SetValue(value);
681 if(!m_find_toolbar->IsShown()){
682 m_find_toolbar->Show(true);
683 SendSizeEvent();
684 }
685 m_find_ctrl->SelectAll();
686 }
687
688 void WebFrame::OnFindDone(wxCommandEvent& WXUNUSED(evt))
689 {
690 m_browser->Find("");
691 m_find_toolbar->Show(false);
692 SendSizeEvent();
693 }
694
695 void WebFrame::OnFindText(wxCommandEvent& evt)
696 {
697 int flags = 0;
698
699 if(m_find_toolbar_wrap->IsChecked())
700 flags |= wxWEBVIEW_FIND_WRAP;
701 if(m_find_toolbar_wholeword->IsChecked())
702 flags |= wxWEBVIEW_FIND_ENTIRE_WORD;
703 if(m_find_toolbar_matchcase->IsChecked())
704 flags |= wxWEBVIEW_FIND_MATCH_CASE;
705 if(m_find_toolbar_highlight->IsChecked())
706 flags |= wxWEBVIEW_FIND_HIGHLIGHT_RESULT;
707
708 if(m_find_toolbar_previous->GetId() == evt.GetId())
709 flags |= wxWEBVIEW_FIND_BACKWARDS;
710
711 wxString find_text = m_find_ctrl->GetValue();
712 long count = m_browser->Find(find_text, flags);
713
714 if(m_findText != find_text)
715 {
716 m_findCount = count;
717 m_findText = find_text;
718 }
719
720 if(count != wxNOT_FOUND || find_text.IsEmpty())
721 {
722 m_find_ctrl->SetBackgroundColour(*wxWHITE);
723 }
724 else
725 {
726 m_find_ctrl->SetBackgroundColour(wxColour(255, 101, 101));
727 }
728
729 m_find_ctrl->Refresh();
730
731 //Log the result, note that count is zero indexed.
732 if(count != m_findCount)
733 {
734 count++;
735 }
736 wxLogMessage("Searching for:%s current match:%i/%i", m_findText.c_str(), count, m_findCount);
737 }
738
739 /**
740 * Callback invoked when there is a request to load a new page (for instance
741 * when the user clicks a link)
742 */
743 void WebFrame::OnNavigationRequest(wxWebViewEvent& evt)
744 {
745 if(m_info->IsShown())
746 {
747 m_info->Dismiss();
748 }
749
750 wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" +
751 evt.GetTarget() + "')");
752
753 wxASSERT(m_browser->IsBusy());
754
755 //If we don't want to handle navigation then veto the event and navigation
756 //will not take place, we also need to stop the loading animation
757 if(!m_tools_handle_navigation->IsChecked())
758 {
759 evt.Veto();
760 m_toolbar->EnableTool( m_toolbar_stop->GetId(), false );
761 }
762 else
763 {
764 UpdateState();
765 }
766 }
767
768 /**
769 * Callback invoked when a navigation request was accepted
770 */
771 void WebFrame::OnNavigationComplete(wxWebViewEvent& evt)
772 {
773 wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'");
774 UpdateState();
775 }
776
777 /**
778 * Callback invoked when a page is finished loading
779 */
780 void WebFrame::OnDocumentLoaded(wxWebViewEvent& evt)
781 {
782 //Only notify if the document is the main frame, not a subframe
783 if(evt.GetURL() == m_browser->GetCurrentURL())
784 {
785 wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'");
786 }
787 UpdateState();
788 }
789
790 /**
791 * On new window, we veto to stop extra windows appearing
792 */
793 void WebFrame::OnNewWindow(wxWebViewEvent& evt)
794 {
795 wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'");
796
797 //If we handle new window events then just load them in this window as we
798 //are a single window browser
799 if(m_tools_handle_new_window->IsChecked())
800 m_browser->LoadURL(evt.GetURL());
801
802 UpdateState();
803 }
804
805 void WebFrame::OnTitleChanged(wxWebViewEvent& evt)
806 {
807 SetTitle(evt.GetString());
808 wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'");
809 }
810
811 /**
812 * Invoked when user selects the "View Source" menu item
813 */
814 void WebFrame::OnViewSourceRequest(wxCommandEvent& WXUNUSED(evt))
815 {
816 SourceViewDialog dlg(this, m_browser->GetPageSource());
817 dlg.ShowModal();
818 }
819
820 /**
821 * Invoked when user selects the "Menu" item
822 */
823 void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
824 {
825 if(m_browser->GetCurrentURL() == "")
826 return;
827
828 m_tools_tiny->Check(false);
829 m_tools_small->Check(false);
830 m_tools_medium->Check(false);
831 m_tools_large->Check(false);
832 m_tools_largest->Check(false);
833
834 wxWebViewZoom zoom = m_browser->GetZoom();
835 switch (zoom)
836 {
837 case wxWEBVIEW_ZOOM_TINY:
838 m_tools_tiny->Check();
839 break;
840 case wxWEBVIEW_ZOOM_SMALL:
841 m_tools_small->Check();
842 break;
843 case wxWEBVIEW_ZOOM_MEDIUM:
844 m_tools_medium->Check();
845 break;
846 case wxWEBVIEW_ZOOM_LARGE:
847 m_tools_large->Check();
848 break;
849 case wxWEBVIEW_ZOOM_LARGEST:
850 m_tools_largest->Check();
851 break;
852 }
853
854 m_edit_cut->Enable(m_browser->CanCut());
855 m_edit_copy->Enable(m_browser->CanCopy());
856 m_edit_paste->Enable(m_browser->CanPaste());
857
858 m_edit_undo->Enable(m_browser->CanUndo());
859 m_edit_redo->Enable(m_browser->CanRedo());
860
861 m_selection_clear->Enable(m_browser->HasSelection());
862 m_selection_delete->Enable(m_browser->HasSelection());
863
864 m_context_menu->Check(m_browser->IsContextMenuEnabled());
865
866 //Firstly we clear the existing menu items, then we add the current ones
867 wxMenuHistoryMap::const_iterator it;
868 for( it = m_histMenuItems.begin(); it != m_histMenuItems.end(); ++it )
869 {
870 m_tools_history_menu->Destroy(it->first);
871 }
872 m_histMenuItems.clear();
873
874 wxVector<wxSharedPtr<wxWebViewHistoryItem> > back = m_browser->GetBackwardHistory();
875 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forward = m_browser->GetForwardHistory();
876
877 wxMenuItem* item;
878
879 unsigned int i;
880 for(i = 0; i < back.size(); i++)
881 {
882 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, back[i]->GetTitle());
883 m_histMenuItems[item->GetId()] = back[i];
884 Connect(item->GetId(), wxEVT_MENU,
885 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
886 }
887
888 wxString title = m_browser->GetCurrentTitle();
889 if ( title.empty() )
890 title = "(untitled)";
891 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, title);
892 item->Check();
893
894 //No need to connect the current item
895 m_histMenuItems[item->GetId()] = wxSharedPtr<wxWebViewHistoryItem>(new wxWebViewHistoryItem(m_browser->GetCurrentURL(), m_browser->GetCurrentTitle()));
896
897 for(i = 0; i < forward.size(); i++)
898 {
899 item = m_tools_history_menu->AppendRadioItem(wxID_ANY, forward[i]->GetTitle());
900 m_histMenuItems[item->GetId()] = forward[i];
901 Connect(item->GetId(), wxEVT_TOOL,
902 wxCommandEventHandler(WebFrame::OnHistory), NULL, this );
903 }
904
905 wxPoint position = ScreenToClient( wxGetMousePosition() );
906 PopupMenu(m_tools_menu, position.x, position.y);
907 }
908
909 /**
910 * Invoked when user selects the zoom size in the menu
911 */
912 void WebFrame::OnSetZoom(wxCommandEvent& evt)
913 {
914 if (evt.GetId() == m_tools_tiny->GetId())
915 {
916 m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY);
917 }
918 else if (evt.GetId() == m_tools_small->GetId())
919 {
920 m_browser->SetZoom(wxWEBVIEW_ZOOM_SMALL);
921 }
922 else if (evt.GetId() == m_tools_medium->GetId())
923 {
924 m_browser->SetZoom(wxWEBVIEW_ZOOM_MEDIUM);
925 }
926 else if (evt.GetId() == m_tools_large->GetId())
927 {
928 m_browser->SetZoom(wxWEBVIEW_ZOOM_LARGE);
929 }
930 else if (evt.GetId() == m_tools_largest->GetId())
931 {
932 m_browser->SetZoom(wxWEBVIEW_ZOOM_LARGEST);
933 }
934 else
935 {
936 wxFAIL;
937 }
938 }
939
940 void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt))
941 {
942 if(m_tools_layout->IsChecked())
943 m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT);
944 else
945 m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT);
946 }
947
948 void WebFrame::OnHistory(wxCommandEvent& evt)
949 {
950 m_browser->LoadHistoryItem(m_histMenuItems[evt.GetId()]);
951 }
952
953 void WebFrame::OnRunScript(wxCommandEvent& WXUNUSED(evt))
954 {
955 wxTextEntryDialog dialog(this, "Enter JavaScript to run.", wxGetTextFromUserPromptStr, "", wxOK|wxCANCEL|wxCENTRE|wxTE_MULTILINE);
956 if(dialog.ShowModal() == wxID_OK)
957 {
958 m_browser->RunScript(dialog.GetValue());
959 }
960 }
961
962 void WebFrame::OnClearSelection(wxCommandEvent& WXUNUSED(evt))
963 {
964 m_browser->ClearSelection();
965 }
966
967 void WebFrame::OnDeleteSelection(wxCommandEvent& WXUNUSED(evt))
968 {
969 m_browser->DeleteSelection();
970 }
971
972 void WebFrame::OnSelectAll(wxCommandEvent& WXUNUSED(evt))
973 {
974 m_browser->SelectAll();
975 }
976
977 /**
978 * Callback invoked when a loading error occurs
979 */
980 void WebFrame::OnError(wxWebViewEvent& evt)
981 {
982 #define WX_ERROR_CASE(type) \
983 case type: \
984 category = #type; \
985 break;
986
987 wxString category;
988 switch (evt.GetInt())
989 {
990 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CONNECTION);
991 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CERTIFICATE);
992 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_AUTH);
993 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_SECURITY);
994 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_NOT_FOUND);
995 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_REQUEST);
996 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_USER_CANCELLED);
997 WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_OTHER);
998 }
999
1000 wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + category + " (" + evt.GetString() + ")'");
1001
1002 //Show the info bar with an error
1003 m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" +
1004 "'" + category + "'", wxICON_ERROR);
1005
1006 UpdateState();
1007 }
1008
1009 /**
1010 * Invoked when user selects "Print" from the menu
1011 */
1012 void WebFrame::OnPrint(wxCommandEvent& WXUNUSED(evt))
1013 {
1014 m_browser->Print();
1015 }
1016
1017 SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
1018 wxDialog(parent, wxID_ANY, "Source Code",
1019 wxDefaultPosition, wxSize(700,500),
1020 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
1021 {
1022 wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY);
1023 text->SetMarginWidth(1, 30);
1024 text->SetMarginType(1, wxSTC_MARGIN_NUMBER);
1025 text->SetText(source);
1026
1027 text->StyleClearAll();
1028 text->SetLexer(wxSTC_LEX_HTML);
1029 text->StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(255,0,0));
1030 text->StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(255,0,0));
1031 text->StyleSetForeground(wxSTC_H_ENTITY, wxColour(255,0,0));
1032 text->StyleSetForeground(wxSTC_H_TAG, wxColour(0,150,0));
1033 text->StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(0,150,0));
1034 text->StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,0,150));
1035 text->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(0,0,150));
1036 text->StyleSetForeground(wxSTC_H_COMMENT, wxColour(150,150,150));
1037
1038 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
1039 sizer->Add(text, 1, wxEXPAND);
1040 SetSizer(sizer);
1041 }