FileSelector.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. OBJECTTYPESTATIC(FileSelector);
  48. FileSelector::FileSelector(Context* context) :
  49. Object(context),
  50. directoryMode_(false),
  51. ignoreEvents_(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. Vector<String> defaultFilters;
  90. defaultFilters.Push("*.*");
  91. SetFilters(defaultFilters, 0);
  92. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  93. if (fileSystem)
  94. SetPath(fileSystem->GetCurrentDir());
  95. // Focus the fileselector's filelist initially when created, and bring to front
  96. UI* ui = GetSubsystem<UI>();
  97. if (ui)
  98. {
  99. ui->GetRoot()->AddChild(window_);
  100. ui->SetFocusElement(fileList_);
  101. window_->SetModal(true);
  102. }
  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. SubscribeToEvent(window_, E_MODALCHANGED, HANDLER(FileSelector, HandleCancelPressed));
  113. }
  114. FileSelector::~FileSelector()
  115. {
  116. window_->Remove();
  117. }
  118. void FileSelector::RegisterObject(Context* context)
  119. {
  120. context->RegisterFactory<FileSelector>();
  121. }
  122. void FileSelector::SetDefaultStyle(XMLFile* style)
  123. {
  124. if (!style)
  125. return;
  126. window_->SetDefaultStyle(style);
  127. window_->SetStyle("FileSelector");
  128. titleText_->SetStyle("FileSelectorTitleText");
  129. closeButton_->SetStyle("CloseButton");
  130. okButtonText_->SetStyle("FileSelectorButtonText");
  131. cancelButtonText_->SetStyle("FileSelectorButtonText");
  132. titleLayout->SetStyle("FileSelectorTitleLayout");
  133. fileNameLayout_->SetStyle("FileSelectorLayout");
  134. buttonLayout_->SetStyle("FileSelectorLayout");
  135. fileList_->SetStyle("FileSelectorListView");
  136. fileNameEdit_->SetStyle("FileSelectorLineEdit");
  137. pathEdit_->SetStyle("FileSelectorLineEdit");
  138. filterList_->SetStyle("FileSelectorFilterList");
  139. okButton_->SetStyle("FileSelectorButton");
  140. cancelButton_->SetStyle("FileSelectorButton");
  141. const Vector<SharedPtr<UIElement> >& filterTexts = filterList_->GetListView()->GetContentElement()->GetChildren();
  142. for (unsigned i = 0; i < filterTexts.Size(); ++i)
  143. filterTexts[i]->SetStyle("FileSelectorFilterText");
  144. const Vector<SharedPtr<UIElement> >& listTexts = fileList_->GetContentElement()->GetChildren();
  145. for (unsigned i = 0; i < listTexts.Size(); ++i)
  146. listTexts[i]->SetStyle("FileSelectorListText");
  147. UpdateElements();
  148. }
  149. void FileSelector::SetTitle(const String& text)
  150. {
  151. titleText_->SetText(text);
  152. }
  153. void FileSelector::SetButtonTexts(const String& okText, const String& cancelText)
  154. {
  155. okButtonText_->SetText(okText);
  156. cancelButtonText_->SetText(cancelText);
  157. }
  158. void FileSelector::SetPath(const String& path)
  159. {
  160. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  161. if (!fileSystem)
  162. return;
  163. if (fileSystem->DirExists(path))
  164. {
  165. path_ = AddTrailingSlash(path);
  166. SetLineEditText(pathEdit_, path_);
  167. RefreshFiles();
  168. }
  169. else
  170. {
  171. // If path was invalid, restore the old path to the line edit
  172. if (pathEdit_->GetText() != path_)
  173. SetLineEditText(pathEdit_, path_);
  174. }
  175. }
  176. void FileSelector::SetFileName(const String& fileName)
  177. {
  178. SetLineEditText(fileNameEdit_, fileName);
  179. }
  180. void FileSelector::SetFilters(const Vector<String>& filters, unsigned defaultIndex)
  181. {
  182. if (filters.Empty())
  183. return;
  184. ignoreEvents_ = true;
  185. filters_ = filters;
  186. filterList_->RemoveAllItems();
  187. for (unsigned i = 0; i < filters_.Size(); ++i)
  188. {
  189. Text* filterText = new Text(context_);
  190. filterList_->AddItem(filterText);
  191. filterText->SetText(filters_[i]);
  192. filterText->SetStyle("FileSelectorFilterText");
  193. }
  194. if (defaultIndex > filters.Size())
  195. defaultIndex = 0;
  196. filterList_->SetSelection(defaultIndex);
  197. ignoreEvents_ = false;
  198. if (GetFilter() != lastUsedFilter_)
  199. RefreshFiles();
  200. }
  201. void FileSelector::SetDirectoryMode(bool enable)
  202. {
  203. directoryMode_ = enable;
  204. }
  205. void FileSelector::UpdateElements()
  206. {
  207. {
  208. const IntRect& clipBorder = pathEdit_->GetClipBorder();
  209. pathEdit_->SetFixedHeight(pathEdit_->GetTextElement()->GetRowHeight() + clipBorder.top_ + clipBorder.bottom_);
  210. }
  211. {
  212. const IntRect& clipBorder = fileNameEdit_->GetClipBorder();
  213. int fileNameHeight = fileNameEdit_->GetTextElement()->GetRowHeight() + clipBorder.top_ + clipBorder.bottom_;
  214. fileNameEdit_->SetFixedHeight(fileNameHeight);
  215. filterList_->SetFixedHeight(fileNameHeight);
  216. fileNameLayout_->SetFixedHeight(fileNameHeight);
  217. }
  218. buttonLayout_->SetFixedHeight(Max(okButton_->GetHeight(), cancelButton_->GetHeight()));
  219. }
  220. XMLFile* FileSelector::GetDefaultStyle() const
  221. {
  222. return window_->GetDefaultStyle(false);
  223. }
  224. const String& FileSelector::GetTitle() const
  225. {
  226. return titleText_->GetText();
  227. }
  228. const String& FileSelector::GetFileName() const
  229. {
  230. return fileNameEdit_->GetText();
  231. }
  232. const String& FileSelector::GetFilter() const
  233. {
  234. Text* selectedFilter = static_cast<Text*>(filterList_->GetSelectedItem());
  235. if (selectedFilter)
  236. return selectedFilter->GetText();
  237. else
  238. return String::EMPTY;
  239. }
  240. unsigned FileSelector::GetFilterIndex() const
  241. {
  242. return filterList_->GetSelection();
  243. }
  244. void FileSelector::SetLineEditText(LineEdit* edit, const String& text)
  245. {
  246. ignoreEvents_ = true;
  247. edit->SetText(text);
  248. ignoreEvents_ = false;
  249. }
  250. void FileSelector::RefreshFiles()
  251. {
  252. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  253. if (!fileSystem)
  254. return;
  255. ignoreEvents_ = true;
  256. fileList_->RemoveAllItems();
  257. fileEntries_.Clear();
  258. Vector<String> directories;
  259. Vector<String> files;
  260. fileSystem->ScanDir(directories, path_, "*", SCAN_DIRS, false);
  261. fileSystem->ScanDir(files, path_, GetFilter(), SCAN_FILES, false);
  262. fileEntries_.Reserve(directories.Size() + files.Size());
  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. fileList_->AddItem(entryText);
  291. entryText->SetText(displayName);
  292. entryText->SetStyle("FileSelectorListText");
  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::EMPTY);
  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. int key = eventData[P_KEY].GetInt();
  370. if (key == KEY_RETURN || key == KEY_RETURN2 || KEY_KP_ENTER)
  371. {
  372. bool entered = EnterFile();
  373. // When a key is used to enter a directory, select the first file if no selection
  374. if (entered && !fileList_->GetSelectedItem())
  375. fileList_->SetSelection(0);
  376. }
  377. }
  378. void FileSelector::HandleOKPressed(StringHash eventType, VariantMap& eventData)
  379. {
  380. if (ignoreEvents_)
  381. return;
  382. const String& fileName = GetFileName();
  383. if (!directoryMode_)
  384. {
  385. if (!fileName.Empty())
  386. {
  387. using namespace FileSelected;
  388. VariantMap newEventData;
  389. newEventData[P_FILENAME] = path_ + GetFileName();
  390. newEventData[P_OK] = true;
  391. SendEvent(E_FILESELECTED, newEventData);
  392. }
  393. }
  394. else if (eventType == E_RELEASED && !path_.Empty())
  395. {
  396. using namespace FileSelected;
  397. VariantMap newEventData;
  398. newEventData[P_FILENAME] = path_;
  399. newEventData[P_OK] = true;
  400. SendEvent(E_FILESELECTED, newEventData);
  401. }
  402. }
  403. void FileSelector::HandleCancelPressed(StringHash eventType, VariantMap& eventData)
  404. {
  405. if (ignoreEvents_)
  406. return;
  407. if (eventType == E_MODALCHANGED && eventData[ModalChanged::P_MODAL].GetBool())
  408. return;
  409. using namespace FileSelected;
  410. VariantMap newEventData;
  411. newEventData[P_FILENAME] = String::EMPTY;
  412. newEventData[P_OK] = false;
  413. SendEvent(E_FILESELECTED, newEventData);
  414. }
  415. }