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.

155 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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))
  33. return f.withFileExtension (".h");
  34. else if (f.hasFileExtension (headerFileExtensions))
  35. return f.withFileExtension (".cpp");
  36. return File::nonexistent;
  37. }
  38. void setName (const String& newName) override
  39. {
  40. if (newName != File::createLegalFileName (newName))
  41. {
  42. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  43. "That filename contained some illegal characters!");
  44. triggerAsyncRename (item);
  45. return;
  46. }
  47. File oldFile (getFile());
  48. File newFile (oldFile.getSiblingFile (newName));
  49. File correspondingFile (findCorrespondingHeaderOrCpp (oldFile));
  50. if (correspondingFile.exists() && newFile.hasFileExtension (oldFile.getFileExtension()))
  51. {
  52. Project::Item correspondingItem (item.project.getMainGroup().findItemForFile (correspondingFile));
  53. if (correspondingItem.isValid())
  54. {
  55. if (AlertWindow::showOkCancelBox (AlertWindow::NoIcon, "File Rename",
  56. "Do you also want to rename the corresponding file \"" + correspondingFile.getFileName()
  57. + "\" to match?"))
  58. {
  59. if (! item.renameFile (newFile))
  60. {
  61. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  62. "Failed to rename \"" + oldFile.getFullPathName() + "\"!\n\nCheck your file permissions!");
  63. return;
  64. }
  65. if (! correspondingItem.renameFile (newFile.withFileExtension (correspondingFile.getFileExtension())))
  66. {
  67. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  68. "Failed to rename \"" + correspondingFile.getFullPathName() + "\"!\n\nCheck your file permissions!");
  69. }
  70. }
  71. }
  72. }
  73. if (! item.renameFile (newFile))
  74. {
  75. AlertWindow::showMessageBox (AlertWindow::WarningIcon, "File Rename",
  76. "Failed to rename the file!\n\nCheck your file permissions!");
  77. }
  78. }
  79. ProjectTreeItemBase* createSubItem (const Project::Item&) override
  80. {
  81. jassertfalse;
  82. return nullptr;
  83. }
  84. void showDocument() override
  85. {
  86. const File f (getFile());
  87. if (f.exists())
  88. if (ProjectContentComponent* pcc = getProjectContentComponent())
  89. pcc->showEditorForFile (f, false);
  90. }
  91. void showPopupMenu() override
  92. {
  93. PopupMenu m;
  94. if (GroupItem* parentGroup = dynamic_cast<GroupItem*> (getParentProjectItem()))
  95. {
  96. parentGroup->addCreateFileMenuItems (m);
  97. m.addSeparator();
  98. }
  99. m.addItem (1, "Open in external editor");
  100. m.addItem (2,
  101. #if JUCE_MAC
  102. "Reveal in Finder");
  103. #else
  104. "Reveal in Explorer");
  105. #endif
  106. m.addItem (4, "Rename File...");
  107. m.addSeparator();
  108. m.addItem (3, "Delete");
  109. launchPopupMenu (m);
  110. }
  111. void handlePopupMenuResult (int resultCode) override
  112. {
  113. switch (resultCode)
  114. {
  115. case 1: getFile().startAsProcess(); break;
  116. case 2: revealInFinder(); break;
  117. case 3: deleteAllSelectedItems(); break;
  118. case 4: triggerAsyncRename (item); break;
  119. default:
  120. if (GroupItem* parentGroup = dynamic_cast<GroupItem*> (getParentProjectItem()))
  121. parentGroup->processCreateFileMenuItem (resultCode);
  122. break;
  123. }
  124. }
  125. };