FileSelector.cpp 15 KB

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