Switch to tinyfiledialogs, implement unicode on Windows

This commit is contained in:
Joshua Granick
2016-12-27 17:25:33 -08:00
parent 4956adc595
commit ec60520d52
7 changed files with 258 additions and 123 deletions

View File

@@ -98,6 +98,22 @@ namespace lime {
}
std::wstring* hxstring_to_wstring (HxString val) {
if (val.c_str ()) {
std::string _val = std::string (val.c_str ());
return new std::wstring (_val.begin (), _val.end ());
} else {
return 0;
}
}
value lime_application_create (value callback) {
Application* application = CreateApplication ();
@@ -318,63 +334,89 @@ namespace lime {
value lime_file_dialog_open_directory (HxString filter, HxString defaultPath) {
#ifdef LIME_NFD
const char* path = FileDialog::OpenDirectory (filter.c_str (), defaultPath.c_str ());
#ifdef LIME_TINYFILEDIALOGS
std::wstring* _filter = hxstring_to_wstring (filter);
std::wstring* _defaultPath = hxstring_to_wstring (defaultPath);
std::wstring* path = FileDialog::OpenDirectory (_filter, _defaultPath);
if (_filter) delete _filter;
if (_defaultPath) delete _defaultPath;
if (path) {
value _path = alloc_string (path);
free ((char*) path);
value _path = alloc_wstring (path->c_str ());
delete path;
return _path;
} else {
delete path;
return alloc_null ();
}
#endif
return 0;
return alloc_null ();
}
value lime_file_dialog_open_file (HxString filter, HxString defaultPath) {
#ifdef LIME_NFD
const char* path = FileDialog::OpenFile (filter.c_str (), defaultPath.c_str ());
#ifdef LIME_TINYFILEDIALOGS
std::wstring* _filter = hxstring_to_wstring (filter);
std::wstring* _defaultPath = hxstring_to_wstring (defaultPath);
std::wstring* path = FileDialog::OpenFile (_filter, _defaultPath);
if (_filter) delete _filter;
if (_defaultPath) delete _defaultPath;
if (path) {
value _path = alloc_string (path);
free ((char*) path);
value _path = alloc_wstring (path->c_str ());
delete path;
return _path;
} else {
delete path;
return alloc_null ();
}
#endif
return 0;
return alloc_null ();
}
value lime_file_dialog_open_files (HxString filter, HxString defaultPath) {
#ifdef LIME_NFD
std::vector<const char*> files;
#ifdef LIME_TINYFILEDIALOGS
FileDialog::OpenFiles (&files, filter.c_str (), defaultPath.c_str ());
std::wstring* _filter = hxstring_to_wstring (filter);
std::wstring* _defaultPath = hxstring_to_wstring (defaultPath);
std::vector<std::wstring*> files;
FileDialog::OpenFiles (&files, _filter, _defaultPath);
value result = alloc_array (files.size ());
if (_filter) delete _filter;
if (_defaultPath) delete _defaultPath;
for (int i = 0; i < files.size (); i++) {
val_array_set_i (result, i, alloc_string (files[i]));
free ((char*)files[i]);
val_array_set_i (result, i, alloc_wstring (files[i]->c_str ()));
delete files[i];
}
#else
value result = alloc_array (0);
#endif
@@ -386,23 +428,32 @@ namespace lime {
value lime_file_dialog_save_file (HxString filter, HxString defaultPath) {
#ifdef LIME_NFD
const char* path = FileDialog::SaveFile (filter.c_str (), defaultPath.c_str ());
#ifdef LIME_TINYFILEDIALOGS
std::wstring* _filter = hxstring_to_wstring (filter);
std::wstring* _defaultPath = hxstring_to_wstring (defaultPath);
std::wstring* path = FileDialog::SaveFile (_filter, _defaultPath);
if (_filter) delete _filter;
if (_defaultPath) delete _defaultPath;
if (path) {
value _path = alloc_string (path);
free ((char*) path);
value _path = alloc_wstring (path->c_str ());
delete path;
return _path;
} else {
delete path;
return alloc_null ();
}
#endif
return 0;
return alloc_null ();
}
@@ -413,7 +464,7 @@ namespace lime {
Font *font = (Font*)val_data (fontHandle);
return font->GetAscender ();
#else
return 0;
return alloc_null ();
#endif
}
@@ -425,7 +476,7 @@ namespace lime {
Font *font = (Font*)val_data (fontHandle);
return font->GetDescender ();
#else
return 0;
return alloc_null ();
#endif
}
@@ -440,7 +491,7 @@ namespace lime {
delete name;
return result;
#else
return 0;
return alloc_null ();
#endif
}
@@ -488,7 +539,7 @@ namespace lime {
Font *font = (Font*)val_data (fontHandle);
return font->GetHeight ();
#else
return 0;
return alloc_null ();
#endif
}
@@ -512,7 +563,7 @@ namespace lime {
Font *font = (Font*)val_data (fontHandle);
return font->GetUnderlinePosition ();
#else
return 0;
return alloc_null ();
#endif
}
@@ -524,7 +575,7 @@ namespace lime {
Font *font = (Font*)val_data (fontHandle);
return font->GetUnderlineThickness ();
#else
return 0;
return alloc_null ();
#endif
}
@@ -536,7 +587,7 @@ namespace lime {
Font *font = (Font*)val_data (fontHandle);
return font->GetUnitsPerEM ();
#else
return 0;
return alloc_null ();
#endif
}

View File

@@ -1,124 +1,207 @@
#include <ui/FileDialog.h>
#include <nfd.h>
#include <stdio.h>
#include <tinyfiledialogs.h>
namespace lime {
const char* FileDialog::OpenDirectory (const char* filter, const char* defaultPath) {
std::wstring* FileDialog::OpenDirectory (std::wstring* filter, std::wstring* defaultPath) {
nfdchar_t *savePath = 0;
nfdresult_t result = NFD_OpenDirectoryDialog (filter, defaultPath, &savePath);
// TODO: Filters
switch (result) {
#ifdef HX_WINDOWS
const wchar_t* path = tinyfd_selectFolderDialogW (L"", defaultPath ? defaultPath->c_str () : 0);
if (path) {
case NFD_OKAY:
return savePath;
break;
case NFD_CANCEL:
break;
default:
printf ("Error: %s\n", NFD_GetError ());
break;
std::wstring* _path = new std::wstring (path);
return _path;
}
return savePath;
#else
char* _defaultPath = 0;
if (defaultPath) {
int size = std::wcslen (defaultPath->c_str ());
char* _defaultPath = (char*)malloc (size);
std::wcstombs (_defaultPath, defaultPath->c_str (), size);
}
const char* path = tinyfd_selectFolderDialog ("", _defaultPath);
if (_defaultPath) free (_defaultPath);
if (path) {
std::string _path = std::string (path);
std::wstring* __path = new std::wstring (_path.begin (), _path.end ());
return __path;
}
#endif
return 0;
}
const char* FileDialog::OpenFile (const char* filter, const char* defaultPath) {
std::wstring* FileDialog::OpenFile (std::wstring* filter, std::wstring* defaultPath) {
nfdchar_t *savePath = 0;
nfdresult_t result = NFD_OpenDialog (filter, defaultPath, &savePath);
// TODO: Filters
switch (result) {
#ifdef HX_WINDOWS
const wchar_t* path = tinyfd_openFileDialogW (L"", defaultPath ? defaultPath->c_str () : 0, 0, NULL, NULL, 0);
if (path) {
case NFD_OKAY:
return savePath;
break;
case NFD_CANCEL:
break;
default:
printf ("Error: %s\n", NFD_GetError ());
break;
std::wstring* _path = new std::wstring (path);
return _path;
}
return savePath;
#else
char* _defaultPath = 0;
if (defaultPath) {
int size = std::wcslen (defaultPath->c_str ());
char* _defaultPath = (char*)malloc (size);
std::wcstombs (_defaultPath, defaultPath->c_str (), size);
}
const char* path = tinyfd_openFileDialog ("", _defaultPath, 0, NULL, NULL, 0);
if (_defaultPath) free (_defaultPath);
if (path) {
std::string _path = std::string (path);
std::wstring* __path = new std::wstring (_path.begin (), _path.end ());
return __path;
}
#endif
return 0;
}
void FileDialog::OpenFiles (std::vector<const char*>* files, const char* filter, const char* defaultPath) {
void FileDialog::OpenFiles (std::vector<std::wstring*>* files, std::wstring* filter, std::wstring* defaultPath) {
nfdpathset_t pathSet;
nfdresult_t result = NFD_OpenDialogMultiple (filter, defaultPath, &pathSet);
// TODO: Filters
switch (result) {
std::wstring* __paths = 0;
#ifdef HX_WINDOWS
const wchar_t* paths = tinyfd_openFileDialogW (L"", defaultPath ? defaultPath->c_str () : 0, 0, NULL, NULL, 1);
if (paths) {
case NFD_OKAY:
{
for (int i = 0; i < NFD_PathSet_GetCount (&pathSet); i++) {
files->push_back (NFD_PathSet_GetPath (&pathSet, i));
}
__paths = new std::wstring (paths);
}
#else
char* _defaultPath = 0;
if (defaultPath) {
int size = std::wcslen (defaultPath->c_str ());
char* _defaultPath = (char*)malloc (size);
std::wcstombs (_defaultPath, defaultPath->c_str (), size);
}
const char* paths = tinyfd_openFileDialog ("", _defaultPath, 0, NULL, NULL, 1);
if (_defaultPath) free (_defaultPath);
if (paths) {
std::string _paths = std::string (paths);
__paths = new std::wstring (_paths.begin (), _paths.end ());
}
#endif
if (__paths) {
std::wstring sep = L"|";
std::size_t start = 0, end = 0;
while ((end = __paths->find (sep, start)) != std::wstring::npos) {
files->push_back (new std::wstring (__paths->substr (start, end - start).c_str ()));
start = end + 1;
NFD_PathSet_Free (&pathSet);
break;
}
case NFD_CANCEL:
break;
default:
printf ("Error: %s\n", NFD_GetError ());
break;
files->push_back (new std::wstring (__paths->substr (start).c_str ()));
}
}
const char* FileDialog::SaveFile (const char* filter, const char* defaultPath) {
std::wstring* FileDialog::SaveFile (std::wstring* filter, std::wstring* defaultPath) {
nfdchar_t *savePath = 0;
nfdresult_t result = NFD_SaveDialog (filter, defaultPath, &savePath);
// TODO: Filters
switch (result) {
#ifdef HX_WINDOWS
const wchar_t* path = tinyfd_saveFileDialogW (L"", defaultPath ? defaultPath->c_str () : 0, 0, NULL, NULL);
if (path) {
case NFD_OKAY:
return savePath;
break;
case NFD_CANCEL:
break;
default:
printf ("Error: %s\n", NFD_GetError ());
break;
std::wstring* _path = new std::wstring (path);
return _path;
}
return savePath;
#else
char* _defaultPath = 0;
if (defaultPath) {
int size = std::wcslen (defaultPath->c_str ());
char* _defaultPath = (char*)malloc (size);
std::wcstombs (_defaultPath, defaultPath->c_str (), size);
}
const char* path = tinyfd_saveFileDialog ("", _defaultPath, 0, NULL, NULL);
if (_defaultPath) free (_defaultPath);
if (path) {
std::string _path = std::string (path);
std::wstring* __path = new std::wstring (_path.begin (), _path.end ());
return __path;
}
#endif
return 0;
}