FileSelector.cpp 15 KB

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