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.

138 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../jucer_Headers.h"
  20. #include "jucer_DependencyPathPropertyComponent.h"
  21. #include "../Application/jucer_GlobalPreferences.h"
  22. //==============================================================================
  23. DependencyPathValueSource::DependencyPathValueSource (const Value& projectSettingsPath,
  24. Identifier globalSettingsKey,
  25. DependencyPathOS osThisSettingAppliesTo)
  26. : projectSettingsValue (projectSettingsPath),
  27. globalKey (globalSettingsKey),
  28. os (osThisSettingAppliesTo),
  29. globalSettingsValue (getAppSettings().getGlobalPath (globalKey, os)),
  30. fallbackValue (getAppSettings().getFallbackPath (globalKey, os))
  31. {
  32. globalSettingsValue.addListener (this);
  33. }
  34. bool DependencyPathValueSource::isValidPath (const File& relativeTo) const
  35. {
  36. // if we are on another OS than the one which this path setting is for,
  37. // we have no way of knowing whether the path is valid - so just assume it is:
  38. if (! appliesToThisOS())
  39. return true;
  40. return getAppSettings().isGlobalPathValid (relativeTo, globalKey, getValue().toString());
  41. }
  42. bool DependencyPathValueSource::isValidPath() const
  43. {
  44. return isValidPath (File::getCurrentWorkingDirectory());
  45. }
  46. //==============================================================================
  47. DependencyPathPropertyComponent::DependencyPathPropertyComponent (const File& pathRelativeToUse,
  48. const Value& value,
  49. const String& propertyName)
  50. try : TextPropertyComponent (propertyName, 1024, false),
  51. pathRelativeTo (pathRelativeToUse),
  52. pathValue (value),
  53. pathValueSource (dynamic_cast<DependencyPathValueSource&> (pathValue.getValueSource()))
  54. {
  55. bool initialValueIsEmpty = ! pathValueSource.isUsingProjectSettings();
  56. getValue().referTo (pathValue);
  57. // the following step is necessary because the above referTo() has internally called setValue(),
  58. // which has set the project value to whatever is displayed in the label (this may be the
  59. // global/fallback value). In this case we have to reset the project value to blank:
  60. if (initialValueIsEmpty)
  61. getValue().setValue (String());
  62. getValue().addListener (this);
  63. if (Label* label = dynamic_cast<Label*> (getChildComponent (0)))
  64. label->addListener (this);
  65. else
  66. jassertfalse;
  67. lookAndFeelChanged();
  68. }
  69. catch (const std::bad_cast&)
  70. {
  71. // a DependencyPathPropertyComponent must be initialised with a Value
  72. // that is referring to a DependencyPathValueSource!
  73. jassertfalse;
  74. throw;
  75. }
  76. void DependencyPathPropertyComponent::valueChanged (Value& value)
  77. {
  78. // this callback handles the update of this setting in case
  79. // the user changed the global preferences.
  80. if (value.refersToSameSourceAs (pathValue) && pathValueSource.isUsingGlobalSettings())
  81. textWasEdited();
  82. }
  83. void DependencyPathPropertyComponent::textWasEdited()
  84. {
  85. setColour (textColourId, getTextColourToDisplay());
  86. TextPropertyComponent::textWasEdited();
  87. }
  88. Colour DependencyPathPropertyComponent::getTextColourToDisplay() const
  89. {
  90. if (! pathValueSource.isUsingProjectSettings())
  91. return pathValueSource.isValidPath (pathRelativeTo) ? findColour (widgetTextColourId).withMultipliedAlpha (0.5f)
  92. : Colours::red.withMultipliedAlpha (0.5f);
  93. return pathValueSource.isValidPath (pathRelativeTo) ? findColour (widgetTextColourId)
  94. : Colours::red;
  95. }
  96. void DependencyPathPropertyComponent::labelTextChanged (Label*)
  97. {
  98. }
  99. void DependencyPathPropertyComponent::editorShown (Label* /*label*/, TextEditor& editor)
  100. {
  101. if (! pathValueSource.isUsingProjectSettings())
  102. editor.setText (String(), dontSendNotification);
  103. }
  104. void DependencyPathPropertyComponent::editorHidden (Label*, TextEditor&)
  105. {
  106. }
  107. void DependencyPathPropertyComponent::lookAndFeelChanged()
  108. {
  109. textWasEdited();
  110. }