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.

182 lines
5.0KB

  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. RecentlyOpenedFilesList::RecentlyOpenedFilesList()
  16. : maxNumberOfItems (10)
  17. {
  18. }
  19. RecentlyOpenedFilesList::~RecentlyOpenedFilesList()
  20. {
  21. }
  22. //==============================================================================
  23. void RecentlyOpenedFilesList::setMaxNumberOfItems (const int newMaxNumber)
  24. {
  25. maxNumberOfItems = jmax (1, newMaxNumber);
  26. files.removeRange (maxNumberOfItems, getNumFiles());
  27. }
  28. int RecentlyOpenedFilesList::getNumFiles() const
  29. {
  30. return files.size();
  31. }
  32. File RecentlyOpenedFilesList::getFile (const int index) const
  33. {
  34. return File (files [index]);
  35. }
  36. void RecentlyOpenedFilesList::clear()
  37. {
  38. files.clear();
  39. }
  40. void RecentlyOpenedFilesList::addFile (const File& file)
  41. {
  42. removeFile (file);
  43. files.insert (0, file.getFullPathName());
  44. setMaxNumberOfItems (maxNumberOfItems);
  45. }
  46. void RecentlyOpenedFilesList::removeFile (const File& file)
  47. {
  48. files.removeString (file.getFullPathName());
  49. }
  50. void RecentlyOpenedFilesList::removeNonExistentFiles()
  51. {
  52. for (int i = getNumFiles(); --i >= 0;)
  53. if (! getFile(i).exists())
  54. files.remove (i);
  55. }
  56. //==============================================================================
  57. int RecentlyOpenedFilesList::createPopupMenuItems (PopupMenu& menuToAddTo,
  58. const int baseItemId,
  59. const bool showFullPaths,
  60. const bool dontAddNonExistentFiles,
  61. const File** filesToAvoid)
  62. {
  63. int num = 0;
  64. for (int i = 0; i < getNumFiles(); ++i)
  65. {
  66. const File f (getFile(i));
  67. if ((! dontAddNonExistentFiles) || f.exists())
  68. {
  69. bool needsAvoiding = false;
  70. if (filesToAvoid != nullptr)
  71. {
  72. for (const File** avoid = filesToAvoid; *avoid != nullptr; ++avoid)
  73. {
  74. if (f == **avoid)
  75. {
  76. needsAvoiding = true;
  77. break;
  78. }
  79. }
  80. }
  81. if (! needsAvoiding)
  82. {
  83. menuToAddTo.addItem (baseItemId + i,
  84. showFullPaths ? f.getFullPathName()
  85. : f.getFileName());
  86. ++num;
  87. }
  88. }
  89. }
  90. return num;
  91. }
  92. //==============================================================================
  93. String RecentlyOpenedFilesList::toString() const
  94. {
  95. return files.joinIntoString ("\n");
  96. }
  97. void RecentlyOpenedFilesList::restoreFromString (const String& stringifiedVersion)
  98. {
  99. clear();
  100. files.addLines (stringifiedVersion);
  101. setMaxNumberOfItems (maxNumberOfItems);
  102. }
  103. //==============================================================================
  104. void RecentlyOpenedFilesList::registerRecentFileNatively (const File& file)
  105. {
  106. #if JUCE_MAC
  107. JUCE_AUTORELEASEPOOL
  108. {
  109. [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: createNSURLFromFile (file)];
  110. }
  111. #else
  112. ignoreUnused (file);
  113. #endif
  114. }
  115. void RecentlyOpenedFilesList::forgetRecentFileNatively (const File& file)
  116. {
  117. #if JUCE_MAC
  118. JUCE_AUTORELEASEPOOL
  119. {
  120. // for some reason, OSX doesn't provide a method to just remove a single file
  121. // from the recent list, so we clear them all and add them back excluding
  122. // the specified file
  123. auto sharedDocController = [NSDocumentController sharedDocumentController];
  124. auto recentDocumentURLs = [sharedDocController recentDocumentURLs];
  125. [sharedDocController clearRecentDocuments: nil];
  126. auto* nsFile = createNSURLFromFile (file);
  127. auto reverseEnumerator = [recentDocumentURLs reverseObjectEnumerator];
  128. for (NSURL* url : reverseEnumerator)
  129. if (! [url isEqual:nsFile])
  130. [sharedDocController noteNewRecentDocumentURL:url];
  131. }
  132. #else
  133. ignoreUnused (file);
  134. #endif
  135. }
  136. void RecentlyOpenedFilesList::clearRecentFilesNatively()
  137. {
  138. #if JUCE_MAC
  139. JUCE_AUTORELEASEPOOL
  140. {
  141. [[NSDocumentController sharedDocumentController] clearRecentDocuments: nil];
  142. }
  143. #endif
  144. }
  145. } // namespace juce