Author: bgbnbigben
Date: 2011-02-04 16:48:46 -0800 (Fri, 04 Feb 2011)
New Revision: 8369
Log:
Added in fltk::fltk_fopen() to allow the user to open a utf8 directory.
Almost there with this damn FileChooser.....


Modified:
   trunk/fltk/filename.h
   trunk/fltk/run.h
   trunk/src/FileChooser2.cxx
   trunk/src/filename_isdir.cxx
   trunk/src/osx/run.cxx
   trunk/src/run.cxx
   trunk/src/win32/run.cxx
   trunk/src/x11/run.cxx

Modified: trunk/fltk/filename.h
===================================================================
--- trunk/fltk/filename.h       2011-02-04 23:32:53 UTC (rev 8368)
+++ trunk/fltk/filename.h       2011-02-05 00:48:46 UTC (rev 8369)
@@ -32,10 +32,6 @@
 ////////////////////////////////////////////////////////////////
 #ifndef DOXYGEN
 
-#if defined (_WIN32) && !defined (__CYGWIN__)
-# define stat _stat
-#endif
-
 // dirent (what a pain)...
 
 // FC: UNDER WIN32/VC6 long long is undefined, so use __int64 instead
@@ -109,7 +105,16 @@
 /// Some functions to manipulate filenames, to make portable programs.
 //@{
 
+// To deal with Windows' Unicode chars, the "stat" struct has also been 
gratuitously changed
+// the stat struct to _stat. This needs to be set and unset accordingly.
+#if defined (_WIN32) && !defined (__CYGWIN__)
+# define stat _stat
+#endif
 FL_API int fltk_stat(const char* name, struct stat *buffer);
+#ifdef _stat
+# undef _stat
+#endif
+
 FL_API int filename_absolute(char *to, int tolen, const char *from, const 
char* cwd=0);
 FL_API int filename_relative(char *to, int tolen, const char *from, const 
char* cwd=0);
 FL_API const char *filename_name(const char *);

Modified: trunk/fltk/run.h
===================================================================
--- trunk/fltk/run.h    2011-02-04 23:32:53 UTC (rev 8368)
+++ trunk/fltk/run.h    2011-02-05 00:48:46 UTC (rev 8369)
@@ -79,6 +79,7 @@
 FL_API void add_fd(int fd, int when, FileHandler, void* =0);
 FL_API void add_fd(int fd, FileHandler, void* = 0);
 FL_API void remove_fd(int, int when = -1);
+FL_API FILE* fltk_fopen(const char* name, const char* flags);
 
 FL_API void lock();
 FL_API void unlock();

Modified: trunk/src/FileChooser2.cxx
===================================================================
--- trunk/src/FileChooser2.cxx  2011-02-04 23:32:53 UTC (rev 8368)
+++ trunk/src/FileChooser2.cxx  2011-02-05 00:48:46 UTC (rev 8369)
@@ -56,6 +56,7 @@
 #include <fltk/SharedImage.h>
 #include <fltk/Item.h>
 #include <fltk/string.h>
+#include <fltk/utf.h>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -986,16 +987,25 @@
 */
 
 void FileChooser::update_preview() {
-  const char           *filename;      // Current filename
-  SharedImage  *image,         // New image
+  const char           *cfilename;     // Current filename
+  char                 *filename;      // UTF-8 converted filename
+  SharedImage          *image,         // New image
                        *oldimage;      // Old image
   int                  pbw, pbh;       // Width and height of preview box
   int                  w, h;           // Width and height of preview image
 
 
   if (!previewButton->value()) return;
-
-  if ((filename = value()) == NULL || fltk::filename_isdir(filename)) image = 
NULL;
+  cfilename = value();
+  if (cfilename != NULL && !utf8test(cfilename, strlen(cfilename))) {
+    int length = utf8frommb(NULL, 0, cfilename, strlen(cfilename));
+    filename = (char*)malloc(sizeof(char)*length+11);
+    utf8frommb(filename, length+1, cfilename, length+1);
+  } else {
+    filename = (char*)malloc(sizeof(char)*strlen(cfilename)+11);
+    strcpy(filename, cfilename);
+  }
+  if (filename == NULL || fltk::filename_isdir(filename)) image = NULL;
   else {
     window->cursor(fltk::CURSOR_WAIT);
     fltk::check();

Modified: trunk/src/filename_isdir.cxx
===================================================================
--- trunk/src/filename_isdir.cxx        2011-02-04 23:32:53 UTC (rev 8368)
+++ trunk/src/filename_isdir.cxx        2011-02-05 00:48:46 UTC (rev 8369)
@@ -31,6 +31,7 @@
 #include <sys/stat.h>
 #if defined(_WIN32) && !defined(__CYGWIN__)
 # include <io.h>
+# include <windows.h>
 #else
 # include <unistd.h>
 #endif
@@ -42,9 +43,11 @@
 static const char *last_statname = 0;
 static bool last_result = false;
 
-/** Portably calls the system's stat() function, to deal with UTF-8 
-    and UTF-16 filenames.
+/** Portably calls the system's stat() function, to deal with
+    native Unicode filenames.
     Has the same return values and use as the system's stat.
+    The string passed to fltk_stat \b must be UTF8 (note that ASCII is
+    a subset of UTF8)
 */
 int fltk::fltk_stat(const char* name, struct stat *buffer) {
 #if defined(_WIN32) && !defined (__CYGWIN__)
@@ -90,7 +93,7 @@
 /** Returns true if the file exists and is a directory. */
 bool fltk::filename_isdir(const char* name) {
   if (!fill_stat(name)) return false;
-  return (last_stat.st_mode&0170000)==0040000;
+  return ((last_stat.st_mode & 0170000) == 0040000);
 }
 
 /** Returns true if the file exists and is a regular file. */

Modified: trunk/src/osx/run.cxx
===================================================================
--- trunk/src/osx/run.cxx       2011-02-04 23:32:53 UTC (rev 8368)
+++ trunk/src/osx/run.cxx       2011-02-05 00:48:46 UTC (rev 8369)
@@ -1930,6 +1930,10 @@
   return CreatedWindow::find(w)->xid;
 }
 
+FILE* fltk::fltk_fopen(const char* name, const char* flags) {
+  return fopen(name, flags);
+}
+
 //
 // End of "$Id$".
 //

Modified: trunk/src/run.cxx
===================================================================
--- trunk/src/run.cxx   2011-02-04 23:32:53 UTC (rev 8368)
+++ trunk/src/run.cxx   2011-02-05 00:48:46 UTC (rev 8369)
@@ -1327,6 +1327,18 @@
   return false;
 }
 
+/*! \fn FILE* fltk::fltk_fopen(const char* name, const char* flags)
+  Portably calls the fopen() function for different systems.
+  Note that ALL calls to this function MUST make sure the filename is
+  UTF8 encoded.
+
+  \param name UTF8 encoded directory name
+  \param flags Flags to pass to fopen (commonly 'r', 'w', etc...)
+  \see fltk::utf8froma()
+  \see fltk::utf8frommb();
+  \see fltk::utf8test();
+  \return Returns the same FILE* that fopen() returns
+*/
 //
 // End of "$Id$".
 //

Modified: trunk/src/win32/run.cxx
===================================================================
--- trunk/src/win32/run.cxx     2011-02-04 23:32:53 UTC (rev 8368)
+++ trunk/src/win32/run.cxx     2011-02-05 00:48:46 UTC (rev 8369)
@@ -2582,6 +2582,23 @@
 
 }; /* extern "C" */
 
+
+FILE* fltk::fltk_fopen(const char* name, const char* flags) {
+       wchar_t *wname, *wflags;
+       unsigned namelen, flaglen;
+
+       flaglen = utf8towc(flags, strlen(flags), NULL, 0);
+       wflags = new wchar_t [flaglen+10];
+       utf8towc(flags, strlen(flags), wflags, flaglen+1);
+       namelen = utf8towc(name, strlen(name), NULL, 0);
+       wname = new wchar_t [namelen+10];
+       utf8towc(name, strlen(name), wname, namelen+1);
+
+       FILE* ret = _wfopen(wname, wflags);
+       delete [] wname;
+       delete [] wflags;
+       return ret;
+}
 //
 // End of "$Id$".
 //

Modified: trunk/src/x11/run.cxx
===================================================================
--- trunk/src/x11/run.cxx       2011-02-04 23:32:53 UTC (rev 8368)
+++ trunk/src/x11/run.cxx       2011-02-05 00:48:46 UTC (rev 8369)
@@ -2791,6 +2791,10 @@
   }
 }
 
+FILE* fltk::fltk_fopen(const char* name, const char* flags) {
+  return fopen(name, flags);
+}
+
 //
 // End of "$Id$".
 //

_______________________________________________
fltk-commit mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-commit

Reply via email to