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.

180 lines
6.4KB

  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. #pragma once
  18. //==============================================================================
  19. /** This ValueSource type implements the fallback logic required for dependency
  20. path settings: use the project exporter value; if this is empty, fall back to
  21. the global preference value; if the exporter is supposed to run on another
  22. OS and we don't know what the global preferences on that other machine are,
  23. fall back to a generic OS-specific fallback value.
  24. */
  25. class DependencyPathValueSource : public Value::ValueSource,
  26. private Value::Listener
  27. {
  28. public:
  29. DependencyPathValueSource (const Value& projectSettingsPath,
  30. Identifier globalSettingsKey,
  31. DependencyPathOS osThisSettingAppliesTo);
  32. /** This gets the currently used value, which may be either
  33. the project setting, the global setting, or the fallback value. */
  34. var getValue() const override
  35. {
  36. if (isUsingProjectSettings())
  37. return projectSettingsValue.getValue();
  38. if (isUsingGlobalSettings())
  39. return globalSettingsValue.getValue();
  40. return fallbackValue;
  41. }
  42. void setValue (const var& newValue) override
  43. {
  44. projectSettingsValue = newValue;
  45. if (isUsingProjectSettings())
  46. sendChangeMessage (false);
  47. }
  48. bool isUsingProjectSettings() const
  49. {
  50. return projectSettingsValueIsValid();
  51. }
  52. bool isUsingGlobalSettings() const
  53. {
  54. return ! projectSettingsValueIsValid() && globalSettingsValueIsValid();
  55. }
  56. bool isUsingFallbackValue() const
  57. {
  58. return ! projectSettingsValueIsValid() && !globalSettingsValueIsValid();
  59. }
  60. bool appliesToThisOS() const
  61. {
  62. return os == TargetOS::getThisOS();
  63. }
  64. bool isValidPath (const File& relativeTo) const;
  65. bool isValidPath() const;
  66. private:
  67. void valueChanged (Value& value) override
  68. {
  69. if ((value.refersToSameSourceAs (globalSettingsValue) && isUsingGlobalSettings()))
  70. {
  71. sendChangeMessage (true);
  72. setValue (String()); // make sure that the project-specific value is still blank
  73. }
  74. }
  75. /** This defines when to use the project setting, and when to
  76. consider it invalid and to fall back to the global setting or
  77. the fallback value. */
  78. bool projectSettingsValueIsValid() const
  79. {
  80. return ! projectSettingsValue.toString().isEmpty();
  81. }
  82. /** This defines when to use the global setting - given the project setting
  83. is invalid, and when to fall back to the fallback value instead. */
  84. bool globalSettingsValueIsValid() const
  85. {
  86. // only use the global settings if they are set on the same OS
  87. // that this setting is for!
  88. DependencyPathOS thisOS = TargetOS::getThisOS();
  89. return thisOS == TargetOS::unknown ? false : os == thisOS;
  90. }
  91. /** the dependency path setting as set in this Projucer project. */
  92. Value projectSettingsValue;
  93. /** the global key used in the application settings for the global setting value.
  94. needed for checking whether the path is valid. */
  95. Identifier globalKey;
  96. /** on what operating system should this dependency path be used?
  97. note that this is *not* the os that is targeted by the project,
  98. but rather the os on which the project will be compiled
  99. (= on which the path settings need to be set correctly). */
  100. DependencyPathOS os;
  101. /** the dependency path global setting on this machine.
  102. used when there value set for this project is invalid. */
  103. Value globalSettingsValue;
  104. /** the dependency path fallback setting. used instead of the global setting
  105. whenever the latter doesn't apply, e.g. the setting is for another
  106. OS than the ome this machine is running. */
  107. String fallbackValue;
  108. };
  109. //==============================================================================
  110. class DependencyPathPropertyComponent : public TextPropertyComponent,
  111. private Value::Listener,
  112. private Label::Listener
  113. {
  114. public:
  115. DependencyPathPropertyComponent (const File& pathRelativeToUse,
  116. const Value& value,
  117. const String& propertyName);
  118. private:
  119. /** This function defines what colour the label text should assume
  120. depending on the current state of the value the component tracks. */
  121. Colour getTextColourToDisplay() const;
  122. /** This function handles path changes because of user input. */
  123. void textWasEdited() override;
  124. /** This function handles path changes because the global path changed. */
  125. void valueChanged (Value& value) override;
  126. /** If the dependency path is relative, relative to which directory should
  127. we check if an object is available. */
  128. File pathRelativeTo;
  129. /** the value that represents this dependency path setting. */
  130. Value pathValue;
  131. /** a reference to the value source that this value refers to. */
  132. DependencyPathValueSource& pathValueSource;
  133. // Label::Listener overrides:
  134. void labelTextChanged (Label* labelThatHasChanged) override;
  135. void editorShown (Label*, TextEditor&) override;
  136. void editorHidden (Label*, TextEditor&) override;
  137. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DependencyPathPropertyComponent)
  138. };