]>
Commit | Line | Data |
---|---|---|
61b98a2d | 1 | ///////////////////////////////////////////////////////////////////////////// |
467d261e | 2 | // Name: webview.cpp |
ec4ac0f7 | 3 | // Purpose: wxWebView sample |
61b98a2d | 4 | // Author: Marianne Gagnon |
ec4ac0f7 | 5 | // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton |
61b98a2d SL |
6 | // Licence: wxWindows licence |
7 | ///////////////////////////////////////////////////////////////////////////// | |
abcb9c6e | 8 | |
ec4ac0f7 SL |
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 | ||
4794f127 SL |
24 | #if !wxUSE_WEBVIEW_WEBKIT && !wxUSE_WEBVIEW_IE |
25 | #error "A wxWebView backend is required by this sample" | |
26 | #endif | |
27 | ||
b8a14a17 | 28 | #include "wx/artprov.h" |
0eb47013 | 29 | #include "wx/cmdline.h" |
b8a14a17 SC |
30 | #include "wx/notifmsg.h" |
31 | #include "wx/settings.h" | |
32 | #include "wx/webview.h" | |
7d8d6163 | 33 | #include "wx/webviewarchivehandler.h" |
0bfd90b3 | 34 | #include "wx/webviewfshandler.h" |
b8a14a17 SC |
35 | #include "wx/infobar.h" |
36 | #include "wx/filesys.h" | |
37 | #include "wx/fs_arc.h" | |
0bfd90b3 | 38 | #include "wx/fs_mem.h" |
ec4ac0f7 | 39 | |
e7092398 | 40 | #ifndef wxHAS_IMAGES_IN_RESOURCES |
ec4ac0f7 SL |
41 | #include "../sample.xpm" |
42 | #endif | |
61b98a2d SL |
43 | |
44 | #if wxUSE_STC | |
b8a14a17 | 45 | #include "wx/stc/stc.h" |
61b98a2d SL |
46 | #else |
47 | #error "wxStyledTextControl is needed by this sample" | |
48 | #endif | |
49 | ||
8bccab8f | 50 | #if defined(__WXMSW__) || defined(__WXOSX__) |
61b98a2d SL |
51 | #include "stop.xpm" |
52 | #include "refresh.xpm" | |
8bccab8f SL |
53 | #endif |
54 | ||
dffce0bd | 55 | #include "wxlogo.xpm" |
61b98a2d | 56 | |
10ad4ba6 SL |
57 | |
58 | //We map menu items to their history items | |
c13d6ac1 | 59 | WX_DECLARE_HASH_MAP(int, wxSharedPtr<wxWebViewHistoryItem>, |
10ad4ba6 SL |
60 | wxIntegerHash, wxIntegerEqual, wxMenuHistoryMap); |
61 | ||
ec4ac0f7 | 62 | class WebApp : public wxApp |
61b98a2d SL |
63 | { |
64 | public: | |
0eb47013 VZ |
65 | WebApp() : |
66 | m_url("http://www.wxwidgets.org") | |
67 | { | |
68 | } | |
69 | ||
ec4ac0f7 | 70 | virtual bool OnInit(); |
0eb47013 VZ |
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; | |
61b98a2d SL |
96 | }; |
97 | ||
ec4ac0f7 | 98 | class WebFrame : public wxFrame |
61b98a2d | 99 | { |
ec4ac0f7 | 100 | public: |
0eb47013 VZ |
101 | WebFrame(const wxString& url); |
102 | virtual ~WebFrame(); | |
ec4ac0f7 | 103 | |
ec4ac0f7 | 104 | void UpdateState(); |
a1788524 | 105 | void OnIdle(wxIdleEvent& evt); |
ec4ac0f7 SL |
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); | |
152a5808 SL |
111 | void OnClearHistory(wxCommandEvent& evt); |
112 | void OnEnableHistory(wxCommandEvent& evt); | |
04fa04d8 SL |
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); | |
ec4ac0f7 SL |
118 | void OnViewSourceRequest(wxCommandEvent& evt); |
119 | void OnToolsClicked(wxCommandEvent& evt); | |
120 | void OnSetZoom(wxCommandEvent& evt); | |
04fa04d8 | 121 | void OnError(wxWebViewEvent& evt); |
ec4ac0f7 | 122 | void OnPrint(wxCommandEvent& evt); |
4681a3ea SL |
123 | void OnCut(wxCommandEvent& evt); |
124 | void OnCopy(wxCommandEvent& evt); | |
125 | void OnPaste(wxCommandEvent& evt); | |
97e49559 SL |
126 | void OnUndo(wxCommandEvent& evt); |
127 | void OnRedo(wxCommandEvent& evt); | |
c7cbe308 | 128 | void OnMode(wxCommandEvent& evt); |
87d482ec | 129 | void OnZoomLayout(wxCommandEvent& evt); |
10ad4ba6 | 130 | void OnHistory(wxCommandEvent& evt); |
1f7c17f4 VZ |
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(); } | |
54883129 | 135 | void OnRunScript(wxCommandEvent& evt); |
603cfe42 SL |
136 | void OnClearSelection(wxCommandEvent& evt); |
137 | void OnDeleteSelection(wxCommandEvent& evt); | |
138 | void OnSelectAll(wxCommandEvent& evt); | |
49f07bec | 139 | void OnLoadScheme(wxCommandEvent& evt); |
0bfd90b3 | 140 | void OnUseMemoryFS(wxCommandEvent& evt); |
66ac0400 SL |
141 | void OnFind(wxCommandEvent& evt); |
142 | void OnFindDone(wxCommandEvent& evt); | |
143 | void OnFindText(wxCommandEvent& evt); | |
144 | void OnFindOptions(wxCommandEvent& evt); | |
c420d57b | 145 | void OnEnableContextMenu(wxCommandEvent& evt); |
ec4ac0f7 SL |
146 | |
147 | private: | |
148 | wxTextCtrl* m_url; | |
149 | wxWebView* m_browser; | |
150 | ||
61b98a2d | 151 | wxToolBar* m_toolbar; |
ec4ac0f7 SL |
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 | ||
66ac0400 SL |
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 | ||
ec4ac0f7 | 167 | wxMenu* m_tools_menu; |
10ad4ba6 | 168 | wxMenu* m_tools_history_menu; |
87d482ec | 169 | wxMenuItem* m_tools_layout; |
ec4ac0f7 SL |
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; | |
62e8551f SL |
175 | wxMenuItem* m_tools_handle_navigation; |
176 | wxMenuItem* m_tools_handle_new_window; | |
152a5808 | 177 | wxMenuItem* m_tools_enable_history; |
4681a3ea SL |
178 | wxMenuItem* m_edit_cut; |
179 | wxMenuItem* m_edit_copy; | |
180 | wxMenuItem* m_edit_paste; | |
97e49559 SL |
181 | wxMenuItem* m_edit_undo; |
182 | wxMenuItem* m_edit_redo; | |
c7cbe308 | 183 | wxMenuItem* m_edit_mode; |
1f7c17f4 VZ |
184 | wxMenuItem* m_scroll_line_up; |
185 | wxMenuItem* m_scroll_line_down; | |
186 | wxMenuItem* m_scroll_page_up; | |
187 | wxMenuItem* m_scroll_page_down; | |
603cfe42 SL |
188 | wxMenuItem* m_selection_clear; |
189 | wxMenuItem* m_selection_delete; | |
66ac0400 | 190 | wxMenuItem* m_find; |
c420d57b | 191 | wxMenuItem* m_context_menu; |
ec4ac0f7 | 192 | |
ec4ac0f7 SL |
193 | wxInfoBar *m_info; |
194 | wxStaticText* m_info_text; | |
66ac0400 SL |
195 | wxTextCtrl* m_find_ctrl; |
196 | wxToolBar* m_find_toolbar; | |
10ad4ba6 SL |
197 | |
198 | wxMenuHistoryMap m_histMenuItems; | |
66ac0400 SL |
199 | wxString m_findText; |
200 | int m_findFlags, m_findCount; | |
ec4ac0f7 SL |
201 | }; |
202 | ||
203 | class SourceViewDialog : public wxDialog | |
204 | { | |
61b98a2d | 205 | public: |
ec4ac0f7 SL |
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 | ||
0bfd90b3 SL |
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 | ||
0eb47013 | 241 | WebFrame *frame = new WebFrame(m_url); |
ec4ac0f7 SL |
242 | frame->Show(); |
243 | ||
244 | return true; | |
245 | } | |
246 | ||
0eb47013 VZ |
247 | WebFrame::WebFrame(const wxString& url) : |
248 | wxFrame(NULL, wxID_ANY, "wxWebView Sample") | |
ec4ac0f7 SL |
249 | { |
250 | // set the frame icon | |
251 | SetIcon(wxICON(sample)); | |
252 | SetTitle("wxWebView Sample"); | |
253 | ||
ec4ac0f7 SL |
254 | wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL); |
255 | ||
256 | // Create the toolbar | |
257 | m_toolbar = CreateToolBar(wxTB_TEXT); | |
258 | m_toolbar->SetToolBitmapSize(wxSize(32, 32)); | |
abcb9c6e | 259 | |
dffce0bd SL |
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); | |
abcb9c6e | 264 | #else |
dffce0bd SL |
265 | wxBitmap stop = wxBitmap(stop_xpm); |
266 | #endif | |
267 | #ifdef __WXGTK__ | |
268 | wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR); | |
abcb9c6e | 269 | #else |
dffce0bd SL |
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); | |
ec4ac0f7 | 277 | m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER ); |
9a00d078 | 278 | m_toolbar->AddControl(m_url, _("URL")); |
ec4ac0f7 SL |
279 | m_toolbar_tools = m_toolbar->AddTool(wxID_ANY, _("Menu"), wxBitmap(wxlogo_xpm)); |
280 | ||
281 | m_toolbar->Realize(); | |
282 | ||
66ac0400 | 283 | // Set find values. |
236cff73 | 284 | m_findFlags = wxWEBVIEW_FIND_DEFAULT; |
66ac0400 SL |
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 | ||
ec4ac0f7 SL |
324 | // Create the info panel |
325 | m_info = new wxInfoBar(this); | |
326 | topsizer->Add(m_info, wxSizerFlags().Expand()); | |
327 | ||
328 | // Create the webview | |
0eb47013 | 329 | m_browser = wxWebView::New(this, wxID_ANY, url); |
ec4ac0f7 SL |
330 | topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1)); |
331 | ||
7d8d6163 SL |
332 | //We register the wxfs:// protocol for testing purposes |
333 | m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs"))); | |
0bfd90b3 SL |
334 | //And the memory: file system |
335 | m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewFSHandler("memory"))); | |
eff8f795 | 336 | |
ec4ac0f7 SL |
337 | SetSizer(topsizer); |
338 | ||
9a00d078 SL |
339 | //Set a more sensible size for web browsing |
340 | SetSize(wxSize(800, 600)); | |
341 | ||
ec4ac0f7 | 342 | // Create a log window |
be2878d8 | 343 | new wxLogWindow(this, _("Logging"), true, false); |
ec4ac0f7 SL |
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(); | |
87d482ec | 350 | m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom")); |
ec4ac0f7 SL |
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")); | |
62e8551f SL |
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")); | |
152a5808 | 359 | m_tools_menu->AppendSeparator(); |
10ad4ba6 | 360 | |
66ac0400 SL |
361 | //Find |
362 | m_find = m_tools_menu->Append(wxID_ANY, _("Find")); | |
363 | m_tools_menu->AppendSeparator(); | |
364 | ||
10ad4ba6 SL |
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 | ||
54883129 | 371 | m_tools_menu->AppendSubMenu(m_tools_history_menu, "History"); |
62e8551f | 372 | |
4681a3ea SL |
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")); | |
97e49559 SL |
378 | editmenu->AppendSeparator(); |
379 | m_edit_undo = editmenu->Append(wxID_ANY, _("Undo")); | |
380 | m_edit_redo = editmenu->Append(wxID_ANY, _("Redo")); | |
c7cbe308 SL |
381 | editmenu->AppendSeparator(); |
382 | m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode")); | |
4681a3ea SL |
383 | |
384 | m_tools_menu->AppendSeparator(); | |
385 | m_tools_menu->AppendSubMenu(editmenu, "Edit"); | |
386 | ||
1f7c17f4 VZ |
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 | ||
54883129 SL |
394 | wxMenuItem* script = m_tools_menu->Append(wxID_ANY, _("Run Script")); |
395 | ||
603cfe42 SL |
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 | ||
49f07bec | 404 | wxMenuItem* loadscheme = m_tools_menu->Append(wxID_ANY, _("Custom Scheme Example")); |
0bfd90b3 | 405 | wxMenuItem* usememoryfs = m_tools_menu->Append(wxID_ANY, _("Memory File System Example")); |
49f07bec | 406 | |
c420d57b SL |
407 | m_context_menu = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable Context Menu")); |
408 | ||
61635eed | 409 | //By default we want to handle navigation and new windows |
62e8551f SL |
410 | m_tools_handle_navigation->Check(); |
411 | m_tools_handle_new_window->Check(); | |
152a5808 | 412 | m_tools_enable_history->Check(); |
236cff73 | 413 | if(!m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT)) |
87d482ec | 414 | m_tools_layout->Enable(false); |
ec4ac0f7 SL |
415 | |
416 | ||
417 | // Connect the toolbar events | |
ce7fe42e | 418 | Connect(m_toolbar_back->GetId(), wxEVT_TOOL, |
ec4ac0f7 | 419 | wxCommandEventHandler(WebFrame::OnBack), NULL, this ); |
ce7fe42e | 420 | Connect(m_toolbar_forward->GetId(), wxEVT_TOOL, |
ec4ac0f7 | 421 | wxCommandEventHandler(WebFrame::OnForward), NULL, this ); |
ce7fe42e | 422 | Connect(m_toolbar_stop->GetId(), wxEVT_TOOL, |
ec4ac0f7 | 423 | wxCommandEventHandler(WebFrame::OnStop), NULL, this ); |
ce7fe42e | 424 | Connect(m_toolbar_reload->GetId(), wxEVT_TOOL, |
ec4ac0f7 | 425 | wxCommandEventHandler(WebFrame::OnReload),NULL, this ); |
ce7fe42e | 426 | Connect(m_toolbar_tools->GetId(), wxEVT_TOOL, |
ec4ac0f7 SL |
427 | wxCommandEventHandler(WebFrame::OnToolsClicked), NULL, this ); |
428 | ||
ce7fe42e | 429 | Connect(m_url->GetId(), wxEVT_TEXT_ENTER, |
ec4ac0f7 SL |
430 | wxCommandEventHandler(WebFrame::OnUrl), NULL, this ); |
431 | ||
66ac0400 | 432 | // Connect find toolbar events. |
ce7fe42e | 433 | Connect(m_find_toolbar_done->GetId(), wxEVT_TOOL, |
66ac0400 | 434 | wxCommandEventHandler(WebFrame::OnFindDone), NULL, this ); |
ce7fe42e | 435 | Connect(m_find_toolbar_next->GetId(), wxEVT_TOOL, |
66ac0400 | 436 | wxCommandEventHandler(WebFrame::OnFindText), NULL, this ); |
ce7fe42e | 437 | Connect(m_find_toolbar_previous->GetId(), wxEVT_TOOL, |
66ac0400 SL |
438 | wxCommandEventHandler(WebFrame::OnFindText), NULL, this ); |
439 | ||
440 | // Connect find control events. | |
ce7fe42e | 441 | Connect(m_find_ctrl->GetId(), wxEVT_TEXT, |
66ac0400 | 442 | wxCommandEventHandler(WebFrame::OnFindText), NULL, this ); |
ce7fe42e | 443 | Connect(m_find_ctrl->GetId(), wxEVT_TEXT_ENTER, |
66ac0400 SL |
444 | wxCommandEventHandler(WebFrame::OnFindText), NULL, this ); |
445 | ||
ec4ac0f7 | 446 | // Connect the webview events |
ce7fe42e | 447 | Connect(m_browser->GetId(), wxEVT_WEBVIEW_NAVIGATING, |
04fa04d8 | 448 | wxWebViewEventHandler(WebFrame::OnNavigationRequest), NULL, this); |
ce7fe42e | 449 | Connect(m_browser->GetId(), wxEVT_WEBVIEW_NAVIGATED, |
04fa04d8 | 450 | wxWebViewEventHandler(WebFrame::OnNavigationComplete), NULL, this); |
ce7fe42e | 451 | Connect(m_browser->GetId(), wxEVT_WEBVIEW_LOADED, |
abcb9c6e | 452 | wxWebViewEventHandler(WebFrame::OnDocumentLoaded), NULL, this); |
ce7fe42e | 453 | Connect(m_browser->GetId(), wxEVT_WEBVIEW_ERROR, |
04fa04d8 | 454 | wxWebViewEventHandler(WebFrame::OnError), NULL, this); |
ce7fe42e | 455 | Connect(m_browser->GetId(), wxEVT_WEBVIEW_NEWWINDOW, |
04fa04d8 | 456 | wxWebViewEventHandler(WebFrame::OnNewWindow), NULL, this); |
ce7fe42e | 457 | Connect(m_browser->GetId(), wxEVT_WEBVIEW_TITLE_CHANGED, |
04fa04d8 | 458 | wxWebViewEventHandler(WebFrame::OnTitleChanged), NULL, this); |
ec4ac0f7 SL |
459 | |
460 | // Connect the menu events | |
ce7fe42e | 461 | Connect(viewSource->GetId(), wxEVT_MENU, |
ec4ac0f7 | 462 | wxCommandEventHandler(WebFrame::OnViewSourceRequest), NULL, this ); |
ce7fe42e | 463 | Connect(print->GetId(), wxEVT_MENU, |
ec4ac0f7 | 464 | wxCommandEventHandler(WebFrame::OnPrint), NULL, this ); |
ce7fe42e | 465 | Connect(m_tools_layout->GetId(), wxEVT_MENU, |
87d482ec | 466 | wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this ); |
ce7fe42e | 467 | Connect(m_tools_tiny->GetId(), wxEVT_MENU, |
ec4ac0f7 | 468 | wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this ); |
ce7fe42e | 469 | Connect(m_tools_small->GetId(), wxEVT_MENU, |
ec4ac0f7 | 470 | wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this ); |
ce7fe42e | 471 | Connect(m_tools_medium->GetId(), wxEVT_MENU, |
ec4ac0f7 | 472 | wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this ); |
ce7fe42e | 473 | Connect(m_tools_large->GetId(), wxEVT_MENU, |
ec4ac0f7 | 474 | wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this ); |
ce7fe42e | 475 | Connect(m_tools_largest->GetId(), wxEVT_MENU, |
ec4ac0f7 | 476 | wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this ); |
ce7fe42e | 477 | Connect(clearhist->GetId(), wxEVT_MENU, |
152a5808 | 478 | wxCommandEventHandler(WebFrame::OnClearHistory), NULL, this ); |
ce7fe42e | 479 | Connect(m_tools_enable_history->GetId(), wxEVT_MENU, |
152a5808 | 480 | wxCommandEventHandler(WebFrame::OnEnableHistory), NULL, this ); |
ce7fe42e | 481 | Connect(m_edit_cut->GetId(), wxEVT_MENU, |
4681a3ea | 482 | wxCommandEventHandler(WebFrame::OnCut), NULL, this ); |
ce7fe42e | 483 | Connect(m_edit_copy->GetId(), wxEVT_MENU, |
4681a3ea | 484 | wxCommandEventHandler(WebFrame::OnCopy), NULL, this ); |
ce7fe42e | 485 | Connect(m_edit_paste->GetId(), wxEVT_MENU, |
4681a3ea | 486 | wxCommandEventHandler(WebFrame::OnPaste), NULL, this ); |
ce7fe42e | 487 | Connect(m_edit_undo->GetId(), wxEVT_MENU, |
97e49559 | 488 | wxCommandEventHandler(WebFrame::OnUndo), NULL, this ); |
ce7fe42e | 489 | Connect(m_edit_redo->GetId(), wxEVT_MENU, |
97e49559 | 490 | wxCommandEventHandler(WebFrame::OnRedo), NULL, this ); |
ce7fe42e | 491 | Connect(m_edit_mode->GetId(), wxEVT_MENU, |
c7cbe308 | 492 | wxCommandEventHandler(WebFrame::OnMode), NULL, this ); |
ce7fe42e | 493 | Connect(m_scroll_line_up->GetId(), wxEVT_MENU, |
1f7c17f4 | 494 | wxCommandEventHandler(WebFrame::OnScrollLineUp), NULL, this ); |
ce7fe42e | 495 | Connect(m_scroll_line_down->GetId(), wxEVT_MENU, |
1f7c17f4 | 496 | wxCommandEventHandler(WebFrame::OnScrollLineDown), NULL, this ); |
ce7fe42e | 497 | Connect(m_scroll_page_up->GetId(), wxEVT_MENU, |
1f7c17f4 | 498 | wxCommandEventHandler(WebFrame::OnScrollPageUp), NULL, this ); |
ce7fe42e | 499 | Connect(m_scroll_page_down->GetId(), wxEVT_MENU, |
1f7c17f4 | 500 | wxCommandEventHandler(WebFrame::OnScrollPageDown), NULL, this ); |
ce7fe42e | 501 | Connect(script->GetId(), wxEVT_MENU, |
54883129 | 502 | wxCommandEventHandler(WebFrame::OnRunScript), NULL, this ); |
ce7fe42e | 503 | Connect(m_selection_clear->GetId(), wxEVT_MENU, |
603cfe42 | 504 | wxCommandEventHandler(WebFrame::OnClearSelection), NULL, this ); |
ce7fe42e | 505 | Connect(m_selection_delete->GetId(), wxEVT_MENU, |
603cfe42 | 506 | wxCommandEventHandler(WebFrame::OnDeleteSelection), NULL, this ); |
ce7fe42e | 507 | Connect(selectall->GetId(), wxEVT_MENU, |
603cfe42 | 508 | wxCommandEventHandler(WebFrame::OnSelectAll), NULL, this ); |
ce7fe42e | 509 | Connect(loadscheme->GetId(), wxEVT_MENU, |
49f07bec | 510 | wxCommandEventHandler(WebFrame::OnLoadScheme), NULL, this ); |
ce7fe42e | 511 | Connect(usememoryfs->GetId(), wxEVT_MENU, |
0bfd90b3 | 512 | wxCommandEventHandler(WebFrame::OnUseMemoryFS), NULL, this ); |
ce7fe42e | 513 | Connect(m_find->GetId(), wxEVT_MENU, |
66ac0400 | 514 | wxCommandEventHandler(WebFrame::OnFind), NULL, this ); |
ce7fe42e | 515 | Connect(m_context_menu->GetId(), wxEVT_MENU, |
c420d57b | 516 | wxCommandEventHandler(WebFrame::OnEnableContextMenu), NULL, this ); |
a1788524 SL |
517 | |
518 | //Connect the idle events | |
519 | Connect(wxID_ANY, wxEVT_IDLE, wxIdleEventHandler(WebFrame::OnIdle), NULL, this); | |
ec4ac0f7 SL |
520 | } |
521 | ||
7892e035 SL |
522 | WebFrame::~WebFrame() |
523 | { | |
524 | delete m_tools_menu; | |
525 | } | |
526 | ||
ec4ac0f7 SL |
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() ); | |
abcb9c6e | 535 | |
ec4ac0f7 | 536 | if (m_browser->IsBusy()) |
61b98a2d | 537 | { |
abcb9c6e | 538 | m_toolbar->EnableTool( m_toolbar_stop->GetId(), true ); |
61b98a2d | 539 | } |
ec4ac0f7 | 540 | else |
61b98a2d | 541 | { |
abcb9c6e | 542 | m_toolbar->EnableTool( m_toolbar_stop->GetId(), false ); |
61b98a2d | 543 | } |
abcb9c6e | 544 | |
ec4ac0f7 SL |
545 | SetTitle( m_browser->GetCurrentTitle() ); |
546 | m_url->SetValue( m_browser->GetCurrentURL() ); | |
547 | } | |
548 | ||
c49d2434 | 549 | void WebFrame::OnIdle(wxIdleEvent& WXUNUSED(evt)) |
a1788524 SL |
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 | ||
ec4ac0f7 SL |
563 | /** |
564 | * Callback invoked when user entered an URL and pressed enter | |
565 | */ | |
8bccab8f | 566 | void WebFrame::OnUrl(wxCommandEvent& WXUNUSED(evt)) |
ec4ac0f7 | 567 | { |
4d0dddc7 | 568 | m_browser->LoadURL( m_url->GetValue() ); |
c49d2434 | 569 | m_browser->SetFocus(); |
ec4ac0f7 SL |
570 | UpdateState(); |
571 | } | |
572 | ||
573 | /** | |
574 | * Callback invoked when user pressed the "back" button | |
575 | */ | |
8bccab8f | 576 | void WebFrame::OnBack(wxCommandEvent& WXUNUSED(evt)) |
ec4ac0f7 SL |
577 | { |
578 | m_browser->GoBack(); | |
579 | UpdateState(); | |
580 | } | |
581 | ||
582 | /** | |
583 | * Callback invoked when user pressed the "forward" button | |
584 | */ | |
8bccab8f | 585 | void WebFrame::OnForward(wxCommandEvent& WXUNUSED(evt)) |
ec4ac0f7 SL |
586 | { |
587 | m_browser->GoForward(); | |
588 | UpdateState(); | |
589 | } | |
590 | ||
591 | /** | |
592 | * Callback invoked when user pressed the "stop" button | |
593 | */ | |
8bccab8f | 594 | void WebFrame::OnStop(wxCommandEvent& WXUNUSED(evt)) |
ec4ac0f7 SL |
595 | { |
596 | m_browser->Stop(); | |
597 | UpdateState(); | |
598 | } | |
599 | ||
600 | /** | |
601 | * Callback invoked when user pressed the "reload" button | |
602 | */ | |
8bccab8f | 603 | void WebFrame::OnReload(wxCommandEvent& WXUNUSED(evt)) |
ec4ac0f7 SL |
604 | { |
605 | m_browser->Reload(); | |
606 | UpdateState(); | |
607 | } | |
608 | ||
8bccab8f | 609 | void WebFrame::OnClearHistory(wxCommandEvent& WXUNUSED(evt)) |
152a5808 SL |
610 | { |
611 | m_browser->ClearHistory(); | |
612 | UpdateState(); | |
613 | } | |
614 | ||
8bccab8f | 615 | void WebFrame::OnEnableHistory(wxCommandEvent& WXUNUSED(evt)) |
152a5808 SL |
616 | { |
617 | m_browser->EnableHistory(m_tools_enable_history->IsChecked()); | |
618 | UpdateState(); | |
619 | } | |
620 | ||
8bccab8f | 621 | void WebFrame::OnCut(wxCommandEvent& WXUNUSED(evt)) |
4681a3ea SL |
622 | { |
623 | m_browser->Cut(); | |
624 | } | |
625 | ||
8bccab8f | 626 | void WebFrame::OnCopy(wxCommandEvent& WXUNUSED(evt)) |
4681a3ea SL |
627 | { |
628 | m_browser->Copy(); | |
629 | } | |
630 | ||
8bccab8f | 631 | void WebFrame::OnPaste(wxCommandEvent& WXUNUSED(evt)) |
4681a3ea SL |
632 | { |
633 | m_browser->Paste(); | |
634 | } | |
635 | ||
8bccab8f | 636 | void WebFrame::OnUndo(wxCommandEvent& WXUNUSED(evt)) |
97e49559 SL |
637 | { |
638 | m_browser->Undo(); | |
639 | } | |
640 | ||
8bccab8f | 641 | void WebFrame::OnRedo(wxCommandEvent& WXUNUSED(evt)) |
97e49559 SL |
642 | { |
643 | m_browser->Redo(); | |
644 | } | |
645 | ||
8bccab8f | 646 | void WebFrame::OnMode(wxCommandEvent& WXUNUSED(evt)) |
c7cbe308 SL |
647 | { |
648 | m_browser->SetEditable(m_edit_mode->IsChecked()); | |
649 | } | |
650 | ||
49f07bec SL |
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"; | |
4d0dddc7 | 659 | m_browser->LoadURL(path); |
66ac0400 SL |
660 | } |
661 | ||
0bfd90b3 SL |
662 | void WebFrame::OnUseMemoryFS(wxCommandEvent& WXUNUSED(evt)) |
663 | { | |
664 | m_browser->LoadURL("memory:page1.htm"); | |
665 | } | |
666 | ||
c420d57b SL |
667 | void WebFrame::OnEnableContextMenu(wxCommandEvent& evt) |
668 | { | |
669 | m_browser->EnableContextMenu(evt.IsChecked()); | |
670 | } | |
671 | ||
66ac0400 SL |
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()) | |
236cff73 | 699 | flags |= wxWEBVIEW_FIND_WRAP; |
66ac0400 | 700 | if(m_find_toolbar_wholeword->IsChecked()) |
236cff73 | 701 | flags |= wxWEBVIEW_FIND_ENTIRE_WORD; |
66ac0400 | 702 | if(m_find_toolbar_matchcase->IsChecked()) |
236cff73 | 703 | flags |= wxWEBVIEW_FIND_MATCH_CASE; |
66ac0400 | 704 | if(m_find_toolbar_highlight->IsChecked()) |
236cff73 | 705 | flags |= wxWEBVIEW_FIND_HIGHLIGHT_RESULT; |
66ac0400 SL |
706 | |
707 | if(m_find_toolbar_previous->GetId() == evt.GetId()) | |
236cff73 | 708 | flags |= wxWEBVIEW_FIND_BACKWARDS; |
66ac0400 SL |
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); | |
49f07bec | 736 | } |
c7cbe308 | 737 | |
ec4ac0f7 SL |
738 | /** |
739 | * Callback invoked when there is a request to load a new page (for instance | |
740 | * when the user clicks a link) | |
741 | */ | |
04fa04d8 | 742 | void WebFrame::OnNavigationRequest(wxWebViewEvent& evt) |
ec4ac0f7 | 743 | { |
75b0b0bc SL |
744 | if(m_info->IsShown()) |
745 | { | |
70544c1e | 746 | m_info->Dismiss(); |
75b0b0bc SL |
747 | } |
748 | ||
e40741b9 | 749 | wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" + |
ec4ac0f7 | 750 | evt.GetTarget() + "')"); |
abcb9c6e | 751 | |
ec4ac0f7 | 752 | wxASSERT(m_browser->IsBusy()); |
62e8551f SL |
753 | |
754 | //If we don't want to handle navigation then veto the event and navigation | |
0e830c7c | 755 | //will not take place, we also need to stop the loading animation |
62e8551f | 756 | if(!m_tools_handle_navigation->IsChecked()) |
0e830c7c | 757 | { |
62e8551f | 758 | evt.Veto(); |
abcb9c6e | 759 | m_toolbar->EnableTool( m_toolbar_stop->GetId(), false ); |
0e830c7c SL |
760 | } |
761 | else | |
762 | { | |
763 | UpdateState(); | |
764 | } | |
ec4ac0f7 SL |
765 | } |
766 | ||
767 | /** | |
768 | * Callback invoked when a navigation request was accepted | |
769 | */ | |
04fa04d8 | 770 | void WebFrame::OnNavigationComplete(wxWebViewEvent& evt) |
ec4ac0f7 | 771 | { |
e40741b9 | 772 | wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'"); |
ec4ac0f7 SL |
773 | UpdateState(); |
774 | } | |
775 | ||
776 | /** | |
777 | * Callback invoked when a page is finished loading | |
778 | */ | |
04fa04d8 | 779 | void WebFrame::OnDocumentLoaded(wxWebViewEvent& evt) |
ec4ac0f7 | 780 | { |
113e0a92 | 781 | //Only notify if the document is the main frame, not a subframe |
e40741b9 | 782 | if(evt.GetURL() == m_browser->GetCurrentURL()) |
8bccab8f | 783 | { |
e40741b9 | 784 | wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'"); |
8bccab8f | 785 | } |
ec4ac0f7 SL |
786 | UpdateState(); |
787 | } | |
788 | ||
789 | /** | |
790 | * On new window, we veto to stop extra windows appearing | |
791 | */ | |
04fa04d8 | 792 | void WebFrame::OnNewWindow(wxWebViewEvent& evt) |
ec4ac0f7 | 793 | { |
e40741b9 | 794 | wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'"); |
62e8551f | 795 | |
abcb9c6e | 796 | //If we handle new window events then just load them in this window as we |
62e8551f SL |
797 | //are a single window browser |
798 | if(m_tools_handle_new_window->IsChecked()) | |
4d0dddc7 | 799 | m_browser->LoadURL(evt.GetURL()); |
62e8551f | 800 | |
ec4ac0f7 SL |
801 | UpdateState(); |
802 | } | |
803 | ||
04fa04d8 | 804 | void WebFrame::OnTitleChanged(wxWebViewEvent& evt) |
153530af | 805 | { |
7cd03861 | 806 | SetTitle(evt.GetString()); |
153530af | 807 | wxLogMessage("%s", "Title changed; title='" + evt.GetString() + "'"); |
153530af SL |
808 | } |
809 | ||
ec4ac0f7 SL |
810 | /** |
811 | * Invoked when user selects the "View Source" menu item | |
812 | */ | |
8bccab8f | 813 | void WebFrame::OnViewSourceRequest(wxCommandEvent& WXUNUSED(evt)) |
ec4ac0f7 SL |
814 | { |
815 | SourceViewDialog dlg(this, m_browser->GetPageSource()); | |
816 | dlg.ShowModal(); | |
817 | } | |
818 | ||
819 | /** | |
820 | * Invoked when user selects the "Menu" item | |
821 | */ | |
8bccab8f | 822 | void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt)) |
ec4ac0f7 SL |
823 | { |
824 | if(m_browser->GetCurrentURL() == "") | |
825 | return; | |
826 | ||
827 | m_tools_tiny->Check(false); | |
abcb9c6e | 828 | m_tools_small->Check(false); |
ec4ac0f7 SL |
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) | |
61b98a2d | 835 | { |
236cff73 | 836 | case wxWEBVIEW_ZOOM_TINY: |
ec4ac0f7 SL |
837 | m_tools_tiny->Check(); |
838 | break; | |
236cff73 | 839 | case wxWEBVIEW_ZOOM_SMALL: |
ec4ac0f7 SL |
840 | m_tools_small->Check(); |
841 | break; | |
236cff73 | 842 | case wxWEBVIEW_ZOOM_MEDIUM: |
ec4ac0f7 SL |
843 | m_tools_medium->Check(); |
844 | break; | |
236cff73 | 845 | case wxWEBVIEW_ZOOM_LARGE: |
ec4ac0f7 SL |
846 | m_tools_large->Check(); |
847 | break; | |
236cff73 | 848 | case wxWEBVIEW_ZOOM_LARGEST: |
ec4ac0f7 SL |
849 | m_tools_largest->Check(); |
850 | break; | |
61b98a2d | 851 | } |
4681a3ea SL |
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()); | |
97e49559 SL |
856 | |
857 | m_edit_undo->Enable(m_browser->CanUndo()); | |
858 | m_edit_redo->Enable(m_browser->CanRedo()); | |
10ad4ba6 | 859 | |
603cfe42 SL |
860 | m_selection_clear->Enable(m_browser->HasSelection()); |
861 | m_selection_delete->Enable(m_browser->HasSelection()); | |
862 | ||
c420d57b SL |
863 | m_context_menu->Check(m_browser->IsContextMenuEnabled()); |
864 | ||
10ad4ba6 SL |
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 | ||
c13d6ac1 SL |
873 | wxVector<wxSharedPtr<wxWebViewHistoryItem> > back = m_browser->GetBackwardHistory(); |
874 | wxVector<wxSharedPtr<wxWebViewHistoryItem> > forward = m_browser->GetForwardHistory(); | |
10ad4ba6 SL |
875 | |
876 | wxMenuItem* item; | |
877 | ||
abcb9c6e JS |
878 | unsigned int i; |
879 | for(i = 0; i < back.size(); i++) | |
10ad4ba6 SL |
880 | { |
881 | item = m_tools_history_menu->AppendRadioItem(wxID_ANY, back[i]->GetTitle()); | |
882 | m_histMenuItems[item->GetId()] = back[i]; | |
ce7fe42e | 883 | Connect(item->GetId(), wxEVT_MENU, |
10ad4ba6 SL |
884 | wxCommandEventHandler(WebFrame::OnHistory), NULL, this ); |
885 | } | |
886 | ||
14a0b47d VZ |
887 | wxString title = m_browser->GetCurrentTitle(); |
888 | if ( title.empty() ) | |
889 | title = "(untitled)"; | |
890 | item = m_tools_history_menu->AppendRadioItem(wxID_ANY, title); | |
10ad4ba6 SL |
891 | item->Check(); |
892 | ||
893 | //No need to connect the current item | |
c13d6ac1 | 894 | m_histMenuItems[item->GetId()] = wxSharedPtr<wxWebViewHistoryItem>(new wxWebViewHistoryItem(m_browser->GetCurrentURL(), m_browser->GetCurrentTitle())); |
10ad4ba6 | 895 | |
abcb9c6e | 896 | for(i = 0; i < forward.size(); i++) |
10ad4ba6 SL |
897 | { |
898 | item = m_tools_history_menu->AppendRadioItem(wxID_ANY, forward[i]->GetTitle()); | |
899 | m_histMenuItems[item->GetId()] = forward[i]; | |
ce7fe42e | 900 | Connect(item->GetId(), wxEVT_TOOL, |
10ad4ba6 SL |
901 | wxCommandEventHandler(WebFrame::OnHistory), NULL, this ); |
902 | } | |
abcb9c6e | 903 | |
ec4ac0f7 SL |
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()) | |
61b98a2d | 914 | { |
236cff73 | 915 | m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY); |
61b98a2d | 916 | } |
ec4ac0f7 | 917 | else if (evt.GetId() == m_tools_small->GetId()) |
61b98a2d | 918 | { |
236cff73 | 919 | m_browser->SetZoom(wxWEBVIEW_ZOOM_SMALL); |
61b98a2d | 920 | } |
ec4ac0f7 | 921 | else if (evt.GetId() == m_tools_medium->GetId()) |
61b98a2d | 922 | { |
236cff73 | 923 | m_browser->SetZoom(wxWEBVIEW_ZOOM_MEDIUM); |
61b98a2d | 924 | } |
ec4ac0f7 | 925 | else if (evt.GetId() == m_tools_large->GetId()) |
61b98a2d | 926 | { |
236cff73 | 927 | m_browser->SetZoom(wxWEBVIEW_ZOOM_LARGE); |
61b98a2d | 928 | } |
ec4ac0f7 | 929 | else if (evt.GetId() == m_tools_largest->GetId()) |
61b98a2d | 930 | { |
236cff73 | 931 | m_browser->SetZoom(wxWEBVIEW_ZOOM_LARGEST); |
61b98a2d | 932 | } |
ec4ac0f7 | 933 | else |
61b98a2d | 934 | { |
ec4ac0f7 | 935 | wxFAIL; |
61b98a2d | 936 | } |
ec4ac0f7 | 937 | } |
853b6cd0 | 938 | |
8bccab8f | 939 | void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt)) |
87d482ec SL |
940 | { |
941 | if(m_tools_layout->IsChecked()) | |
236cff73 | 942 | m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT); |
87d482ec | 943 | else |
236cff73 | 944 | m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT); |
87d482ec SL |
945 | } |
946 | ||
10ad4ba6 SL |
947 | void WebFrame::OnHistory(wxCommandEvent& evt) |
948 | { | |
949 | m_browser->LoadHistoryItem(m_histMenuItems[evt.GetId()]); | |
950 | } | |
951 | ||
54883129 SL |
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 | ||
603cfe42 SL |
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 | ||
ec4ac0f7 SL |
976 | /** |
977 | * Callback invoked when a loading error occurs | |
978 | */ | |
04fa04d8 | 979 | void WebFrame::OnError(wxWebViewEvent& evt) |
c1381581 SL |
980 | { |
981 | #define WX_ERROR_CASE(type) \ | |
982 | case type: \ | |
983 | category = #type; \ | |
ec4ac0f7 | 984 | break; |
abcb9c6e | 985 | |
c1381581 SL |
986 | wxString category; |
987 | switch (evt.GetInt()) | |
988 | { | |
236cff73 SL |
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); | |
61b98a2d | 997 | } |
abcb9c6e | 998 | |
c1381581 | 999 | wxLogMessage("%s", "Error; url='" + evt.GetURL() + "', error='" + category + " (" + evt.GetString() + ")'"); |
abcb9c6e | 1000 | |
ec4ac0f7 | 1001 | //Show the info bar with an error |
e40741b9 | 1002 | m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" + |
c1381581 | 1003 | "'" + category + "'", wxICON_ERROR); |
abcb9c6e | 1004 | |
ec4ac0f7 SL |
1005 | UpdateState(); |
1006 | } | |
61b98a2d | 1007 | |
ec4ac0f7 SL |
1008 | /** |
1009 | * Invoked when user selects "Print" from the menu | |
1010 | */ | |
8bccab8f | 1011 | void WebFrame::OnPrint(wxCommandEvent& WXUNUSED(evt)) |
61b98a2d | 1012 | { |
ec4ac0f7 SL |
1013 | m_browser->Print(); |
1014 | } | |
61b98a2d | 1015 | |
ec4ac0f7 SL |
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); | |
abcb9c6e | 1023 | text->SetMarginType(1, wxSTC_MARGIN_NUMBER); |
ec4ac0f7 | 1024 | text->SetText(source); |
61b98a2d | 1025 | |
ec4ac0f7 SL |
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)); | |
61b98a2d | 1036 | |
61b98a2d | 1037 | wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); |
ec4ac0f7 SL |
1038 | sizer->Add(text, 1, wxEXPAND); |
1039 | SetSizer(sizer); | |
61b98a2d | 1040 | } |