guiFileTreeCtrl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. docsURL;
  64. addGroup( "File Tree" );
  65. addField( "rootPath", TypeRealString, Offset( mRootPath, GuiFileTreeCtrl ), "Path in game directory that should be displayed in the control." );
  66. addProtectedField( "fileFilter", TypeRealString, Offset( mFileFilter, GuiFileTreeCtrl ),
  67. &_setFileFilterValue, &defaultProtectedGetFn, "Vector of file patterns. If not empty, only files matching the pattern will be shown in the control." );
  68. endGroup( "File Tree" );
  69. Parent::initPersistFields();
  70. }
  71. static void _dumpFiles(const char *path, Vector<StringTableEntry> &directoryVector, S32 depth = 0)
  72. {
  73. Vector<Platform::FileInfo> fileVec;
  74. Platform::dumpPath( path, fileVec, depth);
  75. for(U32 i = 0; i < fileVec.size(); i++)
  76. {
  77. directoryVector.push_back( StringTable->insert(fileVec[i].pFileName) );
  78. }
  79. }
  80. void GuiFileTreeCtrl::updateTree()
  81. {
  82. // Kill off any existing items
  83. _destroyTree();
  84. // Here we're going to grab our system volumes from the platform layer and create them as roots
  85. //
  86. // Note : that we're passing a 1 as the last parameter to Platform::dumpDirectories, which tells it
  87. // how deep to dump in recursion. This is an optimization to keep from dumping the whole file system
  88. // to the tree. The tree will dump more paths as necessary when the virtual parents are expanded,
  89. // much as windows does.
  90. // Determine the root path.
  91. String rootPath = Platform::getMainDotCsDir();
  92. if( !mRootPath.isEmpty() )
  93. rootPath = String::ToString( "%s/%s", rootPath.c_str(), mRootPath.c_str() );
  94. // get the files in the main.tscript dir
  95. Vector<StringTableEntry> pathVec;
  96. Platform::dumpDirectories( rootPath, pathVec, 0, true);
  97. _dumpFiles( rootPath, pathVec, 0);
  98. if( ! pathVec.empty() )
  99. {
  100. // get the last folder in the path.
  101. char *dirname = dStrdup(rootPath);
  102. U32 last = dStrlen(dirname)-1;
  103. if(dirname[last] == '/')
  104. dirname[last] = '\0';
  105. char* lastPathComponent = dStrrchr(dirname,'/');
  106. if(lastPathComponent)
  107. *lastPathComponent++ = '\0';
  108. else
  109. lastPathComponent = dirname;
  110. // Iterate through the returned paths and add them to the tree
  111. Vector<StringTableEntry>::iterator j = pathVec.begin();
  112. for( ; j != pathVec.end(); j++ )
  113. {
  114. char fullModPathSub [512];
  115. dMemset( fullModPathSub, 0, 512 );
  116. dSprintf( fullModPathSub, 512, "%s/%s", lastPathComponent, (*j) );
  117. addPathToTree( *j );
  118. }
  119. dFree(dirname);
  120. }
  121. }
  122. bool GuiFileTreeCtrl::onWake()
  123. {
  124. if( !Parent::onWake() )
  125. return false;
  126. updateTree();
  127. return true;
  128. }
  129. bool GuiFileTreeCtrl::onVirtualParentExpand(Item *item)
  130. {
  131. if( !item || !item->isExpanded() )
  132. return true;
  133. const char* pathToExpand = item->getValue();
  134. if( !pathToExpand )
  135. {
  136. Con::errorf("GuiFileTreeCtrl::onVirtualParentExpand - Unable to retrieve item value!");
  137. return false;
  138. }
  139. Vector<StringTableEntry> pathVec;
  140. _dumpFiles( pathToExpand, pathVec, 0 );
  141. Platform::dumpDirectories( pathToExpand, pathVec, 0, true);
  142. if( ! pathVec.empty() )
  143. {
  144. // Iterate through the returned paths and add them to the tree
  145. Vector<StringTableEntry>::iterator i = pathVec.begin();
  146. for( ; i != pathVec.end(); i++ )
  147. recurseInsert(item, (*i) );
  148. item->setExpanded( true );
  149. }
  150. item->setVirtualParent( false );
  151. // Update our tree view
  152. buildVisibleTree();
  153. return true;
  154. }
  155. void GuiFileTreeCtrl::addPathToTree( StringTableEntry path )
  156. {
  157. if( !path )
  158. {
  159. Con::errorf("GuiFileTreeCtrl::addPathToTree - Invalid Path!");
  160. return;
  161. }
  162. // Identify which root (volume) this path belongs to (if any)
  163. S32 root = getFirstRootItem();
  164. StringTableEntry ourPath = &path[ dStrcspn( path, "/" ) + 1];
  165. StringTableEntry ourRoot = StringUnit::getUnit( path, 0, "/" );
  166. // There are no current roots, we can safely create one
  167. if( root == 0 )
  168. {
  169. recurseInsert( NULL, path );
  170. }
  171. else
  172. {
  173. while( root != 0 )
  174. {
  175. if( dStricmp( getItemValue( root ), ourRoot ) == 0 )
  176. {
  177. recurseInsert( getItem( root ), ourPath );
  178. break;
  179. }
  180. root = this->getNextSiblingItem( root );
  181. }
  182. // We found none so we'll create one
  183. if ( root == 0 )
  184. {
  185. recurseInsert( NULL, path );
  186. }
  187. }
  188. }
  189. void GuiFileTreeCtrl::onItemSelected( Item *item )
  190. {
  191. Con::executef( this, "onSelectPath", avar("%s",item->getValue()) );
  192. mSelPath = item->getValue();
  193. if( _hasChildren( mSelPath ) )
  194. item->setVirtualParent( true );
  195. }
  196. bool GuiFileTreeCtrl::_setFileFilterValue( void *object, const char *index, const char *data )
  197. {
  198. GuiFileTreeCtrl* ctrl = ( GuiFileTreeCtrl* ) object;
  199. ctrl->mFileFilter = data;
  200. ctrl->_initFilters();
  201. return false;
  202. }
  203. void GuiFileTreeCtrl::_initFilters()
  204. {
  205. mFilters.clear();
  206. U32 index = 0;
  207. while( true )
  208. {
  209. const char* pattern = StringUnit::getUnit( mFileFilter, index, " " );
  210. if( !pattern[ 0 ] )
  211. break;
  212. mFilters.push_back( pattern );
  213. ++ index;
  214. }
  215. }
  216. bool GuiFileTreeCtrl::matchesFilters(const char* filename)
  217. {
  218. if( !mFilters.size() )
  219. return true;
  220. for(S32 i = 0; i < mFilters.size(); i++)
  221. {
  222. if(FindMatch::isMatch( mFilters[i], filename))
  223. return true;
  224. }
  225. return false;
  226. }
  227. void GuiFileTreeCtrl::recurseInsert( Item* parent, StringTableEntry path )
  228. {
  229. if( !path )
  230. return;
  231. char szPathCopy [ 1024 ];
  232. dMemset( szPathCopy, 0, 1024 );
  233. dStrcpy( szPathCopy, path, 1024 );
  234. // Jump over the first character if it's a root /
  235. char *curPos = szPathCopy;
  236. if( *curPos == '/' )
  237. curPos++;
  238. char szValue[1024];
  239. dMemset( szValue, 0, 1024 );
  240. if( parent )
  241. {
  242. dMemset( szValue, 0, sizeof( szValue ) );
  243. dSprintf( szValue, sizeof( szValue ), "%s/%s", parent->getValue(), curPos );
  244. }
  245. else
  246. {
  247. dStrncpy( szValue, curPos, sizeof( szValue ) );
  248. szValue[ sizeof( szValue ) - 1 ] = 0;
  249. }
  250. const U32 valueLen = dStrlen( szValue );
  251. char* value = new char[ valueLen + 1 ];
  252. dMemcpy( value, szValue, valueLen + 1 );
  253. char *delim = dStrchr( curPos, '/' );
  254. if ( delim )
  255. {
  256. // terminate our / and then move our pointer to the next character (rest of the path)
  257. *delim = 0x00;
  258. delim++;
  259. }
  260. S32 itemIndex = 0;
  261. // only insert blindly if we have no root
  262. if( !parent )
  263. itemIndex = insertItem( 0, curPos, curPos );
  264. else
  265. {
  266. bool allowed = (_isDirInMainDotCsPath(value) || matchesFilters(value));
  267. Item *exists = parent->findChildByValue( szValue );
  268. if( allowed && !exists && String::compare( curPos, "" ) != 0 )
  269. {
  270. // Since we're adding a child this parent can't be a virtual parent, so clear that flag
  271. parent->setVirtualParent( false );
  272. itemIndex = insertItem( parent->getID(), curPos);
  273. Item *newitem = getItem(itemIndex);
  274. newitem->setValue( value );
  275. }
  276. else
  277. {
  278. itemIndex = ( parent != NULL ) ? ( ( exists != NULL ) ? exists->getID() : -1 ) : -1;
  279. }
  280. }
  281. Item *newitem = getItem(itemIndex);
  282. if(newitem)
  283. {
  284. newitem->setValue( value );
  285. if( _isDirInMainDotCsPath( value ) )
  286. {
  287. newitem->setNormalImage( Icon_FolderClosed );
  288. newitem->setExpandedImage( Icon_Folder );
  289. newitem->setVirtualParent(true);
  290. newitem->setExpanded(false);
  291. }
  292. else
  293. {
  294. newitem->setNormalImage( Icon_Doc );
  295. }
  296. }
  297. // since we're only dealing with volumes and directories, all end nodes will be virtual parents
  298. // so if we are at the bottom of the rabbit hole, set the item to be a virtual parent
  299. Item* item = getItem( itemIndex );
  300. if(item)
  301. {
  302. item->setExpanded(false);
  303. if(parent && _isDirInMainDotCsPath(item->getValue()) && Platform::hasSubDirectory(item->getValue()))
  304. item->setVirtualParent(true);
  305. }
  306. if( delim )
  307. {
  308. if( ( String::compare( delim, "" ) == 0 ) && item )
  309. {
  310. item->setExpanded( false );
  311. if( parent && _hasChildren( item->getValue() ) )
  312. item->setVirtualParent( true );
  313. }
  314. }
  315. else
  316. {
  317. if( item )
  318. {
  319. item->setExpanded( false );
  320. if( parent && _hasChildren( item->getValue() ) )
  321. item->setVirtualParent( true );
  322. }
  323. }
  324. // Down the rabbit hole we go
  325. recurseInsert( getItem( itemIndex ), delim );
  326. }
  327. DefineEngineMethod( GuiFileTreeCtrl, getSelectedPath, const char*, (), , "getSelectedPath() - returns the currently selected path in the tree")
  328. {
  329. const String& path = object->getSelectedPath();
  330. return Con::getStringArg( path );
  331. }
  332. DefineEngineMethod( GuiFileTreeCtrl, setSelectedPath, bool, (const char * path), , "setSelectedPath(path) - expands the tree to the specified path")
  333. {
  334. return object->setSelectedPath( path );
  335. }
  336. DefineEngineMethod( GuiFileTreeCtrl, reload, void, (), , "() - Reread the directory tree hierarchy." )
  337. {
  338. object->updateTree();
  339. }
  340. bool GuiFileTreeCtrl::setSelectedPath( const char* path )
  341. {
  342. if( !path )
  343. return false;
  344. // 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
  345. // or else we wouldn't be able to select a path we hadn't previously browsed to. :)
  346. if( _isDirInMainDotCsPath( path ) )
  347. addPathToTree( path );
  348. // see if we have a child that matches what we want
  349. for(U32 i = 0; i < mItems.size(); i++)
  350. {
  351. if( dStricmp( mItems[i]->getValue(), path ) == 0 )
  352. {
  353. Item* item = mItems[i];
  354. AssertFatal(item,"GuiFileTreeCtrl::setSelectedPath - Item Index Bad, Fatal Mistake!!!");
  355. item->setExpanded( true );
  356. clearSelection();
  357. setItemSelected( item->getID(), true );
  358. // make sure all of it's parents are expanded
  359. S32 parent = getParentItem( item->getID() );
  360. while( parent != 0 )
  361. {
  362. setItemExpanded( parent, true );
  363. parent = getParentItem( parent );
  364. }
  365. // Rebuild our tree just incase we've oops'd
  366. buildVisibleTree();
  367. scrollVisible( item );
  368. }
  369. }
  370. return false;
  371. }