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