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.

348 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "jucer_ProjectContentComponent.h"
  19. #include "../Application/jucer_MainWindow.h"
  20. #include "../Code Editor/jucer_SourceCodeEditor.h"
  21. #include "jucer_ProjectInformationComponent.h"
  22. #include "jucer_TreeViewTypes.h"
  23. #include "../Project Saving/jucer_ProjectExporter.h"
  24. //==============================================================================
  25. ProjectContentComponent::ProjectContentComponent()
  26. : project (nullptr),
  27. currentDocument (nullptr)
  28. {
  29. setOpaque (true);
  30. setWantsKeyboardFocus (true);
  31. treeSizeConstrainer.setMinimumWidth (100);
  32. treeSizeConstrainer.setMaximumWidth (500);
  33. }
  34. ProjectContentComponent::~ProjectContentComponent()
  35. {
  36. setProject (nullptr);
  37. contentView = nullptr;
  38. jassert (getNumChildComponents() == 0);
  39. }
  40. void ProjectContentComponent::paint (Graphics& g)
  41. {
  42. g.fillAll (Colour::greyLevel (0.8f));
  43. }
  44. void ProjectContentComponent::setProject (Project* newProject)
  45. {
  46. if (project != newProject)
  47. {
  48. if (project != nullptr)
  49. project->removeChangeListener (this);
  50. contentView = nullptr;
  51. resizerBar = nullptr;
  52. if (projectTree != nullptr)
  53. {
  54. StoredSettings::getInstance()->getProps().setValue ("projectTreeviewWidth", projectTree->getWidth());
  55. projectTree->deleteRootItem();
  56. projectTree = nullptr;
  57. }
  58. project = newProject;
  59. if (project != nullptr)
  60. {
  61. addAndMakeVisible (projectTree = new TreeView());
  62. projectTree->setComponentID ("tree");
  63. projectTree->setRootItemVisible (true);
  64. projectTree->setMultiSelectEnabled (true);
  65. projectTree->setDefaultOpenness (true);
  66. projectTree->setColour (TreeView::backgroundColourId, Colour::greyLevel (0.93f));
  67. projectTree->setIndentSize (14);
  68. projectTree->setRootItem (new GroupTreeViewItem (project->getMainGroup()));
  69. projectTree->getRootItem()->setOpen (true);
  70. String lastTreeWidth (StoredSettings::getInstance()->getProps().getValue ("projectTreeviewWidth"));
  71. if (lastTreeWidth.getIntValue() < 150)
  72. lastTreeWidth = "250";
  73. projectTree->setBounds ("0, 0, left + " + lastTreeWidth + ", parent.height");
  74. addAndMakeVisible (resizerBar = new ResizableEdgeComponent (projectTree, &treeSizeConstrainer,
  75. ResizableEdgeComponent::rightEdge));
  76. resizerBar->setComponentID ("resizer");
  77. resizerBar->setBounds ("tree.right, 0, tree.right + 4, parent.height");
  78. project->addChangeListener (this);
  79. if (currentDocument == nullptr)
  80. invokeDirectly (CommandIDs::showProjectSettings, true);
  81. updateMissingFileStatuses();
  82. }
  83. }
  84. }
  85. void ProjectContentComponent::changeListenerCallback (ChangeBroadcaster*)
  86. {
  87. updateMissingFileStatuses();
  88. }
  89. void ProjectContentComponent::updateMissingFileStatuses()
  90. {
  91. if (projectTree != nullptr)
  92. {
  93. ProjectTreeViewBase* p = dynamic_cast <ProjectTreeViewBase*> (projectTree->getRootItem());
  94. if (p != nullptr)
  95. p->checkFileStatus();
  96. }
  97. }
  98. bool ProjectContentComponent::showEditorForFile (const File& f)
  99. {
  100. return showDocument (OpenDocumentManager::getInstance()
  101. ->getDocumentForFile (project, f));
  102. }
  103. bool ProjectContentComponent::showDocument (OpenDocumentManager::Document* doc)
  104. {
  105. if (doc == nullptr)
  106. return false;
  107. OpenDocumentManager::getInstance()->moveDocumentToTopOfStack (doc);
  108. if (doc->hasFileBeenModifiedExternally())
  109. doc->reloadFromFile();
  110. return setEditorComponent (doc->createEditor(), doc);
  111. }
  112. void ProjectContentComponent::hideDocument (OpenDocumentManager::Document* doc)
  113. {
  114. if (doc == currentDocument)
  115. {
  116. currentDocument = nullptr;
  117. contentView = nullptr;
  118. updateMainWindowTitle();
  119. commandManager->commandStatusChanged();
  120. }
  121. }
  122. bool ProjectContentComponent::setEditorComponent (Component* editor, OpenDocumentManager::Document* doc)
  123. {
  124. if (editor != nullptr)
  125. {
  126. contentView = editor;
  127. currentDocument = doc;
  128. addAndMakeVisible (editor);
  129. editor->setBounds ("resizer.right, 0, parent.right, parent.height");
  130. updateMainWindowTitle();
  131. commandManager->commandStatusChanged();
  132. return true;
  133. }
  134. updateMainWindowTitle();
  135. return false;
  136. }
  137. void ProjectContentComponent::updateMainWindowTitle()
  138. {
  139. MainWindow* mw = Component::findParentComponentOfClass ((MainWindow*) 0);
  140. if (mw != nullptr)
  141. mw->updateTitle (currentDocument != nullptr ? currentDocument->getName() : String::empty);
  142. }
  143. ApplicationCommandTarget* ProjectContentComponent::getNextCommandTarget()
  144. {
  145. return findFirstTargetParentComponent();
  146. }
  147. void ProjectContentComponent::getAllCommands (Array <CommandID>& commands)
  148. {
  149. const CommandID ids[] = { CommandIDs::saveProject,
  150. CommandIDs::saveProjectAs,
  151. CommandIDs::closeProject,
  152. CommandIDs::openInIDE,
  153. CommandIDs::saveAndOpenInIDE,
  154. CommandIDs::showProjectSettings,
  155. StandardApplicationCommandIDs::del};
  156. commands.addArray (ids, numElementsInArray (ids));
  157. }
  158. void ProjectContentComponent::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  159. {
  160. switch (commandID)
  161. {
  162. case CommandIDs::saveProject:
  163. result.setInfo ("Save Project",
  164. "Saves the current project",
  165. CommandCategories::general, 0);
  166. result.setActive (project != nullptr);
  167. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier, 0));
  168. break;
  169. case CommandIDs::saveProjectAs:
  170. result.setInfo ("Save Project As...",
  171. "Saves the current project to a different filename",
  172. CommandCategories::general, 0);
  173. result.setActive (project != nullptr);
  174. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0));
  175. break;
  176. case CommandIDs::closeProject:
  177. result.setInfo ("Close Project",
  178. "Closes the current project",
  179. CommandCategories::general, 0);
  180. result.setActive (project != nullptr);
  181. break;
  182. case CommandIDs::openInIDE:
  183. #if JUCE_MAC
  184. result.setInfo ("Open in XCode...",
  185. #elif JUCE_WINDOWS
  186. result.setInfo ("Open in Visual Studio...",
  187. #else
  188. result.setInfo ("Open as a Makefile...",
  189. #endif
  190. "Launches the project in an external IDE",
  191. CommandCategories::general, 0);
  192. result.setActive (project != nullptr);
  193. break;
  194. case CommandIDs::saveAndOpenInIDE:
  195. #if JUCE_MAC
  196. result.setInfo ("Save Project and Open in XCode...",
  197. #elif JUCE_WINDOWS
  198. result.setInfo ("Save Project and Open in Visual Studio...",
  199. #else
  200. result.setInfo ("Save Project and Open as a Makefile...",
  201. #endif
  202. "Saves the project and launches it in an external IDE",
  203. CommandCategories::general, 0);
  204. result.setActive (project != nullptr);
  205. result.defaultKeypresses.add (KeyPress ('l', ModifierKeys::commandModifier, 0));
  206. break;
  207. case CommandIDs::showProjectSettings:
  208. result.setInfo ("Show Project Build Settings",
  209. "Shows the build options for the project",
  210. CommandCategories::general, 0);
  211. result.setActive (project != nullptr);
  212. result.defaultKeypresses.add (KeyPress ('i', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0));
  213. break;
  214. case StandardApplicationCommandIDs::del:
  215. result.setInfo ("Delete", String::empty, CommandCategories::general, 0);
  216. result.defaultKeypresses.add (KeyPress (KeyPress::deleteKey, 0, 0));
  217. result.defaultKeypresses.add (KeyPress (KeyPress::backspaceKey, 0, 0));
  218. result.setActive (projectTree != nullptr);
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. bool ProjectContentComponent::isCommandActive (const CommandID commandID)
  225. {
  226. return project != nullptr;
  227. }
  228. bool ProjectContentComponent::perform (const InvocationInfo& info)
  229. {
  230. switch (info.commandID)
  231. {
  232. case CommandIDs::saveProject:
  233. if (project != nullptr)
  234. project->save (true, true);
  235. break;
  236. case CommandIDs::saveProjectAs:
  237. if (project != nullptr)
  238. project->saveAsInteractive (true);
  239. break;
  240. case CommandIDs::closeProject:
  241. {
  242. MainWindow* mw = Component::findParentComponentOfClass ((MainWindow*) 0);
  243. if (mw != nullptr)
  244. mw->closeCurrentProject();
  245. }
  246. break;
  247. case CommandIDs::openInIDE:
  248. if (project != nullptr)
  249. {
  250. ScopedPointer <ProjectExporter> exporter (ProjectExporter::createPlatformDefaultExporter (*project));
  251. if (exporter != nullptr)
  252. exporter->launchProject();
  253. }
  254. break;
  255. case CommandIDs::saveAndOpenInIDE:
  256. if (project != nullptr && project->save (true, true) == FileBasedDocument::savedOk)
  257. {
  258. ScopedPointer <ProjectExporter> exporter (ProjectExporter::createPlatformDefaultExporter (*project));
  259. if (exporter != nullptr)
  260. exporter->launchProject();
  261. }
  262. break;
  263. case CommandIDs::showProjectSettings:
  264. if (projectTree != nullptr)
  265. projectTree->getRootItem()->setSelected (true, true);
  266. break;
  267. case StandardApplicationCommandIDs::del:
  268. if (projectTree != nullptr)
  269. {
  270. ProjectTreeViewBase* p = dynamic_cast <ProjectTreeViewBase*> (projectTree->getRootItem());
  271. if (p != nullptr)
  272. p->deleteAllSelectedItems();
  273. }
  274. break;
  275. default:
  276. return false;
  277. }
  278. return true;
  279. }