FileSelector.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 "Context.h"
  25. #include "DropDownList.h"
  26. #include "File.h"
  27. #include "FileSelector.h"
  28. #include "FileSystem.h"
  29. #include "InputEvents.h"
  30. #include "LineEdit.h"
  31. #include "ListView.h"
  32. #include "Text.h"
  33. #include "UI.h"
  34. #include "UIEvents.h"
  35. #include "Window.h"
  36. #include "Sort.h"
  37. #include "DebugNew.h"
  38. static const String emptyFilter;
  39. static bool CompareEntries(const FileSelectorEntry& lhs, const FileSelectorEntry& rhs)
  40. {
  41. if ((lhs.directory_) && (!rhs.directory_))
  42. return true;
  43. if ((!lhs.directory_) && (rhs.directory_))
  44. return false;
  45. return lhs.name_ < rhs.name_;
  46. }
  47. OBJECTTYPESTATIC(FileSelector);
  48. FileSelector::FileSelector(Context* context) :
  49. Object(context),
  50. ignoreEvents_(false),
  51. directoryMode_(false)
  52. {
  53. window_ = new Window(context_);
  54. window_->SetLayout(LM_VERTICAL);
  55. titleLayout = new UIElement(context_);
  56. titleLayout->SetLayout(LM_HORIZONTAL);
  57. window_->AddChild(titleLayout);
  58. titleText_ = new Text(context_);
  59. titleLayout->AddChild(titleText_);
  60. closeButton_ = new Button(context_);
  61. titleLayout->AddChild(closeButton_);
  62. pathEdit_ = new LineEdit(context_);
  63. window_->AddChild(pathEdit_);
  64. fileList_ = new ListView(context_);
  65. window_->AddChild(fileList_);
  66. fileNameLayout_ = new UIElement(context_);
  67. fileNameLayout_->SetLayout(LM_HORIZONTAL);
  68. fileNameEdit_ = new LineEdit(context_);
  69. fileNameLayout_->AddChild(fileNameEdit_);
  70. filterList_ = new DropDownList(context_);
  71. fileNameLayout_->AddChild(filterList_);
  72. window_->AddChild(fileNameLayout_);
  73. buttonLayout_ = new UIElement(context_);
  74. buttonLayout_->SetLayout(LM_HORIZONTAL);
  75. buttonLayout_->AddChild(new UIElement(context_)); // Add spacer
  76. okButton_ = new Button(context_);
  77. okButtonText_ = new Text(context_);
  78. okButtonText_->SetAlignment(HA_CENTER, VA_CENTER);
  79. okButton_->AddChild(okButtonText_);
  80. buttonLayout_->AddChild(okButton_);
  81. buttonLayout_->AddChild(new UIElement(context_)); // Add spacer
  82. cancelButton_ = new Button(context_);
  83. cancelButtonText_ = new Text(context_);
  84. cancelButtonText_->SetAlignment(HA_CENTER, VA_CENTER);
  85. cancelButton_->AddChild(cancelButtonText_);
  86. buttonLayout_->AddChild(cancelButton_);
  87. buttonLayout_->AddChild(new UIElement(context_)); // Add spacer
  88. window_->AddChild(buttonLayout_);
  89. UI* ui = GetSubsystem<UI>();
  90. if (ui)
  91. ui->GetRootElement()->AddChild(window_);
  92. Vector<String> defaultFilters;
  93. defaultFilters.Push("*.*");
  94. SetFilters(defaultFilters, 0);
  95. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  96. if (fileSystem)
  97. SetPath(fileSystem->GetCurrentDir());
  98. // Focus the fileselector's filelist initially when created, and bring to front
  99. if (ui)
  100. ui->SetFocusElement(fileList_);
  101. window_->BringToFront();
  102. SubscribeToEvent(filterList_, E_ITEMSELECTED, HANDLER(FileSelector, HandleFilterChanged));
  103. SubscribeToEvent(pathEdit_, E_TEXTFINISHED, HANDLER(FileSelector, HandlePathChanged));
  104. SubscribeToEvent(fileNameEdit_, E_TEXTFINISHED, HANDLER(FileSelector, HandleOKPressed));
  105. SubscribeToEvent(fileList_, E_ITEMSELECTED, HANDLER(FileSelector, HandleFileSelected));
  106. SubscribeToEvent(fileList_, E_ITEMDOUBLECLICKED, HANDLER(FileSelector, HandleFileDoubleClicked));
  107. SubscribeToEvent(fileList_, E_UNHANDLEDKEY, HANDLER(FileSelector, HandleFileListKey));
  108. SubscribeToEvent(okButton_, E_RELEASED, HANDLER(FileSelector, HandleOKPressed));
  109. SubscribeToEvent(cancelButton_, E_RELEASED, HANDLER(FileSelector, HandleCancelPressed));
  110. SubscribeToEvent(closeButton_, E_RELEASED, HANDLER(FileSelector, HandleCancelPressed));
  111. }
  112. FileSelector::~FileSelector()
  113. {
  114. UI* ui = GetSubsystem<UI>();
  115. if (ui)
  116. ui->GetRootElement()->RemoveChild(window_);
  117. }
  118. void FileSelector::RegisterObject(Context* context)
  119. {
  120. context->RegisterFactory<FileSelector>();
  121. }
  122. void FileSelector::SetStyle(XMLFile* style)
  123. {
  124. if (!style)
  125. return;
  126. style_ = style;
  127. window_->SetStyleAuto(style);
  128. window_->SetStyle(style, "FileSelector");
  129. titleText_->SetStyle(style, "FileSelectorTitleText");
  130. closeButton_->SetStyle(style, "CloseButton");
  131. okButtonText_->SetStyle(style, "FileSelectorButtonText");
  132. cancelButtonText_->SetStyle(style, "FileSelectorButtonText");
  133. titleLayout->SetStyle(style, "FileSelectorTitleLayout");
  134. fileNameLayout_->SetStyle(style, "FileSelectorLayout");
  135. buttonLayout_->SetStyle(style, "FileSelectorLayout");
  136. fileList_->SetStyleAuto(style);
  137. fileNameEdit_->SetStyleAuto(style);
  138. pathEdit_->SetStyleAuto(style);
  139. filterList_->SetStyleAuto(style);
  140. filterList_->SetStyle(style, "FileSelectorFilterList");
  141. okButton_->SetStyleAuto(style);
  142. cancelButton_->SetStyleAuto(style);
  143. okButton_->SetStyle(style, "FileSelectorButton");
  144. cancelButton_->SetStyle(style, "FileSelectorButton");
  145. PODVector<UIElement*> filterTexts = filterList_->GetListView()->GetContentElement()->GetChildren();
  146. for (unsigned i = 0; i < filterTexts.Size(); ++i)
  147. filterTexts[i]->SetStyle(style, "FileSelectorFilterText");
  148. PODVector<UIElement*> listTexts = fileList_->GetContentElement()->GetChildren();
  149. for (unsigned i = 0; i < listTexts.Size(); ++i)
  150. listTexts[i]->SetStyle(style, "FileSelectorListText");
  151. UpdateElements();
  152. }
  153. void FileSelector::SetTitle(const String& text)
  154. {
  155. titleText_->SetText(text);
  156. }
  157. void FileSelector::SetButtonTexts(const String& okText, const String& cancelText)
  158. {
  159. okButtonText_->SetText(okText);
  160. cancelButtonText_->SetText(cancelText);
  161. }
  162. void FileSelector::SetPath(const String& path)
  163. {
  164. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  165. if (!fileSystem)
  166. return;
  167. if (fileSystem->DirExists(path))
  168. {
  169. path_ = AddTrailingSlash(path);
  170. SetLineEditText(pathEdit_, path_);
  171. RefreshFiles();
  172. }
  173. else
  174. {
  175. // If path was invalid, restore the old path to the line edit
  176. if (pathEdit_->GetText() != path_)
  177. SetLineEditText(pathEdit_, path_);
  178. }
  179. }
  180. void FileSelector::SetFileName(const String& fileName)
  181. {
  182. SetLineEditText(fileNameEdit_, fileName);
  183. }
  184. void FileSelector::SetFilters(const Vector<String>& filters, unsigned defaultIndex)
  185. {
  186. if (filters.Empty())
  187. return;
  188. ignoreEvents_ = true;
  189. filters_ = filters;
  190. filterList_->RemoveAllItems();
  191. for (unsigned i = 0; i < filters_.Size(); ++i)
  192. {
  193. Text* filterText = new Text(context_);
  194. filterText->SetText(filters_[i]);
  195. filterText->SetStyle(style_, "FileSelectorFilterText");
  196. filterList_->AddItem(filterText);
  197. }
  198. if (defaultIndex > filters.Size())
  199. defaultIndex = 0;
  200. filterList_->SetSelection(defaultIndex);
  201. ignoreEvents_ = false;
  202. if (GetFilter() != lastUsedFilter_)
  203. RefreshFiles();
  204. }
  205. void FileSelector::SetDirectoryMode(bool enable)
  206. {
  207. directoryMode_ = enable;
  208. }
  209. void FileSelector::UpdateElements()
  210. {
  211. {
  212. const IntRect& clipBorder = pathEdit_->GetClipBorder();
  213. pathEdit_->SetFixedHeight(pathEdit_->GetTextElement()->GetRowHeight() + clipBorder.top_ + clipBorder.bottom_);
  214. }
  215. {
  216. const IntRect& clipBorder = fileNameEdit_->GetClipBorder();
  217. int fileNameHeight = fileNameEdit_->GetTextElement()->GetRowHeight() + clipBorder.top_ + clipBorder.bottom_;
  218. fileNameEdit_->SetFixedHeight(fileNameHeight);
  219. filterList_->SetFixedHeight(fileNameHeight);
  220. fileNameLayout_->SetFixedHeight(fileNameHeight);
  221. }
  222. buttonLayout_->SetFixedHeight(Max(okButton_->GetHeight(), cancelButton_->GetHeight()));
  223. }
  224. const String& FileSelector::GetFileName() const
  225. {
  226. return fileNameEdit_->GetText();
  227. }
  228. const String& FileSelector::GetFilter() const
  229. {
  230. Text* selectedFilter = static_cast<Text*>(filterList_->GetSelectedItem());
  231. if (selectedFilter)
  232. return selectedFilter->GetText();
  233. return emptyFilter;
  234. }
  235. unsigned FileSelector::GetFilterIndex() const
  236. {
  237. return filterList_->GetSelection();
  238. }
  239. void FileSelector::SetLineEditText(LineEdit* edit, const String& text)
  240. {
  241. ignoreEvents_ = true;
  242. edit->SetText(text);
  243. ignoreEvents_ = false;
  244. }
  245. void FileSelector::RefreshFiles()
  246. {
  247. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  248. if (!fileSystem)
  249. return;
  250. ignoreEvents_ = true;
  251. fileList_->RemoveAllItems();
  252. fileEntries_.Clear();
  253. Vector<String> directories;
  254. Vector<String> files;
  255. fileSystem->ScanDir(directories, path_, "*.*", SCAN_DIRS, false);
  256. fileSystem->ScanDir(files, path_, GetFilter(), SCAN_FILES, false);
  257. for (unsigned i = 0; i < directories.Size(); ++i)
  258. {
  259. FileSelectorEntry newEntry;
  260. newEntry.name_ = directories[i];
  261. newEntry.directory_ = true;
  262. fileEntries_.Push(newEntry);
  263. }
  264. for (unsigned i = 0; i < files.Size(); ++i)
  265. {
  266. FileSelectorEntry newEntry;
  267. newEntry.name_ = files[i];
  268. newEntry.directory_ = false;
  269. fileEntries_.Push(newEntry);
  270. }
  271. // Sort and add to the list view
  272. // While items are being added, disable layout update for performance optimization
  273. Sort(fileEntries_.Begin(), fileEntries_.End(), CompareEntries);
  274. UIElement* listContent = fileList_->GetContentElement();
  275. listContent->DisableLayoutUpdate();
  276. for (unsigned i = 0; i < fileEntries_.Size(); ++i)
  277. {
  278. String displayName;
  279. if (fileEntries_[i].directory_)
  280. displayName = "<DIR> " + fileEntries_[i].name_;
  281. else
  282. displayName = fileEntries_[i].name_;
  283. Text* entryText = new Text(context_);
  284. entryText->SetText(displayName);
  285. entryText->SetStyle(style_, "FileSelectorListText");
  286. fileList_->AddItem(entryText);
  287. }
  288. listContent->EnableLayoutUpdate();
  289. listContent->UpdateLayout();
  290. ignoreEvents_ = false;
  291. // Clear filename from the previous dir so that there is no confusion
  292. SetFileName(String());
  293. lastUsedFilter_ = GetFilter();
  294. }
  295. bool FileSelector::EnterFile()
  296. {
  297. unsigned index = fileList_->GetSelection();
  298. if (index >= fileEntries_.Size())
  299. return false;
  300. if (fileEntries_[index].directory_)
  301. {
  302. // If a directory doubleclicked, enter it. Recognize . and .. as a special case
  303. const String& newPath = fileEntries_[index].name_;
  304. if ((newPath != ".") && (newPath != ".."))
  305. SetPath(path_ + newPath);
  306. else if (newPath == "..")
  307. {
  308. String parentPath = GetParentPath(path_);
  309. SetPath(parentPath);
  310. }
  311. return true;
  312. }
  313. else
  314. {
  315. // Doubleclicking a file is the same as pressing OK
  316. if (!directoryMode_)
  317. {
  318. using namespace FileSelected;
  319. VariantMap eventData;
  320. eventData[P_FILENAME] = path_ + fileEntries_[index].name_;
  321. eventData[P_OK] = true;
  322. SendEvent(E_FILESELECTED, eventData);
  323. }
  324. }
  325. return false;
  326. }
  327. void FileSelector::HandleFilterChanged(StringHash eventType, VariantMap& eventData)
  328. {
  329. if (ignoreEvents_)
  330. return;
  331. if (GetFilter() != lastUsedFilter_)
  332. RefreshFiles();
  333. }
  334. void FileSelector::HandlePathChanged(StringHash eventType, VariantMap& eventData)
  335. {
  336. if (ignoreEvents_)
  337. return;
  338. // Attempt to set path. Restores old if does not exist
  339. SetPath(pathEdit_->GetText());
  340. }
  341. void FileSelector::HandleFileSelected(StringHash eventType, VariantMap& eventData)
  342. {
  343. if (ignoreEvents_)
  344. return;
  345. unsigned index = fileList_->GetSelection();
  346. if (index >= fileEntries_.Size())
  347. return;
  348. // If a file selected, update the filename edit field
  349. if (!fileEntries_[index].directory_)
  350. SetFileName(fileEntries_[index].name_);
  351. }
  352. void FileSelector::HandleFileDoubleClicked(StringHash eventType, VariantMap& eventData)
  353. {
  354. if (ignoreEvents_)
  355. return;
  356. EnterFile();
  357. }
  358. void FileSelector::HandleFileListKey(StringHash eventType, VariantMap& eventData)
  359. {
  360. if (ignoreEvents_)
  361. return;
  362. using namespace UnhandledKey;
  363. if (eventData[P_KEY].GetInt() == KEY_RETURN)
  364. {
  365. bool entered = EnterFile();
  366. // When a key is used to enter a directory, select the first file if no selection
  367. if ((entered) && (!fileList_->GetSelectedItem()))
  368. fileList_->SetSelection(0);
  369. }
  370. }
  371. void FileSelector::HandleOKPressed(StringHash eventType, VariantMap& eventData)
  372. {
  373. if (ignoreEvents_)
  374. return;
  375. const String& fileName = GetFileName();
  376. if (!directoryMode_)
  377. {
  378. if (!fileName.Empty())
  379. {
  380. using namespace FileSelected;
  381. VariantMap newEventData;
  382. newEventData[P_FILENAME] = path_ + GetFileName();
  383. newEventData[P_OK] = true;
  384. SendEvent(E_FILESELECTED, newEventData);
  385. }
  386. }
  387. else if ((eventType == E_RELEASED) && (!path_.Empty()))
  388. {
  389. using namespace FileSelected;
  390. VariantMap newEventData;
  391. newEventData[P_FILENAME] = path_;
  392. newEventData[P_OK] = true;
  393. SendEvent(E_FILESELECTED, newEventData);
  394. }
  395. }
  396. void FileSelector::HandleCancelPressed(StringHash eventType, VariantMap& eventData)
  397. {
  398. if (ignoreEvents_)
  399. return;
  400. using namespace FileSelected;
  401. VariantMap newEventData;
  402. newEventData[P_FILENAME] = String();
  403. newEventData[P_OK] = false;
  404. SendEvent(E_FILESELECTED, newEventData);
  405. }