nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList, const nfdchar_t *defaultPath, nfdchar_t **outPath ) { GtkWidget *dialog; nfdresult_t result; if ( !gtk_init_check( NULL, NULL ) ) { NFDi_SetError(INIT_FAIL_MSG); return NFD_ERROR; } dialog = gtk_file_chooser_dialog_new( "Save File", NULL, GTK_FILE_CHOOSER_ACTION_SAVE, "_Cancel", GTK_RESPONSE_CANCEL, "_Save", GTK_RESPONSE_ACCEPT, NULL ); gtk_file_chooser_set_do_overwrite_confirmation( GTK_FILE_CHOOSER(dialog), TRUE ); /* Build the filter list */ AddFiltersToDialog(dialog, filterList); /* Set the default path */ SetDefaultPath(dialog, defaultPath); result = NFD_CANCEL; if ( gtk_dialog_run( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT ) { char *filename; filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(dialog) ); { size_t len = strlen(filename); *outPath = NFDi_Malloc( len + 1 ); memcpy( *outPath, filename, len + 1 ); if ( !*outPath ) { g_free( filename ); gtk_widget_destroy(dialog); return NFD_ERROR; } } g_free(filename); result = NFD_OKAY; } WaitForCleanup(); gtk_widget_destroy(dialog); WaitForCleanup(); return result; }
nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList, const nfdchar_t *defaultPath, nfdpathset_t *outPaths ) { GtkWidget *dialog; nfdresult_t result; if ( !gtk_init_check( NULL, NULL ) ) { NFDi_SetError(INIT_FAIL_MSG); return NFD_ERROR; } dialog = gtk_file_chooser_dialog_new( "Open Files", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, "_Cancel", GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, NULL ); gtk_file_chooser_set_select_multiple( GTK_FILE_CHOOSER(dialog), TRUE ); /* Build the filter list */ AddFiltersToDialog(dialog, filterList); /* Set the default path */ SetDefaultPath(dialog, defaultPath); result = NFD_CANCEL; if ( gtk_dialog_run( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT ) { GSList *fileList = gtk_file_chooser_get_filenames( GTK_FILE_CHOOSER(dialog) ); if ( AllocPathSet( fileList, outPaths ) == NFD_ERROR ) { gtk_widget_destroy(dialog); return NFD_ERROR; } result = NFD_OKAY; } WaitForCleanup(); gtk_widget_destroy(dialog); WaitForCleanup(); return result; }
nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList, const nfdchar_t *defaultPath, nfdchar_t **outPath ) { nfdresult_t nfdResult = NFD_ERROR; // Init COM library. HRESULT result = ::CoInitializeEx(NULL, ::COINIT_APARTMENTTHREADED | ::COINIT_DISABLE_OLE1DDE ); if ( !SUCCEEDED(result)) { NFDi_SetError("Could not initialize COM."); return NFD_ERROR; } ::IFileSaveDialog *fileSaveDialog(NULL); // Create dialog result = ::CoCreateInstance(::CLSID_FileSaveDialog, NULL, CLSCTX_ALL, ::IID_IFileSaveDialog, reinterpret_cast<void**>(&fileSaveDialog) ); if ( !SUCCEEDED(result) ) { NFDi_SetError("Could not create dialog."); goto end; } // Build the filter list if ( !AddFiltersToDialog( fileSaveDialog, filterList ) ) { goto end; } // Set the default path if ( !SetDefaultPath( fileSaveDialog, defaultPath ) ) { goto end; } // Show the dialog. result = fileSaveDialog->Show(NULL); if ( SUCCEEDED(result) ) { // Get the file name ::IShellItem *shellItem; result = fileSaveDialog->GetResult(&shellItem); if ( !SUCCEEDED(result) ) { NFDi_SetError("Could not get shell item from dialog."); goto end; } wchar_t *filePath(NULL); result = shellItem->GetDisplayName(::SIGDN_FILESYSPATH, &filePath); if ( !SUCCEEDED(result) ) { NFDi_SetError("Could not get file path for selected."); goto end; } CopyWCharToNFDChar( filePath, outPath ); CoTaskMemFree(filePath); if ( !*outPath ) { /* error is malloc-based, error message would be redundant */ goto end; } nfdResult = NFD_OKAY; shellItem->Release(); } else if (result == HRESULT_FROM_WIN32(ERROR_CANCELLED) ) { nfdResult = NFD_CANCEL; } else { NFDi_SetError("File dialog box show failed."); nfdResult = NFD_ERROR; } end: ::CoUninitialize(); return nfdResult; }
nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList, const nfdchar_t *defaultPath, nfdpathset_t *outPaths ) { nfdresult_t nfdResult = NFD_ERROR; // Init COM library. HRESULT result = ::CoInitializeEx(NULL, ::COINIT_APARTMENTTHREADED | ::COINIT_DISABLE_OLE1DDE ); if ( !SUCCEEDED(result)) { NFDi_SetError("Could not initialize COM."); return NFD_ERROR; } ::IFileOpenDialog *fileOpenDialog(NULL); // Create dialog result = ::CoCreateInstance(::CLSID_FileOpenDialog, NULL, CLSCTX_ALL, ::IID_IFileOpenDialog, reinterpret_cast<void**>(&fileOpenDialog) ); if ( !SUCCEEDED(result) ) { NFDi_SetError("Could not create dialog."); goto end; } // Build the filter list if ( !AddFiltersToDialog( fileOpenDialog, filterList ) ) { goto end; } // Set the default path if ( !SetDefaultPath( fileOpenDialog, defaultPath ) ) { goto end; } // Set a flag for multiple options DWORD dwFlags; result = fileOpenDialog->GetOptions(&dwFlags); if ( !SUCCEEDED(result) ) { NFDi_SetError("Could not get options."); goto end; } result = fileOpenDialog->SetOptions(dwFlags | FOS_ALLOWMULTISELECT); if ( !SUCCEEDED(result) ) { NFDi_SetError("Could not set options."); goto end; } // Show the dialog. result = fileOpenDialog->Show(NULL); if ( SUCCEEDED(result) ) { IShellItemArray *shellItems; result = fileOpenDialog->GetResults( &shellItems ); if ( !SUCCEEDED(result) ) { NFDi_SetError("Could not get shell items."); goto end; } if ( AllocPathSet( shellItems, outPaths ) == NFD_ERROR ) { goto end; } shellItems->Release(); nfdResult = NFD_OKAY; } else if (result == HRESULT_FROM_WIN32(ERROR_CANCELLED) ) { nfdResult = NFD_CANCEL; } else { NFDi_SetError("File dialog box show failed."); nfdResult = NFD_ERROR; } end: ::CoUninitialize(); return nfdResult; }