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.7KB

  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 "../jucer_Headers.h"
  20. #include "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. const CodeEditorComponent::ColourScheme cs (editor.getColourScheme());
  33. for (int i = cs.types.size(); --i >= 0;)
  34. {
  35. CodeEditorComponent::ColourScheme::TokenType& 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. const File file (getSchemesFolder().getChildFile (name).withFileExtension (getSchemeFileSuffix()));
  52. AppearanceSettings settings (false);
  53. ScopedPointer<XmlElement> xml (XmlDocument::parse (xmlString));
  54. if (xml != nullptr)
  55. settings.readFromXML (*xml);
  56. settings.writeToFile (file);
  57. }
  58. void AppearanceSettings::refreshPresetSchemeList()
  59. {
  60. writeDefaultSchemeFile (BinaryData::colourscheme_dark_xml, "Default (Dark)");
  61. writeDefaultSchemeFile (BinaryData::colourscheme_light_xml, "Default (Light)");
  62. Array<File> newSchemes;
  63. getSchemesFolder().findChildFiles (newSchemes, File::findFiles, false, String ("*") + getSchemeFileSuffix());
  64. if (newSchemes != presetSchemeFiles)
  65. {
  66. presetSchemeFiles.swapWith (newSchemes);
  67. ProjucerApplication::getCommandManager().commandStatusChanged();
  68. }
  69. }
  70. StringArray AppearanceSettings::getPresetSchemes()
  71. {
  72. StringArray s;
  73. for (int i = 0; i < presetSchemeFiles.size(); ++i)
  74. s.add (presetSchemeFiles.getReference(i).getFileNameWithoutExtension());
  75. return s;
  76. }
  77. void AppearanceSettings::selectPresetScheme (int index)
  78. {
  79. readFromFile (presetSchemeFiles [index]);
  80. }
  81. bool AppearanceSettings::readFromXML (const XmlElement& xml)
  82. {
  83. if (xml.hasTagName (settings.getType().toString()))
  84. {
  85. const ValueTree newSettings (ValueTree::fromXml (xml));
  86. // we'll manually copy across the new properties to the existing tree so that
  87. // any open editors will be kept up to date..
  88. settings.copyPropertiesFrom (newSettings, nullptr);
  89. for (int i = settings.getNumChildren(); --i >= 0;)
  90. {
  91. ValueTree c (settings.getChild (i));
  92. const ValueTree newValue (newSettings.getChildWithProperty (Ids::name, c.getProperty (Ids::name)));
  93. if (newValue.isValid())
  94. c.copyPropertiesFrom (newValue, nullptr);
  95. }
  96. return true;
  97. }
  98. return false;
  99. }
  100. bool AppearanceSettings::readFromFile (const File& file)
  101. {
  102. const ScopedPointer<XmlElement> xml (XmlDocument::parse (file));
  103. return xml != nullptr && readFromXML (*xml);
  104. }
  105. bool AppearanceSettings::writeToFile (const File& file) const
  106. {
  107. const ScopedPointer<XmlElement> xml (settings.createXml());
  108. return xml != nullptr && xml->writeToFile (file, String());
  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 (int i = 0; i < settings.getNumChildren(); ++i)
  118. {
  119. const ValueTree c (settings.getChild(i));
  120. if (c.hasType ("COLOUR"))
  121. s.add (c [Ids::name]);
  122. }
  123. return s;
  124. }
  125. void AppearanceSettings::updateColourScheme()
  126. {
  127. ProjucerApplication::getApp().mainWindowList.sendLookAndFeelChange();
  128. }
  129. void AppearanceSettings::applyToCodeEditor (CodeEditorComponent& editor) const
  130. {
  131. CodeEditorComponent::ColourScheme cs (editor.getColourScheme());
  132. for (int i = cs.types.size(); --i >= 0;)
  133. {
  134. CodeEditorComponent::ColourScheme::TokenType& t = cs.types.getReference(i);
  135. getColour (t.name, t.colour);
  136. }
  137. editor.setColourScheme (cs);
  138. editor.setFont (getCodeFont());
  139. editor.setColour (ScrollBar::thumbColourId, editor.findColour (CodeEditorComponent::backgroundColourId)
  140. .contrasting()
  141. .withAlpha (0.13f));
  142. }
  143. Font AppearanceSettings::getCodeFont() const
  144. {
  145. const String fontString (settings [Ids::font].toString());
  146. if (fontString.isEmpty())
  147. return getDefaultCodeFont();
  148. return Font::fromString (fontString);
  149. }
  150. Value AppearanceSettings::getCodeFontValue()
  151. {
  152. return settings.getPropertyAsValue (Ids::font, nullptr);
  153. }
  154. Value AppearanceSettings::getColourValue (const String& colourName)
  155. {
  156. ValueTree c (settings.getChildWithProperty (Ids::name, colourName));
  157. if (! c.isValid())
  158. {
  159. c = ValueTree ("COLOUR");
  160. c.setProperty (Ids::name, colourName, nullptr);
  161. settings.addChild (c, -1, nullptr);
  162. }
  163. return c.getPropertyAsValue (Ids::colour, nullptr);
  164. }
  165. bool AppearanceSettings::getColour (const String& name, Colour& result) const
  166. {
  167. const ValueTree colour (settings.getChildWithProperty (Ids::name, name));
  168. if (colour.isValid())
  169. {
  170. result = Colour::fromString (colour [Ids::colour].toString());
  171. return true;
  172. }
  173. return false;
  174. }