1+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+ /*
4+ Part of the Processing project - http://processing.org
5+ Copyright (c) 2012-16 The Processing Foundation
6+
7+ This program is free software; you can redistribute it and/or modify
8+ it under the terms of the GNU General Public License version 2
9+ as published by the Free Software Foundation.
10+
11+ This program is distributed in the hope that it will be useful,
12+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+ GNU General Public License for more details.
15+
16+ You should have received a copy of the GNU General Public License
17+ along with this program; if not, write to the Free Software Foundation, Inc.
18+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+ */
20+
21+ package processing .app .syntax ;
22+
23+ import java .awt .Cursor ;
24+ import java .awt .Image ;
25+ import java .awt .event .MouseEvent ;
26+ import java .awt .event .MouseMotionAdapter ;
27+ import java .util .HashMap ;
28+ import java .util .Map ;
29+
30+ import processing .app .Mode ;
31+ import processing .app .ui .Editor ;
32+
33+
34+ /**
35+ * Extensions to JEditTextArea to for the PDE. These were moved out of
36+ * JavaTextArea because they were not Java-specific and would be helpful
37+ * for other Mode implementations.
38+ */
39+ public class PdeTextArea extends JEditTextArea {
40+ protected final Editor editor ;
41+
42+ protected Image gutterGradient ;
43+
44+ /// the text marker for highlighting breakpoints in the gutter
45+ static public final String BREAK_MARKER = "<>" ;
46+ /// the text marker for highlighting the current line in the gutter
47+ static public final String STEP_MARKER = "->" ;
48+
49+ /// maps line index to gutter text
50+ protected final Map <Integer , String > gutterText = new HashMap <>();
51+
52+
53+ public PdeTextArea (TextAreaDefaults defaults , InputHandler inputHandler ,
54+ Editor editor ) {
55+ super (defaults , inputHandler );
56+ this .editor = editor ;
57+
58+ // change cursor to pointer in the gutter area
59+ painter .addMouseMotionListener (gutterCursorMouseAdapter );
60+
61+ add (CENTER , painter );
62+
63+ // load settings from theme.txt
64+ Mode mode = editor .getMode ();
65+ gutterGradient = mode .makeGradient ("editor" , Editor .LEFT_GUTTER , 500 );
66+ }
67+
68+
69+ public Image getGutterGradient () {
70+ return gutterGradient ;
71+ }
72+
73+
74+ public void setMode (Mode mode ) {
75+ ((PdeTextAreaPainter ) painter ).setMode (mode );
76+ }
77+
78+
79+ /**
80+ * Set the gutter text of a specific line.
81+ *
82+ * @param lineIdx
83+ * the line index (0-based)
84+ * @param text
85+ * the text
86+ */
87+ public void setGutterText (int lineIdx , String text ) {
88+ gutterText .put (lineIdx , text );
89+ painter .invalidateLine (lineIdx );
90+ }
91+
92+
93+ /**
94+ * Clear the gutter text of a specific line.
95+ *
96+ * @param lineIdx
97+ * the line index (0-based)
98+ */
99+ public void clearGutterText (int lineIdx ) {
100+ gutterText .remove (lineIdx );
101+ painter .invalidateLine (lineIdx );
102+ }
103+
104+
105+ /**
106+ * Clear all gutter text.
107+ */
108+ public void clearGutterText () {
109+ for (int lineIdx : gutterText .keySet ()) {
110+ painter .invalidateLine (lineIdx );
111+ }
112+ gutterText .clear ();
113+ }
114+
115+
116+ /**
117+ * Retrieve the gutter text of a specific line.
118+ * @param lineIdx the line index (0-based)
119+ * @return the gutter text
120+ */
121+ public String getGutterText (int lineIdx ) {
122+ return gutterText .get (lineIdx );
123+ }
124+
125+
126+ /**
127+ * Convert a character offset to a horizontal pixel position inside
128+ * the text area. Overridden to take gutter width into account.
129+ * @param line the 0-based line number
130+ * @param offset the character offset (0 is the first character on a line)
131+ * @return the horizontal position
132+ */
133+ @ Override
134+ public int _offsetToX (int line , int offset ) {
135+ return super ._offsetToX (line , offset ) + Editor .LEFT_GUTTER ;
136+ }
137+
138+
139+ /**
140+ * Convert a horizontal pixel position to a character offset. Overridden to
141+ * take gutter width into account.
142+ * @param line the 0-based line number
143+ * @param x the horizontal pixel position
144+ * @return he character offset (0 is the first character on a line)
145+ */
146+ @ Override
147+ public int xToOffset (int line , int x ) {
148+ return super .xToOffset (line , x - Editor .LEFT_GUTTER );
149+ }
150+
151+
152+ /**
153+ * Sets default cursor (instead of text cursor) in the gutter area.
154+ */
155+ protected final MouseMotionAdapter gutterCursorMouseAdapter = new MouseMotionAdapter () {
156+ private int lastX ; // previous horizontal position of the mouse cursor
157+
158+ @ Override
159+ public void mouseMoved (MouseEvent me ) {
160+ if (me .getX () < Editor .LEFT_GUTTER ) {
161+ if (lastX >= Editor .LEFT_GUTTER ) {
162+ painter .setCursor (new Cursor (Cursor .DEFAULT_CURSOR ));
163+ }
164+ } else {
165+ if (lastX < Editor .LEFT_GUTTER ) {
166+ painter .setCursor (new Cursor (Cursor .TEXT_CURSOR ));
167+ }
168+ }
169+ lastX = me .getX ();
170+ }
171+ };
172+
173+
174+ public Editor getEditor () {
175+ return editor ;
176+ }
177+ }
0 commit comments