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.

109 lines
4.0KB

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