Debian Codegraph @

pt_pal_editor/src/tinyfiledialogs/tinyfiledialogs.c

raw symbols SCIP
1 /* this file can be renamed with extension ".cpp" and compiled as C++.
2 The code is 100% compatible C C++
3 (just comment out << extern "C" >> in the header file) */
4
5 /*_________
6 / \ tinyfiledialogs.c v3.10 [Mar 27, 2023] zlib licence
7 |tiny file| Unique code file created [November 9, 2014]
8 | dialogs | Copyright (c) 2014 - 2023 Guillaume Vareille http://ysengrin.com
9 \____ ___/ http://tinyfiledialogs.sourceforge.net
10 \| git clone http://git.code.sf.net/p/tinyfiledialogs/code tinyfd
11 ____________________________________________
12 | |
13 | email: tinyfiledialogs at ysengrin.com |
14 |____________________________________________|
15 _________________________________________________________________________________
16 | |
17 | the windows only wchar_t UTF-16 prototypes are at the bottom of the header file |
18 |_________________________________________________________________________________|
19 _________________________________________________________
20 | |
21 | on windows: - since v3.6 char is UTF-8 by default |
22 | - if you want MBCS set tinyfd_winUtf8 to 0 |
23 | - functions like fopen expect MBCS |
24 |_________________________________________________________|
25
26 If you like tinyfiledialogs, please upvote my stackoverflow answer
27 https://stackoverflow.com/a/47651444
28
29 - License -
30 This software is provided 'as-is', without any express or implied
31 warranty. In no event will the authors be held liable for any damages
32 arising from the use of this software.
33 Permission is granted to anyone to use this software for any purpose,
34 including commercial applications, and to alter it and redistribute it
35 freely, subject to the following restrictions:
36 1. The origin of this software must not be misrepresented; you must not
37 claim that you wrote the original software. If you use this software
38 in a product, an acknowledgment in the product documentation would be
39 appreciated but is not required.
40 2. Altered source versions must be plainly marked as such, and must not be
41 misrepresented as being the original software.
42 3. This notice may not be removed or altered from any source distribution.
43 -----------
44
45 Thanks for contributions, bug corrections & thorough testing to:
46 - Don Heyse http://ldglite.sf.net for bug corrections & thorough testing!
47 - Paul Rouget
48 */
49
50
51 #ifndef __sun
52 #ifndef _POSIX_C_SOURCE
53 #define _POSIX_C_SOURCE 2 /* to accept POSIX 2 in old ANSI C standards */
54 #endif
55 #endif
56
57 #if !defined(_WIN32) && ( defined(__GNUC__) || defined(__clang__) )
58 #if !defined(_GNU_SOURCE)
59 #define _GNU_SOURCE /* used only to resolve symbolic links. Can be commented out */
60 #endif
61 #endif
62
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <ctype.h>
67 #include <sys/stat.h>
68
69 #ifdef _WIN32
70 #ifdef __BORLANDC__
71 #define _getch getch
72 #endif
73 #ifndef _WIN32_WINNT
74 #define _WIN32_WINNT 0x0500
75 #endif
76 #include <windows.h>
77 #include <commdlg.h>
78 #include <shlobj.h>
79 #include <conio.h>
80 #include <direct.h>
81 #define TINYFD_NOCCSUNICODE
82 #define SLASH "\\"
83 #else
84 #include <limits.h>
85 #include <unistd.h>
86 #include <dirent.h> /* on old systems try <sys/dir.h> instead */
87 #include <termios.h>
88 #include <sys/utsname.h>
89 #include <signal.h> /* on old systems try <sys/signal.h> instead */
90 #define SLASH "/"
91 #endif /* _WIN32 */
92
93 #include "tinyfiledialogs.h"
94
95 #define MAX_PATH_OR_CMD 1024 /* _MAX_PATH or MAX_PATH */
96
97 #ifndef MAX_MULTIPLE_FILES
98 #define MAX_MULTIPLE_FILES 1024
99 #endif
100 #define LOW_MULTIPLE_FILES 32
101
102 char tinyfd_version[8] = "3.10";
103
104 /******************************************************************************************************/
105 /**************************************** UTF-8 on Windows ********************************************/
106 /******************************************************************************************************/
107 #ifdef _WIN32
108 /* if you want to use UTF-8 ( instead of the UTF-16/wchar_t functions at the end of tinyfiledialogs.h )
109 Make sure your code is really prepared for UTF-8 (on windows, functions like fopen() expect MBCS and not UTF-8) */
110 int tinyfd_winUtf8 = 1; /* on windows char strings can be 1:UTF-8(default) or 0:MBCS */
111 /* for MBCS change this to 0, here or in your code */
112 #endif
113 /******************************************************************************************************/
114 /******************************************************************************************************/
115 /******************************************************************************************************/
116
117 int tinyfd_verbose = 0 ; /* on unix: prints the command line calls */
118 int tinyfd_silent = 1 ; /* 1 (default) or 0 : on unix, hide errors and warnings from called dialogs */
119
120 /* Curses dialogs are difficult to use, on windows they are only ascii and uses the unix backslah */
121 int tinyfd_allowCursesDialogs = 0 ; /* 0 (default) or 1 */
122 int tinyfd_forceConsole = 0 ; /* 0 (default) or 1 */
123 /* for unix & windows: 0 (graphic mode) or 1 (console mode).
124 0: try to use a graphic solution, if it fails then it uses console mode.
125 1: forces all dialogs into console mode even when the X server is present.
126 it can use the package dialog or dialog.exe.
127 on windows it only make sense for console applications */
128
129 int tinyfd_assumeGraphicDisplay = 0; /* 0 (default) or 1 */
130 /* some systems don't set the environment variable DISPLAY even when a graphic display is present.
131 set this to 1 to tell tinyfiledialogs to assume the existence of a graphic display */
132
133
134 char tinyfd_response[1024];
135 /* if you pass "tinyfd_query" as aTitle,
136 the functions will not display the dialogs
137 but and return 0 for console mode, 1 for graphic mode.
138 tinyfd_response is then filled with the retain solution.
139 possible values for tinyfd_response are (all lowercase)
140 for graphic mode:
141 windows_wchar windows applescript kdialog zenity zenity3 matedialog
142 shellementary qarma yad python2-tkinter python3-tkinter python-dbus
143 perl-dbus gxmessage gmessage xmessage xdialog gdialog
144 for console mode:
145 dialog whiptail basicinput no_solution */
146
147 static int gWarningDisplayed = 0 ;
148 static char gTitle[]="missing software! (we will try basic console input)";
149
150 #ifdef _WIN32
151 char tinyfd_needs[] = "\
152 ___________\n\
153 / \\ \n\
154 | tiny file |\n\
155 | dialogs |\n\
156 \\_____ ____/\n\
157 \\|\
158 \ntiny file dialogs on Windows needs:\
159 \n a graphic display\
160 \nor dialog.exe (curses console mode)\
161 \nor a console for basic input";
162 #else
163 char tinyfd_needs[] = "\
164 ___________\n\
165 / \\ \n\
166 | tiny file |\n\
167 | dialogs |\n\
168 \\_____ ____/\n\
169 \\|\
170 \ntiny file dialogs on UNIX needs:\
171 \n applescript or kdialog or yad or Xdialog\
172 \nor zenity (or matedialog or shellementary or qarma)\
173 \nor python (2 or 3) + tkinter + python-dbus (optional)\
174 \nor dialog (opens console if needed) ** Disabled by default **/\
175 \nor xterm + bash (opens console for basic input)\
176 \nor existing console for basic input";
177 #endif
178
179 #ifdef _MSC_VER
180 #pragma warning(disable:4996) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
181 #pragma warning(disable:4100) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
182 #pragma warning(disable:4706) /* allows usage of strncpy, strcpy, strcat, sprintf, fopen */
183 #endif
184
185 static int getenvDISPLAY(void)
186 {
187 return tinyfd_assumeGraphicDisplay || getenv("DISPLAY");
188 }
189
190
191 static char * getCurDir(void)
192 {
193 static char lCurDir[MAX_PATH_OR_CMD];
194 return getcwd(lCurDir, sizeof(lCurDir));
195 }
196
197
198 static char * getPathWithoutFinalSlash(
199 char * aoDestination, /* make sure it is allocated, use _MAX_PATH */
200 char const * aSource) /* aoDestination and aSource can be the same */
201 {
202 char const * lTmp ;
203 if ( aSource )
204 {
205 lTmp = strrchr(aSource, '/');
206 if (!lTmp)
207 {
208 lTmp = strrchr(aSource, '\\');
209 }
210 if (lTmp)
211 {
212 strncpy(aoDestination, aSource, lTmp - aSource );
213 aoDestination[lTmp - aSource] = '\0';
214 }
215 else
216 {
217 * aoDestination = '\0';
218 }
219 }
220 else
221 {
222 * aoDestination = '\0';
223 }
224 return aoDestination;
225 }
226
227
228 static char * getLastName(
229 char * aoDestination, /* make sure it is allocated */
230 char const * aSource)
231 {
232 /* copy the last name after '/' or '\' */
233 char const * lTmp ;
234 if ( aSource )
235 {
236 lTmp = strrchr(aSource, '/');
237 if (!lTmp)
238 {
239 lTmp = strrchr(aSource, '\\');
240 }
241 if (lTmp)
242 {
243 strcpy(aoDestination, lTmp + 1);
244 }
245 else
246 {
247 strcpy(aoDestination, aSource);
248 }
249 }
250 else
251 {
252 * aoDestination = '\0';
253 }
254 return aoDestination;
255 }
256
257
258 static void ensureFinalSlash( char * aioString )
259 {
260 if ( aioString && strlen( aioString ) )
261 {
262 char * lastcar = aioString + strlen( aioString ) - 1 ;
263 if ( strncmp( lastcar , SLASH , 1 ) )
264 {
265 strcat( lastcar , SLASH ) ;
266 }
267 }
268 }
269
270
271 static void Hex2RGB( char const aHexRGB[8] , unsigned char aoResultRGB[3] )
272 {
273 char lColorChannel[8] ;
274 if ( aoResultRGB )
275 {
276 if ( aHexRGB )
277 {
278 strcpy(lColorChannel, aHexRGB ) ;
279 aoResultRGB[2] = (unsigned char)strtoul(lColorChannel+5,NULL,16);
280 lColorChannel[5] = '\0';
281 aoResultRGB[1] = (unsigned char)strtoul(lColorChannel+3,NULL,16);
282 lColorChannel[3] = '\0';
283 aoResultRGB[0] = (unsigned char)strtoul(lColorChannel+1,NULL,16);
284 /* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */
285 }
286 else
287 {
288 aoResultRGB[0]=0;
289 aoResultRGB[1]=0;
290 aoResultRGB[2]=0;
291 }
292 }
293 }
294
295 static void RGB2Hex( unsigned char const aRGB[3], char aoResultHexRGB[8] )
296 {
297 if ( aoResultHexRGB )
298 {
299 if ( aRGB )
300 {
301 #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
302 sprintf(aoResultHexRGB, "#%02hhx%02hhx%02hhx", aRGB[0], aRGB[1], aRGB[2]);
303 #else
304 sprintf(aoResultHexRGB, "#%02hx%02hx%02hx", aRGB[0], aRGB[1], aRGB[2]);
305 #endif
306 /*printf("aoResultHexRGB %s\n", aoResultHexRGB);*/
307 }
308 else
309 {
310 aoResultHexRGB[0]=0;
311 aoResultHexRGB[1]=0;
312 aoResultHexRGB[2]=0;
313 }
314 }
315 }
316
317
318 void tfd_replaceSubStr( char const * aSource, char const * aOldSubStr,
319 char const * aNewSubStr, char * aoDestination )
320 {
321 char const * pOccurence ;
322 char const * p ;
323 char const * lNewSubStr = "" ;
324 size_t lOldSubLen = strlen( aOldSubStr ) ;
325
326 if ( ! aSource )
327 {
328 * aoDestination = '\0' ;
329 return ;
330 }
331 if ( ! aOldSubStr )
332 {
333 strcpy( aoDestination , aSource ) ;
334 return ;
335 }
336 if ( aNewSubStr )
337 {
338 lNewSubStr = aNewSubStr ;
339 }
340 p = aSource ;
341 * aoDestination = '\0' ;
342 while ( ( pOccurence = strstr( p , aOldSubStr ) ) != NULL )
343 {
344 strncat( aoDestination , p , pOccurence - p ) ;
345 strcat( aoDestination , lNewSubStr ) ;
346 p = pOccurence + lOldSubLen ;
347 }
348 strcat( aoDestination , p ) ;
349 }
350
351
352 static int filenameValid( char const * aFileNameWithoutPath )
353 {
354 if ( ! aFileNameWithoutPath
355 || ! strlen(aFileNameWithoutPath)
356 || strpbrk(aFileNameWithoutPath , "\\/:*?\"<>|") )
357 {
358 return 0 ;
359 }
360 return 1 ;
361 }
362
363 #ifndef _WIN32
364
365 static int fileExists( char const * aFilePathAndName )
366 {
367 FILE * lIn ;
368 if ( ! aFilePathAndName || ! strlen(aFilePathAndName) )
369 {
370 return 0 ;
371 }
372 lIn = fopen( aFilePathAndName , "r" ) ;
373 if ( ! lIn )
374 {
375 return 0 ;
376 }
377 fclose( lIn ) ;
378 return 1 ;
379 }
380
381 #endif
382
383
384 static void wipefile(char const * aFilename)
385 {
386 int i;
387 struct stat st;
388 FILE * lIn;
389
390 if (stat(aFilename, &st) == 0)
391 {
392 if ((lIn = fopen(aFilename, "w")))
393 {
394 for (i = 0; i < st.st_size; i++)
395 {
396 fputc('A', lIn);
397 }
398 fclose(lIn);
399 }
400 }
401 }
402
403
404 int tfd_quoteDetected(char const * aString)
405 {
406 char const * p;
407
408 if (!aString) return 0;
409
410 p = aString;
411 while ((p = strchr(p, '\'')))
412 {
413 return 1;
414 }
415
416 p = aString;
417 while ((p = strchr(p, '\"')))
418 {
419 return 1;
420 }
421
422 return 0;
423 }
424
425
426 char const * tinyfd_getGlobalChar(char const * aCharVariableName) /* to be called from C# (you don't need this in C or C++) */
427 {
428 if (!aCharVariableName || !strlen(aCharVariableName)) return NULL;
429 else if (!strcmp(aCharVariableName, "tinyfd_version")) return tinyfd_version;
430 else if (!strcmp(aCharVariableName, "tinyfd_needs")) return tinyfd_needs;
431 else if (!strcmp(aCharVariableName, "tinyfd_response")) return tinyfd_response;
432 else return NULL ;
433 }
434
435
436 int tinyfd_getGlobalInt(char const * aIntVariableName) /* to be called from C# (you don't need this in C or C++) */
437 {
438 if ( !aIntVariableName || !strlen(aIntVariableName) ) return -1 ;
439 else if ( !strcmp(aIntVariableName, "tinyfd_verbose") ) return tinyfd_verbose ;
440 else if ( !strcmp(aIntVariableName, "tinyfd_silent") ) return tinyfd_silent ;
441 else if ( !strcmp(aIntVariableName, "tinyfd_allowCursesDialogs") ) return tinyfd_allowCursesDialogs ;
442 else if ( !strcmp(aIntVariableName, "tinyfd_forceConsole") ) return tinyfd_forceConsole ;
443 else if ( !strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay") ) return tinyfd_assumeGraphicDisplay ;
444 #ifdef _WIN32
445 else if ( !strcmp(aIntVariableName, "tinyfd_winUtf8") ) return tinyfd_winUtf8 ;
446 #endif
447 else return -1;
448 }
449
450
451 int tinyfd_setGlobalInt(char const * aIntVariableName, int aValue) /* to be called from C# (you don't need this in C or C++) */
452 {
453 if (!aIntVariableName || !strlen(aIntVariableName)) return -1 ;
454 else if (!strcmp(aIntVariableName, "tinyfd_verbose")) { tinyfd_verbose = aValue; return tinyfd_verbose; }
455 else if (!strcmp(aIntVariableName, "tinyfd_silent")) { tinyfd_silent = aValue; return tinyfd_silent; }
456 else if (!strcmp(aIntVariableName, "tinyfd_allowCursesDialogs")) { tinyfd_allowCursesDialogs = aValue; return tinyfd_allowCursesDialogs; }
457 else if (!strcmp(aIntVariableName, "tinyfd_forceConsole")) { tinyfd_forceConsole = aValue; return tinyfd_forceConsole; }
458 else if (!strcmp(aIntVariableName, "tinyfd_assumeGraphicDisplay")) { tinyfd_assumeGraphicDisplay = aValue; return tinyfd_assumeGraphicDisplay; }
459 #ifdef _WIN32
460 else if (!strcmp(aIntVariableName, "tinyfd_winUtf8")) { tinyfd_winUtf8 = aValue; return tinyfd_winUtf8; }
461 #endif
462 else return -1;
463 }
464
465
466 #ifdef _WIN32
467 static int powershellPresent(void)
468 { /*only on vista and above (or installed on xp)*/
469 static int lPowershellPresent = -1;
470 char lBuff[MAX_PATH_OR_CMD];
471 FILE* lIn;
472 char const* lString = "powershell.exe";
473
474 if (lPowershellPresent < 0)
475 {
476 if (!(lIn = _popen("where powershell.exe", "r")))
477 {
478 lPowershellPresent = 0;
479 return 0;
480 }
481 while (fgets(lBuff, sizeof(lBuff), lIn) != NULL)
482 {
483 }
484 _pclose(lIn);
485 if (lBuff[strlen(lBuff) - 1] == '\n')
486 {
487 lBuff[strlen(lBuff) - 1] = '\0';
488 }
489 if (strcmp(lBuff + strlen(lBuff) - strlen(lString), lString))
490 {
491 lPowershellPresent = 0;
492 }
493 else
494 {
495 lPowershellPresent = 1;
496 }
497 }
498 return lPowershellPresent;
499 }
500
501 static int windowsVersion(void)
502 {
503 #if !defined(__MINGW32__) || defined(__MINGW64_VERSION_MAJOR)
504 typedef LONG NTSTATUS ;
505 typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
506 HMODULE hMod;
507 RtlGetVersionPtr lFxPtr;
508 RTL_OSVERSIONINFOW lRovi = { 0 };
509
510 hMod = GetModuleHandleW(L"ntdll.dll");
511 if (hMod) {
512 lFxPtr = (RtlGetVersionPtr)GetProcAddress(hMod, "RtlGetVersion");
513 if (lFxPtr)
514 {
515 lRovi.dwOSVersionInfoSize = sizeof(lRovi);
516 if (!lFxPtr(&lRovi))
517 {
518 return lRovi.dwMajorVersion;
519 }
520 }
521 }
522 #endif
523 if (powershellPresent()) return 6; /*minimum is vista or installed on xp*/
524 return 0;
525 }
526
527
528 static void replaceChr(char * aString, char aOldChr, char aNewChr)
529 {
530 char * p;
531
532 if (!aString) return;
533 if (aOldChr == aNewChr) return;
534
535 p = aString;
536 while ((p = strchr(p, aOldChr)))
537 {
538 *p = aNewChr;
539 p++;
540 }
541 return;
542 }
543
544
545 #if !defined(WC_ERR_INVALID_CHARS)
546 /* undefined prior to Vista, so not yet in MINGW header file */
547 #define WC_ERR_INVALID_CHARS 0x00000000 /* 0x00000080 for MINGW maybe ? */
548 #endif
549
550 static int sizeUtf16From8(char const * aUtf8string)
551 {
552 return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
553 aUtf8string, -1, NULL, 0);
554 }
555
556
557 static int sizeUtf16FromMbcs(char const * aMbcsString)
558 {
559 return MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS,
560 aMbcsString, -1, NULL, 0);
561 }
562
563
564 static int sizeUtf8(wchar_t const * aUtf16string)
565 {
566 return WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
567 aUtf16string, -1, NULL, 0, NULL, NULL);
568 }
569
570
571 static int sizeMbcs(wchar_t const * aMbcsString)
572 {
573 int lRes = WideCharToMultiByte(CP_ACP, 0,
574 aMbcsString, -1, NULL, 0, NULL, NULL);
575 /* DWORD licic = GetLastError(); */
576 return lRes;
577 }
578
579
580 wchar_t* tinyfd_mbcsTo16(char const* aMbcsString)
581 {
582 static wchar_t* lMbcsString = NULL;
583 int lSize;
584
585 free(lMbcsString);
586 if (!aMbcsString) { lMbcsString = NULL; return NULL; }
587 lSize = sizeUtf16FromMbcs(aMbcsString);
588 if (lSize)
589 {
590 lMbcsString = (wchar_t*)malloc(lSize * sizeof(wchar_t));
591 lSize = MultiByteToWideChar(CP_ACP, 0, aMbcsString, -1, lMbcsString, lSize);
592 }
593 else wcscpy(lMbcsString, L"");
594 return lMbcsString;
595 }
596
597
598 wchar_t * tinyfd_utf8to16(char const * aUtf8string)
599 {
600 static wchar_t * lUtf16string = NULL;
601 int lSize;
602
603 free(lUtf16string);
604 if (!aUtf8string) {lUtf16string = NULL; return NULL;}
605 lSize = sizeUtf16From8(aUtf8string);
606 if (lSize)
607 {
608 lUtf16string = (wchar_t*)malloc(lSize * sizeof(wchar_t));
609 lSize = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
610 aUtf8string, -1, lUtf16string, lSize);
611 return lUtf16string;
612 }
613 else
614 {
615 /* let's try mbcs anyway */
616 lUtf16string = NULL;
617 return tinyfd_mbcsTo16(aUtf8string);
618 }
619 }
620
621
622 char * tinyfd_utf16toMbcs(wchar_t const * aUtf16string)
623 {
624 static char * lMbcsString = NULL;
625 int lSize;
626
627 free(lMbcsString);
628 if (!aUtf16string) { lMbcsString = NULL; return NULL; }
629 lSize = sizeMbcs(aUtf16string);
630 if (lSize)
631 {
632 lMbcsString = (char*)malloc(lSize);
633 lSize = WideCharToMultiByte(CP_ACP, 0, aUtf16string, -1, lMbcsString, lSize, NULL, NULL);
634 }
635 else strcpy(lMbcsString, "");
636 return lMbcsString;
637 }
638
639
640 char * tinyfd_utf8toMbcs(char const * aUtf8string)
641 {
642 wchar_t const * lUtf16string;
643 lUtf16string = tinyfd_utf8to16(aUtf8string);
644 return tinyfd_utf16toMbcs(lUtf16string);
645 }
646
647
648 char * tinyfd_utf16to8(wchar_t const * aUtf16string)
649 {
650 static char * lUtf8string = NULL;
651 int lSize;
652
653 free(lUtf8string);
654 if (!aUtf16string) { lUtf8string = NULL; return NULL; }
655 lSize = sizeUtf8(aUtf16string);
656 if (lSize)
657 {
658 lUtf8string = (char*)malloc(lSize);
659 lSize = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, aUtf16string, -1, lUtf8string, lSize, NULL, NULL);
660 }
661 else strcpy(lUtf8string, "");
662 return lUtf8string;
663 }
664
665
666 char * tinyfd_mbcsTo8(char const * aMbcsString)
667 {
668 wchar_t const * lUtf16string;
669 lUtf16string = tinyfd_mbcsTo16(aMbcsString);
670 return tinyfd_utf16to8(lUtf16string);
671 }
672
673
674 void tinyfd_beep(void)
675 {
676 if (windowsVersion() > 5) Beep(440, 300);
677 else MessageBeep(MB_OK);
678 }
679
680
681 static void wipefileW(wchar_t const * aFilename)
682 {
683 int i;
684 FILE * lIn;
685 #if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
686 struct _stat st;
687 if (_wstat(aFilename, &st) == 0)
688 #else
689 struct __stat64 st;
690 if (_wstat64(aFilename, &st) == 0)
691 #endif
692 {
693 if ((lIn = _wfopen(aFilename, L"w")))
694 {
695 for (i = 0; i < st.st_size; i++)
696 {
697 fputc('A', lIn);
698 }
699 fclose(lIn);
700 }
701 }
702 }
703
704
705 static wchar_t * getPathWithoutFinalSlashW(
706 wchar_t * aoDestination, /* make sure it is allocated, use _MAX_PATH */
707 wchar_t const * aSource) /* aoDestination and aSource can be the same */
708 {
709 wchar_t const * lTmp;
710 if (aSource)
711 {
712 lTmp = wcsrchr(aSource, L'/');
713 if (!lTmp)
714 {
715 lTmp = wcsrchr(aSource, L'\\');
716 }
717 if (lTmp)
718 {
719 wcsncpy(aoDestination, aSource, lTmp - aSource);
720 aoDestination[lTmp - aSource] = L'\0';
721 }
722 else
723 {
724 *aoDestination = L'\0';
725 }
726 }
727 else
728 {
729 *aoDestination = L'\0';
730 }
731 return aoDestination;
732 }
733
734
735 static wchar_t * getLastNameW(
736 wchar_t * aoDestination, /* make sure it is allocated */
737 wchar_t const * aSource)
738 {
739 /* copy the last name after '/' or '\' */
740 wchar_t const * lTmp;
741 if (aSource)
742 {
743 lTmp = wcsrchr(aSource, L'/');
744 if (!lTmp)
745 {
746 lTmp = wcsrchr(aSource, L'\\');
747 }
748 if (lTmp)
749 {
750 wcscpy(aoDestination, lTmp + 1);
751 }
752 else
753 {
754 wcscpy(aoDestination, aSource);
755 }
756 }
757 else
758 {
759 *aoDestination = L'\0';
760 }
761 return aoDestination;
762 }
763
764
765 static void Hex2RGBW(wchar_t const aHexRGB[8], unsigned char aoResultRGB[3])
766 {
767 wchar_t lColorChannel[8];
768 if (aoResultRGB)
769 {
770 if (aHexRGB)
771 {
772 wcscpy(lColorChannel, aHexRGB);
773 aoResultRGB[2] = (unsigned char)wcstoul(lColorChannel + 5, NULL, 16);
774 lColorChannel[5] = '\0';
775 aoResultRGB[1] = (unsigned char)wcstoul(lColorChannel + 3, NULL, 16);
776 lColorChannel[3] = '\0';
777 aoResultRGB[0] = (unsigned char)wcstoul(lColorChannel + 1, NULL, 16);
778 /* printf("%d %d %d\n", aoResultRGB[0], aoResultRGB[1], aoResultRGB[2]); */
779 }
780 else
781 {
782 aoResultRGB[0] = 0;
783 aoResultRGB[1] = 0;
784 aoResultRGB[2] = 0;
785 }
786 }
787 }
788
789
790 static void RGB2HexW( unsigned char const aRGB[3], wchar_t aoResultHexRGB[8])
791 {
792 #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
793 wchar_t const * const lPrintFormat = L"#%02hhx%02hhx%02hhx";
794 #else
795 wchar_t const * const lPrintFormat = L"#%02hx%02hx%02hx";
796 #endif
797
798 if (aoResultHexRGB)
799 {
800 if (aRGB)
801 {
802 /* wprintf(L"aoResultHexRGB %s\n", aoResultHexRGB); */
803 #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
804 swprintf(aoResultHexRGB, 8, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]);
805 #else
806 swprintf(aoResultHexRGB, lPrintFormat, aRGB[0], aRGB[1], aRGB[2]);
807 #endif
808
809 }
810 else
811 {
812 aoResultHexRGB[0] = 0;
813 aoResultHexRGB[1] = 0;
814 aoResultHexRGB[2] = 0;
815 }
816 }
817 }
818
819
820 static int dirExists(char const * aDirPath)
821 {
822 #if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
823 struct _stat lInfo;
824 #else
825 struct __stat64 lInfo;
826 #endif
827 wchar_t * lTmpWChar;
828 int lStatRet;
829 size_t lDirLen;
830
831 if (!aDirPath)
832 return 0;
833 lDirLen = strlen(aDirPath);
834 if (!lDirLen)
835 return 1;
836 if ( (lDirLen == 2) && (aDirPath[1] == ':') )
837 return 1;
838
839 if (tinyfd_winUtf8)
840 {
841 lTmpWChar = tinyfd_utf8to16(aDirPath);
842 #if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
843 lStatRet = _wstat(lTmpWChar, &lInfo);
844 #else
845 lStatRet = _wstat64(lTmpWChar, &lInfo);
846 #endif
847 if (lStatRet != 0)
848 return 0;
849 else if (lInfo.st_mode & S_IFDIR)
850 return 1;
851 else
852 return 0;
853 }
854 #if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
855 else if (_stat(aDirPath, &lInfo) != 0)
856 #else
857 else if (_stat64(aDirPath, &lInfo) != 0)
858 #endif
859 return 0;
860 else if (lInfo.st_mode & S_IFDIR)
861 return 1;
862 else
863 return 0;
864 }
865
866
867 static int fileExists(char const * aFilePathAndName)
868 {
869 #if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
870 struct _stat lInfo;
871 #else
872 struct __stat64 lInfo;
873 #endif
874 wchar_t * lTmpWChar;
875 int lStatRet;
876 FILE * lIn;
877
878 if (!aFilePathAndName || !strlen(aFilePathAndName))
879 {
880 return 0;
881 }
882
883 if (tinyfd_winUtf8)
884 {
885 lTmpWChar = tinyfd_utf8to16(aFilePathAndName);
886 #if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64__) && (__MINGW32_MAJOR_VERSION <= 3)
887 lStatRet = _wstat(lTmpWChar, &lInfo);
888 #else
889 lStatRet = _wstat64(lTmpWChar, &lInfo);
890 #endif
891
892 if (lStatRet != 0)
893 return 0;
894 else if (lInfo.st_mode & _S_IFREG)
895 return 1;
896 else
897 return 0;
898 }
899 else
900 {
901 lIn = fopen(aFilePathAndName, "r");
902 if (!lIn)
903 {
904 return 0;
905 }
906 fclose(lIn);
907 return 1;
908 }
909 }
910
911 static void replaceWchar(wchar_t * aString,
912 wchar_t aOldChr,
913 wchar_t aNewChr)
914 {
915 wchar_t * p;
916
917 if (!aString)
918 {
919 return ;
920 }
921
922 if (aOldChr == aNewChr)
923 {
924 return ;
925 }
926
927 p = aString;
928 while ((p = wcsrchr(p, aOldChr)))
929 {
930 *p = aNewChr;
931 #ifdef TINYFD_NOCCSUNICODE
932 p++;
933 #endif
934 p++;
935 }
936 return ;
937 }
938
939
940 static int quoteDetectedW(wchar_t const * aString)
941 {
942 wchar_t const * p;
943
944 if (!aString) return 0;
945
946 p = aString;
947 while ((p = wcsrchr(p, L'\'')))
948 {
949 return 1;
950 }
951
952 p = aString;
953 while ((p = wcsrchr(p, L'\"')))
954 {
955 return 1;
956 }
957
958 return 0;
959 }
960
961 #endif /* _WIN32 */
962
963 /* source and destination can be the same or ovelap*/
964 static char * ensureFilesExist(char * aDestination,
965 char const * aSourcePathsAndNames)
966 {
967 char * lDestination = aDestination;
968 char const * p;
969 char const * p2;
970 size_t lLen;
971
972 if (!aSourcePathsAndNames)
973 {
974 return NULL;
975 }
976 lLen = strlen(aSourcePathsAndNames);
977 if (!lLen)
978 {
979 return NULL;
980 }
981
982 p = aSourcePathsAndNames;
983 while ((p2 = strchr(p, '|')) != NULL)
984 {
985 lLen = p2 - p;
986 memmove(lDestination, p, lLen);
987 lDestination[lLen] = '\0';
988 if (fileExists(lDestination))
989 {
990 lDestination += lLen;
991 *lDestination = '|';
992 lDestination++;
993 }
994 p = p2 + 1;
995 }
996 if (fileExists(p))
997 {
998 lLen = strlen(p);
999 memmove(lDestination, p, lLen);
1000 lDestination[lLen] = '\0';
1001 }
1002 else
1003 {
1004 *(lDestination - 1) = '\0';
1005 }
1006 return aDestination;
1007 }
1008
1009 #ifdef _WIN32
1010
1011 static int __stdcall EnumThreadWndProc(HWND hwnd, LPARAM lParam)
1012 {
1013 wchar_t lTitleName[MAX_PATH];
1014 wchar_t const* lDialogTitle = (wchar_t const *) lParam;
1015
1016 GetWindowTextW(hwnd, lTitleName, MAX_PATH);
1017 /* wprintf(L"lTitleName %ls \n", lTitleName); */
1018
1019 if (wcscmp(lDialogTitle, lTitleName) == 0)
1020 {
1021 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
1022 return 0;
1023 }
1024 return 1;
1025 }
1026
1027
1028 static void hiddenConsoleW(wchar_t const * aString, wchar_t const * aDialogTitle, int aInFront)
1029 {
1030 STARTUPINFOW StartupInfo;
1031 PROCESS_INFORMATION ProcessInfo;
1032
1033 if (!aString || !wcslen(aString) ) return;
1034
1035 memset(&StartupInfo, 0, sizeof(StartupInfo));
1036 StartupInfo.cb = sizeof(STARTUPINFOW);
1037 StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
1038 StartupInfo.wShowWindow = SW_HIDE;
1039
1040 if (!CreateProcessW(NULL, (LPWSTR)aString, NULL, NULL, FALSE,
1041 CREATE_NEW_CONSOLE, NULL, NULL,
1042 &StartupInfo, &ProcessInfo))
1043 {
1044 return; /* GetLastError(); */
1045 }
1046
1047 WaitForInputIdle(ProcessInfo.hProcess, INFINITE);
1048 if (aInFront)
1049 {
1050 while (EnumWindows(EnumThreadWndProc, (LPARAM)aDialogTitle)) {}
1051 }
1052 WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
1053 CloseHandle(ProcessInfo.hThread);
1054 CloseHandle(ProcessInfo.hProcess);
1055 }
1056
1057
1058 int tinyfd_messageBoxW(
1059 wchar_t const * aTitle, /* NULL or "" */
1060 wchar_t const * aMessage, /* NULL or "" may contain \n and \t */
1061 wchar_t const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */
1062 wchar_t const * aIconType, /* "info" "warning" "error" "question" */
1063 int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
1064 {
1065 int lBoxReturnValue;
1066 UINT aCode;
1067
1068 if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return 1; }
1069
1070 if (quoteDetectedW(aTitle)) return tinyfd_messageBoxW(L"INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton);
1071 if (quoteDetectedW(aMessage)) return tinyfd_messageBoxW(aTitle, L"INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton);
1072
1073 if (aIconType && !wcscmp(L"warning", aIconType))
1074 {
1075 aCode = MB_ICONWARNING;
1076 }
1077 else if (aIconType && !wcscmp(L"error", aIconType))
1078 {
1079 aCode = MB_ICONERROR;
1080 }
1081 else if (aIconType && !wcscmp(L"question", aIconType))
1082 {
1083 aCode = MB_ICONQUESTION;
1084 }
1085 else
1086 {
1087 aCode = MB_ICONINFORMATION;
1088 }
1089
1090 if (aDialogType && !wcscmp(L"okcancel", aDialogType))
1091 {
1092 aCode += MB_OKCANCEL;
1093 if (!aDefaultButton)
1094 {
1095 aCode += MB_DEFBUTTON2;
1096 }
1097 }
1098 else if (aDialogType && !wcscmp(L"yesno", aDialogType))
1099 {
1100 aCode += MB_YESNO;
1101 if (!aDefaultButton)
1102 {
1103 aCode += MB_DEFBUTTON2;
1104 }
1105 }
1106 else if (aDialogType && !wcscmp(L"yesnocancel", aDialogType))
1107 {
1108 aCode += MB_YESNOCANCEL;
1109 if (aDefaultButton == 1)
1110 {
1111 aCode += MB_DEFBUTTON1;
1112 }
1113 else if (aDefaultButton == 2)
1114 {
1115 aCode += MB_DEFBUTTON2;
1116 }
1117 else
1118 {
1119 aCode += MB_DEFBUTTON3;
1120 }
1121 }
1122 else
1123 {
1124 aCode += MB_OK;
1125 }
1126
1127 aCode += MB_TOPMOST;
1128
1129 lBoxReturnValue = MessageBoxW(GetForegroundWindow(), aMessage, aTitle, aCode);
1130
1131 if ( (lBoxReturnValue == IDNO) && (aDialogType && !wcscmp(L"yesnocancel", aDialogType)) )
1132 {
1133 return 2;
1134 }
1135 else if ( (lBoxReturnValue == IDOK) || (lBoxReturnValue == IDYES) )
1136 {
1137 return 1;
1138 }
1139 else
1140 {
1141 return 0;
1142 }
1143 }
1144
1145
1146 /* return has only meaning for tinyfd_query */
1147 int tinyfd_notifyPopupW(
1148 wchar_t const * aTitle, /* NULL or L"" */
1149 wchar_t const * aMessage, /* NULL or L"" may contain \n \t */
1150 wchar_t const * aIconType) /* L"info" L"warning" L"error" */
1151 {
1152 wchar_t * lDialogString;
1153 size_t lTitleLen;
1154 size_t lMessageLen;
1155 size_t lDialogStringLen;
1156
1157 if (aTitle && !wcscmp(aTitle, L"tinyfd_query")) { strcpy(tinyfd_response, "windows_wchar"); return 1; }
1158
1159 if (quoteDetectedW(aTitle)) return tinyfd_notifyPopupW(L"INVALID TITLE WITH QUOTES", aMessage, aIconType);
1160 if (quoteDetectedW(aMessage)) return tinyfd_notifyPopupW(aTitle, L"INVALID MESSAGE WITH QUOTES", aIconType);
1161
1162 lTitleLen = aTitle ? wcslen(aTitle) : 0;
1163 lMessageLen = aMessage ? wcslen(aMessage) : 0;
1164 lDialogStringLen = 3 * MAX_PATH_OR_CMD + lTitleLen + lMessageLen;
1165 lDialogString = (wchar_t *)malloc(2 * lDialogStringLen);
1166 if (!lDialogString) return 0;
1167
1168 wcscpy(lDialogString, L"powershell.exe -command \"\
1169 function Show-BalloonTip {\
1170 [cmdletbinding()] \
1171 param( \
1172 [string]$Title = ' ', \
1173 [string]$Message = ' ', \
1174 [ValidateSet('info', 'warning', 'error')] \
1175 [string]$IconType = 'info');\
1176 [system.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null ; \
1177 $balloon = New-Object System.Windows.Forms.NotifyIcon ; \
1178 $path = Get-Process -id $pid | Select-Object -ExpandProperty Path ; \
1179 $icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) ;");
1180
1181 wcscat(lDialogString, L"\
1182 $balloon.Icon = $icon ; \
1183 $balloon.BalloonTipIcon = $IconType ; \
1184 $balloon.BalloonTipText = $Message ; \
1185 $balloon.BalloonTipTitle = $Title ; \
1186 $balloon.Text = 'tinyfiledialogs' ; \
1187 $balloon.Visible = $true ; \
1188 $balloon.ShowBalloonTip(5000)};\
1189 Show-BalloonTip");
1190
1191 if (aTitle && wcslen(aTitle))
1192 {
1193 wcscat(lDialogString, L" -Title '");
1194 wcscat(lDialogString, aTitle);
1195 wcscat(lDialogString, L"'");
1196 }
1197 if (aMessage && wcslen(aMessage))
1198 {
1199 wcscat(lDialogString, L" -Message '");
1200 wcscat(lDialogString, aMessage);
1201 wcscat(lDialogString, L"'");
1202 }
1203 if (aMessage && wcslen(aIconType))
1204 {
1205 wcscat(lDialogString, L" -IconType '");
1206 wcscat(lDialogString, aIconType);
1207 wcscat(lDialogString, L"'");
1208 }
1209 wcscat(lDialogString, L"\"");
1210
1211 /* wprintf ( L"lDialogString: %ls\n" , lDialogString ) ; */
1212
1213 hiddenConsoleW(lDialogString, aTitle, 0);
1214 free(lDialogString);
1215 return 1;
1216 }
1217
1218
1219 wchar_t * tinyfd_inputBoxW(
1220 wchar_t const * aTitle, /* NULL or L"" */
1221 wchar_t const * aMessage, /* NULL or L"" (\n and \t have no effect) */
1222 wchar_t const * aDefaultInput) /* L"" , if NULL it's a passwordBox */
1223 {
1224 static wchar_t lBuff[MAX_PATH_OR_CMD];
1225 wchar_t * lDialogString;
1226 FILE * lIn;
1227 FILE * lFile;
1228 int lResult;
1229 size_t lTitleLen;
1230 size_t lMessageLen;
1231 size_t lDialogStringLen;
1232
1233 if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; }
1234
1235 if (quoteDetectedW(aTitle)) return tinyfd_inputBoxW(L"INVALID TITLE WITH QUOTES", aMessage, aDefaultInput);
1236 if (quoteDetectedW(aMessage)) return tinyfd_inputBoxW(aTitle, L"INVALID MESSAGE WITH QUOTES", aDefaultInput);
1237 if (quoteDetectedW(aDefaultInput)) return tinyfd_inputBoxW(aTitle, aMessage, L"INVALID DEFAULT_INPUT WITH QUOTES");
1238
1239 lTitleLen = aTitle ? wcslen(aTitle) : 0 ;
1240 lMessageLen = aMessage ? wcslen(aMessage) : 0 ;
1241 lDialogStringLen = 3 * MAX_PATH_OR_CMD + lTitleLen + lMessageLen;
1242 lDialogString = (wchar_t *)malloc(2 * lDialogStringLen);
1243
1244 if (aDefaultInput)
1245 {
1246 swprintf(lDialogString,
1247 #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
1248 lDialogStringLen,
1249 #endif
1250 L"%ls\\tinyfd.vbs", _wgetenv(L"TEMP"));
1251 }
1252 else
1253 {
1254 swprintf(lDialogString,
1255 #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
1256 lDialogStringLen,
1257 #endif
1258 L"%ls\\tinyfd.hta", _wgetenv(L"TEMP"));
1259 }
1260 lIn = _wfopen(lDialogString, L"w");
1261 if (!lIn)
1262 {
1263 free(lDialogString);
1264 return NULL;
1265 }
1266
1267 if ( aDefaultInput )
1268 {
1269 wcscpy(lDialogString, L"Dim result:result=InputBox(\"");
1270 if (aMessage && wcslen(aMessage))
1271 {
1272 wcscpy(lBuff, aMessage);
1273 replaceWchar(lBuff, L'\n', L' ');
1274 wcscat(lDialogString, lBuff);
1275 }
1276 wcscat(lDialogString, L"\",\"");
1277 if (aTitle) wcscat(lDialogString, aTitle);
1278 wcscat(lDialogString, L"\",\"");
1279
1280 if (aDefaultInput && wcslen(aDefaultInput))
1281 {
1282 wcscpy(lBuff, aDefaultInput);
1283 replaceWchar(lBuff, L'\n', L' ');
1284 wcscat(lDialogString, lBuff);
1285 }
1286 wcscat(lDialogString, L"\"):If IsEmpty(result) then:WScript.Echo 0");
1287 wcscat(lDialogString, L":Else: WScript.Echo \"1\" & result : End If");
1288 }
1289 else
1290 {
1291 wcscpy(lDialogString, L"\n\
1292 <html>\n\
1293 <head>\n\
1294 <title>");
1295 if (aTitle) wcscat(lDialogString, aTitle);
1296 wcscat(lDialogString, L"</title>\n\
1297 <HTA:APPLICATION\n\
1298 ID = 'tinyfdHTA'\n\
1299 APPLICATIONNAME = 'tinyfd_inputBox'\n\
1300 MINIMIZEBUTTON = 'no'\n\
1301 MAXIMIZEBUTTON = 'no'\n\
1302 BORDER = 'dialog'\n\
1303 SCROLL = 'no'\n\
1304 SINGLEINSTANCE = 'yes'\n\
1305 WINDOWSTATE = 'hidden'>\n\
1306 \n\
1307 <script language = 'VBScript'>\n\
1308 \n\
1309 intWidth = Screen.Width/4\n\
1310 intHeight = Screen.Height/6\n\
1311 ResizeTo intWidth, intHeight\n\
1312 MoveTo((Screen.Width/2)-(intWidth/2)),((Screen.Height/2)-(intHeight/2))\n\
1313 result = 0\n\
1314 \n\
1315 Sub Window_onLoad\n\
1316 txt_input.Focus\n\
1317 End Sub\n\
1318 \n");
1319
1320 wcscat(lDialogString, L"\
1321 Sub Window_onUnload\n\
1322 Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n\
1323 Set oShell = CreateObject(\"WScript.Shell\")\n\
1324 strTempFolder = oShell.ExpandEnvironmentStrings(\"%TEMP%\")\n\
1325 Set objFile = objFSO.CreateTextFile(strTempFolder & \"\\tinyfd.txt\",True,True)\n\
1326 If result = 1 Then\n\
1327 objFile.Write 1 & txt_input.Value\n\
1328 Else\n\
1329 objFile.Write 0\n\
1330 End If\n\
1331 objFile.Close\n\
1332 End Sub\n\
1333 \n\
1334 Sub Run_ProgramOK\n\
1335 result = 1\n\
1336 window.Close\n\
1337 End Sub\n\
1338 \n\
1339 Sub Run_ProgramCancel\n\
1340 window.Close\n\
1341 End Sub\n\
1342 \n");
1343
1344 wcscat(lDialogString, L"Sub Default_Buttons\n\
1345 If Window.Event.KeyCode = 13 Then\n\
1346 btn_OK.Click\n\
1347 ElseIf Window.Event.KeyCode = 27 Then\n\
1348 btn_Cancel.Click\n\
1349 End If\n\
1350 End Sub\n\
1351 \n\
1352 </script>\n\
1353 </head>\n\
1354 <body style = 'background-color:#EEEEEE' onkeypress = 'vbs:Default_Buttons' align = 'top'>\n\
1355 <table width = '100%' height = '80%' align = 'center' border = '0'>\n\
1356 <tr border = '0'>\n\
1357 <td align = 'left' valign = 'middle' style='Font-Family:Arial'>\n");
1358
1359 wcscat(lDialogString, aMessage ? aMessage : L"");
1360
1361 wcscat(lDialogString, L"\n\
1362 </td>\n\
1363 <td align = 'right' valign = 'middle' style = 'margin-top: 0em'>\n\
1364 <table align = 'right' style = 'margin-right: 0em;'>\n\
1365 <tr align = 'right' style = 'margin-top: 5em;'>\n\
1366 <input type = 'button' value = 'OK' name = 'btn_OK' onClick = 'vbs:Run_ProgramOK' style = 'width: 5em; margin-top: 2em;'><br>\n\
1367 <input type = 'button' value = 'Cancel' name = 'btn_Cancel' onClick = 'vbs:Run_ProgramCancel' style = 'width: 5em;'><br><br>\n\
1368 </tr>\n\
1369 </table>\n\
1370 </td>\n\
1371 </tr>\n\
1372 </table>\n");
1373
1374 wcscat(lDialogString, L"<table width = '100%' height = '100%' align = 'center' border = '0'>\n\
1375 <tr>\n\
1376 <td align = 'left' valign = 'top'>\n\
1377 <input type = 'password' id = 'txt_input'\n\
1378 name = 'txt_input' value = '' style = 'float:left;width:100%' ><BR>\n\
1379 </td>\n\
1380 </tr>\n\
1381 </table>\n\
1382 </body>\n\
1383 </html>\n\
1384 " ) ;
1385 }
1386 fputws(lDialogString, lIn);
1387 fclose(lIn);
1388
1389 if (aDefaultInput)
1390 {
1391 swprintf(lDialogString,
1392 #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
1393 lDialogStringLen,
1394 #endif
1395 L"%ls\\tinyfd.txt",_wgetenv(L"TEMP"));
1396
1397 #ifdef TINYFD_NOCCSUNICODE
1398 lFile = _wfopen(lDialogString, L"w");
1399 fputc(0xFF, lFile);
1400 fputc(0xFE, lFile);
1401 #else
1402 lFile = _wfopen(lDialogString, L"wt, ccs=UNICODE"); /*or ccs=UTF-16LE*/
1403 #endif
1404 fclose(lFile);
1405
1406 wcscpy(lDialogString, L"cmd.exe /c cscript.exe //U //Nologo ");
1407 wcscat(lDialogString, L"\"%TEMP%\\tinyfd.vbs\" ");
1408 wcscat(lDialogString, L">> \"%TEMP%\\tinyfd.txt\"");
1409 }
1410 else
1411 {
1412 wcscpy(lDialogString,
1413 L"cmd.exe /c mshta.exe \"%TEMP%\\tinyfd.hta\"");
1414 }
1415
1416 /* wprintf ( "lDialogString: %ls\n" , lDialogString ) ; */
1417
1418 hiddenConsoleW(lDialogString, aTitle, 1);
1419
1420 swprintf(lDialogString,
1421 #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
1422 lDialogStringLen,
1423 #endif
1424 L"%ls\\tinyfd.txt", _wgetenv(L"TEMP"));
1425 /* wprintf(L"lDialogString: %ls\n", lDialogString); */
1426 #ifdef TINYFD_NOCCSUNICODE
1427 if (!(lIn = _wfopen(lDialogString, L"r")))
1428 #else
1429 if (!(lIn = _wfopen(lDialogString, L"rt, ccs=UNICODE"))) /*or ccs=UTF-16LE*/
1430 #endif
1431 {
1432 _wremove(lDialogString);
1433 free(lDialogString);
1434 return NULL;
1435 }
1436
1437 memset(lBuff, 0, MAX_PATH_OR_CMD * sizeof(wchar_t) );
1438
1439 #ifdef TINYFD_NOCCSUNICODE
1440 fgets((char *)lBuff, 2*MAX_PATH_OR_CMD, lIn);
1441 #else
1442 fgetws(lBuff, MAX_PATH_OR_CMD, lIn);
1443 #endif
1444 fclose(lIn);
1445 wipefileW(lDialogString);
1446 _wremove(lDialogString);
1447
1448 if (aDefaultInput)
1449 {
1450 swprintf(lDialogString,
1451 #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
1452 lDialogStringLen,
1453 #endif
1454 L"%ls\\tinyfd.vbs", _wgetenv(L"TEMP"));
1455 }
1456 else
1457 {
1458 swprintf(lDialogString,
1459 #if !defined(__BORLANDC__) && !defined(__TINYC__) && !(defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
1460 lDialogStringLen,
1461 #endif
1462 L"%ls\\tinyfd.hta", _wgetenv(L"TEMP"));
1463 }
1464 _wremove(lDialogString);
1465 free(lDialogString);
1466 /* wprintf( L"lBuff: %ls\n" , lBuff ) ; */
1467 #ifdef TINYFD_NOCCSUNICODE
1468 lResult = !wcsncmp(lBuff+1, L"1", 1);
1469 #else
1470 lResult = !wcsncmp(lBuff, L"1", 1);
1471 #endif
1472
1473 /* printf( "lResult: %d \n" , lResult ) ; */
1474 if (!lResult)
1475 {
1476 return NULL ;
1477 }
1478
1479 /* wprintf( "lBuff+1: %ls\n" , lBuff+1 ) ; */
1480
1481 #ifdef TINYFD_NOCCSUNICODE
1482 if (aDefaultInput)
1483 {
1484 lDialogStringLen = wcslen(lBuff) ;
1485 lBuff[lDialogStringLen - 1] = L'\0';
1486 lBuff[lDialogStringLen - 2] = L'\0';
1487 }
1488 return lBuff + 2;
1489 #else
1490 if (aDefaultInput) lBuff[wcslen(lBuff) - 1] = L'\0';
1491 return lBuff + 1;
1492 #endif
1493 }
1494
1495
1496 wchar_t * tinyfd_saveFileDialogW(
1497 wchar_t const * aTitle, /* NULL or "" */
1498 wchar_t const * aDefaultPathAndFile, /* NULL or "" */
1499 int aNumOfFilterPatterns, /* 0 */
1500 wchar_t const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
1501 wchar_t const * aSingleFilterDescription) /* NULL or "image files" */
1502 {
1503 static wchar_t lBuff[MAX_PATH_OR_CMD];
1504 wchar_t lDirname[MAX_PATH_OR_CMD];
1505 wchar_t lDialogString[MAX_PATH_OR_CMD];
1506 wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L"";
1507 wchar_t * p;
1508 wchar_t * lRetval;
1509 wchar_t const * ldefExt = NULL;
1510 int i;
1511 HRESULT lHResult;
1512 OPENFILENAMEW ofn = {0};
1513
1514 if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; }
1515
1516 if (quoteDetectedW(aTitle)) return tinyfd_saveFileDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
1517 if (quoteDetectedW(aDefaultPathAndFile)) return tinyfd_saveFileDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
1518 if (quoteDetectedW(aSingleFilterDescription)) return tinyfd_saveFileDialogW(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, L"INVALID FILTER_DESCRIPTION WITH QUOTES");
1519 for (i = 0; i < aNumOfFilterPatterns; i++)
1520 {
1521 if (quoteDetectedW(aFilterPatterns[i])) return tinyfd_saveFileDialogW(L"INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL);
1522 }
1523
1524 lHResult = CoInitializeEx(NULL, 0);
1525
1526 getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile);
1527 getLastNameW(lBuff, aDefaultPathAndFile);
1528
1529 if (aNumOfFilterPatterns > 0)
1530 {
1531 ldefExt = aFilterPatterns[0];
1532
1533 if (aSingleFilterDescription && wcslen(aSingleFilterDescription))
1534 {
1535 wcscpy(lFilterPatterns, aSingleFilterDescription);
1536 wcscat(lFilterPatterns, L"\n");
1537 }
1538 wcscat(lFilterPatterns, aFilterPatterns[0]);
1539 for (i = 1; i < aNumOfFilterPatterns; i++)
1540 {
1541 wcscat(lFilterPatterns, L";");
1542 wcscat(lFilterPatterns, aFilterPatterns[i]);
1543 }
1544 wcscat(lFilterPatterns, L"\n");
1545 if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription)))
1546 {
1547 wcscpy(lDialogString, lFilterPatterns);
1548 wcscat(lFilterPatterns, lDialogString);
1549 }
1550 wcscat(lFilterPatterns, L"All Files\n*.*\n");
1551 p = lFilterPatterns;
1552 while ((p = wcschr(p, L'\n')) != NULL)
1553 {
1554 *p = L'\0';
1555 p++;
1556 }
1557 }
1558
1559 ofn.lStructSize = sizeof(OPENFILENAMEW);
1560 ofn.hwndOwner = GetForegroundWindow();
1561 ofn.hInstance = 0;
1562 ofn.lpstrFilter = wcslen(lFilterPatterns) ? lFilterPatterns : NULL;
1563 ofn.lpstrCustomFilter = NULL;
1564 ofn.nMaxCustFilter = 0;
1565 ofn.nFilterIndex = 1;
1566 ofn.lpstrFile = lBuff;
1567
1568 ofn.nMaxFile = MAX_PATH_OR_CMD;
1569 ofn.lpstrFileTitle = NULL;
1570 ofn.nMaxFileTitle = MAX_PATH_OR_CMD/2;
1571 ofn.lpstrInitialDir = wcslen(lDirname) ? lDirname : NULL;
1572 ofn.lpstrTitle = aTitle && wcslen(aTitle) ? aTitle : NULL;
1573 ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST ;
1574 ofn.nFileOffset = 0;
1575 ofn.nFileExtension = 0;
1576 ofn.lpstrDefExt = ldefExt;
1577 ofn.lCustData = 0L;
1578 ofn.lpfnHook = NULL;
1579 ofn.lpTemplateName = NULL;
1580
1581 if (GetSaveFileNameW(&ofn) == 0)
1582 {
1583 lRetval = NULL;
1584 }
1585 else
1586 {
1587 lRetval = lBuff;
1588 }
1589
1590 if (lHResult == S_OK || lHResult == S_FALSE)
1591 {
1592 CoUninitialize();
1593 }
1594 return lRetval;
1595 }
1596
1597
1598 wchar_t * tinyfd_openFileDialogW(
1599 wchar_t const * aTitle, /* NULL or "" */
1600 wchar_t const * aDefaultPathAndFile, /* NULL or "" */
1601 int aNumOfFilterPatterns, /* 0 */
1602 wchar_t const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
1603 wchar_t const * aSingleFilterDescription, /* NULL or "image files" */
1604 int aAllowMultipleSelects) /* 0 or 1 ; -1 to free allocated memory and return */
1605 {
1606 size_t lLengths[MAX_MULTIPLE_FILES];
1607 wchar_t lDirname[MAX_PATH_OR_CMD];
1608 wchar_t lFilterPatterns[MAX_PATH_OR_CMD] = L"";
1609 wchar_t lDialogString[MAX_PATH_OR_CMD];
1610 wchar_t * lPointers[MAX_MULTIPLE_FILES+1];
1611 wchar_t * p;
1612 int i, j;
1613 size_t lBuffLen;
1614 DWORD lFullBuffLen;
1615 HRESULT lHResult;
1616 OPENFILENAMEW ofn = { 0 };
1617 static wchar_t * lBuff = NULL;
1618
1619 free(lBuff);
1620 lBuff = NULL;
1621 if (aAllowMultipleSelects < 0) return (wchar_t *)0;
1622
1623 if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; }
1624
1625 if (quoteDetectedW(aTitle)) return tinyfd_openFileDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
1626 if (quoteDetectedW(aDefaultPathAndFile)) return tinyfd_openFileDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
1627 if (quoteDetectedW(aSingleFilterDescription)) return tinyfd_openFileDialogW(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, L"INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects);
1628 for (i = 0; i < aNumOfFilterPatterns; i++)
1629 {
1630 if (quoteDetectedW(aFilterPatterns[i])) return tinyfd_openFileDialogW(L"INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects);
1631 }
1632
1633 if (aAllowMultipleSelects)
1634 {
1635 lFullBuffLen = MAX_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1;
1636 lBuff = (wchar_t*)(malloc(lFullBuffLen * sizeof(wchar_t)));
1637 if (!lBuff)
1638 {
1639 lFullBuffLen = LOW_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1;
1640 lBuff = (wchar_t*)( malloc( lFullBuffLen * sizeof(wchar_t)));
1641 }
1642 }
1643 else
1644 {
1645 lFullBuffLen = MAX_PATH_OR_CMD + 1;
1646 lBuff = (wchar_t*)(malloc(lFullBuffLen * sizeof(wchar_t)));
1647 }
1648 if (!lBuff) return NULL;
1649
1650 lHResult = CoInitializeEx(NULL, 0);
1651
1652 getPathWithoutFinalSlashW(lDirname, aDefaultPathAndFile);
1653 getLastNameW(lBuff, aDefaultPathAndFile);
1654
1655 if (aNumOfFilterPatterns > 0)
1656 {
1657 if (aSingleFilterDescription && wcslen(aSingleFilterDescription))
1658 {
1659 wcscpy(lFilterPatterns, aSingleFilterDescription);
1660 wcscat(lFilterPatterns, L"\n");
1661 }
1662 wcscat(lFilterPatterns, aFilterPatterns[0]);
1663 for (i = 1; i < aNumOfFilterPatterns; i++)
1664 {
1665 wcscat(lFilterPatterns, L";");
1666 wcscat(lFilterPatterns, aFilterPatterns[i]);
1667 }
1668 wcscat(lFilterPatterns, L"\n");
1669 if (!(aSingleFilterDescription && wcslen(aSingleFilterDescription)))
1670 {
1671 wcscpy(lDialogString, lFilterPatterns);
1672 wcscat(lFilterPatterns, lDialogString);
1673 }
1674 wcscat(lFilterPatterns, L"All Files\n*.*\n");
1675 p = lFilterPatterns;
1676 while ((p = wcschr(p, L'\n')) != NULL)
1677 {
1678 *p = L'\0';
1679 p++;
1680 }
1681 }
1682
1683 ofn.lStructSize = sizeof(OPENFILENAME);
1684 ofn.hwndOwner = GetForegroundWindow();
1685 ofn.hInstance = 0;
1686 ofn.lpstrFilter = wcslen(lFilterPatterns) ? lFilterPatterns : NULL;
1687 ofn.lpstrCustomFilter = NULL;
1688 ofn.nMaxCustFilter = 0;
1689 ofn.nFilterIndex = 1;
1690 ofn.lpstrFile = lBuff;
1691 ofn.nMaxFile = lFullBuffLen;
1692 ofn.lpstrFileTitle = NULL;
1693 ofn.nMaxFileTitle = MAX_PATH_OR_CMD / 2;
1694 ofn.lpstrInitialDir = wcslen(lDirname) ? lDirname : NULL;
1695 ofn.lpstrTitle = aTitle && wcslen(aTitle) ? aTitle : NULL;
1696 ofn.Flags = OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
1697 ofn.nFileOffset = 0;
1698 ofn.nFileExtension = 0;
1699 ofn.lpstrDefExt = NULL;
1700 ofn.lCustData = 0L;
1701 ofn.lpfnHook = NULL;
1702 ofn.lpTemplateName = NULL;
1703
1704 if (aAllowMultipleSelects)
1705 {
1706 ofn.Flags |= OFN_ALLOWMULTISELECT;
1707 }
1708
1709 if (GetOpenFileNameW(&ofn) == 0)
1710 {
1711 free(lBuff);
1712 lBuff = NULL;
1713 }
1714 else
1715 {
1716 lBuffLen = wcslen(lBuff);
1717 lPointers[0] = lBuff + lBuffLen + 1;
1718 if (aAllowMultipleSelects && (lPointers[0][0] != L'\0'))
1719 {
1720 i = 0;
1721 do
1722 {
1723 lLengths[i] = wcslen(lPointers[i]);
1724 lPointers[i + 1] = lPointers[i] + lLengths[i] + 1;
1725 i++;
1726 } while (lPointers[i][0] != L'\0' && i < MAX_MULTIPLE_FILES );
1727 if (i > MAX_MULTIPLE_FILES)
1728 {
1729 free(lBuff);
1730 lBuff = NULL;
1731 }
1732 else
1733 {
1734 i--;
1735 p = lBuff + lFullBuffLen - 1;
1736 *p = L'\0';
1737 for (j = i; j >= 0; j--)
1738 {
1739 p -= lLengths[j];
1740 memmove(p, lPointers[j], lLengths[j] * sizeof(wchar_t));
1741 p--;
1742 *p = L'\\';
1743 p -= lBuffLen;
1744 memmove(p, lBuff, lBuffLen*sizeof(wchar_t));
1745 p--;
1746 *p = L'|';
1747 }
1748 p++;
1749 wcscpy(lBuff, p);
1750 lBuffLen = wcslen(lBuff);
1751 }
1752 }
1753 if (lBuff) lBuff = (wchar_t*)(realloc(lBuff, (lBuffLen + 1) * sizeof(wchar_t)));
1754 }
1755
1756 if (lHResult == S_OK || lHResult == S_FALSE)
1757 {
1758 CoUninitialize();
1759 }
1760 return lBuff;
1761 }
1762
1763
1764 BOOL CALLBACK BrowseCallbackProcW_enum(HWND hWndChild, LPARAM lParam)
1765 {
1766 wchar_t buf[255];
1767 GetClassNameW(hWndChild, buf, sizeof(buf));
1768 if (wcscmp(buf, L"SysTreeView32") == 0) {
1769 HTREEITEM hNode = TreeView_GetSelection(hWndChild);
1770 TreeView_EnsureVisible(hWndChild, hNode);
1771 return FALSE;
1772 }
1773 return TRUE;
1774 }
1775
1776
1777 static int __stdcall BrowseCallbackProcW(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
1778 {
1779 switch (uMsg) {
1780 case BFFM_INITIALIZED:
1781 SendMessage(hwnd, BFFM_SETSELECTIONW, TRUE, (LPARAM)pData);
1782 break;
1783 case BFFM_SELCHANGED:
1784 EnumChildWindows(hwnd, BrowseCallbackProcW_enum, 0);
1785 }
1786 return 0;
1787 }
1788
1789 wchar_t * tinyfd_selectFolderDialogW(
1790 wchar_t const * aTitle, /* NULL or "" */
1791 wchar_t const * aDefaultPath) /* NULL or "" */
1792 {
1793 static wchar_t lBuff[MAX_PATH_OR_CMD];
1794 wchar_t * lRetval;
1795
1796 BROWSEINFOW bInfo;
1797 LPITEMIDLIST lpItem;
1798 HRESULT lHResult;
1799
1800 if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; }
1801
1802 if (quoteDetectedW(aTitle)) return tinyfd_selectFolderDialogW(L"INVALID TITLE WITH QUOTES", aDefaultPath);
1803 if (quoteDetectedW(aDefaultPath)) return tinyfd_selectFolderDialogW(aTitle, L"INVALID DEFAULT_PATH WITH QUOTES");
1804
1805 lHResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1806
1807 bInfo.hwndOwner = GetForegroundWindow();
1808 bInfo.pidlRoot = NULL;
1809 bInfo.pszDisplayName = lBuff;
1810 bInfo.lpszTitle = aTitle && wcslen(aTitle) ? aTitle : NULL;
1811 if (lHResult == S_OK || lHResult == S_FALSE)
1812 {
1813 bInfo.ulFlags = BIF_USENEWUI;
1814 }
1815 bInfo.lpfn = BrowseCallbackProcW;
1816 bInfo.lParam = (LPARAM)aDefaultPath;
1817 bInfo.iImage = -1;
1818
1819 lpItem = SHBrowseForFolderW(&bInfo);
1820 if (!lpItem)
1821 {
1822 lRetval = NULL;
1823 }
1824 else
1825 {
1826 SHGetPathFromIDListW(lpItem, lBuff);
1827 lRetval = lBuff ;
1828 }
1829
1830 if (lHResult == S_OK || lHResult == S_FALSE)
1831 {
1832 CoUninitialize();
1833 }
1834 return lRetval;
1835 }
1836
1837
1838 wchar_t * tinyfd_colorChooserW(
1839 wchar_t const * aTitle, /* NULL or "" */
1840 wchar_t const * aDefaultHexRGB, /* NULL or "#FF0000"*/
1841 unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */
1842 unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */
1843 {
1844 static wchar_t lResultHexRGB[8];
1845 CHOOSECOLORW cc;
1846 COLORREF crCustColors[16];
1847 unsigned char lDefaultRGB[3];
1848 int lRet;
1849
1850 HRESULT lHResult;
1851
1852 if (aTitle&&!wcscmp(aTitle, L"tinyfd_query")){ strcpy(tinyfd_response, "windows_wchar"); return (wchar_t *)1; }
1853
1854 if (quoteDetectedW(aTitle)) return tinyfd_colorChooserW(L"INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB);
1855 if (quoteDetectedW(aDefaultHexRGB)) return tinyfd_colorChooserW(aTitle, L"INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB);
1856
1857 lHResult = CoInitializeEx(NULL, 0);
1858
1859 if ( aDefaultHexRGB )
1860 {
1861 Hex2RGBW(aDefaultHexRGB, lDefaultRGB);
1862 }
1863 else
1864 {
1865 lDefaultRGB[0] = aDefaultRGB[0];
1866 lDefaultRGB[1] = aDefaultRGB[1];
1867 lDefaultRGB[2] = aDefaultRGB[2];
1868 }
1869
1870 /* we can't use aTitle */
1871 cc.lStructSize = sizeof(CHOOSECOLOR);
1872 cc.hwndOwner = GetForegroundWindow();
1873 cc.hInstance = NULL;
1874 cc.rgbResult = RGB(lDefaultRGB[0], lDefaultRGB[1], lDefaultRGB[2]);
1875 cc.lpCustColors = crCustColors;
1876 cc.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ANYCOLOR ;
1877 cc.lCustData = 0;
1878 cc.lpfnHook = NULL;
1879 cc.lpTemplateName = NULL;
1880
1881 lRet = ChooseColorW(&cc);
1882
1883 if (!lRet)
1884 {
1885 return NULL;
1886 }
1887
1888 aoResultRGB[0] = GetRValue(cc.rgbResult);
1889 aoResultRGB[1] = GetGValue(cc.rgbResult);
1890 aoResultRGB[2] = GetBValue(cc.rgbResult);
1891
1892 RGB2HexW(aoResultRGB, lResultHexRGB);
1893
1894 if (lHResult == S_OK || lHResult == S_FALSE)
1895 {
1896 CoUninitialize();
1897 }
1898
1899 return lResultHexRGB;
1900 }
1901
1902
1903 static int messageBoxWinGui(
1904 char const * aTitle, /* NULL or "" */
1905 char const * aMessage, /* NULL or "" may contain \n and \t */
1906 char const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */
1907 char const * aIconType, /* "info" "warning" "error" "question" */
1908 int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
1909 {
1910 int lIntRetVal;
1911 wchar_t lTitle[128] = L"";
1912 wchar_t * lMessage = NULL;
1913 wchar_t lDialogType[16] = L"";
1914 wchar_t lIconType[16] = L"";
1915 wchar_t * lTmpWChar;
1916
1917 if (aTitle)
1918 {
1919 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
1920 else lTmpWChar = tinyfd_mbcsTo16(aTitle);
1921 wcscpy(lTitle, lTmpWChar);
1922 }
1923 if (aMessage)
1924 {
1925 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage);
1926 else lTmpWChar = tinyfd_mbcsTo16(aMessage);
1927 lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t));
1928 if (lMessage) wcscpy(lMessage, lTmpWChar);
1929 }
1930 if (aDialogType)
1931 {
1932 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDialogType);
1933 else lTmpWChar = tinyfd_mbcsTo16(aDialogType);
1934 wcscpy(lDialogType, lTmpWChar);
1935 }
1936 if (aIconType)
1937 {
1938 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aIconType);
1939 else lTmpWChar = tinyfd_mbcsTo16(aIconType);
1940 wcscpy(lIconType, lTmpWChar);
1941 }
1942
1943 lIntRetVal = tinyfd_messageBoxW(lTitle, lMessage, lDialogType, lIconType, aDefaultButton);
1944
1945 free(lMessage);
1946
1947 return lIntRetVal;
1948 }
1949
1950
1951 static int notifyWinGui(
1952 char const * aTitle, /* NULL or "" */
1953 char const * aMessage, /* NULL or "" may NOT contain \n nor \t */
1954 char const * aIconType)
1955 {
1956 wchar_t lTitle[128] = L"";
1957 wchar_t * lMessage = NULL;
1958 wchar_t lIconType[16] = L"";
1959 wchar_t * lTmpWChar;
1960
1961 if (aTitle)
1962 {
1963 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
1964 else lTmpWChar = tinyfd_mbcsTo16(aTitle);
1965 wcscpy(lTitle, lTmpWChar);
1966 }
1967 if (aMessage)
1968 {
1969 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage);
1970 else lTmpWChar = tinyfd_mbcsTo16(aMessage);
1971 lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t));
1972 if (lMessage) wcscpy(lMessage, lTmpWChar);
1973 }
1974 if (aIconType)
1975 {
1976 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aIconType);
1977 else lTmpWChar = tinyfd_mbcsTo16(aIconType);
1978 wcscpy(lIconType, lTmpWChar);
1979 }
1980
1981 tinyfd_notifyPopupW(lTitle, lMessage, lIconType);
1982
1983 free(lMessage);
1984
1985 return 1;
1986 }
1987
1988
1989 static int inputBoxWinGui(
1990 char * aoBuff,
1991 char const * aTitle, /* NULL or "" */
1992 char const * aMessage, /* NULL or "" may NOT contain \n nor \t */
1993 char const * aDefaultInput) /* "" , if NULL it's a passwordBox */
1994 {
1995 wchar_t lTitle[128] = L"";
1996 wchar_t * lMessage = NULL;
1997 wchar_t lDefaultInput[MAX_PATH_OR_CMD] = L"";
1998 wchar_t * lTmpWChar;
1999 char * lTmpChar;
2000
2001 if (aTitle)
2002 {
2003 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
2004 else lTmpWChar = tinyfd_mbcsTo16(aTitle);
2005 wcscpy(lTitle, lTmpWChar);
2006 }
2007 if (aMessage)
2008 {
2009 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aMessage);
2010 else lTmpWChar = tinyfd_mbcsTo16(aMessage);
2011 lMessage = (wchar_t *) malloc((wcslen(lTmpWChar) + 1)* sizeof(wchar_t));
2012 if (lMessage) wcscpy(lMessage, lTmpWChar);
2013 }
2014 if (aDefaultInput)
2015 {
2016 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultInput);
2017 else lTmpWChar = tinyfd_mbcsTo16(aDefaultInput);
2018 wcscpy(lDefaultInput, lTmpWChar);
2019 lTmpWChar = tinyfd_inputBoxW(lTitle, lMessage, lDefaultInput);
2020 }
2021 else lTmpWChar = tinyfd_inputBoxW(lTitle, lMessage, NULL);
2022
2023 free(lMessage);
2024
2025 if (!lTmpWChar)
2026 {
2027 aoBuff[0] = '\0';
2028 return 0;
2029 }
2030
2031 if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar);
2032 else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar);
2033
2034 strcpy(aoBuff, lTmpChar);
2035
2036 return 1;
2037 }
2038
2039
2040 static char * saveFileDialogWinGui(
2041 char * aoBuff,
2042 char const * aTitle, /* NULL or "" */
2043 char const * aDefaultPathAndFile, /* NULL or "" */
2044 int aNumOfFilterPatterns, /* 0 */
2045 char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
2046 char const * aSingleFilterDescription) /* NULL or "image files" */
2047 {
2048 wchar_t lTitle[128] = L"";
2049 wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD] = L"";
2050 wchar_t lSingleFilterDescription[128] = L"";
2051 wchar_t * * lFilterPatterns;
2052 wchar_t * lTmpWChar;
2053 char * lTmpChar;
2054 int i;
2055
2056 lFilterPatterns = (wchar_t **)malloc(aNumOfFilterPatterns*sizeof(wchar_t *));
2057 for (i = 0; i < aNumOfFilterPatterns; i++)
2058 {
2059 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]);
2060 else lTmpWChar = tinyfd_mbcsTo16(aFilterPatterns[i]);
2061 lFilterPatterns[i] = (wchar_t *)malloc((wcslen(lTmpWChar) + 1) * sizeof(wchar_t *));
2062 if (lFilterPatterns[i]) wcscpy(lFilterPatterns[i], lTmpWChar);
2063 }
2064
2065 if (aTitle)
2066 {
2067 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
2068 else lTmpWChar = tinyfd_mbcsTo16(aTitle);
2069 wcscpy(lTitle, lTmpWChar);
2070 }
2071 if (aDefaultPathAndFile)
2072 {
2073 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile);
2074 else lTmpWChar = tinyfd_mbcsTo16(aDefaultPathAndFile);
2075 wcscpy(lDefaultPathAndFile, lTmpWChar);
2076 }
2077 if (aSingleFilterDescription)
2078 {
2079 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription);
2080 else lTmpWChar = tinyfd_mbcsTo16(aSingleFilterDescription);
2081 wcscpy(lSingleFilterDescription, lTmpWChar);
2082 }
2083
2084 lTmpWChar = tinyfd_saveFileDialogW(
2085 lTitle,
2086 lDefaultPathAndFile,
2087 aNumOfFilterPatterns,
2088 (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/
2089 lSingleFilterDescription);
2090
2091 for (i = 0; i < aNumOfFilterPatterns; i++)
2092 {
2093 free(lFilterPatterns[i]);
2094 }
2095 free(lFilterPatterns);
2096
2097 if (!lTmpWChar)
2098 {
2099 return NULL;
2100 }
2101
2102 if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar);
2103 else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar);
2104 strcpy(aoBuff, lTmpChar);
2105 if (tinyfd_winUtf8) (void)tinyfd_utf16to8(NULL);
2106 else (void)tinyfd_utf16toMbcs(NULL);
2107
2108 return aoBuff;
2109 }
2110
2111
2112 static char * openFileDialogWinGui(
2113 char const * aTitle, /* NULL or "" */
2114 char const * aDefaultPathAndFile, /* NULL or "" */
2115 int aNumOfFilterPatterns, /* 0 */
2116 char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
2117 char const * aSingleFilterDescription, /* NULL or "image files" */
2118 int aAllowMultipleSelects) /* 0 or 1 */
2119 {
2120 wchar_t lTitle[128] = L"";
2121 wchar_t lDefaultPathAndFile[MAX_PATH_OR_CMD] = L"";
2122 wchar_t lSingleFilterDescription[128] = L"";
2123 wchar_t * * lFilterPatterns;
2124 wchar_t * lTmpWChar;
2125 char * lTmpChar;
2126 int i;
2127
2128 lFilterPatterns = (wchar_t * *)malloc(aNumOfFilterPatterns*sizeof(wchar_t *));
2129 for (i = 0; i < aNumOfFilterPatterns; i++)
2130 {
2131 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aFilterPatterns[i]);
2132 else lTmpWChar = tinyfd_mbcsTo16(aFilterPatterns[i]);
2133 lFilterPatterns[i] = (wchar_t *)malloc((wcslen(lTmpWChar) + 1)*sizeof(wchar_t *));
2134 if (lFilterPatterns[i]) wcscpy(lFilterPatterns[i], lTmpWChar);
2135 }
2136
2137 if (aTitle)
2138 {
2139 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
2140 else lTmpWChar = tinyfd_mbcsTo16(aTitle);
2141 wcscpy(lTitle, lTmpWChar);
2142 }
2143 if (aDefaultPathAndFile)
2144 {
2145 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPathAndFile);
2146 else lTmpWChar = tinyfd_mbcsTo16(aDefaultPathAndFile);
2147 wcscpy(lDefaultPathAndFile, lTmpWChar);
2148 }
2149 if (aSingleFilterDescription)
2150 {
2151 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aSingleFilterDescription);
2152 else lTmpWChar = tinyfd_mbcsTo16(aSingleFilterDescription);
2153 wcscpy(lSingleFilterDescription, lTmpWChar);
2154 }
2155
2156 lTmpWChar = tinyfd_openFileDialogW(
2157 lTitle,
2158 lDefaultPathAndFile,
2159 aNumOfFilterPatterns,
2160 (wchar_t const**) lFilterPatterns, /*stupid cast for gcc*/
2161 lSingleFilterDescription,
2162 aAllowMultipleSelects);
2163
2164 for (i = 0; i < aNumOfFilterPatterns; i++)
2165 {
2166 free(lFilterPatterns[i]);
2167 }
2168 free(lFilterPatterns);
2169
2170 if (!lTmpWChar) return NULL;
2171
2172 if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar);
2173 else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar);
2174 (void)tinyfd_openFileDialogW(NULL, NULL, 0, NULL, NULL, -1);
2175
2176 return lTmpChar;
2177 }
2178
2179
2180 static char * selectFolderDialogWinGui(
2181 char * aoBuff,
2182 char const * aTitle, /* NULL or "" */
2183 char const * aDefaultPath) /* NULL or "" */
2184 {
2185 wchar_t lTitle[128] = L"";
2186 wchar_t lDefaultPath[MAX_PATH_OR_CMD] = L"";
2187 wchar_t * lTmpWChar;
2188 char * lTmpChar;
2189
2190 if (aTitle)
2191 {
2192 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
2193 else lTmpWChar = tinyfd_mbcsTo16(aTitle);
2194 wcscpy(lTitle, lTmpWChar);
2195 }
2196 if (aDefaultPath)
2197 {
2198 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultPath);
2199 else lTmpWChar = tinyfd_mbcsTo16(aDefaultPath);
2200 wcscpy(lDefaultPath, lTmpWChar);
2201 }
2202
2203 lTmpWChar = tinyfd_selectFolderDialogW(
2204 lTitle,
2205 lDefaultPath);
2206
2207 if (!lTmpWChar)
2208 {
2209 return NULL;
2210 }
2211
2212 if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar);
2213 else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar);
2214 strcpy(aoBuff, lTmpChar);
2215
2216 return aoBuff;
2217 }
2218
2219
2220 static char * colorChooserWinGui(
2221 char const * aTitle, /* NULL or "" */
2222 char const * aDefaultHexRGB, /* NULL or "#FF0000"*/
2223 unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */
2224 unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */
2225 {
2226 static char lResultHexRGB[8];
2227
2228 wchar_t lTitle[128];
2229 wchar_t lDefaultHexRGB[16];
2230 wchar_t * lTmpWChar;
2231 char * lTmpChar;
2232
2233 if (aTitle)
2234 {
2235 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aTitle);
2236 else lTmpWChar = tinyfd_mbcsTo16(aTitle);
2237 wcscpy(lTitle, lTmpWChar);
2238 }
2239 if (aDefaultHexRGB)
2240 {
2241 if (tinyfd_winUtf8) lTmpWChar = tinyfd_utf8to16(aDefaultHexRGB);
2242 else lTmpWChar = tinyfd_mbcsTo16(aDefaultHexRGB);
2243 wcscpy(lDefaultHexRGB, lTmpWChar);
2244 }
2245
2246 lTmpWChar = tinyfd_colorChooserW(
2247 lTitle,
2248 lDefaultHexRGB,
2249 aDefaultRGB,
2250 aoResultRGB );
2251
2252 if (!lTmpWChar)
2253 {
2254 return NULL;
2255 }
2256
2257 if (tinyfd_winUtf8) lTmpChar = tinyfd_utf16to8(lTmpWChar);
2258 else lTmpChar = tinyfd_utf16toMbcs(lTmpWChar);
2259 strcpy(lResultHexRGB, lTmpChar);
2260
2261 return lResultHexRGB;
2262 }
2263
2264
2265 static int dialogPresent(void)
2266 {
2267 static int lDialogPresent = -1 ;
2268 char lBuff[MAX_PATH_OR_CMD] ;
2269 FILE * lIn ;
2270 char const * lString = "dialog.exe";
2271 if (!tinyfd_allowCursesDialogs) return 0;
2272 if (lDialogPresent < 0)
2273 {
2274 if (!(lIn = _popen("where dialog.exe","r")))
2275 {
2276 lDialogPresent = 0 ;
2277 return 0 ;
2278 }
2279 while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
2280 {}
2281 _pclose( lIn ) ;
2282 if ( lBuff[strlen( lBuff ) -1] == '\n' )
2283 {
2284 lBuff[strlen( lBuff ) -1] = '\0' ;
2285 }
2286 if ( strcmp(lBuff+strlen(lBuff)-strlen(lString),lString) )
2287 {
2288 lDialogPresent = 0 ;
2289 }
2290 else
2291 {
2292 lDialogPresent = 1 ;
2293 }
2294 }
2295 return lDialogPresent;
2296 }
2297
2298
2299 static int messageBoxWinConsole(
2300 char const * aTitle , /* NULL or "" */
2301 char const * aMessage , /* NULL or "" may contain \n and \t */
2302 char const * aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */
2303 char const * aIconType , /* "info" "warning" "error" "question" */
2304 int aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
2305 {
2306 char lDialogString[MAX_PATH_OR_CMD];
2307 char lDialogFile[MAX_PATH_OR_CMD];
2308 FILE * lIn;
2309 char lBuff[MAX_PATH_OR_CMD] = "";
2310
2311 strcpy(lDialogString, "dialog ");
2312 if (aTitle && strlen(aTitle))
2313 {
2314 strcat(lDialogString, "--title \"") ;
2315 strcat(lDialogString, aTitle) ;
2316 strcat(lDialogString, "\" ") ;
2317 }
2318
2319 if ( aDialogType && ( !strcmp( "okcancel" , aDialogType )
2320 || !strcmp("yesno", aDialogType) || !strcmp("yesnocancel", aDialogType) ) )
2321 {
2322 strcat(lDialogString, "--backtitle \"") ;
2323 strcat(lDialogString, "tab: move focus") ;
2324 strcat(lDialogString, "\" ") ;
2325 }
2326
2327 if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
2328 {
2329 if ( ! aDefaultButton )
2330 {
2331 strcat( lDialogString , "--defaultno " ) ;
2332 }
2333 strcat( lDialogString ,
2334 "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ;
2335 }
2336 else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
2337 {
2338 if ( ! aDefaultButton )
2339 {
2340 strcat( lDialogString , "--defaultno " ) ;
2341 }
2342 strcat( lDialogString , "--yesno " ) ;
2343 }
2344 else if (aDialogType && !strcmp("yesnocancel", aDialogType))
2345 {
2346 if (!aDefaultButton)
2347 {
2348 strcat(lDialogString, "--defaultno ");
2349 }
2350 strcat(lDialogString, "--menu ");
2351 }
2352 else
2353 {
2354 strcat( lDialogString , "--msgbox " ) ;
2355 }
2356
2357 strcat( lDialogString , "\"" ) ;
2358 if ( aMessage && strlen(aMessage) )
2359 {
2360 tfd_replaceSubStr( aMessage , "\n" , "\\n" , lBuff ) ;
2361 strcat(lDialogString, lBuff) ;
2362 lBuff[0]='\0';
2363 }
2364 strcat(lDialogString, "\" ");
2365
2366 if (aDialogType && !strcmp("yesnocancel", aDialogType))
2367 {
2368 strcat(lDialogString, "0 60 0 Yes \"\" No \"\"");
2369 strcat(lDialogString, "2>>");
2370 }
2371 else
2372 {
2373 strcat(lDialogString, "10 60");
2374 strcat(lDialogString, " && echo 1 > ");
2375 }
2376
2377 strcpy(lDialogFile, getenv("TEMP"));
2378 strcat(lDialogFile, "\\tinyfd.txt");
2379 strcat(lDialogString, lDialogFile);
2380
2381 /*if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;*/
2382 system( lDialogString ) ;
2383
2384 if (!(lIn = fopen(lDialogFile, "r")))
2385 {
2386 remove(lDialogFile);
2387 return 0 ;
2388 }
2389 while (fgets(lBuff, sizeof(lBuff), lIn) != NULL)
2390 {}
2391 fclose(lIn);
2392 remove(lDialogFile);
2393 if ( lBuff[strlen( lBuff ) -1] == '\n' )
2394 {
2395 lBuff[strlen( lBuff ) -1] = '\0' ;
2396 }
2397
2398 /* if (tinyfd_verbose) printf("lBuff: %s\n", lBuff); */
2399 if ( ! strlen(lBuff) )
2400 {
2401 return 0;
2402 }
2403
2404 if (aDialogType && !strcmp("yesnocancel", aDialogType))
2405 {
2406 if (lBuff[0] == 'Y') return 1;
2407 else return 2;
2408 }
2409
2410 return 1;
2411 }
2412
2413
2414 static int inputBoxWinConsole(
2415 char * aoBuff ,
2416 char const * aTitle , /* NULL or "" */
2417 char const * aMessage , /* NULL or "" may NOT contain \n nor \t */
2418 char const * aDefaultInput ) /* "" , if NULL it's a passwordBox */
2419 {
2420 char lDialogString[MAX_PATH_OR_CMD];
2421 char lDialogFile[MAX_PATH_OR_CMD];
2422 FILE * lIn;
2423 int lResult;
2424
2425 strcpy(lDialogFile, getenv("TEMP"));
2426 strcat(lDialogFile, "\\tinyfd.txt");
2427 strcpy(lDialogString , "echo|set /p=1 >" ) ;
2428 strcat(lDialogString, lDialogFile);
2429 strcat( lDialogString , " & " ) ;
2430
2431 strcat( lDialogString , "dialog " ) ;
2432 if ( aTitle && strlen(aTitle) )
2433 {
2434 strcat(lDialogString, "--title \"") ;
2435 strcat(lDialogString, aTitle) ;
2436 strcat(lDialogString, "\" ") ;
2437 }
2438
2439 strcat(lDialogString, "--backtitle \"") ;
2440 strcat(lDialogString, "tab: move focus") ;
2441 if ( ! aDefaultInput )
2442 {
2443 strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ;
2444 }
2445
2446 strcat(lDialogString, "\" ") ;
2447
2448 if ( ! aDefaultInput )
2449 {
2450 strcat( lDialogString , "--insecure --passwordbox" ) ;
2451 }
2452 else
2453 {
2454 strcat( lDialogString , "--inputbox" ) ;
2455 }
2456 strcat( lDialogString , " \"" ) ;
2457 if ( aMessage && strlen(aMessage) )
2458 {
2459 strcat(lDialogString, aMessage) ;
2460 }
2461 strcat(lDialogString,"\" 10 60 ") ;
2462 if ( aDefaultInput && strlen(aDefaultInput) )
2463 {
2464 strcat(lDialogString, "\"") ;
2465 strcat(lDialogString, aDefaultInput) ;
2466 strcat(lDialogString, "\" ") ;
2467 }
2468
2469 strcat(lDialogString, "2>>");
2470 strcpy(lDialogFile, getenv("TEMP"));
2471 strcat(lDialogFile, "\\tinyfd.txt");
2472 strcat(lDialogString, lDialogFile);
2473 strcat(lDialogString, " || echo 0 > ");
2474 strcat(lDialogString, lDialogFile);
2475
2476 /* printf( "lDialogString: %s\n" , lDialogString ) ; */
2477 system( lDialogString ) ;
2478
2479 if (!(lIn = fopen(lDialogFile, "r")))
2480 {
2481 remove(lDialogFile);
2482 aoBuff[0] = '\0';
2483 return 0;
2484 }
2485 while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
2486 {}
2487 fclose(lIn);
2488
2489 wipefile(lDialogFile);
2490 remove(lDialogFile);
2491 if ( aoBuff[strlen( aoBuff ) -1] == '\n' )
2492 {
2493 aoBuff[strlen( aoBuff ) -1] = '\0' ;
2494 }
2495 /* printf( "aoBuff: %s\n" , aoBuff ) ; */
2496
2497 /* printf( "aoBuff: %s len: %lu \n" , aoBuff , strlen(aoBuff) ) ; */
2498 lResult = strncmp( aoBuff , "1" , 1) ? 0 : 1 ;
2499 /* printf( "lResult: %d \n" , lResult ) ; */
2500 if ( ! lResult )
2501 {
2502 aoBuff[0] = '\0';
2503 return 0 ;
2504 }
2505 /* printf( "aoBuff+1: %s\n" , aoBuff+1 ) ; */
2506 strcpy(aoBuff, aoBuff+3);
2507 return 1;
2508 }
2509
2510
2511 static char * saveFileDialogWinConsole(
2512 char * aoBuff ,
2513 char const * aTitle , /* NULL or "" */
2514 char const * aDefaultPathAndFile ) /* NULL or "" */
2515 {
2516 char lDialogString[MAX_PATH_OR_CMD];
2517 char lPathAndFile[MAX_PATH_OR_CMD] = "";
2518 FILE * lIn;
2519
2520 strcpy( lDialogString , "dialog " ) ;
2521 if ( aTitle && strlen(aTitle) )
2522 {
2523 strcat(lDialogString, "--title \"") ;
2524 strcat(lDialogString, aTitle) ;
2525 strcat(lDialogString, "\" ") ;
2526 }
2527
2528 strcat(lDialogString, "--backtitle \"") ;
2529 strcat(lDialogString,
2530 "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
2531 strcat(lDialogString, "\" ") ;
2532
2533 strcat( lDialogString , "--fselect \"" ) ;
2534 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
2535 {
2536 /* dialog.exe uses unix separators even on windows */
2537 strcpy(lPathAndFile, aDefaultPathAndFile);
2538 replaceChr( lPathAndFile , '\\' , '/' ) ;
2539 }
2540
2541 /* dialog.exe needs at least one separator */
2542 if ( ! strchr(lPathAndFile, '/') )
2543 {
2544 strcat(lDialogString, "./") ;
2545 }
2546 strcat(lDialogString, lPathAndFile) ;
2547 strcat(lDialogString, "\" 0 60 2>");
2548 strcpy(lPathAndFile, getenv("TEMP"));
2549 strcat(lPathAndFile, "\\tinyfd.txt");
2550 strcat(lDialogString, lPathAndFile);
2551
2552 /* printf( "lDialogString: %s\n" , lDialogString ) ; */
2553 system( lDialogString ) ;
2554
2555 if (!(lIn = fopen(lPathAndFile, "r")))
2556 {
2557 remove(lPathAndFile);
2558 return NULL;
2559 }
2560 while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
2561 {}
2562 fclose(lIn);
2563 remove(lPathAndFile);
2564 replaceChr( aoBuff , '/' , '\\' ) ;
2565 /* printf( "aoBuff: %s\n" , aoBuff ) ; */
2566 getLastName(lDialogString,aoBuff);
2567 if ( ! strlen(lDialogString) )
2568 {
2569 return NULL;
2570 }
2571 return aoBuff;
2572 }
2573
2574
2575 static char * openFileDialogWinConsole(
2576 char const * aTitle , /* NULL or "" */
2577 char const * aDefaultPathAndFile ) /* NULL or "" */
2578 {
2579 char lFilterPatterns[MAX_PATH_OR_CMD] = "";
2580 char lDialogString[MAX_PATH_OR_CMD] ;
2581 FILE * lIn;
2582
2583 static char aoBuff[MAX_PATH_OR_CMD];
2584
2585 strcpy( lDialogString , "dialog " ) ;
2586 if ( aTitle && strlen(aTitle) )
2587 {
2588 strcat(lDialogString, "--title \"") ;
2589 strcat(lDialogString, aTitle) ;
2590 strcat(lDialogString, "\" ") ;
2591 }
2592
2593 strcat(lDialogString, "--backtitle \"") ;
2594 strcat(lDialogString,
2595 "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
2596 strcat(lDialogString, "\" ") ;
2597
2598 strcat( lDialogString , "--fselect \"" ) ;
2599 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
2600 {
2601 /* dialog.exe uses unix separators even on windows */
2602 strcpy(lFilterPatterns, aDefaultPathAndFile);
2603 replaceChr( lFilterPatterns , '\\' , '/' ) ;
2604 }
2605
2606 /* dialog.exe needs at least one separator */
2607 if ( ! strchr(lFilterPatterns, '/') )
2608 {
2609 strcat(lDialogString, "./") ;
2610 }
2611 strcat(lDialogString, lFilterPatterns) ;
2612 strcat(lDialogString, "\" 0 60 2>");
2613 strcpy(lFilterPatterns, getenv("TEMP"));
2614 strcat(lFilterPatterns, "\\tinyfd.txt");
2615 strcat(lDialogString, lFilterPatterns);
2616
2617 /* printf( "lDialogString: %s\n" , lDialogString ) ; */
2618 system( lDialogString ) ;
2619
2620 if (!(lIn = fopen(lFilterPatterns, "r")))
2621 {
2622 remove(lFilterPatterns);
2623 return NULL;
2624 }
2625 while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
2626 {}
2627 fclose(lIn);
2628 remove(lFilterPatterns);
2629 replaceChr( aoBuff , '/' , '\\' ) ;
2630 /* printf( "aoBuff: %s\n" , aoBuff ) ; */
2631 return aoBuff;
2632 }
2633
2634
2635 static char * selectFolderDialogWinConsole(
2636 char * aoBuff ,
2637 char const * aTitle , /* NULL or "" */
2638 char const * aDefaultPath ) /* NULL or "" */
2639 {
2640 char lDialogString[MAX_PATH_OR_CMD] ;
2641 char lString[MAX_PATH_OR_CMD] ;
2642 FILE * lIn ;
2643
2644 strcpy( lDialogString , "dialog " ) ;
2645 if ( aTitle && strlen(aTitle) )
2646 {
2647 strcat(lDialogString, "--title \"") ;
2648 strcat(lDialogString, aTitle) ;
2649 strcat(lDialogString, "\" ") ;
2650 }
2651
2652 strcat(lDialogString, "--backtitle \"") ;
2653 strcat(lDialogString,
2654 "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
2655 strcat(lDialogString, "\" ") ;
2656
2657 strcat( lDialogString , "--dselect \"" ) ;
2658 if ( aDefaultPath && strlen(aDefaultPath) )
2659 {
2660 /* dialog.exe uses unix separators even on windows */
2661 strcpy(lString, aDefaultPath) ;
2662 ensureFinalSlash(lString);
2663 replaceChr( lString , '\\' , '/' ) ;
2664 strcat(lDialogString, lString) ;
2665 }
2666 else
2667 {
2668 /* dialog.exe needs at least one separator */
2669 strcat(lDialogString, "./") ;
2670 }
2671 strcat(lDialogString, "\" 0 60 2>");
2672 strcpy(lString, getenv("TEMP"));
2673 strcat(lString, "\\tinyfd.txt");
2674 strcat(lDialogString, lString);
2675
2676 /* printf( "lDialogString: %s\n" , lDialogString ) ; */
2677 system( lDialogString ) ;
2678
2679 if (!(lIn = fopen(lString, "r")))
2680 {
2681 remove(lString);
2682 return NULL;
2683 }
2684 while (fgets(aoBuff, MAX_PATH_OR_CMD, lIn) != NULL)
2685 {}
2686 fclose(lIn);
2687 remove(lString);
2688 replaceChr( aoBuff , '/' , '\\' ) ;
2689 /* printf( "aoBuff: %s\n" , aoBuff ) ; */
2690 return aoBuff;
2691 }
2692
2693 static void writeUtf8( char const * aUtf8String )
2694 {
2695 unsigned long lNum;
2696 void * lConsoleHandle;
2697 wchar_t * lTmpWChar;
2698
2699 lConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
2700 lTmpWChar = tinyfd_utf8to16(aUtf8String);
2701 (void)WriteConsoleW(lConsoleHandle, lTmpWChar, (DWORD) wcslen(lTmpWChar), &lNum, NULL);
2702 }
2703
2704
2705 int tinyfd_messageBox(
2706 char const * aTitle, /* NULL or "" */
2707 char const * aMessage, /* NULL or "" may contain \n and \t */
2708 char const * aDialogType, /* "ok" "okcancel" "yesno" "yesnocancel" */
2709 char const * aIconType, /* "info" "warning" "error" "question" */
2710 int aDefaultButton) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
2711 {
2712 char lChar;
2713 UINT lOriginalCP = 0;
2714 UINT lOriginalOutputCP = 0;
2715
2716 if (tfd_quoteDetected(aTitle)) return tinyfd_messageBox("INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton);
2717 if (tfd_quoteDetected(aMessage)) return tinyfd_messageBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton);
2718
2719 if ((!tinyfd_forceConsole || !(GetConsoleWindow() || dialogPresent()))
2720 && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
2721 {
2722 if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "windows"); return 1; }
2723 return messageBoxWinGui(aTitle, aMessage, aDialogType, aIconType, aDefaultButton);
2724 }
2725 else if (dialogPresent())
2726 {
2727 if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return 0; }
2728 return messageBoxWinConsole(
2729 aTitle, aMessage, aDialogType, aIconType, aDefaultButton);
2730 }
2731 else
2732 {
2733 if (!tinyfd_winUtf8)
2734 {
2735 lOriginalCP = GetConsoleCP();
2736 lOriginalOutputCP = GetConsoleOutputCP();
2737 (void)SetConsoleCP(GetACP());
2738 (void)SetConsoleOutputCP(GetACP());
2739 }
2740
2741 if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return 0; }
2742 if (!gWarningDisplayed && !tinyfd_forceConsole)
2743 {
2744 gWarningDisplayed = 1;
2745 printf("\n\n%s\n", gTitle);
2746 printf("%s\n\n", tinyfd_needs);
2747 }
2748
2749 if (aTitle && strlen(aTitle))
2750 {
2751 printf("\n");
2752 if (tinyfd_winUtf8) writeUtf8(aTitle);
2753 else printf("%s", aTitle);
2754 printf("\n\n");
2755 }
2756 if (aDialogType && !strcmp("yesno", aDialogType))
2757 {
2758 do
2759 {
2760 if (aMessage && strlen(aMessage))
2761 {
2762 if (tinyfd_winUtf8) writeUtf8(aMessage);
2763 else printf("%s", aMessage);
2764 printf("\n");
2765 }
2766 printf("y/n: ");
2767 lChar = (char)tolower(_getch());
2768 printf("\n\n");
2769 } while (lChar != 'y' && lChar != 'n');
2770 if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); }
2771 return lChar == 'y' ? 1 : 0;
2772 }
2773 else if (aDialogType && !strcmp("okcancel", aDialogType))
2774 {
2775 do
2776 {
2777 if (aMessage && strlen(aMessage))
2778 {
2779 if (tinyfd_winUtf8) writeUtf8(aMessage);
2780 else printf("%s", aMessage);
2781 printf("\n");
2782 }
2783 printf("[O]kay/[C]ancel: ");
2784 lChar = (char)tolower(_getch());
2785 printf("\n\n");
2786 } while (lChar != 'o' && lChar != 'c');
2787 if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); }
2788 return lChar == 'o' ? 1 : 0;
2789 }
2790 else if (aDialogType && !strcmp("yesnocancel", aDialogType))
2791 {
2792 do
2793 {
2794 if (aMessage && strlen(aMessage))
2795 {
2796 if (tinyfd_winUtf8) writeUtf8(aMessage);
2797 else printf("%s", aMessage);
2798 printf("\n");
2799 }
2800 printf("[Y]es/[N]o/[C]ancel: ");
2801 lChar = (char)tolower(_getch());
2802 printf("\n\n");
2803 } while (lChar != 'y' && lChar != 'n' && lChar != 'c');
2804 if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); }
2805 return (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0;
2806 }
2807 else
2808 {
2809 if (aMessage && strlen(aMessage))
2810 {
2811 if (tinyfd_winUtf8) writeUtf8(aMessage);
2812 else printf("%s", aMessage);
2813 printf("\n\n");
2814 }
2815 printf("press enter to continue ");
2816 lChar = (char)_getch();
2817 printf("\n\n");
2818 if (!tinyfd_winUtf8) { (void)SetConsoleCP(lOriginalCP); (void)SetConsoleOutputCP(lOriginalOutputCP); }
2819 return 1;
2820 }
2821 }
2822 }
2823
2824
2825 /* return has only meaning for tinyfd_query */
2826 int tinyfd_notifyPopup(
2827 char const * aTitle, /* NULL or "" */
2828 char const * aMessage , /* NULL or "" may contain \n \t */
2829 char const * aIconType ) /* "info" "warning" "error" */
2830 {
2831 if (tfd_quoteDetected(aTitle)) return tinyfd_notifyPopup("INVALID TITLE WITH QUOTES", aMessage, aIconType);
2832 if (tfd_quoteDetected(aMessage)) return tinyfd_notifyPopup(aTitle, "INVALID MESSAGE WITH QUOTES", aIconType);
2833
2834 if ( powershellPresent() && (!tinyfd_forceConsole || !(
2835 GetConsoleWindow() ||
2836 dialogPresent()))
2837 && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
2838 {
2839 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return 1;}
2840 return notifyWinGui(aTitle, aMessage, aIconType);
2841 }
2842 else
2843 return tinyfd_messageBox(aTitle, aMessage, "ok" , aIconType, 0);
2844 }
2845
2846
2847 /* returns NULL on cancel */
2848 char * tinyfd_inputBox(
2849 char const * aTitle , /* NULL or "" */
2850 char const * aMessage , /* NULL or "" (\n and \t have no effect) */
2851 char const * aDefaultInput ) /* "" , if NULL it's a passwordBox */
2852 {
2853 static char lBuff[MAX_PATH_OR_CMD] = "";
2854 char * lEOF;
2855
2856 DWORD mode = 0;
2857 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
2858
2859 unsigned long lNum;
2860 void * lConsoleHandle;
2861 char * lTmpChar;
2862 wchar_t lBuffW[1024];
2863
2864 UINT lOriginalCP = 0;
2865 UINT lOriginalOutputCP = 0;
2866
2867 if (!aTitle && !aMessage && !aDefaultInput) return lBuff; /* now I can fill lBuff from outside */
2868
2869 if (tfd_quoteDetected(aTitle)) return tinyfd_inputBox("INVALID TITLE WITH QUOTES", aMessage, aDefaultInput);
2870 if (tfd_quoteDetected(aMessage)) return tinyfd_inputBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDefaultInput);
2871 if (tfd_quoteDetected(aDefaultInput)) return tinyfd_inputBox(aTitle, aMessage, "INVALID DEFAULT_INPUT WITH QUOTES");
2872
2873 mode = 0;
2874 hStdin = GetStdHandle(STD_INPUT_HANDLE);
2875
2876 if ((!tinyfd_forceConsole || !(
2877 GetConsoleWindow() ||
2878 dialogPresent()))
2879 && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
2880 {
2881 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;}
2882 lBuff[0]='\0';
2883 if (inputBoxWinGui(lBuff, aTitle, aMessage, aDefaultInput)) return lBuff;
2884 else return NULL;
2885 }
2886 else if ( dialogPresent() )
2887 {
2888 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
2889 lBuff[0]='\0';
2890 if (inputBoxWinConsole(lBuff, aTitle, aMessage, aDefaultInput) ) return lBuff;
2891 else return NULL;
2892 }
2893 else
2894 {
2895 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;}
2896 lBuff[0]='\0';
2897 if (!gWarningDisplayed && !tinyfd_forceConsole)
2898 {
2899 gWarningDisplayed = 1 ;
2900 printf("\n\n%s\n", gTitle);
2901 printf("%s\n\n", tinyfd_needs);
2902 }
2903
2904 if (!tinyfd_winUtf8)
2905 {
2906 lOriginalCP = GetConsoleCP();
2907 lOriginalOutputCP = GetConsoleOutputCP();
2908 (void)SetConsoleCP(GetACP());
2909 (void)SetConsoleOutputCP(GetACP());
2910 }
2911
2912 if (aTitle && strlen(aTitle))
2913 {
2914 printf("\n");
2915 if (tinyfd_winUtf8) writeUtf8(aTitle);
2916 else printf("%s", aTitle);
2917 printf("\n\n");
2918 }
2919 if ( aMessage && strlen(aMessage) )
2920 {
2921 if (tinyfd_winUtf8) writeUtf8(aMessage);
2922 else printf("%s", aMessage);
2923 printf("\n");
2924 }
2925 printf("(ctrl-Z + enter to cancel): ");
2926 if ( ! aDefaultInput )
2927 {
2928 (void) GetConsoleMode(hStdin, &mode);
2929 (void) SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
2930 }
2931 if (tinyfd_winUtf8)
2932 {
2933 lConsoleHandle = GetStdHandle(STD_INPUT_HANDLE);
2934 (void) ReadConsoleW(lConsoleHandle, lBuffW, MAX_PATH_OR_CMD, &lNum, NULL);
2935 if (!aDefaultInput)
2936 {
2937 (void)SetConsoleMode(hStdin, mode);
2938 printf("\n");
2939 }
2940 lBuffW[lNum] = '\0';
2941 if (lBuffW[wcslen(lBuffW) - 1] == '\n') lBuffW[wcslen(lBuffW) - 1] = '\0';
2942 if (lBuffW[wcslen(lBuffW) - 1] == '\r') lBuffW[wcslen(lBuffW) - 1] = '\0';
2943 lTmpChar = tinyfd_utf16to8(lBuffW);
2944 if (lTmpChar)
2945 {
2946 strcpy(lBuff, lTmpChar);
2947 return lBuff;
2948 }
2949 else
2950 return NULL;
2951 }
2952 else
2953 {
2954 lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin);
2955 if (!aDefaultInput)
2956 {
2957 (void)SetConsoleMode(hStdin, mode);
2958 printf("\n");
2959 }
2960
2961 if (!tinyfd_winUtf8)
2962 {
2963 (void)SetConsoleCP(lOriginalCP);
2964 (void)SetConsoleOutputCP(lOriginalOutputCP);
2965 }
2966
2967 if (!lEOF)
2968 {
2969 return NULL;
2970 }
2971 printf("\n");
2972 if (strchr(lBuff, 27))
2973 {
2974 return NULL;
2975 }
2976 if (lBuff[strlen(lBuff) - 1] == '\n')
2977 {
2978 lBuff[strlen(lBuff) - 1] = '\0';
2979 }
2980 return lBuff;
2981 }
2982 }
2983 }
2984
2985
2986 char * tinyfd_saveFileDialog(
2987 char const * aTitle , /* NULL or "" */
2988 char const * aDefaultPathAndFile , /* NULL or "" */
2989 int aNumOfFilterPatterns , /* 0 */
2990 char const * const * aFilterPatterns , /* NULL or {"*.jpg","*.png"} */
2991 char const * aSingleFilterDescription ) /* NULL or "image files" */
2992 {
2993 static char lBuff[MAX_PATH_OR_CMD] ;
2994 char lString[MAX_PATH_OR_CMD] ;
2995 char * p ;
2996 char * lPointerInputBox;
2997 int i;
2998
2999 lBuff[0]='\0';
3000
3001 if ( ! aFilterPatterns ) aNumOfFilterPatterns = 0 ;
3002 if (tfd_quoteDetected(aTitle)) return tinyfd_saveFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
3003 if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_saveFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
3004 if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_saveFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES");
3005 for (i = 0; i < aNumOfFilterPatterns; i++)
3006 {
3007 if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_saveFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL);
3008 }
3009
3010
3011 if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) )
3012 && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
3013 {
3014 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;}
3015 p = saveFileDialogWinGui(lBuff,
3016 aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, (char const * const *)aFilterPatterns, aSingleFilterDescription);
3017 }
3018 else if (dialogPresent())
3019 {
3020 if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; }
3021 p = saveFileDialogWinConsole(lBuff, aTitle, aDefaultPathAndFile);
3022 }
3023 else
3024 {
3025 if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; }
3026 strcpy(lBuff, "Save file in ");
3027 strcat(lBuff, getCurDir());
3028
3029 lPointerInputBox = tinyfd_inputBox(NULL,NULL,NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
3030 if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
3031 p = tinyfd_inputBox(aTitle, lBuff, "");
3032 if (p) strcpy(lBuff, p); else lBuff[0] = '\0';
3033 if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */
3034 p = lBuff;
3035 }
3036
3037 if ( ! p || ! strlen( p ) )
3038 {
3039 return NULL;
3040 }
3041 getPathWithoutFinalSlash( lString , p ) ;
3042 if ( strlen( lString ) && ! dirExists( lString ) )
3043 {
3044 return NULL ;
3045 }
3046 getLastName(lString,p);
3047 if ( ! filenameValid(lString) )
3048 {
3049 return NULL;
3050 }
3051 return p ;
3052 }
3053
3054
3055 /* in case of multiple files, the separator is | */
3056 char * tinyfd_openFileDialog(
3057 char const * aTitle , /* NULL or "" */
3058 char const * aDefaultPathAndFile, /* NULL or "" */
3059 int aNumOfFilterPatterns , /* 0 */
3060 char const * const * aFilterPatterns, /* NULL or {"*.jpg","*.png"} */
3061 char const * aSingleFilterDescription, /* NULL or "image files" */
3062 int aAllowMultipleSelects ) /* 0 or 1 */
3063 {
3064 static char lBuff[MAX_PATH_OR_CMD];
3065 char lString[MAX_PATH_OR_CMD];
3066 char * p;
3067 char * lPointerInputBox;
3068 int i;
3069
3070 if ( ! aFilterPatterns ) aNumOfFilterPatterns = 0 ;
3071 if (tfd_quoteDetected(aTitle)) return tinyfd_openFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
3072 if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_openFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
3073 if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_openFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects);
3074 for (i = 0; i < aNumOfFilterPatterns; i++)
3075 {
3076 if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_openFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects);
3077 }
3078
3079 if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) )
3080 && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
3081 {
3082 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;}
3083 p = openFileDialogWinGui( aTitle, aDefaultPathAndFile, aNumOfFilterPatterns,
3084 (char const * const *)aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
3085 }
3086 else if (dialogPresent())
3087 {
3088 if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; }
3089 p = openFileDialogWinConsole(aTitle, aDefaultPathAndFile);
3090 }
3091 else
3092 {
3093 if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; }
3094 strcpy(lBuff, "Open file from ");
3095 strcat(lBuff, getCurDir());
3096 lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
3097 if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
3098 p = tinyfd_inputBox(aTitle, lBuff, "");
3099 if (p) strcpy(lBuff, p); else lBuff[0] = '\0';
3100 if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */
3101 p = lBuff;
3102 }
3103
3104 if ( ! p || ! strlen( p ) )
3105 {
3106 return NULL;
3107 }
3108 if ( aAllowMultipleSelects && strchr(p, '|') )
3109 {
3110 p = ensureFilesExist( (char *) p , p ) ;
3111 }
3112 else if ( ! fileExists(p) )
3113 {
3114 return NULL ;
3115 }
3116 /* printf( "lBuff3: %s\n" , p ) ; */
3117 return p ;
3118 }
3119
3120
3121 char * tinyfd_selectFolderDialog(
3122 char const * aTitle , /* NULL or "" */
3123 char const * aDefaultPath ) /* NULL or "" */
3124 {
3125 static char lBuff[MAX_PATH_OR_CMD];
3126 char * p;
3127 char * lPointerInputBox;
3128 char lString[MAX_PATH_OR_CMD];
3129
3130 if (tfd_quoteDetected(aTitle)) return tinyfd_selectFolderDialog("INVALID TITLE WITH QUOTES", aDefaultPath);
3131 if (tfd_quoteDetected(aDefaultPath)) return tinyfd_selectFolderDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES");
3132
3133 if ( ( !tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent() ) )
3134 && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
3135 {
3136 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;}
3137 p = selectFolderDialogWinGui(lBuff, aTitle, aDefaultPath);
3138 }
3139 else
3140 if (dialogPresent())
3141 {
3142 if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; }
3143 p = selectFolderDialogWinConsole(lBuff, aTitle, aDefaultPath);
3144 }
3145 else
3146 {
3147 if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; }
3148 strcpy(lBuff, "Select folder from ");
3149 strcat(lBuff, getCurDir());
3150 lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
3151 if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
3152 p = tinyfd_inputBox(aTitle, lBuff, "");
3153 if (p) strcpy(lBuff, p); else lBuff[0] = '\0';
3154 if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */
3155 p = lBuff;
3156 }
3157
3158 if ( ! p || ! strlen( p ) || ! dirExists( p ) )
3159 {
3160 return NULL ;
3161 }
3162 return p ;
3163 }
3164
3165
3166 /* aDefaultRGB is used only if aDefaultHexRGB is absent */
3167 /* aDefaultRGB and aoResultRGB can be the same array */
3168 /* returns NULL on cancel */
3169 /* returns the hexcolor as a string "#FF0000" */
3170 /* aoResultRGB also contains the result */
3171 char * tinyfd_colorChooser(
3172 char const * aTitle, /* NULL or "" */
3173 char const * aDefaultHexRGB, /* NULL or "" or "#FF0000"*/
3174 unsigned char const aDefaultRGB[3], /* { 0 , 255 , 255 } */
3175 unsigned char aoResultRGB[3]) /* { 0 , 0 , 0 } */
3176 {
3177 static char lDefaultHexRGB[16];
3178 int i;
3179 char * p ;
3180 char * lPointerInputBox;
3181 char lString[MAX_PATH_OR_CMD];
3182
3183 lDefaultHexRGB[0] = '\0';
3184
3185 if (tfd_quoteDetected(aTitle)) return tinyfd_colorChooser("INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB);
3186 if (tfd_quoteDetected(aDefaultHexRGB)) return tinyfd_colorChooser(aTitle, "INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB);
3187
3188 if ( (!tinyfd_forceConsole || !( GetConsoleWindow() || dialogPresent()) )
3189 && (!getenv("SSH_CLIENT") || getenvDISPLAY()))
3190 {
3191 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"windows");return (char *)1;}
3192 p = colorChooserWinGui(aTitle, aDefaultHexRGB, aDefaultRGB, aoResultRGB);
3193 if (p)
3194 {
3195 strcpy(lDefaultHexRGB, p);
3196 return lDefaultHexRGB;
3197 }
3198 return NULL;
3199 }
3200 else if (dialogPresent())
3201 {
3202 if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "dialog"); return (char *)0; }
3203 }
3204 else
3205 {
3206 if (aTitle&&!strcmp(aTitle, "tinyfd_query")){ strcpy(tinyfd_response, "basicinput"); return (char *)0; }
3207 }
3208
3209 if (aDefaultHexRGB && (strlen(aDefaultHexRGB)==7) )
3210 {
3211 strncpy(lDefaultHexRGB, aDefaultHexRGB,7);
3212 lDefaultHexRGB[7]='\0';
3213 }
3214 else
3215 {
3216 RGB2Hex(aDefaultRGB, lDefaultHexRGB);
3217 }
3218
3219 lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
3220 if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
3221 p = tinyfd_inputBox(aTitle, "Enter hex rgb color (i.e. #f5ca20)", lDefaultHexRGB);
3222
3223 if ( !p || (strlen(p) != 7) || (p[0] != '#') )
3224 {
3225 return NULL ;
3226 }
3227 for ( i = 1 ; i < 7 ; i ++ )
3228 {
3229 if ( ! isxdigit( (int) p[i] ) )
3230 {
3231 return NULL ;
3232 }
3233 }
3234 Hex2RGB(p,aoResultRGB);
3235
3236 strcpy(lDefaultHexRGB, p);
3237
3238 if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */
3239
3240 return lDefaultHexRGB;
3241 }
3242
3243
3244 #else /* unix */
3245
3246 static char gPython2Name[16];
3247 static char gPython3Name[16];
3248 static char gPythonName[16];
3249
3250 int tfd_isDarwin(void)
3251 {
3252 static int lsIsDarwin = -1 ;
3253 struct utsname lUtsname ;
3254 if ( lsIsDarwin < 0 )
3255 {
3256 lsIsDarwin = !uname(&lUtsname) && !strcmp(lUtsname.sysname,"Darwin") ;
3257 }
3258 return lsIsDarwin ;
3259 }
3260
3261
3262 static int dirExists( char const * aDirPath )
3263 {
3264 DIR * lDir ;
3265 if ( ! aDirPath || ! strlen( aDirPath ) )
3266 return 0 ;
3267 lDir = opendir( aDirPath ) ;
3268 if ( ! lDir )
3269 {
3270 return 0 ;
3271 }
3272 closedir( lDir ) ;
3273 return 1 ;
3274 }
3275
3276
3277 static int detectPresence( char const * aExecutable )
3278 {
3279 char lBuff[MAX_PATH_OR_CMD] ;
3280 char lTestedString[MAX_PATH_OR_CMD] = "which " ;
3281 FILE * lIn ;
3282 #ifdef _GNU_SOURCE
3283 char* lAllocatedCharString;
3284 int lSubstringUndetected;
3285 #endif
3286
3287 strcat( lTestedString , aExecutable ) ;
3288 strcat( lTestedString, " 2>/dev/null ");
3289 lIn = popen( lTestedString , "r" ) ;
3290 if ( ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
3291 && ( ! strchr( lBuff , ':' ) ) && ( strncmp(lBuff, "no ", 3) ) )
3292 { /* present */
3293 pclose( lIn ) ;
3294
3295 #ifdef _GNU_SOURCE /*to bypass this, just comment out "#define _GNU_SOURCE" at the top of the file*/
3296 if ( lBuff[strlen( lBuff ) -1] == '\n' ) lBuff[strlen( lBuff ) -1] = '\0' ;
3297 lAllocatedCharString = realpath(lBuff,NULL); /*same as canonicalize_file_name*/
3298 lSubstringUndetected = ! strstr(lAllocatedCharString, aExecutable);
3299 free(lAllocatedCharString);
3300 if (lSubstringUndetected)
3301 {
3302 if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 0);
3303 return 0;
3304 }
3305 #endif /*_GNU_SOURCE*/
3306
3307 if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 1);
3308 return 1 ;
3309 }
3310 else
3311 {
3312 pclose( lIn ) ;
3313 if (tinyfd_verbose) printf("detectPresence %s %d\n", aExecutable, 0);
3314 return 0 ;
3315 }
3316 }
3317
3318
3319 static char * getVersion( char const * aExecutable ) /*version must be first numeral*/
3320 {
3321 static char lBuff[MAX_PATH_OR_CMD] ;
3322 char lTestedString[MAX_PATH_OR_CMD] ;
3323 FILE * lIn ;
3324 char * lTmp ;
3325
3326 strcpy( lTestedString , aExecutable ) ;
3327 strcat( lTestedString , " --version" ) ;
3328
3329 lIn = popen( lTestedString , "r" ) ;
3330 lTmp = fgets( lBuff , sizeof( lBuff ) , lIn ) ;
3331 pclose( lIn ) ;
3332
3333 lTmp += strcspn(lTmp,"0123456789");
3334 /* printf("lTmp:%s\n", lTmp); */
3335 return lTmp ;
3336 }
3337
3338
3339 static int * getMajorMinorPatch( char const * aExecutable )
3340 {
3341 static int lArray[3] ;
3342 char * lTmp ;
3343
3344 lTmp = (char *) getVersion(aExecutable);
3345 lArray[0] = atoi( strtok(lTmp," ,.-") ) ;
3346 /* printf("lArray0 %d\n", lArray[0]); */
3347 lArray[1] = atoi( strtok(0," ,.-") ) ;
3348 /* printf("lArray1 %d\n", lArray[1]); */
3349 lArray[2] = atoi( strtok(0," ,.-") ) ;
3350 /* printf("lArray2 %d\n", lArray[2]); */
3351
3352 if ( !lArray[0] && !lArray[1] && !lArray[2] ) return NULL;
3353 return lArray ;
3354 }
3355
3356
3357 static int tryCommand( char const * aCommand )
3358 {
3359 char lBuff[MAX_PATH_OR_CMD] ;
3360 FILE * lIn ;
3361
3362 lIn = popen( aCommand , "r" ) ;
3363 if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL )
3364 { /* present */
3365 pclose( lIn ) ;
3366 return 1 ;
3367 }
3368 else
3369 {
3370 pclose( lIn ) ;
3371 return 0 ;
3372 }
3373
3374 }
3375
3376
3377 static int isTerminalRunning(void)
3378 {
3379 static int lIsTerminalRunning = -1 ;
3380 if ( lIsTerminalRunning < 0 )
3381 {
3382 lIsTerminalRunning = isatty(1);
3383 if (tinyfd_verbose) printf("isTerminalRunning %d\n", lIsTerminalRunning );
3384 }
3385 return lIsTerminalRunning;
3386 }
3387
3388
3389 static char * dialogNameOnly(void)
3390 {
3391 static char lDialogName[128] = "*" ;
3392 if ( lDialogName[0] == '*' )
3393 {
3394 if (!tinyfd_allowCursesDialogs)
3395 {
3396 strcpy(lDialogName , "" );
3397 }
3398 else if ( tfd_isDarwin() && * strcpy(lDialogName , "/opt/local/bin/dialog" )
3399 && detectPresence( lDialogName ) )
3400 {}
3401 else if ( * strcpy(lDialogName , "dialog" )
3402 && detectPresence( lDialogName ) )
3403 {}
3404 else
3405 {
3406 strcpy(lDialogName , "" );
3407 }
3408 }
3409 return lDialogName ;
3410 }
3411
3412
3413 int isDialogVersionBetter09b(void)
3414 {
3415 char const * lDialogName ;
3416 char * lVersion ;
3417 int lMajor ;
3418 int lMinor ;
3419 int lDate ;
3420 int lResult ;
3421 char * lMinorP ;
3422 char * lLetter ;
3423 char lBuff[128] ;
3424
3425 /*char lTest[128] = " 0.9b-20031126" ;*/
3426
3427 lDialogName = dialogNameOnly() ;
3428 if ( ! strlen(lDialogName) || !(lVersion = (char *) getVersion(lDialogName)) ) return 0 ;
3429 /*lVersion = lTest ;*/
3430 /*printf("lVersion %s\n", lVersion);*/
3431 strcpy(lBuff,lVersion);
3432 lMajor = atoi( strtok(lVersion," ,.-") ) ;
3433 /*printf("lMajor %d\n", lMajor);*/
3434 lMinorP = strtok(0," ,.-abcdefghijklmnopqrstuvxyz");
3435 lMinor = atoi( lMinorP ) ;
3436 /*printf("lMinor %d\n", lMinor );*/
3437 lDate = atoi( strtok(0," ,.-") ) ;
3438 if (lDate<0) lDate = - lDate;
3439 /*printf("lDate %d\n", lDate);*/
3440 lLetter = lMinorP + strlen(lMinorP) ;
3441 strcpy(lVersion,lBuff);
3442 strtok(lLetter," ,.-");
3443 /*printf("lLetter %s\n", lLetter);*/
3444 lResult = (lMajor > 0) || ( ( lMinor == 9 ) && (*lLetter == 'b') && (lDate >= 20031126) );
3445 /*printf("lResult %d\n", lResult);*/
3446 return lResult;
3447 }
3448
3449
3450 static int whiptailPresentOnly(void)
3451 {
3452 static int lWhiptailPresent = -1 ;
3453 if (!tinyfd_allowCursesDialogs) return 0;
3454 if ( lWhiptailPresent < 0 )
3455 {
3456 lWhiptailPresent = detectPresence( "whiptail" ) ;
3457 }
3458 return lWhiptailPresent ;
3459 }
3460
3461
3462 static char * terminalName(void)
3463 {
3464 static char lTerminalName[128] = "*" ;
3465 char lShellName[64] = "*" ;
3466 int * lArray;
3467
3468 if ( lTerminalName[0] == '*' )
3469 {
3470 if ( detectPresence( "bash" ) )
3471 {
3472 strcpy(lShellName , "bash -c " ) ; /*good for basic input*/
3473 }
3474 else if ( strlen(dialogNameOnly()) || whiptailPresentOnly() )
3475 {
3476 strcpy(lShellName , "sh -c " ) ; /*good enough for dialog & whiptail*/
3477 }
3478 else
3479 {
3480 strcpy(lTerminalName , "" ) ;
3481 return NULL ;
3482 }
3483
3484 if ( tfd_isDarwin() )
3485 {
3486 if ( * strcpy(lTerminalName , "/opt/X11/bin/xterm" )
3487 && detectPresence( lTerminalName ) )
3488 {
3489 strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ;
3490 strcat(lTerminalName , lShellName ) ;
3491 }
3492 else
3493 {
3494 strcpy(lTerminalName , "" ) ;
3495 }
3496 }
3497 else if ( * strcpy(lTerminalName,"xterm") /*good (small without parameters)*/
3498 && detectPresence(lTerminalName) )
3499 {
3500 strcat(lTerminalName , " -fa 'DejaVu Sans Mono' -fs 10 -title tinyfiledialogs -e " ) ;
3501 strcat(lTerminalName , lShellName ) ;
3502 }
3503 else if ( * strcpy(lTerminalName,"terminator") /*good*/
3504 && detectPresence(lTerminalName) )
3505 {
3506 strcat(lTerminalName , " -x " ) ;
3507 strcat(lTerminalName , lShellName ) ;
3508 }
3509 else if ( * strcpy(lTerminalName,"lxterminal") /*good*/
3510 && detectPresence(lTerminalName) )
3511 {
3512 strcat(lTerminalName , " -e " ) ;
3513 strcat(lTerminalName , lShellName ) ;
3514 }
3515 else if ( * strcpy(lTerminalName,"konsole") /*good*/
3516 && detectPresence(lTerminalName) )
3517 {
3518 strcat(lTerminalName , " -e " ) ;
3519 strcat(lTerminalName , lShellName ) ;
3520 }
3521 else if ( * strcpy(lTerminalName,"kterm") /*good*/
3522 && detectPresence(lTerminalName) )
3523 {
3524 strcat(lTerminalName , " -e " ) ;
3525 strcat(lTerminalName , lShellName ) ;
3526 }
3527 else if ( * strcpy(lTerminalName,"tilix") /*good*/
3528 && detectPresence(lTerminalName) )
3529 {
3530 strcat(lTerminalName , " -e " ) ;
3531 strcat(lTerminalName , lShellName ) ;
3532 }
3533 else if ( * strcpy(lTerminalName,"xfce4-terminal") /*good*/
3534 && detectPresence(lTerminalName) )
3535 {
3536 strcat(lTerminalName , " -x " ) ;
3537 strcat(lTerminalName , lShellName ) ;
3538 }
3539 else if ( * strcpy(lTerminalName,"mate-terminal") /*good*/
3540 && detectPresence(lTerminalName) )
3541 {
3542 strcat(lTerminalName , " -x " ) ;
3543 strcat(lTerminalName , lShellName ) ;
3544 }
3545 else if ( * strcpy(lTerminalName,"Eterm") /*good*/
3546 && detectPresence(lTerminalName) )
3547 {
3548 strcat(lTerminalName , " -e " ) ;
3549 strcat(lTerminalName , lShellName ) ;
3550 }
3551 else if ( * strcpy(lTerminalName,"evilvte") /*good*/
3552 && detectPresence(lTerminalName) )
3553 {
3554 strcat(lTerminalName , " -e " ) ;
3555 strcat(lTerminalName , lShellName ) ;
3556 }
3557 else if ( * strcpy(lTerminalName,"pterm") /*good (only letters)*/
3558 && detectPresence(lTerminalName) )
3559 {
3560 strcat(lTerminalName , " -e " ) ;
3561 strcat(lTerminalName , lShellName ) ;
3562 }
3563 else if ( * strcpy(lTerminalName,"gnome-terminal")
3564 && detectPresence(lTerminalName) && (lArray = getMajorMinorPatch(lTerminalName))
3565 && ((lArray[0]<3) || (lArray[0]==3 && lArray[1]<=6)) )
3566 {
3567 strcat(lTerminalName , " --disable-factory -x " ) ;
3568 strcat(lTerminalName , lShellName ) ;
3569 }
3570 else
3571 {
3572 strcpy(lTerminalName , "" ) ;
3573 }
3574 /* bad: koi rxterm guake tilda vala-terminal qterminal
3575 aterm Terminal terminology sakura lilyterm weston-terminal
3576 roxterm termit xvt rxvt mrxvt urxvt */
3577 }
3578 if ( strlen(lTerminalName) )
3579 {
3580 return lTerminalName ;
3581 }
3582 else
3583 {
3584 return NULL ;
3585 }
3586 }
3587
3588
3589 static char * dialogName(void)
3590 {
3591 char * lDialogName ;
3592 lDialogName = dialogNameOnly( ) ;
3593 if ( strlen(lDialogName) && ( isTerminalRunning() || terminalName() ) )
3594 {
3595 return lDialogName ;
3596 }
3597 else
3598 {
3599 return NULL ;
3600 }
3601 }
3602
3603
3604 static int whiptailPresent(void)
3605 {
3606 int lWhiptailPresent ;
3607 lWhiptailPresent = whiptailPresentOnly( ) ;
3608 if ( lWhiptailPresent && ( isTerminalRunning() || terminalName() ) )
3609 {
3610 return lWhiptailPresent ;
3611 }
3612 else
3613 {
3614 return 0 ;
3615 }
3616 }
3617
3618
3619
3620 static int graphicMode(void)
3621 {
3622 return !( tinyfd_forceConsole && (isTerminalRunning() || terminalName()) )
3623 && ( getenvDISPLAY()
3624 || (tfd_isDarwin() && (!getenv("SSH_TTY") || getenvDISPLAY() ) ) ) ;
3625 }
3626
3627
3628 static int pactlPresent(void)
3629 {
3630 static int lPactlPresent = -1 ;
3631 if ( lPactlPresent < 0 )
3632 {
3633 lPactlPresent = detectPresence("pactl") ;
3634 }
3635 return lPactlPresent ;
3636 }
3637
3638
3639 static int speakertestPresent(void)
3640 {
3641 static int lSpeakertestPresent = -1 ;
3642 if ( lSpeakertestPresent < 0 )
3643 {
3644 lSpeakertestPresent = detectPresence("speaker-test") ;
3645 }
3646 return lSpeakertestPresent ;
3647 }
3648
3649
3650 static int playPresent()
3651 {
3652 static int lPlayPresent = -1;
3653 if (lPlayPresent < 0)
3654 {
3655 lPlayPresent = detectPresence("sox"); /*if sox is present, play is ready*/
3656 }
3657 return lPlayPresent;
3658 }
3659
3660
3661 static int beepexePresent()
3662 {
3663 static int lBeepexePresent = -1;
3664 if (lBeepexePresent < 0)
3665 {
3666 lBeepexePresent = detectPresence("beep.exe");
3667 }
3668 return lBeepexePresent;
3669 }
3670
3671
3672 static int beepPresent(void)
3673 {
3674 static int lBeepPresent = -1 ;
3675 if ( lBeepPresent < 0 )
3676 {
3677 lBeepPresent = detectPresence("beep") ;
3678 }
3679 return lBeepPresent ;
3680 }
3681
3682
3683 static int xmessagePresent(void)
3684 {
3685 static int lXmessagePresent = -1 ;
3686 if ( lXmessagePresent < 0 )
3687 {
3688 lXmessagePresent = detectPresence("xmessage");/*if not tty,not on osxpath*/
3689 }
3690 return lXmessagePresent && graphicMode( ) ;
3691 }
3692
3693
3694 static int gxmessagePresent(void)
3695 {
3696 static int lGxmessagePresent = -1 ;
3697 if ( lGxmessagePresent < 0 )
3698 {
3699 lGxmessagePresent = detectPresence("gxmessage") ;
3700 }
3701 return lGxmessagePresent && graphicMode( ) ;
3702 }
3703
3704
3705 static int gmessagePresent(void)
3706 {
3707 static int lGmessagePresent = -1 ;
3708 if ( lGmessagePresent < 0 )
3709 {
3710 lGmessagePresent = detectPresence("gmessage") ;
3711 }
3712 return lGmessagePresent && graphicMode( ) ;
3713 }
3714
3715
3716 static int notifysendPresent(void)
3717 {
3718 static int lNotifysendPresent = -1 ;
3719 if ( lNotifysendPresent < 0 )
3720 {
3721 lNotifysendPresent = detectPresence("notify-send") ;
3722 }
3723 return lNotifysendPresent && graphicMode( ) ;
3724 }
3725
3726
3727 static int perlPresent(void)
3728 {
3729 static int lPerlPresent = -1 ;
3730 char lBuff[MAX_PATH_OR_CMD] ;
3731 FILE * lIn ;
3732
3733 if ( lPerlPresent < 0 )
3734 {
3735 lPerlPresent = detectPresence("perl") ;
3736 if (lPerlPresent)
3737 {
3738 lIn = popen("perl -MNet::DBus -e \"Net::DBus->session->get_service('org.freedesktop.Notifications')\" 2>&1", "r");
3739 if (fgets(lBuff, sizeof(lBuff), lIn) == NULL)
3740 {
3741 lPerlPresent = 2;
3742 }
3743 pclose(lIn);
3744 if (tinyfd_verbose) printf("perl-dbus %d\n", lPerlPresent);
3745 }
3746 }
3747 return graphicMode() ? lPerlPresent : 0 ;
3748 }
3749
3750
3751 static int afplayPresent(void)
3752 {
3753 static int lAfplayPresent = -1 ;
3754 char lBuff[MAX_PATH_OR_CMD] ;
3755 FILE * lIn ;
3756
3757 if ( lAfplayPresent < 0 )
3758 {
3759 lAfplayPresent = detectPresence("afplay") ;
3760 if ( lAfplayPresent )
3761 {
3762 lIn = popen( "test -e /System/Library/Sounds/Ping.aiff || echo Ping" , "r" ) ;
3763 if ( fgets( lBuff , sizeof( lBuff ) , lIn ) == NULL )
3764 {
3765 lAfplayPresent = 2 ;
3766 }
3767 pclose( lIn ) ;
3768 if (tinyfd_verbose) printf("afplay %d\n", lAfplayPresent);
3769 }
3770 }
3771 return graphicMode() ? lAfplayPresent : 0 ;
3772 }
3773
3774
3775 static int xdialogPresent(void)
3776 {
3777 static int lXdialogPresent = -1 ;
3778 if ( lXdialogPresent < 0 )
3779 {
3780 lXdialogPresent = detectPresence("Xdialog") ;
3781 }
3782 return lXdialogPresent && graphicMode( ) ;
3783 }
3784
3785
3786 static int gdialogPresent(void)
3787 {
3788 static int lGdialoglPresent = -1 ;
3789 if ( lGdialoglPresent < 0 )
3790 {
3791 lGdialoglPresent = detectPresence( "gdialog" ) ;
3792 }
3793 return lGdialoglPresent && graphicMode( ) ;
3794 }
3795
3796
3797 static int osascriptPresent(void)
3798 {
3799 static int lOsascriptPresent = -1 ;
3800 if ( lOsascriptPresent < 0 )
3801 {
3802 gWarningDisplayed |= !!getenv("SSH_TTY");
3803 lOsascriptPresent = detectPresence( "osascript" ) ;
3804 }
3805 return lOsascriptPresent && graphicMode() && !getenv("SSH_TTY") ;
3806 }
3807
3808
3809 int tfd_qarmaPresent(void)
3810 {
3811 static int lQarmaPresent = -1 ;
3812 if ( lQarmaPresent < 0 )
3813 {
3814 lQarmaPresent = detectPresence("qarma") ;
3815 }
3816 return lQarmaPresent && graphicMode( ) ;
3817 }
3818
3819
3820 int tfd_matedialogPresent(void)
3821 {
3822 static int lMatedialogPresent = -1 ;
3823 if ( lMatedialogPresent < 0 )
3824 {
3825 lMatedialogPresent = detectPresence("matedialog") ;
3826 }
3827 return lMatedialogPresent && graphicMode( ) ;
3828 }
3829
3830
3831 int tfd_shellementaryPresent(void)
3832 {
3833 static int lShellementaryPresent = -1 ;
3834 if ( lShellementaryPresent < 0 )
3835 {
3836 lShellementaryPresent = 0 ; /*detectPresence("shellementary"); shellementary is not ready yet */
3837 }
3838 return lShellementaryPresent && graphicMode( ) ;
3839 }
3840
3841
3842 int tfd_xpropPresent(void)
3843 {
3844 static int lXpropPresent = -1 ;
3845 if ( lXpropPresent < 0 )
3846 {
3847 lXpropPresent = detectPresence("xprop") ;
3848 }
3849 return lXpropPresent && graphicMode( ) ;
3850 }
3851
3852
3853 int tfd_zenityPresent(void)
3854 {
3855 static int lZenityPresent = -1 ;
3856 if ( lZenityPresent < 0 )
3857 {
3858 lZenityPresent = detectPresence("zenity") ;
3859 }
3860 return lZenityPresent && graphicMode( ) ;
3861 }
3862
3863
3864 int tfd_yadPresent(void)
3865 {
3866 static int lYadPresent = -1;
3867 if (lYadPresent < 0)
3868 {
3869 lYadPresent = detectPresence("yad");
3870 }
3871 return lYadPresent && graphicMode();
3872 }
3873
3874
3875 int tfd_zenity3Present(void)
3876 {
3877 static int lZenity3Present = -1 ;
3878 char lBuff[MAX_PATH_OR_CMD] ;
3879 FILE * lIn ;
3880 int lIntTmp ;
3881
3882 if ( lZenity3Present < 0 )
3883 {
3884 lZenity3Present = 0 ;
3885 if ( tfd_zenityPresent() )
3886 {
3887 lIn = popen( "zenity --version" , "r" ) ;
3888 if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
3889 {
3890 if ( atoi(lBuff) >= 3 )
3891 {
3892 lZenity3Present = 3 ;
3893 lIntTmp = atoi(strtok(lBuff,".")+2 ) ;
3894 if ( lIntTmp >= 18 )
3895 {
3896 lZenity3Present = 5 ;
3897 }
3898 else if ( lIntTmp >= 10 )
3899 {
3900 lZenity3Present = 4 ;
3901 }
3902 }
3903 else if ( ( atoi(lBuff) == 2 ) && ( atoi(strtok(lBuff,".")+2 ) >= 32 ) )
3904 {
3905 lZenity3Present = 2 ;
3906 }
3907 if (tinyfd_verbose) printf("zenity type %d\n", lZenity3Present);
3908 }
3909 pclose( lIn ) ;
3910 }
3911 }
3912 return graphicMode() ? lZenity3Present : 0 ;
3913 }
3914
3915
3916 int tfd_kdialogPresent(void)
3917 {
3918 static int lKdialogPresent = -1 ;
3919 char lBuff[MAX_PATH_OR_CMD] ;
3920 FILE * lIn ;
3921 char * lDesktop;
3922
3923 if ( lKdialogPresent < 0 )
3924 {
3925 if ( tfd_zenityPresent() )
3926 {
3927 lDesktop = getenv("XDG_SESSION_DESKTOP");
3928 if ( !lDesktop || ( strcmp(lDesktop, "KDE") && strcmp(lDesktop, "lxqt") ) )
3929 {
3930 lKdialogPresent = 0 ;
3931 return lKdialogPresent ;
3932 }
3933 }
3934
3935 lKdialogPresent = detectPresence("kdialog") ;
3936 if ( lKdialogPresent && !getenv("SSH_TTY") )
3937 {
3938 lIn = popen( "kdialog --attach 2>&1" , "r" ) ;
3939 if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
3940 {
3941 if ( ! strstr( "Unknown" , lBuff ) )
3942 {
3943 lKdialogPresent = 2 ;
3944 if (tinyfd_verbose) printf("kdialog-attach %d\n", lKdialogPresent);
3945 }
3946 }
3947 pclose( lIn ) ;
3948
3949 if (lKdialogPresent == 2)
3950 {
3951 lKdialogPresent = 1 ;
3952 lIn = popen( "kdialog --passivepopup 2>&1" , "r" ) ;
3953 if ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
3954 {
3955 if ( ! strstr( "Unknown" , lBuff ) )
3956 {
3957 lKdialogPresent = 2 ;
3958 if (tinyfd_verbose) printf("kdialog-popup %d\n", lKdialogPresent);
3959 }
3960 }
3961 pclose( lIn ) ;
3962 }
3963 }
3964 }
3965 return graphicMode() ? lKdialogPresent : 0 ;
3966 }
3967
3968
3969 static int osx9orBetter(void)
3970 {
3971 static int lOsx9orBetter = -1 ;
3972 char lBuff[MAX_PATH_OR_CMD] ;
3973 FILE * lIn ;
3974 int V,v;
3975
3976 if ( lOsx9orBetter < 0 )
3977 {
3978 lOsx9orBetter = 0 ;
3979 lIn = popen( "osascript -e 'set osver to system version of (system info)'" , "r" ) ;
3980 V = 0 ;
3981 if ( ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
3982 && ( 2 == sscanf(lBuff, "%d.%d", &V, &v) ) )
3983 {
3984 V = V * 100 + v;
3985 if ( V >= 1009 )
3986 {
3987 lOsx9orBetter = 1 ;
3988 }
3989 }
3990 pclose( lIn ) ;
3991 if (tinyfd_verbose) printf("Osx10 = %d, %d = %s\n", lOsx9orBetter, V, lBuff) ;
3992 }
3993 return lOsx9orBetter ;
3994 }
3995
3996
3997 static int python3Present(void)
3998 {
3999 static int lPython3Present = -1 ;
4000 int i;
4001
4002 if ( lPython3Present < 0 )
4003 {
4004 lPython3Present = 0 ;
4005 strcpy(gPython3Name , "python3" ) ;
4006 if ( detectPresence(gPython3Name) ) lPython3Present = 1;
4007 else
4008 {
4009 for ( i = 9 ; i >= 0 ; i -- )
4010 {
4011 sprintf( gPython3Name , "python3.%d" , i ) ;
4012 if ( detectPresence(gPython3Name) )
4013 {
4014 lPython3Present = 1;
4015 break;
4016 }
4017 }
4018 }
4019 if (tinyfd_verbose) printf("lPython3Present %d\n", lPython3Present) ;
4020 if (tinyfd_verbose) printf("gPython3Name %s\n", gPython3Name) ;
4021 }
4022 return lPython3Present ;
4023 }
4024
4025
4026 static int python2Present(void)
4027 {
4028 static int lPython2Present = -1 ;
4029 int i;
4030
4031 if ( lPython2Present < 0 )
4032 {
4033 lPython2Present = 0 ;
4034 strcpy(gPython2Name , "python2" ) ;
4035 if ( detectPresence(gPython2Name) ) lPython2Present = 1;
4036 else
4037 {
4038 for ( i = 9 ; i >= 0 ; i -- )
4039 {
4040 sprintf( gPython2Name , "python2.%d" , i ) ;
4041 if ( detectPresence(gPython2Name) )
4042 {
4043 lPython2Present = 1;
4044 break;
4045 }
4046 }
4047 }
4048 if (tinyfd_verbose) printf("lPython2Present %d\n", lPython2Present) ;
4049 if (tinyfd_verbose) printf("gPython2Name %s\n", gPython2Name) ;
4050 }
4051 return lPython2Present ;
4052 }
4053
4054
4055 static int tkinter3Present(void)
4056 {
4057 static int lTkinter3Present = -1 ;
4058 char lPythonCommand[256];
4059 char lPythonParams[128] =
4060 "-S -c \"try:\n\timport tkinter;\nexcept:\n\tprint(0);\"";
4061
4062 if ( lTkinter3Present < 0 )
4063 {
4064 lTkinter3Present = 0 ;
4065 if ( python3Present() )
4066 {
4067 sprintf( lPythonCommand , "%s %s" , gPython3Name , lPythonParams ) ;
4068 lTkinter3Present = tryCommand(lPythonCommand) ;
4069 }
4070 if (tinyfd_verbose) printf("lTkinter3Present %d\n", lTkinter3Present) ;
4071 }
4072 return lTkinter3Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") );
4073 }
4074
4075
4076 static int tkinter2Present(void)
4077 {
4078 static int lTkinter2Present = -1 ;
4079 char lPythonCommand[256];
4080 char lPythonParams[128] =
4081 "-S -c \"try:\n\timport Tkinter;\nexcept:\n\tprint 0;\"";
4082
4083 if ( lTkinter2Present < 0 )
4084 {
4085 lTkinter2Present = 0 ;
4086 if ( python2Present() )
4087 {
4088 sprintf( lPythonCommand , "%s %s" , gPython2Name , lPythonParams ) ;
4089 lTkinter2Present = tryCommand(lPythonCommand) ;
4090 }
4091 if (tinyfd_verbose) printf("lTkinter2Present %d\n", lTkinter2Present) ;
4092 }
4093 return lTkinter2Present && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") );
4094 }
4095
4096
4097 static int pythonDbusPresent(void)
4098 {
4099 static int lPythonDbusPresent = -1 ;
4100 char lPythonCommand[384];
4101 char lPythonParams[256] =
4102 "-c \"try:\n\timport dbus;bus=dbus.SessionBus();\
4103 notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');\
4104 notify=dbus.Interface(notif,'org.freedesktop.Notifications');\nexcept:\n\tprint(0);\"";
4105
4106 if (lPythonDbusPresent < 0 )
4107 {
4108 lPythonDbusPresent = 0 ;
4109 if ( python2Present() )
4110 {
4111 strcpy(gPythonName , gPython2Name ) ;
4112 sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ;
4113 lPythonDbusPresent = tryCommand(lPythonCommand) ;
4114 }
4115
4116 if ( !lPythonDbusPresent && python3Present() )
4117 {
4118 strcpy(gPythonName , gPython3Name ) ;
4119 sprintf( lPythonCommand , "%s %s" , gPythonName , lPythonParams ) ;
4120 lPythonDbusPresent = tryCommand(lPythonCommand) ;
4121 }
4122
4123 if (tinyfd_verbose) printf("lPythonDbusPresent %d\n", lPythonDbusPresent) ;
4124 if (tinyfd_verbose) printf("gPythonName %s\n", gPythonName) ;
4125 }
4126 return lPythonDbusPresent && graphicMode() && !(tfd_isDarwin() && getenv("SSH_TTY") );
4127 }
4128
4129
4130 static void sigHandler(int signum)
4131 {
4132 FILE * lIn ;
4133 if ( ( lIn = popen( "pactl unload-module module-sine" , "r" ) ) )
4134 {
4135 pclose( lIn ) ;
4136 }
4137 if (tinyfd_verbose) printf("tinyfiledialogs caught signal %d\n", signum);
4138 }
4139
4140 void tinyfd_beep(void)
4141 {
4142 char lDialogString[256] ;
4143 FILE * lIn ;
4144
4145 if ( osascriptPresent() )
4146 {
4147 if ( afplayPresent() >= 2 )
4148 {
4149 strcpy( lDialogString , "afplay /System/Library/Sounds/Ping.aiff") ;
4150 }
4151 else
4152 {
4153 strcpy( lDialogString , "osascript -e 'tell application \"System Events\" to beep'") ;
4154 }
4155 }
4156 else if ( pactlPresent() )
4157 {
4158 signal(SIGINT, sigHandler);
4159 /*strcpy( lDialogString , "pactl load-module module-sine frequency=440;sleep .3;pactl unload-module module-sine" ) ;*/
4160 strcpy( lDialogString , "thnum=$(pactl load-module module-sine frequency=440);sleep .3;pactl unload-module $thnum" ) ;
4161 }
4162 else if ( speakertestPresent() )
4163 {
4164 /*strcpy( lDialogString , "timeout -k .3 .3 speaker-test --frequency 440 --test sine > /dev/tty" ) ;*/
4165 strcpy( lDialogString , "( speaker-test -t sine -f 440 > /dev/tty )& pid=$!;sleep .4; kill -9 $pid" ) ; /*.3 was too short for mac g3*/
4166 }
4167 else if (beepexePresent())
4168 {
4169 strcpy(lDialogString, "beep.exe 440 300");
4170 }
4171 else if (playPresent()) /* play is part of sox */
4172 {
4173 strcpy(lDialogString, "play -q -n synth .3 sine 440");
4174 }
4175 else if ( beepPresent() )
4176 {
4177 strcpy( lDialogString , "beep -f 440 -l 300" ) ;
4178 }
4179 else
4180 {
4181 strcpy( lDialogString , "printf '\a' > /dev/tty" ) ;
4182 }
4183
4184 if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
4185
4186 if ( ( lIn = popen( lDialogString , "r" ) ) )
4187 {
4188 pclose( lIn ) ;
4189 }
4190
4191 if ( pactlPresent() )
4192 {
4193 signal(SIGINT, SIG_DFL);
4194 }
4195 }
4196
4197
4198 int tinyfd_messageBox(
4199 char const * aTitle , /* NULL or "" */
4200 char const * aMessage , /* NULL or "" may contain \n and \t */
4201 char const * aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */
4202 char const * aIconType , /* "info" "warning" "error" "question" */
4203 int aDefaultButton ) /* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
4204 {
4205 char lBuff[MAX_PATH_OR_CMD] ;
4206 char * lDialogString = NULL ;
4207 char * lpDialogString;
4208 FILE * lIn ;
4209 int lWasGraphicDialog = 0 ;
4210 int lWasXterm = 0 ;
4211 int lResult ;
4212 char lChar ;
4213 struct termios infoOri;
4214 struct termios info;
4215 size_t lTitleLen ;
4216 size_t lMessageLen ;
4217
4218 lBuff[0]='\0';
4219
4220 if (tfd_quoteDetected(aTitle)) return tinyfd_messageBox("INVALID TITLE WITH QUOTES", aMessage, aDialogType, aIconType, aDefaultButton);
4221 if (tfd_quoteDetected(aMessage)) return tinyfd_messageBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDialogType, aIconType, aDefaultButton);
4222
4223 lTitleLen = aTitle ? strlen(aTitle) : 0 ;
4224 lMessageLen = aMessage ? strlen(aMessage) : 0 ;
4225 if ( !aTitle || strcmp(aTitle,"tinyfd_query") )
4226 {
4227 lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen );
4228 }
4229
4230 if ( osascriptPresent( ) )
4231 {
4232 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;}
4233
4234 strcpy( lDialogString , "osascript ");
4235 if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
4236 strcat( lDialogString , " -e 'try' -e 'set {vButton} to {button returned} of ( display dialog \"") ;
4237 if ( aMessage && strlen(aMessage) )
4238 {
4239 strcat(lDialogString, aMessage) ;
4240 }
4241 strcat(lDialogString, "\" ") ;
4242 if ( aTitle && strlen(aTitle) )
4243 {
4244 strcat(lDialogString, "with title \"") ;
4245 strcat(lDialogString, aTitle) ;
4246 strcat(lDialogString, "\" ") ;
4247 }
4248 strcat(lDialogString, "with icon ") ;
4249 if ( aIconType && ! strcmp( "error" , aIconType ) )
4250 {
4251 strcat(lDialogString, "stop " ) ;
4252 }
4253 else if ( aIconType && ! strcmp( "warning" , aIconType ) )
4254 {
4255 strcat(lDialogString, "caution " ) ;
4256 }
4257 else /* question or info */
4258 {
4259 strcat(lDialogString, "note " ) ;
4260 }
4261 if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
4262 {
4263 if ( ! aDefaultButton )
4264 {
4265 strcat( lDialogString ,"default button \"Cancel\" " ) ;
4266 }
4267 }
4268 else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
4269 {
4270 strcat( lDialogString ,"buttons {\"No\", \"Yes\"} " ) ;
4271 if (aDefaultButton)
4272 {
4273 strcat( lDialogString ,"default button \"Yes\" " ) ;
4274 }
4275 else
4276 {
4277 strcat( lDialogString ,"default button \"No\" " ) ;
4278 }
4279 strcat( lDialogString ,"cancel button \"No\"" ) ;
4280 }
4281 else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
4282 {
4283 strcat( lDialogString ,"buttons {\"No\", \"Yes\", \"Cancel\"} " ) ;
4284 switch (aDefaultButton)
4285 {
4286 case 1: strcat( lDialogString ,"default button \"Yes\" " ) ; break;
4287 case 2: strcat( lDialogString ,"default button \"No\" " ) ; break;
4288 case 0: strcat( lDialogString ,"default button \"Cancel\" " ) ; break;
4289 }
4290 strcat( lDialogString ,"cancel button \"Cancel\"" ) ;
4291 }
4292 else
4293 {
4294 strcat( lDialogString ,"buttons {\"OK\"} " ) ;
4295 strcat( lDialogString ,"default button \"OK\" " ) ;
4296 }
4297 strcat( lDialogString, ")' ") ;
4298
4299 strcat( lDialogString,
4300 "-e 'if vButton is \"Yes\" then' -e 'return 1'\
4301 -e 'else if vButton is \"OK\" then' -e 'return 1'\
4302 -e 'else if vButton is \"No\" then' -e 'return 2'\
4303 -e 'else' -e 'return 0' -e 'end if' " );
4304
4305 strcat( lDialogString, "-e 'on error number -128' " ) ;
4306 strcat( lDialogString, "-e '0' " );
4307
4308 strcat( lDialogString, "-e 'end try'") ;
4309 if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
4310 }
4311 else if ( tfd_kdialogPresent() )
4312 {
4313 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;}
4314
4315 strcpy( lDialogString , "kdialog" ) ;
4316 if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
4317 {
4318 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
4319 }
4320
4321 strcat( lDialogString , " --" ) ;
4322 if ( aDialogType && ( ! strcmp( "okcancel" , aDialogType )
4323 || ! strcmp( "yesno" , aDialogType ) || ! strcmp( "yesnocancel" , aDialogType ) ) )
4324 {
4325 if ( aIconType && ( ! strcmp( "warning" , aIconType )
4326 || ! strcmp( "error" , aIconType ) ) )
4327 {
4328 strcat( lDialogString , "warning" ) ;
4329 }
4330 if ( ! strcmp( "yesnocancel" , aDialogType ) )
4331 {
4332 strcat( lDialogString , "yesnocancel" ) ;
4333 }
4334 else
4335 {
4336 strcat( lDialogString , "yesno" ) ;
4337 }
4338 }
4339 else if ( aIconType && ! strcmp( "error" , aIconType ) )
4340 {
4341 strcat( lDialogString , "error" ) ;
4342 }
4343 else if ( aIconType && ! strcmp( "warning" , aIconType ) )
4344 {
4345 strcat( lDialogString , "sorry" ) ;
4346 }
4347 else
4348 {
4349 strcat( lDialogString , "msgbox" ) ;
4350 }
4351 strcat( lDialogString , " \"" ) ;
4352 if ( aMessage )
4353 {
4354 strcat( lDialogString , aMessage ) ;
4355 }
4356 strcat( lDialogString , "\"" ) ;
4357 if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
4358 {
4359 strcat( lDialogString ,
4360 " --yes-label Ok --no-label Cancel" ) ;
4361 }
4362 if ( aTitle && strlen(aTitle) )
4363 {
4364 strcat(lDialogString, " --title \"") ;
4365 strcat(lDialogString, aTitle) ;
4366 strcat(lDialogString, "\"") ;
4367 }
4368
4369 if ( ! strcmp( "yesnocancel" , aDialogType ) )
4370 {
4371 strcat( lDialogString , "; x=$? ;if [ $x = 0 ] ;then echo 1;elif [ $x = 1 ] ;then echo 2;else echo 0;fi");
4372 }
4373 else
4374 {
4375 strcat( lDialogString , ";if [ $? = 0 ];then echo 1;else echo 0;fi");
4376 }
4377 }
4378 else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
4379 {
4380 if ( tfd_zenityPresent() )
4381 {
4382 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;}
4383 strcpy( lDialogString , "szAnswer=$(zenity" ) ;
4384 if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
4385 {
4386 strcat(lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
4387 }
4388 }
4389 else if ( tfd_matedialogPresent() )
4390 {
4391 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return 1;}
4392 strcpy( lDialogString , "szAnswer=$(matedialog" ) ;
4393 }
4394 else if ( tfd_shellementaryPresent() )
4395 {
4396 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return 1;}
4397 strcpy( lDialogString , "szAnswer=$(shellementary" ) ;
4398 }
4399 else
4400 {
4401 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return 1;}
4402 strcpy( lDialogString , "szAnswer=$(qarma" ) ;
4403 if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
4404 {
4405 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
4406 }
4407 }
4408 strcat(lDialogString, " --");
4409
4410 if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
4411 {
4412 strcat( lDialogString ,
4413 "question --ok-label=Ok --cancel-label=Cancel" ) ;
4414 }
4415 else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
4416 {
4417 strcat( lDialogString , "question" ) ;
4418 }
4419 else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
4420 {
4421 strcat( lDialogString , "list --column \"\" --hide-header \"Yes\" \"No\"" ) ;
4422 }
4423 else if ( aIconType && ! strcmp( "error" , aIconType ) )
4424 {
4425 strcat( lDialogString , "error" ) ;
4426 }
4427 else if ( aIconType && ! strcmp( "warning" , aIconType ) )
4428 {
4429 strcat( lDialogString , "warning" ) ;
4430 }
4431 else
4432 {
4433 strcat( lDialogString , "info" ) ;
4434 }
4435
4436 strcat(lDialogString, " --title=\"");
4437 if ( aTitle && strlen(aTitle) ) strcat(lDialogString, aTitle) ;
4438 strcat(lDialogString, "\"");
4439
4440 if (strcmp("yesnocancel", aDialogType)) strcat(lDialogString, " --no-wrap");
4441
4442 strcat(lDialogString, " --text=\"") ;
4443 if (aMessage && strlen(aMessage)) strcat(lDialogString, aMessage) ;
4444 strcat(lDialogString, "\"") ;
4445
4446 if ( (tfd_zenity3Present() >= 3) || (!tfd_zenityPresent() && (tfd_shellementaryPresent() || tfd_qarmaPresent()) ) )
4447 {
4448 strcat( lDialogString , " --icon-name=dialog-" ) ;
4449 if ( aIconType && (! strcmp( "question" , aIconType )
4450 || ! strcmp( "error" , aIconType )
4451 || ! strcmp( "warning" , aIconType ) ) )
4452 {
4453 strcat( lDialogString , aIconType ) ;
4454 }
4455 else
4456 {
4457 strcat( lDialogString , "information" ) ;
4458 }
4459 }
4460
4461 if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
4462
4463 if ( ! strcmp( "yesnocancel" , aDialogType ) )
4464 {
4465 strcat( lDialogString ,
4466 ");if [ $? = 1 ];then echo 0;elif [ $szAnswer = \"No\" ];then echo 2;else echo 1;fi");
4467 }
4468 else
4469 {
4470 strcat( lDialogString , ");if [ $? = 0 ];then echo 1;else echo 0;fi");
4471 }
4472 }
4473
4474 else if (tfd_yadPresent())
4475 {
4476 if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return 1; }
4477 strcpy(lDialogString, "szAnswer=$(yad --");
4478 if (aDialogType && !strcmp("ok", aDialogType))
4479 {
4480 strcat(lDialogString,"button=Ok:1");
4481 }
4482 else if (aDialogType && !strcmp("okcancel", aDialogType))
4483 {
4484 strcat(lDialogString,"button=Ok:1 --button=Cancel:0");
4485 }
4486 else if (aDialogType && !strcmp("yesno", aDialogType))
4487 {
4488 strcat(lDialogString, "button=Yes:1 --button=No:0");
4489 }
4490 else if (aDialogType && !strcmp("yesnocancel", aDialogType))
4491 {
4492 strcat(lDialogString, "button=Yes:1 --button=No:2 --button=Cancel:0");
4493 }
4494 else if (aIconType && !strcmp("error", aIconType))
4495 {
4496 strcat(lDialogString, "error");
4497 }
4498 else if (aIconType && !strcmp("warning", aIconType))
4499 {
4500 strcat(lDialogString, "warning");
4501 }
4502 else
4503 {
4504 strcat(lDialogString, "info");
4505 }
4506 if (aTitle && strlen(aTitle))
4507 {
4508 strcat(lDialogString, " --title=\"");
4509 strcat(lDialogString, aTitle);
4510 strcat(lDialogString, "\"");
4511 }
4512 if (aMessage && strlen(aMessage))
4513 {
4514 strcat(lDialogString, " --text=\"");
4515 strcat(lDialogString, aMessage);
4516 strcat(lDialogString, "\"");
4517 }
4518
4519 strcat(lDialogString, " --icon-name=dialog-");
4520 if (aIconType && (!strcmp("question", aIconType)
4521 || !strcmp("error", aIconType)
4522 || !strcmp("warning", aIconType)))
4523 {
4524 strcat(lDialogString, aIconType);
4525 }
4526 else
4527 {
4528 strcat(lDialogString, "information");
4529 }
4530
4531 if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
4532 strcat(lDialogString,");echo $?");
4533 }
4534
4535 else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter3Present() )
4536 {
4537 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return 1;}
4538
4539 strcpy( lDialogString , gPython3Name ) ;
4540 strcat( lDialogString ,
4541 " -S -c \"import tkinter;from tkinter import messagebox;root=tkinter.Tk();root.withdraw();");
4542
4543 strcat( lDialogString ,"res=messagebox." ) ;
4544 if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
4545 {
4546 strcat( lDialogString , "askokcancel(" ) ;
4547 if ( aDefaultButton )
4548 {
4549 strcat( lDialogString , "default=messagebox.OK," ) ;
4550 }
4551 else
4552 {
4553 strcat( lDialogString , "default=messagebox.CANCEL," ) ;
4554 }
4555 }
4556 else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
4557 {
4558 strcat( lDialogString , "askyesno(" ) ;
4559 if ( aDefaultButton )
4560 {
4561 strcat( lDialogString , "default=messagebox.YES," ) ;
4562 }
4563 else
4564 {
4565 strcat( lDialogString , "default=messagebox.NO," ) ;
4566 }
4567 }
4568 else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
4569 {
4570 strcat( lDialogString , "askyesnocancel(" ) ;
4571 switch ( aDefaultButton )
4572 {
4573 case 1: strcat( lDialogString , "default=messagebox.YES," ); break;
4574 case 2: strcat( lDialogString , "default=messagebox.NO," ); break;
4575 case 0: strcat( lDialogString , "default=messagebox.CANCEL," ); break;
4576 }
4577 }
4578 else
4579 {
4580 strcat( lDialogString , "showinfo(" ) ;
4581 }
4582
4583 strcat( lDialogString , "icon='" ) ;
4584 if ( aIconType && (! strcmp( "question" , aIconType )
4585 || ! strcmp( "error" , aIconType )
4586 || ! strcmp( "warning" , aIconType ) ) )
4587 {
4588 strcat( lDialogString , aIconType ) ;
4589 }
4590 else
4591 {
4592 strcat( lDialogString , "info" ) ;
4593 }
4594
4595 strcat(lDialogString, "',") ;
4596 if ( aTitle && strlen(aTitle) )
4597 {
4598 strcat(lDialogString, "title='") ;
4599 strcat(lDialogString, aTitle) ;
4600 strcat(lDialogString, "',") ;
4601 }
4602 if ( aMessage && strlen(aMessage) )
4603 {
4604 strcat(lDialogString, "message='") ;
4605 lpDialogString = lDialogString + strlen(lDialogString);
4606 tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
4607 strcat(lDialogString, "'") ;
4608 }
4609
4610 if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
4611 {
4612 strcat(lDialogString, ");\n\
4613 if res is None :\n\tprint(0)\n\
4614 elif res is False :\n\tprint(2)\n\
4615 else :\n\tprint (1)\n\"" ) ;
4616 }
4617 else
4618 {
4619 strcat(lDialogString, ");\n\
4620 if res is False :\n\tprint(0)\n\
4621 else :\n\tprint(1)\n\"" ) ;
4622 }
4623 }
4624 else if ( !gxmessagePresent() && !gmessagePresent() && !gdialogPresent() && !xdialogPresent() && tkinter2Present() )
4625 {
4626 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return 1;}
4627 strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
4628 strcat( lDialogString , gPython2Name ) ;
4629 if ( ! isTerminalRunning( ) && tfd_isDarwin( ) )
4630 {
4631 strcat( lDialogString , " -i" ) ; /* for osx without console */
4632 }
4633
4634 strcat( lDialogString ,
4635 " -S -c \"import Tkinter,tkMessageBox;root=Tkinter.Tk();root.withdraw();");
4636
4637 if ( tfd_isDarwin( ) )
4638 {
4639 strcat( lDialogString ,
4640 "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
4641 frontmost of process \\\"Python\\\" to true' ''');");
4642 }
4643
4644 strcat( lDialogString ,"res=tkMessageBox." ) ;
4645 if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
4646 {
4647 strcat( lDialogString , "askokcancel(" ) ;
4648 if ( aDefaultButton )
4649 {
4650 strcat( lDialogString , "default=tkMessageBox.OK," ) ;
4651 }
4652 else
4653 {
4654 strcat( lDialogString , "default=tkMessageBox.CANCEL," ) ;
4655 }
4656 }
4657 else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
4658 {
4659 strcat( lDialogString , "askyesno(" ) ;
4660 if ( aDefaultButton )
4661 {
4662 strcat( lDialogString , "default=tkMessageBox.YES," ) ;
4663 }
4664 else
4665 {
4666 strcat( lDialogString , "default=tkMessageBox.NO," ) ;
4667 }
4668 }
4669 else if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
4670 {
4671 strcat( lDialogString , "askyesnocancel(" ) ;
4672 switch ( aDefaultButton )
4673 {
4674 case 1: strcat( lDialogString , "default=tkMessageBox.YES," ); break;
4675 case 2: strcat( lDialogString , "default=tkMessageBox.NO," ); break;
4676 case 0: strcat( lDialogString , "default=tkMessageBox.CANCEL," ); break;
4677 }
4678 }
4679 else
4680 {
4681 strcat( lDialogString , "showinfo(" ) ;
4682 }
4683
4684 strcat( lDialogString , "icon='" ) ;
4685 if ( aIconType && (! strcmp( "question" , aIconType )
4686 || ! strcmp( "error" , aIconType )
4687 || ! strcmp( "warning" , aIconType ) ) )
4688 {
4689 strcat( lDialogString , aIconType ) ;
4690 }
4691 else
4692 {
4693 strcat( lDialogString , "info" ) ;
4694 }
4695
4696 strcat(lDialogString, "',") ;
4697 if ( aTitle && strlen(aTitle) )
4698 {
4699 strcat(lDialogString, "title='") ;
4700 strcat(lDialogString, aTitle) ;
4701 strcat(lDialogString, "',") ;
4702 }
4703 if ( aMessage && strlen(aMessage) )
4704 {
4705 strcat(lDialogString, "message='") ;
4706 lpDialogString = lDialogString + strlen(lDialogString);
4707 tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
4708 strcat(lDialogString, "'") ;
4709 }
4710
4711 if ( aDialogType && ! strcmp( "yesnocancel" , aDialogType ) )
4712 {
4713 strcat(lDialogString, ");\n\
4714 if res is None :\n\tprint 0\n\
4715 elif res is False :\n\tprint 2\n\
4716 else :\n\tprint 1\n\"" ) ;
4717 }
4718 else
4719 {
4720 strcat(lDialogString, ");\n\
4721 if res is False :\n\tprint 0\n\
4722 else :\n\tprint 1\n\"" ) ;
4723 }
4724 }
4725 else if ( gxmessagePresent() || gmessagePresent() || (!gdialogPresent() && !xdialogPresent() && xmessagePresent()) )
4726 {
4727 if ( gxmessagePresent() )
4728 {
4729 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return 1;}
4730 strcpy( lDialogString , "gxmessage");
4731 }
4732 else if ( gmessagePresent() )
4733 {
4734 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return 1;}
4735 strcpy( lDialogString , "gmessage");
4736 }
4737 else
4738 {
4739 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xmessage");return 1;}
4740 strcpy( lDialogString , "xmessage");
4741 }
4742
4743 if ( aDialogType && ! strcmp("okcancel" , aDialogType) )
4744 {
4745 strcat( lDialogString , " -buttons Ok:1,Cancel:0");
4746 switch ( aDefaultButton )
4747 {
4748 case 1: strcat( lDialogString , " -default Ok"); break;
4749 case 0: strcat( lDialogString , " -default Cancel"); break;
4750 }
4751 }
4752 else if ( aDialogType && ! strcmp("yesno" , aDialogType) )
4753 {
4754 strcat( lDialogString , " -buttons Yes:1,No:0");
4755 switch ( aDefaultButton )
4756 {
4757 case 1: strcat( lDialogString , " -default Yes"); break;
4758 case 0: strcat( lDialogString , " -default No"); break;
4759 }
4760 }
4761 else if ( aDialogType && ! strcmp("yesnocancel" , aDialogType) )
4762 {
4763 strcat( lDialogString , " -buttons Yes:1,No:2,Cancel:0");
4764 switch ( aDefaultButton )
4765 {
4766 case 1: strcat( lDialogString , " -default Yes"); break;
4767 case 2: strcat( lDialogString , " -default No"); break;
4768 case 0: strcat( lDialogString , " -default Cancel"); break;
4769 }
4770 }
4771 else
4772 {
4773 strcat( lDialogString , " -buttons Ok:1");
4774 strcat( lDialogString , " -default Ok");
4775 }
4776
4777 strcat( lDialogString , " -center \"");
4778 if ( aMessage && strlen(aMessage) )
4779 {
4780 strcat( lDialogString , aMessage ) ;
4781 }
4782 strcat(lDialogString, "\"" ) ;
4783 if ( aTitle && strlen(aTitle) )
4784 {
4785 strcat( lDialogString , " -title \"");
4786 strcat( lDialogString , aTitle ) ;
4787 strcat( lDialogString, "\"" ) ;
4788 }
4789 strcat( lDialogString , " ; echo $? ");
4790 }
4791 else if ( xdialogPresent() || gdialogPresent() || dialogName() || whiptailPresent() )
4792 {
4793 if ( gdialogPresent( ) )
4794 {
4795 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return 1;}
4796 lWasGraphicDialog = 1 ;
4797 strcpy( lDialogString , "(gdialog " ) ;
4798 }
4799 else if ( xdialogPresent( ) )
4800 {
4801 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return 1;}
4802 lWasGraphicDialog = 1 ;
4803 strcpy( lDialogString , "(Xdialog " ) ;
4804 }
4805 else if ( dialogName( ) )
4806 {
4807 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return 0;}
4808 if ( isTerminalRunning( ) )
4809 {
4810 strcpy( lDialogString , "(dialog " ) ;
4811 }
4812 else
4813 {
4814 lWasXterm = 1 ;
4815 strcpy( lDialogString , terminalName() ) ;
4816 strcat( lDialogString , "'(" ) ;
4817 strcat( lDialogString , dialogName() ) ;
4818 strcat( lDialogString , " " ) ;
4819 }
4820 }
4821 else if ( isTerminalRunning( ) )
4822 {
4823 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;}
4824 strcpy( lDialogString , "(whiptail " ) ;
4825 }
4826 else
4827 {
4828 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return 0;}
4829 lWasXterm = 1 ;
4830 strcpy( lDialogString , terminalName() ) ;
4831 strcat( lDialogString , "'(whiptail " ) ;
4832 }
4833
4834 if ( aTitle && strlen(aTitle) )
4835 {
4836 strcat(lDialogString, "--title \"") ;
4837 strcat(lDialogString, aTitle) ;
4838 strcat(lDialogString, "\" ") ;
4839 }
4840
4841 if ( !xdialogPresent() && !gdialogPresent() )
4842 {
4843 if ( aDialogType && ( !strcmp( "okcancel" , aDialogType ) || !strcmp( "yesno" , aDialogType )
4844 || !strcmp( "yesnocancel" , aDialogType ) ) )
4845 {
4846 strcat(lDialogString, "--backtitle \"") ;
4847 strcat(lDialogString, "tab: move focus") ;
4848 strcat(lDialogString, "\" ") ;
4849 }
4850 }
4851
4852 if ( aDialogType && ! strcmp( "okcancel" , aDialogType ) )
4853 {
4854 if ( ! aDefaultButton )
4855 {
4856 strcat( lDialogString , "--defaultno " ) ;
4857 }
4858 strcat( lDialogString ,
4859 "--yes-label \"Ok\" --no-label \"Cancel\" --yesno " ) ;
4860 }
4861 else if ( aDialogType && ! strcmp( "yesno" , aDialogType ) )
4862 {
4863 if ( ! aDefaultButton )
4864 {
4865 strcat( lDialogString , "--defaultno " ) ;
4866 }
4867 strcat( lDialogString , "--yesno " ) ;
4868 }
4869 else if (aDialogType && !strcmp("yesnocancel", aDialogType))
4870 {
4871 if (!aDefaultButton)
4872 {
4873 strcat(lDialogString, "--defaultno ");
4874 }
4875 strcat(lDialogString, "--menu ");
4876 }
4877 else
4878 {
4879 strcat( lDialogString , "--msgbox " ) ;
4880
4881 }
4882 strcat( lDialogString , "\"" ) ;
4883 if ( aMessage && strlen(aMessage) )
4884 {
4885 strcat(lDialogString, aMessage) ;
4886 }
4887 strcat(lDialogString, "\" ");
4888
4889 if ( lWasGraphicDialog )
4890 {
4891 if (aDialogType && !strcmp("yesnocancel", aDialogType))
4892 {
4893 strcat(lDialogString,"0 60 0 Yes \"\" No \"\") 2>/tmp/tinyfd.txt;\
4894 if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
4895 tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
4896 }
4897 else
4898 {
4899 strcat(lDialogString,
4900 "10 60 ) 2>&1;if [ $? = 0 ];then echo 1;else echo 0;fi");
4901 }
4902 }
4903 else
4904 {
4905 if (aDialogType && !strcmp("yesnocancel", aDialogType))
4906 {
4907 strcat(lDialogString,"0 60 0 Yes \"\" No \"\" >/dev/tty ) 2>/tmp/tinyfd.txt;\
4908 if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
4909 tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
4910
4911 if ( lWasXterm )
4912 {
4913 strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt");
4914 }
4915 else
4916 {
4917 strcat(lDialogString, "; clear >/dev/tty") ;
4918 }
4919 }
4920 else
4921 {
4922 strcat(lDialogString, "10 60 >/dev/tty) 2>&1;if [ $? = 0 ];");
4923 if ( lWasXterm )
4924 {
4925 strcat( lDialogString ,
4926 "then\n\techo 1\nelse\n\techo 0\nfi >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
4927 }
4928 else
4929 {
4930 strcat(lDialogString,
4931 "then echo 1;else echo 0;fi;clear >/dev/tty");
4932 }
4933 }
4934 }
4935 }
4936 else if ( !isTerminalRunning() && terminalName() )
4937 {
4938 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;}
4939 strcpy( lDialogString , terminalName() ) ;
4940 strcat( lDialogString , "'" ) ;
4941 if ( !gWarningDisplayed && !tinyfd_forceConsole)
4942 {
4943 gWarningDisplayed = 1 ;
4944 strcat( lDialogString , "echo \"" ) ;
4945 strcat( lDialogString, gTitle) ;
4946 strcat( lDialogString , "\";" ) ;
4947 strcat( lDialogString , "echo \"" ) ;
4948 strcat( lDialogString, tinyfd_needs) ;
4949 strcat( lDialogString , "\";echo;echo;" ) ;
4950 }
4951 if ( aTitle && strlen(aTitle) )
4952 {
4953 strcat( lDialogString , "echo \"" ) ;
4954 strcat( lDialogString, aTitle) ;
4955 strcat( lDialogString , "\";echo;" ) ;
4956 }
4957 if ( aMessage && strlen(aMessage) )
4958 {
4959 strcat( lDialogString , "echo \"" ) ;
4960 strcat( lDialogString, aMessage) ;
4961 strcat( lDialogString , "\"; " ) ;
4962 }
4963 if ( aDialogType && !strcmp("yesno",aDialogType) )
4964 {
4965 strcat( lDialogString , "echo -n \"y/n: \"; " ) ;
4966 strcat( lDialogString , "stty sane -echo;" ) ;
4967 strcat( lDialogString ,
4968 "answer=$( while ! head -c 1 | grep -i [ny];do true ;done);");
4969 strcat( lDialogString ,
4970 "if echo \"$answer\" | grep -iq \"^y\";then\n");
4971 strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ;
4972 }
4973 else if ( aDialogType && !strcmp("okcancel",aDialogType) )
4974 {
4975 strcat( lDialogString , "echo -n \"[O]kay/[C]ancel: \"; " ) ;
4976 strcat( lDialogString , "stty sane -echo;" ) ;
4977 strcat( lDialogString ,
4978 "answer=$( while ! head -c 1 | grep -i [oc];do true ;done);");
4979 strcat( lDialogString ,
4980 "if echo \"$answer\" | grep -iq \"^o\";then\n");
4981 strcat( lDialogString , "\techo 1\nelse\n\techo 0\nfi" ) ;
4982 }
4983 else if ( aDialogType && !strcmp("yesnocancel",aDialogType) )
4984 {
4985 strcat( lDialogString , "echo -n \"[Y]es/[N]o/[C]ancel: \"; " ) ;
4986 strcat( lDialogString , "stty sane -echo;" ) ;
4987 strcat( lDialogString ,
4988 "answer=$( while ! head -c 1 | grep -i [nyc];do true ;done);");
4989 strcat( lDialogString ,
4990 "if echo \"$answer\" | grep -iq \"^y\";then\n\techo 1\n");
4991 strcat( lDialogString , "elif echo \"$answer\" | grep -iq \"^n\";then\n\techo 2\n" ) ;
4992 strcat( lDialogString , "else\n\techo 0\nfi" ) ;
4993 }
4994 else
4995 {
4996 strcat(lDialogString , "echo -n \"press enter to continue \"; ");
4997 strcat( lDialogString , "stty sane -echo;" ) ;
4998 strcat( lDialogString ,
4999 "answer=$( while ! head -c 1;do true ;done);echo 1");
5000 }
5001 strcat( lDialogString ,
5002 " >/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
5003 }
5004 else if ( !isTerminalRunning() && pythonDbusPresent() && !strcmp("ok" , aDialogType) )
5005 {
5006 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python-dbus");return 1;}
5007 strcpy( lDialogString , gPythonName ) ;
5008 strcat( lDialogString ," -c \"import dbus;bus=dbus.SessionBus();");
5009 strcat( lDialogString ,"notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');" ) ;
5010 strcat( lDialogString ,"notify=dbus.Interface(notif,'org.freedesktop.Notifications');" ) ;
5011 strcat( lDialogString ,"notify.Notify('',0,'" ) ;
5012 if ( aIconType && strlen(aIconType) )
5013 {
5014 strcat( lDialogString , aIconType ) ;
5015 }
5016 strcat(lDialogString, "','") ;
5017 if ( aTitle && strlen(aTitle) )
5018 {
5019 strcat(lDialogString, aTitle) ;
5020 }
5021 strcat(lDialogString, "','") ;
5022 if ( aMessage && strlen(aMessage) )
5023 {
5024 lpDialogString = lDialogString + strlen(lDialogString);
5025 tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
5026 }
5027 strcat(lDialogString, "','','',5000)\"") ;
5028 }
5029 else if ( !isTerminalRunning() && (perlPresent() >= 2) && !strcmp("ok" , aDialogType) )
5030 {
5031 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;}
5032
5033 strcpy( lDialogString , "perl -e \"use Net::DBus;\
5034 my \\$sessionBus = Net::DBus->session;\
5035 my \\$notificationsService = \\$sessionBus->get_service('org.freedesktop.Notifications');\
5036 my \\$notificationsObject = \\$notificationsService->get_object('/org/freedesktop/Notifications',\
5037 'org.freedesktop.Notifications');");
5038
5039 sprintf( lDialogString + strlen(lDialogString),
5040 "my \\$notificationId;\\$notificationId = \\$notificationsObject->Notify(shift, 0, '%s', '%s', '%s', [], {}, -1);\" ",
5041 aIconType?aIconType:"", aTitle?aTitle:"", aMessage?aMessage:"" ) ;
5042 }
5043 else if ( !isTerminalRunning() && notifysendPresent() && !strcmp("ok" , aDialogType) )
5044 {
5045
5046 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notifysend");return 1;}
5047 strcpy( lDialogString , "notify-send" ) ;
5048 if ( aIconType && strlen(aIconType) )
5049 {
5050 strcat( lDialogString , " -i '" ) ;
5051 strcat( lDialogString , aIconType ) ;
5052 strcat( lDialogString , "'" ) ;
5053 }
5054 strcat( lDialogString , " \"" ) ;
5055 if ( aTitle && strlen(aTitle) )
5056 {
5057 strcat(lDialogString, aTitle) ;
5058 strcat( lDialogString , " | " ) ;
5059 }
5060 if ( aMessage && strlen(aMessage) )
5061 {
5062 tfd_replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ;
5063 tfd_replaceSubStr( aMessage , "\n" , " | " , lBuff ) ;
5064 tfd_replaceSubStr( aMessage , "\t" , " " , lBuff ) ;
5065 strcat(lDialogString, lBuff) ;
5066 }
5067 strcat( lDialogString , "\"" ) ;
5068 }
5069 else
5070 {
5071 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return 0;}
5072 if ( !gWarningDisplayed && !tinyfd_forceConsole)
5073 {
5074 gWarningDisplayed = 1 ;
5075 printf("\n\n%s\n", gTitle);
5076 printf("%s\n\n", tinyfd_needs);
5077 }
5078 if ( aTitle && strlen(aTitle) )
5079 {
5080 printf("\n%s\n", aTitle);
5081 }
5082
5083 tcgetattr(0, &infoOri);
5084 tcgetattr(0, &info);
5085 info.c_lflag &= ~ICANON;
5086 info.c_cc[VMIN] = 1;
5087 info.c_cc[VTIME] = 0;
5088 tcsetattr(0, TCSANOW, &info);
5089 if ( aDialogType && !strcmp("yesno",aDialogType) )
5090 {
5091 do
5092 {
5093 if ( aMessage && strlen(aMessage) )
5094 {
5095 printf("\n%s\n",aMessage);
5096 }
5097 printf("y/n: "); fflush(stdout);
5098 lChar = tolower( getchar() ) ;
5099 printf("\n\n");
5100 }
5101 while ( lChar != 'y' && lChar != 'n' );
5102 lResult = lChar == 'y' ? 1 : 0 ;
5103 }
5104 else if ( aDialogType && !strcmp("okcancel",aDialogType) )
5105 {
5106 do
5107 {
5108 if ( aMessage && strlen(aMessage) )
5109 {
5110 printf("\n%s\n",aMessage);
5111 }
5112 printf("[O]kay/[C]ancel: "); fflush(stdout);
5113 lChar = tolower( getchar() ) ;
5114 printf("\n\n");
5115 }
5116 while ( lChar != 'o' && lChar != 'c' );
5117 lResult = lChar == 'o' ? 1 : 0 ;
5118 }
5119 else if ( aDialogType && !strcmp("yesnocancel",aDialogType) )
5120 {
5121 do
5122 {
5123 if ( aMessage && strlen(aMessage) )
5124 {
5125 printf("\n%s\n",aMessage);
5126 }
5127 printf("[Y]es/[N]o/[C]ancel: "); fflush(stdout);
5128 lChar = tolower( getchar() ) ;
5129 printf("\n\n");
5130 }
5131 while ( lChar != 'y' && lChar != 'n' && lChar != 'c' );
5132 lResult = (lChar == 'y') ? 1 : (lChar == 'n') ? 2 : 0 ;
5133 }
5134 else
5135 {
5136 if ( aMessage && strlen(aMessage) )
5137 {
5138 printf("\n%s\n\n",aMessage);
5139 }
5140 printf("press enter to continue "); fflush(stdout);
5141 getchar() ;
5142 printf("\n\n");
5143 lResult = 1 ;
5144 }
5145 tcsetattr(0, TCSANOW, &infoOri);
5146 free(lDialogString);
5147 return lResult ;
5148 }
5149
5150 if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
5151
5152 if ( ! ( lIn = popen( lDialogString , "r" ) ) )
5153 {
5154 free(lDialogString);
5155 return 0 ;
5156 }
5157 while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
5158 {}
5159
5160 pclose( lIn ) ;
5161
5162 /* printf( "lBuff: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
5163 if ( lBuff[strlen( lBuff ) -1] == '\n' )
5164 {
5165 lBuff[strlen( lBuff ) -1] = '\0' ;
5166 }
5167 /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
5168
5169 if (aDialogType && !strcmp("yesnocancel", aDialogType))
5170 {
5171 if ( lBuff[0]=='1' )
5172 {
5173 if ( !strcmp( lBuff+1 , "Yes" )) strcpy(lBuff,"1");
5174 else if ( !strcmp( lBuff+1 , "No" )) strcpy(lBuff,"2");
5175 }
5176 }
5177 /* printf( "lBuff2: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
5178
5179 lResult = !strcmp( lBuff , "2" ) ? 2 : !strcmp( lBuff , "1" ) ? 1 : 0;
5180
5181 /* printf( "lResult: %d\n" , lResult ) ; */
5182 free(lDialogString);
5183 return lResult ;
5184 }
5185
5186
5187 /* return has only meaning for tinyfd_query */
5188 int tinyfd_notifyPopup(
5189 char const * aTitle , /* NULL or "" */
5190 char const * aMessage , /* NULL or "" may contain \n and \t */
5191 char const * aIconType ) /* "info" "warning" "error" */
5192 {
5193 char lBuff[MAX_PATH_OR_CMD];
5194 char * lDialogString = NULL ;
5195 char * lpDialogString ;
5196 FILE * lIn ;
5197 size_t lTitleLen ;
5198 size_t lMessageLen ;
5199
5200 if (tfd_quoteDetected(aTitle)) return tinyfd_notifyPopup("INVALID TITLE WITH QUOTES", aMessage, aIconType);
5201 if (tfd_quoteDetected(aMessage)) return tinyfd_notifyPopup(aTitle, "INVALID MESSAGE WITH QUOTES", aIconType);
5202
5203 if ( getenv("SSH_TTY") )
5204 {
5205 return tinyfd_messageBox(aTitle, aMessage, "ok", aIconType, 0);
5206 }
5207
5208 lTitleLen = aTitle ? strlen(aTitle) : 0 ;
5209 lMessageLen = aMessage ? strlen(aMessage) : 0 ;
5210 if ( !aTitle || strcmp(aTitle,"tinyfd_query") )
5211 {
5212 lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen );
5213 }
5214
5215 if ( osascriptPresent( ) )
5216 {
5217 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return 1;}
5218
5219 strcpy( lDialogString , "osascript ");
5220 if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
5221 strcat( lDialogString , " -e 'try' -e 'display notification \"") ;
5222 if ( aMessage && strlen(aMessage) )
5223 {
5224 strcat(lDialogString, aMessage) ;
5225 }
5226 strcat(lDialogString, " \" ") ;
5227 if ( aTitle && strlen(aTitle) )
5228 {
5229 strcat(lDialogString, "with title \"") ;
5230 strcat(lDialogString, aTitle) ;
5231 strcat(lDialogString, "\" ") ;
5232 }
5233
5234 strcat( lDialogString, "' -e 'end try'") ;
5235 if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
5236 }
5237 else if ( tfd_kdialogPresent() )
5238 {
5239 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return 1;}
5240 strcpy( lDialogString , "kdialog" ) ;
5241
5242 if ( aIconType && strlen(aIconType) )
5243 {
5244 strcat( lDialogString , " --icon '" ) ;
5245 strcat( lDialogString , aIconType ) ;
5246 strcat( lDialogString , "'" ) ;
5247 }
5248 if ( aTitle && strlen(aTitle) )
5249 {
5250 strcat( lDialogString , " --title \"" ) ;
5251 strcat( lDialogString , aTitle ) ;
5252 strcat( lDialogString , "\"" ) ;
5253 }
5254
5255 strcat( lDialogString , " --passivepopup" ) ;
5256 strcat( lDialogString , " \"" ) ;
5257 if ( aMessage )
5258 {
5259 strcat( lDialogString , aMessage ) ;
5260 }
5261 strcat( lDialogString , " \" 5" ) ;
5262 }
5263 else if ( (tfd_zenity3Present()>=5) )
5264 {
5265 /* zenity 2.32 & 3.14 has the notification but with a bug: it doesnt return from it */
5266 /* zenity 3.8 show the notification as an alert ok cancel box */
5267 if ( tfd_zenity3Present()>=5 )
5268 {
5269 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return 1;}
5270 strcpy( lDialogString , "zenity" ) ;
5271 }
5272
5273 strcat( lDialogString , " --notification");
5274
5275 if ( aIconType && strlen( aIconType ) )
5276 {
5277 strcat( lDialogString , " --window-icon '");
5278 strcat( lDialogString , aIconType ) ;
5279 strcat( lDialogString , "'" ) ;
5280 }
5281
5282 strcat( lDialogString , " --text \"" ) ;
5283 if ( aTitle && strlen(aTitle) )
5284 {
5285 strcat(lDialogString, aTitle) ;
5286 strcat(lDialogString, "\n") ;
5287 }
5288 if ( aMessage && strlen( aMessage ) )
5289 {
5290 strcat( lDialogString , aMessage ) ;
5291 }
5292 strcat( lDialogString , " \"" ) ;
5293 }
5294 else if ( perlPresent() >= 2 )
5295 {
5296 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"perl-dbus");return 1;}
5297
5298 strcpy( lDialogString , "perl -e \"use Net::DBus;\
5299 my \\$sessionBus = Net::DBus->session;\
5300 my \\$notificationsService = \\$sessionBus->get_service('org.freedesktop.Notifications');\
5301 my \\$notificationsObject = \\$notificationsService->get_object('/org/freedesktop/Notifications',\
5302 'org.freedesktop.Notifications');");
5303
5304 sprintf( lDialogString + strlen(lDialogString) ,
5305 "my \\$notificationId;\\$notificationId = \\$notificationsObject->Notify(shift, 0, '%s', '%s', '%s', [], {}, -1);\" ",
5306 aIconType?aIconType:"", aTitle?aTitle:"", aMessage?aMessage:"" ) ;
5307 }
5308 else if ( pythonDbusPresent( ) )
5309 {
5310 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python-dbus");return 1;}
5311 strcpy( lDialogString , gPythonName ) ;
5312 strcat( lDialogString ," -c \"import dbus;bus=dbus.SessionBus();");
5313 strcat( lDialogString ,"notif=bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications');" ) ;
5314 strcat( lDialogString ,"notify=dbus.Interface(notif,'org.freedesktop.Notifications');" ) ;
5315 strcat( lDialogString ,"notify.Notify('',0,'" ) ;
5316 if ( aIconType && strlen(aIconType) )
5317 {
5318 strcat( lDialogString , aIconType ) ;
5319 }
5320 strcat(lDialogString, "','") ;
5321 if ( aTitle && strlen(aTitle) )
5322 {
5323 strcat(lDialogString, aTitle) ;
5324 }
5325 strcat(lDialogString, "','") ;
5326 if ( aMessage && strlen(aMessage) )
5327 {
5328 lpDialogString = lDialogString + strlen(lDialogString);
5329 tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
5330 }
5331 strcat(lDialogString, "','','',5000)\"") ;
5332 }
5333 else if ( notifysendPresent() )
5334 {
5335 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"notifysend");return 1;}
5336 strcpy( lDialogString , "notify-send" ) ;
5337 if ( aIconType && strlen(aIconType) )
5338 {
5339 strcat( lDialogString , " -i '" ) ;
5340 strcat( lDialogString , aIconType ) ;
5341 strcat( lDialogString , "'" ) ;
5342 }
5343 strcat( lDialogString , " \"" ) ;
5344 if ( aTitle && strlen(aTitle) )
5345 {
5346 strcat(lDialogString, aTitle) ;
5347 strcat( lDialogString , " | " ) ;
5348 }
5349 if ( aMessage && strlen(aMessage) )
5350 {
5351 tfd_replaceSubStr( aMessage , "\n\t" , " | " , lBuff ) ;
5352 tfd_replaceSubStr( aMessage , "\n" , " | " , lBuff ) ;
5353 tfd_replaceSubStr( aMessage , "\t" , " " , lBuff ) ;
5354 strcat(lDialogString, lBuff) ;
5355 }
5356 strcat( lDialogString , "\"" ) ;
5357 }
5358 else
5359 {
5360 return tinyfd_messageBox(aTitle, aMessage, "ok", aIconType, 0);
5361 }
5362
5363 if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
5364
5365 if ( ! ( lIn = popen( lDialogString , "r" ) ) )
5366 {
5367 free(lDialogString);
5368 return 0 ;
5369 }
5370
5371 pclose( lIn ) ;
5372 free(lDialogString);
5373 return 1;
5374 }
5375
5376
5377 /* returns NULL on cancel */
5378 char * tinyfd_inputBox(
5379 char const * aTitle , /* NULL or "" */
5380 char const * aMessage , /* NULL or "" (\n and \t have no effect) */
5381 char const * aDefaultInput ) /* "" , if NULL it's a passwordBox */
5382 {
5383 static char lBuff[MAX_PATH_OR_CMD];
5384 char * lDialogString = NULL;
5385 char * lpDialogString;
5386 FILE * lIn ;
5387 int lResult ;
5388 int lWasGdialog = 0 ;
5389 int lWasGraphicDialog = 0 ;
5390 int lWasXterm = 0 ;
5391 int lWasBasicXterm = 0 ;
5392 struct termios oldt ;
5393 struct termios newt ;
5394 char * lEOF;
5395 size_t lTitleLen ;
5396 size_t lMessageLen ;
5397
5398 if (!aTitle && !aMessage && !aDefaultInput) return lBuff; /* now I can fill lBuff from outside */
5399
5400 lBuff[0]='\0';
5401
5402 if (tfd_quoteDetected(aTitle)) return tinyfd_inputBox("INVALID TITLE WITH QUOTES", aMessage, aDefaultInput);
5403 if (tfd_quoteDetected(aMessage)) return tinyfd_inputBox(aTitle, "INVALID MESSAGE WITH QUOTES", aDefaultInput);
5404 if (tfd_quoteDetected(aDefaultInput)) return tinyfd_inputBox(aTitle, aMessage, "INVALID DEFAULT_INPUT WITH QUOTES");
5405
5406 lTitleLen = aTitle ? strlen(aTitle) : 0 ;
5407 lMessageLen = aMessage ? strlen(aMessage) : 0 ;
5408 if ( !aTitle || strcmp(aTitle,"tinyfd_query") )
5409 {
5410 lDialogString = (char *) malloc( MAX_PATH_OR_CMD + lTitleLen + lMessageLen );
5411 }
5412
5413 if ( osascriptPresent( ) )
5414 {
5415 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;}
5416 strcpy( lDialogString , "osascript ");
5417 if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
5418 strcat( lDialogString , " -e 'try' -e 'display dialog \"") ;
5419 if ( aMessage && strlen(aMessage) )
5420 {
5421 strcat(lDialogString, aMessage) ;
5422 }
5423 strcat(lDialogString, "\" ") ;
5424 strcat(lDialogString, "default answer \"") ;
5425 if ( aDefaultInput && strlen(aDefaultInput) )
5426 {
5427 strcat(lDialogString, aDefaultInput) ;
5428 }
5429 strcat(lDialogString, "\" ") ;
5430 if ( ! aDefaultInput )
5431 {
5432 strcat(lDialogString, "hidden answer true ") ;
5433 }
5434 if ( aTitle && strlen(aTitle) )
5435 {
5436 strcat(lDialogString, "with title \"") ;
5437 strcat(lDialogString, aTitle) ;
5438 strcat(lDialogString, "\" ") ;
5439 }
5440 strcat(lDialogString, "with icon note' ") ;
5441 strcat(lDialogString, "-e '\"1\" & text returned of result' " );
5442 strcat(lDialogString, "-e 'on error number -128' " ) ;
5443 strcat(lDialogString, "-e '0' " );
5444 strcat(lDialogString, "-e 'end try'") ;
5445 if ( ! osx9orBetter() ) strcat(lDialogString, " -e 'end tell'") ;
5446 }
5447 else if ( tfd_kdialogPresent() )
5448 {
5449 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;}
5450 strcpy( lDialogString , "szAnswer=$(kdialog" ) ;
5451
5452 if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
5453 {
5454 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
5455 }
5456
5457 if ( ! aDefaultInput )
5458 {
5459 strcat(lDialogString, " --password ") ;
5460 }
5461 else
5462 {
5463 strcat(lDialogString, " --inputbox ") ;
5464
5465 }
5466 strcat(lDialogString, "\"") ;
5467 if ( aMessage && strlen(aMessage) )
5468 {
5469 strcat(lDialogString, aMessage ) ;
5470 }
5471 strcat(lDialogString , "\" \"" ) ;
5472 if ( aDefaultInput && strlen(aDefaultInput) )
5473 {
5474 strcat(lDialogString, aDefaultInput ) ;
5475 }
5476 strcat(lDialogString , "\"" ) ;
5477 if ( aTitle && strlen(aTitle) )
5478 {
5479 strcat(lDialogString, " --title \"") ;
5480 strcat(lDialogString, aTitle) ;
5481 strcat(lDialogString, "\"") ;
5482 }
5483 strcat( lDialogString ,
5484 ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi");
5485 }
5486 else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
5487 {
5488 if ( tfd_zenityPresent() )
5489 {
5490 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;}
5491 strcpy( lDialogString , "szAnswer=$(zenity" ) ;
5492 if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
5493 {
5494 strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
5495 }
5496 }
5497 else if ( tfd_matedialogPresent() )
5498 {
5499 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;}
5500 strcpy( lDialogString , "szAnswer=$(matedialog" ) ;
5501 }
5502 else if ( tfd_shellementaryPresent() )
5503 {
5504 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;}
5505 strcpy( lDialogString , "szAnswer=$(shellementary" ) ;
5506 }
5507 else
5508 {
5509 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;}
5510 strcpy( lDialogString , "szAnswer=$(qarma" ) ;
5511 if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
5512 {
5513 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
5514 }
5515 }
5516 strcat( lDialogString ," --entry" ) ;
5517
5518 strcat(lDialogString, " --title=\"") ;
5519 if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ;
5520 strcat(lDialogString, "\"") ;
5521
5522 strcat(lDialogString, " --text=\"") ;
5523 if (aMessage && strlen(aMessage)) strcat(lDialogString, aMessage) ;
5524 strcat(lDialogString, "\"") ;
5525
5526 if ( aDefaultInput )
5527 {
5528 strcat(lDialogString, " --entry-text=\"") ;
5529 strcat(lDialogString, aDefaultInput) ;
5530 strcat(lDialogString, "\"") ;
5531 }
5532 else
5533 {
5534 strcat(lDialogString, " --hide-text") ;
5535 }
5536 if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
5537 strcat( lDialogString ,
5538 ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi");
5539 }
5540 else if (tfd_yadPresent())
5541 {
5542 if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; }
5543 strcpy(lDialogString, "szAnswer=$(yad --entry");
5544 if (aTitle && strlen(aTitle))
5545 {
5546 strcat(lDialogString, " --title=\"");
5547 strcat(lDialogString, aTitle);
5548 strcat(lDialogString, "\"");
5549 }
5550 if (aMessage && strlen(aMessage))
5551 {
5552 strcat(lDialogString, " --text=\"");
5553 strcat(lDialogString, aMessage);
5554 strcat(lDialogString, "\"");
5555 }
5556 if (aDefaultInput && strlen(aDefaultInput))
5557 {
5558 strcat(lDialogString, " --entry-text=\"");
5559 strcat(lDialogString, aDefaultInput);
5560 strcat(lDialogString, "\"");
5561 }
5562 else
5563 {
5564 strcat(lDialogString, " --hide-text");
5565 }
5566 if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
5567 strcat(lDialogString,
5568 ");if [ $? = 0 ];then echo 1$szAnswer;else echo 0$szAnswer;fi");
5569 }
5570 else if ( gxmessagePresent() || gmessagePresent() )
5571 {
5572 if ( gxmessagePresent() ) {
5573 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gxmessage");return (char *)1;}
5574 strcpy( lDialogString , "szAnswer=$(gxmessage -buttons Ok:1,Cancel:0 -center \"");
5575 }
5576 else
5577 {
5578 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gmessage");return (char *)1;}
5579 strcpy( lDialogString , "szAnswer=$(gmessage -buttons Ok:1,Cancel:0 -center \"");
5580 }
5581
5582 if ( aMessage && strlen(aMessage) )
5583 {
5584 strcat( lDialogString , aMessage ) ;
5585 }
5586 strcat(lDialogString, "\"" ) ;
5587 if ( aTitle && strlen(aTitle) )
5588 {
5589 strcat( lDialogString , " -title \"");
5590 strcat( lDialogString , aTitle ) ;
5591 strcat(lDialogString, "\" " ) ;
5592 }
5593 strcat(lDialogString, " -entrytext \"" ) ;
5594 if ( aDefaultInput && strlen(aDefaultInput) )
5595 {
5596 strcat( lDialogString , aDefaultInput ) ;
5597 }
5598 strcat(lDialogString, "\"" ) ;
5599 strcat( lDialogString , ");echo $?$szAnswer");
5600 }
5601 else if ( !gdialogPresent() && !xdialogPresent() && tkinter3Present( ) )
5602 {
5603 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;}
5604 strcpy( lDialogString , gPython3Name ) ;
5605 strcat( lDialogString ,
5606 " -S -c \"import tkinter; from tkinter import simpledialog;root=tkinter.Tk();root.withdraw();");
5607 strcat( lDialogString ,"res=simpledialog.askstring(" ) ;
5608 if ( aTitle && strlen(aTitle) )
5609 {
5610 strcat(lDialogString, "title='") ;
5611 strcat(lDialogString, aTitle) ;
5612 strcat(lDialogString, "',") ;
5613 }
5614 if ( aMessage && strlen(aMessage) )
5615 {
5616
5617 strcat(lDialogString, "prompt='") ;
5618 lpDialogString = lDialogString + strlen(lDialogString);
5619 tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
5620 strcat(lDialogString, "',") ;
5621 }
5622 if ( aDefaultInput )
5623 {
5624 if ( strlen(aDefaultInput) )
5625 {
5626 strcat(lDialogString, "initialvalue='") ;
5627 strcat(lDialogString, aDefaultInput) ;
5628 strcat(lDialogString, "',") ;
5629 }
5630 }
5631 else
5632 {
5633 strcat(lDialogString, "show='*'") ;
5634 }
5635 strcat(lDialogString, ");\nif res is None :\n\tprint(0)");
5636 strcat(lDialogString, "\nelse :\n\tprint('1'+res)\n\"" ) ;
5637 }
5638 else if ( !gdialogPresent() && !xdialogPresent() && tkinter2Present( ) )
5639 {
5640 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;}
5641 strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
5642 strcat( lDialogString , gPython2Name ) ;
5643 if ( ! isTerminalRunning( ) && tfd_isDarwin( ) )
5644 {
5645 strcat( lDialogString , " -i" ) ; /* for osx without console */
5646 }
5647
5648 strcat( lDialogString ,
5649 " -S -c \"import Tkinter,tkSimpleDialog;root=Tkinter.Tk();root.withdraw();");
5650
5651 if ( tfd_isDarwin( ) )
5652 {
5653 strcat( lDialogString ,
5654 "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
5655 frontmost of process \\\"Python\\\" to true' ''');");
5656 }
5657
5658 strcat( lDialogString ,"res=tkSimpleDialog.askstring(" ) ;
5659 if ( aTitle && strlen(aTitle) )
5660 {
5661 strcat(lDialogString, "title='") ;
5662 strcat(lDialogString, aTitle) ;
5663 strcat(lDialogString, "',") ;
5664 }
5665 if ( aMessage && strlen(aMessage) )
5666 {
5667
5668 strcat(lDialogString, "prompt='") ;
5669 lpDialogString = lDialogString + strlen(lDialogString);
5670 tfd_replaceSubStr( aMessage , "\n" , "\\n" , lpDialogString ) ;
5671 strcat(lDialogString, "',") ;
5672 }
5673 if ( aDefaultInput )
5674 {
5675 if ( strlen(aDefaultInput) )
5676 {
5677 strcat(lDialogString, "initialvalue='") ;
5678 strcat(lDialogString, aDefaultInput) ;
5679 strcat(lDialogString, "',") ;
5680 }
5681 }
5682 else
5683 {
5684 strcat(lDialogString, "show='*'") ;
5685 }
5686 strcat(lDialogString, ");\nif res is None :\n\tprint 0");
5687 strcat(lDialogString, "\nelse :\n\tprint '1'+res\n\"" ) ;
5688 }
5689 else if ( gdialogPresent() || xdialogPresent() || dialogName() || whiptailPresent() )
5690 {
5691 if ( gdialogPresent( ) )
5692 {
5693 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"gdialog");return (char *)1;}
5694 lWasGraphicDialog = 1 ;
5695 lWasGdialog = 1 ;
5696 strcpy( lDialogString , "(gdialog " ) ;
5697 }
5698 else if ( xdialogPresent( ) )
5699 {
5700 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;}
5701 lWasGraphicDialog = 1 ;
5702 strcpy( lDialogString , "(Xdialog " ) ;
5703 }
5704 else if ( dialogName( ) )
5705 {
5706 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
5707 if ( isTerminalRunning( ) )
5708 {
5709 strcpy( lDialogString , "(dialog " ) ;
5710 }
5711 else
5712 {
5713 lWasXterm = 1 ;
5714 strcpy( lDialogString , terminalName() ) ;
5715 strcat( lDialogString , "'(" ) ;
5716 strcat( lDialogString , dialogName() ) ;
5717 strcat( lDialogString , " " ) ;
5718 }
5719 }
5720 else if ( isTerminalRunning( ) )
5721 {
5722 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char *)0;}
5723 strcpy( lDialogString , "(whiptail " ) ;
5724 }
5725 else
5726 {
5727 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"whiptail");return (char *)0;}
5728 lWasXterm = 1 ;
5729 strcpy( lDialogString , terminalName() ) ;
5730 strcat( lDialogString , "'(whiptail " ) ;
5731 }
5732
5733 if ( aTitle && strlen(aTitle) )
5734 {
5735 strcat(lDialogString, "--title \"") ;
5736 strcat(lDialogString, aTitle) ;
5737 strcat(lDialogString, "\" ") ;
5738 }
5739
5740 if ( !xdialogPresent() && !gdialogPresent() )
5741 {
5742 strcat(lDialogString, "--backtitle \"") ;
5743 strcat(lDialogString, "tab: move focus") ;
5744 if ( ! aDefaultInput && !lWasGdialog )
5745 {
5746 strcat(lDialogString, " (sometimes nothing, no blink nor star, is shown in text field)") ;
5747 }
5748 strcat(lDialogString, "\" ") ;
5749 }
5750
5751 if ( aDefaultInput || lWasGdialog )
5752 {
5753 strcat( lDialogString , "--inputbox" ) ;
5754 }
5755 else
5756 {
5757 if ( !lWasGraphicDialog && dialogName() && isDialogVersionBetter09b() )
5758 {
5759 strcat( lDialogString , "--insecure " ) ;
5760 }
5761 strcat( lDialogString , "--passwordbox" ) ;
5762 }
5763 strcat( lDialogString , " \"" ) ;
5764 if ( aMessage && strlen(aMessage) )
5765 {
5766 strcat(lDialogString, aMessage) ;
5767 }
5768 strcat(lDialogString,"\" 10 60 ") ;
5769 if ( aDefaultInput && strlen(aDefaultInput) )
5770 {
5771 strcat(lDialogString, "\"") ;
5772 strcat(lDialogString, aDefaultInput) ;
5773 strcat(lDialogString, "\" ") ;
5774 }
5775 if ( lWasGraphicDialog )
5776 {
5777 strcat(lDialogString,") 2>/tmp/tinyfd.txt;\
5778 if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
5779 tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
5780 }
5781 else
5782 {
5783 strcat(lDialogString,">/dev/tty ) 2>/tmp/tinyfd.txt;\
5784 if [ $? = 0 ];then tinyfdBool=1;else tinyfdBool=0;fi;\
5785 tinyfdRes=$(cat /tmp/tinyfd.txt);echo $tinyfdBool$tinyfdRes") ;
5786
5787 if ( lWasXterm )
5788 {
5789 strcat(lDialogString," >/tmp/tinyfd0.txt';cat /tmp/tinyfd0.txt");
5790 }
5791 else
5792 {
5793 strcat(lDialogString, "; clear >/dev/tty") ;
5794 }
5795 }
5796 }
5797 else if ( ! isTerminalRunning( ) && terminalName() )
5798 {
5799 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;}
5800 lWasBasicXterm = 1 ;
5801 strcpy( lDialogString , terminalName() ) ;
5802 strcat( lDialogString , "'" ) ;
5803 if ( !gWarningDisplayed && !tinyfd_forceConsole)
5804 {
5805 gWarningDisplayed = 1 ;
5806 tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0);
5807 }
5808 if ( aTitle && strlen(aTitle) && !tinyfd_forceConsole)
5809 {
5810 strcat( lDialogString , "echo \"" ) ;
5811 strcat( lDialogString, aTitle) ;
5812 strcat( lDialogString , "\";echo;" ) ;
5813 }
5814
5815 strcat( lDialogString , "echo \"" ) ;
5816 if ( aMessage && strlen(aMessage) )
5817 {
5818 strcat( lDialogString, aMessage) ;
5819 }
5820 strcat( lDialogString , "\";read " ) ;
5821 if ( ! aDefaultInput )
5822 {
5823 strcat( lDialogString , "-s " ) ;
5824 }
5825 strcat( lDialogString , "-p \"" ) ;
5826 strcat( lDialogString , "(esc+enter to cancel): \" ANSWER " ) ;
5827 strcat( lDialogString , ";echo 1$ANSWER >/tmp/tinyfd.txt';" ) ;
5828 strcat( lDialogString , "cat -v /tmp/tinyfd.txt");
5829 }
5830 else if ( !gWarningDisplayed && ! isTerminalRunning( ) && ! terminalName() ) {
5831 gWarningDisplayed = 1 ;
5832 tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0);
5833 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"no_solution");return (char *)0;}
5834 free(lDialogString);
5835 return NULL;
5836 }
5837 else
5838 {
5839 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"basicinput");return (char *)0;}
5840 if ( !gWarningDisplayed && !tinyfd_forceConsole)
5841 {
5842 gWarningDisplayed = 1 ;
5843 tinyfd_messageBox(gTitle,tinyfd_needs,"ok","warning",0);
5844 }
5845 if ( aTitle && strlen(aTitle) )
5846 {
5847 printf("\n%s\n", aTitle);
5848 }
5849 if ( aMessage && strlen(aMessage) )
5850 {
5851 printf("\n%s\n",aMessage);
5852 }
5853 printf("(esc+enter to cancel): "); fflush(stdout);
5854 if ( ! aDefaultInput )
5855 {
5856 tcgetattr(STDIN_FILENO, & oldt) ;
5857 newt = oldt ;
5858 newt.c_lflag &= ~ECHO ;
5859 tcsetattr(STDIN_FILENO, TCSANOW, & newt);
5860 }
5861
5862 lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin);
5863 /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */
5864 if ( ! lEOF || (lBuff[0] == '\0') )
5865 {
5866 free(lDialogString);
5867 return NULL;
5868 }
5869
5870 if ( lBuff[0] == '\n' )
5871 {
5872 lEOF = fgets(lBuff, MAX_PATH_OR_CMD, stdin);
5873 /* printf("lbuff<%c><%d>\n",lBuff[0],lBuff[0]); */
5874 if ( ! lEOF || (lBuff[0] == '\0') )
5875 {
5876 free(lDialogString);
5877 return NULL;
5878 }
5879 }
5880
5881 if ( ! aDefaultInput )
5882 {
5883 tcsetattr(STDIN_FILENO, TCSANOW, & oldt);
5884 printf("\n");
5885 }
5886 printf("\n");
5887 if ( strchr(lBuff,27) )
5888 {
5889 free(lDialogString);
5890 return NULL ;
5891 }
5892 if ( lBuff[strlen( lBuff ) -1] == '\n' )
5893 {
5894 lBuff[strlen( lBuff ) -1] = '\0' ;
5895 }
5896 free(lDialogString);
5897 return lBuff ;
5898 }
5899
5900 if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
5901 lIn = popen( lDialogString , "r" );
5902 if ( ! lIn )
5903 {
5904 if ( fileExists("/tmp/tinyfd.txt") )
5905 {
5906 wipefile("/tmp/tinyfd.txt");
5907 remove("/tmp/tinyfd.txt");
5908 }
5909 if ( fileExists("/tmp/tinyfd0.txt") )
5910 {
5911 wipefile("/tmp/tinyfd0.txt");
5912 remove("/tmp/tinyfd0.txt");
5913 }
5914 free(lDialogString);
5915 return NULL ;
5916 }
5917 while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
5918 {}
5919
5920 pclose( lIn ) ;
5921
5922 if ( fileExists("/tmp/tinyfd.txt") )
5923 {
5924 wipefile("/tmp/tinyfd.txt");
5925 remove("/tmp/tinyfd.txt");
5926 }
5927 if ( fileExists("/tmp/tinyfd0.txt") )
5928 {
5929 wipefile("/tmp/tinyfd0.txt");
5930 remove("/tmp/tinyfd0.txt");
5931 }
5932
5933 /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */
5934 /* printf( "lBuff0: %s\n" , lBuff ) ; */
5935 if ( lBuff[strlen( lBuff ) -1] == '\n' )
5936 {
5937 lBuff[strlen( lBuff ) -1] = '\0' ;
5938 }
5939 /* printf( "lBuff1: %s len: %lu \n" , lBuff , strlen(lBuff) ) ; */
5940 if ( lWasBasicXterm )
5941 {
5942 if ( strstr(lBuff,"^[") ) /* esc was pressed */
5943 {
5944 free(lDialogString);
5945 return NULL ;
5946 }
5947 }
5948
5949 lResult = strncmp( lBuff , "1" , 1) ? 0 : 1 ;
5950 /* printf( "lResult: %d \n" , lResult ) ; */
5951 if ( ! lResult )
5952 {
5953 free(lDialogString);
5954 return NULL ;
5955 }
5956
5957 /* printf( "lBuff+1: %s\n" , lBuff+1 ) ; */
5958 free(lDialogString);
5959 return lBuff+1 ;
5960 }
5961
5962
5963 char * tinyfd_saveFileDialog(
5964 char const * aTitle , /* NULL or "" */
5965 char const * aDefaultPathAndFile , /* NULL or "" */
5966 int aNumOfFilterPatterns , /* 0 */
5967 char const * const * aFilterPatterns , /* NULL or {"*.txt","*.doc"} */
5968 char const * aSingleFilterDescription ) /* NULL or "text files" */
5969 {
5970 static char lBuff[MAX_PATH_OR_CMD] ;
5971 char lDialogString[MAX_PATH_OR_CMD] ;
5972 char lString[MAX_PATH_OR_CMD] ;
5973 int i ;
5974 int lWasGraphicDialog = 0 ;
5975 int lWasXterm = 0 ;
5976 char * p ;
5977 char * lPointerInputBox ;
5978 FILE * lIn ;
5979 lBuff[0]='\0';
5980
5981 if ( ! aFilterPatterns ) aNumOfFilterPatterns = 0 ;
5982 if (tfd_quoteDetected(aTitle)) return tinyfd_saveFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
5983 if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_saveFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription);
5984 if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_saveFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES");
5985 for (i = 0; i < aNumOfFilterPatterns; i++)
5986 {
5987 if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_saveFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL);
5988 }
5989
5990 if ( osascriptPresent( ) )
5991 {
5992 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;}
5993 strcpy( lDialogString , "osascript ");
5994 if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"Finder\"' -e 'Activate'");
5995 strcat( lDialogString , " -e 'try' -e 'POSIX path of ( choose file name " );
5996 if ( aTitle && strlen(aTitle) )
5997 {
5998 strcat(lDialogString, "with prompt \"") ;
5999 strcat(lDialogString, aTitle) ;
6000 strcat(lDialogString, "\" ") ;
6001 }
6002 getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ;
6003 if ( strlen(lString) )
6004 {
6005 strcat(lDialogString, "default location \"") ;
6006 strcat(lDialogString, lString ) ;
6007 strcat(lDialogString , "\" " ) ;
6008 }
6009 getLastName( lString , aDefaultPathAndFile ) ;
6010 if ( strlen(lString) )
6011 {
6012 strcat(lDialogString, "default name \"") ;
6013 strcat(lDialogString, lString ) ;
6014 strcat(lDialogString , "\" " ) ;
6015 }
6016 strcat( lDialogString , ")' " ) ;
6017 strcat(lDialogString, "-e 'on error number -128' " ) ;
6018 strcat(lDialogString, "-e 'end try'") ;
6019 if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
6020 }
6021 else if ( tfd_kdialogPresent() )
6022 {
6023 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;}
6024
6025 strcpy( lDialogString , "kdialog" ) ;
6026 if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
6027 {
6028 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
6029 }
6030 strcat( lDialogString , " --getsavefilename " ) ;
6031
6032 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
6033 {
6034 if ( aDefaultPathAndFile[0] != '/' )
6035 {
6036 strcat(lDialogString, "$PWD/") ;
6037 }
6038 strcat(lDialogString, "\"") ;
6039 strcat(lDialogString, aDefaultPathAndFile ) ;
6040 strcat(lDialogString , "\"" ) ;
6041 }
6042 else
6043 {
6044 strcat(lDialogString, "$PWD/") ;
6045 }
6046
6047 if ( aNumOfFilterPatterns > 0 )
6048 {
6049 strcat(lDialogString , " \"" ) ;
6050 strcat( lDialogString , aFilterPatterns[0] ) ;
6051 for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ )
6052 {
6053 strcat( lDialogString , " " ) ;
6054 strcat( lDialogString , aFilterPatterns[i] ) ;
6055 }
6056 if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
6057 {
6058 strcat( lDialogString , " | " ) ;
6059 strcat( lDialogString , aSingleFilterDescription ) ;
6060 }
6061 strcat( lDialogString , "\"" ) ;
6062 }
6063 if ( aTitle && strlen(aTitle) )
6064 {
6065 strcat(lDialogString, " --title \"") ;
6066 strcat(lDialogString, aTitle) ;
6067 strcat(lDialogString, "\"") ;
6068 }
6069 }
6070 else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
6071 {
6072 if ( tfd_zenityPresent() )
6073 {
6074 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;}
6075 strcpy( lDialogString , "zenity" ) ;
6076 if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
6077 {
6078 strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
6079 }
6080 }
6081 else if ( tfd_matedialogPresent() )
6082 {
6083 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;}
6084 strcpy( lDialogString , "matedialog" ) ;
6085 }
6086 else if ( tfd_shellementaryPresent() )
6087 {
6088 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;}
6089 strcpy( lDialogString , "shellementary" ) ;
6090 }
6091 else
6092 {
6093 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;}
6094 strcpy( lDialogString , "qarma" ) ;
6095 if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
6096 {
6097 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
6098 }
6099 }
6100 strcat(lDialogString, " --file-selection --save --confirm-overwrite" ) ;
6101
6102 strcat(lDialogString, " --title=\"") ;
6103 if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ;
6104 strcat(lDialogString, "\"") ;
6105
6106 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
6107 {
6108 strcat(lDialogString, " --filename=\"") ;
6109 strcat(lDialogString, aDefaultPathAndFile) ;
6110 strcat(lDialogString, "\"") ;
6111 }
6112 if ( aNumOfFilterPatterns > 0 )
6113 {
6114 strcat( lDialogString , " --file-filter='" ) ;
6115 if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
6116 {
6117 strcat( lDialogString , aSingleFilterDescription ) ;
6118 strcat( lDialogString , " |" ) ;
6119 }
6120 for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
6121 {
6122 strcat( lDialogString , " " ) ;
6123 strcat( lDialogString , aFilterPatterns[i] ) ;
6124 }
6125 strcat( lDialogString , "' --file-filter='All files | *'" ) ;
6126 }
6127 if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
6128 }
6129 else if (tfd_yadPresent())
6130 {
6131 if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; }
6132 strcpy(lDialogString, "yad --file-selection --save --confirm-overwrite");
6133 if (aTitle && strlen(aTitle))
6134 {
6135 strcat(lDialogString, " --title=\"");
6136 strcat(lDialogString, aTitle);
6137 strcat(lDialogString, "\"");
6138 }
6139 if (aDefaultPathAndFile && strlen(aDefaultPathAndFile))
6140 {
6141 strcat(lDialogString, " --filename=\"");
6142 strcat(lDialogString, aDefaultPathAndFile);
6143 strcat(lDialogString, "\"");
6144 }
6145 if (aNumOfFilterPatterns > 0)
6146 {
6147 strcat(lDialogString, " --file-filter='");
6148 if (aSingleFilterDescription && strlen(aSingleFilterDescription))
6149 {
6150 strcat(lDialogString, aSingleFilterDescription);
6151 strcat(lDialogString, " |");
6152 }
6153 for (i = 0; i < aNumOfFilterPatterns; i++)
6154 {
6155 strcat(lDialogString, " ");
6156 strcat(lDialogString, aFilterPatterns[i]);
6157 }
6158 strcat(lDialogString, "' --file-filter='All files | *'");
6159 }
6160 if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
6161 }
6162 else if ( !xdialogPresent() && tkinter3Present( ) )
6163 {
6164 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;}
6165 strcpy( lDialogString , gPython3Name ) ;
6166 strcat( lDialogString ,
6167 " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();");
6168 strcat( lDialogString , "res=filedialog.asksaveasfilename(");
6169 if ( aTitle && strlen(aTitle) )
6170 {
6171 strcat(lDialogString, "title='") ;
6172 strcat(lDialogString, aTitle) ;
6173 strcat(lDialogString, "',") ;
6174 }
6175 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
6176 {
6177 getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ;
6178 if ( strlen(lString) )
6179 {
6180 strcat(lDialogString, "initialdir='") ;
6181 strcat(lDialogString, lString ) ;
6182 strcat(lDialogString , "'," ) ;
6183 }
6184 getLastName( lString , aDefaultPathAndFile ) ;
6185 if ( strlen(lString) )
6186 {
6187 strcat(lDialogString, "initialfile='") ;
6188 strcat(lDialogString, lString ) ;
6189 strcat(lDialogString , "'," ) ;
6190 }
6191 }
6192 if ( ( aNumOfFilterPatterns > 1 )
6193 || ( (aNumOfFilterPatterns == 1) /* test because poor osx behaviour */
6194 && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) )
6195 {
6196 strcat(lDialogString , "filetypes=(" ) ;
6197 strcat( lDialogString , "('" ) ;
6198 if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
6199 {
6200 strcat( lDialogString , aSingleFilterDescription ) ;
6201 }
6202 strcat( lDialogString , "',(" ) ;
6203 for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
6204 {
6205 strcat( lDialogString , "'" ) ;
6206 strcat( lDialogString , aFilterPatterns[i] ) ;
6207 strcat( lDialogString , "'," ) ;
6208 }
6209 strcat( lDialogString , "))," ) ;
6210 strcat( lDialogString , "('All files','*'))" ) ;
6211 }
6212 strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint(res)\n\"" ) ;
6213 }
6214 else if ( !xdialogPresent() && tkinter2Present( ) )
6215 {
6216 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;}
6217 strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
6218 strcat( lDialogString , gPython2Name ) ;
6219 if ( ! isTerminalRunning( ) && tfd_isDarwin( ))
6220 {
6221 strcat( lDialogString , " -i" ) ; /* for osx without console */
6222 }
6223 strcat( lDialogString ,
6224 " -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();");
6225
6226 if ( tfd_isDarwin( ) )
6227 {
6228 strcat( lDialogString ,
6229 "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set\
6230 frontmost of process \\\"Python\\\" to true' ''');");
6231 }
6232
6233 strcat( lDialogString , "res=tkFileDialog.asksaveasfilename(");
6234 if ( aTitle && strlen(aTitle) )
6235 {
6236 strcat(lDialogString, "title='") ;
6237 strcat(lDialogString, aTitle) ;
6238 strcat(lDialogString, "',") ;
6239 }
6240 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
6241 {
6242 getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ;
6243 if ( strlen(lString) )
6244 {
6245 strcat(lDialogString, "initialdir='") ;
6246 strcat(lDialogString, lString ) ;
6247 strcat(lDialogString , "'," ) ;
6248 }
6249 getLastName( lString , aDefaultPathAndFile ) ;
6250 if ( strlen(lString) )
6251 {
6252 strcat(lDialogString, "initialfile='") ;
6253 strcat(lDialogString, lString ) ;
6254 strcat(lDialogString , "'," ) ;
6255 }
6256 }
6257 if ( ( aNumOfFilterPatterns > 1 )
6258 || ( (aNumOfFilterPatterns == 1) /* test because poor osx behaviour */
6259 && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) )
6260 {
6261 strcat(lDialogString , "filetypes=(" ) ;
6262 strcat( lDialogString , "('" ) ;
6263 if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
6264 {
6265 strcat( lDialogString , aSingleFilterDescription ) ;
6266 }
6267 strcat( lDialogString , "',(" ) ;
6268 for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
6269 {
6270 strcat( lDialogString , "'" ) ;
6271 strcat( lDialogString , aFilterPatterns[i] ) ;
6272 strcat( lDialogString , "'," ) ;
6273 }
6274 strcat( lDialogString , "))," ) ;
6275 strcat( lDialogString , "('All files','*'))" ) ;
6276 }
6277 strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint res \n\"" ) ;
6278 }
6279 else if ( xdialogPresent() || dialogName() )
6280 {
6281 if ( xdialogPresent( ) )
6282 {
6283 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;}
6284 lWasGraphicDialog = 1 ;
6285 strcpy( lDialogString , "(Xdialog " ) ;
6286 }
6287 else if ( isTerminalRunning( ) )
6288 {
6289 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
6290 strcpy( lDialogString , "(dialog " ) ;
6291 }
6292 else
6293 {
6294 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
6295 lWasXterm = 1 ;
6296 strcpy( lDialogString , terminalName() ) ;
6297 strcat( lDialogString , "'(" ) ;
6298 strcat( lDialogString , dialogName() ) ;
6299 strcat( lDialogString , " " ) ;
6300 }
6301
6302 if ( aTitle && strlen(aTitle) )
6303 {
6304 strcat(lDialogString, "--title \"") ;
6305 strcat(lDialogString, aTitle) ;
6306 strcat(lDialogString, "\" ") ;
6307 }
6308
6309 if ( !xdialogPresent() && !gdialogPresent() )
6310 {
6311 strcat(lDialogString, "--backtitle \"") ;
6312 strcat(lDialogString,
6313 "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
6314 strcat(lDialogString, "\" ") ;
6315 }
6316
6317 strcat( lDialogString , "--fselect \"" ) ;
6318 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
6319 {
6320 if ( ! strchr(aDefaultPathAndFile, '/') )
6321 {
6322 strcat(lDialogString, "./") ;
6323 }
6324 strcat(lDialogString, aDefaultPathAndFile) ;
6325 }
6326 else if ( ! isTerminalRunning( ) && !lWasGraphicDialog )
6327 {
6328 strcat(lDialogString, getenv("HOME")) ;
6329 strcat(lDialogString, "/") ;
6330 }
6331 else
6332 {
6333 strcat(lDialogString, "./") ;
6334 }
6335
6336 if ( lWasGraphicDialog )
6337 {
6338 strcat(lDialogString, "\" 0 60 ) 2>&1 ") ;
6339 }
6340 else
6341 {
6342 strcat(lDialogString, "\" 0 60 >/dev/tty) ") ;
6343 if ( lWasXterm )
6344 {
6345 strcat( lDialogString ,
6346 "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
6347 }
6348 else
6349 {
6350 strcat(lDialogString, "2>&1 ; clear >/dev/tty") ;
6351 }
6352 }
6353 }
6354 else
6355 {
6356 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);}
6357 strcpy(lBuff, "Save file in ");
6358 strcat(lBuff, getCurDir());
6359 lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
6360 if (lPointerInputBox) strcpy(lString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
6361 p = tinyfd_inputBox(aTitle, lBuff, "");
6362 if (p) strcpy(lBuff, p); else lBuff[0] = '\0';
6363 if (lPointerInputBox) strcpy(lPointerInputBox, lString); /* restore its previous content to tinyfd_inputBox */
6364 p = lBuff;
6365
6366 getPathWithoutFinalSlash( lString , p ) ;
6367 if ( strlen( lString ) && ! dirExists( lString ) )
6368 {
6369 return NULL ;
6370 }
6371 getLastName(lString,p);
6372 if ( ! strlen(lString) )
6373 {
6374 return NULL;
6375 }
6376 return p ;
6377 }
6378
6379 if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
6380 if ( ! ( lIn = popen( lDialogString , "r" ) ) )
6381 {
6382 return NULL ;
6383 }
6384 while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
6385 {}
6386 pclose( lIn ) ;
6387 if ( lBuff[strlen( lBuff ) -1] == '\n' )
6388 {
6389 lBuff[strlen( lBuff ) -1] = '\0' ;
6390 }
6391 /* printf( "lBuff: %s\n" , lBuff ) ; */
6392 if ( ! strlen(lBuff) )
6393 {
6394 return NULL;
6395 }
6396 getPathWithoutFinalSlash( lString , lBuff ) ;
6397 if ( strlen( lString ) && ! dirExists( lString ) )
6398 {
6399 return NULL ;
6400 }
6401 getLastName(lString,lBuff);
6402 if ( ! filenameValid(lString) )
6403 {
6404 return NULL;
6405 }
6406 return lBuff ;
6407 }
6408
6409
6410 /* in case of multiple files, the separator is | */
6411 char * tinyfd_openFileDialog(
6412 char const * aTitle , /* NULL or "" */
6413 char const * aDefaultPathAndFile , /* NULL or "" */
6414 int aNumOfFilterPatterns , /* 0 */
6415 char const * const * aFilterPatterns , /* NULL or {"*.jpg","*.png"} */
6416 char const * aSingleFilterDescription , /* NULL or "image files" */
6417 int aAllowMultipleSelects ) /* 0 or 1 */
6418 {
6419 char lDialogString[MAX_PATH_OR_CMD] ;
6420 char lString[MAX_PATH_OR_CMD] ;
6421 int i ;
6422 FILE * lIn ;
6423 char * p ;
6424 char * lPointerInputBox ;
6425 int lWasKdialog = 0 ;
6426 int lWasGraphicDialog = 0 ;
6427 int lWasXterm = 0 ;
6428 size_t lFullBuffLen ;
6429 static char * lBuff = NULL;
6430
6431 if ( ! aFilterPatterns ) aNumOfFilterPatterns = 0 ;
6432 if (tfd_quoteDetected(aTitle)) return tinyfd_openFileDialog("INVALID TITLE WITH QUOTES", aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
6433 if (tfd_quoteDetected(aDefaultPathAndFile)) return tinyfd_openFileDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES", aNumOfFilterPatterns, aFilterPatterns, aSingleFilterDescription, aAllowMultipleSelects);
6434 if (tfd_quoteDetected(aSingleFilterDescription)) return tinyfd_openFileDialog(aTitle, aDefaultPathAndFile, aNumOfFilterPatterns, aFilterPatterns, "INVALID FILTER_DESCRIPTION WITH QUOTES", aAllowMultipleSelects);
6435 for (i = 0; i < aNumOfFilterPatterns; i++)
6436 {
6437 if (tfd_quoteDetected(aFilterPatterns[i])) return tinyfd_openFileDialog("INVALID FILTER_PATTERN WITH QUOTES", aDefaultPathAndFile, 0, NULL, NULL, aAllowMultipleSelects);
6438 }
6439
6440 free(lBuff);
6441 if (aTitle&&!strcmp(aTitle,"tinyfd_query"))
6442 {
6443 lBuff = NULL;
6444 }
6445 else
6446 {
6447 if (aAllowMultipleSelects)
6448 {
6449 lFullBuffLen = MAX_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1;
6450 lBuff = (char *)(malloc(lFullBuffLen * sizeof(char)));
6451 if (!lBuff)
6452 {
6453 lFullBuffLen = LOW_MULTIPLE_FILES * MAX_PATH_OR_CMD + 1;
6454 lBuff = (char *)( malloc( lFullBuffLen * sizeof(char)));
6455 }
6456 }
6457 else
6458 {
6459 lFullBuffLen = MAX_PATH_OR_CMD + 1;
6460 lBuff = (char *)(malloc(lFullBuffLen * sizeof(char)));
6461 }
6462 if (!lBuff) return NULL;
6463 lBuff[0]='\0';
6464 }
6465
6466 if ( osascriptPresent( ) )
6467 {
6468 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;}
6469 strcpy( lDialogString , "osascript ");
6470 if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
6471 strcat( lDialogString , " -e 'try' -e '" );
6472 if ( ! aAllowMultipleSelects )
6473 {
6474
6475
6476 strcat( lDialogString , "POSIX path of ( " );
6477 }
6478 else
6479 {
6480 strcat( lDialogString , "set mylist to " );
6481 }
6482 strcat( lDialogString , "choose file " );
6483 if ( aTitle && strlen(aTitle) )
6484 {
6485 strcat(lDialogString, "with prompt \"") ;
6486 strcat(lDialogString, aTitle) ;
6487 strcat(lDialogString, "\" ") ;
6488 }
6489 getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ;
6490 if ( strlen(lString) )
6491 {
6492 strcat(lDialogString, "default location \"") ;
6493 strcat(lDialogString, lString ) ;
6494 strcat(lDialogString , "\" " ) ;
6495 }
6496 if ( aNumOfFilterPatterns > 0 )
6497 {
6498 strcat(lDialogString , "of type {\"" );
6499 strcat( lDialogString , aFilterPatterns[0] + 2 ) ;
6500 strcat( lDialogString , "\"" ) ;
6501 for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ )
6502 {
6503 strcat( lDialogString , ",\"" ) ;
6504 strcat( lDialogString , aFilterPatterns[i] + 2) ;
6505 strcat( lDialogString , "\"" ) ;
6506 }
6507 strcat( lDialogString , "} " ) ;
6508 }
6509 if ( aAllowMultipleSelects )
6510 {
6511 strcat( lDialogString , "multiple selections allowed true ' " ) ;
6512 strcat( lDialogString ,
6513 "-e 'set mystring to POSIX path of item 1 of mylist' " );
6514 strcat( lDialogString ,
6515 "-e 'repeat with i from 2 to the count of mylist' " );
6516 strcat( lDialogString , "-e 'set mystring to mystring & \"|\"' " );
6517 strcat( lDialogString ,
6518 "-e 'set mystring to mystring & POSIX path of item i of mylist' " );
6519 strcat( lDialogString , "-e 'end repeat' " );
6520 strcat( lDialogString , "-e 'mystring' " );
6521 }
6522 else
6523 {
6524 strcat( lDialogString , ")' " ) ;
6525 }
6526 strcat(lDialogString, "-e 'on error number -128' " ) ;
6527 strcat(lDialogString, "-e 'end try'") ;
6528 if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
6529 }
6530 else if ( tfd_kdialogPresent() )
6531 {
6532 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;}
6533 lWasKdialog = 1 ;
6534
6535 strcpy( lDialogString , "kdialog" ) ;
6536 if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
6537 {
6538 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
6539 }
6540 strcat( lDialogString , " --getopenfilename " ) ;
6541
6542 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
6543 {
6544 if ( aDefaultPathAndFile[0] != '/' )
6545 {
6546 strcat(lDialogString, "$PWD/") ;
6547 }
6548 strcat(lDialogString, "\"") ;
6549 strcat(lDialogString, aDefaultPathAndFile ) ;
6550 strcat(lDialogString , "\"" ) ;
6551 }
6552 else
6553 {
6554 strcat(lDialogString, "$PWD/") ;
6555 }
6556
6557 if ( aNumOfFilterPatterns > 0 )
6558 {
6559 strcat(lDialogString , " \"" ) ;
6560 strcat( lDialogString , aFilterPatterns[0] ) ;
6561 for ( i = 1 ; i < aNumOfFilterPatterns ; i ++ )
6562 {
6563 strcat( lDialogString , " " ) ;
6564 strcat( lDialogString , aFilterPatterns[i] ) ;
6565 }
6566 if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
6567 {
6568 strcat( lDialogString , " | " ) ;
6569 strcat( lDialogString , aSingleFilterDescription ) ;
6570 }
6571 strcat( lDialogString , "\"" ) ;
6572 }
6573 if ( aAllowMultipleSelects )
6574 {
6575 strcat( lDialogString , " --multiple --separate-output" ) ;
6576 }
6577 if ( aTitle && strlen(aTitle) )
6578 {
6579 strcat(lDialogString, " --title \"") ;
6580 strcat(lDialogString, aTitle) ;
6581 strcat(lDialogString, "\"") ;
6582 }
6583 }
6584 else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
6585 {
6586 if ( tfd_zenityPresent() )
6587 {
6588 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;}
6589 strcpy( lDialogString , "zenity" ) ;
6590 if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
6591 {
6592 strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
6593 }
6594 }
6595 else if ( tfd_matedialogPresent() )
6596 {
6597 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;}
6598 strcpy( lDialogString , "matedialog" ) ;
6599 }
6600 else if ( tfd_shellementaryPresent() )
6601 {
6602 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;}
6603 strcpy( lDialogString , "shellementary" ) ;
6604 }
6605 else
6606 {
6607 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;}
6608 strcpy( lDialogString , "qarma" ) ;
6609 if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
6610 {
6611 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
6612 }
6613 }
6614 strcat( lDialogString , " --file-selection" ) ;
6615
6616 if ( aAllowMultipleSelects )
6617 {
6618 strcat( lDialogString , " --multiple" ) ;
6619 }
6620
6621 strcat(lDialogString, " --title=\"") ;
6622 if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ;
6623 strcat(lDialogString, "\"") ;
6624
6625 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
6626 {
6627 strcat(lDialogString, " --filename=\"") ;
6628 strcat(lDialogString, aDefaultPathAndFile) ;
6629 strcat(lDialogString, "\"") ;
6630 }
6631 if ( aNumOfFilterPatterns > 0 )
6632 {
6633 strcat( lDialogString , " --file-filter='" ) ;
6634 if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
6635 {
6636 strcat( lDialogString , aSingleFilterDescription ) ;
6637 strcat( lDialogString , " |" ) ;
6638 }
6639 for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
6640 {
6641 strcat( lDialogString , " " ) ;
6642 strcat( lDialogString , aFilterPatterns[i] ) ;
6643 }
6644 strcat( lDialogString , "' --file-filter='All files | *'" ) ;
6645 }
6646 if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
6647 }
6648 else if (tfd_yadPresent())
6649 {
6650 if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; }
6651 strcpy(lDialogString, "yad --file-selection");
6652 if (aAllowMultipleSelects)
6653 {
6654 strcat(lDialogString, " --multiple");
6655 }
6656 if (aTitle && strlen(aTitle))
6657 {
6658 strcat(lDialogString, " --title=\"");
6659 strcat(lDialogString, aTitle);
6660 strcat(lDialogString, "\"");
6661 }
6662 if (aDefaultPathAndFile && strlen(aDefaultPathAndFile))
6663 {
6664 strcat(lDialogString, " --filename=\"");
6665 strcat(lDialogString, aDefaultPathAndFile);
6666 strcat(lDialogString, "\"");
6667 }
6668 if (aNumOfFilterPatterns > 0)
6669 {
6670 strcat(lDialogString, " --file-filter='");
6671 if (aSingleFilterDescription && strlen(aSingleFilterDescription))
6672 {
6673 strcat(lDialogString, aSingleFilterDescription);
6674 strcat(lDialogString, " |");
6675 }
6676 for (i = 0; i < aNumOfFilterPatterns; i++)
6677 {
6678 strcat(lDialogString, " ");
6679 strcat(lDialogString, aFilterPatterns[i]);
6680 }
6681 strcat(lDialogString, "' --file-filter='All files | *'");
6682 }
6683 if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
6684 }
6685 else if ( tkinter3Present( ) )
6686 {
6687 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;}
6688 strcpy( lDialogString , gPython3Name ) ;
6689 strcat( lDialogString ,
6690 " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();");
6691 strcat( lDialogString , "lFiles=filedialog.askopenfilename(");
6692 if ( aAllowMultipleSelects )
6693 {
6694 strcat( lDialogString , "multiple=1," ) ;
6695 }
6696 if ( aTitle && strlen(aTitle) )
6697 {
6698 strcat(lDialogString, "title='") ;
6699 strcat(lDialogString, aTitle) ;
6700 strcat(lDialogString, "',") ;
6701 }
6702 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
6703 {
6704 getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ;
6705 if ( strlen(lString) )
6706 {
6707 strcat(lDialogString, "initialdir='") ;
6708 strcat(lDialogString, lString ) ;
6709 strcat(lDialogString , "'," ) ;
6710 }
6711 getLastName( lString , aDefaultPathAndFile ) ;
6712 if ( strlen(lString) )
6713 {
6714 strcat(lDialogString, "initialfile='") ;
6715 strcat(lDialogString, lString ) ;
6716 strcat(lDialogString , "'," ) ;
6717 }
6718 }
6719 if ( ( aNumOfFilterPatterns > 1 )
6720 || ( ( aNumOfFilterPatterns == 1 ) /*test because poor osx behaviour*/
6721 && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) )
6722 {
6723 strcat(lDialogString , "filetypes=(" ) ;
6724 strcat( lDialogString , "('" ) ;
6725 if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
6726 {
6727 strcat( lDialogString , aSingleFilterDescription ) ;
6728 }
6729 strcat( lDialogString , "',(" ) ;
6730 for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
6731 {
6732 strcat( lDialogString , "'" ) ;
6733 strcat( lDialogString , aFilterPatterns[i] ) ;
6734 strcat( lDialogString , "'," ) ;
6735 }
6736 strcat( lDialogString , "))," ) ;
6737 strcat( lDialogString , "('All files','*'))" ) ;
6738 }
6739 strcat( lDialogString , ");\
6740 \nif not isinstance(lFiles, tuple):\n\tprint(lFiles)\nelse:\
6741 \n\tlFilesString=''\n\tfor lFile in lFiles:\n\t\tlFilesString+=str(lFile)+'|'\
6742 \n\tprint(lFilesString[:-1])\n\"" ) ;
6743 }
6744 else if ( tkinter2Present( ) )
6745 {
6746 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;}
6747 strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
6748 strcat( lDialogString , gPython2Name ) ;
6749 if ( ! isTerminalRunning( ) && tfd_isDarwin( ) )
6750 {
6751 strcat( lDialogString , " -i" ) ; /* for osx without console */
6752 }
6753 strcat( lDialogString ,
6754 " -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();");
6755
6756 if ( tfd_isDarwin( ) )
6757 {
6758 strcat( lDialogString ,
6759 "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
6760 frontmost of process \\\"Python\\\" to true' ''');");
6761 }
6762 strcat( lDialogString , "lFiles=tkFileDialog.askopenfilename(");
6763 if ( aAllowMultipleSelects )
6764 {
6765 strcat( lDialogString , "multiple=1," ) ;
6766 }
6767 if ( aTitle && strlen(aTitle) )
6768 {
6769 strcat(lDialogString, "title='") ;
6770 strcat(lDialogString, aTitle) ;
6771 strcat(lDialogString, "',") ;
6772 }
6773 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
6774 {
6775 getPathWithoutFinalSlash( lString , aDefaultPathAndFile ) ;
6776 if ( strlen(lString) )
6777 {
6778 strcat(lDialogString, "initialdir='") ;
6779 strcat(lDialogString, lString ) ;
6780 strcat(lDialogString , "'," ) ;
6781 }
6782 getLastName( lString , aDefaultPathAndFile ) ;
6783 if ( strlen(lString) )
6784 {
6785 strcat(lDialogString, "initialfile='") ;
6786 strcat(lDialogString, lString ) ;
6787 strcat(lDialogString , "'," ) ;
6788 }
6789 }
6790 if ( ( aNumOfFilterPatterns > 1 )
6791 || ( ( aNumOfFilterPatterns == 1 ) /*test because poor osx behaviour*/
6792 && ( aFilterPatterns[0][strlen(aFilterPatterns[0])-1] != '*' ) ) )
6793 {
6794 strcat(lDialogString , "filetypes=(" ) ;
6795 strcat( lDialogString , "('" ) ;
6796 if ( aSingleFilterDescription && strlen(aSingleFilterDescription) )
6797 {
6798 strcat( lDialogString , aSingleFilterDescription ) ;
6799 }
6800 strcat( lDialogString , "',(" ) ;
6801 for ( i = 0 ; i < aNumOfFilterPatterns ; i ++ )
6802 {
6803 strcat( lDialogString , "'" ) ;
6804 strcat( lDialogString , aFilterPatterns[i] ) ;
6805 strcat( lDialogString , "'," ) ;
6806 }
6807 strcat( lDialogString , "))," ) ;
6808 strcat( lDialogString , "('All files','*'))" ) ;
6809 }
6810 strcat( lDialogString , ");\
6811 \nif not isinstance(lFiles, tuple):\n\tprint lFiles\nelse:\
6812 \n\tlFilesString=''\n\tfor lFile in lFiles:\n\t\tlFilesString+=str(lFile)+'|'\
6813 \n\tprint lFilesString[:-1]\n\"" ) ;
6814 }
6815 else if ( xdialogPresent() || dialogName() )
6816 {
6817 if ( xdialogPresent( ) )
6818 {
6819 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;}
6820 lWasGraphicDialog = 1 ;
6821 strcpy( lDialogString , "(Xdialog " ) ;
6822 }
6823 else if ( isTerminalRunning( ) )
6824 {
6825 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
6826 strcpy( lDialogString , "(dialog " ) ;
6827 }
6828 else
6829 {
6830 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
6831 lWasXterm = 1 ;
6832 strcpy( lDialogString , terminalName() ) ;
6833 strcat( lDialogString , "'(" ) ;
6834 strcat( lDialogString , dialogName() ) ;
6835 strcat( lDialogString , " " ) ;
6836 }
6837
6838 if ( aTitle && strlen(aTitle) )
6839 {
6840 strcat(lDialogString, "--title \"") ;
6841 strcat(lDialogString, aTitle) ;
6842 strcat(lDialogString, "\" ") ;
6843 }
6844
6845 if ( !xdialogPresent() && !gdialogPresent() )
6846 {
6847 strcat(lDialogString, "--backtitle \"") ;
6848 strcat(lDialogString,
6849 "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
6850 strcat(lDialogString, "\" ") ;
6851 }
6852
6853 strcat( lDialogString , "--fselect \"" ) ;
6854 if ( aDefaultPathAndFile && strlen(aDefaultPathAndFile) )
6855 {
6856 if ( ! strchr(aDefaultPathAndFile, '/') )
6857 {
6858 strcat(lDialogString, "./") ;
6859 }
6860 strcat(lDialogString, aDefaultPathAndFile) ;
6861 }
6862 else if ( ! isTerminalRunning( ) && !lWasGraphicDialog )
6863 {
6864 strcat(lDialogString, getenv("HOME")) ;
6865 strcat(lDialogString, "/");
6866 }
6867 else
6868 {
6869 strcat(lDialogString, "./") ;
6870 }
6871
6872 if ( lWasGraphicDialog )
6873 {
6874 strcat(lDialogString, "\" 0 60 ) 2>&1 ") ;
6875 }
6876 else
6877 {
6878 strcat(lDialogString, "\" 0 60 >/dev/tty) ") ;
6879 if ( lWasXterm )
6880 {
6881 strcat( lDialogString ,
6882 "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
6883 }
6884 else
6885 {
6886 strcat(lDialogString, "2>&1 ; clear >/dev/tty") ;
6887 }
6888 }
6889 }
6890 else
6891 {
6892 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);}
6893 strcpy(lBuff, "Open file from ");
6894 strcat(lBuff, getCurDir());
6895 lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
6896 if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
6897 p = tinyfd_inputBox(aTitle, lBuff, "");
6898 if ( p ) strcpy(lBuff, p); else lBuff[0] = '\0';
6899 if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */
6900 if ( ! fileExists(lBuff) )
6901 {
6902 free(lBuff);
6903 lBuff = NULL;
6904 }
6905 else
6906 {
6907 lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char)));
6908 }
6909 return lBuff ;
6910 }
6911
6912 if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
6913 if ( ! ( lIn = popen( lDialogString , "r" ) ) )
6914 {
6915 free(lBuff);
6916 lBuff = NULL;
6917 return NULL ;
6918 }
6919 lBuff[0]='\0';
6920 p = lBuff;
6921 while ( fgets( p , sizeof( lBuff ) , lIn ) != NULL )
6922 {
6923 p += strlen( p );
6924 }
6925 pclose( lIn ) ;
6926 if ( lBuff[strlen( lBuff ) -1] == '\n' )
6927 {
6928 lBuff[strlen( lBuff ) -1] = '\0' ;
6929 }
6930 /* printf( "lBuff: %s\n" , lBuff ) ; */
6931 if ( lWasKdialog && aAllowMultipleSelects )
6932 {
6933 p = lBuff ;
6934 while ( ( p = strchr( p , '\n' ) ) )
6935 * p = '|' ;
6936 }
6937 /* printf( "lBuff2: %s\n" , lBuff ) ; */
6938 if ( ! strlen( lBuff ) )
6939 {
6940 free(lBuff);
6941 lBuff = NULL;
6942 return NULL;
6943 }
6944 if ( aAllowMultipleSelects && strchr(lBuff, '|') )
6945 {
6946 if( ! ensureFilesExist( lBuff , lBuff ) )
6947 {
6948 free(lBuff);
6949 lBuff = NULL;
6950 return NULL;
6951 }
6952 }
6953 else if ( !fileExists(lBuff) )
6954 {
6955 free(lBuff);
6956 lBuff = NULL;
6957 return NULL;
6958 }
6959
6960 lBuff = (char *)( realloc( lBuff, (strlen(lBuff)+1) * sizeof(char)));
6961
6962 /*printf( "lBuff3 [%lu]: %s\n" , strlen(lBuff) , lBuff ) ; */
6963 return lBuff ;
6964 }
6965
6966
6967 char * tinyfd_selectFolderDialog(
6968 char const * aTitle , /* "" */
6969 char const * aDefaultPath ) /* "" */
6970 {
6971 static char lBuff[MAX_PATH_OR_CMD] ;
6972 char lDialogString[MAX_PATH_OR_CMD] ;
6973 FILE * lIn ;
6974 char * p ;
6975 char * lPointerInputBox ;
6976 int lWasGraphicDialog = 0 ;
6977 int lWasXterm = 0 ;
6978 lBuff[0]='\0';
6979
6980 if (tfd_quoteDetected(aTitle)) return tinyfd_selectFolderDialog("INVALID TITLE WITH QUOTES", aDefaultPath);
6981 if (tfd_quoteDetected(aDefaultPath)) return tinyfd_selectFolderDialog(aTitle, "INVALID DEFAULT_PATH WITH QUOTES");
6982
6983 if ( osascriptPresent( ))
6984 {
6985 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;}
6986 strcpy( lDialogString , "osascript ");
6987 if ( ! osx9orBetter() ) strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
6988 strcat( lDialogString , " -e 'try' -e 'POSIX path of ( choose folder ");
6989 if ( aTitle && strlen(aTitle) )
6990 {
6991 strcat(lDialogString, "with prompt \"") ;
6992 strcat(lDialogString, aTitle) ;
6993 strcat(lDialogString, "\" ") ;
6994 }
6995 if ( aDefaultPath && strlen(aDefaultPath) )
6996 {
6997 strcat(lDialogString, "default location \"") ;
6998 strcat(lDialogString, aDefaultPath ) ;
6999 strcat(lDialogString , "\" " ) ;
7000 }
7001 strcat( lDialogString , ")' " ) ;
7002 strcat(lDialogString, "-e 'on error number -128' " ) ;
7003 strcat(lDialogString, "-e 'end try'") ;
7004 if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
7005 }
7006 else if ( tfd_kdialogPresent() )
7007 {
7008 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;}
7009 strcpy( lDialogString , "kdialog" ) ;
7010 if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
7011 {
7012 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
7013 }
7014 strcat( lDialogString , " --getexistingdirectory " ) ;
7015
7016 if ( aDefaultPath && strlen(aDefaultPath) )
7017 {
7018 if ( aDefaultPath[0] != '/' )
7019 {
7020 strcat(lDialogString, "$PWD/") ;
7021 }
7022 strcat(lDialogString, "\"") ;
7023 strcat(lDialogString, aDefaultPath ) ;
7024 strcat(lDialogString , "\"" ) ;
7025 }
7026 else
7027 {
7028 strcat(lDialogString, "$PWD/") ;
7029 }
7030
7031 if ( aTitle && strlen(aTitle) )
7032 {
7033 strcat(lDialogString, " --title \"") ;
7034 strcat(lDialogString, aTitle) ;
7035 strcat(lDialogString, "\"") ;
7036 }
7037 }
7038 else if ( tfd_zenityPresent() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
7039 {
7040 if ( tfd_zenityPresent() )
7041 {
7042 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity");return (char *)1;}
7043 strcpy( lDialogString , "zenity" ) ;
7044 if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
7045 {
7046 strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
7047 }
7048 }
7049 else if ( tfd_matedialogPresent() )
7050 {
7051 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;}
7052 strcpy( lDialogString , "matedialog" ) ;
7053 }
7054 else if ( tfd_shellementaryPresent() )
7055 {
7056 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;}
7057 strcpy( lDialogString , "shellementary" ) ;
7058 }
7059 else
7060 {
7061 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;}
7062 strcpy( lDialogString , "qarma" ) ;
7063 if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
7064 {
7065 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
7066 }
7067 }
7068 strcat( lDialogString , " --file-selection --directory" ) ;
7069
7070 strcat(lDialogString, " --title=\"") ;
7071 if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ;
7072 strcat(lDialogString, "\"") ;
7073
7074 if ( aDefaultPath && strlen(aDefaultPath) )
7075 {
7076 strcat(lDialogString, " --filename=\"") ;
7077 strcat(lDialogString, aDefaultPath) ;
7078 strcat(lDialogString, "\"") ;
7079 }
7080 if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
7081 }
7082 else if (tfd_yadPresent())
7083 {
7084 if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; }
7085 strcpy(lDialogString, "yad --file-selection --directory");
7086 if (aTitle && strlen(aTitle))
7087 {
7088 strcat(lDialogString, " --title=\"");
7089 strcat(lDialogString, aTitle);
7090 strcat(lDialogString, "\"");
7091 }
7092 if (aDefaultPath && strlen(aDefaultPath))
7093 {
7094 strcat(lDialogString, " --filename=\"");
7095 strcat(lDialogString, aDefaultPath);
7096 strcat(lDialogString, "\"");
7097 }
7098 if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
7099 }
7100 else if ( !xdialogPresent() && tkinter3Present( ) )
7101 {
7102 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;}
7103 strcpy( lDialogString , gPython3Name ) ;
7104 strcat( lDialogString ,
7105 " -S -c \"import tkinter;from tkinter import filedialog;root=tkinter.Tk();root.withdraw();");
7106 strcat( lDialogString , "res=filedialog.askdirectory(");
7107 if ( aTitle && strlen(aTitle) )
7108 {
7109 strcat(lDialogString, "title='") ;
7110 strcat(lDialogString, aTitle) ;
7111 strcat(lDialogString, "',") ;
7112 }
7113 if ( aDefaultPath && strlen(aDefaultPath) )
7114 {
7115 strcat(lDialogString, "initialdir='") ;
7116 strcat(lDialogString, aDefaultPath ) ;
7117 strcat(lDialogString , "'" ) ;
7118 }
7119 strcat( lDialogString, ");\nif not isinstance(res, tuple):\n\tprint(res)\n\"" ) ;
7120 }
7121 else if ( !xdialogPresent() && tkinter2Present( ) )
7122 {
7123 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;}
7124 strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
7125 strcat( lDialogString , gPython2Name ) ;
7126 if ( ! isTerminalRunning( ) && tfd_isDarwin( ) )
7127 {
7128 strcat( lDialogString , " -i" ) ; /* for osx without console */
7129 }
7130 strcat( lDialogString ,
7131 " -S -c \"import Tkinter,tkFileDialog;root=Tkinter.Tk();root.withdraw();");
7132
7133 if ( tfd_isDarwin( ) )
7134 {
7135 strcat( lDialogString ,
7136 "import os;os.system('''/usr/bin/osascript -e 'tell app \\\"Finder\\\" to set \
7137 frontmost of process \\\"Python\\\" to true' ''');");
7138 }
7139
7140 strcat( lDialogString , "print tkFileDialog.askdirectory(");
7141 if ( aTitle && strlen(aTitle) )
7142 {
7143 strcat(lDialogString, "title='") ;
7144 strcat(lDialogString, aTitle) ;
7145 strcat(lDialogString, "',") ;
7146 }
7147 if ( aDefaultPath && strlen(aDefaultPath) )
7148 {
7149 strcat(lDialogString, "initialdir='") ;
7150 strcat(lDialogString, aDefaultPath ) ;
7151 strcat(lDialogString , "'" ) ;
7152 }
7153 strcat( lDialogString , ")\"" ) ;
7154 }
7155 else if ( xdialogPresent() || dialogName() )
7156 {
7157 if ( xdialogPresent( ) )
7158 {
7159 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;}
7160 lWasGraphicDialog = 1 ;
7161 strcpy( lDialogString , "(Xdialog " ) ;
7162 }
7163 else if ( isTerminalRunning( ) )
7164 {
7165 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
7166 strcpy( lDialogString , "(dialog " ) ;
7167 }
7168 else
7169 {
7170 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"dialog");return (char *)0;}
7171 lWasXterm = 1 ;
7172 strcpy( lDialogString , terminalName() ) ;
7173 strcat( lDialogString , "'(" ) ;
7174 strcat( lDialogString , dialogName() ) ;
7175 strcat( lDialogString , " " ) ;
7176 }
7177
7178 if ( aTitle && strlen(aTitle) )
7179 {
7180 strcat(lDialogString, "--title \"") ;
7181 strcat(lDialogString, aTitle) ;
7182 strcat(lDialogString, "\" ") ;
7183 }
7184
7185 if ( !xdialogPresent() && !gdialogPresent() )
7186 {
7187 strcat(lDialogString, "--backtitle \"") ;
7188 strcat(lDialogString,
7189 "tab: focus | /: populate | spacebar: fill text field | ok: TEXT FIELD ONLY") ;
7190 strcat(lDialogString, "\" ") ;
7191 }
7192
7193 strcat( lDialogString , "--dselect \"" ) ;
7194 if ( aDefaultPath && strlen(aDefaultPath) )
7195 {
7196 strcat(lDialogString, aDefaultPath) ;
7197 ensureFinalSlash(lDialogString);
7198 }
7199 else if ( ! isTerminalRunning( ) && !lWasGraphicDialog )
7200 {
7201 strcat(lDialogString, getenv("HOME")) ;
7202 strcat(lDialogString, "/");
7203 }
7204 else
7205 {
7206 strcat(lDialogString, "./") ;
7207 }
7208
7209 if ( lWasGraphicDialog )
7210 {
7211 strcat(lDialogString, "\" 0 60 ) 2>&1 ") ;
7212 }
7213 else
7214 {
7215 strcat(lDialogString, "\" 0 60 >/dev/tty) ") ;
7216 if ( lWasXterm )
7217 {
7218 strcat( lDialogString ,
7219 "2>/tmp/tinyfd.txt';cat /tmp/tinyfd.txt;rm /tmp/tinyfd.txt");
7220 }
7221 else
7222 {
7223 strcat(lDialogString, "2>&1 ; clear >/dev/tty") ;
7224 }
7225 }
7226 }
7227 else
7228 {
7229 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);}
7230 strcpy(lBuff, "Select folder from ");
7231 strcat(lBuff, getCurDir());
7232 lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
7233 if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
7234 p = tinyfd_inputBox(aTitle, lBuff, "");
7235 if (p) strcpy(lBuff, p); else lBuff[0] = '\0';
7236 if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */
7237 p = lBuff;
7238
7239 if ( !p || ! strlen( p ) || ! dirExists( p ) )
7240 {
7241 return NULL ;
7242 }
7243 return p ;
7244 }
7245 if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
7246 if ( ! ( lIn = popen( lDialogString , "r" ) ) )
7247 {
7248 return NULL ;
7249 }
7250 while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
7251 {}
7252 pclose( lIn ) ;
7253 if ( lBuff[strlen( lBuff ) -1] == '\n' )
7254 {
7255 lBuff[strlen( lBuff ) -1] = '\0' ;
7256 }
7257 /* printf( "lBuff: %s\n" , lBuff ) ; */
7258 if ( ! strlen( lBuff ) || ! dirExists( lBuff ) )
7259 {
7260 return NULL ;
7261 }
7262 return lBuff ;
7263 }
7264
7265
7266 /* aDefaultRGB is used only if aDefaultHexRGB is absent */
7267 /* aDefaultRGB and aoResultRGB can be the same array */
7268 /* returns NULL on cancel */
7269 /* returns the hexcolor as a string "#FF0000" */
7270 /* aoResultRGB also contains the result */
7271 char * tinyfd_colorChooser(
7272 char const * aTitle , /* NULL or "" */
7273 char const * aDefaultHexRGB , /* NULL or "#FF0000"*/
7274 unsigned char const aDefaultRGB[3] , /* { 0 , 255 , 255 } */
7275 unsigned char aoResultRGB[3] ) /* { 0 , 0 , 0 } */
7276 {
7277 static char lDefaultHexRGB[16];
7278 char lBuff[128] ;
7279
7280 char lTmp[128] ;
7281 #if !((defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__))
7282 char * lTmp2 ;
7283 #endif
7284 char lDialogString[MAX_PATH_OR_CMD] ;
7285 unsigned char lDefaultRGB[3];
7286 char * p;
7287 char * lPointerInputBox;
7288 FILE * lIn ;
7289 int i ;
7290 int lWasZenity3 = 0 ;
7291 int lWasOsascript = 0 ;
7292 int lWasXdialog = 0 ;
7293 lBuff[0]='\0';
7294
7295 if (tfd_quoteDetected(aTitle)) return tinyfd_colorChooser("INVALID TITLE WITH QUOTES", aDefaultHexRGB, aDefaultRGB, aoResultRGB);
7296 if (tfd_quoteDetected(aDefaultHexRGB)) return tinyfd_colorChooser(aTitle, "INVALID DEFAULT_HEX_RGB WITH QUOTES", aDefaultRGB, aoResultRGB);
7297
7298 if (aDefaultHexRGB && (strlen(aDefaultHexRGB)==7) )
7299 {
7300 Hex2RGB(aDefaultHexRGB, lDefaultRGB);
7301 strcpy(lDefaultHexRGB, aDefaultHexRGB);
7302 }
7303 else
7304 {
7305 lDefaultRGB[0] = aDefaultRGB[0];
7306 lDefaultRGB[1] = aDefaultRGB[1];
7307 lDefaultRGB[2] = aDefaultRGB[2];
7308 RGB2Hex(aDefaultRGB, lDefaultHexRGB);
7309 }
7310
7311 if ( osascriptPresent( ) )
7312 {
7313 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"applescript");return (char *)1;}
7314 lWasOsascript = 1 ;
7315 strcpy( lDialogString , "osascript");
7316
7317 if ( ! osx9orBetter() )
7318 {
7319 strcat( lDialogString , " -e 'tell application \"System Events\"' -e 'Activate'");
7320 strcat( lDialogString , " -e 'try' -e 'set mycolor to choose color default color {");
7321 }
7322 else
7323 {
7324 strcat( lDialogString ,
7325 " -e 'try' -e 'tell app (path to frontmost application as Unicode text) \
7326 to set mycolor to choose color default color {");
7327 }
7328
7329 sprintf(lTmp, "%d", 256 * lDefaultRGB[0] ) ;
7330 strcat(lDialogString, lTmp ) ;
7331 strcat(lDialogString, "," ) ;
7332 sprintf(lTmp, "%d", 256 * lDefaultRGB[1] ) ;
7333 strcat(lDialogString, lTmp ) ;
7334 strcat(lDialogString, "," ) ;
7335 sprintf(lTmp, "%d", 256 * lDefaultRGB[2] ) ;
7336 strcat(lDialogString, lTmp ) ;
7337 strcat(lDialogString, "}' " ) ;
7338 strcat( lDialogString ,
7339 "-e 'set mystring to ((item 1 of mycolor) div 256 as integer) as string' " );
7340 strcat( lDialogString ,
7341 "-e 'repeat with i from 2 to the count of mycolor' " );
7342 strcat( lDialogString ,
7343 "-e 'set mystring to mystring & \" \" & ((item i of mycolor) div 256 as integer) as string' " );
7344 strcat( lDialogString , "-e 'end repeat' " );
7345 strcat( lDialogString , "-e 'mystring' ");
7346 strcat(lDialogString, "-e 'on error number -128' " ) ;
7347 strcat(lDialogString, "-e 'end try'") ;
7348 if ( ! osx9orBetter() ) strcat( lDialogString, " -e 'end tell'") ;
7349 }
7350 else if ( tfd_kdialogPresent() )
7351 {
7352 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"kdialog");return (char *)1;}
7353 strcpy( lDialogString , "kdialog" ) ;
7354 if ( (tfd_kdialogPresent() == 2) && tfd_xpropPresent() )
7355 {
7356 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
7357 }
7358 sprintf( lDialogString + strlen(lDialogString) , " --getcolor --default '%s'" , lDefaultHexRGB ) ;
7359
7360 if ( aTitle && strlen(aTitle) )
7361 {
7362 strcat(lDialogString, " --title \"") ;
7363 strcat(lDialogString, aTitle) ;
7364 strcat(lDialogString, "\"") ;
7365 }
7366 }
7367 else if ( tfd_zenity3Present() || tfd_matedialogPresent() || tfd_shellementaryPresent() || tfd_qarmaPresent() )
7368 {
7369 lWasZenity3 = 1 ;
7370 if ( tfd_zenity3Present() )
7371 {
7372 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"zenity3");return (char *)1;}
7373 strcpy( lDialogString , "zenity" );
7374 if ( (tfd_zenity3Present() >= 4) && !getenv("SSH_TTY") && tfd_xpropPresent() )
7375 {
7376 strcat( lDialogString, " --attach=$(sleep .01;xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
7377 }
7378 }
7379 else if ( tfd_matedialogPresent() )
7380 {
7381 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"matedialog");return (char *)1;}
7382 strcpy( lDialogString , "matedialog" ) ;
7383 }
7384 else if ( tfd_shellementaryPresent() )
7385 {
7386 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"shellementary");return (char *)1;}
7387 strcpy( lDialogString , "shellementary" ) ;
7388 }
7389 else
7390 {
7391 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"qarma");return (char *)1;}
7392 strcpy( lDialogString , "qarma" ) ;
7393 if ( !getenv("SSH_TTY") && tfd_xpropPresent() )
7394 {
7395 strcat(lDialogString, " --attach=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)"); /* contribution: Paul Rouget */
7396 }
7397 }
7398 strcat( lDialogString , " --color-selection --show-palette" ) ;
7399 sprintf( lDialogString + strlen(lDialogString), " --color=%s" , lDefaultHexRGB ) ;
7400
7401 strcat(lDialogString, " --title=\"") ;
7402 if (aTitle && strlen(aTitle)) strcat(lDialogString, aTitle) ;
7403 strcat(lDialogString, "\"") ;
7404
7405 if (tinyfd_silent) strcat( lDialogString , " 2>/dev/null ");
7406 }
7407 else if (tfd_yadPresent())
7408 {
7409 if (aTitle && !strcmp(aTitle, "tinyfd_query")) { strcpy(tinyfd_response, "yad"); return (char*)1; }
7410 strcpy(lDialogString, "yad --color");
7411 sprintf(lDialogString + strlen(lDialogString), " --init-color=%s", lDefaultHexRGB);
7412 if (aTitle && strlen(aTitle))
7413 {
7414 strcat(lDialogString, " --title=\"");
7415 strcat(lDialogString, aTitle);
7416 strcat(lDialogString, "\"");
7417 }
7418 if (tinyfd_silent) strcat(lDialogString, " 2>/dev/null ");
7419 }
7420 else if ( xdialogPresent() )
7421 {
7422 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"xdialog");return (char *)1;}
7423 lWasXdialog = 1 ;
7424 strcpy( lDialogString , "Xdialog --colorsel \"" ) ;
7425 if ( aTitle && strlen(aTitle) )
7426 {
7427 strcat(lDialogString, aTitle) ;
7428 }
7429 strcat(lDialogString, "\" 0 60 ") ;
7430 #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
7431 sprintf(lTmp,"%hhu %hhu %hhu",lDefaultRGB[0],lDefaultRGB[1],lDefaultRGB[2]);
7432 #else
7433 sprintf(lTmp,"%hu %hu %hu",lDefaultRGB[0],lDefaultRGB[1],lDefaultRGB[2]);
7434 #endif
7435 strcat(lDialogString, lTmp) ;
7436 strcat(lDialogString, " 2>&1");
7437 }
7438 else if ( tkinter3Present( ) )
7439 {
7440 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python3-tkinter");return (char *)1;}
7441 strcpy( lDialogString , gPython3Name ) ;
7442 strcat( lDialogString ,
7443 " -S -c \"import tkinter;from tkinter import colorchooser;root=tkinter.Tk();root.withdraw();");
7444 strcat( lDialogString , "res=colorchooser.askcolor(color='" ) ;
7445 strcat(lDialogString, lDefaultHexRGB ) ;
7446 strcat(lDialogString, "'") ;
7447
7448 if ( aTitle && strlen(aTitle) )
7449 {
7450 strcat(lDialogString, ",title='") ;
7451 strcat(lDialogString, aTitle) ;
7452 strcat(lDialogString, "'") ;
7453 }
7454 strcat( lDialogString , ");\
7455 \nif res[1] is not None:\n\tprint(res[1])\"" ) ;
7456 }
7457 else if ( tkinter2Present( ) )
7458 {
7459 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){strcpy(tinyfd_response,"python2-tkinter");return (char *)1;}
7460 strcpy( lDialogString , "export PYTHONIOENCODING=utf-8;" ) ;
7461 strcat( lDialogString , gPython2Name ) ;
7462 if ( ! isTerminalRunning( ) && tfd_isDarwin( ) )
7463 {
7464 strcat( lDialogString , " -i" ) ; /* for osx without console */
7465 }
7466
7467 strcat( lDialogString ,
7468 " -S -c \"import Tkinter,tkColorChooser;root=Tkinter.Tk();root.withdraw();");
7469
7470 if ( tfd_isDarwin( ) )
7471 {
7472 strcat( lDialogString ,
7473 "import os;os.system('''osascript -e 'tell app \\\"Finder\\\" to set \
7474 frontmost of process \\\"Python\\\" to true' ''');");
7475 }
7476
7477 strcat( lDialogString , "res=tkColorChooser.askcolor(color='" ) ;
7478 strcat(lDialogString, lDefaultHexRGB ) ;
7479 strcat(lDialogString, "'") ;
7480
7481
7482 if ( aTitle && strlen(aTitle) )
7483 {
7484 strcat(lDialogString, ",title='") ;
7485 strcat(lDialogString, aTitle) ;
7486 strcat(lDialogString, "'") ;
7487 }
7488 strcat( lDialogString , ");\
7489 \nif res[1] is not None:\n\tprint res[1]\"" ) ;
7490 }
7491 else
7492 {
7493 if (aTitle&&!strcmp(aTitle,"tinyfd_query")){return tinyfd_inputBox(aTitle,NULL,NULL);}
7494 lPointerInputBox = tinyfd_inputBox(NULL, NULL, NULL); /* obtain a pointer on the current content of tinyfd_inputBox */
7495 if (lPointerInputBox) strcpy(lDialogString, lPointerInputBox); /* preserve the current content of tinyfd_inputBox */
7496 p = tinyfd_inputBox(aTitle, "Enter hex rgb color (i.e. #f5ca20)", lDefaultHexRGB);
7497
7498 if ( !p || (strlen(p) != 7) || (p[0] != '#') )
7499 {
7500 return NULL ;
7501 }
7502 for ( i = 1 ; i < 7 ; i ++ )
7503 {
7504 if ( ! isxdigit( (int) p[i] ) )
7505 {
7506 return NULL ;
7507 }
7508 }
7509 Hex2RGB(p,aoResultRGB);
7510 strcpy(lDefaultHexRGB, p);
7511 if (lPointerInputBox) strcpy(lPointerInputBox, lDialogString); /* restore its previous content to tinyfd_inputBox */
7512 return lDefaultHexRGB;
7513 }
7514
7515 if (tinyfd_verbose) printf( "lDialogString: %s\n" , lDialogString ) ;
7516 if ( ! ( lIn = popen( lDialogString , "r" ) ) )
7517 {
7518 return NULL ;
7519 }
7520 while ( fgets( lBuff , sizeof( lBuff ) , lIn ) != NULL )
7521 {
7522 }
7523 pclose( lIn ) ;
7524 if ( ! strlen( lBuff ) )
7525 {
7526 return NULL ;
7527 }
7528 /* printf( "len Buff: %lu\n" , strlen(lBuff) ) ; */
7529 /* printf( "lBuff0: %s\n" , lBuff ) ; */
7530 if ( lBuff[strlen( lBuff ) -1] == '\n' )
7531 {
7532 lBuff[strlen( lBuff ) -1] = '\0' ;
7533 }
7534
7535 if ( lWasZenity3 )
7536 {
7537 if ( lBuff[0] == '#' )
7538 {
7539 if ( strlen(lBuff)>7 )
7540 {
7541 lBuff[3]=lBuff[5];
7542 lBuff[4]=lBuff[6];
7543 lBuff[5]=lBuff[9];
7544 lBuff[6]=lBuff[10];
7545 lBuff[7]='\0';
7546 }
7547 Hex2RGB(lBuff,aoResultRGB);
7548 }
7549 else if ( lBuff[3] == '(' ) {
7550 #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
7551 sscanf(lBuff,"rgb(%hhu,%hhu,%hhu", & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]);
7552 #else
7553 aoResultRGB[0] = strtol(lBuff+4, & lTmp2, 10 );
7554 aoResultRGB[1] = strtol(lTmp2+1, & lTmp2, 10 );
7555 aoResultRGB[2] = strtol(lTmp2+1, NULL, 10 );
7556 #endif
7557 RGB2Hex(aoResultRGB,lBuff);
7558 }
7559 else if ( lBuff[4] == '(' ) {
7560 #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
7561 sscanf(lBuff,"rgba(%hhu,%hhu,%hhu", & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]);
7562 #else
7563 aoResultRGB[0] = strtol(lBuff+5, & lTmp2, 10 );
7564 aoResultRGB[1] = strtol(lTmp2+1, & lTmp2, 10 );
7565 aoResultRGB[2] = strtol(lTmp2+1, NULL, 10 );
7566 #endif
7567 RGB2Hex(aoResultRGB,lBuff);
7568 }
7569 }
7570 else if ( lWasOsascript || lWasXdialog )
7571 {
7572 /* printf( "lBuff: %s\n" , lBuff ) ; */
7573 #if (defined(__cplusplus ) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__clang__)
7574 sscanf(lBuff,"%hhu %hhu %hhu", & aoResultRGB[0], & aoResultRGB[1],& aoResultRGB[2]);
7575 #else
7576 aoResultRGB[0] = strtol(lBuff, & lTmp2, 10 );
7577 aoResultRGB[1] = strtol(lTmp2+1, & lTmp2, 10 );
7578 aoResultRGB[2] = strtol(lTmp2+1, NULL, 10 );
7579 #endif
7580 RGB2Hex(aoResultRGB,lBuff);
7581 }
7582 else
7583 {
7584 Hex2RGB(lBuff,aoResultRGB);
7585 }
7586 /* printf("%d %d %d\n", aoResultRGB[0],aoResultRGB[1],aoResultRGB[2]); */
7587 /* printf( "lBuff: %s\n" , lBuff ) ; */
7588
7589 strcpy(lDefaultHexRGB,lBuff);
7590 return lDefaultHexRGB ;
7591 }
7592
7593 #endif /* _WIN32 */
7594
7595
7596 /*
7597 int main( int argc , char * argv[] )
7598 {
7599 char const * lTmp;
7600 char const * lTheSaveFileName;
7601 char const * lTheOpenFileName;
7602 char const * lTheSelectFolderName;
7603 char const * lTheHexColor;
7604 char const * lWillBeGraphicMode;
7605 unsigned char lRgbColor[3];
7606 FILE * lIn;
7607 char lBuffer[1024];
7608 char lString[1024];
7609 char const * lFilterPatterns[2] = { "*.txt", "*.text" };
7610
7611 tinyfd_verbose = argc - 1;
7612 tinyfd_silent = 1;
7613
7614 lWillBeGraphicMode = tinyfd_inputBox("tinyfd_query", NULL, NULL);
7615
7616 strcpy(lBuffer, "v");
7617 strcat(lBuffer, tinyfd_version);
7618 if (lWillBeGraphicMode)
7619 {
7620 strcat(lBuffer, "\ngraphic mode: ");
7621 }
7622 else
7623 {
7624 strcat(lBuffer, "\nconsole mode: ");
7625 }
7626 strcat(lBuffer, tinyfd_response);
7627 strcat(lBuffer, "\n");
7628 strcat(lBuffer, tinyfd_needs+78);
7629 strcpy(lString, "tinyfiledialogs");
7630 tinyfd_messageBox(lString, lBuffer, "ok", "info", 0);
7631
7632 tinyfd_notifyPopup("the title", "the message\n\tfrom outer-space", "info");
7633
7634 if (lWillBeGraphicMode && !tinyfd_forceConsole)
7635 {
7636 tinyfd_forceConsole = ! tinyfd_messageBox("Hello World",
7637 "graphic dialogs [yes] / console mode [no]?",
7638 "yesno", "question", 1);
7639 }
7640
7641 lTmp = tinyfd_inputBox(
7642 "a password box", "your password will be revealed", NULL);
7643
7644 if (!lTmp) return 1;
7645
7646 strcpy(lString, lTmp);
7647
7648 lTheSaveFileName = tinyfd_saveFileDialog(
7649 "let us save this password",
7650 "passwordFile.txt",
7651 2,
7652 lFilterPatterns,
7653 NULL);
7654
7655 if (!lTheSaveFileName)
7656 {
7657 tinyfd_messageBox(
7658 "Error",
7659 "Save file name is NULL",
7660 "ok",
7661 "error",
7662 1);
7663 return 1;
7664 }
7665
7666 lIn = fopen(lTheSaveFileName, "w");
7667 if (!lIn)
7668 {
7669 tinyfd_messageBox(
7670 "Error",
7671 "Can not open this file in write mode",
7672 "ok",
7673 "error",
7674 1);
7675 return 1;
7676 }
7677 fputs(lString, lIn);
7678 fclose(lIn);
7679
7680 lTheOpenFileName = tinyfd_openFileDialog(
7681 "let us read the password back",
7682 "",
7683 2,
7684 lFilterPatterns,
7685 NULL,
7686 0);
7687
7688 if (!lTheOpenFileName)
7689 {
7690 tinyfd_messageBox(
7691 "Error",
7692 "Open file name is NULL",
7693 "ok",
7694 "error",
7695 1);
7696 return 1;
7697 }
7698
7699 lIn = fopen(lTheOpenFileName, "r");
7700
7701 if (!lIn)
7702 {
7703 tinyfd_messageBox(
7704 "Error",
7705 "Can not open this file in read mode",
7706 "ok",
7707 "error",
7708 1);
7709 return(1);
7710 }
7711 lBuffer[0] = '\0';
7712 fgets(lBuffer, sizeof(lBuffer), lIn);
7713 fclose(lIn);
7714
7715 tinyfd_messageBox("your password is",
7716 lBuffer, "ok", "info", 1);
7717
7718 lTheSelectFolderName = tinyfd_selectFolderDialog(
7719 "let us just select a directory", NULL);
7720
7721 if (!lTheSelectFolderName)
7722 {
7723 tinyfd_messageBox(
7724 "Error",
7725 "Select folder name is NULL",
7726 "ok",
7727 "error",
7728 1);
7729 return 1;
7730 }
7731
7732 tinyfd_messageBox("The selected folder is",
7733 lTheSelectFolderName, "ok", "info", 1);
7734
7735 lTheHexColor = tinyfd_colorChooser(
7736 "choose a nice color",
7737 "#FF0077",
7738 lRgbColor,
7739 lRgbColor);
7740
7741 if (!lTheHexColor)
7742 {
7743 tinyfd_messageBox(
7744 "Error",
7745 "hexcolor is NULL",
7746 "ok",
7747 "error",
7748 1);
7749 return 1;
7750 }
7751
7752 tinyfd_messageBox("The selected hexcolor is",
7753 lTheHexColor, "ok", "info", 1);
7754
7755 tinyfd_beep();
7756
7757 return 0;
7758 }
7759 */
7760
7761 #ifdef _MSC_VER
7762 #pragma warning(default:4996)
7763 #pragma warning(default:4100)
7764 #pragma warning(default:4706)
7765 #endif