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.

287 lines
10KB

  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. #include "../jucer_Headers.h"
  20. #include "jucer_DependencyPathPropertyComponent.h"
  21. //==============================================================================
  22. DependencyPathValueSource::DependencyPathValueSource (const Value& projectSettingsPath,
  23. Identifier globalSettingsKey,
  24. DependencyPathOS osThisSettingAppliesTo)
  25. : projectSettingsValue (projectSettingsPath),
  26. globalKey (globalSettingsKey),
  27. os (osThisSettingAppliesTo),
  28. globalSettingsValue (getAppSettings().getStoredPath (globalKey)),
  29. fallbackValue (getAppSettings().getFallbackPathForOS (globalKey, os))
  30. {
  31. globalSettingsValue.addListener (this);
  32. fallbackValue.addListener (this);
  33. }
  34. bool DependencyPathValueSource::isValidPath (const File& relativeTo) const
  35. {
  36. // if we are on another OS than the one which this path setting is for,
  37. // we have no way of knowing whether the path is valid - so just assume it is:
  38. if (! appliesToThisOS())
  39. return true;
  40. return getAppSettings().isGlobalPathValid (relativeTo, globalKey, getValue().toString());
  41. }
  42. bool DependencyPathValueSource::isValidPath() const
  43. {
  44. return isValidPath (File::getCurrentWorkingDirectory());
  45. }
  46. //==============================================================================
  47. DependencyPathPropertyComponent::DependencyPathPropertyComponent (const File& pathRelativeToUse,
  48. const Value& value,
  49. const String& propertyName)
  50. try : TextPropertyComponent (propertyName, 1024, false),
  51. pathRelativeTo (pathRelativeToUse),
  52. pathValue (value),
  53. pathValueSource (dynamic_cast<DependencyPathValueSource&> (pathValue.getValueSource()))
  54. {
  55. bool initialValueIsEmpty = ! pathValueSource.isUsingProjectSettings();
  56. getValue().referTo (pathValue);
  57. // the following step is necessary because the above referTo() has internally called setValue(),
  58. // which has set the project value to whatever is displayed in the label (this may be the
  59. // global/fallback value). In this case we have to reset the project value to blank:
  60. if (initialValueIsEmpty)
  61. getValue().setValue (String());
  62. getValue().addListener (this);
  63. if (Label* label = dynamic_cast<Label*> (getChildComponent (0)))
  64. label->addListener (this);
  65. else
  66. jassertfalse;
  67. lookAndFeelChanged();
  68. }
  69. catch (const std::bad_cast&)
  70. {
  71. // a DependencyPathPropertyComponent must be initialised with a Value
  72. // that is referring to a DependencyPathValueSource!
  73. jassertfalse;
  74. throw;
  75. }
  76. void DependencyPathPropertyComponent::valueChanged (Value& value)
  77. {
  78. // this callback handles the update of this setting in case
  79. // the user changed the global preferences.
  80. if (value.refersToSameSourceAs (pathValue) && pathValueSource.isUsingGlobalSettings())
  81. textWasEdited();
  82. }
  83. void DependencyPathPropertyComponent::textWasEdited()
  84. {
  85. setColour (textColourId, getTextColourToDisplay());
  86. TextPropertyComponent::textWasEdited();
  87. }
  88. Colour DependencyPathPropertyComponent::getTextColourToDisplay() const
  89. {
  90. if (! pathValueSource.isUsingProjectSettings())
  91. return pathValueSource.isValidPath (pathRelativeTo) ? findColour (widgetTextColourId).withMultipliedAlpha (0.5f)
  92. : Colours::red.withMultipliedAlpha (0.5f);
  93. return pathValueSource.isValidPath (pathRelativeTo) ? findColour (widgetTextColourId)
  94. : Colours::red;
  95. }
  96. void DependencyPathPropertyComponent::labelTextChanged (Label*)
  97. {
  98. }
  99. void DependencyPathPropertyComponent::editorShown (Label* /*label*/, TextEditor& editor)
  100. {
  101. if (! pathValueSource.isUsingProjectSettings())
  102. editor.setText (String(), dontSendNotification);
  103. }
  104. void DependencyPathPropertyComponent::editorHidden (Label*, TextEditor&)
  105. {
  106. }
  107. void DependencyPathPropertyComponent::lookAndFeelChanged()
  108. {
  109. textWasEdited();
  110. }
  111. //==============================================================================
  112. DependencyFilePathPropertyComponent::DependencyFilePathPropertyComponent (Value& value,
  113. const String& propertyDescription,
  114. bool isDir,
  115. const String& wc,
  116. const File& rootToUseForRelativePaths)
  117. try : TextPropertyComponent (propertyDescription, 1024, false),
  118. pathRelativeTo (rootToUseForRelativePaths),
  119. pathValue (value),
  120. pathValueSource (dynamic_cast<DependencyPathValueSource&> (pathValue.getValueSource())),
  121. browseButton ("..."),
  122. isDirectory (isDir),
  123. wildcards (wc)
  124. {
  125. auto initialValueIsEmpty = ! pathValueSource.isUsingProjectSettings();
  126. getValue().referTo (pathValue);
  127. if (initialValueIsEmpty)
  128. getValue().setValue (String());
  129. getValue().addListener (this);
  130. if (auto* label = dynamic_cast<Label*> (getChildComponent (0)))
  131. label->addListener (this);
  132. else
  133. jassertfalse;
  134. setInterestedInFileDrag (false);
  135. addAndMakeVisible (browseButton);
  136. browseButton.addListener (this);
  137. lookAndFeelChanged();
  138. }
  139. catch (const std::bad_cast&)
  140. {
  141. // a DependencyPathPropertyComponent must be initialised with a Value
  142. // that is referring to a DependencyPathValueSource!
  143. jassertfalse;
  144. throw;
  145. }
  146. void DependencyFilePathPropertyComponent::resized()
  147. {
  148. auto bounds = getLookAndFeel().getPropertyComponentContentPosition (*this);
  149. browseButton.setBounds (bounds.removeFromRight (30));
  150. getChildComponent (0)->setBounds (bounds);
  151. }
  152. void DependencyFilePathPropertyComponent::paintOverChildren (Graphics& g)
  153. {
  154. if (highlightForDragAndDrop)
  155. {
  156. g.setColour (findColour (defaultHighlightColourId).withAlpha (0.5f));
  157. g.fillRect (getChildComponent (0)->getBounds());
  158. }
  159. }
  160. void DependencyFilePathPropertyComponent::filesDropped (const StringArray& files, int, int)
  161. {
  162. const File firstFile (files[0]);
  163. if (isDirectory)
  164. setTo (firstFile.isDirectory() ? firstFile
  165. : firstFile.getParentDirectory());
  166. else
  167. setTo (firstFile);
  168. highlightForDragAndDrop = false;
  169. }
  170. void DependencyFilePathPropertyComponent::setTo (const File& f)
  171. {
  172. pathValue = (pathRelativeTo == File()) ? f.getFullPathName()
  173. : f.getRelativePathFrom (pathRelativeTo);
  174. textWasEdited();
  175. }
  176. void DependencyFilePathPropertyComponent::enablementChanged()
  177. {
  178. getValue().referTo (isEnabled() ? pathValue
  179. : pathValueSource.appliesToThisOS() ? pathValueSource.getGlobalSettingsValue()
  180. : pathValueSource.getFallbackSettingsValue());
  181. textWasEdited();
  182. repaint();
  183. }
  184. void DependencyFilePathPropertyComponent::textWasEdited()
  185. {
  186. setColour (textColourId, getTextColourToDisplay());
  187. TextPropertyComponent::textWasEdited();
  188. }
  189. void DependencyFilePathPropertyComponent::valueChanged (Value& value)
  190. {
  191. if ((value.refersToSameSourceAs (pathValue) && pathValueSource.isUsingGlobalSettings())
  192. || value.refersToSameSourceAs (pathValueSource.getGlobalSettingsValue()))
  193. textWasEdited();
  194. }
  195. void DependencyFilePathPropertyComponent::editorShown (Label*, TextEditor& editor)
  196. {
  197. if (! pathValueSource.isUsingProjectSettings())
  198. editor.setText (String(), dontSendNotification);
  199. }
  200. void DependencyFilePathPropertyComponent::buttonClicked (Button*)
  201. {
  202. auto currentFile = pathRelativeTo.getChildFile (pathValue.toString());
  203. if (isDirectory)
  204. {
  205. FileChooser chooser ("Select directory", currentFile);
  206. if (chooser.browseForDirectory())
  207. setTo (chooser.getResult());
  208. }
  209. else
  210. {
  211. FileChooser chooser ("Select file", currentFile, wildcards);
  212. if (chooser.browseForFileToOpen())
  213. setTo (chooser.getResult());
  214. }
  215. }
  216. Colour DependencyFilePathPropertyComponent::getTextColourToDisplay() const
  217. {
  218. auto alpha = 1.0f;
  219. auto key = pathValueSource.getKey();
  220. const auto& globalSettingsValue = pathValueSource.getGlobalSettingsValue();
  221. if (! pathValueSource.isUsingProjectSettings() && isEnabled())
  222. alpha = 0.5f;
  223. if ((key == Ids::defaultUserModulePath && getValue().toString().contains (";")) || ! pathValueSource.appliesToThisOS())
  224. return findColour (widgetTextColourId).withMultipliedAlpha (alpha);
  225. auto usingGlobalPath = (getValue().refersToSameSourceAs (globalSettingsValue));
  226. auto isValidPath = getAppSettings().isGlobalPathValid (pathRelativeTo, key,
  227. (usingGlobalPath ? globalSettingsValue : pathValue).toString());
  228. return isValidPath ? findColour (widgetTextColourId).withMultipliedAlpha (alpha)
  229. : Colours::red.withMultipliedAlpha (alpha);
  230. }