]>
Commit | Line | Data |
---|---|---|
61b98a2d | 1 | ///////////////////////////////////////////////////////////////////////////// |
ec4ac0f7 SL |
2 | // Name: web.cpp |
3 | // Purpose: wxWebView sample | |
61b98a2d SL |
4 | // Author: Marianne Gagnon |
5 | // Id: $Id$ | |
ec4ac0f7 | 6 | // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton |
61b98a2d SL |
7 | // Licence: wxWindows licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
ec4ac0f7 SL |
10 | // ---------------------------------------------------------------------------- |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
14 | // For compilers that support precompilation, includes "wx/wx.h". | |
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/wx.h" | |
23 | #endif | |
24 | ||
61b98a2d SL |
25 | #include <wx/artprov.h> |
26 | #include <wx/notifmsg.h> | |
27 | #include <wx/settings.h> | |
ec4ac0f7 SL |
28 | #include <wx/webview.h> |
29 | #include <wx/infobar.h> | |
30 | ||
31 | #if !defined(__WXMSW__) && !defined(__WXPM__) | |
32 | #include "../sample.xpm" | |
33 | #endif | |
61b98a2d SL |
34 | |
35 | #if wxUSE_STC | |
36 | #include <wx/stc/stc.h> | |
37 | #else | |
38 | #error "wxStyledTextControl is needed by this sample" | |
39 | #endif | |
40 | ||
61b98a2d SL |
41 | #include "stop.xpm" |
42 | #include "refresh.xpm" | |
dffce0bd | 43 | #include "wxlogo.xpm" |
61b98a2d | 44 | |
ec4ac0f7 | 45 | class WebApp : public wxApp |
61b98a2d SL |
46 | { |
47 | public: | |
ec4ac0f7 | 48 | virtual bool OnInit(); |
61b98a2d SL |
49 | }; |
50 | ||
ec4ac0f7 | 51 | class WebFrame : public wxFrame |
61b98a2d | 52 | { |
ec4ac0f7 SL |
53 | public: |
54 | WebFrame(); | |
55 | ||
56 | void OnAnimationTimer(wxTimerEvent& evt); | |
57 | void UpdateState(); | |
58 | void OnUrl(wxCommandEvent& evt); | |
59 | void OnBack(wxCommandEvent& evt); | |
60 | void OnForward(wxCommandEvent& evt); | |
61 | void OnStop(wxCommandEvent& evt); | |
62 | void OnReload(wxCommandEvent& evt); | |
152a5808 SL |
63 | void OnClearHistory(wxCommandEvent& evt); |
64 | void OnEnableHistory(wxCommandEvent& evt); | |
ec4ac0f7 SL |
65 | void OnNavigationRequest(wxWebNavigationEvent& evt); |
66 | void OnNavigationComplete(wxWebNavigationEvent& evt); | |
67 | void OnDocumentLoaded(wxWebNavigationEvent& evt); | |
68 | void OnNewWindow(wxWebNavigationEvent& evt); | |
69 | void OnViewSourceRequest(wxCommandEvent& evt); | |
70 | void OnToolsClicked(wxCommandEvent& evt); | |
71 | void OnSetZoom(wxCommandEvent& evt); | |
72 | void OnError(wxWebNavigationEvent& evt); | |
73 | void OnPrint(wxCommandEvent& evt); | |
4681a3ea SL |
74 | void OnCut(wxCommandEvent& evt); |
75 | void OnCopy(wxCommandEvent& evt); | |
76 | void OnPaste(wxCommandEvent& evt); | |
97e49559 SL |
77 | void OnUndo(wxCommandEvent& evt); |
78 | void OnRedo(wxCommandEvent& evt); | |
c7cbe308 | 79 | void OnMode(wxCommandEvent& evt); |
87d482ec | 80 | void OnZoomLayout(wxCommandEvent& evt); |
ec4ac0f7 SL |
81 | |
82 | private: | |
83 | wxTextCtrl* m_url; | |
84 | wxWebView* m_browser; | |
85 | ||
61b98a2d | 86 | wxToolBar* m_toolbar; |
ec4ac0f7 SL |
87 | wxToolBarToolBase* m_toolbar_back; |
88 | wxToolBarToolBase* m_toolbar_forward; | |
89 | wxToolBarToolBase* m_toolbar_stop; | |
90 | wxToolBarToolBase* m_toolbar_reload; | |
91 | wxToolBarToolBase* m_toolbar_tools; | |
92 | ||
93 | wxMenu* m_tools_menu; | |
87d482ec | 94 | wxMenuItem* m_tools_layout; |
ec4ac0f7 SL |
95 | wxMenuItem* m_tools_tiny; |
96 | wxMenuItem* m_tools_small; | |
97 | wxMenuItem* m_tools_medium; | |
98 | wxMenuItem* m_tools_large; | |
99 | wxMenuItem* m_tools_largest; | |
62e8551f SL |
100 | wxMenuItem* m_tools_handle_navigation; |
101 | wxMenuItem* m_tools_handle_new_window; | |
152a5808 | 102 | wxMenuItem* m_tools_enable_history; |
4681a3ea SL |
103 | wxMenuItem* m_edit_cut; |
104 | wxMenuItem* m_edit_copy; | |
105 | wxMenuItem* m_edit_paste; | |
97e49559 SL |
106 | wxMenuItem* m_edit_undo; |
107 | wxMenuItem* m_edit_redo; | |
c7cbe308 | 108 | wxMenuItem* m_edit_mode; |
ec4ac0f7 | 109 | |
61b98a2d SL |
110 | wxTimer* m_timer; |
111 | int m_animation_angle; | |
ec4ac0f7 | 112 | |
ec4ac0f7 SL |
113 | wxInfoBar *m_info; |
114 | wxStaticText* m_info_text; | |
115 | }; | |
116 | ||
117 | class SourceViewDialog : public wxDialog | |
118 | { | |
61b98a2d | 119 | public: |
ec4ac0f7 SL |
120 | SourceViewDialog(wxWindow* parent, wxString source); |
121 | }; | |
122 | ||
123 | IMPLEMENT_APP(WebApp) | |
124 | ||
125 | // ============================================================================ | |
126 | // implementation | |
127 | // ============================================================================ | |
128 | ||
129 | bool WebApp::OnInit() | |
130 | { | |
131 | if ( !wxApp::OnInit() ) | |
132 | return false; | |
133 | ||
134 | WebFrame *frame = new WebFrame(); | |
135 | frame->Show(); | |
136 | ||
137 | return true; | |
138 | } | |
139 | ||
140 | WebFrame::WebFrame() : wxFrame(NULL, wxID_ANY, "wxWebView Sample") | |
141 | { | |
142 | // set the frame icon | |
143 | SetIcon(wxICON(sample)); | |
144 | SetTitle("wxWebView Sample"); | |
145 | ||
146 | m_timer = NULL; | |
147 | m_animation_angle = 0; | |
148 | ||
149 | ||
150 | wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL); | |
151 | ||
152 | // Create the toolbar | |
153 | m_toolbar = CreateToolBar(wxTB_TEXT); | |
154 | m_toolbar->SetToolBitmapSize(wxSize(32, 32)); | |
dffce0bd SL |
155 | |
156 | wxBitmap back = wxArtProvider::GetBitmap(wxART_GO_BACK , wxART_TOOLBAR); | |
157 | wxBitmap forward = wxArtProvider::GetBitmap(wxART_GO_FORWARD , wxART_TOOLBAR); | |
158 | #ifdef __WXGTK__ | |
159 | wxBitmap stop = wxArtProvider::GetBitmap("gtk-stop", wxART_TOOLBAR); | |
160 | #else | |
161 | wxBitmap stop = wxBitmap(stop_xpm); | |
162 | #endif | |
163 | #ifdef __WXGTK__ | |
164 | wxBitmap refresh = wxArtProvider::GetBitmap("gtk-refresh", wxART_TOOLBAR); | |
165 | #else | |
166 | wxBitmap refresh = wxBitmap(refresh_xpm); | |
167 | #endif | |
168 | ||
169 | m_toolbar_back = m_toolbar->AddTool(wxID_ANY, _("Back"), back); | |
170 | m_toolbar_forward = m_toolbar->AddTool(wxID_ANY, _("Forward"), forward); | |
171 | m_toolbar_stop = m_toolbar->AddTool(wxID_ANY, _("Stop"), stop); | |
172 | m_toolbar_reload = m_toolbar->AddTool(wxID_ANY, _("Reload"), refresh); | |
ec4ac0f7 | 173 | m_url = new wxTextCtrl(m_toolbar, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1), wxTE_PROCESS_ENTER ); |
9a00d078 | 174 | m_toolbar->AddControl(m_url, _("URL")); |
ec4ac0f7 SL |
175 | m_toolbar_tools = m_toolbar->AddTool(wxID_ANY, _("Menu"), wxBitmap(wxlogo_xpm)); |
176 | ||
177 | m_toolbar->Realize(); | |
178 | ||
179 | // Create the info panel | |
180 | m_info = new wxInfoBar(this); | |
181 | topsizer->Add(m_info, wxSizerFlags().Expand()); | |
182 | ||
183 | // Create the webview | |
9ef101cd | 184 | m_browser = wxWebView::New(this, wxID_ANY, "http://www.wxwidgets.org"); |
ec4ac0f7 SL |
185 | topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1)); |
186 | ||
187 | SetSizer(topsizer); | |
188 | ||
9a00d078 SL |
189 | //Set a more sensible size for web browsing |
190 | SetSize(wxSize(800, 600)); | |
191 | ||
ec4ac0f7 SL |
192 | // Create a log window |
193 | new wxLogWindow(this, _("Logging")); | |
194 | ||
195 | // Create the Tools menu | |
196 | m_tools_menu = new wxMenu(); | |
197 | wxMenuItem* print = m_tools_menu->Append(wxID_ANY , _("Print")); | |
198 | wxMenuItem* viewSource = m_tools_menu->Append(wxID_ANY , _("View Source")); | |
199 | m_tools_menu->AppendSeparator(); | |
87d482ec | 200 | m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom")); |
ec4ac0f7 SL |
201 | m_tools_tiny = m_tools_menu->AppendCheckItem(wxID_ANY, _("Tiny")); |
202 | m_tools_small = m_tools_menu->AppendCheckItem(wxID_ANY, _("Small")); | |
203 | m_tools_medium = m_tools_menu->AppendCheckItem(wxID_ANY, _("Medium")); | |
204 | m_tools_large = m_tools_menu->AppendCheckItem(wxID_ANY, _("Large")); | |
205 | m_tools_largest = m_tools_menu->AppendCheckItem(wxID_ANY, _("Largest")); | |
62e8551f SL |
206 | m_tools_menu->AppendSeparator(); |
207 | m_tools_handle_navigation = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle Navigation")); | |
208 | m_tools_handle_new_window = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle New Windows")); | |
152a5808 SL |
209 | m_tools_menu->AppendSeparator(); |
210 | wxMenuItem* clearhist = m_tools_menu->Append(wxID_ANY, _("Clear History")); | |
211 | m_tools_enable_history = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable History")); | |
62e8551f | 212 | |
4681a3ea SL |
213 | //Create an editing menu |
214 | wxMenu* editmenu = new wxMenu(); | |
215 | m_edit_cut = editmenu->Append(wxID_ANY, _("Cut")); | |
216 | m_edit_copy = editmenu->Append(wxID_ANY, _("Copy")); | |
217 | m_edit_paste = editmenu->Append(wxID_ANY, _("Paste")); | |
97e49559 SL |
218 | editmenu->AppendSeparator(); |
219 | m_edit_undo = editmenu->Append(wxID_ANY, _("Undo")); | |
220 | m_edit_redo = editmenu->Append(wxID_ANY, _("Redo")); | |
c7cbe308 SL |
221 | editmenu->AppendSeparator(); |
222 | m_edit_mode = editmenu->AppendCheckItem(wxID_ANY, _("Edit Mode")); | |
4681a3ea SL |
223 | |
224 | m_tools_menu->AppendSeparator(); | |
225 | m_tools_menu->AppendSubMenu(editmenu, "Edit"); | |
226 | ||
61635eed | 227 | //By default we want to handle navigation and new windows |
62e8551f SL |
228 | m_tools_handle_navigation->Check(); |
229 | m_tools_handle_new_window->Check(); | |
152a5808 | 230 | m_tools_enable_history->Check(); |
87d482ec SL |
231 | if(!m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT)) |
232 | m_tools_layout->Enable(false); | |
ec4ac0f7 SL |
233 | |
234 | ||
235 | // Connect the toolbar events | |
236 | Connect(m_toolbar_back->GetId(), wxEVT_COMMAND_TOOL_CLICKED, | |
237 | wxCommandEventHandler(WebFrame::OnBack), NULL, this ); | |
238 | Connect(m_toolbar_forward->GetId(), wxEVT_COMMAND_TOOL_CLICKED, | |
239 | wxCommandEventHandler(WebFrame::OnForward), NULL, this ); | |
240 | Connect(m_toolbar_stop->GetId(), wxEVT_COMMAND_TOOL_CLICKED, | |
241 | wxCommandEventHandler(WebFrame::OnStop), NULL, this ); | |
242 | Connect(m_toolbar_reload->GetId(), wxEVT_COMMAND_TOOL_CLICKED, | |
243 | wxCommandEventHandler(WebFrame::OnReload),NULL, this ); | |
244 | Connect(m_toolbar_tools->GetId(), wxEVT_COMMAND_TOOL_CLICKED, | |
245 | wxCommandEventHandler(WebFrame::OnToolsClicked), NULL, this ); | |
246 | ||
247 | Connect(m_url->GetId(), wxEVT_COMMAND_TEXT_ENTER, | |
248 | wxCommandEventHandler(WebFrame::OnUrl), NULL, this ); | |
249 | ||
250 | // Connect the webview events | |
251 | Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATING, | |
252 | wxWebNavigationEventHandler(WebFrame::OnNavigationRequest), NULL, this); | |
253 | Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NAVIGATED, | |
254 | wxWebNavigationEventHandler(WebFrame::OnNavigationComplete), NULL, this); | |
255 | Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_LOADED, | |
256 | wxWebNavigationEventHandler(WebFrame::OnDocumentLoaded), NULL, this); | |
257 | Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_ERROR, | |
258 | wxWebNavigationEventHandler(WebFrame::OnError), NULL, this); | |
259 | Connect(m_browser->GetId(), wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, | |
260 | wxWebNavigationEventHandler(WebFrame::OnNewWindow), NULL, this); | |
261 | ||
262 | // Connect the menu events | |
263 | Connect(viewSource->GetId(), wxEVT_COMMAND_MENU_SELECTED, | |
264 | wxCommandEventHandler(WebFrame::OnViewSourceRequest), NULL, this ); | |
265 | Connect(print->GetId(), wxEVT_COMMAND_MENU_SELECTED, | |
266 | wxCommandEventHandler(WebFrame::OnPrint), NULL, this ); | |
87d482ec SL |
267 | Connect(m_tools_layout->GetId(), wxEVT_COMMAND_MENU_SELECTED, |
268 | wxCommandEventHandler(WebFrame::OnZoomLayout), NULL, this ); | |
ec4ac0f7 SL |
269 | Connect(m_tools_tiny->GetId(), wxEVT_COMMAND_MENU_SELECTED, |
270 | wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this ); | |
271 | Connect(m_tools_small->GetId(), wxEVT_COMMAND_MENU_SELECTED, | |
272 | wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this ); | |
273 | Connect(m_tools_medium->GetId(), wxEVT_COMMAND_MENU_SELECTED, | |
274 | wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this ); | |
275 | Connect(m_tools_large->GetId(), wxEVT_COMMAND_MENU_SELECTED, | |
276 | wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this ); | |
277 | Connect(m_tools_largest->GetId(), wxEVT_COMMAND_MENU_SELECTED, | |
278 | wxCommandEventHandler(WebFrame::OnSetZoom), NULL, this ); | |
152a5808 SL |
279 | Connect(clearhist->GetId(), wxEVT_COMMAND_MENU_SELECTED, |
280 | wxCommandEventHandler(WebFrame::OnClearHistory), NULL, this ); | |
281 | Connect(m_tools_enable_history->GetId(), wxEVT_COMMAND_MENU_SELECTED, | |
282 | wxCommandEventHandler(WebFrame::OnEnableHistory), NULL, this ); | |
4681a3ea SL |
283 | Connect(m_edit_cut->GetId(), wxEVT_COMMAND_MENU_SELECTED, |
284 | wxCommandEventHandler(WebFrame::OnCut), NULL, this ); | |
285 | Connect(m_edit_copy->GetId(), wxEVT_COMMAND_MENU_SELECTED, | |
286 | wxCommandEventHandler(WebFrame::OnCopy), NULL, this ); | |
287 | Connect(m_edit_paste->GetId(), wxEVT_COMMAND_MENU_SELECTED, | |
288 | wxCommandEventHandler(WebFrame::OnPaste), NULL, this ); | |
97e49559 SL |
289 | Connect(m_edit_undo->GetId(), wxEVT_COMMAND_MENU_SELECTED, |
290 | wxCommandEventHandler(WebFrame::OnUndo), NULL, this ); | |
291 | Connect(m_edit_redo->GetId(), wxEVT_COMMAND_MENU_SELECTED, | |
292 | wxCommandEventHandler(WebFrame::OnRedo), NULL, this ); | |
c7cbe308 SL |
293 | Connect(m_edit_mode->GetId(), wxEVT_COMMAND_MENU_SELECTED, |
294 | wxCommandEventHandler(WebFrame::OnMode), NULL, this ); | |
ec4ac0f7 SL |
295 | } |
296 | ||
297 | void WebFrame::OnAnimationTimer(wxTimerEvent& evt) | |
298 | { | |
299 | m_animation_angle += 15; | |
300 | if (m_animation_angle > 360) m_animation_angle -= 360; | |
61b98a2d | 301 | |
61635eed | 302 | wxBitmap image(24, 24); |
61b98a2d | 303 | { |
61b98a2d SL |
304 | wxMemoryDC dc; |
305 | dc.SelectObject(image); | |
306 | dc.SetBackground(wxBrush(wxColour(255,0,255))); | |
307 | dc.Clear(); | |
308 | ||
309 | if (m_animation_angle >= 0 && m_animation_angle <= 180) | |
310 | { | |
311 | dc.SetBrush(*wxYELLOW_BRUSH); | |
312 | dc.SetPen(*wxYELLOW_PEN); | |
313 | dc.DrawCircle(16 - int(sin(m_animation_angle*0.01745f /* convert to radians */)*14.0f), | |
ec4ac0f7 | 314 | 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 ); |
61b98a2d SL |
315 | } |
316 | ||
317 | dc.DrawBitmap(wxBitmap(wxlogo_xpm), 0, 0, true); | |
318 | ||
319 | if (m_animation_angle > 180) | |
320 | { | |
321 | dc.SetBrush(*wxYELLOW_BRUSH); | |
322 | dc.SetPen(*wxYELLOW_PEN); | |
323 | dc.DrawCircle(16 - int(sin(m_animation_angle*0.01745f /* convert to radians */)*14.0f), | |
ec4ac0f7 | 324 | 16 + int(cos(m_animation_angle*0.01745f /* convert to radians */)*14.0f), 3 ); |
61b98a2d | 325 | } |
ec4ac0f7 SL |
326 | } |
327 | image.SetMask(new wxMask(image, wxColour(255,0,255))); | |
328 | m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), image); | |
329 | } | |
330 | ||
331 | /** | |
332 | * Method that retrieves the current state from the web control and updates the GUI | |
333 | * the reflect this current state. | |
334 | */ | |
335 | void WebFrame::UpdateState() | |
336 | { | |
337 | m_toolbar->EnableTool( m_toolbar_back->GetId(), m_browser->CanGoBack() ); | |
338 | m_toolbar->EnableTool( m_toolbar_forward->GetId(), m_browser->CanGoForward() ); | |
61b98a2d | 339 | |
ec4ac0f7 | 340 | if (m_browser->IsBusy()) |
61b98a2d | 341 | { |
ec4ac0f7 | 342 | if (m_timer == NULL) |
61b98a2d | 343 | { |
ec4ac0f7 SL |
344 | m_timer = new wxTimer(this); |
345 | this->Connect(wxEVT_TIMER, wxTimerEventHandler(WebFrame::OnAnimationTimer), NULL, this); | |
61b98a2d | 346 | } |
ec4ac0f7 | 347 | m_timer->Start(100); // start animation timer |
61b98a2d | 348 | |
ec4ac0f7 | 349 | m_toolbar->EnableTool( m_toolbar_stop->GetId(), true ); |
61b98a2d | 350 | } |
ec4ac0f7 | 351 | else |
61b98a2d | 352 | { |
ec4ac0f7 SL |
353 | if (m_timer != NULL) m_timer->Stop(); // stop animation timer |
354 | m_toolbar->SetToolNormalBitmap(m_toolbar_tools->GetId(), wxBitmap(wxlogo_xpm)); | |
355 | m_toolbar->EnableTool( m_toolbar_stop->GetId(), false ); | |
61b98a2d SL |
356 | } |
357 | ||
ec4ac0f7 SL |
358 | SetTitle( m_browser->GetCurrentTitle() ); |
359 | m_url->SetValue( m_browser->GetCurrentURL() ); | |
360 | } | |
361 | ||
362 | /** | |
363 | * Callback invoked when user entered an URL and pressed enter | |
364 | */ | |
365 | void WebFrame::OnUrl(wxCommandEvent& evt) | |
366 | { | |
367 | m_browser->LoadUrl( m_url->GetValue() ); | |
368 | UpdateState(); | |
369 | } | |
370 | ||
371 | /** | |
372 | * Callback invoked when user pressed the "back" button | |
373 | */ | |
374 | void WebFrame::OnBack(wxCommandEvent& evt) | |
375 | { | |
376 | m_browser->GoBack(); | |
377 | UpdateState(); | |
378 | } | |
379 | ||
380 | /** | |
381 | * Callback invoked when user pressed the "forward" button | |
382 | */ | |
383 | void WebFrame::OnForward(wxCommandEvent& evt) | |
384 | { | |
385 | m_browser->GoForward(); | |
386 | UpdateState(); | |
387 | } | |
388 | ||
389 | /** | |
390 | * Callback invoked when user pressed the "stop" button | |
391 | */ | |
392 | void WebFrame::OnStop(wxCommandEvent& evt) | |
393 | { | |
394 | m_browser->Stop(); | |
395 | UpdateState(); | |
396 | } | |
397 | ||
398 | /** | |
399 | * Callback invoked when user pressed the "reload" button | |
400 | */ | |
401 | void WebFrame::OnReload(wxCommandEvent& evt) | |
402 | { | |
403 | m_browser->Reload(); | |
404 | UpdateState(); | |
405 | } | |
406 | ||
152a5808 SL |
407 | void WebFrame::OnClearHistory(wxCommandEvent& evt) |
408 | { | |
409 | m_browser->ClearHistory(); | |
410 | UpdateState(); | |
411 | } | |
412 | ||
413 | void WebFrame::OnEnableHistory(wxCommandEvent& evt) | |
414 | { | |
415 | m_browser->EnableHistory(m_tools_enable_history->IsChecked()); | |
416 | UpdateState(); | |
417 | } | |
418 | ||
4681a3ea SL |
419 | void WebFrame::OnCut(wxCommandEvent& evt) |
420 | { | |
421 | m_browser->Cut(); | |
422 | } | |
423 | ||
424 | void WebFrame::OnCopy(wxCommandEvent& evt) | |
425 | { | |
426 | m_browser->Copy(); | |
427 | } | |
428 | ||
429 | void WebFrame::OnPaste(wxCommandEvent& evt) | |
430 | { | |
431 | m_browser->Paste(); | |
432 | } | |
433 | ||
97e49559 SL |
434 | void WebFrame::OnUndo(wxCommandEvent& evt) |
435 | { | |
436 | m_browser->Undo(); | |
437 | } | |
438 | ||
439 | void WebFrame::OnRedo(wxCommandEvent& evt) | |
440 | { | |
441 | m_browser->Redo(); | |
442 | } | |
443 | ||
c7cbe308 SL |
444 | void WebFrame::OnMode(wxCommandEvent& evt) |
445 | { | |
446 | m_browser->SetEditable(m_edit_mode->IsChecked()); | |
447 | } | |
448 | ||
449 | ||
ec4ac0f7 SL |
450 | /** |
451 | * Callback invoked when there is a request to load a new page (for instance | |
452 | * when the user clicks a link) | |
453 | */ | |
454 | void WebFrame::OnNavigationRequest(wxWebNavigationEvent& evt) | |
455 | { | |
e40741b9 | 456 | wxLogMessage("%s", "Navigation request to '" + evt.GetURL() + "' (target='" + |
ec4ac0f7 SL |
457 | evt.GetTarget() + "')"); |
458 | ||
459 | wxASSERT(m_browser->IsBusy()); | |
62e8551f SL |
460 | |
461 | //If we don't want to handle navigation then veto the event and navigation | |
462 | //will not take place | |
463 | if(!m_tools_handle_navigation->IsChecked()) | |
464 | evt.Veto(); | |
465 | ||
ec4ac0f7 SL |
466 | UpdateState(); |
467 | } | |
468 | ||
469 | /** | |
470 | * Callback invoked when a navigation request was accepted | |
471 | */ | |
472 | void WebFrame::OnNavigationComplete(wxWebNavigationEvent& evt) | |
473 | { | |
e40741b9 | 474 | wxLogMessage("%s", "Navigation complete; url='" + evt.GetURL() + "'"); |
ec4ac0f7 SL |
475 | UpdateState(); |
476 | } | |
477 | ||
478 | /** | |
479 | * Callback invoked when a page is finished loading | |
480 | */ | |
481 | void WebFrame::OnDocumentLoaded(wxWebNavigationEvent& evt) | |
482 | { | |
113e0a92 | 483 | //Only notify if the document is the main frame, not a subframe |
e40741b9 SL |
484 | if(evt.GetURL() == m_browser->GetCurrentURL()) |
485 | wxLogMessage("%s", "Document loaded; url='" + evt.GetURL() + "'"); | |
ec4ac0f7 SL |
486 | UpdateState(); |
487 | } | |
488 | ||
489 | /** | |
490 | * On new window, we veto to stop extra windows appearing | |
491 | */ | |
492 | void WebFrame::OnNewWindow(wxWebNavigationEvent& evt) | |
493 | { | |
e40741b9 | 494 | wxLogMessage("%s", "New window; url='" + evt.GetURL() + "'"); |
62e8551f SL |
495 | |
496 | //If we handle new window events then just load them in this window as we | |
497 | //are a single window browser | |
498 | if(m_tools_handle_new_window->IsChecked()) | |
e40741b9 | 499 | m_browser->LoadUrl(evt.GetURL()); |
62e8551f | 500 | |
ec4ac0f7 SL |
501 | UpdateState(); |
502 | } | |
503 | ||
504 | /** | |
505 | * Invoked when user selects the "View Source" menu item | |
506 | */ | |
507 | void WebFrame::OnViewSourceRequest(wxCommandEvent& evt) | |
508 | { | |
509 | SourceViewDialog dlg(this, m_browser->GetPageSource()); | |
510 | dlg.ShowModal(); | |
511 | } | |
512 | ||
513 | /** | |
514 | * Invoked when user selects the "Menu" item | |
515 | */ | |
516 | void WebFrame::OnToolsClicked(wxCommandEvent& evt) | |
517 | { | |
518 | if(m_browser->GetCurrentURL() == "") | |
519 | return; | |
520 | ||
521 | m_tools_tiny->Check(false); | |
522 | m_tools_small->Check(false); | |
523 | m_tools_medium->Check(false); | |
524 | m_tools_large->Check(false); | |
525 | m_tools_largest->Check(false); | |
526 | ||
527 | wxWebViewZoom zoom = m_browser->GetZoom(); | |
528 | switch (zoom) | |
61b98a2d | 529 | { |
ec4ac0f7 SL |
530 | case wxWEB_VIEW_ZOOM_TINY: |
531 | m_tools_tiny->Check(); | |
532 | break; | |
533 | case wxWEB_VIEW_ZOOM_SMALL: | |
534 | m_tools_small->Check(); | |
535 | break; | |
536 | case wxWEB_VIEW_ZOOM_MEDIUM: | |
537 | m_tools_medium->Check(); | |
538 | break; | |
539 | case wxWEB_VIEW_ZOOM_LARGE: | |
540 | m_tools_large->Check(); | |
541 | break; | |
542 | case wxWEB_VIEW_ZOOM_LARGEST: | |
543 | m_tools_largest->Check(); | |
544 | break; | |
61b98a2d | 545 | } |
4681a3ea SL |
546 | |
547 | m_edit_cut->Enable(m_browser->CanCut()); | |
548 | m_edit_copy->Enable(m_browser->CanCopy()); | |
549 | m_edit_paste->Enable(m_browser->CanPaste()); | |
97e49559 SL |
550 | |
551 | m_edit_undo->Enable(m_browser->CanUndo()); | |
552 | m_edit_redo->Enable(m_browser->CanRedo()); | |
61b98a2d | 553 | |
ec4ac0f7 SL |
554 | wxPoint position = ScreenToClient( wxGetMousePosition() ); |
555 | PopupMenu(m_tools_menu, position.x, position.y); | |
556 | } | |
557 | ||
558 | /** | |
559 | * Invoked when user selects the zoom size in the menu | |
560 | */ | |
561 | void WebFrame::OnSetZoom(wxCommandEvent& evt) | |
562 | { | |
563 | if (evt.GetId() == m_tools_tiny->GetId()) | |
61b98a2d | 564 | { |
ec4ac0f7 | 565 | m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY); |
61b98a2d | 566 | } |
ec4ac0f7 | 567 | else if (evt.GetId() == m_tools_small->GetId()) |
61b98a2d | 568 | { |
ec4ac0f7 | 569 | m_browser->SetZoom(wxWEB_VIEW_ZOOM_SMALL); |
61b98a2d | 570 | } |
ec4ac0f7 | 571 | else if (evt.GetId() == m_tools_medium->GetId()) |
61b98a2d | 572 | { |
ec4ac0f7 | 573 | m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM); |
61b98a2d | 574 | } |
ec4ac0f7 | 575 | else if (evt.GetId() == m_tools_large->GetId()) |
61b98a2d | 576 | { |
ec4ac0f7 | 577 | m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGE); |
61b98a2d | 578 | } |
ec4ac0f7 | 579 | else if (evt.GetId() == m_tools_largest->GetId()) |
61b98a2d | 580 | { |
ec4ac0f7 | 581 | m_browser->SetZoom(wxWEB_VIEW_ZOOM_LARGEST); |
61b98a2d | 582 | } |
ec4ac0f7 | 583 | else |
61b98a2d | 584 | { |
ec4ac0f7 | 585 | wxFAIL; |
61b98a2d | 586 | } |
ec4ac0f7 | 587 | } |
853b6cd0 | 588 | |
87d482ec SL |
589 | void WebFrame::OnZoomLayout(wxCommandEvent& evt) |
590 | { | |
591 | if(m_tools_layout->IsChecked()) | |
592 | m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT); | |
593 | else | |
594 | m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT); | |
595 | } | |
596 | ||
ec4ac0f7 SL |
597 | /** |
598 | * Callback invoked when a loading error occurs | |
599 | */ | |
600 | void WebFrame::OnError(wxWebNavigationEvent& evt) | |
601 | { | |
602 | wxString errorCategory; | |
603 | switch (evt.GetInt()) | |
61b98a2d | 604 | { |
ec4ac0f7 SL |
605 | case wxWEB_NAV_ERR_CONNECTION: |
606 | errorCategory = "wxWEB_NAV_ERR_CONNECTION"; | |
607 | break; | |
61b98a2d | 608 | |
ec4ac0f7 SL |
609 | case wxWEB_NAV_ERR_CERTIFICATE: |
610 | errorCategory = "wxWEB_NAV_ERR_CERTIFICATE"; | |
611 | break; | |
61b98a2d | 612 | |
ec4ac0f7 SL |
613 | case wxWEB_NAV_ERR_AUTH: |
614 | errorCategory = "wxWEB_NAV_ERR_AUTH"; | |
615 | break; | |
61b98a2d | 616 | |
ec4ac0f7 SL |
617 | case wxWEB_NAV_ERR_SECURITY: |
618 | errorCategory = "wxWEB_NAV_ERR_SECURITY"; | |
619 | break; | |
61b98a2d | 620 | |
ec4ac0f7 SL |
621 | case wxWEB_NAV_ERR_NOT_FOUND: |
622 | errorCategory = "wxWEB_NAV_ERR_NOT_FOUND"; | |
623 | break; | |
61b98a2d | 624 | |
ec4ac0f7 SL |
625 | case wxWEB_NAV_ERR_REQUEST: |
626 | errorCategory = "wxWEB_NAV_ERR_REQUEST"; | |
627 | break; | |
628 | ||
629 | case wxWEB_NAV_ERR_USER_CANCELLED: | |
630 | errorCategory = "wxWEB_NAV_ERR_USER_CANCELLED"; | |
631 | break; | |
632 | ||
633 | case wxWEB_NAV_ERR_OTHER: | |
634 | errorCategory = "wxWEB_NAV_ERR_OTHER"; | |
635 | break; | |
61b98a2d SL |
636 | } |
637 | ||
e40741b9 | 638 | wxLogMessage("Error; url='" + evt.GetURL() + "', error='" + errorCategory + "' (" + evt.GetString() + ")"); |
61b98a2d | 639 | |
ec4ac0f7 | 640 | //Show the info bar with an error |
e40741b9 | 641 | m_info->ShowMessage(_("An error occurred loading ") + evt.GetURL() + "\n" + |
ec4ac0f7 | 642 | "'" + errorCategory + "' (" + evt.GetString() + ")", wxICON_ERROR); |
61b98a2d | 643 | |
ec4ac0f7 SL |
644 | UpdateState(); |
645 | } | |
61b98a2d | 646 | |
ec4ac0f7 SL |
647 | /** |
648 | * Invoked when user selects "Print" from the menu | |
649 | */ | |
650 | void WebFrame::OnPrint(wxCommandEvent& evt) | |
61b98a2d | 651 | { |
ec4ac0f7 SL |
652 | m_browser->Print(); |
653 | } | |
61b98a2d | 654 | |
ec4ac0f7 SL |
655 | SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) : |
656 | wxDialog(parent, wxID_ANY, "Source Code", | |
657 | wxDefaultPosition, wxSize(700,500), | |
658 | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) | |
659 | { | |
660 | wxStyledTextCtrl* text = new wxStyledTextCtrl(this, wxID_ANY); | |
661 | text->SetMarginWidth(1, 30); | |
662 | text->SetMarginType(1, wxSTC_MARGIN_NUMBER); | |
663 | text->SetText(source); | |
61b98a2d | 664 | |
ec4ac0f7 SL |
665 | text->StyleClearAll(); |
666 | text->SetLexer(wxSTC_LEX_HTML); | |
667 | text->StyleSetForeground(wxSTC_H_DOUBLESTRING, wxColour(255,0,0)); | |
668 | text->StyleSetForeground(wxSTC_H_SINGLESTRING, wxColour(255,0,0)); | |
669 | text->StyleSetForeground(wxSTC_H_ENTITY, wxColour(255,0,0)); | |
670 | text->StyleSetForeground(wxSTC_H_TAG, wxColour(0,150,0)); | |
671 | text->StyleSetForeground(wxSTC_H_TAGUNKNOWN, wxColour(0,150,0)); | |
672 | text->StyleSetForeground(wxSTC_H_ATTRIBUTE, wxColour(0,0,150)); | |
673 | text->StyleSetForeground(wxSTC_H_ATTRIBUTEUNKNOWN, wxColour(0,0,150)); | |
674 | text->StyleSetForeground(wxSTC_H_COMMENT, wxColour(150,150,150)); | |
61b98a2d | 675 | |
61b98a2d | 676 | wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); |
ec4ac0f7 SL |
677 | sizer->Add(text, 1, wxEXPAND); |
678 | SetSizer(sizer); | |
61b98a2d | 679 | } |