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.

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