changeset 1298:79db0563c7c2

Code to split the file and path apart on Mac... Also fixed issues with how we construct the folder URL.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 01 Nov 2011 13:05:26 +0000
parents b8f6e7dd4544
children 66d4e16349e0
files mac/dw.m
diffstat 1 files changed, 54 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/mac/dw.m	Tue Nov 01 12:21:33 2011 +0000
+++ b/mac/dw.m	Tue Nov 01 13:05:26 2011 +0000
@@ -3126,17 +3126,61 @@
  */
 char * API dw_file_browse(char *title, char *defpath, char *ext, int flags)
 {
+    char temp[PATH_MAX+1];
+    char *file = NULL, *path = NULL;
+    
+    /* Figure out path information */
+    if(defpath)
+    {
+        struct stat buf;
+        
+        /* Get an absolute path */
+        if(!realpath(defpath, temp))
+            strcpy(temp, defpath);
+        
+        /* Check if the defpath exists */
+        if(stat(temp, &buf) == 0)
+        {
+            /* Can be a directory or file */
+            if(buf.st_mode & S_IFDIR)
+                path = temp;
+            else
+                file = temp;
+        }
+        /* If it wasn't a directory... check if there is a path */
+        if(!path && strchr(temp, '/'))
+        {
+            unsigned long x = strlen(temp) - 1;
+            
+            /* Trim off the filename */
+            while(x > 0 && temp[x] != '/')
+            {
+                x--;
+            }
+            if(temp[x] == '/')
+            {
+                temp[x] = 0;
+                /* Check to make sure the trimmed piece is a directory */
+                if(stat(temp, &buf) == 0)
+                {
+                    if(buf.st_mode & S_IFDIR)
+                    {
+                        /* We now have it split */
+                        path = temp;
+                        file = &temp[x+1];
+                    }
+                }
+            }
+        }
+    }
+    
     if(flags == DW_FILE_OPEN || flags == DW_DIRECTORY_OPEN)
     {
         /* Create the File Open Dialog class. */
         NSOpenPanel* openDlg = [NSOpenPanel openPanel];
-        struct stat buf;
         
-        if(defpath && stat(defpath, &buf) == 0)
-        {
-            if(buf.st_mode & S_IFDIR)
-                [openDlg setDirectoryURL:[NSURL URLWithString:[NSString stringWithUTF8String:defpath]]];
-        }
+        if(path)
+            [openDlg setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:path]]];
 
         /* Enable the selection of files in the dialog. */
         if(flags == DW_FILE_OPEN)
@@ -3178,15 +3222,11 @@
     {
         /* Create the File Save Dialog class. */
         NSSavePanel* saveDlg = [NSSavePanel savePanel];
-        struct stat buf;
         
-        if(defpath && stat(defpath, &buf) == 0)
-        {
-            if(buf.st_mode & S_IFDIR)
-                [saveDlg setDirectoryURL:[NSURL URLWithString:[NSString stringWithUTF8String:defpath]]];
-            else
-                [saveDlg setNameFieldStringValue:[NSString stringWithUTF8String:defpath]];
-        }
+        if(path)
+            [saveDlg setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:path]]];
+        if(file)
+            [saveDlg setNameFieldStringValue:[NSString stringWithUTF8String:file]];
 
         /* Enable the creation of directories in the dialog. */
         [saveDlg setCanCreateDirectories:YES];