FileSelector.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Core/Object.h"
  5. namespace Urho3D
  6. {
  7. class Button;
  8. class DropDownList;
  9. class Font;
  10. class LineEdit;
  11. class ListView;
  12. class ResourceCache;
  13. class Text;
  14. class UIElement;
  15. class Window;
  16. class XMLFile;
  17. /// %File selector's list entry (file or directory).
  18. struct FileSelectorEntry
  19. {
  20. /// Name.
  21. String name_;
  22. /// Directory flag.
  23. bool directory_;
  24. };
  25. /// %File selector dialog.
  26. class URHO3D_API FileSelector : public Object
  27. {
  28. URHO3D_OBJECT(FileSelector, Object);
  29. public:
  30. /// Construct.
  31. explicit FileSelector(Context* context);
  32. /// Destruct.
  33. ~FileSelector() override;
  34. /// Register object factory.
  35. /// @nobind
  36. static void RegisterObject(Context* context);
  37. /// Set fileselector UI style.
  38. /// @property
  39. void SetDefaultStyle(XMLFile* style);
  40. /// Set title text.
  41. /// @property
  42. void SetTitle(const String& text);
  43. /// Set button texts.
  44. void SetButtonTexts(const String& okText, const String& cancelText);
  45. /// Set current path.
  46. /// @property
  47. void SetPath(const String& path);
  48. /// Set current filename.
  49. /// @property
  50. void SetFileName(const String& fileName);
  51. /// Set filters.
  52. void SetFilters(const Vector<String>& filters, unsigned defaultIndex);
  53. /// Set directory selection mode. Default false.
  54. /// @property
  55. void SetDirectoryMode(bool enable);
  56. /// Update elements to layout properly. Call this after manually adjusting the sub-elements.
  57. void UpdateElements();
  58. /// Return the UI style file.
  59. /// @property
  60. XMLFile* GetDefaultStyle() const;
  61. /// Return fileselector window.
  62. /// @property
  63. Window* GetWindow() const { return window_; }
  64. /// Return window title text element.
  65. /// @property
  66. Text* GetTitleText() const { return titleText_; }
  67. /// Return file list.
  68. /// @property
  69. ListView* GetFileList() const { return fileList_; }
  70. /// Return path editor.
  71. /// @property
  72. LineEdit* GetPathEdit() const { return pathEdit_; }
  73. /// Return filename editor.
  74. /// @property
  75. LineEdit* GetFileNameEdit() const { return fileNameEdit_; }
  76. /// Return filter dropdown.
  77. /// @property
  78. DropDownList* GetFilterList() const { return filterList_; }
  79. /// Return OK button.
  80. /// @property{get_okButton}
  81. Button* GetOKButton() const { return okButton_; }
  82. /// Return cancel button.
  83. /// @property
  84. Button* GetCancelButton() const { return cancelButton_; }
  85. /// Return close button.
  86. Button* GetCloseButton() const { return closeButton_; }
  87. /// Return window title.
  88. /// @property
  89. const String& GetTitle() const;
  90. /// Return current path.
  91. /// @property
  92. const String& GetPath() const { return path_; }
  93. /// Return current filename.
  94. /// @property
  95. const String& GetFileName() const;
  96. /// Return current filter.
  97. /// @property
  98. const String& GetFilter() const;
  99. /// Return current filter index.
  100. /// @property
  101. unsigned GetFilterIndex() const;
  102. /// Return directory mode flag.
  103. /// @property
  104. bool GetDirectoryMode() const { return directoryMode_; }
  105. private:
  106. /// Set the text of an edit field and ignore the resulting event.
  107. void SetLineEditText(LineEdit* edit, const String& text);
  108. /// Refresh the directory listing.
  109. void RefreshFiles();
  110. /// Enter a directory or confirm a file. Return true if a directory entered.
  111. bool EnterFile();
  112. /// Handle filter changed.
  113. void HandleFilterChanged(StringHash eventType, VariantMap& eventData);
  114. /// Handle path edited.
  115. void HandlePathChanged(StringHash eventType, VariantMap& eventData);
  116. /// Handle file selected from the list.
  117. void HandleFileSelected(StringHash eventType, VariantMap& eventData);
  118. /// Handle file doubleclicked from the list (enter directory / OK the file selection).
  119. void HandleFileDoubleClicked(StringHash eventType, VariantMap& eventData);
  120. /// Handle file list key pressed.
  121. void HandleFileListKey(StringHash eventType, VariantMap& eventData);
  122. /// Handle OK button pressed.
  123. void HandleOKPressed(StringHash eventType, VariantMap& eventData);
  124. /// Handle cancel button pressed.
  125. void HandleCancelPressed(StringHash eventType, VariantMap& eventData);
  126. /// Fileselector window.
  127. SharedPtr<Window> window_;
  128. /// Title layout.
  129. UIElement* titleLayout;
  130. /// Window title text.
  131. Text* titleText_;
  132. /// File list.
  133. ListView* fileList_;
  134. /// Path editor.
  135. LineEdit* pathEdit_;
  136. /// Filename editor.
  137. LineEdit* fileNameEdit_;
  138. /// Filter dropdown.
  139. DropDownList* filterList_;
  140. /// OK button.
  141. Button* okButton_;
  142. /// OK button text.
  143. Text* okButtonText_;
  144. /// Cancel button.
  145. Button* cancelButton_;
  146. /// Cancel button text.
  147. Text* cancelButtonText_;
  148. /// Close button.
  149. Button* closeButton_;
  150. /// Filename and filter layout.
  151. UIElement* fileNameLayout_;
  152. /// Separator layout.
  153. UIElement* separatorLayout_;
  154. /// Button layout.
  155. UIElement* buttonLayout_;
  156. /// Current directory.
  157. String path_;
  158. /// Filters.
  159. Vector<String> filters_;
  160. /// File entries.
  161. Vector<FileSelectorEntry> fileEntries_;
  162. /// Filter used to get the file list.
  163. String lastUsedFilter_;
  164. /// Directory mode flag.
  165. bool directoryMode_;
  166. /// Ignore events flag, used when changing line edits manually.
  167. bool ignoreEvents_;
  168. };
  169. }