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.

288 lines
8.3KB

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