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.

387 lines
14KB

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