From 4a13954e98cc4d9793211fdafc363d06dcd67055 Mon Sep 17 00:00:00 2001 From: Tim Gfrerer Date: Tue, 20 Mar 2012 13:28:07 +0000 Subject: [PATCH] added executeJavascript() method, and string2wstring() helper --- src/ofxBerkelium.h | 33 ++++++++++++------ src/ofxBerkeliumUtil.h | 79 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 13 deletions(-) diff --git a/src/ofxBerkelium.h b/src/ofxBerkelium.h index 0efd8b1..493b119 100644 --- a/src/ofxBerkelium.h +++ b/src/ofxBerkelium.h @@ -25,7 +25,7 @@ class ofxBerkelium : Berkelium::WindowDelegate { public: ofxBerkelium(unsigned int _w, unsigned int _h, bool _usetrans); ~ofxBerkelium(); - + void setListener( ofxBerkeliumListener* listener ); Berkelium::Window* getWindow(); Berkelium::Window* window() const { @@ -34,7 +34,7 @@ class ofxBerkelium : Berkelium::WindowDelegate { void clear(); void draw(float x, float y); void draw(float x, float y, float w, float h); - + void keyboard(int key, bool pressed); void mouseMoved(int x, int y ); void mouseClick(int x, int y, int button, bool pressed); @@ -42,21 +42,32 @@ class ofxBerkelium : Berkelium::WindowDelegate { unsigned char* getPixels() { // TO DO: Implement this, preferably without getting the pixels out of the texture (too slow?) } - + + void executeJavaScript(std::string& script){ + Berkelium::WideString tmpString; + + std::wstring widestring = string2wstring(script); + + tmpString = Berkelium::WideString::point_to(widestring); + + bk_window->executeJavascript(tmpString); + + } + void back() { bk_window->goBack(); } - + void forward() { bk_window->goForward(); } - + string status; string title; string addressBar; - + #pragma mark CALLBACKS - + void onPaint(Berkelium::Window* wini, const unsigned char* bitmap_in, const Berkelium::Rect& bitmap_rect, size_t num_copy_rects, const Berkelium::Rect* copy_rects, int dx, int dy, const Berkelium::Rect& scroll_rect); void onAddressBarChanged(Berkelium::Window *win, Berkelium::URLString newURL); void onStartLoading(Berkelium::Window *win, Berkelium::URLString newURL); @@ -82,7 +93,7 @@ class ofxBerkelium : Berkelium::WindowDelegate { void onRunFileChooser(Berkelium::Window *win, int mode, Berkelium::WideString title, Berkelium::FileString defaultFile); void onExternalHost(Berkelium::Window *win, Berkelium::WideString message, Berkelium::URLString origin, Berkelium::URLString target); - + private: bool mapOnPaintToTexture(Berkelium::Window *wini, const unsigned char* bitmap_in, const Berkelium::Rect& bitmap_rect, @@ -90,7 +101,7 @@ class ofxBerkelium : Berkelium::WindowDelegate { int dx, int dy, const Berkelium::Rect& scroll_rect, bool ignore_partial); - + // The Berkelium window, i.e. our web page Berkelium::Window* bk_window; // Width and height of our window. @@ -101,7 +112,7 @@ class ofxBerkelium : Berkelium::WindowDelegate { bool needs_full_refresh; // Buffer used to store data for scrolling char* scroll_buffer; - // The testApp (or some other delagate class) that receives events from the browser + // The testApp (or some other delagate class) that receives events from the browser ofxBerkeliumListener* listener; -}; \ No newline at end of file +}; diff --git a/src/ofxBerkeliumUtil.h b/src/ofxBerkeliumUtil.h index 133a4d9..9d2af53 100644 --- a/src/ofxBerkeliumUtil.h +++ b/src/ofxBerkeliumUtil.h @@ -48,7 +48,7 @@ inline unsigned int isASCIISpecialToBerkelium(unsigned int glut_char) { unsigned char ASCII_TAB = 9; unsigned char ASCII_ESCAPE = 27; unsigned char ASCII_DELETE = 127; - + return (glut_char == ASCII_BACKSPACE || glut_char == ASCII_TAB || glut_char == ASCII_ESCAPE || @@ -68,7 +68,7 @@ enum VirtKeys { BK_KEYCODE_DOWN = 0x28, BK_KEYCODE_INSERT = 0x2D, BK_KEYCODE_DELETE = 0x2E - + }; /** Given an input key from GLUT, convert it to a form that can be passed to @@ -97,3 +97,78 @@ inline string wstring2string(const wstring& ws) { string ns(ws.begin(), ws.end()); return ns; } + + +/** tig: The follwing string2wstring method is based on: + * + * + * Copyright (c) 2009 SegFault aka "ErV" (altctrlbackspace.blogspot.com) + * + * Redistribution and use of this source code, with or without modification, is + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +inline std::wstring string2wstring(const string& src){ + wstring dest; + + dest.clear(); + wchar_t w = 0; + int bytes = 0; + wchar_t err = L'?'; + for (size_t i = 0; i < src.size(); i++){ + unsigned char c = (unsigned char)src[i]; + if (c <= 0x7f){//first byte + if (bytes){ + dest.push_back(err); + bytes = 0; + } + dest.push_back((wchar_t)c); + } + else if (c <= 0xbf){//second/third/etc byte + if (bytes){ + w = ((w << 6)|(c & 0x3f)); + bytes--; + if (bytes == 0) + dest.push_back(w); + } + else + dest.push_back(err); + } + else if (c <= 0xdf){//2byte sequence start + bytes = 1; + w = c & 0x1f; + } + else if (c <= 0xef){//3byte sequence start + bytes = 2; + w = c & 0x0f; + } + else if (c <= 0xf7){//3byte sequence start + bytes = 3; + w = c & 0x07; + } + else{ + dest.push_back(err); + bytes = 0; + } + } + if (bytes) + dest.push_back(err); + + + return dest; +}