The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
6.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. Manages a set of files for use as a list of recently-opened documents.
  18. This is a handy class for holding your list of recently-opened documents, with
  19. helpful methods for things like purging any non-existent files, automatically
  20. adding them to a menu, and making persistence easy.
  21. @see File, FileBasedDocument
  22. @tags{GUI}
  23. */
  24. class JUCE_API RecentlyOpenedFilesList
  25. {
  26. public:
  27. //==============================================================================
  28. /** Creates an empty list.
  29. */
  30. RecentlyOpenedFilesList();
  31. /** Destructor. */
  32. ~RecentlyOpenedFilesList();
  33. //==============================================================================
  34. /** Sets a limit for the number of files that will be stored in the list.
  35. When addFile() is called, then if there is no more space in the list, the
  36. least-recently added file will be dropped.
  37. @see getMaxNumberOfItems
  38. */
  39. void setMaxNumberOfItems (int newMaxNumber);
  40. /** Returns the number of items that this list will store.
  41. @see setMaxNumberOfItems
  42. */
  43. int getMaxNumberOfItems() const noexcept { return maxNumberOfItems; }
  44. /** Returns the number of files in the list.
  45. The most recently added file is always at index 0.
  46. */
  47. int getNumFiles() const;
  48. /** Returns one of the files in the list.
  49. The most recently added file is always at index 0.
  50. */
  51. File getFile (int index) const;
  52. /** Returns an array of all the absolute pathnames in the list.
  53. */
  54. const StringArray& getAllFilenames() const noexcept { return files; }
  55. /** Clears all the files from the list. */
  56. void clear();
  57. /** Adds a file to the list.
  58. The file will be added at index 0. If this file is already in the list, it will
  59. be moved up to index 0, but a file can only appear once in the list.
  60. If the list already contains the maximum number of items that is permitted, the
  61. least-recently added file will be dropped from the end.
  62. */
  63. void addFile (const File& file);
  64. /** Removes a file from the list. */
  65. void removeFile (const File& file);
  66. /** Checks each of the files in the list, removing any that don't exist.
  67. You might want to call this after reloading a list of files, or before putting them
  68. on a menu.
  69. */
  70. void removeNonExistentFiles();
  71. /** Tells the OS to add a file to the OS-managed list of recent documents for this app.
  72. Not all OSes maintain a list of recent files for an application, so this
  73. function will have no effect on some OSes. Currently it's just implemented for OSX.
  74. */
  75. static void registerRecentFileNatively (const File& file);
  76. /** Tells the OS to remove a file from the OS-managed list of recent documents for this app.
  77. Not all OSes maintain a list of recent files for an application, so this
  78. function will have no effect on some OSes. Currently it's just implemented for OSX.
  79. */
  80. static void forgetRecentFileNatively (const File& file);
  81. /** Tells the OS to clear the OS-managed list of recent documents for this app.
  82. Not all OSes maintain a list of recent files for an application, so this
  83. function will have no effect on some OSes. Currently it's just implemented for OSX.
  84. */
  85. static void clearRecentFilesNatively();
  86. //==============================================================================
  87. /** Adds entries to a menu, representing each of the files in the list.
  88. This is handy for creating an "open recent file..." menu in your app. The
  89. menu items are numbered consecutively starting with the baseItemId value,
  90. and can either be added as complete pathnames, or just the last part of the
  91. filename.
  92. If dontAddNonExistentFiles is true, then each file will be checked and only those
  93. that exist will be added.
  94. If filesToAvoid is not a nullptr, then it is considered to be a zero-terminated array
  95. of pointers to file objects. Any files that appear in this list will not be added to
  96. the menu - the reason for this is that you might have a number of files already open,
  97. so might not want these to be shown in the menu.
  98. It returns the number of items that were added.
  99. */
  100. int createPopupMenuItems (PopupMenu& menuToAddItemsTo,
  101. int baseItemId,
  102. bool showFullPaths,
  103. bool dontAddNonExistentFiles,
  104. const File** filesToAvoid = nullptr);
  105. //==============================================================================
  106. /** Returns a string that encapsulates all the files in the list.
  107. The string that is returned can later be passed into restoreFromString() in
  108. order to recreate the list. This is handy for persisting your list, e.g. in
  109. a PropertiesFile object.
  110. @see restoreFromString
  111. */
  112. String toString() const;
  113. /** Restores the list from a previously stringified version of the list.
  114. Pass in a stringified version created with toString() in order to persist/restore
  115. your list.
  116. @see toString
  117. */
  118. void restoreFromString (const String& stringifiedVersion);
  119. private:
  120. //==============================================================================
  121. StringArray files;
  122. int maxNumberOfItems;
  123. JUCE_LEAK_DETECTOR (RecentlyOpenedFilesList)
  124. };
  125. } // namespace juce