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.

394 lines
14KB

  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_GlobalPreferences.h"
  21. #include "../Utility/jucer_FloatingToolWindow.h"
  22. #include "../Utility/jucer_ColourPropertyComponent.h"
  23. //==============================================================================
  24. PathSettingsTab::PathSettingsTab (DependencyPathOS os)
  25. {
  26. const int maxChars = 1024;
  27. auto& settings = getAppSettings();
  28. vst3PathComponent = pathComponents.add (new TextPropertyComponent (settings.getGlobalPath (Ids::vst3Path, os), "VST3 SDK", maxChars, false));
  29. #if ! JUCE_LINUX
  30. rtasPathComponent = pathComponents.add (new TextPropertyComponent (settings.getGlobalPath (Ids::rtasPath, os), "RTAS SDK", maxChars, false));
  31. aaxPathComponent = pathComponents.add (new TextPropertyComponent (settings.getGlobalPath (Ids::aaxPath, os), "AAX SDK", maxChars, false));
  32. #endif
  33. androidSdkPathComponent = pathComponents.add (new TextPropertyComponent (settings.getGlobalPath (Ids::androidSDKPath, os), "Android SDK", maxChars, false));
  34. androidNdkPathComponent = pathComponents.add (new TextPropertyComponent (settings.getGlobalPath (Ids::androidNDKPath, os), "Android NDK", maxChars, false));
  35. for (auto component : pathComponents)
  36. {
  37. addAndMakeVisible (component);
  38. component->addListener (this);
  39. textPropertyComponentChanged (component);
  40. }
  41. }
  42. PathSettingsTab::~PathSettingsTab()
  43. {
  44. }
  45. void PathSettingsTab::textPropertyComponentChanged (TextPropertyComponent* textPropertyComponent)
  46. {
  47. auto keyName = getKeyForPropertyComponent (textPropertyComponent);
  48. auto textColour = getAppSettings().isGlobalPathValid (File::getCurrentWorkingDirectory(), keyName, textPropertyComponent->getText())
  49. ? findColour (widgetTextColourId)
  50. : Colours::red;
  51. textPropertyComponent->setColour (TextPropertyComponent::textColourId, textColour);
  52. }
  53. Identifier PathSettingsTab::getKeyForPropertyComponent (TextPropertyComponent* component) const
  54. {
  55. if (component == vst3PathComponent) return Ids::vst3Path;
  56. if (component == rtasPathComponent) return Ids::rtasPath;
  57. if (component == aaxPathComponent) return Ids::aaxPath;
  58. if (component == androidSdkPathComponent) return Ids::androidSDKPath;
  59. if (component == androidNdkPathComponent) return Ids::androidNDKPath;
  60. // this property component does not have a key associated to it!
  61. jassertfalse;
  62. return {};
  63. }
  64. Component* PathSettingsTab::getContent()
  65. {
  66. return this;
  67. }
  68. String PathSettingsTab::getName() const noexcept
  69. {
  70. return "Paths";
  71. }
  72. void PathSettingsTab::resized()
  73. {
  74. const int componentHeight = 25;
  75. for (auto component : pathComponents)
  76. {
  77. const auto elementNumber = pathComponents.indexOf (component);
  78. component->setBounds (10, componentHeight * elementNumber, getWidth() - 20, componentHeight);
  79. }
  80. }
  81. void PathSettingsTab::lookAndFeelChanged()
  82. {
  83. for (auto* comp : pathComponents)
  84. textPropertyComponentChanged (comp);
  85. }
  86. //==============================================================================
  87. struct AppearanceEditor
  88. {
  89. struct FontScanPanel : public Component,
  90. private Timer
  91. {
  92. FontScanPanel()
  93. {
  94. fontsToScan = Font::findAllTypefaceNames();
  95. startTimer (1);
  96. }
  97. void paint (Graphics& g) override
  98. {
  99. g.fillAll (findColour (backgroundColourId));
  100. g.setFont (14.0f);
  101. g.setColour (findColour (defaultTextColourId));
  102. g.drawFittedText ("Scanning for fonts..", getLocalBounds(), Justification::centred, 2);
  103. const auto size = 30;
  104. getLookAndFeel().drawSpinningWaitAnimation (g, Colours::white, (getWidth() - size) / 2, getHeight() / 2 - 50, size, size);
  105. }
  106. void timerCallback() override
  107. {
  108. repaint();
  109. if (fontsToScan.size() == 0)
  110. {
  111. getAppSettings().monospacedFontNames = fontsFound;
  112. if (auto* tab = findParentComponentOfClass<AppearanceSettingsTab>())
  113. tab->changeContent (new EditorPanel());
  114. }
  115. else
  116. {
  117. if (isMonospacedTypeface (fontsToScan[0]))
  118. fontsFound.add (fontsToScan[0]);
  119. fontsToScan.remove (0);
  120. }
  121. }
  122. // A rather hacky trick to select only the fixed-pitch fonts..
  123. // This is unfortunately a bit slow, but will work on all platforms.
  124. static bool isMonospacedTypeface (const String& name)
  125. {
  126. const Font font (name, 20.0f, Font::plain);
  127. const auto width = font.getStringWidth ("....");
  128. return width == font.getStringWidth ("WWWW")
  129. && width == font.getStringWidth ("0000")
  130. && width == font.getStringWidth ("1111")
  131. && width == font.getStringWidth ("iiii");
  132. }
  133. StringArray fontsToScan, fontsFound;
  134. };
  135. //==============================================================================
  136. struct EditorPanel : public Component,
  137. private ButtonListener
  138. {
  139. EditorPanel()
  140. : loadButton ("Load Scheme..."),
  141. saveButton ("Save Scheme...")
  142. {
  143. rebuildProperties();
  144. addAndMakeVisible (panel);
  145. addAndMakeVisible (loadButton);
  146. addAndMakeVisible (saveButton);
  147. loadButton.addListener (this);
  148. saveButton.addListener (this);
  149. }
  150. void rebuildProperties()
  151. {
  152. auto& scheme = getAppSettings().appearance;
  153. Array<PropertyComponent*> props;
  154. auto fontValue = scheme.getCodeFontValue();
  155. props.add (FontNameValueSource::createProperty ("Code Editor Font", fontValue));
  156. props.add (FontSizeValueSource::createProperty ("Font Size", fontValue));
  157. const auto colourNames = scheme.getColourNames();
  158. for (int i = 0; i < colourNames.size(); ++i)
  159. props.add (new ColourPropertyComponent (nullptr, colourNames[i],
  160. scheme.getColourValue (colourNames[i]),
  161. Colours::white, false));
  162. panel.clear();
  163. panel.addProperties (props);
  164. }
  165. void resized() override
  166. {
  167. auto r = getLocalBounds();
  168. panel.setBounds (r.removeFromTop (getHeight() - 28).reduced (10, 2));
  169. loadButton.setBounds (r.removeFromLeft (getWidth() / 2).reduced (10, 4));
  170. saveButton.setBounds (r.reduced (10, 3));
  171. }
  172. private:
  173. PropertyPanel panel;
  174. TextButton loadButton, saveButton;
  175. void buttonClicked (Button* b) override
  176. {
  177. if (b == &loadButton)
  178. loadScheme();
  179. else
  180. saveScheme();
  181. }
  182. void saveScheme()
  183. {
  184. FileChooser fc ("Select a file in which to save this colour-scheme...",
  185. getAppSettings().appearance.getSchemesFolder()
  186. .getNonexistentChildFile ("Scheme", AppearanceSettings::getSchemeFileSuffix()),
  187. AppearanceSettings::getSchemeFileWildCard());
  188. if (fc.browseForFileToSave (true))
  189. {
  190. File file (fc.getResult().withFileExtension (AppearanceSettings::getSchemeFileSuffix()));
  191. getAppSettings().appearance.writeToFile (file);
  192. getAppSettings().appearance.refreshPresetSchemeList();
  193. }
  194. }
  195. void loadScheme()
  196. {
  197. FileChooser fc ("Please select a colour-scheme file to load...",
  198. getAppSettings().appearance.getSchemesFolder(),
  199. AppearanceSettings::getSchemeFileWildCard());
  200. if (fc.browseForFileToOpen())
  201. if (getAppSettings().appearance.readFromFile (fc.getResult()))
  202. rebuildProperties();
  203. }
  204. void lookAndFeelChanged() override
  205. {
  206. loadButton.setColour (TextButton::buttonColourId,
  207. findColour (secondaryButtonBackgroundColourId));
  208. }
  209. JUCE_DECLARE_NON_COPYABLE (EditorPanel)
  210. };
  211. //==============================================================================
  212. struct FontNameValueSource : public ValueSourceFilter
  213. {
  214. FontNameValueSource (const Value& source) : ValueSourceFilter (source) {}
  215. var getValue() const override
  216. {
  217. return Font::fromString (sourceValue.toString()).getTypefaceName();
  218. }
  219. void setValue (const var& newValue) override
  220. {
  221. auto font = Font::fromString (sourceValue.toString());
  222. font.setTypefaceName (newValue.toString().isEmpty() ? Font::getDefaultMonospacedFontName()
  223. : newValue.toString());
  224. sourceValue = font.toString();
  225. }
  226. static ChoicePropertyComponent* createProperty (const String& title, const Value& value)
  227. {
  228. auto fontNames = getAppSettings().monospacedFontNames;
  229. Array<var> values;
  230. values.add (Font::getDefaultMonospacedFontName());
  231. values.add (var());
  232. for (int i = 0; i < fontNames.size(); ++i)
  233. values.add (fontNames[i]);
  234. StringArray names;
  235. names.add ("<Default Monospaced>");
  236. names.add (String());
  237. names.addArray (getAppSettings().monospacedFontNames);
  238. return new ChoicePropertyComponent (Value (new FontNameValueSource (value)),
  239. title, names, values);
  240. }
  241. };
  242. //==============================================================================
  243. struct FontSizeValueSource : public ValueSourceFilter
  244. {
  245. FontSizeValueSource (const Value& source) : ValueSourceFilter (source) {}
  246. var getValue() const override
  247. {
  248. return Font::fromString (sourceValue.toString()).getHeight();
  249. }
  250. void setValue (const var& newValue) override
  251. {
  252. sourceValue = Font::fromString (sourceValue.toString()).withHeight (newValue).toString();
  253. }
  254. static PropertyComponent* createProperty (const String& title, const Value& value)
  255. {
  256. return new SliderPropertyComponent (Value (new FontSizeValueSource (value)),
  257. title, 5.0, 40.0, 0.1, 0.5);
  258. }
  259. };
  260. };
  261. void AppearanceSettings::showGlobalPreferences (ScopedPointer<Component>& ownerPointer)
  262. {
  263. if (ownerPointer != nullptr)
  264. ownerPointer->toFront (true);
  265. else
  266. new FloatingToolWindow ("Preferences",
  267. "globalPreferencesEditorPos",
  268. new GlobalPreferencesComponent(),
  269. ownerPointer, false,
  270. 500, 500, 500, 500, 500, 500);
  271. }
  272. //==============================================================================
  273. AppearanceSettingsTab::AppearanceSettingsTab()
  274. {
  275. if (getAppSettings().monospacedFontNames.size() == 0)
  276. content = new AppearanceEditor::FontScanPanel();
  277. else
  278. content = new AppearanceEditor::EditorPanel();
  279. changeContent (content);
  280. }
  281. Component* AppearanceSettingsTab::getContent()
  282. {
  283. return this;
  284. }
  285. void AppearanceSettingsTab::changeContent (Component* newContent)
  286. {
  287. content = newContent;
  288. addAndMakeVisible (content);
  289. content->setBounds (getLocalBounds());
  290. }
  291. String AppearanceSettingsTab::getName() const noexcept
  292. {
  293. return "Code Editor";
  294. }
  295. void AppearanceSettingsTab::resized()
  296. {
  297. content->setBounds (getLocalBounds());
  298. }
  299. //==============================================================================
  300. GlobalPreferencesComponent::GlobalPreferencesComponent()
  301. : TabbedComponent (TabbedButtonBar::TabsAtTop)
  302. {
  303. preferenceTabs.add (new PathSettingsTab (TargetOS::getThisOS()));
  304. preferenceTabs.add (new AppearanceSettingsTab);
  305. for (GlobalPreferencesTab** tab = preferenceTabs.begin(); tab != preferenceTabs.end(); ++tab)
  306. addTab ((*tab)->getName(), findColour (backgroundColourId, true), (*tab)->getContent(), true);
  307. }
  308. void GlobalPreferencesComponent::paint (Graphics& g)
  309. {
  310. g.fillAll (findColour (backgroundColourId));
  311. }
  312. void GlobalPreferencesComponent::lookAndFeelChanged()
  313. {
  314. for (auto* tab : preferenceTabs)
  315. tab->getContent()->sendLookAndFeelChange();
  316. }