guiDirectoryFileListCtrl.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "console/engineAPI.h"
  23. #include "core/strings/findMatch.h"
  24. #include "gui/controls/guiDirectoryFileListCtrl.h"
  25. IMPLEMENT_CONOBJECT( GuiDirectoryFileListCtrl );
  26. ConsoleDocClass( GuiDirectoryFileListCtrl,
  27. "@brief A control that displays a list of files from within a single directory "
  28. "in the game file system.\n\n"
  29. "@tsexample\n\n"
  30. "new GuiDirectoryFileListCtrl()\n"
  31. "{\n"
  32. " filePath = \"art/shapes\";\n"
  33. " fileFilter = \"*.dts\" TAB \"*.dae\";\n"
  34. " //Properties not specific to this control have been omitted from this example.\n"
  35. "};\n"
  36. "@endtsexample\n\n"
  37. "@ingroup GuiControls\n"
  38. );
  39. GuiDirectoryFileListCtrl::GuiDirectoryFileListCtrl()
  40. {
  41. mFilePath = StringTable->insert( "" );
  42. mFilter = StringTable->insert( "*.*" );
  43. }
  44. void GuiDirectoryFileListCtrl::initPersistFields()
  45. {
  46. docsURL;
  47. addProtectedField( "filePath", TypeString, Offset( mFilePath, GuiDirectoryFileListCtrl ),
  48. &_setFilePath, &defaultProtectedGetFn, "Path in game directory from which to list files." );
  49. addProtectedField( "fileFilter", TypeString, Offset( mFilter, GuiDirectoryFileListCtrl ),
  50. &_setFilter, &defaultProtectedGetFn, "Tab-delimited list of file name patterns. Only matched files will be displayed." );
  51. Parent::initPersistFields();
  52. }
  53. bool GuiDirectoryFileListCtrl::onWake()
  54. {
  55. if( !Parent::onWake() )
  56. return false;
  57. update();
  58. return true;
  59. }
  60. void GuiDirectoryFileListCtrl::onMouseDown(const GuiEvent &event)
  61. {
  62. Parent::onMouseDown( event );
  63. if( event.mouseClickCount == 2 )
  64. onDoubleClick_callback();
  65. }
  66. void GuiDirectoryFileListCtrl::openDirectory()
  67. {
  68. String path;
  69. if( mFilePath && mFilePath[ 0 ] )
  70. path = String::ToString( "%s/%s", Platform::getMainDotCsDir(), mFilePath );
  71. else
  72. path = Platform::getMainDotCsDir();
  73. Vector<Platform::FileInfo> fileVector;
  74. Platform::dumpPath( path, fileVector, 0 );
  75. // Clear the current file listing
  76. clearItems();
  77. // Does this dir have any files?
  78. if( fileVector.empty() )
  79. return;
  80. // If so, iterate through and list them
  81. Vector<Platform::FileInfo>::iterator i = fileVector.begin();
  82. for( S32 j=0 ; i != fileVector.end(); i++, j++ )
  83. {
  84. if( !mFilter[ 0 ] || FindMatch::isMatchMultipleExprs( mFilter, (*i).pFileName,false ) )
  85. addItem( (*i).pFileName );
  86. }
  87. }
  88. void GuiDirectoryFileListCtrl::setCurrentFilter( const char* filter )
  89. {
  90. if( !filter )
  91. filter = "";
  92. mFilter = StringTable->insert( filter );
  93. // Update our view
  94. openDirectory();
  95. }
  96. DefineEngineMethod( GuiDirectoryFileListCtrl, setFilter, void, ( const char* filter ),,
  97. "Set the file filter.\n\n"
  98. "@param filter Tab-delimited list of file name patterns. Only matched files will be displayed.\n" )
  99. {
  100. object->setCurrentFilter( filter );
  101. }
  102. bool GuiDirectoryFileListCtrl::setCurrentPath( const char* path, const char* filter )
  103. {
  104. if( !path )
  105. return false;
  106. const U32 pathLen = dStrlen( path );
  107. if( pathLen > 0 && path[ pathLen - 1 ] == '/' )
  108. mFilePath = StringTable->insertn( path, pathLen - 1 );
  109. else
  110. mFilePath = StringTable->insert( path );
  111. if( filter )
  112. mFilter = StringTable->insert( filter );
  113. // Update our view
  114. openDirectory();
  115. return true;
  116. }
  117. DefineEngineMethod( GuiDirectoryFileListCtrl, reload, void, (),,
  118. "Update the file list." )
  119. {
  120. object->update();
  121. }
  122. DefineEngineMethod( GuiDirectoryFileListCtrl, setPath, bool, ( const char* path, const char* filter ),,
  123. "Set the search path and file filter.\n\n"
  124. "@param path Path in game directory from which to list files.\n"
  125. "@param filter Tab-delimited list of file name patterns. Only matched files will be displayed.\n" )
  126. {
  127. return object->setCurrentPath( path, filter );
  128. }
  129. DefineEngineMethod( GuiDirectoryFileListCtrl, getSelectedFiles, const char*, (),,
  130. "Get the list of selected files.\n\n"
  131. "@return A space separated list of selected files" )
  132. {
  133. Vector<S32> ItemVector;
  134. object->getSelectedItems( ItemVector );
  135. if( ItemVector.empty() )
  136. return StringTable->insert( "" );
  137. // Get an adequate buffer
  138. static const U32 itemBufSize = 256;
  139. char itemBuffer[itemBufSize];
  140. static const U32 bufSize = ItemVector.size() * 64;
  141. char* returnBuffer = Con::getReturnBuffer( bufSize );
  142. dMemset( returnBuffer, 0, bufSize );
  143. // Fetch the first entry
  144. StringTableEntry itemText = object->getItemText( ItemVector[0] );
  145. if( !itemText )
  146. return StringTable->lookup("");
  147. dSprintf( returnBuffer, bufSize, "%s", itemText );
  148. // If only one entry, return it.
  149. if( ItemVector.size() == 1 )
  150. return returnBuffer;
  151. // Fetch the remaining entries
  152. for( S32 i = 1; i < ItemVector.size(); i++ )
  153. {
  154. itemText = object->getItemText( ItemVector[i] );
  155. if( !itemText )
  156. continue;
  157. dMemset( itemBuffer, 0, itemBufSize );
  158. dSprintf( itemBuffer, itemBufSize, " %s", itemText );
  159. dStrcat( returnBuffer, itemBuffer, itemBufSize );
  160. }
  161. return returnBuffer;
  162. }
  163. StringTableEntry GuiDirectoryFileListCtrl::getSelectedFileName()
  164. {
  165. S32 item = getSelectedItem();
  166. if( item == -1 )
  167. return StringTable->lookup("");
  168. StringTableEntry itemText = getItemText( item );
  169. if( !itemText )
  170. return StringTable->lookup("");
  171. return itemText;
  172. }
  173. DefineEngineMethod( GuiDirectoryFileListCtrl, getSelectedFile, const char*, (),,
  174. "Get the currently selected filename.\n\n"
  175. "@return The filename of the currently selected file\n" )
  176. {
  177. return object->getSelectedFileName();
  178. }