FileSelector.cpp 14 KB

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