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.

300 lines
8.6KB

  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. JucerApplication::getApp()->closeWindow (this);
  89. }
  90. bool MainWindow::closeProject (Project* project)
  91. {
  92. jassert (project == currentProject && project != nullptr);
  93. if (project == nullptr)
  94. return true;
  95. StoredSettings::getInstance()->getProps()
  96. .setValue (getProjectWindowPosName(), getWindowStateAsString());
  97. if (! OpenDocumentManager::getInstance()->closeAllDocumentsUsingProject (*project, true))
  98. return false;
  99. ProjectContentComponent* const pcc = getProjectContentComponent();
  100. if (pcc != nullptr)
  101. pcc->saveTreeViewState();
  102. FileBasedDocument::SaveResult r = project->saveIfNeededAndUserAgrees();
  103. if (r == FileBasedDocument::savedOk)
  104. {
  105. setProject (nullptr);
  106. return true;
  107. }
  108. return false;
  109. }
  110. bool MainWindow::closeCurrentProject()
  111. {
  112. return currentProject == nullptr || closeProject (currentProject);
  113. }
  114. void MainWindow::setProject (Project* newProject)
  115. {
  116. createProjectContentCompIfNeeded();
  117. getProjectContentComponent()->setProject (newProject);
  118. currentProject = newProject;
  119. commandManager->commandStatusChanged();
  120. // (mustn't do this when the project is 0, because that'll happen on shutdown,
  121. // which will erase the list of recent projects)
  122. if (newProject != nullptr)
  123. JucerApplication::getApp()->updateRecentProjectList();
  124. }
  125. void MainWindow::restoreWindowPosition()
  126. {
  127. String windowState;
  128. if (currentProject != nullptr)
  129. windowState = StoredSettings::getInstance()->getProps().getValue (getProjectWindowPosName());
  130. if (windowState.isEmpty())
  131. windowState = StoredSettings::getInstance()->getProps().getValue ("lastMainWindowPos");
  132. restoreWindowStateFromString (windowState);
  133. }
  134. bool MainWindow::canOpenFile (const File& file) const
  135. {
  136. return file.hasFileExtension (Project::projectFileExtension)
  137. || OpenDocumentManager::getInstance()->canOpenFile (file);
  138. }
  139. bool MainWindow::openFile (const File& file)
  140. {
  141. createProjectContentCompIfNeeded();
  142. if (file.hasFileExtension (Project::projectFileExtension))
  143. {
  144. ScopedPointer <Project> newDoc (new Project (file));
  145. if (newDoc->loadFrom (file, true)
  146. && closeCurrentProject())
  147. {
  148. setProject (newDoc.release());
  149. return true;
  150. }
  151. }
  152. else if (file.exists())
  153. {
  154. return getProjectContentComponent()->showEditorForFile (file);
  155. }
  156. return false;
  157. }
  158. bool MainWindow::isInterestedInFileDrag (const StringArray& filenames)
  159. {
  160. for (int i = filenames.size(); --i >= 0;)
  161. if (canOpenFile (filenames[i]))
  162. return true;
  163. return false;
  164. }
  165. void MainWindow::filesDropped (const StringArray& filenames, int mouseX, int mouseY)
  166. {
  167. for (int i = filenames.size(); --i >= 0;)
  168. {
  169. const File f (filenames[i]);
  170. if (canOpenFile (f) && openFile (f))
  171. break;
  172. }
  173. }
  174. void MainWindow::activeWindowStatusChanged()
  175. {
  176. DocumentWindow::activeWindowStatusChanged();
  177. if (getProjectContentComponent() != nullptr)
  178. getProjectContentComponent()->updateMissingFileStatuses();
  179. OpenDocumentManager::getInstance()->reloadModifiedFiles();
  180. }
  181. void MainWindow::updateTitle (const String& documentName)
  182. {
  183. String name (JucerApplication::getApp()->getApplicationName());
  184. if (currentProject != nullptr)
  185. name = currentProject->getDocumentTitle() + " - " + name;
  186. if (documentName.isNotEmpty())
  187. name = documentName + " - " + name;
  188. setName (name);
  189. }
  190. void MainWindow::showNewProjectWizard()
  191. {
  192. jassert (currentProject == nullptr);
  193. setContentOwned (NewProjectWizard::createComponent(), true);
  194. makeVisible();
  195. }
  196. //==============================================================================
  197. ApplicationCommandTarget* MainWindow::getNextCommandTarget()
  198. {
  199. return 0;
  200. }
  201. void MainWindow::getAllCommands (Array <CommandID>& commands)
  202. {
  203. const CommandID ids[] = { CommandIDs::closeWindow };
  204. commands.addArray (ids, numElementsInArray (ids));
  205. }
  206. void MainWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  207. {
  208. switch (commandID)
  209. {
  210. case CommandIDs::closeWindow:
  211. result.setInfo ("Close Window", "Closes the current window", CommandCategories::general, 0);
  212. result.defaultKeypresses.add (KeyPress ('w', ModifierKeys::commandModifier, 0));
  213. break;
  214. default:
  215. break;
  216. }
  217. }
  218. bool MainWindow::perform (const InvocationInfo& info)
  219. {
  220. switch (info.commandID)
  221. {
  222. case CommandIDs::closeWindow:
  223. closeButtonPressed();
  224. break;
  225. default:
  226. return false;
  227. }
  228. return true;
  229. }