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.

123 lines
4.6KB

  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
  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 (globalKey, getValue().toString());
  39. }
  40. //==============================================================================
  41. DependencyPathPropertyComponent::DependencyPathPropertyComponent (const Value& value,
  42. const String& propertyName)
  43. try : TextPropertyComponent (propertyName, 1024, false),
  44. pathValue (value),
  45. pathValueSource (dynamic_cast<DependencyPathValueSource&> (pathValue.getValueSource()))
  46. {
  47. bool initialValueIsEmpty = ! pathValueSource.isUsingProjectSettings();
  48. getValue().referTo (pathValue);
  49. // the following step is necessary because the above referTo() has internally called setValue(),
  50. // which has set the project value to whatever is displayed in the label (this may be the
  51. // global/fallback value). In this case we have to reset the project value to blank:
  52. if (initialValueIsEmpty)
  53. getValue().setValue (String::empty);
  54. getValue().addListener (this);
  55. setColour (textColourId, getTextColourToDisplay());
  56. if (Label* label = dynamic_cast<Label*> (getChildComponent (0)))
  57. label->addListener (this);
  58. else
  59. jassertfalse;
  60. }
  61. catch (const std::bad_cast&)
  62. {
  63. // a DependencyPathPropertyComponent must be initialised with a Value
  64. // that is referring to a DependencyPathValueSource!
  65. jassertfalse;
  66. throw;
  67. }
  68. void DependencyPathPropertyComponent::valueChanged (Value& value)
  69. {
  70. // this callback handles the update of this setting in case
  71. // the user changed the global preferences.
  72. if (value.refersToSameSourceAs (pathValue) && pathValueSource.isUsingGlobalSettings())
  73. textWasEdited();
  74. }
  75. void DependencyPathPropertyComponent::textWasEdited()
  76. {
  77. setColour (textColourId, getTextColourToDisplay());
  78. TextPropertyComponent::textWasEdited();
  79. }
  80. Colour DependencyPathPropertyComponent::getTextColourToDisplay() const
  81. {
  82. if (! pathValueSource.isUsingProjectSettings())
  83. return pathValueSource.isValidPath() ? Colours::grey
  84. : Colours::lightpink;
  85. return pathValueSource.isValidPath() ? Colours::black
  86. : Colours::red;
  87. }
  88. void DependencyPathPropertyComponent::labelTextChanged (Label*)
  89. {
  90. }
  91. void DependencyPathPropertyComponent::editorShown (Label* /*label*/, TextEditor& editor)
  92. {
  93. if (! pathValueSource.isUsingProjectSettings())
  94. editor.setText (String::empty, dontSendNotification);
  95. }
  96. void DependencyPathPropertyComponent::editorHidden (Label*, TextEditor&)
  97. {
  98. }