FileSelector.cpp 14 KB

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