FileSelector.cpp 15 KB

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