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.

130 lines
4.9KB

  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_DependencyPathPropertyComponent.h"
  19. #include "../Application/jucer_GlobalPreferences.h"
  20. //==============================================================================
  21. DependencyPathValueSource::DependencyPathValueSource (const Value& projectSettingsPath,
  22. Identifier globalSettingsKey,
  23. DependencyPathOS osThisSettingAppliesTo)
  24. : projectSettingsValue (projectSettingsPath),
  25. globalKey (globalSettingsKey),
  26. os (osThisSettingAppliesTo),
  27. globalSettingsValue (getAppSettings().getGlobalPath (globalKey, os)),
  28. fallbackValue (getAppSettings().getFallbackPath (globalKey, os))
  29. {
  30. globalSettingsValue.addListener (this);
  31. }
  32. bool DependencyPathValueSource::isValidPath (const File& relativeTo) const
  33. {
  34. // if we are on another OS than the one which this path setting is for,
  35. // we have no way of knowing whether the path is valid - so just assume it is:
  36. if (! appliesToThisOS())
  37. return true;
  38. return getAppSettings().isGlobalPathValid (relativeTo, globalKey, getValue().toString());
  39. }
  40. bool DependencyPathValueSource::isValidPath() const
  41. {
  42. return isValidPath (File::getCurrentWorkingDirectory());
  43. }
  44. //==============================================================================
  45. DependencyPathPropertyComponent::DependencyPathPropertyComponent (const File& pathRelativeToUse,
  46. const Value& value,
  47. const String& propertyName)
  48. try : TextPropertyComponent (propertyName, 1024, false),
  49. pathRelativeTo (pathRelativeToUse),
  50. pathValue (value),
  51. pathValueSource (dynamic_cast<DependencyPathValueSource&> (pathValue.getValueSource()))
  52. {
  53. bool initialValueIsEmpty = ! pathValueSource.isUsingProjectSettings();
  54. getValue().referTo (pathValue);
  55. // the following step is necessary because the above referTo() has internally called setValue(),
  56. // which has set the project value to whatever is displayed in the label (this may be the
  57. // global/fallback value). In this case we have to reset the project value to blank:
  58. if (initialValueIsEmpty)
  59. getValue().setValue (String());
  60. getValue().addListener (this);
  61. setColour (textColourId, getTextColourToDisplay());
  62. if (Label* label = dynamic_cast<Label*> (getChildComponent (0)))
  63. label->addListener (this);
  64. else
  65. jassertfalse;
  66. }
  67. catch (const std::bad_cast&)
  68. {
  69. // a DependencyPathPropertyComponent must be initialised with a Value
  70. // that is referring to a DependencyPathValueSource!
  71. jassertfalse;
  72. throw;
  73. }
  74. void DependencyPathPropertyComponent::valueChanged (Value& value)
  75. {
  76. // this callback handles the update of this setting in case
  77. // the user changed the global preferences.
  78. if (value.refersToSameSourceAs (pathValue) && pathValueSource.isUsingGlobalSettings())
  79. textWasEdited();
  80. }
  81. void DependencyPathPropertyComponent::textWasEdited()
  82. {
  83. setColour (textColourId, getTextColourToDisplay());
  84. TextPropertyComponent::textWasEdited();
  85. }
  86. Colour DependencyPathPropertyComponent::getTextColourToDisplay() const
  87. {
  88. if (! pathValueSource.isUsingProjectSettings())
  89. return pathValueSource.isValidPath (pathRelativeTo) ? Colours::grey
  90. : Colours::lightpink;
  91. return pathValueSource.isValidPath (pathRelativeTo) ? Colours::black
  92. : Colours::red;
  93. }
  94. void DependencyPathPropertyComponent::labelTextChanged (Label*)
  95. {
  96. }
  97. void DependencyPathPropertyComponent::editorShown (Label* /*label*/, TextEditor& editor)
  98. {
  99. if (! pathValueSource.isUsingProjectSettings())
  100. editor.setText (String(), dontSendNotification);
  101. }
  102. void DependencyPathPropertyComponent::editorHidden (Label*, TextEditor&)
  103. {
  104. }