+// begin wxmac
+
+#include "wx/mac/private.h"
+
+#include <Navigation.h>
+
+#ifdef __DARWIN__
+# include "MoreFilesX.h"
+#else
+# include "MoreFiles.h"
+# include "MoreFilesExtras.h"
+#endif
+
+extern bool gUseNavServices ;
+
+// the data we need to pass to our standard file hook routine
+// includes a pointer to the dialog, a pointer to the standard
+// file reply record (so we can inspect the current selection)
+// and a copy of the "previous" file spec of the reply record
+// so we can see if the selection has changed
+
+struct OpenUserDataRec {
+ int currentfilter ;
+ bool saveMode ;
+ wxArrayString name ;
+ wxArrayString extensions ;
+ wxArrayLong filtermactypes ;
+ NavMenuItemSpecArrayHandle menuitems ;
+};
+
+typedef struct OpenUserDataRec
+ OpenUserDataRec, *OpenUserDataRecPtr;
+
+static pascal void NavEventProc(
+ NavEventCallbackMessage inSelector,
+ NavCBRecPtr ioParams,
+ NavCallBackUserData ioUserData);
+
+#if TARGET_CARBON
+ static NavEventUPP sStandardNavEventFilter = NewNavEventUPP(NavEventProc);
+#else
+ static NavEventUPP sStandardNavEventFilter = NewNavEventProc(NavEventProc);
+#endif
+
+static pascal void
+NavEventProc(
+ NavEventCallbackMessage inSelector,
+ NavCBRecPtr ioParams,
+ NavCallBackUserData ioUserData )
+{
+ OpenUserDataRec * data = ( OpenUserDataRec *) ioUserData ;
+ if (inSelector == kNavCBEvent) {
+#if !TARGET_CARBON
+ wxTheApp->MacHandleOneEvent(ioParams->eventData.eventDataParms.event);
+#endif
+ }
+ else if ( inSelector == kNavCBStart )
+ {
+ if ( data->menuitems )
+ NavCustomControl(ioParams->context, kNavCtlSelectCustomType, &(*data->menuitems)[data->currentfilter]);
+ }
+ else if ( inSelector == kNavCBPopupMenuSelect )
+ {
+ NavMenuItemSpec * menu = (NavMenuItemSpec *) ioParams->eventData.eventDataParms.param ;
+ if ( menu->menuCreator == 'WXNG' )
+ {
+ data->currentfilter = menu->menuType ;
+ if ( data->saveMode )
+ {
+ int i = menu->menuType ;
+ wxString extension = data->extensions[i].AfterLast('.') ;
+ extension.MakeLower() ;
+ Str255 filename ;
+ // get the current filename
+ NavCustomControl(ioParams->context, kNavCtlGetEditFileName, &filename);
+ wxString sfilename = wxMacMakeStringFromPascal( filename ) ;
+ int pos = sfilename.Find('.',TRUE) ;
+ if ( pos != wxNOT_FOUND )
+ {
+ sfilename = sfilename.Left(pos+1)+extension ;
+ wxMacStringToPascal( sfilename , filename ) ;
+ NavCustomControl(ioParams->context, kNavCtlSetEditFileName, &filename);
+ }
+ }
+ }
+ }
+}
+
+const wxChar * gfilters[] =
+{
+ wxT("*.TXT") ,
+ wxT("*.TIF") ,
+ wxT("*.JPG") ,
+
+ NULL
+} ;
+
+OSType gfiltersmac[] =
+{
+ 'TEXT' ,
+ 'TIFF' ,
+ 'JPEG' ,
+
+ '****'
+} ;
+
+
+
+void MakeUserDataRec(OpenUserDataRec *myData , const wxString& filter )
+{
+ myData->menuitems = NULL ;
+ myData->currentfilter = 0 ;
+ myData->saveMode = FALSE ;
+
+ if ( filter && filter[0] )
+ {
+ wxString filter2(filter) ;
+ int filterIndex = 0;
+ bool isName = true ;
+ wxString current ;
+ for( unsigned int i = 0; i < filter2.Len() ; i++ )
+ {
+ if( filter2.GetChar(i) == wxT('|') )
+ {
+ if( isName ) {
+ myData->name.Add( current ) ;
+ }
+ else {
+ myData->extensions.Add( current.MakeUpper() ) ;
+ ++filterIndex ;
+ }
+ isName = !isName ;
+ current = wxEmptyString ;
+ }
+ else
+ {
+ current += filter2.GetChar(i) ;
+ }
+ }
+ // we allow for compatibility reason to have a single filter expression (like *.*) without
+ // an explanatory text, in that case the first part is name and extension at the same time
+
+ wxASSERT_MSG( filterIndex == 0 || !isName , wxT("incorrect format of format string") ) ;
+ if ( current.IsEmpty() )
+ myData->extensions.Add( myData->name[filterIndex] ) ;
+ else
+ myData->extensions.Add( current.MakeUpper() ) ;
+ if ( filterIndex == 0 || isName )
+ myData->name.Add( current.MakeUpper() ) ;
+
+ ++filterIndex ;
+
+
+ const size_t extCount = myData->extensions.GetCount();
+ for ( size_t i = 0 ; i < extCount; i++ )
+ {
+ int j ;
+ for ( j = 0 ; gfilters[j] ; j++ )
+ {
+ if ( myData->extensions[i] == gfilters[j] )
+ {
+ myData->filtermactypes.Add( gfiltersmac[j] ) ;
+ break ;
+ }
+ }
+ if( gfilters[j] == NULL )
+ {
+ myData->filtermactypes.Add( '****' ) ;
+ }
+ }
+ }
+}
+
+static Boolean CheckFile( ConstStr255Param name , OSType type , OpenUserDataRecPtr data)
+{
+/*
+ Str255 filename ;
+
+#if TARGET_CARBON
+ p2cstrcpy((char *)filename, name) ;
+#else
+ PLstrcpy( filename , name ) ;
+ p2cstr( filename ) ;
+#endif
+ wxString file(filename) ;
+*/
+ wxString file = wxMacMakeStringFromPascal( name ) ;
+ file.MakeUpper() ;
+
+ if ( data->extensions.GetCount() > 0 )
+ {
+ //for ( int i = 0 ; i < data->numfilters ; ++i )
+ int i = data->currentfilter ;
+ if ( data->extensions[i].Right(2) == wxT(".*") )
+ return true ;
+
+ {
+ if ( type == (OSType)data->filtermactypes[i] )
+ return true ;
+
+ wxStringTokenizer tokenizer( data->extensions[i] , wxT(";") ) ;
+ while( tokenizer.HasMoreTokens() )
+ {
+ wxString extension = tokenizer.GetNextToken() ;
+ if ( extension.GetChar(0) == '*' )
+ extension = extension.Mid(1) ;
+
+ if ( file.Len() >= extension.Len() && extension == file.Right(extension.Len() ) )
+ return true ;
+ }
+ }
+ return false ;
+ }
+ return true ;
+}
+
+#ifndef __DARWIN__
+static pascal Boolean CrossPlatformFileFilter(CInfoPBPtr myCInfoPBPtr, void *dataPtr)
+{
+ OpenUserDataRecPtr data = (OpenUserDataRecPtr) dataPtr ;
+ // return true if this item is invisible or a file
+
+ Boolean visibleFlag;
+ Boolean folderFlag;
+
+ visibleFlag = ! (myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible);
+ folderFlag = (myCInfoPBPtr->hFileInfo.ioFlAttrib & 0x10);
+
+ // because the semantics of the filter proc are "true means don't show
+ // it" we need to invert the result that we return
+
+ if ( !visibleFlag )
+ return true ;
+
+ if ( !folderFlag )
+ {
+ return !CheckFile( myCInfoPBPtr->hFileInfo.ioNamePtr , myCInfoPBPtr->hFileInfo.ioFlFndrInfo.fdType , data ) ;
+ }
+
+ return false ;
+}
+#endif
+
+// end wxmac
+
+wxString wxFileSelector(const wxChar *title,
+ const wxChar *defaultDir, const wxChar *defaultFileName,
+ const wxChar *defaultExtension, const wxChar *filter, int flags,