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.

189 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. RecentlyOpenedFilesList::RecentlyOpenedFilesList()
  21. : maxNumberOfItems (10)
  22. {
  23. }
  24. RecentlyOpenedFilesList::~RecentlyOpenedFilesList()
  25. {
  26. }
  27. //==============================================================================
  28. void RecentlyOpenedFilesList::setMaxNumberOfItems (const int newMaxNumber)
  29. {
  30. maxNumberOfItems = jmax (1, newMaxNumber);
  31. files.removeRange (maxNumberOfItems, getNumFiles());
  32. }
  33. int RecentlyOpenedFilesList::getNumFiles() const
  34. {
  35. return files.size();
  36. }
  37. File RecentlyOpenedFilesList::getFile (const int index) const
  38. {
  39. return File (files [index]);
  40. }
  41. void RecentlyOpenedFilesList::clear()
  42. {
  43. files.clear();
  44. }
  45. void RecentlyOpenedFilesList::addFile (const File& file)
  46. {
  47. removeFile (file);
  48. files.insert (0, file.getFullPathName());
  49. setMaxNumberOfItems (maxNumberOfItems);
  50. }
  51. void RecentlyOpenedFilesList::removeFile (const File& file)
  52. {
  53. files.removeString (file.getFullPathName());
  54. }
  55. void RecentlyOpenedFilesList::removeNonExistentFiles()
  56. {
  57. for (int i = getNumFiles(); --i >= 0;)
  58. if (! getFile(i).exists())
  59. files.remove (i);
  60. }
  61. //==============================================================================
  62. int RecentlyOpenedFilesList::createPopupMenuItems (PopupMenu& menuToAddTo,
  63. const int baseItemId,
  64. const bool showFullPaths,
  65. const bool dontAddNonExistentFiles,
  66. const File** filesToAvoid)
  67. {
  68. int num = 0;
  69. for (int i = 0; i < getNumFiles(); ++i)
  70. {
  71. const File f (getFile(i));
  72. if ((! dontAddNonExistentFiles) || f.exists())
  73. {
  74. bool needsAvoiding = false;
  75. if (filesToAvoid != nullptr)
  76. {
  77. for (const File** avoid = filesToAvoid; *avoid != nullptr; ++avoid)
  78. {
  79. if (f == **avoid)
  80. {
  81. needsAvoiding = true;
  82. break;
  83. }
  84. }
  85. }
  86. if (! needsAvoiding)
  87. {
  88. menuToAddTo.addItem (baseItemId + i,
  89. showFullPaths ? f.getFullPathName()
  90. : f.getFileName());
  91. ++num;
  92. }
  93. }
  94. }
  95. return num;
  96. }
  97. //==============================================================================
  98. String RecentlyOpenedFilesList::toString() const
  99. {
  100. return files.joinIntoString ("\n");
  101. }
  102. void RecentlyOpenedFilesList::restoreFromString (const String& stringifiedVersion)
  103. {
  104. clear();
  105. files.addLines (stringifiedVersion);
  106. setMaxNumberOfItems (maxNumberOfItems);
  107. }
  108. //==============================================================================
  109. void RecentlyOpenedFilesList::registerRecentFileNatively (const File& file)
  110. {
  111. #if JUCE_MAC
  112. JUCE_AUTORELEASEPOOL
  113. {
  114. [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: createNSURLFromFile (file)];
  115. }
  116. #else
  117. ignoreUnused (file);
  118. #endif
  119. }
  120. void RecentlyOpenedFilesList::forgetRecentFileNatively (const File& file)
  121. {
  122. #if JUCE_MAC
  123. JUCE_AUTORELEASEPOOL
  124. {
  125. // for some reason, OSX doesn't provide a method to just remove a single file
  126. // from the recent list, so we clear them all and add them back excluding
  127. // the specified file
  128. auto sharedDocController = [NSDocumentController sharedDocumentController];
  129. auto recentDocumentURLs = [sharedDocController recentDocumentURLs];
  130. [sharedDocController clearRecentDocuments: nil];
  131. auto* nsFile = createNSURLFromFile (file);
  132. auto reverseEnumerator = [recentDocumentURLs reverseObjectEnumerator];
  133. for (NSURL* url : reverseEnumerator)
  134. if (! [url isEqual:nsFile])
  135. [sharedDocController noteNewRecentDocumentURL:url];
  136. }
  137. #else
  138. ignoreUnused (file);
  139. #endif
  140. }
  141. void RecentlyOpenedFilesList::clearRecentFilesNatively()
  142. {
  143. #if JUCE_MAC
  144. JUCE_AUTORELEASEPOOL
  145. {
  146. [[NSDocumentController sharedDocumentController] clearRecentDocuments: nil];
  147. }
  148. #endif
  149. }
  150. } // namespace juce