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.

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