FileSelector.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. {
  49. if (!mUI)
  50. EXCEPTION("Null UI for FileSelector");
  51. mWindow = new Window();
  52. mWindow->setLayout(LM_VERTICAL);
  53. mTitleText = new Text();
  54. mWindow->addChild(mTitleText);
  55. mPathEdit = new LineEdit();
  56. mWindow->addChild(mPathEdit);
  57. mFileList = new ListView();
  58. mWindow->addChild(mFileList);
  59. mFileNameLayout = new UIElement();
  60. mFileNameLayout->setLayout(LM_HORIZONTAL);
  61. mFileNameEdit = new LineEdit();
  62. mFileNameLayout->addChild(mFileNameEdit);
  63. mFilterList = new DropDownList();
  64. mFileNameLayout->addChild(mFilterList);
  65. mWindow->addChild(mFileNameLayout);
  66. mButtonLayout = new UIElement();
  67. mButtonLayout->setLayout(LM_HORIZONTAL);
  68. mButtonLayout->addChild(new UIElement()); // Add spacer
  69. mOKButton = new Button();
  70. mOKButtonText = new Text();
  71. mOKButtonText->setAlignment(HA_CENTER, VA_CENTER);
  72. mOKButton->addChild(mOKButtonText);
  73. mButtonLayout->addChild(mOKButton);
  74. mButtonLayout->addChild(new UIElement()); // Add spacer
  75. mCancelButton = new Button();
  76. mCancelButtonText = new Text();
  77. mCancelButtonText->setAlignment(HA_CENTER, VA_CENTER);
  78. mCancelButton->addChild(mCancelButtonText);
  79. mButtonLayout->addChild(mCancelButton);
  80. mButtonLayout->addChild(new UIElement()); // Add spacer
  81. mWindow->addChild(mButtonLayout);
  82. mUI->getRootElement()->addChild(mWindow);
  83. std::vector<std::string> defaultFilters;
  84. defaultFilters.push_back("*.*");
  85. setFilters(defaultFilters, 0);
  86. setPath(getCurrentDirectory());
  87. // Focus the fileselector's filelist initially when created, and bring to front
  88. mUI->setFocusElement(mFileList);
  89. mWindow->bringToFront();
  90. subscribeToEvent(mFilterList, EVENT_ITEMSELECTED, EVENT_HANDLER(FileSelector, handleFilterChanged));
  91. subscribeToEvent(mPathEdit, EVENT_TEXTFINISHED, EVENT_HANDLER(FileSelector, handlePathChanged));
  92. subscribeToEvent(mFileList, EVENT_ITEMSELECTED, EVENT_HANDLER(FileSelector, handleFileSelected));
  93. subscribeToEvent(mFileList, EVENT_ITEMDOUBLECLICKED, EVENT_HANDLER(FileSelector, handleFileDoubleClicked));
  94. subscribeToEvent(mFileList, EVENT_UNHANDLEDKEY, EVENT_HANDLER(FileSelector, handleFileListKey));
  95. subscribeToEvent(mOKButton, EVENT_PRESSED, EVENT_HANDLER(FileSelector, handleOKPressed));
  96. subscribeToEvent(mCancelButton, EVENT_PRESSED, EVENT_HANDLER(FileSelector, handleCancelPressed));
  97. }
  98. FileSelector::~FileSelector()
  99. {
  100. UIElement* root = mUI->getRootElement();
  101. //! \todo This should not be necessary
  102. root->removeChild(mFilterList->getPopup());
  103. root->removeChild(mWindow);
  104. }
  105. void FileSelector::setStyle(XMLFile* style)
  106. {
  107. if (!style)
  108. return;
  109. mStyle = style;
  110. ResourceCache* cache = mUI->getResourceCache();
  111. mWindow->setStyleAuto(style, cache);
  112. mWindow->setStyle(style, "FileSelector", cache);
  113. mTitleText->setStyle(style, "FileSelectorTitleText", cache);
  114. mOKButtonText->setStyle(style, "FileSelectorButtonText", cache);
  115. mCancelButtonText->setStyle(style, "FileSelectorButtonText", cache);
  116. mFileNameLayout->setStyle(style, "FileSelectorLayout", cache);
  117. mButtonLayout->setStyle(style, "FileSelectorLayout", cache);
  118. mFileList->setStyleAuto(style, cache);
  119. mFileNameEdit->setStyleAuto(style, cache);
  120. mPathEdit->setStyleAuto(style, cache);
  121. mFilterList->setStyleAuto(style, cache);
  122. mFilterList->setStyle(style, "FileSelectorFilterList", cache);
  123. mOKButton->setStyleAuto(style, cache);
  124. mCancelButton->setStyleAuto(style, cache);
  125. mOKButton->setStyle(style, "FileSelectorButton", cache);
  126. mCancelButton->setStyle(style, "FileSelectorButton", cache);
  127. std::vector<UIElement*> filterTexts = mFilterList->getListView()->getContentElement()->getChildren();
  128. for (unsigned i = 0; i < filterTexts.size(); ++i)
  129. filterTexts[i]->setStyle(style, "FileSelectorFilterText", cache);
  130. std::vector<UIElement*> listTexts = mFileList->getContentElement()->getChildren();
  131. for (unsigned i = 0; i < listTexts.size(); ++i)
  132. listTexts[i]->setStyle(style, "FileSelectorListText", cache);
  133. updateElements();
  134. }
  135. void FileSelector::setTitle(const std::string& text)
  136. {
  137. mTitleText->setText(text);
  138. }
  139. void FileSelector::setButtonTexts(const std::string& okText, const std::string& cancelText)
  140. {
  141. mOKButtonText->setText(okText);
  142. mCancelButtonText->setText(cancelText);
  143. }
  144. void FileSelector::setPath(const std::string& path)
  145. {
  146. if (directoryExists(path))
  147. {
  148. mPath = fixPath(path);
  149. mIgnoreEvents = true;
  150. mPathEdit->setText(mPath);
  151. mIgnoreEvents = false;
  152. refreshFiles();
  153. }
  154. else
  155. {
  156. // If path was invalid, restore the old path to the line edit
  157. if (mPathEdit->getText() != mPath)
  158. {
  159. mIgnoreEvents = true;
  160. mPathEdit->setText(mPath);
  161. mIgnoreEvents = false;
  162. }
  163. }
  164. }
  165. void FileSelector::setFileName(const std::string& fileName)
  166. {
  167. mIgnoreEvents = true;
  168. mFileNameEdit->setText(fileName);
  169. mIgnoreEvents = false;
  170. }
  171. void FileSelector::setFilters(const std::vector<std::string>& filters, unsigned defaultIndex)
  172. {
  173. if (filters.empty())
  174. return;
  175. mIgnoreEvents = true;
  176. mFilters = filters;
  177. mFilterList->removeAllItems();
  178. for (unsigned i = 0; i < mFilters.size(); ++i)
  179. {
  180. Text* filterText = new Text();
  181. filterText->setText(mFilters[i]);
  182. filterText->setStyle(mStyle, "FileSelectorFilterText", mUI->getResourceCache());
  183. mFilterList->addItem(filterText);
  184. }
  185. if (defaultIndex > filters.size())
  186. defaultIndex = 0;
  187. mFilterList->setSelection(defaultIndex);
  188. mIgnoreEvents = false;
  189. if (getFilter() != mLastUsedFilter)
  190. refreshFiles();
  191. }
  192. void FileSelector::updateElements()
  193. {
  194. {
  195. const IntRect& clipBorder = mPathEdit->getClipBorder();
  196. mPathEdit->setFixedHeight(mPathEdit->getTextElement()->getRowHeight() + clipBorder.mTop + clipBorder.mBottom);
  197. }
  198. {
  199. const IntRect& clipBorder = mFileNameEdit->getClipBorder();
  200. int fileNameHeight = mFileNameEdit->getTextElement()->getRowHeight() + clipBorder.mTop + clipBorder.mBottom;
  201. mFileNameEdit->setFixedHeight(fileNameHeight);
  202. mFilterList->setFixedHeight(fileNameHeight);
  203. mFileNameLayout->setFixedHeight(fileNameHeight);
  204. }
  205. mButtonLayout->setFixedHeight(max(mOKButton->getHeight(), mCancelButton->getHeight()));
  206. }
  207. const std::string& FileSelector::getFileName() const
  208. {
  209. return mFileNameEdit->getText();
  210. }
  211. const std::string& FileSelector::getFilter() const
  212. {
  213. static std::string emptyFilter;
  214. Text* selectedFilter = static_cast<Text*>(mFilterList->getSelectedItem());
  215. if (selectedFilter)
  216. return selectedFilter->getText();
  217. return emptyFilter;
  218. }
  219. unsigned FileSelector::getFilterIndex() const
  220. {
  221. return mFilterList->getSelection();
  222. }
  223. void FileSelector::refreshFiles()
  224. {
  225. mIgnoreEvents = true;
  226. mFileList->removeAllItems();
  227. mFileEntries.clear();
  228. std::vector<std::string> directories;
  229. std::vector<std::string> files;
  230. scanDirectory(directories, mPath, "*.*", SCAN_DIRECTORIES, false);
  231. scanDirectory(files, mPath, getFilter(), SCAN_FILES, false);
  232. for (unsigned i = 0; i < directories.size(); ++i)
  233. {
  234. FileSelectorEntry newEntry;
  235. newEntry.mName = directories[i];
  236. newEntry.mDirectory = true;
  237. mFileEntries.push_back(newEntry);
  238. }
  239. for (unsigned i = 0; i < files.size(); ++i)
  240. {
  241. FileSelectorEntry newEntry;
  242. newEntry.mName = files[i];
  243. newEntry.mDirectory = false;
  244. mFileEntries.push_back(newEntry);
  245. }
  246. // Sort and add to the list view
  247. // While items are being added, disable layout update for performance optimization
  248. std::sort(mFileEntries.begin(), mFileEntries.end(), compareEntries);
  249. UIElement* listContent = mFileList->getContentElement();
  250. listContent->disableLayoutUpdate();
  251. for (unsigned i = 0; i < mFileEntries.size(); ++i)
  252. {
  253. std::string displayName;
  254. if (mFileEntries[i].mDirectory)
  255. displayName = "<DIR> " + mFileEntries[i].mName;
  256. else
  257. displayName = mFileEntries[i].mName;
  258. Text* entryText = new Text();
  259. entryText->setText(displayName);
  260. entryText->setStyle(mStyle, "FileSelectorListText", mUI->getResourceCache());
  261. mFileList->addItem(entryText);
  262. }
  263. listContent->enableLayoutUpdate();
  264. listContent->updateLayout();
  265. mIgnoreEvents = false;
  266. // Clear filename from the previous dir so that there is no confusion
  267. setFileName(std::string());
  268. mLastUsedFilter = getFilter();
  269. }
  270. bool FileSelector::enterFile()
  271. {
  272. unsigned index = mFileList->getSelection();
  273. if (index >= mFileEntries.size())
  274. return false;
  275. if (mFileEntries[index].mDirectory)
  276. {
  277. // If a directory doubleclicked, enter it. Recognize . and .. as a special case
  278. const std::string& newPath = mFileEntries[index].mName;
  279. if ((newPath != ".") && (newPath != ".."))
  280. setPath(mPath + newPath);
  281. else if (newPath == "..")
  282. {
  283. std::string parentPath = getParentPath(mPath);
  284. setPath(parentPath);
  285. }
  286. return true;
  287. }
  288. else
  289. {
  290. // Doubleclicking a file is the same as pressing OK
  291. using namespace FileSelected;
  292. VariantMap eventData;
  293. eventData[P_FILENAME] = mPath + mFileEntries[index].mName;
  294. eventData[P_OK] = true;
  295. sendEvent(EVENT_FILESELECTED, eventData);
  296. }
  297. return false;
  298. }
  299. void FileSelector::handleFilterChanged(StringHash eventType, VariantMap& eventData)
  300. {
  301. if (mIgnoreEvents)
  302. return;
  303. if (getFilter() != mLastUsedFilter)
  304. refreshFiles();
  305. }
  306. void FileSelector::handlePathChanged(StringHash eventType, VariantMap& eventData)
  307. {
  308. if (mIgnoreEvents)
  309. return;
  310. // Attempt to set path. Restores old if does not exist
  311. setPath(mPathEdit->getText());
  312. }
  313. void FileSelector::handleFileSelected(StringHash eventType, VariantMap& eventData)
  314. {
  315. if (mIgnoreEvents)
  316. return;
  317. unsigned index = mFileList->getSelection();
  318. if (index >= mFileEntries.size())
  319. return;
  320. // If a file selected, update the filename edit field
  321. if (!mFileEntries[index].mDirectory)
  322. setFileName(mFileEntries[index].mName);
  323. }
  324. void FileSelector::handleFileDoubleClicked(StringHash eventType, VariantMap& eventData)
  325. {
  326. if (mIgnoreEvents)
  327. return;
  328. enterFile();
  329. }
  330. void FileSelector::handleFileListKey(StringHash eventType, VariantMap& eventData)
  331. {
  332. if (mIgnoreEvents)
  333. return;
  334. using namespace UnhandledKey;
  335. if (eventData[P_KEY].getInt() == KEY_RETURN)
  336. {
  337. bool entered = enterFile();
  338. // When a key is used to enter a directory, select the first file if no selection
  339. if ((entered) && (!mFileList->getSelectedItem()))
  340. mFileList->setSelection(0);
  341. }
  342. }
  343. void FileSelector::handleOKPressed(StringHash eventType, VariantMap& eventData)
  344. {
  345. if (mIgnoreEvents)
  346. return;
  347. const std::string& fileName = getFileName();
  348. if (!fileName.empty())
  349. {
  350. using namespace FileSelected;
  351. VariantMap newEventData;
  352. newEventData[P_FILENAME] = mPath + getFileName();
  353. newEventData[P_OK] = true;
  354. sendEvent(EVENT_FILESELECTED, newEventData);
  355. }
  356. }
  357. void FileSelector::handleCancelPressed(StringHash eventType, VariantMap& eventData)
  358. {
  359. if (mIgnoreEvents)
  360. return;
  361. using namespace FileSelected;
  362. VariantMap newEventData;
  363. newEventData[P_FILENAME] = std::string();
  364. newEventData[P_OK] = false;
  365. sendEvent(EVENT_FILESELECTED, newEventData);
  366. }