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.

484 lines
17KB

  1. /*
  2. ==============================================================================
  3. jucer_GlobalPreferences.cpp
  4. Created: 22 Jul 2015 11:05:35am
  5. Author: Timur Doumler
  6. ==============================================================================
  7. */
  8. #include "../jucer_Headers.h"
  9. #include "jucer_GlobalPreferences.h"
  10. //==============================================================================
  11. const String PathSettingsTab::vst2KeyName = "vst2Path";
  12. const String PathSettingsTab::vst3KeyName = "vst3Path";
  13. const String PathSettingsTab::rtasKeyName = "rtasPath";
  14. const String PathSettingsTab::aaxKeyName = "aaxPath";
  15. const String PathSettingsTab::androidSdkKeyName = "androidSdkPath";
  16. const String PathSettingsTab::androidNdkKeyName = "androidNdkPath";
  17. //==============================================================================
  18. class AppearanceSettingsTab : public GlobalPreferencesTab,
  19. public Component
  20. {
  21. public:
  22. AppearanceSettingsTab();
  23. Component* getContent() override;
  24. void changeContent (Component* newContent);
  25. String getName() const noexcept override;
  26. ScopedPointer<Component> content;
  27. };
  28. //==============================================================================
  29. namespace PathSettingsHelpers
  30. {
  31. bool checkSdkPathContainsFile (const String& path, const String& fileToCheckFor)
  32. {
  33. return File::getCurrentWorkingDirectory().getChildFile( path + "/" + fileToCheckFor).existsAsFile();
  34. }
  35. }
  36. PathSettingsTab::PathSettingsTab (DependencyPathOS os)
  37. {
  38. const int maxChars = 1024;
  39. vst2PathComponent = pathComponents.add (new TextPropertyComponent (getPathByKey (vst2KeyName, os), "VST SDK", maxChars, false));
  40. vst3PathComponent = pathComponents.add (new TextPropertyComponent (getPathByKey (vst3KeyName, os), "VST3 SDK", maxChars, false));
  41. #if ! JUCE_LINUX
  42. rtasPathComponent = pathComponents.add (new TextPropertyComponent (getPathByKey (rtasKeyName, os), "RTAS SDK", maxChars, false));
  43. aaxPathComponent = pathComponents.add (new TextPropertyComponent (getPathByKey (aaxKeyName, os), "AAX SDK", maxChars, false));
  44. #endif
  45. androidSdkPathComponent = pathComponents.add (new TextPropertyComponent (getPathByKey (androidSdkKeyName, os), "Android SDK", maxChars, false));
  46. androidNdkPathComponent = pathComponents.add (new TextPropertyComponent (getPathByKey (androidNdkKeyName, os), "Android NDK", maxChars, false));
  47. for (TextPropertyComponent** component = pathComponents.begin(); component != pathComponents.end(); ++component)
  48. {
  49. addAndMakeVisible (**component);
  50. (*component)->addListener (this);
  51. textPropertyComponentChanged (*component);
  52. }
  53. }
  54. PathSettingsTab::~PathSettingsTab()
  55. {
  56. }
  57. void PathSettingsTab::textPropertyComponentChanged (TextPropertyComponent* textPropertyComponent)
  58. {
  59. String keyName = getKeyForPropertyComponent (textPropertyComponent);
  60. Colour textColour = checkPathByKey (keyName, textPropertyComponent->getText()) ? Colours::black : Colours::red;
  61. textPropertyComponent->setColour (TextPropertyComponent::textColourId, textColour);
  62. }
  63. String PathSettingsTab::getKeyForPropertyComponent (TextPropertyComponent* component) const
  64. {
  65. if (component == vst2PathComponent) return vst2KeyName;
  66. if (component == vst3PathComponent) return vst3KeyName;
  67. if (component == rtasPathComponent) return rtasKeyName;
  68. if (component == aaxPathComponent) return aaxKeyName;
  69. if (component == androidSdkPathComponent) return androidSdkKeyName;
  70. if (component == androidNdkPathComponent) return androidNdkKeyName;
  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. Value& PathSettingsTab::getPathByKey (const String& key, DependencyPathOS os)
  94. {
  95. getAppSettings().pathValues[key].referTo (getAppSettings().projectDefaults.getPropertyAsValue (key, nullptr));
  96. Value& value = getAppSettings().pathValues[key];
  97. if (value.toString().isEmpty())
  98. value = getFallbackPathByKey (key, os);
  99. return value;
  100. }
  101. //==============================================================================
  102. String PathSettingsTab::getFallbackPathByKey (const String& key, DependencyPathOS os)
  103. {
  104. if (key == vst2KeyName || key == vst3KeyName)
  105. return os == DependencyPath::windows ? "c:\\SDKs\\VST3 SDK"
  106. : "~/SDKs/VST3 SDK";
  107. if (key == rtasKeyName)
  108. {
  109. if (os == DependencyPath::windows) return "c:\\SDKs\\PT_80_SDK";
  110. if (os == DependencyPath::osx) return "~/SDKs/PT_80_SDK";
  111. // no RTAS on this OS!
  112. jassertfalse;
  113. return String();
  114. }
  115. if (key == aaxKeyName)
  116. {
  117. if (os == DependencyPath::windows) return "c:\\SDKs\\AAX";
  118. if (os == DependencyPath::osx) return "~/SDKs/AAX" ;
  119. // no RTAS on this OS!
  120. jassertfalse;
  121. return String();
  122. }
  123. if (key == androidSdkKeyName)
  124. return os == DependencyPath::windows ? "c:\\SDKs\\android-sdk"
  125. : "~/Library/Android/sdk";
  126. if (key == androidNdkKeyName)
  127. return os == DependencyPath::windows ? "c:\\SDKs\\android-ndk"
  128. : "~/Library/Android/ndk";
  129. // didn't recognise the key provided!
  130. jassertfalse;
  131. return String();
  132. }
  133. //==============================================================================
  134. bool PathSettingsTab::checkPathByKey (const String& key, const String& path)
  135. {
  136. String fileToCheckFor;
  137. if (key == vst2KeyName)
  138. {
  139. fileToCheckFor = "public.sdk/source/vst2.x/audioeffectx.h";
  140. }
  141. else if (key == vst3KeyName)
  142. {
  143. fileToCheckFor = "base/source/baseiids.cpp";
  144. }
  145. else if (key == rtasKeyName)
  146. {
  147. fileToCheckFor = "AlturaPorts/TDMPlugIns/PlugInLibrary/EffectClasses/CEffectProcessMIDI.cpp";
  148. }
  149. else if (key == aaxKeyName)
  150. {
  151. fileToCheckFor = "Interfaces/AAX_Exports.cpp";
  152. }
  153. else if (key == androidSdkKeyName)
  154. {
  155. #if JUCE_WINDOWS
  156. fileToCheckFor = "platform-tools/adb.exe";
  157. #else
  158. fileToCheckFor = "platform-tools/adb";
  159. #endif
  160. }
  161. else if (key == androidNdkKeyName)
  162. {
  163. #if JUCE_WINDOWS
  164. fileToCheckFor = "ndk-depends.exe";
  165. #else
  166. fileToCheckFor = "ndk-depends";
  167. #endif
  168. }
  169. else
  170. {
  171. // didn't recognise the key provided!
  172. jassertfalse;
  173. return false;
  174. }
  175. return PathSettingsHelpers::checkSdkPathContainsFile (path, fileToCheckFor);
  176. }
  177. //==============================================================================
  178. struct AppearanceEditor
  179. {
  180. struct FontScanPanel : public Component,
  181. private Timer
  182. {
  183. FontScanPanel()
  184. {
  185. fontsToScan = Font::findAllTypefaceNames();
  186. startTimer (1);
  187. }
  188. void paint (Graphics& g) override
  189. {
  190. g.fillAll (Colours::darkgrey);
  191. g.setFont (14.0f);
  192. g.setColour (Colours::white);
  193. g.drawFittedText ("Scanning for fonts..", getLocalBounds(), Justification::centred, 2);
  194. const int size = 30;
  195. getLookAndFeel().drawSpinningWaitAnimation (g, Colours::white, (getWidth() - size) / 2, getHeight() / 2 - 50, size, size);
  196. }
  197. void timerCallback() override
  198. {
  199. repaint();
  200. if (fontsToScan.size() == 0)
  201. {
  202. getAppSettings().monospacedFontNames = fontsFound;
  203. if (AppearanceSettingsTab* tab = findParentComponentOfClass<AppearanceSettingsTab>())
  204. tab->changeContent (new EditorPanel());
  205. }
  206. else
  207. {
  208. if (isMonospacedTypeface (fontsToScan[0]))
  209. fontsFound.add (fontsToScan[0]);
  210. fontsToScan.remove (0);
  211. }
  212. }
  213. // A rather hacky trick to select only the fixed-pitch fonts..
  214. // This is unfortunately a bit slow, but will work on all platforms.
  215. static bool isMonospacedTypeface (const String& name)
  216. {
  217. const Font font (name, 20.0f, Font::plain);
  218. const int width = font.getStringWidth ("....");
  219. return width == font.getStringWidth ("WWWW")
  220. && width == font.getStringWidth ("0000")
  221. && width == font.getStringWidth ("1111")
  222. && width == font.getStringWidth ("iiii");
  223. }
  224. StringArray fontsToScan, fontsFound;
  225. };
  226. //==============================================================================
  227. struct EditorPanel : public Component,
  228. private ButtonListener
  229. {
  230. EditorPanel()
  231. : loadButton ("Load Scheme..."),
  232. saveButton ("Save Scheme...")
  233. {
  234. rebuildProperties();
  235. addAndMakeVisible (panel);
  236. loadButton.setColour (TextButton::buttonColourId, Colours::lightgrey.withAlpha (0.5f));
  237. saveButton.setColour (TextButton::buttonColourId, Colours::lightgrey.withAlpha (0.5f));
  238. loadButton.setColour (TextButton::textColourOffId, Colours::white);
  239. saveButton.setColour (TextButton::textColourOffId, Colours::white);
  240. addAndMakeVisible (loadButton);
  241. addAndMakeVisible (saveButton);
  242. loadButton.addListener (this);
  243. saveButton.addListener (this);
  244. }
  245. void rebuildProperties()
  246. {
  247. AppearanceSettings& scheme = getAppSettings().appearance;
  248. Array<PropertyComponent*> props;
  249. Value fontValue (scheme.getCodeFontValue());
  250. props.add (FontNameValueSource::createProperty ("Code Editor Font", fontValue));
  251. props.add (FontSizeValueSource::createProperty ("Font Size", fontValue));
  252. const StringArray colourNames (scheme.getColourNames());
  253. for (int i = 0; i < colourNames.size(); ++i)
  254. props.add (new ColourPropertyComponent (nullptr, colourNames[i],
  255. scheme.getColourValue (colourNames[i]),
  256. Colours::white, false));
  257. panel.clear();
  258. panel.addProperties (props);
  259. }
  260. void resized() override
  261. {
  262. Rectangle<int> r (getLocalBounds());
  263. panel.setBounds (r.removeFromTop (getHeight() - 28).reduced (4, 2));
  264. loadButton.setBounds (r.removeFromLeft (getWidth() / 2).reduced (10, 4));
  265. saveButton.setBounds (r.reduced (10, 3));
  266. }
  267. private:
  268. PropertyPanel panel;
  269. TextButton loadButton, saveButton;
  270. void buttonClicked (Button* b) override
  271. {
  272. if (b == &loadButton)
  273. loadScheme();
  274. else
  275. saveScheme();
  276. }
  277. void saveScheme()
  278. {
  279. FileChooser fc ("Select a file in which to save this colour-scheme...",
  280. getAppSettings().appearance.getSchemesFolder()
  281. .getNonexistentChildFile ("Scheme", AppearanceSettings::getSchemeFileSuffix()),
  282. AppearanceSettings::getSchemeFileWildCard());
  283. if (fc.browseForFileToSave (true))
  284. {
  285. File file (fc.getResult().withFileExtension (AppearanceSettings::getSchemeFileSuffix()));
  286. getAppSettings().appearance.writeToFile (file);
  287. getAppSettings().appearance.refreshPresetSchemeList();
  288. }
  289. }
  290. void loadScheme()
  291. {
  292. FileChooser fc ("Please select a colour-scheme file to load...",
  293. getAppSettings().appearance.getSchemesFolder(),
  294. AppearanceSettings::getSchemeFileWildCard());
  295. if (fc.browseForFileToOpen())
  296. if (getAppSettings().appearance.readFromFile (fc.getResult()))
  297. rebuildProperties();
  298. }
  299. JUCE_DECLARE_NON_COPYABLE (EditorPanel)
  300. };
  301. //==============================================================================
  302. struct FontNameValueSource : public ValueSourceFilter
  303. {
  304. FontNameValueSource (const Value& source) : ValueSourceFilter (source) {}
  305. var getValue() const override
  306. {
  307. return Font::fromString (sourceValue.toString()).getTypefaceName();
  308. }
  309. void setValue (const var& newValue) override
  310. {
  311. Font font (Font::fromString (sourceValue.toString()));
  312. font.setTypefaceName (newValue.toString().isEmpty() ? Font::getDefaultMonospacedFontName()
  313. : newValue.toString());
  314. sourceValue = font.toString();
  315. }
  316. static ChoicePropertyComponent* createProperty (const String& title, const Value& value)
  317. {
  318. StringArray fontNames = getAppSettings().monospacedFontNames;
  319. Array<var> values;
  320. values.add (Font::getDefaultMonospacedFontName());
  321. values.add (var());
  322. for (int i = 0; i < fontNames.size(); ++i)
  323. values.add (fontNames[i]);
  324. StringArray names;
  325. names.add ("<Default Monospaced>");
  326. names.add (String::empty);
  327. names.addArray (getAppSettings().monospacedFontNames);
  328. return new ChoicePropertyComponent (Value (new FontNameValueSource (value)),
  329. title, names, values);
  330. }
  331. };
  332. //==============================================================================
  333. struct FontSizeValueSource : public ValueSourceFilter
  334. {
  335. FontSizeValueSource (const Value& source) : ValueSourceFilter (source) {}
  336. var getValue() const override
  337. {
  338. return Font::fromString (sourceValue.toString()).getHeight();
  339. }
  340. void setValue (const var& newValue) override
  341. {
  342. sourceValue = Font::fromString (sourceValue.toString()).withHeight (newValue).toString();
  343. }
  344. static PropertyComponent* createProperty (const String& title, const Value& value)
  345. {
  346. return new SliderPropertyComponent (Value (new FontSizeValueSource (value)),
  347. title, 5.0, 40.0, 0.1, 0.5);
  348. }
  349. };
  350. };
  351. void AppearanceSettings::showGlobalPreferences (ScopedPointer<Component>& ownerPointer)
  352. {
  353. if (ownerPointer != nullptr)
  354. {
  355. ownerPointer->toFront (true);
  356. }
  357. else
  358. {
  359. new FloatingToolWindow ("Global Preferences",
  360. "globalPreferencesEditorPos",
  361. new GlobalPreferencesComponent,
  362. ownerPointer,
  363. 500, 500, 500, 500, 500, 500);
  364. }
  365. }
  366. //==============================================================================
  367. AppearanceSettingsTab::AppearanceSettingsTab()
  368. {
  369. if (getAppSettings().monospacedFontNames.size() == 0)
  370. content = new AppearanceEditor::FontScanPanel();
  371. else
  372. content = new AppearanceEditor::EditorPanel();
  373. changeContent (content);
  374. }
  375. Component* AppearanceSettingsTab::getContent()
  376. {
  377. return this;
  378. }
  379. void AppearanceSettingsTab::changeContent (Component* newContent)
  380. {
  381. content = newContent;
  382. addAndMakeVisible(content);
  383. content->setBoundsInset(BorderSize<int>());
  384. }
  385. String AppearanceSettingsTab::getName() const noexcept
  386. {
  387. return "Code Editor";
  388. }
  389. //==============================================================================
  390. GlobalPreferencesComponent::GlobalPreferencesComponent()
  391. : TabbedComponent (TabbedButtonBar::TabsAtTop)
  392. {
  393. preferenceTabs.add (new PathSettingsTab (DependencyPath::getThisOS()));
  394. preferenceTabs.add (new AppearanceSettingsTab);
  395. for (GlobalPreferencesTab** tab = preferenceTabs.begin(); tab != preferenceTabs.end(); ++tab)
  396. addTab ((*tab)->getName(), findColour(mainBackgroundColourId, true), (*tab)->getContent(), true);
  397. }