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.

309 lines
8.8KB

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