FileSelector.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "DropDownList.h"
  25. #include "File.h"
  26. #include "FileSelector.h"
  27. #include "InputEvents.h"
  28. #include "LineEdit.h"
  29. #include "ListView.h"
  30. #include "ProcessUtils.h"
  31. #include "Text.h"
  32. #include "UI.h"
  33. #include "UIEvents.h"
  34. #include "Window.h"
  35. #include <algorithm>
  36. #include "DebugNew.h"
  37. static bool compareEntries(const FileSelectorEntry& lhs, const FileSelectorEntry& rhs)
  38. {
  39. if ((lhs.mDirectory) && (!rhs.mDirectory))
  40. return true;
  41. if ((!lhs.mDirectory) && (rhs.mDirectory))
  42. return false;
  43. return lhs.mName < rhs.mName;
  44. }
  45. FileSelector::FileSelector(UI* ui) :
  46. mUI(ui),
  47. mIgnoreEvents(false),
  48. mDirectoryMode(false)
  49. {
  50. if (!mUI)
  51. EXCEPTION("Null UI for FileSelector");
  52. mWindow = new Window("FileSelector");
  53. mWindow->setLayout(LM_VERTICAL);
  54. mTitleLayout = new UIElement();
  55. mTitleLayout->setLayout(LM_HORIZONTAL);
  56. mWindow->addChild(mTitleLayout);
  57. mTitleText = new Text();
  58. mTitleLayout->addChild(mTitleText);
  59. mCloseButton = new Button();
  60. mTitleLayout->addChild(mCloseButton);
  61. mPathEdit = new LineEdit();
  62. mWindow->addChild(mPathEdit);
  63. mFileList = new ListView();
  64. mWindow->addChild(mFileList);
  65. mFileNameLayout = new UIElement();
  66. mFileNameLayout->setLayout(LM_HORIZONTAL);
  67. mFileNameEdit = new LineEdit();
  68. mFileNameLayout->addChild(mFileNameEdit);
  69. mFilterList = new DropDownList();
  70. mFileNameLayout->addChild(mFilterList);
  71. mWindow->addChild(mFileNameLayout);
  72. mButtonLayout = new UIElement();
  73. mButtonLayout->setLayout(LM_HORIZONTAL);
  74. mButtonLayout->addChild(new UIElement()); // Add spacer
  75. mOKButton = new Button();
  76. mOKButtonText = new Text();
  77. mOKButtonText->setAlignment(HA_CENTER, VA_CENTER);
  78. mOKButton->addChild(mOKButtonText);
  79. mButtonLayout->addChild(mOKButton);
  80. mButtonLayout->addChild(new UIElement()); // Add spacer
  81. mCancelButton = new Button();
  82. mCancelButtonText = new Text();
  83. mCancelButtonText->setAlignment(HA_CENTER, VA_CENTER);
  84. mCancelButton->addChild(mCancelButtonText);
  85. mButtonLayout->addChild(mCancelButton);
  86. mButtonLayout->addChild(new UIElement()); // Add spacer
  87. mWindow->addChild(mButtonLayout);
  88. mUI->getRootElement()->addChild(mWindow);
  89. std::vector<std::string> defaultFilters;
  90. defaultFilters.push_back("*.*");
  91. setFilters(defaultFilters, 0);
  92. setPath(getCurrentDirectory());
  93. // Focus the fileselector's filelist initially when created, and bring to front
  94. mUI->setFocusElement(mFileList);
  95. mWindow->bringToFront();
  96. subscribeToEvent(mFilterList, EVENT_ITEMSELECTED, EVENT_HANDLER(FileSelector, handleFilterChanged));
  97. subscribeToEvent(mPathEdit, EVENT_TEXTFINISHED, EVENT_HANDLER(FileSelector, handlePathChanged));
  98. subscribeToEvent(mFileNameEdit, EVENT_TEXTFINISHED, EVENT_HANDLER(FileSelector, handleOKPressed));
  99. subscribeToEvent(mFileList, EVENT_ITEMSELECTED, EVENT_HANDLER(FileSelector, handleFileSelected));
  100. subscribeToEvent(mFileList, EVENT_ITEMDOUBLECLICKED, EVENT_HANDLER(FileSelector, handleFileDoubleClicked));
  101. subscribeToEvent(mFileList, EVENT_UNHANDLEDKEY, EVENT_HANDLER(FileSelector, handleFileListKey));
  102. subscribeToEvent(mOKButton, EVENT_RELEASED, EVENT_HANDLER(FileSelector, handleOKPressed));
  103. subscribeToEvent(mCancelButton, EVENT_RELEASED, EVENT_HANDLER(FileSelector, handleCancelPressed));
  104. subscribeToEvent(mCloseButton, EVENT_RELEASED, EVENT_HANDLER(FileSelector, handleCancelPressed));
  105. }
  106. FileSelector::~FileSelector()
  107. {
  108. UIElement* root = mUI->getRootElement();
  109. root->removeChild(mWindow);
  110. }
  111. void FileSelector::setStyle(XMLFile* style)
  112. {
  113. if (!style)
  114. return;
  115. mStyle = style;
  116. ResourceCache* cache = mUI->getResourceCache();
  117. mWindow->setStyleAuto(style, cache);
  118. mWindow->setStyle(style, "FileSelector", cache);
  119. mTitleText->setStyle(style, "FileSelectorTitleText", cache);
  120. mCloseButton->setStyle(style, "CloseButton", cache);
  121. mOKButtonText->setStyle(style, "FileSelectorButtonText", cache);
  122. mCancelButtonText->setStyle(style, "FileSelectorButtonText", cache);
  123. mTitleLayout->setStyle(style, "FileSelectorTitleLayout", cache);
  124. mFileNameLayout->setStyle(style, "FileSelectorLayout", cache);
  125. mButtonLayout->setStyle(style, "FileSelectorLayout", cache);
  126. mFileList->setStyleAuto(style, cache);
  127. mFileNameEdit->setStyleAuto(style, cache);
  128. mPathEdit->setStyleAuto(style, cache);
  129. mFilterList->setStyleAuto(style, cache);
  130. mFilterList->setStyle(style, "FileSelectorFilterList", cache);
  131. mOKButton->setStyleAuto(style, cache);
  132. mCancelButton->setStyleAuto(style, cache);
  133. mOKButton->setStyle(style, "FileSelectorButton", cache);
  134. mCancelButton->setStyle(style, "FileSelectorButton", cache);
  135. std::vector<UIElement*> filterTexts = mFilterList->getListView()->getContentElement()->getChildren();
  136. for (unsigned i = 0; i < filterTexts.size(); ++i)
  137. filterTexts[i]->setStyle(style, "FileSelectorFilterText", cache);
  138. std::vector<UIElement*> listTexts = mFileList->getContentElement()->getChildren();
  139. for (unsigned i = 0; i < listTexts.size(); ++i)
  140. listTexts[i]->setStyle(style, "FileSelectorListText", cache);
  141. updateElements();
  142. }
  143. void FileSelector::setTitle(const std::string& text)
  144. {
  145. mTitleText->setText(text);
  146. }
  147. void FileSelector::setButtonTexts(const std::string& okText, const std::string& cancelText)
  148. {
  149. mOKButtonText->setText(okText);
  150. mCancelButtonText->setText(cancelText);
  151. }
  152. void FileSelector::setPath(const std::string& path)
  153. {
  154. if (directoryExists(path))
  155. {
  156. mPath = fixPath(path);
  157. mIgnoreEvents = true;
  158. mPathEdit->setText(mPath);
  159. mIgnoreEvents = false;
  160. refreshFiles();
  161. }
  162. else
  163. {
  164. // If path was invalid, restore the old path to the line edit
  165. if (mPathEdit->getText() != mPath)
  166. {
  167. mIgnoreEvents = true;
  168. mPathEdit->setText(mPath);
  169. mIgnoreEvents = false;
  170. }
  171. }
  172. }
  173. void FileSelector::setFileName(const std::string& fileName)
  174. {
  175. mIgnoreEvents = true;
  176. mFileNameEdit->setText(fileName);
  177. mIgnoreEvents = false;
  178. }
  179. void FileSelector::setFilters(const std::vector<std::string>& filters, unsigned defaultIndex)
  180. {
  181. if (filters.empty())
  182. return;
  183. mIgnoreEvents = true;
  184. mFilters = filters;
  185. mFilterList->removeAllItems();
  186. for (unsigned i = 0; i < mFilters.size(); ++i)
  187. {
  188. Text* filterText = new Text();
  189. filterText->setText(mFilters[i]);
  190. filterText->setStyle(mStyle, "FileSelectorFilterText", mUI->getResourceCache());
  191. mFilterList->addItem(filterText);
  192. }
  193. if (defaultIndex > filters.size())
  194. defaultIndex = 0;
  195. mFilterList->setSelection(defaultIndex);
  196. mIgnoreEvents = false;
  197. if (getFilter() != mLastUsedFilter)
  198. refreshFiles();
  199. }
  200. void FileSelector::setDirectoryMode(bool enable)
  201. {
  202. mDirectoryMode = enable;
  203. }
  204. void FileSelector::updateElements()
  205. {
  206. {
  207. const IntRect& clipBorder = mPathEdit->getClipBorder();
  208. mPathEdit->setFixedHeight(mPathEdit->getTextElement()->getRowHeight() + clipBorder.mTop + clipBorder.mBottom);
  209. }
  210. {
  211. const IntRect& clipBorder = mFileNameEdit->getClipBorder();
  212. int fileNameHeight = mFileNameEdit->getTextElement()->getRowHeight() + clipBorder.mTop + clipBorder.mBottom;
  213. mFileNameEdit->setFixedHeight(fileNameHeight);
  214. mFilterList->setFixedHeight(fileNameHeight);
  215. mFileNameLayout->setFixedHeight(fileNameHeight);
  216. }
  217. mButtonLayout->setFixedHeight(max(mOKButton->getHeight(), mCancelButton->getHeight()));
  218. }
  219. const std::string& FileSelector::getFileName() const
  220. {
  221. return mFileNameEdit->getText();
  222. }
  223. const std::string& FileSelector::getFilter() const
  224. {
  225. static std::string emptyFilter;
  226. Text* selectedFilter = static_cast<Text*>(mFilterList->getSelectedItem());
  227. if (selectedFilter)
  228. return selectedFilter->getText();
  229. return emptyFilter;
  230. }
  231. unsigned FileSelector::getFilterIndex() const
  232. {
  233. return mFilterList->getSelection();
  234. }
  235. void FileSelector::refreshFiles()
  236. {
  237. mIgnoreEvents = true;
  238. mFileList->removeAllItems();
  239. mFileEntries.clear();
  240. std::vector<std::string> directories;
  241. std::vector<std::string> files;
  242. scanDirectory(directories, mPath, "*.*", SCAN_DIRECTORIES, false);
  243. scanDirectory(files, mPath, getFilter(), SCAN_FILES, false);
  244. for (unsigned i = 0; i < directories.size(); ++i)
  245. {
  246. FileSelectorEntry newEntry;
  247. newEntry.mName = directories[i];
  248. newEntry.mDirectory = true;
  249. mFileEntries.push_back(newEntry);
  250. }
  251. for (unsigned i = 0; i < files.size(); ++i)
  252. {
  253. FileSelectorEntry newEntry;
  254. newEntry.mName = files[i];
  255. newEntry.mDirectory = false;
  256. mFileEntries.push_back(newEntry);
  257. }
  258. // Sort and add to the list view
  259. // While items are being added, disable layout update for performance optimization
  260. std::sort(mFileEntries.begin(), mFileEntries.end(), compareEntries);
  261. UIElement* listContent = mFileList->getContentElement();
  262. listContent->disableLayoutUpdate();
  263. for (unsigned i = 0; i < mFileEntries.size(); ++i)
  264. {
  265. std::string displayName;
  266. if (mFileEntries[i].mDirectory)
  267. displayName = "<DIR> " + mFileEntries[i].mName;
  268. else
  269. displayName = mFileEntries[i].mName;
  270. Text* entryText = new Text();
  271. entryText->setText(displayName);
  272. entryText->setStyle(mStyle, "FileSelectorListText", mUI->getResourceCache());
  273. mFileList->addItem(entryText);
  274. }
  275. listContent->enableLayoutUpdate();
  276. listContent->updateLayout();
  277. mIgnoreEvents = false;
  278. // Clear filename from the previous dir so that there is no confusion
  279. setFileName(std::string());
  280. mLastUsedFilter = getFilter();
  281. }
  282. bool FileSelector::enterFile()
  283. {
  284. unsigned index = mFileList->getSelection();
  285. if (index >= mFileEntries.size())
  286. return false;
  287. if (mFileEntries[index].mDirectory)
  288. {
  289. // If a directory doubleclicked, enter it. Recognize . and .. as a special case
  290. const std::string& newPath = mFileEntries[index].mName;
  291. if ((newPath != ".") && (newPath != ".."))
  292. setPath(mPath + newPath);
  293. else if (newPath == "..")
  294. {
  295. std::string parentPath = getParentPath(mPath);
  296. setPath(parentPath);
  297. }
  298. return true;
  299. }
  300. else
  301. {
  302. // Doubleclicking a file is the same as pressing OK
  303. if (!mDirectoryMode)
  304. {
  305. using namespace FileSelected;
  306. VariantMap eventData;
  307. eventData[P_FILENAME] = mPath + mFileEntries[index].mName;
  308. eventData[P_OK] = true;
  309. sendEvent(EVENT_FILESELECTED, eventData);
  310. }
  311. }
  312. return false;
  313. }
  314. void FileSelector::handleFilterChanged(StringHash eventType, VariantMap& eventData)
  315. {
  316. if (mIgnoreEvents)
  317. return;
  318. if (getFilter() != mLastUsedFilter)
  319. refreshFiles();
  320. }
  321. void FileSelector::handlePathChanged(StringHash eventType, VariantMap& eventData)
  322. {
  323. if (mIgnoreEvents)
  324. return;
  325. // Attempt to set path. Restores old if does not exist
  326. setPath(mPathEdit->getText());
  327. }
  328. void FileSelector::handleFileSelected(StringHash eventType, VariantMap& eventData)
  329. {
  330. if (mIgnoreEvents)
  331. return;
  332. unsigned index = mFileList->getSelection();
  333. if (index >= mFileEntries.size())
  334. return;
  335. // If a file selected, update the filename edit field
  336. if (!mFileEntries[index].mDirectory)
  337. setFileName(mFileEntries[index].mName);
  338. }
  339. void FileSelector::handleFileDoubleClicked(StringHash eventType, VariantMap& eventData)
  340. {
  341. if (mIgnoreEvents)
  342. return;
  343. enterFile();
  344. }
  345. void FileSelector::handleFileListKey(StringHash eventType, VariantMap& eventData)
  346. {
  347. if (mIgnoreEvents)
  348. return;
  349. using namespace UnhandledKey;
  350. if (eventData[P_KEY].getInt() == KEY_RETURN)
  351. {
  352. bool entered = enterFile();
  353. // When a key is used to enter a directory, select the first file if no selection
  354. if ((entered) && (!mFileList->getSelectedItem()))
  355. mFileList->setSelection(0);
  356. }
  357. }
  358. void FileSelector::handleOKPressed(StringHash eventType, VariantMap& eventData)
  359. {
  360. if (mIgnoreEvents)
  361. return;
  362. const std::string& fileName = getFileName();
  363. if (!mDirectoryMode)
  364. {
  365. if (!fileName.empty())
  366. {
  367. using namespace FileSelected;
  368. VariantMap newEventData;
  369. newEventData[P_FILENAME] = mPath + getFileName();
  370. newEventData[P_OK] = true;
  371. sendEvent(EVENT_FILESELECTED, newEventData);
  372. }
  373. }
  374. else if ((eventType == EVENT_RELEASED) && (!mPath.empty()))
  375. {
  376. using namespace FileSelected;
  377. VariantMap newEventData;
  378. newEventData[P_FILENAME] = mPath;
  379. newEventData[P_OK] = true;
  380. sendEvent(EVENT_FILESELECTED, newEventData);
  381. }
  382. }
  383. void FileSelector::handleCancelPressed(StringHash eventType, VariantMap& eventData)
  384. {
  385. if (mIgnoreEvents)
  386. return;
  387. using namespace FileSelected;
  388. VariantMap newEventData;
  389. newEventData[P_FILENAME] = std::string();
  390. newEventData[P_OK] = false;
  391. sendEvent(EVENT_FILESELECTED, newEventData);
  392. }