FileSelector.cpp 14 KB

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