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.

163 lines
5.7KB

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