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.

229 lines
6.6KB

  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. const std::unique_ptr<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.appendChild (c, 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. }