fileDialogBase.ed.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. //-----------------------------------------------------------------------------
  23. // File Dialog Base - Add to Sim Callback
  24. // Purpose : Intitialize Variables and Setup State.
  25. //-----------------------------------------------------------------------------
  26. function FileDialogBase::onAdd( %this )
  27. {
  28. // Callback function Succeed
  29. %this.SuccessCallback = 0;
  30. // Callback function Cancel
  31. %this.CancelCallback = 0;
  32. // Multiple Select Flag
  33. %this.MultipleSelect = false;
  34. // File Extensions Group
  35. %this.FileExtensions = new SimGroup();
  36. %this.AddFilter("*.*","All Files");
  37. }
  38. //-----------------------------------------------------------------------------
  39. // File Dialog Base - Remove from Sim Callback
  40. // Purpose : Destroy Resources.
  41. //-----------------------------------------------------------------------------
  42. function FileDialogBase::onRemove( %this )
  43. {
  44. // Remove FileExtensions Group
  45. if ( isObject( %this.FileExtensions ) )
  46. %this.FileExtensions.delete();
  47. // Remove Returned Files Group
  48. if( isObject( %this.ReturnFiles ) )
  49. %this.ReturnFiles.delete();
  50. }
  51. //-----------------------------------------------------------------------------
  52. // File Dialog Base - Show on Screen Callback
  53. // Purpose : Destroy Resources.
  54. //-----------------------------------------------------------------------------
  55. function FileDialogBase::onWake( %this )
  56. {
  57. // Necessary
  58. %dirTree = %this.findObjectByInternalName("DirectoryTree", true);
  59. %fileList = %this.findObjectByInternalName("FileList", true);
  60. %filterList = %this.findObjectByInternalName("FilterList", true);
  61. %cancelButton = %this.findObjectByInternalName("CancelButton", true);
  62. %okButton = %this.findObjectByInternalName("OkButton", true);
  63. // Optional
  64. %fileName = %this.findObjectByInternalName("FileName", true);
  65. // Check for functionality Components.
  66. if( !isObject( %dirTree ) || !isObject( %fileList ) || !isObject( %filterList ) )
  67. {
  68. error("FileDialogBase::onWake - Unable to find NECESSARY child controls.");
  69. return false;
  70. }
  71. // Check for button components.
  72. if( !isObject( %cancelButton ) || !isObject( %okButton ) )
  73. {
  74. error("FileDialogBase::onWake - Unable to find accept and cancel buttons!");
  75. return false;
  76. }
  77. // Tag controls so that they can navigate our dialog.
  78. %dirTree.parent = %this;
  79. %fileList.parent = %this;
  80. %filterList.parent = %this;
  81. %okButton.parent = %this;
  82. %cancelButton.parent = %this;
  83. // Tag optionals
  84. if( isObject( %fileName ) )
  85. %fileName.parent = %this;
  86. // Finally, make sure our ReturnFiles group is empty.
  87. if( isObject( %this.ReturnFiles ) )
  88. %this.ReturnFiles.delete();
  89. %this.ReturnFiles = new SimGroup();
  90. %this.add( %this.ReturnFiles );
  91. // If no filters
  92. if( %this.GetFilterCount() == 0 )
  93. %this.addfilter("*.*","All Files");
  94. %this.PopulateFilters();
  95. }
  96. //-----------------------------------------------------------------------------
  97. // File Dialog Base - Add a file extension filter to the list
  98. //-----------------------------------------------------------------------------
  99. function FileDialogBase::AddFilter( %this, %extension, %caption )
  100. {
  101. if( !isObject( %this.FileExtensions ) )
  102. {
  103. error("OpenFileDialog::AddFilter - FileExtensions Group does not exist!");
  104. return false;
  105. }
  106. %filter = new ScriptObject()
  107. {
  108. extension = %extension;
  109. caption = %caption;
  110. };
  111. // Add to filter list
  112. %this.FileExtensions.add( %filter );
  113. return %filter;
  114. }
  115. //-----------------------------------------------------------------------------
  116. // File Dialog Base - Clear filters by file extension
  117. //-----------------------------------------------------------------------------
  118. function FileDialogBase::ClearFilters( %this )
  119. {
  120. if( isObject( %this.FileExtensions ) )
  121. %this.FileExtensions.delete();
  122. %this.FileExtensions = new SimGroup();
  123. }
  124. //-----------------------------------------------------------------------------
  125. // File Dialog Base - Get number of filters
  126. //-----------------------------------------------------------------------------
  127. function FileDialogBase::GetFilterCount( %this )
  128. {
  129. if( !isObject( %this.FileExtensions ) )
  130. return 0;
  131. // Return Count
  132. return %this.FileExtensions.getCount();
  133. }
  134. //-----------------------------------------------------------------------------
  135. // File Dialog Base - Populate dropdown with filter options
  136. //-----------------------------------------------------------------------------
  137. function FileDialogBase::PopulateFilters( %this )
  138. {
  139. %fileExtensions = %this.FileExtensions;
  140. if( !isObject( %fileExtensions ) )
  141. {
  142. error("OpenFileDialog::PopulateFilters - FileExtensions Group does not exist!");
  143. return false;
  144. }
  145. %filterList = %this.findObjectByInternalName("FilterList", true);
  146. if( !isObject( %filterList ) )
  147. {
  148. error("FileDialogBase::PopulateFilters - Filter List Dropdown not found!");
  149. return false;
  150. }
  151. // Clear filter list
  152. %filterList.clear();
  153. // Populate List
  154. for( %i = 0; %i < %fileExtensions.getCount(); %i++ )
  155. {
  156. // Fetch Filter Script Object
  157. %filter = %fileExtensions.getObject( %i );
  158. // Add item to list
  159. %filterList.add( %filter.Caption SPC "(" SPC %filter.Extension SPC ")", %filter.getID() );
  160. }
  161. // Set First Item to Selected.
  162. %filterList.setFirstSelected();
  163. }
  164. function FileDialogOkButton::onClick( %this )
  165. {
  166. if( !isObject( %this.parent ) )
  167. {
  168. error("FileDialogBase->FileDialogOkButton::onClick - Unable to find proper parent control! Functionality Compromised!");
  169. return;
  170. }
  171. %dirTree = %this.parent.findObjectByInternalName("DirectoryTree", true);
  172. %fileList = %this.parent.findObjectByInternalName("FileList", true);
  173. %filterList = %this.parent.findObjectByInternalName("FilterList", true);
  174. // Check for functionality Components.
  175. if( !isObject( %dirTree ) || !isObject( %fileList ) || !isObject( %filterList ) )
  176. {
  177. error("FileDialogOkButton::onClick - Unable to find NECESSARY sibling controls.");
  178. return;
  179. }
  180. //
  181. // Fetch Path
  182. //
  183. %path = %dirTree.getSelectedPath();
  184. //
  185. // Compose File Name
  186. //
  187. %fileNameCtrl = %this.parent.findObjectByInternalName("FileName", true);
  188. // FileName TextEdit?
  189. if( isObject( %fileNameCtrl ) )
  190. {
  191. // Get FileName from TextEdit
  192. %fileName = %fileNameCtrl.getText();
  193. // Get Filter Object from dropdown list
  194. %filterObj = %filterList.getSelected();
  195. // Validate File Extension
  196. if( fileExt( %fileName ) $= "" && isObject( %filterObj ) )
  197. {
  198. // Append Extension to FileName
  199. %fileName = %fileName @ fileExt( %filterObj.Extension );
  200. }
  201. }
  202. else
  203. %fileName = %fileList.getSelectedFile();
  204. //
  205. // Build Full Path
  206. //
  207. %fullPath = %path @ "/" @ %fileName;
  208. Canvas.popDialog( %this.parent );
  209. // Callback
  210. eval( %this.parent.SuccessCallback @ "(\"" @ %fullPath @"\");" );
  211. %parent.SuccessCallback = 0;
  212. //error("Ok");
  213. }
  214. function FileDialogCancelButton::onClick( %this )
  215. {
  216. Canvas.popDialog( %this.parent );
  217. //error("Cancel");
  218. }
  219. function FileDialogDirectoryTree::onSelectPath( %this, %path )
  220. {
  221. %fileList = %this.parent.findObjectByInternalName("FileList", true);
  222. %filterList = %this.parent.findObjectByInternalName("FilterList", true);
  223. %filterObj = %filterList.getSelected();
  224. if( !isObject( %filterObj ) )
  225. %filter = "*.*";
  226. else
  227. %filter = %filterObj.Extension;
  228. %fileList.setPath( %path, %filter );
  229. }
  230. function FileDialogFilterList::onSelect( %this, %id, %text )
  231. {
  232. if( !isObject( %id ) )
  233. {
  234. error("FileDialogFilterList::onSelect - Invalid Filter Object!");
  235. return;
  236. }
  237. %fileList = %this.parent.findObjectByInternalName("FileList", true);
  238. %fileList.setFilter( %id.Extension );
  239. }
  240. function FileDialogFileList::onDoubleClick( %this )
  241. {
  242. //error("DoubleClick");
  243. %okButton = %this.parent.findObjectByInternalName("OkButton", true);
  244. if( isObject( %okButton ) )
  245. %okButton.performClick();
  246. }
  247. function FileDialogFileList::onSelect( %this, %listid, %file )
  248. {
  249. %fileNameCtrl = %this.parent.findObjectByInternalName("FileName", true);
  250. // FileName TextEdit?
  251. if( !isObject( %fileNameCtrl ) )
  252. return;
  253. // Update our file name to the one selected
  254. %fileNameCtrl.setText( %file );
  255. }
  256. function FileDialogFileName::onReturn( %this )
  257. {
  258. //error("onReturn");
  259. %okButton = %this.parent.findObjectByInternalName("OkButton", true);
  260. if( isObject( %okButton ) )
  261. %okButton.performClick();
  262. }