diff --git a/src/configep2.cpp b/src/configep2.cpp index 0c14fc2..8093333 100644 --- a/src/configep2.cpp +++ b/src/configep2.cpp @@ -42,8 +42,6 @@ #define K 1024u - - static std::uint16_t memory_block_size(const std::string &block_size) { if (block_size == "4K") { return 4u*K; @@ -113,6 +111,8 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis } if (path.empty()) { + std::cout << "standard config file location: " ETCDIR "/epple2/epple2.conf" << std::endl; + /* On Windows, the default directory will be C:\Program Files\Epple2 if they start the @@ -172,7 +172,7 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis } if (path.empty()) { - std::cerr << "Cannot open config file /etc/epple2/epple2.conf" << std::endl; + std::cerr << "Cannot find config file. Running without any RAM, ROM, or cards." << std::endl; return; } diff --git a/src/gui.cpp b/src/gui.cpp index 056319c..7ba343b 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -15,10 +15,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#ifdef HAVE_CONFIG_H -#include "../config.h" -#endif - #include #include "gui.h" #include diff --git a/src/main.cpp b/src/main.cpp index 75b006d..899351e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,10 +15,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#ifdef HAVE_CONFIG_H -#include "../config.h" -#endif - #include #include "emulator.h" #include "configep2.h" diff --git a/src/ptr_less.h b/src/ptr_less.h index 59f1b09..c195da3 100644 --- a/src/ptr_less.h +++ b/src/ptr_less.h @@ -11,7 +11,7 @@ template struct ptr_less { - bool operator()(T* pa, T* pb) { + bool operator()(T* pa, T* pb) const { return *pa < *pb; } diff --git a/src/tinyfiledialogs.cpp b/src/tinyfiledialogs.cpp index 5a19ca6..bdfc607 100644 --- a/src/tinyfiledialogs.cpp +++ b/src/tinyfiledialogs.cpp @@ -1,84 +1,38 @@ +/* this file can be renamed with extension ".cpp" and compiled as C++. +The code is 100% compatible C C++ +(just comment out << extern "C" >> in the header file) */ + /*_________ - / \ tinyfiledialogs.c v3.3.8 [Nov 4, 2018] zlib licence + / \ tinyfiledialogs.c v3.8.9 [Oct 27, 2022] zlib licence |tiny file| Unique code file created [November 9, 2014] - | dialogs | Copyright (c) 2014 - 2018 Guillaume Vareille http://ysengrin.com + | dialogs | Copyright (c) 2014 - 2021 Guillaume Vareille http://ysengrin.com \____ ___/ http://tinyfiledialogs.sourceforge.net \| git clone http://git.code.sf.net/p/tinyfiledialogs/code tinyfd - ____________________________________________ - | | - | email: tinyfiledialogs at ysengrin.com | - |____________________________________________| - ___________________________________________________________________ - | | - | the windows only wchar_t UTF-16 prototypes are in the header file | - |___________________________________________________________________| + ____________________________________________ + | | + | email: tinyfiledialogs at ysengrin.com | + |____________________________________________| + _________________________________________________________________________________ + | | + | the windows only wchar_t UTF-16 prototypes are at the bottom of the header file | + |_________________________________________________________________________________| + _________________________________________________________ + | | + | on windows: - since v3.6 char is UTF-8 by default | + | - if you want MBCS set tinyfd_winUtf8 to 0 | + | - functions like fopen expect MBCS | + |_________________________________________________________| -Please upvote my stackoverflow answer https://stackoverflow.com/a/47651444 -[Chris Mosher: upvoted!] - -tiny file dialogs (cross-platform C C++) -InputBox PasswordBox MessageBox ColorPicker -OpenFileDialog SaveFileDialog SelectFolderDialog -Native dialog library for WINDOWS MAC OSX GTK+ QT CONSOLE & more -SSH supported via automatic switch to console mode or X11 forwarding - -one C file + a header (add them to your C or C++ project) with 8 functions: -- beep -- notify popup (tray) -- message & question -- input & password -- save file -- open file(s) -- select folder -- color picker - -Complements OpenGL Vulkan GLFW GLUT GLUI VTK SFML TGUI -SDL Ogre Unity3d ION OpenCV CEGUI MathGL GLM CPW GLOW -Open3D IMGUI MyGUI GLT NGL STB & GUI less programs - -NO INIT -NO MAIN LOOP -NO LINKING -NO INCLUDE - -The dialogs can be forced into console mode - -Windows (XP to 10) ASCII MBCS UTF-8 UTF-16 -- native code & vbs create the graphic dialogs -- enhanced console mode can use dialog.exe from -http://andrear.altervista.org/home/cdialog.php -- basic console input - -Unix (command line calls) ASCII UTF-8 -- applescript, kdialog, zenity -- python (2 or 3) + tkinter + python-dbus (optional) -- dialog (opens a console if needed) -- basic console input -The same executable can run across desktops & distributions - -C89 & C++98 compliant: tested with C & C++ compilers -VisualStudio MinGW-gcc GCC Clang TinyCC OpenWatcom-v2 BorlandC SunCC ZapCC -on Windows Mac Linux Bsd Solaris Minix Raspbian -using Gnome Kde Enlightenment Mate Cinnamon Budgie Unity Lxde Lxqt Xfce -WindowMaker IceWm Cde Jds OpenBox Awesome Jwm Xdm - -Bindings for LUA and C# dll, Haskell -Included in LWJGL(java), Rust, Allegrobasic - -Thanks for contributions, bug corrections & thorough testing to: -- Don Heyse http://ldglite.sf.net for bug corrections & thorough testing! -- Paul Rouget +If you like tinyfiledialogs, please upvote my stackoverflow answer +https://stackoverflow.com/a/47651444 - License - - This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. - Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: - 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be @@ -86,11 +40,25 @@ appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. +----------- + +Thanks for contributions, bug corrections & thorough testing to: +- Don Heyse http://ldglite.sf.net for bug corrections & thorough testing! +- Paul Rouget */ + #ifndef __sun +#ifndef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 2 /* to accept POSIX 2 in old ANSI C standards */ #endif +#endif + +#if !defined(_WIN32) && ( defined(__GNUC__) || defined(__clang__) ) +#if !defined(_GNU_SOURCE) + #define _GNU_SOURCE /* used only to resolve symbolic links. Can be commented out */ +#endif +#endif #include #include @@ -98,9 +66,6 @@ misrepresented as being the original software. #include #include -#include "tinyfiledialogs.h" -/* #define TINYFD_NOLIB */ - #ifdef _WIN32 #ifdef __BORLANDC__ #define _getch getch @@ -108,18 +73,13 @@ misrepresented as being the original software. #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0500 #endif - #ifndef TINYFD_NOLIB - #include - /*#define TINYFD_NOSELECTFOLDERWIN*/ - #ifndef TINYFD_NOSELECTFOLDERWIN - #include - #endif /*TINYFD_NOSELECTFOLDERWIN*/ - #endif - #include + #include #include + #include + #include + #include #define TINYFD_NOCCSUNICODE #define SLASH "\\" - int tinyfd_winUtf8 = 0 ; /* on windows string char can be 0:MBCS or 1:UTF-8 */ #else #include #include @@ -130,25 +90,46 @@ misrepresented as being the original software. #define SLASH "/" #endif /* _WIN32 */ +#include "tinyfiledialogs.h" + #define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */ -#define MAX_MULTIPLE_FILES 32 -char const tinyfd_version [8] = "3.3.8"; - -int tinyfd_verbose = 1 ; /* on unix: prints the command line calls */ -int tinyfd_silent = 0 ; /* 1 (default) or 0 : on unix, - hide errors and warnings from called dialog*/ - -#if defined(TINYFD_NOLIB) && defined(_WIN32) -int tinyfd_forceConsole = 1 ; -#else -int tinyfd_forceConsole = 0 ; /* 0 (default) or 1 */ +#ifndef MAX_MULTIPLE_FILES +#define MAX_MULTIPLE_FILES 1024 #endif +#define LOW_MULTIPLE_FILES 32 + +char tinyfd_version[8] = "3.8.9"; + +/******************************************************************************************************/ +/**************************************** UTF-8 on Windows ********************************************/ +/******************************************************************************************************/ +#ifdef _WIN32 +/* if you want to use UTF-8 ( instead of the UTF-16/wchar_t functions at the end of tinyfiledialogs.h ) +Make sure your code is really prepared for UTF-8 (on windows, functions like fopen() expect MBCS and not UTF-8) */ +int tinyfd_winUtf8 = 1; /* on windows char strings can be 1:UTF-8(default) or 0:MBCS */ +/* for MBCS change this to 0, here or in your code */ +#endif +/******************************************************************************************************/ +/******************************************************************************************************/ +/******************************************************************************************************/ + +int tinyfd_verbose = 0 ; /* on unix: prints the command line calls */ +int tinyfd_silent = 1 ; /* 1 (default) or 0 : on unix, hide errors and warnings from called dialogs */ + +/* Curses dialogs are difficult to use, on windows they are only ascii and uses the unix backslah */ +int tinyfd_allowCursesDialogs = 0 ; /* 0 (default) or 1 */ +int tinyfd_forceConsole = 0 ; /* 0 (default) or 1 */ /* for unix & windows: 0 (graphic mode) or 1 (console mode). 0: try to use a graphic solution, if it fails then it uses console mode. -1: forces all dialogs into console mode even when the X server is present, - if the package dialog (and a console is present) or dialog.exe is installed. - on windows it only make sense for console applications */ +1: forces all dialogs into console mode even when the X server is present. + it can use the package dialog or dialog.exe. + on windows it only make sense for console applications */ + +int tinyfd_assumeGraphicDisplay = 0; /* 0 (default) or 1 */ +/* some systems don't set the environment variable DISPLAY even when a graphic display is present. +set this to 1 to tell tinyfiledialogs to assume the existence of a graphic display */ + char tinyfd_response[1024]; /* if you pass "tinyfd_query" as aTitle, @@ -157,23 +138,17 @@ but and return 0 for console mode, 1 for graphic mode. tinyfd_response is then filled with the retain solution. possible values for tinyfd_response are (all lowercase) for graphic mode: - windows_wchar windows - applescript kdialog zenity zenity3 matedialog qarma - python2-tkinter python3-tkinter python-dbus perl-dbus - gxmessage gmessage xmessage xdialog gdialog + windows_wchar windows applescript kdialog zenity zenity3 matedialog + shellementary qarma yad python2-tkinter python3-tkinter python-dbus + perl-dbus gxmessage gmessage xmessage xdialog gdialog for console mode: dialog whiptail basicinput no_solution */ -#if defined(TINYFD_NOLIB) && defined(_WIN32) -static int gWarningDisplayed = 1 ; -#else static int gWarningDisplayed = 0 ; -#endif - -static char const gTitle[]="missing software! (we will try basic console input)"; +static char gTitle[]="missing software! (we will try basic console input)"; #ifdef _WIN32 -char const tinyfd_needs[] = "\ +char tinyfd_needs[] = "\ ___________\n\ / \\ \n\ | tiny file |\n\ @@ -182,10 +157,10 @@ char const tinyfd_needs[] = "\ \\|\ \ntiny file dialogs on Windows needs:\ \n a graphic display\ -\nor dialog.exe (enhanced console mode)\ +\nor dialog.exe (curses console mode)\ \nor a console for basic input"; #else -char const tinyfd_needs[] = "\ +char tinyfd_needs[] = "\ ___________\n\ / \\ \n\ | tiny file |\n\ @@ -193,14 +168,11 @@ char const tinyfd_needs[] = "\ \\_____ ____/\n\ \\|\ \ntiny file dialogs on UNIX needs:\ -\n applescript\ -\nor kdialog\ -\nor zenity (or matedialog or qarma)\ -\nor python (2 or 3)\ -\n + tkinter + python-dbus (optional)\ -\nor dialog (opens console if needed)\ -\nor xterm + bash\ -\n (opens console for basic input)\ +\n applescript or kdialog or yad or Xdialog\ +\nor zenity (or matedialog or shellementary or qarma)\ +\nor python (2 or 3) + tkinter + python-dbus (optional)\ +\nor dialog (opens console if needed) ** Disabled by default **/\ +\nor xterm + bash (opens console for basic input)\ \nor existing console for basic input"; #endif @@ -210,9 +182,22 @@ char const tinyfd_needs[] = "\ #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ #endif +static int getenvDISPLAY(void) +{ + return tinyfd_assumeGraphicDisplay || getenv("DISPLAY"); +} + + +static char * getCurDir(void) +{ + static char lCurDir[MAX_PATH_OR_CMD]; + return getcwd(lCurDir, sizeof(lCurDir)); +} + + static char * getPathWithoutFinalSlash( - char * const aoDestination, /* make sure it is allocated, use _MAX_PATH */ - char const * const aSource) /* aoDestination and aSource can be the same */ + char * aoDestination, /* make sure it is allocated, use _MAX_PATH */ + char const * aSource) /* aoDestination and aSource can be the same */ { char const * lTmp ; if ( aSource ) @@ -241,8 +226,8 @@ static char * getPathWithoutFinalSlash( static char * getLastName( - char * const aoDestination, /* make sure it is allocated */ - char const * const aSource) + char * aoDestination, /* make sure it is allocated */ + char const * aSource) { /* copy the last name after '/' or '\' */ char const * lTmp ; @@ -270,7 +255,7 @@ static char * getLastName( } -static void ensureFinalSlash( char * const aioString ) +static void ensureFinalSlash( char * aioString ) { if ( aioString && strlen( aioString ) ) { @@ -283,10 +268,9 @@ static void ensureFinalSlash( char * const aioString ) } -static void Hex2RGB( char const aHexRGB [8] , - unsigned char aoResultRGB [3] ) +static void Hex2RGB( char const aHexRGB[8] , unsigned char aoResultRGB[3] ) { - char lColorChannel [8] ; + char lColorChannel[8] ; if ( aoResultRGB ) { if ( aHexRGB ) @@ -308,20 +292,18 @@ static void Hex2RGB( char const aHexRGB [8] , } } -static void RGB2Hex( unsigned char const aRGB [3] , - char aoResultHexRGB [8] ) +static void RGB2Hex( unsigned char const aRGB[3], char aoResultHexRGB[8] ) { if ( aoResultHexRGB ) { if ( aRGB ) { -#if defined(__GNUC__) && defined(_WIN32) - sprintf(aoResultHexRGB, "#%02hx%02hx%02hx", +#if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__) + sprintf(aoResultHexRGB, "#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]); #else - sprintf(aoResultHexRGB, "#%02hhx%02hhx%02hhx", + sprintf(aoResultHexRGB, "#%02hx%02hx%02hx", aRGB[0], aRGB[1], aRGB[2]); #endif - aRGB[0], aRGB[1], aRGB[2]); - /* printf("aoResultHexRGB %s\n", aoResultHexRGB); */ + /*printf("aoResultHexRGB %s\n", aoResultHexRGB);*/ } else { @@ -333,16 +315,14 @@ static void RGB2Hex( unsigned char const aRGB [3] , } -static void replaceSubStr( char const * const aSource , - char const * const aOldSubStr , - char const * const aNewSubStr , - char * const aoDestination ) +void tfd_replaceSubStr( char const * aSource, char const * aOldSubStr, + char const * aNewSubStr, char * aoDestination ) { char const * pOccurence ; char const * p ; char const * lNewSubStr = "" ; size_t lOldSubLen = strlen( aOldSubStr ) ; - + if ( ! aSource ) { * aoDestination = '\0' ; @@ -369,7 +349,7 @@ static void replaceSubStr( char const * const aSource , } -static int filenameValid( char const * const aFileNameWithoutPath ) +static int filenameValid( char const * aFileNameWithoutPath ) { if ( ! aFileNameWithoutPath || ! strlen(aFileNameWithoutPath) @@ -382,7 +362,7 @@ static int filenameValid( char const * const aFileNameWithoutPath ) #ifndef _WIN32 -static int fileExists( char const * const aFilePathAndName ) +static int fileExists( char const * aFilePathAndName ) { FILE * lIn ; if ( ! aFilePathAndName || ! strlen(aFilePathAndName) ) @@ -398,32 +378,10 @@ static int fileExists( char const * const aFilePathAndName ) return 1 ; } -#elif defined(TINYFD_NOLIB) - -static int fileExists( char const * const aFilePathAndName ) -{ - FILE * lIn ; - if ( ! aFilePathAndName || ! strlen(aFilePathAndName) ) - { - return 0 ; - } - - if ( tinyfd_winUtf8 ) - return 1; /* we cannot test */ - - lIn = fopen( aFilePathAndName , "r" ) ; - if ( ! lIn ) - { - return 0 ; - } - fclose( lIn ) ; - return 1 ; -} - #endif -static void wipefile(char const * const aFilename) +static void wipefile(char const * aFilename) { int i; struct stat st; @@ -437,80 +395,300 @@ static void wipefile(char const * const aFilename) { fputc('A', lIn); } + fclose(lIn); } - fclose(lIn); } } +int tfd_quoteDetected(char const * aString) +{ + char const * p; + + if (!aString) return 0; + + p = aString; + while ((p = strchr(p, '\''))) + { + return 1; + } + + p = aString; + while ((p = strchr(p, '\"'))) + { + return 1; + } + + return 0; +} + + +char const * tinyfd_getGlobalChar(char const * aCharVariableName) /* to be called from C# (you don't need this in C or C++) */ +{ + if (!aCharVariableName || !strlen(aCharVariableName)) return NULL; + else if (!strcmp(aCharVariableName, "tinyfd_version")) return tinyfd_version; + else if (!strcmp(aCharVariableName, "tinyfd_needs")) return tinyfd_needs; + else if (!strcmp(aCharVariableName, "tinyfd_response")) return tinyfd_response; + else return NULL ; +} + + +int tinyfd_getGlobalInt(char const * aIntVariableName) /* to be called from C# (you don't need this in C or C++) */ +{ + if ( !aIntVariableName || !strlen(aIntVariableName) ) return -1 ; + else if ( !strcmp(aIntVariableName, "tinyfd_verbose") ) return tinyfd_verbose ; + else if ( !strcmp(aIntVariableName, "tinyfd_silent") ) return tinyfd_silent ; + else if ( !strcmp(aIntVariableName, "tinyfd_allowCursesDialogs") ) return tinyfd_allowCursesDialogs ; + else if ( !strcmp(aIntVariableName, "tinyfd_forceConsole") ) return tinyfd_forceConsole ; + else if ( !strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay") ) return tinyfd_assumeGraphicDisplay ; +#ifdef _WIN32 + else if ( !strcmp(aIntVariableName, "tinyfd_winUtf8") ) return tinyfd_winUtf8 ; +#endif + else return -1; +} + + +int tinyfd_setGlobalInt(char const * aIntVariableName, int aValue) /* to be called from C# (you don't need this in C or C++) */ +{ + if (!aIntVariableName || !strlen(aIntVariableName)) return -1 ; + else if (!strcmp(aIntVariableName, "tinyfd_verbose")) { tinyfd_verbose = aValue; return tinyfd_verbose; } + else if (!strcmp(aIntVariableName, "tinyfd_silent")) { tinyfd_silent = aValue; return tinyfd_silent; } + else if (!strcmp(aIntVariableName, "tinyfd_allowCursesDialogs")) { tinyfd_allowCursesDialogs = aValue; return tinyfd_allowCursesDialogs; } + else if (!strcmp(aIntVariableName, "tinyfd_forceConsole")) { tinyfd_forceConsole = aValue; return tinyfd_forceConsole; } + else if (!strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay")) { tinyfd_assumeGraphicDisplay = aValue; return tinyfd_assumeGraphicDisplay; } +#ifdef _WIN32 + else if (!strcmp(aIntVariableName, "tinyfd_winUtf8")) { tinyfd_winUtf8 = aValue; return tinyfd_winUtf8; } +#endif + else return -1; +} + + #ifdef _WIN32 +static int powershellPresent(void) +{ /*only on vista and above (or installed on xp)*/ + static int lPowershellPresent = -1; + char lBuff[MAX_PATH_OR_CMD]; + FILE* lIn; + char const* lString = "powershell.exe"; -static int replaceChr( char * const aString , - char const aOldChr , - char const aNewChr ) -{ - char * p ; - int lRes = 0 ; - - if ( ! aString ) + if (lPowershellPresent < 0) + { + if (!(lIn = _popen("where powershell.exe", "r"))) { - return 0 ; + lPowershellPresent = 0; + return 0; } - - if ( aOldChr == aNewChr ) + while (fgets(lBuff, sizeof(lBuff), lIn) != NULL) { - return 0 ; } - - p = aString ; - while ( (p = strchr( p , aOldChr )) ) + _pclose(lIn); + if (lBuff[strlen(lBuff) - 1] == '\n') { - * p = aNewChr ; - p ++ ; - lRes = 1 ; + lBuff[strlen(lBuff) - 1] = '\0'; + } + if (strcmp(lBuff + strlen(lBuff) - strlen(lString), lString)) + { + lPowershellPresent = 0; } - return lRes ; -} - -#ifdef TINYFD_NOLIB - -static int dirExists(char const * const aDirPath) -{ - struct stat lInfo; - - if (!aDirPath || !strlen(aDirPath)) - return 0; - if (stat(aDirPath, &lInfo) != 0) - return 0; - else if ( tinyfd_winUtf8 ) - return 1; /* we cannot test */ - else if (lInfo.st_mode & S_IFDIR) - return 1; else - return 0; + { + lPowershellPresent = 1; + } + } + return lPowershellPresent; } - -void tinyfd_beep() +static int windowsVersion(void) { - printf("\a"); +#if !defined(__MINGW32__) || defined(__MINGW64_VERSION_MAJOR) + typedef LONG NTSTATUS ; + typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW); + HMODULE hMod; + RtlGetVersionPtr lFxPtr; + RTL_OSVERSIONINFOW lRovi = { 0 }; + + hMod = GetModuleHandleW(L"ntdll.dll"); + if (hMod) { + lFxPtr = (RtlGetVersionPtr)GetProcAddress(hMod, "RtlGetVersion"); + if (lFxPtr) + { + lRovi.dwOSVersionInfoSize = sizeof(lRovi); + if (!lFxPtr(&lRovi)) + { + return lRovi.dwMajorVersion; + } + } + } +#endif + if (powershellPresent()) return 6; /*minimum is vista or installed on xp*/ + return 0; } -#else /* ndef TINYFD_NOLIB */ -void tinyfd_beep() +static void replaceChr(char * aString, char aOldChr, char aNewChr) { - Beep(440,300); + char * p; + + if (!aString) return; + if (aOldChr == aNewChr) return; + + p = aString; + while ((p = strchr(p, aOldChr))) + { + *p = aNewChr; + p++; + } + return; } -static void wipefileW(wchar_t const * const aFilename) +#if !defined(WC_ERR_INVALID_CHARS) +/* undefined prior to Vista, so not yet in MINGW header file */ +#define WC_ERR_INVALID_CHARS 0x00000000 /* 0x00000080 for MINGW maybe ? */ +#endif + +static int sizeUtf16From8(char const * aUtf8string) +{ + return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, + aUtf8string, -1, NULL, 0); +} + + +static int sizeUtf16FromMbcs(char const * aMbcsString) +{ + return MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, + aMbcsString, -1, NULL, 0); +} + + +static int sizeUtf8(wchar_t const * aUtf16string) +{ + return WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, + aUtf16string, -1, NULL, 0, NULL, NULL); +} + + +static int sizeMbcs(wchar_t const * aMbcsString) +{ + int lRes = WideCharToMultiByte(CP_ACP, 0, + aMbcsString, -1, NULL, 0, NULL, NULL); + /* DWORD licic = GetLastError(); */ + return lRes; +} + + +wchar_t* tinyfd_mbcsTo16(char const* aMbcsString) +{ + static wchar_t* lMbcsString = NULL; + int lSize; + + free(lMbcsString); + if (!aMbcsString) { lMbcsString = NULL; return NULL; } + lSize = sizeUtf16FromMbcs(aMbcsString); + if (lSize) + { + lMbcsString = (wchar_t*)malloc(lSize * sizeof(wchar_t)); + lSize = MultiByteToWideChar(CP_ACP, 0, aMbcsString, -1, lMbcsString, lSize); + } + else wcscpy(lMbcsString, L""); + return lMbcsString; +} + + +wchar_t * tinyfd_utf8to16(char const * aUtf8string) +{ + static wchar_t * lUtf16string = NULL; + int lSize; + + free(lUtf16string); + if (!aUtf8string) {lUtf16string = NULL; return NULL;} + lSize = sizeUtf16From8(aUtf8string); + if (lSize) + { + lUtf16string = (wchar_t*)malloc(lSize * sizeof(wchar_t)); + lSize = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, + aUtf8string, -1, lUtf16string, lSize); + return lUtf16string; + } + else + { + /* let's try mbcs anyway */ + lUtf16string = NULL; + return tinyfd_mbcsTo16(aUtf8string); + } +} + + +char * tinyfd_utf16toMbcs(wchar_t const * aUtf16string) +{ + static char * lMbcsString = NULL; + int lSize; + + free(lMbcsString); + if (!aUtf16string) { lMbcsString = NULL; return NULL; } + lSize = sizeMbcs(aUtf16string); + if (lSize) + { + lMbcsString = (char*)malloc(lSize); + lSize = WideCharToMultiByte(CP_ACP, 0, aUtf16string, -1, lMbcsString, lSize, NULL, NULL); + } + else strcpy(lMbcsString, ""); + return lMbcsString; +} + + +char * tinyfd_utf8toMbcs(char const * aUtf8string) +{ + wchar_t const * lUtf16string; + lUtf16string = tinyfd_utf8to16(aUtf8string); + return tinyfd_utf16toMbcs(lUtf16string); +} + + +char * tinyfd_utf16to8(wchar_t const * aUtf16string) +{ + static char * lUtf8string = NULL; + int lSize; + + free(lUtf8string); + if (!aUtf16string) { lUtf8string = NULL; return NULL; } + lSize = sizeUtf8(aUtf16string); + if (lSize) + { + lUtf8string = (char*)malloc(lSize); + lSize = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, aUtf16string, -1, lUtf8string, lSize, NULL, NULL); + } + else strcpy(lUtf8string, ""); + return lUtf8string; +} + + +char * tinyfd_mbcsTo8(char const * aMbcsString) +{ + wchar_t const * lUtf16string; + lUtf16string = tinyfd_mbcsTo16(aMbcsString); + return tinyfd_utf16to8(lUtf16string); +} + + +void tinyfd_beep(void) +{ + if (windowsVersion() > 5) Beep(440, 300); + else MessageBeep(MB_OK); +} + + +static void wipefileW(wchar_t const * aFilename) { int i; - struct _stat st; FILE * lIn; - +#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3) + struct _stat st; if (_wstat(aFilename, &st) == 0) +#else + struct __stat64 st; + if (_wstat64(aFilename, &st) == 0) +#endif { if ((lIn = _wfopen(aFilename, L"w"))) { @@ -518,15 +696,15 @@ static void wipefileW(wchar_t const * const aFilename) { fputc('A', lIn); } + fclose(lIn); } - fclose(lIn); } } static wchar_t * getPathWithoutFinalSlashW( - wchar_t * const aoDestination, /* make sure it is allocated, use _MAX_PATH */ - wchar_t const * const aSource) /* aoDestination and aSource can be the same */ + wchar_t * aoDestination, /* make sure it is allocated, use _MAX_PATH */ + wchar_t const * aSource) /* aoDestination and aSource can be the same */ { wchar_t const * lTmp; if (aSource) @@ -555,8 +733,8 @@ static wchar_t * getPathWithoutFinalSlashW( static wchar_t * getLastNameW( - wchar_t * const aoDestination, /* make sure it is allocated */ - wchar_t const * const aSource) + wchar_t * aoDestination, /* make sure it is allocated */ + wchar_t const * aSource) { /* copy the last name after '/' or '\' */ wchar_t const * lTmp; @@ -584,8 +762,7 @@ static wchar_t * getLastNameW( } -static void Hex2RGBW(wchar_t const aHexRGB[8], - unsigned char aoResultRGB[3]) +static void Hex2RGBW(wchar_t const aHexRGB[8], unsigned char aoResultRGB[3]) { wchar_t lColorChannel[8]; if (aoResultRGB) @@ -610,20 +787,25 @@ static void Hex2RGBW(wchar_t const aHexRGB[8], } -static void RGB2HexW( - unsigned char const aRGB[3], - wchar_t aoResultHexRGB[8]) +static void RGB2HexW( unsigned char const aRGB[3], wchar_t aoResultHexRGB[8]) { +#if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__) + wchar_t const * const lPrintFormat = L"#%02hhx%02hhx%02hhx"; +#else + wchar_t const * const lPrintFormat = L"#%02hx%02hx%02hx"; +#endif + if (aoResultHexRGB) { if (aRGB) { /* wprintf(L"aoResultHexRGB %s\n", aoResultHexRGB); */ - swprintf(aoResultHexRGB, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 ) - 8, +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) + swprintf(aoResultHexRGB, 8, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]); +#else + swprintf(aoResultHexRGB, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]); #endif - L"#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]); + } else { @@ -635,105 +817,16 @@ static void RGB2HexW( } -#if !defined(WC_ERR_INVALID_CHARS) -/* undefined prior to Vista, so not yet in MINGW header file */ -#define WC_ERR_INVALID_CHARS 0x00000080 +static int dirExists(char const * aDirPath) +{ +#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3) + struct _stat lInfo; +#else + struct __stat64 lInfo; #endif - - -static int sizeUtf16(char const * const aUtf8string) -{ - return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, - aUtf8string, -1, NULL, 0); -} - - -static int sizeUtf8(wchar_t const * const aUtf16string) -{ - return WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, - aUtf16string, -1, NULL, 0, NULL, NULL); -} - - -static int sizeMbcs(wchar_t const * const aMbcsString) -{ - int lRes = WideCharToMultiByte(CP_ACP, 0, - aMbcsString, -1, NULL, 0, NULL, NULL); - /* DWORD licic = GetLastError(); */ - return lRes; -} - - -static wchar_t * utf8to16(char const * const aUtf8string) -{ - wchar_t * lUtf16string ; - int lSize = sizeUtf16(aUtf8string); - lUtf16string = (wchar_t *) malloc( lSize * sizeof(wchar_t) ); - lSize = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, - aUtf8string, -1, lUtf16string, lSize); - if (lSize == 0) - { - free(lUtf16string); - return NULL; - } - return lUtf16string; -} - - -static wchar_t * mbcsTo16(char const * const aMbcsString) -{ - wchar_t * lMbcsString; - int lSize = sizeUtf16(aMbcsString); - lMbcsString = (wchar_t *)malloc(lSize * sizeof(wchar_t)); - lSize = MultiByteToWideChar(CP_ACP, 0, - aMbcsString, -1, lMbcsString, lSize); - if (lSize == 0) - { - free(lMbcsString); - return NULL; - } - return lMbcsString; -} - - -static char * utf16to8(wchar_t const * const aUtf16string) -{ - char * lUtf8string ; - int lSize = sizeUtf8(aUtf16string); - lUtf8string = (char *) malloc( lSize ); - lSize = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, - aUtf16string, -1, lUtf8string, lSize, NULL, NULL); - if (lSize == 0) - { - free(lUtf8string); - return NULL; - } - return lUtf8string; -} - - -static char * utf16toMbcs(wchar_t const * const aUtf16string) -{ - char * lMbcsString; - int lSize = sizeMbcs(aUtf16string); - lMbcsString = (char *)malloc(lSize); - lSize = WideCharToMultiByte(CP_ACP, 0, - aUtf16string, -1, lMbcsString, lSize, NULL, NULL); - if (lSize == 0) - { - free(lMbcsString); - return NULL; - } - return lMbcsString; -} - - -static int dirExists(char const * const aDirPath) -{ - struct _stat lInfo; wchar_t * lTmpWChar; int lStatRet; - int lDirLen; + size_t lDirLen; if (!aDirPath) return 0; @@ -745,17 +838,24 @@ static int dirExists(char const * const aDirPath) if (tinyfd_winUtf8) { - lTmpWChar = utf8to16(aDirPath); - lStatRet = _wstat(lTmpWChar, &lInfo); - free(lTmpWChar); - if (lStatRet != 0) - return 0; - else if (lInfo.st_mode & S_IFDIR) - return 1; - else + lTmpWChar = tinyfd_utf8to16(aDirPath); +#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3) + lStatRet = _wstat(lTmpWChar, &lInfo); +#else + lStatRet = _wstat64(lTmpWChar, &lInfo); +#endif + if (lStatRet != 0) + return 0; + else if (lInfo.st_mode & S_IFDIR) + return 1; + else return 0; } +#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3) else if (_stat(aDirPath, &lInfo) != 0) +#else + else if (_stat64(aDirPath, &lInfo) != 0) +#endif return 0; else if (lInfo.st_mode & S_IFDIR) return 1; @@ -764,9 +864,13 @@ static int dirExists(char const * const aDirPath) } -static int fileExists(char const * const aFilePathAndName) +static int fileExists(char const * aFilePathAndName) { - struct _stat lInfo; +#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3) + struct _stat lInfo; +#else + struct __stat64 lInfo; +#endif wchar_t * lTmpWChar; int lStatRet; FILE * lIn; @@ -778,15 +882,19 @@ static int fileExists(char const * const aFilePathAndName) if (tinyfd_winUtf8) { - lTmpWChar = utf8to16(aFilePathAndName); - lStatRet = _wstat(lTmpWChar, &lInfo); - free(lTmpWChar); - if (lStatRet != 0) - return 0; - else if (lInfo.st_mode & _S_IFREG) - return 1; - else - return 0; + lTmpWChar = tinyfd_utf8to16(aFilePathAndName); +#if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3) + lStatRet = _wstat(lTmpWChar, &lInfo); +#else + lStatRet = _wstat64(lTmpWChar, &lInfo); +#endif + + if (lStatRet != 0) + return 0; + else if (lInfo.st_mode & _S_IFREG) + return 1; + else + return 0; } else { @@ -800,12 +908,61 @@ static int fileExists(char const * const aFilePathAndName) } } -#endif /* TINYFD_NOLIB */ +static void replaceWchar(wchar_t * aString, + wchar_t aOldChr, + wchar_t aNewChr) +{ + wchar_t * p; + + if (!aString) + { + return ; + } + + if (aOldChr == aNewChr) + { + return ; + } + + p = aString; + while ((p = wcsrchr(p, aOldChr))) + { + *p = aNewChr; +#ifdef TINYFD_NOCCSUNICODE + p++; +#endif + p++; + } + return ; +} + + +static int quoteDetectedW(wchar_t const * aString) +{ + wchar_t const * p; + + if (!aString) return 0; + + p = aString; + while ((p = wcsrchr(p, L'\''))) + { + return 1; + } + + p = aString; + while ((p = wcsrchr(p, L'\"'))) + { + return 1; + } + + return 0; +} + #endif /* _WIN32 */ /* source and destination can be the same or ovelap*/ -static char const * ensureFilesExist(char * const aDestination, - char const * const aSourcePathsAndNames) +static char * ensureFilesExist(char * aDestination, + char const * aSourcePathsAndNames) { char * lDestination = aDestination; char const * p; @@ -850,10 +1007,9 @@ static char const * ensureFilesExist(char * const aDestination, } #ifdef _WIN32 -#ifndef TINYFD_NOLIB static int __stdcall EnumThreadWndProc(HWND hwnd, LPARAM lParam) -{ +{ wchar_t lTitleName[MAX_PATH]; GetWindowTextW(hwnd, lTitleName, MAX_PATH); /* wprintf(L"lTitleName %ls \n", lTitleName); */ @@ -866,7 +1022,7 @@ static int __stdcall EnumThreadWndProc(HWND hwnd, LPARAM lParam) } -static void hiddenConsoleW(wchar_t const * const aString, wchar_t const * const aDialogTitle, int const aInFront) +static void hiddenConsoleW(wchar_t const * aString, wchar_t const * aDialogTitle, int aInFront) { STARTUPINFOW StartupInfo; PROCESS_INFORMATION ProcessInfo; @@ -898,17 +1054,20 @@ static void hiddenConsoleW(wchar_t const * const aString, wchar_t const * const int tinyfd_messageBoxW( - wchar_t const * const aTitle, /* NULL or "" */ - wchar_t const * const aMessage, /* NULL or "" may contain \n and \t */ - wchar_t const * const aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */ - wchar_t const * const aIconType, /* "info" "warning" "error" "question" */ - int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ + wchar_t const * aTitle, /* NULL or "" */ + wchar_t const * aMessage, /* NULL or "" may contain \n and \t */ + wchar_t const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */ + wchar_t const * aIconType, /* "info" "warning" "error" "question" */ + int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { int lBoxReturnValue; UINT aCode; if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return 1; } + if (quoteDetectedW(aTitle)) return tinyfd_messageBoxW(L"INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton); + if (quoteDetectedW(aMessage)) return tinyfd_messageBoxW(aTitle, L"INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton); + if (aIconType && !wcscmp(L"warning", aIconType)) { aCode = MB_ICONWARNING; @@ -942,6 +1101,22 @@ int tinyfd_messageBoxW( aCode += MB_DEFBUTTON2; } } + else if (aDialogType && !wcscmp(L"yesnocancel", aDialogType)) + { + aCode += MB_YESNOCANCEL; + if (aDefaultButton == 1) + { + aCode += MB_DEFBUTTON1; + } + else if (aDefaultButton == 2) + { + aCode += MB_DEFBUTTON2; + } + else + { + aCode += MB_DEFBUTTON3; + } + } else { aCode += MB_OK; @@ -950,68 +1125,43 @@ int tinyfd_messageBoxW( aCode += MB_TOPMOST; lBoxReturnValue = MessageBoxW(GetForegroundWindow(), aMessage, aTitle, aCode); - if (((aDialogType - && wcscmp(L"okcancel", aDialogType) - && wcscmp(L"yesno", aDialogType))) - || (lBoxReturnValue == IDOK) - || (lBoxReturnValue == IDYES)) + + if ( (lBoxReturnValue == IDNO) && (aDialogType && !wcscmp(L"yesnocancel", aDialogType)) ) { - return 1; + return 2; + } + else if ( (lBoxReturnValue == IDOK) || (lBoxReturnValue == IDYES) ) + { + return 1; } else { - return 0; + return 0; } } -static int messageBoxWinGui8( - char const * const aTitle, /* NULL or "" */ - char const * const aMessage, /* NULL or "" may contain \n and \t */ - char const * const aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */ - char const * const aIconType, /* "info" "warning" "error" "question" */ - int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ -{ - int lIntRetVal; - wchar_t * lTitle; - wchar_t * lMessage; - wchar_t * lDialogType; - wchar_t * lIconType; - - lTitle = utf8to16(aTitle); - lMessage = utf8to16(aMessage); - lDialogType = utf8to16(aDialogType); - lIconType = utf8to16(aIconType); - - lIntRetVal = tinyfd_messageBoxW(lTitle, lMessage, - lDialogType, lIconType, aDefaultButton ); - - free(lTitle); - free(lMessage); - free(lDialogType); - free(lIconType); - - return lIntRetVal ; -} - - /* return has only meaning for tinyfd_query */ int tinyfd_notifyPopupW( - wchar_t const * const aTitle, /* NULL or L"" */ - wchar_t const * const aMessage, /* NULL or L"" may contain \n \t */ - wchar_t const * const aIconType) /* L"info" L"warning" L"error" */ + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aMessage, /* NULL or L"" may contain \n \t */ + wchar_t const * aIconType) /* L"info" L"warning" L"error" */ { wchar_t * lDialogString; size_t lTitleLen; size_t lMessageLen; size_t lDialogStringLen; - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return 1; } + if (aTitle && !wcscmp(aTitle, L"tinyfd_query")) { strcpy(tinyfd_response, "windows_wchar"); return 1; } + + if (quoteDetectedW(aTitle)) return tinyfd_notifyPopupW(L"INVALID TITLE WITH QUOTES", aMessage, aIconType); + if (quoteDetectedW(aMessage)) return tinyfd_notifyPopupW(aTitle, L"INVALID MESSAGE WITH QUOTES", aIconType); lTitleLen = aTitle ? wcslen(aTitle) : 0; lMessageLen = aMessage ? wcslen(aMessage) : 0; lDialogStringLen = 3 * MAX_PATH_OR_CMD + lTitleLen + lMessageLen; lDialogString = (wchar_t *)malloc(2 * lDialogStringLen); + if (!lDialogString) return 0; wcscpy(lDialogString, L"powershell.exe -command \"\ function Show-BalloonTip {\ @@ -1031,7 +1181,7 @@ $balloon.Icon = $icon ; \ $balloon.BalloonTipIcon = $IconType ; \ $balloon.BalloonTipText = $Message ; \ $balloon.BalloonTipTitle = $Title ; \ -$balloon.Text = 'lalala' ; \ +$balloon.Text = 'tinyfiledialogs' ; \ $balloon.Visible = $true ; \ $balloon.ShowBalloonTip(5000)};\ Show-BalloonTip"); @@ -1064,41 +1214,10 @@ Show-BalloonTip"); } -static int notifyWinGui( - char const * const aTitle, /* NULL or "" */ - char const * const aMessage, /* NULL or "" may NOT contain \n nor \t */ - char const * const aIconType) -{ - wchar_t * lTitle; - wchar_t * lMessage; - wchar_t * lIconType; - - if (tinyfd_winUtf8) - { - lTitle = utf8to16(aTitle); - lMessage = utf8to16(aMessage); - lIconType = utf8to16(aIconType); - } - else - { - lTitle = mbcsTo16(aTitle); - lMessage = mbcsTo16(aMessage); - lIconType = mbcsTo16(aIconType); - } - - tinyfd_notifyPopupW( lTitle, lMessage, lIconType); - - free(lTitle); - free(lMessage); - free(lIconType); - return 1; -} - - -wchar_t const * tinyfd_inputBoxW( - wchar_t const * const aTitle, /* NULL or L"" */ - wchar_t const * const aMessage, /* NULL or L"" may NOT contain \n nor \t */ - wchar_t const * const aDefaultInput) /* L"" , if NULL it's a passwordBox */ +wchar_t * tinyfd_inputBoxW( + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aMessage, /* NULL or L"" (\n and \t have no effect) */ + wchar_t const * aDefaultInput) /* L"" , if NULL it's a passwordBox */ { static wchar_t lBuff[MAX_PATH_OR_CMD]; wchar_t * lDialogString; @@ -1109,7 +1228,11 @@ wchar_t const * tinyfd_inputBoxW( size_t lMessageLen; size_t lDialogStringLen; - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t const *)1; } + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } + + if (quoteDetectedW(aTitle)) return tinyfd_inputBoxW(L"INVALID TITLE WITH QUOTES", aMessage, aDefaultInput); + if (quoteDetectedW(aMessage)) return tinyfd_inputBoxW(aTitle, L"INVALID MESSAGE WITH QUOTES", aDefaultInput); + if (quoteDetectedW(aDefaultInput)) return tinyfd_inputBoxW(aTitle, aMessage, L"INVALID DEFAULT_INPUT WITH QUOTES"); lTitleLen = aTitle ? wcslen(aTitle) : 0 ; lMessageLen = aMessage ? wcslen(aMessage) : 0 ; @@ -1119,18 +1242,18 @@ wchar_t const * tinyfd_inputBoxW( if (aDefaultInput) { swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 ) +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) lDialogStringLen, #endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.vbs", _wgetenv(L"USERPROFILE")); + L"%ls\\tinyfd.vbs", _wgetenv(L"TEMP")); } else { swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 ) +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) lDialogStringLen, #endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.hta", _wgetenv(L"USERPROFILE")); + L"%ls\\tinyfd.hta", _wgetenv(L"TEMP")); } lIn = _wfopen(lDialogString, L"w"); if (!lIn) @@ -1144,12 +1267,16 @@ wchar_t const * tinyfd_inputBoxW( wcscpy(lDialogString, L"Dim result:result=InputBox(\""); if (aMessage && wcslen(aMessage)) { - wcscat(lDialogString, aMessage); + wcscpy(lBuff, aMessage); + replaceWchar(lBuff, L'\n', L' '); + wcscat(lDialogString, lBuff); } wcscat(lDialogString, L"\",\"tinyfiledialogsTopWindow\",\""); if (aDefaultInput && wcslen(aDefaultInput)) { - wcscat(lDialogString, aDefaultInput); + wcscpy(lBuff, aDefaultInput); + replaceWchar(lBuff, L'\n', L' '); + wcscat(lDialogString, lBuff); } wcscat(lDialogString, L"\"):If IsEmpty(result) then:WScript.Echo 0"); wcscat(lDialogString, L":Else: WScript.Echo \"1\" & result : End If"); @@ -1190,8 +1317,8 @@ End Sub\n\ Sub Window_onUnload\n\ Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n\ Set oShell = CreateObject(\"WScript.Shell\")\n\ -strHomeFolder = oShell.ExpandEnvironmentStrings(\"%USERPROFILE%\")\n\ -Set objFile = objFSO.CreateTextFile(strHomeFolder & \"\\AppData\\Local\\Temp\\tinyfd.txt\",True,True)\n\ +strTempFolder = oShell.ExpandEnvironmentStrings(\"%TEMP%\")\n\ +Set objFile = objFSO.CreateTextFile(strTempFolder & \"\\tinyfd.txt\",True,True)\n\ If result = 1 Then\n\ objFile.Write 1 & txt_input.Value\n\ Else\n\ @@ -1258,10 +1385,10 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ if (aDefaultInput) { swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 ) +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) lDialogStringLen, #endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.txt",_wgetenv(L"USERPROFILE")); + L"%ls\\tinyfd.txt",_wgetenv(L"TEMP")); #ifdef TINYFD_NOCCSUNICODE lFile = _wfopen(lDialogString, L"w"); @@ -1273,13 +1400,13 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ fclose(lFile); wcscpy(lDialogString, L"cmd.exe /c cscript.exe //U //Nologo "); - wcscat(lDialogString, L"\"%USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.vbs\" "); - wcscat(lDialogString, L">> \"%USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.txt\""); + wcscat(lDialogString, L"\"%TEMP%\\tinyfd.vbs\" "); + wcscat(lDialogString, L">> \"%TEMP%\\tinyfd.txt\""); } else { wcscpy(lDialogString, - L"cmd.exe /c mshta.exe \"%USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.hta\""); + L"cmd.exe /c mshta.exe \"%TEMP%\\tinyfd.hta\""); } /* wprintf ( "lDialogString: %ls\n" , lDialogString ) ; */ @@ -1287,10 +1414,10 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ hiddenConsoleW(lDialogString, aTitle, 1); swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 ) +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) lDialogStringLen, #endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.txt", _wgetenv(L"USERPROFILE")); + L"%ls\\tinyfd.txt", _wgetenv(L"TEMP")); /* wprintf(L"lDialogString: %ls\n", lDialogString); */ #ifdef TINYFD_NOCCSUNICODE if (!(lIn = _wfopen(lDialogString, L"r"))) @@ -1302,6 +1429,9 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ free(lDialogString); return NULL; } + + memset(lBuff, 0, MAX_PATH_OR_CMD * sizeof(wchar_t) ); + #ifdef TINYFD_NOCCSUNICODE fgets((char *)lBuff, 2*MAX_PATH_OR_CMD, lIn); #else @@ -1314,20 +1444,18 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ if (aDefaultInput) { swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 ) +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) lDialogStringLen, #endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.vbs", - _wgetenv(L"USERPROFILE")); + L"%ls\\tinyfd.vbs", _wgetenv(L"TEMP")); } else { swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 ) +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) lDialogStringLen, #endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.hta", - _wgetenv(L"USERPROFILE")); + L"%ls\\tinyfd.hta", _wgetenv(L"TEMP")); } _wremove(lDialogString); free(lDialogString); @@ -1337,7 +1465,7 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ #else lResult = !wcsncmp(lBuff, L"1", 1); #endif - + /* printf( "lResult: %d \n" , lResult ) ; */ if (!lResult) { @@ -1345,71 +1473,28 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ } /* wprintf( "lBuff+1: %ls\n" , lBuff+1 ) ; */ + #ifdef TINYFD_NOCCSUNICODE + if (aDefaultInput) + { + lDialogStringLen = wcslen(lBuff) ; + lBuff[lDialogStringLen - 1] = L'\0'; + lBuff[lDialogStringLen - 2] = L'\0'; + } return lBuff + 2; #else + if (aDefaultInput) lBuff[wcslen(lBuff) - 1] = L'\0'; return lBuff + 1; #endif } -static char const * inputBoxWinGui( - char * const aoBuff, - char const * const aTitle, /* NULL or "" */ - char const * const aMessage, /* NULL or "" may NOT contain \n nor \t */ - char const * const aDefaultInput) /* "" , if NULL it's a passwordBox */ -{ - wchar_t * lTitle; - wchar_t * lMessage; - wchar_t * lDefaultInput; - wchar_t const * lTmpWChar; - char * lTmpChar; - - if (tinyfd_winUtf8) - { - lTitle = utf8to16(aTitle); - lMessage = utf8to16(aMessage); - lDefaultInput = utf8to16(aDefaultInput); - } - else - { - lTitle = mbcsTo16(aTitle); - lMessage = mbcsTo16(aMessage); - lDefaultInput = mbcsTo16(aDefaultInput); - } - - lTmpWChar = tinyfd_inputBoxW( lTitle, lMessage, lDefaultInput); - - free(lTitle); - free(lMessage); - free(lDefaultInput); - - if (!lTmpWChar) - { - return NULL; - } - - if (tinyfd_winUtf8) - { - lTmpChar = utf16to8(lTmpWChar); - } - else - { - lTmpChar = utf16toMbcs(lTmpWChar); - } - strcpy(aoBuff, lTmpChar); - free(lTmpChar); - - return aoBuff; -} - - -wchar_t const * tinyfd_saveFileDialogW( - wchar_t const * const aTitle, /* NULL or "" */ - wchar_t const * const aDefaultPathAndFile, /* NULL or "" */ - int const aNumOfFilterPatterns, /* 0 */ - wchar_t const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ - wchar_t const * const aSingleFilterDescription) /* NULL or "image files" */ +wchar_t * tinyfd_saveFileDialogW( + wchar_t const * aTitle, /* NULL or "" */ + wchar_t const * aDefaultPathAndFile, /* NULL or "" */ + int aNumOfFilterPatterns, /* 0 */ + wchar_t const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ + wchar_t const * aSingleFilterDescription) /* NULL or "image files" */ { static wchar_t lBuff[MAX_PATH_OR_CMD]; wchar_t lDirname[MAX_PATH_OR_CMD]; @@ -1417,11 +1502,20 @@ wchar_t const * tinyfd_saveFileDialogW( wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L""; wchar_t * p; wchar_t * lRetval; - int i; + wchar_t const * ldefExt = NULL; + int i; HRESULT lHResult; OPENFILENAMEW ofn = {0}; - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t const *)1; } + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } + + if (quoteDetectedW(aTitle)) return tinyfd_saveFileDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (quoteDetectedW(aDefaultPathAndFile)) return tinyfd_saveFileDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (quoteDetectedW(aSingleFilterDescription)) return tinyfd_saveFileDialogW(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, L"INVALID FILTER_DESCRIPTION WITH QUOTES"); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (quoteDetectedW(aFilterPatterns[i])) return tinyfd_saveFileDialogW(L"INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL); + } lHResult = CoInitializeEx(NULL, 0); @@ -1430,6 +1524,8 @@ wchar_t const * tinyfd_saveFileDialogW( if (aNumOfFilterPatterns > 0) { + ldefExt = aFilterPatterns[0]; + if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) { wcscpy(lFilterPatterns, aSingleFilterDescription); @@ -1459,7 +1555,7 @@ wchar_t const * tinyfd_saveFileDialogW( ofn.lStructSize = sizeof(OPENFILENAMEW); ofn.hwndOwner = GetForegroundWindow(); ofn.hInstance = 0; - ofn.lpstrFilter = lFilterPatterns && wcslen(lFilterPatterns) ? lFilterPatterns : NULL; + ofn.lpstrFilter = wcslen(lFilterPatterns) ? lFilterPatterns : NULL; ofn.lpstrCustomFilter = NULL; ofn.nMaxCustFilter = 0; ofn.nFilterIndex = 1; @@ -1468,12 +1564,12 @@ wchar_t const * tinyfd_saveFileDialogW( ofn.nMaxFile = MAX_PATH_OR_CMD; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = MAX_PATH_OR_CMD/2; - ofn.lpstrInitialDir = lDirname && wcslen(lDirname) ? lDirname : NULL; + ofn.lpstrInitialDir = wcslen(lDirname) ? lDirname : NULL; ofn.lpstrTitle = aTitle && wcslen(aTitle) ? aTitle : NULL; ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST ; ofn.nFileOffset = 0; ofn.nFileExtension = 0; - ofn.lpstrDefExt = NULL; + ofn.lpstrDefExt = ldefExt; ofn.lCustData = 0L; ofn.lpfnHook = NULL; ofn.lpTemplateName = NULL; @@ -1495,84 +1591,57 @@ wchar_t const * tinyfd_saveFileDialogW( } -static char const * saveFileDialogWinGui8( - char * const aoBuff, - char const * const aTitle, /* NULL or "" */ - char const * const aDefaultPathAndFile, /* NULL or "" */ - int const aNumOfFilterPatterns, /* 0 */ - char const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription) /* NULL or "image files" */ +wchar_t * tinyfd_openFileDialogW( + wchar_t const * aTitle, /* NULL or "" */ + wchar_t const * aDefaultPathAndFile, /* NULL or "" */ + int aNumOfFilterPatterns, /* 0 */ + wchar_t const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ + wchar_t const * aSingleFilterDescription, /* NULL or "image files" */ + int aAllowMultipleSelects) /* 0 or 1 ; -1 to free allocated memory and return */ { - wchar_t * lTitle; - wchar_t * lDefaultPathAndFile; - wchar_t * lSingleFilterDescription; - wchar_t * * lFilterPatterns; - wchar_t const * lTmpWChar; - char * lTmpChar; - int i ; - - lFilterPatterns = (wchar_t **) malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - lFilterPatterns[i] = utf8to16(aFilterPatterns[i]); - } - - lTitle = utf8to16(aTitle); - lDefaultPathAndFile = utf8to16(aDefaultPathAndFile); - lSingleFilterDescription = utf8to16(aSingleFilterDescription); - - lTmpWChar = tinyfd_saveFileDialogW( - lTitle, - lDefaultPathAndFile, - aNumOfFilterPatterns, - (wchar_t const** ) /*stupid cast for gcc*/ - lFilterPatterns, - lSingleFilterDescription); - - free(lTitle); - free(lDefaultPathAndFile); - free(lSingleFilterDescription); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - free(lFilterPatterns[i]); - } - free(lFilterPatterns); - - if (!lTmpWChar) - { - return NULL; - } - - lTmpChar = utf16to8(lTmpWChar); - strcpy(aoBuff, lTmpChar); - free(lTmpChar); - - return aoBuff; -} - - -wchar_t const * tinyfd_openFileDialogW( - wchar_t const * const aTitle, /* NULL or "" */ - wchar_t const * const aDefaultPathAndFile, /* NULL or "" */ - int const aNumOfFilterPatterns, /* 0 */ - wchar_t const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ - wchar_t const * const aSingleFilterDescription, /* NULL or "image files" */ - int const aAllowMultipleSelects) /* 0 or 1 */ -{ - static wchar_t lBuff[MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD]; - size_t lLengths[MAX_MULTIPLE_FILES]; wchar_t lDirname[MAX_PATH_OR_CMD]; wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L""; wchar_t lDialogString[MAX_PATH_OR_CMD]; - wchar_t * lPointers[MAX_MULTIPLE_FILES]; - wchar_t * lRetval, * p; + wchar_t * lPointers[MAX_MULTIPLE_FILES+1]; + wchar_t * p; int i, j; - size_t lBuffLen; + size_t lBuffLen; + DWORD lFullBuffLen; HRESULT lHResult; OPENFILENAMEW ofn = { 0 }; + static wchar_t * lBuff = NULL; - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t const *)1; } + free(lBuff); + lBuff = NULL; + if (aAllowMultipleSelects < 0) return (wchar_t *)0; + + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } + + if (quoteDetectedW(aTitle)) return tinyfd_openFileDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (quoteDetectedW(aDefaultPathAndFile)) return tinyfd_openFileDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (quoteDetectedW(aSingleFilterDescription)) return tinyfd_openFileDialogW(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, L"INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (quoteDetectedW(aFilterPatterns[i])) return tinyfd_openFileDialogW(L"INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects); + } + + if (aAllowMultipleSelects) + { + lFullBuffLen = MAX_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; + lBuff = (wchar_t*)(malloc(lFullBuffLen * sizeof(wchar_t))); + if (!lBuff) + { + lFullBuffLen = LOW_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; + lBuff = (wchar_t*)( malloc( lFullBuffLen * sizeof(wchar_t))); + } + } + else + { + lFullBuffLen = MAX_PATH_OR_CMD + 1; + lBuff = (wchar_t*)(malloc(lFullBuffLen * sizeof(wchar_t))); + } + if (!lBuff) return NULL; lHResult = CoInitializeEx(NULL, 0); @@ -1610,15 +1679,15 @@ wchar_t const * tinyfd_openFileDialogW( ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = GetForegroundWindow(); ofn.hInstance = 0; - ofn.lpstrFilter = lFilterPatterns && wcslen(lFilterPatterns) ? lFilterPatterns : NULL; + ofn.lpstrFilter = wcslen(lFilterPatterns) ? lFilterPatterns : NULL; ofn.lpstrCustomFilter = NULL; ofn.nMaxCustFilter = 0; ofn.nFilterIndex = 1; ofn.lpstrFile = lBuff; - ofn.nMaxFile = MAX_PATH_OR_CMD; + ofn.nMaxFile = lFullBuffLen; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = MAX_PATH_OR_CMD / 2; - ofn.lpstrInitialDir = lDirname && wcslen(lDirname) ? lDirname : NULL; + ofn.lpstrInitialDir = wcslen(lDirname) ? lDirname : NULL; ofn.lpstrTitle = aTitle && wcslen(aTitle) ? aTitle : NULL; ofn.Flags = OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; ofn.nFileOffset = 0; @@ -1635,139 +1704,99 @@ wchar_t const * tinyfd_openFileDialogW( if (GetOpenFileNameW(&ofn) == 0) { - lRetval = NULL; + free(lBuff); + lBuff = NULL; } else { lBuffLen = wcslen(lBuff); lPointers[0] = lBuff + lBuffLen + 1; - if (!aAllowMultipleSelects || (lPointers[0][0] == L'\0')) - { - lRetval = lBuff; - } - else - { + if (aAllowMultipleSelects && (lPointers[0][0] != L'\0')) + { i = 0; do { lLengths[i] = wcslen(lPointers[i]); lPointers[i + 1] = lPointers[i] + lLengths[i] + 1; i++; - } while (lPointers[i][0] != L'\0'); - i--; - p = lBuff + MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD - 1; - *p = L'\0'; - for (j = i; j >= 0; j--) - { - p -= lLengths[j]; - memmove(p, lPointers[j], lLengths[j]*sizeof(wchar_t)); - p--; - *p = L'\\'; - p -= lBuffLen; - memmove(p, lBuff, lBuffLen*sizeof(wchar_t)); - p--; - *p = L'|'; - } - p++; - lRetval = p; - } + } while (lPointers[i][0] != L'\0' && i < MAX_MULTIPLE_FILES ); + if (i > MAX_MULTIPLE_FILES) + { + free(lBuff); + lBuff = NULL; + } + else + { + i--; + p = lBuff + lFullBuffLen - 1; + *p = L'\0'; + for (j = i; j >= 0; j--) + { + p -= lLengths[j]; + memmove(p, lPointers[j], lLengths[j] * sizeof(wchar_t)); + p--; + *p = L'\\'; + p -= lBuffLen; + memmove(p, lBuff, lBuffLen*sizeof(wchar_t)); + p--; + *p = L'|'; + } + p++; + wcscpy(lBuff, p); + lBuffLen = wcslen(lBuff); + } + } + if (lBuff) lBuff = (wchar_t*)(realloc(lBuff, (lBuffLen + 1) * sizeof(wchar_t))); } if (lHResult == S_OK || lHResult == S_FALSE) { CoUninitialize(); } - return lRetval; + return lBuff; } -static char const * openFileDialogWinGui8( - char * const aoBuff, - char const * const aTitle, /* NULL or "" */ - char const * const aDefaultPathAndFile, /* NULL or "" */ - int const aNumOfFilterPatterns, /* 0 */ - char const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription, /* NULL or "image files" */ - int const aAllowMultipleSelects) /* 0 or 1 */ +BOOL CALLBACK BrowseCallbackProcW_enum(HWND hWndChild, LPARAM lParam) { - wchar_t * lTitle; - wchar_t * lDefaultPathAndFile; - wchar_t * lSingleFilterDescription; - wchar_t * * lFilterPatterns; - wchar_t const * lTmpWChar; - char * lTmpChar; - int i; - - lFilterPatterns = (wchar_t * *) malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - lFilterPatterns[i] = utf8to16(aFilterPatterns[i]); - } - - lTitle = utf8to16(aTitle); - lDefaultPathAndFile = utf8to16(aDefaultPathAndFile); - lSingleFilterDescription = utf8to16(aSingleFilterDescription); - - lTmpWChar = tinyfd_openFileDialogW( - lTitle, - lDefaultPathAndFile, - aNumOfFilterPatterns, - (wchar_t const**) /*stupid cast for gcc*/ - lFilterPatterns, - lSingleFilterDescription, - aAllowMultipleSelects); - - free(lTitle); - free(lDefaultPathAndFile); - free(lSingleFilterDescription); - for (i = 0; i < aNumOfFilterPatterns; i++) - { - free(lFilterPatterns[i]); - } - free(lFilterPatterns); - - if (!lTmpWChar) - { - return NULL; - } - - lTmpChar = utf16to8(lTmpWChar); - strcpy(aoBuff, lTmpChar); - free(lTmpChar); - - return aoBuff; + wchar_t buf[255]; + GetClassNameW(hWndChild, buf, sizeof(buf)); + if (wcscmp(buf, L"SysTreeView32") == 0) { + HTREEITEM hNode = TreeView_GetSelection(hWndChild); + TreeView_EnsureVisible(hWndChild, hNode); + return FALSE; + } + return TRUE; } -#ifndef TINYFD_NOSELECTFOLDERWIN -static int __stdcall BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) -{ - if (uMsg == BFFM_INITIALIZED) - { - SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData); - } - return 0; -} static int __stdcall BrowseCallbackProcW(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) { - if (uMsg == BFFM_INITIALIZED) - { - SendMessage(hwnd, BFFM_SETSELECTIONW, TRUE, (LPARAM)pData); - } - return 0; + switch (uMsg) { + case BFFM_INITIALIZED: + SendMessage(hwnd, BFFM_SETSELECTIONW, TRUE, (LPARAM)pData); + break; + case BFFM_SELCHANGED: + EnumChildWindows(hwnd, BrowseCallbackProcW_enum, 0); + } + return 0; } -wchar_t const * tinyfd_selectFolderDialogW( - wchar_t const * const aTitle, /* NULL or "" */ - wchar_t const * const aDefaultPath) /* NULL or "" */ +wchar_t * tinyfd_selectFolderDialogW( + wchar_t const * aTitle, /* NULL or "" */ + wchar_t const * aDefaultPath) /* NULL or "" */ { static wchar_t lBuff[MAX_PATH_OR_CMD]; - + wchar_t * lRetval; + BROWSEINFOW bInfo; LPITEMIDLIST lpItem; HRESULT lHResult; - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t const *)1; } + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } + + if (quoteDetectedW(aTitle)) return tinyfd_selectFolderDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPath); + if (quoteDetectedW(aDefaultPath)) return tinyfd_selectFolderDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES"); lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); @@ -1784,55 +1813,27 @@ wchar_t const * tinyfd_selectFolderDialogW( bInfo.iImage = -1; lpItem = SHBrowseForFolderW(&bInfo); - if (lpItem) + if (!lpItem) + { + lRetval = NULL; + } + else { SHGetPathFromIDListW(lpItem, lBuff); + lRetval = lBuff ; } if (lHResult == S_OK || lHResult == S_FALSE) { CoUninitialize(); } - return lBuff; + return lRetval; } -static char const * selectFolderDialogWinGui8( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) /* NULL or "" */ -{ - wchar_t * lTitle; - wchar_t * lDefaultPath; - wchar_t const * lTmpWChar; - char * lTmpChar; - - lTitle = utf8to16(aTitle); - lDefaultPath = utf8to16(aDefaultPath); - - lTmpWChar = tinyfd_selectFolderDialogW( - lTitle, - lDefaultPath); - - free(lTitle); - free(lDefaultPath); - if (!lTmpWChar) - { - return NULL; - } - - lTmpChar = utf16to8(lTmpWChar); - strcpy(aoBuff, lTmpChar); - free(lTmpChar); - - return aoBuff; -} -#endif /*TINYFD_NOSELECTFOLDERWIN*/ - - -wchar_t const * tinyfd_colorChooserW( - wchar_t const * const aTitle, /* NULL or "" */ - wchar_t const * const aDefaultHexRGB, /* NULL or "#FF0000"*/ +wchar_t * tinyfd_colorChooserW( + wchar_t const * aTitle, /* NULL or "" */ + wchar_t const * aDefaultHexRGB, /* NULL or "#FF0000"*/ unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ { @@ -1844,11 +1845,14 @@ wchar_t const * tinyfd_colorChooserW( HRESULT lHResult; - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t const *)1; } + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } + + if (quoteDetectedW(aTitle)) return tinyfd_colorChooserW(L"INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB); + if (quoteDetectedW(aDefaultHexRGB)) return tinyfd_colorChooserW(aTitle, L"INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB); lHResult = CoInitializeEx(NULL, 0); - if (aDefaultHexRGB) + if ( aDefaultHexRGB ) { Hex2RGBW(aDefaultHexRGB, lDefaultRGB); } @@ -1892,21 +1896,348 @@ wchar_t const * tinyfd_colorChooserW( } -static char const * colorChooserWinGui8( - char const * const aTitle, /* NULL or "" */ - char const * const aDefaultHexRGB, /* NULL or "#FF0000"*/ +static int messageBoxWinGui( + char const * aTitle, /* NULL or "" */ + char const * aMessage, /* NULL or "" may contain \n and \t */ + char const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */ + char const * aIconType, /* "info" "warning" "error" "question" */ + int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ +{ + int lIntRetVal; + wchar_t lTitle[128] = L""; + wchar_t * lMessage = NULL; + wchar_t lDialogType[16] = L""; + wchar_t lIconType[16] = L""; + wchar_t * lTmpWChar; + + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aMessage) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage); + else lTmpWChar = tinyfd_mbcsTo16(aMessage); + lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t)); + if (lMessage) wcscpy(lMessage, lTmpWChar); + } + if (aDialogType) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDialogType); + else lTmpWChar = tinyfd_mbcsTo16(aDialogType); + wcscpy(lDialogType, lTmpWChar); + } + if (aIconType) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aIconType); + else lTmpWChar = tinyfd_mbcsTo16(aIconType); + wcscpy(lIconType, lTmpWChar); + } + + lIntRetVal = tinyfd_messageBoxW(lTitle, lMessage, lDialogType, lIconType, aDefaultButton); + + free(lMessage); + + return lIntRetVal; +} + + +static int notifyWinGui( + char const * aTitle, /* NULL or "" */ + char const * aMessage, /* NULL or "" may NOT contain \n nor \t */ + char const * aIconType) +{ + wchar_t lTitle[128] = L""; + wchar_t * lMessage = NULL; + wchar_t lIconType[16] = L""; + wchar_t * lTmpWChar; + + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aMessage) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage); + else lTmpWChar = tinyfd_mbcsTo16(aMessage); + lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t)); + if (lMessage) wcscpy(lMessage, lTmpWChar); + } + if (aIconType) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aIconType); + else lTmpWChar = tinyfd_mbcsTo16(aIconType); + wcscpy(lIconType, lTmpWChar); + } + + tinyfd_notifyPopupW(lTitle, lMessage, lIconType); + + free(lMessage); + + return 1; +} + + +static int inputBoxWinGui( + char * aoBuff, + char const * aTitle, /* NULL or "" */ + char const * aMessage, /* NULL or "" may NOT contain \n nor \t */ + char const * aDefaultInput) /* "" , if NULL it's a passwordBox */ +{ + wchar_t lTitle[128] = L""; + wchar_t * lMessage = NULL; + wchar_t lDefaultInput[MAX_PATH_OR_CMD] = L""; + wchar_t * lTmpWChar; + char * lTmpChar; + + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aMessage) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage); + else lTmpWChar = tinyfd_mbcsTo16(aMessage); + lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t)); + if (lMessage) wcscpy(lMessage, lTmpWChar); + } + if (aDefaultInput) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultInput); + else lTmpWChar = tinyfd_mbcsTo16(aDefaultInput); + wcscpy(lDefaultInput, lTmpWChar); + lTmpWChar = tinyfd_inputBoxW(lTitle, lMessage, lDefaultInput); + } + else lTmpWChar = tinyfd_inputBoxW(lTitle, lMessage, NULL); + + free(lMessage); + + if (!lTmpWChar) + { + aoBuff[0] = '\0'; + return 0; + } + + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + + strcpy(aoBuff, lTmpChar); + + return 1; +} + + +static char * saveFileDialogWinGui( + char * aoBuff, + char const * aTitle, /* NULL or "" */ + char const * aDefaultPathAndFile, /* NULL or "" */ + int aNumOfFilterPatterns, /* 0 */ + char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ + char const * aSingleFilterDescription) /* NULL or "image files" */ +{ + wchar_t lTitle[128] = L""; + wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD] = L""; + wchar_t lSingleFilterDescription[128] = L""; + wchar_t * * lFilterPatterns; + wchar_t * lTmpWChar; + char * lTmpChar; + int i; + + lFilterPatterns = (wchar_t **)malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]); + else lTmpWChar = tinyfd_mbcsTo16(aFilterPatterns[i]); + lFilterPatterns[i] = (wchar_t *)malloc((wcslen(lTmpWChar) + 1) * sizeof(wchar_t *)); + if (lFilterPatterns[i]) wcscpy(lFilterPatterns[i], lTmpWChar); + } + + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aDefaultPathAndFile) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile); + else lTmpWChar = tinyfd_mbcsTo16(aDefaultPathAndFile); + wcscpy(lDefaultPathAndFile, lTmpWChar); + } + if (aSingleFilterDescription) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription); + else lTmpWChar = tinyfd_mbcsTo16(aSingleFilterDescription); + wcscpy(lSingleFilterDescription, lTmpWChar); + } + + lTmpWChar = tinyfd_saveFileDialogW( + lTitle, + lDefaultPathAndFile, + aNumOfFilterPatterns, + (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/ + lSingleFilterDescription); + + for (i = 0; i < aNumOfFilterPatterns; i++) + { + free(lFilterPatterns[i]); + } + free(lFilterPatterns); + + if (!lTmpWChar) + { + return NULL; + } + + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + strcpy(aoBuff, lTmpChar); + if (tinyfd_winUtf8) (void)tinyfd_utf16to8(NULL); + else (void)tinyfd_utf16toMbcs(NULL); + + return aoBuff; +} + + +static char * openFileDialogWinGui( + char const * aTitle, /* NULL or "" */ + char const * aDefaultPathAndFile, /* NULL or "" */ + int aNumOfFilterPatterns, /* 0 */ + char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ + char const * aSingleFilterDescription, /* NULL or "image files" */ + int aAllowMultipleSelects) /* 0 or 1 */ +{ + wchar_t lTitle[128] = L""; + wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD] = L""; + wchar_t lSingleFilterDescription[128] = L""; + wchar_t * * lFilterPatterns; + wchar_t * lTmpWChar; + char * lTmpChar; + int i; + + lFilterPatterns = (wchar_t * *)malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]); + else lTmpWChar = tinyfd_mbcsTo16(aFilterPatterns[i]); + lFilterPatterns[i] = (wchar_t *)malloc((wcslen(lTmpWChar) + 1)*sizeof(wchar_t *)); + if (lFilterPatterns[i]) wcscpy(lFilterPatterns[i], lTmpWChar); + } + + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aDefaultPathAndFile) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile); + else lTmpWChar = tinyfd_mbcsTo16(aDefaultPathAndFile); + wcscpy(lDefaultPathAndFile, lTmpWChar); + } + if (aSingleFilterDescription) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription); + else lTmpWChar = tinyfd_mbcsTo16(aSingleFilterDescription); + wcscpy(lSingleFilterDescription, lTmpWChar); + } + + lTmpWChar = tinyfd_openFileDialogW( + lTitle, + lDefaultPathAndFile, + aNumOfFilterPatterns, + (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/ + lSingleFilterDescription, + aAllowMultipleSelects); + + for (i = 0; i < aNumOfFilterPatterns; i++) + { + free(lFilterPatterns[i]); + } + free(lFilterPatterns); + + if (!lTmpWChar) return NULL; + + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + (void)tinyfd_openFileDialogW(NULL, NULL, 0, NULL, NULL, -1); + + return lTmpChar; +} + + +static char * selectFolderDialogWinGui( + char * aoBuff, + char const * aTitle, /* NULL or "" */ + char const * aDefaultPath) /* NULL or "" */ +{ + wchar_t lTitle[128] = L""; + wchar_t lDefaultPath[MAX_PATH_OR_CMD] = L""; + wchar_t * lTmpWChar; + char * lTmpChar; + + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aDefaultPath) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPath); + else lTmpWChar = tinyfd_mbcsTo16(aDefaultPath); + wcscpy(lDefaultPath, lTmpWChar); + } + + lTmpWChar = tinyfd_selectFolderDialogW( + lTitle, + lDefaultPath); + + if (!lTmpWChar) + { + return NULL; + } + + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + strcpy(aoBuff, lTmpChar); + + return aoBuff; +} + + +static char * colorChooserWinGui( + char const * aTitle, /* NULL or "" */ + char const * aDefaultHexRGB, /* NULL or "#FF0000"*/ unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ { static char lResultHexRGB[8]; - wchar_t * lTitle; - wchar_t * lDefaultHexRGB; - wchar_t const * lTmpWChar; + wchar_t lTitle[128]; + wchar_t lDefaultHexRGB[16]; + wchar_t * lTmpWChar; char * lTmpChar; - lTitle = utf8to16(aTitle); - lDefaultHexRGB = utf8to16(aDefaultHexRGB); + if (aTitle) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle); + else lTmpWChar = tinyfd_mbcsTo16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aDefaultHexRGB) + { + if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultHexRGB); + else lTmpWChar = tinyfd_mbcsTo16(aDefaultHexRGB); + wcscpy(lDefaultHexRGB, lTmpWChar); + } lTmpWChar = tinyfd_colorChooserW( lTitle, @@ -1914,426 +2245,27 @@ static char const * colorChooserWinGui8( aDefaultRGB, aoResultRGB ); - free(lTitle); - free(lDefaultHexRGB); if (!lTmpWChar) { return NULL; } - lTmpChar = utf16to8(lTmpWChar); - strcpy(lResultHexRGB, lTmpChar); - free(lTmpChar); + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + strcpy(lResultHexRGB, lTmpChar); return lResultHexRGB; } -static int messageBoxWinGuiA( - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may contain \n and \t */ - char const * const aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */ - char const * const aIconType , /* "info" "warning" "error" "question" */ - int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ -{ - int lBoxReturnValue; - UINT aCode ; - - if ( aIconType && ! strcmp( "warning" , aIconType ) ) - { - aCode = MB_ICONWARNING ; - } - else if ( aIconType && ! strcmp("error", aIconType)) - { - aCode = MB_ICONERROR ; - } - else if ( aIconType && ! strcmp("question", aIconType)) - { - aCode = MB_ICONQUESTION ; - } - else - { - aCode = MB_ICONINFORMATION ; - } - - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - aCode += MB_OKCANCEL ; - if ( ! aDefaultButton ) - { - aCode += MB_DEFBUTTON2 ; - } - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - aCode += MB_YESNO ; - if ( ! aDefaultButton ) - { - aCode += MB_DEFBUTTON2 ; - } - } - else if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - aCode += MB_YESNOCANCEL; - if (!aDefaultButton) - { - aCode += MB_DEFBUTTON3; - } - else if (aDefaultButton == 2) - { - aCode += MB_DEFBUTTON2; - } - } - else - { - aCode += MB_OK ; - } - - aCode += MB_TOPMOST; - - lBoxReturnValue = MessageBoxA(GetForegroundWindow(), aMessage, aTitle, aCode); - - if (((aDialogType && !strcmp("yesnocancel", aDialogType)) - && (lBoxReturnValue == IDNO))) - { - return 2; - } - - if ( ( ( aDialogType - && strcmp("yesnocancel", aDialogType) - && strcmp("okcancel", aDialogType) - && strcmp("yesno", aDialogType))) - || (lBoxReturnValue == IDOK) - || (lBoxReturnValue == IDYES) ) - { - return 1 ; - } - else - { - return 0 ; - } -} - - -static char const * saveFileDialogWinGuiA( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile , /* NULL or "" */ - int const aNumOfFilterPatterns , /* 0 */ - char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription ) /* NULL or "image files" */ -{ - char lDirname [MAX_PATH_OR_CMD] ; - char lDialogString[MAX_PATH_OR_CMD]; - char lFilterPatterns[MAX_PATH_OR_CMD] = ""; - int i ; - char * p; - char * lRetval; - HRESULT lHResult; - OPENFILENAMEA ofn = { 0 }; - - lHResult = CoInitializeEx(NULL,0); - - getPathWithoutFinalSlash(lDirname, aDefaultPathAndFile); - getLastName(aoBuff, aDefaultPathAndFile); - - if (aNumOfFilterPatterns > 0) - { - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) - { - strcpy(lFilterPatterns, aSingleFilterDescription); - strcat(lFilterPatterns, "\n"); - } - strcat(lFilterPatterns, aFilterPatterns[0]); - for (i = 1; i < aNumOfFilterPatterns; i++) - { - strcat(lFilterPatterns, ";"); - strcat(lFilterPatterns, aFilterPatterns[i]); - } - strcat(lFilterPatterns, "\n"); - if ( ! (aSingleFilterDescription && strlen(aSingleFilterDescription) ) ) - { - strcpy(lDialogString, lFilterPatterns); - strcat(lFilterPatterns, lDialogString); - } - strcat(lFilterPatterns, "All Files\n*.*\n"); - p = lFilterPatterns; - while ((p = strchr(p, '\n')) != NULL) - { - *p = '\0'; - p ++ ; - } - } - - ofn.lStructSize = sizeof(OPENFILENAME) ; - ofn.hwndOwner = GetForegroundWindow(); - ofn.hInstance = 0 ; - ofn.lpstrFilter = lFilterPatterns && strlen(lFilterPatterns) ? lFilterPatterns : NULL; - ofn.lpstrCustomFilter = NULL ; - ofn.nMaxCustFilter = 0 ; - ofn.nFilterIndex = 1 ; - ofn.lpstrFile = aoBuff; - - ofn.nMaxFile = MAX_PATH_OR_CMD ; - ofn.lpstrFileTitle = NULL ; - ofn.nMaxFileTitle = MAX_PATH_OR_CMD / 2; - ofn.lpstrInitialDir = lDirname && strlen(lDirname) ? lDirname : NULL; - ofn.lpstrTitle = aTitle && strlen(aTitle) ? aTitle : NULL; - ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR ; - ofn.nFileOffset = 0 ; - ofn.nFileExtension = 0 ; - ofn.lpstrDefExt = NULL ; - ofn.lCustData = 0L ; - ofn.lpfnHook = NULL ; - ofn.lpTemplateName = NULL ; - - if ( GetSaveFileNameA ( & ofn ) == 0 ) - { - lRetval = NULL ; - } - else - { - lRetval = aoBuff ; - } - - if (lHResult==S_OK || lHResult==S_FALSE) - { - CoUninitialize(); - } - return lRetval ; -} - - -static char const * openFileDialogWinGuiA( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile , /* NULL or "" */ - int const aNumOfFilterPatterns , /* 0 */ - char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription , /* NULL or "image files" */ - int const aAllowMultipleSelects ) /* 0 or 1 */ -{ - char lDirname [MAX_PATH_OR_CMD] ; - char lFilterPatterns[MAX_PATH_OR_CMD] = ""; - char lDialogString[MAX_PATH_OR_CMD] ; - char * lPointers[MAX_MULTIPLE_FILES]; - size_t lLengths[MAX_MULTIPLE_FILES]; - int i , j ; - char * p; - size_t lBuffLen ; - char * lRetval; - HRESULT lHResult; - OPENFILENAMEA ofn = {0}; - - lHResult = CoInitializeEx(NULL,0); - - getPathWithoutFinalSlash(lDirname, aDefaultPathAndFile); - getLastName(aoBuff, aDefaultPathAndFile); - - if (aNumOfFilterPatterns > 0) - { - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) - { - strcpy(lFilterPatterns, aSingleFilterDescription); - strcat(lFilterPatterns, "\n"); - } - strcat(lFilterPatterns, aFilterPatterns[0]); - for (i = 1; i < aNumOfFilterPatterns; i++) - { - strcat(lFilterPatterns, ";"); - strcat(lFilterPatterns, aFilterPatterns[i]); - } - strcat(lFilterPatterns, "\n"); - if ( ! (aSingleFilterDescription && strlen(aSingleFilterDescription) ) ) - { - strcpy(lDialogString, lFilterPatterns); - strcat(lFilterPatterns, lDialogString); - } - strcat(lFilterPatterns, "All Files\n*.*\n"); - p = lFilterPatterns; - while ((p = strchr(p, '\n')) != NULL) - { - *p = '\0'; - p ++ ; - } - } - - ofn.lStructSize = sizeof( OPENFILENAME ) ; - ofn.hwndOwner = GetForegroundWindow(); - ofn.hInstance = 0 ; - ofn.lpstrFilter = lFilterPatterns && strlen(lFilterPatterns) ? lFilterPatterns : NULL; - ofn.lpstrCustomFilter = NULL ; - ofn.nMaxCustFilter = 0 ; - ofn.nFilterIndex = 1 ; - ofn.lpstrFile = aoBuff ; - ofn.nMaxFile = MAX_PATH_OR_CMD ; - ofn.lpstrFileTitle = NULL ; - ofn.nMaxFileTitle = MAX_PATH_OR_CMD / 2; - ofn.lpstrInitialDir = lDirname && strlen(lDirname) ? lDirname : NULL; - ofn.lpstrTitle = aTitle && strlen(aTitle) ? aTitle : NULL; - ofn.Flags = OFN_EXPLORER | OFN_NOCHANGEDIR ; - ofn.nFileOffset = 0 ; - ofn.nFileExtension = 0 ; - ofn.lpstrDefExt = NULL ; - ofn.lCustData = 0L ; - ofn.lpfnHook = NULL ; - ofn.lpTemplateName = NULL ; - - if ( aAllowMultipleSelects ) - { - ofn.Flags |= OFN_ALLOWMULTISELECT; - } - - if ( GetOpenFileNameA( & ofn ) == 0 ) - { - lRetval = NULL ; - } - else - { - lBuffLen = strlen(aoBuff) ; - lPointers[0] = aoBuff + lBuffLen + 1 ; - if ( !aAllowMultipleSelects || (lPointers[0][0] == '\0') ) - { - lRetval = aoBuff ; - } - else - { - i = 0 ; - do - { - lLengths[i] = strlen(lPointers[i]); - lPointers[i+1] = lPointers[i] + lLengths[i] + 1 ; - i ++ ; - } - while ( lPointers[i][0] != '\0' ); - i--; - p = aoBuff + MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD - 1 ; - * p = '\0'; - for ( j = i ; j >=0 ; j-- ) - { - p -= lLengths[j]; - memmove(p, lPointers[j], lLengths[j]); - p--; - *p = '\\'; - p -= lBuffLen ; - memmove(p, aoBuff, lBuffLen); - p--; - *p = '|'; - } - p++; - lRetval = p ; - } - } - - if (lHResult==S_OK || lHResult==S_FALSE) - { - CoUninitialize(); - } - return lRetval; -} - -#ifndef TINYFD_NOSELECTFOLDERWIN -static char const * selectFolderDialogWinGuiA( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) /* NULL or "" */ -{ - BROWSEINFOA bInfo ; - LPITEMIDLIST lpItem ; - HRESULT lHResult ; - char * lRetval = NULL ; - - lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); - - /* we can't use aDefaultPath */ - bInfo.hwndOwner = GetForegroundWindow(); - bInfo.pidlRoot = NULL ; - bInfo.pszDisplayName = aoBuff ; - bInfo.lpszTitle = aTitle && strlen(aTitle) ? aTitle : NULL; - if (lHResult == S_OK || lHResult == S_FALSE) - { - bInfo.ulFlags = BIF_USENEWUI; - } - bInfo.lpfn = BrowseCallbackProc; - bInfo.lParam = (LPARAM)aDefaultPath; - bInfo.iImage = -1 ; - - lpItem = SHBrowseForFolderA( & bInfo ) ; - if ( lpItem ) - { - SHGetPathFromIDListA( lpItem , aoBuff ) ; - lRetval = aoBuff; - } - - if (lHResult==S_OK || lHResult==S_FALSE) - { - CoUninitialize(); - } - return lRetval; -} -#endif /*TINYFD_NOSELECTFOLDERWIN*/ - - -static char const * colorChooserWinGuiA( - char const * const aTitle, /* NULL or "" */ - char const * const aDefaultHexRGB, /* NULL or "#FF0000"*/ - unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ - unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ -{ - static char lResultHexRGB[8]; - - CHOOSECOLORA cc; - COLORREF crCustColors[16]; - unsigned char lDefaultRGB[3]; - int lRet; - - if ( aDefaultHexRGB ) - { - Hex2RGB(aDefaultHexRGB, lDefaultRGB); - } - else - { - lDefaultRGB[0]=aDefaultRGB[0]; - lDefaultRGB[1]=aDefaultRGB[1]; - lDefaultRGB[2]=aDefaultRGB[2]; - } - - /* we can't use aTitle */ - cc.lStructSize = sizeof( CHOOSECOLOR ) ; - cc.hwndOwner = GetForegroundWindow(); - cc.hInstance = NULL ; - cc.rgbResult = RGB(lDefaultRGB[0], lDefaultRGB[1], lDefaultRGB[2]); - cc.lpCustColors = crCustColors; - cc.Flags = CC_RGBINIT | CC_FULLOPEN; - cc.lCustData = 0; - cc.lpfnHook = NULL; - cc.lpTemplateName = NULL; - - lRet = ChooseColorA(&cc); - - if ( ! lRet ) - { - return NULL; - } - - aoResultRGB[0] = GetRValue(cc.rgbResult); - aoResultRGB[1] = GetGValue(cc.rgbResult); - aoResultRGB[2] = GetBValue(cc.rgbResult); - - RGB2Hex(aoResultRGB, lResultHexRGB); - - return lResultHexRGB; -} - -#endif /* TINYFD_NOLIB */ - -static int dialogPresent( ) +static int dialogPresent(void) { static int lDialogPresent = -1 ; - char lBuff [MAX_PATH_OR_CMD] ; + char lBuff[MAX_PATH_OR_CMD] ; FILE * lIn ; char const * lString = "dialog.exe"; - if ( lDialogPresent < 0 ) + if (!tinyfd_allowCursesDialogs) return 0; + if (lDialogPresent < 0) { if (!(lIn = _popen("where dialog.exe","r"))) { @@ -2356,24 +2288,24 @@ static int dialogPresent( ) lDialogPresent = 1 ; } } - return lDialogPresent; + return lDialogPresent; } static int messageBoxWinConsole( - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may contain \n and \t */ - char const * const aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */ - char const * const aIconType , /* "info" "warning" "error" "question" */ - int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ + char const * aTitle , /* NULL or "" */ + char const * aMessage , /* NULL or "" may contain \n and \t */ + char const * aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */ + char const * aIconType , /* "info" "warning" "error" "question" */ + int aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { char lDialogString[MAX_PATH_OR_CMD]; char lDialogFile[MAX_PATH_OR_CMD]; FILE * lIn; - char lBuff [MAX_PATH_OR_CMD] = ""; - - strcpy( lDialogString , "dialog " ) ; - if ( aTitle && strlen(aTitle) ) + char lBuff[MAX_PATH_OR_CMD] = ""; + + strcpy(lDialogString, "dialog "); + if (aTitle && strlen(aTitle)) { strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; @@ -2421,7 +2353,7 @@ static int messageBoxWinConsole( strcat( lDialogString , "\"" ) ; if ( aMessage && strlen(aMessage) ) { - replaceSubStr( aMessage , "\n" , "\\n" , lBuff ) ; + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lBuff ) ; strcat(lDialogString, lBuff) ; lBuff[0]='\0'; } @@ -2438,19 +2370,19 @@ static int messageBoxWinConsole( strcat(lDialogString, " && echo 1 > "); } - strcpy(lDialogFile, getenv("USERPROFILE")); - strcat(lDialogFile, "\\AppData\\Local\\Temp\\tinyfd.txt"); + strcpy(lDialogFile, getenv("TEMP")); + strcat(lDialogFile, "\\tinyfd.txt"); strcat(lDialogString, lDialogFile); /*if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;*/ system( lDialogString ) ; - + if (!(lIn = fopen(lDialogFile, "r"))) { remove(lDialogFile); return 0 ; } - while (fgets(lBuff, sizeof(lBuff), lIn) != NULL) + while (fgets(lBuff, sizeof(lBuff), lIn) != NULL) {} fclose(lIn); remove(lDialogFile); @@ -2475,19 +2407,19 @@ static int messageBoxWinConsole( } -static char const * inputBoxWinConsole( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may NOT contain \n nor \t */ - char const * const aDefaultInput ) /* "" , if NULL it's a passwordBox */ +static int inputBoxWinConsole( + char * aoBuff , + char const * aTitle , /* NULL or "" */ + char const * aMessage , /* NULL or "" may NOT contain \n nor \t */ + char const * aDefaultInput ) /* "" , if NULL it's a passwordBox */ { char lDialogString[MAX_PATH_OR_CMD]; char lDialogFile[MAX_PATH_OR_CMD]; FILE * lIn; int lResult; - strcpy(lDialogFile, getenv("USERPROFILE")); - strcat(lDialogFile, "\\AppData\\Local\\Temp\\tinyfd.txt"); + strcpy(lDialogFile, getenv("TEMP")); + strcat(lDialogFile, "\\tinyfd.txt"); strcpy(lDialogString , "echo|set /p=1 >" ) ; strcat(lDialogString, lDialogFile); strcat( lDialogString , " & " ) ; @@ -2531,8 +2463,8 @@ static char const * inputBoxWinConsole( } strcat(lDialogString, "2>>"); - strcpy(lDialogFile, getenv("USERPROFILE")); - strcat(lDialogFile, "\\AppData\\Local\\Temp\\tinyfd.txt"); + strcpy(lDialogFile, getenv("TEMP")); + strcat(lDialogFile, "\\tinyfd.txt"); strcat(lDialogString, lDialogFile); strcat(lDialogString, " || echo 0 > "); strcat(lDialogString, lDialogFile); @@ -2543,7 +2475,8 @@ static char const * inputBoxWinConsole( if (!(lIn = fopen(lDialogFile, "r"))) { remove(lDialogFile); - return 0 ; + aoBuff[0] = '\0'; + return 0; } while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) {} @@ -2560,19 +2493,21 @@ static char const * inputBoxWinConsole( /* printf( "aoBuff: %s len: %lu \n" , aoBuff , strlen(aoBuff) ) ; */ lResult = strncmp( aoBuff , "1" , 1) ? 0 : 1 ; /* printf( "lResult: %d \n" , lResult ) ; */ - if ( ! lResult ) - { - return NULL ; - } - /* printf( "aoBuff+1: %s\n" , aoBuff+1 ) ; */ - return aoBuff+3 ; + if ( ! lResult ) + { + aoBuff[0] = '\0'; + return 0 ; + } + /* printf( "aoBuff+1: %s\n" , aoBuff+1 ) ; */ + strcpy(aoBuff, aoBuff+3); + return 1; } -static char const * saveFileDialogWinConsole( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile ) /* NULL or "" */ +static char * saveFileDialogWinConsole( + char * aoBuff , + char const * aTitle , /* NULL or "" */ + char const * aDefaultPathAndFile ) /* NULL or "" */ { char lDialogString[MAX_PATH_OR_CMD]; char lPathAndFile[MAX_PATH_OR_CMD] = ""; @@ -2585,7 +2520,7 @@ static char const * saveFileDialogWinConsole( strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - + strcat(lDialogString, "--backtitle \"") ; strcat(lDialogString, "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; @@ -2598,7 +2533,7 @@ static char const * saveFileDialogWinConsole( strcpy(lPathAndFile, aDefaultPathAndFile); replaceChr( lPathAndFile , '\\' , '/' ) ; } - + /* dialog.exe needs at least one separator */ if ( ! strchr(lPathAndFile, '/') ) { @@ -2606,8 +2541,8 @@ static char const * saveFileDialogWinConsole( } strcat(lDialogString, lPathAndFile) ; strcat(lDialogString, "\" 0 60 2>"); - strcpy(lPathAndFile, getenv("USERPROFILE")); - strcat(lPathAndFile, "\\AppData\\Local\\Temp\\tinyfd.txt"); + strcpy(lPathAndFile, getenv("TEMP")); + strcat(lPathAndFile, "\\tinyfd.txt"); strcat(lDialogString, lPathAndFile); /* printf( "lDialogString: %s\n" , lDialogString ) ; */ @@ -2633,16 +2568,16 @@ static char const * saveFileDialogWinConsole( } -static char const * openFileDialogWinConsole( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile , /* NULL or "" */ - int const aAllowMultipleSelects ) /* 0 or 1 */ +static char * openFileDialogWinConsole( + char const * aTitle , /* NULL or "" */ + char const * aDefaultPathAndFile ) /* NULL or "" */ { char lFilterPatterns[MAX_PATH_OR_CMD] = ""; char lDialogString[MAX_PATH_OR_CMD] ; FILE * lIn; + static char aoBuff[MAX_PATH_OR_CMD]; + strcpy( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { @@ -2663,7 +2598,7 @@ static char const * openFileDialogWinConsole( strcpy(lFilterPatterns, aDefaultPathAndFile); replaceChr( lFilterPatterns , '\\' , '/' ) ; } - + /* dialog.exe needs at least one separator */ if ( ! strchr(lFilterPatterns, '/') ) { @@ -2671,8 +2606,8 @@ static char const * openFileDialogWinConsole( } strcat(lDialogString, lFilterPatterns) ; strcat(lDialogString, "\" 0 60 2>"); - strcpy(lFilterPatterns, getenv("USERPROFILE")); - strcat(lFilterPatterns, "\\AppData\\Local\\Temp\\tinyfd.txt"); + strcpy(lFilterPatterns, getenv("TEMP")); + strcat(lFilterPatterns, "\\tinyfd.txt"); strcat(lDialogString, lFilterPatterns); /* printf( "lDialogString: %s\n" , lDialogString ) ; */ @@ -2693,15 +2628,15 @@ static char const * openFileDialogWinConsole( } -static char const * selectFolderDialogWinConsole( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) /* NULL or "" */ +static char * selectFolderDialogWinConsole( + char * aoBuff , + char const * aTitle , /* NULL or "" */ + char const * aDefaultPath ) /* NULL or "" */ { - char lDialogString [MAX_PATH_OR_CMD] ; - char lString [MAX_PATH_OR_CMD] ; + char lDialogString[MAX_PATH_OR_CMD] ; + char lString[MAX_PATH_OR_CMD] ; FILE * lIn ; - + strcpy( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { @@ -2730,8 +2665,8 @@ static char const * selectFolderDialogWinConsole( strcat(lDialogString, "./") ; } strcat(lDialogString, "\" 0 60 2>"); - strcpy(lString, getenv("USERPROFILE")); - strcat(lString, "\\AppData\\Local\\Temp\\tinyfd.txt"); + strcpy(lString, getenv("TEMP")); + strcat(lString, "\\tinyfd.txt"); strcat(lDialogString, lString); /* printf( "lDialogString: %s\n" , lDialogString ) ; */ @@ -2751,168 +2686,209 @@ static char const * selectFolderDialogWinConsole( return aoBuff; } +static void writeUtf8( char const * aUtf8String ) +{ + unsigned long lNum; + void * lConsoleHandle; + wchar_t * lTmpWChar; + + lConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); + lTmpWChar = tinyfd_utf8to16(aUtf8String); + (void)WriteConsoleW(lConsoleHandle, lTmpWChar, (DWORD) wcslen(lTmpWChar), &lNum, NULL); +} + int tinyfd_messageBox( - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may contain \n and \t */ - char const * const aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */ - char const * const aIconType , /* "info" "warning" "error" "question" */ - int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ + char const * aTitle, /* NULL or "" */ + char const * aMessage, /* NULL or "" may contain \n and \t */ + char const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */ + char const * aIconType, /* "info" "warning" "error" "question" */ + int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { - char lChar ; + char lChar; + UINT lOriginalCP = 0; + UINT lOriginalOutputCP = 0; -#ifndef TINYFD_NOLIB - if ((!tinyfd_forceConsole || !(GetConsoleWindow() || dialogPresent())) - && (!getenv("SSH_CLIENT") || getenv("DISPLAY"))) - { - if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "windows"); return 1; } - if (tinyfd_winUtf8) - { - return messageBoxWinGui8( - aTitle, aMessage, aDialogType, aIconType, aDefaultButton); - } - else - { - return messageBoxWinGuiA( - aTitle, aMessage, aDialogType, aIconType, aDefaultButton); - } - } - else -#endif /* TINYFD_NOLIB */ - if ( dialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return 0;} - return messageBoxWinConsole( - aTitle,aMessage,aDialogType,aIconType,aDefaultButton); - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;} - if (!gWarningDisplayed && !tinyfd_forceConsole ) - { - gWarningDisplayed = 1; - printf("\n\n%s\n", gTitle); - printf("%s\n\n", tinyfd_needs); - } - if ( aTitle && strlen(aTitle) ) - { - printf("\n%s\n\n", aTitle); - } - if ( aDialogType && !strcmp("yesno",aDialogType) ) - { - do - { - if ( aMessage && strlen(aMessage) ) - { - printf("%s\n",aMessage); - } - printf("y/n: "); - lChar = (char) tolower( _getch() ) ; - printf("\n\n"); - } - while ( lChar != 'y' && lChar != 'n' ) ; - return lChar == 'y' ? 1 : 0 ; - } - else if ( aDialogType && !strcmp("okcancel",aDialogType) ) - { - do - { - if ( aMessage && strlen(aMessage) ) - { - printf("%s\n",aMessage); - } - printf("[O]kay/[C]ancel: "); - lChar = (char) tolower( _getch() ) ; - printf("\n\n"); - } - while ( lChar != 'o' && lChar != 'c' ) ; - return lChar == 'o' ? 1 : 0 ; - } - else if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - do - { - if (aMessage && strlen(aMessage)) - { - printf("%s\n", aMessage); - } - printf("[Y]es/[N]o/[C]ancel: "); - lChar = (char)tolower(_getch()); - printf("\n\n"); - } while (lChar != 'y' && lChar != 'n' && lChar != 'c'); - return (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0 ; - } - else - { - if ( aMessage && strlen(aMessage) ) - { - printf("%s\n\n",aMessage); - } - printf("press enter to continue "); - lChar = (char) _getch() ; - printf("\n\n"); - return 1 ; - } - } + if (tfd_quoteDetected(aTitle)) return tinyfd_messageBox("INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton); + if (tfd_quoteDetected(aMessage)) return tinyfd_messageBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton); + + if ((!tinyfd_forceConsole || !(GetConsoleWindow() || dialogPresent())) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "windows"); return 1; } + return messageBoxWinGui(aTitle, aMessage, aDialogType, aIconType, aDefaultButton); + } + else if (dialogPresent()) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return 0; } + return messageBoxWinConsole( + aTitle, aMessage, aDialogType, aIconType, aDefaultButton); + } + else + { + if (!tinyfd_winUtf8) + { + lOriginalCP = GetConsoleCP(); + lOriginalOutputCP = GetConsoleOutputCP(); + (void)SetConsoleCP(GetACP()); + (void)SetConsoleOutputCP(GetACP()); + } + + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return 0; } + if (!gWarningDisplayed && !tinyfd_forceConsole) + { + gWarningDisplayed = 1; + printf("\n\n%s\n", gTitle); + printf("%s\n\n", tinyfd_needs); + } + + if (aTitle && strlen(aTitle)) + { + printf("\n"); + if (tinyfd_winUtf8) writeUtf8(aTitle); + else printf("%s", aTitle); + printf("\n\n"); + } + if (aDialogType && !strcmp("yesno", aDialogType)) + { + do + { + if (aMessage && strlen(aMessage)) + { + if (tinyfd_winUtf8) writeUtf8(aMessage); + else printf("%s", aMessage); + printf("\n"); + } + printf("y/n: "); + lChar = (char)tolower(_getch()); + printf("\n\n"); + } while (lChar != 'y' && lChar != 'n'); + if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } + return lChar == 'y' ? 1 : 0; + } + else if (aDialogType && !strcmp("okcancel", aDialogType)) + { + do + { + if (aMessage && strlen(aMessage)) + { + if (tinyfd_winUtf8) writeUtf8(aMessage); + else printf("%s", aMessage); + printf("\n"); + } + printf("[O]kay/[C]ancel: "); + lChar = (char)tolower(_getch()); + printf("\n\n"); + } while (lChar != 'o' && lChar != 'c'); + if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } + return lChar == 'o' ? 1 : 0; + } + else if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + do + { + if (aMessage && strlen(aMessage)) + { + if (tinyfd_winUtf8) writeUtf8(aMessage); + else printf("%s", aMessage); + printf("\n"); + } + printf("[Y]es/[N]o/[C]ancel: "); + lChar = (char)tolower(_getch()); + printf("\n\n"); + } while (lChar != 'y' && lChar != 'n' && lChar != 'c'); + if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } + return (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0; + } + else + { + if (aMessage && strlen(aMessage)) + { + if (tinyfd_winUtf8) writeUtf8(aMessage); + else printf("%s", aMessage); + printf("\n\n"); + } + printf("press enter to continue "); + lChar = (char)_getch(); + printf("\n\n"); + if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); } + return 1; + } + } } /* return has only meaning for tinyfd_query */ int tinyfd_notifyPopup( - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may contain \n \t */ - char const * const aIconType ) /* "info" "warning" "error" */ + char const * aTitle, /* NULL or "" */ + char const * aMessage , /* NULL or "" may contain \n \t */ + char const * aIconType ) /* "info" "warning" "error" */ { -#ifndef TINYFD_NOLIB - if ((!tinyfd_forceConsole || !( - GetConsoleWindow() || - dialogPresent())) - && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return 1;} - return notifyWinGui(aTitle, aMessage, aIconType); - } - else -#endif /* TINYFD_NOLIB */ - { - return tinyfd_messageBox(aTitle, aMessage, "ok" , aIconType, 0); - } + if (tfd_quoteDetected(aTitle)) return tinyfd_notifyPopup("INVALID TITLE WITH QUOTES", aMessage, aIconType); + if (tfd_quoteDetected(aMessage)) return tinyfd_notifyPopup(aTitle, "INVALID MESSAGE WITH QUOTES", aIconType); + + if ( powershellPresent() && (!tinyfd_forceConsole || !( + GetConsoleWindow() || + dialogPresent())) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return 1;} + return notifyWinGui(aTitle, aMessage, aIconType); + } + else + return tinyfd_messageBox(aTitle, aMessage, "ok" , aIconType, 0); } /* returns NULL on cancel */ -char const * tinyfd_inputBox( - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may NOT contain \n nor \t */ - char const * const aDefaultInput ) /* "" , if NULL it's a passwordBox */ +char * tinyfd_inputBox( + char const * aTitle , /* NULL or "" */ + char const * aMessage , /* NULL or "" (\n and \t have no effect) */ + char const * aDefaultInput ) /* "" , if NULL it's a passwordBox */ { - static char lBuff [MAX_PATH_OR_CMD] ; - char * lEOF; + static char lBuff[MAX_PATH_OR_CMD] = ""; + char * lEOF; -#ifndef TINYFD_NOLIB - DWORD mode = 0; - HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); + DWORD mode = 0; + HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); - if ((!tinyfd_forceConsole || !( - GetConsoleWindow() || - dialogPresent())) - && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;} - lBuff[0]='\0'; - return inputBoxWinGui(lBuff, aTitle, aMessage, aDefaultInput); - } - else -#endif /* TINYFD_NOLIB */ - if ( dialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} - lBuff[0]='\0'; - return inputBoxWinConsole(lBuff,aTitle,aMessage,aDefaultInput); - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} + unsigned long lNum; + void * lConsoleHandle; + char * lTmpChar; + wchar_t lBuffW[1024]; + + UINT lOriginalCP = 0; + UINT lOriginalOutputCP = 0; + + if (!aTitle && !aMessage && !aDefaultInput) return lBuff; /* now I can fill lBuff from outside */ + + if (tfd_quoteDetected(aTitle)) return tinyfd_inputBox("INVALID TITLE WITH QUOTES", aMessage, aDefaultInput); + if (tfd_quoteDetected(aMessage)) return tinyfd_inputBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDefaultInput); + if (tfd_quoteDetected(aDefaultInput)) return tinyfd_inputBox(aTitle, aMessage, "INVALID DEFAULT_INPUT WITH QUOTES"); + + mode = 0; + hStdin = GetStdHandle(STD_INPUT_HANDLE); + + if ((!tinyfd_forceConsole || !( + GetConsoleWindow() || + dialogPresent())) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} + lBuff[0]='\0'; + if (inputBoxWinGui(lBuff, aTitle, aMessage, aDefaultInput)) return lBuff; + else return NULL; + } + else if ( dialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} + lBuff[0]='\0'; + if (inputBoxWinConsole(lBuff, aTitle, aMessage, aDefaultInput) ) return lBuff; + else return NULL; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;} lBuff[0]='\0'; if (!gWarningDisplayed && !tinyfd_forceConsole) { @@ -2920,87 +2896,138 @@ char const * tinyfd_inputBox( printf("\n\n%s\n", gTitle); printf("%s\n\n", tinyfd_needs); } - if ( aTitle && strlen(aTitle) ) + + if (!tinyfd_winUtf8) + { + lOriginalCP = GetConsoleCP(); + lOriginalOutputCP = GetConsoleOutputCP(); + (void)SetConsoleCP(GetACP()); + (void)SetConsoleOutputCP(GetACP()); + } + + if (aTitle && strlen(aTitle)) { - printf("\n%s\n\n", aTitle); - } + printf("\n"); + if (tinyfd_winUtf8) writeUtf8(aTitle); + else printf("%s", aTitle); + printf("\n\n"); + } if ( aMessage && strlen(aMessage) ) { - printf("%s\n",aMessage); + if (tinyfd_winUtf8) writeUtf8(aMessage); + else printf("%s", aMessage); + printf("\n"); } printf("(ctrl-Z + enter to cancel): "); -#ifndef TINYFD_NOLIB if ( ! aDefaultInput ) { - GetConsoleMode(hStdin,&mode); - SetConsoleMode(hStdin,mode & (~ENABLE_ECHO_INPUT) ); + (void) GetConsoleMode(hStdin, &mode); + (void) SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); } -#endif /* TINYFD_NOLIB */ - lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); - if ( ! lEOF ) - { - return NULL; - } -#ifndef TINYFD_NOLIB - if ( ! aDefaultInput ) - { - SetConsoleMode(hStdin,mode); - printf("\n"); - } -#endif /* TINYFD_NOLIB */ - printf("\n"); - if ( strchr(lBuff,27) ) - { - return NULL ; - } - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - return lBuff ; - } + if (tinyfd_winUtf8) + { + lConsoleHandle = GetStdHandle(STD_INPUT_HANDLE); + (void) ReadConsoleW(lConsoleHandle, lBuffW, MAX_PATH_OR_CMD, &lNum, NULL); + if (!aDefaultInput) + { + (void)SetConsoleMode(hStdin, mode); + printf("\n"); + } + lBuffW[lNum] = '\0'; + if (lBuffW[wcslen(lBuffW) - 1] == '\n') lBuffW[wcslen(lBuffW) - 1] = '\0'; + if (lBuffW[wcslen(lBuffW) - 1] == '\r') lBuffW[wcslen(lBuffW) - 1] = '\0'; + lTmpChar = tinyfd_utf16to8(lBuffW); + if (lTmpChar) + { + strcpy(lBuff, lTmpChar); + return lBuff; + } + else + return NULL; + } + else + { + lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); + if (!aDefaultInput) + { + (void)SetConsoleMode(hStdin, mode); + printf("\n"); + } + + if (!tinyfd_winUtf8) + { + (void)SetConsoleCP(lOriginalCP); + (void)SetConsoleOutputCP(lOriginalOutputCP); + } + + if (!lEOF) + { + return NULL; + } + printf("\n"); + if (strchr(lBuff, 27)) + { + return NULL; + } + if (lBuff[strlen(lBuff) - 1] == '\n') + { + lBuff[strlen(lBuff) - 1] = '\0'; + } + return lBuff; + } + } } -char const * tinyfd_saveFileDialog( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile , /* NULL or "" */ - int const aNumOfFilterPatterns , /* 0 */ - char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription ) /* NULL or "image files" */ +char * tinyfd_saveFileDialog( + char const * aTitle , /* NULL or "" */ + char const * aDefaultPathAndFile , /* NULL or "" */ + int aNumOfFilterPatterns , /* 0 */ + char const * const * aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ + char const * aSingleFilterDescription ) /* NULL or "image files" */ { - static char lBuff [MAX_PATH_OR_CMD] ; + static char lBuff[MAX_PATH_OR_CMD] ; char lString[MAX_PATH_OR_CMD] ; - char const * p ; + char * p ; + char * lPointerInputBox; + int i; + lBuff[0]='\0'; -#ifndef TINYFD_NOLIB - if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) - && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) ) + + if (tfd_quoteDetected(aTitle)) return tinyfd_saveFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_saveFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_saveFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES"); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_saveFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL); + } + + + if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;} - if (tinyfd_winUtf8) - { - p = saveFileDialogWinGui8(lBuff, - aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); - } - else - { - p = saveFileDialogWinGuiA(lBuff, - aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); - } - } - else -#endif /* TINYFD_NOLIB */ - if ( dialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} - p = saveFileDialogWinConsole(lBuff,aTitle,aDefaultPathAndFile); - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} - p = tinyfd_inputBox(aTitle, "Save file",""); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} + p = saveFileDialogWinGui(lBuff, + aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, (char const * const *)aFilterPatterns, aSingleFilterDescription); } + else if (dialogPresent()) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } + p = saveFileDialogWinConsole(lBuff, aTitle, aDefaultPathAndFile); + } + else + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } + strcpy(lBuff, "Save file in "); + strcat(lBuff, getCurDir()); + + lPointerInputBox = tinyfd_inputBox(NULL,NULL,NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ + p = lBuff; + } if ( ! p || ! strlen( p ) ) { @@ -3021,47 +3048,52 @@ char const * tinyfd_saveFileDialog( /* in case of multiple files, the separator is | */ -char const * tinyfd_openFileDialog( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile , /* NULL or "" */ - int const aNumOfFilterPatterns , /* 0 */ - char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription , /* NULL or "image files" */ - int const aAllowMultipleSelects ) /* 0 or 1 */ +char * tinyfd_openFileDialog( + char const * aTitle , /* NULL or "" */ + char const * aDefaultPathAndFile, /* NULL or "" */ + int aNumOfFilterPatterns , /* 0 */ + char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ + char const * aSingleFilterDescription, /* NULL or "image files" */ + int aAllowMultipleSelects ) /* 0 or 1 */ { - static char lBuff[MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD]; - char const * p ; -#ifndef TINYFD_NOLIB - if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) - && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) ) + char lString[MAX_PATH_OR_CMD]; + char lBuff[MAX_PATH_OR_CMD]; + char * p; + char * lPointerInputBox; + int i; + + if (tfd_quoteDetected(aTitle)) return tinyfd_openFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_openFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_openFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_openFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects); + } + + if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;} - if (tinyfd_winUtf8) - { - p = openFileDialogWinGui8(lBuff, - aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, - aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); - } - else - { - p = openFileDialogWinGuiA(lBuff, - aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, - aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); - } - } - else -#endif /* TINYFD_NOLIB */ - if ( dialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} - p = openFileDialogWinConsole(lBuff, - aTitle,aDefaultPathAndFile,aAllowMultipleSelects); - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} - p = tinyfd_inputBox(aTitle, "Open file",""); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} + p = openFileDialogWinGui( aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, + (char const * const *)aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); } + else if (dialogPresent()) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } + p = openFileDialogWinConsole(aTitle, aDefaultPathAndFile); + } + else + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } + strcpy(lBuff, "Open file from "); + strcat(lBuff, getCurDir()); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ + p = lBuff; + } if ( ! p || ! strlen( p ) ) { @@ -3069,7 +3101,7 @@ char const * tinyfd_openFileDialog( } if ( aAllowMultipleSelects && strchr(p, '|') ) { - p = ensureFilesExist( lBuff , p ) ; + p = ensureFilesExist( (char *) p , p ) ; } else if ( ! fileExists(p) ) { @@ -3080,41 +3112,43 @@ char const * tinyfd_openFileDialog( } -char const * tinyfd_selectFolderDialog( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) /* NULL or "" */ +char * tinyfd_selectFolderDialog( + char const * aTitle , /* NULL or "" */ + char const * aDefaultPath ) /* NULL or "" */ { - static char lBuff [MAX_PATH_OR_CMD] ; - char const * p ; -#ifndef TINYFD_NOLIB - if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) - && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) ) + static char lBuff[MAX_PATH_OR_CMD]; + char * p; + char * lPointerInputBox; + char lString[MAX_PATH_OR_CMD]; + + if (tfd_quoteDetected(aTitle)) return tinyfd_selectFolderDialog("INVALID TITLE WITH QUOTES", aDefaultPath); + if (tfd_quoteDetected(aDefaultPath)) return tinyfd_selectFolderDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES"); + + if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;} - if (tinyfd_winUtf8) - { -#ifndef TINYFD_NOSELECTFOLDERWIN - p = selectFolderDialogWinGui8(lBuff, aTitle, aDefaultPath); - } - else - { - p = selectFolderDialogWinGuiA(lBuff, aTitle, aDefaultPath); -#endif /*TINYFD_NOSELECTFOLDERWIN*/ - } + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} + p = selectFolderDialogWinGui(lBuff, aTitle, aDefaultPath); } - else -#endif /* TINYFD_NOLIB */ - if ( dialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} - p = selectFolderDialogWinConsole(lBuff,aTitle,aDefaultPath); - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} - p = tinyfd_inputBox(aTitle, "Select folder",""); - } - + else + if (dialogPresent()) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } + p = selectFolderDialogWinConsole(lBuff, aTitle, aDefaultPath); + } + else + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } + strcpy(lBuff, "Select folder from "); + strcat(lBuff, getCurDir()); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ + p = lBuff; + } + if ( ! p || ! strlen( p ) || ! dirExists( p ) ) { return NULL ; @@ -3127,61 +3161,76 @@ char const * tinyfd_selectFolderDialog( /* aoResultRGB also contains the result */ /* aDefaultRGB is used only if aDefaultHexRGB is NULL */ /* aDefaultRGB and aoResultRGB can be the same array */ -char const * tinyfd_colorChooser( - char const * const aTitle, /* NULL or "" */ - char const * const aDefaultHexRGB, /* NULL or "#FF0000"*/ +char * tinyfd_colorChooser( + char const * aTitle, /* NULL or "" */ + char const * aDefaultHexRGB, /* NULL or "#FF0000"*/ unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ { - char lDefaultHexRGB[8]; - char * lpDefaultHexRGB; - int i; - char const * p ; + static char lDefaultHexRGB[16]; + int i; + char * p ; + char * lPointerInputBox; + char lString[MAX_PATH_OR_CMD]; -#ifndef TINYFD_NOLIB - if ( (!tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent()) ) - && (!getenv("SSH_CLIENT") || getenv("DISPLAY")) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;} - if (tinyfd_winUtf8) - { - return colorChooserWinGui8( - aTitle, aDefaultHexRGB, aDefaultRGB, aoResultRGB); - } - else - { - return colorChooserWinGuiA( - aTitle, aDefaultHexRGB, aDefaultRGB, aoResultRGB); - } - } - else -#endif /* TINYFD_NOLIB */ - if ( aDefaultHexRGB ) - { - lpDefaultHexRGB = (char *) aDefaultHexRGB ; - } - else - { - RGB2Hex( aDefaultRGB , lDefaultHexRGB ) ; - lpDefaultHexRGB = (char *) lDefaultHexRGB ; - } - p = tinyfd_inputBox(aTitle, - "Enter hex rgb color (i.e. #f5ca20)",lpDefaultHexRGB); - if (aTitle&&!strcmp(aTitle,"tinyfd_query")) return p; + lDefaultHexRGB[0] = '\0'; - if ( !p || (strlen(p) != 7) || (p[0] != '#') ) + if (tfd_quoteDetected(aTitle)) return tinyfd_colorChooser("INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB); + if (tfd_quoteDetected(aDefaultHexRGB)) return tinyfd_colorChooser(aTitle, "INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB); + + if ( (!tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent()) ) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} + p = colorChooserWinGui(aTitle, aDefaultHexRGB, aDefaultRGB, aoResultRGB); + if (p) { - return NULL ; + strcpy(lDefaultHexRGB, p); + return lDefaultHexRGB; } - for ( i = 1 ; i < 7 ; i ++ ) - { - if ( ! isxdigit( p[i] ) ) - { - return NULL ; - } - } - Hex2RGB(p,aoResultRGB); - return p ; + return NULL; + } + else if (dialogPresent()) + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; } + } + else + { + if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; } + } + + if (aDefaultHexRGB) + { + strncpy(lDefaultHexRGB, aDefaultHexRGB,7); + lDefaultHexRGB[7]='\0'; + } + else + { + RGB2Hex(aDefaultRGB, lDefaultHexRGB); + } + + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, "Enter hex rgb color (i.e. #f5ca20)", lDefaultHexRGB); + + if ( !p || (strlen(p) != 7) || (p[0] != '#') ) + { + return NULL ; + } + for ( i = 1 ; i < 7 ; i ++ ) + { + if ( ! isxdigit( (int) p[i] ) ) + { + return NULL ; + } + } + Hex2RGB(p,aoResultRGB); + + strcpy(lDefaultHexRGB, p); + + if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ + + return lDefaultHexRGB; } @@ -3191,7 +3240,7 @@ static char gPython2Name[16]; static char gPython3Name[16]; static char gPythonName[16]; -static int isDarwin( ) +int tfd_isDarwin(void) { static int lsIsDarwin = -1 ; struct utsname lUtsname ; @@ -3203,7 +3252,7 @@ static int isDarwin( ) } -static int dirExists( char const * const aDirPath ) +static int dirExists( char const * aDirPath ) { DIR * lDir ; if ( ! aDirPath || ! strlen( aDirPath ) ) @@ -3211,62 +3260,78 @@ static int dirExists( char const * const aDirPath ) lDir = opendir( aDirPath ) ; if ( ! lDir ) { - return 0 ; + return 0 ; } closedir( lDir ) ; return 1 ; } - -static int detectPresence( char const * const aExecutable ) -{ - char lBuff [MAX_PATH_OR_CMD] ; - char lTestedString [MAX_PATH_OR_CMD] = "which " ; - FILE * lIn ; - strcat( lTestedString , aExecutable ) ; - strcat( lTestedString, " 2>/dev/null "); - lIn = popen( lTestedString , "r" ) ; - if ( ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - && ( ! strchr( lBuff , ':' ) ) - && ( strncmp(lBuff, "no ", 3) ) ) - { /* present */ - pclose( lIn ) ; - if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 1); - return 1 ; - } - else - { - pclose( lIn ) ; - if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 0); - return 0 ; - } +static int detectPresence( char const * aExecutable ) +{ + char lBuff[MAX_PATH_OR_CMD] ; + char lTestedString[MAX_PATH_OR_CMD] = "which " ; + FILE * lIn ; +#ifdef _GNU_SOURCE + char* lAllocatedCharString; + int lSubstringUndetected; +#endif + + strcat( lTestedString , aExecutable ) ; + strcat( lTestedString, " 2>/dev/null "); + lIn = popen( lTestedString , "r" ) ; + if ( ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + && ( ! strchr( lBuff , ':' ) ) && ( strncmp(lBuff, "no ", 3) ) ) + { /* present */ + pclose( lIn ) ; + +#ifdef _GNU_SOURCE /*to bypass this, just comment out "#define _GNU_SOURCE" at the top of the file*/ + if ( lBuff[strlen( lBuff ) -1] == '\n' ) lBuff[strlen( lBuff ) -1] = '\0' ; + lAllocatedCharString = realpath(lBuff,NULL); /*same as canonicalize_file_name*/ + lSubstringUndetected = ! strstr(lAllocatedCharString, aExecutable); + free(lAllocatedCharString); + if (lSubstringUndetected) + { + if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 0); + return 0; + } +#endif /*_GNU_SOURCE*/ + + if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 1); + return 1 ; + } + else + { + pclose( lIn ) ; + if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 0); + return 0 ; + } } -static char const * getVersion( char const * const aExecutable ) /*version must be first numeral*/ +static char * getVersion( char const * aExecutable ) /*version must be first numeral*/ { - static char lBuff [MAX_PATH_OR_CMD] ; - char lTestedString [MAX_PATH_OR_CMD] ; + static char lBuff[MAX_PATH_OR_CMD] ; + char lTestedString[MAX_PATH_OR_CMD] ; FILE * lIn ; char * lTmp ; - + strcpy( lTestedString , aExecutable ) ; strcat( lTestedString , " --version" ) ; lIn = popen( lTestedString , "r" ) ; lTmp = fgets( lBuff , sizeof( lBuff ) , lIn ) ; pclose( lIn ) ; - + lTmp += strcspn(lTmp,"0123456789"); /* printf("lTmp:%s\n", lTmp); */ return lTmp ; } -static int * const getMajorMinorPatch( char const * const aExecutable ) +static int * getMajorMinorPatch( char const * aExecutable ) { - static int lArray [3] ; + static int lArray[3] ; char * lTmp ; lTmp = (char *) getVersion(aExecutable); @@ -3282,9 +3347,9 @@ static int * const getMajorMinorPatch( char const * const aExecutable ) } -static int tryCommand( char const * const aCommand ) +static int tryCommand( char const * aCommand ) { - char lBuff [MAX_PATH_OR_CMD] ; + char lBuff[MAX_PATH_OR_CMD] ; FILE * lIn ; lIn = popen( aCommand , "r" ) ; @@ -3302,10 +3367,10 @@ static int tryCommand( char const * const aCommand ) } -static int isTerminalRunning( ) +static int isTerminalRunning(void) { static int lIsTerminalRunning = -1 ; - if ( lIsTerminalRunning < 0 ) + if ( lIsTerminalRunning < 0 ) { lIsTerminalRunning = isatty(1); if (tinyfd_verbose) printf("isTerminalRunning %d\n", lIsTerminalRunning ); @@ -3314,27 +3379,31 @@ static int isTerminalRunning( ) } -static char const * dialogNameOnly( ) +static char * dialogNameOnly(void) { - static char lDialogName[128] = "*" ; - if ( lDialogName[0] == '*' ) - { - if ( isDarwin() && strcpy(lDialogName , "/opt/local/bin/dialog" ) - && detectPresence( lDialogName ) ) - {} - else if ( strcpy(lDialogName , "dialog" ) - && detectPresence( lDialogName ) ) - {} - else - { - strcpy(lDialogName , "" ) ; - } - } - return lDialogName ; + static char lDialogName[128] = "*" ; + if ( lDialogName[0] == '*' ) + { + if (!tinyfd_allowCursesDialogs) + { + strcpy(lDialogName , "" ); + } + else if ( tfd_isDarwin() && * strcpy(lDialogName , "/opt/local/bin/dialog" ) + && detectPresence( lDialogName ) ) + {} + else if ( * strcpy(lDialogName , "dialog" ) + && detectPresence( lDialogName ) ) + {} + else + { + strcpy(lDialogName , "" ); + } + } + return lDialogName ; } -int isDialogVersionBetter09b( ) +int isDialogVersionBetter09b(void) { char const * lDialogName ; char * lVersion ; @@ -3371,9 +3440,10 @@ int isDialogVersionBetter09b( ) } -static int whiptailPresentOnly( ) +static int whiptailPresentOnly(void) { static int lWhiptailPresent = -1 ; + if (!tinyfd_allowCursesDialogs) return 0; if ( lWhiptailPresent < 0 ) { lWhiptailPresent = detectPresence( "whiptail" ) ; @@ -3382,7 +3452,7 @@ static int whiptailPresentOnly( ) } -static char const * terminalName( ) +static char * terminalName(void) { static char lTerminalName[128] = "*" ; char lShellName[64] = "*" ; @@ -3404,9 +3474,9 @@ static char const * terminalName( ) return NULL ; } - if ( isDarwin() ) + if ( tfd_isDarwin() ) { - if ( strcpy(lTerminalName , "/opt/X11/bin/xterm" ) + if ( * strcpy(lTerminalName , "/opt/X11/bin/xterm" ) && detectPresence( lTerminalName ) ) { strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ; @@ -3417,73 +3487,73 @@ static char const * terminalName( ) strcpy(lTerminalName , "" ) ; } } - else if ( strcpy(lTerminalName,"xterm") /*good (small without parameters)*/ + else if ( * strcpy(lTerminalName,"xterm") /*good (small without parameters)*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"terminator") /*good*/ + else if ( * strcpy(lTerminalName,"terminator") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -x " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"lxterminal") /*good*/ + else if ( * strcpy(lTerminalName,"lxterminal") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"konsole") /*good*/ + else if ( * strcpy(lTerminalName,"konsole") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"kterm") /*good*/ + else if ( * strcpy(lTerminalName,"kterm") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"tilix") /*good*/ + else if ( * strcpy(lTerminalName,"tilix") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"xfce4-terminal") /*good*/ + else if ( * strcpy(lTerminalName,"xfce4-terminal") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -x " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"mate-terminal") /*good*/ + else if ( * strcpy(lTerminalName,"mate-terminal") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -x " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"Eterm") /*good*/ + else if ( * strcpy(lTerminalName,"Eterm") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"evilvte") /*good*/ + else if ( * strcpy(lTerminalName,"evilvte") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"pterm") /*good (only letters)*/ + else if ( * strcpy(lTerminalName,"pterm") /*good (only letters)*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"gnome-terminal") + else if ( * strcpy(lTerminalName,"gnome-terminal") && detectPresence(lTerminalName) && (lArray = getMajorMinorPatch(lTerminalName)) && ((lArray[0]<3) || (lArray[0]==3 && lArray[1]<=6)) ) { @@ -3509,9 +3579,9 @@ static char const * terminalName( ) } -static char const * dialogName( ) +static char * dialogName(void) { - char const * lDialogName ; + char * lDialogName ; lDialogName = dialogNameOnly( ) ; if ( strlen(lDialogName) && ( isTerminalRunning() || terminalName() ) ) { @@ -3524,7 +3594,7 @@ static char const * dialogName( ) } -static int whiptailPresent( ) +static int whiptailPresent(void) { int lWhiptailPresent ; lWhiptailPresent = whiptailPresentOnly( ) ; @@ -3540,15 +3610,15 @@ static int whiptailPresent( ) -static int graphicMode( ) +static int graphicMode(void) { return !( tinyfd_forceConsole && (isTerminalRunning() || terminalName()) ) - && ( getenv("DISPLAY") - || (isDarwin() && (!getenv("SSH_TTY") || getenv("DISPLAY") ) ) ) ; + && ( getenvDISPLAY() + || (tfd_isDarwin() && (!getenv("SSH_TTY") || getenvDISPLAY() ) ) ) ; } -static int pactlPresent( ) +static int pactlPresent(void) { static int lPactlPresent = -1 ; if ( lPactlPresent < 0 ) @@ -3559,7 +3629,7 @@ static int pactlPresent( ) } -static int speakertestPresent( ) +static int speakertestPresent(void) { static int lSpeakertestPresent = -1 ; if ( lSpeakertestPresent < 0 ) @@ -3570,18 +3640,40 @@ static int speakertestPresent( ) } -static int beepexePresent( ) +static int playPresent() { - static int lBeepexePresent = -1 ; - if ( lBeepexePresent < 0 ) - { - lBeepexePresent = detectPresence("beep.exe") ; - } - return lBeepexePresent ; + static int lPlayPresent = -1; + if (lPlayPresent < 0) + { + lPlayPresent = detectPresence("sox"); /*if sox is present, play is ready*/ + } + return lPlayPresent; } -static int xmessagePresent( ) +static int beepexePresent() +{ + static int lBeepexePresent = -1; + if (lBeepexePresent < 0) + { + lBeepexePresent = detectPresence("beep.exe"); + } + return lBeepexePresent; +} + + +static int beepPresent(void) +{ + static int lBeepPresent = -1 ; + if ( lBeepPresent < 0 ) + { + lBeepPresent = detectPresence("beep") ; + } + return lBeepPresent ; +} + + +static int xmessagePresent(void) { static int lXmessagePresent = -1 ; if ( lXmessagePresent < 0 ) @@ -3592,7 +3684,7 @@ static int xmessagePresent( ) } -static int gxmessagePresent( ) +static int gxmessagePresent(void) { static int lGxmessagePresent = -1 ; if ( lGxmessagePresent < 0 ) @@ -3603,7 +3695,7 @@ static int gxmessagePresent( ) } -static int gmessagePresent( ) +static int gmessagePresent(void) { static int lGmessagePresent = -1 ; if ( lGmessagePresent < 0 ) @@ -3614,7 +3706,7 @@ static int gmessagePresent( ) } -static int notifysendPresent( ) +static int notifysendPresent(void) { static int lNotifysendPresent = -1 ; if ( lNotifysendPresent < 0 ) @@ -3625,34 +3717,34 @@ static int notifysendPresent( ) } -static int perlPresent( ) +static int perlPresent(void) { - static int lPerlPresent = -1 ; - char lBuff [MAX_PATH_OR_CMD] ; - FILE * lIn ; + static int lPerlPresent = -1 ; + char lBuff[MAX_PATH_OR_CMD] ; + FILE * lIn ; - if ( lPerlPresent < 0 ) - { - lPerlPresent = detectPresence("perl") ; - if ( lPerlPresent ) - { - lIn = popen( "perl -MNet::DBus -e \"Net::DBus->session->get_service('org.freedesktop.Notifications')\" 2>&1" , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL ) - { - lPerlPresent = 2 ; - } - pclose( lIn ) ; - if (tinyfd_verbose) printf("perl-dbus %d\n", lPerlPresent); - } - } - return graphicMode() ? lPerlPresent : 0 ; + if ( lPerlPresent < 0 ) + { + lPerlPresent = detectPresence("perl") ; + if (lPerlPresent) + { + lIn = popen("perl -MNet::DBus -e \"Net::DBus->session->get_service('org.freedesktop.Notifications')\" 2>&1", "r"); + if (fgets(lBuff, sizeof(lBuff), lIn) == NULL) + { + lPerlPresent = 2; + } + pclose(lIn); + if (tinyfd_verbose) printf("perl-dbus %d\n", lPerlPresent); + } + } + return graphicMode() ? lPerlPresent : 0 ; } -static int afplayPresent( ) +static int afplayPresent(void) { static int lAfplayPresent = -1 ; - char lBuff [MAX_PATH_OR_CMD] ; + char lBuff[MAX_PATH_OR_CMD] ; FILE * lIn ; if ( lAfplayPresent < 0 ) @@ -3673,7 +3765,7 @@ static int afplayPresent( ) } -static int xdialogPresent( ) +static int xdialogPresent(void) { static int lXdialogPresent = -1 ; if ( lXdialogPresent < 0 ) @@ -3684,7 +3776,7 @@ static int xdialogPresent( ) } -static int gdialogPresent( ) +static int gdialogPresent(void) { static int lGdialoglPresent = -1 ; if ( lGdialoglPresent < 0 ) @@ -3695,7 +3787,7 @@ static int gdialogPresent( ) } -static int osascriptPresent( ) +static int osascriptPresent(void) { static int lOsascriptPresent = -1 ; if ( lOsascriptPresent < 0 ) @@ -3707,7 +3799,7 @@ static int osascriptPresent( ) } -static int qarmaPresent( ) +int tfd_qarmaPresent(void) { static int lQarmaPresent = -1 ; if ( lQarmaPresent < 0 ) @@ -3718,7 +3810,7 @@ static int qarmaPresent( ) } -static int matedialogPresent( ) +int tfd_matedialogPresent(void) { static int lMatedialogPresent = -1 ; if ( lMatedialogPresent < 0 ) @@ -3729,7 +3821,7 @@ static int matedialogPresent( ) } -static int shellementaryPresent( ) +int tfd_shellementaryPresent(void) { static int lShellementaryPresent = -1 ; if ( lShellementaryPresent < 0 ) @@ -3740,7 +3832,18 @@ static int shellementaryPresent( ) } -static int zenityPresent( ) +int tfd_xpropPresent(void) +{ + static int lXpropPresent = -1 ; + if ( lXpropPresent < 0 ) + { + lXpropPresent = detectPresence("xprop") ; + } + return lXpropPresent && graphicMode( ) ; +} + + +int tfd_zenityPresent(void) { static int lZenityPresent = -1 ; if ( lZenityPresent < 0 ) @@ -3751,17 +3854,28 @@ static int zenityPresent( ) } -static int zenity3Present() +int tfd_yadPresent(void) +{ + static int lYadPresent = -1; + if (lYadPresent < 0) + { + lYadPresent = detectPresence("yad"); + } + return lYadPresent && graphicMode(); +} + + +int tfd_zenity3Present(void) { static int lZenity3Present = -1 ; - char lBuff [MAX_PATH_OR_CMD] ; + char lBuff[MAX_PATH_OR_CMD] ; FILE * lIn ; int lIntTmp ; if ( lZenity3Present < 0 ) { lZenity3Present = 0 ; - if ( zenityPresent() ) + if ( tfd_zenityPresent() ) { lIn = popen( "zenity --version" , "r" ) ; if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) @@ -3783,7 +3897,7 @@ static int zenity3Present() { lZenity3Present = 2 ; } - if (tinyfd_verbose) printf("zenity %d\n", lZenity3Present); + if (tinyfd_verbose) printf("zenity type %d\n", lZenity3Present); } pclose( lIn ) ; } @@ -3792,16 +3906,16 @@ static int zenity3Present() } -static int kdialogPresent( ) +int tfd_kdialogPresent(void) { static int lKdialogPresent = -1 ; - char lBuff [MAX_PATH_OR_CMD] ; + char lBuff[MAX_PATH_OR_CMD] ; FILE * lIn ; char * lDesktop; if ( lKdialogPresent < 0 ) { - if ( zenityPresent() ) + if ( tfd_zenityPresent() ) { lDesktop = getenv("XDG_SESSION_DESKTOP"); if ( !lDesktop || ( strcmp(lDesktop, "KDE") && strcmp(lDesktop, "lxqt") ) ) @@ -3845,10 +3959,10 @@ static int kdialogPresent( ) } -static int osx9orBetter( ) +static int osx9orBetter(void) { static int lOsx9orBetter = -1 ; - char lBuff [MAX_PATH_OR_CMD] ; + char lBuff[MAX_PATH_OR_CMD] ; FILE * lIn ; int V,v; @@ -3866,47 +3980,13 @@ static int osx9orBetter( ) } } pclose( lIn ) ; - if (tinyfd_verbose) printf("Osx10 = %d, %d = %s\n", lOsx9orBetter, V, lBuff) ; + if (tinyfd_verbose) printf("Osx10 = %d, %d = %s\n", lOsx9orBetter, V, lBuff) ; } return lOsx9orBetter ; } -static int python2Present( ) -{ - static int lPython2Present = -1 ; - int i; - - if ( lPython2Present < 0 ) - { - lPython2Present = 0 ; - strcpy(gPython2Name , "python2" ) ; - if ( detectPresence(gPython2Name) ) lPython2Present = 1; - else - { - for ( i = 9 ; i >= 0 ; i -- ) - { - sprintf( gPython2Name , "python2.%d" , i ) ; - if ( detectPresence(gPython2Name) ) - { - lPython2Present = 1; - break; - } - } - /*if ( ! lPython2Present ) - { - strcpy(gPython2Name , "python" ) ; - if ( detectPresence(gPython2Name) ) lPython2Present = 1; - }*/ - } - if (tinyfd_verbose) printf("lPython2Present %d\n", lPython2Present) ; - if (tinyfd_verbose) printf("gPython2Name %s\n", gPython2Name) ; - } - return lPython2Present ; -} - - -static int python3Present( ) +static int python3Present(void) { static int lPython3Present = -1 ; int i; @@ -3927,46 +4007,48 @@ static int python3Present( ) break; } } - /*if ( ! lPython3Present ) - { - strcpy(gPython3Name , "python" ) ; - if ( detectPresence(gPython3Name) ) lPython3Present = 1; - }*/ } if (tinyfd_verbose) printf("lPython3Present %d\n", lPython3Present) ; if (tinyfd_verbose) printf("gPython3Name %s\n", gPython3Name) ; } - return lPython3Present ; + return lPython3Present ; } -static int tkinter2Present( ) +static int python2Present(void) { - static int lTkinter2Present = -1 ; - char lPythonCommand[256]; - char lPythonParams[256] = -"-S -c \"try:\n\timport Tkinter;\nexcept:\n\tprint 0;\""; + static int lPython2Present = -1 ; + int i; - - if ( lTkinter2Present < 0 ) - { - lTkinter2Present = 0 ; - if ( python2Present() ) - { - sprintf( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; - lTkinter2Present = tryCommand(lPythonCommand) ; - } - if (tinyfd_verbose) printf("lTkinter2Present %d\n", lTkinter2Present) ; - } - return lTkinter2Present && graphicMode() && !(isDarwin() && getenv("SSH_TTY") ); + if ( lPython2Present < 0 ) + { + lPython2Present = 0 ; + strcpy(gPython2Name , "python2" ) ; + if ( detectPresence(gPython2Name) ) lPython2Present = 1; + else + { + for ( i = 9 ; i >= 0 ; i -- ) + { + sprintf( gPython2Name , "python2.%d" , i ) ; + if ( detectPresence(gPython2Name) ) + { + lPython2Present = 1; + break; + } + } + } + if (tinyfd_verbose) printf("lPython2Present %d\n", lPython2Present) ; + if (tinyfd_verbose) printf("gPython2Name %s\n", gPython2Name) ; + } + return lPython2Present ; } -static int tkinter3Present( ) +static int tkinter3Present(void) { static int lTkinter3Present = -1 ; char lPythonCommand[256]; - char lPythonParams[256] = + char lPythonParams[128] = "-S -c \"try:\n\timport tkinter;\nexcept:\n\tprint(0);\""; if ( lTkinter3Present < 0 ) @@ -3979,55 +4061,77 @@ static int tkinter3Present( ) } if (tinyfd_verbose) printf("lTkinter3Present %d\n", lTkinter3Present) ; } - return lTkinter3Present && graphicMode() && !(isDarwin() && getenv("SSH_TTY") ); + return lTkinter3Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") ); } -static int pythonDbusPresent( ) +static int tkinter2Present(void) { - static int lDbusPresent = -1 ; - char lPythonCommand[256]; + static int lTkinter2Present = -1 ; + char lPythonCommand[256]; + char lPythonParams[128] = + "-S -c \"try:\n\timport Tkinter;\nexcept:\n\tprint 0;\""; + + if ( lTkinter2Present < 0 ) + { + lTkinter2Present = 0 ; + if ( python2Present() ) + { + sprintf( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; + lTkinter2Present = tryCommand(lPythonCommand) ; + } + if (tinyfd_verbose) printf("lTkinter2Present %d\n", lTkinter2Present) ; + } + return lTkinter2Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") ); +} + + +static int pythonDbusPresent(void) +{ + static int lPythonDbusPresent = -1 ; + char lPythonCommand[384]; char lPythonParams[256] = "-c \"try:\n\timport dbus;bus=dbus.SessionBus();\ notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');\ notify=dbus.Interface(notif,'org.freedesktop.Notifications');\nexcept:\n\tprint(0);\""; - if ( lDbusPresent < 0 ) + if (lPythonDbusPresent < 0 ) { - lDbusPresent = 0 ; + lPythonDbusPresent = 0 ; if ( python2Present() ) { strcpy(gPythonName , gPython2Name ) ; sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ; - lDbusPresent = tryCommand(lPythonCommand) ; + lPythonDbusPresent = tryCommand(lPythonCommand) ; } - if ( ! lDbusPresent && python3Present() ) + if ( !lPythonDbusPresent && python3Present() ) { strcpy(gPythonName , gPython3Name ) ; sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ; - lDbusPresent = tryCommand(lPythonCommand) ; + lPythonDbusPresent = tryCommand(lPythonCommand) ; } - if (tinyfd_verbose) printf("lDbusPresent %d\n", lDbusPresent) ; + if (tinyfd_verbose) printf("lPythonDbusPresent %d\n", lPythonDbusPresent) ; if (tinyfd_verbose) printf("gPythonName %s\n", gPythonName) ; } - return lDbusPresent && graphicMode() && !(isDarwin() && getenv("SSH_TTY") ); + return lPythonDbusPresent && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") ); } -static void sigHandler(int sig) +static void sigHandler(int signum) { FILE * lIn ; if ( ( lIn = popen( "pactl unload-module module-sine" , "r" ) ) ) { pclose( lIn ) ; } + if (tinyfd_verbose) printf("tinyfiledialogs caught signal %d\n", signum); } -void tinyfd_beep() +void tinyfd_beep(void) { - char lDialogString [256] ; + char lDialogString[256] ; FILE * lIn ; if ( osascriptPresent() ) @@ -4041,20 +4145,28 @@ void tinyfd_beep() strcpy( lDialogString , "osascript -e 'tell application \"System Events\" to beep'") ; } } - else if ( pactlPresent() ) + else if ( pactlPresent() ) { signal(SIGINT, sigHandler); /*strcpy( lDialogString , "pactl load-module module-sine frequency=440;sleep .3;pactl unload-module module-sine" ) ;*/ strcpy( lDialogString , "thnum=$(pactl load-module module-sine frequency=440);sleep .3;pactl unload-module $thnum" ) ; } - else if ( speakertestPresent() ) + else if ( speakertestPresent() ) { /*strcpy( lDialogString , "timeout -k .3 .3 speaker-test --frequency 440 --test sine > /dev/tty" ) ;*/ - strcpy( lDialogString , "( speaker-test -t sine -f 440 > /dev/tty )& pid=$!;sleep .3; kill -9 $pid" ) ; + strcpy( lDialogString , "( speaker-test -t sine -f 440 > /dev/tty )& pid=$!;sleep .4; kill -9 $pid" ) ; /*.3 was too short for mac g3*/ } - else if ( beepexePresent() ) + else if (beepexePresent()) { - strcpy( lDialogString , "beep.exe 440 300" ) ; + strcpy(lDialogString, "beep.exe 440 300"); + } + else if (playPresent()) /* play is part of sox */ + { + strcpy(lDialogString, "play -q -n synth .3 sine 440"); + } + else if ( beepPresent() ) + { + strcpy( lDialogString , "beep -f 440 -l 300" ) ; } else { @@ -4076,13 +4188,13 @@ void tinyfd_beep() int tinyfd_messageBox( - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may contain \n and \t */ - char const * const aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */ - char const * const aIconType , /* "info" "warning" "error" "question" */ - int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ + char const * aTitle , /* NULL or "" */ + char const * aMessage , /* NULL or "" may contain \n and \t */ + char const * aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */ + char const * aIconType , /* "info" "warning" "error" "question" */ + int aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { - char lBuff [MAX_PATH_OR_CMD] ; + char lBuff[MAX_PATH_OR_CMD] ; char * lDialogString = NULL ; char * lpDialogString; FILE * lIn ; @@ -4097,6 +4209,9 @@ int tinyfd_messageBox( lBuff[0]='\0'; + if (tfd_quoteDetected(aTitle)) return tinyfd_messageBox("INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton); + if (tfd_quoteDetected(aMessage)) return tinyfd_messageBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton); + lTitleLen = aTitle ? strlen(aTitle) : 0 ; lMessageLen = aMessage ? strlen(aMessage) : 0 ; if ( !aTitle || strcmp(aTitle,"tinyfd_query") ) @@ -4145,7 +4260,7 @@ int tinyfd_messageBox( else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) { strcat( lDialogString ,"buttons {\"No\", \"Yes\"} " ) ; - if (aDefaultButton) + if (aDefaultButton) { strcat( lDialogString ,"default button \"Yes\" " ) ; } @@ -4158,7 +4273,7 @@ int tinyfd_messageBox( else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) { strcat( lDialogString ,"buttons {\"No\", \"Yes\", \"Cancel\"} " ) ; - switch (aDefaultButton) + switch (aDefaultButton) { case 1: strcat( lDialogString ,"default button \"Yes\" " ) ; break; case 2: strcat( lDialogString ,"default button \"No\" " ) ; break; @@ -4185,12 +4300,12 @@ int tinyfd_messageBox( strcat( lDialogString, "-e 'end try'") ; if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; } - else if ( kdialogPresent() ) + else if ( tfd_kdialogPresent() ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} strcpy( lDialogString , "kdialog" ) ; - if ( kdialogPresent() == 2 ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -4252,23 +4367,23 @@ int tinyfd_messageBox( strcat( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi"); } } - else if ( zenityPresent() || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) + else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) { - if ( zenityPresent() ) + if ( tfd_zenityPresent() ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;} strcpy( lDialogString , "szAnswer=$(zenity" ) ; - if ( (zenity3Present() >= 4) && !getenv("SSH_TTY") ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } } - else if ( matedialogPresent() ) + else if ( tfd_matedialogPresent() ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return 1;} strcpy( lDialogString , "szAnswer=$(matedialog" ) ; } - else if ( shellementaryPresent() ) + else if ( tfd_shellementaryPresent() ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return 1;} strcpy( lDialogString , "szAnswer=$(shellementary" ) ; @@ -4277,12 +4392,12 @@ int tinyfd_messageBox( { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return 1;} strcpy( lDialogString , "szAnswer=$(qarma" ) ; - if ( !getenv("SSH_TTY") ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } } - strcat(lDialogString, " --"); + strcat(lDialogString, " --"); if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) { @@ -4309,19 +4424,18 @@ int tinyfd_messageBox( { strcat( lDialogString , "info" ) ; } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title=\"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, " --text=\"") ; - strcat(lDialogString, aMessage) ; - strcat(lDialogString, "\"") ; - } - if ( (zenity3Present() >= 3) || (!zenityPresent() && (shellementaryPresent() || qarmaPresent()) ) ) + + strcat(lDialogString, " --title=\""); + if ( aTitle && strlen(aTitle) ) strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\""); + + if (strcmp("yesnocancel", aDialogType)) strcat(lDialogString, " --no-wrap"); + + strcat(lDialogString, " --text=\"") ; + if (aMessage && strlen(aMessage)) strcat(lDialogString, aMessage) ; + strcat(lDialogString, "\"") ; + + if ( (tfd_zenity3Present() >= 3) || (!tfd_zenityPresent() && (tfd_shellementaryPresent() || tfd_qarmaPresent()) ) ) { strcat( lDialogString , " --icon-name=dialog-" ) ; if ( aIconType && (! strcmp( "question" , aIconType ) @@ -4347,21 +4461,172 @@ int tinyfd_messageBox( { strcat( lDialogString , ");if [ $? = 0 ];then echo 1;else echo 0;fi"); } - } - else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter2Present() ) + } + + else if (tfd_yadPresent()) + { + if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return 1; } + strcpy(lDialogString, "szAnswer=$(yad --"); + if (aDialogType && !strcmp("ok", aDialogType)) + { + strcat(lDialogString,"button=Ok:1"); + } + else if (aDialogType && !strcmp("okcancel", aDialogType)) + { + strcat(lDialogString,"button=Ok:1 --button=Cancel:0"); + } + else if (aDialogType && !strcmp("yesno", aDialogType)) + { + strcat(lDialogString, "button=Yes:1 --button=No:0"); + } + else if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + strcat(lDialogString, "button=Yes:1 --button=No:2 --button=Cancel:0"); + } + else if (aIconType && !strcmp("error", aIconType)) + { + strcat(lDialogString, "error"); + } + else if (aIconType && !strcmp("warning", aIconType)) + { + strcat(lDialogString, "warning"); + } + else + { + strcat(lDialogString, "info"); + } + if (aTitle && strlen(aTitle)) + { + strcat(lDialogString, " --title=\""); + strcat(lDialogString, aTitle); + strcat(lDialogString, "\""); + } + if (aMessage && strlen(aMessage)) + { + strcat(lDialogString, " --text=\""); + strcat(lDialogString, aMessage); + strcat(lDialogString, "\""); + } + + strcat(lDialogString, " --icon-name=dialog-"); + if (aIconType && (!strcmp("question", aIconType) + || !strcmp("error", aIconType) + || !strcmp("warning", aIconType))) + { + strcat(lDialogString, aIconType); + } + else + { + strcat(lDialogString, "information"); + } + + if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null "); + strcat(lDialogString,");echo $?"); + } + + else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter3Present() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return 1;} + + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter;from tkinter import messagebox;root=tkinter.Tk();root.withdraw();"); + + strcat( lDialogString ,"res=messagebox." ) ; + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + strcat( lDialogString , "askokcancel(" ) ; + if ( aDefaultButton ) + { + strcat( lDialogString , "default=messagebox.OK," ) ; + } + else + { + strcat( lDialogString , "default=messagebox.CANCEL," ) ; + } + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + strcat( lDialogString , "askyesno(" ) ; + if ( aDefaultButton ) + { + strcat( lDialogString , "default=messagebox.YES," ) ; + } + else + { + strcat( lDialogString , "default=messagebox.NO," ) ; + } + } + else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , "askyesnocancel(" ) ; + switch ( aDefaultButton ) + { + case 1: strcat( lDialogString , "default=messagebox.YES," ); break; + case 2: strcat( lDialogString , "default=messagebox.NO," ); break; + case 0: strcat( lDialogString , "default=messagebox.CANCEL," ); break; + } + } + else + { + strcat( lDialogString , "showinfo(" ) ; + } + + strcat( lDialogString , "icon='" ) ; + if ( aIconType && (! strcmp( "question" , aIconType ) + || ! strcmp( "error" , aIconType ) + || ! strcmp( "warning" , aIconType ) ) ) + { + strcat( lDialogString , aIconType ) ; + } + else + { + strcat( lDialogString , "info" ) ; + } + + strcat(lDialogString, "',") ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "',") ; + } + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, "message='") ; + lpDialogString = lDialogString + strlen(lDialogString); + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + strcat(lDialogString, "'") ; + } + + if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat(lDialogString, ");\n\ +if res is None :\n\tprint(0)\n\ +elif res is False :\n\tprint(2)\n\ +else :\n\tprint (1)\n\"" ) ; + } + else + { + strcat(lDialogString, ");\n\ +if res is False :\n\tprint(0)\n\ +else :\n\tprint(1)\n\"" ) ; + } + } + else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter2Present() ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return 1;} - - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( ) ) + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcat( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) { strcat( lDialogString , " -i" ) ; /* for osx without console */ } - + strcat( lDialogString , " -S -c \"import Tkinter,tkMessageBox;root=Tkinter.Tk();root.withdraw();"); - - if ( isDarwin( ) ) + + if ( tfd_isDarwin( ) ) { strcat( lDialogString , "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \ @@ -4419,7 +4684,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); { strcat( lDialogString , "info" ) ; } - + strcat(lDialogString, "',") ; if ( aTitle && strlen(aTitle) ) { @@ -4431,7 +4696,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); { strcat(lDialogString, "message='") ; lpDialogString = lDialogString + strlen(lDialogString); - replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; strcat(lDialogString, "'") ; } @@ -4449,95 +4714,6 @@ if res is False :\n\tprint 0\n\ else :\n\tprint 1\n\"" ) ; } } - else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter3Present() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return 1;} - - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter;from tkinter import messagebox;root=tkinter.Tk();root.withdraw();"); - - strcat( lDialogString ,"res=messagebox." ) ; - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - strcat( lDialogString , "askokcancel(" ) ; - if ( aDefaultButton ) - { - strcat( lDialogString , "default=messagebox.OK," ) ; - } - else - { - strcat( lDialogString , "default=messagebox.CANCEL," ) ; - } - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - strcat( lDialogString , "askyesno(" ) ; - if ( aDefaultButton ) - { - strcat( lDialogString , "default=messagebox.YES," ) ; - } - else - { - strcat( lDialogString , "default=messagebox.NO," ) ; - } - } - else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , "askyesnocancel(" ) ; - switch ( aDefaultButton ) - { - case 1: strcat( lDialogString , "default=messagebox.YES," ); break; - case 2: strcat( lDialogString , "default=messagebox.NO," ); break; - case 0: strcat( lDialogString , "default=messagebox.CANCEL," ); break; - } - } - else - { - strcat( lDialogString , "showinfo(" ) ; - } - - strcat( lDialogString , "icon='" ) ; - if ( aIconType && (! strcmp( "question" , aIconType ) - || ! strcmp( "error" , aIconType ) - || ! strcmp( "warning" , aIconType ) ) ) - { - strcat( lDialogString , aIconType ) ; - } - else - { - strcat( lDialogString , "info" ) ; - } - - strcat(lDialogString, "',") ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; - } - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, "message='") ; - lpDialogString = lDialogString + strlen(lDialogString); - replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; - strcat(lDialogString, "'") ; - } - - if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat(lDialogString, ");\n\ -if res is None :\n\tprint(0)\n\ -elif res is False :\n\tprint(2)\n\ -else :\n\tprint 1\n\"" ) ; - } - else - { - strcat(lDialogString, ");\n\ -if res is False :\n\tprint(0)\n\ -else :\n\tprint(1)\n\"" ) ; - } - } else if ( gxmessagePresent() || gmessagePresent() || (!gdialogPresent() && !xdialogPresent() && xmessagePresent()) ) { if ( gxmessagePresent() ) @@ -4656,7 +4832,7 @@ else :\n\tprint(1)\n\"" ) ; if ( !xdialogPresent() && !gdialogPresent() ) { - if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) + if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) || !strcmp( "yesnocancel" , aDialogType ) ) ) { strcat(lDialogString, "--backtitle \"") ; @@ -4838,7 +5014,7 @@ tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; if ( aMessage && strlen(aMessage) ) { lpDialogString = lDialogString + strlen(lDialogString); - replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; } strcat(lDialogString, "','','',5000)\"") ; } @@ -4846,13 +5022,15 @@ tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;} - sprintf( lDialogString , "perl -e \"use Net::DBus;\ - my \\$sessionBus = Net::DBus->session;\ - my \\$notificationsService = \\$sessionBus->get_service('org.freedesktop.Notifications');\ - my \\$notificationsObject = \\$notificationsService->get_object('/org/freedesktop/Notifications',\ - 'org.freedesktop.Notifications');\ - my \\$notificationId;\\$notificationId = \\$notificationsObject->Notify(shift, 0, '%s', '%s', '%s', [], {}, -1);\" ", - aIconType?aIconType:"", aTitle?aTitle:"", aMessage?aMessage:"" ) ; + strcpy( lDialogString , "perl -e \"use Net::DBus;\ +my \\$sessionBus = Net::DBus->session;\ +my \\$notificationsService = \\$sessionBus->get_service('org.freedesktop.Notifications');\ +my \\$notificationsObject = \\$notificationsService->get_object('/org/freedesktop/Notifications',\ +'org.freedesktop.Notifications');"); + + sprintf( lDialogString + strlen(lDialogString), +"my \\$notificationId;\\$notificationId = \\$notificationsObject->Notify(shift, 0, '%s', '%s', '%s', [], {}, -1);\" ", + aIconType?aIconType:"", aTitle?aTitle:"", aMessage?aMessage:"" ) ; } else if ( !isTerminalRunning() && notifysendPresent() && !strcmp("ok" , aDialogType) ) { @@ -4873,9 +5051,9 @@ tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; } if ( aMessage && strlen(aMessage) ) { - replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ; - replaceSubStr( aMessage , "\n" , " | " , lBuff ) ; - replaceSubStr( aMessage , "\t" , " " , lBuff ) ; + tfd_replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ; + tfd_replaceSubStr( aMessage , "\n" , " | " , lBuff ) ; + tfd_replaceSubStr( aMessage , "\t" , " " , lBuff ) ; strcat(lDialogString, lBuff) ; } strcat( lDialogString , "\"" ) ; @@ -4953,7 +5131,7 @@ tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; } printf("press enter to continue "); fflush(stdout); getchar() ; - printf("\n\n"); + printf("\n\n"); lResult = 1 ; } tcsetattr(0, TCSANOW, &infoOri); @@ -4980,10 +5158,10 @@ tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; } /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ - if (aDialogType && !strcmp("yesnocancel", aDialogType)) + if (aDialogType && !strcmp("yesnocancel", aDialogType)) { if ( lBuff[0]=='1' ) - { + { if ( !strcmp( lBuff+1 , "Yes" )) strcpy(lBuff,"1"); else if ( !strcmp( lBuff+1 , "No" )) strcpy(lBuff,"2"); } @@ -5000,9 +5178,9 @@ tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; /* return has only meaning for tinyfd_query */ int tinyfd_notifyPopup( - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may contain \n and \t */ - char const * const aIconType ) /* "info" "warning" "error" */ + char const * aTitle , /* NULL or "" */ + char const * aMessage , /* NULL or "" may contain \n and \t */ + char const * aIconType ) /* "info" "warning" "error" */ { char lBuff[MAX_PATH_OR_CMD]; char * lDialogString = NULL ; @@ -5011,6 +5189,9 @@ int tinyfd_notifyPopup( size_t lTitleLen ; size_t lMessageLen ; + if (tfd_quoteDetected(aTitle)) return tinyfd_notifyPopup("INVALID TITLE WITH QUOTES", aMessage, aIconType); + if (tfd_quoteDetected(aMessage)) return tinyfd_notifyPopup(aTitle, "INVALID MESSAGE WITH QUOTES", aIconType); + if ( getenv("SSH_TTY") ) { return tinyfd_messageBox(aTitle, aMessage, "ok", aIconType, 0); @@ -5041,11 +5222,11 @@ int tinyfd_notifyPopup( strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - + strcat( lDialogString, "' -e 'end try'") ; if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; } - else if ( kdialogPresent() ) + else if ( tfd_kdialogPresent() ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} strcpy( lDialogString , "kdialog" ) ; @@ -5071,36 +5252,21 @@ int tinyfd_notifyPopup( } strcat( lDialogString , " \" 5" ) ; } - else if ( (zenity3Present()>=5) || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) + else if ( (tfd_zenity3Present()>=5) ) { /* zenity 2.32 & 3.14 has the notification but with a bug: it doesnt return from it */ /* zenity 3.8 show the notification as an alert ok cancel box */ - if ( zenity3Present()>=5 ) + if ( tfd_zenity3Present()>=5 ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;} strcpy( lDialogString , "zenity" ) ; } - else if ( matedialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return 1;} - strcpy( lDialogString , "matedialog" ) ; - } - else if ( shellementaryPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return 1;} - strcpy( lDialogString , "shellementary" ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return 1;} - strcpy( lDialogString , "qarma" ) ; - } - strcat( lDialogString , " --notification"); + strcat( lDialogString , " --notification"); if ( aIconType && strlen( aIconType ) ) { - strcat( lDialogString , " --window-icon '"); + strcat( lDialogString , " --window-icon '"); strcat( lDialogString , aIconType ) ; strcat( lDialogString , "'" ) ; } @@ -5120,13 +5286,16 @@ int tinyfd_notifyPopup( else if ( perlPresent() >= 2 ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;} - sprintf( lDialogString , "perl -e \"use Net::DBus;\ - my \\$sessionBus = Net::DBus->session;\ - my \\$notificationsService = \\$sessionBus->get_service('org.freedesktop.Notifications');\ - my \\$notificationsObject = \\$notificationsService->get_object('/org/freedesktop/Notifications',\ - 'org.freedesktop.Notifications');\ - my \\$notificationId;\\$notificationId = \\$notificationsObject->Notify(shift, 0, '%s', '%s', '%s', [], {}, -1);\" ", - aIconType?aIconType:"", aTitle?aTitle:"", aMessage?aMessage:"" ) ; + + strcpy( lDialogString , "perl -e \"use Net::DBus;\ +my \\$sessionBus = Net::DBus->session;\ +my \\$notificationsService = \\$sessionBus->get_service('org.freedesktop.Notifications');\ +my \\$notificationsObject = \\$notificationsService->get_object('/org/freedesktop/Notifications',\ +'org.freedesktop.Notifications');"); + + sprintf( lDialogString + strlen(lDialogString) , +"my \\$notificationId;\\$notificationId = \\$notificationsObject->Notify(shift, 0, '%s', '%s', '%s', [], {}, -1);\" ", +aIconType?aIconType:"", aTitle?aTitle:"", aMessage?aMessage:"" ) ; } else if ( pythonDbusPresent( ) ) { @@ -5149,7 +5318,7 @@ int tinyfd_notifyPopup( if ( aMessage && strlen(aMessage) ) { lpDialogString = lDialogString + strlen(lDialogString); - replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; } strcat(lDialogString, "','','',5000)\"") ; } @@ -5171,9 +5340,9 @@ int tinyfd_notifyPopup( } if ( aMessage && strlen(aMessage) ) { - replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ; - replaceSubStr( aMessage , "\n" , " | " , lBuff ) ; - replaceSubStr( aMessage , "\t" , " " , lBuff ) ; + tfd_replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ; + tfd_replaceSubStr( aMessage , "\n" , " | " , lBuff ) ; + tfd_replaceSubStr( aMessage , "\t" , " " , lBuff ) ; strcat(lDialogString, lBuff) ; } strcat( lDialogString , "\"" ) ; @@ -5198,10 +5367,10 @@ int tinyfd_notifyPopup( /* returns NULL on cancel */ -char const * tinyfd_inputBox( - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may NOT contain \n nor \t */ - char const * const aDefaultInput ) /* "" , if NULL it's a passwordBox */ +char * tinyfd_inputBox( + char const * aTitle , /* NULL or "" */ + char const * aMessage , /* NULL or "" (\n and \t have no effect) */ + char const * aDefaultInput ) /* "" , if NULL it's a passwordBox */ { static char lBuff[MAX_PATH_OR_CMD]; char * lDialogString = NULL; @@ -5218,8 +5387,14 @@ char const * tinyfd_inputBox( size_t lTitleLen ; size_t lMessageLen ; + if (!aTitle && !aMessage && !aDefaultInput) return lBuff; /* now I can fill lBuff from outside */ + lBuff[0]='\0'; + if (tfd_quoteDetected(aTitle)) return tinyfd_inputBox("INVALID TITLE WITH QUOTES", aMessage, aDefaultInput); + if (tfd_quoteDetected(aMessage)) return tinyfd_inputBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDefaultInput); + if (tfd_quoteDetected(aDefaultInput)) return tinyfd_inputBox(aTitle, aMessage, "INVALID DEFAULT_INPUT WITH QUOTES"); + lTitleLen = aTitle ? strlen(aTitle) : 0 ; lMessageLen = aMessage ? strlen(aMessage) : 0 ; if ( !aTitle || strcmp(aTitle,"tinyfd_query") ) @@ -5229,7 +5404,7 @@ char const * tinyfd_inputBox( if ( osascriptPresent( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;} strcpy( lDialogString , "osascript "); if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); strcat( lDialogString , " -e 'try' -e 'display dialog \"") ; @@ -5261,12 +5436,12 @@ char const * tinyfd_inputBox( strcat(lDialogString, "-e 'end try'") ; if ( ! osx9orBetter() ) strcat(lDialogString, " -e 'end tell'") ; } - else if ( kdialogPresent() ) + else if ( tfd_kdialogPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} strcpy( lDialogString , "szAnswer=$(kdialog" ) ; - if ( kdialogPresent() == 2 ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -5300,50 +5475,46 @@ char const * tinyfd_inputBox( strcat( lDialogString , ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi"); } - else if ( zenityPresent() || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) + else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) { - if ( zenityPresent() ) + if ( tfd_zenityPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} strcpy( lDialogString , "szAnswer=$(zenity" ) ; - if ( (zenity3Present() >= 4) && !getenv("SSH_TTY") ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } } - else if ( matedialogPresent() ) + else if ( tfd_matedialogPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} strcpy( lDialogString , "szAnswer=$(matedialog" ) ; } - else if ( shellementaryPresent() ) + else if ( tfd_shellementaryPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;} strcpy( lDialogString , "szAnswer=$(shellementary" ) ; } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;} strcpy( lDialogString , "szAnswer=$(qarma" ) ; - if ( !getenv("SSH_TTY") ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } } strcat( lDialogString ," --entry" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title=\"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, " --text=\"") ; - strcat(lDialogString, aMessage) ; - strcat(lDialogString, "\"") ; - } + strcat(lDialogString, " --title=\"") ; + if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + + strcat(lDialogString, " --text=\"") ; + if (aMessage && strlen(aMessage)) strcat(lDialogString, aMessage) ; + strcat(lDialogString, "\"") ; + if ( aDefaultInput && strlen(aDefaultInput) ) { strcat(lDialogString, " --entry-text=\"") ; @@ -5358,15 +5529,45 @@ char const * tinyfd_inputBox( strcat( lDialogString , ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi"); } + else if (tfd_yadPresent()) + { + if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; } + strcpy(lDialogString, "szAnswer=$(yad --entry"); + if (aTitle && strlen(aTitle)) + { + strcat(lDialogString, " --title=\""); + strcat(lDialogString, aTitle); + strcat(lDialogString, "\""); + } + if (aMessage && strlen(aMessage)) + { + strcat(lDialogString, " --text=\""); + strcat(lDialogString, aMessage); + strcat(lDialogString, "\""); + } + if (aDefaultInput && strlen(aDefaultInput)) + { + strcat(lDialogString, " --entry-text=\""); + strcat(lDialogString, aDefaultInput); + strcat(lDialogString, "\""); + } + else + { + strcat(lDialogString, " --hide-text"); + } + if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null "); + strcat(lDialogString, + ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi"); + } else if ( gxmessagePresent() || gmessagePresent() ) { if ( gxmessagePresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return (char *)1;} strcpy( lDialogString , "szAnswer=$(gxmessage -buttons Ok:1,Cancel:0 -center \""); } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return (char *)1;} strcpy( lDialogString , "szAnswer=$(gmessage -buttons Ok:1,Cancel:0 -center \""); } @@ -5389,25 +5590,63 @@ char const * tinyfd_inputBox( strcat(lDialogString, "\"" ) ; strcat( lDialogString , ");echo $?$szAnswer"); } - else if ( !gdialogPresent() && !xdialogPresent() && tkinter2Present( ) ) + else if ( !gdialogPresent() && !xdialogPresent() && tkinter3Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter; from tkinter import simpledialog;root=tkinter.Tk();root.withdraw();"); + strcat( lDialogString ,"res=simpledialog.askstring(" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "',") ; + } + if ( aMessage && strlen(aMessage) ) + { + + strcat(lDialogString, "prompt='") ; + lpDialogString = lDialogString + strlen(lDialogString); + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + strcat(lDialogString, "',") ; + } + if ( aDefaultInput ) + { + if ( strlen(aDefaultInput) ) + { + strcat(lDialogString, "initialvalue='") ; + strcat(lDialogString, aDefaultInput) ; + strcat(lDialogString, "',") ; + } + } + else + { + strcat(lDialogString, "show='*'") ; + } + strcat(lDialogString, ");\nif res is None :\n\tprint(0)"); + strcat(lDialogString, "\nelse :\n\tprint('1'+res)\n\"" ) ; + } + else if ( !gdialogPresent() && !xdialogPresent() && tkinter2Present( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char const *)1;} - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( ) ) + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;} + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcat( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) { strcat( lDialogString , " -i" ) ; /* for osx without console */ } - - strcat( lDialogString , -" -S -c \"import Tkinter,tkSimpleDialog;root=Tkinter.Tk();root.withdraw();"); - - if ( isDarwin( ) ) + + strcat( lDialogString , + " -S -c \"import Tkinter,tkSimpleDialog;root=Tkinter.Tk();root.withdraw();"); + + if ( tfd_isDarwin( ) ) { strcat( lDialogString , "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \ frontmost of process \\\"Python\\\" to true' ''');"); } - + strcat( lDialogString ,"res=tkSimpleDialog.askstring(" ) ; if ( aTitle && strlen(aTitle) ) { @@ -5420,7 +5659,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); strcat(lDialogString, "prompt='") ; lpDialogString = lDialogString + strlen(lDialogString); - replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; strcat(lDialogString, "',") ; } if ( aDefaultInput ) @@ -5439,61 +5678,24 @@ frontmost of process \\\"Python\\\" to true' ''');"); strcat(lDialogString, ");\nif res is None :\n\tprint 0"); strcat(lDialogString, "\nelse :\n\tprint '1'+res\n\"" ) ; } - else if ( !gdialogPresent() && !xdialogPresent() && tkinter3Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char const *)1;} - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter; from tkinter import simpledialog;root=tkinter.Tk();root.withdraw();"); - strcat( lDialogString ,"res=simpledialog.askstring(" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; - } - if ( aMessage && strlen(aMessage) ) - { - - strcat(lDialogString, "prompt='") ; - lpDialogString = lDialogString + strlen(lDialogString); - replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; - strcat(lDialogString, "',") ; - } - if ( aDefaultInput ) - { - if ( strlen(aDefaultInput) ) - { - strcat(lDialogString, "initialvalue='") ; - strcat(lDialogString, aDefaultInput) ; - strcat(lDialogString, "',") ; - } - } - else - { - strcat(lDialogString, "show='*'") ; - } - strcat(lDialogString, ");\nif res is None :\n\tprint(0)"); - strcat(lDialogString, "\nelse :\n\tprint('1'+res)\n\"" ) ; - } else if ( gdialogPresent() || xdialogPresent() || dialogName() || whiptailPresent() ) { if ( gdialogPresent( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return (char *)1;} lWasGraphicDialog = 1 ; lWasGdialog = 1 ; strcpy( lDialogString , "(gdialog " ) ; } else if ( xdialogPresent( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;} lWasGraphicDialog = 1 ; strcpy( lDialogString , "(Xdialog " ) ; } else if ( dialogName( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} if ( isTerminalRunning( ) ) { strcpy( lDialogString , "(dialog " ) ; @@ -5509,12 +5711,12 @@ frontmost of process \\\"Python\\\" to true' ''');"); } else if ( isTerminalRunning( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char *)0;} strcpy( lDialogString , "(whiptail " ) ; } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char *)0;} lWasXterm = 1 ; strcpy( lDialogString , terminalName() ) ; strcat( lDialogString , "'(whiptail " ) ; @@ -5586,7 +5788,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); } else if ( ! isTerminalRunning( ) && terminalName() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;} lWasBasicXterm = 1 ; strcpy( lDialogString , terminalName() ) ; strcat( lDialogString , "'" ) ; @@ -5601,7 +5803,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); strcat( lDialogString, aTitle) ; strcat( lDialogString , "\";echo;" ) ; } - + strcat( lDialogString , "echo \"" ) ; if ( aMessage && strlen(aMessage) ) { @@ -5620,12 +5822,13 @@ frontmost of process \\\"Python\\\" to true' ''');"); else if ( !gWarningDisplayed && ! isTerminalRunning( ) && ! terminalName() ) { gWarningDisplayed = 1 ; tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"no_solution");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"no_solution");return (char *)0;} + free(lDialogString); return NULL; } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;} if ( !gWarningDisplayed && !tinyfd_forceConsole) { gWarningDisplayed = 1 ; @@ -5742,34 +5945,42 @@ frontmost of process \\\"Python\\\" to true' ''');"); free(lDialogString); return NULL ; } + /* printf( "lBuff+1: %s\n" , lBuff+1 ) ; */ free(lDialogString); - return lBuff+1 ; } -char const * tinyfd_saveFileDialog( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile , /* NULL or "" */ - int const aNumOfFilterPatterns , /* 0 */ - char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription ) /* NULL or "image files" */ +char * tinyfd_saveFileDialog( + char const * aTitle , /* NULL or "" */ + char const * aDefaultPathAndFile , /* NULL or "" */ + int aNumOfFilterPatterns , /* 0 */ + char const * const * aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ + char const * aSingleFilterDescription ) /* NULL or "image files" */ { - - static char lBuff [MAX_PATH_OR_CMD] ; - char lDialogString [MAX_PATH_OR_CMD] ; - char lString [MAX_PATH_OR_CMD] ; + static char lBuff[MAX_PATH_OR_CMD] ; + char lDialogString[MAX_PATH_OR_CMD] ; + char lString[MAX_PATH_OR_CMD] ; int i ; int lWasGraphicDialog = 0 ; int lWasXterm = 0 ; - char const * p ; + char * p ; + char * lPointerInputBox ; FILE * lIn ; lBuff[0]='\0'; + if (tfd_quoteDetected(aTitle)) return tinyfd_saveFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_saveFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_saveFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES"); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_saveFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL); + } + if ( osascriptPresent( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;} strcpy( lDialogString , "osascript "); if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"Finder\"' -e 'Activate'"); strcat( lDialogString , " -e 'try' -e 'POSIX path of ( choose file name " ); @@ -5798,12 +6009,12 @@ char const * tinyfd_saveFileDialog( strcat(lDialogString, "-e 'end try'") ; if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; } - else if ( kdialogPresent() ) + else if ( tfd_kdialogPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} strcpy( lDialogString , "kdialog" ) ; - if ( kdialogPresent() == 2 ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -5827,10 +6038,11 @@ char const * tinyfd_saveFileDialog( if ( aNumOfFilterPatterns > 0 ) { strcat(lDialogString , " \"" ) ; - for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) + strcat( lDialogString , aFilterPatterns[0] ) ; + for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat( lDialogString , aFilterPatterns [i] ) ; - strcat( lDialogString , " " ) ; + strcat( lDialogString , " " ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; } if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) { @@ -5846,86 +6058,170 @@ char const * tinyfd_saveFileDialog( strcat(lDialogString, "\"") ; } } - else if ( zenityPresent() || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) + else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) { - if ( zenityPresent() ) + if ( tfd_zenityPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} strcpy( lDialogString , "zenity" ) ; - if ( (zenity3Present() >= 4) && !getenv("SSH_TTY") ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } } - else if ( matedialogPresent() ) + else if ( tfd_matedialogPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} strcpy( lDialogString , "matedialog" ) ; } - else if ( shellementaryPresent() ) + else if ( tfd_shellementaryPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;} strcpy( lDialogString , "shellementary" ) ; } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;} strcpy( lDialogString , "qarma" ) ; - if ( !getenv("SSH_TTY") ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } } strcat(lDialogString, " --file-selection --save --confirm-overwrite" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title=\"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + strcat(lDialogString, " --title=\"") ; + if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) { strcat(lDialogString, " --filename=\"") ; strcat(lDialogString, aDefaultPathAndFile) ; strcat(lDialogString, "\"") ; - } + } if ( aNumOfFilterPatterns > 0 ) { strcat( lDialogString , " --file-filter='" ) ; if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) { strcat( lDialogString , aSingleFilterDescription ) ; - strcat( lDialogString , " | " ) ; + strcat( lDialogString , " |" ) ; } for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat( lDialogString , aFilterPatterns [i] ) ; - strcat( lDialogString , " " ) ; + strcat( lDialogString , " " ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; } strcat( lDialogString , "' --file-filter='All files | *'" ) ; } if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null "); } - else if ( !xdialogPresent() && tkinter2Present( ) ) + else if (tfd_yadPresent()) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char const *)1;} - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( )) + if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; } + strcpy(lDialogString, "yad --file-selection --save --confirm-overwrite"); + if (aTitle && strlen(aTitle)) + { + strcat(lDialogString, " --title=\""); + strcat(lDialogString, aTitle); + strcat(lDialogString, "\""); + } + if (aDefaultPathAndFile && strlen(aDefaultPathAndFile)) + { + strcat(lDialogString, " --filename=\""); + strcat(lDialogString, aDefaultPathAndFile); + strcat(lDialogString, "\""); + } + if (aNumOfFilterPatterns > 0) + { + strcat(lDialogString, " --file-filter='"); + if (aSingleFilterDescription && strlen(aSingleFilterDescription)) + { + strcat(lDialogString, aSingleFilterDescription); + strcat(lDialogString, " |"); + } + for (i = 0; i < aNumOfFilterPatterns; i++) + { + strcat(lDialogString, " "); + strcat(lDialogString, aFilterPatterns[i]); + } + strcat(lDialogString, "' --file-filter='All files | *'"); + } + if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null "); + } + else if ( !xdialogPresent() && tkinter3Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); + strcat( lDialogString , "res=filedialog.asksaveasfilename("); + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "',") ; + } + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "initialdir='") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "'," ) ; + } + getLastName( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "initialfile='") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "'," ) ; + } + } + if ( ( aNumOfFilterPatterns > 1 ) + || ( (aNumOfFilterPatterns == 1) /* test because poor osx behaviour */ + && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) ) + { + strcat(lDialogString , "filetypes=(" ) ; + strcat( lDialogString , "('" ) ; + if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) + { + strcat( lDialogString , aSingleFilterDescription ) ; + } + strcat( lDialogString , "',(" ) ; + for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat( lDialogString , "'" ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; + strcat( lDialogString , "'," ) ; + } + strcat( lDialogString , "))," ) ; + strcat( lDialogString , "('All files','*'))" ) ; + } + strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint(res)\n\"" ) ; + } + else if ( !xdialogPresent() && tkinter2Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;} + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcat( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && tfd_isDarwin( )) { strcat( lDialogString , " -i" ) ; /* for osx without console */ } strcat( lDialogString , " -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();"); - if ( isDarwin( ) ) + if ( tfd_isDarwin( ) ) { strcat( lDialogString , "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set\ frontmost of process \\\"Python\\\" to true' ''');"); } - strcat( lDialogString , "print tkFileDialog.asksaveasfilename("); + strcat( lDialogString , "res=tkFileDialog.asksaveasfilename("); if ( aTitle && strlen(aTitle) ) { strcat(lDialogString, "title='") ; @@ -5963,82 +6259,30 @@ char const * tinyfd_saveFileDialog( for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) { strcat( lDialogString , "'" ) ; - strcat( lDialogString , aFilterPatterns [i] ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; strcat( lDialogString , "'," ) ; } strcat( lDialogString , "))," ) ; strcat( lDialogString , "('All files','*'))" ) ; } - strcat( lDialogString , ")\"" ) ; - } - else if ( !xdialogPresent() && tkinter3Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char const *)1;} - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); - strcat( lDialogString , "print( filedialog.asksaveasfilename("); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; - } - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "initialdir='") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "'," ) ; - } - getLastName( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "initialfile='") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "'," ) ; - } - } - if ( ( aNumOfFilterPatterns > 1 ) - || ( (aNumOfFilterPatterns == 1) /* test because poor osx behaviour */ - && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) ) - { - strcat(lDialogString , "filetypes=(" ) ; - strcat( lDialogString , "('" ) ; - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) - { - strcat( lDialogString , aSingleFilterDescription ) ; - } - strcat( lDialogString , "',(" ) ; - for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat( lDialogString , "'" ) ; - strcat( lDialogString , aFilterPatterns [i] ) ; - strcat( lDialogString , "'," ) ; - } - strcat( lDialogString , "))," ) ; - strcat( lDialogString , "('All files','*'))" ) ; - } - strcat( lDialogString , "))\"" ) ; - } + strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint res \n\"" ) ; + } else if ( xdialogPresent() || dialogName() ) { if ( xdialogPresent( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;} lWasGraphicDialog = 1 ; strcpy( lDialogString , "(Xdialog " ) ; } else if ( isTerminalRunning( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} strcpy( lDialogString , "(dialog " ) ; } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} lWasXterm = 1 ; strcpy( lDialogString , terminalName() ) ; strcat( lDialogString , "'(" ) ; @@ -6101,8 +6345,16 @@ char const * tinyfd_saveFileDialog( else { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - p = tinyfd_inputBox( aTitle , "Save file" , "" ) ; - getPathWithoutFinalSlash( lString , p ) ; + strcpy(lBuff, "Save file in "); + strcat(lBuff, getCurDir()); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */ + p = lBuff; + + getPathWithoutFinalSlash( lString , p ) ; if ( strlen( lString ) && ! dirExists( lString ) ) { return NULL ; @@ -6145,31 +6397,65 @@ char const * tinyfd_saveFileDialog( return lBuff ; } - + /* in case of multiple files, the separator is | */ -char const * tinyfd_openFileDialog( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile , /* NULL or "" */ - int const aNumOfFilterPatterns , /* 0 */ - char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription , /* NULL or "image files" */ - int const aAllowMultipleSelects ) /* 0 or 1 */ +char * tinyfd_openFileDialog( + char const * aTitle , /* NULL or "" */ + char const * aDefaultPathAndFile , /* NULL or "" */ + int aNumOfFilterPatterns , /* 0 */ + char const * const * aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ + char const * aSingleFilterDescription , /* NULL or "image files" */ + int aAllowMultipleSelects ) /* 0 or 1 */ { - static char lBuff [MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD] ; - char lDialogString [MAX_PATH_OR_CMD] ; - char lString [MAX_PATH_OR_CMD] ; - int i ; - FILE * lIn ; - char * p ; - char const * p2 ; - int lWasKdialog = 0 ; - int lWasGraphicDialog = 0 ; - int lWasXterm = 0 ; - lBuff[0]='\0'; + char lDialogString[MAX_PATH_OR_CMD] ; + char lString[MAX_PATH_OR_CMD] ; + int i ; + FILE * lIn ; + char * p ; + char * lPointerInputBox ; + int lWasKdialog = 0 ; + int lWasGraphicDialog = 0 ; + int lWasXterm = 0 ; + size_t lFullBuffLen ; + static char * lBuff = NULL; + + if (tfd_quoteDetected(aTitle)) return tinyfd_openFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_openFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects); + if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_openFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_openFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects); + } + + free(lBuff); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")) + { + lBuff = NULL; + } + else + { + if (aAllowMultipleSelects) + { + lFullBuffLen = MAX_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; + lBuff = (char *)(malloc(lFullBuffLen * sizeof(char))); + if (!lBuff) + { + lFullBuffLen = LOW_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1; + lBuff = (char *)( malloc( lFullBuffLen * sizeof(char))); + } + } + else + { + lFullBuffLen = MAX_PATH_OR_CMD + 1; + lBuff = (char *)(malloc(lFullBuffLen * sizeof(char))); + } + if (!lBuff) return NULL; + lBuff[0]='\0'; + } if ( osascriptPresent( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;} strcpy( lDialogString , "osascript "); if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); strcat( lDialogString , " -e 'try' -e '" ); @@ -6200,12 +6486,12 @@ char const * tinyfd_openFileDialog( if ( aNumOfFilterPatterns > 0 ) { strcat(lDialogString , "of type {\"" ); - strcat( lDialogString , aFilterPatterns [0] + 2 ) ; + strcat( lDialogString , aFilterPatterns[0] + 2 ) ; strcat( lDialogString , "\"" ) ; for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) { strcat( lDialogString , ",\"" ) ; - strcat( lDialogString , aFilterPatterns [i] + 2) ; + strcat( lDialogString , aFilterPatterns[i] + 2) ; strcat( lDialogString , "\"" ) ; } strcat( lDialogString , "} " ) ; @@ -6231,13 +6517,13 @@ char const * tinyfd_openFileDialog( strcat(lDialogString, "-e 'end try'") ; if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; } - else if ( kdialogPresent() ) + else if ( tfd_kdialogPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} lWasKdialog = 1 ; strcpy( lDialogString , "kdialog" ) ; - if ( kdialogPresent() == 2 ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -6261,10 +6547,11 @@ char const * tinyfd_openFileDialog( if ( aNumOfFilterPatterns > 0 ) { strcat(lDialogString , " \"" ) ; - for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) + strcat( lDialogString , aFilterPatterns[0] ) ; + for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat( lDialogString , aFilterPatterns [i] ) ; - strcat( lDialogString , " " ) ; + strcat( lDialogString , " " ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; } if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) { @@ -6284,32 +6571,32 @@ char const * tinyfd_openFileDialog( strcat(lDialogString, "\"") ; } } - else if ( zenityPresent() || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) + else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) { - if ( zenityPresent() ) + if ( tfd_zenityPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} strcpy( lDialogString , "zenity" ) ; - if ( (zenity3Present() >= 4) && !getenv("SSH_TTY") ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } } - else if ( matedialogPresent() ) + else if ( tfd_matedialogPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} strcpy( lDialogString , "matedialog" ) ; } - else if ( shellementaryPresent() ) + else if ( tfd_shellementaryPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;} strcpy( lDialogString , "shellementary" ) ; } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;} strcpy( lDialogString , "qarma" ) ; - if ( !getenv("SSH_TTY") ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -6320,12 +6607,11 @@ char const * tinyfd_openFileDialog( { strcat( lDialogString , " --multiple" ) ; } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title=\"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } + + strcat(lDialogString, " --title=\"") ; + if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) { strcat(lDialogString, " --filename=\"") ; @@ -6334,33 +6620,130 @@ char const * tinyfd_openFileDialog( } if ( aNumOfFilterPatterns > 0 ) { - strcat( lDialogString , " --file-filter='" ) ; + strcat( lDialogString , " --file-filter='" ) ; if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) { strcat( lDialogString , aSingleFilterDescription ) ; - strcat( lDialogString , " | " ) ; + strcat( lDialogString , " |" ) ; } for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat( lDialogString , aFilterPatterns [i] ) ; - strcat( lDialogString , " " ) ; + strcat( lDialogString , " " ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; } strcat( lDialogString , "' --file-filter='All files | *'" ) ; } if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null "); } - else if ( tkinter2Present( ) ) + else if (tfd_yadPresent()) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char const *)1;} - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( ) ) + if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; } + strcpy(lDialogString, "yad --file-selection"); + if (aAllowMultipleSelects) + { + strcat(lDialogString, " --multiple"); + } + if (aTitle && strlen(aTitle)) + { + strcat(lDialogString, " --title=\""); + strcat(lDialogString, aTitle); + strcat(lDialogString, "\""); + } + if (aDefaultPathAndFile && strlen(aDefaultPathAndFile)) + { + strcat(lDialogString, " --filename=\""); + strcat(lDialogString, aDefaultPathAndFile); + strcat(lDialogString, "\""); + } + if (aNumOfFilterPatterns > 0) + { + strcat(lDialogString, " --file-filter='"); + if (aSingleFilterDescription && strlen(aSingleFilterDescription)) + { + strcat(lDialogString, aSingleFilterDescription); + strcat(lDialogString, " |"); + } + for (i = 0; i < aNumOfFilterPatterns; i++) + { + strcat(lDialogString, " "); + strcat(lDialogString, aFilterPatterns[i]); + } + strcat(lDialogString, "' --file-filter='All files | *'"); + } + if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null "); + } + else if ( tkinter3Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); + strcat( lDialogString , "lFiles=filedialog.askopenfilename("); + if ( aAllowMultipleSelects ) + { + strcat( lDialogString , "multiple=1," ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "',") ; + } + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "initialdir='") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "'," ) ; + } + getLastName( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "initialfile='") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "'," ) ; + } + } + if ( ( aNumOfFilterPatterns > 1 ) + || ( ( aNumOfFilterPatterns == 1 ) /*test because poor osx behaviour*/ + && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) ) + { + strcat(lDialogString , "filetypes=(" ) ; + strcat( lDialogString , "('" ) ; + if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) + { + strcat( lDialogString , aSingleFilterDescription ) ; + } + strcat( lDialogString , "',(" ) ; + for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat( lDialogString , "'" ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; + strcat( lDialogString , "'," ) ; + } + strcat( lDialogString , "))," ) ; + strcat( lDialogString , "('All files','*'))" ) ; + } + strcat( lDialogString , ");\ +\nif not isinstance(lFiles, tuple):\n\tprint(lFiles)\nelse:\ +\n\tlFilesString=''\n\tfor lFile in lFiles:\n\t\tlFilesString+=str(lFile)+'|'\ +\n\tprint(lFilesString[:-1])\n\"" ) ; + } + else if ( tkinter2Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;} + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcat( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) { strcat( lDialogString , " -i" ) ; /* for osx without console */ } strcat( lDialogString , " -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();"); - if ( isDarwin( ) ) + if ( tfd_isDarwin( ) ) { strcat( lDialogString , "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \ @@ -6408,7 +6791,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) { strcat( lDialogString , "'" ) ; - strcat( lDialogString , aFilterPatterns [i] ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; strcat( lDialogString , "'," ) ; } strcat( lDialogString , "))," ) ; @@ -6418,82 +6801,23 @@ frontmost of process \\\"Python\\\" to true' ''');"); \nif not isinstance(lFiles, tuple):\n\tprint lFiles\nelse:\ \n\tlFilesString=''\n\tfor lFile in lFiles:\n\t\tlFilesString+=str(lFile)+'|'\ \n\tprint lFilesString[:-1]\n\"" ) ; - } - else if ( tkinter3Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char const *)1;} - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); - strcat( lDialogString , "lFiles=filedialog.askopenfilename("); - if ( aAllowMultipleSelects ) - { - strcat( lDialogString , "multiple=1," ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; - } - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "initialdir='") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "'," ) ; - } - getLastName( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "initialfile='") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "'," ) ; - } - } - if ( ( aNumOfFilterPatterns > 1 ) - || ( ( aNumOfFilterPatterns == 1 ) /*test because poor osx behaviour*/ - && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) ) - { - strcat(lDialogString , "filetypes=(" ) ; - strcat( lDialogString , "('" ) ; - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) - { - strcat( lDialogString , aSingleFilterDescription ) ; - } - strcat( lDialogString , "',(" ) ; - for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat( lDialogString , "'" ) ; - strcat( lDialogString , aFilterPatterns [i] ) ; - strcat( lDialogString , "'," ) ; - } - strcat( lDialogString , "))," ) ; - strcat( lDialogString , "('All files','*'))" ) ; - } - strcat( lDialogString , ");\ -\nif not isinstance(lFiles, tuple):\n\tprint(lFiles)\nelse:\ -\n\tlFilesString=''\n\tfor lFile in lFiles:\n\t\tlFilesString+=str(lFile)+'|'\ -\n\tprint(lFilesString[:-1])\n\"" ) ; } else if ( xdialogPresent() || dialogName() ) { if ( xdialogPresent( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;} lWasGraphicDialog = 1 ; strcpy( lDialogString , "(Xdialog " ) ; } else if ( isTerminalRunning( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} strcpy( lDialogString , "(dialog " ) ; } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} lWasXterm = 1 ; strcpy( lDialogString , terminalName() ) ; strcat( lDialogString , "'(" ) ; @@ -6555,22 +6879,35 @@ frontmost of process \\\"Python\\\" to true' ''');"); } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - p2 = tinyfd_inputBox(aTitle, "Open file",""); - if ( ! fileExists(p2) ) - { - return NULL ; - } - return p2 ; + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} + strcpy(lBuff, "Open file from "); + strcat(lBuff, getCurDir()); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if ( p ) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */ + if ( ! fileExists(lBuff) ) + { + free(lBuff); + lBuff = NULL; + } + else + { + lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char))); + } + return lBuff ; } if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; if ( ! ( lIn = popen( lDialogString , "r" ) ) ) { - return NULL ; + free(lBuff); + lBuff = NULL; + return NULL ; } lBuff[0]='\0'; - p=lBuff; + p = lBuff; while ( fgets( p , sizeof( lBuff ) , lIn ) != NULL ) { p += strlen( p ); @@ -6590,41 +6927,52 @@ frontmost of process \\\"Python\\\" to true' ''');"); /* printf( "lBuff2: %s\n" , lBuff ) ; */ if ( ! strlen( lBuff ) ) { - return NULL; + free(lBuff); + lBuff = NULL; + return NULL; } if ( aAllowMultipleSelects && strchr(lBuff, '|') ) { - p2 = ensureFilesExist( lBuff , lBuff ) ; + if( ! ensureFilesExist( lBuff , lBuff ) ) + { + free(lBuff); + lBuff = NULL; + return NULL; + } } - else if ( fileExists(lBuff) ) + else if ( !fileExists(lBuff) ) { - p2 = lBuff ; - } - else - { - return NULL ; - } - /* printf( "lBuff3: %s\n" , p2 ) ; */ + free(lBuff); + lBuff = NULL; + return NULL; + } - return p2 ; + lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char))); + + /*printf( "lBuff3: %s\n" , lBuff ) ; */ + return lBuff ; } -char const * tinyfd_selectFolderDialog( - char const * const aTitle , /* "" */ - char const * const aDefaultPath ) /* "" */ +char * tinyfd_selectFolderDialog( + char const * aTitle , /* "" */ + char const * aDefaultPath ) /* "" */ { - static char lBuff [MAX_PATH_OR_CMD] ; - char lDialogString [MAX_PATH_OR_CMD] ; + static char lBuff[MAX_PATH_OR_CMD] ; + char lDialogString[MAX_PATH_OR_CMD] ; FILE * lIn ; - char const * p ; + char * p ; + char * lPointerInputBox ; int lWasGraphicDialog = 0 ; int lWasXterm = 0 ; lBuff[0]='\0'; + if (tfd_quoteDetected(aTitle)) return tinyfd_selectFolderDialog("INVALID TITLE WITH QUOTES", aDefaultPath); + if (tfd_quoteDetected(aDefaultPath)) return tinyfd_selectFolderDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES"); + if ( osascriptPresent( )) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;} strcpy( lDialogString , "osascript "); if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); strcat( lDialogString , " -e 'try' -e 'POSIX path of ( choose folder "); @@ -6645,11 +6993,11 @@ char const * tinyfd_selectFolderDialog( strcat(lDialogString, "-e 'end try'") ; if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; } - else if ( kdialogPresent() ) + else if ( tfd_kdialogPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} strcpy( lDialogString , "kdialog" ) ; - if ( kdialogPresent() == 2 ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } @@ -6677,45 +7025,43 @@ char const * tinyfd_selectFolderDialog( strcat(lDialogString, "\"") ; } } - else if ( zenityPresent() || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) + else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) { - if ( zenityPresent() ) + if ( tfd_zenityPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} strcpy( lDialogString , "zenity" ) ; - if ( (zenity3Present() >= 4) && !getenv("SSH_TTY") ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } } - else if ( matedialogPresent() ) + else if ( tfd_matedialogPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} strcpy( lDialogString , "matedialog" ) ; } - else if ( shellementaryPresent() ) + else if ( tfd_shellementaryPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;} strcpy( lDialogString , "shellementary" ) ; } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;} strcpy( lDialogString , "qarma" ) ; - if ( !getenv("SSH_TTY") ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } } strcat( lDialogString , " --file-selection --directory" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title=\"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - if ( aDefaultPath && strlen(aDefaultPath) ) + strcat(lDialogString, " --title=\"") ; + if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + + if ( aDefaultPath && strlen(aDefaultPath) ) { strcat(lDialogString, " --filename=\"") ; strcat(lDialogString, aDefaultPath) ; @@ -6723,18 +7069,58 @@ char const * tinyfd_selectFolderDialog( } if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null "); } - else if ( !xdialogPresent() && tkinter2Present( ) ) + else if (tfd_yadPresent()) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char const *)1;} - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( ) ) + if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; } + strcpy(lDialogString, "yad --file-selection --directory"); + if (aTitle && strlen(aTitle)) + { + strcat(lDialogString, " --title=\""); + strcat(lDialogString, aTitle); + strcat(lDialogString, "\""); + } + if (aDefaultPath && strlen(aDefaultPath)) + { + strcat(lDialogString, " --filename=\""); + strcat(lDialogString, aDefaultPath); + strcat(lDialogString, "\""); + } + if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null "); + } + else if ( !xdialogPresent() && tkinter3Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); + strcat( lDialogString , "res=filedialog.askdirectory("); + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "',") ; + } + if ( aDefaultPath && strlen(aDefaultPath) ) + { + strcat(lDialogString, "initialdir='") ; + strcat(lDialogString, aDefaultPath ) ; + strcat(lDialogString , "'" ) ; + } + strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint(res)\n\"" ) ; + } + else if ( !xdialogPresent() && tkinter2Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;} + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcat( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) { strcat( lDialogString , " -i" ) ; /* for osx without console */ } strcat( lDialogString , " -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();"); - if ( isDarwin( ) ) + if ( tfd_isDarwin( ) ) { strcat( lDialogString , "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \ @@ -6756,43 +7142,22 @@ frontmost of process \\\"Python\\\" to true' ''');"); } strcat( lDialogString , ")\"" ) ; } - else if ( !xdialogPresent() && tkinter3Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char const *)1;} - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();"); - strcat( lDialogString , "print( filedialog.askdirectory("); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; - } - if ( aDefaultPath && strlen(aDefaultPath) ) - { - strcat(lDialogString, "initialdir='") ; - strcat(lDialogString, aDefaultPath ) ; - strcat(lDialogString , "'" ) ; - } - strcat( lDialogString , ") )\"" ) ; - } else if ( xdialogPresent() || dialogName() ) { if ( xdialogPresent( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;} lWasGraphicDialog = 1 ; strcpy( lDialogString , "(Xdialog " ) ; } else if ( isTerminalRunning( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} strcpy( lDialogString , "(dialog " ) ; } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} lWasXterm = 1 ; strcpy( lDialogString , terminalName() ) ; strcat( lDialogString , "'(" ) ; @@ -6830,7 +7195,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); { strcat(lDialogString, "./") ; } - + if ( lWasGraphicDialog ) { strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; @@ -6852,7 +7217,15 @@ frontmost of process \\\"Python\\\" to true' ''');"); else { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - p = tinyfd_inputBox(aTitle, "Select folder",""); + strcpy(lBuff, "Select folder from "); + strcat(lBuff, getCurDir()); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, lBuff, ""); + if (p) strcpy(lBuff, p); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */ + p = lBuff; + if ( !p || ! strlen( p ) || ! dirExists( p ) ) { return NULL ; @@ -6884,19 +7257,23 @@ frontmost of process \\\"Python\\\" to true' ''');"); /* aoResultRGB also contains the result */ /* aDefaultRGB is used only if aDefaultHexRGB is NULL */ /* aDefaultRGB and aoResultRGB can be the same array */ -char const * tinyfd_colorChooser( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultHexRGB , /* NULL or "#FF0000"*/ +char * tinyfd_colorChooser( + char const * aTitle , /* NULL or "" */ + char const * aDefaultHexRGB , /* NULL or "#FF0000"*/ unsigned char const aDefaultRGB[3] , /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3] ) /* { 0 , 0 , 0 } */ { - static char lBuff [128] ; - char lTmp [128] ; - char lDialogString [MAX_PATH_OR_CMD] ; - char lDefaultHexRGB[8]; - char * lpDefaultHexRGB; + static char lDefaultHexRGB[16]; + char lBuff[128] ; + + char lTmp[128] ; +#if !((defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)) + char * lTmp2 ; +#endif + char lDialogString[MAX_PATH_OR_CMD] ; unsigned char lDefaultRGB[3]; - char const * p; + char * p; + char * lPointerInputBox; FILE * lIn ; int i ; int lWasZenity3 = 0 ; @@ -6904,32 +7281,34 @@ char const * tinyfd_colorChooser( int lWasXdialog = 0 ; lBuff[0]='\0'; - if ( aDefaultHexRGB ) - { - Hex2RGB( aDefaultHexRGB , lDefaultRGB ) ; - lpDefaultHexRGB = (char *) aDefaultHexRGB ; - } - else - { - lDefaultRGB[0]=aDefaultRGB[0]; - lDefaultRGB[1]=aDefaultRGB[1]; - lDefaultRGB[2]=aDefaultRGB[2]; - RGB2Hex( aDefaultRGB , lDefaultHexRGB ) ; - lpDefaultHexRGB = (char *) lDefaultHexRGB ; - } + if (tfd_quoteDetected(aTitle)) return tinyfd_colorChooser("INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB); + if (tfd_quoteDetected(aDefaultHexRGB)) return tinyfd_colorChooser(aTitle, "INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB); + + if (aDefaultHexRGB) + { + Hex2RGB(aDefaultHexRGB, lDefaultRGB); + strcpy(lDefaultHexRGB, aDefaultHexRGB); + } + else + { + lDefaultRGB[0] = aDefaultRGB[0]; + lDefaultRGB[1] = aDefaultRGB[1]; + lDefaultRGB[2] = aDefaultRGB[2]; + RGB2Hex(aDefaultRGB, lDefaultHexRGB); + } if ( osascriptPresent( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;} lWasOsascript = 1 ; strcpy( lDialogString , "osascript"); - - if ( ! osx9orBetter() ) + + if ( ! osx9orBetter() ) { strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); strcat( lDialogString , " -e 'try' -e 'set mycolor to choose color default color {"); } - else + else { strcat( lDialogString , " -e 'try' -e 'tell app (path to frontmost application as Unicode text) \ @@ -6957,15 +7336,15 @@ to set mycolor to choose color default color {"); strcat(lDialogString, "-e 'end try'") ; if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; } - else if ( kdialogPresent() ) + else if ( tfd_kdialogPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} strcpy( lDialogString , "kdialog" ) ; - if ( kdialogPresent() == 2 ) + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } - sprintf( lDialogString + strlen(lDialogString) , " --getcolor --default '%s'" , lpDefaultHexRGB ) ; + sprintf( lDialogString + strlen(lDialogString) , " --getcolor --default '%s'" , lDefaultHexRGB ) ; if ( aTitle && strlen(aTitle) ) { @@ -6974,51 +7353,62 @@ to set mycolor to choose color default color {"); strcat(lDialogString, "\"") ; } } - else if ( zenity3Present() || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) + else if ( tfd_zenity3Present() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) { lWasZenity3 = 1 ; - if ( zenity3Present() ) + if ( tfd_zenity3Present() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity3");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity3");return (char *)1;} strcpy( lDialogString , "zenity" ); - if ( (zenity3Present() >= 4) && !getenv("SSH_TTY") ) + if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } } - else if ( matedialogPresent() ) + else if ( tfd_matedialogPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} strcpy( lDialogString , "matedialog" ) ; } - else if ( shellementaryPresent() ) + else if ( tfd_shellementaryPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;} strcpy( lDialogString , "shellementary" ) ; } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;} strcpy( lDialogString , "qarma" ) ; - if ( !getenv("SSH_TTY") ) + if ( !getenv("SSH_TTY") && tfd_xpropPresent() ) { strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ } } strcat( lDialogString , " --color-selection --show-palette" ) ; - sprintf( lDialogString + strlen(lDialogString), " --color=%s" , lpDefaultHexRGB ) ; + sprintf( lDialogString + strlen(lDialogString), " --color=%s" , lDefaultHexRGB ) ; + + strcat(lDialogString, " --title=\"") ; + if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title=\"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null "); } + else if (tfd_yadPresent()) + { + if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; } + strcpy(lDialogString, "yad --color"); + sprintf(lDialogString + strlen(lDialogString), " --init-color=%s", lDefaultHexRGB); + if (aTitle && strlen(aTitle)) + { + strcat(lDialogString, " --title=\""); + strcat(lDialogString, aTitle); + strcat(lDialogString, "\""); + } + if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null "); + } else if ( xdialogPresent() ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;} lWasXdialog = 1 ; strcpy( lDialogString , "Xdialog --colorsel \"" ) ; if ( aTitle && strlen(aTitle) ) @@ -7026,24 +7416,47 @@ to set mycolor to choose color default color {"); strcat(lDialogString, aTitle) ; } strcat(lDialogString, "\" 0 60 ") ; - sprintf(lTmp,"%hhu %hhu %hhu",lDefaultRGB[0], - lDefaultRGB[1],lDefaultRGB[2]); +#if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__) + sprintf(lTmp,"%hhu %hhu %hhu",lDefaultRGB[0],lDefaultRGB[1],lDefaultRGB[2]); +#else + sprintf(lTmp,"%hu %hu %hu",lDefaultRGB[0],lDefaultRGB[1],lDefaultRGB[2]); +#endif strcat(lDialogString, lTmp) ; strcat(lDialogString, " 2>&1"); } - else if ( tkinter2Present( ) ) + else if ( tkinter3Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;} + strcpy( lDialogString , gPython3Name ) ; + strcat( lDialogString , + " -S -c \"import tkinter;from tkinter import colorchooser;root=tkinter.Tk();root.withdraw();"); + strcat( lDialogString , "res=colorchooser.askcolor(color='" ) ; + strcat(lDialogString, lDefaultHexRGB ) ; + strcat(lDialogString, "'") ; + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, ",title='") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "'") ; + } + strcat( lDialogString , ");\ +\nif res[1] is not None:\n\tprint(res[1])\"" ) ; + } + else if ( tkinter2Present( ) ) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char const *)1;} - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( ) ) + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;} + strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ; + strcat( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && tfd_isDarwin( ) ) { strcat( lDialogString , " -i" ) ; /* for osx without console */ } - + strcat( lDialogString , " -S -c \"import Tkinter,tkColorChooser;root=Tkinter.Tk();root.withdraw();"); - if ( isDarwin( ) ) + if ( tfd_isDarwin( ) ) { strcat( lDialogString , "import os;os.system('''osascript -e 'tell app \\\"Finder\\\" to set \ @@ -7051,7 +7464,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); } strcat( lDialogString , "res=tkColorChooser.askcolor(color='" ) ; - strcat(lDialogString, lpDefaultHexRGB ) ; + strcat(lDialogString, lDefaultHexRGB ) ; strcat(lDialogString, "'") ; @@ -7063,44 +7476,29 @@ frontmost of process \\\"Python\\\" to true' ''');"); } strcat( lDialogString , ");\ \nif res[1] is not None:\n\tprint res[1]\"" ) ; - } - else if ( tkinter3Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char const *)1;} - strcpy( lDialogString , gPython3Name ) ; - strcat( lDialogString , - " -S -c \"import tkinter;from tkinter import colorchooser;root=tkinter.Tk();root.withdraw();"); - strcat( lDialogString , "res=colorchooser.askcolor(color='" ) ; - strcat(lDialogString, lpDefaultHexRGB ) ; - strcat(lDialogString, "'") ; - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, ",title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "'") ; - } - strcat( lDialogString , ");\ -\nif res[1] is not None:\n\tprint(res[1])\"" ) ; } else { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - p = tinyfd_inputBox(aTitle, - "Enter hex rgb color (i.e. #f5ca20)",lpDefaultHexRGB); + lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */ + if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */ + p = tinyfd_inputBox(aTitle, "Enter hex rgb color (i.e. #f5ca20)", lDefaultHexRGB); + if ( !p || (strlen(p) != 7) || (p[0] != '#') ) { return NULL ; } for ( i = 1 ; i < 7 ; i ++ ) { - if ( ! isxdigit( p[i] ) ) + if ( ! isxdigit( (int) p[i] ) ) { return NULL ; } } - Hex2RGB(p,aoResultRGB); - return p ; + Hex2RGB(p,aoResultRGB); + strcpy(lDefaultHexRGB, p); + if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */ + return lDefaultHexRGB; } if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; @@ -7122,7 +7520,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); { lBuff[strlen( lBuff ) -1] = '\0' ; } - + if ( lWasZenity3 ) { if ( lBuff[0] == '#' ) @@ -7138,135 +7536,49 @@ frontmost of process \\\"Python\\\" to true' ''');"); Hex2RGB(lBuff,aoResultRGB); } else if ( lBuff[3] == '(' ) { - sscanf(lBuff,"rgb(%hhu,%hhu,%hhu", - & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]); - RGB2Hex(aoResultRGB,lBuff); +#if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__) + sscanf(lBuff,"rgb(%hhu,%hhu,%hhu", & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]); +#else + aoResultRGB[0] = strtol(lBuff+4, & lTmp2, 10 ); + aoResultRGB[1] = strtol(lTmp2+1, & lTmp2, 10 ); + aoResultRGB[2] = strtol(lTmp2+1, NULL, 10 ); +#endif + RGB2Hex(aoResultRGB,lBuff); } else if ( lBuff[4] == '(' ) { - sscanf(lBuff,"rgba(%hhu,%hhu,%hhu", - & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]); - RGB2Hex(aoResultRGB,lBuff); +#if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__) + sscanf(lBuff,"rgba(%hhu,%hhu,%hhu", & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]); +#else + aoResultRGB[0] = strtol(lBuff+5, & lTmp2, 10 ); + aoResultRGB[1] = strtol(lTmp2+1, & lTmp2, 10 ); + aoResultRGB[2] = strtol(lTmp2+1, NULL, 10 ); +#endif + RGB2Hex(aoResultRGB,lBuff); } } else if ( lWasOsascript || lWasXdialog ) { /* printf( "lBuff: %s\n" , lBuff ) ; */ - sscanf(lBuff,"%hhu %hhu %hhu", - & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]); - RGB2Hex(aoResultRGB,lBuff); +#if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__) + sscanf(lBuff,"%hhu %hhu %hhu", & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]); +#else + aoResultRGB[0] = strtol(lBuff, & lTmp2, 10 ); + aoResultRGB[1] = strtol(lTmp2+1, & lTmp2, 10 ); + aoResultRGB[2] = strtol(lTmp2+1, NULL, 10 ); +#endif + RGB2Hex(aoResultRGB,lBuff); } else { Hex2RGB(lBuff,aoResultRGB); - } - /* printf("%d %d %d\n", aoResultRGB[0],aoResultRGB[1],aoResultRGB[2]); */ - /* printf( "lBuff: %s\n" , lBuff ) ; */ - return lBuff ; + } + /* printf("%d %d %d\n", aoResultRGB[0],aoResultRGB[1],aoResultRGB[2]); */ + /* printf( "lBuff: %s\n" , lBuff ) ; */ + + strcpy(lDefaultHexRGB,lBuff); + return lDefaultHexRGB ; } - -/* not cross platform - zenity only */ -/* contributed by Attila Dusnoki */ -char const * tinyfd_arrayDialog( - char const * const aTitle , /* "" */ - int const aNumOfColumns , /* 2 */ - char const * const * const aColumns , /* {"Column 1","Column 2"} */ - int const aNumOfRows , /* 2 */ - char const * const * const aCells ) - /* {"Row1 Col1","Row1 Col2","Row2 Col1","Row2 Col2"} */ -{ - static char lBuff [MAX_PATH_OR_CMD] ; - char lDialogString [MAX_PATH_OR_CMD] ; - FILE * lIn ; - int i ; - - lBuff[0]='\0'; - - if ( zenityPresent() || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) - { - if ( zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} - strcpy( lDialogString , "zenity" ) ; - if ( (zenity3Present() >= 4) && !getenv("SSH_TTY") ) - { - strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ - } - } - else if ( matedialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;} - strcpy( lDialogString , "matedialog" ) ; - } - else if ( shellementaryPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char const *)1;} - strcpy( lDialogString , "shellementary" ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} - strcpy( lDialogString , "qarma" ) ; - if ( !getenv("SSH_TTY") ) - { - strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ - } - } - strcat( lDialogString , " --list --print-column=ALL" ) ; - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title=\"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - - if ( aColumns && (aNumOfColumns > 0) ) - { - for ( i = 0 ; i < aNumOfColumns ; i ++ ) - { - strcat( lDialogString , " --column=\"" ) ; - strcat( lDialogString , aColumns [i] ) ; - strcat( lDialogString , "\"" ) ; - } - } - - if ( aCells && (aNumOfRows > 0) ) - { - strcat( lDialogString , " " ) ; - for ( i = 0 ; i < aNumOfRows*aNumOfColumns ; i ++ ) - { - strcat( lDialogString , "\"" ) ; - strcat( lDialogString , aCells [i] ) ; - strcat( lDialogString , "\" " ) ; - } - } - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"");return (char const *)0;} - return NULL ; - } - - if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - if ( ! ( lIn = popen( lDialogString , "r" ) ) ) - { - return NULL ; - } - while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - {} - pclose( lIn ) ; - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - /* printf( "lBuff: %s\n" , lBuff ) ; */ - if ( ! strlen( lBuff ) ) - { - return NULL ; - } - return lBuff ; -} #endif /* _WIN32 */ diff --git a/src/tinyfiledialogs.h b/src/tinyfiledialogs.h index b7c38a5..96dd77b 100644 --- a/src/tinyfiledialogs.h +++ b/src/tinyfiledialogs.h @@ -1,79 +1,42 @@ +/* If you are using a C++ compiler to compile tinyfiledialogs.c (maybe renamed with an extension ".cpp") +then comment out << extern "C" >> bellow in this header file) */ + /*_________ - / \ tinyfiledialogs.h v3.3.8 [Nov 4, 2018] zlib licence + / \ tinyfiledialogs.h v3.8.9 [Oct 27, 2022] zlib licence |tiny file| Unique header file created [November 9, 2014] - | dialogs | Copyright (c) 2014 - 2018 Guillaume Vareille http://ysengrin.com + | dialogs | Copyright (c) 2014 - 2021 Guillaume Vareille http://ysengrin.com \____ ___/ http://tinyfiledialogs.sourceforge.net \| git clone http://git.code.sf.net/p/tinyfiledialogs/code tinyfd - ____________________________________________ - | | - | email: tinyfiledialogs at ysengrin.com | - |____________________________________________| - ________________________________________________________________________ - | | - | the windows only wchar_t UTF-16 prototypes are at the end of this file | - |________________________________________________________________________| + ____________________________________________ +| | +| email: tinyfiledialogs at ysengrin.com | +|____________________________________________| + ________________________________________________________________________________ +| ____________________________________________________________________________ | +| | | | +| | on windows: | | +| | - for UTF-16, use the wchar_t functions at the bottom of the header file | | +| | - _wfopen() requires wchar_t | | +| | | | +| | - in tinyfiledialogs, char is UTF-8 by default (since v3.6) | | +| | - but fopen() expects MBCS (not UTF-8) | | +| | - if you want char to be MBCS: set tinyfd_winUtf8 to 0 | | +| | | | +| | - alternatively, tinyfiledialogs provides | | +| | functions to convert between UTF-8, UTF-16 and MBCS | | +| |____________________________________________________________________________| | +|________________________________________________________________________________| -Please upvote my stackoverflow answer https://stackoverflow.com/a/47651444 - -tiny file dialogs (cross-platform C C++) -InputBox PasswordBox MessageBox ColorPicker -OpenFileDialog SaveFileDialog SelectFolderDialog -Native dialog library for WINDOWS MAC OSX GTK+ QT CONSOLE & more -SSH supported via automatic switch to console mode or X11 forwarding - -one C file + a header (add them to your C or C++ project) with 8 functions: -- beep -- notify popup (tray) -- message & question -- input & password -- save file -- open file(s) -- select folder -- color picker - -Complements OpenGL Vulkan GLFW GLUT GLUI VTK SFML TGUI -SDL Ogre Unity3d ION OpenCV CEGUI MathGL GLM CPW GLOW -Open3D IMGUI MyGUI GLT NGL STB & GUI less programs - -NO INIT -NO MAIN LOOP -NO LINKING -NO INCLUDE - -The dialogs can be forced into console mode - -Windows (XP to 10) ASCII MBCS UTF-8 UTF-16 -- native code & vbs create the graphic dialogs -- enhanced console mode can use dialog.exe from -http://andrear.altervista.org/home/cdialog.php -- basic console input - -Unix (command line calls) ASCII UTF-8 -- applescript, kdialog, zenity -- python (2 or 3) + tkinter + python-dbus (optional) -- dialog (opens a console if needed) -- basic console input -The same executable can run across desktops & distributions - -C89 & C++98 compliant: tested with C & C++ compilers -VisualStudio MinGW-gcc GCC Clang TinyCC OpenWatcom-v2 BorlandC SunCC ZapCC -on Windows Mac Linux Bsd Solaris Minix Raspbian -using Gnome Kde Enlightenment Mate Cinnamon Budgie Unity Lxde Lxqt Xfce -WindowMaker IceWm Cde Jds OpenBox Awesome Jwm Xdm - -Bindings for LUA and C# dll, Haskell -Included in LWJGL(java), Rust, Allegrobasic +If you like tinyfiledialogs, please upvote my stackoverflow answer +https://stackoverflow.com/a/47651444 - License - - This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. - Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: - 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be @@ -86,41 +49,61 @@ misrepresented as being the original software. #ifndef TINYFILEDIALOGS_H #define TINYFILEDIALOGS_H -/* #define TINYFD_NOLIB */ -/* On windows, define TINYFD_NOLIB here -if you don't want to include the code creating the graphic dialogs. -Then you won't need to link against Comdlg32.lib and Ole32.lib */ - -/* if tinydialogs.c is compiled as C++ code rather than C code, -you may need to comment out: -extern "C" { -and the corresponding closing bracket near the end of this file: -} -*/ #ifdef __cplusplus +/* if tinydialogs.c is compiled as C++ code rather than C code, you may need to comment this out + and the corresponding closing bracket near the end of this file. */ extern "C" { #endif -extern char const tinyfd_version[8]; /* contains tinyfd current version number */ -extern char const tinyfd_needs[]; /* info about requirements */ -extern int tinyfd_verbose; /* 0 (default) or 1 : on unix, prints the command line calls */ -extern int tinyfd_silent; /* 1 (default) or 0 : on unix, - hide errors and warnings from called dialog*/ - +/******************************************************************************************************/ +/**************************************** UTF-8 on Windows ********************************************/ +/******************************************************************************************************/ #ifdef _WIN32 -/* for UTF-16 use the functions at the end of this files */ -extern int tinyfd_winUtf8; /* 0 (default MBCS) or 1 (UTF-8)*/ -/* on windows string char can be 0:MBCS or 1:UTF-8 -unless your code is really prepared for UTF-8 on windows, leave this on MBSC. -Or you can use the UTF-16 (wchar) prototypes at the end of ths file.*/ +/* On windows, if you want to use UTF-8 ( instead of the UTF-16/wchar_t functions at the end of this file ) +Make sure your code is really prepared for UTF-8 (on windows, functions like fopen() expect MBCS and not UTF-8) */ +extern int tinyfd_winUtf8; /* on windows char strings can be 1:UTF-8(default) or 0:MBCS */ +/* for MBCS change this to 0, in tinyfiledialogs.c or in your code */ + +/* Here are some functions to help you convert between UTF-16 UTF-8 MBSC */ +char * tinyfd_utf8toMbcs(char const * aUtf8string); +char * tinyfd_utf16toMbcs(wchar_t const * aUtf16string); +wchar_t * tinyfd_mbcsTo16(char const * aMbcsString); +char * tinyfd_mbcsTo8(char const * aMbcsString); +wchar_t * tinyfd_utf8to16(char const * aUtf8string); +char * tinyfd_utf16to8(wchar_t const * aUtf16string); #endif +/******************************************************************************************************/ +/******************************************************************************************************/ +/******************************************************************************************************/ + +/************* 3 funtions for C# (you don't need this in C or C++) : */ +char const * tinyfd_getGlobalChar(char const * aCharVariableName); /* returns NULL on error */ +int tinyfd_getGlobalInt(char const * aIntVariableName); /* returns -1 on error */ +int tinyfd_setGlobalInt(char const * aIntVariableName, int aValue); /* returns -1 on error */ +/* aCharVariableName: "tinyfd_version" "tinyfd_needs" "tinyfd_response" + aIntVariableName : "tinyfd_verbose" "tinyfd_silent" "tinyfd_allowCursesDialogs" + "tinyfd_forceConsole" "tinyfd_assumeGraphicDisplay" "tinyfd_winUtf8" +**************/ + + +extern char tinyfd_version[8]; /* contains tinyfd current version number */ +extern char tinyfd_needs[]; /* info about requirements */ +extern int tinyfd_verbose; /* 0 (default) or 1 : on unix, prints the command line calls */ +extern int tinyfd_silent; /* 1 (default) or 0 : on unix, hide errors and warnings from called dialogs */ + +/* Curses dialogs are difficult to use, on windows they are only ascii and uses the unix backslah */ +extern int tinyfd_allowCursesDialogs; /* 0 (default) or 1 */ extern int tinyfd_forceConsole; /* 0 (default) or 1 */ /* for unix & windows: 0 (graphic mode) or 1 (console mode). 0: try to use a graphic solution, if it fails then it uses console mode. 1: forces all dialogs into console mode even when an X server is present, - if the package dialog (and a console is present) or dialog.exe is installed. - on windows it only make sense for console applications */ + it can use the package dialog or dialog.exe. + on windows it only make sense for console applications */ + +extern int tinyfd_assumeGraphicDisplay; /* 0 (default) or 1 */ +/* some systems don't set the environment variable DISPLAY even when a graphic display is present. +set this to 1 to tell tinyfiledialogs to assume the existence of a graphic display */ extern char tinyfd_response[1024]; /* if you pass "tinyfd_query" as aTitle, @@ -129,63 +112,62 @@ but will return 0 for console mode, 1 for graphic mode. tinyfd_response is then filled with the retain solution. possible values for tinyfd_response are (all lowercase) for graphic mode: - windows_wchar windows - applescript kdialog zenity zenity3 matedialog qarma - python2-tkinter python3-tkinter python-dbus perl-dbus - gxmessage gmessage xmessage xdialog gdialog + windows_wchar windows applescript kdialog zenity zenity3 matedialog + shellementary qarma yad python2-tkinter python3-tkinter python-dbus + perl-dbus gxmessage gmessage xmessage xdialog gdialog for console mode: dialog whiptail basicinput no_solution */ -void tinyfd_beep(); +void tinyfd_beep(void); int tinyfd_notifyPopup( - char const * const aTitle, /* NULL or "" */ - char const * const aMessage, /* NULL or "" may contain \n \t */ - char const * const aIconType); /* "info" "warning" "error" */ + char const * aTitle, /* NULL or "" */ + char const * aMessage, /* NULL or "" may contain \n \t */ + char const * aIconType); /* "info" "warning" "error" */ /* return has only meaning for tinyfd_query */ int tinyfd_messageBox( - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may contain \n \t */ - char const * const aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */ - char const * const aIconType , /* "info" "warning" "error" "question" */ - int const aDefaultButton ) ; + char const * aTitle , /* NULL or "" */ + char const * aMessage , /* NULL or "" may contain \n \t */ + char const * aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */ + char const * aIconType , /* "info" "warning" "error" "question" */ + int aDefaultButton ) ; /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ -char const * tinyfd_inputBox( - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may NOT contain \n \t on windows */ - char const * const aDefaultInput ) ; /* "" , if NULL it's a passwordBox */ +char * tinyfd_inputBox( + char const * aTitle , /* NULL or "" */ + char const * aMessage , /* NULL or "" (\n and \t have no effect) */ + char const * aDefaultInput ) ; /* NULL passwordBox, "" inputbox */ /* returns NULL on cancel */ -char const * tinyfd_saveFileDialog( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile , /* NULL or "" */ - int const aNumOfFilterPatterns , /* 0 */ - char const * const * const aFilterPatterns , /* NULL | {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription ) ; /* NULL | "text files" */ +char * tinyfd_saveFileDialog( + char const * aTitle , /* NULL or "" */ + char const * aDefaultPathAndFile , /* NULL or "" */ + int aNumOfFilterPatterns , /* 0 (1 in the following example) */ + char const * const * aFilterPatterns , /* NULL or char const * lFilterPatterns[1]={"*.txt"} */ + char const * aSingleFilterDescription ) ; /* NULL or "text files" */ /* returns NULL on cancel */ -char const * tinyfd_openFileDialog( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile , /* NULL or "" */ - int const aNumOfFilterPatterns , /* 0 */ - char const * const * const aFilterPatterns , /* NULL | {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription , /* NULL | "image files" */ - int const aAllowMultipleSelects ) ; /* 0 or 1 */ +char * tinyfd_openFileDialog( + char const * aTitle, /* NULL or "" */ + char const * aDefaultPathAndFile, /* NULL or "" */ + int aNumOfFilterPatterns , /* 0 (2 in the following example) */ + char const * const * aFilterPatterns, /* NULL or char const * lFilterPatterns[2]={"*.png","*.jpg"}; */ + char const * aSingleFilterDescription, /* NULL or "image files" */ + int aAllowMultipleSelects ) ; /* 0 or 1 */ /* in case of multiple files, the separator is | */ /* returns NULL on cancel */ -char const * tinyfd_selectFolderDialog( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) ; /* NULL or "" */ +char * tinyfd_selectFolderDialog( + char const * aTitle, /* NULL or "" */ + char const * aDefaultPath); /* NULL or "" */ /* returns NULL on cancel */ -char const * tinyfd_colorChooser( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultHexRGB , /* NULL or "#FF0000" */ - unsigned char const aDefaultRGB[3] , /* { 0 , 255 , 255 } */ - unsigned char aoResultRGB[3] ) ; /* { 0 , 0 , 0 } */ +char * tinyfd_colorChooser( + char const * aTitle, /* NULL or "" */ + char const * aDefaultHexRGB, /* NULL or "#FF0000" */ + unsigned char const aDefaultRGB[3] , /* unsigned char lDefaultRGB[3] = { 0 , 128 , 255 }; */ + unsigned char aoResultRGB[3] ) ; /* unsigned char lResultRGB[3]; */ /* returns the hexcolor as a string "#FF0000" */ /* aoResultRGB also contains the result */ /* aDefaultRGB is used only if aDefaultHexRGB is NULL */ @@ -193,134 +175,129 @@ char const * tinyfd_colorChooser( /* returns NULL on cancel */ -/************ NOT CROSS PLATFORM SECTION STARTS HERE ************************/ +/************ WINDOWS ONLY SECTION ************************/ #ifdef _WIN32 -#ifndef TINYFD_NOLIB /* windows only - utf-16 version */ int tinyfd_notifyPopupW( - wchar_t const * const aTitle, /* NULL or L"" */ - wchar_t const * const aMessage, /* NULL or L"" may contain \n \t */ - wchar_t const * const aIconType); /* L"info" L"warning" L"error" */ + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aMessage, /* NULL or L"" may contain \n \t */ + wchar_t const * aIconType); /* L"info" L"warning" L"error" */ /* windows only - utf-16 version */ int tinyfd_messageBoxW( - wchar_t const * const aTitle , /* NULL or L"" */ - wchar_t const * const aMessage, /* NULL or L"" may contain \n \t */ - wchar_t const * const aDialogType, /* L"ok" L"okcancel" L"yesno" */ - wchar_t const * const aIconType, /* L"info" L"warning" L"error" L"question" */ - int const aDefaultButton ); /* 0 for cancel/no , 1 for ok/yes */ + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aMessage, /* NULL or L"" may contain \n \t */ + wchar_t const * aDialogType, /* L"ok" L"okcancel" L"yesno" */ + wchar_t const * aIconType, /* L"info" L"warning" L"error" L"question" */ + int aDefaultButton ); /* 0 for cancel/no , 1 for ok/yes */ /* returns 0 for cancel/no , 1 for ok/yes */ /* windows only - utf-16 version */ -wchar_t const * tinyfd_inputBoxW( - wchar_t const * const aTitle, /* NULL or L"" */ - wchar_t const * const aMessage, /* NULL or L"" may NOT contain \n nor \t */ - wchar_t const * const aDefaultInput ); /* L"" , if NULL it's a passwordBox */ - +wchar_t * tinyfd_inputBoxW( + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aMessage, /* NULL or L"" (\n nor \t not respected) */ + wchar_t const * aDefaultInput); /* NULL passwordBox, L"" inputbox */ + /* windows only - utf-16 version */ -wchar_t const * tinyfd_saveFileDialogW( - wchar_t const * const aTitle, /* NULL or L"" */ - wchar_t const * const aDefaultPathAndFile, /* NULL or L"" */ - int const aNumOfFilterPatterns, /* 0 */ - wchar_t const * const * const aFilterPatterns, /* NULL or {L"*.jpg",L"*.png"} */ - wchar_t const * const aSingleFilterDescription); /* NULL or L"image files" */ +wchar_t * tinyfd_saveFileDialogW( + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aDefaultPathAndFile, /* NULL or L"" */ + int aNumOfFilterPatterns, /* 0 (1 in the following example) */ + wchar_t const * const * aFilterPatterns, /* NULL or wchar_t const * lFilterPatterns[1]={L"*.txt"} */ + wchar_t const * aSingleFilterDescription); /* NULL or L"text files" */ /* returns NULL on cancel */ /* windows only - utf-16 version */ -wchar_t const * tinyfd_openFileDialogW( - wchar_t const * const aTitle, /* NULL or L"" */ - wchar_t const * const aDefaultPathAndFile, /* NULL or L"" */ - int const aNumOfFilterPatterns , /* 0 */ - wchar_t const * const * const aFilterPatterns, /* NULL {L"*.jpg",L"*.png"} */ - wchar_t const * const aSingleFilterDescription, /* NULL or L"image files" */ - int const aAllowMultipleSelects ) ; /* 0 or 1 */ +wchar_t * tinyfd_openFileDialogW( + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aDefaultPathAndFile, /* NULL or L"" */ + int aNumOfFilterPatterns , /* 0 (2 in the following example) */ + wchar_t const * const * aFilterPatterns, /* NULL or wchar_t const * lFilterPatterns[2]={L"*.png","*.jpg"} */ + wchar_t const * aSingleFilterDescription, /* NULL or L"image files" */ + int aAllowMultipleSelects ) ; /* 0 or 1 */ /* in case of multiple files, the separator is | */ /* returns NULL on cancel */ /* windows only - utf-16 version */ -wchar_t const * tinyfd_selectFolderDialogW( - wchar_t const * const aTitle, /* NULL or L"" */ - wchar_t const * const aDefaultPath); /* NULL or L"" */ +wchar_t * tinyfd_selectFolderDialogW( + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aDefaultPath); /* NULL or L"" */ /* returns NULL on cancel */ /* windows only - utf-16 version */ -wchar_t const * tinyfd_colorChooserW( - wchar_t const * const aTitle, /* NULL or L"" */ - wchar_t const * const aDefaultHexRGB, /* NULL or L"#FF0000" */ - unsigned char const aDefaultRGB[3] , /* { 0 , 255 , 255 } */ - unsigned char aoResultRGB[3] ) ; /* { 0 , 0 , 0 } */ +wchar_t * tinyfd_colorChooserW( + wchar_t const * aTitle, /* NULL or L"" */ + wchar_t const * aDefaultHexRGB, /* NULL or L"#FF0000" */ + unsigned char const aDefaultRGB[3], /* unsigned char lDefaultRGB[3] = { 0 , 128 , 255 }; */ + unsigned char aoResultRGB[3]); /* unsigned char lResultRGB[3]; */ /* returns the hexcolor as a string L"#FF0000" */ /* aoResultRGB also contains the result */ /* aDefaultRGB is used only if aDefaultHexRGB is NULL */ /* aDefaultRGB and aoResultRGB can be the same array */ /* returns NULL on cancel */ - -#endif /*TINYFD_NOLIB*/ -#else /*_WIN32*/ - -/* unix zenity only */ -char const * tinyfd_arrayDialog( - char const * const aTitle , /* NULL or "" */ - int const aNumOfColumns , /* 2 */ - char const * const * const aColumns, /* {"Column 1","Column 2"} */ - int const aNumOfRows, /* 2 */ - char const * const * const aCells); - /* {"Row1 Col1","Row1 Col2","Row2 Col1","Row2 Col2"} */ - #endif /*_WIN32 */ #ifdef __cplusplus -} +} /*extern "C"*/ #endif #endif /* TINYFILEDIALOGS_H */ /* -- This is not for android nor ios. -- The code is pure C, perfectly compatible with C++. -- the windows only wchar_t (utf-16) prototypes are in the header file -- windows is fully supported from XP to 10 (maybe even older versions) -- C# & LUA via dll, see example files + ________________________________________________________________________________ +| ____________________________________________________________________________ | +| | | | +| | on windows: | | +| | - for UTF-16, use the wchar_t functions at the bottom of the header file | | +| | - _wfopen() requires wchar_t | | +| | | | +| | - in tinyfiledialogs, char is UTF-8 by default (since v3.6) | | +| | - but fopen() expects MBCS (not UTF-8) | | +| | - if you want char to be MBCS: set tinyfd_winUtf8 to 0 | | +| | | | +| | - alternatively, tinyfiledialogs provides | | +| | functions to convert between UTF-8, UTF-16 and MBCS | | +| |____________________________________________________________________________| | +|________________________________________________________________________________| + +- This is not for ios nor android (it works in termux though). +- The files can be renamed with extension ".cpp" as the code is 100% compatible C C++ + (just comment out << extern "C" >> in the header file) +- Windows is fully supported from XP to 10 (maybe even older versions) +- C# & LUA via dll, see files in the folder EXTRAS - OSX supported from 10.4 to latest (maybe even older versions) -- Avoid using " and ' in titles and messages. +- Do not use " and ' as the dialogs will be displayed with a warning + instead of the title, message, etc... - There's one file filter only, it may contain several patterns. - If no filter description is provided, the list of patterns will become the description. -- char const * filterPatterns[3] = { "*.obj" , "*.stl" , "*.dxf" } ; -- On windows char defaults to MBCS, set tinyfd_winUtf8=1 to use UTF-8 - On windows link against Comdlg32.lib and Ole32.lib - This linking is not compulsary for console mode (see above). -- On unix: it tries command line calls, so no such need. + (on windows the no linking claim is a lie) +- On unix: it tries command line calls, so no such need (NO LINKING). - On unix you need one of the following: - applescript, kdialog, zenity, matedialog, shellementary, qarma, + applescript, kdialog, zenity, matedialog, shellementary, qarma, yad, python (2 or 3)/tkinter/python-dbus (optional), Xdialog - or dialog (opens terminal if running without console) or xterm. + or curses dialogs (opens terminal if running without console). - One of those is already included on most (if not all) desktops. - In the absence of those it will use gdialog, gxmessage or whiptail - with a textinputbox. -- If nothing is found, it switches to basic console input, + with a textinputbox. If nothing is found, it switches to basic console input, it opens a console if needed (requires xterm + bash). -- Use windows separators on windows and unix separators on unix. +- for curses dialogs you must set tinyfd_allowCursesDialogs=1 +- You can query the type of dialog that will be used (pass "tinyfd_query" as aTitle) - String memory is preallocated statically for all the returned values. -- File and path names are tested before return, they are valid. -- If you pass only a path instead of path + filename, - make sure it ends with a separator. +- File and path names are tested before return, they should be valid. - tinyfd_forceConsole=1; at run time, forces dialogs into console mode. - On windows, console mode only make sense for console applications. -- On windows, Console mode is not implemented for wchar_T UTF-16. -- Mutiple selects are not allowed in console mode. -- The package dialog must be installed to run in enhanced console mode. +- On windows, console mode is not implemented for wchar_T UTF-16. +- Mutiple selects are not possible in console mode. +- The package dialog must be installed to run in curses dialogs in console mode. It is already installed on most unix systems. - On osx, the package dialog can be installed via http://macappstore.org/dialog or http://macports.org -- On windows, for enhanced console mode, +- On windows, for curses dialogs console mode, dialog.exe should be copied somewhere on your executable path. It can be found at the bottom of the following page: http://andrear.altervista.org/home/cdialog.php -- If dialog is missing, it will switch to basic console input. -- You can query the type of dialog that will be use. -- MinGW needs gcc >= v4.9 otherwise some headers are incomplete. -- The Hello World (and a bit more) is on the sourceforge site: */