FileSelector.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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 "EventListener.h"
  24. #include "SharedPtr.h"
  25. class Button;
  26. class DropDownList;
  27. class Font;
  28. class LineEdit;
  29. class ListView;
  30. class ResourceCache;
  31. class Text;
  32. class UI;
  33. class UIElement;
  34. class Window;
  35. class XMLFile;
  36. //! File selector entry
  37. struct FileSelectorEntry
  38. {
  39. //! Name
  40. std::string mName;
  41. //! Directory flag
  42. bool mDirectory;
  43. };
  44. //! File selector dialog
  45. class FileSelector : public RefCounted, public EventListener
  46. {
  47. public:
  48. //! Construct
  49. FileSelector(UI* ui);
  50. //! Destruct
  51. virtual ~FileSelector();
  52. //! Set fileselector UI style
  53. void setStyle(XMLFile* style);
  54. //! Set title text
  55. void setTitle(const std::string& text);
  56. //! Set button texts
  57. void setButtonTexts(const std::string& okText, const std::string& cancelText);
  58. //! Set current path
  59. void setPath(const std::string& path);
  60. //! Set current filename
  61. void setFileName(const std::string& fileName);
  62. //! Set filters
  63. void setFilters(const std::vector<std::string>& filters, unsigned defaultIndex);
  64. //! Update elements to layout properly. Call this after manually adjusting the sub-elements
  65. void updateElements();
  66. //! Return the UI style file
  67. XMLFile* getStyle() const { return mStyle; }
  68. //! Return fileselector window
  69. Window* getWindow() const { return mWindow; }
  70. //! Return window title text
  71. Text* getTitleText() const { return mTitleText; }
  72. //! Return file list
  73. ListView* getFileList() const { return mFileList; }
  74. //! Return path editor
  75. LineEdit* getPathEdit() const { return mPathEdit; }
  76. //! Return filename editor
  77. LineEdit* getFileNameEdit() const { return mFileNameEdit; }
  78. //! Return filter dropdown
  79. DropDownList* getFilterList() const { return mFilterList; }
  80. //! Return OK button
  81. Button* getOKButton() const { return mOKButton; }
  82. //! Return cancel button
  83. Button* getCancelButton() const { return mCancelButton; }
  84. //! Return current path
  85. const std::string& getPath() const { return mPath; }
  86. //! Return current filename
  87. const std::string& getFileName() const;
  88. //! Return current filter
  89. const std::string& getFilter() const;
  90. private:
  91. //! Refresh the directory listing
  92. void refreshFiles();
  93. //! Enter a directory or confirm a file. Return true if selection valid
  94. bool enterFile();
  95. //! Handle filter changed
  96. void handleFilterChanged(StringHash eventType, VariantMap& eventData);
  97. //! Handle path edited
  98. void handlePathChanged(StringHash eventType, VariantMap& eventData);
  99. //! Handle file selected from the list
  100. void handleFileSelected(StringHash eventType, VariantMap& eventData);
  101. //! Handle file doubleclicked from the list (enter directory / OK the file selection)
  102. void handleFileDoubleClicked(StringHash eventType, VariantMap& eventData);
  103. //! Handle file list key pressed
  104. void handleFileListKey(StringHash eventType, VariantMap& eventData);
  105. //! Handle OK button pressed
  106. void handleOKPressed(StringHash eventType, VariantMap& eventData);
  107. //! Handle cancel button pressed
  108. void handleCancelPressed(StringHash eventType, VariantMap& eventData);
  109. //! UI subsystem
  110. SharedPtr<UI> mUI;
  111. //! UI style file
  112. SharedPtr<XMLFile> mStyle;
  113. //! Fileselector window
  114. SharedPtr<Window> mWindow;
  115. //! Window title text
  116. SharedPtr<Text> mTitleText;
  117. //! File list
  118. SharedPtr<ListView> mFileList;
  119. //! Path editor
  120. SharedPtr<LineEdit> mPathEdit;
  121. //! Filename editor
  122. SharedPtr<LineEdit> mFileNameEdit;
  123. //! Filter dropdown
  124. SharedPtr<DropDownList> mFilterList;
  125. //! OK button
  126. SharedPtr<Button> mOKButton;
  127. //! OK button text
  128. SharedPtr<Text> mOKButtonText;
  129. //! Cancel button
  130. SharedPtr<Button> mCancelButton;
  131. //! Cancel button text
  132. SharedPtr<Text> mCancelButtonText;
  133. //! Filename and filter layout
  134. SharedPtr<UIElement> mFileNameLayout;
  135. //! Button layout
  136. SharedPtr<UIElement> mButtonLayout;
  137. //! Current path
  138. std::string mPath;
  139. //! Filters
  140. std::vector<std::string> mFilters;
  141. //! File entries
  142. std::vector<FileSelectorEntry> mFileEntries;
  143. //! Filter used to get the file list
  144. std::string mLastUsedFilter;
  145. //! Ignore event flag (used when changing the edit fields programmatically)
  146. bool mIgnoreEvents;
  147. };