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.

370 lines
13KB

  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. ? findColour (widgetTextColourId)
  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 {};
  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 (findColour (backgroundColourId));
  93. g.setFont (14.0f);
  94. g.setColour (findColour (defaultTextColourId));
  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. addAndMakeVisible (loadButton);
  139. addAndMakeVisible (saveButton);
  140. loadButton.addListener (this);
  141. saveButton.addListener (this);
  142. }
  143. void rebuildProperties()
  144. {
  145. AppearanceSettings& scheme = getAppSettings().appearance;
  146. Array<PropertyComponent*> props;
  147. Value fontValue (scheme.getCodeFontValue());
  148. props.add (FontNameValueSource::createProperty ("Code Editor Font", fontValue));
  149. props.add (FontSizeValueSource::createProperty ("Font Size", fontValue));
  150. const StringArray colourNames (scheme.getColourNames());
  151. for (int i = 0; i < colourNames.size(); ++i)
  152. props.add (new ColourPropertyComponent (nullptr, colourNames[i],
  153. scheme.getColourValue (colourNames[i]),
  154. Colours::white, false));
  155. panel.clear();
  156. panel.addProperties (props);
  157. }
  158. void resized() override
  159. {
  160. Rectangle<int> r (getLocalBounds());
  161. panel.setBounds (r.removeFromTop (getHeight() - 28).reduced (4, 2));
  162. loadButton.setBounds (r.removeFromLeft (getWidth() / 2).reduced (10, 4));
  163. saveButton.setBounds (r.reduced (10, 3));
  164. }
  165. private:
  166. PropertyPanel panel;
  167. TextButton loadButton, saveButton;
  168. void buttonClicked (Button* b) override
  169. {
  170. if (b == &loadButton)
  171. loadScheme();
  172. else
  173. saveScheme();
  174. }
  175. void saveScheme()
  176. {
  177. FileChooser fc ("Select a file in which to save this colour-scheme...",
  178. getAppSettings().appearance.getSchemesFolder()
  179. .getNonexistentChildFile ("Scheme", AppearanceSettings::getSchemeFileSuffix()),
  180. AppearanceSettings::getSchemeFileWildCard());
  181. if (fc.browseForFileToSave (true))
  182. {
  183. File file (fc.getResult().withFileExtension (AppearanceSettings::getSchemeFileSuffix()));
  184. getAppSettings().appearance.writeToFile (file);
  185. getAppSettings().appearance.refreshPresetSchemeList();
  186. }
  187. }
  188. void loadScheme()
  189. {
  190. FileChooser fc ("Please select a colour-scheme file to load...",
  191. getAppSettings().appearance.getSchemesFolder(),
  192. AppearanceSettings::getSchemeFileWildCard());
  193. if (fc.browseForFileToOpen())
  194. if (getAppSettings().appearance.readFromFile (fc.getResult()))
  195. rebuildProperties();
  196. }
  197. JUCE_DECLARE_NON_COPYABLE (EditorPanel)
  198. };
  199. //==============================================================================
  200. struct FontNameValueSource : public ValueSourceFilter
  201. {
  202. FontNameValueSource (const Value& source) : ValueSourceFilter (source) {}
  203. var getValue() const override
  204. {
  205. return Font::fromString (sourceValue.toString()).getTypefaceName();
  206. }
  207. void setValue (const var& newValue) override
  208. {
  209. Font font (Font::fromString (sourceValue.toString()));
  210. font.setTypefaceName (newValue.toString().isEmpty() ? Font::getDefaultMonospacedFontName()
  211. : newValue.toString());
  212. sourceValue = font.toString();
  213. }
  214. static ChoicePropertyComponent* createProperty (const String& title, const Value& value)
  215. {
  216. StringArray fontNames = getAppSettings().monospacedFontNames;
  217. Array<var> values;
  218. values.add (Font::getDefaultMonospacedFontName());
  219. values.add (var());
  220. for (int i = 0; i < fontNames.size(); ++i)
  221. values.add (fontNames[i]);
  222. StringArray names;
  223. names.add ("<Default Monospaced>");
  224. names.add (String());
  225. names.addArray (getAppSettings().monospacedFontNames);
  226. return new ChoicePropertyComponent (Value (new FontNameValueSource (value)),
  227. title, names, values);
  228. }
  229. };
  230. //==============================================================================
  231. struct FontSizeValueSource : public ValueSourceFilter
  232. {
  233. FontSizeValueSource (const Value& source) : ValueSourceFilter (source) {}
  234. var getValue() const override
  235. {
  236. return Font::fromString (sourceValue.toString()).getHeight();
  237. }
  238. void setValue (const var& newValue) override
  239. {
  240. sourceValue = Font::fromString (sourceValue.toString()).withHeight (newValue).toString();
  241. }
  242. static PropertyComponent* createProperty (const String& title, const Value& value)
  243. {
  244. return new SliderPropertyComponent (Value (new FontSizeValueSource (value)),
  245. title, 5.0, 40.0, 0.1, 0.5);
  246. }
  247. };
  248. };
  249. void AppearanceSettings::showGlobalPreferences (ScopedPointer<Component>& ownerPointer)
  250. {
  251. if (ownerPointer != nullptr)
  252. ownerPointer->toFront (true);
  253. else
  254. new FloatingToolWindow ("Preferences",
  255. "globalPreferencesEditorPos",
  256. new GlobalPreferencesComponent,
  257. ownerPointer, false,
  258. 500, 500, 500, 500, 500, 500);
  259. }
  260. //==============================================================================
  261. AppearanceSettingsTab::AppearanceSettingsTab()
  262. {
  263. if (getAppSettings().monospacedFontNames.size() == 0)
  264. content = new AppearanceEditor::FontScanPanel();
  265. else
  266. content = new AppearanceEditor::EditorPanel();
  267. changeContent (content);
  268. }
  269. Component* AppearanceSettingsTab::getContent()
  270. {
  271. return this;
  272. }
  273. void AppearanceSettingsTab::changeContent (Component* newContent)
  274. {
  275. content = newContent;
  276. addAndMakeVisible (content);
  277. content->setBounds (getLocalBounds());
  278. }
  279. String AppearanceSettingsTab::getName() const noexcept
  280. {
  281. return "Code Editor";
  282. }
  283. void AppearanceSettingsTab::resized()
  284. {
  285. content->setBounds (getLocalBounds());
  286. }
  287. //==============================================================================
  288. GlobalPreferencesComponent::GlobalPreferencesComponent()
  289. : TabbedComponent (TabbedButtonBar::TabsAtTop)
  290. {
  291. preferenceTabs.add (new PathSettingsTab (TargetOS::getThisOS()));
  292. preferenceTabs.add (new AppearanceSettingsTab);
  293. for (GlobalPreferencesTab** tab = preferenceTabs.begin(); tab != preferenceTabs.end(); ++tab)
  294. addTab ((*tab)->getName(), findColour (backgroundColourId, true), (*tab)->getContent(), true);
  295. }