]> git.saurik.com Git - wxWidgets.git/blobdiff - src/mac/carbon/filedlg.cpp
Consistent tab behaviour
[wxWidgets.git] / src / mac / carbon / filedlg.cpp
index bbb873f7dee90bf0ecafd168879d2fc7f8bfcb2f..3385b176a9d5ac751524783e2080fd4fceb19e2e 100644 (file)
@@ -20,6 +20,7 @@
 #include "wx/filedlg.h"
 #include "wx/intl.h"
 #include "wx/tokenzr.h"
+#include "wx/filename.h"
 
 #ifndef __DARWIN__
   #include "PLStringFuncs.h"
@@ -104,6 +105,14 @@ NavEventProc(
             if (noErr == ::AECreateDesc(typeFSS, &theFSSpec, sizeof(FSSpec), &theLocation))
                 ::NavCustomControl(ioParams->context, kNavCtlSetLocation, (void *) &theLocation);
         }
+
+        NavMenuItemSpec  menuItem;
+        menuItem.version = kNavMenuItemSpecVersion;
+        menuItem.menuCreator = 'WXNG';
+        menuItem.menuType = data->currentfilter;
+        wxMacStringToPascal( data->name[data->currentfilter] , (StringPtr)(menuItem.menuItemName) ) ;
+        ::NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &menuItem);
+
 #else
         if ( data->menuitems )
             NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &(*data->menuitems)[data->currentfilter]);
@@ -113,6 +122,9 @@ NavEventProc(
     {
         NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
 #if TARGET_CARBON
+        const size_t numFilters = data->extensions.GetCount();
+
+        if ( menu->menuType < numFilters )
 #else
         if ( menu->menuCreator == 'WXNG' )
 #endif
@@ -140,7 +152,7 @@ NavEventProc(
                 {
                     sfilename = sfilename.Left(pos+1)+extension ;
 #if TARGET_CARBON
-                    cfString = sfilename ;
+                    cfString.Assign( sfilename , wxFONTENCODING_DEFAULT ) ;
                     NavDialogSetSaveFileName( ioParams->context , cfString ) ;
 #else
                     wxMacStringToPascal( sfilename , filename ) ;
@@ -152,24 +164,6 @@ NavEventProc(
     }
 }
 
-const wxChar * gfilters[] =
-{
-    wxT("*.TXT") ,
-    wxT("*.TIF") ,
-    wxT("*.JPG") ,
-
-    NULL
-} ;
-
-OSType gfiltersmac[] =
-{
-    'TEXT' ,
-    'TIFF' ,
-    'JPEG' ,
-
-    '****'
-} ;
-
 
 void MakeUserDataRec(OpenUserDataRec    *myData , const wxString& filter )
 {
@@ -218,19 +212,26 @@ void MakeUserDataRec(OpenUserDataRec    *myData , const wxString& filter )
         const size_t extCount = myData->extensions.GetCount();
         for ( size_t i = 0 ; i < extCount; i++ )
         {
-            int j ;
-            for ( j = 0 ; gfilters[j] ; j++ )
+            wxUint32 fileType;
+            wxUint32 creator;
+            wxString extension = myData->extensions[i];
+
+            if (extension.GetChar(0) == '*')
+                extension = extension.Mid(1);  // Remove leading *
+
+            if (extension.GetChar(0) == '.')
             {
-                if ( myData->extensions[i] == gfilters[j]  )
-                {
-                    myData->filtermactypes.Add( gfiltersmac[j] ) ;
-                    break ;
-                }
+                extension = extension.Mid(1);  // Remove leading .
             }
-            if( gfilters[j] == NULL )
+       
+            if (wxFileName::MacFindDefaultTypeAndCreator( extension, &fileType, &creator ))
             {
-                myData->filtermactypes.Add( '****' ) ;
+                myData->filtermactypes.Add( (OSType)fileType );
             }
+            else
+            {
+                myData->filtermactypes.Add( '****' ) ;         // We'll fail safe if it's not recognized
+               }
         }
     }
 }
@@ -332,10 +333,20 @@ pascal Boolean CrossPlatformFilterCallback (
             {
                 FSRef fsref ;
                 memcpy( &fsref , *theItem->dataHandle , sizeof(FSRef) ) ;
-                wxString file ;
-                const short maxpath = 1024 ;
-                FSRefMakePath( &fsref , (UInt8*) file.GetWriteBuf(maxpath+1),maxpath) ;
-                file.UngetWriteBuf() ;
+
+
+
+               CFURLRef fullURLRef;
+                fullURLRef = ::CFURLCreateFromFSRef(NULL, &fsref);
+#ifdef __UNIX__
+               CFURLPathStyle pathstyle = kCFURLPOSIXPathStyle;
+#else
+               CFURLPathStyle pathstyle = kCFURLHFSPathStyle;
+#endif
+               CFStringRef cfString = CFURLCopyFileSystemPath(fullURLRef, pathstyle);
+               ::CFRelease( fullURLRef ) ;
+               wxString file = wxMacCFStringHolder(cfString).AsString(wxFont::GetDefaultEncoding());
+
                 display = CheckFile( file , theInfo->fileAndFolder.fileInfo.finderInfo.fdType , data ) ;
             }
 #endif
@@ -356,67 +367,58 @@ int wxFileDialog::ShowModal()
     // this was always unset in the old code
     dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation;
 
-#if wxUSE_UNICODE
-    // tried using wxMacCFStringHolder in the code below, but it seems
-    // the CFStrings were being released before the save dialog was called,
-    // causing a crash - open dialog works fine with or without wxMacCFStringHolder
-    CFStringRef titleRef = ::CFStringCreateWithCharacters( kCFAllocatorDefault,
-                                (const unsigned short*)m_message.wc_str(), 
-                                m_message.Len() );
-#else
-    CFStringRef titleRef = ::CFStringCreateWithCString(NULL,
-                                                       m_message.c_str(),
-                                                       CFStringGetSystemEncoding());
-#endif
-    dialogCreateOptions.windowTitle = titleRef;
-#if wxUSE_UNICODE
-    CFStringRef defaultFileNameRef = ::CFStringCreateWithCharacters( kCFAllocatorDefault,
-                                        (const unsigned short*)m_fileName.wc_str(), 
-                                        m_fileName.Len() );
-#else
-    CFStringRef defaultFileNameRef = ::CFStringCreateWithCString(NULL,
-                                                                 m_fileName.c_str(),
-                                                                 CFStringGetSystemEncoding());
-#endif
-    dialogCreateOptions.saveFileName = defaultFileNameRef;
+    wxMacCFStringHolder message(m_message, m_font.GetEncoding());
+    dialogCreateOptions.windowTitle = message;
+
+    wxMacCFStringHolder defaultFileName(m_fileName, m_font.GetEncoding());
+    dialogCreateOptions.saveFileName = defaultFileName;
+
+
     NavDialogRef dialog;
     NavObjectFilterUPP navFilterUPP = NULL;
     CFArrayRef cfArray = NULL; // for popupExtension
     OpenUserDataRec myData;
     myData.defaultLocation = m_dir;
 
+    MakeUserDataRec(&myData , m_wildCard);
+    myData.currentfilter = m_filterIndex;
+    size_t numFilters = myData.extensions.GetCount();
+    if (numFilters)
+    {
+        CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
+            numFilters , &kCFTypeArrayCallBacks ) ;
+        dialogCreateOptions.popupExtension = popup ;
+        myData.menuitems = dialogCreateOptions.popupExtension ;
+        for ( size_t i = 0 ; i < numFilters ; ++i )
+        {
+            CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] , m_font.GetEncoding() ) ) ;
+        }
+    }
+
     if (m_dialogStyle & wxSAVE)
     {
-        dialogCreateOptions.optionFlags |= kNavNoTypePopup;
+        myData.saveMode = true;
+
+        if (!numFilters)
+        {
+            dialogCreateOptions.optionFlags |= kNavNoTypePopup;
+        }
         dialogCreateOptions.optionFlags |= kNavDontAutoTranslate;
         dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems;
 
         // The extension is important
-        dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
+        if (numFilters < 2)
+            dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension;
 
         err = ::NavCreatePutFileDialog(&dialogCreateOptions,
-                                       'TEXT',
-                                       'TEXT',
+                                       // Suppresses the 'Default' (top) menu item
+                                       kNavGenericSignature, kNavGenericSignature,
                                        sStandardNavEventFilter,
                                        &myData, // for defaultLocation
                                        &dialog);
     }
     else
     {
-        MakeUserDataRec(&myData , m_wildCard);
-        size_t numfilters = myData.extensions.GetCount();
-        if (numfilters > 0)
-        {
-            CFMutableArrayRef popup = CFArrayCreateMutable( kCFAllocatorDefault ,
-                numfilters , &kCFTypeArrayCallBacks ) ;
-            dialogCreateOptions.popupExtension = popup ;
-            myData.menuitems = dialogCreateOptions.popupExtension ;
-            for ( size_t i = 0 ; i < numfilters ; ++i )
-            {
-                CFArrayAppendValue( popup , (CFStringRef) wxMacCFStringHolder( myData.name[i] ) ) ;
-            }
-        }
-
         navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback);
         err = ::NavCreateGetFileDialog(&dialogCreateOptions,
                                        NULL, // NavTypeListHandle
@@ -434,20 +436,8 @@ int wxFileDialog::ShowModal()
     if (navFilterUPP)
         ::DisposeNavObjectFilterUPP(navFilterUPP);
     if (cfArray)
-    {
-        CFIndex n = ::CFArrayGetCount(cfArray);
-        for (CFIndex i = 0; i < n; i++)
-        {
-            CFStringRef str = (CFStringRef) ::CFArrayGetValueAtIndex(cfArray, i);
-            if (str)
-                ::CFRelease(str);
-        }
         ::CFRelease(cfArray);
-    }
-    if (titleRef)
-        ::CFRelease(titleRef);
-    if (defaultFileNameRef)
-        ::CFRelease(defaultFileNameRef);
+
     if (err != noErr)
         return wxID_CANCEL;
 
@@ -460,73 +450,48 @@ int wxFileDialog::ShowModal()
         Size        actualSize;
         FSRef       theFSRef;
         wxString thePath ;
+
+        m_filterIndex = myData.currentfilter ;
+
         long count;
         ::AECountItems(&navReply.selection , &count);
         for (long i = 1; i <= count; ++i)
         {
-            err = ::AEGetNthPtr(&(navReply.selection), 1, typeFSRef, &theKeyword, &actualType,
+            err = ::AEGetNthPtr(&(navReply.selection), i, typeFSRef, &theKeyword, &actualType,
                                 &theFSRef, sizeof(theFSRef), &actualSize);
             if (err != noErr)
                 break;
 
+            CFURLRef fullURLRef = 0 ;
             if (m_dialogStyle & wxSAVE)
             {
                 CFURLRef parentURLRef = ::CFURLCreateFromFSRef(NULL, &theFSRef);
 
                 if (parentURLRef)
                 {
-                    CFURLRef fullURLRef =
+                    fullURLRef =
                         ::CFURLCreateCopyAppendingPathComponent(NULL,
                                                                 parentURLRef,
                                                                 navReply.saveFileName,
                                                                 false);
                     ::CFRelease(parentURLRef);
-                    if (fullURLRef)
-                    {
-                        CFStringRef cfString = ::CFURLCopyPath(fullURLRef);
-                        ::CFRelease(fullURLRef);
-
-                        if (cfString)
-                        {
-                            // unescape the URL for
-                            // "file name" instead of "file%20name"
-                            CFStringRef cfStringUnescaped =
-                                ::CFURLCreateStringByReplacingPercentEscapes(NULL,
-                                                                             cfString,
-                                                                             CFSTR(""));
-                            ::CFRelease(cfString);
-
-                            if (cfStringUnescaped)
-                            {
-                                Size len = CFStringGetLength( cfStringUnescaped )  ;
-                                wxChar* buf = thePath.GetWriteBuf( len ) ;
-                                //buf[0] = '\0';
-#if wxUSE_UNICODE
-                                CFStringGetCharacters(cfStringUnescaped , CFRangeMake( 0 , len ) , (UniChar*) buf ) ;
-#else
-                                CFStringGetCString( cfStringUnescaped , buf , len+1 , CFStringGetSystemEncoding() ) ;
-#endif
-                                buf[len] = 0 ;
-                                wxMacConvertNewlines10To13( buf ) ;
-                                thePath.UngetWriteBuf() ;
-                                ::CFRelease(cfStringUnescaped);
-                            }
-                        }
-                    }
-                }
-                if (!thePath)
-                {
-                    ::NavDisposeReply(&navReply);
-                    return wxID_CANCEL;
                 }
             }
             else
             {
-                const short maxpath = 1024 ;
-                ::FSRefMakePath( &theFSRef , (UInt8*) thePath.GetWriteBuf(maxpath+1),maxpath) ;
-                thePath.UngetWriteBuf() ;
-                if (err != noErr)
-                    break;
+                fullURLRef = ::CFURLCreateFromFSRef(NULL, &theFSRef);
+            }
+#ifdef __UNIX__
+            CFURLPathStyle pathstyle = kCFURLPOSIXPathStyle;
+#else
+            CFURLPathStyle pathstyle = kCFURLHFSPathStyle;
+#endif
+            CFStringRef cfString = CFURLCopyFileSystemPath(fullURLRef, pathstyle);
+            thePath = wxMacCFStringHolder(cfString).AsString(m_font.GetEncoding());
+            if (!thePath)
+            {
+                ::NavDisposeReply(&navReply);
+                return wxID_CANCEL;
             }
             m_path = thePath;
             m_paths.Add(m_path);