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.

345 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/jucer_ColourPropertyComponent.h"
  21. class EditorColourSchemeWindowComponent : public Component
  22. {
  23. public:
  24. EditorColourSchemeWindowComponent()
  25. {
  26. if (getAppSettings().monospacedFontNames.size() == 0)
  27. content = new AppearanceEditor::FontScanPanel();
  28. else
  29. content = new AppearanceEditor::EditorPanel();
  30. changeContent (content);
  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 = newContent;
  43. addAndMakeVisible (content);
  44. content->setBounds (getLocalBounds().reduced (10));
  45. }
  46. private:
  47. ScopedPointer<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. private ButtonListener
  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.addListener (this);
  110. saveButton.addListener (this);
  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 buttonClicked (Button* b) override
  147. {
  148. if (b == &loadButton)
  149. loadScheme();
  150. else
  151. saveScheme (false);
  152. }
  153. void saveScheme (bool isExit)
  154. {
  155. FileChooser fc ("Select a file in which to save this colour-scheme...",
  156. getAppSettings().appearance.getSchemesFolder()
  157. .getNonexistentChildFile ("Scheme", AppearanceSettings::getSchemeFileSuffix()),
  158. AppearanceSettings::getSchemeFileWildCard());
  159. if (fc.browseForFileToSave (true))
  160. {
  161. File file (fc.getResult().withFileExtension (AppearanceSettings::getSchemeFileSuffix()));
  162. getAppSettings().appearance.writeToFile (file);
  163. getAppSettings().appearance.refreshPresetSchemeList();
  164. saveSchemeState();
  165. ProjucerApplication::getApp().selectEditorColourSchemeWithName (file.getFileNameWithoutExtension());
  166. }
  167. else if (isExit)
  168. {
  169. restorePreviousScheme();
  170. }
  171. }
  172. void loadScheme()
  173. {
  174. FileChooser fc ("Please select a colour-scheme file to load...",
  175. getAppSettings().appearance.getSchemesFolder(),
  176. AppearanceSettings::getSchemeFileWildCard());
  177. if (fc.browseForFileToOpen())
  178. {
  179. if (getAppSettings().appearance.readFromFile (fc.getResult()))
  180. {
  181. rebuildProperties();
  182. saveSchemeState();
  183. }
  184. }
  185. }
  186. void lookAndFeelChanged() override
  187. {
  188. loadButton.setColour (TextButton::buttonColourId,
  189. findColour (secondaryButtonBackgroundColourId));
  190. }
  191. void saveSchemeState()
  192. {
  193. auto& appearance = getAppSettings().appearance;
  194. const auto colourNames = appearance.getColourNames();
  195. codeFont = appearance.getCodeFont();
  196. colourValues.clear();
  197. for (int i = 0; i < colourNames.size(); ++i)
  198. colourValues.add (appearance.getColourValue (colourNames[i]).getValue());
  199. }
  200. bool hasSchemeBeenModifiedSinceSave()
  201. {
  202. auto& appearance = getAppSettings().appearance;
  203. const auto colourNames = appearance.getColourNames();
  204. if (codeFont != appearance.getCodeFont())
  205. return true;
  206. for (int i = 0; i < colourNames.size(); ++i)
  207. if (colourValues[i] != appearance.getColourValue (colourNames[i]).getValue())
  208. return true;
  209. return false;
  210. }
  211. void restorePreviousScheme()
  212. {
  213. auto& appearance = getAppSettings().appearance;
  214. const auto colourNames = appearance.getColourNames();
  215. appearance.getCodeFontValue().setValue (codeFont.toString());
  216. for (int i = 0; i < colourNames.size(); ++i)
  217. appearance.getColourValue (colourNames[i]).setValue (colourValues[i]);
  218. }
  219. JUCE_DECLARE_NON_COPYABLE (EditorPanel)
  220. };
  221. //==============================================================================
  222. struct FontNameValueSource : public ValueSourceFilter
  223. {
  224. FontNameValueSource (const Value& source) : ValueSourceFilter (source) {}
  225. var getValue() const override
  226. {
  227. return Font::fromString (sourceValue.toString()).getTypefaceName();
  228. }
  229. void setValue (const var& newValue) override
  230. {
  231. auto font = Font::fromString (sourceValue.toString());
  232. font.setTypefaceName (newValue.toString().isEmpty() ? Font::getDefaultMonospacedFontName()
  233. : newValue.toString());
  234. sourceValue = font.toString();
  235. }
  236. static ChoicePropertyComponent* createProperty (const String& title, const Value& value)
  237. {
  238. auto fontNames = getAppSettings().monospacedFontNames;
  239. Array<var> values;
  240. values.add (Font::getDefaultMonospacedFontName());
  241. values.add (var());
  242. for (int i = 0; i < fontNames.size(); ++i)
  243. values.add (fontNames[i]);
  244. StringArray names;
  245. names.add ("<Default Monospaced>");
  246. names.add (String());
  247. names.addArray (getAppSettings().monospacedFontNames);
  248. return new ChoicePropertyComponent (Value (new FontNameValueSource (value)),
  249. title, names, values);
  250. }
  251. };
  252. //==============================================================================
  253. struct FontSizeValueSource : public ValueSourceFilter
  254. {
  255. FontSizeValueSource (const Value& source) : ValueSourceFilter (source) {}
  256. var getValue() const override
  257. {
  258. return Font::fromString (sourceValue.toString()).getHeight();
  259. }
  260. void setValue (const var& newValue) override
  261. {
  262. sourceValue = Font::fromString (sourceValue.toString()).withHeight (newValue).toString();
  263. }
  264. static PropertyComponent* createProperty (const String& title, const Value& value)
  265. {
  266. return new SliderPropertyComponent (Value (new FontSizeValueSource (value)),
  267. title, 5.0, 40.0, 0.1, 0.5);
  268. }
  269. };
  270. };
  271. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EditorColourSchemeWindowComponent)
  272. };