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.

153 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class SourceFileItem : public ProjectTreeItemBase
  18. {
  19. public:
  20. SourceFileItem (const Project::Item& projectItem)
  21. : ProjectTreeItemBase (projectItem)
  22. {
  23. }
  24. bool acceptsFileDrop (const StringArray&) const override { return false; }
  25. bool acceptsDragItems (const OwnedArray <Project::Item>&) override { return false; }
  26. String getDisplayName() const override
  27. {
  28. return getFile().getFileName();
  29. }
  30. static File findCorrespondingHeaderOrCpp (const File& f)
  31. {
  32. if (f.hasFileExtension (sourceFileExtensions)) return f.withFileExtension (".h");
  33. if (f.hasFileExtension (headerFileExtensions)) return f.withFileExtension (".cpp");
  34. return File();
  35. }
  36. void setName (const String& newName) override
  37. {
  38. if (newName != File::createLegalFileName (newName))
  39. {
  40. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  41. "That filename contained some illegal characters!");
  42. triggerAsyncRename (item);
  43. return;
  44. }
  45. File oldFile (getFile());
  46. File newFile (oldFile.getSiblingFile (newName));
  47. File correspondingFile (findCorrespondingHeaderOrCpp (oldFile));
  48. if (correspondingFile.exists() && newFile.hasFileExtension (oldFile.getFileExtension()))
  49. {
  50. Project::Item correspondingItem (item.project.getMainGroup().findItemForFile (correspondingFile));
  51. if (correspondingItem.isValid())
  52. {
  53. if (AlertWindow::showOkCancelBox (AlertWindow::NoIcon, "File Rename",
  54. "Do you also want to rename the corresponding file \"" + correspondingFile.getFileName()
  55. + "\" to match?"))
  56. {
  57. if (! item.renameFile (newFile))
  58. {
  59. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  60. "Failed to rename \"" + oldFile.getFullPathName() + "\"!\n\nCheck your file permissions!");
  61. return;
  62. }
  63. if (! correspondingItem.renameFile (newFile.withFileExtension (correspondingFile.getFileExtension())))
  64. {
  65. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  66. "Failed to rename \"" + correspondingFile.getFullPathName() + "\"!\n\nCheck your file permissions!");
  67. }
  68. }
  69. }
  70. }
  71. if (! item.renameFile (newFile))
  72. {
  73. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  74. "Failed to rename the file!\n\nCheck your file permissions!");
  75. }
  76. }
  77. ProjectTreeItemBase* createSubItem (const Project::Item&) override
  78. {
  79. jassertfalse;
  80. return nullptr;
  81. }
  82. void showDocument() override
  83. {
  84. const File f (getFile());
  85. if (f.exists())
  86. if (ProjectContentComponent* pcc = getProjectContentComponent())
  87. pcc->showEditorForFile (f, false);
  88. }
  89. void showPopupMenu() override
  90. {
  91. PopupMenu m;
  92. if (GroupItem* parentGroup = dynamic_cast<GroupItem*> (getParentProjectItem()))
  93. {
  94. parentGroup->addCreateFileMenuItems (m);
  95. m.addSeparator();
  96. }
  97. m.addItem (1, "Open in external editor");
  98. m.addItem (2,
  99. #if JUCE_MAC
  100. "Reveal in Finder");
  101. #else
  102. "Reveal in Explorer");
  103. #endif
  104. m.addItem (4, "Rename File...");
  105. m.addSeparator();
  106. m.addItem (3, "Delete");
  107. launchPopupMenu (m);
  108. }
  109. void handlePopupMenuResult (int resultCode) override
  110. {
  111. switch (resultCode)
  112. {
  113. case 1: getFile().startAsProcess(); break;
  114. case 2: revealInFinder(); break;
  115. case 3: deleteAllSelectedItems(); break;
  116. case 4: triggerAsyncRename (item); break;
  117. default:
  118. if (GroupItem* parentGroup = dynamic_cast<GroupItem*> (getParentProjectItem()))
  119. parentGroup->processCreateFileMenuItem (resultCode);
  120. break;
  121. }
  122. }
  123. };