FileSelector.h 6.3 KB

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