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.

228 lines
6.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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_Application.h"
  19. #include "jucer_AppearanceSettings.h"
  20. #include "jucer_GlobalPreferences.h"
  21. //==============================================================================
  22. AppearanceSettings::AppearanceSettings (bool updateAppWhenChanged)
  23. : settings ("COLOUR_SCHEME")
  24. {
  25. if (! ProjucerApplication::getApp().isRunningCommandLine)
  26. {
  27. ProjucerLookAndFeel lf;
  28. CodeDocument doc;
  29. CPlusPlusCodeTokeniser tokeniser;
  30. CodeEditorComponent editor (doc, &tokeniser);
  31. const CodeEditorComponent::ColourScheme cs (editor.getColourScheme());
  32. for (int i = cs.types.size(); --i >= 0;)
  33. {
  34. CodeEditorComponent::ColourScheme::TokenType& t = cs.types.getReference(i);
  35. getColourValue (t.name) = t.colour.toString();
  36. }
  37. getCodeFontValue() = getDefaultCodeFont().toString();
  38. if (updateAppWhenChanged)
  39. settings.addListener (this);
  40. }
  41. }
  42. File AppearanceSettings::getSchemesFolder()
  43. {
  44. File f (getGlobalProperties().getFile().getSiblingFile ("Schemes"));
  45. f.createDirectory();
  46. return f;
  47. }
  48. void AppearanceSettings::writeDefaultSchemeFile (const String& xmlString, const String& name)
  49. {
  50. const File file (getSchemesFolder().getChildFile (name).withFileExtension (getSchemeFileSuffix()));
  51. AppearanceSettings settings (false);
  52. ScopedPointer<XmlElement> xml (XmlDocument::parse (xmlString));
  53. if (xml != nullptr)
  54. settings.readFromXML (*xml);
  55. settings.writeToFile (file);
  56. }
  57. void AppearanceSettings::refreshPresetSchemeList()
  58. {
  59. writeDefaultSchemeFile (BinaryData::colourscheme_dark_xml, "Default (Dark)");
  60. writeDefaultSchemeFile (BinaryData::colourscheme_light_xml, "Default (Light)");
  61. Array<File> newSchemes;
  62. getSchemesFolder().findChildFiles (newSchemes, File::findFiles, false, String ("*") + getSchemeFileSuffix());
  63. if (newSchemes != presetSchemeFiles)
  64. {
  65. presetSchemeFiles.swapWith (newSchemes);
  66. ProjucerApplication::getCommandManager().commandStatusChanged();
  67. }
  68. }
  69. StringArray AppearanceSettings::getPresetSchemes()
  70. {
  71. StringArray s;
  72. for (int i = 0; i < presetSchemeFiles.size(); ++i)
  73. s.add (presetSchemeFiles.getReference(i).getFileNameWithoutExtension());
  74. return s;
  75. }
  76. void AppearanceSettings::selectPresetScheme (int index)
  77. {
  78. readFromFile (presetSchemeFiles [index]);
  79. }
  80. bool AppearanceSettings::readFromXML (const XmlElement& xml)
  81. {
  82. if (xml.hasTagName (settings.getType().toString()))
  83. {
  84. const ValueTree newSettings (ValueTree::fromXml (xml));
  85. // we'll manually copy across the new properties to the existing tree so that
  86. // any open editors will be kept up to date..
  87. settings.copyPropertiesFrom (newSettings, nullptr);
  88. for (int i = settings.getNumChildren(); --i >= 0;)
  89. {
  90. ValueTree c (settings.getChild (i));
  91. const ValueTree newValue (newSettings.getChildWithProperty (Ids::name, c.getProperty (Ids::name)));
  92. if (newValue.isValid())
  93. c.copyPropertiesFrom (newValue, nullptr);
  94. }
  95. return true;
  96. }
  97. return false;
  98. }
  99. bool AppearanceSettings::readFromFile (const File& file)
  100. {
  101. const ScopedPointer<XmlElement> xml (XmlDocument::parse (file));
  102. return xml != nullptr && readFromXML (*xml);
  103. }
  104. bool AppearanceSettings::writeToFile (const File& file) const
  105. {
  106. const ScopedPointer<XmlElement> xml (settings.createXml());
  107. return xml != nullptr && xml->writeToFile (file, String());
  108. }
  109. Font AppearanceSettings::getDefaultCodeFont()
  110. {
  111. return Font (Font::getDefaultMonospacedFontName(), Font::getDefaultStyle(), 13.0f);
  112. }
  113. StringArray AppearanceSettings::getColourNames() const
  114. {
  115. StringArray s;
  116. for (int i = 0; i < settings.getNumChildren(); ++i)
  117. {
  118. const ValueTree c (settings.getChild(i));
  119. if (c.hasType ("COLOUR"))
  120. s.add (c [Ids::name]);
  121. }
  122. return s;
  123. }
  124. void AppearanceSettings::updateColourScheme()
  125. {
  126. ProjucerApplication::getApp().mainWindowList.sendLookAndFeelChange();
  127. }
  128. void AppearanceSettings::applyToCodeEditor (CodeEditorComponent& editor) const
  129. {
  130. CodeEditorComponent::ColourScheme cs (editor.getColourScheme());
  131. for (int i = cs.types.size(); --i >= 0;)
  132. {
  133. CodeEditorComponent::ColourScheme::TokenType& t = cs.types.getReference(i);
  134. getColour (t.name, t.colour);
  135. }
  136. editor.setColourScheme (cs);
  137. editor.setFont (getCodeFont());
  138. editor.setColour (ScrollBar::thumbColourId, editor.findColour (CodeEditorComponent::backgroundColourId)
  139. .contrasting()
  140. .withAlpha (0.13f));
  141. }
  142. Font AppearanceSettings::getCodeFont() const
  143. {
  144. const String fontString (settings [Ids::font].toString());
  145. if (fontString.isEmpty())
  146. return getDefaultCodeFont();
  147. return Font::fromString (fontString);
  148. }
  149. Value AppearanceSettings::getCodeFontValue()
  150. {
  151. return settings.getPropertyAsValue (Ids::font, nullptr);
  152. }
  153. Value AppearanceSettings::getColourValue (const String& colourName)
  154. {
  155. ValueTree c (settings.getChildWithProperty (Ids::name, colourName));
  156. if (! c.isValid())
  157. {
  158. c = ValueTree ("COLOUR");
  159. c.setProperty (Ids::name, colourName, nullptr);
  160. settings.addChild (c, -1, nullptr);
  161. }
  162. return c.getPropertyAsValue (Ids::colour, nullptr);
  163. }
  164. bool AppearanceSettings::getColour (const String& name, Colour& result) const
  165. {
  166. const ValueTree colour (settings.getChildWithProperty (Ids::name, name));
  167. if (colour.isValid())
  168. {
  169. result = Colour::fromString (colour [Ids::colour].toString());
  170. return true;
  171. }
  172. return false;
  173. }