guiFileTreeCtrl.cpp 13 KB

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