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