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.

283 lines
8.3KB

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