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.

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