FileSelector.cpp 15 KB

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