Skip to content

Commit

Permalink
Fix 221, 127, 208
Browse files Browse the repository at this point in the history
Adds TCHAR and tstring to handle unicode in win32 and to be compatible
with linux char.
  • Loading branch information
milani committed Oct 26, 2012
1 parent e80e7bf commit 4abdd9b
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 47 deletions.
4 changes: 3 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@
],
'defines': [
'__WIN__',
'_WINSOCKAPI_'
'_WINSOCKAPI_',
'_UNICODE',
'UNICODE'
],
'link_settings': {
'libraries': [
Expand Down
22 changes: 20 additions & 2 deletions src/appjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

#include <node.h>
#include <node_version.h>
#include <string>

#ifndef __WIN__
typedef char TCHAR;
#define TEXT(t) t
#endif

typedef std::basic_string<TCHAR> tstring;

#define APPJS_PSYMBOL(s) v8::Persistent<v8::String>::New(v8::String::NewSymbol(s))
#define MAKE_BOOLEAN(v) (v)->BooleanValue()
Expand Down Expand Up @@ -68,8 +76,6 @@
constructor = Persistent<Function>::New(tpl->GetFunction())




#define CREATE_INSTANCE_ACCESSOR(Type, PropertyName, GetterType, SetterType) \
void Type::Set##PropertyName(Local<String> property, Local<Value> value, const AccessorInfo& info) { \
Native##Type *obj = ObjectWrap::Unwrap<Native##Type>(info.Holder()); \
Expand All @@ -83,6 +89,18 @@
}


#define CREATE_STRING_INSTANCE_ACCESSOR(Type, PropertyName, GetterType, SetterType) \
void Type::Set##PropertyName(Local<String> property, Local<Value> value, const AccessorInfo& info) { \
Native##Type *obj = ObjectWrap::Unwrap<Native##Type>(info.Holder()); \
obj->Set##PropertyName(SetterType(value->ToString())); \
} \
\
Handle<Value> Type::Get##PropertyName(Local<String> property, const AccessorInfo &info) { \
HandleScope scope; \
Native##Type *obj = ObjectWrap::Unwrap<Native##Type>(info.Holder()); \
return scope.Close(GetterType::New((uint16_t*)obj->Get##PropertyName())); \
}


#define CREATE_PROTOTYPE_INVOKER(Type, Method) \
Handle<Value> Type::Method(const Arguments& args) { \
Expand Down
2 changes: 1 addition & 1 deletion src/appjs_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ CREATE_INSTANCE_ACCESSOR(Window, Left, Integer, MAKE_INT32)
CREATE_INSTANCE_ACCESSOR(Window, Top, Integer, MAKE_INT32)
CREATE_INSTANCE_ACCESSOR(Window, Width, Integer, MAKE_INT32)
CREATE_INSTANCE_ACCESSOR(Window, Height, Integer, MAKE_INT32)
CREATE_INSTANCE_ACCESSOR(Window, Title, String, V8StringToChar)
CREATE_STRING_INSTANCE_ACCESSOR(Window, Title, String, V8StringToWCHAR)
CREATE_INSTANCE_ACCESSOR(Window, Topmost, Boolean, MAKE_BOOLEAN)
CREATE_INSTANCE_ACCESSOR(Window, Resizable, Boolean, MAKE_BOOLEAN)
CREATE_INSTANCE_ACCESSOR(Window, ShowChrome, Boolean, MAKE_BOOLEAN)
Expand Down
2 changes: 1 addition & 1 deletion src/includes/cef_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void ClientHandler::OnContentsSizeChange(CefRefPtr<CefBrowser> browser, CefRefPt
void ClientHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title) {
REQUIRE_UI_THREAD();
if (!browser->IsPopup()) {
std::string titleStr(title);
tstring titleStr(title);
NativeWindow::GetWindow(browser)->SetTitle(titleStr.c_str());
}
}
4 changes: 2 additions & 2 deletions src/includes/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ char* Settings::getString(const char* property, char* defaultValue = "") {
return (tmp->IsString())? V8StringToChar(get(property)->ToString()) : defaultValue;
}

#if defined(__WIN__)
WCHAR* Settings::getString(const char* property, WCHAR* defaultValue = L"") {
#ifdef __WIN__
TCHAR* Settings::getString(const char* property, TCHAR* defaultValue = L"") {
Local<Value> tmp = get(property);
return (tmp->IsString())? V8StringToWCHAR(get(property)->ToString()) : defaultValue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/includes/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Settings {
bool getBoolean(const char*,bool);
char* getString(const char*, char*);

#if defined(__WIN__)
WCHAR* getString(const char*,WCHAR*);
#ifdef __WIN__
TCHAR* getString(const char*,TCHAR*);
#endif

v8::Local<v8::Object> getObject(const char*,v8::Local<v8::Object>);
Expand Down
10 changes: 5 additions & 5 deletions src/native_window/native_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ void NativeWindow::OpenDialog(Settings* settings,Persistent<Function> cb) {
dialog_settings.me = this;
dialog_settings.cb = cb;
dialog_settings.type = (NW_DIALOGTYPE) settings->getInteger("type",NW_DIALOGTYPE_FILE_OPEN);
dialog_settings.title = std::string(settings->getString("title",(char*)this->GetTitle()));
dialog_settings.title = tstring(settings->getString("title",(TCHAR*)this->GetTitle()));
// initialValue is the filename which the dialog should select by default.
// it can be empty.
dialog_settings.initialValue = std::string(settings->getString("initialValue",""));
dialog_settings.initialValue = tstring(settings->getString("initialValue",TEXT("")));

dialog_in_progress = true;
dialog_work.data = &dialog_settings;
Expand All @@ -68,7 +68,7 @@ void NativeWindow::OpenDialog(Settings* settings,Persistent<Function> cb) {

// acceptTypes is comma-separated MIME types such as "text/plain,text/html".
// defaults to everything
dialog_settings.reserveString1 = std::string(settings->getString("acceptTypes","All Files:*.*"));
dialog_settings.reserveString1 = tstring(settings->getString("acceptTypes",TEXT("All Files:*.*")));
// multiSelect allows multiple item selection in dialog box.
dialog_settings.reserveBool1 = settings->getBoolean("multiSelect",false);
// dirSelect is a boolean indicating directory selectation state.
Expand All @@ -81,12 +81,12 @@ void NativeWindow::OpenDialog(Settings* settings,Persistent<Function> cb) {
#endif
} else if( dialog_settings.type == NW_DIALOGTYPE_FONT ) {

dialog_settings.reserveString1 = std::string(settings->getString("sampleText","Sample"));
dialog_settings.reserveString1 = tstring(settings->getString("sampleText",TEXT("Sample")));
uv_queue_work(uv_default_loop(), &dialog_work, OpenFontDialog, ProcessFontDialog);

} else if( dialog_settings.type == NW_DIALOGTYPE_COLOR ) {

dialog_settings.reserveString1 = std::string(settings->getString("previousColor",""));
dialog_settings.reserveString1 = tstring(settings->getString("previousColor",TEXT("")));
dialog_settings.reserveBool1 = settings->getBoolean("opacity",false);
uv_queue_work(uv_default_loop(), &dialog_work, OpenColorDialog, ProcessColorDialog);

Expand Down
18 changes: 7 additions & 11 deletions src/native_window/native_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define APPJS_BASE_NATIVE_WINDOW_H
#pragma once

#include "appjs.h"
#include "include/cef_browser.h"
#include "includes/util.h"
#include <node.h>
Expand Down Expand Up @@ -50,12 +51,7 @@ class NativeWindow {
static NativeWindow* GetWindow(CefWindowHandle handle);
static NativeWindow* GetWindow(CefRefPtr<CefBrowser> browser);

#if defined(__WIN__)
void SetIcon(NW_ICONSIZE size, WCHAR* path);
#else
void SetIcon(NW_ICONSIZE size, char* path);
#endif

void SetIcon(NW_ICONSIZE size, TCHAR* path);
void Emit(v8::Handle<v8::Value>* args);
void Emit(const char* event);
void Emit(const char* event, v8::Handle<v8::Value> arg);
Expand Down Expand Up @@ -109,8 +105,8 @@ class NativeWindow {
int GetWidth();
int GetHeight();

void SetTitle(const char* title);
const char* GetTitle();
void SetTitle(const TCHAR* title);
const TCHAR* GetTitle();

void OpenDialog(Settings* settings,v8::Persistent<v8::Function> cb);
static void DialogClosed();
Expand Down Expand Up @@ -181,9 +177,9 @@ typedef struct _appjs_dialog_settings {
NW_DIALOGTYPE type;
NativeWindow* me;
void* result;
std::string title;
std::string initialValue;
std::string reserveString1;
tstring title;
tstring initialValue;
tstring reserveString1;
int reserveNumber1;
bool reserveBool1;
bool reserveBool2;
Expand Down
44 changes: 22 additions & 22 deletions src/native_window/native_window_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ void BlurBehind(HWND hwnd, bool enable){
}
}

void MakeIcon(HICON* icon, WCHAR* path) {
void MakeIcon(HICON* icon, TCHAR* path) {
Gdiplus::Bitmap* bitmap = Gdiplus::Bitmap::FromFile(path);
if (bitmap->GetWidth()) {
bitmap->GetHICON(icon);
delete bitmap;
}
}

HICON MakeIcon(WCHAR* path) {
HICON MakeIcon(TCHAR* path) {
HICON icon;
MakeIcon(&icon, path);
return icon;
Expand Down Expand Up @@ -187,8 +187,8 @@ void NativeWindow::Init(char* url, Settings* settings) {
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

WCHAR* wSmallIconPath = icons->getString("small", L"");
WCHAR* wBigIconPath = icons->getString("big", L"");
TCHAR* wSmallIconPath = icons->getString("small", TEXT(""));
TCHAR* wBigIconPath = icons->getString("big", TEXT(""));

smallIcon = MakeIcon(wSmallIconPath);
bigIcon = MakeIcon(wSmallIconPath);
Expand All @@ -197,7 +197,7 @@ void NativeWindow::Init(char* url, Settings* settings) {
delete[] wBigIconPath;

hInstance = (HINSTANCE)GetCurrentModuleHandle();
strcpy(szWindowClass, "AppjsWindow");
wcscpy(szWindowClass, TEXT("AppjsWindow"));
MyRegisterClass(hInstance);
}

Expand All @@ -208,7 +208,7 @@ void NativeWindow::Init(char* url, Settings* settings) {
rect_.top = (GetSystemMetrics(SM_CYSCREEN) - rect_.height) / 2;
}
browser_ = NULL;
handle_ = CreateWindowEx(NULL, szWindowClass, "", WS_OVERLAPPEDWINDOW,
handle_ = CreateWindowEx(NULL, szWindowClass, TEXT(""), WS_OVERLAPPEDWINDOW,
rect_.left, rect_.top, rect_.width, rect_.height,
NULL, NULL, hInstance, NULL);

Expand Down Expand Up @@ -316,7 +316,7 @@ void NativeWindow::Fullscreen(){
// }


void NativeWindow::SetIcon(NW_ICONSIZE size, WCHAR* path) {
void NativeWindow::SetIcon(NW_ICONSIZE size, TCHAR* path) {
int flag;
switch (size) {
case NW_ICONSIZE_SMALLER: flag = ICON_SMALL; break;
Expand All @@ -327,13 +327,13 @@ void NativeWindow::SetIcon(NW_ICONSIZE size, WCHAR* path) {
SendMessage(handle_, WM_SETICON, flag, (LPARAM)MakeIcon(path));
}

const char* NativeWindow::GetTitle() {
const TCHAR* NativeWindow::GetTitle() {
TCHAR title[80];
GetWindowText(handle_, title, 80);
return title;
}

void NativeWindow::SetTitle(const char* title) {
void NativeWindow::SetTitle(const TCHAR* title) {
SetWindowText(handle_, title);
}

Expand Down Expand Up @@ -567,12 +567,12 @@ int CALLBACK DirectorySelectHook(HWND hwnd, UINT msg, LPARAM lParam, LPARAM data

void NativeWindow::OpenFileDialog(uv_work_t* req) {
AppjsDialogSettings* settings = (AppjsDialogSettings*)req->data;
std::string acceptTypes = settings->reserveString1;
tstring acceptTypes = settings->reserveString1;
bool multiSelect = settings->reserveBool1;
bool dirSelect = settings->reserveBool2;

settings->result = NULL;
char filename[MAX_PATH*10];
TCHAR filename[MAX_PATH*10];
ZeroMemory(&filename, sizeof(filename));

if (dirSelect) {
Expand All @@ -585,15 +585,15 @@ void NativeWindow::OpenFileDialog(uv_work_t* req) {
bi.pszDisplayName = filename;
bi.lpszTitle = settings->title.c_str();
bi.ulFlags = BIF_USENEWUI | BIF_BROWSEFILEJUNCTIONS | BIF_RETURNONLYFSDIRS | BIF_RETURNFSANCESTORS;
bi.lParam = (LPARAM)ToWChar(settings->initialValue);
bi.lParam = (LPARAM)settings->initialValue.c_str();
bi.iImage = -1;
bi.lpfn = DirectorySelectHook;

LPITEMIDLIST item;
if (item = SHBrowseForFolder(&bi)) {
char dir[MAX_PATH];
TCHAR dir[MAX_PATH];
if (SHGetPathFromIDList(item, dir)) {
std::vector<char*> paths;
std::vector<TCHAR*> paths;
paths.push_back(dir);
settings->result = &paths;
}
Expand All @@ -602,7 +602,7 @@ void NativeWindow::OpenFileDialog(uv_work_t* req) {
} else {
std::replace(acceptTypes.begin(), acceptTypes.end(), ':', '\0');
std::replace(acceptTypes.begin(), acceptTypes.end(), ',', '\0');
acceptTypes += '\0';
acceptTypes += L'\0';

OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
Expand All @@ -616,7 +616,7 @@ void NativeWindow::OpenFileDialog(uv_work_t* req) {
ofn.lpstrInitialDir = settings->initialValue.c_str();
ofn.lpstrFile = filename;
} else {
strcpy(filename, settings->initialValue.c_str());
wcscpy(filename, settings->initialValue.c_str());
ofn.lpstrFile = filename;
}

Expand Down Expand Up @@ -645,16 +645,16 @@ void NativeWindow::ProcessFileDialog(uv_work_t* req) {

if (result != NULL) {

std::vector<char*> filenames;
char* offset = (char*)result;
std::vector<TCHAR*> filenames;
TCHAR* offset = (TCHAR*)result;

do {
filenames.push_back(offset);
offset += strlen(offset) + 1;
offset += wcslen(offset) + 1;
} while (offset[0] != '\0');

std::vector<char*>::iterator file = filenames.begin();
Handle<String> base = String::New(*file);
std::vector<TCHAR*>::iterator file = filenames.begin();
Handle<String> base = String::New((uint16_t*)*file);
Handle<Value> error = Undefined();
Local<Array> files;

Expand All @@ -667,7 +667,7 @@ void NativeWindow::ProcessFileDialog(uv_work_t* req) {
int index = 0;

for (file++; file != filenames.end(); ++file) {
files->Set(index, String::Concat(base, String::New(*file)));
files->Set(index, String::Concat(base, String::New((uint16_t*)*file)));
index++;
}
}
Expand Down

0 comments on commit 4abdd9b

Please sign in to comment.