// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== // // // Creation Date: December 9, 1996 // // Description: // File Browser. // // Input Arguments: // String Callback on OK action // String action name for button and window title // String The type of file requested. // Int The browser mode. // 0 for single file read. // 1 for write. (export) // 2 for write without paths. (file save) // 4 directories have meaning when used with the action button. // 5 for multiple file read // // // Globals that may be used: // $gDefaultFileBrowserDir - If not "" then the file options // for default Dir will take you to the // directory specified. // // Return Value: // Done through the callback provided. The callback has the following // parameters: // String The file specified by the user. // String The type of file specified by the user. // // If the user wishes to provide more parameters to the callback they // may do so providing that the last two parameters are the ones given // above. // // The callback returns true if the window should be removed when // done. // // Eg. // myCallBack myParam1 myParam2 filePath fileType // global string $gDefaultFileBrowserDir = ""; global proc int pv_goDirectory (string $dirPath) // // Description: // Change the workspace directory to the given path // { if ($dirPath == "") { error (uiRes("m_fileBrowser.kInvalidDirectoryPath")); return 0; } else { workspace -dir $dirPath; return 1; } } proc int checkOldShortNames( int $mode ) { // Check for short names // These are being phased out of the UI, but we'll // strip out the 100 in case there are scripts around // which use it still. int $catch_oldShortNames = int($mode/100); $mode = $mode - 100 * $catch_oldShortNames; return $mode; } global proc int fileBrowserWithFilter ( string $callBack, string $action, string $title, string $type, int $mode, string $filters[], string $dir ) { global string $gOperationMode; global string $gFileDialogShortCutTip; global int $gBrowserMode; $gBrowserMode = checkOldShortNames( $mode ); $mode = $gBrowserMode; if (`about -nt` || `about -mac`) { string $filterList = "fileBrowserDialog -fileCommand "; $filterList += "\""; $filterList += $callBack; $filterList += "\""; $filterList += " -mode "; $filterList += $mode; $filterList += " -windowTitle "; $filterList += "\""; $filterList += $title; $filterList += "\""; $filterList += " -dialogStyle "; $filterList += 1; $filterList += " -fileType "; $filterList += "\""; $filterList += $type; $filterList += "\""; $filterList += " -actionName "; $filterList += "\""; $filterList += $action; $filterList += "\""; $filterList += " -operationMode "; $filterList += "\""; $filterList += $gOperationMode; $filterList += "\""; $filterList += " -includeName "; $filterList += "\""; $filterList += $dir; $filterList += "\""; int $i = 0; for ( $i=0; $i 0) { workspace -dir $gDefaultFileBrowserDir; } string $workspace = `workspace -q -dir`; // Defaults are single existing file for read. int $fileMode = 1; if ($mode == 4) { $fileMode = 3; } else if ( $mode == 5 ) { $fileMode = 4; } else if ($mode != 0) { $fileMode = 0; } string $options[] = fileBrowserActionSetup($type, $fileMode); string $fileCmd = ("fileDialog2 "); $fileCmd += (" -caption \"" + $action + "\""); $fileCmd += (" -okCaption \"" + $options[0] + "\""); $fileCmd += (" -fileMode " + $fileMode); $fileCmd += (" -startingDirectory \"" + $workspace + "\""); $fileCmd += (" -returnFilter 1"); $fileCmd += (" -fileFilter \"" + $options[2] + "\""); $fileCmd += (" -selectFileFilter \"" + $options[1] + "\""); // Obtain n+1 entries corresponding to the n files selected and // the fileType determined by the filter. Since returnFilter=1 // (see fileDialog2 command above) $n will never be 1. // $n = 0 if no files are selected and $n > 1 if at least one file is selected. // string $file[] = `eval $fileCmd`; int $n = size($file); // Now that we have all $n+1 entries ... // ... determine the filter type for the first $n entries. string $filterType = ""; if ( $n > 1 ) { string $last = $file[$n-1] ; if ( size( $last ) ) $filterType = filterDescriptionToFileType( $last ); } // ... and iterate over the first $n entries, performing $cmd for each. for ( $i=0; $i<$n-1; $i++ ) { // If the $i^th entry is empty, skip it if ( size($file[$i]) == 0 ) continue; // Otherwise ... string $path = $file[$i]; // ... strip the QT's .* postfix // OS native dialog does not have the .* postfix if the filter is * or *.* if ( $fileMode == 0 && endsWith($path, ".*") ) { $path = substring($path, 1, size($path)-2); } // ... get the full path to the file $path = fromNativePath( $path ); // ... compose the $cmd string $cmd = `format -s $callBack -s $path -s $filterType "^1s \"^2s\" \"^3s\""`; // ... and evaluate it eval $cmd; } return 1; }