FileSelector.cpp 14 KB

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