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.

205 lines
5.9KB

  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_StoredSettings.h"
  20. #include "../Application/jucer_Application.h"
  21. //==============================================================================
  22. StoredSettings& getAppSettings()
  23. {
  24. return *IntrojucerApp::getApp().settings;
  25. }
  26. PropertiesFile& getGlobalProperties()
  27. {
  28. return getAppSettings().getGlobalProperties();
  29. }
  30. //==============================================================================
  31. StoredSettings::StoredSettings()
  32. : appearance (true)
  33. {
  34. reload();
  35. }
  36. StoredSettings::~StoredSettings()
  37. {
  38. flush();
  39. }
  40. PropertiesFile& StoredSettings::getGlobalProperties()
  41. {
  42. return *propertyFiles.getUnchecked (0);
  43. }
  44. static PropertiesFile* createPropsFile (const String& filename)
  45. {
  46. PropertiesFile::Options options;
  47. options.applicationName = filename;
  48. options.filenameSuffix = "settings";
  49. options.osxLibrarySubFolder = "Application Support";
  50. #if JUCE_LINUX
  51. options.folderName = ".introjucer";
  52. #else
  53. options.folderName = "Introjucer";
  54. #endif
  55. return new PropertiesFile (options);
  56. }
  57. PropertiesFile& StoredSettings::getProjectProperties (const String& projectUID)
  58. {
  59. const String filename ("Introjucer_Project_" + projectUID);
  60. for (int i = propertyFiles.size(); --i >= 0;)
  61. {
  62. PropertiesFile* const props = propertyFiles.getUnchecked(i);
  63. if (props->getFile().getFileNameWithoutExtension() == filename)
  64. return *props;
  65. }
  66. PropertiesFile* p = createPropsFile (filename);
  67. propertyFiles.add (p);
  68. return *p;
  69. }
  70. void StoredSettings::updateGlobalProps()
  71. {
  72. PropertiesFile& props = getGlobalProperties();
  73. {
  74. const ScopedPointer<XmlElement> xml (appearance.settings.createXml());
  75. props.setValue ("editorColours", xml);
  76. }
  77. props.setValue ("recentFiles", recentFiles.toString());
  78. props.removeValue ("keyMappings");
  79. if (commandManager != nullptr)
  80. {
  81. const ScopedPointer <XmlElement> keys (commandManager->getKeyMappings()->createXml (true));
  82. if (keys != nullptr)
  83. props.setValue ("keyMappings", keys);
  84. }
  85. }
  86. void StoredSettings::flush()
  87. {
  88. updateGlobalProps();
  89. saveSwatchColours();
  90. for (int i = propertyFiles.size(); --i >= 0;)
  91. propertyFiles.getUnchecked(i)->saveIfNeeded();
  92. }
  93. void StoredSettings::reload()
  94. {
  95. propertyFiles.clear();
  96. propertyFiles.add (createPropsFile ("Introjucer"));
  97. // recent files...
  98. recentFiles.restoreFromString (getGlobalProperties().getValue ("recentFiles"));
  99. recentFiles.removeNonExistentFiles();
  100. ScopedPointer<XmlElement> xml (getGlobalProperties().getXmlValue ("editorColours"));
  101. if (xml == nullptr)
  102. xml = XmlDocument::parse (BinaryData::colourscheme_dark_xml);
  103. appearance.readFromXML (*xml);
  104. appearance.updateColourScheme();
  105. loadSwatchColours();
  106. }
  107. Array<File> StoredSettings::getLastProjects()
  108. {
  109. StringArray s;
  110. s.addTokens (getGlobalProperties().getValue ("lastProjects"), "|", "");
  111. Array<File> f;
  112. for (int i = 0; i < s.size(); ++i)
  113. f.add (File (s[i]));
  114. return f;
  115. }
  116. void StoredSettings::setLastProjects (const Array<File>& files)
  117. {
  118. StringArray s;
  119. for (int i = 0; i < files.size(); ++i)
  120. s.add (files.getReference(i).getFullPathName());
  121. getGlobalProperties().setValue ("lastProjects", s.joinIntoString ("|"));
  122. }
  123. //==============================================================================
  124. void StoredSettings::loadSwatchColours()
  125. {
  126. swatchColours.clear();
  127. #define COL(col) Colours::col,
  128. const Colour colours[] =
  129. {
  130. #include "jucer_Colours.h"
  131. Colours::transparentBlack
  132. };
  133. #undef COL
  134. const int numSwatchColours = 24;
  135. PropertiesFile& props = getGlobalProperties();
  136. for (int i = 0; i < numSwatchColours; ++i)
  137. swatchColours.add (Colour::fromString (props.getValue ("swatchColour" + String (i),
  138. colours [2 + i].toString())));
  139. }
  140. void StoredSettings::saveSwatchColours()
  141. {
  142. PropertiesFile& props = getGlobalProperties();
  143. for (int i = 0; i < swatchColours.size(); ++i)
  144. props.setValue ("swatchColour" + String (i), swatchColours.getReference(i).toString());
  145. }
  146. int StoredSettings::ColourSelectorWithSwatches::getNumSwatches() const
  147. {
  148. return getAppSettings().swatchColours.size();
  149. }
  150. Colour StoredSettings::ColourSelectorWithSwatches::getSwatchColour (int index) const
  151. {
  152. return getAppSettings().swatchColours [index];
  153. }
  154. void StoredSettings::ColourSelectorWithSwatches::setSwatchColour (int index, const Colour& newColour) const
  155. {
  156. getAppSettings().swatchColours.set (index, newColour);
  157. }