guiDirectoryFileListCtrl.cpp 6.7 KB

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