FileSelector.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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();
  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. //! \todo This should not be necessary
  110. root->removeChild(mFilterList->getPopup());
  111. root->removeChild(mWindow);
  112. }
  113. void FileSelector::setStyle(XMLFile* style)
  114. {
  115. if (!style)
  116. return;
  117. mStyle = style;
  118. ResourceCache* cache = mUI->getResourceCache();
  119. mWindow->setStyleAuto(style, cache);
  120. mWindow->setStyle(style, "FileSelector", cache);
  121. mTitleText->setStyle(style, "FileSelectorTitleText", cache);
  122. mCloseButton->setStyle(style, "CloseButton", cache);
  123. mOKButtonText->setStyle(style, "FileSelectorButtonText", cache);
  124. mCancelButtonText->setStyle(style, "FileSelectorButtonText", cache);
  125. mTitleLayout->setStyle(style, "FileSelectorTitleLayout", cache);
  126. mFileNameLayout->setStyle(style, "FileSelectorLayout", cache);
  127. mButtonLayout->setStyle(style, "FileSelectorLayout", cache);
  128. mFileList->setStyleAuto(style, cache);
  129. mFileNameEdit->setStyleAuto(style, cache);
  130. mPathEdit->setStyleAuto(style, cache);
  131. mFilterList->setStyleAuto(style, cache);
  132. mFilterList->setStyle(style, "FileSelectorFilterList", cache);
  133. mOKButton->setStyleAuto(style, cache);
  134. mCancelButton->setStyleAuto(style, cache);
  135. mOKButton->setStyle(style, "FileSelectorButton", cache);
  136. mCancelButton->setStyle(style, "FileSelectorButton", cache);
  137. std::vector<UIElement*> filterTexts = mFilterList->getListView()->getContentElement()->getChildren();
  138. for (unsigned i = 0; i < filterTexts.size(); ++i)
  139. filterTexts[i]->setStyle(style, "FileSelectorFilterText", cache);
  140. std::vector<UIElement*> listTexts = mFileList->getContentElement()->getChildren();
  141. for (unsigned i = 0; i < listTexts.size(); ++i)
  142. listTexts[i]->setStyle(style, "FileSelectorListText", cache);
  143. updateElements();
  144. }
  145. void FileSelector::setTitle(const std::string& text)
  146. {
  147. mTitleText->setText(text);
  148. }
  149. void FileSelector::setButtonTexts(const std::string& okText, const std::string& cancelText)
  150. {
  151. mOKButtonText->setText(okText);
  152. mCancelButtonText->setText(cancelText);
  153. }
  154. void FileSelector::setPath(const std::string& path)
  155. {
  156. if (directoryExists(path))
  157. {
  158. mPath = fixPath(path);
  159. mIgnoreEvents = true;
  160. mPathEdit->setText(mPath);
  161. mIgnoreEvents = false;
  162. refreshFiles();
  163. }
  164. else
  165. {
  166. // If path was invalid, restore the old path to the line edit
  167. if (mPathEdit->getText() != mPath)
  168. {
  169. mIgnoreEvents = true;
  170. mPathEdit->setText(mPath);
  171. mIgnoreEvents = false;
  172. }
  173. }
  174. }
  175. void FileSelector::setFileName(const std::string& fileName)
  176. {
  177. mIgnoreEvents = true;
  178. mFileNameEdit->setText(fileName);
  179. mIgnoreEvents = false;
  180. }
  181. void FileSelector::setFilters(const std::vector<std::string>& filters, unsigned defaultIndex)
  182. {
  183. if (filters.empty())
  184. return;
  185. mIgnoreEvents = true;
  186. mFilters = filters;
  187. mFilterList->removeAllItems();
  188. for (unsigned i = 0; i < mFilters.size(); ++i)
  189. {
  190. Text* filterText = new Text();
  191. filterText->setText(mFilters[i]);
  192. filterText->setStyle(mStyle, "FileSelectorFilterText", mUI->getResourceCache());
  193. mFilterList->addItem(filterText);
  194. }
  195. if (defaultIndex > filters.size())
  196. defaultIndex = 0;
  197. mFilterList->setSelection(defaultIndex);
  198. mIgnoreEvents = false;
  199. if (getFilter() != mLastUsedFilter)
  200. refreshFiles();
  201. }
  202. void FileSelector::setDirectoryMode(bool enable)
  203. {
  204. mDirectoryMode = enable;
  205. }
  206. void FileSelector::updateElements()
  207. {
  208. {
  209. const IntRect& clipBorder = mPathEdit->getClipBorder();
  210. mPathEdit->setFixedHeight(mPathEdit->getTextElement()->getRowHeight() + clipBorder.mTop + clipBorder.mBottom);
  211. }
  212. {
  213. const IntRect& clipBorder = mFileNameEdit->getClipBorder();
  214. int fileNameHeight = mFileNameEdit->getTextElement()->getRowHeight() + clipBorder.mTop + clipBorder.mBottom;
  215. mFileNameEdit->setFixedHeight(fileNameHeight);
  216. mFilterList->setFixedHeight(fileNameHeight);
  217. mFileNameLayout->setFixedHeight(fileNameHeight);
  218. }
  219. mButtonLayout->setFixedHeight(max(mOKButton->getHeight(), mCancelButton->getHeight()));
  220. }
  221. const std::string& FileSelector::getFileName() const
  222. {
  223. return mFileNameEdit->getText();
  224. }
  225. const std::string& FileSelector::getFilter() const
  226. {
  227. static std::string emptyFilter;
  228. Text* selectedFilter = static_cast<Text*>(mFilterList->getSelectedItem());
  229. if (selectedFilter)
  230. return selectedFilter->getText();
  231. return emptyFilter;
  232. }
  233. unsigned FileSelector::getFilterIndex() const
  234. {
  235. return mFilterList->getSelection();
  236. }
  237. void FileSelector::refreshFiles()
  238. {
  239. mIgnoreEvents = true;
  240. mFileList->removeAllItems();
  241. mFileEntries.clear();
  242. std::vector<std::string> directories;
  243. std::vector<std::string> files;
  244. scanDirectory(directories, mPath, "*.*", SCAN_DIRECTORIES, false);
  245. scanDirectory(files, mPath, getFilter(), SCAN_FILES, false);
  246. for (unsigned i = 0; i < directories.size(); ++i)
  247. {
  248. FileSelectorEntry newEntry;
  249. newEntry.mName = directories[i];
  250. newEntry.mDirectory = true;
  251. mFileEntries.push_back(newEntry);
  252. }
  253. for (unsigned i = 0; i < files.size(); ++i)
  254. {
  255. FileSelectorEntry newEntry;
  256. newEntry.mName = files[i];
  257. newEntry.mDirectory = false;
  258. mFileEntries.push_back(newEntry);
  259. }
  260. // Sort and add to the list view
  261. // While items are being added, disable layout update for performance optimization
  262. std::sort(mFileEntries.begin(), mFileEntries.end(), compareEntries);
  263. UIElement* listContent = mFileList->getContentElement();
  264. listContent->disableLayoutUpdate();
  265. for (unsigned i = 0; i < mFileEntries.size(); ++i)
  266. {
  267. std::string displayName;
  268. if (mFileEntries[i].mDirectory)
  269. displayName = "<DIR> " + mFileEntries[i].mName;
  270. else
  271. displayName = mFileEntries[i].mName;
  272. Text* entryText = new Text();
  273. entryText->setText(displayName);
  274. entryText->setStyle(mStyle, "FileSelectorListText", mUI->getResourceCache());
  275. mFileList->addItem(entryText);
  276. }
  277. listContent->enableLayoutUpdate();
  278. listContent->updateLayout();
  279. mIgnoreEvents = false;
  280. // Clear filename from the previous dir so that there is no confusion
  281. setFileName(std::string());
  282. mLastUsedFilter = getFilter();
  283. }
  284. bool FileSelector::enterFile()
  285. {
  286. unsigned index = mFileList->getSelection();
  287. if (index >= mFileEntries.size())
  288. return false;
  289. if (mFileEntries[index].mDirectory)
  290. {
  291. // If a directory doubleclicked, enter it. Recognize . and .. as a special case
  292. const std::string& newPath = mFileEntries[index].mName;
  293. if ((newPath != ".") && (newPath != ".."))
  294. setPath(mPath + newPath);
  295. else if (newPath == "..")
  296. {
  297. std::string parentPath = getParentPath(mPath);
  298. setPath(parentPath);
  299. }
  300. return true;
  301. }
  302. else
  303. {
  304. // Doubleclicking a file is the same as pressing OK
  305. if (!mDirectoryMode)
  306. {
  307. using namespace FileSelected;
  308. VariantMap eventData;
  309. eventData[P_FILENAME] = mPath + mFileEntries[index].mName;
  310. eventData[P_OK] = true;
  311. sendEvent(EVENT_FILESELECTED, eventData);
  312. }
  313. }
  314. return false;
  315. }
  316. void FileSelector::handleFilterChanged(StringHash eventType, VariantMap& eventData)
  317. {
  318. if (mIgnoreEvents)
  319. return;
  320. if (getFilter() != mLastUsedFilter)
  321. refreshFiles();
  322. }
  323. void FileSelector::handlePathChanged(StringHash eventType, VariantMap& eventData)
  324. {
  325. if (mIgnoreEvents)
  326. return;
  327. // Attempt to set path. Restores old if does not exist
  328. setPath(mPathEdit->getText());
  329. }
  330. void FileSelector::handleFileSelected(StringHash eventType, VariantMap& eventData)
  331. {
  332. if (mIgnoreEvents)
  333. return;
  334. unsigned index = mFileList->getSelection();
  335. if (index >= mFileEntries.size())
  336. return;
  337. // If a file selected, update the filename edit field
  338. if (!mFileEntries[index].mDirectory)
  339. setFileName(mFileEntries[index].mName);
  340. }
  341. void FileSelector::handleFileDoubleClicked(StringHash eventType, VariantMap& eventData)
  342. {
  343. if (mIgnoreEvents)
  344. return;
  345. enterFile();
  346. }
  347. void FileSelector::handleFileListKey(StringHash eventType, VariantMap& eventData)
  348. {
  349. if (mIgnoreEvents)
  350. return;
  351. using namespace UnhandledKey;
  352. if (eventData[P_KEY].getInt() == KEY_RETURN)
  353. {
  354. bool entered = enterFile();
  355. // When a key is used to enter a directory, select the first file if no selection
  356. if ((entered) && (!mFileList->getSelectedItem()))
  357. mFileList->setSelection(0);
  358. }
  359. }
  360. void FileSelector::handleOKPressed(StringHash eventType, VariantMap& eventData)
  361. {
  362. if (mIgnoreEvents)
  363. return;
  364. const std::string& fileName = getFileName();
  365. if (!mDirectoryMode)
  366. {
  367. if (!fileName.empty())
  368. {
  369. using namespace FileSelected;
  370. VariantMap newEventData;
  371. newEventData[P_FILENAME] = mPath + getFileName();
  372. newEventData[P_OK] = true;
  373. sendEvent(EVENT_FILESELECTED, newEventData);
  374. }
  375. }
  376. else if ((eventType == EVENT_RELEASED) && (!mPath.empty()))
  377. {
  378. using namespace FileSelected;
  379. VariantMap newEventData;
  380. newEventData[P_FILENAME] = mPath;
  381. newEventData[P_OK] = true;
  382. sendEvent(EVENT_FILESELECTED, newEventData);
  383. }
  384. }
  385. void FileSelector::handleCancelPressed(StringHash eventType, VariantMap& eventData)
  386. {
  387. if (mIgnoreEvents)
  388. return;
  389. using namespace FileSelected;
  390. VariantMap newEventData;
  391. newEventData[P_FILENAME] = std::string();
  392. newEventData[P_OK] = false;
  393. sendEvent(EVENT_FILESELECTED, newEventData);
  394. }