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.

466 lines
16KB

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