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.

184 lines
6.5KB

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