From 1980cdf61f31f91911cb1563244a22008038ac22 Mon Sep 17 00:00:00 2001 From: Attila Dusnoki Date: Wed, 11 May 2016 08:03:00 +0200 Subject: [PATCH 01/41] Update to v2.3.8 --- libtinyfiledialogs/tinyfiledialogs.c | 178 ++++++++++++++++++++++----- libtinyfiledialogs/tinyfiledialogs.h | 28 ++++- 2 files changed, 172 insertions(+), 34 deletions(-) diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index 4a2363c..e4f9c73 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -19,7 +19,7 @@ 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 -v2.3.6 [April 16, 2016] zlib licence. +v2.3.8 [May 10, 2016] zlib licence. A single C file (add it to your C or C++ project) with 6 modal function calls: - message box & question box @@ -29,9 +29,13 @@ A single C file (add it to your C or C++ project) with 6 modal function calls: - select folder dialog - color picker. -Complement to OpenGL GLFW GLUT GLUI VTK SDL Ogre3D Unity3D -or any GUI-less program, there is NO INIT & NO MAIN LOOP. -The dialogs can be forced into console mode. +Complement to OpenGL GLFW GLUT GLUI +VTK SFML SDL Ogre Unity CEGUI ION MathGL +CPW GLOW GLT NGL STB & GUI less programs + +NO INIT & NO MAIN LOOP + +The dialogs can be forced into console mode On Windows: - native code & some vbs create the graphic dialogs @@ -510,7 +514,6 @@ static char const * inputBoxWinGui( strcat(lDialogString, "\",\""); if (aTitle && strlen(aTitle)) { - strcat(lDialogString, aTitle); } strcat(lDialogString, "\",\""); @@ -1042,10 +1045,17 @@ static int messageBoxWinConsole ( strcpy ( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "--backtitle \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } + + if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) ) ) { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "tab =move focus") ; + strcat(lDialogString, "\" ") ; + } + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) { if ( ! aDefaultButton ) @@ -1130,10 +1140,15 @@ static char const * inputBoxWinConsole( strcat ( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "--backtitle \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } + + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "tab =move focus") ; + strcat(lDialogString, "\" ") ; + if ( ! aDefaultInput ) { strcat ( lDialogString , "--passwordbox" ) ; @@ -1204,11 +1219,15 @@ static char const * saveFileDialogWinConsole ( strcpy ( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--title \"") ; - /*strcat(lDialogString, aTitle) ;*/ - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } + + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "\" ") ; + strcat ( lDialogString , "--fselect \"" ) ; if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) { @@ -1265,11 +1284,15 @@ static char const * openFileDialogWinConsole ( strcpy ( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--title \"") ; - /*strcat(lDialogString, aTitle) ;*/ - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } + + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "\" ") ; + strcat ( lDialogString , "--fselect \"" ) ; if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) { @@ -1319,11 +1342,15 @@ static char const * selectFolderDialogWinConsole ( strcpy ( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--title \"") ; - /*strcat(lDialogString, aTitle) ;*/ - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } + + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "\" ") ; + strcat ( lDialogString , "--dselect \"" ) ; if ( aDefaultPath && strlen(aDefaultPath) ) { @@ -2522,10 +2549,17 @@ else :\n\tprint 1\n\"" ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "--backtitle \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } + + if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) ) ) { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "tab =move focus") ; + strcat(lDialogString, "\" ") ; + } + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) { if ( ! aDefaultButton ) @@ -2940,10 +2974,15 @@ frontmost of process \\\"Python\\\" to true' ''');"); if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "--backtitle \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } + + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "tab =move focus") ; + strcat(lDialogString, "\" ") ; + if ( aDefaultInput || lWasGdialog ) { strcat ( lDialogString , "--inputbox" ) ; @@ -3306,11 +3345,15 @@ char const * tinyfd_saveFileDialog ( if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--title \"") ; - /*strcat(lDialogString, aTitle) ;*/ - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } + + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "\" ") ; + strcat ( lDialogString , "--fselect \"" ) ; if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) { @@ -3639,11 +3682,15 @@ frontmost of process \\\"Python\\\" to true' ''');"); if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--title \"") ; - /*strcat(lDialogString, aTitle) ;*/ - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } + + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "\" ") ; + strcat ( lDialogString , "--fselect \"" ) ; if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) { @@ -3858,11 +3905,15 @@ frontmost of process \\\"Python\\\" to true' ''');"); if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--title \"") ; - /*strcat(lDialogString, aTitle) ;*/ - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } + + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "\" ") ; + strcat ( lDialogString , "--dselect \"" ) ; if ( aDefaultPath && strlen(aDefaultPath) ) { @@ -4120,7 +4171,77 @@ frontmost of process \\\"Python\\\" to true' ''');"); return lBuff ; } +/* 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 ; + lBuff[0]='\0'; + int i ; + + if ( zenityPresent() ) + { + strcpy ( lDialogString , "zenity --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 + { + return NULL ; + } + + /* 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 */ @@ -4205,5 +4326,6 @@ VisualStudio : Create a console application project, it links against Comdlg32.lib & Ole32.lib. Right click on your Project, select Properties. - Configuration Properties/General Character Set to Multi-Byte. + Configuration Properties/General + Character Set to "Multi-Byte" or "Not Set" */ diff --git a/libtinyfiledialogs/tinyfiledialogs.h b/libtinyfiledialogs/tinyfiledialogs.h index 4b38186..6936bf4 100644 --- a/libtinyfiledialogs/tinyfiledialogs.h +++ b/libtinyfiledialogs/tinyfiledialogs.h @@ -19,7 +19,7 @@ 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 -v2.3.6 [April 16, 2016] zlib licence. +v2.3.8 [May 10, 2016] zlib licence. A single C file (add it to your C or C++ project) with 6 modal function calls: - message box & question box @@ -29,9 +29,13 @@ A single C file (add it to your C or C++ project) with 6 modal function calls: - select folder dialog - color picker. -Complement to OpenGL GLFW GLUT GLUI VTK SDL Ogre3D Unity3D -or any GUI-less program, there is NO INIT & NO MAIN LOOP. -The dialogs can be forced into console mode. +Complement to OpenGL GLFW GLUT GLUI +VTK SFML SDL Ogre Unity CEGUI ION MathGL +CPW GLOW GLT NGL STB & GUI less programs + +NO INIT & NO MAIN LOOP + +The dialogs can be forced into console mode On Windows: - native code & some vbs create the graphic dialogs @@ -138,6 +142,16 @@ char const * tinyfd_colorChooser( /* aDefaultRGB and aoResultRGB can be the same array */ /* returns NULL on cancel */ +/* not cross platform - zenity only */ +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"} */ + + extern int tinyfd_forceConsole ; /* 0 (default) or 1 can be modified at run time. for unix & windows: 0 (graphic mode) or 1 (console mode). @@ -170,7 +184,8 @@ Then you don't need link against Comdlg32.lib and Ole32.lib */ - On windows, inputbox and passwordbox are not as smooth as they should be: they open a console window for a few seconds. - On visual studio: - set Configuration Properties/General Character Set to Multi-Byte. + set Properties/Configuration Properties/General + Character Set to "Multi-Byte" or "Not Set" - 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. @@ -286,5 +301,6 @@ VisualStudio : Create a console application project, it links against Comdlg32.lib & Ole32.lib. Right click on your Project, select Properties. - Configuration Properties/General Character Set to Multi-Byte. + Configuration Properties/General + Character Set to "Multi-Byte" or "Not Set" */ From 9b47556c46744c6932f3da804ca8fddbe271ccc7 Mon Sep 17 00:00:00 2001 From: Attila Dusnoki Date: Wed, 11 May 2016 09:48:10 +0200 Subject: [PATCH 02/41] Add list dialog --- examples/main.rs | 11 +++++++++++ src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/examples/main.rs b/examples/main.rs index 3920a9f..a391574 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -37,10 +37,21 @@ fn main() { None => color = "null".to_string(), } + let list: String; + match tinyfiledialogs::list_dialog("Test Dialog", + &["Id", "Name"], + Some(&["471", "Donald Duck", + "1143", "Chris P. Bacon", + "6509", "Moon Doge"])) { + Some(result) => list = result, + None => list = "null".to_string(), + } + println!("Choice {:?}", choice); println!("User input {:?}", user_input); println!("Save file {:?}", save_file); println!("Open file {:?}", open_file); println!("folder {:?}", folder); println!("color {:?}", color); + println!("List {:?}", list); } diff --git a/src/lib.rs b/src/lib.rs index c72efdf..a8eb04e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,13 @@ extern { aTitle: *const c_char, aDefaultPath: *const c_char) -> *const c_char; + fn tinyfd_arrayDialog ( + aTitle: *const c_char, + aNumOfColumns: c_int, + aColumns: *const *const c_char, + aNumOfRows: c_int, + aCells: *const *const c_char) -> *const c_char; + fn tinyfd_colorChooser ( aTitle: *const c_char, aDefaultHexRGB: *const c_char, @@ -240,6 +247,41 @@ pub fn select_folder_dialog(title: &str, path: &str) -> Option { } } +pub fn list_dialog(title: &str, + columns: &[&str], + cells: Option<&[&str]>) -> Option { + let list_dialog_title = CString::new(title).unwrap(); + + if columns.is_empty() { + return None; + } + + let list_dialog_columns = + columns.iter().map(|s| CString::new(*s).unwrap()).collect::>(); + let ptr_list_dialog_columns = + list_dialog_columns.iter().map(|c| c.as_ptr()).collect::>(); + + let list_dialog_cells = + cells.map_or(vec![], |f| f.iter().map(|s| CString::new(*s).unwrap()).collect()); + let ptr_list_dialog_cells = + list_dialog_cells.iter().map(|c| c.as_ptr()).collect::>(); + + let dialog = unsafe { + tinyfd_arrayDialog(list_dialog_title.as_ptr(), + list_dialog_columns.len() as c_int, + ptr_list_dialog_columns.as_ptr(), + (list_dialog_cells.len() / list_dialog_columns.len()) as c_int, + ptr_list_dialog_cells.as_ptr()) + }; + if !dialog.is_null() { + unsafe { + Some(CStr::from_ptr(dialog).to_string_lossy().into_owned()) + } + } else { + None + } +} + pub enum DefaultColorValue<'a> { Hex(&'a str), RGB(&'a [u8; 3]), From 2639c8d60c64e1194185a7b64c61e495e34c6b2f Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Tue, 5 Jul 2016 12:56:47 -0400 Subject: [PATCH 03/41] Add script for easy updating from master. --- update.sh | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 update.sh diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..e5379c0 --- /dev/null +++ b/update.sh @@ -0,0 +1,4 @@ +wget https://sourceforge.net/projects/tinyfiledialogs/files/tinyfiledialogs.h/download -O tinyfiledialogs.h +tr -d '\r' < tinyfiledialogs.h > libtinyfiledialogs/tinyfiledialogs.h +wget https://sourceforge.net/projects/tinyfiledialogs/files/tinyfiledialogs.c/download -O tinyfiledialogs.c +tr -d '\r' < tinyfiledialogs.c > libtinyfiledialogs/tinyfiledialogs.c From 849227347e9e3ffb1a6de011d9c3ab700db4ebe8 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Tue, 5 Jul 2016 12:57:07 -0400 Subject: [PATCH 04/41] Update to July 5 master. --- libtinyfiledialogs/tinyfiledialogs.c | 2706 ++++++++++++++++++-------- libtinyfiledialogs/tinyfiledialogs.h | 336 ++-- 2 files changed, 2090 insertions(+), 952 deletions(-) diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index e4f9c73..acd5ec9 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -6,9 +6,12 @@ \____ ___/ http://tinyfiledialogs.sourceforge.net \| mailto:tinfyfiledialogs@ysengrin.com +A big thank you to Don Heyse http://ldglite.sf.net for + his code contributions, bug corrections & thorough testing! + git://git.code.sf.net/p/tinyfiledialogs/code - Please +Please 1) let me know - if you are including tiny file dialogs, I'll be happy to add your link to the list of projects using it. @@ -18,44 +21,43 @@ 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 -v2.3.8 [May 10, 2016] zlib licence. - -A single C file (add it to your C or C++ project) with 6 modal function calls: -- message box & question box -- input box & password box -- save file dialog -- open file dialog & multiple files -- select folder dialog +Native dialog library for WINDOWS MAC OSX (10.4~10.11) GTK+ QT CONSOLE & more +v2.5.2 [July 2, 2016] zlib licence + +A single C file (add it to your C or C++ project) with 6 boxes: +- message / question +- input / password +- save file +- open file & multiple files +- select folder - color picker. -Complement to OpenGL GLFW GLUT GLUI -VTK SFML SDL Ogre Unity CEGUI ION MathGL -CPW GLOW GLT NGL STB & GUI less programs +Complements OpenGL GLFW GLUT GLUI VTK SFML SDL Ogre Unity ION +CEGUI MathGL CPW GLOW IMGUI GLT NGL STB & GUI less programs -NO INIT & NO MAIN LOOP +NO INIT +NO MAIN LOOP The dialogs can be forced into console mode -On Windows: +Windows [UTF-8 + UTF-16] - native code & some vbs create the graphic dialogs - enhanced console mode can use dialog.exe from - http://andrear.altervista.org/home/cdialog.php -- basic console input. +http://andrear.altervista.org/home/cdialog.php +- basic console input -On Unix (command line call attempts): +Unix [UTF-8] (command line call attempts) - applescript -- zenity +- zenity / matedialog - kdialog - Xdialog - python2 tkinter - dialog (opens a console if needed) -- whiptail, gdialog, gxmessage -- basic console input. -The same executable can run across desktops & distributions. +- basic console input +The same executable can run across desktops & distributions tested with C & C++ compilers -on Visual Studio MinGW OSX LINUX FREEBSD ILLUMOS SOLARIS MINIX RASPBIAN +on VisualStudio MinGW Mac Linux Bsd Solaris Minix Raspbian C# fortran (iso_c) using Gnome Kde Enlightenment Mate Cinnamon Unity Lxde Lxqt Xfce WindowMaker IceWm Cde Jds OpenBox @@ -84,16 +86,19 @@ misrepresented as being the original software. #include #include "tinyfiledialogs.h" -/* #define TINYFD_WIN_CONSOLE_ONLY //*/ +/* #define TINYFD_NOLIB //*/ #ifdef _WIN32 + #pragma warning(disable:4996) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ + #pragma warning(disable:4100) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ + #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0500 #endif - #ifndef TINYFD_WIN_CONSOLE_ONLY + #ifndef TINYFD_NOLIB #include #include - #endif /* TINYFD_WIN_CONSOLE_ONLY */ + #endif /* TINYFD_NOLIB */ #include #include #define SLASH "\\" @@ -107,37 +112,57 @@ misrepresented as being the original software. #endif /* _WIN32 */ #define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */ -#define MAX_MULTIPLE 32 +#define MAX_MULTIPLE_FILES 32 + +char tinyfd_version [ 8 ] = "2.5.2"; -#ifdef TINYFD_WIN_CONSOLE_ONLY -/*on windows if you don't compile with the GUI then you must use the console*/ -int tinyfd_forceConsole = 1 ; -#else int tinyfd_forceConsole = 0 ; /* 0 (default) or 1 -can be modified at run time. 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 */ -#endif /* TINYFD_WIN_CONSOLE_ONLY */ + if the package dialog (and a console is present) or dialog.exe is installed. + on windows it only make sense for console applications */ + +char tinyfd_response[1024]; +/* if you pass "tinyfd_query" as aTitle, +the functions will not display the dialogs +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 the graphic mode: + windows applescript zenity zenity3 matedialog kdialog + xdialog tkinter gdialog gxmessage xmessage +for the console mode: + dialog whiptail basicinput */ + +#if defined(TINYFD_NOLIB) && defined(_WIN32) +static int gWarningDisplayed = 1 ; +#else +static int gWarningDisplayed = 0 ; +#endif -#pragma warning(disable:4996) -/* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ +static char gTitle[]="missing software! (so we switch to basic console input)"; -static int gWarningDisplayed = 0 ; -static char gTitle[]= "missing software! (so we switch to basic console input)"; -static char gMessageWin[] = "tiny file dialogs on Windows needs:\n\t\ -a graphic display\nor\tdialog.exe (enhanced console mode)\ -\nor\ta console for basic input" ; +static char gAsciiArt[] ="\ + ___________\n\ +/ \\ \n\ +| tiny file |\n\ +| dialogs |\n\ +\\_____ ____/\n\ + \\|"; static char gMessageUnix[] = "tiny file dialogs on UNIX needs:\n\tapplescript\ -\nor\tzenity (version 3 for the color chooser)\nor\tkdialog\ +\nor\tzenity (version 3 for the color chooser)\ +\nor\tmatedialog\nor\tkdialog\ \nor\tXdialog\nor\tpython 2 with tkinter\ \nor\tdialog (opens a console if needed)\ -\nor\twhiptail, gdialog or gxmessage (really?)\ +\nor\twhiptail, gdialog, gxmessage or xmessage (really?)\ \nor\tit will open a console (if needed) for basic input (you had it comming!)"; +static char gMessageWin[] = "tiny file dialogs on Windows needs:\n\t\ +a graphic display\nor\tdialog.exe (enhanced console mode)\ +\nor\ta console for basic input"; + 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 */ @@ -293,34 +318,6 @@ static void replaceSubStr ( char const * const aSource , } -static int replaceChr ( char * const aString , - char const aOldChr , - char const aNewChr ) -{ - char * p ; - int lRes = 0 ; - - if ( ! aString ) - { - return 0 ; - } - - if ( aOldChr == aNewChr ) - { - return 0 ; - } - - p = aString ; - while ( (p = strchr ( p , aOldChr )) ) - { - * p = aNewChr ; - p ++ ; - lRes = 1 ; - } - return lRes ; -} - - static int filenameValid( char const * const aFileNameWithoutPath ) { if ( ! aFileNameWithoutPath @@ -399,6 +396,34 @@ static char const * ensureFilesExist( char * const aDestination , #ifdef _WIN32 +static int replaceChr ( char * const aString , + char const aOldChr , + char const aNewChr ) +{ + char * p ; + int lRes = 0 ; + + if ( ! aString ) + { + return 0 ; + } + + if ( aOldChr == aNewChr ) + { + return 0 ; + } + + p = aString ; + while ( (p = strchr ( p , aOldChr )) ) + { + * p = aNewChr ; + p ++ ; + lRes = 1 ; + } + return lRes ; +} + + static int dirExists ( char const * const aDirPath ) { struct stat lInfo; @@ -412,71 +437,316 @@ static int dirExists ( char const * const aDirPath ) return 0 ; } -#ifndef TINYFD_WIN_CONSOLE_ONLY -static int messageBoxWinGui ( - char const * const aTitle , /* NULL or "" */ - char const * const aMessage , /* NULL or "" may contain \n and \t */ - char const * const aDialogType , /* "ok" "okcancel" "yesno" */ - char const * const aIconType , /* "info" "warning" "error" "question" */ - int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes */ +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 const * lTmp; + if (aSource) + { + lTmp = wcsrchr(aSource, L'/'); + if (!lTmp) + { + lTmp = wcsrchr(aSource, L'\\'); + } + if (lTmp) + { + wcsncpy(aoDestination, aSource, lTmp - aSource); + aoDestination[lTmp - aSource] = L'\0'; + } + else + { + *aoDestination = L'\0'; + } + } + else + { + *aoDestination = L'\0'; + } + return aoDestination; +} + + +static wchar_t * getLastNameW( + wchar_t * const aoDestination, /* make sure it is allocated */ + wchar_t const * const aSource) +{ + /* copy the last name after '/' or '\' */ + wchar_t const * lTmp; + if (aSource) + { + lTmp = wcsrchr(aSource, L'/'); + if (!lTmp) + { + lTmp = wcsrchr(aSource, L'\\'); + } + if (lTmp) + { + wcscpy(aoDestination, lTmp + 1); + } + else + { + wcscpy(aoDestination, aSource); + } + } + else + { + *aoDestination = L'\0'; + } + return aoDestination; +} + + +static void Hex2RGBW(wchar_t const aHexRGB[8], + unsigned char aoResultRGB[3]) +{ + wchar_t lColorChannel[8]; + if (aoResultRGB) + { + if (aHexRGB) + { + wcscpy(lColorChannel, aHexRGB); + aoResultRGB[2] = (unsigned char)wcstoul(lColorChannel + 5, NULL, 16); + lColorChannel[5] = '\0'; + aoResultRGB[1] = (unsigned char)wcstoul(lColorChannel + 3, NULL, 16); + lColorChannel[3] = '\0'; + aoResultRGB[0] = (unsigned char)wcstoul(lColorChannel + 1, NULL, 16); + /* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); //*/ + } + else + { + aoResultRGB[0] = 0; + aoResultRGB[1] = 0; + aoResultRGB[2] = 0; + } + } +} + + +static void RGB2HexW( + unsigned char const aRGB[3], + wchar_t aoResultHexRGB[8]) +{ + if (aoResultHexRGB) + { + if (aRGB) + { +#if defined(__GNUC__) && __GNUC__ < 5 +swprintf(aoResultHexRGB, L"#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]); +#else +swprintf(aoResultHexRGB, 8, L"#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]); +#endif + /* wprintf(L"aoResultHexRGB %s\n", aoResultHexRGB); //*/ + } + else + { + aoResultHexRGB[0] = 0; + aoResultHexRGB[1] = 0; + aoResultHexRGB[2] = 0; + } + } +} + +#ifndef TINYFD_NOLIB + +static DWORD runSilent(char* aString) +{ + STARTUPINFOA StartupInfo; + PROCESS_INFORMATION ProcessInfo; + char Args[4096]; + char *pEnvCMD = NULL; + char *pDefaultCMD = "CMD.EXE"; + ULONG rc; + + memset(&StartupInfo, 0, sizeof(StartupInfo)); + StartupInfo.cb = sizeof(STARTUPINFOA); + StartupInfo.dwFlags = STARTF_USESHOWWINDOW; + StartupInfo.wShowWindow = SW_HIDE; + + Args[0] = 0; + + pEnvCMD = getenv("COMSPEC"); + + if (pEnvCMD){ + + strcpy(Args, pEnvCMD); + } + else{ + strcpy(Args, pDefaultCMD); + } + + // "/c" option - Do the command then terminate the command window + strcat(Args, " /c "); + //the application you would like to run from the command window + //the parameters passed to the application being run from the command window. + strcat(Args, aString); + + if (!CreateProcessA(NULL, Args, NULL, NULL, FALSE, + CREATE_NEW_CONSOLE, + NULL, + NULL, + &StartupInfo, + &ProcessInfo)) + { + return GetLastError(); + } + + WaitForSingleObject(ProcessInfo.hProcess, INFINITE); + if (!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) + rc = 0; + + CloseHandle(ProcessInfo.hThread); + CloseHandle(ProcessInfo.hProcess); + + return rc; +} + +wchar_t const * tinyfd_utf8to16(char const * const aUtf8string) +{ + static wchar_t lUtf16string[MAX_PATH_OR_CMD]; + int lSize = MultiByteToWideChar(CP_UTF8, + MB_ERR_INVALID_CHARS, + aUtf8string, -1, lUtf16string, + sizeof lUtf16string / sizeof(wchar_t) ); + if (lSize == 0) + { + return NULL; + } + return lUtf16string; +} + + +#if !defined(WC_ERR_INVALID_CHARS) +/* undefined prior to Vista, so not yet in MINGW header file */ +#define WC_ERR_INVALID_CHARS 0x00000080 +#endif +char const * tinyfd_utf16to8(wchar_t const * const aUtf16string) +{ + static char lUtf8string[MAX_PATH_OR_CMD]; + int lSize = WideCharToMultiByte(CP_UTF8, + WC_ERR_INVALID_CHARS, + aUtf16string, -1, lUtf8string, sizeof lUtf8string, + NULL, NULL); + if (lSize == 0) + { + return NULL; + } + return lUtf8string; +} + + +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" */ + wchar_t const * const aIconType, /* "info" "warning" "error" "question" */ + int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes */ { int lBoxReturnValue; - UINT aCode ; - - if ( aIconType && ! strcmp( "warning" , aIconType ) ) + UINT aCode; + + if (aIconType && !wcscmp(L"warning", aIconType)) { - aCode = MB_ICONWARNING ; + aCode = MB_ICONWARNING; } - else if ( aIconType && ! strcmp("error", aIconType)) + else if (aIconType && !wcscmp(L"error", aIconType)) { - aCode = MB_ICONERROR ; + aCode = MB_ICONERROR; } - else if ( aIconType && ! strcmp("question", aIconType)) + else if (aIconType && !wcscmp(L"question", aIconType)) { - aCode = MB_ICONQUESTION ; + aCode = MB_ICONQUESTION; } else { - aCode = MB_ICONINFORMATION ; + aCode = MB_ICONINFORMATION; } - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + if (aDialogType && !wcscmp(L"okcancel", aDialogType)) { - aCode += MB_OKCANCEL ; - if ( ! aDefaultButton ) + aCode += MB_OKCANCEL; + if (!aDefaultButton) { - aCode += MB_DEFBUTTON2 ; + aCode += MB_DEFBUTTON2; } } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + else if (aDialogType && !wcscmp(L"yesno", aDialogType)) { - aCode += MB_YESNO ; - if ( ! aDefaultButton ) + aCode += MB_YESNO; + if (!aDefaultButton) { - aCode += MB_DEFBUTTON2 ; + aCode += MB_DEFBUTTON2; } } else { - aCode += MB_OK ; + aCode += MB_OK; } - lBoxReturnValue = MessageBox(NULL, aMessage, aTitle, aCode); - if ( ( ( aDialogType - && strcmp("okcancel", aDialogType) - && strcmp("yesno", aDialogType) ) ) + lBoxReturnValue = MessageBoxW(NULL, aMessage, aTitle, aCode); + if (((aDialogType + && wcscmp(L"okcancel", aDialogType) + && wcscmp(L"yesno", aDialogType))) || (lBoxReturnValue == IDOK) - || (lBoxReturnValue == IDYES) ) + || (lBoxReturnValue == IDYES)) { - return 1 ; + return 1; } else { - return 0 ; + return 0; + } +} + + +static int messageBoxWinGui( + char const * const aTitle, /* NULL or "" */ + char const * const aMessage, /* NULL or "" may contain \n and \t */ + char const * const aDialogType, /* "ok" "okcancel" "yesno" */ + char const * const aIconType, /* "info" "warning" "error" "question" */ + int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes */ +{ + wchar_t lTitle[MAX_PATH_OR_CMD]; + wchar_t lMessage[MAX_PATH_OR_CMD]; + wchar_t lDialogType[16]; + wchar_t lIconType[16]; + wchar_t const * lTmp; + + lTitle[0] = L'\0'; + lMessage[0] = L'\0'; + lDialogType[0] = L'\0'; + lIconType[0] = L'\0'; + + if (aTitle) + { + lTmp = tinyfd_utf8to16(aTitle); + wcscpy(lTitle, lTmp); } + + if (aMessage) + { + lTmp = tinyfd_utf8to16(aMessage); + wcscpy(lMessage, lTmp); + } + + if (aDialogType) + { + lTmp = tinyfd_utf8to16(aDialogType); + wcscpy(lDialogType, lTmp); + } + + if (aIconType) + { + lTmp = tinyfd_utf8to16(aIconType); + wcscpy(lIconType, lTmp); + } + + return tinyfd_messageBoxW(lTitle, lMessage, + lDialogType, lIconType, aDefaultButton ); } +#endif /* TINYFD_NOLIB */ static char const * inputBoxWinGui( char * const aoBuff , @@ -487,6 +757,9 @@ static char const * inputBoxWinGui( char lDialogString[4*MAX_PATH_OR_CMD]; FILE * lIn; int lResult; +#ifndef TINYFD_NOLIB + DWORD lDword; +#endif if (aDefaultInput) { @@ -545,8 +818,8 @@ WINDOWSTATE = 'hidden'\n\ result = 0\n\ \n\ Sub Window_onLoad\n\ -intWidth = 375\n\ -intHeight = 165\n\ +intWidth = 600\n\ +intHeight = 250\n\ Me.ResizeTo intWidth, intHeight\n\ Me.MoveTo((Screen.Width/2)-(intWidth/2)),((Screen.Height/2)-(intHeight/2))\n\ txt_input.Focus\n\ @@ -621,14 +894,15 @@ name = 'txt_input' style = 'font-size: 11px;' value = '' >
\n\ fputs(lDialogString, lIn); fclose(lIn); - if ( GetConsoleWindow() ) - { - strcpy(lDialogString, ""); - } - else + strcpy(lDialogString, ""); + +#ifndef TINYFD_NOLIB + if ( ! GetConsoleWindow() ) { - strcpy(lDialogString, "powershell -WindowStyle Hidden -Command \""); + strcat(lDialogString, "powershell -WindowStyle Hidden -Command \""); } +#endif + if (aDefaultInput) { strcat(lDialogString, @@ -644,7 +918,10 @@ name = 'txt_input' style = 'font-size: 11px;' value = '' >
\n\ strcat(lDialogString, "\""); } /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ - if (!(lIn = _popen(lDialogString,"r"))) +//#ifndef TINYFD_NOLIB +// lDword = runSilent(lDialogString); +//#else + if (!(lIn = _popen(lDialogString, "r"))) { return NULL ; } @@ -655,6 +932,7 @@ name = 'txt_input' style = 'font-size: 11px;' value = '' >
\n\ { aoBuff[ strlen ( aoBuff ) -1 ] = '\0' ; } +//#endif if (aDefaultInput) { sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.vbs", @@ -690,218 +968,299 @@ name = 'txt_input' style = 'font-size: 11px;' value = '' >
\n\ return aoBuff + 1; } +#ifndef TINYFD_NOLIB -static char const * saveFileDialogWinGui ( - 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 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" */ { - char lDirname [ MAX_PATH_OR_CMD ] ; - char lDialogString[MAX_PATH_OR_CMD]; - char lFilterPatterns[MAX_PATH_OR_CMD] = ""; - int i ; - char * p; - OPENFILENAME ofn ; - char * lRetval; + static wchar_t lBuff[MAX_PATH_OR_CMD]; + wchar_t lDirname[MAX_PATH_OR_CMD]; + wchar_t lDialogString[MAX_PATH_OR_CMD]; + wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L""; + wchar_t * p; + wchar_t * lRetval; + int i; + OPENFILENAMEW ofn; HRESULT lHResult; - lHResult = CoInitializeEx(NULL,0); + lHResult = CoInitializeEx(NULL, 0); + + getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile); + getLastNameW(lBuff, aDefaultPathAndFile); - getPathWithoutFinalSlash(lDirname, aDefaultPathAndFile); - getLastName(aoBuff, aDefaultPathAndFile); - if (aNumOfFilterPatterns > 0) { - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) + if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) { - strcpy(lFilterPatterns, aSingleFilterDescription); - strcat(lFilterPatterns, "\n"); + wcscpy(lFilterPatterns, aSingleFilterDescription); + wcscat(lFilterPatterns, L"\n"); } - strcat(lFilterPatterns, aFilterPatterns[0]); + wcscat(lFilterPatterns, aFilterPatterns[0]); for (i = 1; i < aNumOfFilterPatterns; i++) { - strcat(lFilterPatterns, ";"); - strcat(lFilterPatterns, aFilterPatterns[i]); + wcscat(lFilterPatterns, L";"); + wcscat(lFilterPatterns, aFilterPatterns[i]); } - strcat(lFilterPatterns, "\n"); - if ( ! (aSingleFilterDescription && strlen(aSingleFilterDescription) ) ) + wcscat(lFilterPatterns, L"\n"); + if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription))) { - strcpy(lDialogString, lFilterPatterns); - strcat(lFilterPatterns, lDialogString); + wcscpy(lDialogString, lFilterPatterns); + wcscat(lFilterPatterns, lDialogString); } - strcat(lFilterPatterns, "All Files\n*.*\n"); + wcscat(lFilterPatterns, L"All Files\n*.*\n"); p = lFilterPatterns; - while ((p = strchr(p, '\n')) != NULL) + while ((p = wcschr(p, L'\n')) != NULL) { - *p = '\0'; - p ++ ; + *p = L'\0'; + p++; } } - - ofn.lStructSize = sizeof(OPENFILENAME) ; - ofn.hwndOwner = 0 ; - ofn.hInstance = 0 ; - ofn.lpstrFilter = lFilterPatterns ; - ofn.lpstrCustomFilter = NULL ; - ofn.nMaxCustFilter = 0 ; - ofn.nFilterIndex = 1 ; - ofn.lpstrFile = aoBuff; - ofn.nMaxFile = MAX_PATH_OR_CMD ; - ofn.lpstrFileTitle = NULL ; - ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ; + ofn.lStructSize = sizeof(OPENFILENAMEW); + ofn.hwndOwner = 0; + ofn.hInstance = 0; + ofn.lpstrFilter = lFilterPatterns; + ofn.lpstrCustomFilter = NULL; + ofn.nMaxCustFilter = 0; + ofn.nFilterIndex = 1; + ofn.lpstrFile = lBuff; + + ofn.nMaxFile = MAX_PATH_OR_CMD; + ofn.lpstrFileTitle = NULL; + ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT; ofn.lpstrInitialDir = lDirname; - ofn.lpstrTitle = aTitle ; - ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR ; - ofn.nFileOffset = 0 ; - ofn.nFileExtension = 0 ; - ofn.lpstrDefExt = NULL ; - ofn.lCustData = 0L ; - ofn.lpfnHook = NULL ; - ofn.lpTemplateName = NULL ; + ofn.lpstrTitle = aTitle; + 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 ( GetSaveFileName ( & ofn ) == 0 ) + if (GetSaveFileNameW(&ofn) == 0) { - lRetval = NULL ; + lRetval = NULL; } - else - { - lRetval = aoBuff ; + else + { + lRetval = lBuff; } - if (lHResult==S_OK || lHResult==S_FALSE) + if (lHResult == S_OK || lHResult == S_FALSE) { CoUninitialize(); } - return lRetval ; + return lRetval; } -static char const * openFileDialogWinGui ( - 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 */ +static char const * saveFileDialogWinGui( + 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 lFilterPatterns[MAX_PATH_OR_CMD] = ""; - char lDialogString[MAX_PATH_OR_CMD] ; - char * lPointers[MAX_MULTIPLE]; - size_t lLengths[MAX_MULTIPLE]; - int i , j ; - char * p; - OPENFILENAME ofn; - size_t lBuffLen ; - char * lRetval; - HRESULT lHResult; + wchar_t lTitle[MAX_PATH_OR_CMD]; + wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD]; + wchar_t lSingleFilterDescription[MAX_PATH_OR_CMD]; + wchar_t * * lFilterPatterns; + wchar_t const * lTmpWChar; + char const * lTmpChar; + int i ; + + lTitle[0] = L'\0'; + lDefaultPathAndFile[0] = L'\0'; + lSingleFilterDescription[0] = L'\0'; + + lFilterPatterns = malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (aFilterPatterns[i]) + { + int lSize = strlen(aFilterPatterns[i]); + lFilterPatterns[i] = malloc(2*lSize*sizeof(wchar_t)); //2*bigger to be sure + lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]); + wcscpy(lFilterPatterns[i], lTmpWChar); + } + else + { + lFilterPatterns[i] = NULL; + } + } + + if (aTitle) + { + lTmpWChar = tinyfd_utf8to16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aDefaultPathAndFile) + { + lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile); + wcscpy(lDefaultPathAndFile, lTmpWChar); + } + if (aSingleFilterDescription) + { + lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription); + wcscpy(lSingleFilterDescription, lTmpWChar); + } + + lTmpWChar = tinyfd_saveFileDialogW( + lTitle, + lDefaultPathAndFile, + aNumOfFilterPatterns, + (wchar_t const** ) /*stupid cast for gcc*/ + lFilterPatterns, + lSingleFilterDescription); + + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (aFilterPatterns[i]) + { + free(lFilterPatterns[i]); + } + } + free(lFilterPatterns); + + if (!lTmpWChar) + { + return NULL; + } + + lTmpChar = tinyfd_utf16to8(lTmpWChar); + strcpy(aoBuff, 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]; - lHResult = CoInitializeEx(NULL,0); + 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; + int i, j; + OPENFILENAMEW ofn; + size_t lBuffLen; + HRESULT lHResult; - getPathWithoutFinalSlash(lDirname, aDefaultPathAndFile); - getLastName(aoBuff, aDefaultPathAndFile); + lHResult = CoInitializeEx(NULL, 0); + + getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile); + getLastNameW(lBuff, aDefaultPathAndFile); if (aNumOfFilterPatterns > 0) { - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) + if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) { - strcpy(lFilterPatterns, aSingleFilterDescription); - strcat(lFilterPatterns, "\n"); + wcscpy(lFilterPatterns, aSingleFilterDescription); + wcscat(lFilterPatterns, L"\n"); } - strcat(lFilterPatterns, aFilterPatterns[0]); + wcscat(lFilterPatterns, aFilterPatterns[0]); for (i = 1; i < aNumOfFilterPatterns; i++) { - strcat(lFilterPatterns, ";"); - strcat(lFilterPatterns, aFilterPatterns[i]); + wcscat(lFilterPatterns, L";"); + wcscat(lFilterPatterns, aFilterPatterns[i]); } - strcat(lFilterPatterns, "\n"); - if ( ! (aSingleFilterDescription && strlen(aSingleFilterDescription) ) ) + wcscat(lFilterPatterns, L"\n"); + if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription))) { - strcpy(lDialogString, lFilterPatterns); - strcat(lFilterPatterns, lDialogString); + wcscpy(lDialogString, lFilterPatterns); + wcscat(lFilterPatterns, lDialogString); } - strcat(lFilterPatterns, "All Files\n*.*\n"); + wcscat(lFilterPatterns, L"All Files\n*.*\n"); p = lFilterPatterns; - while ((p = strchr(p, '\n')) != NULL) - { - *p = '\0'; - p ++ ; - } - } - - ofn.lStructSize = sizeof ( OPENFILENAME ) ; - ofn.hwndOwner = 0 ; - ofn.hInstance = 0 ; - ofn.lpstrFilter = lFilterPatterns; - ofn.lpstrCustomFilter = NULL ; - ofn.nMaxCustFilter = 0 ; - ofn.nFilterIndex = 1 ; - ofn.lpstrFile = aoBuff ; - ofn.nMaxFile = MAX_PATH_OR_CMD ; - ofn.lpstrFileTitle = NULL ; - ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ; - ofn.lpstrInitialDir = lDirname ; - ofn.lpstrTitle = aTitle ; - 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 ) + while ((p = wcschr(p, L'\n')) != NULL) + { + *p = L'\0'; + p++; + } + } + + ofn.lStructSize = sizeof(OPENFILENAME); + ofn.hwndOwner = 0; + ofn.hInstance = 0; + ofn.lpstrFilter = lFilterPatterns; + ofn.lpstrCustomFilter = NULL; + ofn.nMaxCustFilter = 0; + ofn.nFilterIndex = 1; + ofn.lpstrFile = lBuff; + ofn.nMaxFile = MAX_PATH_OR_CMD; + ofn.lpstrFileTitle = NULL; + ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT; + ofn.lpstrInitialDir = lDirname; + ofn.lpstrTitle = aTitle; + 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 ( GetOpenFileName ( & ofn ) == 0 ) + if (GetOpenFileNameW(&ofn) == 0) { - lRetval = NULL ; + lRetval = NULL; } - else + else { - lBuffLen = strlen(aoBuff) ; - lPointers[0] = aoBuff + lBuffLen + 1 ; - if ( !aAllowMultipleSelects || (lPointers[0][0] == '\0') ) + lBuffLen = wcslen(lBuff); + lPointers[0] = lBuff + lBuffLen + 1; + if (!aAllowMultipleSelects || (lPointers[0][0] == L'\0')) { - lRetval = aoBuff ; + lRetval = lBuff; } - else + else { - i = 0 ; + i = 0; do { - lLengths[i] = strlen(lPointers[i]); - lPointers[i+1] = lPointers[i] + lLengths[i] + 1 ; - i ++ ; - } - while ( lPointers[i][0] != '\0' ); + lLengths[i] = wcslen(lPointers[i]); + lPointers[i + 1] = lPointers[i] + lLengths[i] + 1; + i++; + } while (lPointers[i][0] != L'\0'); i--; - p = aoBuff + MAX_MULTIPLE*MAX_PATH_OR_CMD - 1 ; - * p = '\0'; - for ( j = i ; j >=0 ; j-- ) + 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]); + memmove(p, lPointers[j], lLengths[j]*sizeof(wchar_t)); p--; - *p = '\\'; - p -= lBuffLen ; - memmove(p, aoBuff, lBuffLen); + *p = L'\\'; + p -= lBuffLen; + memmove(p, lBuff, lBuffLen*sizeof(wchar_t)); p--; - *p = '|'; + *p = L'|'; } p++; - lRetval = p ; + lRetval = p; } } - if (lHResult==S_OK || lHResult==S_FALSE) + if (lHResult == S_OK || lHResult == S_FALSE) { CoUninitialize(); } @@ -909,68 +1268,195 @@ static char const * openFileDialogWinGui ( } +static char const * openFileDialogWinGui( + 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 */ +{ + wchar_t lTitle[MAX_PATH_OR_CMD]; + wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD]; + wchar_t lSingleFilterDescription[MAX_PATH_OR_CMD]; + wchar_t * * lFilterPatterns; + wchar_t const * lTmpWChar; + char const * lTmpChar; + int i; + + lTitle[0] = L'\0'; + lDefaultPathAndFile[0] = L'\0'; + lSingleFilterDescription[0] = L'\0'; + + lFilterPatterns = malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (aFilterPatterns[i]) + { + int lSize = strlen(aFilterPatterns[i]); + lFilterPatterns[i] = malloc(2 * lSize*sizeof(wchar_t)); //2*bigger to be sure + lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]); + wcscpy(lFilterPatterns[i], lTmpWChar); + } + else + { + lFilterPatterns[i] = NULL; + } + } + + if (aTitle) + { + lTmpWChar = tinyfd_utf8to16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aDefaultPathAndFile) + { + lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile); + wcscpy(lDefaultPathAndFile, lTmpWChar); + } + if (aSingleFilterDescription) + { + lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription); + wcscpy(lSingleFilterDescription, lTmpWChar); + } + + lTmpWChar = tinyfd_openFileDialogW( + lTitle, + lDefaultPathAndFile, + aNumOfFilterPatterns, + (wchar_t const**) /*stupid cast for gcc*/ + lFilterPatterns, + lSingleFilterDescription, + aAllowMultipleSelects); + + for (i = 0; i < aNumOfFilterPatterns; i++) + { + if (aFilterPatterns[i]) + { + free(lFilterPatterns[i]); + } + } + free(lFilterPatterns); + + if (!lTmpWChar) + { + return NULL; + } + + lTmpChar = tinyfd_utf16to8(lTmpWChar); + strcpy(aoBuff, lTmpChar); + + return aoBuff; +} + + +wchar_t const * tinyfd_selectFolderDialogW( + wchar_t const * const aTitle, /* NULL or "" */ + wchar_t const * const aDefaultPath) /* NULL or "" */ +{ + static wchar_t lBuff[MAX_PATH_OR_CMD]; + + BROWSEINFOW bInfo; + LPITEMIDLIST lpItem; + HRESULT lHResult; + + lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); + + /* we can't use aDefaultPath */ + bInfo.hwndOwner = 0; + bInfo.pidlRoot = NULL; + bInfo.pszDisplayName = lBuff; + bInfo.lpszTitle = aTitle; + bInfo.ulFlags = BIF_USENEWUI; + bInfo.lpfn = NULL; + bInfo.lParam = 0; + bInfo.iImage = -1; + + lpItem = SHBrowseForFolderW(&bInfo); + if (lpItem) + { + SHGetPathFromIDListW(lpItem, lBuff); + } + + if (lHResult == S_OK || lHResult == S_FALSE) + { + CoUninitialize(); + } + return lBuff; +} + + static char const * selectFolderDialogWinGui ( char * const aoBuff , char const * const aTitle , /* NULL or "" */ char const * const aDefaultPath ) /* NULL or "" */ { - BROWSEINFO bInfo ; - LPITEMIDLIST lpItem ; - HRESULT lHResult; + wchar_t lTitle[MAX_PATH_OR_CMD]; + wchar_t lDefaultPath[MAX_PATH_OR_CMD]; + wchar_t const * lTmpWChar; + char const * lTmpChar; - lHResult = CoInitializeEx(NULL,0); - - /* we can't use aDefaultPath */ - bInfo.hwndOwner = 0 ; - bInfo.pidlRoot = NULL ; - bInfo.pszDisplayName = aoBuff ; - bInfo.lpszTitle = aTitle ; - bInfo.ulFlags = 0 ; - bInfo.lpfn = NULL ; - bInfo.lParam = 0 ; - bInfo.iImage = -1 ; + lTitle[0] = L'\0'; + lDefaultPath[0] = L'\0'; - lpItem = SHBrowseForFolder ( & bInfo ) ; - if ( lpItem ) + if (aTitle) { - SHGetPathFromIDList ( lpItem , aoBuff ) ; + lTmpWChar = tinyfd_utf8to16(aTitle); + wcscpy(lTitle, lTmpWChar); } + if (aDefaultPath) + { + lTmpWChar = tinyfd_utf8to16(aDefaultPath); + wcscpy(lDefaultPath, lTmpWChar); + } + + lTmpWChar = tinyfd_selectFolderDialogW( + lTitle, + lDefaultPath); - if (lHResult==S_OK || lHResult==S_FALSE) + if (!lTmpWChar) { - CoUninitialize(); + return NULL; } - return aoBuff ; + + lTmpChar = tinyfd_utf16to8(lTmpWChar); + strcpy(aoBuff, lTmpChar); + + return aoBuff; } -static char const * colorChooserWinGui( - char const * const aTitle, /* NULL or "" */ - char const * const aDefaultHexRGB, /* NULL or "#FF0000"*/ +wchar_t const * tinyfd_colorChooserW( + wchar_t const * const aTitle, /* NULL or "" */ + wchar_t const * const aDefaultHexRGB, /* NULL or "#FF0000"*/ unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ { - static CHOOSECOLOR cc; - static COLORREF crCustColors[16]; - static char lResultHexRGB[8]; + static wchar_t lResultHexRGB[8]; + CHOOSECOLORW cc; + COLORREF crCustColors[16]; unsigned char lDefaultRGB[3]; int lRet; - if ( aDefaultHexRGB ) + //HRESULT lHResult; + //lHResult = CoInitializeEx(NULL, 0); + + if (aDefaultHexRGB) { - Hex2RGB(aDefaultHexRGB, lDefaultRGB); + Hex2RGBW(aDefaultHexRGB, lDefaultRGB); } else { - lDefaultRGB[0]=aDefaultRGB[0]; - lDefaultRGB[1]=aDefaultRGB[1]; - lDefaultRGB[2]=aDefaultRGB[2]; + lDefaultRGB[0] = aDefaultRGB[0]; + lDefaultRGB[1] = aDefaultRGB[1]; + lDefaultRGB[2] = aDefaultRGB[2]; } /* we can't use aTitle */ - cc.lStructSize = sizeof ( CHOOSECOLOR ) ; - cc.hwndOwner = NULL ; - cc.hInstance = NULL ; + cc.lStructSize = sizeof(CHOOSECOLOR); + cc.hwndOwner = NULL; + cc.hInstance = NULL; cc.rgbResult = RGB(lDefaultRGB[0], lDefaultRGB[1], lDefaultRGB[2]); cc.lpCustColors = crCustColors; cc.Flags = CC_RGBINIT | CC_FULLOPEN; @@ -978,24 +1464,442 @@ static char const * colorChooserWinGui( cc.lpfnHook = NULL; cc.lpTemplateName = NULL; - lRet = ChooseColor(&cc); + lRet = ChooseColorW(&cc); + + if (!lRet) + { + return NULL; + } + + aoResultRGB[0] = GetRValue(cc.rgbResult); + aoResultRGB[1] = GetGValue(cc.rgbResult); + aoResultRGB[2] = GetBValue(cc.rgbResult); + + RGB2HexW(aoResultRGB, lResultHexRGB); + + //if (lHResult == S_OK || lHResult == S_FALSE) + //{ + // CoUninitialize(); + //} + + return lResultHexRGB; +} + + +static char const * colorChooserWinGui( + 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]; + + wchar_t lTitle[MAX_PATH_OR_CMD]; + wchar_t lDefaultHexRGB[MAX_PATH_OR_CMD]; + wchar_t const * lTmpWChar; + char const * lTmpChar; - if ( ! lRet ) + lTitle[0] = L'\0'; + lDefaultHexRGB[0] = L'\0'; + + if (aTitle) { - return NULL; + lTmpWChar = tinyfd_utf8to16(aTitle); + wcscpy(lTitle, lTmpWChar); + } + if (aDefaultHexRGB) + { + lTmpWChar = tinyfd_utf8to16(aDefaultHexRGB); + wcscpy(lDefaultHexRGB, lTmpWChar); } - aoResultRGB[0] = GetRValue(cc.rgbResult); - aoResultRGB[1] = GetGValue(cc.rgbResult); - aoResultRGB[2] = GetBValue(cc.rgbResult); + lTmpWChar = tinyfd_colorChooserW( + lTitle, + lDefaultHexRGB, + aDefaultRGB, + aoResultRGB ); + + if (!lTmpWChar) + { + return NULL; + } - RGB2Hex(aoResultRGB, lResultHexRGB); + lTmpChar = tinyfd_utf16to8(lTmpWChar); + strcpy(lResultHexRGB, lTmpChar); return lResultHexRGB; } -#endif /* TINYFD_WIN_CONSOLE_ONLY */ +//static int messageBoxWinGui ( +// char const * const aTitle , /* NULL or "" */ +// char const * const aMessage , /* NULL or "" may contain \n and \t */ +// char const * const aDialogType , /* "ok" "okcancel" "yesno" */ +// char const * const aIconType , /* "info" "warning" "error" "question" */ +// int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes */ +//{ +// 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 +// { +// aCode += MB_OK ; +// } +// +// lBoxReturnValue = MessageBoxA(NULL, aMessage, aTitle, aCode); +// if ( ( ( aDialogType +// && strcmp("okcancel", aDialogType) +// && strcmp("yesno", aDialogType) ) ) +// || (lBoxReturnValue == IDOK) +// || (lBoxReturnValue == IDYES) ) +// { +// return 1 ; +// } +// else +// { +// return 0 ; +// } +//} + + +//static char const * saveFileDialogWinGui ( +// 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; +// OPENFILENAMEA ofn ; +// char * lRetval; +// HRESULT lHResult; +// +// 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 = 0 ; +// ofn.hInstance = 0 ; +// ofn.lpstrFilter = lFilterPatterns ; +// ofn.lpstrCustomFilter = NULL ; +// ofn.nMaxCustFilter = 0 ; +// ofn.nFilterIndex = 1 ; +// ofn.lpstrFile = aoBuff; +// +// ofn.nMaxFile = MAX_PATH_OR_CMD ; +// ofn.lpstrFileTitle = NULL ; +// ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ; +// ofn.lpstrInitialDir = lDirname; +// ofn.lpstrTitle = aTitle ; +// 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 * openFileDialogWinGui ( +// 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; +// OPENFILENAMEA ofn; +// size_t lBuffLen ; +// char * lRetval; +// HRESULT lHResult; +// +// 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 = 0 ; +// ofn.hInstance = 0 ; +// ofn.lpstrFilter = lFilterPatterns; +// ofn.lpstrCustomFilter = NULL ; +// ofn.nMaxCustFilter = 0 ; +// ofn.nFilterIndex = 1 ; +// ofn.lpstrFile = aoBuff ; +// ofn.nMaxFile = MAX_PATH_OR_CMD ; +// ofn.lpstrFileTitle = NULL ; +// ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ; +// ofn.lpstrInitialDir = lDirname ; +// ofn.lpstrTitle = aTitle ; +// 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; +//} + + +//static char const * selectFolderDialogWinGui ( +// char * const aoBuff , +// char const * const aTitle , /* NULL or "" */ +// char const * const aDefaultPath ) /* NULL or "" */ +//{ +// BROWSEINFOA bInfo ; +// LPITEMIDLIST lpItem ; +// HRESULT lHResult; +// +// lHResult = CoInitializeEx(NULL,0); +// +// /* we can't use aDefaultPath */ +// bInfo.hwndOwner = 0 ; +// bInfo.pidlRoot = NULL ; +// bInfo.pszDisplayName = aoBuff ; +// bInfo.lpszTitle = aTitle ; +// bInfo.ulFlags = 0 ; +// bInfo.lpfn = NULL ; +// bInfo.lParam = 0 ; +// bInfo.iImage = -1 ; +// +// lpItem = SHBrowseForFolderA ( & bInfo ) ; +// if ( lpItem ) +// { +// SHGetPathFromIDListA ( lpItem , aoBuff ) ; +// } +// +// if (lHResult==S_OK || lHResult==S_FALSE) +// { +// CoUninitialize(); +// } +// return aoBuff ; +//} + + +//static char const * colorChooserWinGui( +// 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 = NULL ; +// 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 ( ) { @@ -1045,14 +1949,15 @@ static int messageBoxWinConsole ( strcpy ( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) ) ) { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, "tab =move focus") ; + if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) ) ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab -> move focus") ; strcat(lDialogString, "\" ") ; } @@ -1079,7 +1984,6 @@ static int messageBoxWinConsole ( } - strcat ( lDialogString , "\"" ) ; if ( aMessage && strlen(aMessage) ) { @@ -1140,13 +2044,13 @@ static char const * inputBoxWinConsole( strcat ( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, "tab =move focus") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab -> move focus") ; strcat(lDialogString, "\" ") ; if ( ! aDefaultInput ) @@ -1219,13 +2123,14 @@ static char const * saveFileDialogWinConsole ( strcpy ( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab -> focus | spacebar -> select | / -> populate | enter -> ok input line") ; strcat(lDialogString, "\" ") ; strcat ( lDialogString , "--fselect \"" ) ; @@ -1280,17 +2185,17 @@ static char const * openFileDialogWinConsole ( char lDialogString[MAX_PATH_OR_CMD] ; FILE * lIn; - strcpy ( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab -> focus | spacebar -> select | / -> populate | enter -> ok input line") ; strcat(lDialogString, "\" ") ; strcat ( lDialogString , "--fselect \"" ) ; @@ -1342,13 +2247,14 @@ static char const * selectFolderDialogWinConsole ( strcpy ( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab -> focus | spacebar -> select | / -> populate | enter -> ok input line") ; strcat(lDialogString, "\" ") ; strcat ( lDialogString , "--dselect \"" ) ; @@ -1398,27 +2304,31 @@ int tinyfd_messageBox ( { char lChar ; -#ifndef TINYFD_WIN_CONSOLE_ONLY +#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 messageBoxWinGui( aTitle,aMessage,aDialogType,aIconType,aDefaultButton); } else -#endif /* TINYFD_WIN_CONSOLE_ONLY */ +#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 ( !gWarningDisplayed ) + 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\n", gMessageWin); + gWarningDisplayed = 1; + printf("\n\n%s", gAsciiArt); + printf("\n%s\n", gTitle); + printf("%s\n\n\n", gMessageWin); } if ( aTitle && strlen(aTitle) ) { @@ -1460,7 +2370,7 @@ int tinyfd_messageBox ( { printf("%s\n\n",aMessage); } - printf("press any key to continue "); + printf("press enter to continue "); lChar = (char) _getch() ; printf("\n\n"); return 1 ; @@ -1476,31 +2386,36 @@ char const * tinyfd_inputBox( char const * const aDefaultInput ) /* "" , if NULL it's a passwordBox */ { static char lBuff [ MAX_PATH_OR_CMD ] ; + char * lEOF; -#ifndef TINYFD_WIN_CONSOLE_ONLY +#ifndef TINYFD_NOLIB 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_WIN_CONSOLE_ONLY */ +#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;} lBuff[0]='\0'; - if ( !gWarningDisplayed ) + if (!gWarningDisplayed && !tinyfd_forceConsole) { gWarningDisplayed = 1 ; - printf ("\n\n%s\n", gTitle); - printf ("%s\n\n\n", gMessageWin); + printf("\n\n%s", gAsciiArt); + printf("\n%s\n", gTitle); + printf("%s\n\n\n", gMessageWin); } if ( aTitle && strlen(aTitle) ) { @@ -1510,22 +2425,26 @@ char const * tinyfd_inputBox( { printf("%s\n",aMessage); } - printf("(esc+enter to cancel): "); -#ifndef TINYFD_WIN_CONSOLE_ONLY + printf("(ctrl-Z + enter to cancel): "); +#ifndef TINYFD_NOLIB if ( ! aDefaultInput ) { GetConsoleMode(hStdin,&mode); SetConsoleMode(hStdin,mode & (~ENABLE_ECHO_INPUT) ); } -#endif /* TINYFD_WIN_CONSOLE_ONLY */ - fgets(lBuff, MAX_PATH_OR_CMD, stdin); -#ifndef TINYFD_WIN_CONSOLE_ONLY +#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_WIN_CONSOLE_ONLY */ +#endif /* TINYFD_NOLIB */ printf ("\n"); if ( strchr(lBuff,27) ) { @@ -1551,21 +2470,24 @@ char const * tinyfd_saveFileDialog ( char lString[MAX_PATH_OR_CMD] ; char const * p ; lBuff[0]='\0'; -#ifndef TINYFD_WIN_CONSOLE_ONLY +#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;} p = saveFileDialogWinGui(lBuff, aTitle,aDefaultPathAndFile,aNumOfFilterPatterns,aFilterPatterns,aSingleFilterDescription); } else -#endif /* TINYFD_WIN_CONSOLE_ONLY */ +#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",""); } @@ -1596,25 +2518,28 @@ char const * tinyfd_openFileDialog ( char const * const aSingleFilterDescription , /* NULL or "image files" */ int const aAllowMultipleSelects ) /* 0 or 1 */ { - static char lBuff[MAX_MULTIPLE*MAX_PATH_OR_CMD]; + static char lBuff[MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD]; char const * p ; -#ifndef TINYFD_WIN_CONSOLE_ONLY +#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;} p = openFileDialogWinGui(lBuff, aTitle,aDefaultPathAndFile,aNumOfFilterPatterns, aFilterPatterns,aSingleFilterDescription,aAllowMultipleSelects); } else -#endif /* TINYFD_WIN_CONSOLE_ONLY */ +#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",""); } @@ -1641,20 +2566,23 @@ char const * tinyfd_selectFolderDialog ( { static char lBuff [ MAX_PATH_OR_CMD ] ; char const * p ; -#ifndef TINYFD_WIN_CONSOLE_ONLY +#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;} p = selectFolderDialogWinGui(lBuff,aTitle,aDefaultPath); } else -#endif /* TINYFD_WIN_CONSOLE_ONLY */ +#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",""); } @@ -1681,15 +2609,16 @@ char const * tinyfd_colorChooser( int i; char const * p ; -#ifndef TINYFD_WIN_CONSOLE_ONLY +#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;} return colorChooserWinGui( aTitle,aDefaultHexRGB,aDefaultRGB,aoResultRGB); } else -#endif /* TINYFD_WIN_CONSOLE_ONLY */ +#endif /* TINYFD_NOLIB */ if ( aDefaultHexRGB ) { lpDefaultHexRGB = (char *) aDefaultHexRGB ; @@ -1701,6 +2630,8 @@ char const * tinyfd_colorChooser( } p = tinyfd_inputBox(aTitle, "Enter hex rgb color (i.e. #f5ca20)",lpDefaultHexRGB); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")) return p; + if ( !p || (strlen(p) != 7) || (p[0] != '#') ) { return NULL ; @@ -1792,8 +2723,8 @@ static int tryCommand ( char const * const aCommand ) static char const * terminalName ( ) { static char lTerminalName[64] = "*" ; - if ( lTerminalName[0] == '*' ) - { + if ( lTerminalName[0] == '*' ) + { if ( isDarwin() ) { if ( strcpy(lTerminalName , "/opt/X11/bin/xterm" ) @@ -1806,182 +2737,98 @@ static char const * terminalName ( ) strcpy(lTerminalName , "" ) ; } } - else if ( strcpy(lTerminalName,"gnome-terminal") + else if ( strcpy(lTerminalName,"terminator") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -x bash -c " ) ; } - else if ( strcpy(lTerminalName,"konsole") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } - else if ( strcpy(lTerminalName,"xterm") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - //strcat(lTerminalName , " -e " ) ; - } - else if ( strcpy(lTerminalName,"lxterminal") + else if ( strcpy(lTerminalName,"lxterminal") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"xfce4-terminal") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -x bash -c " ) ; - } - else if ( strcpy(lTerminalName,"Terminal") + else if ( strcpy(lTerminalName,"mate-terminal") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -x bash -c " ) ; } - else if ( strcpy(lTerminalName,"rxvt") + else if ( strcpy(lTerminalName,"konsole") && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"urxvt") + else if ( strcpy(lTerminalName,"rxvt") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"mrxvt") + else if ( strcpy(lTerminalName,"urxvt") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"wterm") + else if ( strcpy(lTerminalName,"mrxvt") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"eterm") + else if ( strcpy(lTerminalName,"evilvte") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"aterm") + else if ( strcpy(lTerminalName,"termit") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"terminology") + else if ( strcpy(lTerminalName,"kterm") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"multi-gnome-terminal") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -x bash -c " ) ; - } - else if ( strcpy(lTerminalName,"hpterm") + else if ( strcpy(lTerminalName,"roxterm") /*good*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"winterm") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -c bash -c " ) ; - } - else if ( strcpy(lTerminalName,"roxterm") - && detectPresence(lTerminalName) ) + else if ( strcpy(lTerminalName,"xterm") /*good small*/ + && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"st") + else if ( strcpy(lTerminalName,"lxterm") /*good small*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"sakura") + else if ( strcpy(lTerminalName,"xvt") /*good B&W*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"mlterm") + else if ( strcpy(lTerminalName,"pterm") /*good only letters*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"vte") + else if ( strcpy(lTerminalName,"x-terminal-emulator") /*alias*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -e bash -c " ) ; } - else if ( strcpy(lTerminalName,"terminator") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -x bash -c " ) ; - } - else if ( strcpy(lTerminalName,"lilyterm") + else if ( strcpy(lTerminalName,"$TERM") /*alias*/ && detectPresence(lTerminalName) ) { strcat(lTerminalName , " -x bash -c " ) ; } - else if ( strcpy(lTerminalName,"dtterm") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } - else if ( strcpy(lTerminalName,"nxterm") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } - else if ( strcpy(lTerminalName,"pterm") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } - else if ( strcpy(lTerminalName,"xgterm") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } - else if ( strcpy(lTerminalName,"evilvte") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } - else if ( strcpy(lTerminalName,"kterm") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } - else if ( strcpy(lTerminalName,"xiterm") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } - else if ( strcpy(lTerminalName,"termit") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } - else if ( strcpy(lTerminalName,"xvt") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } - else if ( strcpy(lTerminalName,"vala-terminal") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } - else if ( strcpy(lTerminalName,"osso-xterm") - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e bash -c " ) ; - } else { strcpy(lTerminalName , "" ) ; } - } + /* bad: koi8rxterm xfce4-terminal gnome-terminal guake tilda + vala-terminal Eterm aterm Terminal terminology sakura lilyterm*/ + } if ( strlen(lTerminalName) ) { return lTerminalName ; @@ -1996,10 +2843,10 @@ static char const * terminalName ( ) static char const * dialogName ( ) { static char lDialogName[64] = "*" ; - if ( lDialogName[0] == '*' ) - { + if ( lDialogName[0] == '*' ) + { if ( isDarwin() && strcpy(lDialogName , "/opt/local/bin/dialog" ) - && detectPresence ( lDialogName ) ) + && detectPresence ( lDialogName ) ) {} else if ( strcpy(lDialogName , "dialog" ) && detectPresence ( lDialogName ) ) @@ -2008,7 +2855,7 @@ static char const * dialogName ( ) { strcpy(lDialogName , "" ) ; } - } + } if ( strlen(lDialogName) && ( isatty(1) || terminalName() ) ) { return lDialogName ; @@ -2022,11 +2869,11 @@ static char const * dialogName ( ) static int whiptailPresent ( ) { - static int lWhiptailPresent = -1 ; - if ( lWhiptailPresent < 0 ) - { - lWhiptailPresent = detectPresence ( "whiptail" ) ; - } + static int lWhiptailPresent = -1 ; + if ( lWhiptailPresent < 0 ) + { + lWhiptailPresent = detectPresence ( "whiptail" ) ; + } return lWhiptailPresent && ( isatty(1) || terminalName() ) ; } @@ -2040,12 +2887,12 @@ static int graphicMode() static int xmessagePresent ( ) { - static int lXmessagePresent = -1 ; - if ( lXmessagePresent < 0 ) - { - lXmessagePresent = detectPresence("xmessage");/*if not tty,not on osxpath*/ - } - return lXmessagePresent && graphicMode ( ) ; + static int lXmessagePresent = -1 ; + if ( lXmessagePresent < 0 ) + { + lXmessagePresent = detectPresence("xmessage");/*if not tty,not on osxpath*/ + } + return lXmessagePresent && graphicMode ( ) ; } @@ -2115,6 +2962,17 @@ static int kdialogPresent ( ) } +static int matedialogPresent ( ) +{ + static int lMatedialogPresent = -1 ; + if ( lMatedialogPresent < 0 ) + { + lMatedialogPresent = detectPresence("matedialog") ; + } + return lMatedialogPresent && graphicMode ( ) ; +} + + static int zenityPresent ( ) { static int lZenityPresent = -1 ; @@ -2126,13 +2984,40 @@ static int zenityPresent ( ) } +static int osx9orBetter ( ) +{ + static int lOsx9orBetter = -1 ; + char lBuff [ MAX_PATH_OR_CMD ] ; + FILE * lIn ; + int V,v; + + if ( lOsx9orBetter < 0 ) + { + lOsx9orBetter = 0 ; + lIn = popen ( "osascript -e 'set osver to system version of (system info)'" , "r" ) ; + if ( ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL ) + && ( 2 == sscanf(lBuff, "%d.%d", &V, &v) ) ) + { + V = V * 100 + v; + if ( V >= 1009 ) + { + lOsx9orBetter = 1 ; + } + } + pclose ( lIn ) ; + /* printf ("Osx10 = %d, %d = <%s>\n", lOsx9orBetter, V, lBuff) ; //*/ + } + return lOsx9orBetter ; +} + + + static int zenity3Present ( ) { - static int lZenity3Present = -1 ; - char lBuff [ MAX_PATH_OR_CMD ] ; - FILE * lIn ; + static int lZenity3Present = -1 ; + char lBuff [ MAX_PATH_OR_CMD ] ; + FILE * lIn ; - if ( lZenity3Present < 0 ) { if ( ! zenityPresent() ) @@ -2155,7 +3040,7 @@ static int zenity3Present ( ) pclose ( lIn ) ; } } - return lZenity3Present && graphicMode ( ) ; + return lZenity3Present && graphicMode ( ) ; } @@ -2208,31 +3093,35 @@ int tinyfd_messageBox ( char const * const aIconType , /* "info" "warning" "error" "question" */ int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes */ { - char lBuff [ MAX_PATH_OR_CMD ] ; - char lDialogString [ MAX_PATH_OR_CMD ] ; - FILE * lIn ; + char lBuff [ MAX_PATH_OR_CMD ] ; + char lDialogString [ MAX_PATH_OR_CMD ] ; + FILE * lIn ; int lWasGraphicDialog = 0 ; int lWasXterm = 0 ; - int lResult ; + int lResult ; char lChar ; struct termios infoOri; struct termios info; lBuff[0]='\0'; if ( osascriptPresent ( ) ) - { - strcpy ( lDialogString , "osascript -e 'try' -e 'display dialog \"") ; - if ( aMessage && strlen(aMessage) ) - { + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;} + + strcpy ( lDialogString , "osascript "); + if ( ! osx9orBetter() ) strcat ( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat ( lDialogString , " -e 'try' -e 'display dialog \"") ; + if ( aMessage && strlen(aMessage) ) + { strcat(lDialogString, aMessage) ; - } + } strcat(lDialogString, "\" ") ; - if ( aTitle && strlen(aTitle) ) - { + if ( aTitle && strlen(aTitle) ) + { strcat(lDialogString, "with title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; - } + } strcat(lDialogString, "with icon ") ; if ( aIconType && ! strcmp( "error" , aIconType ) ) { @@ -2270,38 +3159,48 @@ int tinyfd_messageBox ( { strcat ( lDialogString ,"buttons {\"OK\"} " ) ; strcat ( lDialogString ,"default button \"OK\" " ) ; - } - strcat(lDialogString, "' ") ; - strcat(lDialogString, "-e '1' " ); - strcat(lDialogString, "-e 'on error number -128' " ) ; - strcat(lDialogString, "-e '0' " ); - strcat(lDialogString, "-e 'end try'") ; + strcat ( lDialogString, "' ") ; + strcat ( lDialogString, "-e '1' " ); + strcat ( lDialogString, "-e 'on error number -128' " ) ; + strcat ( lDialogString, "-e '0' " ); + strcat ( lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat ( lDialogString, " -e 'end tell'") ; } - else if ( zenityPresent() ) - { - strcpy ( lDialogString , "zenity --" ) ; - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - strcat ( lDialogString , - "question --ok-label=Ok --cancel-label=Cancel" ) ; - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - strcat ( lDialogString , "question" ) ; - } - else if ( aIconType && ! strcmp( "error" , aIconType ) ) + else if ( zenityPresent() || matedialogPresent() ) + { + if ( zenityPresent() ) { - strcat ( lDialogString , "error" ) ; - } - else if ( aIconType && ! strcmp( "warning" , aIconType ) ) + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;} + strcpy ( lDialogString , "zenity --" ) ; + } + else { - strcat ( lDialogString , "warning" ) ; - } - else + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return 1;} + strcpy ( lDialogString , "matedialog --" ) ; + } + + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) { - strcat ( lDialogString , "info" ) ; - } + strcat ( lDialogString , + "question --ok-label=Ok --cancel-label=Cancel" ) ; + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + strcat ( lDialogString , "question" ) ; + } + 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=\"") ; @@ -2325,13 +3224,15 @@ int tinyfd_messageBox ( } else { - strcat ( lDialogString , "info" ) ; + strcat ( lDialogString , "information" ) ; } } strcat ( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi"); } else if ( kdialogPresent() ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} + strcpy ( lDialogString , "kdialog --" ) ; if ( aDialogType && ( ! strcmp( "okcancel" , aDialogType ) || ! strcmp( "yesno" , aDialogType ) ) ) @@ -2376,11 +3277,13 @@ int tinyfd_messageBox ( } else if ( ! xdialogPresent() && tkinter2Present ( ) ) { - strcpy ( lDialogString , gPython2Name ) ; - if ( ! isatty ( 1 ) && isDarwin ( ) ) - { - strcat ( lDialogString , " -i" ) ; /* for osx without console */ - } + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return 1;} + + strcpy ( lDialogString , gPython2Name ) ; + if ( ! isatty ( 1 ) && isDarwin ( ) ) + { + strcat ( lDialogString , " -i" ) ; /* for osx without console */ + } strcat ( lDialogString , " -c \"import Tkinter,tkMessageBox;root=Tkinter.Tk();root.withdraw();"); @@ -2393,10 +3296,10 @@ frontmost of process \\\"Python\\\" to true' ''');"); } strcat ( lDialogString ,"res=tkMessageBox." ) ; - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - strcat ( lDialogString , "askokcancel(" ) ; - if ( aDefaultButton ) + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + strcat ( lDialogString , "askokcancel(" ) ; + if ( aDefaultButton ) { strcat ( lDialogString , "default=tkMessageBox.OK," ) ; } @@ -2453,17 +3356,20 @@ else :\n\tprint 1\n\"" ) ; } else if (!xdialogPresent() && !gdialogPresent() && gxmessagePresent() ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return 1;} + strcpy ( lDialogString , "gxmessage"); + if ( aDialogType && ! strcmp("okcancel" , aDialogType) ) { - strcpy ( lDialogString , " -buttons Ok:1,Cancel:0"); + strcat ( lDialogString , " -buttons Ok:1,Cancel:0"); } else if ( aDialogType && ! strcmp("yesno" , aDialogType) ) { - strcpy ( lDialogString , " -buttons Yes:1,No:0"); + strcat ( lDialogString , " -buttons Yes:1,No:0"); } - strcpy ( lDialogString , " -center \""); + strcat ( lDialogString , " -center \""); if ( aMessage && strlen(aMessage) ) { strcat ( lDialogString , aMessage ) ; @@ -2473,13 +3379,16 @@ else :\n\tprint 1\n\"" ) ; { strcat ( lDialogString , " -title \""); strcat ( lDialogString , aTitle ) ; - strcat(lDialogString, "\"" ) ; + strcat ( lDialogString, "\"" ) ; } + strcat ( lDialogString , " ; echo $? "); } else if (!xdialogPresent() && !gdialogPresent() && notifysendPresent() && strcmp("okcancel" , aDialogType) && strcmp("yesno" , aDialogType) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notify");return 1;} + strcpy ( lDialogString , "notify-send \"" ) ; if ( aTitle && strlen(aTitle) ) { @@ -2496,6 +3405,8 @@ else :\n\tprint 1\n\"" ) ; && strcmp("okcancel" , aDialogType) && strcmp("yesno" , aDialogType) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xmessage");return 1;} + strcpy ( lDialogString , "xmessage -center \""); if ( aTitle && strlen(aTitle) ) { @@ -2513,16 +3424,19 @@ else :\n\tprint 1\n\"" ) ; { if ( xdialogPresent ( ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return 1;} lWasGraphicDialog = 1 ; strcpy ( lDialogString , "(Xdialog " ) ; } else if ( gdialogPresent ( ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return 1;} lWasGraphicDialog = 1 ; strcpy ( lDialogString , "(gdialog " ) ; } else if ( dialogName ( ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return 0;} if ( isatty ( 1 ) ) { strcpy ( lDialogString , "(dialog " ) ; @@ -2538,10 +3452,12 @@ else :\n\tprint 1\n\"" ) ; } else if ( isatty ( 1 ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;} strcpy ( lDialogString , "(whiptail " ) ; } else { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;} lWasXterm = 1 ; strcpy ( lDialogString , terminalName() ) ; strcat ( lDialogString , "'(whiptail " ) ; @@ -2549,15 +3465,19 @@ else :\n\tprint 1\n\"" ) ; if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) ) ) { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, "tab =move focus") ; - strcat(lDialogString, "\" ") ; + if ( !xdialogPresent() && !gdialogPresent() ) + { + if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) ) ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab -> move focus") ; + strcat(lDialogString, "\" ") ; + } } if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) @@ -2611,12 +3531,16 @@ cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); } else if ( ! isatty ( 1 ) && terminalName() ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;} strcpy ( lDialogString , terminalName() ) ; strcat ( lDialogString , "'" ) ; - if ( !gWarningDisplayed ) + if ( !gWarningDisplayed && !tinyfd_forceConsole) { gWarningDisplayed = 1 ; strcat ( lDialogString , "echo \"" ) ; + strcat ( lDialogString, gAsciiArt) ; + strcat ( lDialogString , " \";" ) ; + strcat ( lDialogString , "echo \"" ) ; strcat ( lDialogString, gTitle) ; strcat ( lDialogString , "\";" ) ; strcat ( lDialogString , "echo \"" ) ; @@ -2657,7 +3581,7 @@ cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); } else { - strcat(lDialogString , "echo -n \"press any key to continue \"; "); + strcat(lDialogString , "echo -n \"press enter to continue \"; "); strcat ( lDialogString , "stty raw -echo;" ) ; strcat ( lDialogString , "answer=$( while ! head -c 1;do true ;done);echo 1"); @@ -2667,10 +3591,12 @@ cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); } else { - if ( !gWarningDisplayed ) + 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("\n\n%s", gAsciiArt); + printf ("\n%s\n", gTitle); printf ("%s\n\n\n", gMessageUnix); } if ( aTitle && strlen(aTitle) ) @@ -2692,7 +3618,7 @@ cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); { printf("%s\n",aMessage); } - printf("y/n: "); + printf("y/n: "); fflush(stdout); lChar = tolower ( getchar() ) ; printf("\n\n"); } @@ -2707,7 +3633,7 @@ cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); { printf("%s\n",aMessage); } - printf("[O]kay/[C]ancel: "); + printf("[O]kay/[C]ancel: "); fflush(stdout); lChar = tolower ( getchar() ) ; printf("\n\n"); } @@ -2720,9 +3646,9 @@ cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); { printf("%s\n\n",aMessage); } - printf("press any key to continue "); + printf("press enter to continue "); fflush(stdout); getchar() ; - printf("\n\n"); + printf("\n\n"); lResult = 1 ; } tcsetattr(0, TCSANOW, &infoOri); @@ -2736,14 +3662,17 @@ cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); } while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL ) {} + pclose ( lIn ) ; + /* printf ( "lBuff: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; //*/ if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) { lBuff[ strlen ( lBuff ) -1 ] = '\0' ; } /* printf ( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; //*/ - lResult = strcmp ( lBuff , "1" ) ? 0 : 1 ; + + lResult = strcmp ( lBuff , "1" ) ? 0 : 1 ; /* printf ( "lResult: %d\n" , lResult ) ; //*/ return lResult ; } @@ -2765,15 +3694,19 @@ char const * tinyfd_inputBox( int lWasBasicXterm = 0 ; struct termios oldt ; struct termios newt ; + char * lEOF; lBuff[0]='\0'; - if ( osascriptPresent ( ) ) + if ( osascriptPresent ( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} + strcpy ( lDialogString , "osascript "); + if ( ! osx9orBetter() ) strcat ( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat ( lDialogString , " -e 'try' -e 'display dialog \"") ; + if ( aMessage && strlen(aMessage) ) { - strcpy ( lDialogString , "osascript -e 'try' -e 'display dialog \"") ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } + strcat(lDialogString, aMessage) ; + } strcat(lDialogString, "\" ") ; strcat(lDialogString, "default answer \"") ; if ( aDefaultInput && strlen(aDefaultInput) ) @@ -2786,20 +3719,31 @@ char const * tinyfd_inputBox( strcat(lDialogString, "hidden answer true ") ; } if ( aTitle && strlen(aTitle) ) - { + { strcat(lDialogString, "with title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; - } + } strcat(lDialogString, "with icon note' ") ; strcat(lDialogString, "-e '\"1\" & text returned of result' " ); strcat(lDialogString, "-e 'on error number -128' " ) ; strcat(lDialogString, "-e '0' " ); strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat(lDialogString, " -e 'end tell'") ; } - else if ( zenityPresent() ) - { - strcpy ( lDialogString , "szAnswer=$(zenity --entry" ) ; + else if ( zenityPresent() || matedialogPresent() ) + { + if ( zenityPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} + strcpy ( lDialogString , "szAnswer=$(zenity --entry" ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;} + strcpy ( lDialogString , "szAnswer=$(matedialog --entry" ) ; + } + if ( aTitle && strlen(aTitle) ) { strcat(lDialogString, " --title=\"") ; @@ -2812,14 +3756,11 @@ char const * tinyfd_inputBox( strcat(lDialogString, aMessage) ; strcat(lDialogString, "\"") ; } - if ( aDefaultInput ) + if ( aDefaultInput && strlen(aDefaultInput) ) { - if ( strlen(aDefaultInput) ) - { - strcat(lDialogString, " --entry-text=\"") ; - strcat(lDialogString, aDefaultInput) ; - strcat(lDialogString, "\"") ; - } + strcat(lDialogString, " --entry-text=\"") ; + strcat(lDialogString, aDefaultInput) ; + strcat(lDialogString, "\"") ; } else { @@ -2830,6 +3771,7 @@ char const * tinyfd_inputBox( } else if ( kdialogPresent() ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} strcpy ( lDialogString , "szAnswer=$(kdialog" ) ; if ( ! aDefaultInput ) { @@ -2842,7 +3784,6 @@ char const * tinyfd_inputBox( } strcat(lDialogString, "\"") ; if ( aMessage && strlen(aMessage) ) - { strcat(lDialogString, aMessage ) ; } @@ -2863,6 +3804,7 @@ char const * tinyfd_inputBox( } else if ( ! xdialogPresent() && tkinter2Present ( ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;} strcpy ( lDialogString , gPython2Name ) ; if ( ! isatty ( 1 ) && isDarwin ( ) ) { @@ -2912,7 +3854,9 @@ frontmost of process \\\"Python\\\" to true' ''');"); } else if (!xdialogPresent() && !gdialogPresent() && gxmessagePresent() ) { - strcpy ( lDialogString , "gxmessage -buttons Ok:1,Cancel:0 -center \""); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return (char const *)1;} + strcpy ( lDialogString , "szAnswer=$(gxmessage -buttons Ok:1,Cancel:0 -center \""); + if ( aMessage && strlen(aMessage) ) { strcat ( lDialogString , aMessage ) ; @@ -2930,23 +3874,27 @@ frontmost of process \\\"Python\\\" to true' ''');"); strcat ( lDialogString , aDefaultInput ) ; } strcat(lDialogString, "\"" ) ; + strcat ( lDialogString , ");echo $?$szAnswer"); } else if ( xdialogPresent() || gdialogPresent() || dialogName() || whiptailPresent() ) { if ( xdialogPresent ( ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} lWasGraphicDialog = 1 ; strcpy ( lDialogString , "(Xdialog " ) ; } else if ( gdialogPresent ( ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return (char const *)1;} lWasGraphicDialog = 1 ; lWasGdialog = 1 ; strcpy ( lDialogString , "(gdialog " ) ; } else if ( dialogName ( ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} if ( isatty ( 1 ) ) { strcpy ( lDialogString , "(dialog " ) ; @@ -2962,10 +3910,12 @@ frontmost of process \\\"Python\\\" to true' ''');"); } else if ( isatty ( 1 ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char const *)0;} strcpy ( lDialogString , "(whiptail " ) ; } else { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char const *)0;} lWasXterm = 1 ; strcpy ( lDialogString , terminalName() ) ; strcat ( lDialogString , "'(whiptail " ) ; @@ -2974,15 +3924,18 @@ frontmost of process \\\"Python\\\" to true' ''');"); if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, "tab =move focus") ; - strcat(lDialogString, "\" ") ; - + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab -> move focus") ; + strcat(lDialogString, "\" ") ; + } + if ( aDefaultInput || lWasGdialog ) { strcat ( lDialogString , "--inputbox" ) ; @@ -3029,20 +3982,24 @@ frontmost of process \\\"Python\\\" to true' ''');"); } else if ( ! isatty ( 1 ) && terminalName() ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} lWasBasicXterm = 1 ; strcpy ( lDialogString , terminalName() ) ; strcat ( lDialogString , "'" ) ; - if ( !gWarningDisplayed ) + if ( !gWarningDisplayed && !tinyfd_forceConsole) { gWarningDisplayed = 1 ; strcat ( lDialogString , "echo \"" ) ; + strcat ( lDialogString, gAsciiArt) ; + strcat ( lDialogString , "\";" ) ; + strcat ( lDialogString , "echo \"" ) ; strcat ( lDialogString, gTitle) ; strcat ( lDialogString , "\";" ) ; strcat ( lDialogString , "echo \"" ) ; strcat ( lDialogString, gMessageUnix) ; strcat ( lDialogString , "\";echo;echo;" ) ; } - if ( aTitle && strlen(aTitle) ) + if ( aTitle && strlen(aTitle) && !tinyfd_forceConsole) { strcat ( lDialogString , "echo \"" ) ; strcat ( lDialogString, aTitle) ; @@ -3060,16 +4017,18 @@ frontmost of process \\\"Python\\\" to true' ''');"); strcat ( lDialogString , "-s " ) ; } strcat ( lDialogString , "-p \"" ) ; - strcat(lDialogString , "(esc+enter to cancel): \" ANSWER " ) ; - strcat(lDialogString , ";echo 1$ANSWER >/tmp/tinyfd.txt';" ) ; - strcat(lDialogString , "cat -v /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); + strcat ( lDialogString , "(esc+enter to cancel): \" ANSWER " ) ; + strcat ( lDialogString , ";echo 1$ANSWER >/tmp/tinyfd.txt';" ) ; + strcat ( lDialogString , "cat -v /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); } - else if ( isatty ( 1 ) ) + else { - if ( !gWarningDisplayed ) + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} + if ( !gWarningDisplayed && !tinyfd_forceConsole) { gWarningDisplayed = 1 ; - printf ("\n\n%s\n", gTitle); + printf ("\n\n%s", gAsciiArt); + printf ("\n%s\n", gTitle); printf ("%s\n\n\n", gMessageUnix); } if ( aTitle && strlen(aTitle) ) @@ -3080,7 +4039,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); { printf("%s\n",aMessage); } - printf("(esc+enter to cancel): "); + printf("(esc+enter to cancel): "); fflush(stdout); if ( ! aDefaultInput ) { tcgetattr(STDIN_FILENO, & oldt) ; @@ -3088,7 +4047,24 @@ frontmost of process \\\"Python\\\" to true' ''');"); newt.c_lflag &= ~ECHO ; tcsetattr(STDIN_FILENO, TCSANOW, & newt); } - fgets(lBuff, MAX_PATH_OR_CMD, stdin); + + lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); + /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); //*/ + if ( ! lEOF || (lBuff[0] == '\0') ) + { + return NULL; + } + + if ( lBuff[0] == '\n' ) + { + lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); + /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); //*/ + if ( ! lEOF || (lBuff[0] == '\0') ) + { + return NULL; + } + } + if ( ! aDefaultInput ) { tcsetattr(STDIN_FILENO, TCSANOW, & oldt); @@ -3105,15 +4081,6 @@ frontmost of process \\\"Python\\\" to true' ''');"); } return lBuff ; } - else - { - if ( !gWarningDisplayed ) - { - gWarningDisplayed = 1 ; - tinyfd_messageBox ( gTitle , gMessageUnix , "ok", "error" , 1 ) ; - } - return NULL ; - } /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) @@ -3122,7 +4089,9 @@ frontmost of process \\\"Python\\\" to true' ''');"); } while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL ) {} + pclose ( lIn ) ; + /* printf ( "len Buff: %lu\n" , strlen(lBuff) ) ; //*/ /* printf ( "lBuff0: %s\n" , lBuff ) ; //*/ if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) @@ -3137,6 +4106,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); return NULL ; } } + lResult = strncmp ( lBuff , "1" , 1) ? 0 : 1 ; /* printf ( "lResult: %d \n" , lResult ) ; //*/ if ( ! lResult ) @@ -3155,21 +4125,23 @@ char const * tinyfd_saveFileDialog ( char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ char const * 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 ; - DIR * lDir ; - FILE * lIn ; + FILE * lIn ; lBuff[0]='\0'; if ( osascriptPresent ( ) ) { - strcpy ( lDialogString , - "osascript -e 'POSIX path of ( choose file name " ); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)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 " ); if ( aTitle && strlen(aTitle) ) { strcat(lDialogString, "with prompt \"") ; @@ -3190,12 +4162,26 @@ char const * tinyfd_saveFileDialog ( strcat(lDialogString, lString ) ; strcat(lDialogString , "\" " ) ; } - strcat ( lDialogString , ")'" ) ; + strcat ( lDialogString , ")' " ) ; + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat ( lDialogString, " -e 'end tell'") ; } - else if ( zenityPresent() ) - { - strcpy ( lDialogString , - "zenity --file-selection --save --confirm-overwrite" ) ; + else if ( zenityPresent() || matedialogPresent() ) + { + if ( zenityPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} + strcpy ( lDialogString , "zenity" ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;} + strcpy ( lDialogString , "matedialog" ) ; + + } + strcat(lDialogString, " --file-selection --save --confirm-overwrite" ) ; + if ( aTitle && strlen(aTitle) ) { strcat(lDialogString, " --title=\"") ; @@ -3223,12 +4209,13 @@ char const * tinyfd_saveFileDialog ( } strcat ( lDialogString , "' --file-filter='All files | *'" ) ; } - } - else if ( kdialogPresent() ) - { + } + else if ( kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} strcpy ( lDialogString , "kdialog --getsavefilename" ) ; - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { strcat(lDialogString, " \"") ; strcat(lDialogString, aDefaultPathAndFile ) ; strcat(lDialogString , "\"" ) ; @@ -3237,31 +4224,31 @@ char const * tinyfd_saveFileDialog ( { strcat(lDialogString, " :" ) ; } - if ( aNumOfFilterPatterns > 0 ) - { - strcat(lDialogString , " \"" ) ; - for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat ( lDialogString , aFilterPatterns [ i ] ) ; - strcat ( lDialogString , " " ) ; - } - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) - { - strcat ( lDialogString , " | " ) ; - strcat ( lDialogString , aSingleFilterDescription ) ; - } - strcat ( lDialogString , "\"" ) ; - } - if ( aTitle && strlen(aTitle) ) - { + if ( aNumOfFilterPatterns > 0 ) + { + strcat(lDialogString , " \"" ) ; + for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat ( lDialogString , aFilterPatterns [ i ] ) ; + strcat ( lDialogString , " " ) ; + } + if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) + { + strcat ( lDialogString , " | " ) ; + strcat ( lDialogString , aSingleFilterDescription ) ; + } + strcat ( lDialogString , "\"" ) ; + } + if ( aTitle && strlen(aTitle) ) + { strcat(lDialogString, " --title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\"") ; - } } - else if ( ! xdialogPresent() && tkinter2Present ( ) ) - { - + } + else if ( ! xdialogPresent() && tkinter2Present ( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;} strcpy ( lDialogString , gPython2Name ) ; if ( ! isatty ( 1 ) && isDarwin ( )) { @@ -3327,15 +4314,18 @@ char const * tinyfd_saveFileDialog ( { if ( xdialogPresent ( ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} lWasGraphicDialog = 1 ; strcpy ( lDialogString , "(Xdialog " ) ; } else if ( isatty ( 1 ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} strcpy ( lDialogString , "@echo lala;(dialog " ) ; } else { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} lWasXterm = 1 ; strcpy ( lDialogString , terminalName() ) ; strcat ( lDialogString , "'(" ) ; @@ -3345,14 +4335,18 @@ char const * tinyfd_saveFileDialog ( if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; - strcat(lDialogString, "\" ") ; + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab -> focus | spacebar -> select | / -> populate | enter -> ok input line") ; + strcat(lDialogString, "\" ") ; + } strcat ( lDialogString , "--fselect \"" ) ; if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) @@ -3393,6 +4387,7 @@ 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 ) ; if ( strlen ( lString ) && ! dirExists ( lString ) ) @@ -3447,23 +4442,28 @@ char const * tinyfd_openFileDialog ( char const * const aSingleFilterDescription , /* NULL or "image files" */ int const aAllowMultipleSelects ) /* 0 or 1 */ { - static char lBuff [ MAX_MULTIPLE*MAX_PATH_OR_CMD ] ; - char lDialogString [ MAX_PATH_OR_CMD ] ; + 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 ; + FILE * lIn ; char * p ; char const * p2 ; int lWasKdialog = 0 ; int lWasGraphicDialog = 0 ; int lWasXterm = 0 ; - lBuff[0]='\0'; + lBuff[0]='\0'; - if ( osascriptPresent ( ) ) + if ( osascriptPresent ( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} + strcpy ( lDialogString , "osascript "); + if ( ! osx9orBetter() ) strcat ( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat ( lDialogString , " -e 'try' -e '" ); + if ( ! aAllowMultipleSelects ) { - strcpy ( lDialogString , "osascript -e '" ); - if ( ! aAllowMultipleSelects ) - { + + strcat ( lDialogString , "POSIX path of ( " ); } else @@ -3508,16 +4508,29 @@ char const * tinyfd_openFileDialog ( strcat ( lDialogString , "-e 'set mystring to mystring & POSIX path of item i of mylist' " ); strcat ( lDialogString , "-e 'end repeat' " ); - strcat ( lDialogString , "-e 'mystring'" ); + strcat ( lDialogString , "-e 'mystring' " ); } else { - strcat ( lDialogString , ")'" ) ; + strcat ( lDialogString , ")' " ) ; } - } - else if ( zenityPresent() ) - { - strcpy ( lDialogString ,"zenity --file-selection" ) ; + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat ( lDialogString, " -e 'end tell'") ; + } + else if ( zenityPresent() || matedialogPresent() ) + { + if ( zenityPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} + strcpy ( lDialogString , "zenity --file-selection" ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;} + strcpy ( lDialogString , "matedialog --file-selection" ) ; + } + if ( aAllowMultipleSelects ) { strcat ( lDialogString , " --multiple" ) ; @@ -3534,24 +4547,25 @@ char const * tinyfd_openFileDialog ( strcat(lDialogString, aDefaultPathAndFile) ; strcat(lDialogString, "\"") ; } - if ( aNumOfFilterPatterns > 0 ) - { - strcat ( lDialogString , " --file-filter='" ) ; + if ( aNumOfFilterPatterns > 0 ) + { + strcat ( lDialogString , " --file-filter='" ) ; if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) { strcat ( lDialogString , aSingleFilterDescription ) ; strcat ( lDialogString , " | " ) ; } - for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat ( lDialogString , aFilterPatterns [ i ] ) ; - strcat ( lDialogString , " " ) ; - } - strcat ( lDialogString , "' --file-filter='All files | *'" ) ; - } - } + for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat ( lDialogString , aFilterPatterns [ i ] ) ; + strcat ( lDialogString , " " ) ; + } + strcat ( lDialogString , "' --file-filter='All files | *'" ) ; + } + } else if ( kdialogPresent() ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} lWasKdialog = 1 ; strcpy ( lDialogString , "kdialog --getopenfilename" ) ; if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) @@ -3591,35 +4605,36 @@ char const * tinyfd_openFileDialog ( strcat(lDialogString, "\"") ; } } - else if ( ! xdialogPresent() && tkinter2Present ( ) ) - { + else if ( ! xdialogPresent() && tkinter2Present ( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;} strcpy ( lDialogString , gPython2Name ) ; if ( ! isatty ( 1 ) && isDarwin ( ) ) { strcat ( lDialogString , " -i" ) ; /* for osx without console */ } - strcat ( lDialogString , + strcat ( lDialogString , " -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();"); - if ( isDarwin ( ) ) - { + if ( isDarwin ( ) ) + { strcat ( lDialogString , "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \ frontmost of process \\\"Python\\\" to true' ''');"); } strcat ( lDialogString , "lFiles=tkFileDialog.askopenfilename("); - if ( aAllowMultipleSelects ) - { - strcat ( lDialogString , "multiple=1," ) ; - } - if ( aTitle && strlen(aTitle) ) - { + if ( aAllowMultipleSelects ) + { + strcat ( lDialogString , "multiple=1," ) ; + } + if ( aTitle && strlen(aTitle) ) + { strcat(lDialogString, "title='") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "',") ; - } - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { + } + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { getPathWithoutFinalSlash ( lString , aDefaultPathAndFile ) ; if ( strlen(lString) ) { @@ -3635,10 +4650,10 @@ frontmost of process \\\"Python\\\" to true' ''');"); strcat(lDialogString , "'," ) ; } } - if ( ( aNumOfFilterPatterns > 1 ) - || ( ( aNumOfFilterPatterns == 1 ) /*test because poor osx behaviour*/ + 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) ) @@ -3654,25 +4669,28 @@ frontmost of process \\\"Python\\\" to true' ''');"); } 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;} lWasGraphicDialog = 1 ; strcpy ( lDialogString , "(Xdialog " ) ; } else if ( isatty ( 1 ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} strcpy ( lDialogString , "(dialog " ) ; } else { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} lWasXterm = 1 ; strcpy ( lDialogString , terminalName() ) ; strcat ( lDialogString , "'(" ) ; @@ -3682,14 +4700,18 @@ frontmost of process \\\"Python\\\" to true' ''');"); if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; - strcat(lDialogString, "\" ") ; + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab -> focus | spacebar -> select | / -> populate | enter -> ok input line") ; + strcat(lDialogString, "\" ") ; + } strcat ( lDialogString , "--fselect \"" ) ; if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) @@ -3730,6 +4752,7 @@ 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) ) { @@ -3738,7 +4761,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); return p2 ; } - /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ + /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) { return NULL ; @@ -3786,38 +4809,52 @@ frontmost of process \\\"Python\\\" to true' ''');"); char const * tinyfd_selectFolderDialog ( char const * const aTitle , /* "" */ - char const * const aDefaultPath ) /* "" */ { static char lBuff [ MAX_PATH_OR_CMD ] ; - char lDialogString [ MAX_PATH_OR_CMD ] ; - DIR * lDir ; + char lDialogString [ MAX_PATH_OR_CMD ] ; FILE * lIn ; char const * p ; int lWasGraphicDialog = 0 ; int lWasXterm = 0 ; - lBuff[0]='\0'; + lBuff[0]='\0'; - if ( osascriptPresent ( )) - { - strcpy(lDialogString ,"osascript -e 'POSIX path of ( choose folder "); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with prompt \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } + if ( osascriptPresent ( )) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)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 "); + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with prompt \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } if ( aDefaultPath && strlen(aDefaultPath) ) { strcat(lDialogString, "default location \"") ; strcat(lDialogString, aDefaultPath ) ; strcat(lDialogString , "\" " ) ; } - strcat ( lDialogString , ")'" ) ; - } - else if ( zenityPresent() ) - { - strcpy ( lDialogString , "zenity --file-selection --directory" ) ; + strcat ( lDialogString , ")' " ) ; + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat ( lDialogString, " -e 'end tell'") ; + } + else if ( zenityPresent() || matedialogPresent() ) + { + if ( zenityPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} + strcpy ( lDialogString , "zenity --file-selection --directory" ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;} + strcpy ( lDialogString , "matedialog --file-selection --directory" ) ; + } + if ( aTitle && strlen(aTitle) ) { strcat(lDialogString, " --title=\"") ; @@ -3833,6 +4870,7 @@ char const * tinyfd_selectFolderDialog ( } else if ( kdialogPresent() ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} strcpy ( lDialogString , "kdialog --getexistingdirectory" ) ; if ( aDefaultPath && strlen(aDefaultPath) ) { @@ -3853,6 +4891,7 @@ char const * tinyfd_selectFolderDialog ( } else if ( ! xdialogPresent() && tkinter2Present ( ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;} strcpy ( lDialogString , gPython2Name ) ; if ( ! isatty ( 1 ) && isDarwin ( ) ) { @@ -3887,15 +4926,18 @@ frontmost of process \\\"Python\\\" to true' ''');"); { if ( xdialogPresent ( ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} lWasGraphicDialog = 1 ; strcpy ( lDialogString , "(Xdialog " ) ; } else if ( isatty ( 1 ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} strcpy ( lDialogString , "(dialog " ) ; } else { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} lWasXterm = 1 ; strcpy ( lDialogString , terminalName() ) ; strcat ( lDialogString , "'(" ) ; @@ -3905,14 +4947,18 @@ frontmost of process \\\"Python\\\" to true' ''');"); if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "--title \"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\" ") ; } - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, "tab =move focus | spacebar =select | add / =populate") ; - strcat(lDialogString, "\" ") ; + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab -> focus | spacebar -> select | / -> populate | enter -> ok input line") ; + strcat(lDialogString, "\" ") ; + } strcat ( lDialogString , "--dselect \"" ) ; if ( aDefaultPath && strlen(aDefaultPath) ) @@ -3950,6 +4996,7 @@ 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",""); if ( !p || ! strlen ( p ) || ! dirExists ( p ) ) { @@ -4017,11 +5064,22 @@ char const * tinyfd_colorChooser( } if ( osascriptPresent ( ) ) - { - lWasOsascript = 1 ; - strcpy ( lDialogString , "osascript -e 'tell app (path to frontmost \ -application as Unicode text) to set mycolor to choose color default color {"); + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} + lWasOsascript = 1 ; + strcpy ( lDialogString , "osascript"); + if ( ! osx9orBetter() ) + { + strcat ( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat ( lDialogString , " -e 'try' -e 'set mycolor to choose color default color {"); + } + else + { + strcat ( lDialogString , " -e 'try' -e 'tell app (path to frontmost application as Unicode text) to set mycolor to choose color default color {"); + } + sprintf(lTmp, "%d", 256 * lDefaultRGB[0] ) ; + strcat(lDialogString, lTmp ) ; strcat(lDialogString, "," ) ; sprintf(lTmp, "%d", 256 * lDefaultRGB[1] ) ; @@ -4038,13 +5096,27 @@ application as Unicode text) to set mycolor to choose color default color {"); "-e 'set mystring to mystring & \" \" & \ ((item i of mycolor)/256 as integer) as string' " ); strcat ( lDialogString , "-e 'end repeat' " ); - strcat ( lDialogString , "-e 'mystring'"); - } - else if ( zenity3Present() ) - { + strcat ( lDialogString , "-e 'mystring' "); + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat ( lDialogString, " -e 'end tell'") ; + } + else if ( zenity3Present() || matedialogPresent() ) + { lWasZenity3 = 1 ; - sprintf ( lDialogString , + if ( zenity3Present() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity3");return (char const *)1;} + sprintf ( lDialogString , "zenity --color-selection --show-palette --color=%s" , lpDefaultHexRGB ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char const *)1;} + sprintf ( lDialogString , +"matedialog --color-selection --show-palette --color=%s" , lpDefaultHexRGB ) ; + } + if ( aTitle && strlen(aTitle) ) { strcat(lDialogString, " --title=\"") ; @@ -4054,7 +5126,8 @@ application as Unicode text) to set mycolor to choose color default color {"); } else if ( kdialogPresent() ) { - sprintf ( lDialogString , + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} + sprintf ( lDialogString , "kdialog --getcolor --default '%s'" , lpDefaultHexRGB ) ; if ( aTitle && strlen(aTitle) ) { @@ -4065,6 +5138,7 @@ application as Unicode text) to set mycolor to choose color default color {"); } else if ( xdialogPresent() ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} lWasXdialog = 1 ; strcpy ( lDialogString , "Xdialog --colorsel \"" ) ; if ( aTitle && strlen(aTitle) ) @@ -4079,6 +5153,7 @@ application as Unicode text) to set mycolor to choose color default color {"); } else if ( tkinter2Present ( ) ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;} strcpy ( lDialogString , gPython2Name ) ; if ( ! isatty ( 1 ) && isDarwin ( ) ) { @@ -4111,6 +5186,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); } 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); if ( !p || (strlen(p) != 7) || (p[0] != '#') ) @@ -4189,6 +5265,7 @@ char const * tinyfd_arrayDialog ( if ( zenityPresent() ) { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} strcpy ( lDialogString , "zenity --list --print-column=ALL" ) ; if ( aTitle && strlen(aTitle) ) { @@ -4220,6 +5297,7 @@ char const * tinyfd_arrayDialog ( } else { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"");return (char const *)0;} return NULL ; } @@ -4245,87 +5323,163 @@ char const * tinyfd_arrayDialog ( #endif /* _WIN32 */ -/*// hello.c +/* int main() { - char const * lThePassword; - char const * lTheSaveFileName; - char const * lTheOpenFileName; - FILE * lIn; - char lBuffer[1024]; +char const * lTmp; +char const * lTheSaveFileName; +char const * lTheOpenFileName; +char const * lTheSelectFolderName; +char const * lTheHexColor; +char const * lWillBeGraphicMode; +unsigned char lRgbColor[3]; +FILE * lIn; +char lBuffer[1024]; +char lThePassword[1024]; +char const * lFilterPatterns[2] = { "*.txt", "*.text" }; + +lWillBeGraphicMode = tinyfd_inputBox("tinyfd_query", NULL, NULL); + +if (lWillBeGraphicMode) +{ + strcpy(lBuffer, "graphic mode: "); +} +else +{ + strcpy(lBuffer, "console mode: "); +} + +strcat(lBuffer, tinyfd_response); +strcpy(lThePassword, "tinyfiledialogs v"); +strcat(lThePassword, tinyfd_version); +tinyfd_messageBox(lThePassword, lBuffer, "ok", "info", 0); + +if (lWillBeGraphicMode && !tinyfd_forceConsole) +{ + tinyfd_forceConsole = tinyfd_messageBox("Hello World", + "force dialogs into console mode?\ + \n\t(it is better if dialog is installed)", + "yesno", "question", 0); +} + +lTmp = tinyfd_inputBox( + "a password box", "your password will be revealed", NULL); - tinyfd_forceConsole = tinyfd_messageBox("Hello World", - "force dialogs into console mode?\ - \n\t(it is better if dialog is installed)", - "yesno", "question", 0); +if (!lTmp) return 1; - lThePassword = tinyfd_inputBox( - "a password box","your password will be revealed",NULL); +strcpy(lThePassword, lTmp); - lTheSaveFileName = tinyfd_saveFileDialog ( +lTheSaveFileName = tinyfd_saveFileDialog( "let us save this password", - "passwordFile.txt", - 0, - NULL, - NULL ); + "passwordFile.txt", + 2, + lFilterPatterns, + NULL); - lIn = fopen(lTheSaveFileName, "w"); - if (!lIn) - { - tinyfd_messageBox( - "Error", - "Can not open this file in writting mode", - "ok", - "error", - 1 ); - return(1); - } - fputs(lThePassword, lIn); - fclose(lIn); +if (!lTheSaveFileName) +{ + tinyfd_messageBox( + "Error", + "Save file name is NULL", + "ok", + "error", + 1); + return 1; +} - lTheOpenFileName = tinyfd_openFileDialog ( - "let us read this password back", - "", - 0, - NULL, - NULL, - 0); +lIn = fopen(lTheSaveFileName, "w"); +if (!lIn) +{ + tinyfd_messageBox( + "Error", + "Can not open this file in write mode", + "ok", + "error", + 1); + return 1; +} +fputs(lThePassword, lIn); +fclose(lIn); + +lTheOpenFileName = tinyfd_openFileDialog( + "let us read the password back", + "", + 2, + lFilterPatterns, + NULL, + 0); + +if (!lTheOpenFileName) +{ + tinyfd_messageBox( + "Error", + "Open file name is NULL", + "ok", + "error", + 1); + return 1; +} - lIn = fopen(lTheOpenFileName, "r"); - if (!lIn) - { - tinyfd_messageBox( - "Error", - "Can not open this file in reading mode", - "ok", - "error", - 1 ); - return(1); - } - fgets(lBuffer, sizeof(lBuffer), lIn); - fclose(lIn); +lIn = fopen(lTheOpenFileName, "r"); + +if (!lIn) +{ + tinyfd_messageBox( + "Error", + "Can not open this file in read mode", + "ok", + "error", + 1); + return(1); +} +lBuffer[0] = '\0'; +fgets(lBuffer, sizeof(lBuffer), lIn); +fclose(lIn); - if ( *lBuffer ) - tinyfd_messageBox("your password is", lBuffer, "ok", "info", 1); +tinyfd_messageBox("your password is", + lBuffer, "ok", "info", 1); + +lTheSelectFolderName = tinyfd_selectFolderDialog( + "let us just select a directory", NULL); + +if (!lTheSelectFolderName) +{ + tinyfd_messageBox( + "Error", + "Select folder name is NULL", + "ok", + "error", + 1); + return 1; +} + +tinyfd_messageBox("The selected folder is", + lTheSelectFolderName, "ok", "info", 1); + +lTheHexColor = tinyfd_colorChooser( + "choose a nice color", + "#FF0077", + lRgbColor, + lRgbColor); + +if (!lTheHexColor) +{ + tinyfd_messageBox( + "Error", + "hexcolor is NULL", + "ok", + "error", + 1); + return 1; +} + +tinyfd_messageBox("The selected hexcolor is", + lTheHexColor, "ok", "info", 1); } //*/ +#ifdef _WIN32 #pragma warning(default:4996) - -/* -OSX : -$ gcc -o hello.app hello.c tinyfiledialogs.c - -UNIX : -$ gcc -o hello hello.c tinyfiledialogs.c - -MinGW : -> gcc -o hello.exe hello.c tinyfiledialogs.c -LC:/mingw/lib -lcomdlg32 -lole32 - -VisualStudio : - Create a console application project, - it links against Comdlg32.lib & Ole32.lib. - Right click on your Project, select Properties. - Configuration Properties/General - Character Set to "Multi-Byte" or "Not Set" -*/ +#pragma warning(default:4100) +#pragma warning(default:4706) +#endif diff --git a/libtinyfiledialogs/tinyfiledialogs.h b/libtinyfiledialogs/tinyfiledialogs.h index 6936bf4..68c4b9d 100644 --- a/libtinyfiledialogs/tinyfiledialogs.h +++ b/libtinyfiledialogs/tinyfiledialogs.h @@ -1,14 +1,17 @@ /* _________ -/ \ tinyfiledialogs.c -|tiny file| Unique code file of "tiny file dialogs" created [November 9, 2014] +/ \ tinyfiledialogs.h +|tiny file| Unique header file of "tiny file dialogs" created [November 9, 2014] | dialogs | Copyright (c) 2014 - 2016 Guillaume Vareille http://ysengrin.com \____ ___/ http://tinyfiledialogs.sourceforge.net \| mailto:tinfyfiledialogs@ysengrin.com +A big thank you to Don Heyse http://ldglite.sf.net for + his code contributions, bug corrections & thorough testing! + git://git.code.sf.net/p/tinyfiledialogs/code - Please +Please 1) let me know - if you are including tiny file dialogs, I'll be happy to add your link to the list of projects using it. @@ -18,47 +21,46 @@ 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 -v2.3.8 [May 10, 2016] zlib licence. - -A single C file (add it to your C or C++ project) with 6 modal function calls: -- message box & question box -- input box & password box -- save file dialog -- open file dialog & multiple files -- select folder dialog +Native dialog library for WINDOWS MAC OSX (10.4~10.11) GTK+ QT CONSOLE & more +v2.5.2 [July 2, 2016] zlib licence + +A single C file (add it to your C or C++ project) with 6 boxes: +- message / question +- input / password +- save file +- open file & multiple files +- select folder - color picker. -Complement to OpenGL GLFW GLUT GLUI -VTK SFML SDL Ogre Unity CEGUI ION MathGL -CPW GLOW GLT NGL STB & GUI less programs +Complements OpenGL GLFW GLUT GLUI VTK SFML SDL Ogre Unity ION +CEGUI MathGL CPW GLOW IMGUI GLT NGL STB & GUI less programs -NO INIT & NO MAIN LOOP +NO INIT +NO MAIN LOOP The dialogs can be forced into console mode -On Windows: +Windows [UTF-8 + UTF-16] - native code & some vbs create the graphic dialogs - enhanced console mode can use dialog.exe from - http://andrear.altervista.org/home/cdialog.php -- basic console input. +http://andrear.altervista.org/home/cdialog.php +- basic console input -On Unix (command line call attempts): +Unix [UTF-8] (command line call attempts) - applescript -- zenity +- zenity / matedialog - kdialog - Xdialog - python2 tkinter - dialog (opens a console if needed) -- whiptail, gdialog, gxmessage -- basic console input. -The same executable can run across desktops & distributions. +- basic console input +The same executable can run across desktops & distributions tested with C & C++ compilers -on Visual Studio MinGW OSX LINUX FREEBSD ILLUMOS SOLARIS MINIX RASPBIAN +on VisualStudio MinGW Mac Linux Bsd Solaris Minix Raspbian C# fortran (iso_c) using Gnome Kde Enlightenment Mate Cinnamon Unity Lxde Lxqt Xfce WindowMaker IceWm Cde Jds OpenBox - + - License - This software is provided 'as-is', without any express or implied @@ -81,90 +83,162 @@ misrepresented as being the original software. #ifndef TINYFILEDIALOGS_H #define TINYFILEDIALOGS_H -/* -if tinydialogs.c is compiled with a C++ compiler -rather than with a C compiler, you need to comment out: +/* if tinydialogs.c is compiled with a C++ compiler rather than with a C compiler +(ie. you change the extension from .c to .cpp), you need to comment out: extern "C" { and the corresponding closing bracket: -} -*/ - +} */ #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ +extern char tinyfd_version[8]; + +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 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 */ + +extern char tinyfd_response[1024]; +/* if you pass "tinyfd_query" as aTitle, +the functions will not display the dialogs +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 the graphic mode: + windows applescript zenity zenity3 matedialog kdialog + xdialog tkinter gdialog gxmessage xmessage +for the console mode: + dialog whiptail basicinput */ + +/* #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 */ + int tinyfd_messageBox ( - char const * const aTitle , /* "" */ - char const * const aMessage , /* "" may contain \n \t */ - char const * const aDialogType , /* "ok" "okcancel" "yesno" */ - char const * const aIconType , /* "info" "warning" "error" "question" */ - int const aDefaultButton ) ; /* 0 for cancel/no , 1 for ok/yes */ - /* returns 0 for cancel/no , 1 for ok/yes */ + char const * const aTitle , /* "" */ + char const * const aMessage , /* "" may contain \n \t */ + char const * const aDialogType , /* "ok" "okcancel" "yesno" */ + char const * const aIconType , /* "info" "warning" "error" "question" */ + int const aDefaultButton ) ; /* 0 for cancel/no , 1 for ok/yes */ + /* returns 0 for cancel/no , 1 for ok/yes */ char const * tinyfd_inputBox ( char const * const aTitle , /* "" */ char const * const aMessage , /* "" may NOT contain \n \t on windows */ char const * const aDefaultInput ) ; /* "" , if NULL it's a passwordBox */ - /* returns NULL on cancel */ + /* returns NULL on cancel */ char const * tinyfd_saveFileDialog ( - char const * const aTitle , /* "" */ - char const * const aDefaultPathAndFile , /* "" */ - int const aNumOfFilterPatterns , /* 0 */ - char const * const * const aFilterPatterns , /* NULL | {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription ) ; /* NULL | "text files" */ - /* returns NULL on cancel */ + char const * const aTitle , /* "" */ + char const * const aDefaultPathAndFile , /* "" */ + int const aNumOfFilterPatterns , /* 0 */ + char const * const * const aFilterPatterns , /* NULL | {"*.jpg","*.png"} */ + char const * const aSingleFilterDescription ) ; /* NULL | "text files" */ + /* returns NULL on cancel */ char const * tinyfd_openFileDialog ( - char const * const aTitle , /* "" */ - char const * const aDefaultPathAndFile , /* "" */ - 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 */ - /* in case of multiple files, the separator is | */ - /* returns NULL on cancel */ + char const * const aTitle , /* "" */ + char const * const aDefaultPathAndFile , /* "" */ + 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 */ + /* in case of multiple files, the separator is | */ + /* returns NULL on cancel */ char const * tinyfd_selectFolderDialog ( char const * const aTitle , /* "" */ - char const * const aDefaultPath ) ; /* "" */ - /* returns NULL on cancel */ + char const * const aDefaultPath ) ; /* "" */ + /* returns NULL on cancel */ char const * tinyfd_colorChooser( char const * const aTitle , /* "" */ char const * const aDefaultHexRGB , /* NULL or "#FF0000" */ unsigned char const aDefaultRGB[3] , /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3] ) ; /* { 0 , 0 , 0 } */ - /* returns the hexcolor as a string "#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 */ - -/* not cross platform - zenity only */ -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"} */ + /* returns the hexcolor as a string "#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 */ + + +/************ NOT CROSS PLATFORM SECTION STARTS HERE ************************/ +#ifdef _WIN32 +#ifndef TINYFD_NOLIB + +/* windows only */ +wchar_t const * tinyfd_utf8to16(char const * const aUtf8string); + +/* windows only */ +char const * tinyfd_utf16to8(wchar_t const * const aUtf16string); + +/* windows only - utf-16 version */ +int tinyfd_messageBoxW( + wchar_t const * const aTitle , + wchar_t const * const aMessage, /* "" may contain \n \t */ + wchar_t const * const aDialogType, /* "ok" "okcancel" "yesno" */ + wchar_t const * const aIconType, /* "info" "warning" "error" "question" */ + int const 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_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" */ + /* returns NULL on cancel */ + +/* windows only - utf-16 version */ +wchar_t const * tinyfd_openFileDialogW( + wchar_t const * const aTitle, /* "" */ + wchar_t const * const aDefaultPathAndFile, /* "" */ + int const aNumOfFilterPatterns , /* 0 */ + wchar_t const * const * const aFilterPatterns, /* NULL {"*.jpg","*.png"} */ + wchar_t const * const aSingleFilterDescription, /* NULL | "image files" */ + int const 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, /* "" */ + wchar_t const * const aDefaultPath); /* "" */ + /* returns NULL on cancel */ + +/* windows only - utf-16 version */ +wchar_t const * tinyfd_colorChooserW( + wchar_t const * const aTitle, /* "" */ + wchar_t const * const aDefaultHexRGB, /* NULL or "#FF0000" */ + unsigned char const aDefaultRGB[3] , /* { 0 , 255 , 255 } */ + unsigned char aoResultRGB[3] ) ; /* { 0 , 0 , 0 } */ + /* returns the hexcolor as a string "#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 */ -extern int tinyfd_forceConsole ; /* 0 (default) or 1 -can be modified at run time. -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. - it will use the package dialog or dialog.exe if installed. -on windows it only make sense for console applications */ +#endif /*TINYFD_NOLIB*/ +#else /*_WIN32*/ -/* #define TINYFD_WIN_CONSOLE_ONLY //*/ -/* On windows, Define this if you don't want to include the code -creating the GUI dialogs. -Then you don't need link against Comdlg32.lib and Ole32.lib */ +/* unix zenity only */ +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"} */ +#endif /*_WIN32 */ #ifdef __cplusplus } @@ -172,10 +246,11 @@ Then you don't need link against Comdlg32.lib and Ole32.lib */ #endif /* TINYFILEDIALOGS_H */ - /* - This is not for android nor ios. - The code is pure C, perfectly compatible with C++. +- The API is Fortran ISO_C_BINDING compliant +- C# via dll, see example file - AVOID USING " AND ' IN TITLES AND MESSAGES. - There's one file filter only, it may contain several patterns. - If no filter description is provided, @@ -183,14 +258,11 @@ Then you don't need link against Comdlg32.lib and Ole32.lib */ - char const * filterPatterns[3] = { "*.obj" , "*.stl" , "*.dxf" } ; - On windows, inputbox and passwordbox are not as smooth as they should be: they open a console window for a few seconds. -- On visual studio: - set Properties/Configuration Properties/General - Character Set to "Multi-Byte" or "Not Set" - 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 unix you need applescript, zenity, kdialog, Xdialog, python2/tkinter - or dialog (will open a terminal if running without console); +- On unix you need applescript, zenity, matedialog, kdialog, Xdialog, + python2/tkinter or dialog (will open a 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. @@ -212,95 +284,7 @@ Then you don't need link against Comdlg32.lib and Ole32.lib */ 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. - -- Here is the Hello World (and a bit more): - if a console is missing, it will use graphic dialogs - if a graphical display is absent, it will use console dialogs +- You can query the type of dialog that will be use. +- The Hello World (and a bit more) is on the sourceforge site: */ -/* hello.c -#include -#include "tinyfiledialogs.h" -int main() -{ - char const * lThePassword; - char const * lTheSaveFileName; - char const * lTheOpenFileName; - FILE * lIn; - char lBuffer[1024]; - - tinyfd_forceConsole = tinyfd_messageBox("Hello World", - "force dialogs into console mode?\ - \n\t(it is better if dialog is installed)", - "yesno", "question", 0); - - lThePassword = tinyfd_inputBox( - "a password box","your password will be revealed",NULL); - - lTheSaveFileName = tinyfd_saveFileDialog ( - "let us save this password", - "passwordFile.txt", - 0, - NULL, - NULL ); - -#pragma warning(disable:4996) // silences warning about fopen - lIn = fopen(lTheSaveFileName, "w"); -#pragma warning(default:4996) - if (!lIn) - { - tinyfd_messageBox( - "Error", - "Can not open this file in writting mode", - "ok", - "error", - 1 ); - return(1); - } - fputs(lThePassword, lIn); - fclose(lIn); - - lTheOpenFileName = tinyfd_openFileDialog ( - "let us read this password back", - "", - 0, - NULL, - NULL, - 0); - -#pragma warning(disable:4996) // silences warning about fopen - lIn = fopen(lTheOpenFileName, "r"); -#pragma warning(default:4996) - if (!lIn) - { - tinyfd_messageBox( - "Error", - "Can not open this file in reading mode", - "ok", - "error", - 1 ); - return(1); - } - fgets(lBuffer, sizeof(lBuffer), lIn); - fclose(lIn); - - if ( *lBuffer ) - tinyfd_messageBox("your password is", lBuffer, "ok", "info", 1); -} - -OSX : -$ gcc -o hello.app hello.c tinyfiledialogs.c - -UNIX : -$ gcc -o hello hello.c tinyfiledialogs.c - -MinGW : -> gcc -o hello.exe hello.c tinyfiledialogs.c -LC:/mingw/lib -lcomdlg32 -lole32 - -VisualStudio : - Create a console application project, - it links against Comdlg32.lib & Ole32.lib. - Right click on your Project, select Properties. - Configuration Properties/General - Character Set to "Multi-Byte" or "Not Set" -*/ From 2ed61653c6f4a8fdde92fe80e002b12865768f19 Mon Sep 17 00:00:00 2001 From: UK992 Date: Mon, 25 Jul 2016 03:16:50 +0200 Subject: [PATCH 05/41] Make it work on Windows --- Cargo.toml | 6 ++++++ build.rs | 22 +++++++++++++--------- examples/main.rs | 2 +- makefile.cargo | 16 ---------------- src/lib.rs | 9 +++++++++ 5 files changed, 29 insertions(+), 26 deletions(-) delete mode 100644 makefile.cargo diff --git a/Cargo.toml b/Cargo.toml index 546b7fe..3642401 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,11 @@ license = "zlib-acknowledgement" build = "build.rs" +[lib] +name = "tinyfiledialogs" + [dependencies] libc = "0.2.0" + +[build-dependencies] +gcc = "0.3" diff --git a/build.rs b/build.rs index cb500b3..93b6383 100644 --- a/build.rs +++ b/build.rs @@ -2,17 +2,21 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use std::process::Command; +extern crate gcc; + use std::env; fn main() { let target = env::var("TARGET").unwrap(); - assert!(Command::new("make") - .args(&["-R", "-f", "makefile.cargo", &format!("-j{}", env::var("NUM_JOBS").unwrap())]) - .status() - .unwrap() - .success()); - println!("cargo:rustc-flags=-L native={}", env::var("OUT_DIR").unwrap()); - println!("cargo:rustc-flags=-l tinyfiledialogs -L {}", env::var("OUT_DIR").unwrap()); -} + let mut cfg = gcc::Config::new(); + cfg.file("libtinyfiledialogs/tinyfiledialogs.c"); + cfg.flag("-v"); + cfg.compile("libtinyfiledialogs.a"); + + if target.contains("windows") { + println!("cargo:rustc-link-lib=user32"); + println!("cargo:rustc-link-lib=comdlg32"); + println!("cargo:rustc-link-lib=ole32"); + } +} diff --git a/examples/main.rs b/examples/main.rs index a391574..c7694a5 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -3,7 +3,7 @@ extern crate tinyfiledialogs; use tinyfiledialogs::{YesNo, MessageBoxIcon, DefaultColorValue}; fn main() { - + let choice = tinyfiledialogs::message_box_yes_no("hello", "yes or no?", MessageBoxIcon::Question, YesNo::No); diff --git a/makefile.cargo b/makefile.cargo deleted file mode 100644 index 4d91725..0000000 --- a/makefile.cargo +++ /dev/null @@ -1,16 +0,0 @@ -ifneq ($(HOST),$(TARGET)) -CC ?= $(TARGET)-gcc -AR ?= $(TARGET)-ar -else -CC ?= gcc -AR ?= ar -endif - -.PHONY: all -all: $(OUT_DIR)/libtinyfiledialogs.a - -$(OUT_DIR)/tinyfiledialogs.o: libtinyfiledialogs/tinyfiledialogs.c $(CURDIR)/makefile.cargo - mkdir -p `dirname $@` && $(CC) $< -o $@ -c $(CXXFLAGS) -fPIC - -$(OUT_DIR)/libtinyfiledialogs.a: $(OUT_DIR)/tinyfiledialogs.o - $(AR) rcs $@ $(OUT_DIR)/tinyfiledialogs.o diff --git a/src/lib.rs b/src/lib.rs index a8eb04e..133f92f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,7 @@ extern { aTitle: *const c_char, aDefaultPath: *const c_char) -> *const c_char; + #[cfg(not(windows))] fn tinyfd_arrayDialog ( aTitle: *const c_char, aNumOfColumns: c_int, @@ -247,6 +248,7 @@ pub fn select_folder_dialog(title: &str, path: &str) -> Option { } } +#[cfg(not(windows))] pub fn list_dialog(title: &str, columns: &[&str], cells: Option<&[&str]>) -> Option { @@ -282,6 +284,13 @@ pub fn list_dialog(title: &str, } } +#[cfg(windows)] +pub fn list_dialog(_title: &str, + _columns: &[&str], + _cells: Option<&[&str]>) -> Option { + None +} + pub enum DefaultColorValue<'a> { Hex(&'a str), RGB(&'a [u8; 3]), From 080ef6afd2ec335c37340986236296f92877b289 Mon Sep 17 00:00:00 2001 From: UK992 Date: Mon, 25 Jul 2016 03:17:10 +0200 Subject: [PATCH 06/41] Add Appveyor --- appveyor.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 appveyor.yml diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..1071e6f --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,23 @@ +environment: + matrix: + - TARGET: x86_64-pc-windows-msvc + - TARGET: i686-pc-windows-msvc + - TARGET: x86_64-pc-windows-gnu + - TARGET: i686-pc-windows-gnu + +platform: + - x64 + +install: + - ps: Start-FileDownload "https://static.rust-lang.org/dist/rust-nightly-${env:TARGET}.exe" + - rust-nightly-%TARGET%.exe /VERYSILENT /NORESTART /DIR="C:\Program Files (x86)\Rust" + - SET PATH=C:\msys64\mingw64\bin;C:\msys64\usr\bin\;%PATH% + - SET PATH=%PATH%;C:\Program Files (x86)\Rust\bin + - SET RUST_BACKTRACE=1 + - rustc -V + - cargo -V + +build: false + +test_script: + - cargo test --verbose From d763669f633cd16c55fd6e1d70b798b7ae84a395 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Wed, 13 Jul 2016 13:28:51 -0400 Subject: [PATCH 07/41] Update to a564a2e6527d0cb8bfa979d6256d2c268aa7661e. --- libtinyfiledialogs/tinyfiledialogs.c | 1772 ++++++++++++++------------ libtinyfiledialogs/tinyfiledialogs.h | 39 +- 2 files changed, 959 insertions(+), 852 deletions(-) diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index acd5ec9..b9ad64a 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -22,7 +22,7 @@ tiny file dialogs (cross-platform C C++) InputBox PasswordBox MessageBox ColorPicker OpenFileDialog SaveFileDialog SelectFolderDialog Native dialog library for WINDOWS MAC OSX (10.4~10.11) GTK+ QT CONSOLE & more -v2.5.2 [July 2, 2016] zlib licence +v2.5.5 [July 7, 2016] zlib licence A single C file (add it to your C or C++ project) with 6 boxes: - message / question @@ -101,7 +101,9 @@ misrepresented as being the original software. #endif /* TINYFD_NOLIB */ #include #include + /*#include */ #define SLASH "\\" + int tinyfd_winUtf8 = 0 ; /* on windows string char can be 0:MBSC or 1:UTF-8 */ #else #include #include @@ -114,10 +116,14 @@ misrepresented as being the original software. #define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */ #define MAX_MULTIPLE_FILES 32 -char tinyfd_version [ 8 ] = "2.5.2"; +char tinyfd_version [8] = "2.5.5"; -int tinyfd_forceConsole = 0 ; /* 0 (default) or 1 -for unix & windows: 0 (graphic mode) or 1 (console mode). +#if defined(TINYFD_NOLIB) && defined(_WIN32) +int tinyfd_forceConsole = 1 ; +#else +int tinyfd_forceConsole = 0 ; /* 0 (default) or 1 */ +#endif +/* 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. @@ -236,10 +242,10 @@ 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 ) @@ -261,8 +267,8 @@ 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 ) { @@ -550,45 +556,100 @@ swprintf(aoResultHexRGB, 8, L"#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]); #ifndef TINYFD_NOLIB -static DWORD runSilent(char* aString) +#if !defined(WC_ERR_INVALID_CHARS) +/* undefined prior to Vista, so not yet in MINGW header file */ +#define WC_ERR_INVALID_CHARS 0x00000080 +#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 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 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 DWORD const runSilentA(char const * const aString) { STARTUPINFOA StartupInfo; PROCESS_INFORMATION ProcessInfo; - char Args[4096]; - char *pEnvCMD = NULL; - char *pDefaultCMD = "CMD.EXE"; + char * lArgs; + char * pEnvCMD = NULL; + char * pDefaultCMD = "CMD.EXE"; ULONG rc; + int lStringLen = 0; memset(&StartupInfo, 0, sizeof(StartupInfo)); StartupInfo.cb = sizeof(STARTUPINFOA); StartupInfo.dwFlags = STARTF_USESHOWWINDOW; StartupInfo.wShowWindow = SW_HIDE; - Args[0] = 0; + if ( aString ) + { + lStringLen = strlen(aString); + } + lArgs = (char *) malloc( MAX_PATH_OR_CMD + lStringLen ); pEnvCMD = getenv("COMSPEC"); if (pEnvCMD){ - strcpy(Args, pEnvCMD); + strcpy(lArgs, pEnvCMD); } else{ - strcpy(Args, pDefaultCMD); + strcpy(lArgs, pDefaultCMD); } - // "/c" option - Do the command then terminate the command window - strcat(Args, " /c "); - //the application you would like to run from the command window - //the parameters passed to the application being run from the command window. - strcat(Args, aString); + /* c to execute then terminate the command window */ + strcat(lArgs, " /c "); - if (!CreateProcessA(NULL, Args, NULL, NULL, FALSE, - CREATE_NEW_CONSOLE, - NULL, - NULL, - &StartupInfo, - &ProcessInfo)) + /* application and parameters to run from the command window */ + strcat(lArgs, aString); + + if (!CreateProcessA(NULL, lArgs, NULL, NULL, FALSE, + CREATE_NEW_CONSOLE, NULL, NULL, + &StartupInfo, &ProcessInfo)) { + free(lArgs); return GetLastError(); } @@ -599,43 +660,72 @@ static DWORD runSilent(char* aString) CloseHandle(ProcessInfo.hThread); CloseHandle(ProcessInfo.hProcess); + free(lArgs); return rc; } -wchar_t const * tinyfd_utf8to16(char const * const aUtf8string) + +static DWORD const runSilentW(wchar_t const * const aString) { - static wchar_t lUtf16string[MAX_PATH_OR_CMD]; - int lSize = MultiByteToWideChar(CP_UTF8, - MB_ERR_INVALID_CHARS, - aUtf8string, -1, lUtf16string, - sizeof lUtf16string / sizeof(wchar_t) ); - if (lSize == 0) + STARTUPINFOW StartupInfo; + PROCESS_INFORMATION ProcessInfo; + ULONG rc; + wchar_t * lArgs; + wchar_t * pEnvCMD; + wchar_t * pDefaultCMD = L"CMD.EXE"; //powershell.exe + int lStringLen = 0; + + memset(&StartupInfo, 0, sizeof(StartupInfo)); + StartupInfo.cb = sizeof(STARTUPINFOW); + StartupInfo.dwFlags = STARTF_USESHOWWINDOW; + StartupInfo.wShowWindow = SW_HIDE; + + if ( aString ) { - return NULL; + lStringLen = wcslen(aString); } - return lUtf16string; -} + lArgs = (wchar_t *) malloc( (MAX_PATH_OR_CMD + lStringLen) * sizeof(wchar_t) ); + pEnvCMD = utf8to16( getenv("COMSPEC") ); + if (pEnvCMD) + { + wcscpy(lArgs, pEnvCMD); + free(pEnvCMD); + } + else + { + wcscpy(lArgs, pDefaultCMD); + } -#if !defined(WC_ERR_INVALID_CHARS) -/* undefined prior to Vista, so not yet in MINGW header file */ -#define WC_ERR_INVALID_CHARS 0x00000080 -#endif -char const * tinyfd_utf16to8(wchar_t const * const aUtf16string) -{ - static char lUtf8string[MAX_PATH_OR_CMD]; - int lSize = WideCharToMultiByte(CP_UTF8, - WC_ERR_INVALID_CHARS, - aUtf16string, -1, lUtf8string, sizeof lUtf8string, - NULL, NULL); - if (lSize == 0) + /* c to execute then terminate the command window */ + wcscat(lArgs, L" /c "); + + /* application and parameters to run from the command window */ + wcscat(lArgs, aString); + + if (!CreateProcessW(NULL, lArgs, NULL, NULL, FALSE, + CREATE_NEW_CONSOLE, NULL, NULL, + &StartupInfo, &ProcessInfo)) { - return NULL; + free(lArgs); + return GetLastError(); } - return lUtf8string; + + WaitForSingleObject(ProcessInfo.hProcess, INFINITE); + if (!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) + { + rc = 0; + } + + CloseHandle(ProcessInfo.hThread); + CloseHandle(ProcessInfo.hProcess); + + free(lArgs); + return rc; } + int tinyfd_messageBoxW( wchar_t const * const aTitle, /* NULL or "" */ wchar_t const * const aMessage, /* NULL or "" may contain \n and \t */ @@ -700,50 +790,33 @@ int tinyfd_messageBoxW( } -static int messageBoxWinGui( +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" */ char const * const aIconType, /* "info" "warning" "error" "question" */ int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes */ { - wchar_t lTitle[MAX_PATH_OR_CMD]; - wchar_t lMessage[MAX_PATH_OR_CMD]; - wchar_t lDialogType[16]; - wchar_t lIconType[16]; - wchar_t const * lTmp; - - lTitle[0] = L'\0'; - lMessage[0] = L'\0'; - lDialogType[0] = L'\0'; - lIconType[0] = L'\0'; - - if (aTitle) - { - lTmp = tinyfd_utf8to16(aTitle); - wcscpy(lTitle, lTmp); - } - - if (aMessage) - { - lTmp = tinyfd_utf8to16(aMessage); - wcscpy(lMessage, lTmp); - } - - if (aDialogType) - { - lTmp = tinyfd_utf8to16(aDialogType); - wcscpy(lDialogType, lTmp); - } - - if (aIconType) - { - lTmp = tinyfd_utf8to16(aIconType); - wcscpy(lIconType, lTmp); - } - - return tinyfd_messageBoxW(lTitle, lMessage, - lDialogType, lIconType, aDefaultButton ); + 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 ; } #endif /* TINYFD_NOLIB */ @@ -754,13 +827,21 @@ static char const * inputBoxWinGui( char const * const aMessage , /* NULL or "" may NOT contain \n nor \t */ char const * const aDefaultInput ) /* "" , if NULL it's a passwordBox */ { - char lDialogString[4*MAX_PATH_OR_CMD]; + char * lDialogString; FILE * lIn; int lResult; + int lTitleLen; + int lMessageLen; +/* wchar_t * lDialogStringW; */ + #ifndef TINYFD_NOLIB - DWORD lDword; +/* DWORD lDword; */ #endif + lTitleLen = aTitle ? strlen(aTitle) : 0 ; + lMessageLen = aMessage ? strlen(aMessage) : 0 ; + lDialogString = (char *)malloc(3 * MAX_PATH_OR_CMD + lTitleLen + lMessageLen); + if (aDefaultInput) { sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.vbs", @@ -774,6 +855,7 @@ static char const * inputBoxWinGui( lIn = fopen(lDialogString, "w"); if (!lIn) { + free(lDialogString); return NULL; } @@ -896,12 +978,12 @@ name = 'txt_input' style = 'font-size: 11px;' value = '' >
\n\ strcpy(lDialogString, ""); -#ifndef TINYFD_NOLIB +/* #ifdef TINYFD_NOLIB */ if ( ! GetConsoleWindow() ) { strcat(lDialogString, "powershell -WindowStyle Hidden -Command \""); } -#endif +/* #endif */ if (aDefaultInput) { @@ -913,26 +995,39 @@ name = 'txt_input' style = 'font-size: 11px;' value = '' >
\n\ strcat(lDialogString, "mshta.exe %USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.hta"); } - if ( ! GetConsoleWindow() ) + + if (!GetConsoleWindow()) { strcat(lDialogString, "\""); } + /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ -//#ifndef TINYFD_NOLIB -// lDword = runSilent(lDialogString); -//#else if (!(lIn = _popen(lDialogString, "r"))) { + free(lDialogString); return NULL ; } while ( fgets ( aoBuff , MAX_PATH_OR_CMD , lIn ) != NULL ) {} _pclose ( lIn ) ; - if ( aoBuff[ strlen ( aoBuff ) -1 ] == '\n' ) + if ( aoBuff[strlen ( aoBuff ) -1] == '\n' ) + { + aoBuff[strlen ( aoBuff ) -1] = '\0' ; + } + +/* + if (tinyfd_winUtf8) + { + lDialogStringW = utf8to16(lDialogString); + lDword = runSilentW( lDialogStringW ); + free(lDialogStringW); + } + else { - aoBuff[ strlen ( aoBuff ) -1 ] = '\0' ; + lDword = runSilentA(lDialogString); } -//#endif +*/ + if (aDefaultInput) { sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.vbs", @@ -947,6 +1042,7 @@ name = 'txt_input' style = 'font-size: 11px;' value = '' >
\n\ remove(lDialogString); sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.hta", getenv("USERPROFILE")); + free(lDialogString); return NULL; } while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) @@ -957,6 +1053,7 @@ name = 'txt_input' style = 'font-size: 11px;' value = '' >
\n\ getenv("USERPROFILE")); } remove(lDialogString); + free(lDialogString); /* printf ( "aoBuff: %s\n" , aoBuff ) ; //*/ lResult = strncmp(aoBuff, "1", 1) ? 0 : 1; /* printf ( "lResult: %d \n" , lResult ) ; //*/ @@ -1059,7 +1156,7 @@ wchar_t const * tinyfd_saveFileDialogW( } -static char const * saveFileDialogWinGui( +static char const * saveFileDialogWinGui8( char * const aoBuff, char const * const aTitle, /* NULL or "" */ char const * const aDefaultPathAndFile, /* NULL or "" */ @@ -1067,49 +1164,23 @@ static char const * saveFileDialogWinGui( char const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ char const * const aSingleFilterDescription) /* NULL or "image files" */ { - wchar_t lTitle[MAX_PATH_OR_CMD]; - wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD]; - wchar_t lSingleFilterDescription[MAX_PATH_OR_CMD]; + wchar_t * lTitle; + wchar_t * lDefaultPathAndFile; + wchar_t * lSingleFilterDescription; wchar_t * * lFilterPatterns; wchar_t const * lTmpWChar; - char const * lTmpChar; + char * lTmpChar; int i ; - lTitle[0] = L'\0'; - lDefaultPathAndFile[0] = L'\0'; - lSingleFilterDescription[0] = L'\0'; - - lFilterPatterns = malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); + lFilterPatterns = (wchar_t **) malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); for (i = 0; i < aNumOfFilterPatterns; i++) { - if (aFilterPatterns[i]) - { - int lSize = strlen(aFilterPatterns[i]); - lFilterPatterns[i] = malloc(2*lSize*sizeof(wchar_t)); //2*bigger to be sure - lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]); - wcscpy(lFilterPatterns[i], lTmpWChar); - } - else - { - lFilterPatterns[i] = NULL; - } + lFilterPatterns[i] = utf8to16(aFilterPatterns[i]); } - if (aTitle) - { - lTmpWChar = tinyfd_utf8to16(aTitle); - wcscpy(lTitle, lTmpWChar); - } - if (aDefaultPathAndFile) - { - lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile); - wcscpy(lDefaultPathAndFile, lTmpWChar); - } - if (aSingleFilterDescription) - { - lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription); - wcscpy(lSingleFilterDescription, lTmpWChar); - } + lTitle = utf8to16(aTitle); + lDefaultPathAndFile = utf8to16(aDefaultPathAndFile); + lSingleFilterDescription = utf8to16(aSingleFilterDescription); lTmpWChar = tinyfd_saveFileDialogW( lTitle, @@ -1119,12 +1190,12 @@ static char const * saveFileDialogWinGui( lFilterPatterns, lSingleFilterDescription); + free(lTitle); + free(lDefaultPathAndFile); + free(lSingleFilterDescription); for (i = 0; i < aNumOfFilterPatterns; i++) { - if (aFilterPatterns[i]) - { - free(lFilterPatterns[i]); - } + free(lFilterPatterns[i]); } free(lFilterPatterns); @@ -1133,8 +1204,9 @@ static char const * saveFileDialogWinGui( return NULL; } - lTmpChar = tinyfd_utf16to8(lTmpWChar); + lTmpChar = utf16to8(lTmpWChar); strcpy(aoBuff, lTmpChar); + free(lTmpChar); return aoBuff; } @@ -1268,7 +1340,7 @@ wchar_t const * tinyfd_openFileDialogW( } -static char const * openFileDialogWinGui( +static char const * openFileDialogWinGui8( char * const aoBuff, char const * const aTitle, /* NULL or "" */ char const * const aDefaultPathAndFile, /* NULL or "" */ @@ -1277,49 +1349,23 @@ static char const * openFileDialogWinGui( char const * const aSingleFilterDescription, /* NULL or "image files" */ int const aAllowMultipleSelects) /* 0 or 1 */ { - wchar_t lTitle[MAX_PATH_OR_CMD]; - wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD]; - wchar_t lSingleFilterDescription[MAX_PATH_OR_CMD]; + wchar_t * lTitle; + wchar_t * lDefaultPathAndFile; + wchar_t * lSingleFilterDescription; wchar_t * * lFilterPatterns; wchar_t const * lTmpWChar; - char const * lTmpChar; + char * lTmpChar; int i; - lTitle[0] = L'\0'; - lDefaultPathAndFile[0] = L'\0'; - lSingleFilterDescription[0] = L'\0'; - - lFilterPatterns = malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); + lFilterPatterns = (wchar_t * *) malloc(aNumOfFilterPatterns*sizeof(wchar_t *)); for (i = 0; i < aNumOfFilterPatterns; i++) { - if (aFilterPatterns[i]) - { - int lSize = strlen(aFilterPatterns[i]); - lFilterPatterns[i] = malloc(2 * lSize*sizeof(wchar_t)); //2*bigger to be sure - lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]); - wcscpy(lFilterPatterns[i], lTmpWChar); - } - else - { - lFilterPatterns[i] = NULL; - } + lFilterPatterns[i] = utf8to16(aFilterPatterns[i]); } - if (aTitle) - { - lTmpWChar = tinyfd_utf8to16(aTitle); - wcscpy(lTitle, lTmpWChar); - } - if (aDefaultPathAndFile) - { - lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile); - wcscpy(lDefaultPathAndFile, lTmpWChar); - } - if (aSingleFilterDescription) - { - lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription); - wcscpy(lSingleFilterDescription, lTmpWChar); - } + lTitle = utf8to16(aTitle); + lDefaultPathAndFile = utf8to16(aDefaultPathAndFile); + lSingleFilterDescription = utf8to16(aSingleFilterDescription); lTmpWChar = tinyfd_openFileDialogW( lTitle, @@ -1330,12 +1376,12 @@ static char const * openFileDialogWinGui( lSingleFilterDescription, aAllowMultipleSelects); + free(lTitle); + free(lDefaultPathAndFile); + free(lSingleFilterDescription); for (i = 0; i < aNumOfFilterPatterns; i++) { - if (aFilterPatterns[i]) - { - free(lFilterPatterns[i]); - } + free(lFilterPatterns[i]); } free(lFilterPatterns); @@ -1344,8 +1390,9 @@ static char const * openFileDialogWinGui( return NULL; } - lTmpChar = tinyfd_utf16to8(lTmpWChar); + lTmpChar = utf16to8(lTmpWChar); strcpy(aoBuff, lTmpChar); + free(lTmpChar); return aoBuff; } @@ -1387,41 +1434,33 @@ wchar_t const * tinyfd_selectFolderDialogW( } -static char const * selectFolderDialogWinGui ( +static char const * selectFolderDialogWinGui8 ( char * const aoBuff , char const * const aTitle , /* NULL or "" */ char const * const aDefaultPath ) /* NULL or "" */ { - wchar_t lTitle[MAX_PATH_OR_CMD]; - wchar_t lDefaultPath[MAX_PATH_OR_CMD]; + wchar_t * lTitle; + wchar_t * lDefaultPath; wchar_t const * lTmpWChar; - char const * lTmpChar; + char * lTmpChar; - lTitle[0] = L'\0'; - lDefaultPath[0] = L'\0'; - - if (aTitle) - { - lTmpWChar = tinyfd_utf8to16(aTitle); - wcscpy(lTitle, lTmpWChar); - } - if (aDefaultPath) - { - lTmpWChar = tinyfd_utf8to16(aDefaultPath); - wcscpy(lDefaultPath, lTmpWChar); - } + lTitle = utf8to16(aTitle); + lDefaultPath = utf8to16(aDefaultPath); lTmpWChar = tinyfd_selectFolderDialogW( lTitle, lDefaultPath); + free(lTitle); + free(lDefaultPath); if (!lTmpWChar) { return NULL; } - lTmpChar = tinyfd_utf16to8(lTmpWChar); + lTmpChar = utf16to8(lTmpWChar); strcpy(aoBuff, lTmpChar); + free(lTmpChar); return aoBuff; } @@ -1477,16 +1516,370 @@ wchar_t const * tinyfd_colorChooserW( RGB2HexW(aoResultRGB, lResultHexRGB); - //if (lHResult == S_OK || lHResult == S_FALSE) - //{ - // CoUninitialize(); - //} + //if (lHResult == S_OK || lHResult == S_FALSE) + //{ + // CoUninitialize(); + //} + + return lResultHexRGB; +} + + +static char const * colorChooserWinGui8( + 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]; + + wchar_t * lTitle; + wchar_t * lDefaultHexRGB; + wchar_t const * lTmpWChar; + char * lTmpChar; + + lTitle = utf8to16(aTitle); + lDefaultHexRGB = utf8to16(aDefaultHexRGB); + + lTmpWChar = tinyfd_colorChooserW( + lTitle, + lDefaultHexRGB, + aDefaultRGB, + aoResultRGB ); + + free(lTitle); + free(lDefaultHexRGB); + if (!lTmpWChar) + { + return NULL; + } + + lTmpChar = utf16to8(lTmpWChar); + strcpy(lResultHexRGB, lTmpChar); + free(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" */ + char const * const aIconType , /* "info" "warning" "error" "question" */ + int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes */ +{ + 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 + { + aCode += MB_OK ; + } + + lBoxReturnValue = MessageBoxA(NULL, aMessage, aTitle, aCode); + if ( ( ( 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; + OPENFILENAMEA ofn ; + char * lRetval; + HRESULT lHResult; + + 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 = 0 ; + ofn.hInstance = 0 ; + ofn.lpstrFilter = lFilterPatterns ; + ofn.lpstrCustomFilter = NULL ; + ofn.nMaxCustFilter = 0 ; + ofn.nFilterIndex = 1 ; + ofn.lpstrFile = aoBuff; + + ofn.nMaxFile = MAX_PATH_OR_CMD ; + ofn.lpstrFileTitle = NULL ; + ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ; + ofn.lpstrInitialDir = lDirname; + ofn.lpstrTitle = aTitle ; + 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; + OPENFILENAMEA ofn; + size_t lBuffLen ; + char * lRetval; + HRESULT lHResult; + + 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 = 0 ; + ofn.hInstance = 0 ; + ofn.lpstrFilter = lFilterPatterns; + ofn.lpstrCustomFilter = NULL ; + ofn.nMaxCustFilter = 0 ; + ofn.nFilterIndex = 1 ; + ofn.lpstrFile = aoBuff ; + ofn.nMaxFile = MAX_PATH_OR_CMD ; + ofn.lpstrFileTitle = NULL ; + ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ; + ofn.lpstrInitialDir = lDirname ; + ofn.lpstrTitle = aTitle ; + 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; +} + + +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; + + lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); + + /* we can't use aDefaultPath */ + bInfo.hwndOwner = 0 ; + bInfo.pidlRoot = NULL ; + bInfo.pszDisplayName = aoBuff ; + bInfo.lpszTitle = aTitle ; + bInfo.ulFlags = BIF_USENEWUI; + bInfo.lpfn = NULL ; + bInfo.lParam = 0 ; + bInfo.iImage = -1 ; + + lpItem = SHBrowseForFolderA ( & bInfo ) ; + if ( lpItem ) + { + SHGetPathFromIDListA ( lpItem , aoBuff ) ; + } - return lResultHexRGB; + if (lHResult==S_OK || lHResult==S_FALSE) + { + CoUninitialize(); + } + return aoBuff ; } -static char const * colorChooserWinGui( +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 } */ @@ -1494,417 +1887,55 @@ static char const * colorChooserWinGui( { static char lResultHexRGB[8]; - wchar_t lTitle[MAX_PATH_OR_CMD]; - wchar_t lDefaultHexRGB[MAX_PATH_OR_CMD]; - wchar_t const * lTmpWChar; - char const * lTmpChar; - - lTitle[0] = L'\0'; - lDefaultHexRGB[0] = L'\0'; + CHOOSECOLORA cc; + COLORREF crCustColors[16]; + unsigned char lDefaultRGB[3]; + int lRet; - if (aTitle) + if ( aDefaultHexRGB ) { - lTmpWChar = tinyfd_utf8to16(aTitle); - wcscpy(lTitle, lTmpWChar); + Hex2RGB(aDefaultHexRGB, lDefaultRGB); } - if (aDefaultHexRGB) + else { - lTmpWChar = tinyfd_utf8to16(aDefaultHexRGB); - wcscpy(lDefaultHexRGB, lTmpWChar); + lDefaultRGB[0]=aDefaultRGB[0]; + lDefaultRGB[1]=aDefaultRGB[1]; + lDefaultRGB[2]=aDefaultRGB[2]; } - lTmpWChar = tinyfd_colorChooserW( - lTitle, - lDefaultHexRGB, - aDefaultRGB, - aoResultRGB ); + /* we can't use aTitle */ + cc.lStructSize = sizeof ( CHOOSECOLOR ) ; + cc.hwndOwner = NULL ; + 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; - if (!lTmpWChar) + lRet = ChooseColorA(&cc); + + if ( ! lRet ) { return NULL; } - lTmpChar = tinyfd_utf16to8(lTmpWChar); - strcpy(lResultHexRGB, lTmpChar); + aoResultRGB[0] = GetRValue(cc.rgbResult); + aoResultRGB[1] = GetGValue(cc.rgbResult); + aoResultRGB[2] = GetBValue(cc.rgbResult); + + RGB2Hex(aoResultRGB, lResultHexRGB); return lResultHexRGB; } - -//static int messageBoxWinGui ( -// char const * const aTitle , /* NULL or "" */ -// char const * const aMessage , /* NULL or "" may contain \n and \t */ -// char const * const aDialogType , /* "ok" "okcancel" "yesno" */ -// char const * const aIconType , /* "info" "warning" "error" "question" */ -// int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes */ -//{ -// 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 -// { -// aCode += MB_OK ; -// } -// -// lBoxReturnValue = MessageBoxA(NULL, aMessage, aTitle, aCode); -// if ( ( ( aDialogType -// && strcmp("okcancel", aDialogType) -// && strcmp("yesno", aDialogType) ) ) -// || (lBoxReturnValue == IDOK) -// || (lBoxReturnValue == IDYES) ) -// { -// return 1 ; -// } -// else -// { -// return 0 ; -// } -//} - - -//static char const * saveFileDialogWinGui ( -// 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; -// OPENFILENAMEA ofn ; -// char * lRetval; -// HRESULT lHResult; -// -// 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 = 0 ; -// ofn.hInstance = 0 ; -// ofn.lpstrFilter = lFilterPatterns ; -// ofn.lpstrCustomFilter = NULL ; -// ofn.nMaxCustFilter = 0 ; -// ofn.nFilterIndex = 1 ; -// ofn.lpstrFile = aoBuff; -// -// ofn.nMaxFile = MAX_PATH_OR_CMD ; -// ofn.lpstrFileTitle = NULL ; -// ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ; -// ofn.lpstrInitialDir = lDirname; -// ofn.lpstrTitle = aTitle ; -// 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 * openFileDialogWinGui ( -// 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; -// OPENFILENAMEA ofn; -// size_t lBuffLen ; -// char * lRetval; -// HRESULT lHResult; -// -// 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 = 0 ; -// ofn.hInstance = 0 ; -// ofn.lpstrFilter = lFilterPatterns; -// ofn.lpstrCustomFilter = NULL ; -// ofn.nMaxCustFilter = 0 ; -// ofn.nFilterIndex = 1 ; -// ofn.lpstrFile = aoBuff ; -// ofn.nMaxFile = MAX_PATH_OR_CMD ; -// ofn.lpstrFileTitle = NULL ; -// ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ; -// ofn.lpstrInitialDir = lDirname ; -// ofn.lpstrTitle = aTitle ; -// 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; -//} - - -//static char const * selectFolderDialogWinGui ( -// char * const aoBuff , -// char const * const aTitle , /* NULL or "" */ -// char const * const aDefaultPath ) /* NULL or "" */ -//{ -// BROWSEINFOA bInfo ; -// LPITEMIDLIST lpItem ; -// HRESULT lHResult; -// -// lHResult = CoInitializeEx(NULL,0); -// -// /* we can't use aDefaultPath */ -// bInfo.hwndOwner = 0 ; -// bInfo.pidlRoot = NULL ; -// bInfo.pszDisplayName = aoBuff ; -// bInfo.lpszTitle = aTitle ; -// bInfo.ulFlags = 0 ; -// bInfo.lpfn = NULL ; -// bInfo.lParam = 0 ; -// bInfo.iImage = -1 ; -// -// lpItem = SHBrowseForFolderA ( & bInfo ) ; -// if ( lpItem ) -// { -// SHGetPathFromIDListA ( lpItem , aoBuff ) ; -// } -// -// if (lHResult==S_OK || lHResult==S_FALSE) -// { -// CoUninitialize(); -// } -// return aoBuff ; -//} - - -//static char const * colorChooserWinGui( -// 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 = NULL ; -// 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 lDialogPresent = -1 ; - char lBuff [ MAX_PATH_OR_CMD ] ; + char lBuff [MAX_PATH_OR_CMD] ; FILE * lIn ; char const * lString = "dialog.exe"; if ( lDialogPresent < 0 ) @@ -1917,9 +1948,9 @@ static int dialogPresent ( ) while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL ) {} _pclose ( lIn ) ; - if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) + if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { - lBuff[ strlen ( lBuff ) -1 ] = '\0' ; + lBuff[strlen ( lBuff ) -1] = '\0' ; } if ( strcmp(lBuff+strlen(lBuff)-strlen(lString),lString) ) { @@ -1944,7 +1975,7 @@ static int messageBoxWinConsole ( char lDialogString[MAX_PATH_OR_CMD]; char lDialogFile[MAX_PATH_OR_CMD]; FILE * lIn; - char lBuff [ MAX_PATH_OR_CMD ] = ""; + char lBuff [MAX_PATH_OR_CMD] = ""; strcpy ( lDialogString , "dialog " ) ; if ( aTitle && strlen(aTitle) ) @@ -1981,7 +2012,6 @@ static int messageBoxWinConsole ( else { strcat ( lDialogString , "--msgbox " ) ; - } strcat ( lDialogString , "\"" ) ; @@ -2011,9 +2041,9 @@ static int messageBoxWinConsole ( {} fclose(lIn); remove(lDialogFile); - if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) + if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { - lBuff[ strlen ( lBuff ) -1 ] = '\0' ; + lBuff[strlen ( lBuff ) -1] = '\0' ; } /* printf ( "lBuff: %s\n" , lBuff ) ; //*/ if ( ! strlen(lBuff) ) @@ -2093,9 +2123,9 @@ static char const * inputBoxWinConsole( {} fclose(lIn); remove(lDialogFile); - if ( aoBuff[ strlen ( aoBuff ) -1 ] == '\n' ) + if ( aoBuff[strlen ( aoBuff ) -1] == '\n' ) { - aoBuff[ strlen ( aoBuff ) -1 ] = '\0' ; + aoBuff[strlen ( aoBuff ) -1] = '\0' ; } /* printf ( "aoBuff: %s\n" , aoBuff ) ; //*/ @@ -2113,8 +2143,8 @@ static char const * inputBoxWinConsole( static char const * saveFileDialogWinConsole ( char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile ) /* NULL or "" */ + char const * const aTitle , /* NULL or "" */ + char const * const aDefaultPathAndFile ) /* NULL or "" */ { char lDialogString[MAX_PATH_OR_CMD]; char lPathAndFile[MAX_PATH_OR_CMD] = ""; @@ -2177,9 +2207,9 @@ 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 */ + char const * const aTitle , /* NULL or "" */ + char const * const aDefaultPathAndFile , /* NULL or "" */ + int const aAllowMultipleSelects ) /* 0 or 1 */ { char lFilterPatterns[MAX_PATH_OR_CMD] = ""; char lDialogString[MAX_PATH_OR_CMD] ; @@ -2240,8 +2270,8 @@ static char const * selectFolderDialogWinConsole ( char const * const aTitle , /* NULL or "" */ char const * 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 " ) ; @@ -2296,21 +2326,29 @@ static char const * selectFolderDialogWinConsole ( /* returns 0 for cancel/no , 1 for ok/yes */ 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" */ - char const * const aIconType , /* "info" "warning" "error" "question" */ - int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes */ + char const * const aTitle , /* NULL or "" */ + char const * const aMessage , /* NULL or "" may contain \n and \t */ + char const * const aDialogType , /* "ok" "okcancel" "yesno" */ + char const * const aIconType , /* "info" "warning" "error" "question" */ + int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes */ { char lChar ; #ifndef TINYFD_NOLIB - if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) - && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) ) + if ((!tinyfd_forceConsole || !(GetConsoleWindow() || dialogPresent())) + && (!getenv("SSH_CLIENT") || getenv("DISPLAY"))) { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return 1;} - return messageBoxWinGui( - aTitle,aMessage,aDialogType,aIconType,aDefaultButton); + 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 */ @@ -2385,22 +2423,22 @@ char const * tinyfd_inputBox( char const * const aMessage , /* NULL or "" may NOT contain \n nor \t */ char const * const aDefaultInput ) /* "" , if NULL it's a passwordBox */ { - static char lBuff [ MAX_PATH_OR_CMD ] ; + static char lBuff [MAX_PATH_OR_CMD] ; char * lEOF; #ifndef TINYFD_NOLIB DWORD mode = 0; - HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);; - if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) - && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) ) + HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); +#endif /* 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;} lBuff[0]='\0'; return inputBoxWinGui(lBuff,aTitle,aMessage,aDefaultInput); } - else -#endif /* TINYFD_NOLIB */ - if ( dialogPresent() ) + else if ( dialogPresent() ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} lBuff[0]='\0'; @@ -2408,65 +2446,65 @@ char const * tinyfd_inputBox( } else { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} - lBuff[0]='\0'; - if (!gWarningDisplayed && !tinyfd_forceConsole) - { - gWarningDisplayed = 1 ; - printf("\n\n%s", gAsciiArt); - printf("\n%s\n", gTitle); - printf("%s\n\n\n", gMessageWin); - } - if ( aTitle && strlen(aTitle) ) - { - printf ("%s\n\n", aTitle); - } - if ( aMessage && strlen(aMessage) ) - { - printf("%s\n",aMessage); - } - printf("(ctrl-Z + enter to cancel): "); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} + lBuff[0]='\0'; + if (!gWarningDisplayed && !tinyfd_forceConsole) + { + gWarningDisplayed = 1 ; + printf("\n\n%s", gAsciiArt); + printf("\n%s\n", gTitle); + printf("%s\n\n\n", gMessageWin); + } + if ( aTitle && strlen(aTitle) ) + { + printf ("%s\n\n", aTitle); + } + if ( aMessage && strlen(aMessage) ) + { + printf("%s\n",aMessage); + } + printf("(ctrl-Z + enter to cancel): "); #ifndef TINYFD_NOLIB - if ( ! aDefaultInput ) - { - GetConsoleMode(hStdin,&mode); - SetConsoleMode(hStdin,mode & (~ENABLE_ECHO_INPUT) ); - } + if ( ! aDefaultInput ) + { + GetConsoleMode(hStdin,&mode); + SetConsoleMode(hStdin,mode & (~ENABLE_ECHO_INPUT) ); + } #endif /* TINYFD_NOLIB */ - lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); - if ( ! lEOF ) - { - return NULL; - } + lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); + if ( ! lEOF ) + { + return NULL; + } #ifndef TINYFD_NOLIB - if ( ! aDefaultInput ) - { - SetConsoleMode(hStdin,mode); - printf ("\n"); - } + 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 ; - } + 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 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" */ { - static char lBuff [ MAX_PATH_OR_CMD ] ; + static char lBuff [MAX_PATH_OR_CMD] ; char lString[MAX_PATH_OR_CMD] ; char const * p ; lBuff[0]='\0'; @@ -2475,8 +2513,16 @@ char const * tinyfd_saveFileDialog ( && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;} - p = saveFileDialogWinGui(lBuff, - aTitle,aDefaultPathAndFile,aNumOfFilterPatterns,aFilterPatterns,aSingleFilterDescription); + if (tinyfd_winUtf8) + { + p = saveFileDialogWinGui8(lBuff, + aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + } + else + { + p = saveFileDialogWinGuiA(lBuff, + aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); + } } else #endif /* TINYFD_NOLIB */ @@ -2525,9 +2571,18 @@ char const * tinyfd_openFileDialog ( && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;} - p = openFileDialogWinGui(lBuff, - aTitle,aDefaultPathAndFile,aNumOfFilterPatterns, - aFilterPatterns,aSingleFilterDescription,aAllowMultipleSelects); + 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 */ @@ -2564,14 +2619,21 @@ char const * tinyfd_selectFolderDialog ( char const * const aTitle , /* NULL or "" */ char const * const aDefaultPath ) /* NULL or "" */ { - static char lBuff [ MAX_PATH_OR_CMD ] ; + static char lBuff [MAX_PATH_OR_CMD] ; char const * p ; #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;} - p = selectFolderDialogWinGui(lBuff,aTitle,aDefaultPath); + if (tinyfd_winUtf8) + { + p = selectFolderDialogWinGui8(lBuff, aTitle, aDefaultPath); + } + else + { + p = selectFolderDialogWinGuiA(lBuff, aTitle, aDefaultPath); + } } else #endif /* TINYFD_NOLIB */ @@ -2614,8 +2676,16 @@ char const * tinyfd_colorChooser( && (!getenv("SSH_CLIENT") || getenv("DISPLAY")) ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;} - return colorChooserWinGui( - aTitle,aDefaultHexRGB,aDefaultRGB,aoResultRGB); + if (tinyfd_winUtf8) + { + return colorChooserWinGui8( + aTitle, aDefaultHexRGB, aDefaultRGB, aoResultRGB); + } + else + { + return colorChooserWinGuiA( + aTitle, aDefaultHexRGB, aDefaultRGB, aoResultRGB); + } } else #endif /* TINYFD_NOLIB */ @@ -2680,8 +2750,8 @@ static int dirExists ( char const * const aDirPath ) static int detectPresence ( char const * const aExecutable ) { - char lBuff [ MAX_PATH_OR_CMD ] ; - char lTestedString [ MAX_PATH_OR_CMD ] = "which " ; + char lBuff [MAX_PATH_OR_CMD] ; + char lTestedString [MAX_PATH_OR_CMD] = "which " ; FILE * lIn ; strcat ( lTestedString , aExecutable ) ; @@ -2702,20 +2772,20 @@ static int detectPresence ( char const * const aExecutable ) static int tryCommand ( char const * const aCommand ) { - char lBuff [ MAX_PATH_OR_CMD ] ; - FILE * lIn ; + char lBuff [MAX_PATH_OR_CMD] ; + FILE * lIn ; - lIn = popen ( aCommand , "r" ) ; - if ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) == NULL ) - { /* present */ - pclose ( lIn ) ; - return 1 ; - } - else - { - pclose ( lIn ) ; - return 0 ; - } + lIn = popen ( aCommand , "r" ) ; + if ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) == NULL ) + { /* present */ + pclose ( lIn ) ; + return 1 ; + } + else + { + pclose ( lIn ) ; + return 0 ; + } } @@ -2987,7 +3057,7 @@ static int zenityPresent ( ) static int osx9orBetter ( ) { static int lOsx9orBetter = -1 ; - char lBuff [ MAX_PATH_OR_CMD ] ; + char lBuff [MAX_PATH_OR_CMD] ; FILE * lIn ; int V,v; @@ -3015,7 +3085,7 @@ static int osx9orBetter ( ) static int zenity3Present ( ) { static int lZenity3Present = -1 ; - char lBuff [ MAX_PATH_OR_CMD ] ; + char lBuff [MAX_PATH_OR_CMD] ; FILE * lIn ; if ( lZenity3Present < 0 ) @@ -3052,17 +3122,17 @@ static int tkinter2Present ( ) "-c \"try:\n\timport Tkinter;\nexcept:\n\tprint(0);\""; int i; - if ( lTkinter2Present < 0 ) - { + if ( lTkinter2Present < 0 ) + { strcpy(gPython2Name , "python" ) ; sprintf ( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; - lTkinter2Present = tryCommand(lPythonCommand); - if ( ! lTkinter2Present ) - { + lTkinter2Present = tryCommand(lPythonCommand); + if ( ! lTkinter2Present ) + { strcpy(gPython2Name , "python2" ) ; if ( detectPresence(gPython2Name) ) { -sprintf ( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; + sprintf ( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; lTkinter2Present = tryCommand(lPythonCommand); } else @@ -3072,29 +3142,30 @@ sprintf ( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; sprintf ( gPython2Name , "python2.%d" , i ) ; if ( detectPresence(gPython2Name) ) { -sprintf ( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; + sprintf ( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; lTkinter2Present = tryCommand(lPythonCommand); break ; } } } - } - } - /* printf ("gPython2Name %s\n", gPython2Name) ; //*/ - return lTkinter2Present && graphicMode ( ) ; + } + } + /* printf ("gPython2Name %s\n", gPython2Name) ; //*/ + return lTkinter2Present && graphicMode ( ) ; } /* returns 0 for cancel/no , 1 for ok/yes */ 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"*/ - char const * const aIconType , /* "info" "warning" "error" "question" */ - int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes */ + char const * const aTitle , /* NULL or "" */ + char const * const aMessage , /* NULL or "" may contain \n and \t */ + char const * const aDialogType , /* "ok" "okcancel" "yesno"*/ + char const * const aIconType , /* "info" "warning" "error" "question" */ + int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes */ { - char lBuff [ MAX_PATH_OR_CMD ] ; - char lDialogString [ MAX_PATH_OR_CMD ] ; + char lBuff [MAX_PATH_OR_CMD] ; + char * lDialogString = NULL ; + char * lpDialogString; FILE * lIn ; int lWasGraphicDialog = 0 ; int lWasXterm = 0 ; @@ -3102,7 +3173,17 @@ int tinyfd_messageBox ( char lChar ; struct termios infoOri; struct termios info; - lBuff[0]='\0'; + int lTitleLen ; + int lMessageLen ; + + lBuff[0]='\0'; + + lTitleLen = aTitle ? strlen(aTitle) : 0 ; + lMessageLen = aMessage ? strlen(aMessage) : 0 ; + if ( !aTitle || strcmp(aTitle,"tinyfd_query") ) + { + lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); + } if ( osascriptPresent ( ) ) { @@ -3227,8 +3308,8 @@ int tinyfd_messageBox ( strcat ( lDialogString , "information" ) ; } } - strcat ( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi"); - } + strcat ( lDialogString , ";if [$? = 0];then echo 1;else echo 0;fi"); + } else if ( kdialogPresent() ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} @@ -3273,17 +3354,17 @@ int tinyfd_messageBox ( strcat(lDialogString, aTitle) ; strcat(lDialogString, "\"") ; } - strcat ( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi"); + strcat ( lDialogString , ";if [$? = 0];then echo 1;else echo 0;fi"); } - else if ( ! xdialogPresent() && tkinter2Present ( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return 1;} + else if ( ! xdialogPresent() && tkinter2Present ( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return 1;} - strcpy ( lDialogString , gPython2Name ) ; - if ( ! isatty ( 1 ) && isDarwin ( ) ) - { - strcat ( lDialogString , " -i" ) ; /* for osx without console */ - } + strcpy ( lDialogString , gPython2Name ) ; + if ( ! isatty ( 1 ) && isDarwin ( ) ) + { + strcat ( lDialogString , " -i" ) ; /* for osx without console */ + } strcat ( lDialogString , " -c \"import Tkinter,tkMessageBox;root=Tkinter.Tk();root.withdraw();"); @@ -3296,10 +3377,10 @@ frontmost of process \\\"Python\\\" to true' ''');"); } strcat ( lDialogString ,"res=tkMessageBox." ) ; - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - strcat ( lDialogString , "askokcancel(" ) ; - if ( aDefaultButton ) + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + strcat ( lDialogString , "askokcancel(" ) ; + if ( aDefaultButton ) { strcat ( lDialogString , "default=tkMessageBox.OK," ) ; } @@ -3307,11 +3388,11 @@ frontmost of process \\\"Python\\\" to true' ''');"); { strcat ( lDialogString , "default=tkMessageBox.CANCEL," ) ; } - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - strcat ( lDialogString , "askyesno(" ) ; - if ( aDefaultButton ) + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + strcat ( lDialogString , "askyesno(" ) ; + if ( aDefaultButton ) { strcat ( lDialogString , "default=tkMessageBox.YES," ) ; } @@ -3319,36 +3400,38 @@ frontmost of process \\\"Python\\\" to true' ''');"); { strcat ( lDialogString , "default=tkMessageBox.NO," ) ; } - } - 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" ) ; - } + } + 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) ) - { + if ( aTitle && strlen(aTitle) ) + { strcat(lDialogString, "title='") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "',") ; - } + } if ( aMessage && strlen(aMessage) ) { - replaceSubStr ( aMessage , "\n" , "\\n" , lBuff ) ; strcat(lDialogString, "message='") ; - strcat(lDialogString, lBuff) ; + lpDialogString = lDialogString + strlen(lDialogString); + replaceSubStr ( aMessage , "\n" , "\\n" , lpDialogString ) ; + //replaceSubStr ( aMessage , "\n" , "\\n" , lBuff ) ; + //strcat(lDialogString, lBuff) ; + //lBuff[0]='\0'; strcat(lDialogString, "'") ; - lBuff[0]='\0'; } strcat(lDialogString, ");\n\ if res==False :\n\tprint 0\n\ @@ -3511,11 +3594,11 @@ else :\n\tprint 1\n\"" ) ; if ( lWasGraphicDialog ) { strcat(lDialogString, - "\" 10 60 ) 2>&1;if [ $? = 0 ];then echo 1;else echo 0;fi"); + "\" 10 60 ) 2>&1;if [$? = 0];then echo 1;else echo 0;fi"); } else { - strcat(lDialogString, "\" 10 60 >/dev/tty) 2>&1;if [ $? = 0 ];"); + strcat(lDialogString, "\" 10 60 >/dev/tty) 2>&1;if [$? = 0];"); if ( lWasXterm ) { strcat ( lDialogString , @@ -3652,29 +3735,32 @@ cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); lResult = 1 ; } tcsetattr(0, TCSANOW, &infoOri); + free(lDialogString); return lResult ; } /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ - if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) - { - return 0 ; - } + if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) + { + free(lDialogString); + return 0 ; + } while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL ) {} pclose ( lIn ) ; /* printf ( "lBuff: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; //*/ - if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) - { - lBuff[ strlen ( lBuff ) -1 ] = '\0' ; - } + if ( lBuff[strlen ( lBuff ) -1] == '\n' ) + { + lBuff[strlen ( lBuff ) -1] = '\0' ; + } /* printf ( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; //*/ lResult = strcmp ( lBuff , "1" ) ? 0 : 1 ; /* printf ( "lResult: %d\n" , lResult ) ; //*/ - return lResult ; + free(lDialogString); + return lResult ; } @@ -3685,7 +3771,8 @@ char const * tinyfd_inputBox( char const * const aDefaultInput ) /* "" , if NULL it's a passwordBox */ { static char lBuff[MAX_PATH_OR_CMD]; - char lDialogString[MAX_PATH_OR_CMD]; + char * lDialogString = NULL; + char * lpDialogString; FILE * lIn ; int lResult ; int lWasGdialog = 0 ; @@ -3695,18 +3782,28 @@ char const * tinyfd_inputBox( struct termios oldt ; struct termios newt ; char * lEOF; + int lTitleLen ; + int lMessageLen ; + lBuff[0]='\0'; - if ( osascriptPresent ( ) ) - { + lTitleLen = aTitle ? strlen(aTitle) : 0 ; + lMessageLen = aMessage ? strlen(aMessage) : 0 ; + if ( !aTitle || strcmp(aTitle,"tinyfd_query") ) + { + lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); + } + + if ( osascriptPresent ( ) ) + { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} strcpy ( lDialogString , "osascript "); if ( ! osx9orBetter() ) strcat ( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); strcat ( lDialogString , " -e 'try' -e 'display dialog \"") ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } strcat(lDialogString, "\" ") ; strcat(lDialogString, "default answer \"") ; if ( aDefaultInput && strlen(aDefaultInput) ) @@ -3731,8 +3828,8 @@ char const * tinyfd_inputBox( strcat(lDialogString, "-e 'end try'") ; if ( ! osx9orBetter() ) strcat(lDialogString, " -e 'end tell'") ; } - else if ( zenityPresent() || matedialogPresent() ) - { + else if ( zenityPresent() || matedialogPresent() ) + { if ( zenityPresent() ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} @@ -3767,7 +3864,7 @@ char const * tinyfd_inputBox( strcat(lDialogString, " --hide-text") ; } strcat ( lDialogString , - ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi"); + ");if [$? = 0];then echo 1$szAnswer;else echo 0$szAnswer;fi"); } else if ( kdialogPresent() ) { @@ -3800,7 +3897,7 @@ char const * tinyfd_inputBox( strcat(lDialogString, "\"") ; } strcat ( lDialogString , - ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi"); + ");if [$? = 0];then echo 1$szAnswer;else echo 0$szAnswer;fi"); } else if ( ! xdialogPresent() && tkinter2Present ( ) ) { @@ -3830,11 +3927,14 @@ frontmost of process \\\"Python\\\" to true' ''');"); } if ( aMessage && strlen(aMessage) ) { - replaceSubStr ( aMessage , "\n" , "\\n" , lBuff ) ; + strcat(lDialogString, "prompt='") ; - strcat(lDialogString, lBuff) ; + lpDialogString = lDialogString + strlen(lDialogString); + replaceSubStr ( aMessage , "\n" , "\\n" , lpDialogString ) ; + //replaceSubStr ( aMessage , "\n" , "\\n" , lBuff ) ; + //strcat(lDialogString, lBuff) ; + //lBuff[0]='\0'; strcat(lDialogString, "',") ; - lBuff[0]='\0'; } if ( aDefaultInput ) { @@ -3959,14 +4059,14 @@ frontmost of process \\\"Python\\\" to true' ''');"); if ( lWasGraphicDialog ) { strcat(lDialogString,") 2>/tmp/tinyfd.txt;\ - if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ + if [$? = 0];then tinyfdBool=1;else tinyfdBool=0;fi;\ tinyfdRes=$(cat /tmp/tinyfd.txt);\ rm /tmp/tinyfd.txt;echo $tinyfdBool$tinyfdRes") ; } else { strcat(lDialogString,">/dev/tty ) 2>/tmp/tinyfd.txt;\ - if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ + if [$? = 0];then tinyfdBool=1;else tinyfdBool=0;fi;\ tinyfdRes=$(cat /tmp/tinyfd.txt);\ rm /tmp/tinyfd.txt;echo $tinyfdBool$tinyfdRes") ; if ( lWasXterm ) @@ -4052,6 +4152,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); //*/ if ( ! lEOF || (lBuff[0] == '\0') ) { + free(lDialogString); return NULL; } @@ -4061,6 +4162,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); //*/ if ( ! lEOF || (lBuff[0] == '\0') ) { + free(lDialogString); return NULL; } } @@ -4073,18 +4175,21 @@ frontmost of process \\\"Python\\\" to true' ''');"); printf ("\n"); if ( strchr(lBuff,27) ) { + free(lDialogString); return NULL ; } - if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) + if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { - lBuff[ strlen ( lBuff ) -1 ] = '\0' ; + lBuff[strlen ( lBuff ) -1] = '\0' ; } + free(lDialogString); return lBuff ; } /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) { + free(lDialogString); return NULL ; } while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL ) @@ -4094,26 +4199,29 @@ frontmost of process \\\"Python\\\" to true' ''');"); /* printf ( "len Buff: %lu\n" , strlen(lBuff) ) ; //*/ /* printf ( "lBuff0: %s\n" , lBuff ) ; //*/ - if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) + if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { - lBuff[ strlen ( lBuff ) -1 ] = '\0' ; + lBuff[strlen ( lBuff ) -1] = '\0' ; } /* printf ( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; //*/ if ( lWasBasicXterm ) { if ( strstr(lBuff,"^[") ) /* esc was pressed */ { + free(lDialogString); return NULL ; } } lResult = strncmp ( lBuff , "1" , 1) ? 0 : 1 ; /* printf ( "lResult: %d \n" , lResult ) ; //*/ - if ( ! lResult ) - { + if ( ! lResult ) + { + free(lDialogString); return NULL ; } /* printf ( "lBuff+1: %s\n" , lBuff+1 ) ; //*/ + free(lDialogString); return lBuff+1 ; } @@ -4126,9 +4234,9 @@ char const * tinyfd_saveFileDialog ( char const * 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 ; @@ -4204,7 +4312,7 @@ char const * tinyfd_saveFileDialog ( } for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat ( lDialogString , aFilterPatterns [ i ] ) ; + strcat ( lDialogString , aFilterPatterns [i] ) ; strcat ( lDialogString , " " ) ; } strcat ( lDialogString , "' --file-filter='All files | *'" ) ; @@ -4229,7 +4337,7 @@ char const * tinyfd_saveFileDialog ( strcat(lDialogString , " \"" ) ; for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat ( lDialogString , aFilterPatterns [ i ] ) ; + strcat ( lDialogString , aFilterPatterns [i] ) ; strcat ( lDialogString , " " ) ; } if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) @@ -4302,7 +4410,7 @@ char const * tinyfd_saveFileDialog ( for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) { strcat ( lDialogString , "'" ) ; - strcat ( lDialogString , aFilterPatterns [ i ] ) ; + strcat ( lDialogString , aFilterPatterns [i] ) ; strcat ( lDialogString , "'," ) ; } strcat ( lDialogString , "))," ) ; @@ -4410,9 +4518,9 @@ char const * tinyfd_saveFileDialog ( while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL ) {} pclose ( lIn ) ; - if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) + if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { - lBuff[ strlen ( lBuff ) -1 ] = '\0' ; + lBuff[strlen ( lBuff ) -1] = '\0' ; } /* printf ( "lBuff: %s\n" , lBuff ) ; //*/ if ( ! strlen(lBuff) ) @@ -4442,9 +4550,9 @@ char const * tinyfd_openFileDialog ( char const * const aSingleFilterDescription , /* NULL or "image files" */ int const 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 ] ; + 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 ; @@ -4487,12 +4595,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 , "} " ) ; @@ -4557,7 +4665,7 @@ char const * tinyfd_openFileDialog ( } for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat ( lDialogString , aFilterPatterns [ i ] ) ; + strcat ( lDialogString , aFilterPatterns [i] ) ; strcat ( lDialogString , " " ) ; } strcat ( lDialogString , "' --file-filter='All files | *'" ) ; @@ -4584,7 +4692,7 @@ char const * tinyfd_openFileDialog ( strcat(lDialogString , " \"" ) ; for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) { - strcat ( lDialogString , aFilterPatterns [ i ] ) ; + strcat ( lDialogString , aFilterPatterns [i] ) ; strcat ( lDialogString , " " ) ; } if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) @@ -4664,7 +4772,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 , "))," ) ; @@ -4773,9 +4881,9 @@ frontmost of process \\\"Python\\\" to true' ''');"); p += strlen ( p ); } pclose ( lIn ) ; - if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) + if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { - lBuff[ strlen ( lBuff ) -1 ] = '\0' ; + lBuff[strlen ( lBuff ) -1] = '\0' ; } /* printf ( "lBuff: %s\n" , lBuff ) ; //*/ if ( lWasKdialog && aAllowMultipleSelects ) @@ -4811,8 +4919,8 @@ char const * tinyfd_selectFolderDialog ( char const * const aTitle , /* "" */ char const * 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 ; int lWasGraphicDialog = 0 ; @@ -5012,9 +5120,9 @@ frontmost of process \\\"Python\\\" to true' ''');"); while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL ) {} pclose ( lIn ) ; - if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) + if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { - lBuff[ strlen ( lBuff ) -1 ] = '\0' ; + lBuff[strlen ( lBuff ) -1] = '\0' ; } /* printf ( "lBuff: %s\n" , lBuff ) ; //*/ if ( ! strlen ( lBuff ) || ! dirExists ( lBuff ) ) @@ -5035,9 +5143,9 @@ char const * tinyfd_colorChooser( unsigned char const aDefaultRGB[3] , /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3] ) /* { 0 , 0 , 0 } */ { - static char lBuff [ 16 ] ; - char lTmp [ 16 ] ; - char lDialogString [ MAX_PATH_OR_CMD ] ; + static char lBuff [16] ; + char lTmp [16] ; + char lDialogString [MAX_PATH_OR_CMD] ; char lDefaultHexRGB[8]; char * lpDefaultHexRGB; unsigned char lDefaultRGB[3]; @@ -5219,9 +5327,9 @@ frontmost of process \\\"Python\\\" to true' ''');"); } /* printf ( "len Buff: %lu\n" , strlen(lBuff) ) ; //*/ /* printf ( "lBuff0: %s\n" , lBuff ) ; //*/ - if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) + if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { - lBuff[ strlen ( lBuff ) -1 ] = '\0' ; + lBuff[strlen ( lBuff ) -1] = '\0' ; } if ( lWasZenity3 ) { @@ -5257,8 +5365,8 @@ char const * tinyfd_arrayDialog ( 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 ] ; + static char lBuff [MAX_PATH_OR_CMD] ; + char lDialogString [MAX_PATH_OR_CMD] ; FILE * lIn ; lBuff[0]='\0'; int i ; @@ -5279,7 +5387,7 @@ char const * tinyfd_arrayDialog ( for ( i = 0 ; i < aNumOfColumns ; i ++ ) { strcat ( lDialogString , " --column=\"" ) ; - strcat ( lDialogString , aColumns [ i ] ) ; + strcat ( lDialogString , aColumns [i] ) ; strcat ( lDialogString , "\"" ) ; } } @@ -5290,7 +5398,7 @@ char const * tinyfd_arrayDialog ( for ( i = 0 ; i < aNumOfRows*aNumOfColumns ; i ++ ) { strcat ( lDialogString , "\"" ) ; - strcat ( lDialogString , aCells [ i ] ) ; + strcat ( lDialogString , aCells [i] ) ; strcat ( lDialogString , "\" " ) ; } } @@ -5309,9 +5417,9 @@ char const * tinyfd_arrayDialog ( while ( fgets ( lBuff , sizeof ( lBuff ) , lIn ) != NULL ) {} pclose ( lIn ) ; - if ( lBuff[ strlen ( lBuff ) -1 ] == '\n' ) + if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { - lBuff[ strlen ( lBuff ) -1 ] = '\0' ; + lBuff[strlen ( lBuff ) -1] = '\0' ; } /* printf ( "lBuff: %s\n" , lBuff ) ; //*/ if ( ! strlen ( lBuff ) ) diff --git a/libtinyfiledialogs/tinyfiledialogs.h b/libtinyfiledialogs/tinyfiledialogs.h index 68c4b9d..84e7303 100644 --- a/libtinyfiledialogs/tinyfiledialogs.h +++ b/libtinyfiledialogs/tinyfiledialogs.h @@ -22,7 +22,7 @@ tiny file dialogs (cross-platform C C++) InputBox PasswordBox MessageBox ColorPicker OpenFileDialog SaveFileDialog SelectFolderDialog Native dialog library for WINDOWS MAC OSX (10.4~10.11) GTK+ QT CONSOLE & more -v2.5.2 [July 2, 2016] zlib licence +v2.5.5 [July 7, 2016] zlib licence A single C file (add it to your C or C++ project) with 6 boxes: - message / question @@ -83,19 +83,32 @@ 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 with a C++ compiler rather than with a C compiler (ie. you change the extension from .c to .cpp), you need to comment out: extern "C" { -and the corresponding closing bracket: -} */ +and the corresponding closing bracket near the end of this file: +} +*/ #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -extern char tinyfd_version[8]; +extern char tinyfd_version[8]; /* contains tinyfd current version number */ -extern int tinyfd_forceConsole ; /* 0 (default) or 1 -for unix & windows: 0 (graphic mode) or 1 (console mode). +#ifdef _WIN32 +extern int tinyfd_winUtf8; /* 0 (default) or 1 */ +/* on windows string char can be 0:MBSC or 1:UTF-8 (work in progress) +unless your code is really prepared for it, leave this on MBSC. +for UTF-16 choose the functions at the end of this files */ +#endif + +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 the X server is present, if the package dialog (and a console is present) or dialog.exe is installed. @@ -113,12 +126,6 @@ for the graphic mode: for the console mode: dialog whiptail basicinput */ -/* #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 */ - - int tinyfd_messageBox ( char const * const aTitle , /* "" */ char const * const aMessage , /* "" may contain \n \t */ @@ -172,12 +179,6 @@ char const * tinyfd_colorChooser( #ifdef _WIN32 #ifndef TINYFD_NOLIB -/* windows only */ -wchar_t const * tinyfd_utf8to16(char const * const aUtf8string); - -/* windows only */ -char const * tinyfd_utf16to8(wchar_t const * const aUtf16string); - /* windows only - utf-16 version */ int tinyfd_messageBoxW( wchar_t const * const aTitle , @@ -256,8 +257,6 @@ char const * tinyfd_arrayDialog( - If no filter description is provided, the list of patterns will become the description. - char const * filterPatterns[3] = { "*.obj" , "*.stl" , "*.dxf" } ; -- On windows, inputbox and passwordbox are not as smooth as they should be: - they open a console window for a few seconds. - 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. From fec6c35485d08916ff79fee6a3da395c7380fae0 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Thu, 1 Sep 2016 09:41:14 -0400 Subject: [PATCH 08/41] Update to 2.5.7. --- libtinyfiledialogs/tinyfiledialogs.c | 94 ++++++++++++++++------------ libtinyfiledialogs/tinyfiledialogs.h | 7 +-- 2 files changed, 56 insertions(+), 45 deletions(-) diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index b9ad64a..9977c8b 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -1,6 +1,6 @@ /* _________ -/ \ tinyfiledialogs.c +/ \ tinyfiledialogs.c v2.5.7 [August 16, 2016] zlib licence |tiny file| Unique code file of "tiny file dialogs" created [November 9, 2014] | dialogs | Copyright (c) 2014 - 2016 Guillaume Vareille http://ysengrin.com \____ ___/ http://tinyfiledialogs.sourceforge.net @@ -21,8 +21,7 @@ Please tiny file dialogs (cross-platform C C++) InputBox PasswordBox MessageBox ColorPicker OpenFileDialog SaveFileDialog SelectFolderDialog -Native dialog library for WINDOWS MAC OSX (10.4~10.11) GTK+ QT CONSOLE & more -v2.5.5 [July 7, 2016] zlib licence +Native dialog library for WINDOWS MAC OSX GTK+ QT CONSOLE & more A single C file (add it to your C or C++ project) with 6 boxes: - message / question @@ -40,7 +39,7 @@ NO MAIN LOOP The dialogs can be forced into console mode -Windows [UTF-8 + UTF-16] +Windows [MBCS + UTF-8 + UTF-16] - native code & some vbs create the graphic dialogs - enhanced console mode can use dialog.exe from http://andrear.altervista.org/home/cdialog.php @@ -93,12 +92,12 @@ misrepresented as being the original software. #pragma warning(disable:4100) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ #ifndef _WIN32_WINNT - #define _WIN32_WINNT 0x0500 + #define _WIN32_WINNT 0x0500 #endif #ifndef TINYFD_NOLIB #include #include - #endif /* TINYFD_NOLIB */ + #endif #include #include /*#include */ @@ -116,7 +115,7 @@ misrepresented as being the original software. #define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */ #define MAX_MULTIPLE_FILES 32 -char tinyfd_version [8] = "2.5.5"; +char tinyfd_version [8] = "2.5.7"; #if defined(TINYFD_NOLIB) && defined(_WIN32) int tinyfd_forceConsole = 1 ; @@ -832,10 +831,10 @@ static char const * inputBoxWinGui( int lResult; int lTitleLen; int lMessageLen; -/* wchar_t * lDialogStringW; */ #ifndef TINYFD_NOLIB -/* DWORD lDword; */ + wchar_t * lDialogStringW; + DWORD lDword; #endif lTitleLen = aTitle ? strlen(aTitle) : 0 ; @@ -978,12 +977,12 @@ name = 'txt_input' style = 'font-size: 11px;' value = '' >
\n\ strcpy(lDialogString, ""); -/* #ifdef TINYFD_NOLIB */ - if ( ! GetConsoleWindow() ) +#ifndef TINYFD_NOLIB + if ( aDefaultInput && !GetConsoleWindow()) { strcat(lDialogString, "powershell -WindowStyle Hidden -Command \""); } -/* #endif */ +#endif if (aDefaultInput) { @@ -996,37 +995,46 @@ name = 'txt_input' style = 'font-size: 11px;' value = '' >
\n\ "mshta.exe %USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.hta"); } - if (!GetConsoleWindow()) +#ifndef TINYFD_NOLIB + if (aDefaultInput && !GetConsoleWindow()) { strcat(lDialogString, "\""); } +#endif /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ - if (!(lIn = _popen(lDialogString, "r"))) - { - free(lDialogString); - return NULL ; - } - while ( fgets ( aoBuff , MAX_PATH_OR_CMD , lIn ) != NULL ) - {} - _pclose ( lIn ) ; - if ( aoBuff[strlen ( aoBuff ) -1] == '\n' ) - { - aoBuff[strlen ( aoBuff ) -1] = '\0' ; - } -/* - if (tinyfd_winUtf8) +#ifndef TINYFD_NOLIB + if ( ! aDefaultInput ) { - lDialogStringW = utf8to16(lDialogString); - lDword = runSilentW( lDialogStringW ); - free(lDialogStringW); + if (tinyfd_winUtf8) + { + lDialogStringW = utf8to16(lDialogString); + lDword = runSilentW(lDialogStringW); + free(lDialogStringW); + } + else + { + lDword = runSilentA(lDialogString); + } } else +#endif /* TINYFD_NOLIB */ { - lDword = runSilentA(lDialogString); + if (!(lIn = _popen(lDialogString, "r"))) + { + free(lDialogString); + return NULL; + } + while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) + { + } + _pclose(lIn); + if (aoBuff[strlen(aoBuff) - 1] == '\n') + { + aoBuff[strlen(aoBuff) - 1] = '\0'; + } } -*/ if (aDefaultInput) { @@ -2431,7 +2439,11 @@ char const * tinyfd_inputBox( HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); #endif /* TINYFD_NOLIB */ - if ((!tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent())) + if ((!tinyfd_forceConsole || !( +#ifndef TINYFD_NOLIB + GetConsoleWindow() || +#endif /* TINYFD_NOLIB */ + dialogPresent())) && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) ) { if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char const *)1;} @@ -3308,7 +3320,7 @@ int tinyfd_messageBox ( strcat ( lDialogString , "information" ) ; } } - strcat ( lDialogString , ";if [$? = 0];then echo 1;else echo 0;fi"); + strcat ( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi"); } else if ( kdialogPresent() ) { @@ -3354,7 +3366,7 @@ int tinyfd_messageBox ( strcat(lDialogString, aTitle) ; strcat(lDialogString, "\"") ; } - strcat ( lDialogString , ";if [$? = 0];then echo 1;else echo 0;fi"); + strcat ( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi"); } else if ( ! xdialogPresent() && tkinter2Present ( ) ) { @@ -3594,11 +3606,11 @@ else :\n\tprint 1\n\"" ) ; if ( lWasGraphicDialog ) { strcat(lDialogString, - "\" 10 60 ) 2>&1;if [$? = 0];then echo 1;else echo 0;fi"); + "\" 10 60 ) 2>&1;if [ $? = 0 ];then echo 1;else echo 0;fi"); } else { - strcat(lDialogString, "\" 10 60 >/dev/tty) 2>&1;if [$? = 0];"); + strcat(lDialogString, "\" 10 60 >/dev/tty) 2>&1;if [ $? = 0 ];"); if ( lWasXterm ) { strcat ( lDialogString , @@ -3864,7 +3876,7 @@ char const * tinyfd_inputBox( strcat(lDialogString, " --hide-text") ; } strcat ( lDialogString , - ");if [$? = 0];then echo 1$szAnswer;else echo 0$szAnswer;fi"); + ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi"); } else if ( kdialogPresent() ) { @@ -3897,7 +3909,7 @@ char const * tinyfd_inputBox( strcat(lDialogString, "\"") ; } strcat ( lDialogString , - ");if [$? = 0];then echo 1$szAnswer;else echo 0$szAnswer;fi"); + ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi"); } else if ( ! xdialogPresent() && tkinter2Present ( ) ) { @@ -4059,14 +4071,14 @@ frontmost of process \\\"Python\\\" to true' ''');"); if ( lWasGraphicDialog ) { strcat(lDialogString,") 2>/tmp/tinyfd.txt;\ - if [$? = 0];then tinyfdBool=1;else tinyfdBool=0;fi;\ + if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ tinyfdRes=$(cat /tmp/tinyfd.txt);\ rm /tmp/tinyfd.txt;echo $tinyfdBool$tinyfdRes") ; } else { strcat(lDialogString,">/dev/tty ) 2>/tmp/tinyfd.txt;\ - if [$? = 0];then tinyfdBool=1;else tinyfdBool=0;fi;\ + if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ tinyfdRes=$(cat /tmp/tinyfd.txt);\ rm /tmp/tinyfd.txt;echo $tinyfdBool$tinyfdRes") ; if ( lWasXterm ) diff --git a/libtinyfiledialogs/tinyfiledialogs.h b/libtinyfiledialogs/tinyfiledialogs.h index 84e7303..21a466f 100644 --- a/libtinyfiledialogs/tinyfiledialogs.h +++ b/libtinyfiledialogs/tinyfiledialogs.h @@ -1,6 +1,6 @@ /* _________ -/ \ tinyfiledialogs.h +/ \ tinyfiledialogs.h v2.5.7 [August 16, 2016] zlib licence |tiny file| Unique header file of "tiny file dialogs" created [November 9, 2014] | dialogs | Copyright (c) 2014 - 2016 Guillaume Vareille http://ysengrin.com \____ ___/ http://tinyfiledialogs.sourceforge.net @@ -21,8 +21,7 @@ Please tiny file dialogs (cross-platform C C++) InputBox PasswordBox MessageBox ColorPicker OpenFileDialog SaveFileDialog SelectFolderDialog -Native dialog library for WINDOWS MAC OSX (10.4~10.11) GTK+ QT CONSOLE & more -v2.5.5 [July 7, 2016] zlib licence +Native dialog library for WINDOWS MAC OSX GTK+ QT CONSOLE & more A single C file (add it to your C or C++ project) with 6 boxes: - message / question @@ -40,7 +39,7 @@ NO MAIN LOOP The dialogs can be forced into console mode -Windows [UTF-8 + UTF-16] +Windows [MBCS + UTF-8 + UTF-16] - native code & some vbs create the graphic dialogs - enhanced console mode can use dialog.exe from http://andrear.altervista.org/home/cdialog.php From da18773533cb4b0ad4f804aef838c6270d9bead5 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Thu, 1 Sep 2016 09:47:14 -0400 Subject: [PATCH 09/41] Prepare for publishing on crates.io. --- Cargo.toml | 5 ++++- README.md | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3642401..c019c43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,12 @@ [package] name = "tinyfiledialogs" description = "High-level Rust binding for the tinyfiledialogs C library." -version = "0.1.0" +version = "2.5.7" authors = ["Guillaume Vareille ", "Josh Matthews "] license = "zlib-acknowledgement" +readme = "README.md" +keywords = ["dialog", "ui", "windows", "unix"] +repository = "https://github.com/jdm/tinyfiledialogs-rs" build = "build.rs" diff --git a/README.md b/README.md index 97c1c80..180c515 100644 --- a/README.md +++ b/README.md @@ -9,5 +9,5 @@ all non-Rust users of the library. To use this library, add this to the `dependencies` section in `Cargo.toml`: ``` -tinyfiledialogs = { git = "https://github.com/jdm/tinyfiledialogs-rs" } +tinyfiledialogs = "2.0" ``` From e3e5a451742a573010fef9f13597fcaa9eef2051 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Sat, 1 Oct 2016 10:46:04 -0400 Subject: [PATCH 10/41] Update to 2.5.9. --- libtinyfiledialogs/tinyfiledialogs.c | 160 ++++++++++++++------------- libtinyfiledialogs/tinyfiledialogs.h | 21 ++-- 2 files changed, 97 insertions(+), 84 deletions(-) diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index 9977c8b..fd75f2b 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -1,10 +1,10 @@ /* _________ -/ \ tinyfiledialogs.c v2.5.7 [August 16, 2016] zlib licence +/ \ tinyfiledialogs.c v2.5.9 [September 21, 2016] zlib licence |tiny file| Unique code file of "tiny file dialogs" created [November 9, 2014] | dialogs | Copyright (c) 2014 - 2016 Guillaume Vareille http://ysengrin.com \____ ___/ http://tinyfiledialogs.sourceforge.net - \| mailto:tinfyfiledialogs@ysengrin.com + \| mailto:tinyfiledialogs@ysengrin.com A big thank you to Don Heyse http://ldglite.sf.net for his code contributions, bug corrections & thorough testing! @@ -85,12 +85,9 @@ misrepresented as being the original software. #include #include "tinyfiledialogs.h" -/* #define TINYFD_NOLIB //*/ +/* #define TINYFD_NOLIB // */ #ifdef _WIN32 - #pragma warning(disable:4996) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ - #pragma warning(disable:4100) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ - #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0500 #endif @@ -115,7 +112,7 @@ misrepresented as being the original software. #define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */ #define MAX_MULTIPLE_FILES 32 -char tinyfd_version [8] = "2.5.7"; +char tinyfd_version [8] = "2.5.9"; #if defined(TINYFD_NOLIB) && defined(_WIN32) int tinyfd_forceConsole = 1 ; @@ -156,6 +153,11 @@ static char gAsciiArt[] ="\ \\_____ ____/\n\ \\|"; +#ifdef _WIN32 +static char gMessageWin[] = "tiny file dialogs on Windows needs:\n\t\ + a graphic display\nor\tdialog.exe (enhanced console mode)\ + \nor\ta console for basic input"; +#else static char gMessageUnix[] = "tiny file dialogs on UNIX needs:\n\tapplescript\ \nor\tzenity (version 3 for the color chooser)\ \nor\tmatedialog\nor\tkdialog\ @@ -163,10 +165,13 @@ static char gMessageUnix[] = "tiny file dialogs on UNIX needs:\n\tapplescript\ \nor\tdialog (opens a console if needed)\ \nor\twhiptail, gdialog, gxmessage or xmessage (really?)\ \nor\tit will open a console (if needed) for basic input (you had it comming!)"; +#endif -static char gMessageWin[] = "tiny file dialogs on Windows needs:\n\t\ -a graphic display\nor\tdialog.exe (enhanced console mode)\ -\nor\ta console for basic input"; +#ifdef _MSC_VER +#pragma warning(disable:4996) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ +#pragma warning(disable:4100) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ +#pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ +#endif static char * getPathWithoutFinalSlash( char * const aoDestination, /* make sure it is allocated, use _MAX_PATH */ @@ -182,7 +187,7 @@ static char * getPathWithoutFinalSlash( } if (lTmp) { - strncpy(aoDestination, aSource, lTmp - aSource); + strncpy(aoDestination, aSource, lTmp - aSource ); aoDestination[lTmp - aSource] = '\0'; } else @@ -255,7 +260,7 @@ static void Hex2RGB( char const aHexRGB [8] , aoResultRGB[1] = (unsigned char)strtoul(lColorChannel+3,NULL,16); lColorChannel[3] = '\0'; aoResultRGB[0] = (unsigned char)strtoul(lColorChannel+1,NULL,16); -/* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); //*/ +/* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */ } else { @@ -273,9 +278,13 @@ static void RGB2Hex( unsigned char const aRGB [3] , { if ( aRGB ) { - sprintf(aoResultHexRGB,"#%02hhx%02hhx%02hhx", - aRGB[0],aRGB[1],aRGB[2]); - /* printf("aoResultHexRGB %s\n", aoResultHexRGB); //*/ +#if defined(__GNUC__) && defined(_WIN32) + sprintf(aoResultHexRGB, "#%02hx%02hx%02hx", +#else + sprintf(aoResultHexRGB, "#%02hhx%02hhx%02hhx", +#endif + aRGB[0], aRGB[1], aRGB[2]); + /* printf("aoResultHexRGB %s\n", aoResultHexRGB); */ } else { @@ -607,7 +616,7 @@ static char * utf16to8(wchar_t const * const aUtf16string) } -static DWORD const runSilentA(char const * const aString) +static void runSilentA(char const * const aString) { STARTUPINFOA StartupInfo; PROCESS_INFORMATION ProcessInfo; @@ -649,7 +658,7 @@ static DWORD const runSilentA(char const * const aString) &StartupInfo, &ProcessInfo)) { free(lArgs); - return GetLastError(); + return; /* GetLastError(); */ } WaitForSingleObject(ProcessInfo.hProcess, INFINITE); @@ -660,11 +669,11 @@ static DWORD const runSilentA(char const * const aString) CloseHandle(ProcessInfo.hProcess); free(lArgs); - return rc; + return; /* rc */ } -static DWORD const runSilentW(wchar_t const * const aString) +static void runSilentW(wchar_t const * const aString) { STARTUPINFOW StartupInfo; PROCESS_INFORMATION ProcessInfo; @@ -707,7 +716,7 @@ static DWORD const runSilentW(wchar_t const * const aString) &StartupInfo, &ProcessInfo)) { free(lArgs); - return GetLastError(); + return; /* GetLastError(); */ } WaitForSingleObject(ProcessInfo.hProcess, INFINITE); @@ -720,7 +729,7 @@ static DWORD const runSilentW(wchar_t const * const aString) CloseHandle(ProcessInfo.hProcess); free(lArgs); - return rc; + return; /* rc */ } @@ -834,7 +843,6 @@ static char const * inputBoxWinGui( #ifndef TINYFD_NOLIB wchar_t * lDialogStringW; - DWORD lDword; #endif lTitleLen = aTitle ? strlen(aTitle) : 0 ; @@ -1010,12 +1018,12 @@ name = 'txt_input' style = 'font-size: 11px;' value = '' >
\n\ if (tinyfd_winUtf8) { lDialogStringW = utf8to16(lDialogString); - lDword = runSilentW(lDialogStringW); + runSilentW(lDialogStringW); free(lDialogStringW); } else { - lDword = runSilentA(lDialogString); + runSilentA(lDialogString); } } else @@ -1089,8 +1097,8 @@ wchar_t const * tinyfd_saveFileDialogW( wchar_t * p; wchar_t * lRetval; int i; - OPENFILENAMEW ofn; HRESULT lHResult; + OPENFILENAMEW ofn = {0}; lHResult = CoInitializeEx(NULL, 0); @@ -1128,7 +1136,7 @@ wchar_t const * tinyfd_saveFileDialogW( ofn.lStructSize = sizeof(OPENFILENAMEW); ofn.hwndOwner = 0; ofn.hInstance = 0; - ofn.lpstrFilter = lFilterPatterns; + ofn.lpstrFilter = lFilterPatterns && wcslen(lFilterPatterns) ? lFilterPatterns : NULL; ofn.lpstrCustomFilter = NULL; ofn.nMaxCustFilter = 0; ofn.nFilterIndex = 1; @@ -1137,8 +1145,8 @@ wchar_t const * tinyfd_saveFileDialogW( ofn.nMaxFile = MAX_PATH_OR_CMD; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT; - ofn.lpstrInitialDir = lDirname; - ofn.lpstrTitle = aTitle; + ofn.lpstrInitialDir = lDirname && wcslen(lDirname) ? lDirname : NULL; + ofn.lpstrTitle = aTitle && wcslen(aTitle) ? aTitle : NULL; ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR; ofn.nFileOffset = 0; ofn.nFileExtension = 0; @@ -1237,9 +1245,9 @@ wchar_t const * tinyfd_openFileDialogW( wchar_t * lPointers[MAX_MULTIPLE_FILES]; wchar_t * lRetval, * p; int i, j; - OPENFILENAMEW ofn; size_t lBuffLen; HRESULT lHResult; + OPENFILENAMEW ofn = { 0 }; lHResult = CoInitializeEx(NULL, 0); @@ -1277,7 +1285,7 @@ wchar_t const * tinyfd_openFileDialogW( ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = 0; ofn.hInstance = 0; - ofn.lpstrFilter = lFilterPatterns; + ofn.lpstrFilter = lFilterPatterns && wcslen(lFilterPatterns) ? lFilterPatterns : NULL; ofn.lpstrCustomFilter = NULL; ofn.nMaxCustFilter = 0; ofn.nFilterIndex = 1; @@ -1285,8 +1293,8 @@ wchar_t const * tinyfd_openFileDialogW( ofn.nMaxFile = MAX_PATH_OR_CMD; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT; - ofn.lpstrInitialDir = lDirname; - ofn.lpstrTitle = aTitle; + ofn.lpstrInitialDir = lDirname && wcslen(lDirname) ? lDirname : NULL; + ofn.lpstrTitle = aTitle && wcslen(aTitle) ? aTitle : NULL; ofn.Flags = OFN_EXPLORER | OFN_NOCHANGEDIR; ofn.nFileOffset = 0; ofn.nFileExtension = 0; @@ -1422,7 +1430,7 @@ wchar_t const * tinyfd_selectFolderDialogW( bInfo.hwndOwner = 0; bInfo.pidlRoot = NULL; bInfo.pszDisplayName = lBuff; - bInfo.lpszTitle = aTitle; + bInfo.lpszTitle = aTitle && wcslen(aTitle) ? aTitle : NULL; bInfo.ulFlags = BIF_USENEWUI; bInfo.lpfn = NULL; bInfo.lParam = 0; @@ -1647,9 +1655,9 @@ static char const * saveFileDialogWinGuiA ( char lFilterPatterns[MAX_PATH_OR_CMD] = ""; int i ; char * p; - OPENFILENAMEA ofn ; char * lRetval; HRESULT lHResult; + OPENFILENAMEA ofn = { 0 }; lHResult = CoInitializeEx(NULL,0); @@ -1687,7 +1695,7 @@ static char const * saveFileDialogWinGuiA ( ofn.lStructSize = sizeof(OPENFILENAME) ; ofn.hwndOwner = 0 ; ofn.hInstance = 0 ; - ofn.lpstrFilter = lFilterPatterns ; + ofn.lpstrFilter = lFilterPatterns && strlen(lFilterPatterns) ? lFilterPatterns : NULL; ofn.lpstrCustomFilter = NULL ; ofn.nMaxCustFilter = 0 ; ofn.nFilterIndex = 1 ; @@ -1696,8 +1704,8 @@ static char const * saveFileDialogWinGuiA ( ofn.nMaxFile = MAX_PATH_OR_CMD ; ofn.lpstrFileTitle = NULL ; ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ; - ofn.lpstrInitialDir = lDirname; - ofn.lpstrTitle = aTitle ; + 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 ; @@ -1739,11 +1747,11 @@ static char const * openFileDialogWinGuiA ( size_t lLengths[MAX_MULTIPLE_FILES]; int i , j ; char * p; - OPENFILENAMEA ofn; size_t lBuffLen ; char * lRetval; HRESULT lHResult; - + OPENFILENAMEA ofn = {0}; + lHResult = CoInitializeEx(NULL,0); getPathWithoutFinalSlash(lDirname, aDefaultPathAndFile); @@ -1780,7 +1788,7 @@ static char const * openFileDialogWinGuiA ( ofn.lStructSize = sizeof ( OPENFILENAME ) ; ofn.hwndOwner = 0 ; ofn.hInstance = 0 ; - ofn.lpstrFilter = lFilterPatterns; + ofn.lpstrFilter = lFilterPatterns && strlen(lFilterPatterns) ? lFilterPatterns : NULL; ofn.lpstrCustomFilter = NULL ; ofn.nMaxCustFilter = 0 ; ofn.nFilterIndex = 1 ; @@ -1788,8 +1796,8 @@ static char const * openFileDialogWinGuiA ( ofn.nMaxFile = MAX_PATH_OR_CMD ; ofn.lpstrFileTitle = NULL ; ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT ; - ofn.lpstrInitialDir = lDirname ; - ofn.lpstrTitle = aTitle ; + 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 ; @@ -1867,7 +1875,7 @@ static char const * selectFolderDialogWinGuiA ( bInfo.hwndOwner = 0 ; bInfo.pidlRoot = NULL ; bInfo.pszDisplayName = aoBuff ; - bInfo.lpszTitle = aTitle ; + bInfo.lpszTitle = aTitle && strlen(aTitle) ? aTitle : NULL; bInfo.ulFlags = BIF_USENEWUI; bInfo.lpfn = NULL ; bInfo.lParam = 0 ; @@ -3087,7 +3095,7 @@ static int osx9orBetter ( ) } } pclose ( lIn ) ; - /* printf ("Osx10 = %d, %d = <%s>\n", lOsx9orBetter, V, lBuff) ; //*/ + /* printf ("Osx10 = %d, %d = <%s>\n", lOsx9orBetter, V, lBuff) ; */ } return lOsx9orBetter ; } @@ -3162,7 +3170,7 @@ static int tkinter2Present ( ) } } } - /* printf ("gPython2Name %s\n", gPython2Name) ; //*/ + /* printf ("gPython2Name %s\n", gPython2Name) ; */ return lTkinter2Present && graphicMode ( ) ; } @@ -3751,7 +3759,7 @@ cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); return lResult ; } - /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ + /* printf ( "lDialogString: %s\n" , lDialogString ) ; */ if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) { free(lDialogString); @@ -3762,15 +3770,15 @@ cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); pclose ( lIn ) ; - /* printf ( "lBuff: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; //*/ + /* printf ( "lBuff: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { lBuff[strlen ( lBuff ) -1] = '\0' ; } - /* printf ( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; //*/ + /* printf ( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ lResult = strcmp ( lBuff , "1" ) ? 0 : 1 ; - /* printf ( "lResult: %d\n" , lResult ) ; //*/ + /* printf ( "lResult: %d\n" , lResult ) ; */ free(lDialogString); return lResult ; } @@ -4161,7 +4169,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); } lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); - /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); //*/ + /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */ if ( ! lEOF || (lBuff[0] == '\0') ) { free(lDialogString); @@ -4171,7 +4179,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); if ( lBuff[0] == '\n' ) { lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); - /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); //*/ + /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */ if ( ! lEOF || (lBuff[0] == '\0') ) { free(lDialogString); @@ -4198,7 +4206,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); return lBuff ; } - /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ + /* printf ( "lDialogString: %s\n" , lDialogString ) ; */ if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) { free(lDialogString); @@ -4209,13 +4217,13 @@ frontmost of process \\\"Python\\\" to true' ''');"); pclose ( lIn ) ; - /* printf ( "len Buff: %lu\n" , strlen(lBuff) ) ; //*/ - /* printf ( "lBuff0: %s\n" , lBuff ) ; //*/ + /* printf ( "len Buff: %lu\n" , strlen(lBuff) ) ; */ + /* printf ( "lBuff0: %s\n" , lBuff ) ; */ if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { lBuff[strlen ( lBuff ) -1] = '\0' ; } - /* printf ( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; //*/ + /* printf ( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ if ( lWasBasicXterm ) { if ( strstr(lBuff,"^[") ) /* esc was pressed */ @@ -4226,13 +4234,13 @@ frontmost of process \\\"Python\\\" to true' ''');"); } lResult = strncmp ( lBuff , "1" , 1) ? 0 : 1 ; - /* printf ( "lResult: %d \n" , lResult ) ; //*/ + /* printf ( "lResult: %d \n" , lResult ) ; */ if ( ! lResult ) { free(lDialogString); return NULL ; } - /* printf ( "lBuff+1: %s\n" , lBuff+1 ) ; //*/ + /* printf ( "lBuff+1: %s\n" , lBuff+1 ) ; */ free(lDialogString); return lBuff+1 ; } @@ -4522,7 +4530,7 @@ char const * tinyfd_saveFileDialog ( return p ; } - /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ + /* printf ( "lDialogString: %s\n" , lDialogString ) ; */ if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) { return NULL ; @@ -4534,7 +4542,7 @@ char const * tinyfd_saveFileDialog ( { lBuff[strlen ( lBuff ) -1] = '\0' ; } - /* printf ( "lBuff: %s\n" , lBuff ) ; //*/ + /* printf ( "lBuff: %s\n" , lBuff ) ; */ if ( ! strlen(lBuff) ) { return NULL; @@ -4881,7 +4889,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); return p2 ; } - /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ + /* printf ( "lDialogString: %s\n" , lDialogString ) ; */ if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) { return NULL ; @@ -4897,14 +4905,14 @@ frontmost of process \\\"Python\\\" to true' ''');"); { lBuff[strlen ( lBuff ) -1] = '\0' ; } - /* printf ( "lBuff: %s\n" , lBuff ) ; //*/ + /* printf ( "lBuff: %s\n" , lBuff ) ; */ if ( lWasKdialog && aAllowMultipleSelects ) { p = lBuff ; while ( ( p = strchr ( p , '\n' ) ) ) * p = '|' ; } - /* printf ( "lBuff2: %s\n" , lBuff ) ; //*/ + /* printf ( "lBuff2: %s\n" , lBuff ) ; */ if ( ! strlen ( lBuff ) ) { return NULL; @@ -4921,7 +4929,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); { return NULL ; } - /* printf ( "lBuff3: %s\n" , p2 ) ; //*/ + /* printf ( "lBuff3: %s\n" , p2 ) ; */ return p2 ; } @@ -5124,7 +5132,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); } return p ; } - /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ + /* printf ( "lDialogString: %s\n" , lDialogString ) ; */ if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) { return NULL ; @@ -5136,7 +5144,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); { lBuff[strlen ( lBuff ) -1] = '\0' ; } - /* printf ( "lBuff: %s\n" , lBuff ) ; //*/ + /* printf ( "lBuff: %s\n" , lBuff ) ; */ if ( ! strlen ( lBuff ) || ! dirExists ( lBuff ) ) { return NULL ; @@ -5324,7 +5332,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); return p ; } - /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ + /* printf ( "lDialogString: %s\n" , lDialogString ) ; */ if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) { return NULL ; @@ -5337,8 +5345,8 @@ frontmost of process \\\"Python\\\" to true' ''');"); { return NULL ; } - /* printf ( "len Buff: %lu\n" , strlen(lBuff) ) ; //*/ - /* printf ( "lBuff0: %s\n" , lBuff ) ; //*/ + /* printf ( "len Buff: %lu\n" , strlen(lBuff) ) ; */ + /* printf ( "lBuff0: %s\n" , lBuff ) ; */ if ( lBuff[strlen ( lBuff ) -1] == '\n' ) { lBuff[strlen ( lBuff ) -1] = '\0' ; @@ -5362,8 +5370,8 @@ frontmost of process \\\"Python\\\" to true' ''');"); { Hex2RGB(lBuff,aoResultRGB); } - /* printf("%d %d %d\n", aoResultRGB[0],aoResultRGB[1],aoResultRGB[2]); //*/ - /* printf ( "lBuff: %s\n" , lBuff ) ; //*/ + /* printf("%d %d %d\n", aoResultRGB[0],aoResultRGB[1],aoResultRGB[2]); */ + /* printf ( "lBuff: %s\n" , lBuff ) ; */ return lBuff ; } @@ -5421,7 +5429,7 @@ char const * tinyfd_arrayDialog ( return NULL ; } - /* printf ( "lDialogString: %s\n" , lDialogString ) ; //*/ + /* printf ( "lDialogString: %s\n" , lDialogString ) ; */ if ( ! ( lIn = popen ( lDialogString , "r" ) ) ) { return NULL ; @@ -5433,7 +5441,7 @@ char const * tinyfd_arrayDialog ( { lBuff[strlen ( lBuff ) -1] = '\0' ; } - /* printf ( "lBuff: %s\n" , lBuff ) ; //*/ + /* printf ( "lBuff: %s\n" , lBuff ) ; */ if ( ! strlen ( lBuff ) ) { return NULL ; @@ -5444,7 +5452,7 @@ char const * tinyfd_arrayDialog ( /* -int main() +int main(void) { char const * lTmp; char const * lTheSaveFileName; @@ -5595,10 +5603,12 @@ if (!lTheHexColor) tinyfd_messageBox("The selected hexcolor is", lTheHexColor, "ok", "info", 1); + + return 0; } -//*/ +// */ -#ifdef _WIN32 +#ifdef _MSC_VER #pragma warning(default:4996) #pragma warning(default:4100) #pragma warning(default:4706) diff --git a/libtinyfiledialogs/tinyfiledialogs.h b/libtinyfiledialogs/tinyfiledialogs.h index 21a466f..a065a78 100644 --- a/libtinyfiledialogs/tinyfiledialogs.h +++ b/libtinyfiledialogs/tinyfiledialogs.h @@ -1,10 +1,10 @@ /* _________ -/ \ tinyfiledialogs.h v2.5.7 [August 16, 2016] zlib licence +/ \ tinyfiledialogs.h v2.5.9 [September 21, 2016] zlib licence |tiny file| Unique header file of "tiny file dialogs" created [November 9, 2014] | dialogs | Copyright (c) 2014 - 2016 Guillaume Vareille http://ysengrin.com \____ ___/ http://tinyfiledialogs.sourceforge.net - \| mailto:tinfyfiledialogs@ysengrin.com + \| mailto:tinyfiledialogs@ysengrin.com A big thank you to Don Heyse http://ldglite.sf.net for his code contributions, bug corrections & thorough testing! @@ -82,7 +82,7 @@ misrepresented as being the original software. #ifndef TINYFILEDIALOGS_H #define TINYFILEDIALOGS_H -/* #define TINYFD_NOLIB //*/ +/* #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 */ @@ -95,21 +95,21 @@ and the corresponding closing bracket near the end of this file: */ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif extern char tinyfd_version[8]; /* contains tinyfd current version number */ #ifdef _WIN32 +/* for UTF-16 use the functions at the end of this files */ extern int tinyfd_winUtf8; /* 0 (default) or 1 */ /* on windows string char can be 0:MBSC or 1:UTF-8 (work in progress) -unless your code is really prepared for it, leave this on MBSC. -for UTF-16 choose the functions at the end of this files */ +unless your code is really prepared for UTF-8 on windows, leave this on MBSC. */ #endif 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 the X server is present, +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 */ @@ -242,16 +242,18 @@ char const * tinyfd_arrayDialog( #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif #endif /* TINYFILEDIALOGS_H */ /* - This is not for android nor ios. - The code is pure C, perfectly compatible with C++. +- the utf-16 prototypes are in the header file - The API is Fortran ISO_C_BINDING compliant - C# via dll, see example file -- AVOID USING " AND ' IN TITLES AND MESSAGES. +- OSX supported from 10.4 to 10.11 +- Avoid using " and ' in titles and messages. - 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. @@ -283,6 +285,7 @@ char const * tinyfd_arrayDialog( 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: */ From 4bf666ab49bf9aafcbbc34b6d4383db00d737654 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Sat, 1 Oct 2016 10:59:47 -0400 Subject: [PATCH 11/41] Update to 2.5.9 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c019c43..4874a0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tinyfiledialogs" description = "High-level Rust binding for the tinyfiledialogs C library." -version = "2.5.7" +version = "2.5.9" authors = ["Guillaume Vareille ", "Josh Matthews "] license = "zlib-acknowledgement" readme = "README.md" From d5b851102dd0247cccb6f1bd002080ddc894a24e Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Thu, 14 Sep 2017 12:40:08 +0200 Subject: [PATCH 12/41] Fix use-after-free in text input default value handling As [others have noted](https://users.rust-lang.org/t/cstring-as-ptr-is-incredibly-unsafe/11431), `as_ptr()` is quite the footgun. In this case it leads to the default value for text inputs to point to invalid memory. At least with Zenity on Linux, the result is that the default value is simply ignored. --- examples/main.rs | 14 ++++++++++++++ src/lib.rs | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/examples/main.rs b/examples/main.rs index c7694a5..87c509d 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -13,6 +13,18 @@ fn main() { None => user_input = "null".to_string(), } + let user_input_2: String; + match tinyfiledialogs::input_box("Re-enter user name", "Username:", &user_input) { + Some(input) => user_input_2 = input, + None => user_input_2 = "null".to_string(), + } + + let password_input: String; + match tinyfiledialogs::password_box("Enter password", "Password:") { + Some(input) => password_input = input, + None => password_input = "null".to_string(), + } + let save_file: String; match tinyfiledialogs::save_file_dialog("Save", "password.txt") { Some(file) => save_file = file, @@ -49,6 +61,8 @@ fn main() { println!("Choice {:?}", choice); println!("User input {:?}", user_input); + println!("User input 2 {:?}", user_input_2); + println!("Password input {:?}", password_input); println!("Save file {:?}", save_file); println!("Open file {:?}", open_file); println!("folder {:?}", folder); diff --git a/src/lib.rs b/src/lib.rs index 133f92f..ea1aff7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -124,7 +124,7 @@ fn input_box_impl(title: &str, message: &str, default: Option<&str>) -> Option Date: Thu, 14 Sep 2017 12:46:59 +0200 Subject: [PATCH 13/41] version 3.0.4 --- Cargo.toml | 2 +- libtinyfiledialogs/tinyfiledialogs.c | 2935 ++++++++++++++++---------- libtinyfiledialogs/tinyfiledialogs.h | 130 +- 3 files changed, 1877 insertions(+), 1190 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4874a0d..5b61179 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tinyfiledialogs" description = "High-level Rust binding for the tinyfiledialogs C library." -version = "2.5.9" +version = "3.0.4" authors = ["Guillaume Vareille ", "Josh Matthews "] license = "zlib-acknowledgement" readme = "README.md" diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index fd75f2b..5175ef8 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -1,53 +1,58 @@ -/* - _________ -/ \ tinyfiledialogs.c v2.5.9 [September 21, 2016] zlib licence -|tiny file| Unique code file of "tiny file dialogs" created [November 9, 2014] -| dialogs | Copyright (c) 2014 - 2016 Guillaume Vareille http://ysengrin.com -\____ ___/ http://tinyfiledialogs.sourceforge.net - \| mailto:tinyfiledialogs@ysengrin.com +/*_________ + / \ tinyfiledialogs.c v3.0.4 [Sep 15, 2017] zlib licence + |tiny file| Unique code file created [November 9, 2014] + | dialogs | Copyright (c) 2014 - 2017 Guillaume Vareille http://ysengrin.com + \____ ___/ http://tinyfiledialogs.sourceforge.net + \| + git://git.code.sf.net/p/tinyfiledialogs/code + _________________________________________ + | | + | email: tinyfiledialogs@ysengrin.com | + |_________________________________________| + ___________________________________________________________________ + | | + | the windows only wchar_t UTF-16 prototypes are in the header file | + |___________________________________________________________________| A big thank you to Don Heyse http://ldglite.sf.net for his code contributions, bug corrections & thorough testing! - git://git.code.sf.net/p/tinyfiledialogs/code - -Please - 1) let me know - - if you are including tiny file dialogs, - I'll be happy to add your link to the list of projects using it. - - If you are using it on different hardware / OS / compiler. - 2) Be the first to leave a review on Sourceforge. Thanks. +Please 1) let me know If you are using it on different hardware / OS / compiler + 2) leave a very short review on Sourceforge. It helps the ranking in google. 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 -A single C file (add it to your C or C++ project) with 6 boxes: -- message / question -- input / password +One C file (add it to your C or C++ project) with 6 functions: +- message & question +- input & password - save file -- open file & multiple files +- open file(s) - select folder -- color picker. +- color picker -Complements OpenGL GLFW GLUT GLUI VTK SFML SDL Ogre Unity ION -CEGUI MathGL CPW GLOW IMGUI GLT NGL STB & GUI less programs +Complements OpenGL GLFW GLUT GLUI VTK SFML TGUI SDL Ogre Unity3d ION OpenCV +CEGUI MathGL GLM CPW GLOW 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 [MBCS + UTF-8 + UTF-16] -- native code & some vbs create the graphic dialogs +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 [UTF-8] (command line call attempts) +Unix (command line calls) ASCII UTF-8 - applescript -- zenity / matedialog +- zenity / matedialog / qarma (zenity for qt) - kdialog - Xdialog - python2 tkinter @@ -56,9 +61,12 @@ Unix [UTF-8] (command line call attempts) The same executable can run across desktops & distributions tested with C & C++ compilers -on VisualStudio MinGW Mac Linux Bsd Solaris Minix Raspbian C# fortran (iso_c) +on VisualStudio MinGW Mac Linux Bsd Solaris Minix Raspbian using Gnome Kde Enlightenment Mate Cinnamon Unity -Lxde Lxqt Xfce WindowMaker IceWm Cde Jds OpenBox +Lxde Lxqt Xfce WindowMaker IceWm Cde Jds OpenBox Awesome Jwm + +bindings for LUA and C# dll +included in LWJGL(java), rust, Allegrobasic - License - @@ -83,9 +91,10 @@ misrepresented as being the original software. #include #include #include +#include #include "tinyfiledialogs.h" -/* #define TINYFD_NOLIB // */ +/* #define TINYFD_NOLIB */ #ifdef _WIN32 #ifndef _WIN32_WINNT @@ -93,13 +102,15 @@ misrepresented as being the original software. #endif #ifndef TINYFD_NOLIB #include - #include + /*#define TINYFD_NOSELECTFOLDERWIN*/ + #ifndef TINYFD_NOSELECTFOLDERWIN + #include + #endif /*TINYFD_NOSELECTFOLDERWIN*/ #endif - #include #include /*#include */ #define SLASH "\\" - int tinyfd_winUtf8 = 0 ; /* on windows string char can be 0:MBSC or 1:UTF-8 */ + int tinyfd_winUtf8 = 0 ; /* on windows string char can be 0:MBCS or 1:UTF-8 */ #else #include #include @@ -112,7 +123,9 @@ misrepresented as being the original software. #define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */ #define MAX_MULTIPLE_FILES 32 -char tinyfd_version [8] = "2.5.9"; +char tinyfd_version [8] = "3.0.4"; + +static int tinyfd_verbose = 0 ; /* print on unix the command line calls */ #if defined(TINYFD_NOLIB) && defined(_WIN32) int tinyfd_forceConsole = 1 ; @@ -132,8 +145,8 @@ 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 the graphic mode: - windows applescript zenity zenity3 matedialog kdialog - xdialog tkinter gdialog gxmessage xmessage + windows applescript zenity zenity3 matedialog qarma kdialog + tkinter gxmessage gmessage xmessage xdialog gdialog for the console mode: dialog whiptail basicinput */ @@ -143,28 +156,37 @@ static int gWarningDisplayed = 1 ; static int gWarningDisplayed = 0 ; #endif -static char gTitle[]="missing software! (so we switch to basic console input)"; +static char gTitle[]="missing software! (we will try basic console input)"; -static char gAsciiArt[] ="\ +#ifdef _WIN32 +static char gMessageWin[] = "\ ___________\n\ / \\ \n\ | tiny file |\n\ | dialogs |\n\ \\_____ ____/\n\ - \\|"; - -#ifdef _WIN32 -static char gMessageWin[] = "tiny file dialogs on Windows needs:\n\t\ - a graphic display\nor\tdialog.exe (enhanced console mode)\ - \nor\ta console for basic input"; + \\|\ +tiny file dialogs on Windows needs:\ +\n\ta graphic display\ +\nor\tdialog.exe (enhanced console mode)\ +\nor\ta console for basic input"; #else -static char gMessageUnix[] = "tiny file dialogs on UNIX needs:\n\tapplescript\ -\nor\tzenity (version 3 for the color chooser)\ -\nor\tmatedialog\nor\tkdialog\ -\nor\tXdialog\nor\tpython 2 with tkinter\ -\nor\tdialog (opens a console if needed)\ -\nor\twhiptail, gdialog, gxmessage or xmessage (really?)\ -\nor\tit will open a console (if needed) for basic input (you had it comming!)"; +static char gMessageUnix[] = "\ + ___________\n\ +/ \\ \n\ +| tiny file |\n\ +| dialogs |\n\ +\\_____ ____/\n\ + \\|\ +\ntiny file dialogs on UNIX needs:\n\tapplescript\ +\nor\tzenity / matedialog\ +\nor\tqarma (zenity for qt)\ +\nor\tkdialog\ +\nor\tXdialog\ +\nor\tpython 2 + tkinter\ +\nor\tdialog (opens a console xterm if needed)\ +\nor\txterm + bash (opens a console for basic input)\ +\nor\tit will use the existing console for basic input"; #endif #ifdef _MSC_VER @@ -233,14 +255,14 @@ static char * getLastName( } -static void ensureFinalSlash ( char * const aioString ) +static void ensureFinalSlash( char * const aioString ) { - if ( aioString && strlen ( aioString ) ) + if ( aioString && strlen( aioString ) ) { - char * lastcar = aioString + strlen ( aioString ) - 1 ; - if ( strncmp ( lastcar , SLASH , 1 ) ) + char * lastcar = aioString + strlen( aioString ) - 1 ; + if ( strncmp( lastcar , SLASH , 1 ) ) { - strcat ( lastcar , SLASH ) ; + strcat( lastcar , SLASH ) ; } } } @@ -296,15 +318,15 @@ 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 ) +static void replaceSubStr( char const * const aSource , + char const * const aOldSubStr , + char const * const aNewSubStr , + char * const aoDestination ) { char const * pOccurence ; char const * p ; char const * lNewSubStr = "" ; - int lOldSubLen = strlen ( aOldSubStr ) ; + int lOldSubLen = strlen( aOldSubStr ) ; if ( ! aSource ) { @@ -313,7 +335,7 @@ static void replaceSubStr ( char const * const aSource , } if ( ! aOldSubStr ) { - strcpy ( aoDestination , aSource ) ; + strcpy( aoDestination , aSource ) ; return ; } if ( aNewSubStr ) @@ -322,13 +344,13 @@ static void replaceSubStr ( char const * const aSource , } p = aSource ; * aoDestination = '\0' ; - while ( ( pOccurence = strstr ( p , aOldSubStr ) ) != NULL ) + while ( ( pOccurence = strstr( p , aOldSubStr ) ) != NULL ) { - strncat ( aoDestination , p , pOccurence - p ) ; - strcat ( aoDestination , lNewSubStr ) ; + strncat( aoDestination , p , pOccurence - p ) ; + strcat( aoDestination , lNewSubStr ) ; p = pOccurence + lOldSubLen ; } - strcat ( aoDestination , p ) ; + strcat( aoDestination , p ) ; } @@ -354,10 +376,9 @@ static int fileExists( char const * const aFilePathAndName ) lIn = fopen( aFilePathAndName , "r" ) ; if ( ! lIn ) { - return 0 ; } - fclose ( lIn ) ; + fclose( lIn ) ; return 1 ; } @@ -387,7 +408,7 @@ static char const * ensureFilesExist( char * const aDestination , lLen = p2-p ; memmove(lDestination,p,lLen); lDestination[lLen] = '\0'; - if ( fileExists ( lDestination ) ) + if ( fileExists( lDestination ) ) { lDestination += lLen ; * lDestination = '|'; @@ -395,7 +416,7 @@ static char const * ensureFilesExist( char * const aDestination , } p = p2 + 1 ; } - if ( fileExists ( p ) ) + if ( fileExists( p ) ) { lLen = strlen(p) ; memmove(lDestination,p,lLen); @@ -408,11 +429,32 @@ static char const * ensureFilesExist( char * const aDestination , return aDestination ; } + +static void wipefile(char const * const aFilename) +{ + int i; + struct stat st; + FILE * lIn; + + if (stat(aFilename, &st) == 0) + { + if ((lIn = fopen(aFilename, "w"))) + { + for (i = 0; i < st.st_size; i++) + { + fputc('A', lIn); + } + } + fclose(lIn); + } +} + + #ifdef _WIN32 -static int replaceChr ( char * const aString , - char const aOldChr , - char const aNewChr ) +static int replaceChr( char * const aString , + char const aOldChr , + char const aNewChr ) { char * p ; int lRes = 0 ; @@ -428,7 +470,7 @@ static int replaceChr ( char * const aString , } p = aString ; - while ( (p = strchr ( p , aOldChr )) ) + while ( (p = strchr( p , aOldChr )) ) { * p = aNewChr ; p ++ ; @@ -438,12 +480,12 @@ static int replaceChr ( char * const aString , } -static int dirExists ( char const * const aDirPath ) +static int dirExists( char const * const aDirPath ) { struct stat lInfo; - if ( ! aDirPath || ! strlen ( aDirPath ) ) + if ( ! aDirPath || ! strlen( aDirPath ) ) return 0 ; - if ( stat ( aDirPath , & lInfo ) != 0 ) + if ( stat( aDirPath , & lInfo ) != 0 ) return 0 ; else if ( lInfo.st_mode & S_IFDIR ) return 1 ; @@ -451,6 +493,27 @@ static int dirExists ( char const * const aDirPath ) return 0 ; } +#ifndef TINYFD_NOLIB + +static void wipefileW(wchar_t const * const aFilename) +{ + int i; + struct _stat st; + FILE * lIn; + + if (_wstat(aFilename, &st) == 0) + { + if ((lIn = _wfopen(aFilename, L"w"))) + { + for (i = 0; i < st.st_size; i++) + { + fputc('A', lIn); + } + } + fclose(lIn); + } +} + static wchar_t * getPathWithoutFinalSlashW( wchar_t * const aoDestination, /* make sure it is allocated, use _MAX_PATH */ @@ -526,7 +589,7 @@ static void Hex2RGBW(wchar_t const aHexRGB[8], aoResultRGB[1] = (unsigned char)wcstoul(lColorChannel + 3, NULL, 16); lColorChannel[3] = '\0'; aoResultRGB[0] = (unsigned char)wcstoul(lColorChannel + 1, NULL, 16); - /* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); //*/ + /* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */ } else { @@ -546,12 +609,12 @@ static void RGB2HexW( { if (aRGB) { -#if defined(__GNUC__) && __GNUC__ < 5 -swprintf(aoResultHexRGB, L"#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]); -#else -swprintf(aoResultHexRGB, 8, L"#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]); + /* wprintf(L"aoResultHexRGB %s\n", aoResultHexRGB); */ + swprintf(aoResultHexRGB, +#if !defined(__GNUC__) || (__GNUC__) >= 5 + 8, #endif - /* wprintf(L"aoResultHexRGB %s\n", aoResultHexRGB); //*/ + L"#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]); } else { @@ -562,7 +625,6 @@ swprintf(aoResultHexRGB, 8, L"#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]); } } -#ifndef TINYFD_NOLIB #if !defined(WC_ERR_INVALID_CHARS) /* undefined prior to Vista, so not yet in MINGW header file */ @@ -584,6 +646,15 @@ static int sizeUtf8(wchar_t const * const aUtf16string) } +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 ; @@ -600,6 +671,22 @@ static wchar_t * utf8to16(char const * const aUtf8string) } +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 ; @@ -616,6 +703,22 @@ static char * utf16to8(wchar_t const * const aUtf16string) } +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 void runSilentA(char const * const aString) { STARTUPINFOA StartupInfo; @@ -680,7 +783,7 @@ static void runSilentW(wchar_t const * const aString) ULONG rc; wchar_t * lArgs; wchar_t * pEnvCMD; - wchar_t * pDefaultCMD = L"CMD.EXE"; //powershell.exe + wchar_t * pDefaultCMD = L"CMD.EXE"; int lStringLen = 0; memset(&StartupInfo, 0, sizeof(StartupInfo)); @@ -733,17 +836,18 @@ static void runSilentW(wchar_t const * const aString) } - 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" */ + 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 */ + int const 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, "windowsW"); return 1; } + if (aIconType && !wcscmp(L"warning", aIconType)) { aCode = MB_ICONWARNING; @@ -801,9 +905,9 @@ int tinyfd_messageBoxW( 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" */ + 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 */ + int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { int lIntRetVal; wchar_t * lTitle; @@ -827,39 +931,44 @@ static int messageBoxWinGui8( return lIntRetVal ; } -#endif /* TINYFD_NOLIB */ -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 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 */ { - char * lDialogString; + static wchar_t lBuff[MAX_PATH_OR_CMD]; + wchar_t * lDialogString; FILE * lIn; int lResult; int lTitleLen; int lMessageLen; + int lDialogStringLen; -#ifndef TINYFD_NOLIB - wchar_t * lDialogStringW; -#endif + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windowsW"); return (wchar_t const *)1; } - lTitleLen = aTitle ? strlen(aTitle) : 0 ; - lMessageLen = aMessage ? strlen(aMessage) : 0 ; - lDialogString = (char *)malloc(3 * MAX_PATH_OR_CMD + lTitleLen + lMessageLen); + 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 (aDefaultInput) { - sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.vbs", - getenv("USERPROFILE")); + swprintf(lDialogString, +#if !defined(__GNUC__) || (__GNUC__) >= 5 + lDialogStringLen, +#endif + L"%ls\\AppData\\Local\\Temp\\tinyfd.vbs", _wgetenv(L"USERPROFILE")); } else { - sprintf(lDialogString, "%s\\AppData\\Local\\Temp\\tinyfd.hta", - getenv("USERPROFILE")); + swprintf(lDialogString, +#if !defined(__GNUC__) || (__GNUC__) >= 5 + lDialogStringLen, +#endif + L"%ls\\AppData\\Local\\Temp\\tinyfd.hta", _wgetenv(L"USERPROFILE")); } - lIn = fopen(lDialogString, "w"); + lIn = _wfopen(lDialogString, L"w"); if (!lIn) { free(lDialogString); @@ -868,63 +977,61 @@ static char const * inputBoxWinGui( if ( aDefaultInput ) { - strcpy(lDialogString, "Dim result:result=InputBox(\""); - if (aMessage && strlen(aMessage)) + wcscpy(lDialogString, L"Dim result:result=InputBox(\""); + if (aMessage && wcslen(aMessage)) { - strcat(lDialogString, aMessage); + wcscat(lDialogString, aMessage); } - strcat(lDialogString, "\",\""); - if (aTitle && strlen(aTitle)) + wcscat(lDialogString, L"\",\""); + if (aTitle && wcslen(aTitle)) { - strcat(lDialogString, aTitle); + wcscat(lDialogString, aTitle); } - strcat(lDialogString, "\",\""); - if (aDefaultInput && strlen(aDefaultInput)) + wcscat(lDialogString, L"\",\""); + if (aDefaultInput && wcslen(aDefaultInput)) { - strcat(lDialogString, aDefaultInput); + wcscat(lDialogString, aDefaultInput); } - strcat(lDialogString, "\"):If IsEmpty(result) then:WScript.Echo 0"); - strcat(lDialogString, ":Else: WScript.Echo \"1\" & result : End If"); + wcscat(lDialogString, L"\"):If IsEmpty(result) then:WScript.Echo 0"); + wcscat(lDialogString, L":Else: WScript.Echo \"1\" & result : End If"); } else { - sprintf(lDialogString, "\n\ + swprintf(lDialogString, +#if !defined(__GNUC__) || (__GNUC__) >= 5 + lDialogStringLen, +#endif +L"\n\ \n\ \n\ -%s\n\ +%ls\n\ \n\ +WINDOWSTATE = 'hidden'>\n\ \n\ \n\ \n\ \n\ -\n\ +
\n\ \n\ -\n\ \n\ \n\ -
\n\ -%ls\n\ +\n"); + + wcscat(lDialogString, aMessage ? aMessage : L""); + + wcscat(lDialogString, L"\n\ \n\ \n\ @@ -1074,718 +1228,694 @@ End Sub\n\
\n\
\n\ -\n\ +
\n"); + + wcscat(lDialogString, L"\n\ \n\ \n\ \n\
\n\
\n\ +name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\
\n\ \n\ \n\ -" , aTitle ? aTitle : L"", aMessage ? aMessage : L"") ; - } - fputws(lDialogString, lIn); - fclose(lIn); +" ) ; + } + fputws(lDialogString, lIn); + fclose(lIn); - if (aDefaultInput) - { - swprintf(lDialogString, -#if !defined(__GNUC__) || (__GNUC__) >= 5 - lDialogStringLen, + if (aDefaultInput) + { + swprintf(lDialogString, +#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 ) + lDialogStringLen, #endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.txt",_wgetenv(L"USERPROFILE")); - FILE * lala = _wfopen(lDialogString, L"wt, ccs=UNICODE"); - fclose(lala); - - wcscpy(lDialogString, L"cscript.exe "); - wcscat(lDialogString, L"//U //Nologo "); - wcscat(lDialogString, L"%USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.vbs"); - wcscat(lDialogString, L" >> %USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.txt"); - } - else - { - wcscpy(lDialogString, - L"mshta.exe %USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.hta"); - } + L"%ls\\AppData\\Local\\Temp\\tinyfd.txt",_wgetenv(L"USERPROFILE")); - /* printf ( "lDialogString: %s\n" , lDialogString ) ; */ +#ifdef TINYFD_NOCCSUNICODE + lFile = _wfopen(lDialogString, L"w"); + fputc(0xFF, lFile); + fputc(0xFE, lFile); +#else + lFile = _wfopen(lDialogString, L"wt, ccs=UNICODE"); /*or ccs=UTF-16LE*/ +#endif + fclose(lFile); - runSilentW(lDialogString); + 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"); + } + else + { + wcscpy(lDialogString, + L"cmd.exe /c mshta.exe %USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.hta"); + } - if (aDefaultInput) - { - swprintf(lDialogString, -#if !defined(__GNUC__) || (__GNUC__) >= 5 - lDialogStringLen, -#endif - L"%s\\AppData\\Local\\Temp\\tinyfd.txt", _wgetenv(L"USERPROFILE")); - if (!(lIn = _wfopen(lDialogString, L"rt, ccs=UNICODE"))) - { - _wremove(lDialogString); - free(lDialogString); - return NULL; - } + /* wprintf ( "lDialogString: %ls\n" , lDialogString ) ; */ - fgetws(lBuff, MAX_PATH_OR_CMD, lIn); - fclose(lIn); - _wremove(lDialogString); + hiddenConsoleW(lDialogString, aTitle, 1); - swprintf(lDialogString, -#if !defined(__GNUC__) || (__GNUC__) >= 5 - lDialogStringLen, + swprintf(lDialogString, +#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 ) + lDialogStringLen, #endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.vbs", - _wgetenv(L"USERPROFILE")); - } - else - { - swprintf(lDialogString, -#if !defined(__GNUC__) || (__GNUC__) >= 5 - lDialogStringLen, + L"%ls\\AppData\\Local\\Temp\\tinyfd.txt", _wgetenv(L"USERPROFILE")); + /* wprintf(L"lDialogString: %ls\n", lDialogString); */ +#ifdef TINYFD_NOCCSUNICODE + if (!(lIn = _wfopen(lDialogString, L"r"))) +#else + if (!(lIn = _wfopen(lDialogString, L"rt, ccs=UNICODE"))) /*or ccs=UTF-16LE*/ #endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.txt", - _wgetenv(L"USERPROFILE")); - if (!(lIn = _wfopen(lDialogString, L"rt, ccs=UNICODE"))) { - _wremove(lDialogString); - free(lDialogString); - return NULL; - } - + _wremove(lDialogString); + free(lDialogString); + return NULL; + } +#ifdef TINYFD_NOCCSUNICODE + fgets((char *)lBuff, 2*MAX_PATH_OR_CMD, lIn); +#else fgetws(lBuff, MAX_PATH_OR_CMD, lIn); +#endif fclose(lIn); - wipefileW(lDialogString); _wremove(lDialogString); - swprintf(lDialogString, -#if !defined(__GNUC__) || (__GNUC__) >= 5 - lDialogStringLen, -#endif - L"%s\\AppData\\Local\\Temp\\tinyfd.hta", - _wgetenv(L"USERPROFILE")); - } - _wremove(lDialogString); - free(lDialogString); - /* printf( "aoBuff: %s\n" , aoBuff ) ; */ - lResult = wcsncmp(lBuff, L"1", 1) ? 0 : 1; - - /* printf( "lResult: %d \n" , lResult ) ; */ - if (!lResult) - { - return NULL ; - } -// if (aoBuff[wcslen(aoBuff) - 1] == '\n') -// { -// aoBuff[wcslen(aoBuff) - 1] = '\0'; -// } + if (aDefaultInput) + { + swprintf(lDialogString, +#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 ) + lDialogStringLen, +#endif + L"%ls\\AppData\\Local\\Temp\\tinyfd.vbs", + _wgetenv(L"USERPROFILE")); + } + else + { + swprintf(lDialogString, +#if !defined(__BORLANDC__) && !defined(__TINYC__) && ( !defined(__GNUC__) || (__GNUC__) >= 5 ) + lDialogStringLen, +#endif + L"%ls\\AppData\\Local\\Temp\\tinyfd.hta", + _wgetenv(L"USERPROFILE")); + } + _wremove(lDialogString); + free(lDialogString); + /* wprintf( L"lBuff: %ls\n" , lBuff ) ; */ +#ifdef TINYFD_NOCCSUNICODE + lResult = !wcsncmp(lBuff+1, L"1", 1); +#else + lResult = !wcsncmp(lBuff, L"1", 1); +#endif + + /* printf( "lResult: %d \n" , lResult ) ; */ + if (!lResult) + { + return NULL ; + } - /* printf( "aoBuff+1: %s\n" , aoBuff+1 ) ; */ - return lBuff + 1 ; + /* wprintf( "lBuff+1: %ls\n" , lBuff+1 ) ; */ +#ifdef TINYFD_NOCCSUNICODE + return lBuff + 2; +#else + return lBuff + 1; +#endif } -static char const * inputBoxWinGui8( - 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 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; - - lTitle = utf8to16(aTitle); - lMessage = utf8to16(aMessage); - lDefaultInput = utf8to16(aDefaultInput); - - lTmpWChar = tinyfd_inputBoxW( - lTitle, - lMessage, - lDefaultInput); - - free(lTitle); - free(lMessage); - free(lDefaultInput); - - if (!lTmpWChar) - { - return NULL; - } + wchar_t * lTitle; + wchar_t * lMessage; + wchar_t * lDefaultInput; + wchar_t const * lTmpWChar; + char * lTmpChar; - lTmpChar = utf16to8(lTmpWChar); - strcpy(aoBuff, lTmpChar); - free(lTmpChar); + if (tinyfd_winUtf8) + { + lTitle = utf8to16(aTitle); + lMessage = utf8to16(aMessage); + lDefaultInput = utf8to16(aDefaultInput); + } + else + { + lTitle = mbcsTo16(aTitle); + lMessage = mbcsTo16(aMessage); + lDefaultInput = mbcsTo16(aDefaultInput); + } - return aoBuff; -} + lTmpWChar = tinyfd_inputBoxW( lTitle, lMessage, lDefaultInput); + free(lTitle); + free(lMessage); + free(lDefaultInput); -static char const * inputBoxWinGuiA( - 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; - - 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 (!lTmpWChar) + { + return NULL; + } - lTmpChar = utf16toMbcs(lTmpWChar); - strcpy(aoBuff, lTmpChar); - free(lTmpChar); + if (tinyfd_winUtf8) + { + lTmpChar = utf16to8(lTmpWChar); + } + else + { + lTmpChar = utf16toMbcs(lTmpWChar); + } + strcpy(aoBuff, lTmpChar); + free(lTmpChar); - return aoBuff; + 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 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" */ { - static wchar_t lBuff[MAX_PATH_OR_CMD]; - wchar_t lDirname[MAX_PATH_OR_CMD]; - wchar_t lDialogString[MAX_PATH_OR_CMD]; - wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L""; - wchar_t * p; - wchar_t * lRetval; - int i; - HRESULT lHResult; - OPENFILENAMEW ofn = {0}; + static wchar_t lBuff[MAX_PATH_OR_CMD]; + wchar_t lDirname[MAX_PATH_OR_CMD]; + wchar_t lDialogString[MAX_PATH_OR_CMD]; + wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L""; + wchar_t * p; + wchar_t * lRetval; + int i; + HRESULT lHResult; + OPENFILENAMEW ofn = {0}; - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windowsW"); return (wchar_t const *)1; } + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t const *)1; } - lHResult = CoInitializeEx(NULL, 0); + lHResult = CoInitializeEx(NULL, 0); - getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile); - getLastNameW(lBuff, aDefaultPathAndFile); + getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile); + getLastNameW(lBuff, aDefaultPathAndFile); - if (aNumOfFilterPatterns > 0) - { - if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) - { - wcscpy(lFilterPatterns, aSingleFilterDescription); - wcscat(lFilterPatterns, L"\n"); - } - wcscat(lFilterPatterns, aFilterPatterns[0]); - for (i = 1; i < aNumOfFilterPatterns; i++) - { - wcscat(lFilterPatterns, L";"); - wcscat(lFilterPatterns, aFilterPatterns[i]); - } - wcscat(lFilterPatterns, L"\n"); - if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription))) - { - wcscpy(lDialogString, lFilterPatterns); - wcscat(lFilterPatterns, lDialogString); - } - wcscat(lFilterPatterns, L"All Files\n*.*\n"); - p = lFilterPatterns; - while ((p = wcschr(p, L'\n')) != NULL) - { - *p = L'\0'; - p++; - } - } + if (aNumOfFilterPatterns > 0) + { + if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) + { + wcscpy(lFilterPatterns, aSingleFilterDescription); + wcscat(lFilterPatterns, L"\n"); + } + wcscat(lFilterPatterns, aFilterPatterns[0]); + for (i = 1; i < aNumOfFilterPatterns; i++) + { + wcscat(lFilterPatterns, L";"); + wcscat(lFilterPatterns, aFilterPatterns[i]); + } + wcscat(lFilterPatterns, L"\n"); + if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription))) + { + wcscpy(lDialogString, lFilterPatterns); + wcscat(lFilterPatterns, lDialogString); + } + wcscat(lFilterPatterns, L"All Files\n*.*\n"); + p = lFilterPatterns; + while ((p = wcschr(p, L'\n')) != NULL) + { + *p = L'\0'; + p++; + } + } - ofn.lStructSize = sizeof(OPENFILENAMEW); - ofn.hwndOwner = 0; - ofn.hInstance = 0; - ofn.lpstrFilter = lFilterPatterns && wcslen(lFilterPatterns) ? lFilterPatterns : NULL; - ofn.lpstrCustomFilter = NULL; - ofn.nMaxCustFilter = 0; - ofn.nFilterIndex = 1; - ofn.lpstrFile = lBuff; - - ofn.nMaxFile = MAX_PATH_OR_CMD; - ofn.lpstrFileTitle = NULL; - ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT; - ofn.lpstrInitialDir = lDirname && wcslen(lDirname) ? lDirname : NULL; - ofn.lpstrTitle = aTitle && wcslen(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 (GetSaveFileNameW(&ofn) == 0) - { - lRetval = NULL; - } - else - { - lRetval = lBuff; - } + ofn.lStructSize = sizeof(OPENFILENAMEW); + ofn.hwndOwner = GetForegroundWindow(); + ofn.hInstance = 0; + ofn.lpstrFilter = lFilterPatterns && wcslen(lFilterPatterns) ? lFilterPatterns : NULL; + ofn.lpstrCustomFilter = NULL; + ofn.nMaxCustFilter = 0; + ofn.nFilterIndex = 1; + ofn.lpstrFile = lBuff; + + ofn.nMaxFile = MAX_PATH_OR_CMD; + ofn.lpstrFileTitle = NULL; + ofn.nMaxFileTitle = MAX_PATH_OR_CMD/2; + ofn.lpstrInitialDir = lDirname && 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.lCustData = 0L; + ofn.lpfnHook = NULL; + ofn.lpTemplateName = NULL; + + if (GetSaveFileNameW(&ofn) == 0) + { + lRetval = NULL; + } + else + { + lRetval = lBuff; + } - if (lHResult == S_OK || lHResult == S_FALSE) - { - CoUninitialize(); - } - return lRetval; + if (lHResult == S_OK || lHResult == S_FALSE) + { + CoUninitialize(); + } + return lRetval; } 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" */ + 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 * 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]); - } + 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); + 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; - } + if (!lTmpWChar) + { + return NULL; + } - lTmpChar = utf16to8(lTmpWChar); - strcpy(aoBuff, lTmpChar); - free(lTmpChar); + lTmpChar = utf16to8(lTmpWChar); + strcpy(aoBuff, lTmpChar); + free(lTmpChar); - return aoBuff; + 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 */ + 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; - int i, j; - size_t lBuffLen; - HRESULT lHResult; - OPENFILENAMEW ofn = { 0 }; - - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windowsW"); return (wchar_t const *)1; } - - lHResult = CoInitializeEx(NULL, 0); - - getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile); - getLastNameW(lBuff, aDefaultPathAndFile); - - if (aNumOfFilterPatterns > 0) - { - if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) - { - wcscpy(lFilterPatterns, aSingleFilterDescription); - wcscat(lFilterPatterns, L"\n"); - } - wcscat(lFilterPatterns, aFilterPatterns[0]); - for (i = 1; i < aNumOfFilterPatterns; i++) - { - wcscat(lFilterPatterns, L";"); - wcscat(lFilterPatterns, aFilterPatterns[i]); - } - wcscat(lFilterPatterns, L"\n"); - if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription))) - { - wcscpy(lDialogString, lFilterPatterns); - wcscat(lFilterPatterns, lDialogString); - } - wcscat(lFilterPatterns, L"All Files\n*.*\n"); - p = lFilterPatterns; - while ((p = wcschr(p, L'\n')) != NULL) - { - *p = L'\0'; - p++; - } - } + 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; + int i, j; + size_t lBuffLen; + HRESULT lHResult; + OPENFILENAMEW ofn = { 0 }; + + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t const *)1; } + + lHResult = CoInitializeEx(NULL, 0); + + getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile); + getLastNameW(lBuff, aDefaultPathAndFile); + + if (aNumOfFilterPatterns > 0) + { + if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) + { + wcscpy(lFilterPatterns, aSingleFilterDescription); + wcscat(lFilterPatterns, L"\n"); + } + wcscat(lFilterPatterns, aFilterPatterns[0]); + for (i = 1; i < aNumOfFilterPatterns; i++) + { + wcscat(lFilterPatterns, L";"); + wcscat(lFilterPatterns, aFilterPatterns[i]); + } + wcscat(lFilterPatterns, L"\n"); + if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription))) + { + wcscpy(lDialogString, lFilterPatterns); + wcscat(lFilterPatterns, lDialogString); + } + wcscat(lFilterPatterns, L"All Files\n*.*\n"); + p = lFilterPatterns; + while ((p = wcschr(p, L'\n')) != NULL) + { + *p = L'\0'; + p++; + } + } - ofn.lStructSize = sizeof(OPENFILENAME); - ofn.hwndOwner = 0; - ofn.hInstance = 0; - ofn.lpstrFilter = lFilterPatterns && wcslen(lFilterPatterns) ? lFilterPatterns : NULL; - ofn.lpstrCustomFilter = NULL; - ofn.nMaxCustFilter = 0; - ofn.nFilterIndex = 1; - ofn.lpstrFile = lBuff; - ofn.nMaxFile = MAX_PATH_OR_CMD; - ofn.lpstrFileTitle = NULL; - ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT; - ofn.lpstrInitialDir = lDirname && wcslen(lDirname) ? lDirname : NULL; - ofn.lpstrTitle = aTitle && wcslen(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; - } + ofn.lStructSize = sizeof(OPENFILENAME); + ofn.hwndOwner = GetForegroundWindow(); + ofn.hInstance = 0; + ofn.lpstrFilter = lFilterPatterns && wcslen(lFilterPatterns) ? lFilterPatterns : NULL; + ofn.lpstrCustomFilter = NULL; + ofn.nMaxCustFilter = 0; + ofn.nFilterIndex = 1; + ofn.lpstrFile = lBuff; + ofn.nMaxFile = MAX_PATH_OR_CMD; + ofn.lpstrFileTitle = NULL; + ofn.nMaxFileTitle = MAX_PATH_OR_CMD / 2; + ofn.lpstrInitialDir = lDirname && wcslen(lDirname) ? lDirname : NULL; + ofn.lpstrTitle = aTitle && wcslen(aTitle) ? aTitle : NULL; + ofn.Flags = OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; + 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 (GetOpenFileNameW(&ofn) == 0) - { - lRetval = NULL; - } - else - { - lBuffLen = wcslen(lBuff); - lPointers[0] = lBuff + lBuffLen + 1; - if (!aAllowMultipleSelects || (lPointers[0][0] == L'\0')) - { - lRetval = lBuff; - } - else - { - 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; - } - } + if (GetOpenFileNameW(&ofn) == 0) + { + lRetval = NULL; + } + else + { + lBuffLen = wcslen(lBuff); + lPointers[0] = lBuff + lBuffLen + 1; + if (!aAllowMultipleSelects || (lPointers[0][0] == L'\0')) + { + lRetval = lBuff; + } + else + { + 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; + } + } - if (lHResult == S_OK || lHResult == S_FALSE) - { - CoUninitialize(); - } - return lRetval; + if (lHResult == S_OK || lHResult == S_FALSE) + { + CoUninitialize(); + } + return lRetval; } 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 */ + 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 */ { - 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]); - } + 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); + 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; - } + if (!lTmpWChar) + { + return NULL; + } - lTmpChar = utf16to8(lTmpWChar); - strcpy(aoBuff, lTmpChar); - free(lTmpChar); + lTmpChar = utf16to8(lTmpWChar); + strcpy(aoBuff, lTmpChar); + free(lTmpChar); - return aoBuff; + return aoBuff; } #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; + 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; + if (uMsg == BFFM_INITIALIZED) + { + SendMessage(hwnd, BFFM_SETSELECTIONW, TRUE, (LPARAM)pData); + } + return 0; } wchar_t const * tinyfd_selectFolderDialogW( - wchar_t const * const aTitle, /* NULL or "" */ - wchar_t const * const aDefaultPath) /* NULL or "" */ + wchar_t const * const aTitle, /* NULL or "" */ + wchar_t const * const aDefaultPath) /* NULL or "" */ { - static wchar_t lBuff[MAX_PATH_OR_CMD]; - - BROWSEINFOW bInfo; - LPITEMIDLIST lpItem; - HRESULT lHResult; + static wchar_t lBuff[MAX_PATH_OR_CMD]; + + BROWSEINFOW bInfo; + LPITEMIDLIST lpItem; + HRESULT lHResult; - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windowsW"); return (wchar_t const *)1; } + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t const *)1; } - lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); + lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); - bInfo.hwndOwner = 0; - bInfo.pidlRoot = NULL; - bInfo.pszDisplayName = lBuff; - bInfo.lpszTitle = aTitle && wcslen(aTitle) ? aTitle : NULL; - if (lHResult == S_OK || lHResult == S_FALSE) - { - bInfo.ulFlags = BIF_USENEWUI; - } - bInfo.lpfn = BrowseCallbackProcW; - bInfo.lParam = (LPARAM)aDefaultPath; - bInfo.iImage = -1; + bInfo.hwndOwner = GetForegroundWindow(); + bInfo.pidlRoot = NULL; + bInfo.pszDisplayName = lBuff; + bInfo.lpszTitle = aTitle && wcslen(aTitle) ? aTitle : NULL; + if (lHResult == S_OK || lHResult == S_FALSE) + { + bInfo.ulFlags = BIF_USENEWUI; + } + bInfo.lpfn = BrowseCallbackProcW; + bInfo.lParam = (LPARAM)aDefaultPath; + bInfo.iImage = -1; - lpItem = SHBrowseForFolderW(&bInfo); - if (lpItem) - { - SHGetPathFromIDListW(lpItem, lBuff); - } + lpItem = SHBrowseForFolderW(&bInfo); + if (lpItem) + { + SHGetPathFromIDListW(lpItem, lBuff); + } - if (lHResult == S_OK || lHResult == S_FALSE) - { - CoUninitialize(); - } - return lBuff; + if (lHResult == S_OK || lHResult == S_FALSE) + { + CoUninitialize(); + } + return lBuff; } static char const * selectFolderDialogWinGui8( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) /* NULL or "" */ + 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; + wchar_t * lTitle; + wchar_t * lDefaultPath; + wchar_t const * lTmpWChar; + char * lTmpChar; - lTitle = utf8to16(aTitle); - lDefaultPath = utf8to16(aDefaultPath); + lTitle = utf8to16(aTitle); + lDefaultPath = utf8to16(aDefaultPath); - lTmpWChar = tinyfd_selectFolderDialogW( - lTitle, - lDefaultPath); + lTmpWChar = tinyfd_selectFolderDialogW( + lTitle, + lDefaultPath); - free(lTitle); - free(lDefaultPath); - if (!lTmpWChar) - { - return NULL; - } + free(lTitle); + free(lDefaultPath); + if (!lTmpWChar) + { + return NULL; + } - lTmpChar = utf16to8(lTmpWChar); - strcpy(aoBuff, lTmpChar); - free(lTmpChar); + lTmpChar = utf16to8(lTmpWChar); + strcpy(aoBuff, lTmpChar); + free(lTmpChar); - return aoBuff; + 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"*/ - unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ - unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ + wchar_t const * const aTitle, /* NULL or "" */ + wchar_t const * const aDefaultHexRGB, /* NULL or "#FF0000"*/ + unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ + unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ { - static wchar_t lResultHexRGB[8]; - CHOOSECOLORW cc; - COLORREF crCustColors[16]; - unsigned char lDefaultRGB[3]; - int lRet; + static wchar_t lResultHexRGB[8]; + CHOOSECOLORW cc; + COLORREF crCustColors[16]; + unsigned char lDefaultRGB[3]; + int lRet; - HRESULT lHResult; + HRESULT lHResult; - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windowsW"); return (wchar_t const *)1; } + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t const *)1; } - lHResult = CoInitializeEx(NULL, 0); + lHResult = CoInitializeEx(NULL, 0); - if (aDefaultHexRGB) - { - Hex2RGBW(aDefaultHexRGB, lDefaultRGB); - } - else - { - lDefaultRGB[0] = aDefaultRGB[0]; - lDefaultRGB[1] = aDefaultRGB[1]; - lDefaultRGB[2] = aDefaultRGB[2]; - } + if (aDefaultHexRGB) + { + Hex2RGBW(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 = NULL; - 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; + /* 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_ANYCOLOR ; + cc.lCustData = 0; + cc.lpfnHook = NULL; + cc.lpTemplateName = NULL; - lRet = ChooseColorW(&cc); + lRet = ChooseColorW(&cc); - if (!lRet) - { - return NULL; - } + if (!lRet) + { + return NULL; + } - aoResultRGB[0] = GetRValue(cc.rgbResult); - aoResultRGB[1] = GetGValue(cc.rgbResult); - aoResultRGB[2] = GetBValue(cc.rgbResult); + aoResultRGB[0] = GetRValue(cc.rgbResult); + aoResultRGB[1] = GetGValue(cc.rgbResult); + aoResultRGB[2] = GetBValue(cc.rgbResult); - RGB2HexW(aoResultRGB, lResultHexRGB); + RGB2HexW(aoResultRGB, lResultHexRGB); - if (lHResult == S_OK || lHResult == S_FALSE) - { - CoUninitialize(); - } + if (lHResult == S_OK || lHResult == S_FALSE) + { + CoUninitialize(); + } - return lResultHexRGB; + return lResultHexRGB; } static char const * colorChooserWinGui8( - 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 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]; + static char lResultHexRGB[8]; - wchar_t * lTitle; - wchar_t * lDefaultHexRGB; - wchar_t const * lTmpWChar; - char * lTmpChar; + wchar_t * lTitle; + wchar_t * lDefaultHexRGB; + wchar_t const * lTmpWChar; + char * lTmpChar; - lTitle = utf8to16(aTitle); - lDefaultHexRGB = utf8to16(aDefaultHexRGB); + lTitle = utf8to16(aTitle); + lDefaultHexRGB = utf8to16(aDefaultHexRGB); - lTmpWChar = tinyfd_colorChooserW( - lTitle, - lDefaultHexRGB, - aDefaultRGB, - aoResultRGB ); + lTmpWChar = tinyfd_colorChooserW( + lTitle, + lDefaultHexRGB, + aDefaultRGB, + aoResultRGB ); - free(lTitle); - free(lDefaultHexRGB); - if (!lTmpWChar) - { - return NULL; - } + free(lTitle); + free(lDefaultHexRGB); + if (!lTmpWChar) + { + return NULL; + } - lTmpChar = utf16to8(lTmpWChar); - strcpy(lResultHexRGB, lTmpChar); - free(lTmpChar); + lTmpChar = utf16to8(lTmpWChar); + strcpy(lResultHexRGB, lTmpChar); + free(lTmpChar); - return lResultHexRGB; + return lResultHexRGB; } @@ -1796,174 +1926,176 @@ static int messageBoxWinGuiA( 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; + 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 ( 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 ; - } + 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 ; + } - lBoxReturnValue = MessageBoxA(NULL, aMessage, aTitle, aCode); + aCode += MB_TOPMOST; - if (((aDialogType && !strcmp("yesnocancel", aDialogType)) - && (lBoxReturnValue == IDNO))) - { - return 2; - } + lBoxReturnValue = MessageBoxA(GetForegroundWindow(), aMessage, aTitle, aCode); - if ( ( ( aDialogType - && strcmp("yesnocancel", aDialogType) - && strcmp("okcancel", aDialogType) - && strcmp("yesno", aDialogType))) - || (lBoxReturnValue == IDOK) - || (lBoxReturnValue == IDYES) ) - { - return 1 ; - } - else - { - return 0 ; - } + 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 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); + 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 ++ ; - } - } + 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 = 0 ; - 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_FNAME + _MAX_EXT ; - 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 ; - } + 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 ; + if (lHResult==S_OK || lHResult==S_FALSE) + { + CoUninitialize(); + } + return lRetval ; } static char const * openFileDialogWinGuiA( - char * const aoBuff , + char * const aoBuff , char const * const aTitle , /* NULL or "" */ char const * const aDefaultPathAndFile , /* NULL or "" */ int const aNumOfFilterPatterns , /* 0 */ @@ -1971,248 +2103,248 @@ static char const * openFileDialogWinGuiA( 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 ++ ; - } - } + 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 = 0 ; - 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_FNAME + _MAX_EXT ; - 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; - } + 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 ( 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; + 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 "" */ + char * const aoBuff , + char const * const aTitle , /* NULL or "" */ + char const * const aDefaultPath ) /* NULL or "" */ { - BROWSEINFOA bInfo ; - LPITEMIDLIST lpItem ; - HRESULT lHResult; - - lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); - - /* we can't use aDefaultPath */ - bInfo.hwndOwner = 0 ; - 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 ; + BROWSEINFOA bInfo ; + LPITEMIDLIST lpItem ; + HRESULT lHResult; + + 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 ) ; - } + lpItem = SHBrowseForFolderA( & bInfo ) ; + if ( lpItem ) + { + SHGetPathFromIDListA( lpItem , aoBuff ) ; + } - if (lHResult==S_OK || lHResult==S_FALSE) - { - CoUninitialize(); - } - return aoBuff ; + if (lHResult==S_OK || lHResult==S_FALSE) + { + CoUninitialize(); + } + return aoBuff ; } #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 } */ + 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]; + static char lResultHexRGB[8]; - CHOOSECOLORA cc; - COLORREF crCustColors[16]; - unsigned char lDefaultRGB[3]; - int lRet; + 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]; - } + 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 = NULL ; - 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; + /* 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); + lRet = ChooseColorA(&cc); - if ( ! lRet ) - { - return NULL; - } + if ( ! lRet ) + { + return NULL; + } - aoResultRGB[0] = GetRValue(cc.rgbResult); - aoResultRGB[1] = GetGValue(cc.rgbResult); - aoResultRGB[2] = GetBValue(cc.rgbResult); + aoResultRGB[0] = GetRValue(cc.rgbResult); + aoResultRGB[1] = GetGValue(cc.rgbResult); + aoResultRGB[2] = GetBValue(cc.rgbResult); - RGB2Hex(aoResultRGB, lResultHexRGB); + RGB2Hex(aoResultRGB, lResultHexRGB); - return lResultHexRGB; + return lResultHexRGB; } #endif /* TINYFD_NOLIB */ static int dialogPresent( ) { - static int lDialogPresent = -1 ; - char lBuff [MAX_PATH_OR_CMD] ; - FILE * lIn ; - char const * lString = "dialog.exe"; - if ( lDialogPresent < 0 ) - { - if (!(lIn = _popen("where dialog.exe","r"))) - { - lDialogPresent = 0 ; - return 0 ; - } - while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - {} - _pclose( lIn ) ; - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - if ( strcmp(lBuff+strlen(lBuff)-strlen(lString),lString) ) - { - lDialogPresent = 0 ; - } - else - { - lDialogPresent = 1 ; - } - } - return lDialogPresent; + static int lDialogPresent = -1 ; + char lBuff [MAX_PATH_OR_CMD] ; + FILE * lIn ; + char const * lString = "dialog.exe"; + if ( lDialogPresent < 0 ) + { + if (!(lIn = _popen("where dialog.exe","r"))) + { + lDialogPresent = 0 ; + return 0 ; + } + while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + {} + _pclose( lIn ) ; + if ( lBuff[strlen( lBuff ) -1] == '\n' ) + { + lBuff[strlen( lBuff ) -1] = '\0' ; + } + if ( strcmp(lBuff+strlen(lBuff)-strlen(lString),lString) ) + { + lDialogPresent = 0 ; + } + else + { + lDialogPresent = 1 ; + } + } + return lDialogPresent; } @@ -2223,542 +2355,558 @@ static int messageBoxWinConsole( 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 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) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } + 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) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } - if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) - || !strcmp("yesno", aDialogType) || !strcmp("yesnocancel", aDialogType) ) ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, "tab: move focus") ; - strcat(lDialogString, "\" ") ; - } + if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) + || !strcmp("yesno", aDialogType) || !strcmp("yesnocancel", aDialogType) ) ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab: move focus") ; + strcat(lDialogString, "\" ") ; + } - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - if ( ! aDefaultButton ) - { - strcat( lDialogString , "--defaultno " ) ; - } - strcat( lDialogString , - "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ; - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - if ( ! aDefaultButton ) - { - strcat( lDialogString , "--defaultno " ) ; - } - strcat( lDialogString , "--yesno " ) ; - } - else if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - if (!aDefaultButton) - { - strcat(lDialogString, "--defaultno "); - } - strcat(lDialogString, "--menu "); - } - else - { - strcat( lDialogString , "--msgbox " ) ; - } + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + if ( ! aDefaultButton ) + { + strcat( lDialogString , "--defaultno " ) ; + } + strcat( lDialogString , + "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ; + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + if ( ! aDefaultButton ) + { + strcat( lDialogString , "--defaultno " ) ; + } + strcat( lDialogString , "--yesno " ) ; + } + else if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + if (!aDefaultButton) + { + strcat(lDialogString, "--defaultno "); + } + strcat(lDialogString, "--menu "); + } + else + { + strcat( lDialogString , "--msgbox " ) ; + } - strcat( lDialogString , "\"" ) ; - if ( aMessage && strlen(aMessage) ) - { - replaceSubStr( aMessage , "\n" , "\\n" , lBuff ) ; - strcat(lDialogString, lBuff) ; - lBuff[0]='\0'; - } - strcat(lDialogString, "\" "); + strcat( lDialogString , "\"" ) ; + if ( aMessage && strlen(aMessage) ) + { + replaceSubStr( aMessage , "\n" , "\\n" , lBuff ) ; + strcat(lDialogString, lBuff) ; + lBuff[0]='\0'; + } + strcat(lDialogString, "\" "); - if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - strcat(lDialogString, "0 60 0 Yes \"\" No \"\""); - strcat(lDialogString, "2>>"); - } - else - { - strcat(lDialogString, "10 60"); - strcat(lDialogString, " && echo 1 > "); - } + if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + strcat(lDialogString, "0 60 0 Yes \"\" No \"\""); + strcat(lDialogString, "2>>"); + } + else + { + strcat(lDialogString, "10 60"); + strcat(lDialogString, " && echo 1 > "); + } - strcpy(lDialogFile, getenv("USERPROFILE")); - strcat(lDialogFile, "\\AppData\\Local\\Temp\\tinyfd.txt"); - strcat(lDialogString, lDialogFile); + strcpy(lDialogFile, getenv("USERPROFILE")); + strcat(lDialogFile, "\\AppData\\Local\\Temp\\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) - {} - fclose(lIn); - remove(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) + {} + fclose(lIn); + remove(lDialogFile); if ( lBuff[strlen( lBuff ) -1] == '\n' ) { - lBuff[strlen( lBuff ) -1] = '\0' ; + lBuff[strlen( lBuff ) -1] = '\0' ; } - /* if (tinyfd_verbose) printf("lBuff: %s\n", lBuff); */ - if ( ! strlen(lBuff) ) - { - return 0; - } + /* if (tinyfd_verbose) printf("lBuff: %s\n", lBuff); */ + if ( ! strlen(lBuff) ) + { + return 0; + } - if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - if (lBuff[0] == 'Y') return 1; - else return 2; - } + if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + if (lBuff[0] == 'Y') return 1; + else return 2; + } - return 1; + return 1; } 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 */ + 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 */ { - 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(lDialogString , "echo|set /p=1 >" ) ; - strcat(lDialogString, lDialogFile); - strcat( lDialogString , " & " ) ; - - strcat( lDialogString , "dialog " ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } + 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(lDialogString , "echo|set /p=1 >" ) ; + strcat(lDialogString, lDialogFile); + strcat( lDialogString , " & " ) ; + + strcat( lDialogString , "dialog " ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, "tab: move focus") ; - if ( ! aDefaultInput ) - { - strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ; - } + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab: move focus") ; + if ( ! aDefaultInput ) + { + strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ; + } - strcat(lDialogString, "\" ") ; + strcat(lDialogString, "\" ") ; - if ( ! aDefaultInput ) - { - strcat( lDialogString , "--insecure --passwordbox" ) ; - } - else - { - strcat( lDialogString , "--inputbox" ) ; - } - strcat( lDialogString , " \"" ) ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat(lDialogString,"\" 10 60 ") ; - if ( aDefaultInput && strlen(aDefaultInput) ) - { - strcat(lDialogString, "\"") ; - strcat(lDialogString, aDefaultInput) ; - strcat(lDialogString, "\" ") ; - } + if ( ! aDefaultInput ) + { + strcat( lDialogString , "--insecure --passwordbox" ) ; + } + else + { + strcat( lDialogString , "--inputbox" ) ; + } + strcat( lDialogString , " \"" ) ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString,"\" 10 60 ") ; + if ( aDefaultInput && strlen(aDefaultInput) ) + { + strcat(lDialogString, "\"") ; + strcat(lDialogString, aDefaultInput) ; + strcat(lDialogString, "\" ") ; + } - strcat(lDialogString, "2>>"); - strcpy(lDialogFile, getenv("USERPROFILE")); - strcat(lDialogFile, "\\AppData\\Local\\Temp\\tinyfd.txt"); - strcat(lDialogString, lDialogFile); - strcat(lDialogString, " || echo 0 > "); - strcat(lDialogString, lDialogFile); + strcat(lDialogString, "2>>"); + strcpy(lDialogFile, getenv("USERPROFILE")); + strcat(lDialogFile, "\\AppData\\Local\\Temp\\tinyfd.txt"); + strcat(lDialogString, lDialogFile); + strcat(lDialogString, " || echo 0 > "); + strcat(lDialogString, lDialogFile); - /* printf( "lDialogString: %s\n" , lDialogString ) ; */ - system( lDialogString ) ; + /* printf( "lDialogString: %s\n" , lDialogString ) ; */ + system( lDialogString ) ; - if (!(lIn = fopen(lDialogFile, "r"))) - { - remove(lDialogFile); - return 0 ; - } - while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) - {} - fclose(lIn); + if (!(lIn = fopen(lDialogFile, "r"))) + { + remove(lDialogFile); + return 0 ; + } + while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) + {} + fclose(lIn); - wipefile(lDialogFile); - remove(lDialogFile); + wipefile(lDialogFile); + remove(lDialogFile); if ( aoBuff[strlen( aoBuff ) -1] == '\n' ) { - aoBuff[strlen( aoBuff ) -1] = '\0' ; + aoBuff[strlen( aoBuff ) -1] = '\0' ; } - /* printf( "aoBuff: %s\n" , aoBuff ) ; */ + /* printf( "aoBuff: %s\n" , aoBuff ) ; */ - /* printf( "aoBuff: %s len: %lu \n" , aoBuff , strlen(aoBuff) ) ; */ + /* printf( "aoBuff: %s len: %lu \n" , aoBuff , strlen(aoBuff) ) ; */ lResult = strncmp( aoBuff , "1" , 1) ? 0 : 1 ; - /* printf( "lResult: %d \n" , lResult ) ; */ + /* printf( "lResult: %d \n" , lResult ) ; */ if ( ! lResult ) { - return NULL ; - } - /* printf( "aoBuff+1: %s\n" , aoBuff+1 ) ; */ - return aoBuff+3 ; + return NULL ; + } + /* printf( "aoBuff+1: %s\n" , aoBuff+1 ) ; */ + return aoBuff+3 ; } static char const * saveFileDialogWinConsole( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile ) /* NULL or "" */ + char * const aoBuff , + char const * const aTitle , /* NULL or "" */ + char const * const aDefaultPathAndFile ) /* NULL or "" */ { - char lDialogString[MAX_PATH_OR_CMD]; - char lPathAndFile[MAX_PATH_OR_CMD] = ""; - FILE * lIn; + char lDialogString[MAX_PATH_OR_CMD]; + char lPathAndFile[MAX_PATH_OR_CMD] = ""; + FILE * lIn; - strcpy( lDialogString , "dialog " ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; - - strcat( lDialogString , "--fselect \"" ) ; - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - /* dialog.exe uses unix separators even on windows */ - strcpy(lPathAndFile, aDefaultPathAndFile); - replaceChr( lPathAndFile , '\\' , '/' ) ; - } - - /* dialog.exe needs at least one separator */ - if ( ! strchr(lPathAndFile, '/') ) - { - strcat(lDialogString, "./") ; - } - strcat(lDialogString, lPathAndFile) ; - strcat(lDialogString, "\" 0 60 2>"); - strcpy(lPathAndFile, getenv("USERPROFILE")); - strcat(lPathAndFile, "\\AppData\\Local\\Temp\\tinyfd.txt"); - strcat(lDialogString, lPathAndFile); + strcpy( lDialogString , "dialog " ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; + + strcat( lDialogString , "--fselect \"" ) ; + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + /* dialog.exe uses unix separators even on windows */ + strcpy(lPathAndFile, aDefaultPathAndFile); + replaceChr( lPathAndFile , '\\' , '/' ) ; + } + + /* dialog.exe needs at least one separator */ + if ( ! strchr(lPathAndFile, '/') ) + { + strcat(lDialogString, "./") ; + } + strcat(lDialogString, lPathAndFile) ; + strcat(lDialogString, "\" 0 60 2>"); + strcpy(lPathAndFile, getenv("USERPROFILE")); + strcat(lPathAndFile, "\\AppData\\Local\\Temp\\tinyfd.txt"); + strcat(lDialogString, lPathAndFile); - /* printf( "lDialogString: %s\n" , lDialogString ) ; */ - system( lDialogString ) ; + /* printf( "lDialogString: %s\n" , lDialogString ) ; */ + system( lDialogString ) ; - if (!(lIn = fopen(lPathAndFile, "r"))) - { - remove(lPathAndFile); - return NULL; - } - while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) - {} - fclose(lIn); - remove(lPathAndFile); - replaceChr( aoBuff , '/' , '\\' ) ; - /* printf( "aoBuff: %s\n" , aoBuff ) ; */ - getLastName(lDialogString,aoBuff); - if ( ! strlen(lDialogString) ) - { - return NULL; - } - return aoBuff; + if (!(lIn = fopen(lPathAndFile, "r"))) + { + remove(lPathAndFile); + return NULL; + } + while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) + {} + fclose(lIn); + remove(lPathAndFile); + replaceChr( aoBuff , '/' , '\\' ) ; + /* printf( "aoBuff: %s\n" , aoBuff ) ; */ + getLastName(lDialogString,aoBuff); + if ( ! strlen(lDialogString) ) + { + return NULL; + } + return aoBuff; } 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 */ + char * const aoBuff , + char const * const aTitle , /* NULL or "" */ + char const * const aDefaultPathAndFile , /* NULL or "" */ + int const aAllowMultipleSelects ) /* 0 or 1 */ { - char lFilterPatterns[MAX_PATH_OR_CMD] = ""; - char lDialogString[MAX_PATH_OR_CMD] ; - FILE * lIn; + char lFilterPatterns[MAX_PATH_OR_CMD] = ""; + char lDialogString[MAX_PATH_OR_CMD] ; + FILE * lIn; - strcpy( lDialogString , "dialog " ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } + strcpy( lDialogString , "dialog " ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; - strcat( lDialogString , "--fselect \"" ) ; - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - /* dialog.exe uses unix separators even on windows */ - strcpy(lFilterPatterns, aDefaultPathAndFile); - replaceChr( lFilterPatterns , '\\' , '/' ) ; - } - - /* dialog.exe needs at least one separator */ - if ( ! strchr(lFilterPatterns, '/') ) - { - strcat(lDialogString, "./") ; - } - strcat(lDialogString, lFilterPatterns) ; - strcat(lDialogString, "\" 0 60 2>"); - strcpy(lFilterPatterns, getenv("USERPROFILE")); - strcat(lFilterPatterns, "\\AppData\\Local\\Temp\\tinyfd.txt"); - strcat(lDialogString, lFilterPatterns); + strcat( lDialogString , "--fselect \"" ) ; + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + /* dialog.exe uses unix separators even on windows */ + strcpy(lFilterPatterns, aDefaultPathAndFile); + replaceChr( lFilterPatterns , '\\' , '/' ) ; + } + + /* dialog.exe needs at least one separator */ + if ( ! strchr(lFilterPatterns, '/') ) + { + strcat(lDialogString, "./") ; + } + strcat(lDialogString, lFilterPatterns) ; + strcat(lDialogString, "\" 0 60 2>"); + strcpy(lFilterPatterns, getenv("USERPROFILE")); + strcat(lFilterPatterns, "\\AppData\\Local\\Temp\\tinyfd.txt"); + strcat(lDialogString, lFilterPatterns); - /* printf( "lDialogString: %s\n" , lDialogString ) ; */ - system( lDialogString ) ; + /* printf( "lDialogString: %s\n" , lDialogString ) ; */ + system( lDialogString ) ; - if (!(lIn = fopen(lFilterPatterns, "r"))) - { - remove(lFilterPatterns); - return NULL; - } - while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) - {} - fclose(lIn); - remove(lFilterPatterns); - replaceChr( aoBuff , '/' , '\\' ) ; - /* printf( "aoBuff: %s\n" , aoBuff ) ; */ - return aoBuff; + if (!(lIn = fopen(lFilterPatterns, "r"))) + { + remove(lFilterPatterns); + return NULL; + } + while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) + {} + fclose(lIn); + remove(lFilterPatterns); + replaceChr( aoBuff , '/' , '\\' ) ; + /* printf( "aoBuff: %s\n" , aoBuff ) ; */ + return aoBuff; } static char const * selectFolderDialogWinConsole( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) /* NULL or "" */ + char * const aoBuff , + char const * const aTitle , /* NULL or "" */ + char const * const aDefaultPath ) /* NULL or "" */ { - char lDialogString [MAX_PATH_OR_CMD] ; - char lString [MAX_PATH_OR_CMD] ; - FILE * lIn ; - - strcpy( lDialogString , "dialog " ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } + char lDialogString [MAX_PATH_OR_CMD] ; + char lString [MAX_PATH_OR_CMD] ; + FILE * lIn ; + + strcpy( lDialogString , "dialog " ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; - strcat( lDialogString , "--dselect \"" ) ; - if ( aDefaultPath && strlen(aDefaultPath) ) - { - /* dialog.exe uses unix separators even on windows */ - strcpy(lString, aDefaultPath) ; - ensureFinalSlash(lString); - replaceChr( lString , '\\' , '/' ) ; - strcat(lDialogString, lString) ; - } - else - { - /* dialog.exe needs at least one separator */ - strcat(lDialogString, "./") ; - } - strcat(lDialogString, "\" 0 60 2>"); - strcpy(lString, getenv("USERPROFILE")); - strcat(lString, "\\AppData\\Local\\Temp\\tinyfd.txt"); - strcat(lDialogString, lString); + strcat( lDialogString , "--dselect \"" ) ; + if ( aDefaultPath && strlen(aDefaultPath) ) + { + /* dialog.exe uses unix separators even on windows */ + strcpy(lString, aDefaultPath) ; + ensureFinalSlash(lString); + replaceChr( lString , '\\' , '/' ) ; + strcat(lDialogString, lString) ; + } + else + { + /* dialog.exe needs at least one separator */ + strcat(lDialogString, "./") ; + } + strcat(lDialogString, "\" 0 60 2>"); + strcpy(lString, getenv("USERPROFILE")); + strcat(lString, "\\AppData\\Local\\Temp\\tinyfd.txt"); + strcat(lDialogString, lString); - /* printf( "lDialogString: %s\n" , lDialogString ) ; */ - system( lDialogString ) ; + /* printf( "lDialogString: %s\n" , lDialogString ) ; */ + system( lDialogString ) ; - if (!(lIn = fopen(lString, "r"))) - { - remove(lString); - return NULL; - } - while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) - {} - fclose(lIn); - remove(lString); - replaceChr( aoBuff , '/' , '\\' ) ; - /* printf( "aoBuff: %s\n" , aoBuff ) ; */ - return aoBuff; + if (!(lIn = fopen(lString, "r"))) + { + remove(lString); + return NULL; + } + while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) + {} + fclose(lIn); + remove(lString); + replaceChr( aoBuff , '/' , '\\' ) ; + /* printf( "aoBuff: %s\n" , aoBuff ) ; */ + return aoBuff; } 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 * 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 lChar ; + char lChar ; #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 + 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", gMessageWin); - } - 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 ( 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 ; + } + } +} + + +/* 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" */ +{ +#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); + } } /* 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 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 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'; - if (tinyfd_winUtf8) - { - return inputBoxWinGui8(lBuff, aTitle, aMessage, aDefaultInput); - } - else - { - return inputBoxWinGuiA(lBuff, aTitle, aMessage, aDefaultInput); - } - } - else + 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 ( 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;} lBuff[0]='\0'; if (!gWarningDisplayed && !tinyfd_forceConsole) { gWarningDisplayed = 1 ; printf("\n\n%s\n", gTitle); - printf("%s\n\n", gMessageWin); + printf("%s\n\n", tinyfd_needs); } if ( aTitle && strlen(aTitle) ) { @@ -2803,163 +2951,163 @@ char const * tinyfd_inputBox( 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 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" */ { - static char lBuff [MAX_PATH_OR_CMD] ; - char lString[MAX_PATH_OR_CMD] ; - char const * p ; - lBuff[0]='\0'; + static char lBuff [MAX_PATH_OR_CMD] ; + char lString[MAX_PATH_OR_CMD] ; + char const * p ; + lBuff[0]='\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 (char const *)1;} - if (tinyfd_winUtf8) - { - p = saveFileDialogWinGui8(lBuff, - aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); - } - else - { - p = saveFileDialogWinGuiA(lBuff, - aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription); - } - } - else + 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) + { + 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 ( 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 ( ! p || ! strlen( p ) ) - { - return NULL; - } - getPathWithoutFinalSlash( lString , p ) ; - if ( strlen( lString ) && ! dirExists( lString ) ) - { - return NULL ; - } - getLastName(lString,p); - if ( ! filenameValid(lString) ) - { - return NULL; - } - return p ; + if ( ! p || ! strlen( p ) ) + { + return NULL; + } + getPathWithoutFinalSlash( lString , p ) ; + if ( strlen( lString ) && ! dirExists( lString ) ) + { + return NULL ; + } + getLastName(lString,p); + if ( ! filenameValid(lString) ) + { + return NULL; + } + return p ; } /* in case of multiple files, the separator is | */ char const * tinyfd_openFileDialog( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPathAndFile , /* NULL or "" */ + 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 */ { - static char lBuff[MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD]; - char const * p ; + 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") ) ) - { - 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 + 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) + { + 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 ( 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 ( ! p || ! strlen( p ) ) - { - return NULL; - } - if ( aAllowMultipleSelects && strchr(p, '|') ) - { - p = ensureFilesExist( lBuff , p ) ; - } - else if ( ! fileExists(p) ) - { - return NULL ; - } - /* printf( "lBuff3: %s\n" , p ) ; */ - return p ; + if ( ! p || ! strlen( p ) ) + { + return NULL; + } + if ( aAllowMultipleSelects && strchr(p, '|') ) + { + p = ensureFilesExist( lBuff , p ) ; + } + else if ( ! fileExists(p) ) + { + return NULL ; + } + /* printf( "lBuff3: %s\n" , p ) ; */ + return p ; } char const * tinyfd_selectFolderDialog( - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) /* NULL or "" */ + char const * const aTitle , /* NULL or "" */ + char const * const aDefaultPath ) /* NULL or "" */ { static char lBuff [MAX_PATH_OR_CMD] ; - char const * p ; + char const * p ; #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) - { + 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) + { #ifndef TINYFD_NOSELECTFOLDERWIN - p = selectFolderDialogWinGui8(lBuff, aTitle, aDefaultPath); - } - else - { - p = selectFolderDialogWinGuiA(lBuff, aTitle, aDefaultPath); + p = selectFolderDialogWinGui8(lBuff, aTitle, aDefaultPath); + } + else + { + p = selectFolderDialogWinGuiA(lBuff, aTitle, aDefaultPath); #endif /*TINYFD_NOSELECTFOLDERWIN*/ - } - } - else + } + } + 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",""); - } - - if ( ! p || ! strlen( p ) || ! dirExists( p ) ) - { - return NULL ; - } - return p ; + 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",""); + } + + if ( ! p || ! strlen( p ) || ! dirExists( p ) ) + { + return NULL ; + } + return p ; } @@ -2968,348 +3116,364 @@ char const * tinyfd_selectFolderDialog( /* 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"*/ - unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ - unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ + 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 lDefaultHexRGB[8]; - char * lpDefaultHexRGB; - int i; - char const * p ; + char lDefaultHexRGB[8]; + char * lpDefaultHexRGB; + int i; + char const * p ; #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 + 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; + 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; - if ( !p || (strlen(p) != 7) || (p[0] != '#') ) - { - return NULL ; - } - for ( i = 1 ; i < 7 ; i ++ ) - { - if ( ! isxdigit( p[i] ) ) - { - return NULL ; - } - } - Hex2RGB(p,aoResultRGB); - return p ; + if ( !p || (strlen(p) != 7) || (p[0] != '#') ) + { + return NULL ; + } + for ( i = 1 ; i < 7 ; i ++ ) + { + if ( ! isxdigit( p[i] ) ) + { + return NULL ; + } + } + Hex2RGB(p,aoResultRGB); + return p ; } + #else /* unix */ static char gPython2Name[16]; - +static char gPython3Name[16]; +static char gPythonName[16]; + static int isDarwin( ) { - static int lsIsDarwin = -1 ; - struct utsname lUtsname ; - if ( lsIsDarwin < 0 ) - { - lsIsDarwin = !uname(&lUtsname) && !strcmp(lUtsname.sysname,"Darwin") ; - } - return lsIsDarwin ; + static int lsIsDarwin = -1 ; + struct utsname lUtsname ; + if ( lsIsDarwin < 0 ) + { + lsIsDarwin = !uname(&lUtsname) && !strcmp(lUtsname.sysname,"Darwin") ; + } + return lsIsDarwin ; } static int dirExists( char const * const aDirPath ) { - DIR * lDir ; - if ( ! aDirPath || ! strlen( aDirPath ) ) - return 0 ; - lDir = opendir( aDirPath ) ; - if ( ! lDir ) - { - return 0 ; - } - closedir( lDir ) ; - return 1 ; + DIR * lDir ; + if ( ! aDirPath || ! strlen( aDirPath ) ) + return 0 ; + lDir = opendir( aDirPath ) ; + if ( ! lDir ) + { + 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 ; + char lBuff [MAX_PATH_OR_CMD] ; + char lTestedString [MAX_PATH_OR_CMD] = "which " ; + FILE * lIn ; strcat( lTestedString , aExecutable ) ; - strcat( lTestedString, " 2>/dev/null "); + 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 ; + && ( ! 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 ; + pclose( lIn ) ; + if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 0); + return 0 ; } } static char const * getVersion( char const * const aExecutable ) /*version # must follow :*/ { - static char lBuff [MAX_PATH_OR_CMD] ; - char lTestedString [MAX_PATH_OR_CMD] ; - FILE * lIn ; - char * lTmp ; + 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 = fgets( lBuff , sizeof( lBuff ) , lIn ) ; + pclose( lIn ) ; if ( ! lTmp || !(lTmp = strchr( lBuff , ':' )) ) return 0 ; - lTmp ++ ; - /* printf("lTmp %s\n", lTmp); */ - return lTmp ; + lTmp ++ ; + /* printf("lTmp %s\n", lTmp); */ + return lTmp ; } static int tryCommand( char const * const aCommand ) { - char lBuff [MAX_PATH_OR_CMD] ; - FILE * lIn ; - - lIn = popen( aCommand , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL ) - { /* present */ - pclose( lIn ) ; - return 1 ; - } - else - { - pclose( lIn ) ; - return 0 ; - } + char lBuff [MAX_PATH_OR_CMD] ; + FILE * lIn ; + + lIn = popen( aCommand , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL ) + { /* present */ + pclose( lIn ) ; + return 1 ; + } + else + { + pclose( lIn ) ; + return 0 ; + } } -static int isTerminalRunning() +static int isTerminalRunning( ) { - return isatty(1); + static int lIsTerminalRunning = -1 ; + if ( lIsTerminalRunning < 0 ) + { + lIsTerminalRunning = isatty(1); + if (tinyfd_verbose) printf("isTerminalRunning %d\n", lIsTerminalRunning ); + } + return lIsTerminalRunning; } static char const * dialogNameOnly( ) { - 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 , "" ) ; - } - } + 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 ; } int isDialogVersionBetter09b( ) { - char const * lDialogName ; - char * lVersion ; - int lMajor ; - int lMinor ; - int lDate ; - int lResult ; - char * lMinorP ; - char * lLetter ; - char lBuff[128] ; - - /*char lTest[128] = " 0.9b-20031126" ;*/ - - lDialogName = dialogNameOnly() ; - if ( ! strlen(lDialogName) || !(lVersion = (char *) getVersion(lDialogName)) ) return 0 ; - /*lVersion = lTest ;*/ - /*printf("lVersion %s\n", lVersion);*/ - strcpy(lBuff,lVersion); - lMajor = atoi( strtok(lVersion," ,.-") ) ; - /*printf("lMajor %d\n", lMajor);*/ - lMinorP = strtok(0," ,.-abcdefghijklmnopqrstuvxyz"); - lMinor = atoi( lMinorP ) ; - /*printf("lMinor %d\n", lMinor );*/ - lDate = atoi( strtok(0," ,.-") ) ; - if (lDate<0) lDate = - lDate; - /*printf("lDate %d\n", lDate);*/ - lLetter = lMinorP + strlen(lMinorP) ; - strcpy(lVersion,lBuff); - strtok(lLetter," ,.-"); - /*printf("lLetter %s\n", lLetter);*/ - lResult = (lMajor > 0) || ( ( lMinor == 9 ) && (*lLetter == 'b') && (lDate >= 20031126) ); - /*printf("lResult %d\n", lResult);*/ - return lResult; + char const * lDialogName ; + char * lVersion ; + int lMajor ; + int lMinor ; + int lDate ; + int lResult ; + char * lMinorP ; + char * lLetter ; + char lBuff[128] ; + + /*char lTest[128] = " 0.9b-20031126" ;*/ + + lDialogName = dialogNameOnly() ; + if ( ! strlen(lDialogName) || !(lVersion = (char *) getVersion(lDialogName)) ) return 0 ; + /*lVersion = lTest ;*/ + /*printf("lVersion %s\n", lVersion);*/ + strcpy(lBuff,lVersion); + lMajor = atoi( strtok(lVersion," ,.-") ) ; + /*printf("lMajor %d\n", lMajor);*/ + lMinorP = strtok(0," ,.-abcdefghijklmnopqrstuvxyz"); + lMinor = atoi( lMinorP ) ; + /*printf("lMinor %d\n", lMinor );*/ + lDate = atoi( strtok(0," ,.-") ) ; + if (lDate<0) lDate = - lDate; + /*printf("lDate %d\n", lDate);*/ + lLetter = lMinorP + strlen(lMinorP) ; + strcpy(lVersion,lBuff); + strtok(lLetter," ,.-"); + /*printf("lLetter %s\n", lLetter);*/ + lResult = (lMajor > 0) || ( ( lMinor == 9 ) && (*lLetter == 'b') && (lDate >= 20031126) ); + /*printf("lResult %d\n", lResult);*/ + return lResult; } static int whiptailPresentOnly( ) { - static int lWhiptailPresent = -1 ; - if ( lWhiptailPresent < 0 ) - { - lWhiptailPresent = detectPresence( "whiptail" ) ; - } - return lWhiptailPresent ; + static int lWhiptailPresent = -1 ; + if ( lWhiptailPresent < 0 ) + { + lWhiptailPresent = detectPresence( "whiptail" ) ; + } + return lWhiptailPresent ; } static char const * terminalName( ) { - static char lTerminalName[128] = "*" ; - char lShellName[64] = "*" ; + static char lTerminalName[128] = "*" ; + char lShellName[64] = "*" ; - if ( lTerminalName[0] == '*' ) - { - if ( detectPresence( "bash" ) ) - { - strcpy(lShellName , "bash -c " ) ; /*good for basic input*/ - } - else if ( strlen(dialogNameOnly()) || whiptailPresentOnly() ) + if ( lTerminalName[0] == '*' ) + { + if ( detectPresence( "bash" ) ) + { + strcpy(lShellName , "bash -c " ) ; /*good for basic input*/ + } + else if ( strlen(dialogNameOnly()) || whiptailPresentOnly() ) + { + strcpy(lShellName , "sh -c " ) ; /*good enough for dialog & whiptail*/ + } + else + { + strcpy(lTerminalName , "" ) ; + return NULL ; + } + + if ( isDarwin() ) + { + if ( strcpy(lTerminalName , "/opt/X11/bin/xterm" ) + && detectPresence( lTerminalName ) ) + { + strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else + { + strcpy(lTerminalName , "" ) ; + } + } + 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*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -x " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( strcpy(lTerminalName,"lxterminal") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( strcpy(lTerminalName,"konsole") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( strcpy(lTerminalName,"kterm") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( strcpy(lTerminalName,"tilix") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( strcpy(lTerminalName,"xfce4-terminal") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -x " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( strcpy(lTerminalName,"mate-terminal") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -x " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( strcpy(lTerminalName,"Eterm") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( strcpy(lTerminalName,"evilvte") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( strcpy(lTerminalName,"pterm") /*good (only letters)*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( strcpy(lTerminalName,"gnome-terminal") /*bad (good if version <3 or >=3.18)*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " --disable-factory -x " ) ; + strcat(lTerminalName , lShellName ) ; + } + else + { + strcpy(lTerminalName , "" ) ; + } + /* bad: koi rxterm guake tilda vala-terminal qterminal + aterm Terminal terminology sakura lilyterm weston-terminal + roxterm termit xvt rxvt mrxvt urxvt */ + } + if ( strlen(lTerminalName) ) { - strcpy(lShellName , "sh -c " ) ; /*good enough for dialog & whiptail*/ + return lTerminalName ; } else { - return NULL ; + return NULL ; } - - if ( isDarwin() ) - { - if ( strcpy(lTerminalName , "/opt/X11/bin/xterm" ) - && detectPresence( lTerminalName ) ) - { - strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else - { - strcpy(lTerminalName , "" ) ; - } - } - 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*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -x " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( strcpy(lTerminalName,"lxterminal") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( strcpy(lTerminalName,"konsole") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( strcpy(lTerminalName,"kterm") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( strcpy(lTerminalName,"xfce4-terminal") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -x " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( strcpy(lTerminalName,"mate-terminal") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -x " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( strcpy(lTerminalName,"Eterm") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( strcpy(lTerminalName,"evilvte") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( strcpy(lTerminalName,"pterm") /*good (only letters)*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else - { - strcpy(lTerminalName , "" ) ; - } - /*else if ( strcpy(lTerminalName,"gnome-terminal") //bad (good if version < 3) - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " --disable-factory -x " ) ; - strcat(lTerminalName , lShellName ) ; - }*/ - /* bad: koi rxterm guake tilda vala-terminal qterminal - aterm Terminal terminology sakura lilyterm weston-terminal - roxterm termit xvt rxvt mrxvt urxvt */ - } - if ( strlen(lTerminalName) ) - { - return lTerminalName ; - } - else - { - return NULL ; - } } @@ -3317,56 +3481,89 @@ static char const * dialogName( ) { char const * lDialogName ; lDialogName = dialogNameOnly( ) ; - if ( strlen(lDialogName) && ( isTerminalRunning() || terminalName() ) ) - { - return lDialogName ; - } - else - { - return NULL ; - } + if ( strlen(lDialogName) && ( isTerminalRunning() || terminalName() ) ) + { + return lDialogName ; + } + else + { + return NULL ; + } } static int whiptailPresent( ) { - int lWhiptailPresent ; + int lWhiptailPresent ; lWhiptailPresent = whiptailPresentOnly( ) ; - if ( lWhiptailPresent && ( isTerminalRunning() || terminalName() ) ) - { - return lWhiptailPresent ; - } - else - { - return 0 ; - } + if ( lWhiptailPresent && ( isTerminalRunning() || terminalName() ) ) + { + return lWhiptailPresent ; + } + else + { + return 0 ; + } } -static int graphicMode() +static int graphicMode( ) { - return !( tinyfd_forceConsole && (isTerminalRunning() || terminalName()) ) - && ( getenv("DISPLAY") - || (isDarwin() && (!getenv("SSH_TTY") || getenv("DISPLAY") ) ) ) ; + return !( tinyfd_forceConsole && (isTerminalRunning() || terminalName()) ) + && ( getenv("DISPLAY") + || (isDarwin() && (!getenv("SSH_TTY") || getenv("DISPLAY") ) ) ) ; } -static int xmessagePresent( ) +static int pactlPresent( ) { - static int lXmessagePresent = -1 ; - if ( lXmessagePresent < 0 ) - { - lXmessagePresent = detectPresence("xmessage");/*if not tty,not on osxpath*/ - } - return lXmessagePresent && graphicMode( ) ; + static int lPactlPresent = -1 ; + if ( lPactlPresent < 0 ) + { + lPactlPresent = detectPresence("pactl") ; + } + return lPactlPresent ; } -static int gxmessagePresent( ) +static int speakertestPresent( ) { - static int lGxmessagePresent = -1 ; - if ( lGxmessagePresent < 0 ) + static int lSpeakertestPresent = -1 ; + if ( lSpeakertestPresent < 0 ) + { + lSpeakertestPresent = detectPresence("speaker-test") ; + } + return lSpeakertestPresent ; +} + + +static int beepexePresent( ) +{ + static int lBeepexePresent = -1 ; + if ( lBeepexePresent < 0 ) + { + lBeepexePresent = detectPresence("beep.exe") ; + } + return lBeepexePresent ; +} + + +static int xmessagePresent( ) +{ + static int lXmessagePresent = -1 ; + if ( lXmessagePresent < 0 ) + { + lXmessagePresent = detectPresence("xmessage");/*if not tty,not on osxpath*/ + } + return lXmessagePresent && graphicMode( ) ; +} + + +static int gxmessagePresent( ) +{ + static int lGxmessagePresent = -1 ; + if ( lGxmessagePresent < 0 ) { lGxmessagePresent = detectPresence("gxmessage") ; } @@ -3376,12 +3573,12 @@ static int gxmessagePresent( ) static int gmessagePresent( ) { - static int lGmessagePresent = -1 ; - if ( lGmessagePresent < 0 ) - { - lGmessagePresent = detectPresence("gmessage") ; - } - return lGmessagePresent && graphicMode( ) ; + static int lGmessagePresent = -1 ; + if ( lGmessagePresent < 0 ) + { + lGmessagePresent = detectPresence("gmessage") ; + } + return lGmessagePresent && graphicMode( ) ; } @@ -3396,6 +3593,54 @@ static int notifysendPresent( ) } +static int perlPresent( ) +{ + 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 ; +} + + +static int afplayPresent( ) +{ + static int lAfplayPresent = -1 ; + char lBuff [MAX_PATH_OR_CMD] ; + FILE * lIn ; + + if ( lAfplayPresent < 0 ) + { + lAfplayPresent = detectPresence("afplay") ; + if ( lAfplayPresent ) + { + lIn = popen( "test -e /System/Library/Sounds/Ping.aiff || echo Ping" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL ) + { + lAfplayPresent = 2 ; + } + pclose( lIn ) ; + if (tinyfd_verbose) printf("afplay %d\n", lAfplayPresent); + } + } + return graphicMode() ? lAfplayPresent : 0 ; +} + + static int xdialogPresent( ) { static int lXdialogPresent = -1 ; @@ -3423,1423 +3668,2038 @@ static int osascriptPresent( ) static int lOsascriptPresent = -1 ; if ( lOsascriptPresent < 0 ) { - gWarningDisplayed |= !!getenv("SSH_TTY"); - lOsascriptPresent = detectPresence( "osascript" ) ; + gWarningDisplayed |= !!getenv("SSH_TTY"); + lOsascriptPresent = detectPresence( "osascript" ) ; } - return lOsascriptPresent && graphicMode() && !getenv("SSH_TTY") ; + return lOsascriptPresent && graphicMode() && !getenv("SSH_TTY") ; } static int kdialogPresent( ) { - static int lKdialogPresent = -1 ; - if ( lKdialogPresent < 0 ) - { - lKdialogPresent = detectPresence("kdialog") ; - } - return lKdialogPresent && graphicMode( ) ; + static int lKdialogPresent = -1 ; + char lBuff [MAX_PATH_OR_CMD] ; + FILE * lIn ; + + if ( lKdialogPresent < 0 ) + { + lKdialogPresent = detectPresence("kdialog") ; + if ( lKdialogPresent && !getenv("SSH_TTY") ) + { + lIn = popen( "kdialog --attach 2>&1" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + if ( ! strstr( "Unknown" , lBuff ) ) + { + lKdialogPresent = 2 ; + if (tinyfd_verbose) printf("kdialog-attach %d\n", lKdialogPresent); + } + } + pclose( lIn ) ; + + if (lKdialogPresent == 2) + { + lKdialogPresent = 1 ; + lIn = popen( "kdialog --passivepopup 2>&1" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + if ( ! strstr( "Unknown" , lBuff ) ) + { + lKdialogPresent = 2 ; + if (tinyfd_verbose) printf("kdialog-popup %d\n", lKdialogPresent); + } + } + pclose( lIn ) ; + } + } + } + return graphicMode() ? lKdialogPresent : 0 ; } static int qarmaPresent( ) { - static int lQarmaPresent = -1 ; - if ( lQarmaPresent < 0 ) - { - lQarmaPresent = detectPresence("qarma") ; - } - return lQarmaPresent && graphicMode( ) ; + static int lQarmaPresent = -1 ; + if ( lQarmaPresent < 0 ) + { + lQarmaPresent = detectPresence("qarma") ; + } + return lQarmaPresent && graphicMode( ) ; } static int matedialogPresent( ) { - static int lMatedialogPresent = -1 ; - if ( lMatedialogPresent < 0 ) - { - lMatedialogPresent = detectPresence("matedialog") ; - } - return lMatedialogPresent && graphicMode( ) ; + static int lMatedialogPresent = -1 ; + if ( lMatedialogPresent < 0 ) + { + lMatedialogPresent = detectPresence("matedialog") ; + } + return lMatedialogPresent && graphicMode( ) ; +} + + +static int shellementaryPresent( ) +{ + static int lShellementaryPresent = -1 ; + if ( lShellementaryPresent < 0 ) + { + lShellementaryPresent = 0 ; /*detectPresence("shellementary"); shellementary is not ready yet */ + } + return lShellementaryPresent && graphicMode( ) ; } static int zenityPresent( ) { - static int lZenityPresent = -1 ; - if ( lZenityPresent < 0 ) - { - lZenityPresent = detectPresence("zenity") ; - } - return lZenityPresent && graphicMode( ) ; + static int lZenityPresent = -1 ; + if ( lZenityPresent < 0 ) + { + lZenityPresent = detectPresence("zenity") ; + } + return lZenityPresent && graphicMode( ) ; +} + + +static int zenity3Present() +{ + static int lZenity3Present = -1 ; + char lBuff [MAX_PATH_OR_CMD] ; + FILE * lIn ; + int lIntTmp ; + + if ( lZenity3Present < 0 ) + { + lZenity3Present = 0 ; + if ( zenityPresent() ) + { + lIn = popen( "zenity --version" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + if ( atoi(lBuff) >= 3 ) + { + lZenity3Present = 3 ; + lIntTmp = atoi(strtok(lBuff,".")+2 ) ; + if ( lIntTmp >= 18 ) + { + lZenity3Present = 5 ; + } + else if ( lIntTmp >= 10 ) + { + lZenity3Present = 4 ; + } + } + else if ( ( atoi(lBuff) == 2 ) && ( atoi(strtok(lBuff,".")+2 ) >= 32 ) ) + { + lZenity3Present = 2 ; + } + if (tinyfd_verbose) printf("zenity %d\n", lZenity3Present); + } + pclose( lIn ) ; + } + } + return graphicMode() ? lZenity3Present : 0 ; } static int osx9orBetter( ) { - static int lOsx9orBetter = -1 ; - char lBuff [MAX_PATH_OR_CMD] ; - FILE * lIn ; - int V,v; + static int lOsx9orBetter = -1 ; + char lBuff [MAX_PATH_OR_CMD] ; + FILE * lIn ; + int V,v; - if ( lOsx9orBetter < 0 ) - { - lOsx9orBetter = 0 ; - lIn = popen( "osascript -e 'set osver to system version of (system info)'" , "r" ) ; - if ( ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - && ( 2 == sscanf(lBuff, "%d.%d", &V, &v) ) ) - { - V = V * 100 + v; - if ( V >= 1009 ) - { - lOsx9orBetter = 1 ; - } - } - pclose( lIn ) ; - /* printf("Osx10 = %d, %d = <%s>\n", lOsx9orBetter, V, lBuff) ; */ - } - return lOsx9orBetter ; + if ( lOsx9orBetter < 0 ) + { + lOsx9orBetter = 0 ; + lIn = popen( "osascript -e 'set osver to system version of (system info)'" , "r" ) ; + if ( ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + && ( 2 == sscanf(lBuff, "%d.%d", &V, &v) ) ) + { + V = V * 100 + v; + if ( V >= 1009 ) + { + lOsx9orBetter = 1 ; + } + } + pclose( lIn ) ; + if (tinyfd_verbose) printf("Osx10 = %d, %d = %s\n", lOsx9orBetter, V, lBuff) ; + } + return lOsx9orBetter ; } -static int zenity3Present() +static int python2Present( ) { - static int lZenity3Present = -1 ; - char lBuff [MAX_PATH_OR_CMD] ; - FILE * lIn ; + 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 ; +} - if ( lZenity3Present < 0 ) - { - lZenity3Present = 0 ; - if ( zenityPresent() ) - { - lIn = popen( "zenity --version" , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - { - if ( atoi(lBuff) >= 3 ) - { - lZenity3Present = 3 ; - } - else if ( ( atoi(lBuff) == 2 ) && ( atoi(strtok(lBuff,".")+2 ) >= 32 ) ) - { - lZenity3Present = 2 ; - } - } - pclose( lIn ) ; - } - } - return graphicMode() ? lZenity3Present : 0 ; +static int python3Present( ) +{ + static int lPython3Present = -1 ; + int i; + + if ( lPython3Present < 0 ) + { + lPython3Present = 0 ; + strcpy(gPython3Name , "python3" ) ; + if ( detectPresence(gPython3Name) ) lPython3Present = 1; + else + { + for ( i = 9 ; i >= 0 ; i -- ) + { + sprintf( gPython3Name , "python3.%d" , i ) ; + if ( detectPresence(gPython3Name) ) + { + lPython3Present = 1; + 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 ; } static int tkinter2Present( ) { static int lTkinter2Present = -1 ; - char lPythonCommand[256]; - char lPythonParams[256] = -"-c \"try:\n\timport Tkinter;\nexcept:\n\tprint(0);\""; - int i; + char lPythonCommand[256]; + char lPythonParams[256] = +"-S -c \"try:\n\timport Tkinter;\nexcept:\n\tprint 0;\""; - if ( lTkinter2Present < 0 ) - { - lTkinter2Present = 0 ; - strcpy(gPython2Name , "python" ) ; - sprintf( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; - if ( ! detectPresence(gPython2Name) - || ! (lTkinter2Present = tryCommand(lPythonCommand)) ) - { - strcpy(gPython2Name , "python2" ) ; - if ( detectPresence(gPython2Name) ) - { - sprintf( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; - lTkinter2Present = tryCommand(lPythonCommand); - } - else - { - for ( i = 9 ; i >= 0 ; i -- ) - { - sprintf( gPython2Name , "python2.%d" , i ) ; - if ( detectPresence(gPython2Name) ) - { - sprintf( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ; - lTkinter2Present = tryCommand(lPythonCommand); - break ; - } - } - } - } - } - /* printf("lTkinter2Present %d\n", lTkinter2Present) ; */ - /* printf("gPython2Name %s\n", gPython2Name) ; */ - return lTkinter2Present && graphicMode() && !(isDarwin() && getenv("SSH_TTY") ); + + 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") ); } -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 */ +static int tkinter3Present( ) { - char lBuff [MAX_PATH_OR_CMD] ; - char * lDialogString = NULL ; - char * lpDialogString; - FILE * lIn ; - int lWasGraphicDialog = 0 ; - int lWasXterm = 0 ; - int lResult ; - char lChar ; - struct termios infoOri; - struct termios info; - int lTitleLen ; - int lMessageLen ; - - lBuff[0]='\0'; - - lTitleLen = aTitle ? strlen(aTitle) : 0 ; - lMessageLen = aMessage ? strlen(aMessage) : 0 ; - if ( !aTitle || strcmp(aTitle,"tinyfd_query") ) - { - lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); - } + static int lTkinter3Present = -1 ; + char lPythonCommand[256]; + char lPythonParams[256] = + "-S -c \"try:\n\timport tkinter;\nexcept:\n\tprint(0);\""; - if ( osascriptPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;} + if ( lTkinter3Present < 0 ) + { + lTkinter3Present = 0 ; + if ( python3Present() ) + { + sprintf( lPythonCommand , "%s %s" , gPython3Name , lPythonParams ) ; + lTkinter3Present = tryCommand(lPythonCommand) ; + } + if (tinyfd_verbose) printf("lTkinter3Present %d\n", lTkinter3Present) ; + } + return lTkinter3Present && graphicMode() && !(isDarwin() && getenv("SSH_TTY") ); +} - strcpy( lDialogString , "osascript "); - if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); - strcat( lDialogString , " -e 'try' -e 'set {vButton} to {button returned} of ( display dialog \"") ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat(lDialogString, "\" ") ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - strcat(lDialogString, "with icon ") ; - if ( aIconType && ! strcmp( "error" , aIconType ) ) - { - strcat(lDialogString, "stop " ) ; - } - else if ( aIconType && ! strcmp( "warning" , aIconType ) ) - { - strcat(lDialogString, "caution " ) ; - } - else /* question or info */ - { - strcat(lDialogString, "note " ) ; - } - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - if ( ! aDefaultButton ) - { - strcat( lDialogString ,"default button \"Cancel\" " ) ; - } - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - strcat( lDialogString ,"buttons {\"No\", \"Yes\"} " ) ; - if (aDefaultButton) - { - strcat( lDialogString ,"default button \"Yes\" " ) ; - } - else - { - strcat( lDialogString ,"default button \"No\" " ) ; - } - strcat( lDialogString ,"cancel button \"No\"" ) ; - } - else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString ,"buttons {\"No\", \"Yes\", \"Cancel\"} " ) ; - switch (aDefaultButton) - { - case 1: strcat( lDialogString ,"default button \"Yes\" " ) ; break; - case 2: strcat( lDialogString ,"default button \"No\" " ) ; break; - case 0: strcat( lDialogString ,"default button \"Cancel\" " ) ; break; - } - strcat( lDialogString ,"cancel button \"Cancel\"" ) ; - } - else - { - strcat( lDialogString ,"buttons {\"OK\"} " ) ; - strcat( lDialogString ,"default button \"OK\" " ) ; - } - strcat( lDialogString, ")' ") ; - strcat( lDialogString, -"-e 'if vButton is \"Yes\" then' -e 'return 1' -e 'else if vButton is \"No\" then' -e 'return 2' -e 'else' -e 'return 0' -e 'end if' " ); +static int pythonDbusPresent( ) +{ + static int lDbusPresent = -1 ; + char lPythonCommand[256]; + 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 ) + { + lDbusPresent = 0 ; + if ( python2Present() ) + { + strcpy(gPythonName , gPython2Name ) ; + sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ; + lDbusPresent = tryCommand(lPythonCommand) ; + } + + if ( ! lDbusPresent && python3Present() ) + { + strcpy(gPythonName , gPython3Name ) ; + sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ; + lDbusPresent = tryCommand(lPythonCommand) ; + } + + if (tinyfd_verbose) printf("lDbusPresent %d\n", lDbusPresent) ; + if (tinyfd_verbose) printf("gPythonName %s\n", gPythonName) ; + } + return lDbusPresent && graphicMode() && !(isDarwin() && getenv("SSH_TTY") ); +} - strcat( lDialogString, "-e 'on error number -128' " ) ; - strcat( lDialogString, "-e '0' " ); - strcat( lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; - } - else if ( zenityPresent() || matedialogPresent() || qarmaPresent() ) - { - if ( zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;} - strcpy( lDialogString , "szAnswer=$(zenity" ) ; - if ( zenity3Present() >= 3 ) - { - strcat(lDialogString, " --attach=$(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 1;} - strcpy( lDialogString , "szAnswer=$(matedialog" ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return 1;} - strcpy( lDialogString , "szAnswer=$(qarma" ) ; - strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ - } - strcat(lDialogString, " --"); +static void sigHandler(int sig) +{ + FILE * lIn ; + if ( ( lIn = popen( "pactl unload-module module-sine" , "r" ) ) ) + { + pclose( lIn ) ; + } +} - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - strcat( lDialogString , - "question --ok-label=Ok --cancel-label=Cancel" ) ; - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - strcat( lDialogString , "question" ) ; - } - else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , "list --column \"\" --hide-header \"Yes\" \"No\"" ) ; - } - 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, "\"") ; - } - if ( zenity3Present() >= 3 ) - { - strcat( lDialogString , " --icon-name=dialog-" ) ; - if ( aIconType && (! strcmp( "question" , aIconType ) - || ! strcmp( "error" , aIconType ) - || ! strcmp( "warning" , aIconType ) ) ) - { - strcat( lDialogString , aIconType ) ; - } - else - { - strcat( lDialogString , "information" ) ; - } - } - - if ( ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , -");if [ $? = 1 ];then echo 0;elif [ $szAnswer = \"No\" ];then echo 2;else echo 1;fi"); - } - else - { - strcat( lDialogString , ");if [ $? = 0 ];then echo 1;else echo 0;fi"); - } - } - else if ( kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} +void tinyfd_beep() +{ + char lDialogString [256] ; + FILE * lIn ; - strcpy( lDialogString , "kdialog --" ) ; - if ( aDialogType && ( ! strcmp( "okcancel" , aDialogType ) - || ! strcmp( "yesno" , aDialogType ) || ! strcmp( "yesnocancel" , aDialogType ) ) ) - { - if ( aIconType && ( ! strcmp( "warning" , aIconType ) - || ! strcmp( "error" , aIconType ) ) ) - { - strcat( lDialogString , "warning" ) ; - } - if ( ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , "yesnocancel" ) ; - } - else - { - strcat( lDialogString , "yesno" ) ; - } - } - else if ( aIconType && ! strcmp( "error" , aIconType ) ) - { - strcat( lDialogString , "error" ) ; - } - else if ( aIconType && ! strcmp( "warning" , aIconType ) ) - { - strcat( lDialogString , "sorry" ) ; - } - else - { - strcat( lDialogString , "msgbox" ) ; - } - strcat( lDialogString , " \"" ) ; - if ( aMessage ) - { - strcat( lDialogString , aMessage ) ; - } - strcat( lDialogString , "\"" ) ; - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - strcat( lDialogString , - " --yes-label Ok --no-label Cancel" ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } + if ( osascriptPresent() ) + { + if ( afplayPresent() >= 2 ) + { + strcpy( lDialogString , "afplay /System/Library/Sounds/Ping.aiff") ; + } + else + { + strcpy( lDialogString , "osascript -e 'tell application \"System Events\" to beep'") ; + } + } + 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() ) + { + /*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" ) ; + } + else if ( beepexePresent() ) + { + strcpy( lDialogString , "beep.exe 440 300" ) ; + } + else + { + strcpy( lDialogString , "printf '\a' > /dev/tty" ) ; + } - if ( ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , "; x=$? ;if [ $x = 0 ] ;then echo 1;elif [ $x = 1 ] ;then echo 2;else echo 0;fi"); - } - else - { - strcat( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi"); - } - } - else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter2Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return 1;} + if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( ) ) - { - strcat( lDialogString , " -i" ) ; /* for osx without console */ - } - - strcat( lDialogString , -" -c \"import Tkinter,tkMessageBox;root=Tkinter.Tk();root.withdraw();"); - - if ( isDarwin( ) ) - { - strcat( lDialogString , -"import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \ -frontmost of process \\\"Python\\\" to true' ''');"); - } + if ( ( lIn = popen( lDialogString , "r" ) ) ) + { + pclose( lIn ) ; + } - strcat( lDialogString ,"res=tkMessageBox." ) ; - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - strcat( lDialogString , "askokcancel(" ) ; - if ( aDefaultButton ) - { - strcat( lDialogString , "default=tkMessageBox.OK," ) ; - } - else - { - strcat( lDialogString , "default=tkMessageBox.CANCEL," ) ; - } - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - strcat( lDialogString , "askyesno(" ) ; - if ( aDefaultButton ) - { - strcat( lDialogString , "default=tkMessageBox.YES," ) ; - } - else - { - strcat( lDialogString , "default=tkMessageBox.NO," ) ; - } - } - else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , "askyesnocancel(" ) ; - switch ( aDefaultButton ) - { - case 1: strcat( lDialogString , "default=tkMessageBox.YES," ); break; - case 2: strcat( lDialogString , "default=tkMessageBox.NO," ); break; - case 0: strcat( lDialogString , "default=tkMessageBox.CANCEL," ); break; - } - } - else - { - strcat( lDialogString , "showinfo(" ) ; - } + if ( pactlPresent() ) + { + signal(SIGINT, SIG_DFL); + } +} - 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\ +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 lBuff [MAX_PATH_OR_CMD] ; + char * lDialogString = NULL ; + char * lpDialogString; + FILE * lIn ; + int lWasGraphicDialog = 0 ; + int lWasXterm = 0 ; + int lResult ; + char lChar ; + struct termios infoOri; + struct termios info; + size_t lTitleLen ; + size_t lMessageLen ; + + lBuff[0]='\0'; + + lTitleLen = aTitle ? strlen(aTitle) : 0 ; + lMessageLen = aMessage ? strlen(aMessage) : 0 ; + if ( !aTitle || strcmp(aTitle,"tinyfd_query") ) + { + lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); + } + + if ( osascriptPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;} + + strcpy( lDialogString , "osascript "); + if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat( lDialogString , " -e 'try' -e 'set {vButton} to {button returned} of ( display dialog \"") ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString, "\" ") ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + strcat(lDialogString, "with icon ") ; + if ( aIconType && ! strcmp( "error" , aIconType ) ) + { + strcat(lDialogString, "stop " ) ; + } + else if ( aIconType && ! strcmp( "warning" , aIconType ) ) + { + strcat(lDialogString, "caution " ) ; + } + else /* question or info */ + { + strcat(lDialogString, "note " ) ; + } + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + if ( ! aDefaultButton ) + { + strcat( lDialogString ,"default button \"Cancel\" " ) ; + } + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + strcat( lDialogString ,"buttons {\"No\", \"Yes\"} " ) ; + if (aDefaultButton) + { + strcat( lDialogString ,"default button \"Yes\" " ) ; + } + else + { + strcat( lDialogString ,"default button \"No\" " ) ; + } + strcat( lDialogString ,"cancel button \"No\"" ) ; + } + else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString ,"buttons {\"No\", \"Yes\", \"Cancel\"} " ) ; + switch (aDefaultButton) + { + case 1: strcat( lDialogString ,"default button \"Yes\" " ) ; break; + case 2: strcat( lDialogString ,"default button \"No\" " ) ; break; + case 0: strcat( lDialogString ,"default button \"Cancel\" " ) ; break; + } + strcat( lDialogString ,"cancel button \"Cancel\"" ) ; + } + else + { + strcat( lDialogString ,"buttons {\"OK\"} " ) ; + strcat( lDialogString ,"default button \"OK\" " ) ; + } + strcat( lDialogString, ")' ") ; + + strcat( lDialogString, +"-e 'if vButton is \"Yes\" then' -e 'return 1'\ + -e 'else if vButton is \"OK\" then' -e 'return 1'\ + -e 'else if vButton is \"No\" then' -e 'return 2'\ + -e 'else' -e 'return 0' -e 'end if' " ); + + strcat( lDialogString, "-e 'on error number -128' " ) ; + strcat( lDialogString, "-e '0' " ); + + strcat( lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} + + strcpy( lDialogString , "kdialog" ) ; + if ( kdialogPresent() == 2 ) + { + strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ + } + + strcat( lDialogString , " --" ) ; + if ( aDialogType && ( ! strcmp( "okcancel" , aDialogType ) + || ! strcmp( "yesno" , aDialogType ) || ! strcmp( "yesnocancel" , aDialogType ) ) ) + { + if ( aIconType && ( ! strcmp( "warning" , aIconType ) + || ! strcmp( "error" , aIconType ) ) ) + { + strcat( lDialogString , "warning" ) ; + } + if ( ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , "yesnocancel" ) ; + } + else + { + strcat( lDialogString , "yesno" ) ; + } + } + else if ( aIconType && ! strcmp( "error" , aIconType ) ) + { + strcat( lDialogString , "error" ) ; + } + else if ( aIconType && ! strcmp( "warning" , aIconType ) ) + { + strcat( lDialogString , "sorry" ) ; + } + else + { + strcat( lDialogString , "msgbox" ) ; + } + strcat( lDialogString , " \"" ) ; + if ( aMessage ) + { + strcat( lDialogString , aMessage ) ; + } + strcat( lDialogString , "\"" ) ; + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + strcat( lDialogString , + " --yes-label Ok --no-label Cancel" ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + + if ( ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , "; x=$? ;if [ $x = 0 ] ;then echo 1;elif [ $x = 1 ] ;then echo 2;else echo 0;fi"); + } + else + { + strcat( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi"); + } + } + else if ( zenityPresent() || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) + { + if ( zenityPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;} + strcpy( lDialogString , "szAnswer=$(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 1;} + strcpy( lDialogString , "szAnswer=$(matedialog" ) ; + } + else if ( shellementaryPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return 1;} + strcpy( lDialogString , "szAnswer=$(shellementary" ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return 1;} + strcpy( lDialogString , "szAnswer=$(qarma" ) ; + if ( !getenv("SSH_TTY") ) + { + strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ + } + } + strcat(lDialogString, " --"); + + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + strcat( lDialogString , + "question --ok-label=Ok --cancel-label=Cancel" ) ; + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + strcat( lDialogString , "question" ) ; + } + else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , "list --column \"\" --hide-header \"Yes\" \"No\"" ) ; + } + 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, " --no-wrap --text=\"") ; + strcat(lDialogString, aMessage) ; + strcat(lDialogString, "\"") ; + } + if ( (zenity3Present() >= 3) || (!zenityPresent() && (shellementaryPresent() || qarmaPresent()) ) ) + { + strcat( lDialogString , " --icon-name=dialog-" ) ; + if ( aIconType && (! strcmp( "question" , aIconType ) + || ! strcmp( "error" , aIconType ) + || ! strcmp( "warning" , aIconType ) ) ) + { + strcat( lDialogString , aIconType ) ; + } + else + { + strcat( lDialogString , "information" ) ; + } + } + + if ( ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , +");if [ $? = 1 ];then echo 0;elif [ $szAnswer = \"No\" ];then echo 2;else echo 1;fi"); + } + else + { + strcat( lDialogString , ");if [ $? = 0 ];then echo 1;else echo 0;fi"); + } + } + 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( ) ) + { + strcat( lDialogString , " -i" ) ; /* for osx without console */ + } + + strcat( lDialogString , +" -S -c \"import Tkinter,tkMessageBox;root=Tkinter.Tk();root.withdraw();"); + + if ( 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=tkMessageBox." ) ; + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + strcat( lDialogString , "askokcancel(" ) ; + if ( aDefaultButton ) + { + strcat( lDialogString , "default=tkMessageBox.OK," ) ; + } + else + { + strcat( lDialogString , "default=tkMessageBox.CANCEL," ) ; + } + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + strcat( lDialogString , "askyesno(" ) ; + if ( aDefaultButton ) + { + strcat( lDialogString , "default=tkMessageBox.YES," ) ; + } + else + { + strcat( lDialogString , "default=tkMessageBox.NO," ) ; + } + } + else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , "askyesnocancel(" ) ; + switch ( aDefaultButton ) + { + case 1: strcat( lDialogString , "default=tkMessageBox.YES," ); break; + case 2: strcat( lDialogString , "default=tkMessageBox.NO," ); break; + case 0: strcat( lDialogString , "default=tkMessageBox.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\ + } + 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() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return 1;} - strcpy( lDialogString , "gxmessage"); - } - else if ( gmessagePresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return 1;} - strcpy( lDialogString , "gmessage"); - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xmessage");return 1;} - strcpy( lDialogString , "xmessage"); - } + 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() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return 1;} + strcpy( lDialogString , "gxmessage"); + } + else if ( gmessagePresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return 1;} + strcpy( lDialogString , "gmessage"); + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xmessage");return 1;} + strcpy( lDialogString , "xmessage"); + } + + if ( aDialogType && ! strcmp("okcancel" , aDialogType) ) + { + strcat( lDialogString , " -buttons Ok:1,Cancel:0"); + switch ( aDefaultButton ) + { + case 1: strcat( lDialogString , " -default Ok"); break; + case 0: strcat( lDialogString , " -default Cancel"); break; + } + } + else if ( aDialogType && ! strcmp("yesno" , aDialogType) ) + { + strcat( lDialogString , " -buttons Yes:1,No:0"); + switch ( aDefaultButton ) + { + case 1: strcat( lDialogString , " -default Yes"); break; + case 0: strcat( lDialogString , " -default No"); break; + } + } + else if ( aDialogType && ! strcmp("yesnocancel" , aDialogType) ) + { + strcat( lDialogString , " -buttons Yes:1,No:2,Cancel:0"); + switch ( aDefaultButton ) + { + case 1: strcat( lDialogString , " -default Yes"); break; + case 2: strcat( lDialogString , " -default No"); break; + case 0: strcat( lDialogString , " -default Cancel"); break; + } + } + else + { + strcat( lDialogString , " -buttons Ok:1"); + strcat( lDialogString , " -default Ok"); + } + + strcat( lDialogString , " -center \""); + if ( aMessage && strlen(aMessage) ) + { + strcat( lDialogString , aMessage ) ; + } + strcat(lDialogString, "\"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat( lDialogString , " -title \""); + strcat( lDialogString , aTitle ) ; + strcat( lDialogString, "\"" ) ; + } + strcat( lDialogString , " ; echo $? "); + } + else if ( xdialogPresent() || gdialogPresent() || dialogName() || whiptailPresent() ) + { + if ( gdialogPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return 1;} + lWasGraphicDialog = 1 ; + strcpy( lDialogString , "(gdialog " ) ; + } + else if ( xdialogPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return 1;} + lWasGraphicDialog = 1 ; + strcpy( lDialogString , "(Xdialog " ) ; + } + else if ( dialogName( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return 0;} + if ( isTerminalRunning( ) ) + { + strcpy( lDialogString , "(dialog " ) ; + } + else + { + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(" ) ; + strcat( lDialogString , dialogName() ) ; + strcat( lDialogString , " " ) ; + } + } + else if ( isTerminalRunning( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;} + strcpy( lDialogString , "(whiptail " ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;} + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(whiptail " ) ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + if ( !xdialogPresent() && !gdialogPresent() ) + { + if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) + || !strcmp( "yesnocancel" , aDialogType ) ) ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab: move focus") ; + strcat(lDialogString, "\" ") ; + } + } + + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + if ( ! aDefaultButton ) + { + strcat( lDialogString , "--defaultno " ) ; + } + strcat( lDialogString , + "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ; + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + if ( ! aDefaultButton ) + { + strcat( lDialogString , "--defaultno " ) ; + } + strcat( lDialogString , "--yesno " ) ; + } + else if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + if (!aDefaultButton) + { + strcat(lDialogString, "--defaultno "); + } + strcat(lDialogString, "--menu "); + } + else + { + strcat( lDialogString , "--msgbox " ) ; + + } + strcat( lDialogString , "\"" ) ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString, "\" "); + + if ( lWasGraphicDialog ) + { + if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + strcat(lDialogString,"0 60 0 Yes \"\" No \"\") 2>/tmp/tinyfd.txt;\ +if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ +tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; + } + else + { + strcat(lDialogString, + "10 60 ) 2>&1;if [ $? = 0 ];then echo 1;else echo 0;fi"); + } + } + else + { + if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + strcat(lDialogString,"0 60 0 Yes \"\" No \"\" >/dev/tty ) 2>/tmp/tinyfd.txt;\ + if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ + tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; + + if ( lWasXterm ) + { + strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt"); + } + else + { + strcat(lDialogString, "; clear >/dev/tty") ; + } + } + else + { + strcat(lDialogString, "10 60 >/dev/tty) 2>&1;if [ $? = 0 ];"); + if ( lWasXterm ) + { + strcat( lDialogString , +"then\n\techo 1\nelse\n\techo 0\nfi >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); + } + else + { + strcat(lDialogString, + "then echo 1;else echo 0;fi;clear >/dev/tty"); + } + } + } + } + else if ( !isTerminalRunning() && terminalName() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;} + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'" ) ; + if ( !gWarningDisplayed && !tinyfd_forceConsole) + { + gWarningDisplayed = 1 ; + strcat( lDialogString , "echo \"" ) ; + strcat( lDialogString, gTitle) ; + strcat( lDialogString , "\";" ) ; + strcat( lDialogString , "echo \"" ) ; + strcat( lDialogString, tinyfd_needs) ; + strcat( lDialogString , "\";echo;echo;" ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat( lDialogString , "echo \"" ) ; + strcat( lDialogString, aTitle) ; + strcat( lDialogString , "\";echo;" ) ; + } + if ( aMessage && strlen(aMessage) ) + { + strcat( lDialogString , "echo \"" ) ; + strcat( lDialogString, aMessage) ; + strcat( lDialogString , "\"; " ) ; + } + if ( aDialogType && !strcmp("yesno",aDialogType) ) + { + strcat( lDialogString , "echo -n \"y/n: \"; " ) ; + strcat( lDialogString , "stty sane -echo;" ) ; + strcat( lDialogString , + "answer=$( while ! head -c 1 | grep -i [ny];do true ;done);"); + strcat( lDialogString , + "if echo \"$answer\" | grep -iq \"^y\";then\n"); + strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ; + } + else if ( aDialogType && !strcmp("okcancel",aDialogType) ) + { + strcat( lDialogString , "echo -n \"[O]kay/[C]ancel: \"; " ) ; + strcat( lDialogString , "stty sane -echo;" ) ; + strcat( lDialogString , + "answer=$( while ! head -c 1 | grep -i [oc];do true ;done);"); + strcat( lDialogString , + "if echo \"$answer\" | grep -iq \"^o\";then\n"); + strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ; + } + else if ( aDialogType && !strcmp("yesnocancel",aDialogType) ) + { + strcat( lDialogString , "echo -n \"[Y]es/[N]o/[C]ancel: \"; " ) ; + strcat( lDialogString , "stty sane -echo;" ) ; + strcat( lDialogString , + "answer=$( while ! head -c 1 | grep -i [nyc];do true ;done);"); + strcat( lDialogString , + "if echo \"$answer\" | grep -iq \"^y\";then\n\techo 1\n"); + strcat( lDialogString , "elif echo \"$answer\" | grep -iq \"^n\";then\n\techo 2\n" ) ; + strcat( lDialogString , "else\n\techo 0\nfi" ) ; + } + else + { + strcat(lDialogString , "echo -n \"press enter to continue \"; "); + strcat( lDialogString , "stty sane -echo;" ) ; + strcat( lDialogString , + "answer=$( while ! head -c 1;do true ;done);echo 1"); + } + strcat( lDialogString , + " >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); + } + else if ( !isTerminalRunning() && pythonDbusPresent() && !strcmp("ok" , aDialogType) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python-dbus");return 1;} + strcpy( lDialogString , gPythonName ) ; + strcat( lDialogString ," -c \"import dbus;bus=dbus.SessionBus();"); + strcat( lDialogString ,"notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');" ) ; + strcat( lDialogString ,"notify=dbus.Interface(notif,'org.freedesktop.Notifications');" ) ; + strcat( lDialogString ,"notify.Notify('',0,'" ) ; + if ( aIconType && strlen(aIconType) ) + { + strcat( lDialogString , aIconType ) ; + } + strcat(lDialogString, "','") ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + } + strcat(lDialogString, "','") ; + if ( aMessage && strlen(aMessage) ) + { + lpDialogString = lDialogString + strlen(lDialogString); + replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + } + strcat(lDialogString, "','','',5000)\"") ; + } + else if ( !isTerminalRunning() && (perlPresent() >= 2) && !strcmp("ok" , aDialogType) ) + { + 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:"" ) ; + } + else if ( !isTerminalRunning() && notifysendPresent() && !strcmp("ok" , aDialogType) ) + { - if ( aDialogType && ! strcmp("okcancel" , aDialogType) ) - { - strcat( lDialogString , " -buttons Ok:1,Cancel:0"); - switch ( aDefaultButton ) - { - case 1: strcat( lDialogString , " -default Ok"); break; - case 0: strcat( lDialogString , " -default Cancel"); break; - } - } - else if ( aDialogType && ! strcmp("yesno" , aDialogType) ) - { - strcat( lDialogString , " -buttons Yes:1,No:0"); - switch ( aDefaultButton ) - { - case 1: strcat( lDialogString , " -default Yes"); break; - case 0: strcat( lDialogString , " -default No"); break; - } - } - else if ( aDialogType && ! strcmp("yesnocancel" , aDialogType) ) - { - strcat( lDialogString , " -buttons Yes:1,No:2,Cancel:0"); - switch ( aDefaultButton ) - { - case 1: strcat( lDialogString , " -default Yes"); break; - case 2: strcat( lDialogString , " -default No"); break; - case 0: strcat( lDialogString , " -default Cancel"); break; - } - } - else - { - strcat( lDialogString , " -buttons Ok:1"); - strcat( lDialogString , " -default Ok"); - } + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notifysend");return 1;} + strcpy( lDialogString , "notify-send" ) ; + if ( aIconType && strlen(aIconType) ) + { + strcat( lDialogString , " -i '" ) ; + strcat( lDialogString , aIconType ) ; + strcat( lDialogString , "'" ) ; + } + strcat( lDialogString , " \"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + strcat( lDialogString , " | " ) ; + } + if ( aMessage && strlen(aMessage) ) + { + replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ; + replaceSubStr( aMessage , "\n" , " | " , lBuff ) ; + replaceSubStr( aMessage , "\t" , " " , lBuff ) ; + strcat(lDialogString, lBuff) ; + } + strcat( lDialogString , "\"" ) ; + } + 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", aTitle); + } + + tcgetattr(0, &infoOri); + tcgetattr(0, &info); + info.c_lflag &= ~ICANON; + info.c_cc[VMIN] = 1; + info.c_cc[VTIME] = 0; + tcsetattr(0, TCSANOW, &info); + if ( aDialogType && !strcmp("yesno",aDialogType) ) + { + do + { + if ( aMessage && strlen(aMessage) ) + { + printf("\n%s\n",aMessage); + } + printf("y/n: "); fflush(stdout); + lChar = tolower( getchar() ) ; + printf("\n\n"); + } + while ( lChar != 'y' && lChar != 'n' ); + lResult = lChar == 'y' ? 1 : 0 ; + } + else if ( aDialogType && !strcmp("okcancel",aDialogType) ) + { + do + { + if ( aMessage && strlen(aMessage) ) + { + printf("\n%s\n",aMessage); + } + printf("[O]kay/[C]ancel: "); fflush(stdout); + lChar = tolower( getchar() ) ; + printf("\n\n"); + } + while ( lChar != 'o' && lChar != 'c' ); + lResult = lChar == 'o' ? 1 : 0 ; + } + else if ( aDialogType && !strcmp("yesnocancel",aDialogType) ) + { + do + { + if ( aMessage && strlen(aMessage) ) + { + printf("\n%s\n",aMessage); + } + printf("[Y]es/[N]o/[C]ancel: "); fflush(stdout); + lChar = tolower( getchar() ) ; + printf("\n\n"); + } + while ( lChar != 'y' && lChar != 'n' && lChar != 'c' ); + lResult = (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0 ; + } + else + { + if ( aMessage && strlen(aMessage) ) + { + printf("\n%s\n\n",aMessage); + } + printf("press enter to continue "); fflush(stdout); + getchar() ; + printf("\n\n"); + lResult = 1 ; + } + tcsetattr(0, TCSANOW, &infoOri); + free(lDialogString); + return lResult ; + } - strcat( lDialogString , " -center \""); - if ( aMessage && strlen(aMessage) ) - { - strcat( lDialogString , aMessage ) ; - } - strcat(lDialogString, "\"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat( lDialogString , " -title \""); - strcat( lDialogString , aTitle ) ; - strcat( lDialogString, "\"" ) ; - } - strcat( lDialogString , " ; echo $? "); - } - else if ( xdialogPresent() || gdialogPresent() || dialogName() || whiptailPresent() ) - { - if ( gdialogPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return 1;} - lWasGraphicDialog = 1 ; - strcpy( lDialogString , "(gdialog " ) ; - } - else if ( xdialogPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return 1;} - lWasGraphicDialog = 1 ; - strcpy( lDialogString , "(Xdialog " ) ; - } - else if ( dialogName( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return 0;} - if ( isTerminalRunning( ) ) - { - strcpy( lDialogString , "(dialog " ) ; - } - else - { - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(" ) ; - strcat( lDialogString , dialogName() ) ; - strcat( lDialogString , " " ) ; - } - } - else if ( isTerminalRunning( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;} - strcpy( lDialogString , "(whiptail " ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;} - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(whiptail " ) ; - } + if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } + if ( ! ( lIn = popen( lDialogString , "r" ) ) ) + { + free(lDialogString); + return 0 ; + } + while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + {} - if ( !xdialogPresent() && !gdialogPresent() ) - { - if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) - || !strcmp( "yesnocancel" , aDialogType ) ) ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, "tab: move focus") ; - strcat(lDialogString, "\" ") ; - } - } - - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - if ( ! aDefaultButton ) - { - strcat( lDialogString , "--defaultno " ) ; - } - strcat( lDialogString , - "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ; - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - if ( ! aDefaultButton ) - { - strcat( lDialogString , "--defaultno " ) ; - } - strcat( lDialogString , "--yesno " ) ; - } - else if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - if (!aDefaultButton) - { - strcat(lDialogString, "--defaultno "); - } - strcat(lDialogString, "--menu "); - } - else - { - strcat( lDialogString , "--msgbox " ) ; + pclose( lIn ) ; - } - strcat( lDialogString , "\"" ) ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat(lDialogString, "\" "); + /* printf( "lBuff: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ + if ( lBuff[strlen( lBuff ) -1] == '\n' ) + { + lBuff[strlen( lBuff ) -1] = '\0' ; + } + /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ - if ( lWasGraphicDialog ) - { - if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - strcat(lDialogString,"0 60 0 Yes \"\" No \"\") 2>/tmp/tinyfd.txt;\ -if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ -tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; - } - else - { - strcat(lDialogString, - "10 60 ) 2>&1;if [ $? = 0 ];then echo 1;else echo 0;fi"); - } - } - else - { - if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - strcat(lDialogString,"0 60 0 Yes \"\" No \"\" >/dev/tty ) 2>/tmp/tinyfd.txt;\ - if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ - tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; + 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"); + } + } + /* printf( "lBuff2: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ - if ( lWasXterm ) - { - strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt"); - } - else - { - strcat(lDialogString, "; clear >/dev/tty") ; - } - } - else - { - strcat(lDialogString, "10 60 >/dev/tty) 2>&1;if [ $? = 0 ];"); - if ( lWasXterm ) - { - strcat( lDialogString , -"then\n\techo 1\nelse\n\techo 0\nfi >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); - } - else - { - strcat(lDialogString, - "then echo 1;else echo 0;fi;clear >/dev/tty"); - } - } - } - } - else if ( ! isTerminalRunning( ) && terminalName() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;} - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'" ) ; - if ( !gWarningDisplayed && !tinyfd_forceConsole) - { - gWarningDisplayed = 1 ; - strcat( lDialogString , "echo \"" ) ; - strcat( lDialogString, gTitle) ; - strcat( lDialogString , "\";" ) ; - strcat( lDialogString , "echo \"" ) ; - strcat( lDialogString, gMessageUnix) ; - strcat( lDialogString , "\";echo;echo;" ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat( lDialogString , "echo \"" ) ; - strcat( lDialogString, aTitle) ; - strcat( lDialogString , "\";echo;" ) ; - } - if ( aMessage && strlen(aMessage) ) - { - strcat( lDialogString , "echo \"" ) ; - strcat( lDialogString, aMessage) ; - strcat( lDialogString , "\"; " ) ; - } - if ( aDialogType && !strcmp("yesno",aDialogType) ) - { - strcat( lDialogString , "echo -n \"y/n: \"; " ) ; - strcat( lDialogString , "stty sane -echo;" ) ; - strcat( lDialogString , - "answer=$( while ! head -c 1 | grep -i [ny];do true ;done);"); - strcat( lDialogString , - "if echo \"$answer\" | grep -iq \"^y\";then\n"); - strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ; - } - else if ( aDialogType && !strcmp("okcancel",aDialogType) ) - { - strcat( lDialogString , "echo -n \"[O]kay/[C]ancel: \"; " ) ; - strcat( lDialogString , "stty sane -echo;" ) ; - strcat( lDialogString , - "answer=$( while ! head -c 1 | grep -i [oc];do true ;done);"); - strcat( lDialogString , - "if echo \"$answer\" | grep -iq \"^o\";then\n"); - strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ; - } - else if ( aDialogType && !strcmp("yesnocancel",aDialogType) ) - { - strcat( lDialogString , "echo -n \"[Y]es/[N]o/[C]ancel: \"; " ) ; - strcat( lDialogString , "stty sane -echo;" ) ; - strcat( lDialogString , - "answer=$( while ! head -c 1 | grep -i [nyc];do true ;done);"); - strcat( lDialogString , - "if echo \"$answer\" | grep -iq \"^y\";then\n\techo 1\n"); - strcat( lDialogString , "elif echo \"$answer\" | grep -iq \"^n\";then\n\techo 2\n" ) ; - strcat( lDialogString , "else\n\techo 0\nfi" ) ; - } - else - { - strcat(lDialogString , "echo -n \"press enter to continue \"; "); - strcat( lDialogString , "stty sane -echo;" ) ; - strcat( lDialogString , - "answer=$( while ! head -c 1;do true ;done);echo 1"); - } - strcat( lDialogString , - " >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); - } - else if ( !isTerminalRunning() && notifysendPresent() && !strcmp("ok" , aDialogType) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notify");return 1;} + lResult = !strcmp( lBuff , "2" ) ? 2 : !strcmp( lBuff , "1" ) ? 1 : 0; - strcpy( lDialogString , "notify-send \"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, aTitle) ; - strcat( lDialogString , " | " ) ; - } - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat( lDialogString , "\"" ) ; - } - 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", gMessageUnix); - } - if ( aTitle && strlen(aTitle) ) - { - printf("\n%s\n", aTitle); - } - - tcgetattr(0, &infoOri); - tcgetattr(0, &info); - info.c_lflag &= ~ICANON; - info.c_cc[VMIN] = 1; - info.c_cc[VTIME] = 0; - tcsetattr(0, TCSANOW, &info); - if ( aDialogType && !strcmp("yesno",aDialogType) ) - { - do - { - if ( aMessage && strlen(aMessage) ) - { - printf("\n%s\n",aMessage); - } - printf("y/n: "); fflush(stdout); - lChar = tolower( getchar() ) ; - printf("\n\n"); - } - while ( lChar != 'y' && lChar != 'n' ); - lResult = lChar == 'y' ? 1 : 0 ; - } - else if ( aDialogType && !strcmp("okcancel",aDialogType) ) - { - do - { - if ( aMessage && strlen(aMessage) ) - { - printf("\n%s\n",aMessage); - } - printf("[O]kay/[C]ancel: "); fflush(stdout); - lChar = tolower( getchar() ) ; - printf("\n\n"); - } - while ( lChar != 'o' && lChar != 'c' ); - lResult = lChar == 'o' ? 1 : 0 ; - } - else if ( aDialogType && !strcmp("yesnocancel",aDialogType) ) - { - do - { - if ( aMessage && strlen(aMessage) ) - { - printf("\n%s\n",aMessage); - } - printf("[Y]es/[N]o/[C]ancel: "); fflush(stdout); - lChar = tolower( getchar() ) ; - printf("\n\n"); - } - while ( lChar != 'y' && lChar != 'n' && lChar != 'c' ); - lResult = (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0 ; - } - else - { - if ( aMessage && strlen(aMessage) ) - { - printf("\n%s\n\n",aMessage); - } - printf("press enter to continue "); fflush(stdout); - getchar() ; - printf("\n\n"); - lResult = 1 ; - } - tcsetattr(0, TCSANOW, &infoOri); - free(lDialogString); - return lResult ; - } + /* printf( "lResult: %d\n" , lResult ) ; */ + free(lDialogString); + return lResult ; +} - if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - if ( ! ( lIn = popen( lDialogString , "r" ) ) ) - { - free(lDialogString); - return 0 ; - } - while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - {} +/* 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 lBuff[MAX_PATH_OR_CMD]; + char * lDialogString = NULL ; + char * lpDialogString ; + FILE * lIn ; + size_t lTitleLen ; + size_t lMessageLen ; + + if ( getenv("SSH_TTY") ) + { + return tinyfd_messageBox(aTitle, aMessage, "ok", aIconType, 0); + } - pclose( lIn ) ; + lTitleLen = aTitle ? strlen(aTitle) : 0 ; + lMessageLen = aMessage ? strlen(aMessage) : 0 ; + if ( !aTitle || strcmp(aTitle,"tinyfd_query") ) + { + lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); + } - /* printf( "lBuff: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ + if ( osascriptPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;} + + strcpy( lDialogString , "osascript "); + if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat( lDialogString , " -e 'try' -e 'display notification \"") ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString, " \" ") ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + strcat( lDialogString, "' -e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} + strcpy( lDialogString , "kdialog" ) ; + + if ( aIconType && strlen(aIconType) ) + { + strcat( lDialogString , " --icon '" ) ; + strcat( lDialogString , aIconType ) ; + strcat( lDialogString , "'" ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat( lDialogString , " --title \"" ) ; + strcat( lDialogString , aTitle ) ; + strcat( lDialogString , "\"" ) ; + } + + strcat( lDialogString , " --passivepopup" ) ; + strcat( lDialogString , " \"" ) ; + if ( aMessage ) + { + strcat( lDialogString , aMessage ) ; + } + strcat( lDialogString , " \" 5" ) ; + } + else if ( (zenity3Present()>=5) || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) + { + /* 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 (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"); + + if ( aIconType && strlen( aIconType ) ) + { + strcat( lDialogString , " --window-icon '"); + strcat( lDialogString , aIconType ) ; + strcat( lDialogString , "'" ) ; + } + + strcat( lDialogString , " --text \"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\n") ; + } + if ( aMessage && strlen( aMessage ) ) + { + strcat( lDialogString , aMessage ) ; + } + strcat( lDialogString , " \"" ) ; + } + 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:"" ) ; + } + else if ( pythonDbusPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python-dbus");return 1;} + strcpy( lDialogString , gPythonName ) ; + strcat( lDialogString ," -c \"import dbus;bus=dbus.SessionBus();"); + strcat( lDialogString ,"notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');" ) ; + strcat( lDialogString ,"notify=dbus.Interface(notif,'org.freedesktop.Notifications');" ) ; + strcat( lDialogString ,"notify.Notify('',0,'" ) ; + if ( aIconType && strlen(aIconType) ) + { + strcat( lDialogString , aIconType ) ; + } + strcat(lDialogString, "','") ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + } + strcat(lDialogString, "','") ; + if ( aMessage && strlen(aMessage) ) + { + lpDialogString = lDialogString + strlen(lDialogString); + replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + } + strcat(lDialogString, "','','',5000)\"") ; + } + else if ( notifysendPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notifysend");return 1;} + strcpy( lDialogString , "notify-send" ) ; + if ( aIconType && strlen(aIconType) ) + { + strcat( lDialogString , " -i '" ) ; + strcat( lDialogString , aIconType ) ; + strcat( lDialogString , "'" ) ; + } + strcat( lDialogString , " \"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + strcat( lDialogString , " | " ) ; + } + if ( aMessage && strlen(aMessage) ) + { + replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ; + replaceSubStr( aMessage , "\n" , " | " , lBuff ) ; + replaceSubStr( aMessage , "\t" , " " , lBuff ) ; + strcat(lDialogString, lBuff) ; + } + strcat( lDialogString , "\"" ) ; + } + else + { + return tinyfd_messageBox(aTitle, aMessage, "ok", aIconType, 0); + } - 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"); - } - } - /* printf( "lBuff2: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ + if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - lResult = !strcmp( lBuff , "2" ) ? 2 : !strcmp( lBuff , "1" ) ? 1 : 0; + if ( ! ( lIn = popen( lDialogString , "r" ) ) ) + { + free(lDialogString); + return 0 ; + } - /* printf( "lResult: %d\n" , lResult ) ; */ - free(lDialogString); - return lResult ; + pclose( lIn ) ; + free(lDialogString); + return 1; } /* 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 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 char lBuff[MAX_PATH_OR_CMD]; - char * lDialogString = NULL; - char * lpDialogString; - FILE * lIn ; - int lResult ; - int lWasGdialog = 0 ; - int lWasGraphicDialog = 0 ; - int lWasXterm = 0 ; - int lWasBasicXterm = 0 ; - struct termios oldt ; - struct termios newt ; - char * lEOF; - int lTitleLen ; - int lMessageLen ; - - lBuff[0]='\0'; - - lTitleLen = aTitle ? strlen(aTitle) : 0 ; - lMessageLen = aMessage ? strlen(aMessage) : 0 ; - if ( !aTitle || strcmp(aTitle,"tinyfd_query") ) - { - lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); - } - - if ( osascriptPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} - strcpy( lDialogString , "osascript "); - if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); - strcat( lDialogString , " -e 'try' -e 'display dialog \"") ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat(lDialogString, "\" ") ; - strcat(lDialogString, "default answer \"") ; - if ( aDefaultInput && strlen(aDefaultInput) ) - { - strcat(lDialogString, aDefaultInput) ; - } - strcat(lDialogString, "\" ") ; - if ( ! aDefaultInput ) - { - strcat(lDialogString, "hidden answer true ") ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - strcat(lDialogString, "with icon note' ") ; - strcat(lDialogString, "-e '\"1\" & text returned of result' " ); - strcat(lDialogString, "-e 'on error number -128' " ) ; - strcat(lDialogString, "-e '0' " ); - strcat(lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat(lDialogString, " -e 'end tell'") ; - } - else if ( zenityPresent() || matedialogPresent() || qarmaPresent() ) - { - if ( zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} - strcpy( lDialogString , "szAnswer=$(zenity" ) ; - if ( zenity3Present() >= 3 ) - { - strcat( lDialogString, " --attach=$(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 , "szAnswer=$(matedialog" ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} - strcpy( lDialogString , "szAnswer=$(qarma" ) ; - 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, "\"") ; - } - if ( aDefaultInput && strlen(aDefaultInput) ) - { - strcat(lDialogString, " --entry-text=\"") ; - strcat(lDialogString, aDefaultInput) ; - strcat(lDialogString, "\"") ; - } - else - { - strcat(lDialogString, " --hide-text") ; - } - strcat( lDialogString , - ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi"); - } - else if ( kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} - strcpy( lDialogString , "szAnswer=$(kdialog" ) ; - if ( ! aDefaultInput ) - { - strcat(lDialogString, " --password ") ; - } - else - { - strcat(lDialogString, " --inputbox ") ; - - } - strcat(lDialogString, "\"") ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage ) ; - } - strcat(lDialogString , "\" \"" ) ; - if ( aDefaultInput && strlen(aDefaultInput) ) - { - strcat(lDialogString, aDefaultInput ) ; - } - strcat(lDialogString , "\"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - 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;} - 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;} - strcpy( lDialogString , "szAnswer=$(gmessage -buttons Ok:1,Cancel:0 -center \""); - } + static char lBuff[MAX_PATH_OR_CMD]; + char * lDialogString = NULL; + char * lpDialogString; + FILE * lIn ; + int lResult ; + int lWasGdialog = 0 ; + int lWasGraphicDialog = 0 ; + int lWasXterm = 0 ; + int lWasBasicXterm = 0 ; + struct termios oldt ; + struct termios newt ; + char * lEOF; + size_t lTitleLen ; + size_t lMessageLen ; + + lBuff[0]='\0'; + + lTitleLen = aTitle ? strlen(aTitle) : 0 ; + lMessageLen = aMessage ? strlen(aMessage) : 0 ; + if ( !aTitle || strcmp(aTitle,"tinyfd_query") ) + { + lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); + } - if ( aMessage && strlen(aMessage) ) - { - strcat( lDialogString , aMessage ) ; - } - strcat(lDialogString, "\"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat( lDialogString , " -title \""); - strcat( lDialogString , aTitle ) ; - strcat(lDialogString, "\" " ) ; - } - strcat(lDialogString, " -entrytext \"" ) ; - if ( aDefaultInput && strlen(aDefaultInput) ) - { - strcat( lDialogString , aDefaultInput ) ; - } - strcat(lDialogString, "\"" ) ; - strcat( lDialogString , ");echo $?$szAnswer"); - } - else if ( !gdialogPresent() && tkinter2Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;} - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( ) ) - { - strcat( lDialogString , " -i" ) ; /* for osx without console */ - } - - strcat( lDialogString , -" -c \"import Tkinter,tkSimpleDialog;root=Tkinter.Tk();root.withdraw();"); - - if ( isDarwin( ) ) - { - strcat( lDialogString , + if ( osascriptPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} + strcpy( lDialogString , "osascript "); + if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat( lDialogString , " -e 'try' -e 'display dialog \"") ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString, "\" ") ; + strcat(lDialogString, "default answer \"") ; + if ( aDefaultInput && strlen(aDefaultInput) ) + { + strcat(lDialogString, aDefaultInput) ; + } + strcat(lDialogString, "\" ") ; + if ( ! aDefaultInput ) + { + strcat(lDialogString, "hidden answer true ") ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + strcat(lDialogString, "with icon note' ") ; + strcat(lDialogString, "-e '\"1\" & text returned of result' " ); + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e '0' " ); + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat(lDialogString, " -e 'end tell'") ; + } + else if ( kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} + strcpy( lDialogString , "szAnswer=$(kdialog" ) ; + + if ( kdialogPresent() == 2 ) + { + strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ + } + + if ( ! aDefaultInput ) + { + strcat(lDialogString, " --password ") ; + } + else + { + strcat(lDialogString, " --inputbox ") ; + + } + strcat(lDialogString, "\"") ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage ) ; + } + strcat(lDialogString , "\" \"" ) ; + if ( aDefaultInput && strlen(aDefaultInput) ) + { + strcat(lDialogString, aDefaultInput ) ; + } + strcat(lDialogString , "\"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + strcat( lDialogString , + ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi"); + } + else if ( zenityPresent() || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) + { + if ( zenityPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} + strcpy( lDialogString , "szAnswer=$(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 , "szAnswer=$(matedialog" ) ; + } + else if ( shellementaryPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char const *)1;} + strcpy( lDialogString , "szAnswer=$(shellementary" ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} + strcpy( lDialogString , "szAnswer=$(qarma" ) ; + if ( !getenv("SSH_TTY") ) + { + 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, "\"") ; + } + if ( aDefaultInput && strlen(aDefaultInput) ) + { + strcat(lDialogString, " --entry-text=\"") ; + strcat(lDialogString, aDefaultInput) ; + strcat(lDialogString, "\"") ; + } + else + { + strcat(lDialogString, " --hide-text") ; + } + 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;} + 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;} + strcpy( lDialogString , "szAnswer=$(gmessage -buttons Ok:1,Cancel:0 -center \""); + } + + if ( aMessage && strlen(aMessage) ) + { + strcat( lDialogString , aMessage ) ; + } + strcat(lDialogString, "\"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat( lDialogString , " -title \""); + strcat( lDialogString , aTitle ) ; + strcat(lDialogString, "\" " ) ; + } + strcat(lDialogString, " -entrytext \"" ) ; + if ( aDefaultInput && strlen(aDefaultInput) ) + { + strcat( lDialogString , aDefaultInput ) ; + } + strcat(lDialogString, "\"" ) ; + strcat( lDialogString , ");echo $?$szAnswer"); + } + 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( ) ) + { + strcat( lDialogString , " -i" ) ; /* for osx without console */ + } + + strcat( lDialogString , +" -S -c \"import Tkinter,tkSimpleDialog;root=Tkinter.Tk();root.withdraw();"); + + if ( 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) ) - { - 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;} - lWasGraphicDialog = 1 ; - lWasGdialog = 1 ; - strcpy( lDialogString , "(gdialog " ) ; - } - else if ( xdialogPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} - lWasGraphicDialog = 1 ; - strcpy( lDialogString , "(Xdialog " ) ; - } - else if ( dialogName( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} - if ( isTerminalRunning( ) ) - { - strcpy( lDialogString , "(dialog " ) ; - } - else - { - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(" ) ; - strcat( lDialogString , dialogName() ) ; - strcat( lDialogString , " " ) ; - } - } - else if ( isTerminalRunning( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char const *)0;} - strcpy( lDialogString , "(whiptail " ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char const *)0;} - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(whiptail " ) ; - } - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - if ( !xdialogPresent() && !gdialogPresent() ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, "tab: move focus") ; - if ( ! aDefaultInput && !lWasGdialog ) - { - strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ; - } - strcat(lDialogString, "\" ") ; - } - - if ( aDefaultInput || lWasGdialog ) - { - strcat( lDialogString , "--inputbox" ) ; - } - else - { - if ( !lWasGraphicDialog && dialogName() && isDialogVersionBetter09b() ) - { - strcat( lDialogString , "--insecure " ) ; - } - strcat( lDialogString , "--passwordbox" ) ; - } - strcat( lDialogString , " \"" ) ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat(lDialogString,"\" 10 60 ") ; - if ( aDefaultInput && strlen(aDefaultInput) ) - { - strcat(lDialogString, "\"") ; - strcat(lDialogString, aDefaultInput) ; - strcat(lDialogString, "\" ") ; - } - if ( lWasGraphicDialog ) - { - strcat(lDialogString,") 2>/tmp/tinyfd.txt;\ - if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ - tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; - } - else - { - strcat(lDialogString,">/dev/tty ) 2>/tmp/tinyfd.txt;\ - if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ - tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; - - if ( lWasXterm ) - { - strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt"); - } - else - { - strcat(lDialogString, "; clear >/dev/tty") ; - } - } - } - else if ( ! isTerminalRunning( ) && terminalName() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} - lWasBasicXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'" ) ; - if ( !gWarningDisplayed && !tinyfd_forceConsole) - { - tinyfd_messageBox(gTitle,gMessageUnix,"ok","warning",0); - gWarningDisplayed = 1 ; - } - if ( aTitle && strlen(aTitle) && !tinyfd_forceConsole) - { - strcat( lDialogString , "echo \"" ) ; - strcat( lDialogString, aTitle) ; - strcat( lDialogString , "\";echo;" ) ; - } - - strcat( lDialogString , "echo \"" ) ; - if ( aMessage && strlen(aMessage) ) - { - strcat( lDialogString, aMessage) ; - } - strcat( lDialogString , "\";read " ) ; - if ( ! aDefaultInput ) - { - strcat( lDialogString , "-s " ) ; - } - strcat( lDialogString , "-p \"" ) ; - strcat( lDialogString , "(esc+enter to cancel): \" ANSWER " ) ; - strcat( lDialogString , ";echo 1$ANSWER >/tmp/tinyfd.txt';" ) ; - strcat( lDialogString , "cat -v /tmp/tinyfd.txt"); - } - else if ( !gWarningDisplayed && ! isTerminalRunning( ) && ! terminalName() ) { - tinyfd_messageBox(gTitle,gMessageUnix,"ok","warning",0); - gWarningDisplayed = 1 ; - return NULL; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} - if ( !gWarningDisplayed && !tinyfd_forceConsole) - { - tinyfd_messageBox(gTitle,gMessageUnix,"ok","warning",0); + } + + strcat( lDialogString ,"res=tkSimpleDialog.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() && 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;} + lWasGraphicDialog = 1 ; + lWasGdialog = 1 ; + strcpy( lDialogString , "(gdialog " ) ; + } + else if ( xdialogPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} + lWasGraphicDialog = 1 ; + strcpy( lDialogString , "(Xdialog " ) ; + } + else if ( dialogName( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + if ( isTerminalRunning( ) ) + { + strcpy( lDialogString , "(dialog " ) ; + } + else + { + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(" ) ; + strcat( lDialogString , dialogName() ) ; + strcat( lDialogString , " " ) ; + } + } + else if ( isTerminalRunning( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char const *)0;} + strcpy( lDialogString , "(whiptail " ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char const *)0;} + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(whiptail " ) ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab: move focus") ; + if ( ! aDefaultInput && !lWasGdialog ) + { + strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ; + } + strcat(lDialogString, "\" ") ; + } + + if ( aDefaultInput || lWasGdialog ) + { + strcat( lDialogString , "--inputbox" ) ; + } + else + { + if ( !lWasGraphicDialog && dialogName() && isDialogVersionBetter09b() ) + { + strcat( lDialogString , "--insecure " ) ; + } + strcat( lDialogString , "--passwordbox" ) ; + } + strcat( lDialogString , " \"" ) ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString,"\" 10 60 ") ; + if ( aDefaultInput && strlen(aDefaultInput) ) + { + strcat(lDialogString, "\"") ; + strcat(lDialogString, aDefaultInput) ; + strcat(lDialogString, "\" ") ; + } + if ( lWasGraphicDialog ) + { + strcat(lDialogString,") 2>/tmp/tinyfd.txt;\ + if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ + tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; + } + else + { + strcat(lDialogString,">/dev/tty ) 2>/tmp/tinyfd.txt;\ + if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ + tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; + + if ( lWasXterm ) + { + strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt"); + } + else + { + strcat(lDialogString, "; clear >/dev/tty") ; + } + } + } + else if ( ! isTerminalRunning( ) && terminalName() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} + lWasBasicXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'" ) ; + if ( !gWarningDisplayed && !tinyfd_forceConsole) + { + gWarningDisplayed = 1 ; + tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); + } + if ( aTitle && strlen(aTitle) && !tinyfd_forceConsole) + { + strcat( lDialogString , "echo \"" ) ; + strcat( lDialogString, aTitle) ; + strcat( lDialogString , "\";echo;" ) ; + } + + strcat( lDialogString , "echo \"" ) ; + if ( aMessage && strlen(aMessage) ) + { + strcat( lDialogString, aMessage) ; + } + strcat( lDialogString , "\";read " ) ; + if ( ! aDefaultInput ) + { + strcat( lDialogString , "-s " ) ; + } + strcat( lDialogString , "-p \"" ) ; + strcat( lDialogString , "(esc+enter to cancel): \" ANSWER " ) ; + strcat( lDialogString , ";echo 1$ANSWER >/tmp/tinyfd.txt';" ) ; + strcat( lDialogString , "cat -v /tmp/tinyfd.txt"); + } + else if ( !gWarningDisplayed && ! isTerminalRunning( ) && ! terminalName() ) { gWarningDisplayed = 1 ; - } - if ( aTitle && strlen(aTitle) ) - { - printf("\n%s\n", aTitle); - } - if ( aMessage && strlen(aMessage) ) - { - printf("\n%s\n",aMessage); - } - printf("(esc+enter to cancel): "); fflush(stdout); - if ( ! aDefaultInput ) - { - tcgetattr(STDIN_FILENO, & oldt) ; - newt = oldt ; - newt.c_lflag &= ~ECHO ; - tcsetattr(STDIN_FILENO, TCSANOW, & newt); - } - - lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); - /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */ - if ( ! lEOF || (lBuff[0] == '\0') ) - { - free(lDialogString); + tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"no_solution");return (char const *)0;} return NULL; - } - - if ( lBuff[0] == '\n' ) - { - lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); - /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */ - if ( ! lEOF || (lBuff[0] == '\0') ) - { - free(lDialogString); - return NULL; - } - } - - if ( ! aDefaultInput ) - { - tcsetattr(STDIN_FILENO, TCSANOW, & oldt); - printf("\n"); - } - printf("\n"); - if ( strchr(lBuff,27) ) - { - free(lDialogString); - return NULL ; - } - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - free(lDialogString); - return lBuff ; - } + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char const *)0;} + if ( !gWarningDisplayed && !tinyfd_forceConsole) + { + gWarningDisplayed = 1 ; + tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); + } + if ( aTitle && strlen(aTitle) ) + { + printf("\n%s\n", aTitle); + } + if ( aMessage && strlen(aMessage) ) + { + printf("\n%s\n",aMessage); + } + printf("(esc+enter to cancel): "); fflush(stdout); + if ( ! aDefaultInput ) + { + tcgetattr(STDIN_FILENO, & oldt) ; + newt = oldt ; + newt.c_lflag &= ~ECHO ; + tcsetattr(STDIN_FILENO, TCSANOW, & newt); + } + + lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); + /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */ + if ( ! lEOF || (lBuff[0] == '\0') ) + { + free(lDialogString); + return NULL; + } + + if ( lBuff[0] == '\n' ) + { + lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); + /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */ + if ( ! lEOF || (lBuff[0] == '\0') ) + { + free(lDialogString); + return NULL; + } + } + + if ( ! aDefaultInput ) + { + tcsetattr(STDIN_FILENO, TCSANOW, & oldt); + printf("\n"); + } + printf("\n"); + if ( strchr(lBuff,27) ) + { + free(lDialogString); + return NULL ; + } + if ( lBuff[strlen( lBuff ) -1] == '\n' ) + { + lBuff[strlen( lBuff ) -1] = '\0' ; + } + free(lDialogString); + return lBuff ; + } - if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - lIn = popen( lDialogString , "r" ); - if ( ! lIn ) - { - if ( fileExists("/tmp/tinyfd.txt") ) - { - wipefile("/tmp/tinyfd.txt"); - remove("/tmp/tinyfd.txt"); - } - if ( fileExists("/tmp/tinyfd0.txt") ) - { - wipefile("/tmp/tinyfd0.txt"); - remove("/tmp/tinyfd0.txt"); - } - free(lDialogString); - return NULL ; - } - while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - {} + if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; + lIn = popen( lDialogString , "r" ); + if ( ! lIn ) + { + if ( fileExists("/tmp/tinyfd.txt") ) + { + wipefile("/tmp/tinyfd.txt"); + remove("/tmp/tinyfd.txt"); + } + if ( fileExists("/tmp/tinyfd0.txt") ) + { + wipefile("/tmp/tinyfd0.txt"); + remove("/tmp/tinyfd0.txt"); + } + free(lDialogString); + return NULL ; + } + while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + {} - pclose( lIn ) ; + pclose( lIn ) ; - if ( fileExists("/tmp/tinyfd.txt") ) - { - wipefile("/tmp/tinyfd.txt"); - remove("/tmp/tinyfd.txt"); - } - if ( fileExists("/tmp/tinyfd0.txt") ) - { - wipefile("/tmp/tinyfd0.txt"); - remove("/tmp/tinyfd0.txt"); - } + if ( fileExists("/tmp/tinyfd.txt") ) + { + wipefile("/tmp/tinyfd.txt"); + remove("/tmp/tinyfd.txt"); + } + if ( fileExists("/tmp/tinyfd0.txt") ) + { + wipefile("/tmp/tinyfd0.txt"); + remove("/tmp/tinyfd0.txt"); + } - /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */ - /* printf( "lBuff0: %s\n" , lBuff ) ; */ - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ - if ( lWasBasicXterm ) - { - if ( strstr(lBuff,"^[") ) /* esc was pressed */ - { - free(lDialogString); - return NULL ; - } - } + /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */ + /* printf( "lBuff0: %s\n" , lBuff ) ; */ + if ( lBuff[strlen( lBuff ) -1] == '\n' ) + { + lBuff[strlen( lBuff ) -1] = '\0' ; + } + /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ + if ( lWasBasicXterm ) + { + if ( strstr(lBuff,"^[") ) /* esc was pressed */ + { + free(lDialogString); + return NULL ; + } + } - lResult = strncmp( lBuff , "1" , 1) ? 0 : 1 ; - /* printf( "lResult: %d \n" , lResult ) ; */ - if ( ! lResult ) - { - free(lDialogString); - return NULL ; - } - /* printf( "lBuff+1: %s\n" , lBuff+1 ) ; */ - free(lDialogString); + lResult = strncmp( lBuff , "1" , 1) ? 0 : 1 ; + /* printf( "lResult: %d \n" , lResult ) ; */ + if ( ! lResult ) + { + free(lDialogString); + return NULL ; + } + /* printf( "lBuff+1: %s\n" , lBuff+1 ) ; */ + free(lDialogString); - return lBuff+1 ; + return lBuff+1 ; } @@ -4851,292 +5711,364 @@ char const * tinyfd_saveFileDialog( char const * 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] ; - int i ; - int lWasGraphicDialog = 0 ; - int lWasXterm = 0 ; - char const * p ; - FILE * lIn ; - lBuff[0]='\0'; - - if ( osascriptPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)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 " ); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with prompt \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "default location \"") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "\" " ) ; - } - getLastName( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "default name \"") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "\" " ) ; - } - strcat( lDialogString , ")' " ) ; - strcat(lDialogString, "-e 'on error number -128' " ) ; - strcat(lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; - } - else if ( zenityPresent() || matedialogPresent() || qarmaPresent() ) - { - if ( zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} - strcpy( lDialogString , "zenity" ) ; - if ( zenity3Present() >= 3 ) - { - strcat( lDialogString, " --attach=$(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 (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} - strcpy( lDialogString , "qarma" ) ; - 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, " --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 , aFilterPatterns [i] ) ; - strcat( lDialogString , " " ) ; - } - strcat( lDialogString , "' --file-filter='All files | *'" ) ; - } - } - else if ( kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} - strcpy ( lDialogString , "kdialog --getsavefilename" ) ; - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - strcat(lDialogString, " \"") ; - strcat(lDialogString, aDefaultPathAndFile ) ; - strcat(lDialogString , "\"" ) ; - } - else - { - strcat(lDialogString, " :" ) ; - } - if ( aNumOfFilterPatterns > 0 ) - { - strcat(lDialogString , " \"" ) ; - for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat( lDialogString , aFilterPatterns [i] ) ; - strcat( lDialogString , " " ) ; - } - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) - { - strcat( lDialogString , " | " ) ; - strcat( lDialogString , aSingleFilterDescription ) ; - } - strcat( lDialogString , "\"" ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - } - else if ( ! xdialogPresent() && tkinter2Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;} - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( )) - { - strcat( lDialogString , " -i" ) ; /* for osx without console */ - } - strcat( lDialogString , -" -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();"); - - if ( 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("); - 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 , ")\"" ) ; - } - else if ( xdialogPresent() || dialogName() ) - { - if ( xdialogPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} - lWasGraphicDialog = 1 ; - strcpy( lDialogString , "(Xdialog " ) ; - } - else if ( isTerminalRunning( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} - strcpy( lDialogString , "(dialog " ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(" ) ; - strcat( lDialogString , dialogName() ) ; - strcat( lDialogString , " " ) ; - } - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - if ( !xdialogPresent() && !gdialogPresent() ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; - } - - strcat( lDialogString , "--fselect \"" ) ; - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - if ( ! strchr(aDefaultPathAndFile, '/') ) - { - strcat(lDialogString, "./") ; - } - strcat(lDialogString, aDefaultPathAndFile) ; - } - else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) - { - strcat(lDialogString, getenv("HOME")) ; - strcat(lDialogString, "/") ; - } - else - { - strcat(lDialogString, "./") ; - } - - if ( lWasGraphicDialog ) - { - strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; - } - else - { - strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; - if ( lWasXterm ) - { - strcat( lDialogString , - "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); - } - else - { - strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; - } - } - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - p = tinyfd_inputBox( aTitle , "Save file" , "" ) ; - getPathWithoutFinalSlash( lString , p ) ; - if ( strlen( lString ) && ! dirExists( lString ) ) - { - return NULL ; - } - getLastName(lString,p); - if ( ! strlen(lString) ) - { - return NULL; - } - return p ; - } + 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 ; + FILE * lIn ; + lBuff[0]='\0'; + + if ( osascriptPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)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 " ); + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with prompt \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "default location \"") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "\" " ) ; + } + getLastName( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "default name \"") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "\" " ) ; + } + strcat( lDialogString , ")' " ) ; + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} + + strcpy( lDialogString , "kdialog" ) ; + if ( kdialogPresent() == 2 ) + { + strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ + } + strcat( lDialogString , " --getsavefilename " ) ; + + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + if ( aDefaultPathAndFile[0] != '/' ) + { + strcat(lDialogString, "$PWD/") ; + } + strcat(lDialogString, "\"") ; + strcat(lDialogString, aDefaultPathAndFile ) ; + strcat(lDialogString , "\"" ) ; + } + else + { + strcat(lDialogString, "$PWD/") ; + } + + if ( aNumOfFilterPatterns > 0 ) + { + strcat(lDialogString , " \"" ) ; + for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat( lDialogString , aFilterPatterns [i] ) ; + strcat( lDialogString , " " ) ; + } + if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) + { + strcat( lDialogString , " | " ) ; + strcat( lDialogString , aSingleFilterDescription ) ; + } + strcat( lDialogString , "\"" ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + } + else 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, " --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 , aFilterPatterns [i] ) ; + strcat( lDialogString , " " ) ; + } + strcat( lDialogString , "' --file-filter='All files | *'" ) ; + } + } + else if ( !xdialogPresent() && tkinter2Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char const *)1;} + strcpy( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && isDarwin( )) + { + strcat( lDialogString , " -i" ) ; /* for osx without console */ + } + strcat( lDialogString , +" -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();"); + + if ( 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("); + 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 , ")\"" ) ; + } + 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 , "))\"" ) ; + } + else if ( xdialogPresent() || dialogName() ) + { + if ( xdialogPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} + lWasGraphicDialog = 1 ; + strcpy( lDialogString , "(Xdialog " ) ; + } + else if ( isTerminalRunning( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + strcpy( lDialogString , "(dialog " ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(" ) ; + strcat( lDialogString , dialogName() ) ; + strcat( lDialogString , " " ) ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; + } + + strcat( lDialogString , "--fselect \"" ) ; + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + if ( ! strchr(aDefaultPathAndFile, '/') ) + { + strcat(lDialogString, "./") ; + } + strcat(lDialogString, aDefaultPathAndFile) ; + } + else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) + { + strcat(lDialogString, getenv("HOME")) ; + strcat(lDialogString, "/") ; + } + else + { + strcat(lDialogString, "./") ; + } + + if ( lWasGraphicDialog ) + { + strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; + } + else + { + strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; + if ( lWasXterm ) + { + strcat( lDialogString , + "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); + } + else + { + strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; + } + } + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} + p = tinyfd_inputBox( aTitle , "Save file" , "" ) ; + getPathWithoutFinalSlash( lString , p ) ; + if ( strlen( lString ) && ! dirExists( lString ) ) + { + return NULL ; + } + getLastName(lString,p); + if ( ! strlen(lString) ) + { + return NULL; + } + return p ; + } - if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; + if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; if ( ! ( lIn = popen( lDialogString , "r" ) ) ) { return NULL ; @@ -5146,23 +6078,23 @@ char const * tinyfd_saveFileDialog( pclose( lIn ) ; if ( lBuff[strlen( lBuff ) -1] == '\n' ) { - lBuff[strlen( lBuff ) -1] = '\0' ; + lBuff[strlen( lBuff ) -1] = '\0' ; } - /* printf( "lBuff: %s\n" , lBuff ) ; */ - if ( ! strlen(lBuff) ) - { - return NULL; - } + /* printf( "lBuff: %s\n" , lBuff ) ; */ + if ( ! strlen(lBuff) ) + { + return NULL; + } getPathWithoutFinalSlash( lString , lBuff ) ; if ( strlen( lString ) && ! dirExists( lString ) ) { return NULL ; } - getLastName(lString,lBuff); - if ( ! filenameValid(lString) ) - { - return NULL; - } + getLastName(lString,lBuff); + if ( ! filenameValid(lString) ) + { + return NULL; + } return lBuff ; } @@ -5176,608 +6108,726 @@ char const * tinyfd_openFileDialog( char const * const aSingleFilterDescription , /* NULL or "image files" */ int const 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'; - - if ( osascriptPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} - strcpy( lDialogString , "osascript "); - if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); - strcat( lDialogString , " -e 'try' -e '" ); + 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'; + + if ( osascriptPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} + strcpy( lDialogString , "osascript "); + if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat( lDialogString , " -e 'try' -e '" ); if ( ! aAllowMultipleSelects ) { - strcat( lDialogString , "POSIX path of ( " ); - } - else - { - strcat( lDialogString , "set mylist to " ); - } - strcat( lDialogString , "choose file " ); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with prompt \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "default location \"") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "\" " ) ; - } - if ( aNumOfFilterPatterns > 0 ) - { - strcat(lDialogString , "of type {\"" ); - strcat( lDialogString , aFilterPatterns [0] + 2 ) ; - strcat( lDialogString , "\"" ) ; - for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat( lDialogString , ",\"" ) ; - strcat( lDialogString , aFilterPatterns [i] + 2) ; - strcat( lDialogString , "\"" ) ; - } - strcat( lDialogString , "} " ) ; - } - if ( aAllowMultipleSelects ) - { - strcat( lDialogString , "multiple selections allowed true ' " ) ; - strcat( lDialogString , - "-e 'set mystring to POSIX path of item 1 of mylist' " ); - strcat( lDialogString , - "-e 'repeat with i from 2 to the count of mylist' " ); - strcat( lDialogString , "-e 'set mystring to mystring & \"|\"' " ); - strcat( lDialogString , - "-e 'set mystring to mystring & POSIX path of item i of mylist' " ); - strcat( lDialogString , "-e 'end repeat' " ); - strcat( lDialogString , "-e 'mystring' " ); - } - else - { - strcat( lDialogString , ")' " ) ; - } - strcat(lDialogString, "-e 'on error number -128' " ) ; - strcat(lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; - } - else if ( zenityPresent() || matedialogPresent() || qarmaPresent() ) - { - if ( zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} - strcpy( lDialogString , "zenity" ) ; - if ( zenity3Present() >= 3 ) - { - strcat( lDialogString, " --attach=$(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 (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} - strcpy( lDialogString , "qarma" ) ; - strcat( lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ - } - strcat( lDialogString , " --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 , aFilterPatterns [i] ) ; - strcat( lDialogString , " " ) ; - } - strcat( lDialogString , "' --file-filter='All files | *'" ) ; - } - } - else if ( kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} - lWasKdialog = 1 ; - strcpy( lDialogString , "kdialog --getopenfilename" ) ; - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - strcat(lDialogString, " \"") ; - strcat(lDialogString, aDefaultPathAndFile ) ; - - strcat(lDialogString , "\"" ) ; - } - else - { - strcat(lDialogString, " :" ) ; - } - if ( aNumOfFilterPatterns > 0 ) - { - strcat(lDialogString , " \"" ) ; - for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat( lDialogString , aFilterPatterns [i] ) ; - strcat( lDialogString , " " ) ; - } - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) - { - strcat( lDialogString , " | " ) ; - strcat( lDialogString , aSingleFilterDescription ) ; - } - strcat( lDialogString , "\"" ) ; - } - if ( aAllowMultipleSelects ) - { - strcat( lDialogString , " --multiple --separate-output" ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - } - else if ( ! xdialogPresent() && tkinter2Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;} - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( ) ) - { - strcat( lDialogString , " -i" ) ; /* for osx without console */ - } - strcat( lDialogString , -" -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();"); - - if ( isDarwin( ) ) - { - strcat( lDialogString , + strcat( lDialogString , "POSIX path of ( " ); + } + else + { + strcat( lDialogString , "set mylist to " ); + } + strcat( lDialogString , "choose file " ); + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with prompt \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "default location \"") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "\" " ) ; + } + if ( aNumOfFilterPatterns > 0 ) + { + strcat(lDialogString , "of type {\"" ); + strcat( lDialogString , aFilterPatterns [0] + 2 ) ; + strcat( lDialogString , "\"" ) ; + for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat( lDialogString , ",\"" ) ; + strcat( lDialogString , aFilterPatterns [i] + 2) ; + strcat( lDialogString , "\"" ) ; + } + strcat( lDialogString , "} " ) ; + } + if ( aAllowMultipleSelects ) + { + strcat( lDialogString , "multiple selections allowed true ' " ) ; + strcat( lDialogString , + "-e 'set mystring to POSIX path of item 1 of mylist' " ); + strcat( lDialogString , + "-e 'repeat with i from 2 to the count of mylist' " ); + strcat( lDialogString , "-e 'set mystring to mystring & \"|\"' " ); + strcat( lDialogString , + "-e 'set mystring to mystring & POSIX path of item i of mylist' " ); + strcat( lDialogString , "-e 'end repeat' " ); + strcat( lDialogString , "-e 'mystring' " ); + } + else + { + strcat( lDialogString , ")' " ) ; + } + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} + lWasKdialog = 1 ; + + strcpy( lDialogString , "kdialog" ) ; + if ( kdialogPresent() == 2 ) + { + strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ + } + strcat( lDialogString , " --getopenfilename " ) ; + + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + if ( aDefaultPathAndFile[0] != '/' ) + { + strcat(lDialogString, "$PWD/") ; + } + strcat(lDialogString, "\"") ; + strcat(lDialogString, aDefaultPathAndFile ) ; + strcat(lDialogString , "\"" ) ; + } + else + { + strcat(lDialogString, "$PWD/") ; + } + + if ( aNumOfFilterPatterns > 0 ) + { + strcat(lDialogString , " \"" ) ; + for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat( lDialogString , aFilterPatterns [i] ) ; + strcat( lDialogString , " " ) ; + } + if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) + { + strcat( lDialogString , " | " ) ; + strcat( lDialogString , aSingleFilterDescription ) ; + } + strcat( lDialogString , "\"" ) ; + } + if ( aAllowMultipleSelects ) + { + strcat( lDialogString , " --multiple --separate-output" ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + } + else 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 , " --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 , aFilterPatterns [i] ) ; + strcat( lDialogString , " " ) ; + } + strcat( lDialogString , "' --file-filter='All files | *'" ) ; + } + } + else if ( tkinter2Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char const *)1;} + strcpy( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && isDarwin( ) ) + { + strcat( lDialogString , " -i" ) ; /* for osx without console */ + } + strcat( lDialogString , +" -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();"); + + if ( isDarwin( ) ) + { + strcat( lDialogString , "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \ frontmost of process \\\"Python\\\" to true' ''');"); - } - strcat( lDialogString , "lFiles=tkFileDialog.askopenfilename("); + } + strcat( lDialogString , "lFiles=tkFileDialog.askopenfilename("); if ( aAllowMultipleSelects ) { - strcat( lDialogString , "multiple=1," ) ; + strcat( lDialogString , "multiple=1," ) ; } if ( aTitle && strlen(aTitle) ) { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; + 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 , ");\ + 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;} - lWasGraphicDialog = 1 ; - strcpy( lDialogString , "(Xdialog " ) ; - } - else if ( isTerminalRunning( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} - strcpy( lDialogString , "(dialog " ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(" ) ; - strcat( lDialogString , dialogName() ) ; - strcat( lDialogString , " " ) ; - } - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - if ( !xdialogPresent() && !gdialogPresent() ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; - } - - strcat( lDialogString , "--fselect \"" ) ; - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - if ( ! strchr(aDefaultPathAndFile, '/') ) - { - strcat(lDialogString, "./") ; - } - strcat(lDialogString, aDefaultPathAndFile) ; - } - else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) - { - strcat(lDialogString, getenv("HOME")) ; - strcat(lDialogString, "/"); - } - else - { - strcat(lDialogString, "./") ; - } - - if ( lWasGraphicDialog ) - { - strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; - } - else - { - strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; - if ( lWasXterm ) - { - strcat( lDialogString , - "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); - } - else - { - strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; - } - } - } - 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 ; - } + } + 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;} + lWasGraphicDialog = 1 ; + strcpy( lDialogString , "(Xdialog " ) ; + } + else if ( isTerminalRunning( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + strcpy( lDialogString , "(dialog " ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(" ) ; + strcat( lDialogString , dialogName() ) ; + strcat( lDialogString , " " ) ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; + } + + strcat( lDialogString , "--fselect \"" ) ; + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + if ( ! strchr(aDefaultPathAndFile, '/') ) + { + strcat(lDialogString, "./") ; + } + strcat(lDialogString, aDefaultPathAndFile) ; + } + else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) + { + strcat(lDialogString, getenv("HOME")) ; + strcat(lDialogString, "/"); + } + else + { + strcat(lDialogString, "./") ; + } + + if ( lWasGraphicDialog ) + { + strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; + } + else + { + strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; + if ( lWasXterm ) + { + strcat( lDialogString , + "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); + } + else + { + strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; + } + } + } + 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 (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; if ( ! ( lIn = popen( lDialogString , "r" ) ) ) { return NULL ; } - lBuff[0]='\0'; - p=lBuff; - while ( fgets( p , sizeof( lBuff ) , lIn ) != NULL ) - { - p += strlen( p ); - } + lBuff[0]='\0'; + p=lBuff; + while ( fgets( p , sizeof( lBuff ) , lIn ) != NULL ) + { + p += strlen( p ); + } pclose( lIn ) ; if ( lBuff[strlen( lBuff ) -1] == '\n' ) { - lBuff[strlen( lBuff ) -1] = '\0' ; + lBuff[strlen( lBuff ) -1] = '\0' ; } /* printf( "lBuff: %s\n" , lBuff ) ; */ - if ( lWasKdialog && aAllowMultipleSelects ) - { - p = lBuff ; - while ( ( p = strchr( p , '\n' ) ) ) - * p = '|' ; - } - /* printf( "lBuff2: %s\n" , lBuff ) ; */ - if ( ! strlen( lBuff ) ) - { - return NULL; - } - if ( aAllowMultipleSelects && strchr(lBuff, '|') ) - { - p2 = ensureFilesExist( lBuff , lBuff ) ; - } - else if ( fileExists(lBuff) ) - { - p2 = lBuff ; - } - else - { - return NULL ; - } - /* printf( "lBuff3: %s\n" , p2 ) ; */ + if ( lWasKdialog && aAllowMultipleSelects ) + { + p = lBuff ; + while ( ( p = strchr( p , '\n' ) ) ) + * p = '|' ; + } + /* printf( "lBuff2: %s\n" , lBuff ) ; */ + if ( ! strlen( lBuff ) ) + { + return NULL; + } + if ( aAllowMultipleSelects && strchr(lBuff, '|') ) + { + p2 = ensureFilesExist( lBuff , lBuff ) ; + } + else if ( fileExists(lBuff) ) + { + p2 = lBuff ; + } + else + { + return NULL ; + } + /* printf( "lBuff3: %s\n" , p2 ) ; */ - return p2 ; + return p2 ; } char const * tinyfd_selectFolderDialog( - char const * const aTitle , /* "" */ - char const * const aDefaultPath ) /* "" */ + char const * const aTitle , /* "" */ + char const * const aDefaultPath ) /* "" */ { - static char lBuff [MAX_PATH_OR_CMD] ; - char lDialogString [MAX_PATH_OR_CMD] ; - FILE * lIn ; - char const * p ; - int lWasGraphicDialog = 0 ; - int lWasXterm = 0 ; - lBuff[0]='\0'; - - if ( osascriptPresent( )) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)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 "); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with prompt \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - if ( aDefaultPath && strlen(aDefaultPath) ) - { - strcat(lDialogString, "default location \"") ; - strcat(lDialogString, aDefaultPath ) ; - strcat(lDialogString , "\" " ) ; - } - strcat( lDialogString , ")' " ) ; - strcat(lDialogString, "-e 'on error number -128' " ) ; - strcat(lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; - } - else if ( zenityPresent() || matedialogPresent() || qarmaPresent() ) - { - if ( zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} - strcpy( lDialogString , "zenity" ) ; - if ( zenity3Present() >= 3 ) - { - strcat( lDialogString, " --attach=$(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 (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} - strcpy( lDialogString , "qarma" ) ; - 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, " --filename=\"") ; - strcat(lDialogString, aDefaultPath) ; - strcat(lDialogString, "\"") ; - } - } - else if ( kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} - strcpy( lDialogString , "kdialog --getexistingdirectory" ) ; - if ( aDefaultPath && strlen(aDefaultPath) ) - { - strcat(lDialogString, " \"") ; - strcat(lDialogString, aDefaultPath ) ; - strcat(lDialogString , "\"" ) ; - } - else - { - strcat(lDialogString, " :" ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - } - else if ( ! xdialogPresent() && tkinter2Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;} - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( ) ) - { - strcat( lDialogString , " -i" ) ; /* for osx without console */ - } + static char lBuff [MAX_PATH_OR_CMD] ; + char lDialogString [MAX_PATH_OR_CMD] ; + FILE * lIn ; + char const * p ; + int lWasGraphicDialog = 0 ; + int lWasXterm = 0 ; + lBuff[0]='\0'; + + if ( osascriptPresent( )) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)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 "); + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with prompt \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + if ( aDefaultPath && strlen(aDefaultPath) ) + { + strcat(lDialogString, "default location \"") ; + strcat(lDialogString, aDefaultPath ) ; + strcat(lDialogString , "\" " ) ; + } + strcat( lDialogString , ")' " ) ; + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} + strcpy( lDialogString , "kdialog" ) ; + if ( kdialogPresent() == 2 ) + { + strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ + } + strcat( lDialogString , " --getexistingdirectory " ) ; + + if ( aDefaultPath && strlen(aDefaultPath) ) + { + if ( aDefaultPath[0] != '/' ) + { + strcat(lDialogString, "$PWD/") ; + } + strcat(lDialogString, "\"") ; + strcat(lDialogString, aDefaultPath ) ; + strcat(lDialogString , "\"" ) ; + } + else + { + strcat(lDialogString, "$PWD/") ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + } + else 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 , " --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, "\"") ; + } + } + else if ( !xdialogPresent() && tkinter2Present( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char const *)1;} + strcpy( lDialogString , gPython2Name ) ; + if ( ! isTerminalRunning( ) && isDarwin( ) ) + { + strcat( lDialogString , " -i" ) ; /* for osx without console */ + } strcat( lDialogString , -" -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();"); +" -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();"); - if ( isDarwin( ) ) - { - strcat( lDialogString , + if ( 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.askdirectory("); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "',") ; - } + } + + strcat( lDialogString , "print tkFileDialog.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;} - lWasGraphicDialog = 1 ; - strcpy( lDialogString , "(Xdialog " ) ; - } - else if ( isTerminalRunning( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} - strcpy( lDialogString , "(dialog " ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(" ) ; - strcat( lDialogString , dialogName() ) ; - strcat( lDialogString , " " ) ; - } - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - if ( !xdialogPresent() && !gdialogPresent() ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; - } - - strcat( lDialogString , "--dselect \"" ) ; - if ( aDefaultPath && strlen(aDefaultPath) ) - { - strcat(lDialogString, aDefaultPath) ; - ensureFinalSlash(lDialogString); - } - else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) - { - strcat(lDialogString, getenv("HOME")) ; - strcat(lDialogString, "/"); - } - else - { - strcat(lDialogString, "./") ; - } - - if ( lWasGraphicDialog ) - { - strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; - } - else - { - strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; - if ( lWasXterm ) - { - strcat( lDialogString , - "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); - } - else - { - strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; - } - } - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - p = tinyfd_inputBox(aTitle, "Select folder",""); - if ( !p || ! strlen( p ) || ! dirExists( p ) ) - { - return NULL ; - } - return p ; - } + strcat(lDialogString, "initialdir='") ; + strcat(lDialogString, aDefaultPath ) ; + strcat(lDialogString , "'" ) ; + } + 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;} + lWasGraphicDialog = 1 ; + strcpy( lDialogString , "(Xdialog " ) ; + } + else if ( isTerminalRunning( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + strcpy( lDialogString , "(dialog " ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char const *)0;} + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(" ) ; + strcat( lDialogString , dialogName() ) ; + strcat( lDialogString , " " ) ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; + } + + strcat( lDialogString , "--dselect \"" ) ; + if ( aDefaultPath && strlen(aDefaultPath) ) + { + strcat(lDialogString, aDefaultPath) ; + ensureFinalSlash(lDialogString); + } + else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) + { + strcat(lDialogString, getenv("HOME")) ; + strcat(lDialogString, "/"); + } + else + { + strcat(lDialogString, "./") ; + } + + if ( lWasGraphicDialog ) + { + strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; + } + else + { + strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; + if ( lWasXterm ) + { + strcat( lDialogString , + "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); + } + else + { + strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; + } + } + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} + p = tinyfd_inputBox(aTitle, "Select folder",""); + if ( !p || ! strlen( p ) || ! dirExists( p ) ) + { + return NULL ; + } + return p ; + } if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; if ( ! ( lIn = popen( lDialogString , "r" ) ) ) { return NULL ; } - while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - {} - pclose( lIn ) ; + while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + {} + pclose( lIn ) ; if ( lBuff[strlen( lBuff ) -1] == '\n' ) { - lBuff[strlen( lBuff ) -1] = '\0' ; + lBuff[strlen( lBuff ) -1] = '\0' ; } - /* printf( "lBuff: %s\n" , lBuff ) ; */ - if ( ! strlen( lBuff ) || ! dirExists( lBuff ) ) - { - return NULL ; - } - return lBuff ; + /* printf( "lBuff: %s\n" , lBuff ) ; */ + if ( ! strlen( lBuff ) || ! dirExists( lBuff ) ) + { + return NULL ; + } + return lBuff ; } @@ -5786,345 +6836,392 @@ frontmost of process \\\"Python\\\" to true' ''');"); /* 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"*/ - unsigned char const aDefaultRGB[3] , /* { 0 , 255 , 255 } */ - unsigned char aoResultRGB[3] ) /* { 0 , 0 , 0 } */ + 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 lBuff [128] ; - char lTmp [128] ; - char lDialogString [MAX_PATH_OR_CMD] ; - char lDefaultHexRGB[8]; - char * lpDefaultHexRGB; - unsigned char lDefaultRGB[3]; - char const * p; - FILE * lIn ; - int i ; - int lWasZenity3 = 0 ; - int lWasOsascript = 0 ; - 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 ; - } + static char lBuff [128] ; + char lTmp [128] ; + char lDialogString [MAX_PATH_OR_CMD] ; + char lDefaultHexRGB[8]; + char * lpDefaultHexRGB; + unsigned char lDefaultRGB[3]; + char const * p; + FILE * lIn ; + int i ; + int lWasZenity3 = 0 ; + int lWasOsascript = 0 ; + 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 ( osascriptPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} - lWasOsascript = 1 ; - strcpy( lDialogString , "osascript"); - - if ( ! osx9orBetter() ) - { - strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); - strcat( lDialogString , " -e 'try' -e 'set mycolor to choose color default color {"); - } - else - { - strcat( lDialogString , + if ( osascriptPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char const *)1;} + lWasOsascript = 1 ; + strcpy( lDialogString , "osascript"); + + if ( ! osx9orBetter() ) + { + strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat( lDialogString , " -e 'try' -e 'set mycolor to choose color default color {"); + } + else + { + strcat( lDialogString , " -e 'try' -e 'tell app (path to frontmost application as Unicode text) \ to set mycolor to choose color default color {"); - } - - sprintf(lTmp, "%d", 256 * lDefaultRGB[0] ) ; - strcat(lDialogString, lTmp ) ; - strcat(lDialogString, "," ) ; - sprintf(lTmp, "%d", 256 * lDefaultRGB[1] ) ; - strcat(lDialogString, lTmp ) ; - strcat(lDialogString, "," ) ; - sprintf(lTmp, "%d", 256 * lDefaultRGB[2] ) ; - strcat(lDialogString, lTmp ) ; - strcat(lDialogString, "}' " ) ; - strcat( lDialogString , + } + + sprintf(lTmp, "%d", 256 * lDefaultRGB[0] ) ; + strcat(lDialogString, lTmp ) ; + strcat(lDialogString, "," ) ; + sprintf(lTmp, "%d", 256 * lDefaultRGB[1] ) ; + strcat(lDialogString, lTmp ) ; + strcat(lDialogString, "," ) ; + sprintf(lTmp, "%d", 256 * lDefaultRGB[2] ) ; + strcat(lDialogString, lTmp ) ; + strcat(lDialogString, "}' " ) ; + strcat( lDialogString , "-e 'set mystring to ((item 1 of mycolor) div 256 as integer) as string' " ); - strcat( lDialogString , + strcat( lDialogString , "-e 'repeat with i from 2 to the count of mycolor' " ); - strcat( lDialogString , + strcat( lDialogString , "-e 'set mystring to mystring & \" \" & ((item i of mycolor) div 256 as integer) as string' " ); - strcat( lDialogString , "-e 'end repeat' " ); - strcat( lDialogString , "-e 'mystring' "); - strcat(lDialogString, "-e 'on error number -128' " ) ; - strcat(lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; - } - else if ( zenity3Present() || matedialogPresent() || qarmaPresent() ) - { - lWasZenity3 = 1 ; - if ( zenity3Present() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity3");return (char const *)1;} - strcpy( lDialogString , "zenity" ); - if ( zenity3Present() >= 3 ) - { - strcat( lDialogString, " --attach=$(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 (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} - strcpy( lDialogString , "qarma" ) ; - 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 ) ; - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title=\"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - } - else if ( kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} - sprintf( lDialogString , "kdialog --getcolor --default '%s'" , lpDefaultHexRGB ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - } - else if ( xdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} - lWasXdialog = 1 ; - strcpy( lDialogString , "Xdialog --colorsel \"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, aTitle) ; - } - strcat(lDialogString, "\" 0 60 ") ; - sprintf(lTmp,"%hhu %hhu %hhu",lDefaultRGB[0], - lDefaultRGB[1],lDefaultRGB[2]); - strcat(lDialogString, lTmp) ; - strcat(lDialogString, " 2>&1"); - } - else if ( tkinter2Present( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"tkinter");return (char const *)1;} - strcpy( lDialogString , gPython2Name ) ; - if ( ! isTerminalRunning( ) && isDarwin( ) ) - { - strcat( lDialogString , " -i" ) ; /* for osx without console */ - } - - strcat( lDialogString , -" -c \"import Tkinter,tkColorChooser;root=Tkinter.Tk();root.withdraw();"); - - if ( isDarwin( ) ) - { - strcat( lDialogString , + strcat( lDialogString , "-e 'end repeat' " ); + strcat( lDialogString , "-e 'mystring' "); + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char const *)1;} + strcpy( lDialogString , "kdialog" ) ; + if ( kdialogPresent() == 2 ) + { + 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 ) ; + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + } + else if ( zenity3Present() || matedialogPresent() || shellementaryPresent() || qarmaPresent() ) + { + lWasZenity3 = 1 ; + if ( zenity3Present() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity3");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 , " --color-selection --show-palette" ) ; + sprintf( lDialogString + strlen(lDialogString), " --color=%s" , lpDefaultHexRGB ) ; + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title=\"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + } + else if ( xdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char const *)1;} + lWasXdialog = 1 ; + strcpy( lDialogString , "Xdialog --colorsel \"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + } + strcat(lDialogString, "\" 0 60 ") ; + sprintf(lTmp,"%hhu %hhu %hhu",lDefaultRGB[0], + lDefaultRGB[1],lDefaultRGB[2]); + strcat(lDialogString, lTmp) ; + strcat(lDialogString, " 2>&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( ) ) + { + strcat( lDialogString , " -i" ) ; /* for osx without console */ + } + + strcat( lDialogString , +" -S -c \"import Tkinter,tkColorChooser;root=Tkinter.Tk();root.withdraw();"); + + if ( isDarwin( ) ) + { + strcat( lDialogString , "import os;os.system('''osascript -e 'tell app \\\"Finder\\\" to set \ frontmost of process \\\"Python\\\" to true' ''');"); - } + } - strcat( lDialogString , "res=tkColorChooser.askcolor(color='" ) ; - strcat(lDialogString, lpDefaultHexRGB ) ; - strcat(lDialogString, "'") ; + strcat( lDialogString , "res=tkColorChooser.askcolor(color='" ) ; + strcat(lDialogString, lpDefaultHexRGB ) ; + strcat(lDialogString, "'") ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, ",title='") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "'") ; - } - 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); - if ( !p || (strlen(p) != 7) || (p[0] != '#') ) - { - return NULL ; - } - for ( i = 1 ; i < 7 ; i ++ ) - { - if ( ! isxdigit( p[i] ) ) - { - return NULL ; - } - } - Hex2RGB(p,aoResultRGB); - return p ; - } + } + 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); + if ( !p || (strlen(p) != 7) || (p[0] != '#') ) + { + return NULL ; + } + for ( i = 1 ; i < 7 ; i ++ ) + { + if ( ! isxdigit( p[i] ) ) + { + return NULL ; + } + } + Hex2RGB(p,aoResultRGB); + return p ; + } - if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - if ( ! ( lIn = popen( lDialogString , "r" ) ) ) - { - 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 ) ; + while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + } + pclose( lIn ) ; if ( ! strlen( lBuff ) ) { return NULL ; } - /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */ - /* printf( "lBuff0: %s\n" , lBuff ) ; */ + /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */ + /* printf( "lBuff0: %s\n" , lBuff ) ; */ if ( lBuff[strlen( lBuff ) -1] == '\n' ) { - lBuff[strlen( lBuff ) -1] = '\0' ; + lBuff[strlen( lBuff ) -1] = '\0' ; } - if ( lWasZenity3 ) + + if ( lWasZenity3 ) { - if ( lBuff[0] == '#' ) { - lBuff[3]=lBuff[5]; - lBuff[4]=lBuff[6]; - lBuff[5]=lBuff[9]; - lBuff[6]=lBuff[10]; - lBuff[7]='\0'; - Hex2RGB(lBuff,aoResultRGB); - } - else if ( lBuff[3] == '(' ) { - sscanf(lBuff,"rgb(%hhu,%hhu,%hhu", - & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]); - RGB2Hex(aoResultRGB,lBuff); - } - else if ( lBuff[4] == '(' ) { - sscanf(lBuff,"rgba(%hhu,%hhu,%hhu", - & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]); - RGB2Hex(aoResultRGB,lBuff); - } + if ( lBuff[0] == '#' ) + { + if ( strlen(lBuff)>7 ) + { + lBuff[3]=lBuff[5]; + lBuff[4]=lBuff[6]; + lBuff[5]=lBuff[9]; + lBuff[6]=lBuff[10]; + lBuff[7]='\0'; + } + Hex2RGB(lBuff,aoResultRGB); + } + else if ( lBuff[3] == '(' ) { + sscanf(lBuff,"rgb(%hhu,%hhu,%hhu", + & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]); + RGB2Hex(aoResultRGB,lBuff); + } + else if ( lBuff[4] == '(' ) { + sscanf(lBuff,"rgba(%hhu,%hhu,%hhu", + & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]); + 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); + /* printf( "lBuff: %s\n" , lBuff ) ; */ + sscanf(lBuff,"%hhu %hhu %hhu", + & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]); + 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 ; + Hex2RGB(lBuff,aoResultRGB); + } + /* printf("%d %d %d\n", aoResultRGB[0],aoResultRGB[1],aoResultRGB[2]); */ + /* printf( "lBuff: %s\n" , lBuff ) ; */ + return lBuff ; } /* 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"} */ + 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 ; - lBuff[0]='\0'; - int i ; + static char lBuff [MAX_PATH_OR_CMD] ; + char lDialogString [MAX_PATH_OR_CMD] ; + FILE * lIn ; + int i ; - if ( zenityPresent() || matedialogPresent() || qarmaPresent() ) - { - if ( zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char const *)1;} - strcpy( lDialogString , "zenity" ) ; - if ( zenity3Present() >= 3 ) - { - strcat( lDialogString, " --attach=$(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 (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char const *)1;} - strcpy( lDialogString , "qarma" ) ; - strcat( lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ - } - strcat( lDialogString , " --list --print-column=ALL" ) ; + lBuff[0]='\0'; - 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 ( 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 ; + 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 */ /* -int main(void) +int main( int argc , char * argv[] ) { char const * lTmp; char const * lTheSaveFileName; @@ -6135,147 +7232,155 @@ char const * lWillBeGraphicMode; unsigned char lRgbColor[3]; FILE * lIn; char lBuffer[1024]; -char lThePassword[1024]; +char lString[1024]; char const * lFilterPatterns[2] = { "*.txt", "*.text" }; +tinyfd_verbose = argc - 1; + lWillBeGraphicMode = tinyfd_inputBox("tinyfd_query", NULL, NULL); +strcpy(lBuffer, "v"); +strcat(lBuffer, tinyfd_version); if (lWillBeGraphicMode) { - strcpy(lBuffer, "graphic mode: "); + strcat(lBuffer, "\ngraphic mode: "); } else { - strcpy(lBuffer, "console mode: "); + strcat(lBuffer, "\nconsole mode: "); } - strcat(lBuffer, tinyfd_response); -strcpy(lThePassword, "tinyfiledialogs v"); -strcat(lThePassword, tinyfd_version); -tinyfd_messageBox(lThePassword, lBuffer, "ok", "info", 0); +strcat(lBuffer, "\n"); +strcat(lBuffer, tinyfd_needs+78); +strcpy(lString, "tinyfiledialogs"); +tinyfd_messageBox(lString, lBuffer, "ok", "info", 0); + +tinyfd_notifyPopup("the title", "the message\n\tfrom outer-space", "info"); if (lWillBeGraphicMode && !tinyfd_forceConsole) { - tinyfd_forceConsole = ! tinyfd_messageBox("Hello World", - "graphic dialogs [yes] / console mode [no]?", - "yesno", "question", 1); + tinyfd_forceConsole = ! tinyfd_messageBox("Hello World", + "graphic dialogs [yes] / console mode [no]?", + "yesno", "question", 1); } lTmp = tinyfd_inputBox( - "a password box", "your password will be revealed", NULL); + "a password box", "your password will be revealed", NULL); if (!lTmp) return 1; -strcpy(lThePassword, lTmp); +strcpy(lString, lTmp); lTheSaveFileName = tinyfd_saveFileDialog( - "let us save this password", - "passwordFile.txt", - 2, - lFilterPatterns, - NULL); + "let us save this password", + "passwordFile.txt", + 2, + lFilterPatterns, + NULL); if (!lTheSaveFileName) { - tinyfd_messageBox( - "Error", - "Save file name is NULL", - "ok", - "error", - 1); - return 1; + tinyfd_messageBox( + "Error", + "Save file name is NULL", + "ok", + "error", + 1); + return 1; } lIn = fopen(lTheSaveFileName, "w"); if (!lIn) { - tinyfd_messageBox( - "Error", - "Can not open this file in write mode", - "ok", - "error", - 1); - return 1; + tinyfd_messageBox( + "Error", + "Can not open this file in write mode", + "ok", + "error", + 1); + return 1; } -fputs(lThePassword, lIn); +fputs(lString, lIn); fclose(lIn); lTheOpenFileName = tinyfd_openFileDialog( - "let us read the password back", - "", - 2, - lFilterPatterns, - NULL, - 0); + "let us read the password back", + "", + 2, + lFilterPatterns, + NULL, + 0); if (!lTheOpenFileName) { - tinyfd_messageBox( - "Error", - "Open file name is NULL", - "ok", - "error", - 1); - return 1; + tinyfd_messageBox( + "Error", + "Open file name is NULL", + "ok", + "error", + 1); + return 1; } lIn = fopen(lTheOpenFileName, "r"); if (!lIn) { - tinyfd_messageBox( - "Error", - "Can not open this file in read mode", - "ok", - "error", - 1); - return(1); + tinyfd_messageBox( + "Error", + "Can not open this file in read mode", + "ok", + "error", + 1); + return(1); } lBuffer[0] = '\0'; fgets(lBuffer, sizeof(lBuffer), lIn); fclose(lIn); tinyfd_messageBox("your password is", - lBuffer, "ok", "info", 1); + lBuffer, "ok", "info", 1); lTheSelectFolderName = tinyfd_selectFolderDialog( - "let us just select a directory", NULL); + "let us just select a directory", NULL); if (!lTheSelectFolderName) { - tinyfd_messageBox( - "Error", - "Select folder name is NULL", - "ok", - "error", - 1); - return 1; + tinyfd_messageBox( + "Error", + "Select folder name is NULL", + "ok", + "error", + 1); + return 1; } tinyfd_messageBox("The selected folder is", - lTheSelectFolderName, "ok", "info", 1); + lTheSelectFolderName, "ok", "info", 1); lTheHexColor = tinyfd_colorChooser( - "choose a nice color", - "#FF0077", - lRgbColor, - lRgbColor); + "choose a nice color", + "#FF0077", + lRgbColor, + lRgbColor); if (!lTheHexColor) { - tinyfd_messageBox( - "Error", - "hexcolor is NULL", - "ok", - "error", - 1); - return 1; + tinyfd_messageBox( + "Error", + "hexcolor is NULL", + "ok", + "error", + 1); + return 1; } tinyfd_messageBox("The selected hexcolor is", - lTheHexColor, "ok", "info", 1); + lTheHexColor, "ok", "info", 1); + + tinyfd_beep(); - return 0; + return 0; } */ diff --git a/libtinyfiledialogs/tinyfiledialogs.h b/libtinyfiledialogs/tinyfiledialogs.h index 410ea59..76e6c82 100644 --- a/libtinyfiledialogs/tinyfiledialogs.h +++ b/libtinyfiledialogs/tinyfiledialogs.h @@ -1,24 +1,21 @@ /*_________ - / \ tinyfiledialogs.h v3.0.4 [Sep 15, 2017] zlib licence + / \ tinyfiledialogs.h v3.3.4 [Mar 15, 2018] zlib licence |tiny file| Unique header file created [November 9, 2014] - | dialogs | Copyright (c) 2014 - 2017 Guillaume Vareille http://ysengrin.com + | dialogs | Copyright (c) 2014 - 2018 Guillaume Vareille http://ysengrin.com \____ ___/ http://tinyfiledialogs.sourceforge.net - \| - git://git.code.sf.net/p/tinyfiledialogs/code - _________________________________________ - | | - | email: tinyfiledialogs@ysengrin.com | - |_________________________________________| - ________________________________________________________________________ - | | - | the windows only wchar_t UTF-16 prototypes are at the end of this file | - |________________________________________________________________________| - -A big thank you to Don Heyse http://ldglite.sf.net for - his code contributions, bug corrections & thorough testing! - -Please 1) let me know If you are using it on different hardware / OS / compiler - 2) leave a very short review on Sourceforge. It helps the ranking in google. + \| 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 | + |________________________________________________________________________| + +Please 1) upvote my stackoverflow answer/advert https://stackoverflow.com/a/47651444 + 2) leave a one word review on Sourceforge. + 3) let me know If you are using it on exotic hardware/OS/compiler tiny file dialogs (cross-platform C C++) InputBox PasswordBox MessageBox ColorPicker @@ -26,7 +23,9 @@ 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 (add it to your C or C++ project) with 6 functions: +a C file and a header (add them to your C or C++ project) with 8 functions: +- beep +- notify popup - message & question - input & password - save file @@ -34,8 +33,9 @@ One C file (add it to your C or C++ project) with 6 functions: - select folder - color picker -Complements OpenGL GLFW GLUT GLUI VTK SFML TGUI SDL Ogre Unity3d ION OpenCV -CEGUI MathGL GLM CPW GLOW IMGUI MyGUI GLT NGL STB & GUI less programs +Complements OpenGL Vulkan GLFW GLUT GLUI VTK SFML TGUI +SDL Ogre Unity3d ION OpenCV CEGUI MathGL GLM CPW GLOW +IMGUI MyGUI GLT NGL STB & GUI less programs NO INIT NO MAIN LOOP @@ -51,22 +51,20 @@ Windows (XP to 10) ASCII MBCS UTF-8 UTF-16 - basic console input Unix (command line calls) ASCII UTF-8 -- applescript -- zenity / matedialog / qarma (zenity for qt) -- kdialog -- Xdialog -- python2 tkinter +- 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 -tested with C & C++ compilers -on VisualStudio MinGW Mac Linux Bsd Solaris Minix Raspbian -using Gnome Kde Enlightenment Mate Cinnamon Unity -Lxde Lxqt Xfce WindowMaker IceWm Cde Jds OpenBox Awesome Jwm +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 Unity Lxde Lxqt Xfce +WindowMaker IceWm Cde Jds OpenBox Awesome Jwm Xdm -bindings for LUA and C# dll -included in LWJGL(java), rust, Allegrobasic +Bindings for LUA and C# dll, Haskell +Included in LWJGL(java), Rust, Allegrobasic - License - @@ -95,8 +93,8 @@ misrepresented as being the original software. 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 with a C++ compiler rather than with a C compiler -(ie. you change the extension from .c to .cpp), you need to comment out: +/* 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: } @@ -105,17 +103,19 @@ and the corresponding closing bracket near the end of this file: extern "C" { #endif -extern char tinyfd_version[8]; /* contains tinyfd current version number */ +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 */ #ifdef _WIN32 /* for UTF-16 use the functions at the end of this files */ -extern int tinyfd_winUtf8; /* 0 (default) or 1 */ +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.*/ #endif -extern int tinyfd_forceConsole ; /* 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, @@ -128,37 +128,47 @@ the functions will not display the dialogs 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 the graphic mode: - windows applescript zenity zenity3 matedialog qarma kdialog - xdialog tkinter gdialog gxmessage xmessage -for the console mode: - dialog whiptail basicinput */ - -int tinyfd_messageBox ( - char const * const aTitle , /* "" */ - char const * const aMessage , /* "" may contain \n \t */ +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 +for console mode: + dialog whiptail basicinput no_solution */ + +void tinyfd_beep(); + +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" */ + /* 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 ) ; /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ -char const * tinyfd_inputBox ( - char const * const aTitle , /* "" */ - char const * const aMessage , /* "" may NOT contain \n \t on windows */ +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 */ /* returns NULL on cancel */ -char const * tinyfd_saveFileDialog ( - char const * const aTitle , /* "" */ - char const * const aDefaultPathAndFile , /* "" */ +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" */ /* returns NULL on cancel */ -char const * tinyfd_openFileDialog ( - char const * const aTitle , /* "" */ - char const * const aDefaultPathAndFile , /* "" */ +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" */ @@ -166,13 +176,13 @@ char const * tinyfd_openFileDialog ( /* in case of multiple files, the separator is | */ /* returns NULL on cancel */ -char const * tinyfd_selectFolderDialog ( - char const * const aTitle , /* "" */ - char const * const aDefaultPath ) ; /* "" */ +char const * tinyfd_selectFolderDialog( + char const * const aTitle , /* NULL or "" */ + char const * const aDefaultPath ) ; /* NULL or "" */ /* returns NULL on cancel */ char const * tinyfd_colorChooser( - char const * const aTitle , /* "" */ + 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 } */ @@ -187,12 +197,18 @@ char const * tinyfd_colorChooser( #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" */ + /* windows only - utf-16 version */ int tinyfd_messageBoxW( wchar_t const * const aTitle , /* NULL or L"" */ - wchar_t const * const aMessage, /* L"" may contain \n \t */ - wchar_t const * const aDialogType, /* "ok" "okcancel" "yesno" */ - wchar_t const * const aIconType, /* "info" "warning" "error" "question" */ + 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 */ /* returns 0 for cancel/no , 1 for ok/yes */ @@ -213,8 +229,8 @@ wchar_t const * tinyfd_saveFileDialogW( /* windows only - utf-16 version */ wchar_t const * tinyfd_openFileDialogW( - wchar_t const * const aTitle, /* L"" */ - wchar_t const * const aDefaultPathAndFile, /* L"" */ + 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" */ @@ -224,13 +240,13 @@ wchar_t const * tinyfd_openFileDialogW( /* windows only - utf-16 version */ wchar_t const * tinyfd_selectFolderDialogW( - wchar_t const * const aTitle, /* L"" */ - wchar_t const * const aDefaultPath); /* L"" */ + wchar_t const * const aTitle, /* NULL or L"" */ + wchar_t const * const aDefaultPath); /* NULL or L"" */ /* returns NULL on cancel */ /* windows only - utf-16 version */ wchar_t const * tinyfd_colorChooserW( - wchar_t const * const aTitle, /* L"" */ + 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 } */ @@ -246,10 +262,10 @@ wchar_t const * tinyfd_colorChooserW( /* unix zenity only */ char const * tinyfd_arrayDialog( - char const * const aTitle , /* "" */ + char const * const aTitle , /* NULL or "" */ int const aNumOfColumns , /* 2 */ char const * const * const aColumns, /* {"Column 1","Column 2"} */ - int const aNumOfRows, /* 2*/ + int const aNumOfRows, /* 2 */ char const * const * const aCells); /* {"Row1 Col1","Row1 Col2","Row2 Col1","Row2 Col2"} */ @@ -267,7 +283,7 @@ char const * tinyfd_arrayDialog( - 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 -- OSX supported from 10.4 to 10.11 (maybe even older versions) +- OSX supported from 10.4 to latest (maybe even older versions) - Avoid using " and ' in titles and messages. - There's one file filter only, it may contain several patterns. - If no filter description is provided, @@ -277,8 +293,10 @@ char const * tinyfd_arrayDialog( - 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 unix you need applescript, zenity, matedialog, qarma, kdialog, Xdialog, - python2/tkinter or dialog (will open a terminal if running without console). +- On unix you need one of the following: + applescript, kdialog, zenity, matedialog, shellementary, qarma, + python (2 or 3)/tkinter/python-dbus (optional), Xdialog + or dialog (opens terminal if running without console) or xterm. - 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. @@ -295,7 +313,8 @@ char const * tinyfd_arrayDialog( - Mutiple selects are not allowed in console mode. - The package dialog must be installed to run in enhanced console mode. It is already installed on most unix systems. -- On osx, the package dialog can be installed via http://macports.org +- On osx, the package dialog can be installed via + http://macappstore.org/dialog or http://macports.org - On windows, for enhanced console mode, dialog.exe should be copied somewhere on your executable path. It can be found at the bottom of the following page: From 7bec44404ebeda5837efd51d1f880cb1af5a35f5 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Wed, 21 Mar 2018 09:49:06 -0400 Subject: [PATCH 15/41] Remove non-MSVC appveyor targets. --- appveyor.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 1071e6f..fa3bc66 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -2,8 +2,6 @@ environment: matrix: - TARGET: x86_64-pc-windows-msvc - TARGET: i686-pc-windows-msvc - - TARGET: x86_64-pc-windows-gnu - - TARGET: i686-pc-windows-gnu platform: - x64 From de1d1d339dd46866616176bffa5e1d90f85ce039 Mon Sep 17 00:00:00 2001 From: Bastien Orivel Date: Wed, 21 Mar 2018 00:31:37 +0100 Subject: [PATCH 16/41] Replace the gcc crate by cc and bump version --- Cargo.toml | 4 ++-- build.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d3a813c..99ae5d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tinyfiledialogs" description = "High-level Rust binding for the tinyfiledialogs C library." -version = "3.3.4" +version = "3.3.5" authors = ["Guillaume Vareille ", "Josh Matthews "] license = "zlib-acknowledgement" readme = "README.md" @@ -17,4 +17,4 @@ name = "tinyfiledialogs" libc = "0.2.0" [build-dependencies] -gcc = "0.3" +cc = "1" diff --git a/build.rs b/build.rs index 93b6383..e1e7476 100644 --- a/build.rs +++ b/build.rs @@ -2,17 +2,17 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -extern crate gcc; +extern crate cc; use std::env; fn main() { let target = env::var("TARGET").unwrap(); - let mut cfg = gcc::Config::new(); - cfg.file("libtinyfiledialogs/tinyfiledialogs.c"); - cfg.flag("-v"); - cfg.compile("libtinyfiledialogs.a"); + cc::Build::new() + .file("libtinyfiledialogs/tinyfiledialogs.c") + .flag("-v") + .compile("libtinyfiledialogs.a"); if target.contains("windows") { println!("cargo:rustc-link-lib=user32"); From 228de362f26dd1ac8495b537bf893da2381187d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szab=C3=B3=20Attila?= Date: Mon, 15 Oct 2018 16:14:11 +0200 Subject: [PATCH 17/41] Switched to UTF-8 encoding on Windows --- libtinyfiledialogs/tinyfiledialogs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index 7e6a3b5..3bc9f8a 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -117,7 +117,7 @@ misrepresented as being the original software. #include #define TINYFD_NOCCSUNICODE #define SLASH "\\" - int tinyfd_winUtf8 = 0 ; /* on windows string char can be 0:MBCS or 1:UTF-8 */ + int tinyfd_winUtf8 = 1 ; /* on windows string char can be 0:MBCS or 1:UTF-8 */ #else #include #include From 75764b20d814ac9e76718866ababd79cf3b2b109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szab=C3=B3=20Attila?= Date: Tue, 16 Oct 2018 05:34:57 +0200 Subject: [PATCH 18/41] Better native upstream pull --- patches/0001_WinUtf8.patch | 13 +++++++++++++ update.sh | 16 ++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 patches/0001_WinUtf8.patch diff --git a/patches/0001_WinUtf8.patch b/patches/0001_WinUtf8.patch new file mode 100644 index 0000000..004f262 --- /dev/null +++ b/patches/0001_WinUtf8.patch @@ -0,0 +1,13 @@ +diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c +index 7e6a3b5..3bc9f8a 100644 +--- a/libtinyfiledialogs/tinyfiledialogs.c ++++ b/libtinyfiledialogs/tinyfiledialogs.c +@@ -117,7 +117,7 @@ misrepresented as being the original software. + #include + #define TINYFD_NOCCSUNICODE + #define SLASH "\\" +- int tinyfd_winUtf8 = 0 ; /* on windows string char can be 0:MBCS or 1:UTF-8 */ ++ int tinyfd_winUtf8 = 1 ; /* on windows string char can be 0:MBCS or 1:UTF-8 */ + #else + #include + #include diff --git a/update.sh b/update.sh index e5379c0..3949429 100755 --- a/update.sh +++ b/update.sh @@ -1,4 +1,12 @@ -wget https://sourceforge.net/projects/tinyfiledialogs/files/tinyfiledialogs.h/download -O tinyfiledialogs.h -tr -d '\r' < tinyfiledialogs.h > libtinyfiledialogs/tinyfiledialogs.h -wget https://sourceforge.net/projects/tinyfiledialogs/files/tinyfiledialogs.c/download -O tinyfiledialogs.c -tr -d '\r' < tinyfiledialogs.c > libtinyfiledialogs/tinyfiledialogs.c +#!/bin/sh +set -eux + +for ext in h c +do + wget "https://sourceforge.net/projects/tinyfiledialogs/files/tinyfiledialogs.${ext}/download" -O - | tr -d '\r' > libtinyfiledialogs/tinyfiledialogs.${ext} +done + +for p in $(ls patches -v) +do + git apply patches/${p} +done From cb1b1ac4771aa6376479d1a0d4d086ca58060473 Mon Sep 17 00:00:00 2001 From: nabijaczleweli Date: Wed, 31 Jul 2019 14:57:37 +0200 Subject: [PATCH 19/41] Also link to shell32 on Windows --- build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/build.rs b/build.rs index e1e7476..8300e27 100644 --- a/build.rs +++ b/build.rs @@ -18,5 +18,6 @@ fn main() { println!("cargo:rustc-link-lib=user32"); println!("cargo:rustc-link-lib=comdlg32"); println!("cargo:rustc-link-lib=ole32"); + println!("cargo:rustc-link-lib=shell32"); } } From 0ce1a63b7a27a4c3b9a276a3b757cf4d959d68a2 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Thu, 1 Aug 2019 04:41:34 -0700 Subject: [PATCH 20/41] Minor version bump. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 99ae5d7..4ee087c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tinyfiledialogs" description = "High-level Rust binding for the tinyfiledialogs C library." -version = "3.3.5" +version = "3.3.6" authors = ["Guillaume Vareille ", "Josh Matthews "] license = "zlib-acknowledgement" readme = "README.md" From d31ce3a9831e63f9735db1f4cc9a2cbcd6e7adfb Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 7 Sep 2019 22:23:11 +0900 Subject: [PATCH 21/41] Update to 3.3.9 --- Cargo.toml | 2 +- libtinyfiledialogs/tinyfiledialogs.c | 245 +++++++++++++++++++-------- libtinyfiledialogs/tinyfiledialogs.h | 20 +-- 3 files changed, 181 insertions(+), 86 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4ee087c..0e397d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tinyfiledialogs" description = "High-level Rust binding for the tinyfiledialogs C library." -version = "3.3.6" +version = "3.3.9" authors = ["Guillaume Vareille ", "Josh Matthews "] license = "zlib-acknowledgement" readme = "README.md" diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index 3bc9f8a..700cce6 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -1,5 +1,5 @@ /*_________ - / \ tinyfiledialogs.c v3.3.4 [Mar 15, 2018] zlib licence + / \ tinyfiledialogs.c v3.3.9 [Apr 14, 2019] zlib licence |tiny file| Unique code file created [November 9, 2014] | dialogs | Copyright (c) 2014 - 2018 Guillaume Vareille http://ysengrin.com \____ ___/ http://tinyfiledialogs.sourceforge.net @@ -13,9 +13,7 @@ | the windows only wchar_t UTF-16 prototypes are in the header file | |___________________________________________________________________| -Please 1) upvote my stackoverflow answer/advert https://stackoverflow.com/a/47651444 - 2) leave a one word review on Sourceforge. - 3) let me know If you are using it on exotic hardware/OS/compiler +Please upvote my stackoverflow answer https://stackoverflow.com/a/47651444 tiny file dialogs (cross-platform C C++) InputBox PasswordBox MessageBox ColorPicker @@ -23,9 +21,9 @@ 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 -a C file and a header (add them to your C or C++ project) with 8 functions: +one C file + a header (add them to your C or C++ project) with 8 functions: - beep -- notify popup +- notify popup (tray) - message & question - input & password - save file @@ -35,7 +33,7 @@ a C file and a header (add them to your C or C++ project) with 8 functions: Complements OpenGL Vulkan GLFW GLUT GLUI VTK SFML TGUI SDL Ogre Unity3d ION OpenCV CEGUI MathGL GLM CPW GLOW -IMGUI MyGUI GLT NGL STB & GUI less programs +Open3D IMGUI MyGUI GLT NGL STB & GUI less programs NO INIT NO MAIN LOOP @@ -58,9 +56,9 @@ Unix (command line calls) ASCII UTF-8 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 +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 Unity Lxde Lxqt Xfce +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 @@ -89,7 +87,10 @@ misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ +#ifndef __sun #define _POSIX_C_SOURCE 2 /* to accept POSIX 2 in old ANSI C standards */ +#endif + #include #include #include @@ -107,10 +108,10 @@ misrepresented as being the original software. #define _WIN32_WINNT 0x0500 #endif #ifndef TINYFD_NOLIB - #include + #include /*#define TINYFD_NOSELECTFOLDERWIN*/ #ifndef TINYFD_NOSELECTFOLDERWIN - #include + #include #endif /*TINYFD_NOSELECTFOLDERWIN*/ #endif #include @@ -131,9 +132,11 @@ misrepresented as being the original software. #define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */ #define MAX_MULTIPLE_FILES 32 -char const tinyfd_version [8] = "3.3.4"; +char const tinyfd_version [8] = "3.3.9"; 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 dialog*/ #if defined(TINYFD_NOLIB) && defined(_WIN32) int tinyfd_forceConsole = 1 ; @@ -729,9 +732,15 @@ static int dirExists(char const * const aDirPath) struct _stat lInfo; wchar_t * lTmpWChar; int lStatRet; + int lDirLen; - if (!aDirPath || !strlen(aDirPath)) - return 0; + if (!aDirPath) + return 0; + lDirLen = strlen(aDirPath); + if (!lDirLen) + return 1; + if ( (lDirLen == 2) && (aDirPath[1] == ':') ) + return 1; if (tinyfd_winUtf8) { @@ -790,6 +799,36 @@ static int fileExists(char const * const aFilePathAndName) } } +static int replaceWchar(wchar_t * const aString, + wchar_t const aOldChr, + wchar_t const aNewChr) +{ + wchar_t * p; + int lRes = 0; + + if (!aString) + { + return 0; + } + + if (aOldChr == aNewChr) + { + return 0; + } + + p = aString; + while ((p = wcsrchr(p, aOldChr))) + { + *p = aNewChr; +#ifdef TINYFD_NOCCSUNICODE + p++; +#endif + p++; + lRes = 1; + } + return lRes; +} + #endif /* TINYFD_NOLIB */ #endif /* _WIN32 */ @@ -1134,12 +1173,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"); @@ -1263,13 +1306,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"\"%USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.vbs\" "); + wcscat(lDialogString, L">> \"%USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.txt\""); } else { wcscpy(lDialogString, - L"cmd.exe /c mshta.exe %USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.hta"); + L"cmd.exe /c mshta.exe \"%USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.hta\""); } /* wprintf ( "lDialogString: %ls\n" , lDialogString ) ; */ @@ -1292,6 +1335,9 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ free(lDialogString); return NULL; } + + memset(lBuff, 0, MAX_PATH_OR_CMD); + #ifdef TINYFD_NOCCSUNICODE fgets((char *)lBuff, 2*MAX_PATH_OR_CMD, lIn); #else @@ -1335,9 +1381,17 @@ 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 } @@ -2230,7 +2284,8 @@ static char const * selectFolderDialogWinGuiA( { BROWSEINFOA bInfo ; LPITEMIDLIST lpItem ; - HRESULT lHResult; + HRESULT lHResult ; + char * lRetval = NULL ; lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); @@ -2251,13 +2306,14 @@ static char const * selectFolderDialogWinGuiA( if ( lpItem ) { SHGetPathFromIDListA( lpItem , aoBuff ) ; + lRetval = aoBuff; } if (lHResult==S_OK || lHResult==S_FALSE) { CoUninitialize(); } - return aoBuff ; + return lRetval; } #endif /*TINYFD_NOSELECTFOLDERWIN*/ @@ -3232,23 +3288,41 @@ static int detectPresence( char const * const aExecutable ) } -static char const * getVersion( char const * const aExecutable ) /*version # must follow :*/ +static char const * getVersion( char const * const aExecutable ) /*version must be first numeral*/ { - static char lBuff [MAX_PATH_OR_CMD] ; - char lTestedString [MAX_PATH_OR_CMD] ; - FILE * lIn ; - char * lTmp ; - + 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 ) ; - if ( ! lTmp || !(lTmp = strchr( lBuff , ':' )) ) return 0 ; - lTmp ++ ; - /* printf("lTmp %s\n", lTmp); */ - return lTmp ; + + lTmp += strcspn(lTmp,"0123456789"); + /* printf("lTmp:%s\n", lTmp); */ + return lTmp ; +} + + +static int * const getMajorMinorPatch( char const * const aExecutable ) +{ + static int lArray [3] ; + char * lTmp ; + + lTmp = (char *) getVersion(aExecutable); + lArray[0] = atoi( strtok(lTmp," ,.-") ) ; + /* printf("lArray0 %d\n", lArray[0]); */ + lArray[1] = atoi( strtok(0," ,.-") ) ; + /* printf("lArray1 %d\n", lArray[1]); */ + lArray[2] = atoi( strtok(0," ,.-") ) ; + /* printf("lArray2 %d\n", lArray[2]); */ + + if ( !lArray[0] && !lArray[1] && !lArray[2] ) return NULL; + return lArray ; } @@ -3356,6 +3430,7 @@ static char const * terminalName( ) { static char lTerminalName[128] = "*" ; char lShellName[64] = "*" ; + int * lArray; if ( lTerminalName[0] == '*' ) { @@ -3452,8 +3527,9 @@ static char const * terminalName( ) strcat(lTerminalName , " -e " ) ; strcat(lTerminalName , lShellName ) ; } - else if ( strcpy(lTerminalName,"gnome-terminal") /*bad (good if version <3 or >=3.18)*/ - && detectPresence(lTerminalName) ) + else if ( strcpy(lTerminalName,"gnome-terminal") + && detectPresence(lTerminalName) && (lArray = getMajorMinorPatch(lTerminalName)) + && ((lArray[0]<3) || (lArray[0]==3 && lArray[1]<=6)) ) { strcat(lTerminalName , " --disable-factory -x " ) ; strcat(lTerminalName , lShellName ) ; @@ -3675,48 +3751,6 @@ static int osascriptPresent( ) } -static int kdialogPresent( ) -{ - static int lKdialogPresent = -1 ; - char lBuff [MAX_PATH_OR_CMD] ; - FILE * lIn ; - - if ( lKdialogPresent < 0 ) - { - lKdialogPresent = detectPresence("kdialog") ; - if ( lKdialogPresent && !getenv("SSH_TTY") ) - { - lIn = popen( "kdialog --attach 2>&1" , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - { - if ( ! strstr( "Unknown" , lBuff ) ) - { - lKdialogPresent = 2 ; - if (tinyfd_verbose) printf("kdialog-attach %d\n", lKdialogPresent); - } - } - pclose( lIn ) ; - - if (lKdialogPresent == 2) - { - lKdialogPresent = 1 ; - lIn = popen( "kdialog --passivepopup 2>&1" , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - { - if ( ! strstr( "Unknown" , lBuff ) ) - { - lKdialogPresent = 2 ; - if (tinyfd_verbose) printf("kdialog-popup %d\n", lKdialogPresent); - } - } - pclose( lIn ) ; - } - } - } - return graphicMode() ? lKdialogPresent : 0 ; -} - - static int qarmaPresent( ) { static int lQarmaPresent = -1 ; @@ -3802,6 +3836,59 @@ static int zenity3Present() } +static int kdialogPresent( ) +{ + static int lKdialogPresent = -1 ; + char lBuff [MAX_PATH_OR_CMD] ; + FILE * lIn ; + char * lDesktop; + + if ( lKdialogPresent < 0 ) + { + if ( zenityPresent() ) + { + lDesktop = getenv("XDG_SESSION_DESKTOP"); + if ( !lDesktop || ( strcmp(lDesktop, "KDE") && strcmp(lDesktop, "lxqt") ) ) + { + lKdialogPresent = 0 ; + return lKdialogPresent ; + } + } + + lKdialogPresent = detectPresence("kdialog") ; + if ( lKdialogPresent && !getenv("SSH_TTY") ) + { + lIn = popen( "kdialog --attach 2>&1" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + if ( ! strstr( "Unknown" , lBuff ) ) + { + lKdialogPresent = 2 ; + if (tinyfd_verbose) printf("kdialog-attach %d\n", lKdialogPresent); + } + } + pclose( lIn ) ; + + if (lKdialogPresent == 2) + { + lKdialogPresent = 1 ; + lIn = popen( "kdialog --passivepopup 2>&1" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + if ( ! strstr( "Unknown" , lBuff ) ) + { + lKdialogPresent = 2 ; + if (tinyfd_verbose) printf("kdialog-popup %d\n", lKdialogPresent); + } + } + pclose( lIn ) ; + } + } + } + return graphicMode() ? lKdialogPresent : 0 ; +} + + static int osx9orBetter( ) { static int lOsx9orBetter = -1 ; @@ -4293,6 +4380,8 @@ int tinyfd_messageBox( } } + if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null "); + if ( ! strcmp( "yesnocancel" , aDialogType ) ) { strcat( lDialogString , @@ -5309,6 +5398,7 @@ char const * tinyfd_inputBox( { 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"); } @@ -5859,6 +5949,7 @@ char const * tinyfd_saveFileDialog( } strcat( lDialogString , "' --file-filter='All files | *'" ) ; } + if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null "); } else if ( !xdialogPresent() && tkinter2Present( ) ) { @@ -6300,6 +6391,7 @@ char const * tinyfd_openFileDialog( } strcat( lDialogString , "' --file-filter='All files | *'" ) ; } + if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null "); } else if ( tkinter2Present( ) ) { @@ -6673,6 +6765,7 @@ char const * tinyfd_selectFolderDialog( strcat(lDialogString, aDefaultPath) ; strcat(lDialogString, "\"") ; } + if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null "); } else if ( !xdialogPresent() && tkinter2Present( ) ) { @@ -6965,6 +7058,7 @@ to set mycolor to choose color default color {"); strcat(lDialogString, aTitle) ; strcat(lDialogString, "\"") ; } + if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null "); } else if ( xdialogPresent() ) { @@ -7236,6 +7330,7 @@ char lString[1024]; char const * lFilterPatterns[2] = { "*.txt", "*.text" }; tinyfd_verbose = argc - 1; +tinyfd_silent = 1; lWillBeGraphicMode = tinyfd_inputBox("tinyfd_query", NULL, NULL); diff --git a/libtinyfiledialogs/tinyfiledialogs.h b/libtinyfiledialogs/tinyfiledialogs.h index 76e6c82..187656a 100644 --- a/libtinyfiledialogs/tinyfiledialogs.h +++ b/libtinyfiledialogs/tinyfiledialogs.h @@ -1,5 +1,5 @@ /*_________ - / \ tinyfiledialogs.h v3.3.4 [Mar 15, 2018] zlib licence + / \ tinyfiledialogs.h v3.3.9 [Apr 14, 2019] zlib licence |tiny file| Unique header file created [November 9, 2014] | dialogs | Copyright (c) 2014 - 2018 Guillaume Vareille http://ysengrin.com \____ ___/ http://tinyfiledialogs.sourceforge.net @@ -13,9 +13,7 @@ | the windows only wchar_t UTF-16 prototypes are at the end of this file | |________________________________________________________________________| -Please 1) upvote my stackoverflow answer/advert https://stackoverflow.com/a/47651444 - 2) leave a one word review on Sourceforge. - 3) let me know If you are using it on exotic hardware/OS/compiler +Please upvote my stackoverflow answer https://stackoverflow.com/a/47651444 tiny file dialogs (cross-platform C C++) InputBox PasswordBox MessageBox ColorPicker @@ -23,9 +21,9 @@ 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 -a C file and a header (add them to your C or C++ project) with 8 functions: +one C file + a header (add them to your C or C++ project) with 8 functions: - beep -- notify popup +- notify popup (tray) - message & question - input & password - save file @@ -35,7 +33,7 @@ a C file and a header (add them to your C or C++ project) with 8 functions: Complements OpenGL Vulkan GLFW GLUT GLUI VTK SFML TGUI SDL Ogre Unity3d ION OpenCV CEGUI MathGL GLM CPW GLOW -IMGUI MyGUI GLT NGL STB & GUI less programs +Open3D IMGUI MyGUI GLT NGL STB & GUI less programs NO INIT NO MAIN LOOP @@ -58,9 +56,9 @@ Unix (command line calls) ASCII UTF-8 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 +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 Unity Lxde Lxqt Xfce +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 @@ -106,6 +104,8 @@ extern "C" { 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*/ #ifdef _WIN32 /* for UTF-16 use the functions at the end of this files */ @@ -170,7 +170,7 @@ 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 * const aFilterPatterns , /* NULL | {"*.jpg","*.png"} */ char const * const aSingleFilterDescription , /* NULL | "image files" */ int const aAllowMultipleSelects ) ; /* 0 or 1 */ /* in case of multiple files, the separator is | */ From f362f1a4511226980b5cdf66e7add5d024eca52b Mon Sep 17 00:00:00 2001 From: Srinivasa Date: Tue, 21 Jul 2020 19:53:25 +0530 Subject: [PATCH 22/41] Fix color picker --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ea1aff7..c3a25be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -308,7 +308,7 @@ pub fn color_chooser_dialog(title: &str, default: DefaultColorValue) let result = unsafe { tinyfd_colorChooser(color_title.as_ptr(), - color_default_hex.map_or(ptr::null(), |h| h.as_ptr()), + color_default_hex.as_ref().map_or(ptr::null(), |h| h.as_ptr()), color_default_rgb.as_ptr(), color_result_rgb.as_mut_ptr()) }; From 09303f1ef51af83d5222e6dd5af90e2101288d49 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Tue, 21 Jul 2020 10:31:32 -0400 Subject: [PATCH 23/41] Bump version to 3.3.10. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0e397d6..0ac40f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tinyfiledialogs" description = "High-level Rust binding for the tinyfiledialogs C library." -version = "3.3.9" +version = "3.3.10" authors = ["Guillaume Vareille ", "Josh Matthews "] license = "zlib-acknowledgement" readme = "README.md" From 2e3816a3f943135b6dd44ccf4889362279b85f9e Mon Sep 17 00:00:00 2001 From: Srinivasa Date: Tue, 28 Jul 2020 11:07:07 +0530 Subject: [PATCH 24/41] Enabled visual style and fixed hidpi --- build.rs | 1 + libtinyfiledialogs/tinyfiledialogs.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/build.rs b/build.rs index 8300e27..fc57363 100644 --- a/build.rs +++ b/build.rs @@ -19,5 +19,6 @@ fn main() { println!("cargo:rustc-link-lib=comdlg32"); println!("cargo:rustc-link-lib=ole32"); println!("cargo:rustc-link-lib=shell32"); + println!("cargo:rustc-link-lib=shcore"); } } diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index 700cce6..0abf1ff 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -209,6 +209,14 @@ char const tinyfd_needs[] = "\ #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ #endif +#ifdef _WIN32 +#include + +#pragma comment(linker,"\"/manifestdependency:type='win32' \ +name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ +processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") +#endif // _WIN32 + 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 */ @@ -933,6 +941,7 @@ int tinyfd_messageBoxW( 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 */ { + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); int lBoxReturnValue; UINT aCode; @@ -1001,6 +1010,7 @@ static int messageBoxWinGui8( char const * const aIconType, /* "info" "warning" "error" "question" */ int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); int lIntRetVal; wchar_t * lTitle; wchar_t * lMessage; @@ -1030,6 +1040,7 @@ int tinyfd_notifyPopupW( wchar_t const * const aMessage, /* NULL or L"" may contain \n \t */ wchar_t const * const aIconType) /* L"info" L"warning" L"error" */ { + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); wchar_t * lDialogString; size_t lTitleLen; size_t lMessageLen; @@ -1098,6 +1109,7 @@ static int notifyWinGui( char const * const aMessage, /* NULL or "" may NOT contain \n nor \t */ char const * const aIconType) { + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); wchar_t * lTitle; wchar_t * lMessage; wchar_t * lIconType; @@ -1129,6 +1141,7 @@ wchar_t const * tinyfd_inputBoxW( 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 */ { + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); static wchar_t lBuff[MAX_PATH_OR_CMD]; wchar_t * lDialogString; FILE * lIn; @@ -1196,6 +1209,7 @@ wchar_t const * tinyfd_inputBoxW( wcscat(lDialogString, L"tinyfiledialogsTopWindow"); wcscat(lDialogString, L"\n\ +\n\ Date: Tue, 28 Jul 2020 11:50:16 +0530 Subject: [PATCH 25/41] Fix build --- libtinyfiledialogs/tinyfiledialogs.c | 76 +++++++++++++++++++++------- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index 0abf1ff..14619c7 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -209,13 +209,13 @@ char const tinyfd_needs[] = "\ #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ #endif -#ifdef _WIN32 +#ifdef _WIN64 #include #pragma comment(linker,"\"/manifestdependency:type='win32' \ name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") -#endif // _WIN32 +#endif // _WIN64 static char * getPathWithoutFinalSlash( char * const aoDestination, /* make sure it is allocated, use _MAX_PATH */ @@ -941,7 +941,9 @@ int tinyfd_messageBoxW( 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 */ { +#ifdef _WIN64 SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif int lBoxReturnValue; UINT aCode; @@ -1010,7 +1012,9 @@ static int messageBoxWinGui8( char const * const aIconType, /* "info" "warning" "error" "question" */ int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif int lIntRetVal; wchar_t * lTitle; wchar_t * lMessage; @@ -1040,7 +1044,9 @@ int tinyfd_notifyPopupW( wchar_t const * const aMessage, /* NULL or L"" may contain \n \t */ wchar_t const * const aIconType) /* L"info" L"warning" L"error" */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif wchar_t * lDialogString; size_t lTitleLen; size_t lMessageLen; @@ -1109,7 +1115,9 @@ static int notifyWinGui( char const * const aMessage, /* NULL or "" may NOT contain \n nor \t */ char const * const aIconType) { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif wchar_t * lTitle; wchar_t * lMessage; wchar_t * lIconType; @@ -1141,7 +1149,9 @@ wchar_t const * tinyfd_inputBoxW( 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 */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif static wchar_t lBuff[MAX_PATH_OR_CMD]; wchar_t * lDialogString; FILE * lIn; @@ -1417,7 +1427,9 @@ static char const * inputBoxWinGui( char const * const aMessage, /* NULL or "" may NOT contain \n nor \t */ char const * const aDefaultInput) /* "" , if NULL it's a passwordBox */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif wchar_t * lTitle; wchar_t * lMessage; wchar_t * lDefaultInput; @@ -1470,7 +1482,9 @@ wchar_t const * tinyfd_saveFileDialogW( wchar_t const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ wchar_t const * const aSingleFilterDescription) /* NULL or "image files" */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif static wchar_t lBuff[MAX_PATH_OR_CMD]; wchar_t lDirname[MAX_PATH_OR_CMD]; wchar_t lDialogString[MAX_PATH_OR_CMD]; @@ -1563,7 +1577,9 @@ static char const * saveFileDialogWinGui8( char const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ char const * const aSingleFilterDescription) /* NULL or "image files" */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif wchar_t * lTitle; wchar_t * lDefaultPathAndFile; wchar_t * lSingleFilterDescription; @@ -1620,7 +1636,9 @@ wchar_t const * tinyfd_openFileDialogW( wchar_t const * const aSingleFilterDescription, /* NULL or "image files" */ int const aAllowMultipleSelects) /* 0 or 1 */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif static wchar_t lBuff[MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD]; size_t lLengths[MAX_MULTIPLE_FILES]; @@ -1752,7 +1770,9 @@ static char const * openFileDialogWinGui8( char const * const aSingleFilterDescription, /* NULL or "image files" */ int const aAllowMultipleSelects) /* 0 or 1 */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif wchar_t * lTitle; wchar_t * lDefaultPathAndFile; wchar_t * lSingleFilterDescription; @@ -1824,7 +1844,9 @@ wchar_t const * tinyfd_selectFolderDialogW( wchar_t const * const aTitle, /* NULL or "" */ wchar_t const * const aDefaultPath) /* NULL or "" */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif static wchar_t lBuff[MAX_PATH_OR_CMD]; BROWSEINFOW bInfo; @@ -1866,7 +1888,9 @@ static char const * selectFolderDialogWinGui8( char const * const aTitle , /* NULL or "" */ char const * const aDefaultPath ) /* NULL or "" */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif wchar_t * lTitle; wchar_t * lDefaultPath; wchar_t const * lTmpWChar; @@ -1901,7 +1925,9 @@ wchar_t const * tinyfd_colorChooserW( unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ { +#ifdef _WIN64 SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif static wchar_t lResultHexRGB[8]; CHOOSECOLORW cc; COLORREF crCustColors[16]; @@ -1964,7 +1990,9 @@ static char const * colorChooserWinGui8( unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif static char lResultHexRGB[8]; wchar_t * lTitle; @@ -2003,7 +2031,9 @@ static int messageBoxWinGuiA( char const * const aIconType , /* "info" "warning" "error" "question" */ int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif int lBoxReturnValue; UINT aCode ; @@ -2091,7 +2121,9 @@ static char const * saveFileDialogWinGuiA( char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ char const * const aSingleFilterDescription ) /* NULL or "image files" */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif char lDirname [MAX_PATH_OR_CMD] ; char lDialogString[MAX_PATH_OR_CMD]; char lFilterPatterns[MAX_PATH_OR_CMD] = ""; @@ -2182,7 +2214,9 @@ static char const * openFileDialogWinGuiA( char const * const aSingleFilterDescription , /* NULL or "image files" */ int const aAllowMultipleSelects ) /* 0 or 1 */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif char lDirname [MAX_PATH_OR_CMD] ; char lFilterPatterns[MAX_PATH_OR_CMD] = ""; char lDialogString[MAX_PATH_OR_CMD] ; @@ -2308,7 +2342,9 @@ static char const * selectFolderDialogWinGuiA( char const * const aTitle , /* NULL or "" */ char const * const aDefaultPath ) /* NULL or "" */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif BROWSEINFOA bInfo ; LPITEMIDLIST lpItem ; HRESULT lHResult ; @@ -2351,7 +2387,9 @@ static char const * colorChooserWinGuiA( unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ { - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif static char lResultHexRGB[8]; CHOOSECOLORA cc; From a904abeb19eb9ed3809136bdb4c4bd506415a94a Mon Sep 17 00:00:00 2001 From: Srinivasa Date: Tue, 28 Jul 2020 19:24:06 +0530 Subject: [PATCH 26/41] Added 0002_WindowsHidpiVisualStyle.patch --- patches/0002_WindowsHidpiVisualStyle.patch | 217 +++++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 patches/0002_WindowsHidpiVisualStyle.patch diff --git a/patches/0002_WindowsHidpiVisualStyle.patch b/patches/0002_WindowsHidpiVisualStyle.patch new file mode 100644 index 0000000..46369f1 --- /dev/null +++ b/patches/0002_WindowsHidpiVisualStyle.patch @@ -0,0 +1,217 @@ +diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c +index 700cce6..14619c7 100644 +--- a/libtinyfiledialogs/tinyfiledialogs.c ++++ b/libtinyfiledialogs/tinyfiledialogs.c +@@ -209,6 +209,14 @@ char const tinyfd_needs[] = "\ + #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ + #endif + ++#ifdef _WIN64 ++#include ++ ++#pragma comment(linker,"\"/manifestdependency:type='win32' \ ++name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ ++processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") ++#endif // _WIN64 ++ + 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 */ +@@ -933,6 +941,9 @@ int tinyfd_messageBoxW( + 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 */ + { ++#ifdef _WIN64 ++ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); ++#endif + int lBoxReturnValue; + UINT aCode; + +@@ -1001,6 +1012,9 @@ static int messageBoxWinGui8( + char const * const aIconType, /* "info" "warning" "error" "question" */ + int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ + { ++#ifdef _WIN64 ++ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); ++#endif + int lIntRetVal; + wchar_t * lTitle; + wchar_t * lMessage; +@@ -1030,6 +1044,9 @@ int tinyfd_notifyPopupW( + wchar_t const * const aMessage, /* NULL or L"" may contain \n \t */ + wchar_t const * const aIconType) /* L"info" L"warning" L"error" */ + { ++#ifdef _WIN64 ++ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); ++#endif + wchar_t * lDialogString; + size_t lTitleLen; + size_t lMessageLen; +@@ -1098,6 +1115,9 @@ static int notifyWinGui( + char const * const aMessage, /* NULL or "" may NOT contain \n nor \t */ + char const * const aIconType) + { ++#ifdef _WIN64 ++ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); ++#endif + wchar_t * lTitle; + wchar_t * lMessage; + wchar_t * lIconType; +@@ -1129,6 +1149,9 @@ wchar_t const * tinyfd_inputBoxW( + 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 */ + { ++#ifdef _WIN64 ++ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); ++#endif + static wchar_t lBuff[MAX_PATH_OR_CMD]; + wchar_t * lDialogString; + FILE * lIn; +@@ -1196,6 +1219,7 @@ wchar_t const * tinyfd_inputBoxW( + + wcscat(lDialogString, L"tinyfiledialogsTopWindow"); + wcscat(lDialogString, L"\n\ ++\n\ + Date: Fri, 4 Dec 2020 14:01:44 +0100 Subject: [PATCH 27/41] Update libtinyfiledialogs (closes #19) --- libtinyfiledialogs/tinyfiledialogs.c | 4922 +++++++++++++------------- libtinyfiledialogs/tinyfiledialogs.h | 372 +- 2 files changed, 2688 insertions(+), 2606 deletions(-) diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index 14619c7..abfe703 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -1,83 +1,34 @@ /*_________ - / \ tinyfiledialogs.c v3.3.9 [Apr 14, 2019] zlib licence + / \ tinyfiledialogs.c v3.8.3 [Nov 1, 2020] 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 - 2020 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 | - |___________________________________________________________________| - -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 - -Thanks for contributions, bug corrections & thorough testing to: -- Don Heyse http://ldglite.sf.net for bug corrections & thorough testing! -- Paul Rouget + ____________________________________________ + | | + | 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 | + |_________________________________________________________| + +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 @@ -85,11 +36,23 @@ 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__) ) + #define _GNU_SOURCE /* used only to resolve symbolic links. Can be commented out */ +#endif #include #include @@ -97,9 +60,6 @@ misrepresented as being the original software. #include #include -#include "tinyfiledialogs.h" -/* #define TINYFD_NOLIB */ - #ifdef _WIN32 #ifdef __BORLANDC__ #define _getch getch @@ -107,18 +67,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 = 1 ; /* on windows string char can be 0:MBCS or 1:UTF-8 */ #else #include #include @@ -129,25 +84,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.9"; +#ifndef MAX_MULTIPLE_FILES +#define MAX_MULTIPLE_FILES 1024 +#endif +#define LOW_MULTIPLE_FILES 32 + +char const tinyfd_version[8] = "3.8.3"; + +/******************************************************************************************************/ +/**************************************** 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 dialog*/ +int tinyfd_silent = 1 ; /* 1 (default) or 0 : on unix, hide errors and warnings from called dialogs */ -#if defined(TINYFD_NOLIB) && defined(_WIN32) -int tinyfd_forceConsole = 1 ; -#else +/* 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 */ -#endif /* 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, @@ -156,23 +132,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\ @@ -181,10 +151,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\ @@ -192,14 +162,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)\ +\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)\ -\nor xterm + bash\ -\n (opens console for basic input)\ +\nor xterm + bash (opens console for basic input)\ \nor existing console for basic input"; #endif @@ -209,17 +176,22 @@ char const tinyfd_needs[] = "\ #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ #endif -#ifdef _WIN64 -#include +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)); +} -#pragma comment(linker,"\"/manifestdependency:type='win32' \ -name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ -processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") -#endif // _WIN64 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 ) @@ -248,8 +220,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 ; @@ -277,7 +249,7 @@ static char * getLastName( } -static void ensureFinalSlash( char * const aioString ) +static void ensureFinalSlash( char * aioString ) { if ( aioString && strlen( aioString ) ) { @@ -290,10 +262,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 ) @@ -315,20 +286,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 { @@ -340,16 +309,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' ; @@ -376,7 +343,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) @@ -389,35 +356,13 @@ static int filenameValid( char const * const aFileNameWithoutPath ) #ifndef _WIN32 -static int fileExists( char const * const aFilePathAndName ) -{ - FILE * lIn ; - if ( ! aFilePathAndName || ! strlen(aFilePathAndName) ) - { - return 0 ; - } - lIn = fopen( aFilePathAndName , "r" ) ; - if ( ! lIn ) - { - return 0 ; - } - fclose( lIn ) ; - return 1 ; -} - -#elif defined(TINYFD_NOLIB) - -static int fileExists( char const * const aFilePathAndName ) +static int fileExists( char const * aFilePathAndName ) { FILE * lIn ; if ( ! aFilePathAndName || ! strlen(aFilePathAndName) ) { return 0 ; } - - if ( tinyfd_winUtf8 ) - return 1; /* we cannot test */ - lIn = fopen( aFilePathAndName , "r" ) ; if ( ! lIn ) { @@ -430,7 +375,7 @@ static int fileExists( char const * const aFilePathAndName ) #endif -static void wipefile(char const * const aFilename) +static void wipefile(char const * aFilename) { int i; struct stat st; @@ -444,74 +389,231 @@ 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; +} + -static int replaceChr( char * const aString , - char const aOldChr , - char const aNewChr ) +int tinyfd_setGlobalInt(char const * aIntVariableName, int aValue) /* to be called from C# (you don't need this in C or C++) */ { - char * p ; - int lRes = 0 ; + 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; +} - if ( ! aString ) - { - return 0 ; - } - if ( aOldChr == aNewChr ) - { - return 0 ; - } - p = aString ; - while ( (p = strchr( p , aOldChr )) ) - { - * p = aNewChr ; - p ++ ; - lRes = 1 ; - } - return lRes ; +#ifdef _WIN32 + +static void replaceChr(char * aString, char aOldChr, char aNewChr) +{ + char * p; + + if (!aString) return; + if (aOldChr == aNewChr) return; + + p = aString; + while ((p = strchr(p, aOldChr))) + { + *p = aNewChr; + p++; + } + return; } -#ifdef TINYFD_NOLIB -static int dirExists(char const * const aDirPath) +#if !defined(WC_ERR_INVALID_CHARS) +/* undefined prior to Vista, so not yet in MINGW header file */ +#define WC_ERR_INVALID_CHARS 0x00000080 +#endif + +static int sizeUtf16From8(char const * aUtf8string) { - struct stat lInfo; + return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, + aUtf8string, -1, NULL, 0); +} - 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; + +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_utf8to16(char const * aUtf8string) +{ + static wchar_t * lUtf16string = NULL; + int lSize; + + free(lUtf16string); + if (!aUtf8string) {lUtf16string = NULL; return NULL;} + lSize = sizeUtf16From8(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); + lUtf16string = NULL; + } + return lUtf16string; +} + + +char * tinyfd_utf16toMbcs(wchar_t const * aUtf16string) +{ + static char * lMbcsString = NULL; + int lSize; + + free(lMbcsString); + if (!aUtf16string) { lMbcsString = NULL; return NULL; } + lSize = sizeMbcs(aUtf16string); + lMbcsString = (char *)malloc(lSize); + lSize = WideCharToMultiByte(CP_ACP, 0, + aUtf16string, -1, lMbcsString, lSize, NULL, NULL); + if (lSize == 0) + { + free(lMbcsString); + lMbcsString = NULL; + } + return lMbcsString; +} + + +char * tinyfd_utf8toMbcs(char const * aUtf8string) +{ + wchar_t const * lUtf16string; + lUtf16string = tinyfd_utf8to16(aUtf8string); + return tinyfd_utf16toMbcs(lUtf16string); +} + +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); + lMbcsString = (wchar_t *)malloc(lSize * sizeof(wchar_t)); + lSize = MultiByteToWideChar(CP_ACP, 0, + aMbcsString, -1, lMbcsString, lSize); + if (lSize == 0) + { + free(lMbcsString); + lMbcsString = NULL; + } + return lMbcsString; +} + + +char * tinyfd_utf16to8(wchar_t const * aUtf16string) +{ + static char * lUtf8string = NULL; + int lSize; + + free(lUtf8string); + if (!aUtf16string) { lUtf8string = NULL; return NULL; } + 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); + lUtf8string = NULL; + } + return lUtf8string; } -void tinyfd_beep() +char * tinyfd_mbcsTo8(char const * aMbcsString) { - printf("\a"); + wchar_t const * lUtf16string; + lUtf16string = tinyfd_mbcsTo16(aMbcsString); + return tinyfd_utf16to8(lUtf16string); } -#else /* ndef TINYFD_NOLIB */ -void tinyfd_beep() +void tinyfd_beep(void) { Beep(440,300); } -static void wipefileW(wchar_t const * const aFilename) +static void wipefileW(wchar_t const * aFilename) { int i; struct _stat st; @@ -525,15 +627,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) @@ -562,8 +664,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; @@ -591,8 +693,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) @@ -617,20 +718,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 { @@ -642,105 +748,12 @@ 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 -#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) +static int dirExists(char const * aDirPath) { struct _stat lInfo; wchar_t * lTmpWChar; int lStatRet; - int lDirLen; + size_t lDirLen; if (!aDirPath) return 0; @@ -752,14 +765,13 @@ 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); + lStatRet = _wstat(lTmpWChar, &lInfo); + if (lStatRet != 0) + return 0; + else if (lInfo.st_mode & S_IFDIR) + return 1; + else return 0; } else if (_stat(aDirPath, &lInfo) != 0) @@ -771,7 +783,7 @@ static int dirExists(char const * const aDirPath) } -static int fileExists(char const * const aFilePathAndName) +static int fileExists(char const * aFilePathAndName) { struct _stat lInfo; wchar_t * lTmpWChar; @@ -785,15 +797,14 @@ 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); + lStatRet = _wstat(lTmpWChar, &lInfo); + if (lStatRet != 0) + return 0; + else if (lInfo.st_mode & _S_IFREG) + return 1; + else + return 0; } else { @@ -807,21 +818,20 @@ static int fileExists(char const * const aFilePathAndName) } } -static int replaceWchar(wchar_t * const aString, - wchar_t const aOldChr, - wchar_t const aNewChr) +static void replaceWchar(wchar_t * aString, + wchar_t aOldChr, + wchar_t aNewChr) { wchar_t * p; - int lRes = 0; if (!aString) { - return 0; + return ; } if (aOldChr == aNewChr) { - return 0; + return ; } p = aString; @@ -832,17 +842,37 @@ static int replaceWchar(wchar_t * const aString, p++; #endif p++; - lRes = 1; } - return lRes; + 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 /* TINYFD_NOLIB */ #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; @@ -887,10 +917,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); */ @@ -903,7 +932,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; @@ -935,20 +964,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 */ -{ -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif + 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; @@ -1005,48 +1034,12 @@ int tinyfd_messageBoxW( } -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 */ -{ -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - 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" */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif wchar_t * lDialogString; size_t lTitleLen; size_t lMessageLen; @@ -1054,10 +1047,14 @@ int tinyfd_notifyPopupW( 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 {\ @@ -1110,58 +1107,25 @@ 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 * 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 */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - wchar_t * lTitle; - wchar_t * lMessage; - wchar_t * lIconType; + static wchar_t lBuff[MAX_PATH_OR_CMD]; + wchar_t * lDialogString; + FILE * lIn; + FILE * lFile; + int lResult; + size_t lTitleLen; + size_t lMessageLen; + size_t lDialogStringLen; - 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 */ -{ -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - static wchar_t lBuff[MAX_PATH_OR_CMD]; - wchar_t * lDialogString; - FILE * lIn; - FILE * lFile; - int lResult; - size_t lTitleLen; - size_t lMessageLen; - size_t lDialogStringLen; + if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; } - if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t const *)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 ; @@ -1171,7 +1135,7 @@ 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")); @@ -1179,7 +1143,7 @@ wchar_t const * tinyfd_inputBoxW( 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")); @@ -1219,7 +1183,6 @@ wchar_t const * tinyfd_inputBoxW( wcscat(lDialogString, L"tinyfiledialogsTopWindow"); wcscat(lDialogString, L"\n\ -\n\
\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")); @@ -1344,7 +1307,7 @@ 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")); @@ -1360,7 +1323,7 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ return NULL; } - memset(lBuff, 0, MAX_PATH_OR_CMD); + memset(lBuff, 0, MAX_PATH_OR_CMD * sizeof(wchar_t) ); #ifdef TINYFD_NOCCSUNICODE fgets((char *)lBuff, 2*MAX_PATH_OR_CMD, lIn); @@ -1374,7 +1337,7 @@ 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", @@ -1383,7 +1346,7 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ 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", @@ -1397,7 +1360,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) { @@ -1409,7 +1372,7 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ #ifdef TINYFD_NOCCSUNICODE if (aDefaultInput) { - lDialogStringLen = wcslen(lBuff); + lDialogStringLen = wcslen(lBuff) ; lBuff[lDialogStringLen - 1] = L'\0'; lBuff[lDialogStringLen - 2] = L'\0'; } @@ -1421,81 +1384,33 @@ name = 'txt_input' value = '' style = 'float:left;width:100%' >
\n\ } -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 */ -{ -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - 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" */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif static wchar_t lBuff[MAX_PATH_OR_CMD]; wchar_t lDirname[MAX_PATH_OR_CMD]; wchar_t lDialogString[MAX_PATH_OR_CMD]; 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); @@ -1504,6 +1419,8 @@ wchar_t const * tinyfd_saveFileDialogW( if (aNumOfFilterPatterns > 0) { + ldefExt = aFilterPatterns[0]; + if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) { wcscpy(lFilterPatterns, aSingleFilterDescription); @@ -1533,7 +1450,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; @@ -1542,12 +1459,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; @@ -1569,90 +1486,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" */ -{ -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - 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 */ +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 */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - 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); @@ -1690,15 +1574,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; @@ -1715,145 +1599,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) { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - 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 "" */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif 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); @@ -1870,64 +1708,30 @@ 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; -} - - -static char const * selectFolderDialogWinGui8( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) /* NULL or "" */ -{ -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - 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; + return lRetval; } -#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 } */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif static wchar_t lResultHexRGB[8]; CHOOSECOLORW cc; COLORREF crCustColors[16]; @@ -1936,11 +1740,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); } @@ -1984,466 +1791,376 @@ wchar_t const * tinyfd_colorChooserW( } -static char const * colorChooserWinGui8( - 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 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 */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - static char lResultHexRGB[8]; + 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 = 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); + } - wchar_t * lTitle; - wchar_t * lDefaultHexRGB; - wchar_t const * lTmpWChar; - char * lTmpChar; + lIntRetVal = tinyfd_messageBoxW(lTitle, lMessage, lDialogType, lIconType, aDefaultButton); - lTitle = utf8to16(aTitle); - lDefaultHexRGB = utf8to16(aDefaultHexRGB); + free(lMessage); - lTmpWChar = tinyfd_colorChooserW( - lTitle, - lDefaultHexRGB, - aDefaultRGB, - aoResultRGB ); + return lIntRetVal; +} - free(lTitle); - free(lDefaultHexRGB); - if (!lTmpWChar) - { - return NULL; - } - lTmpChar = utf16to8(lTmpWChar); - strcpy(lResultHexRGB, lTmpChar); - free(lTmpChar); +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; - return lResultHexRGB; + 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 = 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 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 */ +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 */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - 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 ; - } + wchar_t lTitle[128] = L""; + wchar_t * lMessage = NULL; + wchar_t lDefaultInput[MAX_PATH_OR_CMD] = L""; + wchar_t * lTmpWChar; + char * lTmpChar; - 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 ; - } + 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 = 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); + } - aCode += MB_TOPMOST; + lTmpWChar = tinyfd_inputBoxW(lTitle, lMessage, lDefaultInput); - lBoxReturnValue = MessageBoxA(GetForegroundWindow(), aMessage, aTitle, aCode); + free(lMessage); - if (((aDialogType && !strcmp("yesnocancel", aDialogType)) - && (lBoxReturnValue == IDNO))) - { - return 2; - } + if (!lTmpWChar) + { + aoBuff[0] = '\0'; + return 0; + } - if ( ( ( aDialogType - && strcmp("yesnocancel", aDialogType) - && strcmp("okcancel", aDialogType) - && strcmp("yesno", aDialogType))) - || (lBoxReturnValue == IDOK) - || (lBoxReturnValue == IDYES) ) - { - return 1 ; - } - else - { - return 0 ; - } + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + + strcpy(aoBuff, lTmpChar); + + return 1; } -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" */ +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" */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - 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 }; + 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); + } - lHResult = CoInitializeEx(NULL,0); + 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); + } - 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; + lTmpWChar = tinyfd_saveFileDialogW( + lTitle, + lDefaultPathAndFile, + aNumOfFilterPatterns, + (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/ + lSingleFilterDescription); - 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 ; + for (i = 0; i < aNumOfFilterPatterns; i++) + { + free(lFilterPatterns[i]); + } + free(lFilterPatterns); - if ( GetSaveFileNameA ( & ofn ) == 0 ) - { - lRetval = NULL ; - } - else - { - lRetval = aoBuff ; - } + if (!lTmpWChar) + { + return NULL; + } - if (lHResult==S_OK || lHResult==S_FALSE) - { - CoUninitialize(); - } - return lRetval ; + 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 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 */ +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 */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - 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}; + 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); + } - lHResult = CoInitializeEx(NULL,0); + 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); + } - getPathWithoutFinalSlash(lDirname, aDefaultPathAndFile); - getLastName(aoBuff, aDefaultPathAndFile); + lTmpWChar = tinyfd_openFileDialogW( + lTitle, + lDefaultPathAndFile, + aNumOfFilterPatterns, + (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/ + lSingleFilterDescription, + aAllowMultipleSelects); - 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; - } + for (i = 0; i < aNumOfFilterPatterns; i++) + { + free(lFilterPatterns[i]); + } + free(lFilterPatterns); - 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 (!lTmpWChar) return NULL; - if (lHResult==S_OK || lHResult==S_FALSE) - { - CoUninitialize(); - } - return lRetval; + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + (void)tinyfd_openFileDialogW(NULL, NULL, 0, NULL, NULL, -1); + + return lTmpChar; } -#ifndef TINYFD_NOSELECTFOLDERWIN -static char const * selectFolderDialogWinGuiA( - char * const aoBuff , - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) /* NULL or "" */ + +static char * selectFolderDialogWinGui( + char * aoBuff, + char const * aTitle, /* NULL or "" */ + char const * aDefaultPath) /* NULL or "" */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif - BROWSEINFOA bInfo ; - LPITEMIDLIST lpItem ; - HRESULT lHResult ; - char * lRetval = NULL ; + wchar_t lTitle[128] = L""; + wchar_t lDefaultPath[MAX_PATH_OR_CMD] = L""; + wchar_t * lTmpWChar; + char * lTmpChar; - lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); + 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); + } - /* 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 ; + lTmpWChar = tinyfd_selectFolderDialogW( + lTitle, + lDefaultPath); - lpItem = SHBrowseForFolderA( & bInfo ) ; - if ( lpItem ) - { - SHGetPathFromIDListA( lpItem , aoBuff ) ; - lRetval = aoBuff; - } + if (!lTmpWChar) + { + return NULL; + } - if (lHResult==S_OK || lHResult==S_FALSE) - { - CoUninitialize(); - } - return lRetval; + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + strcpy(aoBuff, lTmpChar); + + return aoBuff; } -#endif /*TINYFD_NOSELECTFOLDERWIN*/ -static char const * colorChooserWinGuiA( - char const * const aTitle, /* NULL or "" */ - char const * const aDefaultHexRGB, /* NULL or "#FF0000"*/ +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 } */ { -#ifdef _WIN64 - SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -#endif 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]; - } + wchar_t lTitle[128]; + wchar_t lDefaultHexRGB[16]; + wchar_t * lTmpWChar; + char * lTmpChar; - /* 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; + 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); + } - lRet = ChooseColorA(&cc); + lTmpWChar = tinyfd_colorChooserW( + lTitle, + lDefaultHexRGB, + aDefaultRGB, + aoResultRGB ); - if ( ! lRet ) + if (!lTmpWChar) { return NULL; } - aoResultRGB[0] = GetRValue(cc.rgbResult); - aoResultRGB[1] = GetGValue(cc.rgbResult); - aoResultRGB[2] = GetBValue(cc.rgbResult); - - RGB2Hex(aoResultRGB, lResultHexRGB); + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + strcpy(lResultHexRGB, lTmpChar); 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"))) { @@ -2466,24 +2183,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) ; @@ -2531,7 +2248,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'; } @@ -2554,13 +2271,13 @@ static int messageBoxWinConsole( /*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); @@ -2585,11 +2302,11 @@ 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]; @@ -2653,7 +2370,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) {} @@ -2670,19 +2388,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] = ""; @@ -2695,7 +2415,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") ; @@ -2708,7 +2428,7 @@ static char const * saveFileDialogWinConsole( strcpy(lPathAndFile, aDefaultPathAndFile); replaceChr( lPathAndFile , '\\' , '/' ) ; } - + /* dialog.exe needs at least one separator */ if ( ! strchr(lPathAndFile, '/') ) { @@ -2743,16 +2463,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) ) { @@ -2773,7 +2493,7 @@ static char const * openFileDialogWinConsole( strcpy(lFilterPatterns, aDefaultPathAndFile); replaceChr( lFilterPatterns , '\\' , '/' ) ; } - + /* dialog.exe needs at least one separator */ if ( ! strchr(lFilterPatterns, '/') ) { @@ -2803,15 +2523,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) ) { @@ -2861,168 +2581,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() || + 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 ((!tinyfd_forceConsole || !( + GetConsoleWindow() || dialogPresent())) - && ( !getenv("SSH_CLIENT") || getenv("DISPLAY") ) ) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) { 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); - } + 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) { @@ -3030,87 +2791,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) ); - } -#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"); + (void) GetConsoleMode(hStdin, &mode); + (void) SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); } -#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 (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 (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,"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 ) ) { @@ -3131,47 +2943,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 */ -{ - 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") ) ) - { - 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 +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 */ +{ + 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,"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 ) ) { @@ -3179,7 +2996,7 @@ char const * tinyfd_openFileDialog( } if ( aAllowMultipleSelects && strchr(p, '|') ) { - p = ensureFilesExist( lBuff , p ) ; + p = ensureFilesExist( (char *) p , p ) ; } else if ( ! fileExists(p) ) { @@ -3190,41 +3007,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") ) ) - { - 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*/ - } - } - 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 + 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,"basicinput");return (char const *)0;} - p = tinyfd_inputBox(aTitle, "Select folder",""); + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;} + p = selectFolderDialogWinGui(lBuff, aTitle, aDefaultPath); } - + 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 ; @@ -3237,61 +3056,72 @@ 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] != '#') ) - { - return NULL ; - } - for ( i = 1 ; i < 7 ; i ++ ) - { - if ( ! isxdigit( p[i] ) ) - { - return NULL ; - } - } - Hex2RGB(p,aoResultRGB); - return p ; + 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); + strcpy(lDefaultHexRGB, p); + return lDefaultHexRGB; + } + 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; } @@ -3301,7 +3131,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 ; @@ -3313,7 +3143,7 @@ static int isDarwin( ) } -static int dirExists( char const * const aDirPath ) +static int dirExists( char const * aDirPath ) { DIR * lDir ; if ( ! aDirPath || ! strlen( aDirPath ) ) @@ -3321,62 +3151,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 ) + +static int detectPresence( char const * aExecutable ) { - char lBuff [MAX_PATH_OR_CMD] ; - char lTestedString [MAX_PATH_OR_CMD] = "which " ; - FILE * lIn ; + 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 ) ; - 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 ; - } + 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); @@ -3392,9 +3238,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" ) ; @@ -3412,10 +3258,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 ); @@ -3424,27 +3270,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 ; @@ -3481,9 +3331,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" ) ; @@ -3492,7 +3343,7 @@ static int whiptailPresentOnly( ) } -static char const * terminalName( ) +static char * terminalName(void) { static char lTerminalName[128] = "*" ; char lShellName[64] = "*" ; @@ -3514,9 +3365,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 " ) ; @@ -3527,73 +3378,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)) ) { @@ -3619,9 +3470,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() ) ) { @@ -3634,7 +3485,7 @@ static char const * dialogName( ) } -static int whiptailPresent( ) +static int whiptailPresent(void) { int lWhiptailPresent ; lWhiptailPresent = whiptailPresentOnly( ) ; @@ -3650,15 +3501,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 ) @@ -3669,7 +3520,7 @@ static int pactlPresent( ) } -static int speakertestPresent( ) +static int speakertestPresent(void) { static int lSpeakertestPresent = -1 ; if ( lSpeakertestPresent < 0 ) @@ -3680,18 +3531,40 @@ static int speakertestPresent( ) } -static int beepexePresent( ) +static int playPresent() { - static int lBeepexePresent = -1 ; - if ( lBeepexePresent < 0 ) + static int lPlayPresent = -1; + if (lPlayPresent < 0) + { + lPlayPresent = detectPresence("sox"); /*if sox is present, play is ready*/ + } + return lPlayPresent; +} + + +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 ) { - lBeepexePresent = detectPresence("beep.exe") ; + lBeepPresent = detectPresence("beep") ; } - return lBeepexePresent ; + return lBeepPresent ; } -static int xmessagePresent( ) +static int xmessagePresent(void) { static int lXmessagePresent = -1 ; if ( lXmessagePresent < 0 ) @@ -3702,7 +3575,7 @@ static int xmessagePresent( ) } -static int gxmessagePresent( ) +static int gxmessagePresent(void) { static int lGxmessagePresent = -1 ; if ( lGxmessagePresent < 0 ) @@ -3713,7 +3586,7 @@ static int gxmessagePresent( ) } -static int gmessagePresent( ) +static int gmessagePresent(void) { static int lGmessagePresent = -1 ; if ( lGmessagePresent < 0 ) @@ -3724,7 +3597,7 @@ static int gmessagePresent( ) } -static int notifysendPresent( ) +static int notifysendPresent(void) { static int lNotifysendPresent = -1 ; if ( lNotifysendPresent < 0 ) @@ -3735,10 +3608,10 @@ static int notifysendPresent( ) } -static int perlPresent( ) +static int perlPresent(void) { static int lPerlPresent = -1 ; - char lBuff [MAX_PATH_OR_CMD] ; + char lBuff[MAX_PATH_OR_CMD] ; FILE * lIn ; if ( lPerlPresent < 0 ) @@ -3759,10 +3632,10 @@ static int perlPresent( ) } -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 ) @@ -3783,7 +3656,7 @@ static int afplayPresent( ) } -static int xdialogPresent( ) +static int xdialogPresent(void) { static int lXdialogPresent = -1 ; if ( lXdialogPresent < 0 ) @@ -3794,7 +3667,7 @@ static int xdialogPresent( ) } -static int gdialogPresent( ) +static int gdialogPresent(void) { static int lGdialoglPresent = -1 ; if ( lGdialoglPresent < 0 ) @@ -3805,7 +3678,7 @@ static int gdialogPresent( ) } -static int osascriptPresent( ) +static int osascriptPresent(void) { static int lOsascriptPresent = -1 ; if ( lOsascriptPresent < 0 ) @@ -3817,7 +3690,7 @@ static int osascriptPresent( ) } -static int qarmaPresent( ) +int tfd_qarmaPresent(void) { static int lQarmaPresent = -1 ; if ( lQarmaPresent < 0 ) @@ -3828,7 +3701,7 @@ static int qarmaPresent( ) } -static int matedialogPresent( ) +int tfd_matedialogPresent(void) { static int lMatedialogPresent = -1 ; if ( lMatedialogPresent < 0 ) @@ -3839,7 +3712,7 @@ static int matedialogPresent( ) } -static int shellementaryPresent( ) +int tfd_shellementaryPresent(void) { static int lShellementaryPresent = -1 ; if ( lShellementaryPresent < 0 ) @@ -3850,7 +3723,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 ) @@ -3861,17 +3745,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 ) @@ -3893,7 +3788,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 ) ; } @@ -3902,16 +3797,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") ) ) @@ -3955,10 +3850,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; @@ -3976,47 +3871,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; @@ -4037,46 +3898,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 ) @@ -4089,14 +3952,35 @@ 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 tkinter2Present(void) +{ + 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( ) +static int pythonDbusPresent(void) { static int lDbusPresent = -1 ; - char lPythonCommand[256]; + char lPythonCommand[384]; char lPythonParams[256] = "-c \"try:\n\timport dbus;bus=dbus.SessionBus();\ notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');\ @@ -4122,22 +4006,23 @@ notify=dbus.Interface(notif,'org.freedesktop.Notifications');\nexcept:\n\tprint( if (tinyfd_verbose) printf("lDbusPresent %d\n", lDbusPresent) ; if (tinyfd_verbose) printf("gPythonName %s\n", gPythonName) ; } - return lDbusPresent && graphicMode() && !(isDarwin() && getenv("SSH_TTY") ); + return lDbusPresent && 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() ) @@ -4151,20 +4036,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" ) ; } - 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 { @@ -4186,13 +4079,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 ; @@ -4207,6 +4100,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") ) @@ -4255,7 +4151,7 @@ int tinyfd_messageBox( else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) { strcat( lDialogString ,"buttons {\"No\", \"Yes\"} " ) ; - if (aDefaultButton) + if (aDefaultButton) { strcat( lDialogString ,"default button \"Yes\" " ) ; } @@ -4268,7 +4164,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; @@ -4295,12 +4191,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 */ } @@ -4362,23 +4258,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" ) ; @@ -4387,12 +4283,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 ) ) { @@ -4419,19 +4315,20 @@ int tinyfd_messageBox( { strcat( lDialogString , "info" ) ; } - if ( aTitle && strlen(aTitle) ) + if ( aTitle && strlen(aTitle) ) { strcat(lDialogString, " --title=\"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\"") ; } - if ( aMessage && strlen(aMessage) ) + if ( aMessage && strlen(aMessage) ) { - strcat(lDialogString, " --no-wrap --text=\"") ; - strcat(lDialogString, aMessage) ; - strcat(lDialogString, "\"") ; + if (strcmp("yesnocancel", aDialogType)) strcat(lDialogString, " --no-wrap"); + strcat(lDialogString, " --text=\"") ; + strcat(lDialogString, aMessage) ; + strcat(lDialogString, "\"") ; } - if ( (zenity3Present() >= 3) || (!zenityPresent() && (shellementaryPresent() || qarmaPresent()) ) ) + if ( (tfd_zenity3Present() >= 3) || (!tfd_zenityPresent() && (tfd_shellementaryPresent() || tfd_qarmaPresent()) ) ) { strcat( lDialogString , " --icon-name=dialog-" ) ; if ( aIconType && (! strcmp( "question" , aIconType ) @@ -4457,21 +4354,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 \ @@ -4529,7 +4577,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); { strcat( lDialogString , "info" ) ; } - + strcat(lDialogString, "',") ; if ( aTitle && strlen(aTitle) ) { @@ -4541,7 +4589,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, "'") ; } @@ -4559,95 +4607,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() ) @@ -4766,7 +4725,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 \"") ; @@ -4948,7 +4907,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)\"") ; } @@ -4956,13 +4915,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) ) { @@ -4983,9 +4944,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 , "\"" ) ; @@ -5063,7 +5024,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); @@ -5090,10 +5051,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"); } @@ -5110,9 +5071,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 ; @@ -5121,6 +5082,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); @@ -5151,11 +5115,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" ) ; @@ -5181,36 +5145,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 , "'" ) ; } @@ -5230,13 +5179,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( ) ) { @@ -5259,7 +5211,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)\"") ; } @@ -5281,9 +5233,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 , "\"" ) ; @@ -5308,10 +5260,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; @@ -5328,8 +5280,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") ) @@ -5339,7 +5297,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 \"") ; @@ -5371,12 +5329,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 */ } @@ -5410,32 +5368,32 @@ 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 */ } @@ -5468,15 +5426,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 \""); } @@ -5499,25 +5487,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) ) { @@ -5530,7 +5556,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 ) @@ -5549,61 +5575,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 " ) ; @@ -5619,12 +5608,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 " ) ; @@ -5696,7 +5685,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 , "'" ) ; @@ -5711,7 +5700,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); strcat( lDialogString, aTitle) ; strcat( lDialogString , "\";echo;" ) ; } - + strcat( lDialogString , "echo \"" ) ; if ( aMessage && strlen(aMessage) ) { @@ -5730,12 +5719,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 ; @@ -5852,34 +5842,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 " ); @@ -5908,12 +5906,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 */ } @@ -5937,10 +5935,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) ) { @@ -5956,86 +5955,172 @@ 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) ) + if ( aTitle && strlen(aTitle) ) { strcat(lDialogString, " --title=\"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\"") ; } - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + 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, "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 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,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='") ; @@ -6073,82 +6158,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 , "'(" ) ; @@ -6211,8 +6244,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 ; @@ -6255,31 +6296,66 @@ 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 */ -{ - static char lBuff [MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD] ; - char lDialogString [MAX_PATH_OR_CMD] ; - char lString [MAX_PATH_OR_CMD] ; +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 */ +{ + char lDialogString[MAX_PATH_OR_CMD] ; + char lString[MAX_PATH_OR_CMD] ; int i ; FILE * lIn ; char * p ; - char const * p2 ; + char * p2 ; + char * lPointerInputBox ; int lWasKdialog = 0 ; int lWasGraphicDialog = 0 ; int lWasXterm = 0 ; - lBuff[0]='\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 '" ); @@ -6310,12 +6386,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 , "} " ) ; @@ -6341,13 +6417,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 */ } @@ -6371,10 +6447,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) ) { @@ -6394,32 +6471,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 */ } @@ -6444,33 +6521,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, "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 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,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 \ @@ -6518,7 +6692,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 , "))," ) ; @@ -6528,82 +6702,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 , "'(" ) ; @@ -6666,18 +6781,34 @@ 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",""); + 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 */ + p2 = tinyfd_inputBox(aTitle, lBuff, ""); + if (p2) strcpy(lBuff, p2); else lBuff[0] = '\0'; + if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */ + p2 = lBuff; + if ( ! fileExists(p2) ) { - return NULL ; + free(lBuff); + lBuff = NULL; } - return p2 ; + else + { + strcpy(lBuff, p2); + 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; @@ -6700,41 +6831,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 ) ; - } - else if ( fileExists(lBuff) ) - { - p2 = lBuff ; + if( ! ensureFilesExist( lBuff , lBuff ) ) + { + free(lBuff); + lBuff = NULL; + return NULL; + } } - else + else if ( !fileExists(lBuff) ) { - return NULL ; - } - /* printf( "lBuff3: %s\n" , p2 ) ; */ + free(lBuff); + lBuff = NULL; + return NULL; + } + + lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char))); - return p2 ; + /*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 "); @@ -6755,11 +6897,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 */ } @@ -6787,45 +6929,45 @@ 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) ) + if ( aTitle && strlen(aTitle) ) { strcat(lDialogString, " --title=\"") ; strcat(lDialogString, aTitle) ; strcat(lDialogString, "\"") ; } - if ( aDefaultPath && strlen(aDefaultPath) ) + if ( aDefaultPath && strlen(aDefaultPath) ) { strcat(lDialogString, " --filename=\"") ; strcat(lDialogString, aDefaultPath) ; @@ -6833,18 +6975,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, "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 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,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 \ @@ -6866,43 +7048,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 , "'(" ) ; @@ -6940,7 +7101,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); { strcat(lDialogString, "./") ; } - + if ( lWasGraphicDialog ) { strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; @@ -6962,7 +7123,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 ; @@ -6994,19 +7163,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 ; @@ -7014,32 +7187,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) \ @@ -7067,15 +7242,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) ) { @@ -7084,41 +7259,41 @@ 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 ) ; - if ( aTitle && strlen(aTitle) ) + if ( aTitle && strlen(aTitle) ) { strcat(lDialogString, " --title=\"") ; strcat(lDialogString, aTitle) ; @@ -7126,9 +7301,22 @@ to set mycolor to choose color default color {"); } 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) ) @@ -7136,24 +7324,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 \ @@ -7161,7 +7372,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); } strcat( lDialogString , "res=tkColorChooser.askcolor(color='" ) ; - strcat(lDialogString, lpDefaultHexRGB ) ; + strcat(lDialogString, lDefaultHexRGB ) ; strcat(lDialogString, "'") ; @@ -7173,44 +7384,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 ) ; @@ -7232,7 +7428,7 @@ frontmost of process \\\"Python\\\" to true' ''');"); { lBuff[strlen( lBuff ) -1] = '\0' ; } - + if ( lWasZenity3 ) { if ( lBuff[0] == '#' ) @@ -7248,135 +7444,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 ; -} - - -/* 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 ; - } + } + /* printf("%d %d %d\n", aoResultRGB[0],aoResultRGB[1],aoResultRGB[2]); */ + /* printf( "lBuff: %s\n" , lBuff ) ; */ - 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 ; + strcpy(lDefaultHexRGB,lBuff); + return lDefaultHexRGB ; } + #endif /* _WIN32 */ diff --git a/libtinyfiledialogs/tinyfiledialogs.h b/libtinyfiledialogs/tinyfiledialogs.h index 187656a..cf1c17b 100644 --- a/libtinyfiledialogs/tinyfiledialogs.h +++ b/libtinyfiledialogs/tinyfiledialogs.h @@ -1,79 +1,39 @@ /*_________ - / \ tinyfiledialogs.h v3.3.9 [Apr 14, 2019] zlib licence + / \ tinyfiledialogs.h v3.8.3 [Nov 1, 2020] 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 - 2020 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 | - |________________________________________________________________________| - -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 + ____________________________________________ +| | +| 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 | | +| |____________________________________________________________________________| | +|________________________________________________________________________________| + +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 +46,60 @@ 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 -extern "C" { +extern "C" { /* 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. */ #endif +/******************************************************************************************************/ +/**************************************** UTF-8 on Windows ********************************************/ +/******************************************************************************************************/ +#ifdef _WIN32 +/* 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 const tinyfd_version[8]; /* contains tinyfd current version number */ -extern char const tinyfd_needs[]; /* info about requirements */ +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 dialog*/ +extern int tinyfd_silent; /* 1 (default) or 0 : on unix, hide errors and warnings from called dialogs */ -#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.*/ -#endif +/* 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 +108,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 +171,128 @@ 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. + ________________________________________________________________________________ +| ____________________________________________________________________________ | +| | | | +| | 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 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 +- 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: */ From b982cc93862b743769725c7eb33f4635793a9b37 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 4 Dec 2020 10:56:28 -0500 Subject: [PATCH 28/41] Remove binding for missing list dialog API. --- src/lib.rs | 51 --------------------------------------------------- 1 file changed, 51 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c3a25be..6c5a18c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,14 +35,6 @@ extern { aTitle: *const c_char, aDefaultPath: *const c_char) -> *const c_char; - #[cfg(not(windows))] - fn tinyfd_arrayDialog ( - aTitle: *const c_char, - aNumOfColumns: c_int, - aColumns: *const *const c_char, - aNumOfRows: c_int, - aCells: *const *const c_char) -> *const c_char; - fn tinyfd_colorChooser ( aTitle: *const c_char, aDefaultHexRGB: *const c_char, @@ -248,49 +240,6 @@ pub fn select_folder_dialog(title: &str, path: &str) -> Option { } } -#[cfg(not(windows))] -pub fn list_dialog(title: &str, - columns: &[&str], - cells: Option<&[&str]>) -> Option { - let list_dialog_title = CString::new(title).unwrap(); - - if columns.is_empty() { - return None; - } - - let list_dialog_columns = - columns.iter().map(|s| CString::new(*s).unwrap()).collect::>(); - let ptr_list_dialog_columns = - list_dialog_columns.iter().map(|c| c.as_ptr()).collect::>(); - - let list_dialog_cells = - cells.map_or(vec![], |f| f.iter().map(|s| CString::new(*s).unwrap()).collect()); - let ptr_list_dialog_cells = - list_dialog_cells.iter().map(|c| c.as_ptr()).collect::>(); - - let dialog = unsafe { - tinyfd_arrayDialog(list_dialog_title.as_ptr(), - list_dialog_columns.len() as c_int, - ptr_list_dialog_columns.as_ptr(), - (list_dialog_cells.len() / list_dialog_columns.len()) as c_int, - ptr_list_dialog_cells.as_ptr()) - }; - if !dialog.is_null() { - unsafe { - Some(CStr::from_ptr(dialog).to_string_lossy().into_owned()) - } - } else { - None - } -} - -#[cfg(windows)] -pub fn list_dialog(_title: &str, - _columns: &[&str], - _cells: Option<&[&str]>) -> Option { - None -} - pub enum DefaultColorValue<'a> { Hex(&'a str), RGB(&'a [u8; 3]), From 83dea8cda8a6a9120765cf341883a6f2ada3fba5 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 4 Dec 2020 12:08:53 -0500 Subject: [PATCH 29/41] Remove list dialog example. --- examples/main.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/examples/main.rs b/examples/main.rs index 87c509d..a7de51b 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -49,16 +49,6 @@ fn main() { None => color = "null".to_string(), } - let list: String; - match tinyfiledialogs::list_dialog("Test Dialog", - &["Id", "Name"], - Some(&["471", "Donald Duck", - "1143", "Chris P. Bacon", - "6509", "Moon Doge"])) { - Some(result) => list = result, - None => list = "null".to_string(), - } - println!("Choice {:?}", choice); println!("User input {:?}", user_input); println!("User input 2 {:?}", user_input_2); @@ -67,5 +57,4 @@ fn main() { println!("Open file {:?}", open_file); println!("folder {:?}", folder); println!("color {:?}", color); - println!("List {:?}", list); } From 61b4c00609787dac2db291ac8109223e9c8183db Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Sat, 22 May 2021 11:02:51 -0400 Subject: [PATCH 30/41] Publish new version. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0ac40f2..6936c24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tinyfiledialogs" description = "High-level Rust binding for the tinyfiledialogs C library." -version = "3.3.10" +version = "3.8.3" authors = ["Guillaume Vareille ", "Josh Matthews "] license = "zlib-acknowledgement" readme = "README.md" From 1b9cc04426b43aaeedabf4c16660ec8d53702cd8 Mon Sep 17 00:00:00 2001 From: Maciej Pokorski Date: Thu, 30 Sep 2021 21:10:10 +0200 Subject: [PATCH 31/41] Don't link to shcore on windows-gnu targets --- appveyor.yml | 1 + build.rs | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index fa3bc66..365948d 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -2,6 +2,7 @@ environment: matrix: - TARGET: x86_64-pc-windows-msvc - TARGET: i686-pc-windows-msvc + - TARGET: x86_64-pc-windows-gnu platform: - x64 diff --git a/build.rs b/build.rs index fc57363..93d5ac7 100644 --- a/build.rs +++ b/build.rs @@ -19,6 +19,8 @@ fn main() { println!("cargo:rustc-link-lib=comdlg32"); println!("cargo:rustc-link-lib=ole32"); println!("cargo:rustc-link-lib=shell32"); - println!("cargo:rustc-link-lib=shcore"); + if !target.ends_with("pc-windows-gnu") { + println!("cargo:rustc-link-lib=shcore"); + } } } From ad3d26d882b85403ba2f522255f045209c41af3d Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Sun, 21 Nov 2021 16:02:39 -0500 Subject: [PATCH 32/41] Replace travis and appveyor with github actions. --- .github/workflows/main.yml | 71 ++++++++++++++++++++++++++++++++++++++ .travis.yml | 7 ---- appveyor.yml | 22 ------------ 3 files changed, 71 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml delete mode 100644 appveyor.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..da476ee --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,71 @@ +name: Run CI +on: + push: + branches: ["**"] + pull_request: + branches: ["**"] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +env: + RUST_BACKTRACE: 1 + SHELL: /bin/bash + +jobs: + linux-ci: + name: Linux + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 2 + - name: Setup Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + default: true + override: true + - name: Build + run: cargo build + - name: Tests + run: cargo test --all + + mac-ci: + name: macOS + runs-on: macos-latest + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 2 + - name: Setup Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + default: true + override: true + - name: Build + run: cargo build + - name: Tests + run: cargo test --all + + windows-ci: + name: Linux + runs-on: windows-latest + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 2 + - name: Setup Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + default: true + override: true + - name: Build + run: cargo build + - name: Tests + run: cargo test --all diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0b2b076..0000000 --- a/.travis.yml +++ /dev/null @@ -1,7 +0,0 @@ -language: rust -rust: - - nightly - -os: - - linux - - osx diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 365948d..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,22 +0,0 @@ -environment: - matrix: - - TARGET: x86_64-pc-windows-msvc - - TARGET: i686-pc-windows-msvc - - TARGET: x86_64-pc-windows-gnu - -platform: - - x64 - -install: - - ps: Start-FileDownload "https://static.rust-lang.org/dist/rust-nightly-${env:TARGET}.exe" - - rust-nightly-%TARGET%.exe /VERYSILENT /NORESTART /DIR="C:\Program Files (x86)\Rust" - - SET PATH=C:\msys64\mingw64\bin;C:\msys64\usr\bin\;%PATH% - - SET PATH=%PATH%;C:\Program Files (x86)\Rust\bin - - SET RUST_BACKTRACE=1 - - rustc -V - - cargo -V - -build: false - -test_script: - - cargo test --verbose From 3d997f5d9bad92fe55ef18caf18e5712282ea717 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 4 Jan 2022 10:57:46 +0800 Subject: [PATCH 33/41] Update libtiny-file-dialogs to 3.8.8 The corresponding hash is 2681e426ddaebc8e2764a7823b4b9d69564d1684 from the source repository at https://git.code.sf.net/p/tinyfiledialogs/code. This upgrade fixes the issue of the save-file dialog not working on MacOS Monterey. --- libtinyfiledialogs/tinyfiledialogs.c | 15399 +++++++++++++------------ libtinyfiledialogs/tinyfiledialogs.h | 601 +- 2 files changed, 8040 insertions(+), 7960 deletions(-) diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index abfe703..30865fe 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -1,7662 +1,7737 @@ -/*_________ - / \ tinyfiledialogs.c v3.8.3 [Nov 1, 2020] zlib licence - |tiny file| Unique code file created [November 9, 2014] - | dialogs | Copyright (c) 2014 - 2020 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 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 | - |_________________________________________________________| - -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 -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__) ) - #define _GNU_SOURCE /* used only to resolve symbolic links. Can be commented out */ -#endif - -#include -#include -#include -#include -#include - -#ifdef _WIN32 - #ifdef __BORLANDC__ - #define _getch getch - #endif - #ifndef _WIN32_WINNT - #define _WIN32_WINNT 0x0500 - #endif - #include - #include - #include - #include - #include - #define TINYFD_NOCCSUNICODE - #define SLASH "\\" -#else - #include - #include - #include /* on old systems try instead */ - #include - #include - #include /* on old systems try instead */ - #define SLASH "/" -#endif /* _WIN32 */ - -#include "tinyfiledialogs.h" - -#define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */ - -#ifndef MAX_MULTIPLE_FILES -#define MAX_MULTIPLE_FILES 1024 -#endif -#define LOW_MULTIPLE_FILES 32 - -char const tinyfd_version[8] = "3.8.3"; - -/******************************************************************************************************/ -/**************************************** 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. - 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, -the functions will not display the dialogs -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 - shellementary qarma yad python2-tkinter python3-tkinter python-dbus - perl-dbus gxmessage gmessage xmessage xdialog gdialog -for console mode: - dialog whiptail basicinput no_solution */ - -static int gWarningDisplayed = 0 ; -static char gTitle[]="missing software! (we will try basic console input)"; - -#ifdef _WIN32 -char tinyfd_needs[] = "\ - ___________\n\ -/ \\ \n\ -| tiny file |\n\ -| dialogs |\n\ -\\_____ ____/\n\ - \\|\ -\ntiny file dialogs on Windows needs:\ -\n a graphic display\ -\nor dialog.exe (curses console mode)\ -\nor a console for basic input"; -#else -char tinyfd_needs[] = "\ - ___________\n\ -/ \\ \n\ -| tiny file |\n\ -| dialogs |\n\ -\\_____ ____/\n\ - \\|\ -\ntiny file dialogs on UNIX needs:\ -\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)\ -\nor xterm + bash (opens console for basic input)\ -\nor existing console for basic input"; -#endif - -#ifdef _MSC_VER -#pragma warning(disable:4996) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ -#pragma warning(disable:4100) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ -#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 * aoDestination, /* make sure it is allocated, use _MAX_PATH */ - char const * aSource) /* aoDestination and aSource can be the same */ -{ - char const * lTmp ; - if ( aSource ) - { - lTmp = strrchr(aSource, '/'); - if (!lTmp) - { - lTmp = strrchr(aSource, '\\'); - } - if (lTmp) - { - strncpy(aoDestination, aSource, lTmp - aSource ); - aoDestination[lTmp - aSource] = '\0'; - } - else - { - * aoDestination = '\0'; - } - } - else - { - * aoDestination = '\0'; - } - return aoDestination; -} - - -static char * getLastName( - char * aoDestination, /* make sure it is allocated */ - char const * aSource) -{ - /* copy the last name after '/' or '\' */ - char const * lTmp ; - if ( aSource ) - { - lTmp = strrchr(aSource, '/'); - if (!lTmp) - { - lTmp = strrchr(aSource, '\\'); - } - if (lTmp) - { - strcpy(aoDestination, lTmp + 1); - } - else - { - strcpy(aoDestination, aSource); - } - } - else - { - * aoDestination = '\0'; - } - return aoDestination; -} - - -static void ensureFinalSlash( char * aioString ) -{ - if ( aioString && strlen( aioString ) ) - { - char * lastcar = aioString + strlen( aioString ) - 1 ; - if ( strncmp( lastcar , SLASH , 1 ) ) - { - strcat( lastcar , SLASH ) ; - } - } -} - - -static void Hex2RGB( char const aHexRGB[8] , unsigned char aoResultRGB[3] ) -{ - char lColorChannel[8] ; - if ( aoResultRGB ) - { - if ( aHexRGB ) - { - strcpy(lColorChannel, aHexRGB ) ; - aoResultRGB[2] = (unsigned char)strtoul(lColorChannel+5,NULL,16); - lColorChannel[5] = '\0'; - aoResultRGB[1] = (unsigned char)strtoul(lColorChannel+3,NULL,16); - lColorChannel[3] = '\0'; - aoResultRGB[0] = (unsigned char)strtoul(lColorChannel+1,NULL,16); -/* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */ - } - else - { - aoResultRGB[0]=0; - aoResultRGB[1]=0; - aoResultRGB[2]=0; - } - } -} - -static void RGB2Hex( unsigned char const aRGB[3], char aoResultHexRGB[8] ) -{ - if ( aoResultHexRGB ) - { - if ( aRGB ) - { -#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, "#%02hx%02hx%02hx", aRGB[0], aRGB[1], aRGB[2]); -#endif - /*printf("aoResultHexRGB %s\n", aoResultHexRGB);*/ - } - else - { - aoResultHexRGB[0]=0; - aoResultHexRGB[1]=0; - aoResultHexRGB[2]=0; - } - } -} - - -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' ; - return ; - } - if ( ! aOldSubStr ) - { - strcpy( aoDestination , aSource ) ; - return ; - } - if ( aNewSubStr ) - { - lNewSubStr = aNewSubStr ; - } - p = aSource ; - * aoDestination = '\0' ; - while ( ( pOccurence = strstr( p , aOldSubStr ) ) != NULL ) - { - strncat( aoDestination , p , pOccurence - p ) ; - strcat( aoDestination , lNewSubStr ) ; - p = pOccurence + lOldSubLen ; - } - strcat( aoDestination , p ) ; -} - - -static int filenameValid( char const * aFileNameWithoutPath ) -{ - if ( ! aFileNameWithoutPath - || ! strlen(aFileNameWithoutPath) - || strpbrk(aFileNameWithoutPath , "\\/:*?\"<>|") ) - { - return 0 ; - } - return 1 ; -} - -#ifndef _WIN32 - -static int fileExists( char const * aFilePathAndName ) -{ - FILE * lIn ; - if ( ! aFilePathAndName || ! strlen(aFilePathAndName) ) - { - return 0 ; - } - lIn = fopen( aFilePathAndName , "r" ) ; - if ( ! lIn ) - { - return 0 ; - } - fclose( lIn ) ; - return 1 ; -} - -#endif - - -static void wipefile(char const * aFilename) -{ - int i; - struct stat st; - FILE * lIn; - - if (stat(aFilename, &st) == 0) - { - if ((lIn = fopen(aFilename, "w"))) - { - for (i = 0; i < st.st_size; i++) - { - fputc('A', 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 void replaceChr(char * aString, char aOldChr, char aNewChr) -{ - char * p; - - if (!aString) return; - if (aOldChr == aNewChr) return; - - p = aString; - while ((p = strchr(p, aOldChr))) - { - *p = aNewChr; - p++; - } - return; -} - - -#if !defined(WC_ERR_INVALID_CHARS) -/* undefined prior to Vista, so not yet in MINGW header file */ -#define WC_ERR_INVALID_CHARS 0x00000080 -#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_utf8to16(char const * aUtf8string) -{ - static wchar_t * lUtf16string = NULL; - int lSize; - - free(lUtf16string); - if (!aUtf8string) {lUtf16string = NULL; return NULL;} - lSize = sizeUtf16From8(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); - lUtf16string = NULL; - } - return lUtf16string; -} - - -char * tinyfd_utf16toMbcs(wchar_t const * aUtf16string) -{ - static char * lMbcsString = NULL; - int lSize; - - free(lMbcsString); - if (!aUtf16string) { lMbcsString = NULL; return NULL; } - lSize = sizeMbcs(aUtf16string); - lMbcsString = (char *)malloc(lSize); - lSize = WideCharToMultiByte(CP_ACP, 0, - aUtf16string, -1, lMbcsString, lSize, NULL, NULL); - if (lSize == 0) - { - free(lMbcsString); - lMbcsString = NULL; - } - return lMbcsString; -} - - -char * tinyfd_utf8toMbcs(char const * aUtf8string) -{ - wchar_t const * lUtf16string; - lUtf16string = tinyfd_utf8to16(aUtf8string); - return tinyfd_utf16toMbcs(lUtf16string); -} - -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); - lMbcsString = (wchar_t *)malloc(lSize * sizeof(wchar_t)); - lSize = MultiByteToWideChar(CP_ACP, 0, - aMbcsString, -1, lMbcsString, lSize); - if (lSize == 0) - { - free(lMbcsString); - lMbcsString = NULL; - } - return lMbcsString; -} - - -char * tinyfd_utf16to8(wchar_t const * aUtf16string) -{ - static char * lUtf8string = NULL; - int lSize; - - free(lUtf8string); - if (!aUtf16string) { lUtf8string = NULL; return NULL; } - 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); - lUtf8string = NULL; - } - return lUtf8string; -} - - -char * tinyfd_mbcsTo8(char const * aMbcsString) -{ - wchar_t const * lUtf16string; - lUtf16string = tinyfd_mbcsTo16(aMbcsString); - return tinyfd_utf16to8(lUtf16string); -} - - -void tinyfd_beep(void) -{ - Beep(440,300); -} - - -static void wipefileW(wchar_t const * aFilename) -{ - int i; - struct _stat st; - FILE * lIn; - - if (_wstat(aFilename, &st) == 0) - { - if ((lIn = _wfopen(aFilename, L"w"))) - { - for (i = 0; i < st.st_size; i++) - { - fputc('A', lIn); - } - fclose(lIn); - } - } -} - - -static wchar_t * getPathWithoutFinalSlashW( - 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) - { - lTmp = wcsrchr(aSource, L'/'); - if (!lTmp) - { - lTmp = wcsrchr(aSource, L'\\'); - } - if (lTmp) - { - wcsncpy(aoDestination, aSource, lTmp - aSource); - aoDestination[lTmp - aSource] = L'\0'; - } - else - { - *aoDestination = L'\0'; - } - } - else - { - *aoDestination = L'\0'; - } - return aoDestination; -} - - -static wchar_t * getLastNameW( - wchar_t * aoDestination, /* make sure it is allocated */ - wchar_t const * aSource) -{ - /* copy the last name after '/' or '\' */ - wchar_t const * lTmp; - if (aSource) - { - lTmp = wcsrchr(aSource, L'/'); - if (!lTmp) - { - lTmp = wcsrchr(aSource, L'\\'); - } - if (lTmp) - { - wcscpy(aoDestination, lTmp + 1); - } - else - { - wcscpy(aoDestination, aSource); - } - } - else - { - *aoDestination = L'\0'; - } - return aoDestination; -} - - -static void Hex2RGBW(wchar_t const aHexRGB[8], unsigned char aoResultRGB[3]) -{ - wchar_t lColorChannel[8]; - if (aoResultRGB) - { - if (aHexRGB) - { - wcscpy(lColorChannel, aHexRGB); - aoResultRGB[2] = (unsigned char)wcstoul(lColorChannel + 5, NULL, 16); - lColorChannel[5] = '\0'; - aoResultRGB[1] = (unsigned char)wcstoul(lColorChannel + 3, NULL, 16); - lColorChannel[3] = '\0'; - aoResultRGB[0] = (unsigned char)wcstoul(lColorChannel + 1, NULL, 16); - /* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */ - } - else - { - aoResultRGB[0] = 0; - aoResultRGB[1] = 0; - aoResultRGB[2] = 0; - } - } -} - - -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); */ -#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 - - } - else - { - aoResultHexRGB[0] = 0; - aoResultHexRGB[1] = 0; - aoResultHexRGB[2] = 0; - } - } -} - - -static int dirExists(char const * aDirPath) -{ - struct _stat lInfo; - wchar_t * lTmpWChar; - int lStatRet; - size_t lDirLen; - - if (!aDirPath) - return 0; - lDirLen = strlen(aDirPath); - if (!lDirLen) - return 1; - if ( (lDirLen == 2) && (aDirPath[1] == ':') ) - return 1; - - if (tinyfd_winUtf8) - { - lTmpWChar = tinyfd_utf8to16(aDirPath); - lStatRet = _wstat(lTmpWChar, &lInfo); - if (lStatRet != 0) - return 0; - else if (lInfo.st_mode & S_IFDIR) - return 1; - else - return 0; - } - else if (_stat(aDirPath, &lInfo) != 0) - return 0; - else if (lInfo.st_mode & S_IFDIR) - return 1; - else - return 0; -} - - -static int fileExists(char const * aFilePathAndName) -{ - struct _stat lInfo; - wchar_t * lTmpWChar; - int lStatRet; - FILE * lIn; - - if (!aFilePathAndName || !strlen(aFilePathAndName)) - { - return 0; - } - - if (tinyfd_winUtf8) - { - lTmpWChar = tinyfd_utf8to16(aFilePathAndName); - lStatRet = _wstat(lTmpWChar, &lInfo); - if (lStatRet != 0) - return 0; - else if (lInfo.st_mode & _S_IFREG) - return 1; - else - return 0; - } - else - { - lIn = fopen(aFilePathAndName, "r"); - if (!lIn) - { - return 0; - } - fclose(lIn); - return 1; - } -} - -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 * ensureFilesExist(char * aDestination, - char const * aSourcePathsAndNames) -{ - char * lDestination = aDestination; - char const * p; - char const * p2; - size_t lLen; - - if (!aSourcePathsAndNames) - { - return NULL; - } - lLen = strlen(aSourcePathsAndNames); - if (!lLen) - { - return NULL; - } - - p = aSourcePathsAndNames; - while ((p2 = strchr(p, '|')) != NULL) - { - lLen = p2 - p; - memmove(lDestination, p, lLen); - lDestination[lLen] = '\0'; - if (fileExists(lDestination)) - { - lDestination += lLen; - *lDestination = '|'; - lDestination++; - } - p = p2 + 1; - } - if (fileExists(p)) - { - lLen = strlen(p); - memmove(lDestination, p, lLen); - lDestination[lLen] = '\0'; - } - else - { - *(lDestination - 1) = '\0'; - } - return aDestination; -} - -#ifdef _WIN32 - -static int __stdcall EnumThreadWndProc(HWND hwnd, LPARAM lParam) -{ - wchar_t lTitleName[MAX_PATH]; - GetWindowTextW(hwnd, lTitleName, MAX_PATH); - /* wprintf(L"lTitleName %ls \n", lTitleName); */ - if (wcscmp(L"tinyfiledialogsTopWindow", lTitleName) == 0) - { - SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); - return 0; - } - return 1; -} - - -static void hiddenConsoleW(wchar_t const * aString, wchar_t const * aDialogTitle, int aInFront) -{ - STARTUPINFOW StartupInfo; - PROCESS_INFORMATION ProcessInfo; - - if (!aString || !wcslen(aString) ) return; - - memset(&StartupInfo, 0, sizeof(StartupInfo)); - StartupInfo.cb = sizeof(STARTUPINFOW); - StartupInfo.dwFlags = STARTF_USESHOWWINDOW; - StartupInfo.wShowWindow = SW_HIDE; - - if (!CreateProcessW(NULL, (LPWSTR)aString, NULL, NULL, FALSE, - CREATE_NEW_CONSOLE, NULL, NULL, - &StartupInfo, &ProcessInfo)) - { - return; /* GetLastError(); */ - } - - WaitForInputIdle(ProcessInfo.hProcess, INFINITE); - if (aInFront) - { - while (EnumWindows(EnumThreadWndProc, (LPARAM)NULL)) {} - SetWindowTextW(GetForegroundWindow(), aDialogTitle); - } - WaitForSingleObject(ProcessInfo.hProcess, INFINITE); - CloseHandle(ProcessInfo.hThread); - CloseHandle(ProcessInfo.hProcess); -} - - -int tinyfd_messageBoxW( - 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; - } - else if (aIconType && !wcscmp(L"error", aIconType)) - { - aCode = MB_ICONERROR; - } - else if (aIconType && !wcscmp(L"question", aIconType)) - { - aCode = MB_ICONQUESTION; - } - else - { - aCode = MB_ICONINFORMATION; - } - - if (aDialogType && !wcscmp(L"okcancel", aDialogType)) - { - aCode += MB_OKCANCEL; - if (!aDefaultButton) - { - aCode += MB_DEFBUTTON2; - } - } - else if (aDialogType && !wcscmp(L"yesno", aDialogType)) - { - aCode += MB_YESNO; - if (!aDefaultButton) - { - aCode += MB_DEFBUTTON2; - } - } - else - { - aCode += MB_OK; - } - - aCode += MB_TOPMOST; - - lBoxReturnValue = MessageBoxW(GetForegroundWindow(), aMessage, aTitle, aCode); - if (((aDialogType - && wcscmp(L"okcancel", aDialogType) - && wcscmp(L"yesno", aDialogType))) - || (lBoxReturnValue == IDOK) - || (lBoxReturnValue == IDYES)) - { - return 1; - } - else - { - return 0; - } -} - - -/* return has only meaning for tinyfd_query */ -int tinyfd_notifyPopupW( - 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 (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 {\ -[cmdletbinding()] \ -param( \ -[string]$Title = ' ', \ -[string]$Message = ' ', \ -[ValidateSet('info', 'warning', 'error')] \ -[string]$IconType = 'info');\ -[system.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null ; \ -$balloon = New-Object System.Windows.Forms.NotifyIcon ; \ -$path = Get-Process -id $pid | Select-Object -ExpandProperty Path ; \ -$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) ;"); - - wcscat(lDialogString, L"\ -$balloon.Icon = $icon ; \ -$balloon.BalloonTipIcon = $IconType ; \ -$balloon.BalloonTipText = $Message ; \ -$balloon.BalloonTipTitle = $Title ; \ -$balloon.Text = 'lalala' ; \ -$balloon.Visible = $true ; \ -$balloon.ShowBalloonTip(5000)};\ -Show-BalloonTip"); - - if (aTitle && wcslen(aTitle)) - { - wcscat(lDialogString, L" -Title '"); - wcscat(lDialogString, aTitle); - wcscat(lDialogString, L"'"); - } - if (aMessage && wcslen(aMessage)) - { - wcscat(lDialogString, L" -Message '"); - wcscat(lDialogString, aMessage); - wcscat(lDialogString, L"'"); - } - if (aMessage && wcslen(aIconType)) - { - wcscat(lDialogString, L" -IconType '"); - wcscat(lDialogString, aIconType); - wcscat(lDialogString, L"'"); - } - wcscat(lDialogString, L"\""); - - /* wprintf ( L"lDialogString: %ls\n" , lDialogString ) ; */ - - hiddenConsoleW(lDialogString, aTitle, 0); - free(lDialogString); - return 1; -} - - -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; - FILE * lIn; - FILE * lFile; - int lResult; - size_t lTitleLen; - size_t lMessageLen; - size_t lDialogStringLen; - - 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 ; - lDialogStringLen = 3 * MAX_PATH_OR_CMD + lTitleLen + lMessageLen; - lDialogString = (wchar_t *)malloc(2 * lDialogStringLen); - - if (aDefaultInput) - { - swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) - lDialogStringLen, -#endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.vbs", _wgetenv(L"USERPROFILE")); - } - else - { - swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) - lDialogStringLen, -#endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.hta", _wgetenv(L"USERPROFILE")); - } - lIn = _wfopen(lDialogString, L"w"); - if (!lIn) - { - free(lDialogString); - return NULL; - } - - if ( aDefaultInput ) - { - wcscpy(lDialogString, L"Dim result:result=InputBox(\""); - if (aMessage && wcslen(aMessage)) - { - wcscpy(lBuff, aMessage); - replaceWchar(lBuff, L'\n', L' '); - wcscat(lDialogString, lBuff); - } - wcscat(lDialogString, L"\",\"tinyfiledialogsTopWindow\",\""); - if (aDefaultInput && wcslen(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"); - } - else - { - wcscpy(lDialogString, L"\n\ -\n\ -\n\ -"); - - wcscat(lDialogString, L"tinyfiledialogsTopWindow"); - wcscat(lDialogString, L"\n\ -\n\ -\n\ -\n\ -\n\ -\n\ -\n\ -\n\ -\n\ -\n\ -\n\ -
\n"); - - wcscat(lDialogString, aMessage ? aMessage : L""); - - wcscat(lDialogString, L"\n\ -\n\ -\n\ -\n\ -
\n\ -

\n\ -\n\ -
\n\ -
\n"); - - wcscat(lDialogString, L"\n\ -\n\ -\n\ -\n\ -
\n\ -
\n\ -
\n\ -\n\ -\n\ -" ) ; - } - fputws(lDialogString, lIn); - fclose(lIn); - - if (aDefaultInput) - { - swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) - lDialogStringLen, -#endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.txt",_wgetenv(L"USERPROFILE")); - -#ifdef TINYFD_NOCCSUNICODE - lFile = _wfopen(lDialogString, L"w"); - fputc(0xFF, lFile); - fputc(0xFE, lFile); -#else - lFile = _wfopen(lDialogString, L"wt, ccs=UNICODE"); /*or ccs=UTF-16LE*/ -#endif - 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\""); - } - else - { - wcscpy(lDialogString, - L"cmd.exe /c mshta.exe \"%USERPROFILE%\\AppData\\Local\\Temp\\tinyfd.hta\""); - } - - /* wprintf ( "lDialogString: %ls\n" , lDialogString ) ; */ - - hiddenConsoleW(lDialogString, aTitle, 1); - - swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) - lDialogStringLen, -#endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.txt", _wgetenv(L"USERPROFILE")); - /* wprintf(L"lDialogString: %ls\n", lDialogString); */ -#ifdef TINYFD_NOCCSUNICODE - if (!(lIn = _wfopen(lDialogString, L"r"))) -#else - if (!(lIn = _wfopen(lDialogString, L"rt, ccs=UNICODE"))) /*or ccs=UTF-16LE*/ -#endif - { - _wremove(lDialogString); - 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 - fgetws(lBuff, MAX_PATH_OR_CMD, lIn); -#endif - fclose(lIn); - wipefileW(lDialogString); - _wremove(lDialogString); - - if (aDefaultInput) - { - swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) - lDialogStringLen, -#endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.vbs", - _wgetenv(L"USERPROFILE")); - } - else - { - swprintf(lDialogString, -#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) - lDialogStringLen, -#endif - L"%ls\\AppData\\Local\\Temp\\tinyfd.hta", - _wgetenv(L"USERPROFILE")); - } - _wremove(lDialogString); - free(lDialogString); - /* wprintf( L"lBuff: %ls\n" , lBuff ) ; */ -#ifdef TINYFD_NOCCSUNICODE - lResult = !wcsncmp(lBuff+1, L"1", 1); -#else - lResult = !wcsncmp(lBuff, L"1", 1); -#endif - - /* printf( "lResult: %d \n" , lResult ) ; */ - if (!lResult) - { - return NULL ; - } - - /* 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 -} - - -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]; - wchar_t lDialogString[MAX_PATH_OR_CMD]; - wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L""; - wchar_t * p; - wchar_t * lRetval; - 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 *)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); - - getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile); - getLastNameW(lBuff, aDefaultPathAndFile); - - if (aNumOfFilterPatterns > 0) - { - ldefExt = aFilterPatterns[0]; - - if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) - { - wcscpy(lFilterPatterns, aSingleFilterDescription); - wcscat(lFilterPatterns, L"\n"); - } - wcscat(lFilterPatterns, aFilterPatterns[0]); - for (i = 1; i < aNumOfFilterPatterns; i++) - { - wcscat(lFilterPatterns, L";"); - wcscat(lFilterPatterns, aFilterPatterns[i]); - } - wcscat(lFilterPatterns, L"\n"); - if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription))) - { - wcscpy(lDialogString, lFilterPatterns); - wcscat(lFilterPatterns, lDialogString); - } - wcscat(lFilterPatterns, L"All Files\n*.*\n"); - p = lFilterPatterns; - while ((p = wcschr(p, L'\n')) != NULL) - { - *p = L'\0'; - p++; - } - } - - ofn.lStructSize = sizeof(OPENFILENAMEW); - ofn.hwndOwner = GetForegroundWindow(); - ofn.hInstance = 0; - 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.lpstrFileTitle = NULL; - ofn.nMaxFileTitle = MAX_PATH_OR_CMD/2; - 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 = ldefExt; - ofn.lCustData = 0L; - ofn.lpfnHook = NULL; - ofn.lpTemplateName = NULL; - - if (GetSaveFileNameW(&ofn) == 0) - { - lRetval = NULL; - } - else - { - lRetval = lBuff; - } - - if (lHResult == S_OK || lHResult == S_FALSE) - { - CoUninitialize(); - } - return lRetval; -} - - -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 */ -{ - 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+1]; - wchar_t * p; - int i, j; - size_t lBuffLen; - DWORD lFullBuffLen; - HRESULT lHResult; - OPENFILENAMEW ofn = { 0 }; - static wchar_t * lBuff = NULL; - - 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); - - getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile); - getLastNameW(lBuff, aDefaultPathAndFile); - - if (aNumOfFilterPatterns > 0) - { - if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) - { - wcscpy(lFilterPatterns, aSingleFilterDescription); - wcscat(lFilterPatterns, L"\n"); - } - wcscat(lFilterPatterns, aFilterPatterns[0]); - for (i = 1; i < aNumOfFilterPatterns; i++) - { - wcscat(lFilterPatterns, L";"); - wcscat(lFilterPatterns, aFilterPatterns[i]); - } - wcscat(lFilterPatterns, L"\n"); - if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription))) - { - wcscpy(lDialogString, lFilterPatterns); - wcscat(lFilterPatterns, lDialogString); - } - wcscat(lFilterPatterns, L"All Files\n*.*\n"); - p = lFilterPatterns; - while ((p = wcschr(p, L'\n')) != NULL) - { - *p = L'\0'; - p++; - } - } - - ofn.lStructSize = sizeof(OPENFILENAME); - ofn.hwndOwner = GetForegroundWindow(); - ofn.hInstance = 0; - ofn.lpstrFilter = wcslen(lFilterPatterns) ? lFilterPatterns : NULL; - ofn.lpstrCustomFilter = NULL; - ofn.nMaxCustFilter = 0; - ofn.nFilterIndex = 1; - ofn.lpstrFile = lBuff; - ofn.nMaxFile = lFullBuffLen; - ofn.lpstrFileTitle = NULL; - ofn.nMaxFileTitle = MAX_PATH_OR_CMD / 2; - 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; - ofn.nFileExtension = 0; - ofn.lpstrDefExt = NULL; - ofn.lCustData = 0L; - ofn.lpfnHook = NULL; - ofn.lpTemplateName = NULL; - - if (aAllowMultipleSelects) - { - ofn.Flags |= OFN_ALLOWMULTISELECT; - } - - if (GetOpenFileNameW(&ofn) == 0) - { - free(lBuff); - lBuff = NULL; - } - else - { - lBuffLen = wcslen(lBuff); - lPointers[0] = lBuff + lBuffLen + 1; - 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 < 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 lBuff; -} - - -BOOL CALLBACK BrowseCallbackProcW_enum(HWND hWndChild, LPARAM lParam) -{ - 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; -} - - -static int __stdcall BrowseCallbackProcW(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) -{ - 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 * 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 *)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); - - bInfo.hwndOwner = GetForegroundWindow(); - bInfo.pidlRoot = NULL; - bInfo.pszDisplayName = lBuff; - bInfo.lpszTitle = aTitle && wcslen(aTitle) ? aTitle : NULL; - if (lHResult == S_OK || lHResult == S_FALSE) - { - bInfo.ulFlags = BIF_USENEWUI; - } - bInfo.lpfn = BrowseCallbackProcW; - bInfo.lParam = (LPARAM)aDefaultPath; - bInfo.iImage = -1; - - lpItem = SHBrowseForFolderW(&bInfo); - if (!lpItem) - { - lRetval = NULL; - } - else - { - SHGetPathFromIDListW(lpItem, lBuff); - lRetval = lBuff ; - } - - if (lHResult == S_OK || lHResult == S_FALSE) - { - CoUninitialize(); - } - return lRetval; -} - - -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 } */ -{ - static wchar_t lResultHexRGB[8]; - CHOOSECOLORW cc; - COLORREF crCustColors[16]; - unsigned char lDefaultRGB[3]; - int lRet; - - HRESULT lHResult; - - 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 ) - { - Hex2RGBW(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_ANYCOLOR ; - cc.lCustData = 0; - cc.lpfnHook = NULL; - cc.lpTemplateName = NULL; - - lRet = ChooseColorW(&cc); - - if (!lRet) - { - return NULL; - } - - aoResultRGB[0] = GetRValue(cc.rgbResult); - aoResultRGB[1] = GetGValue(cc.rgbResult); - aoResultRGB[2] = GetBValue(cc.rgbResult); - - RGB2HexW(aoResultRGB, lResultHexRGB); - - if (lHResult == S_OK || lHResult == S_FALSE) - { - CoUninitialize(); - } - - return lResultHexRGB; -} - - -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 = 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 = 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 = 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); - - 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[128]; - wchar_t lDefaultHexRGB[16]; - wchar_t * lTmpWChar; - char * lTmpChar; - - 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, - lDefaultHexRGB, - aDefaultRGB, - aoResultRGB ); - - if (!lTmpWChar) - { - return NULL; - } - - if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); - else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); - strcpy(lResultHexRGB, lTmpChar); - - return lResultHexRGB; -} - - -static int dialogPresent(void) -{ - static int lDialogPresent = -1 ; - char lBuff[MAX_PATH_OR_CMD] ; - FILE * lIn ; - char const * lString = "dialog.exe"; - if (!tinyfd_allowCursesDialogs) return 0; - if (lDialogPresent < 0) - { - if (!(lIn = _popen("where dialog.exe","r"))) - { - lDialogPresent = 0 ; - return 0 ; - } - while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - {} - _pclose( lIn ) ; - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - if ( strcmp(lBuff+strlen(lBuff)-strlen(lString),lString) ) - { - lDialogPresent = 0 ; - } - else - { - lDialogPresent = 1 ; - } - } - return lDialogPresent; -} - - -static int messageBoxWinConsole( - 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)) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) - || !strcmp("yesno", aDialogType) || !strcmp("yesnocancel", aDialogType) ) ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, "tab: move focus") ; - strcat(lDialogString, "\" ") ; - } - - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - if ( ! aDefaultButton ) - { - strcat( lDialogString , "--defaultno " ) ; - } - strcat( lDialogString , - "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ; - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - if ( ! aDefaultButton ) - { - strcat( lDialogString , "--defaultno " ) ; - } - strcat( lDialogString , "--yesno " ) ; - } - else if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - if (!aDefaultButton) - { - strcat(lDialogString, "--defaultno "); - } - strcat(lDialogString, "--menu "); - } - else - { - strcat( lDialogString , "--msgbox " ) ; - } - - strcat( lDialogString , "\"" ) ; - if ( aMessage && strlen(aMessage) ) - { - tfd_replaceSubStr( aMessage , "\n" , "\\n" , lBuff ) ; - strcat(lDialogString, lBuff) ; - lBuff[0]='\0'; - } - strcat(lDialogString, "\" "); - - if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - strcat(lDialogString, "0 60 0 Yes \"\" No \"\""); - strcat(lDialogString, "2>>"); - } - else - { - strcat(lDialogString, "10 60"); - strcat(lDialogString, " && echo 1 > "); - } - - strcpy(lDialogFile, getenv("USERPROFILE")); - strcat(lDialogFile, "\\AppData\\Local\\Temp\\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) - {} - fclose(lIn); - remove(lDialogFile); - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - - /* if (tinyfd_verbose) printf("lBuff: %s\n", lBuff); */ - if ( ! strlen(lBuff) ) - { - return 0; - } - - if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - if (lBuff[0] == 'Y') return 1; - else return 2; - } - - return 1; -} - - -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(lDialogString , "echo|set /p=1 >" ) ; - strcat(lDialogString, lDialogFile); - strcat( lDialogString , " & " ) ; - - strcat( lDialogString , "dialog " ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, "tab: move focus") ; - if ( ! aDefaultInput ) - { - strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ; - } - - strcat(lDialogString, "\" ") ; - - if ( ! aDefaultInput ) - { - strcat( lDialogString , "--insecure --passwordbox" ) ; - } - else - { - strcat( lDialogString , "--inputbox" ) ; - } - strcat( lDialogString , " \"" ) ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat(lDialogString,"\" 10 60 ") ; - if ( aDefaultInput && strlen(aDefaultInput) ) - { - strcat(lDialogString, "\"") ; - strcat(lDialogString, aDefaultInput) ; - strcat(lDialogString, "\" ") ; - } - - strcat(lDialogString, "2>>"); - strcpy(lDialogFile, getenv("USERPROFILE")); - strcat(lDialogFile, "\\AppData\\Local\\Temp\\tinyfd.txt"); - strcat(lDialogString, lDialogFile); - strcat(lDialogString, " || echo 0 > "); - strcat(lDialogString, lDialogFile); - - /* printf( "lDialogString: %s\n" , lDialogString ) ; */ - system( lDialogString ) ; - - if (!(lIn = fopen(lDialogFile, "r"))) - { - remove(lDialogFile); - aoBuff[0] = '\0'; - return 0; - } - while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) - {} - fclose(lIn); - - wipefile(lDialogFile); - remove(lDialogFile); - if ( aoBuff[strlen( aoBuff ) -1] == '\n' ) - { - aoBuff[strlen( aoBuff ) -1] = '\0' ; - } - /* printf( "aoBuff: %s\n" , aoBuff ) ; */ - - /* printf( "aoBuff: %s len: %lu \n" , aoBuff , strlen(aoBuff) ) ; */ - lResult = strncmp( aoBuff , "1" , 1) ? 0 : 1 ; - /* printf( "lResult: %d \n" , lResult ) ; */ - if ( ! lResult ) - { - aoBuff[0] = '\0'; - return 0 ; - } - /* printf( "aoBuff+1: %s\n" , aoBuff+1 ) ; */ - strcpy(aoBuff, aoBuff+3); - return 1; -} - - -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] = ""; - FILE * lIn; - - strcpy( lDialogString , "dialog " ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; - - strcat( lDialogString , "--fselect \"" ) ; - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - /* dialog.exe uses unix separators even on windows */ - strcpy(lPathAndFile, aDefaultPathAndFile); - replaceChr( lPathAndFile , '\\' , '/' ) ; - } - - /* dialog.exe needs at least one separator */ - if ( ! strchr(lPathAndFile, '/') ) - { - strcat(lDialogString, "./") ; - } - strcat(lDialogString, lPathAndFile) ; - strcat(lDialogString, "\" 0 60 2>"); - strcpy(lPathAndFile, getenv("USERPROFILE")); - strcat(lPathAndFile, "\\AppData\\Local\\Temp\\tinyfd.txt"); - strcat(lDialogString, lPathAndFile); - - /* printf( "lDialogString: %s\n" , lDialogString ) ; */ - system( lDialogString ) ; - - if (!(lIn = fopen(lPathAndFile, "r"))) - { - remove(lPathAndFile); - return NULL; - } - while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) - {} - fclose(lIn); - remove(lPathAndFile); - replaceChr( aoBuff , '/' , '\\' ) ; - /* printf( "aoBuff: %s\n" , aoBuff ) ; */ - getLastName(lDialogString,aoBuff); - if ( ! strlen(lDialogString) ) - { - return NULL; - } - return aoBuff; -} - - -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) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; - - strcat( lDialogString , "--fselect \"" ) ; - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - /* dialog.exe uses unix separators even on windows */ - strcpy(lFilterPatterns, aDefaultPathAndFile); - replaceChr( lFilterPatterns , '\\' , '/' ) ; - } - - /* dialog.exe needs at least one separator */ - if ( ! strchr(lFilterPatterns, '/') ) - { - strcat(lDialogString, "./") ; - } - strcat(lDialogString, lFilterPatterns) ; - strcat(lDialogString, "\" 0 60 2>"); - strcpy(lFilterPatterns, getenv("USERPROFILE")); - strcat(lFilterPatterns, "\\AppData\\Local\\Temp\\tinyfd.txt"); - strcat(lDialogString, lFilterPatterns); - - /* printf( "lDialogString: %s\n" , lDialogString ) ; */ - system( lDialogString ) ; - - if (!(lIn = fopen(lFilterPatterns, "r"))) - { - remove(lFilterPatterns); - return NULL; - } - while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) - {} - fclose(lIn); - remove(lFilterPatterns); - replaceChr( aoBuff , '/' , '\\' ) ; - /* printf( "aoBuff: %s\n" , aoBuff ) ; */ - return aoBuff; -} - - -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] ; - FILE * lIn ; - - strcpy( lDialogString , "dialog " ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; - - strcat( lDialogString , "--dselect \"" ) ; - if ( aDefaultPath && strlen(aDefaultPath) ) - { - /* dialog.exe uses unix separators even on windows */ - strcpy(lString, aDefaultPath) ; - ensureFinalSlash(lString); - replaceChr( lString , '\\' , '/' ) ; - strcat(lDialogString, lString) ; - } - else - { - /* dialog.exe needs at least one separator */ - strcat(lDialogString, "./") ; - } - strcat(lDialogString, "\" 0 60 2>"); - strcpy(lString, getenv("USERPROFILE")); - strcat(lString, "\\AppData\\Local\\Temp\\tinyfd.txt"); - strcat(lDialogString, lString); - - /* printf( "lDialogString: %s\n" , lDialogString ) ; */ - system( lDialogString ) ; - - if (!(lIn = fopen(lString, "r"))) - { - remove(lString); - return NULL; - } - while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) - {} - fclose(lIn); - remove(lString); - replaceChr( aoBuff , '/' , '\\' ) ; - /* printf( "aoBuff: %s\n" , aoBuff ) ; */ - 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 * 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; - UINT lOriginalCP = 0; - UINT lOriginalOutputCP = 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); - - 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 * aTitle, /* NULL or "" */ - char const * aMessage , /* NULL or "" may contain \n \t */ - char const * aIconType ) /* "info" "warning" "error" */ -{ - 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 ((!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 * 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; - - DWORD mode = 0; - HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); - - 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) - { - gWarningDisplayed = 1 ; - printf("\n\n%s\n", gTitle); - printf("%s\n\n", tinyfd_needs); - } - - if (!tinyfd_winUtf8) - { - lOriginalCP = GetConsoleCP(); - lOriginalOutputCP = GetConsoleOutputCP(); - (void)SetConsoleCP(GetACP()); - (void)SetConsoleOutputCP(GetACP()); - } - - if (aTitle && strlen(aTitle)) - { - printf("\n"); - if (tinyfd_winUtf8) writeUtf8(aTitle); - else printf("%s", aTitle); - printf("\n\n"); - } - if ( aMessage && strlen(aMessage) ) - { - if (tinyfd_winUtf8) writeUtf8(aMessage); - else printf("%s", aMessage); - printf("\n"); - } - printf("(ctrl-Z + enter to cancel): "); - if ( ! aDefaultInput ) - { - (void) GetConsoleMode(hStdin, &mode); - (void) SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); - } - 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 * 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 lString[MAX_PATH_OR_CMD] ; - char * p ; - char * lPointerInputBox; - int i; - - 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 ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) - && (!getenv("SSH_CLIENT") || getenvDISPLAY())) - { - 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 ) ) - { - return NULL; - } - getPathWithoutFinalSlash( lString , p ) ; - if ( strlen( lString ) && ! dirExists( lString ) ) - { - return NULL ; - } - getLastName(lString,p); - if ( ! filenameValid(lString) ) - { - return NULL; - } - return p ; -} - - -/* in case of multiple files, the separator is | */ -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 */ -{ - 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 *)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 ) ) - { - return NULL; - } - if ( aAllowMultipleSelects && strchr(p, '|') ) - { - p = ensureFilesExist( (char *) p , p ) ; - } - else if ( ! fileExists(p) ) - { - return NULL ; - } - /* printf( "lBuff3: %s\n" , p ) ; */ - return p ; -} - - -char * tinyfd_selectFolderDialog( - char const * aTitle , /* NULL or "" */ - char const * aDefaultPath ) /* NULL or "" */ -{ - 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 *)1;} - p = selectFolderDialogWinGui(lBuff, aTitle, aDefaultPath); - } - 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 ; - } - return p ; -} - - -/* returns the hexcolor as a string "#FF0000" */ -/* aoResultRGB also contains the result */ -/* aDefaultRGB is used only if aDefaultHexRGB is NULL */ -/* aDefaultRGB and aoResultRGB can be the same array */ -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 lDefaultHexRGB[16]; - int i; - char * p ; - char * lPointerInputBox; - char lString[MAX_PATH_OR_CMD]; - - lDefaultHexRGB[0] = '\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); - strcpy(lDefaultHexRGB, p); - return lDefaultHexRGB; - } - 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; -} - - -#else /* unix */ - -static char gPython2Name[16]; -static char gPython3Name[16]; -static char gPythonName[16]; - -int tfd_isDarwin(void) -{ - static int lsIsDarwin = -1 ; - struct utsname lUtsname ; - if ( lsIsDarwin < 0 ) - { - lsIsDarwin = !uname(&lUtsname) && !strcmp(lUtsname.sysname,"Darwin") ; - } - return lsIsDarwin ; -} - - -static int dirExists( char const * aDirPath ) -{ - DIR * lDir ; - if ( ! aDirPath || ! strlen( aDirPath ) ) - return 0 ; - lDir = opendir( aDirPath ) ; - if ( ! lDir ) - { - return 0 ; - } - closedir( lDir ) ; - return 1 ; -} - - -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 * getVersion( char const * aExecutable ) /*version must be first numeral*/ -{ - 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 * getMajorMinorPatch( char const * aExecutable ) -{ - static int lArray[3] ; - char * lTmp ; - - lTmp = (char *) getVersion(aExecutable); - lArray[0] = atoi( strtok(lTmp," ,.-") ) ; - /* printf("lArray0 %d\n", lArray[0]); */ - lArray[1] = atoi( strtok(0," ,.-") ) ; - /* printf("lArray1 %d\n", lArray[1]); */ - lArray[2] = atoi( strtok(0," ,.-") ) ; - /* printf("lArray2 %d\n", lArray[2]); */ - - if ( !lArray[0] && !lArray[1] && !lArray[2] ) return NULL; - return lArray ; -} - - -static int tryCommand( char const * aCommand ) -{ - char lBuff[MAX_PATH_OR_CMD] ; - FILE * lIn ; - - lIn = popen( aCommand , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL ) - { /* present */ - pclose( lIn ) ; - return 1 ; - } - else - { - pclose( lIn ) ; - return 0 ; - } - -} - - -static int isTerminalRunning(void) -{ - static int lIsTerminalRunning = -1 ; - if ( lIsTerminalRunning < 0 ) - { - lIsTerminalRunning = isatty(1); - if (tinyfd_verbose) printf("isTerminalRunning %d\n", lIsTerminalRunning ); - } - return lIsTerminalRunning; -} - - -static char * dialogNameOnly(void) -{ - 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(void) -{ - char const * lDialogName ; - char * lVersion ; - int lMajor ; - int lMinor ; - int lDate ; - int lResult ; - char * lMinorP ; - char * lLetter ; - char lBuff[128] ; - - /*char lTest[128] = " 0.9b-20031126" ;*/ - - lDialogName = dialogNameOnly() ; - if ( ! strlen(lDialogName) || !(lVersion = (char *) getVersion(lDialogName)) ) return 0 ; - /*lVersion = lTest ;*/ - /*printf("lVersion %s\n", lVersion);*/ - strcpy(lBuff,lVersion); - lMajor = atoi( strtok(lVersion," ,.-") ) ; - /*printf("lMajor %d\n", lMajor);*/ - lMinorP = strtok(0," ,.-abcdefghijklmnopqrstuvxyz"); - lMinor = atoi( lMinorP ) ; - /*printf("lMinor %d\n", lMinor );*/ - lDate = atoi( strtok(0," ,.-") ) ; - if (lDate<0) lDate = - lDate; - /*printf("lDate %d\n", lDate);*/ - lLetter = lMinorP + strlen(lMinorP) ; - strcpy(lVersion,lBuff); - strtok(lLetter," ,.-"); - /*printf("lLetter %s\n", lLetter);*/ - lResult = (lMajor > 0) || ( ( lMinor == 9 ) && (*lLetter == 'b') && (lDate >= 20031126) ); - /*printf("lResult %d\n", lResult);*/ - return lResult; -} - - -static int whiptailPresentOnly(void) -{ - static int lWhiptailPresent = -1 ; - if (!tinyfd_allowCursesDialogs) return 0; - if ( lWhiptailPresent < 0 ) - { - lWhiptailPresent = detectPresence( "whiptail" ) ; - } - return lWhiptailPresent ; -} - - -static char * terminalName(void) -{ - static char lTerminalName[128] = "*" ; - char lShellName[64] = "*" ; - int * lArray; - - if ( lTerminalName[0] == '*' ) - { - if ( detectPresence( "bash" ) ) - { - strcpy(lShellName , "bash -c " ) ; /*good for basic input*/ - } - else if ( strlen(dialogNameOnly()) || whiptailPresentOnly() ) - { - strcpy(lShellName , "sh -c " ) ; /*good enough for dialog & whiptail*/ - } - else - { - strcpy(lTerminalName , "" ) ; - return NULL ; - } - - if ( tfd_isDarwin() ) - { - if ( * strcpy(lTerminalName , "/opt/X11/bin/xterm" ) - && detectPresence( lTerminalName ) ) - { - strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else - { - strcpy(lTerminalName , "" ) ; - } - } - 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*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -x " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( * strcpy(lTerminalName,"lxterminal") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( * strcpy(lTerminalName,"konsole") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( * strcpy(lTerminalName,"kterm") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( * strcpy(lTerminalName,"tilix") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( * strcpy(lTerminalName,"xfce4-terminal") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -x " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( * strcpy(lTerminalName,"mate-terminal") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -x " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( * strcpy(lTerminalName,"Eterm") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( * strcpy(lTerminalName,"evilvte") /*good*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( * strcpy(lTerminalName,"pterm") /*good (only letters)*/ - && detectPresence(lTerminalName) ) - { - strcat(lTerminalName , " -e " ) ; - strcat(lTerminalName , lShellName ) ; - } - else if ( * strcpy(lTerminalName,"gnome-terminal") - && detectPresence(lTerminalName) && (lArray = getMajorMinorPatch(lTerminalName)) - && ((lArray[0]<3) || (lArray[0]==3 && lArray[1]<=6)) ) - { - strcat(lTerminalName , " --disable-factory -x " ) ; - strcat(lTerminalName , lShellName ) ; - } - else - { - strcpy(lTerminalName , "" ) ; - } - /* bad: koi rxterm guake tilda vala-terminal qterminal - aterm Terminal terminology sakura lilyterm weston-terminal - roxterm termit xvt rxvt mrxvt urxvt */ - } - if ( strlen(lTerminalName) ) - { - return lTerminalName ; - } - else - { - return NULL ; - } -} - - -static char * dialogName(void) -{ - char * lDialogName ; - lDialogName = dialogNameOnly( ) ; - if ( strlen(lDialogName) && ( isTerminalRunning() || terminalName() ) ) - { - return lDialogName ; - } - else - { - return NULL ; - } -} - - -static int whiptailPresent(void) -{ - int lWhiptailPresent ; - lWhiptailPresent = whiptailPresentOnly( ) ; - if ( lWhiptailPresent && ( isTerminalRunning() || terminalName() ) ) - { - return lWhiptailPresent ; - } - else - { - return 0 ; - } -} - - - -static int graphicMode(void) -{ - return !( tinyfd_forceConsole && (isTerminalRunning() || terminalName()) ) - && ( getenvDISPLAY() - || (tfd_isDarwin() && (!getenv("SSH_TTY") || getenvDISPLAY() ) ) ) ; -} - - -static int pactlPresent(void) -{ - static int lPactlPresent = -1 ; - if ( lPactlPresent < 0 ) - { - lPactlPresent = detectPresence("pactl") ; - } - return lPactlPresent ; -} - - -static int speakertestPresent(void) -{ - static int lSpeakertestPresent = -1 ; - if ( lSpeakertestPresent < 0 ) - { - lSpeakertestPresent = detectPresence("speaker-test") ; - } - return lSpeakertestPresent ; -} - - -static int playPresent() -{ - static int lPlayPresent = -1; - if (lPlayPresent < 0) - { - lPlayPresent = detectPresence("sox"); /*if sox is present, play is ready*/ - } - return lPlayPresent; -} - - -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 ) - { - lXmessagePresent = detectPresence("xmessage");/*if not tty,not on osxpath*/ - } - return lXmessagePresent && graphicMode( ) ; -} - - -static int gxmessagePresent(void) -{ - static int lGxmessagePresent = -1 ; - if ( lGxmessagePresent < 0 ) - { - lGxmessagePresent = detectPresence("gxmessage") ; - } - return lGxmessagePresent && graphicMode( ) ; -} - - -static int gmessagePresent(void) -{ - static int lGmessagePresent = -1 ; - if ( lGmessagePresent < 0 ) - { - lGmessagePresent = detectPresence("gmessage") ; - } - return lGmessagePresent && graphicMode( ) ; -} - - -static int notifysendPresent(void) -{ - static int lNotifysendPresent = -1 ; - if ( lNotifysendPresent < 0 ) - { - lNotifysendPresent = detectPresence("notify-send") ; - } - return lNotifysendPresent && graphicMode( ) ; -} - - -static int perlPresent(void) -{ - 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 ; -} - - -static int afplayPresent(void) -{ - static int lAfplayPresent = -1 ; - char lBuff[MAX_PATH_OR_CMD] ; - FILE * lIn ; - - if ( lAfplayPresent < 0 ) - { - lAfplayPresent = detectPresence("afplay") ; - if ( lAfplayPresent ) - { - lIn = popen( "test -e /System/Library/Sounds/Ping.aiff || echo Ping" , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL ) - { - lAfplayPresent = 2 ; - } - pclose( lIn ) ; - if (tinyfd_verbose) printf("afplay %d\n", lAfplayPresent); - } - } - return graphicMode() ? lAfplayPresent : 0 ; -} - - -static int xdialogPresent(void) -{ - static int lXdialogPresent = -1 ; - if ( lXdialogPresent < 0 ) - { - lXdialogPresent = detectPresence("Xdialog") ; - } - return lXdialogPresent && graphicMode( ) ; -} - - -static int gdialogPresent(void) -{ - static int lGdialoglPresent = -1 ; - if ( lGdialoglPresent < 0 ) - { - lGdialoglPresent = detectPresence( "gdialog" ) ; - } - return lGdialoglPresent && graphicMode( ) ; -} - - -static int osascriptPresent(void) -{ - static int lOsascriptPresent = -1 ; - if ( lOsascriptPresent < 0 ) - { - gWarningDisplayed |= !!getenv("SSH_TTY"); - lOsascriptPresent = detectPresence( "osascript" ) ; - } - return lOsascriptPresent && graphicMode() && !getenv("SSH_TTY") ; -} - - -int tfd_qarmaPresent(void) -{ - static int lQarmaPresent = -1 ; - if ( lQarmaPresent < 0 ) - { - lQarmaPresent = detectPresence("qarma") ; - } - return lQarmaPresent && graphicMode( ) ; -} - - -int tfd_matedialogPresent(void) -{ - static int lMatedialogPresent = -1 ; - if ( lMatedialogPresent < 0 ) - { - lMatedialogPresent = detectPresence("matedialog") ; - } - return lMatedialogPresent && graphicMode( ) ; -} - - -int tfd_shellementaryPresent(void) -{ - static int lShellementaryPresent = -1 ; - if ( lShellementaryPresent < 0 ) - { - lShellementaryPresent = 0 ; /*detectPresence("shellementary"); shellementary is not ready yet */ - } - return lShellementaryPresent && graphicMode( ) ; -} - - -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 ) - { - lZenityPresent = detectPresence("zenity") ; - } - return lZenityPresent && graphicMode( ) ; -} - - -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] ; - FILE * lIn ; - int lIntTmp ; - - if ( lZenity3Present < 0 ) - { - lZenity3Present = 0 ; - if ( tfd_zenityPresent() ) - { - lIn = popen( "zenity --version" , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - { - if ( atoi(lBuff) >= 3 ) - { - lZenity3Present = 3 ; - lIntTmp = atoi(strtok(lBuff,".")+2 ) ; - if ( lIntTmp >= 18 ) - { - lZenity3Present = 5 ; - } - else if ( lIntTmp >= 10 ) - { - lZenity3Present = 4 ; - } - } - else if ( ( atoi(lBuff) == 2 ) && ( atoi(strtok(lBuff,".")+2 ) >= 32 ) ) - { - lZenity3Present = 2 ; - } - if (tinyfd_verbose) printf("zenity type %d\n", lZenity3Present); - } - pclose( lIn ) ; - } - } - return graphicMode() ? lZenity3Present : 0 ; -} - - -int tfd_kdialogPresent(void) -{ - static int lKdialogPresent = -1 ; - char lBuff[MAX_PATH_OR_CMD] ; - FILE * lIn ; - char * lDesktop; - - if ( lKdialogPresent < 0 ) - { - if ( tfd_zenityPresent() ) - { - lDesktop = getenv("XDG_SESSION_DESKTOP"); - if ( !lDesktop || ( strcmp(lDesktop, "KDE") && strcmp(lDesktop, "lxqt") ) ) - { - lKdialogPresent = 0 ; - return lKdialogPresent ; - } - } - - lKdialogPresent = detectPresence("kdialog") ; - if ( lKdialogPresent && !getenv("SSH_TTY") ) - { - lIn = popen( "kdialog --attach 2>&1" , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - { - if ( ! strstr( "Unknown" , lBuff ) ) - { - lKdialogPresent = 2 ; - if (tinyfd_verbose) printf("kdialog-attach %d\n", lKdialogPresent); - } - } - pclose( lIn ) ; - - if (lKdialogPresent == 2) - { - lKdialogPresent = 1 ; - lIn = popen( "kdialog --passivepopup 2>&1" , "r" ) ; - if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - { - if ( ! strstr( "Unknown" , lBuff ) ) - { - lKdialogPresent = 2 ; - if (tinyfd_verbose) printf("kdialog-popup %d\n", lKdialogPresent); - } - } - pclose( lIn ) ; - } - } - } - return graphicMode() ? lKdialogPresent : 0 ; -} - - -static int osx9orBetter(void) -{ - static int lOsx9orBetter = -1 ; - char lBuff[MAX_PATH_OR_CMD] ; - FILE * lIn ; - int V,v; - - if ( lOsx9orBetter < 0 ) - { - lOsx9orBetter = 0 ; - lIn = popen( "osascript -e 'set osver to system version of (system info)'" , "r" ) ; - if ( ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - && ( 2 == sscanf(lBuff, "%d.%d", &V, &v) ) ) - { - V = V * 100 + v; - if ( V >= 1009 ) - { - lOsx9orBetter = 1 ; - } - } - pclose( lIn ) ; - if (tinyfd_verbose) printf("Osx10 = %d, %d = %s\n", lOsx9orBetter, V, lBuff) ; - } - return lOsx9orBetter ; -} - - -static int python3Present(void) -{ - static int lPython3Present = -1 ; - int i; - - if ( lPython3Present < 0 ) - { - lPython3Present = 0 ; - strcpy(gPython3Name , "python3" ) ; - if ( detectPresence(gPython3Name) ) lPython3Present = 1; - else - { - for ( i = 9 ; i >= 0 ; i -- ) - { - sprintf( gPython3Name , "python3.%d" , i ) ; - if ( detectPresence(gPython3Name) ) - { - lPython3Present = 1; - break; - } - } - } - if (tinyfd_verbose) printf("lPython3Present %d\n", lPython3Present) ; - if (tinyfd_verbose) printf("gPython3Name %s\n", gPython3Name) ; - } - return lPython3Present ; -} - - -static int python2Present(void) -{ - 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 (tinyfd_verbose) printf("lPython2Present %d\n", lPython2Present) ; - if (tinyfd_verbose) printf("gPython2Name %s\n", gPython2Name) ; - } - return lPython2Present ; -} - - -static int tkinter3Present(void) -{ - static int lTkinter3Present = -1 ; - char lPythonCommand[256]; - char lPythonParams[128] = - "-S -c \"try:\n\timport tkinter;\nexcept:\n\tprint(0);\""; - - if ( lTkinter3Present < 0 ) - { - lTkinter3Present = 0 ; - if ( python3Present() ) - { - sprintf( lPythonCommand , "%s %s" , gPython3Name , lPythonParams ) ; - lTkinter3Present = tryCommand(lPythonCommand) ; - } - if (tinyfd_verbose) printf("lTkinter3Present %d\n", lTkinter3Present) ; - } - return lTkinter3Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") ); -} - - -static int tkinter2Present(void) -{ - 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 lDbusPresent = -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 ) - { - lDbusPresent = 0 ; - if ( python2Present() ) - { - strcpy(gPythonName , gPython2Name ) ; - sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ; - lDbusPresent = tryCommand(lPythonCommand) ; - } - - if ( ! lDbusPresent && python3Present() ) - { - strcpy(gPythonName , gPython3Name ) ; - sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ; - lDbusPresent = tryCommand(lPythonCommand) ; - } - - if (tinyfd_verbose) printf("lDbusPresent %d\n", lDbusPresent) ; - if (tinyfd_verbose) printf("gPythonName %s\n", gPythonName) ; - } - return lDbusPresent && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") ); -} - - -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) -{ - char lDialogString[256] ; - FILE * lIn ; - - if ( osascriptPresent() ) - { - if ( afplayPresent() >= 2 ) - { - strcpy( lDialogString , "afplay /System/Library/Sounds/Ping.aiff") ; - } - else - { - strcpy( lDialogString , "osascript -e 'tell application \"System Events\" to beep'") ; - } - } - 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() ) - { - /*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" ) ; - } - else if (beepexePresent()) - { - 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 - { - strcpy( lDialogString , "printf '\a' > /dev/tty" ) ; - } - - if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - - if ( ( lIn = popen( lDialogString , "r" ) ) ) - { - pclose( lIn ) ; - } - - if ( pactlPresent() ) - { - signal(SIGINT, SIG_DFL); - } -} - - -int tinyfd_messageBox( - 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 * lDialogString = NULL ; - char * lpDialogString; - FILE * lIn ; - int lWasGraphicDialog = 0 ; - int lWasXterm = 0 ; - int lResult ; - char lChar ; - struct termios infoOri; - struct termios info; - size_t lTitleLen ; - size_t lMessageLen ; - - 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") ) - { - lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); - } - - if ( osascriptPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;} - - strcpy( lDialogString , "osascript "); - if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); - strcat( lDialogString , " -e 'try' -e 'set {vButton} to {button returned} of ( display dialog \"") ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat(lDialogString, "\" ") ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - strcat(lDialogString, "with icon ") ; - if ( aIconType && ! strcmp( "error" , aIconType ) ) - { - strcat(lDialogString, "stop " ) ; - } - else if ( aIconType && ! strcmp( "warning" , aIconType ) ) - { - strcat(lDialogString, "caution " ) ; - } - else /* question or info */ - { - strcat(lDialogString, "note " ) ; - } - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - if ( ! aDefaultButton ) - { - strcat( lDialogString ,"default button \"Cancel\" " ) ; - } - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - strcat( lDialogString ,"buttons {\"No\", \"Yes\"} " ) ; - if (aDefaultButton) - { - strcat( lDialogString ,"default button \"Yes\" " ) ; - } - else - { - strcat( lDialogString ,"default button \"No\" " ) ; - } - strcat( lDialogString ,"cancel button \"No\"" ) ; - } - else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString ,"buttons {\"No\", \"Yes\", \"Cancel\"} " ) ; - switch (aDefaultButton) - { - case 1: strcat( lDialogString ,"default button \"Yes\" " ) ; break; - case 2: strcat( lDialogString ,"default button \"No\" " ) ; break; - case 0: strcat( lDialogString ,"default button \"Cancel\" " ) ; break; - } - strcat( lDialogString ,"cancel button \"Cancel\"" ) ; - } - else - { - strcat( lDialogString ,"buttons {\"OK\"} " ) ; - strcat( lDialogString ,"default button \"OK\" " ) ; - } - strcat( lDialogString, ")' ") ; - - strcat( lDialogString, -"-e 'if vButton is \"Yes\" then' -e 'return 1'\ - -e 'else if vButton is \"OK\" then' -e 'return 1'\ - -e 'else if vButton is \"No\" then' -e 'return 2'\ - -e 'else' -e 'return 0' -e 'end if' " ); - - strcat( lDialogString, "-e 'on error number -128' " ) ; - strcat( lDialogString, "-e '0' " ); - - strcat( lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; - } - else if ( tfd_kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} - - strcpy( lDialogString , "kdialog" ) ; - if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) - { - strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ - } - - strcat( lDialogString , " --" ) ; - if ( aDialogType && ( ! strcmp( "okcancel" , aDialogType ) - || ! strcmp( "yesno" , aDialogType ) || ! strcmp( "yesnocancel" , aDialogType ) ) ) - { - if ( aIconType && ( ! strcmp( "warning" , aIconType ) - || ! strcmp( "error" , aIconType ) ) ) - { - strcat( lDialogString , "warning" ) ; - } - if ( ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , "yesnocancel" ) ; - } - else - { - strcat( lDialogString , "yesno" ) ; - } - } - else if ( aIconType && ! strcmp( "error" , aIconType ) ) - { - strcat( lDialogString , "error" ) ; - } - else if ( aIconType && ! strcmp( "warning" , aIconType ) ) - { - strcat( lDialogString , "sorry" ) ; - } - else - { - strcat( lDialogString , "msgbox" ) ; - } - strcat( lDialogString , " \"" ) ; - if ( aMessage ) - { - strcat( lDialogString , aMessage ) ; - } - strcat( lDialogString , "\"" ) ; - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - strcat( lDialogString , - " --yes-label Ok --no-label Cancel" ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - - if ( ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , "; x=$? ;if [ $x = 0 ] ;then echo 1;elif [ $x = 1 ] ;then echo 2;else echo 0;fi"); - } - else - { - strcat( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi"); - } - } - else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) - { - if ( tfd_zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;} - strcpy( lDialogString , "szAnswer=$(zenity" ) ; - 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 ( tfd_matedialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return 1;} - strcpy( lDialogString , "szAnswer=$(matedialog" ) ; - } - else if ( tfd_shellementaryPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return 1;} - strcpy( lDialogString , "szAnswer=$(shellementary" ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return 1;} - strcpy( lDialogString , "szAnswer=$(qarma" ) ; - 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, " --"); - - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - strcat( lDialogString , - "question --ok-label=Ok --cancel-label=Cancel" ) ; - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - strcat( lDialogString , "question" ) ; - } - else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , "list --column \"\" --hide-header \"Yes\" \"No\"" ) ; - } - 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) ) - { - if (strcmp("yesnocancel", aDialogType)) strcat(lDialogString, " --no-wrap"); - strcat(lDialogString, " --text=\"") ; - 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 ) - || ! strcmp( "error" , aIconType ) - || ! strcmp( "warning" , aIconType ) ) ) - { - strcat( lDialogString , aIconType ) ; - } - else - { - strcat( lDialogString , "information" ) ; - } - } - - if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null "); - - if ( ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , -");if [ $? = 1 ];then echo 0;elif [ $szAnswer = \"No\" ];then echo 2;else echo 1;fi"); - } - else - { - strcat( lDialogString , ");if [ $? = 0 ];then echo 1;else echo 0;fi"); - } - } - - 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 , "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 ( 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=tkMessageBox." ) ; - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - strcat( lDialogString , "askokcancel(" ) ; - if ( aDefaultButton ) - { - strcat( lDialogString , "default=tkMessageBox.OK," ) ; - } - else - { - strcat( lDialogString , "default=tkMessageBox.CANCEL," ) ; - } - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - strcat( lDialogString , "askyesno(" ) ; - if ( aDefaultButton ) - { - strcat( lDialogString , "default=tkMessageBox.YES," ) ; - } - else - { - strcat( lDialogString , "default=tkMessageBox.NO," ) ; - } - } - else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) - { - strcat( lDialogString , "askyesnocancel(" ) ; - switch ( aDefaultButton ) - { - case 1: strcat( lDialogString , "default=tkMessageBox.YES," ); break; - case 2: strcat( lDialogString , "default=tkMessageBox.NO," ); break; - case 0: strcat( lDialogString , "default=tkMessageBox.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() && xmessagePresent()) ) - { - if ( gxmessagePresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return 1;} - strcpy( lDialogString , "gxmessage"); - } - else if ( gmessagePresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return 1;} - strcpy( lDialogString , "gmessage"); - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xmessage");return 1;} - strcpy( lDialogString , "xmessage"); - } - - if ( aDialogType && ! strcmp("okcancel" , aDialogType) ) - { - strcat( lDialogString , " -buttons Ok:1,Cancel:0"); - switch ( aDefaultButton ) - { - case 1: strcat( lDialogString , " -default Ok"); break; - case 0: strcat( lDialogString , " -default Cancel"); break; - } - } - else if ( aDialogType && ! strcmp("yesno" , aDialogType) ) - { - strcat( lDialogString , " -buttons Yes:1,No:0"); - switch ( aDefaultButton ) - { - case 1: strcat( lDialogString , " -default Yes"); break; - case 0: strcat( lDialogString , " -default No"); break; - } - } - else if ( aDialogType && ! strcmp("yesnocancel" , aDialogType) ) - { - strcat( lDialogString , " -buttons Yes:1,No:2,Cancel:0"); - switch ( aDefaultButton ) - { - case 1: strcat( lDialogString , " -default Yes"); break; - case 2: strcat( lDialogString , " -default No"); break; - case 0: strcat( lDialogString , " -default Cancel"); break; - } - } - else - { - strcat( lDialogString , " -buttons Ok:1"); - strcat( lDialogString , " -default Ok"); - } - - strcat( lDialogString , " -center \""); - if ( aMessage && strlen(aMessage) ) - { - strcat( lDialogString , aMessage ) ; - } - strcat(lDialogString, "\"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat( lDialogString , " -title \""); - strcat( lDialogString , aTitle ) ; - strcat( lDialogString, "\"" ) ; - } - strcat( lDialogString , " ; echo $? "); - } - else if ( xdialogPresent() || gdialogPresent() || dialogName() || whiptailPresent() ) - { - if ( gdialogPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return 1;} - lWasGraphicDialog = 1 ; - strcpy( lDialogString , "(gdialog " ) ; - } - else if ( xdialogPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return 1;} - lWasGraphicDialog = 1 ; - strcpy( lDialogString , "(Xdialog " ) ; - } - else if ( dialogName( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return 0;} - if ( isTerminalRunning( ) ) - { - strcpy( lDialogString , "(dialog " ) ; - } - else - { - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(" ) ; - strcat( lDialogString , dialogName() ) ; - strcat( lDialogString , " " ) ; - } - } - else if ( isTerminalRunning( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;} - strcpy( lDialogString , "(whiptail " ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;} - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(whiptail " ) ; - } - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - if ( !xdialogPresent() && !gdialogPresent() ) - { - if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) - || !strcmp( "yesnocancel" , aDialogType ) ) ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, "tab: move focus") ; - strcat(lDialogString, "\" ") ; - } - } - - if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) - { - if ( ! aDefaultButton ) - { - strcat( lDialogString , "--defaultno " ) ; - } - strcat( lDialogString , - "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ; - } - else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) - { - if ( ! aDefaultButton ) - { - strcat( lDialogString , "--defaultno " ) ; - } - strcat( lDialogString , "--yesno " ) ; - } - else if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - if (!aDefaultButton) - { - strcat(lDialogString, "--defaultno "); - } - strcat(lDialogString, "--menu "); - } - else - { - strcat( lDialogString , "--msgbox " ) ; - - } - strcat( lDialogString , "\"" ) ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat(lDialogString, "\" "); - - if ( lWasGraphicDialog ) - { - if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - strcat(lDialogString,"0 60 0 Yes \"\" No \"\") 2>/tmp/tinyfd.txt;\ -if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ -tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; - } - else - { - strcat(lDialogString, - "10 60 ) 2>&1;if [ $? = 0 ];then echo 1;else echo 0;fi"); - } - } - else - { - if (aDialogType && !strcmp("yesnocancel", aDialogType)) - { - strcat(lDialogString,"0 60 0 Yes \"\" No \"\" >/dev/tty ) 2>/tmp/tinyfd.txt;\ - if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ - tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; - - if ( lWasXterm ) - { - strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt"); - } - else - { - strcat(lDialogString, "; clear >/dev/tty") ; - } - } - else - { - strcat(lDialogString, "10 60 >/dev/tty) 2>&1;if [ $? = 0 ];"); - if ( lWasXterm ) - { - strcat( lDialogString , -"then\n\techo 1\nelse\n\techo 0\nfi >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); - } - else - { - strcat(lDialogString, - "then echo 1;else echo 0;fi;clear >/dev/tty"); - } - } - } - } - else if ( !isTerminalRunning() && terminalName() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;} - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'" ) ; - if ( !gWarningDisplayed && !tinyfd_forceConsole) - { - gWarningDisplayed = 1 ; - strcat( lDialogString , "echo \"" ) ; - strcat( lDialogString, gTitle) ; - strcat( lDialogString , "\";" ) ; - strcat( lDialogString , "echo \"" ) ; - strcat( lDialogString, tinyfd_needs) ; - strcat( lDialogString , "\";echo;echo;" ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat( lDialogString , "echo \"" ) ; - strcat( lDialogString, aTitle) ; - strcat( lDialogString , "\";echo;" ) ; - } - if ( aMessage && strlen(aMessage) ) - { - strcat( lDialogString , "echo \"" ) ; - strcat( lDialogString, aMessage) ; - strcat( lDialogString , "\"; " ) ; - } - if ( aDialogType && !strcmp("yesno",aDialogType) ) - { - strcat( lDialogString , "echo -n \"y/n: \"; " ) ; - strcat( lDialogString , "stty sane -echo;" ) ; - strcat( lDialogString , - "answer=$( while ! head -c 1 | grep -i [ny];do true ;done);"); - strcat( lDialogString , - "if echo \"$answer\" | grep -iq \"^y\";then\n"); - strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ; - } - else if ( aDialogType && !strcmp("okcancel",aDialogType) ) - { - strcat( lDialogString , "echo -n \"[O]kay/[C]ancel: \"; " ) ; - strcat( lDialogString , "stty sane -echo;" ) ; - strcat( lDialogString , - "answer=$( while ! head -c 1 | grep -i [oc];do true ;done);"); - strcat( lDialogString , - "if echo \"$answer\" | grep -iq \"^o\";then\n"); - strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ; - } - else if ( aDialogType && !strcmp("yesnocancel",aDialogType) ) - { - strcat( lDialogString , "echo -n \"[Y]es/[N]o/[C]ancel: \"; " ) ; - strcat( lDialogString , "stty sane -echo;" ) ; - strcat( lDialogString , - "answer=$( while ! head -c 1 | grep -i [nyc];do true ;done);"); - strcat( lDialogString , - "if echo \"$answer\" | grep -iq \"^y\";then\n\techo 1\n"); - strcat( lDialogString , "elif echo \"$answer\" | grep -iq \"^n\";then\n\techo 2\n" ) ; - strcat( lDialogString , "else\n\techo 0\nfi" ) ; - } - else - { - strcat(lDialogString , "echo -n \"press enter to continue \"; "); - strcat( lDialogString , "stty sane -echo;" ) ; - strcat( lDialogString , - "answer=$( while ! head -c 1;do true ;done);echo 1"); - } - strcat( lDialogString , - " >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); - } - else if ( !isTerminalRunning() && pythonDbusPresent() && !strcmp("ok" , aDialogType) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python-dbus");return 1;} - strcpy( lDialogString , gPythonName ) ; - strcat( lDialogString ," -c \"import dbus;bus=dbus.SessionBus();"); - strcat( lDialogString ,"notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');" ) ; - strcat( lDialogString ,"notify=dbus.Interface(notif,'org.freedesktop.Notifications');" ) ; - strcat( lDialogString ,"notify.Notify('',0,'" ) ; - if ( aIconType && strlen(aIconType) ) - { - strcat( lDialogString , aIconType ) ; - } - strcat(lDialogString, "','") ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, aTitle) ; - } - strcat(lDialogString, "','") ; - if ( aMessage && strlen(aMessage) ) - { - lpDialogString = lDialogString + strlen(lDialogString); - tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; - } - strcat(lDialogString, "','','',5000)\"") ; - } - else if ( !isTerminalRunning() && (perlPresent() >= 2) && !strcmp("ok" , aDialogType) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;} - - 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) ) - { - - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notifysend");return 1;} - strcpy( lDialogString , "notify-send" ) ; - if ( aIconType && strlen(aIconType) ) - { - strcat( lDialogString , " -i '" ) ; - strcat( lDialogString , aIconType ) ; - strcat( lDialogString , "'" ) ; - } - strcat( lDialogString , " \"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, aTitle) ; - strcat( lDialogString , " | " ) ; - } - if ( aMessage && strlen(aMessage) ) - { - tfd_replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ; - tfd_replaceSubStr( aMessage , "\n" , " | " , lBuff ) ; - tfd_replaceSubStr( aMessage , "\t" , " " , lBuff ) ; - strcat(lDialogString, lBuff) ; - } - strcat( lDialogString , "\"" ) ; - } - 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", aTitle); - } - - tcgetattr(0, &infoOri); - tcgetattr(0, &info); - info.c_lflag &= ~ICANON; - info.c_cc[VMIN] = 1; - info.c_cc[VTIME] = 0; - tcsetattr(0, TCSANOW, &info); - if ( aDialogType && !strcmp("yesno",aDialogType) ) - { - do - { - if ( aMessage && strlen(aMessage) ) - { - printf("\n%s\n",aMessage); - } - printf("y/n: "); fflush(stdout); - lChar = tolower( getchar() ) ; - printf("\n\n"); - } - while ( lChar != 'y' && lChar != 'n' ); - lResult = lChar == 'y' ? 1 : 0 ; - } - else if ( aDialogType && !strcmp("okcancel",aDialogType) ) - { - do - { - if ( aMessage && strlen(aMessage) ) - { - printf("\n%s\n",aMessage); - } - printf("[O]kay/[C]ancel: "); fflush(stdout); - lChar = tolower( getchar() ) ; - printf("\n\n"); - } - while ( lChar != 'o' && lChar != 'c' ); - lResult = lChar == 'o' ? 1 : 0 ; - } - else if ( aDialogType && !strcmp("yesnocancel",aDialogType) ) - { - do - { - if ( aMessage && strlen(aMessage) ) - { - printf("\n%s\n",aMessage); - } - printf("[Y]es/[N]o/[C]ancel: "); fflush(stdout); - lChar = tolower( getchar() ) ; - printf("\n\n"); - } - while ( lChar != 'y' && lChar != 'n' && lChar != 'c' ); - lResult = (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0 ; - } - else - { - if ( aMessage && strlen(aMessage) ) - { - printf("\n%s\n\n",aMessage); - } - printf("press enter to continue "); fflush(stdout); - getchar() ; - printf("\n\n"); - lResult = 1 ; - } - tcsetattr(0, TCSANOW, &infoOri); - free(lDialogString); - return lResult ; - } - - if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - - if ( ! ( lIn = popen( lDialogString , "r" ) ) ) - { - free(lDialogString); - return 0 ; - } - while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - {} - - pclose( lIn ) ; - - /* printf( "lBuff: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ - - 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"); - } - } - /* printf( "lBuff2: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ - - lResult = !strcmp( lBuff , "2" ) ? 2 : !strcmp( lBuff , "1" ) ? 1 : 0; - - /* printf( "lResult: %d\n" , lResult ) ; */ - free(lDialogString); - return lResult ; -} - - -/* return has only meaning for tinyfd_query */ -int tinyfd_notifyPopup( - 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 ; - char * lpDialogString ; - FILE * lIn ; - 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); - } - - lTitleLen = aTitle ? strlen(aTitle) : 0 ; - lMessageLen = aMessage ? strlen(aMessage) : 0 ; - if ( !aTitle || strcmp(aTitle,"tinyfd_query") ) - { - lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); - } - - if ( osascriptPresent( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;} - - strcpy( lDialogString , "osascript "); - if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); - strcat( lDialogString , " -e 'try' -e 'display notification \"") ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat(lDialogString, " \" ") ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - strcat( lDialogString, "' -e 'end try'") ; - if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; - } - else if ( tfd_kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} - strcpy( lDialogString , "kdialog" ) ; - - if ( aIconType && strlen(aIconType) ) - { - strcat( lDialogString , " --icon '" ) ; - strcat( lDialogString , aIconType ) ; - strcat( lDialogString , "'" ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat( lDialogString , " --title \"" ) ; - strcat( lDialogString , aTitle ) ; - strcat( lDialogString , "\"" ) ; - } - - strcat( lDialogString , " --passivepopup" ) ; - strcat( lDialogString , " \"" ) ; - if ( aMessage ) - { - strcat( lDialogString , aMessage ) ; - } - strcat( lDialogString , " \" 5" ) ; - } - 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 ( tfd_zenity3Present()>=5 ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;} - strcpy( lDialogString , "zenity" ) ; - } - - strcat( lDialogString , " --notification"); - - if ( aIconType && strlen( aIconType ) ) - { - strcat( lDialogString , " --window-icon '"); - strcat( lDialogString , aIconType ) ; - strcat( lDialogString , "'" ) ; - } - - strcat( lDialogString , " --text \"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\n") ; - } - if ( aMessage && strlen( aMessage ) ) - { - strcat( lDialogString , aMessage ) ; - } - strcat( lDialogString , " \"" ) ; - } - else if ( perlPresent() >= 2 ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;} - - 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( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python-dbus");return 1;} - strcpy( lDialogString , gPythonName ) ; - strcat( lDialogString ," -c \"import dbus;bus=dbus.SessionBus();"); - strcat( lDialogString ,"notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');" ) ; - strcat( lDialogString ,"notify=dbus.Interface(notif,'org.freedesktop.Notifications');" ) ; - strcat( lDialogString ,"notify.Notify('',0,'" ) ; - if ( aIconType && strlen(aIconType) ) - { - strcat( lDialogString , aIconType ) ; - } - strcat(lDialogString, "','") ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, aTitle) ; - } - strcat(lDialogString, "','") ; - if ( aMessage && strlen(aMessage) ) - { - lpDialogString = lDialogString + strlen(lDialogString); - tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; - } - strcat(lDialogString, "','','',5000)\"") ; - } - else if ( notifysendPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notifysend");return 1;} - strcpy( lDialogString , "notify-send" ) ; - if ( aIconType && strlen(aIconType) ) - { - strcat( lDialogString , " -i '" ) ; - strcat( lDialogString , aIconType ) ; - strcat( lDialogString , "'" ) ; - } - strcat( lDialogString , " \"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, aTitle) ; - strcat( lDialogString , " | " ) ; - } - if ( aMessage && strlen(aMessage) ) - { - tfd_replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ; - tfd_replaceSubStr( aMessage , "\n" , " | " , lBuff ) ; - tfd_replaceSubStr( aMessage , "\t" , " " , lBuff ) ; - strcat(lDialogString, lBuff) ; - } - strcat( lDialogString , "\"" ) ; - } - else - { - return tinyfd_messageBox(aTitle, aMessage, "ok", aIconType, 0); - } - - if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - - if ( ! ( lIn = popen( lDialogString , "r" ) ) ) - { - free(lDialogString); - return 0 ; - } - - pclose( lIn ) ; - free(lDialogString); - return 1; -} - - -/* returns NULL on cancel */ -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; - char * lpDialogString; - FILE * lIn ; - int lResult ; - int lWasGdialog = 0 ; - int lWasGraphicDialog = 0 ; - int lWasXterm = 0 ; - int lWasBasicXterm = 0 ; - struct termios oldt ; - struct termios newt ; - char * lEOF; - 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") ) - { - lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); - } - - if ( osascriptPresent( ) ) - { - 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 \"") ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat(lDialogString, "\" ") ; - strcat(lDialogString, "default answer \"") ; - if ( aDefaultInput && strlen(aDefaultInput) ) - { - strcat(lDialogString, aDefaultInput) ; - } - strcat(lDialogString, "\" ") ; - if ( ! aDefaultInput ) - { - strcat(lDialogString, "hidden answer true ") ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - strcat(lDialogString, "with icon note' ") ; - strcat(lDialogString, "-e '\"1\" & text returned of result' " ); - strcat(lDialogString, "-e 'on error number -128' " ) ; - strcat(lDialogString, "-e '0' " ); - strcat(lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat(lDialogString, " -e 'end tell'") ; - } - else if ( tfd_kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} - strcpy( lDialogString , "szAnswer=$(kdialog" ) ; - - if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) - { - strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ - } - - if ( ! aDefaultInput ) - { - strcat(lDialogString, " --password ") ; - } - else - { - strcat(lDialogString, " --inputbox ") ; - - } - strcat(lDialogString, "\"") ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage ) ; - } - strcat(lDialogString , "\" \"" ) ; - if ( aDefaultInput && strlen(aDefaultInput) ) - { - strcat(lDialogString, aDefaultInput ) ; - } - strcat(lDialogString , "\"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - strcat( lDialogString , - ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi"); - } - else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) - { - if ( tfd_zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} - strcpy( lDialogString , "szAnswer=$(zenity" ) ; - 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 ( tfd_matedialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} - strcpy( lDialogString , "szAnswer=$(matedialog" ) ; - } - else if ( tfd_shellementaryPresent() ) - { - 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 *)1;} - strcpy( lDialogString , "szAnswer=$(qarma" ) ; - 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, "\"") ; - } - 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 (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 *)1;} - strcpy( lDialogString , "szAnswer=$(gxmessage -buttons Ok:1,Cancel:0 -center \""); - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return (char *)1;} - strcpy( lDialogString , "szAnswer=$(gmessage -buttons Ok:1,Cancel:0 -center \""); - } - - if ( aMessage && strlen(aMessage) ) - { - strcat( lDialogString , aMessage ) ; - } - strcat(lDialogString, "\"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat( lDialogString , " -title \""); - strcat( lDialogString , aTitle ) ; - strcat(lDialogString, "\" " ) ; - } - strcat(lDialogString, " -entrytext \"" ) ; - if ( aDefaultInput && strlen(aDefaultInput) ) - { - strcat( lDialogString , aDefaultInput ) ; - } - strcat(lDialogString, "\"" ) ; - strcat( lDialogString , ");echo $?$szAnswer"); - } - 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 *)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 ( 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) ) - { - 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() || dialogName() || whiptailPresent() ) - { - if ( gdialogPresent( ) ) - { - 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 *)1;} - lWasGraphicDialog = 1 ; - strcpy( lDialogString , "(Xdialog " ) ; - } - else if ( dialogName( ) ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} - if ( isTerminalRunning( ) ) - { - strcpy( lDialogString , "(dialog " ) ; - } - else - { - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(" ) ; - strcat( lDialogString , dialogName() ) ; - strcat( lDialogString , " " ) ; - } - } - else if ( isTerminalRunning( ) ) - { - 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 *)0;} - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(whiptail " ) ; - } - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - if ( !xdialogPresent() && !gdialogPresent() ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, "tab: move focus") ; - if ( ! aDefaultInput && !lWasGdialog ) - { - strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ; - } - strcat(lDialogString, "\" ") ; - } - - if ( aDefaultInput || lWasGdialog ) - { - strcat( lDialogString , "--inputbox" ) ; - } - else - { - if ( !lWasGraphicDialog && dialogName() && isDialogVersionBetter09b() ) - { - strcat( lDialogString , "--insecure " ) ; - } - strcat( lDialogString , "--passwordbox" ) ; - } - strcat( lDialogString , " \"" ) ; - if ( aMessage && strlen(aMessage) ) - { - strcat(lDialogString, aMessage) ; - } - strcat(lDialogString,"\" 10 60 ") ; - if ( aDefaultInput && strlen(aDefaultInput) ) - { - strcat(lDialogString, "\"") ; - strcat(lDialogString, aDefaultInput) ; - strcat(lDialogString, "\" ") ; - } - if ( lWasGraphicDialog ) - { - strcat(lDialogString,") 2>/tmp/tinyfd.txt;\ - if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ - tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; - } - else - { - strcat(lDialogString,">/dev/tty ) 2>/tmp/tinyfd.txt;\ - if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ - tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; - - if ( lWasXterm ) - { - strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt"); - } - else - { - strcat(lDialogString, "; clear >/dev/tty") ; - } - } - } - else if ( ! isTerminalRunning( ) && terminalName() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;} - lWasBasicXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'" ) ; - if ( !gWarningDisplayed && !tinyfd_forceConsole) - { - gWarningDisplayed = 1 ; - tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); - } - if ( aTitle && strlen(aTitle) && !tinyfd_forceConsole) - { - strcat( lDialogString , "echo \"" ) ; - strcat( lDialogString, aTitle) ; - strcat( lDialogString , "\";echo;" ) ; - } - - strcat( lDialogString , "echo \"" ) ; - if ( aMessage && strlen(aMessage) ) - { - strcat( lDialogString, aMessage) ; - } - strcat( lDialogString , "\";read " ) ; - if ( ! aDefaultInput ) - { - strcat( lDialogString , "-s " ) ; - } - strcat( lDialogString , "-p \"" ) ; - strcat( lDialogString , "(esc+enter to cancel): \" ANSWER " ) ; - strcat( lDialogString , ";echo 1$ANSWER >/tmp/tinyfd.txt';" ) ; - strcat( lDialogString , "cat -v /tmp/tinyfd.txt"); - } - 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 *)0;} - free(lDialogString); - return NULL; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;} - if ( !gWarningDisplayed && !tinyfd_forceConsole) - { - gWarningDisplayed = 1 ; - tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); - } - if ( aTitle && strlen(aTitle) ) - { - printf("\n%s\n", aTitle); - } - if ( aMessage && strlen(aMessage) ) - { - printf("\n%s\n",aMessage); - } - printf("(esc+enter to cancel): "); fflush(stdout); - if ( ! aDefaultInput ) - { - tcgetattr(STDIN_FILENO, & oldt) ; - newt = oldt ; - newt.c_lflag &= ~ECHO ; - tcsetattr(STDIN_FILENO, TCSANOW, & newt); - } - - lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); - /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */ - if ( ! lEOF || (lBuff[0] == '\0') ) - { - free(lDialogString); - return NULL; - } - - if ( lBuff[0] == '\n' ) - { - lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); - /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */ - if ( ! lEOF || (lBuff[0] == '\0') ) - { - free(lDialogString); - return NULL; - } - } - - if ( ! aDefaultInput ) - { - tcsetattr(STDIN_FILENO, TCSANOW, & oldt); - printf("\n"); - } - printf("\n"); - if ( strchr(lBuff,27) ) - { - free(lDialogString); - return NULL ; - } - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - free(lDialogString); - return lBuff ; - } - - if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - lIn = popen( lDialogString , "r" ); - if ( ! lIn ) - { - if ( fileExists("/tmp/tinyfd.txt") ) - { - wipefile("/tmp/tinyfd.txt"); - remove("/tmp/tinyfd.txt"); - } - if ( fileExists("/tmp/tinyfd0.txt") ) - { - wipefile("/tmp/tinyfd0.txt"); - remove("/tmp/tinyfd0.txt"); - } - free(lDialogString); - return NULL ; - } - while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - {} - - pclose( lIn ) ; - - if ( fileExists("/tmp/tinyfd.txt") ) - { - wipefile("/tmp/tinyfd.txt"); - remove("/tmp/tinyfd.txt"); - } - if ( fileExists("/tmp/tinyfd0.txt") ) - { - wipefile("/tmp/tinyfd0.txt"); - remove("/tmp/tinyfd0.txt"); - } - - /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */ - /* printf( "lBuff0: %s\n" , lBuff ) ; */ - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ - if ( lWasBasicXterm ) - { - if ( strstr(lBuff,"^[") ) /* esc was pressed */ - { - free(lDialogString); - return NULL ; - } - } - - lResult = strncmp( lBuff , "1" , 1) ? 0 : 1 ; - /* printf( "lResult: %d \n" , lResult ) ; */ - if ( ! lResult ) - { - free(lDialogString); - return NULL ; - } - - /* printf( "lBuff+1: %s\n" , lBuff+1 ) ; */ - free(lDialogString); - return lBuff+1 ; -} - - -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] ; - int i ; - int lWasGraphicDialog = 0 ; - int lWasXterm = 0 ; - 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 *)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 " ); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with prompt \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "default location \"") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "\" " ) ; - } - getLastName( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "default name \"") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "\" " ) ; - } - strcat( lDialogString , ")' " ) ; - strcat(lDialogString, "-e 'on error number -128' " ) ; - strcat(lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; - } - else if ( tfd_kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} - - strcpy( lDialogString , "kdialog" ) ; - if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) - { - strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ - } - strcat( lDialogString , " --getsavefilename " ) ; - - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - if ( aDefaultPathAndFile[0] != '/' ) - { - strcat(lDialogString, "$PWD/") ; - } - strcat(lDialogString, "\"") ; - strcat(lDialogString, aDefaultPathAndFile ) ; - strcat(lDialogString , "\"" ) ; - } - else - { - strcat(lDialogString, "$PWD/") ; - } - - if ( aNumOfFilterPatterns > 0 ) - { - strcat(lDialogString , " \"" ) ; - strcat( lDialogString , aFilterPatterns[0] ) ; - for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat( lDialogString , " " ) ; - strcat( lDialogString , aFilterPatterns[i] ) ; - } - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) - { - strcat( lDialogString , " | " ) ; - strcat( lDialogString , aSingleFilterDescription ) ; - } - strcat( lDialogString , "\"" ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - } - else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) - { - if ( tfd_zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} - strcpy( lDialogString , "zenity" ) ; - 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 ( tfd_matedialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} - strcpy( lDialogString , "matedialog" ) ; - } - else if ( tfd_shellementaryPresent() ) - { - 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 *)1;} - strcpy( lDialogString , "qarma" ) ; - 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, " --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 (tfd_yadPresent()) - { - 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 ( 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=tkFileDialog.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() || dialogName() ) - { - if ( xdialogPresent( ) ) - { - 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 *)0;} - strcpy( lDialogString , "(dialog " ) ; - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(" ) ; - strcat( lDialogString , dialogName() ) ; - strcat( lDialogString , " " ) ; - } - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - if ( !xdialogPresent() && !gdialogPresent() ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; - } - - strcat( lDialogString , "--fselect \"" ) ; - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - if ( ! strchr(aDefaultPathAndFile, '/') ) - { - strcat(lDialogString, "./") ; - } - strcat(lDialogString, aDefaultPathAndFile) ; - } - else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) - { - strcat(lDialogString, getenv("HOME")) ; - strcat(lDialogString, "/") ; - } - else - { - strcat(lDialogString, "./") ; - } - - if ( lWasGraphicDialog ) - { - strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; - } - else - { - strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; - if ( lWasXterm ) - { - strcat( lDialogString , - "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); - } - else - { - strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; - } - } - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - 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 ; - } - getLastName(lString,p); - if ( ! strlen(lString) ) - { - return NULL; - } - return p ; - } - - 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; - } - getPathWithoutFinalSlash( lString , lBuff ) ; - if ( strlen( lString ) && ! dirExists( lString ) ) - { - return NULL ; - } - getLastName(lString,lBuff); - if ( ! filenameValid(lString) ) - { - return NULL; - } - return lBuff ; -} - - -/* in case of multiple files, the separator is | */ -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 */ -{ - char lDialogString[MAX_PATH_OR_CMD] ; - char lString[MAX_PATH_OR_CMD] ; - int i ; - FILE * lIn ; - char * p ; - char * p2 ; - 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 *)1;} - strcpy( lDialogString , "osascript "); - if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); - strcat( lDialogString , " -e 'try' -e '" ); - if ( ! aAllowMultipleSelects ) - { - - - strcat( lDialogString , "POSIX path of ( " ); - } - else - { - strcat( lDialogString , "set mylist to " ); - } - strcat( lDialogString , "choose file " ); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with prompt \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; - if ( strlen(lString) ) - { - strcat(lDialogString, "default location \"") ; - strcat(lDialogString, lString ) ; - strcat(lDialogString , "\" " ) ; - } - if ( aNumOfFilterPatterns > 0 ) - { - strcat(lDialogString , "of type {\"" ); - strcat( lDialogString , aFilterPatterns[0] + 2 ) ; - strcat( lDialogString , "\"" ) ; - for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat( lDialogString , ",\"" ) ; - strcat( lDialogString , aFilterPatterns[i] + 2) ; - strcat( lDialogString , "\"" ) ; - } - strcat( lDialogString , "} " ) ; - } - if ( aAllowMultipleSelects ) - { - strcat( lDialogString , "multiple selections allowed true ' " ) ; - strcat( lDialogString , - "-e 'set mystring to POSIX path of item 1 of mylist' " ); - strcat( lDialogString , - "-e 'repeat with i from 2 to the count of mylist' " ); - strcat( lDialogString , "-e 'set mystring to mystring & \"|\"' " ); - strcat( lDialogString , - "-e 'set mystring to mystring & POSIX path of item i of mylist' " ); - strcat( lDialogString , "-e 'end repeat' " ); - strcat( lDialogString , "-e 'mystring' " ); - } - else - { - strcat( lDialogString , ")' " ) ; - } - strcat(lDialogString, "-e 'on error number -128' " ) ; - strcat(lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; - } - else if ( tfd_kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} - lWasKdialog = 1 ; - - strcpy( lDialogString , "kdialog" ) ; - if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) - { - strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ - } - strcat( lDialogString , " --getopenfilename " ) ; - - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - if ( aDefaultPathAndFile[0] != '/' ) - { - strcat(lDialogString, "$PWD/") ; - } - strcat(lDialogString, "\"") ; - strcat(lDialogString, aDefaultPathAndFile ) ; - strcat(lDialogString , "\"" ) ; - } - else - { - strcat(lDialogString, "$PWD/") ; - } - - if ( aNumOfFilterPatterns > 0 ) - { - strcat(lDialogString , " \"" ) ; - strcat( lDialogString , aFilterPatterns[0] ) ; - for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) - { - strcat( lDialogString , " " ) ; - strcat( lDialogString , aFilterPatterns[i] ) ; - } - if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) - { - strcat( lDialogString , " | " ) ; - strcat( lDialogString , aSingleFilterDescription ) ; - } - strcat( lDialogString , "\"" ) ; - } - if ( aAllowMultipleSelects ) - { - strcat( lDialogString , " --multiple --separate-output" ) ; - } - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - } - else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) - { - if ( tfd_zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} - strcpy( lDialogString , "zenity" ) ; - 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 ( tfd_matedialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} - strcpy( lDialogString , "matedialog" ) ; - } - else if ( tfd_shellementaryPresent() ) - { - 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 *)1;} - strcpy( lDialogString , "qarma" ) ; - 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" ) ; - - 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 (tfd_yadPresent()) - { - 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 ( 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 , "lFiles=tkFileDialog.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 *)1;} - lWasGraphicDialog = 1 ; - strcpy( lDialogString , "(Xdialog " ) ; - } - else if ( isTerminalRunning( ) ) - { - 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 *)0;} - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(" ) ; - strcat( lDialogString , dialogName() ) ; - strcat( lDialogString , " " ) ; - } - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - if ( !xdialogPresent() && !gdialogPresent() ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; - } - - strcat( lDialogString , "--fselect \"" ) ; - if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) - { - if ( ! strchr(aDefaultPathAndFile, '/') ) - { - strcat(lDialogString, "./") ; - } - strcat(lDialogString, aDefaultPathAndFile) ; - } - else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) - { - strcat(lDialogString, getenv("HOME")) ; - strcat(lDialogString, "/"); - } - else - { - strcat(lDialogString, "./") ; - } - - if ( lWasGraphicDialog ) - { - strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; - } - else - { - strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; - if ( lWasXterm ) - { - strcat( lDialogString , - "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); - } - else - { - strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; - } - } - } - else - { - 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 */ - p2 = tinyfd_inputBox(aTitle, lBuff, ""); - if (p2) strcpy(lBuff, p2); else lBuff[0] = '\0'; - if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */ - p2 = lBuff; - - if ( ! fileExists(p2) ) - { - free(lBuff); - lBuff = NULL; - } - else - { - strcpy(lBuff, p2); - lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char))); - } - return lBuff ; - } - - if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; - if ( ! ( lIn = popen( lDialogString , "r" ) ) ) - { - free(lBuff); - lBuff = NULL; - return NULL ; - } - lBuff[0]='\0'; - p=lBuff; - while ( fgets( p , sizeof( lBuff ) , lIn ) != NULL ) - { - p += strlen( p ); - } - pclose( lIn ) ; - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - /* printf( "lBuff: %s\n" , lBuff ) ; */ - if ( lWasKdialog && aAllowMultipleSelects ) - { - p = lBuff ; - while ( ( p = strchr( p , '\n' ) ) ) - * p = '|' ; - } - /* printf( "lBuff2: %s\n" , lBuff ) ; */ - if ( ! strlen( lBuff ) ) - { - free(lBuff); - lBuff = NULL; - return NULL; - } - if ( aAllowMultipleSelects && strchr(lBuff, '|') ) - { - if( ! ensureFilesExist( lBuff , lBuff ) ) - { - free(lBuff); - lBuff = NULL; - return NULL; - } - } - else if ( !fileExists(lBuff) ) - { - free(lBuff); - lBuff = NULL; - return NULL; - } - - lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char))); - - /*printf( "lBuff3: %s\n" , lBuff ) ; */ - return lBuff ; -} - - -char * tinyfd_selectFolderDialog( - char const * aTitle , /* "" */ - char const * aDefaultPath ) /* "" */ -{ - static char lBuff[MAX_PATH_OR_CMD] ; - char lDialogString[MAX_PATH_OR_CMD] ; - FILE * lIn ; - 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 *)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 "); - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "with prompt \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - if ( aDefaultPath && strlen(aDefaultPath) ) - { - strcat(lDialogString, "default location \"") ; - strcat(lDialogString, aDefaultPath ) ; - strcat(lDialogString , "\" " ) ; - } - strcat( lDialogString , ")' " ) ; - strcat(lDialogString, "-e 'on error number -128' " ) ; - strcat(lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; - } - else if ( tfd_kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} - strcpy( lDialogString , "kdialog" ) ; - if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) - { - strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ - } - strcat( lDialogString , " --getexistingdirectory " ) ; - - if ( aDefaultPath && strlen(aDefaultPath) ) - { - if ( aDefaultPath[0] != '/' ) - { - strcat(lDialogString, "$PWD/") ; - } - strcat(lDialogString, "\"") ; - strcat(lDialogString, aDefaultPath ) ; - strcat(lDialogString , "\"" ) ; - } - else - { - strcat(lDialogString, "$PWD/") ; - } - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - } - else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) - { - if ( tfd_zenityPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} - strcpy( lDialogString , "zenity" ) ; - 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 ( tfd_matedialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} - strcpy( lDialogString , "matedialog" ) ; - } - else if ( tfd_shellementaryPresent() ) - { - 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 *)1;} - strcpy( lDialogString , "qarma" ) ; - 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, " --filename=\"") ; - strcat(lDialogString, aDefaultPath) ; - 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 --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 ( 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.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 *)1;} - lWasGraphicDialog = 1 ; - strcpy( lDialogString , "(Xdialog " ) ; - } - else if ( isTerminalRunning( ) ) - { - 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 *)0;} - lWasXterm = 1 ; - strcpy( lDialogString , terminalName() ) ; - strcat( lDialogString , "'(" ) ; - strcat( lDialogString , dialogName() ) ; - strcat( lDialogString , " " ) ; - } - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, "--title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\" ") ; - } - - if ( !xdialogPresent() && !gdialogPresent() ) - { - strcat(lDialogString, "--backtitle \"") ; - strcat(lDialogString, - "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; - strcat(lDialogString, "\" ") ; - } - - strcat( lDialogString , "--dselect \"" ) ; - if ( aDefaultPath && strlen(aDefaultPath) ) - { - strcat(lDialogString, aDefaultPath) ; - ensureFinalSlash(lDialogString); - } - else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) - { - strcat(lDialogString, getenv("HOME")) ; - strcat(lDialogString, "/"); - } - else - { - strcat(lDialogString, "./") ; - } - - if ( lWasGraphicDialog ) - { - strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; - } - else - { - strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; - if ( lWasXterm ) - { - strcat( lDialogString , - "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); - } - else - { - strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; - } - } - } - else - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - 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 ; - } - return p ; - } - 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 ) || ! dirExists( lBuff ) ) - { - return NULL ; - } - return lBuff ; -} - - -/* returns the hexcolor as a string "#FF0000" */ -/* aoResultRGB also contains the result */ -/* aDefaultRGB is used only if aDefaultHexRGB is NULL */ -/* aDefaultRGB and aoResultRGB can be the same array */ -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 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 * p; - char * lPointerInputBox; - FILE * lIn ; - int i ; - int lWasZenity3 = 0 ; - int lWasOsascript = 0 ; - int lWasXdialog = 0 ; - lBuff[0]='\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 (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 *)1;} - lWasOsascript = 1 ; - strcpy( lDialogString , "osascript"); - - if ( ! osx9orBetter() ) - { - strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); - strcat( lDialogString , " -e 'try' -e 'set mycolor to choose color default color {"); - } - else - { - strcat( lDialogString , -" -e 'try' -e 'tell app (path to frontmost application as Unicode text) \ -to set mycolor to choose color default color {"); - } - - sprintf(lTmp, "%d", 256 * lDefaultRGB[0] ) ; - strcat(lDialogString, lTmp ) ; - strcat(lDialogString, "," ) ; - sprintf(lTmp, "%d", 256 * lDefaultRGB[1] ) ; - strcat(lDialogString, lTmp ) ; - strcat(lDialogString, "," ) ; - sprintf(lTmp, "%d", 256 * lDefaultRGB[2] ) ; - strcat(lDialogString, lTmp ) ; - strcat(lDialogString, "}' " ) ; - strcat( lDialogString , -"-e 'set mystring to ((item 1 of mycolor) div 256 as integer) as string' " ); - strcat( lDialogString , -"-e 'repeat with i from 2 to the count of mycolor' " ); - strcat( lDialogString , -"-e 'set mystring to mystring & \" \" & ((item i of mycolor) div 256 as integer) as string' " ); - strcat( lDialogString , "-e 'end repeat' " ); - strcat( lDialogString , "-e 'mystring' "); - strcat(lDialogString, "-e 'on error number -128' " ) ; - strcat(lDialogString, "-e 'end try'") ; - if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; - } - else if ( tfd_kdialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} - strcpy( lDialogString , "kdialog" ) ; - 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'" , lDefaultHexRGB ) ; - - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, " --title \"") ; - strcat(lDialogString, aTitle) ; - strcat(lDialogString, "\"") ; - } - } - else if ( tfd_zenity3Present() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) - { - lWasZenity3 = 1 ; - if ( tfd_zenity3Present() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity3");return (char *)1;} - strcpy( lDialogString , "zenity" ); - 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 ( tfd_matedialogPresent() ) - { - if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} - strcpy( lDialogString , "matedialog" ) ; - } - else if ( tfd_shellementaryPresent() ) - { - 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 *)1;} - strcpy( lDialogString , "qarma" ) ; - 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" , lDefaultHexRGB ) ; - - 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 *)1;} - lWasXdialog = 1 ; - strcpy( lDialogString , "Xdialog --colorsel \"" ) ; - if ( aTitle && strlen(aTitle) ) - { - strcat(lDialogString, aTitle) ; - } - strcat(lDialogString, "\" 0 60 ") ; -#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 ( 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 *)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 ( tfd_isDarwin( ) ) - { - strcat( lDialogString , -"import os;os.system('''osascript -e 'tell app \\\"Finder\\\" to set \ -frontmost of process \\\"Python\\\" to true' ''');"); - } - - strcat( lDialogString , "res=tkColorChooser.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 (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} - 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( (int) p[i] ) ) - { - return NULL ; - } - } - 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 ) ; - if ( ! ( lIn = popen( lDialogString , "r" ) ) ) - { - return NULL ; - } - while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) - { - } - pclose( lIn ) ; - if ( ! strlen( lBuff ) ) - { - return NULL ; - } - /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */ - /* printf( "lBuff0: %s\n" , lBuff ) ; */ - if ( lBuff[strlen( lBuff ) -1] == '\n' ) - { - lBuff[strlen( lBuff ) -1] = '\0' ; - } - - if ( lWasZenity3 ) - { - if ( lBuff[0] == '#' ) - { - if ( strlen(lBuff)>7 ) - { - lBuff[3]=lBuff[5]; - lBuff[4]=lBuff[6]; - lBuff[5]=lBuff[9]; - lBuff[6]=lBuff[10]; - lBuff[7]='\0'; - } - Hex2RGB(lBuff,aoResultRGB); - } - else if ( lBuff[3] == '(' ) { -#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] == '(' ) { -#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 ) ; */ -#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 ) ; */ - - strcpy(lDefaultHexRGB,lBuff); - return lDefaultHexRGB ; -} - -#endif /* _WIN32 */ - - -/* -int main( int argc , char * argv[] ) -{ -char const * lTmp; -char const * lTheSaveFileName; -char const * lTheOpenFileName; -char const * lTheSelectFolderName; -char const * lTheHexColor; -char const * lWillBeGraphicMode; -unsigned char lRgbColor[3]; -FILE * lIn; -char lBuffer[1024]; -char lString[1024]; -char const * lFilterPatterns[2] = { "*.txt", "*.text" }; - -tinyfd_verbose = argc - 1; -tinyfd_silent = 1; - -lWillBeGraphicMode = tinyfd_inputBox("tinyfd_query", NULL, NULL); - -strcpy(lBuffer, "v"); -strcat(lBuffer, tinyfd_version); -if (lWillBeGraphicMode) -{ - strcat(lBuffer, "\ngraphic mode: "); -} -else -{ - strcat(lBuffer, "\nconsole mode: "); -} -strcat(lBuffer, tinyfd_response); -strcat(lBuffer, "\n"); -strcat(lBuffer, tinyfd_needs+78); -strcpy(lString, "tinyfiledialogs"); -tinyfd_messageBox(lString, lBuffer, "ok", "info", 0); - -tinyfd_notifyPopup("the title", "the message\n\tfrom outer-space", "info"); - -if (lWillBeGraphicMode && !tinyfd_forceConsole) -{ - tinyfd_forceConsole = ! tinyfd_messageBox("Hello World", - "graphic dialogs [yes] / console mode [no]?", - "yesno", "question", 1); -} - -lTmp = tinyfd_inputBox( - "a password box", "your password will be revealed", NULL); - -if (!lTmp) return 1; - -strcpy(lString, lTmp); - -lTheSaveFileName = tinyfd_saveFileDialog( - "let us save this password", - "passwordFile.txt", - 2, - lFilterPatterns, - NULL); - -if (!lTheSaveFileName) -{ - tinyfd_messageBox( - "Error", - "Save file name is NULL", - "ok", - "error", - 1); - return 1; -} - -lIn = fopen(lTheSaveFileName, "w"); -if (!lIn) -{ - tinyfd_messageBox( - "Error", - "Can not open this file in write mode", - "ok", - "error", - 1); - return 1; -} -fputs(lString, lIn); -fclose(lIn); - -lTheOpenFileName = tinyfd_openFileDialog( - "let us read the password back", - "", - 2, - lFilterPatterns, - NULL, - 0); - -if (!lTheOpenFileName) -{ - tinyfd_messageBox( - "Error", - "Open file name is NULL", - "ok", - "error", - 1); - return 1; -} - -lIn = fopen(lTheOpenFileName, "r"); - -if (!lIn) -{ - tinyfd_messageBox( - "Error", - "Can not open this file in read mode", - "ok", - "error", - 1); - return(1); -} -lBuffer[0] = '\0'; -fgets(lBuffer, sizeof(lBuffer), lIn); -fclose(lIn); - -tinyfd_messageBox("your password is", - lBuffer, "ok", "info", 1); - -lTheSelectFolderName = tinyfd_selectFolderDialog( - "let us just select a directory", NULL); - -if (!lTheSelectFolderName) -{ - tinyfd_messageBox( - "Error", - "Select folder name is NULL", - "ok", - "error", - 1); - return 1; -} - -tinyfd_messageBox("The selected folder is", - lTheSelectFolderName, "ok", "info", 1); - -lTheHexColor = tinyfd_colorChooser( - "choose a nice color", - "#FF0077", - lRgbColor, - lRgbColor); - -if (!lTheHexColor) -{ - tinyfd_messageBox( - "Error", - "hexcolor is NULL", - "ok", - "error", - 1); - return 1; -} - -tinyfd_messageBox("The selected hexcolor is", - lTheHexColor, "ok", "info", 1); - - tinyfd_beep(); - - return 0; -} -*/ - -#ifdef _MSC_VER -#pragma warning(default:4996) -#pragma warning(default:4100) -#pragma warning(default:4706) -#endif +/* 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.8.8 [Apr 22, 2021] zlib licence + |tiny file| Unique code file created [November 9, 2014] + | 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 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 | + |_________________________________________________________| + +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 +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 +#include +#include +#include + +#ifdef _WIN32 + #ifdef __BORLANDC__ + #define _getch getch + #endif + #ifndef _WIN32_WINNT + #define _WIN32_WINNT 0x0500 + #endif + #include + #include + #include + #include + #include + #define TINYFD_NOCCSUNICODE + #define SLASH "\\" +#else + #include + #include + #include /* on old systems try instead */ + #include + #include + #include /* on old systems try instead */ + #define SLASH "/" +#endif /* _WIN32 */ + +#include "tinyfiledialogs.h" + +#define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */ + +#ifndef MAX_MULTIPLE_FILES +#define MAX_MULTIPLE_FILES 1024 +#endif +#define LOW_MULTIPLE_FILES 32 + +char tinyfd_version[8] = "3.8.8"; + +/******************************************************************************************************/ +/**************************************** 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. + 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, +the functions will not display the dialogs +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 + shellementary qarma yad python2-tkinter python3-tkinter python-dbus + perl-dbus gxmessage gmessage xmessage xdialog gdialog +for console mode: + dialog whiptail basicinput no_solution */ + +static int gWarningDisplayed = 0 ; +static char gTitle[]="missing software! (we will try basic console input)"; + +#ifdef _WIN32 +char tinyfd_needs[] = "\ + ___________\n\ +/ \\ \n\ +| tiny file |\n\ +| dialogs |\n\ +\\_____ ____/\n\ + \\|\ +\ntiny file dialogs on Windows needs:\ +\n a graphic display\ +\nor dialog.exe (curses console mode)\ +\nor a console for basic input"; +#else +char tinyfd_needs[] = "\ + ___________\n\ +/ \\ \n\ +| tiny file |\n\ +| dialogs |\n\ +\\_____ ____/\n\ + \\|\ +\ntiny file dialogs on UNIX needs:\ +\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 + +#ifdef _MSC_VER +#pragma warning(disable:4996) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ +#pragma warning(disable:4100) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ +#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 * aoDestination, /* make sure it is allocated, use _MAX_PATH */ + char const * aSource) /* aoDestination and aSource can be the same */ +{ + char const * lTmp ; + if ( aSource ) + { + lTmp = strrchr(aSource, '/'); + if (!lTmp) + { + lTmp = strrchr(aSource, '\\'); + } + if (lTmp) + { + strncpy(aoDestination, aSource, lTmp - aSource ); + aoDestination[lTmp - aSource] = '\0'; + } + else + { + * aoDestination = '\0'; + } + } + else + { + * aoDestination = '\0'; + } + return aoDestination; +} + + +static char * getLastName( + char * aoDestination, /* make sure it is allocated */ + char const * aSource) +{ + /* copy the last name after '/' or '\' */ + char const * lTmp ; + if ( aSource ) + { + lTmp = strrchr(aSource, '/'); + if (!lTmp) + { + lTmp = strrchr(aSource, '\\'); + } + if (lTmp) + { + strcpy(aoDestination, lTmp + 1); + } + else + { + strcpy(aoDestination, aSource); + } + } + else + { + * aoDestination = '\0'; + } + return aoDestination; +} + + +static void ensureFinalSlash( char * aioString ) +{ + if ( aioString && strlen( aioString ) ) + { + char * lastcar = aioString + strlen( aioString ) - 1 ; + if ( strncmp( lastcar , SLASH , 1 ) ) + { + strcat( lastcar , SLASH ) ; + } + } +} + + +static void Hex2RGB( char const aHexRGB[8] , unsigned char aoResultRGB[3] ) +{ + char lColorChannel[8] ; + if ( aoResultRGB ) + { + if ( aHexRGB ) + { + strcpy(lColorChannel, aHexRGB ) ; + aoResultRGB[2] = (unsigned char)strtoul(lColorChannel+5,NULL,16); + lColorChannel[5] = '\0'; + aoResultRGB[1] = (unsigned char)strtoul(lColorChannel+3,NULL,16); + lColorChannel[3] = '\0'; + aoResultRGB[0] = (unsigned char)strtoul(lColorChannel+1,NULL,16); +/* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */ + } + else + { + aoResultRGB[0]=0; + aoResultRGB[1]=0; + aoResultRGB[2]=0; + } + } +} + +static void RGB2Hex( unsigned char const aRGB[3], char aoResultHexRGB[8] ) +{ + if ( aoResultHexRGB ) + { + if ( aRGB ) + { +#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, "#%02hx%02hx%02hx", aRGB[0], aRGB[1], aRGB[2]); +#endif + /*printf("aoResultHexRGB %s\n", aoResultHexRGB);*/ + } + else + { + aoResultHexRGB[0]=0; + aoResultHexRGB[1]=0; + aoResultHexRGB[2]=0; + } + } +} + + +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' ; + return ; + } + if ( ! aOldSubStr ) + { + strcpy( aoDestination , aSource ) ; + return ; + } + if ( aNewSubStr ) + { + lNewSubStr = aNewSubStr ; + } + p = aSource ; + * aoDestination = '\0' ; + while ( ( pOccurence = strstr( p , aOldSubStr ) ) != NULL ) + { + strncat( aoDestination , p , pOccurence - p ) ; + strcat( aoDestination , lNewSubStr ) ; + p = pOccurence + lOldSubLen ; + } + strcat( aoDestination , p ) ; +} + + +static int filenameValid( char const * aFileNameWithoutPath ) +{ + if ( ! aFileNameWithoutPath + || ! strlen(aFileNameWithoutPath) + || strpbrk(aFileNameWithoutPath , "\\/:*?\"<>|") ) + { + return 0 ; + } + return 1 ; +} + +#ifndef _WIN32 + +static int fileExists( char const * aFilePathAndName ) +{ + FILE * lIn ; + if ( ! aFilePathAndName || ! strlen(aFilePathAndName) ) + { + return 0 ; + } + lIn = fopen( aFilePathAndName , "r" ) ; + if ( ! lIn ) + { + return 0 ; + } + fclose( lIn ) ; + return 1 ; +} + +#endif + + +static void wipefile(char const * aFilename) +{ + int i; + struct stat st; + FILE * lIn; + + if (stat(aFilename, &st) == 0) + { + if ((lIn = fopen(aFilename, "w"))) + { + for (i = 0; i < st.st_size; i++) + { + fputc('A', 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"; + + if (lPowershellPresent < 0) + { + if (!(lIn = _popen("where powershell.exe", "r"))) + { + lPowershellPresent = 0; + return 0; + } + while (fgets(lBuff, sizeof(lBuff), lIn) != NULL) + { + } + _pclose(lIn); + if (lBuff[strlen(lBuff) - 1] == '\n') + { + lBuff[strlen(lBuff) - 1] = '\0'; + } + if (strcmp(lBuff + strlen(lBuff) - strlen(lString), lString)) + { + lPowershellPresent = 0; + } + else + { + lPowershellPresent = 1; + } + } + return lPowershellPresent; +} + +static int windowsVersion(void) +{ +#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; +} + + +static void replaceChr(char * aString, char aOldChr, char aNewChr) +{ + char * p; + + if (!aString) return; + if (aOldChr == aNewChr) return; + + p = aString; + while ((p = strchr(p, aOldChr))) + { + *p = aNewChr; + p++; + } + return; +} + + +#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; + 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"))) + { + for (i = 0; i < st.st_size; i++) + { + fputc('A', lIn); + } + fclose(lIn); + } + } +} + + +static wchar_t * getPathWithoutFinalSlashW( + 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) + { + lTmp = wcsrchr(aSource, L'/'); + if (!lTmp) + { + lTmp = wcsrchr(aSource, L'\\'); + } + if (lTmp) + { + wcsncpy(aoDestination, aSource, lTmp - aSource); + aoDestination[lTmp - aSource] = L'\0'; + } + else + { + *aoDestination = L'\0'; + } + } + else + { + *aoDestination = L'\0'; + } + return aoDestination; +} + + +static wchar_t * getLastNameW( + wchar_t * aoDestination, /* make sure it is allocated */ + wchar_t const * aSource) +{ + /* copy the last name after '/' or '\' */ + wchar_t const * lTmp; + if (aSource) + { + lTmp = wcsrchr(aSource, L'/'); + if (!lTmp) + { + lTmp = wcsrchr(aSource, L'\\'); + } + if (lTmp) + { + wcscpy(aoDestination, lTmp + 1); + } + else + { + wcscpy(aoDestination, aSource); + } + } + else + { + *aoDestination = L'\0'; + } + return aoDestination; +} + + +static void Hex2RGBW(wchar_t const aHexRGB[8], unsigned char aoResultRGB[3]) +{ + wchar_t lColorChannel[8]; + if (aoResultRGB) + { + if (aHexRGB) + { + wcscpy(lColorChannel, aHexRGB); + aoResultRGB[2] = (unsigned char)wcstoul(lColorChannel + 5, NULL, 16); + lColorChannel[5] = '\0'; + aoResultRGB[1] = (unsigned char)wcstoul(lColorChannel + 3, NULL, 16); + lColorChannel[3] = '\0'; + aoResultRGB[0] = (unsigned char)wcstoul(lColorChannel + 1, NULL, 16); + /* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */ + } + else + { + aoResultRGB[0] = 0; + aoResultRGB[1] = 0; + aoResultRGB[2] = 0; + } + } +} + + +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); */ +#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 + + } + else + { + aoResultHexRGB[0] = 0; + aoResultHexRGB[1] = 0; + aoResultHexRGB[2] = 0; + } + } +} + + +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 + wchar_t * lTmpWChar; + int lStatRet; + size_t lDirLen; + + if (!aDirPath) + return 0; + lDirLen = strlen(aDirPath); + if (!lDirLen) + return 1; + if ( (lDirLen == 2) && (aDirPath[1] == ':') ) + return 1; + + if (tinyfd_winUtf8) + { + 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; + else + return 0; +} + + +static int fileExists(char const * aFilePathAndName) +{ +#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; + + if (!aFilePathAndName || !strlen(aFilePathAndName)) + { + return 0; + } + + if (tinyfd_winUtf8) + { + 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 + { + lIn = fopen(aFilePathAndName, "r"); + if (!lIn) + { + return 0; + } + fclose(lIn); + return 1; + } +} + +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 * ensureFilesExist(char * aDestination, + char const * aSourcePathsAndNames) +{ + char * lDestination = aDestination; + char const * p; + char const * p2; + size_t lLen; + + if (!aSourcePathsAndNames) + { + return NULL; + } + lLen = strlen(aSourcePathsAndNames); + if (!lLen) + { + return NULL; + } + + p = aSourcePathsAndNames; + while ((p2 = strchr(p, '|')) != NULL) + { + lLen = p2 - p; + memmove(lDestination, p, lLen); + lDestination[lLen] = '\0'; + if (fileExists(lDestination)) + { + lDestination += lLen; + *lDestination = '|'; + lDestination++; + } + p = p2 + 1; + } + if (fileExists(p)) + { + lLen = strlen(p); + memmove(lDestination, p, lLen); + lDestination[lLen] = '\0'; + } + else + { + *(lDestination - 1) = '\0'; + } + return aDestination; +} + +#ifdef _WIN32 + +static int __stdcall EnumThreadWndProc(HWND hwnd, LPARAM lParam) +{ + wchar_t lTitleName[MAX_PATH]; + GetWindowTextW(hwnd, lTitleName, MAX_PATH); + /* wprintf(L"lTitleName %ls \n", lTitleName); */ + if (wcscmp(L"tinyfiledialogsTopWindow", lTitleName) == 0) + { + SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); + return 0; + } + return 1; +} + + +static void hiddenConsoleW(wchar_t const * aString, wchar_t const * aDialogTitle, int aInFront) +{ + STARTUPINFOW StartupInfo; + PROCESS_INFORMATION ProcessInfo; + + if (!aString || !wcslen(aString) ) return; + + memset(&StartupInfo, 0, sizeof(StartupInfo)); + StartupInfo.cb = sizeof(STARTUPINFOW); + StartupInfo.dwFlags = STARTF_USESHOWWINDOW; + StartupInfo.wShowWindow = SW_HIDE; + + if (!CreateProcessW(NULL, (LPWSTR)aString, NULL, NULL, FALSE, + CREATE_NEW_CONSOLE, NULL, NULL, + &StartupInfo, &ProcessInfo)) + { + return; /* GetLastError(); */ + } + + WaitForInputIdle(ProcessInfo.hProcess, INFINITE); + if (aInFront) + { + while (EnumWindows(EnumThreadWndProc, (LPARAM)NULL)) {} + SetWindowTextW(GetForegroundWindow(), aDialogTitle); + } + WaitForSingleObject(ProcessInfo.hProcess, INFINITE); + CloseHandle(ProcessInfo.hThread); + CloseHandle(ProcessInfo.hProcess); +} + + +int tinyfd_messageBoxW( + 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; + } + else if (aIconType && !wcscmp(L"error", aIconType)) + { + aCode = MB_ICONERROR; + } + else if (aIconType && !wcscmp(L"question", aIconType)) + { + aCode = MB_ICONQUESTION; + } + else + { + aCode = MB_ICONINFORMATION; + } + + if (aDialogType && !wcscmp(L"okcancel", aDialogType)) + { + aCode += MB_OKCANCEL; + if (!aDefaultButton) + { + aCode += MB_DEFBUTTON2; + } + } + else if (aDialogType && !wcscmp(L"yesno", aDialogType)) + { + aCode += MB_YESNO; + if (!aDefaultButton) + { + aCode += MB_DEFBUTTON2; + } + } + else + { + aCode += MB_OK; + } + + aCode += MB_TOPMOST; + + lBoxReturnValue = MessageBoxW(GetForegroundWindow(), aMessage, aTitle, aCode); + if (((aDialogType + && wcscmp(L"okcancel", aDialogType) + && wcscmp(L"yesno", aDialogType))) + || (lBoxReturnValue == IDOK) + || (lBoxReturnValue == IDYES)) + { + return 1; + } + else + { + return 0; + } +} + + +/* return has only meaning for tinyfd_query */ +int tinyfd_notifyPopupW( + 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 (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 {\ +[cmdletbinding()] \ +param( \ +[string]$Title = ' ', \ +[string]$Message = ' ', \ +[ValidateSet('info', 'warning', 'error')] \ +[string]$IconType = 'info');\ +[system.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null ; \ +$balloon = New-Object System.Windows.Forms.NotifyIcon ; \ +$path = Get-Process -id $pid | Select-Object -ExpandProperty Path ; \ +$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) ;"); + + wcscat(lDialogString, L"\ +$balloon.Icon = $icon ; \ +$balloon.BalloonTipIcon = $IconType ; \ +$balloon.BalloonTipText = $Message ; \ +$balloon.BalloonTipTitle = $Title ; \ +$balloon.Text = 'tinyfiledialogs' ; \ +$balloon.Visible = $true ; \ +$balloon.ShowBalloonTip(5000)};\ +Show-BalloonTip"); + + if (aTitle && wcslen(aTitle)) + { + wcscat(lDialogString, L" -Title '"); + wcscat(lDialogString, aTitle); + wcscat(lDialogString, L"'"); + } + if (aMessage && wcslen(aMessage)) + { + wcscat(lDialogString, L" -Message '"); + wcscat(lDialogString, aMessage); + wcscat(lDialogString, L"'"); + } + if (aMessage && wcslen(aIconType)) + { + wcscat(lDialogString, L" -IconType '"); + wcscat(lDialogString, aIconType); + wcscat(lDialogString, L"'"); + } + wcscat(lDialogString, L"\""); + + /* wprintf ( L"lDialogString: %ls\n" , lDialogString ) ; */ + + hiddenConsoleW(lDialogString, aTitle, 0); + free(lDialogString); + return 1; +} + + +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; + FILE * lIn; + FILE * lFile; + int lResult; + size_t lTitleLen; + size_t lMessageLen; + size_t lDialogStringLen; + + 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 ; + lDialogStringLen = 3 * MAX_PATH_OR_CMD + lTitleLen + lMessageLen; + lDialogString = (wchar_t *)malloc(2 * lDialogStringLen); + + if (aDefaultInput) + { + swprintf(lDialogString, +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) + lDialogStringLen, +#endif + L"%ls\\tinyfd.vbs", _wgetenv(L"TEMP")); + } + else + { + swprintf(lDialogString, +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) + lDialogStringLen, +#endif + L"%ls\\tinyfd.hta", _wgetenv(L"TEMP")); + } + lIn = _wfopen(lDialogString, L"w"); + if (!lIn) + { + free(lDialogString); + return NULL; + } + + if ( aDefaultInput ) + { + wcscpy(lDialogString, L"Dim result:result=InputBox(\""); + if (aMessage && wcslen(aMessage)) + { + wcscpy(lBuff, aMessage); + replaceWchar(lBuff, L'\n', L' '); + wcscat(lDialogString, lBuff); + } + wcscat(lDialogString, L"\",\"tinyfiledialogsTopWindow\",\""); + if (aDefaultInput && wcslen(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"); + } + else + { + wcscpy(lDialogString, L"\n\ +\n\ +\n\ +"); + + wcscat(lDialogString, L"tinyfiledialogsTopWindow"); + wcscat(lDialogString, L"\n\ +\n\ +\n\ +\n\ +\n\ +\n\ +\n\ +\n\ +\n\ +\n\ +\n\ +
\n"); + + wcscat(lDialogString, aMessage ? aMessage : L""); + + wcscat(lDialogString, L"\n\ +\n\ +\n\ +\n\ +
\n\ +

\n\ +\n\ +
\n\ +
\n"); + + wcscat(lDialogString, L"\n\ +\n\ +\n\ +\n\ +
\n\ +
\n\ +
\n\ +\n\ +\n\ +" ) ; + } + fputws(lDialogString, lIn); + fclose(lIn); + + if (aDefaultInput) + { + swprintf(lDialogString, +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) + lDialogStringLen, +#endif + L"%ls\\tinyfd.txt",_wgetenv(L"TEMP")); + +#ifdef TINYFD_NOCCSUNICODE + lFile = _wfopen(lDialogString, L"w"); + fputc(0xFF, lFile); + fputc(0xFE, lFile); +#else + lFile = _wfopen(lDialogString, L"wt, ccs=UNICODE"); /*or ccs=UTF-16LE*/ +#endif + fclose(lFile); + + wcscpy(lDialogString, L"cmd.exe /c cscript.exe //U //Nologo "); + wcscat(lDialogString, L"\"%TEMP%\\tinyfd.vbs\" "); + wcscat(lDialogString, L">> \"%TEMP%\\tinyfd.txt\""); + } + else + { + wcscpy(lDialogString, + L"cmd.exe /c mshta.exe \"%TEMP%\\tinyfd.hta\""); + } + + /* wprintf ( "lDialogString: %ls\n" , lDialogString ) ; */ + + hiddenConsoleW(lDialogString, aTitle, 1); + + swprintf(lDialogString, +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) + lDialogStringLen, +#endif + L"%ls\\tinyfd.txt", _wgetenv(L"TEMP")); + /* wprintf(L"lDialogString: %ls\n", lDialogString); */ +#ifdef TINYFD_NOCCSUNICODE + if (!(lIn = _wfopen(lDialogString, L"r"))) +#else + if (!(lIn = _wfopen(lDialogString, L"rt, ccs=UNICODE"))) /*or ccs=UTF-16LE*/ +#endif + { + _wremove(lDialogString); + 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 + fgetws(lBuff, MAX_PATH_OR_CMD, lIn); +#endif + fclose(lIn); + wipefileW(lDialogString); + _wremove(lDialogString); + + if (aDefaultInput) + { + swprintf(lDialogString, +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) + lDialogStringLen, +#endif + L"%ls\\tinyfd.vbs", _wgetenv(L"TEMP")); + } + else + { + swprintf(lDialogString, +#if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) + lDialogStringLen, +#endif + L"%ls\\tinyfd.hta", _wgetenv(L"TEMP")); + } + _wremove(lDialogString); + free(lDialogString); + /* wprintf( L"lBuff: %ls\n" , lBuff ) ; */ +#ifdef TINYFD_NOCCSUNICODE + lResult = !wcsncmp(lBuff+1, L"1", 1); +#else + lResult = !wcsncmp(lBuff, L"1", 1); +#endif + + /* printf( "lResult: %d \n" , lResult ) ; */ + if (!lResult) + { + return NULL ; + } + + /* 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 +} + + +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]; + wchar_t lDialogString[MAX_PATH_OR_CMD]; + wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L""; + wchar_t * p; + wchar_t * lRetval; + 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 *)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); + + getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile); + getLastNameW(lBuff, aDefaultPathAndFile); + + if (aNumOfFilterPatterns > 0) + { + ldefExt = aFilterPatterns[0]; + + if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) + { + wcscpy(lFilterPatterns, aSingleFilterDescription); + wcscat(lFilterPatterns, L"\n"); + } + wcscat(lFilterPatterns, aFilterPatterns[0]); + for (i = 1; i < aNumOfFilterPatterns; i++) + { + wcscat(lFilterPatterns, L";"); + wcscat(lFilterPatterns, aFilterPatterns[i]); + } + wcscat(lFilterPatterns, L"\n"); + if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription))) + { + wcscpy(lDialogString, lFilterPatterns); + wcscat(lFilterPatterns, lDialogString); + } + wcscat(lFilterPatterns, L"All Files\n*.*\n"); + p = lFilterPatterns; + while ((p = wcschr(p, L'\n')) != NULL) + { + *p = L'\0'; + p++; + } + } + + ofn.lStructSize = sizeof(OPENFILENAMEW); + ofn.hwndOwner = GetForegroundWindow(); + ofn.hInstance = 0; + 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.lpstrFileTitle = NULL; + ofn.nMaxFileTitle = MAX_PATH_OR_CMD/2; + 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 = ldefExt; + ofn.lCustData = 0L; + ofn.lpfnHook = NULL; + ofn.lpTemplateName = NULL; + + if (GetSaveFileNameW(&ofn) == 0) + { + lRetval = NULL; + } + else + { + lRetval = lBuff; + } + + if (lHResult == S_OK || lHResult == S_FALSE) + { + CoUninitialize(); + } + return lRetval; +} + + +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 */ +{ + 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+1]; + wchar_t * p; + int i, j; + size_t lBuffLen; + DWORD lFullBuffLen; + HRESULT lHResult; + OPENFILENAMEW ofn = { 0 }; + static wchar_t * lBuff = NULL; + + 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); + + getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile); + getLastNameW(lBuff, aDefaultPathAndFile); + + if (aNumOfFilterPatterns > 0) + { + if (aSingleFilterDescription && wcslen(aSingleFilterDescription)) + { + wcscpy(lFilterPatterns, aSingleFilterDescription); + wcscat(lFilterPatterns, L"\n"); + } + wcscat(lFilterPatterns, aFilterPatterns[0]); + for (i = 1; i < aNumOfFilterPatterns; i++) + { + wcscat(lFilterPatterns, L";"); + wcscat(lFilterPatterns, aFilterPatterns[i]); + } + wcscat(lFilterPatterns, L"\n"); + if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription))) + { + wcscpy(lDialogString, lFilterPatterns); + wcscat(lFilterPatterns, lDialogString); + } + wcscat(lFilterPatterns, L"All Files\n*.*\n"); + p = lFilterPatterns; + while ((p = wcschr(p, L'\n')) != NULL) + { + *p = L'\0'; + p++; + } + } + + ofn.lStructSize = sizeof(OPENFILENAME); + ofn.hwndOwner = GetForegroundWindow(); + ofn.hInstance = 0; + ofn.lpstrFilter = wcslen(lFilterPatterns) ? lFilterPatterns : NULL; + ofn.lpstrCustomFilter = NULL; + ofn.nMaxCustFilter = 0; + ofn.nFilterIndex = 1; + ofn.lpstrFile = lBuff; + ofn.nMaxFile = lFullBuffLen; + ofn.lpstrFileTitle = NULL; + ofn.nMaxFileTitle = MAX_PATH_OR_CMD / 2; + 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; + ofn.nFileExtension = 0; + ofn.lpstrDefExt = NULL; + ofn.lCustData = 0L; + ofn.lpfnHook = NULL; + ofn.lpTemplateName = NULL; + + if (aAllowMultipleSelects) + { + ofn.Flags |= OFN_ALLOWMULTISELECT; + } + + if (GetOpenFileNameW(&ofn) == 0) + { + free(lBuff); + lBuff = NULL; + } + else + { + lBuffLen = wcslen(lBuff); + lPointers[0] = lBuff + lBuffLen + 1; + 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 < 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 lBuff; +} + + +BOOL CALLBACK BrowseCallbackProcW_enum(HWND hWndChild, LPARAM lParam) +{ + 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; +} + + +static int __stdcall BrowseCallbackProcW(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) +{ + 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 * 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 *)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); + + bInfo.hwndOwner = GetForegroundWindow(); + bInfo.pidlRoot = NULL; + bInfo.pszDisplayName = lBuff; + bInfo.lpszTitle = aTitle && wcslen(aTitle) ? aTitle : NULL; + if (lHResult == S_OK || lHResult == S_FALSE) + { + bInfo.ulFlags = BIF_USENEWUI; + } + bInfo.lpfn = BrowseCallbackProcW; + bInfo.lParam = (LPARAM)aDefaultPath; + bInfo.iImage = -1; + + lpItem = SHBrowseForFolderW(&bInfo); + if (!lpItem) + { + lRetval = NULL; + } + else + { + SHGetPathFromIDListW(lpItem, lBuff); + lRetval = lBuff ; + } + + if (lHResult == S_OK || lHResult == S_FALSE) + { + CoUninitialize(); + } + return lRetval; +} + + +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 } */ +{ + static wchar_t lResultHexRGB[8]; + CHOOSECOLORW cc; + COLORREF crCustColors[16]; + unsigned char lDefaultRGB[3]; + int lRet; + + HRESULT lHResult; + + 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 ) + { + Hex2RGBW(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_ANYCOLOR ; + cc.lCustData = 0; + cc.lpfnHook = NULL; + cc.lpTemplateName = NULL; + + lRet = ChooseColorW(&cc); + + if (!lRet) + { + return NULL; + } + + aoResultRGB[0] = GetRValue(cc.rgbResult); + aoResultRGB[1] = GetGValue(cc.rgbResult); + aoResultRGB[2] = GetBValue(cc.rgbResult); + + RGB2HexW(aoResultRGB, lResultHexRGB); + + if (lHResult == S_OK || lHResult == S_FALSE) + { + CoUninitialize(); + } + + return lResultHexRGB; +} + + +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[128]; + wchar_t lDefaultHexRGB[16]; + wchar_t * lTmpWChar; + char * lTmpChar; + + 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, + lDefaultHexRGB, + aDefaultRGB, + aoResultRGB ); + + if (!lTmpWChar) + { + return NULL; + } + + if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar); + else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar); + strcpy(lResultHexRGB, lTmpChar); + + return lResultHexRGB; +} + + +static int dialogPresent(void) +{ + static int lDialogPresent = -1 ; + char lBuff[MAX_PATH_OR_CMD] ; + FILE * lIn ; + char const * lString = "dialog.exe"; + if (!tinyfd_allowCursesDialogs) return 0; + if (lDialogPresent < 0) + { + if (!(lIn = _popen("where dialog.exe","r"))) + { + lDialogPresent = 0 ; + return 0 ; + } + while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + {} + _pclose( lIn ) ; + if ( lBuff[strlen( lBuff ) -1] == '\n' ) + { + lBuff[strlen( lBuff ) -1] = '\0' ; + } + if ( strcmp(lBuff+strlen(lBuff)-strlen(lString),lString) ) + { + lDialogPresent = 0 ; + } + else + { + lDialogPresent = 1 ; + } + } + return lDialogPresent; +} + + +static int messageBoxWinConsole( + 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)) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) + || !strcmp("yesno", aDialogType) || !strcmp("yesnocancel", aDialogType) ) ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab: move focus") ; + strcat(lDialogString, "\" ") ; + } + + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + if ( ! aDefaultButton ) + { + strcat( lDialogString , "--defaultno " ) ; + } + strcat( lDialogString , + "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ; + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + if ( ! aDefaultButton ) + { + strcat( lDialogString , "--defaultno " ) ; + } + strcat( lDialogString , "--yesno " ) ; + } + else if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + if (!aDefaultButton) + { + strcat(lDialogString, "--defaultno "); + } + strcat(lDialogString, "--menu "); + } + else + { + strcat( lDialogString , "--msgbox " ) ; + } + + strcat( lDialogString , "\"" ) ; + if ( aMessage && strlen(aMessage) ) + { + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lBuff ) ; + strcat(lDialogString, lBuff) ; + lBuff[0]='\0'; + } + strcat(lDialogString, "\" "); + + if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + strcat(lDialogString, "0 60 0 Yes \"\" No \"\""); + strcat(lDialogString, "2>>"); + } + else + { + strcat(lDialogString, "10 60"); + strcat(lDialogString, " && echo 1 > "); + } + + 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) + {} + fclose(lIn); + remove(lDialogFile); + if ( lBuff[strlen( lBuff ) -1] == '\n' ) + { + lBuff[strlen( lBuff ) -1] = '\0' ; + } + + /* if (tinyfd_verbose) printf("lBuff: %s\n", lBuff); */ + if ( ! strlen(lBuff) ) + { + return 0; + } + + if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + if (lBuff[0] == 'Y') return 1; + else return 2; + } + + return 1; +} + + +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("TEMP")); + strcat(lDialogFile, "\\tinyfd.txt"); + strcpy(lDialogString , "echo|set /p=1 >" ) ; + strcat(lDialogString, lDialogFile); + strcat( lDialogString , " & " ) ; + + strcat( lDialogString , "dialog " ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab: move focus") ; + if ( ! aDefaultInput ) + { + strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ; + } + + strcat(lDialogString, "\" ") ; + + if ( ! aDefaultInput ) + { + strcat( lDialogString , "--insecure --passwordbox" ) ; + } + else + { + strcat( lDialogString , "--inputbox" ) ; + } + strcat( lDialogString , " \"" ) ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString,"\" 10 60 ") ; + if ( aDefaultInput && strlen(aDefaultInput) ) + { + strcat(lDialogString, "\"") ; + strcat(lDialogString, aDefaultInput) ; + strcat(lDialogString, "\" ") ; + } + + strcat(lDialogString, "2>>"); + strcpy(lDialogFile, getenv("TEMP")); + strcat(lDialogFile, "\\tinyfd.txt"); + strcat(lDialogString, lDialogFile); + strcat(lDialogString, " || echo 0 > "); + strcat(lDialogString, lDialogFile); + + /* printf( "lDialogString: %s\n" , lDialogString ) ; */ + system( lDialogString ) ; + + if (!(lIn = fopen(lDialogFile, "r"))) + { + remove(lDialogFile); + aoBuff[0] = '\0'; + return 0; + } + while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) + {} + fclose(lIn); + + wipefile(lDialogFile); + remove(lDialogFile); + if ( aoBuff[strlen( aoBuff ) -1] == '\n' ) + { + aoBuff[strlen( aoBuff ) -1] = '\0' ; + } + /* printf( "aoBuff: %s\n" , aoBuff ) ; */ + + /* printf( "aoBuff: %s len: %lu \n" , aoBuff , strlen(aoBuff) ) ; */ + lResult = strncmp( aoBuff , "1" , 1) ? 0 : 1 ; + /* printf( "lResult: %d \n" , lResult ) ; */ + if ( ! lResult ) + { + aoBuff[0] = '\0'; + return 0 ; + } + /* printf( "aoBuff+1: %s\n" , aoBuff+1 ) ; */ + strcpy(aoBuff, aoBuff+3); + return 1; +} + + +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] = ""; + FILE * lIn; + + strcpy( lDialogString , "dialog " ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; + + strcat( lDialogString , "--fselect \"" ) ; + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + /* dialog.exe uses unix separators even on windows */ + strcpy(lPathAndFile, aDefaultPathAndFile); + replaceChr( lPathAndFile , '\\' , '/' ) ; + } + + /* dialog.exe needs at least one separator */ + if ( ! strchr(lPathAndFile, '/') ) + { + strcat(lDialogString, "./") ; + } + strcat(lDialogString, lPathAndFile) ; + strcat(lDialogString, "\" 0 60 2>"); + strcpy(lPathAndFile, getenv("TEMP")); + strcat(lPathAndFile, "\\tinyfd.txt"); + strcat(lDialogString, lPathAndFile); + + /* printf( "lDialogString: %s\n" , lDialogString ) ; */ + system( lDialogString ) ; + + if (!(lIn = fopen(lPathAndFile, "r"))) + { + remove(lPathAndFile); + return NULL; + } + while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) + {} + fclose(lIn); + remove(lPathAndFile); + replaceChr( aoBuff , '/' , '\\' ) ; + /* printf( "aoBuff: %s\n" , aoBuff ) ; */ + getLastName(lDialogString,aoBuff); + if ( ! strlen(lDialogString) ) + { + return NULL; + } + return aoBuff; +} + + +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) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; + + strcat( lDialogString , "--fselect \"" ) ; + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + /* dialog.exe uses unix separators even on windows */ + strcpy(lFilterPatterns, aDefaultPathAndFile); + replaceChr( lFilterPatterns , '\\' , '/' ) ; + } + + /* dialog.exe needs at least one separator */ + if ( ! strchr(lFilterPatterns, '/') ) + { + strcat(lDialogString, "./") ; + } + strcat(lDialogString, lFilterPatterns) ; + strcat(lDialogString, "\" 0 60 2>"); + strcpy(lFilterPatterns, getenv("TEMP")); + strcat(lFilterPatterns, "\\tinyfd.txt"); + strcat(lDialogString, lFilterPatterns); + + /* printf( "lDialogString: %s\n" , lDialogString ) ; */ + system( lDialogString ) ; + + if (!(lIn = fopen(lFilterPatterns, "r"))) + { + remove(lFilterPatterns); + return NULL; + } + while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) + {} + fclose(lIn); + remove(lFilterPatterns); + replaceChr( aoBuff , '/' , '\\' ) ; + /* printf( "aoBuff: %s\n" , aoBuff ) ; */ + return aoBuff; +} + + +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] ; + FILE * lIn ; + + strcpy( lDialogString , "dialog " ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; + + strcat( lDialogString , "--dselect \"" ) ; + if ( aDefaultPath && strlen(aDefaultPath) ) + { + /* dialog.exe uses unix separators even on windows */ + strcpy(lString, aDefaultPath) ; + ensureFinalSlash(lString); + replaceChr( lString , '\\' , '/' ) ; + strcat(lDialogString, lString) ; + } + else + { + /* dialog.exe needs at least one separator */ + strcat(lDialogString, "./") ; + } + strcat(lDialogString, "\" 0 60 2>"); + strcpy(lString, getenv("TEMP")); + strcat(lString, "\\tinyfd.txt"); + strcat(lDialogString, lString); + + /* printf( "lDialogString: %s\n" , lDialogString ) ; */ + system( lDialogString ) ; + + if (!(lIn = fopen(lString, "r"))) + { + remove(lString); + return NULL; + } + while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL) + {} + fclose(lIn); + remove(lString); + replaceChr( aoBuff , '/' , '\\' ) ; + /* printf( "aoBuff: %s\n" , aoBuff ) ; */ + 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 * 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; + UINT lOriginalCP = 0; + UINT lOriginalOutputCP = 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); + + 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 * aTitle, /* NULL or "" */ + char const * aMessage , /* NULL or "" may contain \n \t */ + char const * aIconType ) /* "info" "warning" "error" */ +{ + 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 * 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; + + DWORD mode = 0; + HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); + + 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) + { + gWarningDisplayed = 1 ; + printf("\n\n%s\n", gTitle); + printf("%s\n\n", tinyfd_needs); + } + + if (!tinyfd_winUtf8) + { + lOriginalCP = GetConsoleCP(); + lOriginalOutputCP = GetConsoleOutputCP(); + (void)SetConsoleCP(GetACP()); + (void)SetConsoleOutputCP(GetACP()); + } + + if (aTitle && strlen(aTitle)) + { + printf("\n"); + if (tinyfd_winUtf8) writeUtf8(aTitle); + else printf("%s", aTitle); + printf("\n\n"); + } + if ( aMessage && strlen(aMessage) ) + { + if (tinyfd_winUtf8) writeUtf8(aMessage); + else printf("%s", aMessage); + printf("\n"); + } + printf("(ctrl-Z + enter to cancel): "); + if ( ! aDefaultInput ) + { + (void) GetConsoleMode(hStdin, &mode); + (void) SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); + } + 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 * 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 lString[MAX_PATH_OR_CMD] ; + char * p ; + char * lPointerInputBox; + int i; + + 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 ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) ) + && (!getenv("SSH_CLIENT") || getenvDISPLAY())) + { + 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 ) ) + { + return NULL; + } + getPathWithoutFinalSlash( lString , p ) ; + if ( strlen( lString ) && ! dirExists( lString ) ) + { + return NULL ; + } + getLastName(lString,p); + if ( ! filenameValid(lString) ) + { + return NULL; + } + return p ; +} + + +/* in case of multiple files, the separator is | */ +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 */ +{ + 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 *)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 ) ) + { + return NULL; + } + if ( aAllowMultipleSelects && strchr(p, '|') ) + { + p = ensureFilesExist( (char *) p , p ) ; + } + else if ( ! fileExists(p) ) + { + return NULL ; + } + /* printf( "lBuff3: %s\n" , p ) ; */ + return p ; +} + + +char * tinyfd_selectFolderDialog( + char const * aTitle , /* NULL or "" */ + char const * aDefaultPath ) /* NULL or "" */ +{ + 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 *)1;} + p = selectFolderDialogWinGui(lBuff, aTitle, aDefaultPath); + } + 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 ; + } + return p ; +} + + +/* returns the hexcolor as a string "#FF0000" */ +/* aoResultRGB also contains the result */ +/* aDefaultRGB is used only if aDefaultHexRGB is NULL */ +/* aDefaultRGB and aoResultRGB can be the same array */ +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 lDefaultHexRGB[16]; + int i; + char * p ; + char * lPointerInputBox; + char lString[MAX_PATH_OR_CMD]; + + lDefaultHexRGB[0] = '\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) + { + strcpy(lDefaultHexRGB, p); + return lDefaultHexRGB; + } + 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; +} + + +#else /* unix */ + +static char gPython2Name[16]; +static char gPython3Name[16]; +static char gPythonName[16]; + +int tfd_isDarwin(void) +{ + static int lsIsDarwin = -1 ; + struct utsname lUtsname ; + if ( lsIsDarwin < 0 ) + { + lsIsDarwin = !uname(&lUtsname) && !strcmp(lUtsname.sysname,"Darwin") ; + } + return lsIsDarwin ; +} + + +static int dirExists( char const * aDirPath ) +{ + DIR * lDir ; + if ( ! aDirPath || ! strlen( aDirPath ) ) + return 0 ; + lDir = opendir( aDirPath ) ; + if ( ! lDir ) + { + return 0 ; + } + closedir( lDir ) ; + return 1 ; +} + + +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 * getVersion( char const * aExecutable ) /*version must be first numeral*/ +{ + 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 * getMajorMinorPatch( char const * aExecutable ) +{ + static int lArray[3] ; + char * lTmp ; + + lTmp = (char *) getVersion(aExecutable); + lArray[0] = atoi( strtok(lTmp," ,.-") ) ; + /* printf("lArray0 %d\n", lArray[0]); */ + lArray[1] = atoi( strtok(0," ,.-") ) ; + /* printf("lArray1 %d\n", lArray[1]); */ + lArray[2] = atoi( strtok(0," ,.-") ) ; + /* printf("lArray2 %d\n", lArray[2]); */ + + if ( !lArray[0] && !lArray[1] && !lArray[2] ) return NULL; + return lArray ; +} + + +static int tryCommand( char const * aCommand ) +{ + char lBuff[MAX_PATH_OR_CMD] ; + FILE * lIn ; + + lIn = popen( aCommand , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL ) + { /* present */ + pclose( lIn ) ; + return 1 ; + } + else + { + pclose( lIn ) ; + return 0 ; + } + +} + + +static int isTerminalRunning(void) +{ + static int lIsTerminalRunning = -1 ; + if ( lIsTerminalRunning < 0 ) + { + lIsTerminalRunning = isatty(1); + if (tinyfd_verbose) printf("isTerminalRunning %d\n", lIsTerminalRunning ); + } + return lIsTerminalRunning; +} + + +static char * dialogNameOnly(void) +{ + 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(void) +{ + char const * lDialogName ; + char * lVersion ; + int lMajor ; + int lMinor ; + int lDate ; + int lResult ; + char * lMinorP ; + char * lLetter ; + char lBuff[128] ; + + /*char lTest[128] = " 0.9b-20031126" ;*/ + + lDialogName = dialogNameOnly() ; + if ( ! strlen(lDialogName) || !(lVersion = (char *) getVersion(lDialogName)) ) return 0 ; + /*lVersion = lTest ;*/ + /*printf("lVersion %s\n", lVersion);*/ + strcpy(lBuff,lVersion); + lMajor = atoi( strtok(lVersion," ,.-") ) ; + /*printf("lMajor %d\n", lMajor);*/ + lMinorP = strtok(0," ,.-abcdefghijklmnopqrstuvxyz"); + lMinor = atoi( lMinorP ) ; + /*printf("lMinor %d\n", lMinor );*/ + lDate = atoi( strtok(0," ,.-") ) ; + if (lDate<0) lDate = - lDate; + /*printf("lDate %d\n", lDate);*/ + lLetter = lMinorP + strlen(lMinorP) ; + strcpy(lVersion,lBuff); + strtok(lLetter," ,.-"); + /*printf("lLetter %s\n", lLetter);*/ + lResult = (lMajor > 0) || ( ( lMinor == 9 ) && (*lLetter == 'b') && (lDate >= 20031126) ); + /*printf("lResult %d\n", lResult);*/ + return lResult; +} + + +static int whiptailPresentOnly(void) +{ + static int lWhiptailPresent = -1 ; + if (!tinyfd_allowCursesDialogs) return 0; + if ( lWhiptailPresent < 0 ) + { + lWhiptailPresent = detectPresence( "whiptail" ) ; + } + return lWhiptailPresent ; +} + + +static char * terminalName(void) +{ + static char lTerminalName[128] = "*" ; + char lShellName[64] = "*" ; + int * lArray; + + if ( lTerminalName[0] == '*' ) + { + if ( detectPresence( "bash" ) ) + { + strcpy(lShellName , "bash -c " ) ; /*good for basic input*/ + } + else if ( strlen(dialogNameOnly()) || whiptailPresentOnly() ) + { + strcpy(lShellName , "sh -c " ) ; /*good enough for dialog & whiptail*/ + } + else + { + strcpy(lTerminalName , "" ) ; + return NULL ; + } + + if ( tfd_isDarwin() ) + { + if ( * strcpy(lTerminalName , "/opt/X11/bin/xterm" ) + && detectPresence( lTerminalName ) ) + { + strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else + { + strcpy(lTerminalName , "" ) ; + } + } + 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*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -x " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( * strcpy(lTerminalName,"lxterminal") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( * strcpy(lTerminalName,"konsole") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( * strcpy(lTerminalName,"kterm") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( * strcpy(lTerminalName,"tilix") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( * strcpy(lTerminalName,"xfce4-terminal") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -x " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( * strcpy(lTerminalName,"mate-terminal") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -x " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( * strcpy(lTerminalName,"Eterm") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( * strcpy(lTerminalName,"evilvte") /*good*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( * strcpy(lTerminalName,"pterm") /*good (only letters)*/ + && detectPresence(lTerminalName) ) + { + strcat(lTerminalName , " -e " ) ; + strcat(lTerminalName , lShellName ) ; + } + else if ( * strcpy(lTerminalName,"gnome-terminal") + && detectPresence(lTerminalName) && (lArray = getMajorMinorPatch(lTerminalName)) + && ((lArray[0]<3) || (lArray[0]==3 && lArray[1]<=6)) ) + { + strcat(lTerminalName , " --disable-factory -x " ) ; + strcat(lTerminalName , lShellName ) ; + } + else + { + strcpy(lTerminalName , "" ) ; + } + /* bad: koi rxterm guake tilda vala-terminal qterminal + aterm Terminal terminology sakura lilyterm weston-terminal + roxterm termit xvt rxvt mrxvt urxvt */ + } + if ( strlen(lTerminalName) ) + { + return lTerminalName ; + } + else + { + return NULL ; + } +} + + +static char * dialogName(void) +{ + char * lDialogName ; + lDialogName = dialogNameOnly( ) ; + if ( strlen(lDialogName) && ( isTerminalRunning() || terminalName() ) ) + { + return lDialogName ; + } + else + { + return NULL ; + } +} + + +static int whiptailPresent(void) +{ + int lWhiptailPresent ; + lWhiptailPresent = whiptailPresentOnly( ) ; + if ( lWhiptailPresent && ( isTerminalRunning() || terminalName() ) ) + { + return lWhiptailPresent ; + } + else + { + return 0 ; + } +} + + + +static int graphicMode(void) +{ + return !( tinyfd_forceConsole && (isTerminalRunning() || terminalName()) ) + && ( getenvDISPLAY() + || (tfd_isDarwin() && (!getenv("SSH_TTY") || getenvDISPLAY() ) ) ) ; +} + + +static int pactlPresent(void) +{ + static int lPactlPresent = -1 ; + if ( lPactlPresent < 0 ) + { + lPactlPresent = detectPresence("pactl") ; + } + return lPactlPresent ; +} + + +static int speakertestPresent(void) +{ + static int lSpeakertestPresent = -1 ; + if ( lSpeakertestPresent < 0 ) + { + lSpeakertestPresent = detectPresence("speaker-test") ; + } + return lSpeakertestPresent ; +} + + +static int playPresent() +{ + static int lPlayPresent = -1; + if (lPlayPresent < 0) + { + lPlayPresent = detectPresence("sox"); /*if sox is present, play is ready*/ + } + return lPlayPresent; +} + + +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 ) + { + lXmessagePresent = detectPresence("xmessage");/*if not tty,not on osxpath*/ + } + return lXmessagePresent && graphicMode( ) ; +} + + +static int gxmessagePresent(void) +{ + static int lGxmessagePresent = -1 ; + if ( lGxmessagePresent < 0 ) + { + lGxmessagePresent = detectPresence("gxmessage") ; + } + return lGxmessagePresent && graphicMode( ) ; +} + + +static int gmessagePresent(void) +{ + static int lGmessagePresent = -1 ; + if ( lGmessagePresent < 0 ) + { + lGmessagePresent = detectPresence("gmessage") ; + } + return lGmessagePresent && graphicMode( ) ; +} + + +static int notifysendPresent(void) +{ + static int lNotifysendPresent = -1 ; + if ( lNotifysendPresent < 0 ) + { + lNotifysendPresent = detectPresence("notify-send") ; + } + return lNotifysendPresent && graphicMode( ) ; +} + + +static int perlPresent(void) +{ + 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 ; +} + + +static int afplayPresent(void) +{ + static int lAfplayPresent = -1 ; + char lBuff[MAX_PATH_OR_CMD] ; + FILE * lIn ; + + if ( lAfplayPresent < 0 ) + { + lAfplayPresent = detectPresence("afplay") ; + if ( lAfplayPresent ) + { + lIn = popen( "test -e /System/Library/Sounds/Ping.aiff || echo Ping" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL ) + { + lAfplayPresent = 2 ; + } + pclose( lIn ) ; + if (tinyfd_verbose) printf("afplay %d\n", lAfplayPresent); + } + } + return graphicMode() ? lAfplayPresent : 0 ; +} + + +static int xdialogPresent(void) +{ + static int lXdialogPresent = -1 ; + if ( lXdialogPresent < 0 ) + { + lXdialogPresent = detectPresence("Xdialog") ; + } + return lXdialogPresent && graphicMode( ) ; +} + + +static int gdialogPresent(void) +{ + static int lGdialoglPresent = -1 ; + if ( lGdialoglPresent < 0 ) + { + lGdialoglPresent = detectPresence( "gdialog" ) ; + } + return lGdialoglPresent && graphicMode( ) ; +} + + +static int osascriptPresent(void) +{ + static int lOsascriptPresent = -1 ; + if ( lOsascriptPresent < 0 ) + { + gWarningDisplayed |= !!getenv("SSH_TTY"); + lOsascriptPresent = detectPresence( "osascript" ) ; + } + return lOsascriptPresent && graphicMode() && !getenv("SSH_TTY") ; +} + + +int tfd_qarmaPresent(void) +{ + static int lQarmaPresent = -1 ; + if ( lQarmaPresent < 0 ) + { + lQarmaPresent = detectPresence("qarma") ; + } + return lQarmaPresent && graphicMode( ) ; +} + + +int tfd_matedialogPresent(void) +{ + static int lMatedialogPresent = -1 ; + if ( lMatedialogPresent < 0 ) + { + lMatedialogPresent = detectPresence("matedialog") ; + } + return lMatedialogPresent && graphicMode( ) ; +} + + +int tfd_shellementaryPresent(void) +{ + static int lShellementaryPresent = -1 ; + if ( lShellementaryPresent < 0 ) + { + lShellementaryPresent = 0 ; /*detectPresence("shellementary"); shellementary is not ready yet */ + } + return lShellementaryPresent && graphicMode( ) ; +} + + +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 ) + { + lZenityPresent = detectPresence("zenity") ; + } + return lZenityPresent && graphicMode( ) ; +} + + +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] ; + FILE * lIn ; + int lIntTmp ; + + if ( lZenity3Present < 0 ) + { + lZenity3Present = 0 ; + if ( tfd_zenityPresent() ) + { + lIn = popen( "zenity --version" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + if ( atoi(lBuff) >= 3 ) + { + lZenity3Present = 3 ; + lIntTmp = atoi(strtok(lBuff,".")+2 ) ; + if ( lIntTmp >= 18 ) + { + lZenity3Present = 5 ; + } + else if ( lIntTmp >= 10 ) + { + lZenity3Present = 4 ; + } + } + else if ( ( atoi(lBuff) == 2 ) && ( atoi(strtok(lBuff,".")+2 ) >= 32 ) ) + { + lZenity3Present = 2 ; + } + if (tinyfd_verbose) printf("zenity type %d\n", lZenity3Present); + } + pclose( lIn ) ; + } + } + return graphicMode() ? lZenity3Present : 0 ; +} + + +int tfd_kdialogPresent(void) +{ + static int lKdialogPresent = -1 ; + char lBuff[MAX_PATH_OR_CMD] ; + FILE * lIn ; + char * lDesktop; + + if ( lKdialogPresent < 0 ) + { + if ( tfd_zenityPresent() ) + { + lDesktop = getenv("XDG_SESSION_DESKTOP"); + if ( !lDesktop || ( strcmp(lDesktop, "KDE") && strcmp(lDesktop, "lxqt") ) ) + { + lKdialogPresent = 0 ; + return lKdialogPresent ; + } + } + + lKdialogPresent = detectPresence("kdialog") ; + if ( lKdialogPresent && !getenv("SSH_TTY") ) + { + lIn = popen( "kdialog --attach 2>&1" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + if ( ! strstr( "Unknown" , lBuff ) ) + { + lKdialogPresent = 2 ; + if (tinyfd_verbose) printf("kdialog-attach %d\n", lKdialogPresent); + } + } + pclose( lIn ) ; + + if (lKdialogPresent == 2) + { + lKdialogPresent = 1 ; + lIn = popen( "kdialog --passivepopup 2>&1" , "r" ) ; + if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + if ( ! strstr( "Unknown" , lBuff ) ) + { + lKdialogPresent = 2 ; + if (tinyfd_verbose) printf("kdialog-popup %d\n", lKdialogPresent); + } + } + pclose( lIn ) ; + } + } + } + return graphicMode() ? lKdialogPresent : 0 ; +} + + +static int osx9orBetter(void) +{ + static int lOsx9orBetter = -1 ; + char lBuff[MAX_PATH_OR_CMD] ; + FILE * lIn ; + int V,v; + + if ( lOsx9orBetter < 0 ) + { + lOsx9orBetter = 0 ; + lIn = popen( "osascript -e 'set osver to system version of (system info)'" , "r" ) ; + if ( ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + && ( 2 == sscanf(lBuff, "%d.%d", &V, &v) ) ) + { + V = V * 100 + v; + if ( V >= 1009 ) + { + lOsx9orBetter = 1 ; + } + } + pclose( lIn ) ; + if (tinyfd_verbose) printf("Osx10 = %d, %d = %s\n", lOsx9orBetter, V, lBuff) ; + } + return lOsx9orBetter ; +} + + +static int python3Present(void) +{ + static int lPython3Present = -1 ; + int i; + + if ( lPython3Present < 0 ) + { + lPython3Present = 0 ; + strcpy(gPython3Name , "python3" ) ; + if ( detectPresence(gPython3Name) ) lPython3Present = 1; + else + { + for ( i = 9 ; i >= 0 ; i -- ) + { + sprintf( gPython3Name , "python3.%d" , i ) ; + if ( detectPresence(gPython3Name) ) + { + lPython3Present = 1; + break; + } + } + } + if (tinyfd_verbose) printf("lPython3Present %d\n", lPython3Present) ; + if (tinyfd_verbose) printf("gPython3Name %s\n", gPython3Name) ; + } + return lPython3Present ; +} + + +static int python2Present(void) +{ + 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 (tinyfd_verbose) printf("lPython2Present %d\n", lPython2Present) ; + if (tinyfd_verbose) printf("gPython2Name %s\n", gPython2Name) ; + } + return lPython2Present ; +} + + +static int tkinter3Present(void) +{ + static int lTkinter3Present = -1 ; + char lPythonCommand[256]; + char lPythonParams[128] = + "-S -c \"try:\n\timport tkinter;\nexcept:\n\tprint(0);\""; + + if ( lTkinter3Present < 0 ) + { + lTkinter3Present = 0 ; + if ( python3Present() ) + { + sprintf( lPythonCommand , "%s %s" , gPython3Name , lPythonParams ) ; + lTkinter3Present = tryCommand(lPythonCommand) ; + } + if (tinyfd_verbose) printf("lTkinter3Present %d\n", lTkinter3Present) ; + } + return lTkinter3Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") ); +} + + +static int tkinter2Present(void) +{ + 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 (lPythonDbusPresent < 0 ) + { + lPythonDbusPresent = 0 ; + if ( python2Present() ) + { + strcpy(gPythonName , gPython2Name ) ; + sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ; + lPythonDbusPresent = tryCommand(lPythonCommand) ; + } + + if ( !lPythonDbusPresent && python3Present() ) + { + strcpy(gPythonName , gPython3Name ) ; + sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ; + lPythonDbusPresent = tryCommand(lPythonCommand) ; + } + + if (tinyfd_verbose) printf("lPythonDbusPresent %d\n", lPythonDbusPresent) ; + if (tinyfd_verbose) printf("gPythonName %s\n", gPythonName) ; + } + return lPythonDbusPresent && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") ); +} + + +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) +{ + char lDialogString[256] ; + FILE * lIn ; + + if ( osascriptPresent() ) + { + if ( afplayPresent() >= 2 ) + { + strcpy( lDialogString , "afplay /System/Library/Sounds/Ping.aiff") ; + } + else + { + strcpy( lDialogString , "osascript -e 'tell application \"System Events\" to beep'") ; + } + } + 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() ) + { + /*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 .4; kill -9 $pid" ) ; /*.3 was too short for mac g3*/ + } + else if (beepexePresent()) + { + 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 + { + strcpy( lDialogString , "printf '\a' > /dev/tty" ) ; + } + + if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; + + if ( ( lIn = popen( lDialogString , "r" ) ) ) + { + pclose( lIn ) ; + } + + if ( pactlPresent() ) + { + signal(SIGINT, SIG_DFL); + } +} + + +int tinyfd_messageBox( + 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 * lDialogString = NULL ; + char * lpDialogString; + FILE * lIn ; + int lWasGraphicDialog = 0 ; + int lWasXterm = 0 ; + int lResult ; + char lChar ; + struct termios infoOri; + struct termios info; + size_t lTitleLen ; + size_t lMessageLen ; + + 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") ) + { + lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); + } + + if ( osascriptPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;} + + strcpy( lDialogString , "osascript "); + if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat( lDialogString , " -e 'try' -e 'set {vButton} to {button returned} of ( display dialog \"") ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString, "\" ") ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + strcat(lDialogString, "with icon ") ; + if ( aIconType && ! strcmp( "error" , aIconType ) ) + { + strcat(lDialogString, "stop " ) ; + } + else if ( aIconType && ! strcmp( "warning" , aIconType ) ) + { + strcat(lDialogString, "caution " ) ; + } + else /* question or info */ + { + strcat(lDialogString, "note " ) ; + } + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + if ( ! aDefaultButton ) + { + strcat( lDialogString ,"default button \"Cancel\" " ) ; + } + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + strcat( lDialogString ,"buttons {\"No\", \"Yes\"} " ) ; + if (aDefaultButton) + { + strcat( lDialogString ,"default button \"Yes\" " ) ; + } + else + { + strcat( lDialogString ,"default button \"No\" " ) ; + } + strcat( lDialogString ,"cancel button \"No\"" ) ; + } + else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString ,"buttons {\"No\", \"Yes\", \"Cancel\"} " ) ; + switch (aDefaultButton) + { + case 1: strcat( lDialogString ,"default button \"Yes\" " ) ; break; + case 2: strcat( lDialogString ,"default button \"No\" " ) ; break; + case 0: strcat( lDialogString ,"default button \"Cancel\" " ) ; break; + } + strcat( lDialogString ,"cancel button \"Cancel\"" ) ; + } + else + { + strcat( lDialogString ,"buttons {\"OK\"} " ) ; + strcat( lDialogString ,"default button \"OK\" " ) ; + } + strcat( lDialogString, ")' ") ; + + strcat( lDialogString, +"-e 'if vButton is \"Yes\" then' -e 'return 1'\ + -e 'else if vButton is \"OK\" then' -e 'return 1'\ + -e 'else if vButton is \"No\" then' -e 'return 2'\ + -e 'else' -e 'return 0' -e 'end if' " ); + + strcat( lDialogString, "-e 'on error number -128' " ) ; + strcat( lDialogString, "-e '0' " ); + + strcat( lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( tfd_kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} + + strcpy( lDialogString , "kdialog" ) ; + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) + { + strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ + } + + strcat( lDialogString , " --" ) ; + if ( aDialogType && ( ! strcmp( "okcancel" , aDialogType ) + || ! strcmp( "yesno" , aDialogType ) || ! strcmp( "yesnocancel" , aDialogType ) ) ) + { + if ( aIconType && ( ! strcmp( "warning" , aIconType ) + || ! strcmp( "error" , aIconType ) ) ) + { + strcat( lDialogString , "warning" ) ; + } + if ( ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , "yesnocancel" ) ; + } + else + { + strcat( lDialogString , "yesno" ) ; + } + } + else if ( aIconType && ! strcmp( "error" , aIconType ) ) + { + strcat( lDialogString , "error" ) ; + } + else if ( aIconType && ! strcmp( "warning" , aIconType ) ) + { + strcat( lDialogString , "sorry" ) ; + } + else + { + strcat( lDialogString , "msgbox" ) ; + } + strcat( lDialogString , " \"" ) ; + if ( aMessage ) + { + strcat( lDialogString , aMessage ) ; + } + strcat( lDialogString , "\"" ) ; + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + strcat( lDialogString , + " --yes-label Ok --no-label Cancel" ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + + if ( ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , "; x=$? ;if [ $x = 0 ] ;then echo 1;elif [ $x = 1 ] ;then echo 2;else echo 0;fi"); + } + else + { + strcat( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi"); + } + } + else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) + { + if ( tfd_zenityPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;} + strcpy( lDialogString , "szAnswer=$(zenity" ) ; + 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 ( tfd_matedialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return 1;} + strcpy( lDialogString , "szAnswer=$(matedialog" ) ; + } + else if ( tfd_shellementaryPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return 1;} + strcpy( lDialogString , "szAnswer=$(shellementary" ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return 1;} + strcpy( lDialogString , "szAnswer=$(qarma" ) ; + 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, " --"); + + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + strcat( lDialogString , + "question --ok-label=Ok --cancel-label=Cancel" ) ; + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + strcat( lDialogString , "question" ) ; + } + else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , "list --column \"\" --hide-header \"Yes\" \"No\"" ) ; + } + else if ( aIconType && ! strcmp( "error" , aIconType ) ) + { + strcat( lDialogString , "error" ) ; + } + else if ( aIconType && ! strcmp( "warning" , aIconType ) ) + { + strcat( lDialogString , "warning" ) ; + } + else + { + strcat( lDialogString , "info" ) ; + } + + 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 ) + || ! strcmp( "error" , aIconType ) + || ! strcmp( "warning" , aIconType ) ) ) + { + strcat( lDialogString , aIconType ) ; + } + else + { + strcat( lDialogString , "information" ) ; + } + } + + if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null "); + + if ( ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , +");if [ $? = 1 ];then echo 0;elif [ $szAnswer = \"No\" ];then echo 2;else echo 1;fi"); + } + else + { + strcat( lDialogString , ");if [ $? = 0 ];then echo 1;else echo 0;fi"); + } + } + + 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 , "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 ( 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=tkMessageBox." ) ; + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + strcat( lDialogString , "askokcancel(" ) ; + if ( aDefaultButton ) + { + strcat( lDialogString , "default=tkMessageBox.OK," ) ; + } + else + { + strcat( lDialogString , "default=tkMessageBox.CANCEL," ) ; + } + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + strcat( lDialogString , "askyesno(" ) ; + if ( aDefaultButton ) + { + strcat( lDialogString , "default=tkMessageBox.YES," ) ; + } + else + { + strcat( lDialogString , "default=tkMessageBox.NO," ) ; + } + } + else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) ) + { + strcat( lDialogString , "askyesnocancel(" ) ; + switch ( aDefaultButton ) + { + case 1: strcat( lDialogString , "default=tkMessageBox.YES," ); break; + case 2: strcat( lDialogString , "default=tkMessageBox.NO," ); break; + case 0: strcat( lDialogString , "default=tkMessageBox.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() && xmessagePresent()) ) + { + if ( gxmessagePresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return 1;} + strcpy( lDialogString , "gxmessage"); + } + else if ( gmessagePresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return 1;} + strcpy( lDialogString , "gmessage"); + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xmessage");return 1;} + strcpy( lDialogString , "xmessage"); + } + + if ( aDialogType && ! strcmp("okcancel" , aDialogType) ) + { + strcat( lDialogString , " -buttons Ok:1,Cancel:0"); + switch ( aDefaultButton ) + { + case 1: strcat( lDialogString , " -default Ok"); break; + case 0: strcat( lDialogString , " -default Cancel"); break; + } + } + else if ( aDialogType && ! strcmp("yesno" , aDialogType) ) + { + strcat( lDialogString , " -buttons Yes:1,No:0"); + switch ( aDefaultButton ) + { + case 1: strcat( lDialogString , " -default Yes"); break; + case 0: strcat( lDialogString , " -default No"); break; + } + } + else if ( aDialogType && ! strcmp("yesnocancel" , aDialogType) ) + { + strcat( lDialogString , " -buttons Yes:1,No:2,Cancel:0"); + switch ( aDefaultButton ) + { + case 1: strcat( lDialogString , " -default Yes"); break; + case 2: strcat( lDialogString , " -default No"); break; + case 0: strcat( lDialogString , " -default Cancel"); break; + } + } + else + { + strcat( lDialogString , " -buttons Ok:1"); + strcat( lDialogString , " -default Ok"); + } + + strcat( lDialogString , " -center \""); + if ( aMessage && strlen(aMessage) ) + { + strcat( lDialogString , aMessage ) ; + } + strcat(lDialogString, "\"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat( lDialogString , " -title \""); + strcat( lDialogString , aTitle ) ; + strcat( lDialogString, "\"" ) ; + } + strcat( lDialogString , " ; echo $? "); + } + else if ( xdialogPresent() || gdialogPresent() || dialogName() || whiptailPresent() ) + { + if ( gdialogPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return 1;} + lWasGraphicDialog = 1 ; + strcpy( lDialogString , "(gdialog " ) ; + } + else if ( xdialogPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return 1;} + lWasGraphicDialog = 1 ; + strcpy( lDialogString , "(Xdialog " ) ; + } + else if ( dialogName( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return 0;} + if ( isTerminalRunning( ) ) + { + strcpy( lDialogString , "(dialog " ) ; + } + else + { + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(" ) ; + strcat( lDialogString , dialogName() ) ; + strcat( lDialogString , " " ) ; + } + } + else if ( isTerminalRunning( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;} + strcpy( lDialogString , "(whiptail " ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;} + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(whiptail " ) ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + if ( !xdialogPresent() && !gdialogPresent() ) + { + if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType ) + || !strcmp( "yesnocancel" , aDialogType ) ) ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab: move focus") ; + strcat(lDialogString, "\" ") ; + } + } + + if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) ) + { + if ( ! aDefaultButton ) + { + strcat( lDialogString , "--defaultno " ) ; + } + strcat( lDialogString , + "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ; + } + else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) ) + { + if ( ! aDefaultButton ) + { + strcat( lDialogString , "--defaultno " ) ; + } + strcat( lDialogString , "--yesno " ) ; + } + else if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + if (!aDefaultButton) + { + strcat(lDialogString, "--defaultno "); + } + strcat(lDialogString, "--menu "); + } + else + { + strcat( lDialogString , "--msgbox " ) ; + + } + strcat( lDialogString , "\"" ) ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString, "\" "); + + if ( lWasGraphicDialog ) + { + if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + strcat(lDialogString,"0 60 0 Yes \"\" No \"\") 2>/tmp/tinyfd.txt;\ +if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ +tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; + } + else + { + strcat(lDialogString, + "10 60 ) 2>&1;if [ $? = 0 ];then echo 1;else echo 0;fi"); + } + } + else + { + if (aDialogType && !strcmp("yesnocancel", aDialogType)) + { + strcat(lDialogString,"0 60 0 Yes \"\" No \"\" >/dev/tty ) 2>/tmp/tinyfd.txt;\ + if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ + tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; + + if ( lWasXterm ) + { + strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt"); + } + else + { + strcat(lDialogString, "; clear >/dev/tty") ; + } + } + else + { + strcat(lDialogString, "10 60 >/dev/tty) 2>&1;if [ $? = 0 ];"); + if ( lWasXterm ) + { + strcat( lDialogString , +"then\n\techo 1\nelse\n\techo 0\nfi >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); + } + else + { + strcat(lDialogString, + "then echo 1;else echo 0;fi;clear >/dev/tty"); + } + } + } + } + else if ( !isTerminalRunning() && terminalName() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;} + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'" ) ; + if ( !gWarningDisplayed && !tinyfd_forceConsole) + { + gWarningDisplayed = 1 ; + strcat( lDialogString , "echo \"" ) ; + strcat( lDialogString, gTitle) ; + strcat( lDialogString , "\";" ) ; + strcat( lDialogString , "echo \"" ) ; + strcat( lDialogString, tinyfd_needs) ; + strcat( lDialogString , "\";echo;echo;" ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat( lDialogString , "echo \"" ) ; + strcat( lDialogString, aTitle) ; + strcat( lDialogString , "\";echo;" ) ; + } + if ( aMessage && strlen(aMessage) ) + { + strcat( lDialogString , "echo \"" ) ; + strcat( lDialogString, aMessage) ; + strcat( lDialogString , "\"; " ) ; + } + if ( aDialogType && !strcmp("yesno",aDialogType) ) + { + strcat( lDialogString , "echo -n \"y/n: \"; " ) ; + strcat( lDialogString , "stty sane -echo;" ) ; + strcat( lDialogString , + "answer=$( while ! head -c 1 | grep -i [ny];do true ;done);"); + strcat( lDialogString , + "if echo \"$answer\" | grep -iq \"^y\";then\n"); + strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ; + } + else if ( aDialogType && !strcmp("okcancel",aDialogType) ) + { + strcat( lDialogString , "echo -n \"[O]kay/[C]ancel: \"; " ) ; + strcat( lDialogString , "stty sane -echo;" ) ; + strcat( lDialogString , + "answer=$( while ! head -c 1 | grep -i [oc];do true ;done);"); + strcat( lDialogString , + "if echo \"$answer\" | grep -iq \"^o\";then\n"); + strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ; + } + else if ( aDialogType && !strcmp("yesnocancel",aDialogType) ) + { + strcat( lDialogString , "echo -n \"[Y]es/[N]o/[C]ancel: \"; " ) ; + strcat( lDialogString , "stty sane -echo;" ) ; + strcat( lDialogString , + "answer=$( while ! head -c 1 | grep -i [nyc];do true ;done);"); + strcat( lDialogString , + "if echo \"$answer\" | grep -iq \"^y\";then\n\techo 1\n"); + strcat( lDialogString , "elif echo \"$answer\" | grep -iq \"^n\";then\n\techo 2\n" ) ; + strcat( lDialogString , "else\n\techo 0\nfi" ) ; + } + else + { + strcat(lDialogString , "echo -n \"press enter to continue \"; "); + strcat( lDialogString , "stty sane -echo;" ) ; + strcat( lDialogString , + "answer=$( while ! head -c 1;do true ;done);echo 1"); + } + strcat( lDialogString , + " >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); + } + else if ( !isTerminalRunning() && pythonDbusPresent() && !strcmp("ok" , aDialogType) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python-dbus");return 1;} + strcpy( lDialogString , gPythonName ) ; + strcat( lDialogString ," -c \"import dbus;bus=dbus.SessionBus();"); + strcat( lDialogString ,"notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');" ) ; + strcat( lDialogString ,"notify=dbus.Interface(notif,'org.freedesktop.Notifications');" ) ; + strcat( lDialogString ,"notify.Notify('',0,'" ) ; + if ( aIconType && strlen(aIconType) ) + { + strcat( lDialogString , aIconType ) ; + } + strcat(lDialogString, "','") ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + } + strcat(lDialogString, "','") ; + if ( aMessage && strlen(aMessage) ) + { + lpDialogString = lDialogString + strlen(lDialogString); + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + } + strcat(lDialogString, "','','',5000)\"") ; + } + else if ( !isTerminalRunning() && (perlPresent() >= 2) && !strcmp("ok" , aDialogType) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;} + + 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) ) + { + + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notifysend");return 1;} + strcpy( lDialogString , "notify-send" ) ; + if ( aIconType && strlen(aIconType) ) + { + strcat( lDialogString , " -i '" ) ; + strcat( lDialogString , aIconType ) ; + strcat( lDialogString , "'" ) ; + } + strcat( lDialogString , " \"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + strcat( lDialogString , " | " ) ; + } + if ( aMessage && strlen(aMessage) ) + { + tfd_replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ; + tfd_replaceSubStr( aMessage , "\n" , " | " , lBuff ) ; + tfd_replaceSubStr( aMessage , "\t" , " " , lBuff ) ; + strcat(lDialogString, lBuff) ; + } + strcat( lDialogString , "\"" ) ; + } + 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", aTitle); + } + + tcgetattr(0, &infoOri); + tcgetattr(0, &info); + info.c_lflag &= ~ICANON; + info.c_cc[VMIN] = 1; + info.c_cc[VTIME] = 0; + tcsetattr(0, TCSANOW, &info); + if ( aDialogType && !strcmp("yesno",aDialogType) ) + { + do + { + if ( aMessage && strlen(aMessage) ) + { + printf("\n%s\n",aMessage); + } + printf("y/n: "); fflush(stdout); + lChar = tolower( getchar() ) ; + printf("\n\n"); + } + while ( lChar != 'y' && lChar != 'n' ); + lResult = lChar == 'y' ? 1 : 0 ; + } + else if ( aDialogType && !strcmp("okcancel",aDialogType) ) + { + do + { + if ( aMessage && strlen(aMessage) ) + { + printf("\n%s\n",aMessage); + } + printf("[O]kay/[C]ancel: "); fflush(stdout); + lChar = tolower( getchar() ) ; + printf("\n\n"); + } + while ( lChar != 'o' && lChar != 'c' ); + lResult = lChar == 'o' ? 1 : 0 ; + } + else if ( aDialogType && !strcmp("yesnocancel",aDialogType) ) + { + do + { + if ( aMessage && strlen(aMessage) ) + { + printf("\n%s\n",aMessage); + } + printf("[Y]es/[N]o/[C]ancel: "); fflush(stdout); + lChar = tolower( getchar() ) ; + printf("\n\n"); + } + while ( lChar != 'y' && lChar != 'n' && lChar != 'c' ); + lResult = (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0 ; + } + else + { + if ( aMessage && strlen(aMessage) ) + { + printf("\n%s\n\n",aMessage); + } + printf("press enter to continue "); fflush(stdout); + getchar() ; + printf("\n\n"); + lResult = 1 ; + } + tcsetattr(0, TCSANOW, &infoOri); + free(lDialogString); + return lResult ; + } + + if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; + + if ( ! ( lIn = popen( lDialogString , "r" ) ) ) + { + free(lDialogString); + return 0 ; + } + while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + {} + + pclose( lIn ) ; + + /* printf( "lBuff: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ + if ( lBuff[strlen( lBuff ) -1] == '\n' ) + { + lBuff[strlen( lBuff ) -1] = '\0' ; + } + /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ + + 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"); + } + } + /* printf( "lBuff2: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ + + lResult = !strcmp( lBuff , "2" ) ? 2 : !strcmp( lBuff , "1" ) ? 1 : 0; + + /* printf( "lResult: %d\n" , lResult ) ; */ + free(lDialogString); + return lResult ; +} + + +/* return has only meaning for tinyfd_query */ +int tinyfd_notifyPopup( + 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 ; + char * lpDialogString ; + FILE * lIn ; + 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); + } + + lTitleLen = aTitle ? strlen(aTitle) : 0 ; + lMessageLen = aMessage ? strlen(aMessage) : 0 ; + if ( !aTitle || strcmp(aTitle,"tinyfd_query") ) + { + lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); + } + + if ( osascriptPresent( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;} + + strcpy( lDialogString , "osascript "); + if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat( lDialogString , " -e 'try' -e 'display notification \"") ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString, " \" ") ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + strcat( lDialogString, "' -e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( tfd_kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;} + strcpy( lDialogString , "kdialog" ) ; + + if ( aIconType && strlen(aIconType) ) + { + strcat( lDialogString , " --icon '" ) ; + strcat( lDialogString , aIconType ) ; + strcat( lDialogString , "'" ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat( lDialogString , " --title \"" ) ; + strcat( lDialogString , aTitle ) ; + strcat( lDialogString , "\"" ) ; + } + + strcat( lDialogString , " --passivepopup" ) ; + strcat( lDialogString , " \"" ) ; + if ( aMessage ) + { + strcat( lDialogString , aMessage ) ; + } + strcat( lDialogString , " \" 5" ) ; + } + 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 ( tfd_zenity3Present()>=5 ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;} + strcpy( lDialogString , "zenity" ) ; + } + + strcat( lDialogString , " --notification"); + + if ( aIconType && strlen( aIconType ) ) + { + strcat( lDialogString , " --window-icon '"); + strcat( lDialogString , aIconType ) ; + strcat( lDialogString , "'" ) ; + } + + strcat( lDialogString , " --text \"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\n") ; + } + if ( aMessage && strlen( aMessage ) ) + { + strcat( lDialogString , aMessage ) ; + } + strcat( lDialogString , " \"" ) ; + } + else if ( perlPresent() >= 2 ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;} + + 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( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python-dbus");return 1;} + strcpy( lDialogString , gPythonName ) ; + strcat( lDialogString ," -c \"import dbus;bus=dbus.SessionBus();"); + strcat( lDialogString ,"notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');" ) ; + strcat( lDialogString ,"notify=dbus.Interface(notif,'org.freedesktop.Notifications');" ) ; + strcat( lDialogString ,"notify.Notify('',0,'" ) ; + if ( aIconType && strlen(aIconType) ) + { + strcat( lDialogString , aIconType ) ; + } + strcat(lDialogString, "','") ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + } + strcat(lDialogString, "','") ; + if ( aMessage && strlen(aMessage) ) + { + lpDialogString = lDialogString + strlen(lDialogString); + tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ; + } + strcat(lDialogString, "','','',5000)\"") ; + } + else if ( notifysendPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notifysend");return 1;} + strcpy( lDialogString , "notify-send" ) ; + if ( aIconType && strlen(aIconType) ) + { + strcat( lDialogString , " -i '" ) ; + strcat( lDialogString , aIconType ) ; + strcat( lDialogString , "'" ) ; + } + strcat( lDialogString , " \"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + strcat( lDialogString , " | " ) ; + } + if ( aMessage && strlen(aMessage) ) + { + tfd_replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ; + tfd_replaceSubStr( aMessage , "\n" , " | " , lBuff ) ; + tfd_replaceSubStr( aMessage , "\t" , " " , lBuff ) ; + strcat(lDialogString, lBuff) ; + } + strcat( lDialogString , "\"" ) ; + } + else + { + return tinyfd_messageBox(aTitle, aMessage, "ok", aIconType, 0); + } + + if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; + + if ( ! ( lIn = popen( lDialogString , "r" ) ) ) + { + free(lDialogString); + return 0 ; + } + + pclose( lIn ) ; + free(lDialogString); + return 1; +} + + +/* returns NULL on cancel */ +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; + char * lpDialogString; + FILE * lIn ; + int lResult ; + int lWasGdialog = 0 ; + int lWasGraphicDialog = 0 ; + int lWasXterm = 0 ; + int lWasBasicXterm = 0 ; + struct termios oldt ; + struct termios newt ; + char * lEOF; + 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") ) + { + lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen ); + } + + if ( osascriptPresent( ) ) + { + 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 \"") ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString, "\" ") ; + strcat(lDialogString, "default answer \"") ; + if ( aDefaultInput && strlen(aDefaultInput) ) + { + strcat(lDialogString, aDefaultInput) ; + } + strcat(lDialogString, "\" ") ; + if ( ! aDefaultInput ) + { + strcat(lDialogString, "hidden answer true ") ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + strcat(lDialogString, "with icon note' ") ; + strcat(lDialogString, "-e '\"1\" & text returned of result' " ); + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e '0' " ); + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat(lDialogString, " -e 'end tell'") ; + } + else if ( tfd_kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} + strcpy( lDialogString , "szAnswer=$(kdialog" ) ; + + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) + { + strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ + } + + if ( ! aDefaultInput ) + { + strcat(lDialogString, " --password ") ; + } + else + { + strcat(lDialogString, " --inputbox ") ; + + } + strcat(lDialogString, "\"") ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage ) ; + } + strcat(lDialogString , "\" \"" ) ; + if ( aDefaultInput && strlen(aDefaultInput) ) + { + strcat(lDialogString, aDefaultInput ) ; + } + strcat(lDialogString , "\"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + strcat( lDialogString , + ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi"); + } + else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) + { + if ( tfd_zenityPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} + strcpy( lDialogString , "szAnswer=$(zenity" ) ; + 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 ( tfd_matedialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} + strcpy( lDialogString , "szAnswer=$(matedialog" ) ; + } + else if ( tfd_shellementaryPresent() ) + { + 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 *)1;} + strcpy( lDialogString , "szAnswer=$(qarma" ) ; + 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" ) ; + + 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=\"") ; + 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 (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 *)1;} + strcpy( lDialogString , "szAnswer=$(gxmessage -buttons Ok:1,Cancel:0 -center \""); + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return (char *)1;} + strcpy( lDialogString , "szAnswer=$(gmessage -buttons Ok:1,Cancel:0 -center \""); + } + + if ( aMessage && strlen(aMessage) ) + { + strcat( lDialogString , aMessage ) ; + } + strcat(lDialogString, "\"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat( lDialogString , " -title \""); + strcat( lDialogString , aTitle ) ; + strcat(lDialogString, "\" " ) ; + } + strcat(lDialogString, " -entrytext \"" ) ; + if ( aDefaultInput && strlen(aDefaultInput) ) + { + strcat( lDialogString , aDefaultInput ) ; + } + strcat(lDialogString, "\"" ) ; + strcat( lDialogString , ");echo $?$szAnswer"); + } + 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 *)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 ( 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) ) + { + 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() || dialogName() || whiptailPresent() ) + { + if ( gdialogPresent( ) ) + { + 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 *)1;} + lWasGraphicDialog = 1 ; + strcpy( lDialogString , "(Xdialog " ) ; + } + else if ( dialogName( ) ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} + if ( isTerminalRunning( ) ) + { + strcpy( lDialogString , "(dialog " ) ; + } + else + { + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(" ) ; + strcat( lDialogString , dialogName() ) ; + strcat( lDialogString , " " ) ; + } + } + else if ( isTerminalRunning( ) ) + { + 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 *)0;} + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(whiptail " ) ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, "tab: move focus") ; + if ( ! aDefaultInput && !lWasGdialog ) + { + strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ; + } + strcat(lDialogString, "\" ") ; + } + + if ( aDefaultInput || lWasGdialog ) + { + strcat( lDialogString , "--inputbox" ) ; + } + else + { + if ( !lWasGraphicDialog && dialogName() && isDialogVersionBetter09b() ) + { + strcat( lDialogString , "--insecure " ) ; + } + strcat( lDialogString , "--passwordbox" ) ; + } + strcat( lDialogString , " \"" ) ; + if ( aMessage && strlen(aMessage) ) + { + strcat(lDialogString, aMessage) ; + } + strcat(lDialogString,"\" 10 60 ") ; + if ( aDefaultInput && strlen(aDefaultInput) ) + { + strcat(lDialogString, "\"") ; + strcat(lDialogString, aDefaultInput) ; + strcat(lDialogString, "\" ") ; + } + if ( lWasGraphicDialog ) + { + strcat(lDialogString,") 2>/tmp/tinyfd.txt;\ + if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ + tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; + } + else + { + strcat(lDialogString,">/dev/tty ) 2>/tmp/tinyfd.txt;\ + if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\ + tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ; + + if ( lWasXterm ) + { + strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt"); + } + else + { + strcat(lDialogString, "; clear >/dev/tty") ; + } + } + } + else if ( ! isTerminalRunning( ) && terminalName() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;} + lWasBasicXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'" ) ; + if ( !gWarningDisplayed && !tinyfd_forceConsole) + { + gWarningDisplayed = 1 ; + tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); + } + if ( aTitle && strlen(aTitle) && !tinyfd_forceConsole) + { + strcat( lDialogString , "echo \"" ) ; + strcat( lDialogString, aTitle) ; + strcat( lDialogString , "\";echo;" ) ; + } + + strcat( lDialogString , "echo \"" ) ; + if ( aMessage && strlen(aMessage) ) + { + strcat( lDialogString, aMessage) ; + } + strcat( lDialogString , "\";read " ) ; + if ( ! aDefaultInput ) + { + strcat( lDialogString , "-s " ) ; + } + strcat( lDialogString , "-p \"" ) ; + strcat( lDialogString , "(esc+enter to cancel): \" ANSWER " ) ; + strcat( lDialogString , ";echo 1$ANSWER >/tmp/tinyfd.txt';" ) ; + strcat( lDialogString , "cat -v /tmp/tinyfd.txt"); + } + 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 *)0;} + free(lDialogString); + return NULL; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;} + if ( !gWarningDisplayed && !tinyfd_forceConsole) + { + gWarningDisplayed = 1 ; + tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0); + } + if ( aTitle && strlen(aTitle) ) + { + printf("\n%s\n", aTitle); + } + if ( aMessage && strlen(aMessage) ) + { + printf("\n%s\n",aMessage); + } + printf("(esc+enter to cancel): "); fflush(stdout); + if ( ! aDefaultInput ) + { + tcgetattr(STDIN_FILENO, & oldt) ; + newt = oldt ; + newt.c_lflag &= ~ECHO ; + tcsetattr(STDIN_FILENO, TCSANOW, & newt); + } + + lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); + /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */ + if ( ! lEOF || (lBuff[0] == '\0') ) + { + free(lDialogString); + return NULL; + } + + if ( lBuff[0] == '\n' ) + { + lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin); + /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */ + if ( ! lEOF || (lBuff[0] == '\0') ) + { + free(lDialogString); + return NULL; + } + } + + if ( ! aDefaultInput ) + { + tcsetattr(STDIN_FILENO, TCSANOW, & oldt); + printf("\n"); + } + printf("\n"); + if ( strchr(lBuff,27) ) + { + free(lDialogString); + return NULL ; + } + if ( lBuff[strlen( lBuff ) -1] == '\n' ) + { + lBuff[strlen( lBuff ) -1] = '\0' ; + } + free(lDialogString); + return lBuff ; + } + + if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ; + lIn = popen( lDialogString , "r" ); + if ( ! lIn ) + { + if ( fileExists("/tmp/tinyfd.txt") ) + { + wipefile("/tmp/tinyfd.txt"); + remove("/tmp/tinyfd.txt"); + } + if ( fileExists("/tmp/tinyfd0.txt") ) + { + wipefile("/tmp/tinyfd0.txt"); + remove("/tmp/tinyfd0.txt"); + } + free(lDialogString); + return NULL ; + } + while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + {} + + pclose( lIn ) ; + + if ( fileExists("/tmp/tinyfd.txt") ) + { + wipefile("/tmp/tinyfd.txt"); + remove("/tmp/tinyfd.txt"); + } + if ( fileExists("/tmp/tinyfd0.txt") ) + { + wipefile("/tmp/tinyfd0.txt"); + remove("/tmp/tinyfd0.txt"); + } + + /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */ + /* printf( "lBuff0: %s\n" , lBuff ) ; */ + if ( lBuff[strlen( lBuff ) -1] == '\n' ) + { + lBuff[strlen( lBuff ) -1] = '\0' ; + } + /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */ + if ( lWasBasicXterm ) + { + if ( strstr(lBuff,"^[") ) /* esc was pressed */ + { + free(lDialogString); + return NULL ; + } + } + + lResult = strncmp( lBuff , "1" , 1) ? 0 : 1 ; + /* printf( "lResult: %d \n" , lResult ) ; */ + if ( ! lResult ) + { + free(lDialogString); + return NULL ; + } + + /* printf( "lBuff+1: %s\n" , lBuff+1 ) ; */ + free(lDialogString); + return lBuff+1 ; +} + + +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] ; + int i ; + int lWasGraphicDialog = 0 ; + int lWasXterm = 0 ; + 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 *)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 " ); + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with prompt \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "default location \"") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "\" " ) ; + } + getLastName( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "default name \"") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "\" " ) ; + } + strcat( lDialogString , ")' " ) ; + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( tfd_kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} + + strcpy( lDialogString , "kdialog" ) ; + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) + { + strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ + } + strcat( lDialogString , " --getsavefilename " ) ; + + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + if ( aDefaultPathAndFile[0] != '/' ) + { + strcat(lDialogString, "$PWD/") ; + } + strcat(lDialogString, "\"") ; + strcat(lDialogString, aDefaultPathAndFile ) ; + strcat(lDialogString , "\"" ) ; + } + else + { + strcat(lDialogString, "$PWD/") ; + } + + if ( aNumOfFilterPatterns > 0 ) + { + strcat(lDialogString , " \"" ) ; + strcat( lDialogString , aFilterPatterns[0] ) ; + for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat( lDialogString , " " ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; + } + if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) + { + strcat( lDialogString , " | " ) ; + strcat( lDialogString , aSingleFilterDescription ) ; + } + strcat( lDialogString , "\"" ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + } + else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) + { + if ( tfd_zenityPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} + strcpy( lDialogString , "zenity" ) ; + 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 ( tfd_matedialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} + strcpy( lDialogString , "matedialog" ) ; + } + else if ( tfd_shellementaryPresent() ) + { + 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 *)1;} + strcpy( lDialogString , "qarma" ) ; + 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" ) ; + + 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 , " |" ) ; + } + 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 (tfd_yadPresent()) + { + 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 ( 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=tkFileDialog.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() || dialogName() ) + { + if ( xdialogPresent( ) ) + { + 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 *)0;} + strcpy( lDialogString , "(dialog " ) ; + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;} + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(" ) ; + strcat( lDialogString , dialogName() ) ; + strcat( lDialogString , " " ) ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; + } + + strcat( lDialogString , "--fselect \"" ) ; + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + if ( ! strchr(aDefaultPathAndFile, '/') ) + { + strcat(lDialogString, "./") ; + } + strcat(lDialogString, aDefaultPathAndFile) ; + } + else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) + { + strcat(lDialogString, getenv("HOME")) ; + strcat(lDialogString, "/") ; + } + else + { + strcat(lDialogString, "./") ; + } + + if ( lWasGraphicDialog ) + { + strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; + } + else + { + strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; + if ( lWasXterm ) + { + strcat( lDialogString , + "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); + } + else + { + strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; + } + } + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} + 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 ; + } + getLastName(lString,p); + if ( ! strlen(lString) ) + { + return NULL; + } + return p ; + } + + 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; + } + getPathWithoutFinalSlash( lString , lBuff ) ; + if ( strlen( lString ) && ! dirExists( lString ) ) + { + return NULL ; + } + getLastName(lString,lBuff); + if ( ! filenameValid(lString) ) + { + return NULL; + } + return lBuff ; +} + + +/* in case of multiple files, the separator is | */ +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 */ +{ + 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 *)1;} + strcpy( lDialogString , "osascript "); + if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat( lDialogString , " -e 'try' -e '" ); + if ( ! aAllowMultipleSelects ) + { + + + strcat( lDialogString , "POSIX path of ( " ); + } + else + { + strcat( lDialogString , "set mylist to " ); + } + strcat( lDialogString , "choose file " ); + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with prompt \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ; + if ( strlen(lString) ) + { + strcat(lDialogString, "default location \"") ; + strcat(lDialogString, lString ) ; + strcat(lDialogString , "\" " ) ; + } + if ( aNumOfFilterPatterns > 0 ) + { + strcat(lDialogString , "of type {\"" ); + strcat( lDialogString , aFilterPatterns[0] + 2 ) ; + strcat( lDialogString , "\"" ) ; + for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat( lDialogString , ",\"" ) ; + strcat( lDialogString , aFilterPatterns[i] + 2) ; + strcat( lDialogString , "\"" ) ; + } + strcat( lDialogString , "} " ) ; + } + if ( aAllowMultipleSelects ) + { + strcat( lDialogString , "multiple selections allowed true ' " ) ; + strcat( lDialogString , + "-e 'set mystring to POSIX path of item 1 of mylist' " ); + strcat( lDialogString , + "-e 'repeat with i from 2 to the count of mylist' " ); + strcat( lDialogString , "-e 'set mystring to mystring & \"|\"' " ); + strcat( lDialogString , + "-e 'set mystring to mystring & POSIX path of item i of mylist' " ); + strcat( lDialogString , "-e 'end repeat' " ); + strcat( lDialogString , "-e 'mystring' " ); + } + else + { + strcat( lDialogString , ")' " ) ; + } + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( tfd_kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} + lWasKdialog = 1 ; + + strcpy( lDialogString , "kdialog" ) ; + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) + { + strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ + } + strcat( lDialogString , " --getopenfilename " ) ; + + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + if ( aDefaultPathAndFile[0] != '/' ) + { + strcat(lDialogString, "$PWD/") ; + } + strcat(lDialogString, "\"") ; + strcat(lDialogString, aDefaultPathAndFile ) ; + strcat(lDialogString , "\"" ) ; + } + else + { + strcat(lDialogString, "$PWD/") ; + } + + if ( aNumOfFilterPatterns > 0 ) + { + strcat(lDialogString , " \"" ) ; + strcat( lDialogString , aFilterPatterns[0] ) ; + for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ ) + { + strcat( lDialogString , " " ) ; + strcat( lDialogString , aFilterPatterns[i] ) ; + } + if ( aSingleFilterDescription && strlen(aSingleFilterDescription) ) + { + strcat( lDialogString , " | " ) ; + strcat( lDialogString , aSingleFilterDescription ) ; + } + strcat( lDialogString , "\"" ) ; + } + if ( aAllowMultipleSelects ) + { + strcat( lDialogString , " --multiple --separate-output" ) ; + } + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + } + else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) + { + if ( tfd_zenityPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} + strcpy( lDialogString , "zenity" ) ; + 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 ( tfd_matedialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} + strcpy( lDialogString , "matedialog" ) ; + } + else if ( tfd_shellementaryPresent() ) + { + 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 *)1;} + strcpy( lDialogString , "qarma" ) ; + 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" ) ; + + if ( aAllowMultipleSelects ) + { + strcat( lDialogString , " --multiple" ) ; + } + + 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 , " |" ) ; + } + 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 (tfd_yadPresent()) + { + 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 ( 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 , "lFiles=tkFileDialog.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 *)1;} + lWasGraphicDialog = 1 ; + strcpy( lDialogString , "(Xdialog " ) ; + } + else if ( isTerminalRunning( ) ) + { + 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 *)0;} + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(" ) ; + strcat( lDialogString , dialogName() ) ; + strcat( lDialogString , " " ) ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; + } + + strcat( lDialogString , "--fselect \"" ) ; + if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) ) + { + if ( ! strchr(aDefaultPathAndFile, '/') ) + { + strcat(lDialogString, "./") ; + } + strcat(lDialogString, aDefaultPathAndFile) ; + } + else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) + { + strcat(lDialogString, getenv("HOME")) ; + strcat(lDialogString, "/"); + } + else + { + strcat(lDialogString, "./") ; + } + + if ( lWasGraphicDialog ) + { + strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; + } + else + { + strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; + if ( lWasXterm ) + { + strcat( lDialogString , + "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); + } + else + { + strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; + } + } + } + else + { + 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" ) ) ) + { + free(lBuff); + lBuff = NULL; + return NULL ; + } + lBuff[0]='\0'; + p = lBuff; + while ( fgets( p , sizeof( lBuff ) , lIn ) != NULL ) + { + p += strlen( p ); + } + pclose( lIn ) ; + if ( lBuff[strlen( lBuff ) -1] == '\n' ) + { + lBuff[strlen( lBuff ) -1] = '\0' ; + } + /* printf( "lBuff: %s\n" , lBuff ) ; */ + if ( lWasKdialog && aAllowMultipleSelects ) + { + p = lBuff ; + while ( ( p = strchr( p , '\n' ) ) ) + * p = '|' ; + } + /* printf( "lBuff2: %s\n" , lBuff ) ; */ + if ( ! strlen( lBuff ) ) + { + free(lBuff); + lBuff = NULL; + return NULL; + } + if ( aAllowMultipleSelects && strchr(lBuff, '|') ) + { + if( ! ensureFilesExist( lBuff , lBuff ) ) + { + free(lBuff); + lBuff = NULL; + return NULL; + } + } + else if ( !fileExists(lBuff) ) + { + free(lBuff); + lBuff = NULL; + return NULL; + } + + lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char))); + + /*printf( "lBuff3: %s\n" , lBuff ) ; */ + return lBuff ; +} + + +char * tinyfd_selectFolderDialog( + char const * aTitle , /* "" */ + char const * aDefaultPath ) /* "" */ +{ + static char lBuff[MAX_PATH_OR_CMD] ; + char lDialogString[MAX_PATH_OR_CMD] ; + FILE * lIn ; + 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 *)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 "); + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "with prompt \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + if ( aDefaultPath && strlen(aDefaultPath) ) + { + strcat(lDialogString, "default location \"") ; + strcat(lDialogString, aDefaultPath ) ; + strcat(lDialogString , "\" " ) ; + } + strcat( lDialogString , ")' " ) ; + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( tfd_kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} + strcpy( lDialogString , "kdialog" ) ; + if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() ) + { + strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */ + } + strcat( lDialogString , " --getexistingdirectory " ) ; + + if ( aDefaultPath && strlen(aDefaultPath) ) + { + if ( aDefaultPath[0] != '/' ) + { + strcat(lDialogString, "$PWD/") ; + } + strcat(lDialogString, "\"") ; + strcat(lDialogString, aDefaultPath ) ; + strcat(lDialogString , "\"" ) ; + } + else + { + strcat(lDialogString, "$PWD/") ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + } + else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) + { + if ( tfd_zenityPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;} + strcpy( lDialogString , "zenity" ) ; + 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 ( tfd_matedialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} + strcpy( lDialogString , "matedialog" ) ; + } + else if ( tfd_shellementaryPresent() ) + { + 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 *)1;} + strcpy( lDialogString , "qarma" ) ; + 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" ) ; + + strcat(lDialogString, " --title=\"") ; + if (aTitle && strlen(aTitle)) 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 (tfd_yadPresent()) + { + 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 ( 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.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 *)1;} + lWasGraphicDialog = 1 ; + strcpy( lDialogString , "(Xdialog " ) ; + } + else if ( isTerminalRunning( ) ) + { + 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 *)0;} + lWasXterm = 1 ; + strcpy( lDialogString , terminalName() ) ; + strcat( lDialogString , "'(" ) ; + strcat( lDialogString , dialogName() ) ; + strcat( lDialogString , " " ) ; + } + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, "--title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\" ") ; + } + + if ( !xdialogPresent() && !gdialogPresent() ) + { + strcat(lDialogString, "--backtitle \"") ; + strcat(lDialogString, + "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ; + strcat(lDialogString, "\" ") ; + } + + strcat( lDialogString , "--dselect \"" ) ; + if ( aDefaultPath && strlen(aDefaultPath) ) + { + strcat(lDialogString, aDefaultPath) ; + ensureFinalSlash(lDialogString); + } + else if ( ! isTerminalRunning( ) && !lWasGraphicDialog ) + { + strcat(lDialogString, getenv("HOME")) ; + strcat(lDialogString, "/"); + } + else + { + strcat(lDialogString, "./") ; + } + + if ( lWasGraphicDialog ) + { + strcat(lDialogString, "\" 0 60 ) 2>&1 ") ; + } + else + { + strcat(lDialogString, "\" 0 60 >/dev/tty) ") ; + if ( lWasXterm ) + { + strcat( lDialogString , + "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt"); + } + else + { + strcat(lDialogString, "2>&1 ; clear >/dev/tty") ; + } + } + } + else + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} + 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 ; + } + return p ; + } + 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 ) || ! dirExists( lBuff ) ) + { + return NULL ; + } + return lBuff ; +} + + +/* returns the hexcolor as a string "#FF0000" */ +/* aoResultRGB also contains the result */ +/* aDefaultRGB is used only if aDefaultHexRGB is NULL */ +/* aDefaultRGB and aoResultRGB can be the same array */ +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 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 * p; + char * lPointerInputBox; + FILE * lIn ; + int i ; + int lWasZenity3 = 0 ; + int lWasOsascript = 0 ; + int lWasXdialog = 0 ; + lBuff[0]='\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 (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 *)1;} + lWasOsascript = 1 ; + strcpy( lDialogString , "osascript"); + + if ( ! osx9orBetter() ) + { + strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'"); + strcat( lDialogString , " -e 'try' -e 'set mycolor to choose color default color {"); + } + else + { + strcat( lDialogString , +" -e 'try' -e 'tell app (path to frontmost application as Unicode text) \ +to set mycolor to choose color default color {"); + } + + sprintf(lTmp, "%d", 256 * lDefaultRGB[0] ) ; + strcat(lDialogString, lTmp ) ; + strcat(lDialogString, "," ) ; + sprintf(lTmp, "%d", 256 * lDefaultRGB[1] ) ; + strcat(lDialogString, lTmp ) ; + strcat(lDialogString, "," ) ; + sprintf(lTmp, "%d", 256 * lDefaultRGB[2] ) ; + strcat(lDialogString, lTmp ) ; + strcat(lDialogString, "}' " ) ; + strcat( lDialogString , +"-e 'set mystring to ((item 1 of mycolor) div 256 as integer) as string' " ); + strcat( lDialogString , +"-e 'repeat with i from 2 to the count of mycolor' " ); + strcat( lDialogString , +"-e 'set mystring to mystring & \" \" & ((item i of mycolor) div 256 as integer) as string' " ); + strcat( lDialogString , "-e 'end repeat' " ); + strcat( lDialogString , "-e 'mystring' "); + strcat(lDialogString, "-e 'on error number -128' " ) ; + strcat(lDialogString, "-e 'end try'") ; + if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ; + } + else if ( tfd_kdialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;} + strcpy( lDialogString , "kdialog" ) ; + 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'" , lDefaultHexRGB ) ; + + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, " --title \"") ; + strcat(lDialogString, aTitle) ; + strcat(lDialogString, "\"") ; + } + } + else if ( tfd_zenity3Present() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() ) + { + lWasZenity3 = 1 ; + if ( tfd_zenity3Present() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity3");return (char *)1;} + strcpy( lDialogString , "zenity" ); + 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 ( tfd_matedialogPresent() ) + { + if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;} + strcpy( lDialogString , "matedialog" ) ; + } + else if ( tfd_shellementaryPresent() ) + { + 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 *)1;} + strcpy( lDialogString , "qarma" ) ; + 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" , lDefaultHexRGB ) ; + + strcat(lDialogString, " --title=\"") ; + if (aTitle && strlen(aTitle)) 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 *)1;} + lWasXdialog = 1 ; + strcpy( lDialogString , "Xdialog --colorsel \"" ) ; + if ( aTitle && strlen(aTitle) ) + { + strcat(lDialogString, aTitle) ; + } + strcat(lDialogString, "\" 0 60 ") ; +#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 ( 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 *)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 ( tfd_isDarwin( ) ) + { + strcat( lDialogString , +"import os;os.system('''osascript -e 'tell app \\\"Finder\\\" to set \ +frontmost of process \\\"Python\\\" to true' ''');"); + } + + strcat( lDialogString , "res=tkColorChooser.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 (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);} + 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( (int) p[i] ) ) + { + return NULL ; + } + } + 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 ) ; + if ( ! ( lIn = popen( lDialogString , "r" ) ) ) + { + return NULL ; + } + while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL ) + { + } + pclose( lIn ) ; + if ( ! strlen( lBuff ) ) + { + return NULL ; + } + /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */ + /* printf( "lBuff0: %s\n" , lBuff ) ; */ + if ( lBuff[strlen( lBuff ) -1] == '\n' ) + { + lBuff[strlen( lBuff ) -1] = '\0' ; + } + + if ( lWasZenity3 ) + { + if ( lBuff[0] == '#' ) + { + if ( strlen(lBuff)>7 ) + { + lBuff[3]=lBuff[5]; + lBuff[4]=lBuff[6]; + lBuff[5]=lBuff[9]; + lBuff[6]=lBuff[10]; + lBuff[7]='\0'; + } + Hex2RGB(lBuff,aoResultRGB); + } + else if ( lBuff[3] == '(' ) { +#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] == '(' ) { +#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 ) ; */ +#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 ) ; */ + + strcpy(lDefaultHexRGB,lBuff); + return lDefaultHexRGB ; +} + +#endif /* _WIN32 */ + + +/* +int main( int argc , char * argv[] ) +{ +char const * lTmp; +char const * lTheSaveFileName; +char const * lTheOpenFileName; +char const * lTheSelectFolderName; +char const * lTheHexColor; +char const * lWillBeGraphicMode; +unsigned char lRgbColor[3]; +FILE * lIn; +char lBuffer[1024]; +char lString[1024]; +char const * lFilterPatterns[2] = { "*.txt", "*.text" }; + +tinyfd_verbose = argc - 1; +tinyfd_silent = 1; + +lWillBeGraphicMode = tinyfd_inputBox("tinyfd_query", NULL, NULL); + +strcpy(lBuffer, "v"); +strcat(lBuffer, tinyfd_version); +if (lWillBeGraphicMode) +{ + strcat(lBuffer, "\ngraphic mode: "); +} +else +{ + strcat(lBuffer, "\nconsole mode: "); +} +strcat(lBuffer, tinyfd_response); +strcat(lBuffer, "\n"); +strcat(lBuffer, tinyfd_needs+78); +strcpy(lString, "tinyfiledialogs"); +tinyfd_messageBox(lString, lBuffer, "ok", "info", 0); + +tinyfd_notifyPopup("the title", "the message\n\tfrom outer-space", "info"); + +if (lWillBeGraphicMode && !tinyfd_forceConsole) +{ + tinyfd_forceConsole = ! tinyfd_messageBox("Hello World", + "graphic dialogs [yes] / console mode [no]?", + "yesno", "question", 1); +} + +lTmp = tinyfd_inputBox( + "a password box", "your password will be revealed", NULL); + +if (!lTmp) return 1; + +strcpy(lString, lTmp); + +lTheSaveFileName = tinyfd_saveFileDialog( + "let us save this password", + "passwordFile.txt", + 2, + lFilterPatterns, + NULL); + +if (!lTheSaveFileName) +{ + tinyfd_messageBox( + "Error", + "Save file name is NULL", + "ok", + "error", + 1); + return 1; +} + +lIn = fopen(lTheSaveFileName, "w"); +if (!lIn) +{ + tinyfd_messageBox( + "Error", + "Can not open this file in write mode", + "ok", + "error", + 1); + return 1; +} +fputs(lString, lIn); +fclose(lIn); + +lTheOpenFileName = tinyfd_openFileDialog( + "let us read the password back", + "", + 2, + lFilterPatterns, + NULL, + 0); + +if (!lTheOpenFileName) +{ + tinyfd_messageBox( + "Error", + "Open file name is NULL", + "ok", + "error", + 1); + return 1; +} + +lIn = fopen(lTheOpenFileName, "r"); + +if (!lIn) +{ + tinyfd_messageBox( + "Error", + "Can not open this file in read mode", + "ok", + "error", + 1); + return(1); +} +lBuffer[0] = '\0'; +fgets(lBuffer, sizeof(lBuffer), lIn); +fclose(lIn); + +tinyfd_messageBox("your password is", + lBuffer, "ok", "info", 1); + +lTheSelectFolderName = tinyfd_selectFolderDialog( + "let us just select a directory", NULL); + +if (!lTheSelectFolderName) +{ + tinyfd_messageBox( + "Error", + "Select folder name is NULL", + "ok", + "error", + 1); + return 1; +} + +tinyfd_messageBox("The selected folder is", + lTheSelectFolderName, "ok", "info", 1); + +lTheHexColor = tinyfd_colorChooser( + "choose a nice color", + "#FF0077", + lRgbColor, + lRgbColor); + +if (!lTheHexColor) +{ + tinyfd_messageBox( + "Error", + "hexcolor is NULL", + "ok", + "error", + 1); + return 1; +} + +tinyfd_messageBox("The selected hexcolor is", + lTheHexColor, "ok", "info", 1); + + tinyfd_beep(); + + return 0; +} +*/ + +#ifdef _MSC_VER +#pragma warning(default:4996) +#pragma warning(default:4100) +#pragma warning(default:4706) +#endif diff --git a/libtinyfiledialogs/tinyfiledialogs.h b/libtinyfiledialogs/tinyfiledialogs.h index cf1c17b..9c8a852 100644 --- a/libtinyfiledialogs/tinyfiledialogs.h +++ b/libtinyfiledialogs/tinyfiledialogs.h @@ -1,298 +1,303 @@ -/*_________ - / \ tinyfiledialogs.h v3.8.3 [Nov 1, 2020] zlib licence - |tiny file| Unique header file created [November 9, 2014] - | dialogs | Copyright (c) 2014 - 2020 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 | -|____________________________________________| - ________________________________________________________________________________ -| ____________________________________________________________________________ | -| | | | -| | 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 | | -| |____________________________________________________________________________| | -|________________________________________________________________________________| - -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 -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. -*/ - -#ifndef TINYFILEDIALOGS_H -#define TINYFILEDIALOGS_H - -#ifdef __cplusplus -extern "C" { /* 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. */ -#endif - -/******************************************************************************************************/ -/**************************************** UTF-8 on Windows ********************************************/ -/******************************************************************************************************/ -#ifdef _WIN32 -/* 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 const 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, - 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, -the functions will not display the dialogs -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 - 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); - -int tinyfd_notifyPopup( - 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 * 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 * 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 * 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 * 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 * tinyfd_selectFolderDialog( - char const * aTitle, /* NULL or "" */ - char const * aDefaultPath); /* NULL or "" */ - /* returns NULL on cancel */ - -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 */ - /* aDefaultRGB and aoResultRGB can be the same array */ - /* returns NULL on cancel */ - - -/************ WINDOWS ONLY SECTION ************************/ -#ifdef _WIN32 - -/* windows only - utf-16 version */ -int tinyfd_notifyPopupW( - 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 * 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 * 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 * 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 * 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 * 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 * 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 /*_WIN32 */ - -#ifdef __cplusplus -} /*extern "C"*/ -#endif - -#endif /* TINYFILEDIALOGS_H */ - -/* - ________________________________________________________________________________ -| ____________________________________________________________________________ | -| | | | -| | 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 code is pure C, perfectly compatible with C++. -- 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) -- 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. -- On windows link against Comdlg32.lib and Ole32.lib - (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, yad, - python (2 or 3)/tkinter/python-dbus (optional), Xdialog - 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, - it opens a console if needed (requires xterm + bash). -- 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 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 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 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 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.8.8 [Apr 22, 2021] zlib licence + |tiny file| Unique header file created [November 9, 2014] + | 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 | +|____________________________________________| + ________________________________________________________________________________ +| ____________________________________________________________________________ | +| | | | +| | 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 | | +| |____________________________________________________________________________| | +|________________________________________________________________________________| + +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 +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. +*/ + +#ifndef TINYFILEDIALOGS_H +#define TINYFILEDIALOGS_H + +#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 + +/******************************************************************************************************/ +/**************************************** UTF-8 on Windows ********************************************/ +/******************************************************************************************************/ +#ifdef _WIN32 +/* 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, + 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, +the functions will not display the dialogs +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 + 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); + +int tinyfd_notifyPopup( + 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 * 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 * 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 * 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 * 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 * tinyfd_selectFolderDialog( + char const * aTitle, /* NULL or "" */ + char const * aDefaultPath); /* NULL or "" */ + /* returns NULL on cancel */ + +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 */ + /* aDefaultRGB and aoResultRGB can be the same array */ + /* returns NULL on cancel */ + + +/************ WINDOWS ONLY SECTION ************************/ +#ifdef _WIN32 + +/* windows only - utf-16 version */ +int tinyfd_notifyPopupW( + 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 * 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 * 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 * 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 * 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 * 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 * 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 /*_WIN32 */ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /* TINYFILEDIALOGS_H */ + +/* + ________________________________________________________________________________ +| ____________________________________________________________________________ | +| | | | +| | 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) +- 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. +- On windows link against Comdlg32.lib and Ole32.lib + (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, yad, + python (2 or 3)/tkinter/python-dbus (optional), Xdialog + 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, + it opens a console if needed (requires xterm + bash). +- 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 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 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 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 +*/ From 8014a110b0c582ce6d2857c2d0619136a4d637df Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Tue, 4 Jan 2022 21:17:37 -0500 Subject: [PATCH 34/41] Remove outdated patch. --- patches/0001_WinUtf8.patch | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 patches/0001_WinUtf8.patch diff --git a/patches/0001_WinUtf8.patch b/patches/0001_WinUtf8.patch deleted file mode 100644 index 004f262..0000000 --- a/patches/0001_WinUtf8.patch +++ /dev/null @@ -1,13 +0,0 @@ -diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c -index 7e6a3b5..3bc9f8a 100644 ---- a/libtinyfiledialogs/tinyfiledialogs.c -+++ b/libtinyfiledialogs/tinyfiledialogs.c -@@ -117,7 +117,7 @@ misrepresented as being the original software. - #include - #define TINYFD_NOCCSUNICODE - #define SLASH "\\" -- int tinyfd_winUtf8 = 0 ; /* on windows string char can be 0:MBCS or 1:UTF-8 */ -+ int tinyfd_winUtf8 = 1 ; /* on windows string char can be 0:MBCS or 1:UTF-8 */ - #else - #include - #include From 68670e6e50f3ec3290e1ce12180610014f64666f Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Tue, 4 Jan 2022 21:18:16 -0500 Subject: [PATCH 35/41] Update and apply win64 visual style patch. --- libtinyfiledialogs/tinyfiledialogs.c | 51 ++ patches/0002_WindowsHidpiVisualStyle.patch | 600 +++++++++++++-------- 2 files changed, 437 insertions(+), 214 deletions(-) diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index 30865fe..de38db6 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -182,6 +182,14 @@ char tinyfd_needs[] = "\ #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ #endif +#ifdef _WIN64 +#include + +#pragma comment(linker,"\"/manifestdependency:type='win32' \ +name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ +processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") +#endif // _WIN64 + static int getenvDISPLAY(void) { return tinyfd_assumeGraphicDisplay || getenv("DISPLAY"); @@ -1060,6 +1068,9 @@ int tinyfd_messageBoxW( wchar_t const * aIconType, /* "info" "warning" "error" "question" */ int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif int lBoxReturnValue; UINT aCode; @@ -1130,6 +1141,9 @@ int tinyfd_notifyPopupW( wchar_t const * aMessage, /* NULL or L"" may contain \n \t */ wchar_t const * aIconType) /* L"info" L"warning" L"error" */ { +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif wchar_t * lDialogString; size_t lTitleLen; size_t lMessageLen; @@ -1202,6 +1216,9 @@ wchar_t * tinyfd_inputBoxW( wchar_t const * aMessage, /* NULL or L"" (\n and \t have no effect) */ wchar_t const * aDefaultInput) /* L"" , if NULL it's a passwordBox */ { +#ifdef _WIN64 + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); +#endif static wchar_t lBuff[MAX_PATH_OR_CMD]; wchar_t * lDialogString; FILE * lIn; @@ -1273,6 +1290,7 @@ wchar_t * tinyfd_inputBoxW( wcscat(lDialogString, L"tinyfiledialogsTopWindow"); wcscat(lDialogString, L"\n\ +\n\ \n\ -+\n\ - \n\ ++\n\ + \n\ +-+\n\ +- Date: Tue, 4 Jan 2022 21:18:40 -0500 Subject: [PATCH 36/41] Publish 3.9.0. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 6936c24..c162968 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tinyfiledialogs" description = "High-level Rust binding for the tinyfiledialogs C library." -version = "3.8.3" +version = "3.9.0" authors = ["Guillaume Vareille ", "Josh Matthews "] license = "zlib-acknowledgement" readme = "README.md" From a93c1f32902836be3953177143b5cd65251720c1 Mon Sep 17 00:00:00 2001 From: Maciej Pokorski Date: Thu, 20 Jan 2022 21:35:30 +0100 Subject: [PATCH 37/41] add cargo feature for windows hidpi api --- Cargo.toml | 4 + build.rs | 7 + libtinyfiledialogs/tinyfiledialogs.c | 32 +- patches/0002_WindowsHidpiVisualStyle.patch | 336 ++++++++++----------- 4 files changed, 195 insertions(+), 184 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c162968..683d1b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,10 @@ repository = "https://github.com/jdm/tinyfiledialogs-rs" build = "build.rs" +[features] +default = ["windows-hidpi"] +windows-hidpi = [] + [lib] name = "tinyfiledialogs" diff --git a/build.rs b/build.rs index 93d5ac7..a841007 100644 --- a/build.rs +++ b/build.rs @@ -9,9 +9,16 @@ use std::env; fn main() { let target = env::var("TARGET").unwrap(); + let windows_hidpi = if cfg!(feature = "windows-hidpi") { + "USE_WINDOWS_HIDPI" + }else { + "NO_WINDOWS_HIDPI" + }; + cc::Build::new() .file("libtinyfiledialogs/tinyfiledialogs.c") .flag("-v") + .define(windows_hidpi,None) .compile("libtinyfiledialogs.a"); if target.contains("windows") { diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c index de38db6..b7662fd 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -182,13 +182,13 @@ char tinyfd_needs[] = "\ #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ #endif -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) #include #pragma comment(linker,"\"/manifestdependency:type='win32' \ name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") -#endif // _WIN64 +#endif // USE_WINDOWS_HIDPI static int getenvDISPLAY(void) { @@ -1068,7 +1068,7 @@ int tinyfd_messageBoxW( wchar_t const * aIconType, /* "info" "warning" "error" "question" */ int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif int lBoxReturnValue; @@ -1141,7 +1141,7 @@ int tinyfd_notifyPopupW( wchar_t const * aMessage, /* NULL or L"" may contain \n \t */ wchar_t const * aIconType) /* L"info" L"warning" L"error" */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif wchar_t * lDialogString; @@ -1216,7 +1216,7 @@ wchar_t * tinyfd_inputBoxW( wchar_t const * aMessage, /* NULL or L"" (\n and \t have no effect) */ wchar_t const * aDefaultInput) /* L"" , if NULL it's a passwordBox */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif static wchar_t lBuff[MAX_PATH_OR_CMD]; @@ -1497,7 +1497,7 @@ wchar_t * tinyfd_saveFileDialogW( wchar_t const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ wchar_t const * aSingleFilterDescription) /* NULL or "image files" */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif static wchar_t lBuff[MAX_PATH_OR_CMD]; @@ -1603,7 +1603,7 @@ wchar_t * tinyfd_openFileDialogW( wchar_t const * aSingleFilterDescription, /* NULL or "image files" */ int aAllowMultipleSelects) /* 0 or 1 ; -1 to free allocated memory and return */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif size_t lLengths[MAX_MULTIPLE_FILES]; @@ -1793,7 +1793,7 @@ wchar_t * tinyfd_selectFolderDialogW( wchar_t const * aTitle, /* NULL or "" */ wchar_t const * aDefaultPath) /* NULL or "" */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif static wchar_t lBuff[MAX_PATH_OR_CMD]; @@ -1847,7 +1847,7 @@ wchar_t * tinyfd_colorChooserW( unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif static wchar_t lResultHexRGB[8]; @@ -1916,7 +1916,7 @@ static int messageBoxWinGui( char const * aIconType, /* "info" "warning" "error" "question" */ int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif int lIntRetVal; @@ -1965,7 +1965,7 @@ static int notifyWinGui( char const * aMessage, /* NULL or "" may NOT contain \n nor \t */ char const * aIconType) { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif wchar_t lTitle[128] = L""; @@ -2007,7 +2007,7 @@ static int inputBoxWinGui( char const * aMessage, /* NULL or "" may NOT contain \n nor \t */ char const * aDefaultInput) /* "" , if NULL it's a passwordBox */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif wchar_t lTitle[128] = L""; @@ -2063,7 +2063,7 @@ static char * saveFileDialogWinGui( char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ char const * aSingleFilterDescription) /* NULL or "image files" */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif wchar_t lTitle[128] = L""; @@ -2138,7 +2138,7 @@ static char * openFileDialogWinGui( char const * aSingleFilterDescription, /* NULL or "image files" */ int aAllowMultipleSelects) /* 0 or 1 */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif wchar_t lTitle[128] = L""; @@ -2206,7 +2206,7 @@ static char * selectFolderDialogWinGui( char const * aTitle, /* NULL or "" */ char const * aDefaultPath) /* NULL or "" */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif wchar_t lTitle[128] = L""; @@ -2250,7 +2250,7 @@ static char * colorChooserWinGui( unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ { -#ifdef _WIN64 +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); #endif static char lResultHexRGB[8]; diff --git a/patches/0002_WindowsHidpiVisualStyle.patch b/patches/0002_WindowsHidpiVisualStyle.patch index 8820d92..bb325b3 100644 --- a/patches/0002_WindowsHidpiVisualStyle.patch +++ b/patches/0002_WindowsHidpiVisualStyle.patch @@ -3,168 +3,168 @@ index 30865fe..de38db6 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -182,6 +182,14 @@ char tinyfd_needs[] = "\ - #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ - #endif - -+#ifdef _WIN64 -+#include -+ -+#pragma comment(linker,"\"/manifestdependency:type='win32' \ -+name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ -+processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") -+#endif // _WIN64 -+ - static int getenvDISPLAY(void) - { - return tinyfd_assumeGraphicDisplay || getenv("DISPLAY"); + #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ + #endif + ++#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) ++#include ++ ++#pragma comment(linker,"\"/manifestdependency:type='win32' \ ++name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ ++processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") ++#endif // USE_WINDOWS_HIDPI ++ + static int getenvDISPLAY(void) + { + return tinyfd_assumeGraphicDisplay || getenv("DISPLAY"); @@ -1060,6 +1068,9 @@ int tinyfd_messageBoxW( - wchar_t const * aIconType, /* "info" "warning" "error" "question" */ - int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ - { -+#ifdef _WIN64 -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - int lBoxReturnValue; - UINT aCode; - + wchar_t const * aIconType, /* "info" "warning" "error" "question" */ + int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ + { ++#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) ++ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); ++#endif + int lBoxReturnValue; + UINT aCode; + @@ -1130,6 +1141,9 @@ int tinyfd_notifyPopupW( - wchar_t const * aMessage, /* NULL or L"" may contain \n \t */ - wchar_t const * aIconType) /* L"info" L"warning" L"error" */ - { -+#ifdef _WIN64 -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - wchar_t * lDialogString; - size_t lTitleLen; - size_t lMessageLen; + wchar_t const * aMessage, /* NULL or L"" may contain \n \t */ + wchar_t const * aIconType) /* L"info" L"warning" L"error" */ + { ++#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) ++ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); ++#endif + wchar_t * lDialogString; + size_t lTitleLen; + size_t lMessageLen; @@ -1202,6 +1216,9 @@ wchar_t * tinyfd_inputBoxW( - wchar_t const * aMessage, /* NULL or L"" (\n and \t have no effect) */ - wchar_t const * aDefaultInput) /* L"" , if NULL it's a passwordBox */ - { -+#ifdef _WIN64 -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - static wchar_t lBuff[MAX_PATH_OR_CMD]; - wchar_t * lDialogString; - FILE * lIn; + wchar_t const * aMessage, /* NULL or L"" (\n and \t have no effect) */ + wchar_t const * aDefaultInput) /* L"" , if NULL it's a passwordBox */ + { ++#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) ++ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); ++#endif + static wchar_t lBuff[MAX_PATH_OR_CMD]; + wchar_t * lDialogString; + FILE * lIn; @@ -1273,6 +1290,7 @@ wchar_t * tinyfd_inputBoxW( - - wcscat(lDialogString, L"tinyfiledialogsTopWindow"); - wcscat(lDialogString, L"\n\ -+\n\ - \n\ ++\n\ + -+ -+#pragma comment(linker,"\"/manifestdependency:type='win32' \ -+name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ -+processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") --+#endif // _WIN64 +-+#endif // USE_WINDOWS_HIDPI -+ - static char * getPathWithoutFinalSlash( - char * const aoDestination, /* make sure it is allocated, use _MAX_PATH */ @@ -193,7 +193,7 @@ index 46369f1..56b279b 100644 - 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 */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - int lBoxReturnValue; @@ -203,7 +203,7 @@ index 46369f1..56b279b 100644 - char const * const aIconType, /* "info" "warning" "error" "question" */ - int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - int lIntRetVal; @@ -213,7 +213,7 @@ index 46369f1..56b279b 100644 - wchar_t const * const aMessage, /* NULL or L"" may contain \n \t */ - wchar_t const * const aIconType) /* L"info" L"warning" L"error" */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - wchar_t * lDialogString; @@ -223,7 +223,7 @@ index 46369f1..56b279b 100644 - char const * const aMessage, /* NULL or "" may NOT contain \n nor \t */ - char const * const aIconType) - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - wchar_t * lTitle; @@ -233,7 +233,7 @@ index 46369f1..56b279b 100644 - 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 */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - static wchar_t lBuff[MAX_PATH_OR_CMD]; @@ -251,7 +251,7 @@ index 46369f1..56b279b 100644 - char const * const aMessage, /* NULL or "" may NOT contain \n nor \t */ - char const * const aDefaultInput) /* "" , if NULL it's a passwordBox */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - wchar_t * lTitle; @@ -261,7 +261,7 @@ index 46369f1..56b279b 100644 - wchar_t const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ - wchar_t const * const aSingleFilterDescription) /* NULL or "image files" */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - static wchar_t lBuff[MAX_PATH_OR_CMD]; @@ -271,7 +271,7 @@ index 46369f1..56b279b 100644 - char const * const * const aFilterPatterns, /* NULL or {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription) /* NULL or "image files" */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - wchar_t * lTitle; @@ -281,7 +281,7 @@ index 46369f1..56b279b 100644 - wchar_t const * const aSingleFilterDescription, /* NULL or "image files" */ - int const aAllowMultipleSelects) /* 0 or 1 */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - static wchar_t lBuff[MAX_MULTIPLE_FILES*MAX_PATH_OR_CMD]; @@ -291,7 +291,7 @@ index 46369f1..56b279b 100644 - char const * const aSingleFilterDescription, /* NULL or "image files" */ - int const aAllowMultipleSelects) /* 0 or 1 */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - wchar_t * lTitle; @@ -301,7 +301,7 @@ index 46369f1..56b279b 100644 - wchar_t const * const aTitle, /* NULL or "" */ - wchar_t const * const aDefaultPath) /* NULL or "" */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - static wchar_t lBuff[MAX_PATH_OR_CMD]; @@ -311,7 +311,7 @@ index 46369f1..56b279b 100644 - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) /* NULL or "" */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - wchar_t * lTitle; @@ -321,7 +321,7 @@ index 46369f1..56b279b 100644 - unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ - unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - static wchar_t lResultHexRGB[8]; @@ -331,7 +331,7 @@ index 46369f1..56b279b 100644 - unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ - unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - static char lResultHexRGB[8]; @@ -341,7 +341,7 @@ index 46369f1..56b279b 100644 - char const * const aIconType , /* "info" "warning" "error" "question" */ - int const aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - int lBoxReturnValue; @@ -351,7 +351,7 @@ index 46369f1..56b279b 100644 - char const * const * const aFilterPatterns , /* NULL or {"*.jpg","*.png"} */ - char const * const aSingleFilterDescription ) /* NULL or "image files" */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - char lDirname [MAX_PATH_OR_CMD] ; @@ -361,7 +361,7 @@ index 46369f1..56b279b 100644 - char const * const aSingleFilterDescription , /* NULL or "image files" */ - int const aAllowMultipleSelects ) /* 0 or 1 */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - char lDirname [MAX_PATH_OR_CMD] ; @@ -371,7 +371,7 @@ index 46369f1..56b279b 100644 - char const * const aTitle , /* NULL or "" */ - char const * const aDefaultPath ) /* NULL or "" */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - BROWSEINFOA bInfo ; @@ -381,7 +381,7 @@ index 46369f1..56b279b 100644 - unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */ - unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */ - { --+#ifdef _WIN64 +-+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) -+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); -+#endif - static char lResultHexRGB[8]; From fa7028bdc727b7d20dfd3dbc8ed2e99ac693a78a Mon Sep 17 00:00:00 2001 From: Maciej Pokorski Date: Tue, 25 Jan 2022 20:14:58 +0100 Subject: [PATCH 38/41] fix problem with git apply --- patches/0002_WindowsHidpiVisualStyle.patch | 226 +-------------------- 1 file changed, 2 insertions(+), 224 deletions(-) diff --git a/patches/0002_WindowsHidpiVisualStyle.patch b/patches/0002_WindowsHidpiVisualStyle.patch index bb325b3..2c15ddc 100644 --- a/patches/0002_WindowsHidpiVisualStyle.patch +++ b/patches/0002_WindowsHidpiVisualStyle.patch @@ -1,11 +1,11 @@ diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c -index 30865fe..de38db6 100644 +index 30865fe..b7662fd 100644 --- a/libtinyfiledialogs/tinyfiledialogs.c +++ b/libtinyfiledialogs/tinyfiledialogs.c @@ -182,6 +182,14 @@ char tinyfd_needs[] = "\ #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ #endif - + +#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) +#include + @@ -165,225 +165,3 @@ index 30865fe..de38db6 100644 static char lResultHexRGB[8]; wchar_t lTitle[128]; -diff --git a/patches/0002_WindowsHidpiVisualStyle.patch b/patches/0002_WindowsHidpiVisualStyle.patch -index 46369f1..56b279b 100644 ---- a/patches/0002_WindowsHidpiVisualStyle.patch -+++ b/patches/0002_WindowsHidpiVisualStyle.patch -@@ -1,217 +0,0 @@ --diff --git a/libtinyfiledialogs/tinyfiledialogs.c b/libtinyfiledialogs/tinyfiledialogs.c --index 700cce6..14619c7 100644 ----- a/libtinyfiledialogs/tinyfiledialogs.c --+++ b/libtinyfiledialogs/tinyfiledialogs.c --@@ -209,6 +209,14 @@ char const tinyfd_needs[] = "\ -- #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */ -- #endif -- --+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) --+#include --+ --+#pragma comment(linker,"\"/manifestdependency:type='win32' \ --+name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ --+processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") --+#endif // USE_WINDOWS_HIDPI --+ -- 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 */ --@@ -933,6 +941,9 @@ int tinyfd_messageBoxW( -- 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 */ -- { --+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) --+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); --+#endif -- int lBoxReturnValue; -- UINT aCode; -- --@@ -1001,6 +1012,9 @@ static int messageBoxWinGui8( -- char const * const aIconType, /* "info" "warning" "error" "question" */ -- int const aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */ -- { --+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) --+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); --+#endif -- int lIntRetVal; -- wchar_t * lTitle; -- wchar_t * lMessage; --@@ -1030,6 +1044,9 @@ int tinyfd_notifyPopupW( -- wchar_t const * const aMessage, /* NULL or L"" may contain \n \t */ -- wchar_t const * const aIconType) /* L"info" L"warning" L"error" */ -- { --+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) --+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); --+#endif -- wchar_t * lDialogString; -- size_t lTitleLen; -- size_t lMessageLen; --@@ -1098,6 +1115,9 @@ static int notifyWinGui( -- char const * const aMessage, /* NULL or "" may NOT contain \n nor \t */ -- char const * const aIconType) -- { --+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) --+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); --+#endif -- wchar_t * lTitle; -- wchar_t * lMessage; -- wchar_t * lIconType; --@@ -1129,6 +1149,9 @@ wchar_t const * tinyfd_inputBoxW( -- 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 */ -- { --+#if defined(USE_WINDOWS_HIDPI) && defined(_WIN64) --+ SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); --+#endif -- static wchar_t lBuff[MAX_PATH_OR_CMD]; -- wchar_t * lDialogString; -- FILE * lIn; --@@ -1196,6 +1219,7 @@ wchar_t const * tinyfd_inputBoxW( -- -- wcscat(lDialogString, L"tinyfiledialogsTopWindow"); -- wcscat(lDialogString, L"\n\ --+\n\ -- Date: Sun, 27 Mar 2022 17:07:48 -0400 Subject: [PATCH 39/41] Publish 3.9.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 683d1b6..53ceb1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tinyfiledialogs" description = "High-level Rust binding for the tinyfiledialogs C library." -version = "3.9.0" +version = "3.9.1" authors = ["Guillaume Vareille ", "Josh Matthews "] license = "zlib-acknowledgement" readme = "README.md" From d6c8d91c2bc3c4f1dbcb06ff7a2079139bee8fda Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Thu, 12 Oct 2023 22:16:10 +0200 Subject: [PATCH 40/41] Add security warning to README and lib.rs doc --- README.md | 6 ++++++ src/lib.rs | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/README.md b/README.md index 6533cad..0e48486 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,9 @@ To use this library, add this to the `dependencies` section in `Cargo.toml`: ``` tinyfiledialogs = "3.0" ``` + +## Security Warning + +tinyfiledialogs should only be used with trusted input. Using it with +untrusted input, for example as dialog title or message, can in the worst +case lead to execution of arbitrary commands. diff --git a/src/lib.rs b/src/lib.rs index 6c5a18c..bec01fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,14 @@ +//! # tinyfiledialogs-rs +//! +//! This is a high-level Rust binding to the excellent [tinyfiledialogs library](https://sourceforge.net/projects/tinyfiledialogs/) +//! by Guillaume Vareille. +//! +//! ## Security Warning +//! +//! tinyfiledialogs should only be used with trusted input. Using it with +//! untrusted input, for example as dialog title or message, can in the worst +//! case lead to execution of arbitrary commands. + extern crate libc; use libc::{c_char, c_uchar, c_int}; use std::ffi::{CStr, CString}; From b6bf0d635b5853c7b3ad3f9bac9b9265244a4110 Mon Sep 17 00:00:00 2001 From: Thomas Beckley Date: Tue, 26 Nov 2024 09:15:27 -0500 Subject: [PATCH 41/41] Disable HIDPI for Windows GNU --- build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.rs b/build.rs index a841007..1d32112 100644 --- a/build.rs +++ b/build.rs @@ -9,7 +9,7 @@ use std::env; fn main() { let target = env::var("TARGET").unwrap(); - let windows_hidpi = if cfg!(feature = "windows-hidpi") { + let windows_hidpi = if cfg!(feature = "windows-hidpi") & !target.ends_with("pc-windows-gnu") { "USE_WINDOWS_HIDPI" }else { "NO_WINDOWS_HIDPI"