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.

227 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../Application/jucer_Headers.h"
  20. #include "../Application/jucer_Application.h"
  21. #include "jucer_AppearanceSettings.h"
  22. //==============================================================================
  23. AppearanceSettings::AppearanceSettings (bool updateAppWhenChanged)
  24. : settings ("COLOUR_SCHEME")
  25. {
  26. if (! ProjucerApplication::getApp().isRunningCommandLine)
  27. {
  28. ProjucerLookAndFeel lf;
  29. CodeDocument doc;
  30. CPlusPlusCodeTokeniser tokeniser;
  31. CodeEditorComponent editor (doc, &tokeniser);
  32. CodeEditorComponent::ColourScheme cs (editor.getColourScheme());
  33. for (int i = cs.types.size(); --i >= 0;)
  34. {
  35. auto& t = cs.types.getReference(i);
  36. getColourValue (t.name) = t.colour.toString();
  37. }
  38. getCodeFontValue() = getDefaultCodeFont().toString();
  39. if (updateAppWhenChanged)
  40. settings.addListener (this);
  41. }
  42. }
  43. File AppearanceSettings::getSchemesFolder()
  44. {
  45. File f (getGlobalProperties().getFile().getSiblingFile ("Schemes"));
  46. f.createDirectory();
  47. return f;
  48. }
  49. void AppearanceSettings::writeDefaultSchemeFile (const String& xmlString, const String& name)
  50. {
  51. auto file = getSchemesFolder().getChildFile (name).withFileExtension (getSchemeFileSuffix());
  52. AppearanceSettings settings (false);
  53. if (auto xml = parseXML (xmlString))
  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. auto newSchemes = getSchemesFolder().findChildFiles (File::findFiles, false, String ("*") + getSchemeFileSuffix());
  62. if (newSchemes != presetSchemeFiles)
  63. {
  64. presetSchemeFiles.swapWith (newSchemes);
  65. ProjucerApplication::getCommandManager().commandStatusChanged();
  66. }
  67. }
  68. StringArray AppearanceSettings::getPresetSchemes()
  69. {
  70. StringArray s;
  71. for (int i = 0; i < presetSchemeFiles.size(); ++i)
  72. s.add (presetSchemeFiles.getReference(i).getFileNameWithoutExtension());
  73. return s;
  74. }
  75. void AppearanceSettings::selectPresetScheme (int index)
  76. {
  77. readFromFile (presetSchemeFiles [index]);
  78. }
  79. bool AppearanceSettings::readFromXML (const XmlElement& xml)
  80. {
  81. if (xml.hasTagName (settings.getType().toString()))
  82. {
  83. const ValueTree newSettings (ValueTree::fromXml (xml));
  84. // we'll manually copy across the new properties to the existing tree so that
  85. // any open editors will be kept up to date..
  86. settings.copyPropertiesFrom (newSettings, nullptr);
  87. for (int i = settings.getNumChildren(); --i >= 0;)
  88. {
  89. ValueTree c (settings.getChild (i));
  90. const ValueTree newValue (newSettings.getChildWithProperty (Ids::name, c.getProperty (Ids::name)));
  91. if (newValue.isValid())
  92. c.copyPropertiesFrom (newValue, nullptr);
  93. }
  94. return true;
  95. }
  96. return false;
  97. }
  98. bool AppearanceSettings::readFromFile (const File& file)
  99. {
  100. if (auto xml = parseXML (file))
  101. return readFromXML (*xml);
  102. return false;
  103. }
  104. bool AppearanceSettings::writeToFile (const File& file) const
  105. {
  106. if (auto xml = settings.createXml())
  107. return xml->writeTo (file, {});
  108. return false;
  109. }
  110. Font AppearanceSettings::getDefaultCodeFont()
  111. {
  112. return Font (Font::getDefaultMonospacedFontName(), Font::getDefaultStyle(), 13.0f);
  113. }
  114. StringArray AppearanceSettings::getColourNames() const
  115. {
  116. StringArray s;
  117. for (auto c : settings)
  118. if (c.hasType ("COLOUR"))
  119. s.add (c[Ids::name]);
  120. return s;
  121. }
  122. void AppearanceSettings::updateColourScheme()
  123. {
  124. ProjucerApplication::getApp().mainWindowList.sendLookAndFeelChange();
  125. }
  126. void AppearanceSettings::applyToCodeEditor (CodeEditorComponent& editor) const
  127. {
  128. CodeEditorComponent::ColourScheme cs (editor.getColourScheme());
  129. for (int i = cs.types.size(); --i >= 0;)
  130. {
  131. CodeEditorComponent::ColourScheme::TokenType& t = cs.types.getReference(i);
  132. getColour (t.name, t.colour);
  133. }
  134. editor.setColourScheme (cs);
  135. editor.setFont (getCodeFont());
  136. editor.setColour (ScrollBar::thumbColourId, editor.findColour (CodeEditorComponent::backgroundColourId)
  137. .contrasting()
  138. .withAlpha (0.13f));
  139. }
  140. Font AppearanceSettings::getCodeFont() const
  141. {
  142. const String fontString (settings [Ids::font].toString());
  143. if (fontString.isEmpty())
  144. return getDefaultCodeFont();
  145. return Font::fromString (fontString);
  146. }
  147. Value AppearanceSettings::getCodeFontValue()
  148. {
  149. return settings.getPropertyAsValue (Ids::font, nullptr);
  150. }
  151. Value AppearanceSettings::getColourValue (const String& colourName)
  152. {
  153. ValueTree c (settings.getChildWithProperty (Ids::name, colourName));
  154. if (! c.isValid())
  155. {
  156. c = ValueTree ("COLOUR");
  157. c.setProperty (Ids::name, colourName, nullptr);
  158. settings.appendChild (c, nullptr);
  159. }
  160. return c.getPropertyAsValue (Ids::colour, nullptr);
  161. }
  162. bool AppearanceSettings::getColour (const String& name, Colour& result) const
  163. {
  164. const ValueTree colour (settings.getChildWithProperty (Ids::name, name));
  165. if (colour.isValid())
  166. {
  167. result = Colour::fromString (colour [Ids::colour].toString());
  168. return true;
  169. }
  170. return false;
  171. }