FileSelector.h 6.4 KB

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