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.

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