Skip to content

Commit

Permalink
added executeJavascript() method, and string2wstring() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tgfrerer committed Mar 20, 2012
1 parent 3bcbe87 commit 4a13954
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 13 deletions.
33 changes: 22 additions & 11 deletions src/ofxBerkelium.h
Expand Up @@ -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 {
Expand All @@ -34,29 +34,40 @@ 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);

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);
Expand All @@ -82,15 +93,15 @@ 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,
size_t num_copy_rects, const Berkelium::Rect *copy_rects,
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.
Expand All @@ -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;

};
};
79 changes: 77 additions & 2 deletions src/ofxBerkeliumUtil.h
Expand Up @@ -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 ||
Expand All @@ -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
Expand Down Expand Up @@ -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:
* <http://altctrlbackspace.blogspot.com/2009/03/c-utf8-to-wstring-conversion-routine.html>
*
* 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;
}

0 comments on commit 4a13954

Please sign in to comment.