OSErr squeakFindImage(char* pathName) { NavDialogCreationOptions dialogOptions; NavObjectFilterUPP filterProc = NewNavObjectFilterUPP(findImageFilterProc); OSErr anErr = noErr; NavDialogRef navDialog; FSRef fileAsFSRef; // Specify default options for dialog box anErr = NavGetDefaultDialogCreationOptions(&dialogOptions); if (anErr != noErr) return anErr; // Adjust the options to fit our needs // Set default location option dialogOptions.optionFlags |= kNavSelectDefaultLocation; dialogOptions.optionFlags |= kNavAllFilesInPopup; dialogOptions.optionFlags |= kNavSelectAllReadableItem; // Clear preview option dialogOptions.optionFlags ^= kNavAllowPreviews; // Call NavGetFile() with specified options and // declare our app-defined functions and type list NavCreateChooseFileDialog ( &dialogOptions, nil, nil, nil, filterProc, nil, &navDialog); anErr = NavDialogRun (navDialog); if (anErr != noErr ) return anErr; NavReplyRecord outReply; anErr = NavDialogGetReply (navDialog,&outReply); DisposeNavObjectFilterUPP(filterProc); NavDialogDispose(navDialog); if (anErr != noErr) return anErr; if (!outReply.validRecord) { anErr = NavDisposeReply(&outReply); return -1; } // Get the file anErr = AEGetNthPtr(&(outReply.selection), 1, typeFSRef, NULL, NULL, &fileAsFSRef, sizeof(FSRef), NULL); PathToFileViaFSRef(pathName,DOCUMENT_NAME_SIZE, &fileAsFSRef, gCurrentVMEncoding); // Dispose of NavReplyRecord, resources, descriptors anErr = NavDisposeReply(&outReply); return 0; }
static const NavObjectFilterUPP make_navFilterUPP() { if(mac_navFilterUPP) return mac_navFilterUPP; qAddPostRoutine(cleanup_navFilterUPP); return mac_navFilterUPP = NewNavObjectFilterUPP(qt_mac_nav_filter); }
bool nav_open_file(char *filetype,char *path) { NavDialogCreationOptions navoption; NavReplyRecord navreply; NavEventUPP navevent; NavObjectFilterUPP navfilter; AEKeyword keyword; DescType typecode; Size sz; NavDialogRef diagref; FSRef fsref; strcpy(nav_filetype,filetype); NavGetDefaultDialogCreationOptions(&navoption); navoption.optionFlags-=kNavDontAddTranslateItems; navoption.optionFlags-=kNavAllowPreviews; navevent=NewNavEventUPP(nav_event_proc); navfilter=NewNavObjectFilterUPP(nav_file_filter); NavCreateGetFileDialog(&navoption,NULL,navevent,NULL,navfilter,NULL,&diagref); NavDialogRun(diagref); NavDialogGetReply(diagref,&navreply); NavDialogDispose(diagref); DisposeNavEventUPP(navevent); DisposeNavObjectFilterUPP(navfilter); if (!navreply.validRecord) { NavDisposeReply(&navreply); return(FALSE); } AEGetNthPtr(&(navreply.selection),1,typeFSRef,&keyword,&typecode,(void*)&fsref,sizeof(FSRef),&sz); NavDisposeReply(&navreply); FSRefMakePath(&fsref,(unsigned char*)path,1024); return(TRUE); }
int wxFileDialog::ShowModal() { #if TARGET_CARBON OSErr err; NavDialogCreationOptions dialogCreateOptions; // set default options ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions); // this was always unset in the old code dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation; 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; if (m_dialogStyle & wxSAVE) { dialogCreateOptions.optionFlags |= kNavNoTypePopup; dialogCreateOptions.optionFlags |= kNavDontAutoTranslate; dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems; // The extension is important dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension; err = ::NavCreatePutFileDialog(&dialogCreateOptions, 'TEXT', 'TEXT', 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] , m_font.GetEncoding() ) ) ; } } navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback); err = ::NavCreateGetFileDialog(&dialogCreateOptions, NULL, // NavTypeListHandle sStandardNavEventFilter, NULL, // NavPreviewUPP navFilterUPP, (void *) &myData, // inClientData &dialog); } if (err == noErr) err = ::NavDialogRun(dialog); // clean up filter related data, etc. if (navFilterUPP) ::DisposeNavObjectFilterUPP(navFilterUPP); if (cfArray) ::CFRelease(cfArray); if (err != noErr) return wxID_CANCEL; NavReplyRecord navReply; err = ::NavDialogGetReply(dialog, &navReply); if (err == noErr && navReply.validRecord) { AEKeyword theKeyword; DescType actualType; Size actualSize; FSRef theFSRef; wxString thePath ; long count; ::AECountItems(&navReply.selection , &count); for (long i = 1; i <= count; ++i) { err = ::AEGetNthPtr(&(navReply.selection), i, typeFSRef, &theKeyword, &actualType, &theFSRef, sizeof(theFSRef), &actualSize); if (err != noErr) break; CFURLRef fullURLRef; if (m_dialogStyle & wxSAVE) { CFURLRef parentURLRef = ::CFURLCreateFromFSRef(NULL, &theFSRef); if (parentURLRef) { fullURLRef = ::CFURLCreateCopyAppendingPathComponent(NULL, parentURLRef, navReply.saveFileName, false); ::CFRelease(parentURLRef); } } else { 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); m_fileName = wxFileNameFromPath(m_path); m_fileNames.Add(m_fileName); } // set these to the first hit m_path = m_paths[0]; m_fileName = wxFileNameFromPath(m_path); m_dir = wxPathOnly(m_path); } ::NavDisposeReply(&navReply); return (err == noErr) ? wxID_OK : wxID_CANCEL; #else // TARGET_CARBON NavDialogOptions mNavOptions; NavObjectFilterUPP mNavFilterUPP = NULL; NavPreviewUPP mNavPreviewUPP = NULL ; NavReplyRecord mNavReply; AEDesc mDefaultLocation ; bool mSelectDefault = false ; OSStatus err = noErr ; // setup dialog mNavFilterUPP = nil; mNavPreviewUPP = nil; mSelectDefault = false; mDefaultLocation.descriptorType = typeNull; mDefaultLocation.dataHandle = nil; NavGetDefaultDialogOptions(&mNavOptions); wxMacStringToPascal( m_message , (StringPtr)mNavOptions.message ) ; wxMacStringToPascal( m_fileName , (StringPtr)mNavOptions.savedFileName ) ; // Set default location, the location // that's displayed when the dialog // first appears FSSpec location ; wxMacFilename2FSSpec( m_dir , &location ) ; err = ::AECreateDesc(typeFSS, &location, sizeof(FSSpec), &mDefaultLocation ); if ( mDefaultLocation.dataHandle ) { if (mSelectDefault) { mNavOptions.dialogOptionFlags |= kNavSelectDefaultLocation; } else { mNavOptions.dialogOptionFlags &= ~kNavSelectDefaultLocation; } } memset( &mNavReply , 0 , sizeof( mNavReply ) ) ; mNavReply.validRecord = false; mNavReply.replacing = false; mNavReply.isStationery = false; mNavReply.translationNeeded = false; mNavReply.selection.descriptorType = typeNull; mNavReply.selection.dataHandle = nil; mNavReply.keyScript = smSystemScript; mNavReply.fileTranslation = nil; mNavReply.version = kNavReplyRecordVersion ; // zero all data m_path = wxEmptyString ; m_fileName = wxEmptyString ; m_paths.Empty(); m_fileNames.Empty(); OpenUserDataRec myData; MakeUserDataRec( &myData , m_wildCard ) ; myData.currentfilter = m_filterIndex ; if ( myData.extensions.GetCount() > 0 ) { mNavOptions.popupExtension = (NavMenuItemSpecArrayHandle) NewHandle( sizeof( NavMenuItemSpec ) * myData.extensions.GetCount() ) ; myData.menuitems = mNavOptions.popupExtension ; for ( size_t i = 0 ; i < myData.extensions.GetCount() ; ++i ) { (*mNavOptions.popupExtension)[i].version = kNavMenuItemSpecVersion ; (*mNavOptions.popupExtension)[i].menuCreator = 'WXNG' ; // TODO : according to the new docs -1 to 10 are reserved for the OS (*mNavOptions.popupExtension)[i].menuType = i ; wxMacStringToPascal( myData.name[i] , (StringPtr)(*mNavOptions.popupExtension)[i].menuItemName ) ; } } if ( m_dialogStyle & wxSAVE ) { myData.saveMode = true ; mNavOptions.dialogOptionFlags |= kNavDontAutoTranslate ; mNavOptions.dialogOptionFlags |= kNavDontAddTranslateItems ; err = ::NavPutFile( &mDefaultLocation, &mNavReply, &mNavOptions, sStandardNavEventFilter , NULL, kNavGenericSignature, &myData); // User Data m_filterIndex = myData.currentfilter ; } else { myData.saveMode = false ; mNavFilterUPP = NewNavObjectFilterUPP( CrossPlatformFilterCallback ) ; if ( m_dialogStyle & wxMULTIPLE ) mNavOptions.dialogOptionFlags |= kNavAllowMultipleFiles ; else mNavOptions.dialogOptionFlags &= ~kNavAllowMultipleFiles ; err = ::NavGetFile( &mDefaultLocation, &mNavReply, &mNavOptions, sStandardNavEventFilter , mNavPreviewUPP, mNavFilterUPP, NULL , &myData); m_filterIndex = myData.currentfilter ; } DisposeNavObjectFilterUPP(mNavFilterUPP); if ( mDefaultLocation.dataHandle != nil ) { ::AEDisposeDesc(&mDefaultLocation); } if ( (err != noErr) && (err != userCanceledErr) ) { return wxID_CANCEL ; } if (mNavReply.validRecord) { FSSpec outFileSpec ; AEDesc specDesc ; AEKeyword keyWord ; long count ; ::AECountItems( &mNavReply.selection , &count ) ; for ( long i = 1 ; i <= count ; ++i ) { OSErr err = ::AEGetNthDesc( &mNavReply.selection , i , typeFSS, &keyWord , &specDesc); if ( err != noErr ) { m_path = wxT("") ; return wxID_CANCEL ; } outFileSpec = **(FSSpec**) specDesc.dataHandle; if (specDesc.dataHandle != nil) { ::AEDisposeDesc(&specDesc); } m_path = wxMacFSSpec2MacFilename( &outFileSpec ) ; m_paths.Add( m_path ) ; m_fileName = wxFileNameFromPath(m_path); m_fileNames.Add(m_fileName); } // set these to the first hit m_path = m_paths[ 0 ] ; m_fileName = wxFileNameFromPath(m_path); m_dir = wxPathOnly(m_path); NavDisposeReply( &mNavReply ) ; return wxID_OK ; } return wxID_CANCEL; #endif // TARGET_CARBON }
void RequestMidiFilename(std::wstring *returned_filename, std::wstring *returned_file_title) { // Grab the filename of the last song we played // and pre-load it into the open dialog wstring last_filename = UserSetting::Get(L"Last File", L""); const static int BufferSize = 512; wchar_t filename[BufferSize] = L""; wchar_t filetitle[BufferSize] = L""; #ifdef WIN32 // Try to populate our "File Open" box with the last file selected if (StringCbCopyW(filename, BufferSize, last_filename.c_str()) == STRSAFE_E_INSUFFICIENT_BUFFER) { // If there wasn't a last file, default to the built-in Music directory filename[0] = L'\0'; } wstring default_dir; bool default_directory = false; if (last_filename.length() == 0) { default_directory = true; default_dir = UserSetting::Get(L"Default Music Directory", L""); if (!SetCurrentDirectory(default_dir.c_str())) { // LOGTODO! // This is non-critical. No action required. } } OPENFILENAME ofn; ZeroMemory(&ofn, sizeof(OPENFILENAME)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = 0; ofn.lpstrTitle = L"Piano Game: Choose a MIDI song to play"; ofn.lpstrFilter = L"MIDI Files (*.mid)\0*.mid;*.midi\0All Files (*.*)\0*.*\0"; ofn.lpstrFile = filename; ofn.nMaxFile = BufferSize; ofn.lpstrFileTitle = filetitle; ofn.lpstrInitialDir = default_dir.c_str(); ofn.nMaxFileTitle = BufferSize; ofn.lpstrDefExt = L"mid"; ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST; if (GetOpenFileName(&ofn)) { std::wstring filename = WSTRING(ofn.lpstrFile); SetLastMidiFilename(filename); if (returned_file_title) *returned_file_title = WSTRING(filetitle); if (returned_filename) *returned_filename = filename; return; } if (returned_file_title) *returned_file_title = L""; if (returned_filename) *returned_filename = L""; #else OSStatus status; NavDialogCreationOptions options; status = NavGetDefaultDialogCreationOptions(&options); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't create dialog options. Error code: " << static_cast<int>(status))); options.windowTitle = CFSTR("Piano Game: Choose a MIDI song to play"); // TODO: Should clean this up at shut-down static NavObjectFilterUPP navFilterUPP(0); if (navFilterUPP == 0) navFilterUPP = NewNavObjectFilterUPP(NavOpenFilterProc); NavDialogRef navDialog(0); status = NavCreateChooseFileDialog(&options, 0, 0, 0, navFilterUPP, 0, &navDialog); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't create open dialog. Error code: " << static_cast<int>(status))); status = NavDialogRun(navDialog); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't run open dialog. Error code: " << static_cast<int>(status))); NavReplyRecord navReply; status = NavDialogGetReply(navDialog, &navReply); if (status == userCanceledErr || !navReply.validRecord) { NavDisposeReply(&navReply); if (returned_file_title) *returned_file_title = L""; if (returned_filename) *returned_filename = L""; return; } long item_count = 0; status = AECountItems(&navReply.selection, &item_count); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't count resulting items from open dialog. Error code: " << static_cast<int>(status))); for (long i = 1; i <= item_count; i++) { FSRef fsRef; status = AEGetNthPtr(&navReply.selection, i, typeFSRef, 0, 0, &fsRef, sizeof(FSRef), 0); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't get FSRef pointer from open dialog. Error code: " << static_cast<int>(status))); CFStringRef file_title; status = LSCopyDisplayNameForRef( &fsRef, &file_title ); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't get file title. Error code: " << static_cast<int>(status))); const static int BufferSize(1024); char path_buffer[BufferSize]; status = FSRefMakePath(&fsRef, (UInt8*)path_buffer, BufferSize); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't get file path. Error code: " << static_cast<int>(status))); std::string narrow_path(path_buffer); std::wstring filepath(narrow_path.begin(), narrow_path.end()); if (returned_file_title) *returned_file_title = WideFromMacString(file_title); if (returned_filename) *returned_filename = filepath; CFRelease(file_title); } NavDisposeReply(&navReply); #endif }
int wxFileDialog::ShowModal() { WX_HOOK_MODAL_DIALOG(); m_paths.Empty(); m_fileNames.Empty(); OSErr err; NavDialogCreationOptions dialogCreateOptions; // set default options ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions); // this was always unset in the old code dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation; wxCFStringRef message(m_message, GetFont().GetEncoding()); dialogCreateOptions.windowTitle = message; wxCFStringRef defaultFileName(m_fileName, GetFont().GetEncoding()); dialogCreateOptions.saveFileName = defaultFileName; NavDialogRef dialog; NavObjectFilterUPP navFilterUPP = NULL; OpenUserDataRec myData( this ); dialogCreateOptions.popupExtension = myData.GetMenuItems(); if (HasFdFlag(wxFD_SAVE)) { dialogCreateOptions.optionFlags |= kNavDontAutoTranslate; dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems; if (dialogCreateOptions.popupExtension == NULL) dialogCreateOptions.optionFlags |= kNavNoTypePopup; // The extension is important if ( dialogCreateOptions.popupExtension == NULL || CFArrayGetCount(dialogCreateOptions.popupExtension)<2) dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension; if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT)) dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement; err = ::NavCreatePutFileDialog( &dialogCreateOptions, kNavGenericSignature, // Suppresses the 'Default' (top) menu item kNavGenericSignature, sStandardNavEventFilter, &myData, // for defaultLocation &dialog ); } else { // let the user select bundles/programs in dialogs dialogCreateOptions.optionFlags |= kNavSupportPackages; navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback); err = ::NavCreateGetFileDialog( &dialogCreateOptions, NULL, // NavTypeListHandle sStandardNavEventFilter, NULL, // NavPreviewUPP navFilterUPP, (void *) &myData, // inClientData &dialog ); } SetupExtraControls(NavDialogGetWindow(dialog)); if (err == noErr) { wxDialog::OSXBeginModalDialog(); err = ::NavDialogRun(dialog); wxDialog::OSXEndModalDialog(); } // clean up filter related data, etc. if (navFilterUPP) ::DisposeNavObjectFilterUPP(navFilterUPP); if (err != noErr) { ::NavDialogDispose(dialog); return wxID_CANCEL; } NavReplyRecord navReply; err = ::NavDialogGetReply(dialog, &navReply); if (err == noErr && navReply.validRecord) { AEKeyword theKeyword; DescType actualType; Size actualSize; FSRef theFSRef; wxString thePath ; long count; m_filterIndex = myData.GetCurrentFilter(); ::AECountItems( &navReply.selection, &count ); for (long i = 1; i <= count; ++i) { err = ::AEGetNthPtr( &(navReply.selection), i, typeFSRef, &theKeyword, &actualType, &theFSRef, sizeof(theFSRef), &actualSize ); if (err != noErr) break; if (HasFdFlag(wxFD_SAVE)) thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName ); else thePath = wxMacFSRefToPath( &theFSRef ); if (!thePath) { ::NavDisposeReply(&navReply); ::NavDialogDispose(dialog); return wxID_CANCEL; } m_path = thePath; m_paths.Add(m_path); m_fileName = wxFileNameFromPath(m_path); m_fileNames.Add(m_fileName); } // set these to the first hit m_path = m_paths[0]; m_fileName = wxFileNameFromPath(m_path); m_dir = wxPathOnly(m_path); } UnsubclassWin(); ::NavDisposeReply(&navReply); ::NavDialogDispose(dialog); return (err == noErr) ? wxID_OK : wxID_CANCEL; }
int wxFileDialog::ShowModal() { OSErr err; NavDialogCreationOptions dialogCreateOptions; // set default options ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions); // this was always unset in the old code dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation; wxCFStringRef message(m_message, GetFont().GetEncoding()); dialogCreateOptions.windowTitle = message; wxCFStringRef defaultFileName(m_fileName, GetFont().GetEncoding()); dialogCreateOptions.saveFileName = defaultFileName; NavDialogRef dialog; NavObjectFilterUPP navFilterUPP = NULL; 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) wxCFStringRef( myData.name[i] , GetFont().GetEncoding() ) ) ; } } if (HasFdFlag(wxFD_SAVE)) { myData.saveMode = true; dialogCreateOptions.optionFlags |= kNavDontAutoTranslate; dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems; if (!numFilters) dialogCreateOptions.optionFlags |= kNavNoTypePopup; // The extension is important if (numFilters < 2) dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension; if (!(m_windowStyle & wxFD_OVERWRITE_PROMPT)) dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement; err = ::NavCreatePutFileDialog( &dialogCreateOptions, kNavGenericSignature, // Suppresses the 'Default' (top) menu item kNavGenericSignature, sStandardNavEventFilter, &myData, // for defaultLocation &dialog ); } else { // let the user select bundles/programs in dialogs dialogCreateOptions.optionFlags |= kNavSupportPackages; navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback); err = ::NavCreateGetFileDialog( &dialogCreateOptions, NULL, // NavTypeListHandle sStandardNavEventFilter, NULL, // NavPreviewUPP navFilterUPP, (void *) &myData, // inClientData &dialog ); } if (err == noErr) err = ::NavDialogRun(dialog); // clean up filter related data, etc. if (navFilterUPP) ::DisposeNavObjectFilterUPP(navFilterUPP); if (err != noErr) { ::NavDialogDispose(dialog); return wxID_CANCEL; } NavReplyRecord navReply; err = ::NavDialogGetReply(dialog, &navReply); if (err == noErr && navReply.validRecord) { AEKeyword theKeyword; DescType actualType; Size actualSize; FSRef theFSRef; wxString thePath ; long count; m_filterIndex = myData.currentfilter; ::AECountItems( &navReply.selection, &count ); for (long i = 1; i <= count; ++i) { err = ::AEGetNthPtr( &(navReply.selection), i, typeFSRef, &theKeyword, &actualType, &theFSRef, sizeof(theFSRef), &actualSize ); if (err != noErr) break; if (HasFdFlag(wxFD_SAVE)) thePath = wxMacFSRefToPath( &theFSRef, navReply.saveFileName ); else thePath = wxMacFSRefToPath( &theFSRef ); if (!thePath) { ::NavDisposeReply(&navReply); ::NavDialogDispose(dialog); return wxID_CANCEL; } m_path = thePath; m_paths.Add(m_path); m_fileName = wxFileNameFromPath(m_path); m_fileNames.Add(m_fileName); } // set these to the first hit m_path = m_paths[0]; m_fileName = wxFileNameFromPath(m_path); m_dir = wxPathOnly(m_path); } ::NavDisposeReply(&navReply); ::NavDialogDispose(dialog); return (err == noErr) ? wxID_OK : wxID_CANCEL; }
int FileDialog::ShowModal() { OSErr err; NavDialogCreationOptions dialogCreateOptions; // set default options ::NavGetDefaultDialogCreationOptions(&dialogCreateOptions); // this was always unset in the old code dialogCreateOptions.optionFlags &= ~kNavSelectDefaultLocation; wxMacCFStringHolder message(m_message, GetFont().GetEncoding()); dialogCreateOptions.windowTitle = message; wxMacCFStringHolder defaultFileName(m_fileName, GetFont().GetEncoding()); dialogCreateOptions.saveFileName = defaultFileName; NavDialogRef dialog; NavObjectFilterUPP navFilterUPP = NULL; CustomData myData; SetRect(&myData.bounds, 0, 0, 0, 0); myData.me = this; myData.window = NULL; myData.defaultLocation = m_dir; myData.userpane = NULL; myData.choice = NULL; myData.button = NULL; myData.saveMode = false; myData.showing = true; Rect r; SInt16 base; SInt16 margin = 3; SInt16 gap = 0; MakeUserDataRec(&myData , m_wildCard); myData.currentfilter = m_filterIndex; size_t numFilters = myData.extensions.GetCount(); if (numFilters) { CreateNewMenu(0, 0, &myData.menu); for ( size_t i = 0 ; i < numFilters ; ++i ) { ::AppendMenuItemTextWithCFString(myData.menu, wxMacCFStringHolder(myData.name[i], GetFont().GetEncoding()), 4, i, NULL); } SetRect(&r, 0, margin, 0, 0); CreatePopupButtonControl(NULL, &r, CFSTR("Format:"), -12345, true, -1, teJustLeft, normal, &myData.choice); SetControlID(myData.choice, &kChoiceID); SetControlPopupMenuRef(myData.choice, myData.menu); SetControl32BitMinimum(myData.choice, 1); SetControl32BitMaximum(myData.choice, myData.name.GetCount()); SetControl32BitValue(myData.choice, myData.currentfilter + 1); GetBestControlRect(myData.choice, &r, &base); SizeControl(myData.choice, r.right - r.left, r.bottom - r.top); UnionRect(&myData.bounds, &r, &myData.bounds); gap = 15; HIObjectSetAuxiliaryAccessibilityAttribute((HIObjectRef)myData.choice, 0, kAXDescriptionAttribute, CFSTR("Format")); } if (!m_buttonlabel.IsEmpty()) { wxMacCFStringHolder cfString(wxStripMenuCodes(m_buttonlabel).c_str(), wxFONTENCODING_DEFAULT); SetRect(&r, myData.bounds.right + gap, margin, 0, 0); CreatePushButtonControl(NULL, &r, cfString, &myData.button); SetControlID(myData.button, &kButtonID); GetBestControlRect(myData.button, &r, &base); SizeControl(myData.button, r.right - r.left, r.bottom - r.top); UnionRect(&myData.bounds, &r, &myData.bounds); } // Expand bounding rectangle to include a top and bottom margin myData.bounds.top -= margin; myData.bounds.bottom += margin; dialogCreateOptions.optionFlags |= kNavNoTypePopup; if (m_dialogStyle & wxFD_SAVE) { dialogCreateOptions.modality = kWindowModalityWindowModal; dialogCreateOptions.parentWindow = (WindowRef) GetParent()->MacGetTopLevelWindowRef(); myData.saveMode = true; if (!numFilters) { dialogCreateOptions.optionFlags |= kNavNoTypePopup; } dialogCreateOptions.optionFlags |= kNavDontAutoTranslate; dialogCreateOptions.optionFlags |= kNavDontAddTranslateItems; // The extension is important if (numFilters < 2) dialogCreateOptions.optionFlags |= kNavPreserveSaveFileExtension; #if TARGET_API_MAC_OSX if (!(m_dialogStyle & wxFD_OVERWRITE_PROMPT)) { dialogCreateOptions.optionFlags |= kNavDontConfirmReplacement; } #endif err = ::NavCreatePutFileDialog(&dialogCreateOptions, // Suppresses the 'Default' (top) menu item kNavGenericSignature, kNavGenericSignature, sStandardNavEventFilter, &myData, // for defaultLocation &dialog); } else { //let people select bundles/programs in dialogs dialogCreateOptions.optionFlags |= kNavSupportPackages; navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback); err = ::NavCreateGetFileDialog(&dialogCreateOptions, NULL, // NavTypeListHandle sStandardNavEventFilter, NULL, // NavPreviewUPP navFilterUPP, (void *) &myData, // inClientData &dialog); } if (err == noErr) err = ::NavDialogRun(dialog); if (err == noErr) { myData.window = NavDialogGetWindow(dialog); Rect r; // This creates our "fake" dialog with the same dimensions as the sheet so // that Options dialogs will center properly on the sheet. The "fake" dialog // is never actually seen. GetWindowBounds(myData.window, kWindowStructureRgn, &r); wxDialog::Create(NULL, // no parent...otherwise strange things happen wxID_ANY, wxEmptyString, wxPoint(r.left, r.top), wxSize(r.right - r.left, r.bottom - r.top)); BeginAppModalStateForWindow(myData.window); while (myData.showing) { wxTheApp->MacDoOneEvent(); } EndAppModalStateForWindow(myData.window); } // clean up filter related data, etc. if (navFilterUPP) ::DisposeNavObjectFilterUPP(navFilterUPP); if (err != noErr) return wxID_CANCEL; NavReplyRecord navReply; err = ::NavDialogGetReply(dialog, &navReply); if (err == noErr && navReply.validRecord) { AEKeyword theKeyword; DescType actualType; 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), i, typeFSRef, &theKeyword, &actualType, &theFSRef, sizeof(theFSRef), &actualSize); if (err != noErr) break; if (m_dialogStyle & wxFD_SAVE) thePath = wxMacFSRefToPath( &theFSRef , navReply.saveFileName ) ; else thePath = wxMacFSRefToPath( &theFSRef ) ; if (!thePath) { ::NavDisposeReply(&navReply); return wxID_CANCEL; } m_path = ConvertSlashInFileName(thePath); m_paths.Add(m_path); m_fileName = wxFileNameFromPath(m_path); m_fileNames.Add(m_fileName); } // set these to the first hit m_path = m_paths[0]; m_fileName = wxFileNameFromPath(m_path); m_dir = wxPathOnly(m_path); } ::NavDisposeReply(&navReply); return (err == noErr) ? wxID_OK : wxID_CANCEL; }
bool showOpenFileDialog(std::string& result, const std::string& path, const std::string& filetypes) { class Error : public std::exception { public: Error(OSStatus status) : std::exception(), _status(status) {} OSStatus getStatus() { return _status; } virtual const char* what() { return "Error while showing OpenFileDialog"; } static void checkForError(OSStatus status) { if (status != noErr) throw Error(status); } virtual ~Error() throw () {} private: OSStatus _status; }; OSStatus status; NavDialogRef dlg(NULL); NavObjectFilterUPP navFilterUPP = NULL; NavDialogCreationOptions myDialogOptions; NavReplyRecord navReply; bool success(false); try { status = NavGetDefaultDialogCreationOptions (&myDialogOptions); myDialogOptions.modality = kWindowModalityAppModal; DialogData data; data.defaultLocation = (path.empty()) ? "" : osgDB::findDataFile(path); if (!filetypes.empty()) { navFilterUPP = NewNavObjectFilterUPP(CrossPlatformFilterCallback); std::vector<std::string> exts; cefix::strTokenize(filetypes,exts,","); data.allowedExtensions.insert(exts.begin(), exts.end()); } status = NavCreateChooseFileDialog(&myDialogOptions, NULL, sStandardNavEventFilter, NULL, navFilterUPP, &data, &dlg); Error::checkForError(status); status = NavDialogRun (dlg); Error::checkForError(status); NavUserAction userAction = NavDialogGetUserAction(dlg); if(userAction == kNavUserActionChoose) { const unsigned int max_path = 2048; FSRef fsRef; status = NavDialogGetReply( dlg, &navReply ); Error::checkForError(status); status = AEGetNthPtr(&(navReply.selection), 1, typeFSRef, NULL, NULL, &fsRef, sizeof(FSRef), NULL); char path[max_path]; status = FSRefMakePath ( &fsRef, (UInt8*)path, max_path ); NavDisposeReply ( &navReply ); result = std::string(path); success = true; } } catch (Error e) { log::error("showOpenFileDialog") << "failed with " << e.getStatus() << std::endl; success = false; } if (navFilterUPP) ::DisposeNavObjectFilterUPP(navFilterUPP); if (dlg) NavDialogDispose (dlg); return success; }