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.

199 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_StoredSettings.h"
  19. #include "../Application/jucer_Application.h"
  20. //==============================================================================
  21. StoredSettings& getAppSettings()
  22. {
  23. return *IntrojucerApp::getApp().settings;
  24. }
  25. PropertiesFile& getGlobalProperties()
  26. {
  27. return getAppSettings().getGlobalProperties();
  28. }
  29. //==============================================================================
  30. StoredSettings::StoredSettings()
  31. : appearance (true)
  32. {
  33. reload();
  34. }
  35. StoredSettings::~StoredSettings()
  36. {
  37. flush();
  38. }
  39. PropertiesFile& StoredSettings::getGlobalProperties()
  40. {
  41. return *propertyFiles.getUnchecked (0);
  42. }
  43. static PropertiesFile* createPropsFile (const String& filename)
  44. {
  45. return new PropertiesFile (IntrojucerApp::getApp()
  46. .getPropertyFileOptionsFor (filename));
  47. }
  48. PropertiesFile& StoredSettings::getProjectProperties (const String& projectUID)
  49. {
  50. const String filename ("Introjucer_Project_" + projectUID);
  51. for (int i = propertyFiles.size(); --i >= 0;)
  52. {
  53. PropertiesFile* const props = propertyFiles.getUnchecked(i);
  54. if (props->getFile().getFileNameWithoutExtension() == filename)
  55. return *props;
  56. }
  57. PropertiesFile* p = createPropsFile (filename);
  58. propertyFiles.add (p);
  59. return *p;
  60. }
  61. void StoredSettings::updateGlobalProps()
  62. {
  63. PropertiesFile& props = getGlobalProperties();
  64. {
  65. const ScopedPointer<XmlElement> xml (appearance.settings.createXml());
  66. props.setValue ("editorColours", xml);
  67. }
  68. props.setValue ("recentFiles", recentFiles.toString());
  69. props.removeValue ("keyMappings");
  70. if (ApplicationCommandManager* commandManager = IntrojucerApp::getApp().commandManager)
  71. {
  72. const ScopedPointer <XmlElement> keys (commandManager->getKeyMappings()->createXml (true));
  73. if (keys != nullptr)
  74. props.setValue ("keyMappings", keys);
  75. }
  76. }
  77. void StoredSettings::flush()
  78. {
  79. updateGlobalProps();
  80. saveSwatchColours();
  81. for (int i = propertyFiles.size(); --i >= 0;)
  82. propertyFiles.getUnchecked(i)->saveIfNeeded();
  83. }
  84. void StoredSettings::reload()
  85. {
  86. propertyFiles.clear();
  87. propertyFiles.add (createPropsFile ("Introjucer"));
  88. // recent files...
  89. recentFiles.restoreFromString (getGlobalProperties().getValue ("recentFiles"));
  90. recentFiles.removeNonExistentFiles();
  91. ScopedPointer<XmlElement> xml (getGlobalProperties().getXmlValue ("editorColours"));
  92. if (xml == nullptr)
  93. {
  94. xml = XmlDocument::parse (BinaryData::colourscheme_dark_xml);
  95. jassert (xml != nullptr);
  96. }
  97. appearance.readFromXML (*xml);
  98. appearance.updateColourScheme();
  99. loadSwatchColours();
  100. }
  101. Array<File> StoredSettings::getLastProjects()
  102. {
  103. StringArray s;
  104. s.addTokens (getGlobalProperties().getValue ("lastProjects"), "|", "");
  105. Array<File> f;
  106. for (int i = 0; i < s.size(); ++i)
  107. f.add (File (s[i]));
  108. return f;
  109. }
  110. void StoredSettings::setLastProjects (const Array<File>& files)
  111. {
  112. StringArray s;
  113. for (int i = 0; i < files.size(); ++i)
  114. s.add (files.getReference(i).getFullPathName());
  115. getGlobalProperties().setValue ("lastProjects", s.joinIntoString ("|"));
  116. }
  117. //==============================================================================
  118. void StoredSettings::loadSwatchColours()
  119. {
  120. swatchColours.clear();
  121. #define COL(col) Colours::col,
  122. const Colour colours[] =
  123. {
  124. #include "jucer_Colours.h"
  125. Colours::transparentBlack
  126. };
  127. #undef COL
  128. const int numSwatchColours = 24;
  129. PropertiesFile& props = getGlobalProperties();
  130. for (int i = 0; i < numSwatchColours; ++i)
  131. swatchColours.add (Colour::fromString (props.getValue ("swatchColour" + String (i),
  132. colours [2 + i].toString())));
  133. }
  134. void StoredSettings::saveSwatchColours()
  135. {
  136. PropertiesFile& props = getGlobalProperties();
  137. for (int i = 0; i < swatchColours.size(); ++i)
  138. props.setValue ("swatchColour" + String (i), swatchColours.getReference(i).toString());
  139. }
  140. int StoredSettings::ColourSelectorWithSwatches::getNumSwatches() const
  141. {
  142. return getAppSettings().swatchColours.size();
  143. }
  144. Colour StoredSettings::ColourSelectorWithSwatches::getSwatchColour (int index) const
  145. {
  146. return getAppSettings().swatchColours [index];
  147. }
  148. void StoredSettings::ColourSelectorWithSwatches::setSwatchColour (int index, const Colour& newColour) const
  149. {
  150. getAppSettings().swatchColours.set (index, newColour);
  151. }