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.

303 lines
8.7KB

  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_Headers.h"
  19. #include "jucer_Application.h"
  20. #include "jucer_MainWindow.h"
  21. #include "jucer_OpenDocumentManager.h"
  22. #include "../Code Editor/jucer_SourceCodeEditor.h"
  23. #include "../Project/jucer_NewProjectWizard.h"
  24. ScopedPointer<ApplicationCommandManager> commandManager;
  25. //==============================================================================
  26. MainWindow::MainWindow()
  27. : DocumentWindow (JucerApplication::getApp()->getApplicationName(),
  28. Colour::greyLevel (0.6f),
  29. DocumentWindow::allButtons,
  30. false)
  31. {
  32. setUsingNativeTitleBar (true);
  33. createProjectContentCompIfNeeded();
  34. #if ! JUCE_MAC
  35. setMenuBar (JucerApplication::getApp()->menuModel);
  36. #endif
  37. setResizable (true, false);
  38. centreWithSize (800, 600);
  39. // Register all the app commands..
  40. commandManager->registerAllCommandsForTarget (this);
  41. commandManager->registerAllCommandsForTarget (getProjectContentComponent());
  42. // update key mappings..
  43. {
  44. commandManager->getKeyMappings()->resetToDefaultMappings();
  45. ScopedPointer <XmlElement> keys (StoredSettings::getInstance()->getProps().getXmlValue ("keyMappings"));
  46. if (keys != nullptr)
  47. commandManager->getKeyMappings()->restoreFromXml (*keys);
  48. addKeyListener (commandManager->getKeyMappings());
  49. }
  50. // don't want the window to take focus when the title-bar is clicked..
  51. setWantsKeyboardFocus (false);
  52. //getPeer()->setCurrentRenderingEngine (0);
  53. getLookAndFeel().setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  54. }
  55. MainWindow::~MainWindow()
  56. {
  57. #if ! JUCE_MAC
  58. setMenuBar (nullptr);
  59. #endif
  60. removeKeyListener (commandManager->getKeyMappings());
  61. // save the current size and position to our settings file..
  62. StoredSettings::getInstance()->getProps()
  63. .setValue ("lastMainWindowPos", getWindowStateAsString());
  64. clearContentComponent();
  65. currentProject = nullptr;
  66. }
  67. void MainWindow::createProjectContentCompIfNeeded()
  68. {
  69. if (getProjectContentComponent() == nullptr)
  70. {
  71. clearContentComponent();
  72. setContentOwned (JucerApplication::getApp()->createProjectContentComponent(), false);
  73. }
  74. }
  75. void MainWindow::makeVisible()
  76. {
  77. setVisible (true);
  78. addToDesktop(); // (must add before restoring size so that fullscreen will work)
  79. restoreWindowPosition();
  80. getContentComponent()->grabKeyboardFocus();
  81. }
  82. ProjectContentComponent* MainWindow::getProjectContentComponent() const
  83. {
  84. return dynamic_cast <ProjectContentComponent*> (getContentComponent());
  85. }
  86. void MainWindow::closeButtonPressed()
  87. {
  88. if (! closeCurrentProject())
  89. return;
  90. JucerApplication::getApp()->closeWindow (this);
  91. }
  92. bool MainWindow::closeProject (Project* project)
  93. {
  94. jassert (project == currentProject && project != nullptr);
  95. if (project == nullptr)
  96. return true;
  97. StoredSettings::getInstance()->getProps()
  98. .setValue (getProjectWindowPosName(), getWindowStateAsString());
  99. if (! OpenDocumentManager::getInstance()->closeAllDocumentsUsingProject (*project, true))
  100. return false;
  101. ProjectContentComponent* const pcc = getProjectContentComponent();
  102. if (pcc != nullptr)
  103. pcc->saveTreeViewState();
  104. FileBasedDocument::SaveResult r = project->saveIfNeededAndUserAgrees();
  105. if (r == FileBasedDocument::savedOk)
  106. {
  107. setProject (nullptr);
  108. return true;
  109. }
  110. return false;
  111. }
  112. bool MainWindow::closeCurrentProject()
  113. {
  114. return currentProject == nullptr || closeProject (currentProject);
  115. }
  116. void MainWindow::setProject (Project* newProject)
  117. {
  118. createProjectContentCompIfNeeded();
  119. getProjectContentComponent()->setProject (newProject);
  120. currentProject = newProject;
  121. commandManager->commandStatusChanged();
  122. // (mustn't do this when the project is 0, because that'll happen on shutdown,
  123. // which will erase the list of recent projects)
  124. if (newProject != nullptr)
  125. JucerApplication::getApp()->updateRecentProjectList();
  126. }
  127. void MainWindow::restoreWindowPosition()
  128. {
  129. String windowState;
  130. if (currentProject != nullptr)
  131. windowState = StoredSettings::getInstance()->getProps().getValue (getProjectWindowPosName());
  132. if (windowState.isEmpty())
  133. windowState = StoredSettings::getInstance()->getProps().getValue ("lastMainWindowPos");
  134. restoreWindowStateFromString (windowState);
  135. }
  136. bool MainWindow::canOpenFile (const File& file) const
  137. {
  138. return file.hasFileExtension (Project::projectFileExtension)
  139. || OpenDocumentManager::getInstance()->canOpenFile (file);
  140. }
  141. bool MainWindow::openFile (const File& file)
  142. {
  143. createProjectContentCompIfNeeded();
  144. if (file.hasFileExtension (Project::projectFileExtension))
  145. {
  146. ScopedPointer <Project> newDoc (new Project (file));
  147. if (newDoc->loadFrom (file, true)
  148. && closeCurrentProject())
  149. {
  150. setProject (newDoc.release());
  151. return true;
  152. }
  153. }
  154. else if (file.exists())
  155. {
  156. return getProjectContentComponent()->showEditorForFile (file);
  157. }
  158. return false;
  159. }
  160. bool MainWindow::isInterestedInFileDrag (const StringArray& filenames)
  161. {
  162. for (int i = filenames.size(); --i >= 0;)
  163. if (canOpenFile (filenames[i]))
  164. return true;
  165. return false;
  166. }
  167. void MainWindow::filesDropped (const StringArray& filenames, int mouseX, int mouseY)
  168. {
  169. for (int i = filenames.size(); --i >= 0;)
  170. {
  171. const File f (filenames[i]);
  172. if (canOpenFile (f) && openFile (f))
  173. break;
  174. }
  175. }
  176. void MainWindow::activeWindowStatusChanged()
  177. {
  178. DocumentWindow::activeWindowStatusChanged();
  179. if (getProjectContentComponent() != nullptr)
  180. getProjectContentComponent()->updateMissingFileStatuses();
  181. OpenDocumentManager::getInstance()->reloadModifiedFiles();
  182. }
  183. void MainWindow::updateTitle (const String& documentName)
  184. {
  185. String name (JucerApplication::getApp()->getApplicationName());
  186. if (currentProject != nullptr)
  187. name = currentProject->getDocumentTitle() + " - " + name;
  188. if (documentName.isNotEmpty())
  189. name = documentName + " - " + name;
  190. setName (name);
  191. }
  192. void MainWindow::showNewProjectWizard()
  193. {
  194. jassert (currentProject == nullptr);
  195. setContentOwned (NewProjectWizard::createComponent(), true);
  196. makeVisible();
  197. }
  198. //==============================================================================
  199. ApplicationCommandTarget* MainWindow::getNextCommandTarget()
  200. {
  201. return 0;
  202. }
  203. void MainWindow::getAllCommands (Array <CommandID>& commands)
  204. {
  205. const CommandID ids[] = { CommandIDs::closeWindow };
  206. commands.addArray (ids, numElementsInArray (ids));
  207. }
  208. void MainWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  209. {
  210. switch (commandID)
  211. {
  212. case CommandIDs::closeWindow:
  213. result.setInfo ("Close Window", "Closes the current window", CommandCategories::general, 0);
  214. result.defaultKeypresses.add (KeyPress ('w', ModifierKeys::commandModifier, 0));
  215. break;
  216. default:
  217. break;
  218. }
  219. }
  220. bool MainWindow::perform (const InvocationInfo& info)
  221. {
  222. switch (info.commandID)
  223. {
  224. case CommandIDs::closeWindow:
  225. closeButtonPressed();
  226. break;
  227. default:
  228. return false;
  229. }
  230. return true;
  231. }