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.

195 lines
5.5KB

  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. xml = XmlDocument::parse (BinaryData::colourscheme_dark_xml);
  94. appearance.readFromXML (*xml);
  95. appearance.updateColourScheme();
  96. loadSwatchColours();
  97. }
  98. Array<File> StoredSettings::getLastProjects()
  99. {
  100. StringArray s;
  101. s.addTokens (getGlobalProperties().getValue ("lastProjects"), "|", "");
  102. Array<File> f;
  103. for (int i = 0; i < s.size(); ++i)
  104. f.add (File (s[i]));
  105. return f;
  106. }
  107. void StoredSettings::setLastProjects (const Array<File>& files)
  108. {
  109. StringArray s;
  110. for (int i = 0; i < files.size(); ++i)
  111. s.add (files.getReference(i).getFullPathName());
  112. getGlobalProperties().setValue ("lastProjects", s.joinIntoString ("|"));
  113. }
  114. //==============================================================================
  115. void StoredSettings::loadSwatchColours()
  116. {
  117. swatchColours.clear();
  118. #define COL(col) Colours::col,
  119. const Colour colours[] =
  120. {
  121. #include "jucer_Colours.h"
  122. Colours::transparentBlack
  123. };
  124. #undef COL
  125. const int numSwatchColours = 24;
  126. PropertiesFile& props = getGlobalProperties();
  127. for (int i = 0; i < numSwatchColours; ++i)
  128. swatchColours.add (Colour::fromString (props.getValue ("swatchColour" + String (i),
  129. colours [2 + i].toString())));
  130. }
  131. void StoredSettings::saveSwatchColours()
  132. {
  133. PropertiesFile& props = getGlobalProperties();
  134. for (int i = 0; i < swatchColours.size(); ++i)
  135. props.setValue ("swatchColour" + String (i), swatchColours.getReference(i).toString());
  136. }
  137. int StoredSettings::ColourSelectorWithSwatches::getNumSwatches() const
  138. {
  139. return getAppSettings().swatchColours.size();
  140. }
  141. Colour StoredSettings::ColourSelectorWithSwatches::getSwatchColour (int index) const
  142. {
  143. return getAppSettings().swatchColours [index];
  144. }
  145. void StoredSettings::ColourSelectorWithSwatches::setSwatchColour (int index, const Colour& newColour) const
  146. {
  147. getAppSettings().swatchColours.set (index, newColour);
  148. }