]> git.saurik.com Git - wxWidgets.git/commitdiff
Add RunScript and implement on all backends. Document and add a very simple unit...
authorSteve Lamerton <steve.lamerton@gmail.com>
Fri, 15 Jul 2011 12:38:47 +0000 (12:38 +0000)
committerSteve Lamerton <steve.lamerton@gmail.com>
Fri, 15 Jul 2011 12:38:47 +0000 (12:38 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/SOC2011_WEBVIEW@68275 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/gtk/webview_webkit.h
include/wx/msw/webview_ie.h
include/wx/osx/webview_webkit.h
include/wx/webview.h
interface/wx/webview.h
src/gtk/webview_webkit.cpp
src/msw/webview_ie.cpp
src/osx/webview_webkit.mm
tests/controls/webtest.cpp

index e675d5d98adc5655ddf75983d32fc71f82800c95..3dc9acc282416dda3ded036c375322e9aa835b05 100644 (file)
@@ -149,6 +149,8 @@ public:
     virtual wxString GetSelectedSource();
     virtual void ClearSelection();
 
+    virtual void RunScript(const wxString& javascript);
+
     /** FIXME: hack to work around signals being received too early */
     bool m_ready;
 
index 5d854abd9ddfeb291aa090dae8ff90239d97a4ae..ebf325cc444e44057ca83fea68f0149559b73a1b 100644 (file)
@@ -106,6 +106,7 @@ public:
     virtual wxString GetSelectedSource();
     virtual void ClearSelection();
 
+    virtual void RunScript(const wxString& javascript);
 
     // ---- IE-specific methods
 
index a30f95cccb195b18b3912d8015151b5b897bea36..975bee5ec7f80fbc3b149b2ff9340191002a4afd 100644 (file)
@@ -110,10 +110,10 @@ public:
     virtual wxString GetSelectedText();
     virtual wxString GetSelectedSource() { return ""; }
     virtual void ClearSelection() {}
+    
+    void RunScript(const wxString& javascript);
 
     // ---- methods not from the parent (common) interface
-    wxString RunScript(const wxString& javascript);
-
     bool  CanGetPageSource();
 
     void  SetScrollPos(int pos);
index 22d9a2cbbc1a92e3d0fc3d7d48775d1068c1471d..2a114f96c593c0ede412925d71bc008922409724 100644 (file)
@@ -292,9 +292,11 @@ public:
     virtual wxString GetSelectedSource() = 0;
     virtual void ClearSelection() = 0;
 
+    virtual void RunScript(const wxString& javascript) = 0;
+
     // TODO:
     //     void EnableJavascript(bool enabled);             // maybe?
-    //     wxString RunScript(const wxString& javascript);  // maybe?
+    //       // maybe?
 
     //     void SetScrollPos(int pos);                      // maybe?
     //     int GetScrollPos();                              // maybe?
index 2ee38446881a1111045f5bc114ca67626074d9e2..da6f95214ca0b96eb7fc54955378502a0b93367e 100644 (file)
@@ -262,6 +262,11 @@ public:
     */
     virtual void Reload(wxWebViewReloadFlags flags = wxWEB_VIEW_RELOAD_DEFAULT) = 0;
     
+    /**
+        Runs the given javascript code. 
+    */
+    virtual void RunScript(const wxString& javascript) = 0;
+    
     /**
         Set the editable property of the web control. Enabling allows the user
         to edit the page even if the @c contenteditable attribute is not set.
index 11304653d6d100f807fee8e617df1c098d3efb73..d1aed673fa3ed3b9e938a9db825bc182b2084e04 100644 (file)
@@ -792,6 +792,12 @@ wxString wxWebViewWebKit::GetPageText()
                     wxConvUTF8);
 }
 
+void wxWebViewWebKit::RunScript(const wxString& javascript)
+{
+    webkit_web_view_execute_script(WEBKIT_WEB_VIEW(web_view), 
+                                   javascript.mb_str(wxConvUTF8));
+}
+
 // static
 wxVisualAttributes
 wxWebViewWebKit::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
index 01d00611462dc2b535343ca451cfff45f2b3a7aa..dbb35fdc3a205c475fb542d7ac2fd1b404570196 100644 (file)
@@ -686,6 +686,22 @@ wxString wxWebViewIE::GetPageText()
     return text;
 }
 
+void wxWebViewIE::RunScript(const wxString& javascript)
+{
+    IHTMLDocument2* document = GetDocument();
+    IHTMLWindow2* window;
+    wxString language = "javascript";
+    HRESULT hr = document->get_parentWindow(&window);
+    if(SUCCEEDED(hr))
+    {
+        VARIANT level;
+        VariantInit(&level);
+        V_VT(&level) = VT_EMPTY;
+        window->execScript(SysAllocString(javascript), SysAllocString(language), &level);
+    }
+    document->Release();
+}
+
 bool wxWebViewIE::CanExecCommand(wxString command)
 {
     IHTMLDocument2* document = GetDocument();
index 9ba00d6c634596f9497db7a5406ee83d1ef4b898..59ede2dbb475641535be98c5b0afb4115b0eb210 100644 (file)
@@ -732,42 +732,13 @@ wxString wxWebViewWebKit::GetSelectedText()
     return wxStringWithNSString(selection);
 }
 
-wxString wxWebViewWebKit::RunScript(const wxString& javascript)
+void wxWebViewWebKit::RunScript(const wxString& javascript)
 {
     if ( !m_webView )
         return wxEmptyString;
 
-    id result = [[m_webView windowScriptObject] evaluateWebScript:
+    [[m_webView windowScriptObject] evaluateWebScript:
                     (NSString*)wxNSStringWithWxString( javascript )];
-
-    NSString* resultAsString;
-    NSString* className = NSStringFromClass([result class]);
-
-    if ([className isEqualToString:@"NSCFNumber"])
-    {
-        resultAsString = [NSString stringWithFormat:@"%@", result];
-    }
-    else if ([className isEqualToString:@"NSCFString"])
-    {
-        resultAsString = result;
-    }
-    else if ([className isEqualToString:@"NSCFBoolean"])
-    {
-        if ([result boolValue])
-            resultAsString = @"true";
-        else
-            resultAsString = @"false";
-    }
-    else if ([className isEqualToString:@"WebScriptObject"])
-    {
-        resultAsString = [result stringRepresentation];
-    }
-    else
-    {
-        return wxString();
-    }
-
-    return wxStringWithNSString( resultAsString );
 }
 
 void wxWebViewWebKit::OnSize(wxSizeEvent &event)
index 8154a9032b9a48f2fe28e83a909b4ac2700ee343..ae4cfe224571521ce30e2b13da621dea3e1ff154 100644 (file)
@@ -43,6 +43,7 @@ private:
         CPPUNIT_TEST( Editable );
         CPPUNIT_TEST( Selection );
         CPPUNIT_TEST( Zoom );
+        CPPUNIT_TEST( RunScript );
     CPPUNIT_TEST_SUITE_END();
 
     void Title();
@@ -54,6 +55,7 @@ private:
     void Editable();
     void Selection();
     void Zoom();
+    void RunScript();
     void LoadUrl(int times = 1);
 
     wxWebView* m_browser;
@@ -236,4 +238,10 @@ void WebTestCase::Zoom()
     }
 }
 
+void WebTestCase::RunScript()
+{
+    m_browser->RunScript("document.write(\"Hello World!\");");
+    CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText());
+}
+
 #endif //wxUSE_WEB