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