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.

338 lines
12KB

  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. #pragma once
  20. #include "../../Utility/UI/PropertyComponents/jucer_ColourPropertyComponent.h"
  21. //==============================================================================
  22. class EditorColourSchemeWindowComponent : public Component
  23. {
  24. public:
  25. EditorColourSchemeWindowComponent()
  26. {
  27. if (getAppSettings().monospacedFontNames.size() == 0)
  28. content.reset (new AppearanceEditor::FontScanPanel());
  29. else
  30. content.reset (new AppearanceEditor::EditorPanel());
  31. changeContent (content.get());
  32. }
  33. void paint (Graphics& g) override
  34. {
  35. g.fillAll (findColour (backgroundColourId));
  36. }
  37. void resized() override
  38. {
  39. content->setBounds (getLocalBounds());
  40. }
  41. void changeContent (Component* newContent)
  42. {
  43. content.reset (newContent);
  44. addAndMakeVisible (newContent);
  45. content->setBounds (getLocalBounds().reduced (10));
  46. }
  47. private:
  48. std::unique_ptr<Component> content;
  49. //==============================================================================
  50. struct AppearanceEditor
  51. {
  52. struct FontScanPanel : public Component,
  53. private Timer
  54. {
  55. FontScanPanel()
  56. {
  57. fontsToScan = Font::findAllTypefaceNames();
  58. startTimer (1);
  59. }
  60. void paint (Graphics& g) override
  61. {
  62. g.fillAll (findColour (backgroundColourId));
  63. g.setFont (14.0f);
  64. g.setColour (findColour (defaultTextColourId));
  65. g.drawFittedText ("Scanning for fonts..", getLocalBounds(), Justification::centred, 2);
  66. const auto size = 30;
  67. getLookAndFeel().drawSpinningWaitAnimation (g, Colours::white, (getWidth() - size) / 2, getHeight() / 2 - 50, size, size);
  68. }
  69. void timerCallback() override
  70. {
  71. repaint();
  72. if (fontsToScan.size() == 0)
  73. {
  74. getAppSettings().monospacedFontNames = fontsFound;
  75. if (auto* owner = findParentComponentOfClass<EditorColourSchemeWindowComponent>())
  76. owner->changeContent (new EditorPanel());
  77. }
  78. else
  79. {
  80. if (isMonospacedTypeface (fontsToScan[0]))
  81. fontsFound.add (fontsToScan[0]);
  82. fontsToScan.remove (0);
  83. }
  84. }
  85. // A rather hacky trick to select only the fixed-pitch fonts..
  86. // This is unfortunately a bit slow, but will work on all platforms.
  87. static bool isMonospacedTypeface (const String& name)
  88. {
  89. const Font font (name, 20.0f, Font::plain);
  90. const auto width = font.getStringWidth ("....");
  91. return width == font.getStringWidth ("WWWW")
  92. && width == font.getStringWidth ("0000")
  93. && width == font.getStringWidth ("1111")
  94. && width == font.getStringWidth ("iiii");
  95. }
  96. StringArray fontsToScan, fontsFound;
  97. };
  98. //==============================================================================
  99. struct EditorPanel : public Component
  100. {
  101. EditorPanel()
  102. : loadButton ("Load Scheme..."),
  103. saveButton ("Save Scheme...")
  104. {
  105. rebuildProperties();
  106. addAndMakeVisible (panel);
  107. addAndMakeVisible (loadButton);
  108. addAndMakeVisible (saveButton);
  109. loadButton.onClick = [this] { loadScheme(); };
  110. saveButton.onClick = [this] { saveScheme (false); };
  111. lookAndFeelChanged();
  112. saveSchemeState();
  113. }
  114. ~EditorPanel()
  115. {
  116. if (hasSchemeBeenModifiedSinceSave())
  117. saveScheme (true);
  118. }
  119. void rebuildProperties()
  120. {
  121. auto& scheme = getAppSettings().appearance;
  122. Array<PropertyComponent*> props;
  123. auto fontValue = scheme.getCodeFontValue();
  124. props.add (FontNameValueSource::createProperty ("Code Editor Font", fontValue));
  125. props.add (FontSizeValueSource::createProperty ("Font Size", fontValue));
  126. const auto colourNames = scheme.getColourNames();
  127. for (int i = 0; i < colourNames.size(); ++i)
  128. props.add (new ColourPropertyComponent (nullptr, colourNames[i],
  129. scheme.getColourValue (colourNames[i]),
  130. Colours::white, false));
  131. panel.clear();
  132. panel.addProperties (props);
  133. }
  134. void resized() override
  135. {
  136. auto r = getLocalBounds();
  137. panel.setBounds (r.removeFromTop (getHeight() - 28).reduced (10, 2));
  138. loadButton.setBounds (r.removeFromLeft (getWidth() / 2).reduced (10, 1));
  139. saveButton.setBounds (r.reduced (10, 1));
  140. }
  141. private:
  142. PropertyPanel panel;
  143. TextButton loadButton, saveButton;
  144. Font codeFont;
  145. Array<var> colourValues;
  146. void saveScheme (bool isExit)
  147. {
  148. FileChooser fc ("Select a file in which to save this colour-scheme...",
  149. getAppSettings().appearance.getSchemesFolder()
  150. .getNonexistentChildFile ("Scheme", AppearanceSettings::getSchemeFileSuffix()),
  151. AppearanceSettings::getSchemeFileWildCard());
  152. if (fc.browseForFileToSave (true))
  153. {
  154. File file (fc.getResult().withFileExtension (AppearanceSettings::getSchemeFileSuffix()));
  155. getAppSettings().appearance.writeToFile (file);
  156. getAppSettings().appearance.refreshPresetSchemeList();
  157. saveSchemeState();
  158. ProjucerApplication::getApp().selectEditorColourSchemeWithName (file.getFileNameWithoutExtension());
  159. }
  160. else if (isExit)
  161. {
  162. restorePreviousScheme();
  163. }
  164. }
  165. void loadScheme()
  166. {
  167. FileChooser fc ("Please select a colour-scheme file to load...",
  168. getAppSettings().appearance.getSchemesFolder(),
  169. AppearanceSettings::getSchemeFileWildCard());
  170. if (fc.browseForFileToOpen())
  171. {
  172. if (getAppSettings().appearance.readFromFile (fc.getResult()))
  173. {
  174. rebuildProperties();
  175. saveSchemeState();
  176. }
  177. }
  178. }
  179. void lookAndFeelChanged() override
  180. {
  181. loadButton.setColour (TextButton::buttonColourId,
  182. findColour (secondaryButtonBackgroundColourId));
  183. }
  184. void saveSchemeState()
  185. {
  186. auto& appearance = getAppSettings().appearance;
  187. const auto colourNames = appearance.getColourNames();
  188. codeFont = appearance.getCodeFont();
  189. colourValues.clear();
  190. for (int i = 0; i < colourNames.size(); ++i)
  191. colourValues.add (appearance.getColourValue (colourNames[i]).getValue());
  192. }
  193. bool hasSchemeBeenModifiedSinceSave()
  194. {
  195. auto& appearance = getAppSettings().appearance;
  196. const auto colourNames = appearance.getColourNames();
  197. if (codeFont != appearance.getCodeFont())
  198. return true;
  199. for (int i = 0; i < colourNames.size(); ++i)
  200. if (colourValues[i] != appearance.getColourValue (colourNames[i]).getValue())
  201. return true;
  202. return false;
  203. }
  204. void restorePreviousScheme()
  205. {
  206. auto& appearance = getAppSettings().appearance;
  207. const auto colourNames = appearance.getColourNames();
  208. appearance.getCodeFontValue().setValue (codeFont.toString());
  209. for (int i = 0; i < colourNames.size(); ++i)
  210. appearance.getColourValue (colourNames[i]).setValue (colourValues[i]);
  211. }
  212. JUCE_DECLARE_NON_COPYABLE (EditorPanel)
  213. };
  214. //==============================================================================
  215. struct FontNameValueSource : public ValueSourceFilter
  216. {
  217. FontNameValueSource (const Value& source) : ValueSourceFilter (source) {}
  218. var getValue() const override
  219. {
  220. return Font::fromString (sourceValue.toString()).getTypefaceName();
  221. }
  222. void setValue (const var& newValue) override
  223. {
  224. auto font = Font::fromString (sourceValue.toString());
  225. font.setTypefaceName (newValue.toString().isEmpty() ? Font::getDefaultMonospacedFontName()
  226. : newValue.toString());
  227. sourceValue = font.toString();
  228. }
  229. static ChoicePropertyComponent* createProperty (const String& title, const Value& value)
  230. {
  231. auto fontNames = getAppSettings().monospacedFontNames;
  232. Array<var> values;
  233. values.add (Font::getDefaultMonospacedFontName());
  234. values.add (var());
  235. for (int i = 0; i < fontNames.size(); ++i)
  236. values.add (fontNames[i]);
  237. StringArray names;
  238. names.add ("<Default Monospaced>");
  239. names.add (String());
  240. names.addArray (getAppSettings().monospacedFontNames);
  241. return new ChoicePropertyComponent (Value (new FontNameValueSource (value)),
  242. title, names, values);
  243. }
  244. };
  245. //==============================================================================
  246. struct FontSizeValueSource : public ValueSourceFilter
  247. {
  248. FontSizeValueSource (const Value& source) : ValueSourceFilter (source) {}
  249. var getValue() const override
  250. {
  251. return Font::fromString (sourceValue.toString()).getHeight();
  252. }
  253. void setValue (const var& newValue) override
  254. {
  255. sourceValue = Font::fromString (sourceValue.toString()).withHeight (newValue).toString();
  256. }
  257. static PropertyComponent* createProperty (const String& title, const Value& value)
  258. {
  259. return new SliderPropertyComponent (Value (new FontSizeValueSource (value)),
  260. title, 5.0, 40.0, 0.1, 0.5);
  261. }
  262. };
  263. };
  264. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EditorColourSchemeWindowComponent)
  265. };