FileDialog.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. function PlatformFileDialog::buildFilters(%this)
  2. {
  3. %str = strreplace( %this.data.filters, "|", "\t");
  4. %this.filterCount = getFieldCount( %str ) / 2;
  5. //echo( "Filter count: " @ %str );
  6. for( %i = 0; %i < %this.filterCount; %i++ )
  7. {
  8. %this.filterName[%i] = GetField( %str, (%i*2) + 0 );
  9. %this.filter[%i] = strreplace( GetField( %str, (%i*2) + 1 ), ";", "\t");
  10. //echo( "Filter: " @ %this.filterName[%i] @ " - " @ %this.filter[%i]);
  11. }
  12. }
  13. function PlatformFileDialog::handleFlags(%this, %flags)
  14. {
  15. %this.FDS_OPEN = false;
  16. %this.FDS_SAVE = false;
  17. %this.FDS_OVERWRITEPROMPT = false;
  18. %this.FDS_MUSTEXIST = false;
  19. %this.FDS_BROWSEFOLDER = false;
  20. %flagCount = getFieldCount( %flags );
  21. //echo( "flag count: " @ %flagCount );
  22. for( %i = 0; %i < %flagCount; %i++ )
  23. {
  24. %flag = GetField( %flags, %i );
  25. //echo(%flag);
  26. if( %flag $= "FDS_OPEN" )
  27. {
  28. %this.FDS_OPEN = true;
  29. %this-->Button.setText( "OPEN" );
  30. %this-->Button.command = "PlatformFileDialog.tryFile();";
  31. %this-->window.text = "Select file to OPEN";
  32. }
  33. else if( %flag $= "FDS_SAVE" )
  34. {
  35. %this.FDS_SAVE = true;
  36. %this-->Button.setText( "SAVE" );
  37. %this-->Button.command = "PlatformFileDialog.tryFile();";
  38. %this-->window.text = "Select file to Save";
  39. }
  40. else if( %flag $= "FDS_OVERWRITEPROMPT" )
  41. {
  42. %this.FDS_OVERWRITEPROMPT = true;
  43. }
  44. else if( %flag $= "FDS_MUSTEXIST" )
  45. {
  46. %this.FDS_MUSTEXIST = true;
  47. }
  48. else if( %flag $= "FDS_BROWSEFOLDER" )
  49. {
  50. %this.FDS_BROWSEFOLDER = true;
  51. %this-->window.text = "Select folder to OPEN";
  52. }
  53. }
  54. }
  55. function OpenPlatformFileDialog(%data, %flags)
  56. {
  57. PlatformFileDialog.searchDir = "";
  58. PlatformFileDialog-->fileNameEdit.setText( "" );
  59. PlatformFileDialog.data = %data;
  60. PlatformFileDialog.data.finished = 0;
  61. PlatformFileDialog.handleFlags( %flags );
  62. if( !isObject(PlatformFileDialog.freeItemSet) )
  63. {
  64. PlatformFileDialog.freeItemSet = new SimGroup();
  65. }
  66. PlatformFileDialog.buildFilters();
  67. Canvas.pushDialog(PlatformFileDialog);
  68. }
  69. function PlatformFileDialog::changeDir( %this, %newDir )
  70. {
  71. %this.searchDir = %newDir;
  72. %this.update();
  73. }
  74. function PlatformFileDialog::navigateUp( %this )
  75. {
  76. //echo( "PlatformFileDialog::navigateUp " @ %this.searchDir );
  77. if( %this.searchDir !$= "" )
  78. {
  79. %str = strreplace( %this.searchDir, "/", "\t");
  80. %count = getFieldCount( %str );
  81. if ( %count == 0 )
  82. return;
  83. if ( %count == 1 )
  84. %address = "";
  85. else
  86. %address = getFields( %str, 0, %count - 2 );
  87. %newDir = strreplace( %address, "\t", "/" );
  88. if( %newDir !$= "" )
  89. %newDir = %newDir @ "/";
  90. %this.changeDir( %newDir );
  91. }
  92. }
  93. function PlatformFileDialog::cancel( %this )
  94. {
  95. %this.data.files[0] = "";
  96. %this.data.fileCount = 0;
  97. %this.data.finished = 1;
  98. Canvas.popDialog(%this);
  99. }
  100. function FileDialogItem::onClick( %this )
  101. {
  102. PlatformFileDialog-->fileNameEdit.setText( "" );
  103. if( %this.isDir && %this.FDS_BROWSEFOLDER)
  104. {
  105. PlatformFileDialog-->fileNameEdit.setText( %this.text );
  106. }
  107. else if( !%this.isDir && !%this.FDS_BROWSEFOLDER )
  108. {
  109. PlatformFileDialog-->fileNameEdit.setText( %this.text );
  110. }
  111. }
  112. function FileDialogItem::onDoubleClick( %this )
  113. {
  114. PlatformFileDialog-->fileNameEdit.setText( "" );
  115. if( %this.isDir )
  116. {
  117. PlatformFileDialog.changeDir( PlatformFileDialog.searchDir @ %this.text @ "/" );
  118. }
  119. }
  120. function PlatformFileDialog::tryFile( %this )
  121. {
  122. %file = %this-->fileNameEdit.getText();
  123. if( %file $= "" )
  124. return;
  125. if( %this.FDS_OVERWRITEPROMPT )
  126. {
  127. %callback = "PlatformFileDialog.onFile( \"" @ %file @ "\" );";
  128. MessageBoxOKCancel("Confirm overwrite", "Confirm overwrite", %callback, "");
  129. return;
  130. }
  131. %this.onFile( %file );
  132. }
  133. function PlatformFileDialog::onFile( %this, %file )
  134. {
  135. %this.data.files[0] = "";
  136. %this.data.fileCount = 0;
  137. if( %file !$= "" )
  138. {
  139. %file = %this.searchDir @ %file;
  140. %this.data.fileCount = 1;
  141. }
  142. if( %this.FDS_BROWSEFOLDER && !isDirectory( %file ) )
  143. {
  144. echo("Select a directory");
  145. return;
  146. }
  147. else if( !%this.FDS_BROWSEFOLDER && !isFile( %file ) )
  148. {
  149. echo("Select a file");
  150. return;
  151. }
  152. if( %this.FDS_MUSTEXIST )
  153. {
  154. if( !isFile( %file ) && !isDirectory( %file ) )
  155. {
  156. echo("Target must exist: " @ %file );
  157. return;
  158. }
  159. }
  160. %this.data.finished = 1;
  161. %this.data.files[0] = %file;
  162. Canvas.popDialog(%this);
  163. %this-->fileNameEdit.setText( "" );
  164. }
  165. function PlatformFileDialog::clear( %this )
  166. {
  167. %itemArray = %this-->itemArray;
  168. while( %itemArray.getCount() )
  169. {
  170. %item = %itemArray.getObject( 0 );
  171. %this.freeItem( %item );
  172. }
  173. }
  174. function PlatformFileDialog::getNewItem( %this )
  175. {
  176. if( %this.freeItemSet.getCount() )
  177. %item = %this.freeItemSet.getObject( 0 );
  178. if( isObject(%item) )
  179. {
  180. %this.freeItemSet.remove( %item );
  181. }
  182. else
  183. {
  184. //create new
  185. %item = new GuiIconButtonCtrl();
  186. %item.className = "FileDialogItem";
  187. %item.profile = "ToolsGuiIconButtonProfile";
  188. %item.textLocation = "left";
  189. %item.iconLocation = "left";
  190. %item.iconBitmap = "";
  191. %item.text = "";
  192. }
  193. return %item;
  194. }
  195. function PlatformFileDialog::freeItem( %this, %item )
  196. {
  197. %this-->itemArray.remove( %item );
  198. //clear
  199. %item.setText( "" );
  200. %item.iconBitmap = "";
  201. %item.textMargin = 0;
  202. %item.textLocation = "left";
  203. %item.iconLocation = "left";
  204. %item.resetState();
  205. PlatformFileDialog.freeItemSet.add( %item );
  206. }
  207. function PlatformFileDialog::addDir( %this, %dir )
  208. {
  209. //echo( "Dir: " @ %dir );
  210. %item = %this.getNewItem();
  211. %item.setText( %dir );
  212. %item.isDir = true;
  213. %item.iconBitmap = "core/art/gui/images/folder";
  214. %item.textLocation = "left";
  215. %item.iconLocation = "left";
  216. %item.textMargin = 24;
  217. %this-->itemArray.add( %item );
  218. }
  219. function PlatformFileDialog::addFile( %this, %file )
  220. {
  221. //echo( "File: " @ %file );
  222. %item = %this.getNewItem();
  223. %item.text = strreplace( %file, %this.searchDir, "" );
  224. %item.isDir = false;
  225. %this-->itemArray.add( %item );
  226. }
  227. function PlatformFileDialog::onWake( %this )
  228. {
  229. %this.update();
  230. }
  231. function PlatformFileDialog::onSleep( %this )
  232. {
  233. %this.data.finished = 1;
  234. }
  235. function PlatformFileDialog::update( %this )
  236. {
  237. %this.clear();
  238. %this-->popUpMenu.text = %this.searchDir;
  239. // dirs
  240. %dirList = getDirectoryList( %this.searchDir, 0 );
  241. %wordCount = getFieldCount( %dirList );
  242. for( %i = 0; %i < %wordCount; %i++ )
  243. {
  244. %dirItem = GetField( %dirList, %i );
  245. %this.addDir( %dirItem );
  246. }
  247. //files
  248. %pattern = %this.filter[0];
  249. //echo( %pattern );
  250. %file = findFirstFileMultiExpr( %this.searchDir @ %pattern, false);
  251. while( %file !$= "" )
  252. {
  253. %this.addFile( %file );
  254. %file = findNextFileMultiExpr( %pattern );
  255. }
  256. }