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.

136 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. class TextPropertyComponent::LabelComp : public Label,
  19. public FileDragAndDropTarget
  20. {
  21. public:
  22. LabelComp (TextPropertyComponent& tpc, const int charLimit, const bool multiline)
  23. : Label (String::empty, String::empty),
  24. owner (tpc),
  25. maxChars (charLimit),
  26. isMultiline (multiline)
  27. {
  28. setEditable (true, true, false);
  29. setColour (backgroundColourId, owner.findColour (TextPropertyComponent::backgroundColourId));
  30. setColour (outlineColourId, owner.findColour (TextPropertyComponent::outlineColourId));
  31. setColour (textColourId, owner.findColour (TextPropertyComponent::textColourId));
  32. }
  33. bool isInterestedInFileDrag (const StringArray&)
  34. {
  35. return true;
  36. }
  37. void filesDropped (const StringArray& files, int, int)
  38. {
  39. setText (getText() + files.joinIntoString (isMultiline ? "\n" : ", "), true);
  40. showEditor();
  41. }
  42. TextEditor* createEditorComponent()
  43. {
  44. TextEditor* const ed = Label::createEditorComponent();
  45. ed->setInputRestrictions (maxChars);
  46. if (isMultiline)
  47. {
  48. ed->setMultiLine (true, true);
  49. ed->setReturnKeyStartsNewLine (true);
  50. }
  51. return ed;
  52. }
  53. void textWasEdited()
  54. {
  55. owner.textWasEdited();
  56. }
  57. private:
  58. TextPropertyComponent& owner;
  59. int maxChars;
  60. bool isMultiline;
  61. };
  62. //==============================================================================
  63. TextPropertyComponent::TextPropertyComponent (const String& name,
  64. const int maxNumChars,
  65. const bool isMultiLine)
  66. : PropertyComponent (name)
  67. {
  68. createEditor (maxNumChars, isMultiLine);
  69. }
  70. TextPropertyComponent::TextPropertyComponent (const Value& valueToControl,
  71. const String& name,
  72. const int maxNumChars,
  73. const bool isMultiLine)
  74. : PropertyComponent (name)
  75. {
  76. createEditor (maxNumChars, isMultiLine);
  77. textEditor->getTextValue().referTo (valueToControl);
  78. }
  79. TextPropertyComponent::~TextPropertyComponent()
  80. {
  81. }
  82. void TextPropertyComponent::setText (const String& newText)
  83. {
  84. textEditor->setText (newText, true);
  85. }
  86. String TextPropertyComponent::getText() const
  87. {
  88. return textEditor->getText();
  89. }
  90. void TextPropertyComponent::createEditor (const int maxNumChars, const bool isMultiLine)
  91. {
  92. addAndMakeVisible (textEditor = new LabelComp (*this, maxNumChars, isMultiLine));
  93. if (isMultiLine)
  94. {
  95. textEditor->setJustificationType (Justification::topLeft);
  96. preferredHeight = 100;
  97. }
  98. }
  99. void TextPropertyComponent::refresh()
  100. {
  101. textEditor->setText (getText(), false);
  102. }
  103. void TextPropertyComponent::textWasEdited()
  104. {
  105. const String newText (textEditor->getText());
  106. if (getText() != newText)
  107. setText (newText);
  108. }