forked from rorywalsh/cabbage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCabbageTextEditor.cpp
More file actions
executable file
·179 lines (146 loc) · 7.97 KB
/
CabbageTextEditor.cpp
File metadata and controls
executable file
·179 lines (146 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright (C) 2016 Rory Walsh
Cabbage is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Cabbage is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
*/
#include "CabbageTextEditor.h"
#include "CabbageCsoundConsole.h"
#include "../Audio/Plugins/CabbagePluginEditor.h"
CabbageTextEditor::CabbageTextEditor (ValueTree wData, CabbagePluginEditor* _owner)
: owner (_owner),
widgetData (wData),
textEditor (this),
CabbageWidgetBase(_owner)
{
isMultiline = int(CabbageWidgetData::getNumProp (wData, CabbageIdentifierIds::wrap)) == 0 ? false : true;
textEditor.setMultiLine(isMultiline);
setName(CabbageWidgetData::getStringProp(wData, CabbageIdentifierIds::name));
widgetData.addListener(this); //add listener to valueTree so it gets notified when a widget's property changes
initialiseCommonAttributes(this, wData); //initialise common attributes such as bounds, name, rotation, etc..
const int readOnly = CabbageWidgetData::getProperty (wData, CabbageIdentifierIds::readonly);
textEditor.setReadOnly(readOnly == 1 ? true : false);
int fontSize = CabbageWidgetData::getProperty (wData, CabbageIdentifierIds::fontsize);
const String font = CabbageWidgetData::getStringProp(wData, CabbageIdentifierIds::typeface);
if (font.isNotEmpty())
{
const String fontPath = File(getCsdFile()).getParentDirectory().getChildFile(font).getFullPathName();
if (File(fontPath).existsAsFile())
{
userFont = CabbageUtilities::getFontFromFile(File(fontPath));
userFont.setHeight(fontSize);
textEditor.setFont(userFont);
textEditor.applyFontToAllText(userFont);
}
}
else
{
if (owner->getCustomFontFile().existsAsFile())
{
userFont = CabbageUtilities::getFontFromFile(owner->getCustomFontFile());
userFont.setHeight(fontSize);
textEditor.setFont(userFont);
textEditor.applyFontToAllText(userFont);
}
}
textEditor.toggleEditOnDoubleClick = CabbageWidgetData::getNumProp (wData, CabbageIdentifierIds::doubleclicktogglesedit);
addAndMakeVisible (textEditor);
textEditor.setMultiLine (isMultiline);
const bool showScrollbars = CabbageWidgetData::getNumProp(wData, CabbageIdentifierIds::scrollbars);
textEditor.setScrollbarsShown (showScrollbars);
textEditor.addListener (this);
textEditor.addKeyListener (this);
textEditor.setColour (TextEditor::textColourId, Colour::fromString (CabbageWidgetData::getStringProp (wData, CabbageIdentifierIds::fontcolour)));
textEditor.setColour (TextEditor::backgroundColourId, Colour::fromString (CabbageWidgetData::getStringProp (wData, CabbageIdentifierIds::colour)));
textEditor.setColour (TextEditor::outlineColourId, Colour::fromString (CabbageWidgetData::getStringProp (wData, CabbageIdentifierIds::outlinecolour)));
textEditor.setColour (TextEditor::focusedOutlineColourId, Colour::fromString (CabbageWidgetData::getStringProp (wData, CabbageIdentifierIds::outlinecolour)));
textEditor.setColour (TextEditor::highlightColourId, Colour::fromString (CabbageWidgetData::getStringProp (wData, CabbageIdentifierIds::fontcolour)).contrasting (.5f));
textEditor.setColour(CaretComponent::ColourIds::caretColourId, Colour::fromString (CabbageWidgetData::getStringProp (wData, CabbageIdentifierIds::caretcolour)));
setName (CabbageWidgetData::getStringProp (wData, CabbageIdentifierIds::name));
widgetData.addListener (this); //add listener to valueTree so it gets notified when a widget's property changes
initialiseCommonAttributes (this, wData); //initialise common attributes such as bounds, name, rotation, etc..
const String filename = CabbageWidgetData::getStringProp(wData, CabbageIdentifierIds::file);
const File textFile(File::getCurrentWorkingDirectory().getChildFile(filename).getFullPathName());
if (textFile.existsAsFile())
textEditor.setText(textFile.loadFileAsString(), false);
else
textEditor.setText (getCurrentText(widgetData), dontSendNotification);
setWantsKeyboardFocus(false);
}
void CabbageTextEditor::textEditorReturnKeyPressed (TextEditor&)
{
if (!isMultiline)
sendTextToCsound();
else
textEditor.insertTextAtCaret ("\n");
}
void CabbageTextEditor::resized()
{
textEditor.setBounds (getLocalBounds());
}
void CabbageTextEditor::sendTextToCsound()
{
strings.add (textEditor.getText());
strings.removeDuplicates (false);
stringIndex = strings.size() - 1;
setWidgetText (textEditor.getText());
CabbageWidgetData::setStringProp (widgetData, CabbageIdentifierIds::text, textEditor.getText());
owner->sendChannelStringDataToCsound (getChannel(), textEditor.getText());
// textEditor.applyFontToAllText(userFont);
}
bool CabbageTextEditor::keyPressed (const juce::KeyPress& key, Component*)
{
// textEditor.applyFontToAllText(userFont);
if (!isMultiline)
{
if (key.getTextDescription().contains ("cursor up"))
{
textEditor.setText (strings[jmax (0, stringIndex--)]);
if (stringIndex < 1)
stringIndex = 0;
}
else if (key.getTextDescription().contains ("cursor down"))
{
textEditor.setText (strings[jmin (strings.size() - 1, stringIndex++)]);
if (stringIndex > strings.size() - 1)
stringIndex = strings.size() - 1;
}
}
else
{
if (key.getKeyCode() == KeyPress::returnKey && key.getModifiers().isCommandDown())
{
sendTextToCsound();
}
}
return false;
}
void CabbageTextEditor::valueTreePropertyChanged (ValueTree& valueTree, const Identifier& prop)
{
textEditor.setColour (TextEditor::textColourId, Colour::fromString (CabbageWidgetData::getStringProp (valueTree, CabbageIdentifierIds::fontcolour)));
textEditor.setColour (TextEditor::backgroundColourId, Colour::fromString (CabbageWidgetData::getStringProp (valueTree, CabbageIdentifierIds::colour)));
textEditor.setColour (TextEditor::outlineColourId, Colour::fromString (CabbageWidgetData::getStringProp (valueTree, CabbageIdentifierIds::outlinecolour)));
textEditor.setColour (TextEditor::focusedOutlineColourId, Colour::fromString (CabbageWidgetData::getStringProp (valueTree, CabbageIdentifierIds::outlinecolour)));
textEditor.setColour (TextEditor::highlightColourId, Colour::fromString (CabbageWidgetData::getStringProp (valueTree, CabbageIdentifierIds::fontcolour)).contrasting (.5f));
lookAndFeelChanged();
repaint();
handleCommonUpdates (this, valueTree, prop); //handle comon updates such as bounds, alpha, rotation, visible, etc
const String filename = CabbageWidgetData::getStringProp(valueTree, CabbageIdentifierIds::file);
const File textFile(File::getCurrentWorkingDirectory().getChildFile(filename).getFullPathName());
if (textFile.existsAsFile())
textEditor.setText(textFile.loadFileAsString(), false);
else
textEditor.setText (getCurrentText(valueTree), dontSendNotification);
const int readOnly = CabbageWidgetData::getProperty (valueTree, CabbageIdentifierIds::readonly);
textEditor.setReadOnly(readOnly == 1 ? true : false);
sendTextToCsound();
}