diff --git a/build/bakefiles/files.bkl b/build/bakefiles/files.bkl index 2ba8e21c5d..196db2101e 100644 --- a/build/bakefiles/files.bkl +++ b/build/bakefiles/files.bkl @@ -2610,6 +2610,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! src/osx/iphone/textctrl.mm src/osx/iphone/toolbar.mm src/osx/iphone/utils.mm + src/osx/iphone/webkit.mm src/osx/iphone/window.mm diff --git a/include/wx/html/webkit.h b/include/wx/html/webkit.h index 4f7a3dc8ec..6bb7c429b0 100644 --- a/include/wx/html/webkit.h +++ b/include/wx/html/webkit.h @@ -14,12 +14,17 @@ #if wxUSE_WEBKIT -#if !defined(__WXMAC__) && !defined(__WXCOCOA__) +#if !defined(__WXMAC__) && !defined(__WXCOCOA__) && !defined(__WXOSX_IPHONE__) #error "wxWebKitCtrl not implemented for this platform" #endif #include "wx/control.h" -DECLARE_WXCOCOA_OBJC_CLASS(WebView); + +#if wxOSX_USE_IPHONE + DECLARE_WXCOCOA_OBJC_CLASS(wxUIWebView); +#else + DECLARE_WXCOCOA_OBJC_CLASS(WebView); +#endif // ---------------------------------------------------------------------------- // Web Kit Control @@ -37,7 +42,8 @@ public: wxWindowID winID, const wxString& strURL, const wxPoint& pos = wxDefaultPosition, - const wxSize& size = wxDefaultSize, long style = 0, + const wxSize& size = wxDefaultSize, + long style = 0, const wxValidator& validator = wxDefaultValidator, const wxString& name = wxWebKitCtrlNameStr) { @@ -47,7 +53,8 @@ public: wxWindowID winID, const wxString& strURL, const wxPoint& pos = wxDefaultPosition, - const wxSize& size = wxDefaultSize, long style = 0, + const wxSize& size = wxDefaultSize, + long style = 0, const wxValidator& validator = wxDefaultValidator, const wxString& name = wxWebKitCtrlNameStr); virtual ~wxWebKitCtrl(); @@ -62,7 +69,8 @@ public: void Stop(); bool CanGetPageSource(); wxString GetPageSource(); - void SetPageSource(const wxString& source, const wxString& baseUrl = wxEmptyString); + void SetPageSource(const wxString& source, + const wxString& baseUrl = wxEmptyString); wxString GetPageURL(){ return m_currentURL; } void SetPageTitle(const wxString& title) { m_pageTitle = title; } wxString GetPageTitle(){ return m_pageTitle; } @@ -87,6 +95,15 @@ public: void SetScrollPos(int pos); int GetScrollPos(); + +#if wxOSX_USE_IPHONE + /// If scaling is true, the user will be able to adjust the zoom. + /// The default is false. + void SetUserScaling(bool scaling); + + /// Returns true if the user can adjust the zoom. + bool GetUserScaling() const; +#endif // wxOSX_USE_IPHONE // don't hide base class virtuals virtual void SetScrollPos( int orient, int pos, bool refresh = true ) @@ -107,8 +124,20 @@ private: wxWindowID m_windowID; wxString m_currentURL; wxString m_pageTitle; + +#if wxOSX_USE_IPHONE + // Takes note of the curr. text size; used by IncreaseTextSize() / DecreaseTextSize() + unsigned int m_currTextSize; + + // Adjusts text size according to m_currTextSize + void AdjustTextSize(); +#endif +#if wxOSX_USE_IPHONE + WX_wxUIWebView m_webView; +#else WX_WebView m_webView; +#endif // we may use this later to setup our own mouse events, // so leave it in for now. @@ -125,7 +154,7 @@ enum { wxWEBKIT_STATE_REDIRECTING = 4, wxWEBKIT_STATE_TRANSFERRING = 8, wxWEBKIT_STATE_STOP = 16, - wxWEBKIT_STATE_FAILED = 32 + wxWEBKIT_STATE_FAILED = 32 }; enum { diff --git a/include/wx/mobile/native/webctrl.h b/include/wx/mobile/native/webctrl.h index 97f43cfffb..107800c2eb 100644 --- a/include/wx/mobile/native/webctrl.h +++ b/include/wx/mobile/native/webctrl.h @@ -14,6 +14,7 @@ #include "wx/bitmap.h" #include "wx/control.h" +#include "wx/html/webkit.h" extern WXDLLEXPORT_DATA(const wxChar) wxWebCtrlNameStr[]; @@ -25,11 +26,11 @@ extern WXDLLEXPORT_DATA(const wxChar) wxWebCtrlNameStr[]; @category{wxMobile} */ -class WXDLLEXPORT wxMoWebCtrl : public wxControl +class WXDLLEXPORT wxMoWebCtrl : public wxWebKitCtrl { public: /// Default constructor. - wxMoWebCtrl() { Init(); } + wxMoWebCtrl(); /// Constructor. wxMoWebCtrl(wxWindow *parent, @@ -38,11 +39,7 @@ public: const wxSize& size = wxDefaultSize, long style = 0, const wxValidator& validator = wxDefaultValidator, - const wxString& name = wxWebCtrlNameStr) - { - Init(); - Create(parent, id, pos, size, style, validator, name); - } + const wxString& name = wxWebCtrlNameStr); bool Create(wxWindow *parent, wxWindowID id, @@ -56,28 +53,6 @@ public: void Init(); - /// Begins loading the specified URL. - bool LoadURL(const wxString& url); - - /// Returns true if it is possible to go back in the browser history. - bool CanGoBack(); - - /// Returns true if it is possible to go forward in the browser history. - bool CanGoForward(); - - /// Goes back in the browser history. - bool GoBack(); - - /// Goes forward in the browser history. - bool GoForward(); - - /// If scaling is true, the user will be able to adjust the zoom. - /// The default is false. - void SetUserScaling(bool scaling) { m_userScaling = scaling; } - - /// Returns true if the user can adjust the zoom. - bool GetUserScaling() const { return m_userScaling; } - virtual bool SetBackgroundColour(const wxColour &colour); virtual bool SetForegroundColour(const wxColour &colour); virtual bool SetFont(const wxFont& font); diff --git a/include/wx/osx/iphone/chkconf.h b/include/wx/osx/iphone/chkconf.h index eba0550122..dcab031bed 100644 --- a/include/wx/osx/iphone/chkconf.h +++ b/include/wx/osx/iphone/chkconf.h @@ -313,11 +313,6 @@ #define wxUSE_PROPGRID 0 #endif -#if wxUSE_WEBKIT -#undef wxUSE_WEBKIT -#define wxUSE_WEBKIT 0 -#endif - #if wxUSE_DATAOBJ #undef wxUSE_DATAOBJ #define wxUSE_DATAOBJ 0 diff --git a/samples/mobile/demoios.xcodeproj/project.pbxproj b/samples/mobile/demoios.xcodeproj/project.pbxproj index 84a9624cc3..51967cd41d 100755 --- a/samples/mobile/demoios.xcodeproj/project.pbxproj +++ b/samples/mobile/demoios.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 407EE18A12F04CF900D0F171 /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 407EE18912F04CF900D0F171 /* AudioToolbox.framework */; }; B90DBFCE13C1A3EA00FA1ADF /* segctrl.mm in Sources */ = {isa = PBXBuildFile; fileRef = B90DBFCD13C1A3EA00FA1ADF /* segctrl.mm */; }; B90DC11913C1B30E00FA1ADF /* listbox.mm in Sources */ = {isa = PBXBuildFile; fileRef = B90DC11813C1B30E00FA1ADF /* listbox.mm */; }; + B90DC24413C1D6C800FA1ADF /* webkit.mm in Sources */ = {isa = PBXBuildFile; fileRef = B90DC24313C1D6C800FA1ADF /* webkit.mm */; }; B9133F9513BBC9FB0024E59E /* srchctrl.mm in Sources */ = {isa = PBXBuildFile; fileRef = B9133F9413BBC9FB0024E59E /* srchctrl.mm */; }; B913435013BC0A780024E59E /* navctrl.mm in Sources */ = {isa = PBXBuildFile; fileRef = B913434F13BC0A780024E59E /* navctrl.mm */; }; B9CA6D3B13BEF72300061979 /* tablecell.mm in Sources */ = {isa = PBXBuildFile; fileRef = B9CA6D3A13BEF72300061979 /* tablecell.mm */; }; @@ -102,6 +103,7 @@ 409CBDCA13B13C7100AB1F39 /* wxrelease.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = wxrelease.xcconfig; path = ../../build/osx/wxrelease.xcconfig; sourceTree = SOURCE_ROOT; }; B90DBFCD13C1A3EA00FA1ADF /* segctrl.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = segctrl.mm; path = ../../src/osx/iphone/segctrl.mm; sourceTree = SOURCE_ROOT; }; B90DC11813C1B30E00FA1ADF /* listbox.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = listbox.mm; path = ../../src/osx/iphone/listbox.mm; sourceTree = SOURCE_ROOT; }; + B90DC24313C1D6C800FA1ADF /* webkit.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = webkit.mm; path = ../../src/osx/iphone/webkit.mm; sourceTree = SOURCE_ROOT; }; B9133F9413BBC9FB0024E59E /* srchctrl.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = srchctrl.mm; path = ../../src/osx/iphone/srchctrl.mm; sourceTree = SOURCE_ROOT; }; B913434F13BC0A780024E59E /* navctrl.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = navctrl.mm; path = ../../src/osx/iphone/navctrl.mm; sourceTree = SOURCE_ROOT; }; B9CA6D3A13BEF72300061979 /* tablecell.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = tablecell.mm; path = ../../src/osx/iphone/tablecell.mm; sourceTree = SOURCE_ROOT; }; @@ -307,6 +309,7 @@ B9DF073A13B9FCF700D6F0D8 /* textctrl.mm */, B9DF073B13B9FCF700D6F0D8 /* toolbar.mm */, B9DF073C13B9FCF700D6F0D8 /* utils.mm */, + B90DC24313C1D6C800FA1ADF /* webkit.mm */, B9DF073D13B9FCF700D6F0D8 /* window.mm */, ); name = "src-osx-iphone"; @@ -537,6 +540,7 @@ B9CA731513BFA41000061979 /* pagectrl.mm in Sources */, B90DBFCE13C1A3EA00FA1ADF /* segctrl.mm in Sources */, B90DC11913C1B30E00FA1ADF /* listbox.mm in Sources */, + B90DC24413C1D6C800FA1ADF /* webkit.mm in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/mobile/native/mo_webctrl_n.mm b/src/mobile/native/mo_webctrl_n.mm index b8b068d8ec..ed95d68351 100755 --- a/src/mobile/native/mo_webctrl_n.mm +++ b/src/mobile/native/mo_webctrl_n.mm @@ -23,18 +23,33 @@ #include "wx/arrimpl.cpp" -#ifdef __WXMSW__ -#include "wx/mobile/web/iehtmlwin.h" -#endif - extern WXDLLEXPORT_DATA(const wxChar) wxWebCtrlNameStr[] = wxT("WebCtrl"); -IMPLEMENT_DYNAMIC_CLASS(wxMoWebCtrl, wxControl) +IMPLEMENT_DYNAMIC_CLASS(wxMoWebCtrl, wxWebKitCtrl) -BEGIN_EVENT_TABLE(wxMoWebCtrl, wxControl) +BEGIN_EVENT_TABLE(wxMoWebCtrl, wxWebKitCtrl) EVT_SIZE(wxMoWebCtrl::OnSize) END_EVENT_TABLE() +/// Default constructor. +wxMoWebCtrl::wxMoWebCtrl() +{ + Init(); +} + +/// Constructor. +wxMoWebCtrl::wxMoWebCtrl(wxWindow *parent, + wxWindowID id, + const wxPoint& pos, + const wxSize& size, + long style, + const wxValidator& validator, + const wxString& name) +{ + Init(); + Create(parent, id, pos, size, style, validator, name); +} + bool wxMoWebCtrl::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, @@ -43,13 +58,12 @@ bool wxMoWebCtrl::Create(wxWindow *parent, const wxValidator& validator, const wxString& name) { - // FIXME stub - - return true; + return wxWebKitCtrl::Create(parent, id, wxEmptyString, pos, size, style, validator, name); } wxMoWebCtrl::~wxMoWebCtrl() { + } void wxMoWebCtrl::Init() @@ -97,33 +111,3 @@ void wxMoWebCtrl::OnSize(wxSizeEvent& WXUNUSED(event)) { // FIXME stub } - -bool wxMoWebCtrl::LoadURL(const wxString& url) -{ - // FIXME stub - return true; -} - -bool wxMoWebCtrl::CanGoBack() -{ - // FIXME stub - return true; -} - -bool wxMoWebCtrl::CanGoForward() -{ - // FIXME stub - return true; -} - -bool wxMoWebCtrl::GoBack() -{ - // FIXME stub - return true; -} - -bool wxMoWebCtrl::GoForward() -{ - // FIXME stub - return true; -} diff --git a/src/osx/iphone/webkit.mm b/src/osx/iphone/webkit.mm new file mode 100644 index 0000000000..d5a75df175 --- /dev/null +++ b/src/osx/iphone/webkit.mm @@ -0,0 +1,603 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: src/osx/iphone/webkit.mm +// Purpose: wxWebKitCtrl - embeddable web kit control +// Author: Linas Valiukas +// Modified by: +// Created: 2011-07-04 +// RCS-ID: $Id$ +// Copyright: (c) Linas Valiukas +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" +#include "wx/splitter.h" + +#ifndef WX_PRECOMP +#include "wx/wx.h" +#endif + +#if wxUSE_WEBKIT + +#include "wx/osx/private.h" +#include "wx/html/webkit.h" + +// QuartzCore used for making webpage screenshots for AirPrint, so don't import unless needed +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_2 + #import +#endif + + +// Default text size and upper/lower boundaries +enum { + WX_IPHONE_WEBKIT_TEXTSIZE_DEFAULT = 100, + WX_IPHONE_WEBKIT_TEXTSIZE_MAX = 160, + WX_IPHONE_WEBKIT_TEXTSIZE_MIN = 50, + WX_IPHONE_WEBKIT_TEXTSIZE_STEP = 5 +}; + +extern WXDLLEXPORT_DATA(const char) wxWebKitCtrlNameStr[] = "webkitctrl"; + +// ---------------------------------------------------------------------------- +// macros +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl) + +BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl) +END_EVENT_TABLE() + + +// ---------------------------------------------------------------------------- +// wxWebKit Events (from src/html/htmlctrl/webkit/webkit.mm) +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent ) + +wxDEFINE_EVENT( wxEVT_WEBKIT_STATE_CHANGED, wxWebKitStateChangedEvent ); + +wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win ) +{ + SetEventType( wxEVT_WEBKIT_STATE_CHANGED); + if ( win ) + { + SetEventObject( win ); + SetId(win->GetId()); + } +} + +IMPLEMENT_DYNAMIC_CLASS( wxWebKitBeforeLoadEvent, wxCommandEvent ) + +wxDEFINE_EVENT( wxEVT_WEBKIT_BEFORE_LOAD, wxWebKitBeforeLoadEvent ); + +wxWebKitBeforeLoadEvent::wxWebKitBeforeLoadEvent( wxWindow* win ) +{ + m_cancelled = false; + SetEventType( wxEVT_WEBKIT_BEFORE_LOAD); + if ( win ) + { + SetEventObject( win ); + SetId(win->GetId()); + } +} + + +IMPLEMENT_DYNAMIC_CLASS( wxWebKitNewWindowEvent, wxCommandEvent ) + +wxDEFINE_EVENT( wxEVT_WEBKIT_NEW_WINDOW, wxWebKitNewWindowEvent ); + +wxWebKitNewWindowEvent::wxWebKitNewWindowEvent( wxWindow* win ) +{ + SetEventType( wxEVT_WEBKIT_NEW_WINDOW); + if ( win ) + { + SetEventObject( win ); + SetId(win->GetId()); + } +} + + +//--------------------------------------------------------- +// helper function for NSString <-> wxString conversion (from src/html/htmlctrl/webkit/webkit.mm) +//--------------------------------------------------------- + +inline wxString wxStringWithNSString(NSString *nsstring) +{ +#if wxUSE_UNICODE + return wxString([nsstring UTF8String], wxConvUTF8); +#else + return wxString([nsstring lossyCString]); +#endif // wxUSE_UNICODE +} + +inline NSString* wxNSStringWithWxString(const wxString &wxstring) +{ +#if wxUSE_UNICODE + return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)]; +#else + return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()]; +#endif // wxUSE_UNICODE +} + + + +#pragma mark Cocoa + +@interface wxUIWebView : UIWebView +{ + @private + wxWebKitCtrl *webKitWindow; +} + +- (id)initWithWxWindow:(wxWebKitCtrl *)inWindow; + +- (BOOL)webView:(UIWebView *)webView +shouldStartLoadWithRequest:(NSURLRequest *)request + navigationType:(UIWebViewNavigationType)navigationType; + +- (void)webViewDidStartLoad:(UIWebView *)webView; + +- (void)webViewDidFinishLoad:(UIWebView *)webView; + +- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error; + +@end + +@implementation wxUIWebView + +- (id)init { + if ((self = [super init])) { + webKitWindow = NULL; + } + + return self; +} + +- (id)initWithWxWindow:(wxWebKitCtrl *)inWindow { + if ((self = [super init])) { + webKitWindow = inWindow; // non retained + } + + return self; +} + +- (BOOL)webView:(UIWebView *)webView +shouldStartLoadWithRequest:(NSURLRequest *)request + navigationType:(UIWebViewNavigationType)navigationType { + + return YES; +} + +- (void)webViewDidStartLoad:(UIWebView *)webView { + + if (webKitWindow) { + NSString *url = [[[webView request] URL] absoluteString]; + + wxWebKitStateChangedEvent negotiatingEvent(webKitWindow); + negotiatingEvent.SetState(wxWEBKIT_STATE_NEGOTIATING); + negotiatingEvent.SetURL( wxStringWithNSString( url ) ); + if (webKitWindow->GetEventHandler()) { + webKitWindow->GetEventHandler()->ProcessEvent( negotiatingEvent ); + } + + wxWebKitStateChangedEvent transferringEvent(webKitWindow); + transferringEvent.SetState(wxWEBKIT_STATE_TRANSFERRING); + transferringEvent.SetURL( wxStringWithNSString( url ) ); + if (webKitWindow->GetEventHandler()) { + webKitWindow->GetEventHandler()->ProcessEvent( transferringEvent ); + } + } + +} + +- (void)webViewDidFinishLoad:(UIWebView *)webView { + + if (webKitWindow) { + + // "Finished loading" event + NSString *url = [[[webView request] URL] absoluteString]; + wxWebKitStateChangedEvent thisEvent(webKitWindow); + thisEvent.SetState(wxWEBKIT_STATE_STOP); + thisEvent.SetURL( wxStringWithNSString( url ) ); + if (webKitWindow->GetEventHandler()) { + webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); + } + + // Title + NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"]; + webKitWindow->SetPageTitle(wxStringWithNSString( title )); + } + +} + +- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { + + wxUnusedVar(error); + + if (webKitWindow) { + NSString *url = [[[webView request] URL] absoluteString]; + wxWebKitStateChangedEvent thisEvent(webKitWindow); + thisEvent.SetState(wxWEBKIT_STATE_FAILED); + thisEvent.SetURL( wxStringWithNSString( url ) ); + if (webKitWindow->GetEventHandler()) + webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); + } + +} + +@end + + +#pragma mark wx + +// ---------------------------------------------------------------------------- +// creation/destruction +// ---------------------------------------------------------------------------- + +bool wxWebKitCtrl::Create(wxWindow *parent, + wxWindowID winID, + const wxString& strURL, + const wxPoint& pos, + const wxSize& size, + long style, + const wxValidator& validator, + const wxString& name) +{ + m_currTextSize = 100; + m_currentURL = strURL; + + DontCreatePeer(); + + if (! wxControl::Create(parent, winID, pos, size, style , validator , name)) { + return false; + } + + CGRect r = wxOSXGetFrameForControl(this, pos , size) ; + + m_webView = [[wxUIWebView alloc] initWithWxWindow:this]; + [m_webView setDelegate:m_webView]; + + wxWidgetIPhoneImpl* c = new wxWidgetIPhoneImpl(this, m_webView ); + SetPeer(c); + + MacPostControlCreate(pos, size); + + LoadURL(m_currentURL); + + return true; +} + +wxWebKitCtrl::~wxWebKitCtrl() +{ + +} + +// ---------------------------------------------------------------------------- +// public methods +// ---------------------------------------------------------------------------- + +void wxWebKitCtrl::LoadURL(const wxString &url) +{ + if( !m_webView ) { + return; + } + + NSString *stringURL = [NSString stringWithString:wxCFStringRef(url).AsNSString()]; + if ( !stringURL ) { + return; + } + if ([stringURL isEqualToString:@""]) { + return; + } + + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringURL]]; + [m_webView loadRequest:request]; + + m_currentURL = url; +} + +bool wxWebKitCtrl::CanGoBack() +{ + if ( !m_webView ) { + return false; + } + + return [m_webView canGoBack]; +} + +bool wxWebKitCtrl::CanGoForward() +{ + if ( !m_webView ) { + return false; + } + + return [m_webView canGoForward]; +} + +bool wxWebKitCtrl::GoBack() +{ + + if ( !m_webView ) { + return false; + } + + [m_webView goBack]; + return true; +} + +bool wxWebKitCtrl::GoForward() +{ + + if ( !m_webView ) { + return false; + } + + [m_webView goForward]; + return true; +} + +void wxWebKitCtrl::Reload() +{ + if ( !m_webView ) { + return; + } + + [m_webView reload]; +} + +void wxWebKitCtrl::Stop() +{ + if ( !m_webView ) { + return; + } + + [m_webView stopLoading]; +} + +bool wxWebKitCtrl::CanGetPageSource() +{ + if ( !m_webView ) { + return false; + } + + if ( m_webView.loading ) { + return false; + } + + return true; +} + +wxString wxWebKitCtrl::GetPageSource() +{ + if ( !m_webView ) { + return wxEmptyString; + } + + if ( !CanGetPageSource() ) { + return wxEmptyString; + } + + NSString *source = [m_webView stringByEvaluatingJavaScriptFromString: + @"document.getElementsByTagName('html')[0].outerHTML"]; + if ( !source ) { + return wxEmptyString; + } else { + return wxStringWithNSString(source); + } +} + +wxString wxWebKitCtrl::GetSelection() +{ + if ( !m_webView ) { + return wxEmptyString; + } + + NSString *selection = [m_webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"]; + if ( !selection ) { + return wxEmptyString; + } else { + return wxStringWithNSString(selection); + } +} + +void wxWebKitCtrl::AdjustTextSize() +{ + NSString *adjustString = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", + m_currTextSize]; + [m_webView stringByEvaluatingJavaScriptFromString:adjustString]; +} + +bool wxWebKitCtrl::CanIncreaseTextSize() +{ + if ( !m_webView ) { + return false; + } + + return (m_currTextSize < WX_IPHONE_WEBKIT_TEXTSIZE_MAX); +} + +void wxWebKitCtrl::IncreaseTextSize() +{ + if ( !m_webView ) { + return; + } + + if (CanIncreaseTextSize()) { + m_currTextSize += WX_IPHONE_WEBKIT_TEXTSIZE_STEP; + AdjustTextSize(); + } +} + +bool wxWebKitCtrl::CanDecreaseTextSize() +{ + if ( !m_webView ) { + return false; + } + + return (m_currTextSize > WX_IPHONE_WEBKIT_TEXTSIZE_MIN); +} + +void wxWebKitCtrl::DecreaseTextSize() +{ + if ( !m_webView ) { + return; + } + + if (CanDecreaseTextSize()) { + m_currTextSize -= WX_IPHONE_WEBKIT_TEXTSIZE_STEP; + AdjustTextSize(); + } +} + +void wxWebKitCtrl::SetPageSource(const wxString& source, + const wxString& baseUrl) +{ + if ( !m_webView ) { + return; + } + + [m_webView loadHTMLString:(NSString *)wxNSStringWithWxString(source) + baseURL:[NSURL URLWithString:wxNSStringWithWxString(baseUrl)]]; +} + +void wxWebKitCtrl::Print(bool showPrompt) +{ + +#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_2 + + // No AirPrint since 4.2 + return; + +#else + + if ( !m_webView ) { + return; + } + + // Take screenshot + CGRect myRect = [m_webView bounds]; + + UIGraphicsBeginImageContext(myRect.size); + CGContextRef ctx = UIGraphicsGetCurrentContext(); + [[UIColor blackColor] set]; + CGContextFillRect(ctx, myRect); + + [m_webView.layer renderInContext:ctx]; + + UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); + NSData *imageData = UIImageJPEGRepresentation(viewImage, 1.0); + UIGraphicsEndImageContext(); + + // Send to AirPrint + UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController]; + if ( !pic ) { + return; + } + + if ( ![UIPrintInteractionController canPrintData:imageData] ) { + return; + } + + UIPrintInfo *printInfo = [UIPrintInfo printInfo]; + [printInfo setOutputType:UIPrintInfoOutputGeneral]; + [printInfo setJobName:[NSString stringWithString:wxNSStringWithWxString(m_pageTitle)]]; + [printInfo setDuplex:UIPrintInfoDuplexLongEdge]; + + [pic setPrintInfo:printInfo]; + [pic setShowsPageRange:YES]; + + [pic setPrintingItem:imageData]; + + void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, + BOOL completed, + NSError *error) { + if (!completed && error) { + NSLog(@"AirPrint error; domain: %@; error code: %u", error.domain, error.code); + } + }; + + [pic presentAnimated:YES + completionHandler:completionHandler]; + +#endif // __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_2 +} + +void wxWebKitCtrl::MakeEditable(bool enable) +{ + // Do nothing +} + +bool wxWebKitCtrl::IsEditable() +{ + // Well, I guess. + return true; +} + +int wxWebKitCtrl::GetScrollPos() +{ + if ( !m_webView ) { + return 0; + } + + NSString *scrollPos = [m_webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollTop"]; + if ( scrollPos ) { + return [scrollPos intValue]; + } else { + return 0; + } +} + +void wxWebKitCtrl::SetScrollPos(int pos) +{ + if ( !m_webView ) { + return; + } + + [m_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.scrollTop = %d;", pos]]; +} + +void wxWebKitCtrl::SetUserScaling(bool scaling) +{ + if (! m_webView ) { + return; + } + + [m_webView setScalesPageToFit:(scaling ? YES : NO)]; +} + +/// Returns true if the user can adjust the zoom. +bool wxWebKitCtrl::GetUserScaling() const +{ + if (! m_webView ) { + return false; + } + + return (m_webView.scalesPageToFit == YES); +} + +wxString wxWebKitCtrl::RunScript(const wxString& javascript) +{ + if ( !m_webView ) { + return wxEmptyString; + } + + + NSString *scriptResult = [m_webView stringByEvaluatingJavaScriptFromString:wxNSStringWithWxString(javascript)]; + if (scriptResult) { + return wxStringWithNSString(scriptResult); + } else { + return wxEmptyString; + } +} + +void wxWebKitCtrl::OnSize(wxSizeEvent &event) +{ + event.Skip(); +} + +void wxWebKitCtrl::MacVisibilityChanged() +{ + +} + +#endif //wxUSE_WEBKIT