guiFileTreeCtrl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gui/controls/guiFileTreeCtrl.h"
  23. #include "core/strings/findMatch.h"
  24. #include "core/frameAllocator.h"
  25. #include "core/strings/stringUnit.h"
  26. #include "console/consoleTypes.h"
  27. #include "console/engineAPI.h"
  28. IMPLEMENT_CONOBJECT(GuiFileTreeCtrl);
  29. ConsoleDocClass( GuiFileTreeCtrl,
  30. "@brief A control that displays a hierarchical tree view of a path in the game file system.\n\n"
  31. "@note Currently not used, most likely existed for editors. Possibly deprecated.\n\n"
  32. "@internal"
  33. );
  34. static bool _isDirInMainDotCsPath(const char* dir)
  35. {
  36. StringTableEntry cs = Platform::getMainDotCsDir();
  37. U32 len = dStrlen(cs) + dStrlen(dir) + 2;
  38. FrameTemp<UTF8> fullpath(len);
  39. dSprintf(fullpath, len, "%s/%s", cs, dir);
  40. return Platform::isDirectory(fullpath);
  41. }
  42. static bool _hasChildren(const char* path)
  43. {
  44. if( Platform::hasSubDirectory(path))
  45. return true;
  46. Vector<StringTableEntry> dummy;
  47. Platform::dumpDirectories( path, dummy, 0, true);
  48. return dummy.size() > 0;
  49. }
  50. GuiFileTreeCtrl::GuiFileTreeCtrl()
  51. : Parent()
  52. {
  53. // Parent configuration
  54. setBounds(0,0,200,100);
  55. mDestroyOnSleep = false;
  56. mSupportMouseDragging = false;
  57. mMultipleSelections = false;
  58. mFileFilter = "*." TORQUE_SCRIPT_EXTENSION " *.gui *.ed." TORQUE_SCRIPT_EXTENSION;
  59. _initFilters();
  60. }
  61. void GuiFileTreeCtrl::initPersistFields()
  62. {
  63. addGroup( "File Tree" );
  64. addField( "rootPath", TypeRealString, Offset( mRootPath, GuiFileTreeCtrl ), "Path in game directory that should be displayed in the control." );
  65. addProtectedField( "fileFilter", TypeRealString, Offset( mFileFilter, GuiFileTreeCtrl ),
  66. &_setFileFilterValue, &defaultProtectedGetFn, "Vector of file patterns. If not empty, only files matching the pattern will be shown in the control." );
  67. endGroup( "File Tree" );
  68. Parent::initPersistFields();
  69. }
  70. static void _dumpFiles(const char *path, Vector<StringTableEntry> &directoryVector, S32 depth = 0)
  71. {
  72. Vector<Platform::FileInfo> fileVec;
  73. Platform::dumpPath( path, fileVec, depth);
  74. for(U32 i = 0; i < fileVec.size(); i++)
  75. {
  76. directoryVector.push_back( StringTable->insert(fileVec[i].pFileName) );
  77. }
  78. }
  79. void GuiFileTreeCtrl::updateTree()
  80. {
  81. // Kill off any existing items
  82. _destroyTree();
  83. // Here we're going to grab our system volumes from the platform layer and create them as roots
  84. //
  85. // Note : that we're passing a 1 as the last parameter to Platform::dumpDirectories, which tells it
  86. // how deep to dump in recursion. This is an optimization to keep from dumping the whole file system
  87. // to the tree. The tree will dump more paths as necessary when the virtual parents are expanded,
  88. // much as windows does.
  89. // Determine the root path.
  90. String rootPath = Platform::getMainDotCsDir();
  91. if( !mRootPath.isEmpty() )
  92. rootPath = String::ToString( "%s/%s", rootPath.c_str(), mRootPath.c_str() );
  93. // get the files in the main.tscript dir
  94. Vector<StringTableEntry> pathVec;
  95. Platform::dumpDirectories( rootPath, pathVec, 0, true);
  96. _dumpFiles( rootPath, pathVec, 0);
  97. if( ! pathVec.empty() )
  98. {
  99. // get the last folder in the path.
  100. char *dirname = dStrdup(rootPath);
  101. U32 last = dStrlen(dirname)-1;
  102. if(dirname[last] == '/')
  103. dirname[last] = '\0';
  104. char* lastPathComponent = dStrrchr(dirname,'/');
  105. if(lastPathComponent)
  106. *lastPathComponent++ = '\0';
  107. else
  108. lastPathComponent = dirname;
  109. // Iterate through the returned paths and add them to the tree
  110. Vector<StringTableEntry>::iterator j = pathVec.begin();
  111. for( ; j != pathVec.end(); j++ )
  112. {
  113. char fullModPathSub [512];
  114. dMemset( fullModPathSub, 0, 512 );
  115. dSprintf( fullModPathSub, 512, "%s/%s", lastPathComponent, (*j) );
  116. addPathToTree( *j );
  117. }
  118. dFree(dirname);
  119. }
  120. }
  121. bool GuiFileTreeCtrl::onWake()
  122. {
  123. if( !Parent::onWake() )
  124. return false;
  125. updateTree();
  126. return true;
  127. }
  128. bool GuiFileTreeCtrl::onVirtualParentExpand(Item *item)
  129. {
  130. if( !item || !item->isExpanded() )
  131. return true;
  132. const char* pathToExpand = item->getValue();
  133. if( !pathToExpand )
  134. {
  135. Con::errorf("GuiFileTreeCtrl::onVirtualParentExpand - Unable to retrieve item value!");
  136. return false;
  137. }
  138. Vector<StringTableEntry> pathVec;
  139. _dumpFiles( pathToExpand, pathVec, 0 );
  140. Platform::dumpDirectories( pathToExpand, pathVec, 0, true);
  141. if( ! pathVec.empty() )
  142. {
  143. // Iterate through the returned paths and add them to the tree
  144. Vector<StringTableEntry>::iterator i = pathVec.begin();
  145. for( ; i != pathVec.end(); i++ )
  146. recurseInsert(item, (*i) );
  147. item->setExpanded( true );
  148. }
  149. item->setVirtualParent( false );
  150. // Update our tree view
  151. buildVisibleTree();
  152. return true;
  153. }
  154. void GuiFileTreeCtrl::addPathToTree( StringTableEntry path )
  155. {
  156. if( !path )
  157. {
  158. Con::errorf("GuiFileTreeCtrl::addPathToTree - Invalid Path!");
  159. return;
  160. }
  161. // Identify which root (volume) this path belongs to (if any)
  162. S32 root = getFirstRootItem();
  163. StringTableEntry ourPath = &path[ dStrcspn( path, "/" ) + 1];
  164. StringTableEntry ourRoot = StringUnit::getUnit( path, 0, "/" );
  165. // There are no current roots, we can safely create one
  166. if( root == 0 )
  167. {
  168. recurseInsert( NULL, path );
  169. }
  170. else
  171. {
  172. while( root != 0 )
  173. {
  174. if( dStricmp( getItemValue( root ), ourRoot ) == 0 )
  175. {
  176. recurseInsert( getItem( root ), ourPath );
  177. break;
  178. }
  179. root = this->getNextSiblingItem( root );
  180. }
  181. // We found none so we'll create one
  182. if ( root == 0 )
  183. {
  184. recurseInsert( NULL, path );
  185. }
  186. }
  187. }
  188. void GuiFileTreeCtrl::onItemSelected( Item *item )
  189. {
  190. Con::executef( this, "onSelectPath", avar("%s",item->getValue()) );
  191. mSelPath = item->getValue();
  192. if( _hasChildren( mSelPath ) )
  193. item->setVirtualParent( true );
  194. }
  195. bool GuiFileTreeCtrl::_setFileFilterValue( void *object, const char *index, const char *data )
  196. {
  197. GuiFileTreeCtrl* ctrl = ( GuiFileTreeCtrl* ) object;
  198. ctrl->mFileFilter = data;
  199. ctrl->_initFilters();
  200. return false;
  201. }
  202. void GuiFileTreeCtrl::_initFilters()
  203. {
  204. mFilters.clear();
  205. U32 index = 0;
  206. while( true )
  207. {
  208. const char* pattern = StringUnit::getUnit( mFileFilter, index, " " );
  209. if( !pattern[ 0 ] )
  210. break;
  211. mFilters.push_back( pattern );
  212. ++ index;
  213. }
  214. }
  215. bool GuiFileTreeCtrl::matchesFilters(const char* filename)
  216. {
  217. if( !mFilters.size() )
  218. return true;
  219. for(S32 i = 0; i < mFilters.size(); i++)
  220. {
  221. if(FindMatch::isMatch( mFilters[i], filename))
  222. return true;
  223. }
  224. return false;
  225. }
  226. void GuiFileTreeCtrl::recurseInsert( Item* parent, StringTableEntry path )
  227. {
  228. if( !path )
  229. return;
  230. char szPathCopy [ 1024 ];
  231. dMemset( szPathCopy, 0, 1024 );
  232. dStrcpy( szPathCopy, path, 1024 );
  233. // Jump over the first character if it's a root /
  234. char *curPos = szPathCopy;
  235. if( *curPos == '/' )
  236. curPos++;
  237. char szValue[1024];
  238. dMemset( szValue, 0, 1024 );
  239. if( parent )
  240. {
  241. dMemset( szValue, 0, sizeof( szValue ) );
  242. dSprintf( szValue, sizeof( szValue ), "%s/%s", parent->getValue(), curPos );
  243. }
  244. else
  245. {
  246. dStrncpy( szValue, curPos, sizeof( szValue ) );
  247. szValue[ sizeof( szValue ) - 1 ] = 0;
  248. }
  249. const U32 valueLen = dStrlen( szValue );
  250. char* value = new char[ valueLen + 1 ];
  251. dMemcpy( value, szValue, valueLen + 1 );
  252. char *delim = dStrchr( curPos, '/' );
  253. if ( delim )
  254. {
  255. // terminate our / and then move our pointer to the next character (rest of the path)
  256. *delim = 0x00;
  257. delim++;
  258. }
  259. S32 itemIndex = 0;
  260. // only insert blindly if we have no root
  261. if( !parent )
  262. itemIndex = insertItem( 0, curPos, curPos );
  263. else
  264. {
  265. bool allowed = (_isDirInMainDotCsPath(value) || matchesFilters(value));
  266. Item *exists = parent->findChildByValue( szValue );
  267. if( allowed && !exists && String::compare( curPos, "" ) != 0 )
  268. {
  269. // Since we're adding a child this parent can't be a virtual parent, so clear that flag
  270. parent->setVirtualParent( false );
  271. itemIndex = insertItem( parent->getID(), curPos);
  272. Item *newitem = getItem(itemIndex);
  273. newitem->setValue( value );
  274. }
  275. else
  276. {
  277. itemIndex = ( parent != NULL ) ? ( ( exists != NULL ) ? exists->getID() : -1 ) : -1;
  278. }
  279. }
  280. Item *newitem = getItem(itemIndex);
  281. if(newitem)
  282. {
  283. newitem->setValue( value );
  284. if( _isDirInMainDotCsPath( value ) )
  285. {
  286. newitem->setNormalImage( Icon_FolderClosed );
  287. newitem->setExpandedImage( Icon_Folder );
  288. newitem->setVirtualParent(true);
  289. newitem->setExpanded(false);
  290. }
  291. else
  292. {
  293. newitem->setNormalImage( Icon_Doc );
  294. }
  295. }
  296. // since we're only dealing with volumes and directories, all end nodes will be virtual parents
  297. // so if we are at the bottom of the rabbit hole, set the item to be a virtual parent
  298. Item* item = getItem( itemIndex );
  299. if(item)
  300. {
  301. item->setExpanded(false);
  302. if(parent && _isDirInMainDotCsPath(item->getValue()) && Platform::hasSubDirectory(item->getValue()))
  303. item->setVirtualParent(true);
  304. }
  305. if( delim )
  306. {
  307. if( ( String::compare( delim, "" ) == 0 ) && item )
  308. {
  309. item->setExpanded( false );
  310. if( parent && _hasChildren( item->getValue() ) )
  311. item->setVirtualParent( true );
  312. }
  313. }
  314. else
  315. {
  316. if( item )
  317. {
  318. item->setExpanded( false );
  319. if( parent && _hasChildren( item->getValue() ) )
  320. item->setVirtualParent( true );
  321. }
  322. }
  323. // Down the rabbit hole we go
  324. recurseInsert( getItem( itemIndex ), delim );
  325. }
  326. DefineEngineMethod( GuiFileTreeCtrl, getSelectedPath, const char*, (), , "getSelectedPath() - returns the currently selected path in the tree")
  327. {
  328. const String& path = object->getSelectedPath();
  329. return Con::getStringArg( path );
  330. }
  331. DefineEngineMethod( GuiFileTreeCtrl, setSelectedPath, bool, (const char * path), , "setSelectedPath(path) - expands the tree to the specified path")
  332. {
  333. return object->setSelectedPath( path );
  334. }
  335. DefineEngineMethod( GuiFileTreeCtrl, reload, void, (), , "() - Reread the directory tree hierarchy." )
  336. {
  337. object->updateTree();
  338. }
  339. bool GuiFileTreeCtrl::setSelectedPath( const char* path )
  340. {
  341. if( !path )
  342. return false;
  343. // Since we only list one deep on paths, we need to add the path to the tree just incase it isn't already indexed in the tree
  344. // or else we wouldn't be able to select a path we hadn't previously browsed to. :)
  345. if( _isDirInMainDotCsPath( path ) )
  346. addPathToTree( path );
  347. // see if we have a child that matches what we want
  348. for(U32 i = 0; i < mItems.size(); i++)
  349. {
  350. if( dStricmp( mItems[i]->getValue(), path ) == 0 )
  351. {
  352. Item* item = mItems[i];
  353. AssertFatal(item,"GuiFileTreeCtrl::setSelectedPath - Item Index Bad, Fatal Mistake!!!");
  354. item->setExpanded( true );
  355. clearSelection();
  356. setItemSelected( item->getID(), true );
  357. // make sure all of it's parents are expanded
  358. S32 parent = getParentItem( item->getID() );
  359. while( parent != 0 )
  360. {
  361. setItemExpanded( parent, true );
  362. parent = getParentItem( parent );
  363. }
  364. // Rebuild our tree just incase we've oops'd
  365. buildVisibleTree();
  366. scrollVisible( item );
  367. }
  368. }
  369. return false;
  370. }