author | wenzelm |
Sat, 09 Nov 2024 16:34:14 +0100 | |
changeset 81412 | 4794576828df |
parent 81401 | 874e12fb6c3d |
child 81417 | 964b85e04f1f |
permissions | -rw-r--r-- |
49406 | 1 |
/* Title: Tools/jEdit/src/jedit_lib.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Misc library functions for jEdit. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.jedit |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
65469 | 12 |
import java.io.{File => JFile} |
72899 | 13 |
import java.awt.{Component, Container, GraphicsEnvironment, Point, Rectangle, Dimension, Toolkit} |
53784 | 14 |
import java.awt.event.{InputEvent, KeyEvent, KeyListener} |
53247
bd595338ca18
uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents:
53237
diff
changeset
|
15 |
import javax.swing.{Icon, ImageIcon, JWindow, SwingUtilities} |
49710
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
16 |
|
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56774
diff
changeset
|
17 |
import scala.util.parsing.input.CharSequenceReader |
73354 | 18 |
import scala.jdk.CollectionConverters._ |
49406 | 19 |
|
62264
340f98836fd9
re-init document views for the sake of Text_Overview size;
wenzelm
parents:
61865
diff
changeset
|
20 |
import org.gjt.sp.jedit.{jEdit, Buffer, View, GUIUtilities, Debug, EditPane} |
72927
69f768aff611
clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents:
72899
diff
changeset
|
21 |
import org.gjt.sp.jedit.io.{FileVFS, VFSManager} |
69f768aff611
clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents:
72899
diff
changeset
|
22 |
import org.gjt.sp.jedit.gui.{KeyEventWorkaround, KeyEventTranslator} |
61192 | 23 |
import org.gjt.sp.jedit.buffer.{JEditBuffer, LineManager} |
81299 | 24 |
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaPainter, Selection} |
49406 | 25 |
|
26 |
||
75393 | 27 |
object JEdit_Lib { |
71861 | 28 |
/* jEdit directories */ |
29 |
||
30 |
def directories: List[JFile] = |
|
31 |
(Option(jEdit.getSettingsDirectory).toList ::: List(jEdit.getJEditHome)).map(new JFile(_)) |
|
32 |
||
33 |
||
53019 | 34 |
/* window geometry measurement */ |
35 |
||
36 |
private lazy val dummy_window = new JWindow |
|
37 |
||
75393 | 38 |
final case class Window_Geometry(width: Int, height: Int, inner_width: Int, inner_height: Int) { |
53019 | 39 |
def deco_width: Int = width - inner_width |
40 |
def deco_height: Int = height - inner_height |
|
41 |
} |
|
42 |
||
75393 | 43 |
def window_geometry(outer: Container, inner: Component): Window_Geometry = { |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
56930
diff
changeset
|
44 |
GUI_Thread.require {} |
53019 | 45 |
|
46 |
val old_content = dummy_window.getContentPane |
|
47 |
||
48 |
dummy_window.setContentPane(outer) |
|
81401 | 49 |
dummy_window.pack() |
73367 | 50 |
dummy_window.revalidate() |
53019 | 51 |
|
52 |
val geometry = |
|
53 |
Window_Geometry( |
|
54 |
dummy_window.getWidth, dummy_window.getHeight, inner.getWidth, inner.getHeight) |
|
55 |
||
56 |
dummy_window.setContentPane(old_content) |
|
57 |
||
58 |
geometry |
|
59 |
} |
|
60 |
||
61 |
||
76782 | 62 |
/* plain files */ |
65469 | 63 |
|
64 |
def is_file(name: String): Boolean = |
|
76782 | 65 |
name != null && name.nonEmpty && VFSManager.getVFSForPath(name).isInstanceOf[FileVFS] |
65469 | 66 |
|
67 |
def check_file(name: String): Option[JFile] = |
|
68 |
if (is_file(name)) Some(new JFile(name)) else None |
|
69 |
||
70 |
||
49406 | 71 |
/* buffers */ |
72 |
||
73 |
def buffer_text(buffer: JEditBuffer): String = |
|
74 |
buffer_lock(buffer) { buffer.getText(0, buffer.getLength) } |
|
75 |
||
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56774
diff
changeset
|
76 |
def buffer_reader(buffer: JEditBuffer): CharSequenceReader = |
64824 | 77 |
Scan.char_reader(buffer.getSegment(0, buffer.getLength)) |
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56774
diff
changeset
|
78 |
|
75393 | 79 |
def buffer_mode(buffer: JEditBuffer): String = { |
53274
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
80 |
val mode = buffer.getMode |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
81 |
if (mode == null) "" |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
82 |
else { |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
83 |
val name = mode.getName |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
84 |
if (name == null) "" else name |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
85 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
86 |
} |
1760c01f1c78
maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents:
53247
diff
changeset
|
87 |
|
65469 | 88 |
def buffer_line_manager(buffer: JEditBuffer): LineManager = |
89 |
Untyped.get[LineManager](buffer, "lineMgr") |
|
90 |
||
49406 | 91 |
def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath |
92 |
||
65469 | 93 |
def buffer_file(buffer: Buffer): Option[JFile] = check_file(buffer_name(buffer)) |
61192 | 94 |
|
75393 | 95 |
def buffer_undo_in_progress[A](buffer: JEditBuffer, body: => A): A = { |
68060
3931ed905e93
avoid output showing up in kill ring (via TextArea.setText, JEditBuffer.remove, UndoManager.contentRemoved), e.g. relevant for action "paste-deleted";
wenzelm
parents:
67014
diff
changeset
|
96 |
val undo_in_progress = buffer.isUndoInProgress |
73340 | 97 |
def set(b: Boolean): Unit = Untyped.set[Boolean](buffer, "undoInProgress", b) |
68060
3931ed905e93
avoid output showing up in kill ring (via TextArea.setText, JEditBuffer.remove, UndoManager.contentRemoved), e.g. relevant for action "paste-deleted";
wenzelm
parents:
67014
diff
changeset
|
98 |
try { set(true); body } finally { set(undo_in_progress) } |
3931ed905e93
avoid output showing up in kill ring (via TextArea.setText, JEditBuffer.remove, UndoManager.contentRemoved), e.g. relevant for action "paste-deleted";
wenzelm
parents:
67014
diff
changeset
|
99 |
} |
3931ed905e93
avoid output showing up in kill ring (via TextArea.setText, JEditBuffer.remove, UndoManager.contentRemoved), e.g. relevant for action "paste-deleted";
wenzelm
parents:
67014
diff
changeset
|
100 |
|
49406 | 101 |
|
102 |
/* main jEdit components */ |
|
103 |
||
73354 | 104 |
def jedit_buffers(): Iterator[Buffer] = |
105 |
jEdit.getBufferManager().getBuffers().asScala.iterator |
|
49406 | 106 |
|
107 |
def jedit_buffer(name: String): Option[Buffer] = |
|
108 |
jedit_buffers().find(buffer => buffer_name(buffer) == name) |
|
109 |
||
56457
eea4bbe15745
tuned signature -- prefer static type Document.Node.Name;
wenzelm
parents:
55812
diff
changeset
|
110 |
def jedit_buffer(name: Document.Node.Name): Option[Buffer] = |
eea4bbe15745
tuned signature -- prefer static type Document.Node.Name;
wenzelm
parents:
55812
diff
changeset
|
111 |
jedit_buffer(name.node) |
eea4bbe15745
tuned signature -- prefer static type Document.Node.Name;
wenzelm
parents:
55812
diff
changeset
|
112 |
|
73354 | 113 |
def jedit_views(): Iterator[View] = |
114 |
jEdit.getViewManager().getViews().asScala.iterator |
|
49406 | 115 |
|
69636 | 116 |
def jedit_view(view: View = null): View = |
117 |
if (view == null) jEdit.getActiveView() else view |
|
118 |
||
62264
340f98836fd9
re-init document views for the sake of Text_Overview size;
wenzelm
parents:
61865
diff
changeset
|
119 |
def jedit_edit_panes(view: View): Iterator[EditPane] = |
340f98836fd9
re-init document views for the sake of Text_Overview size;
wenzelm
parents:
61865
diff
changeset
|
120 |
if (view == null) Iterator.empty |
340f98836fd9
re-init document views for the sake of Text_Overview size;
wenzelm
parents:
61865
diff
changeset
|
121 |
else view.getEditPanes().iterator.filter(_ != null) |
340f98836fd9
re-init document views for the sake of Text_Overview size;
wenzelm
parents:
61865
diff
changeset
|
122 |
|
49406 | 123 |
def jedit_text_areas(view: View): Iterator[JEditTextArea] = |
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
124 |
if (view == null) Iterator.empty |
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
125 |
else view.getEditPanes().iterator.filter(_ != null).map(_.getTextArea).filter(_ != null) |
49406 | 126 |
|
127 |
def jedit_text_areas(): Iterator[JEditTextArea] = |
|
71601 | 128 |
jedit_views().flatMap(jedit_text_areas) |
49406 | 129 |
|
130 |
def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] = |
|
131 |
jedit_text_areas().filter(_.getBuffer == buffer) |
|
132 |
||
75393 | 133 |
def buffer_lock[A](buffer: JEditBuffer)(body: => A): A = { |
49406 | 134 |
try { buffer.readLock(); body } |
135 |
finally { buffer.readUnlock() } |
|
136 |
} |
|
49407 | 137 |
|
75393 | 138 |
def buffer_edit[A](buffer: JEditBuffer)(body: => A): A = { |
50195
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
139 |
try { buffer.beginCompoundEdit(); body } |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
140 |
finally { buffer.endCompoundEdit() } |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
141 |
} |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
142 |
|
49407 | 143 |
|
50215 | 144 |
/* get text */ |
145 |
||
67014 | 146 |
def get_text(buffer: JEditBuffer, range: Text.Range): Option[String] = |
50215 | 147 |
try { Some(buffer.getText(range.start, range.length)) } |
148 |
catch { case _: ArrayIndexOutOfBoundsException => None } |
|
149 |
||
150 |
||
49407 | 151 |
/* point range */ |
152 |
||
153 |
def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range = |
|
56592
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
154 |
if (offset < 0) Text.Range.offside |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
155 |
else |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
156 |
buffer_lock(buffer) { |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
157 |
def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
158 |
try { |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
159 |
val c = text(offset) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
160 |
if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1))) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
161 |
Text.Range(offset, offset + 2) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
162 |
else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1))) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
163 |
Text.Range(offset - 1, offset + 1) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
164 |
else |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
165 |
Text.Range(offset, offset + 1) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
166 |
} |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
167 |
catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) } |
49407 | 168 |
} |
49408 | 169 |
|
170 |
||
55812 | 171 |
/* text ranges */ |
172 |
||
173 |
def buffer_range(buffer: JEditBuffer): Text.Range = |
|
174 |
Text.Range(0, buffer.getLength) |
|
49408 | 175 |
|
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
176 |
def line_range(buffer: JEditBuffer, line: Int): Text.Range = |
56589
71c5d1f516c0
more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents:
56587
diff
changeset
|
177 |
Text.Range(buffer.getLineStartOffset(line), buffer.getLineEndOffset(line) min buffer.getLength) |
56587
83777a91f5de
clarified before_caret_range: prevent continuation on next line;
wenzelm
parents:
56574
diff
changeset
|
178 |
|
56592
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
179 |
def caret_range(text_area: TextArea): Text.Range = |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
180 |
point_range(text_area.getBuffer, text_area.getCaretPosition) |
5157f7615e99
prefer direct caret_range for update_dictionary actions, which usually happen outside the flow of editing;
wenzelm
parents:
56589
diff
changeset
|
181 |
|
81299 | 182 |
def selection_ranges(text_area: TextArea): List[Text.Range] = { |
183 |
val buffer = text_area.getBuffer |
|
184 |
text_area.getSelection.toList.flatMap( |
|
185 |
{ |
|
186 |
case rect: Selection.Rect => |
|
187 |
List.from( |
|
188 |
for { |
|
189 |
l <- (rect.getStartLine to rect.getEndLine).iterator |
|
190 |
r = Text.Range(rect.getStart(buffer, l), rect.getEnd(buffer, l)) |
|
191 |
if !r.is_singularity |
|
192 |
} yield r) |
|
193 |
case sel: Selection => List(Text.Range(sel.getStart, sel.getEnd)) |
|
194 |
}) |
|
195 |
} |
|
196 |
||
75393 | 197 |
def visible_range(text_area: TextArea): Option[Text.Range] = { |
49408 | 198 |
val buffer = text_area.getBuffer |
199 |
val n = text_area.getVisibleLines |
|
200 |
if (n > 0) { |
|
201 |
val start = text_area.getScreenLineStartOffset(0) |
|
202 |
val raw_end = text_area.getScreenLineEndOffset(n - 1) |
|
49843
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
203 |
val end = if (raw_end >= 0) raw_end min buffer.getLength else buffer.getLength |
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
204 |
Some(Text.Range(start, end)) |
49408 | 205 |
} |
206 |
else None |
|
207 |
} |
|
208 |
||
75393 | 209 |
def invalidate_range(text_area: TextArea, range: Text.Range): Unit = { |
49408 | 210 |
val buffer = text_area.getBuffer |
55812 | 211 |
buffer_range(buffer).try_restrict(range) match { |
212 |
case Some(range1) if !range1.is_singularity => |
|
56699 | 213 |
try { |
214 |
text_area.invalidateLineRange( |
|
215 |
buffer.getLineOfOffset(range1.start), |
|
216 |
buffer.getLineOfOffset(range1.stop)) |
|
217 |
} |
|
218 |
catch { case _: ArrayIndexOutOfBoundsException => } |
|
55812 | 219 |
case _ => |
220 |
} |
|
49408 | 221 |
} |
49409 | 222 |
|
80554 | 223 |
def invalidate_screen(text_area: TextArea, start: Int = -1, end: Int = -1): Unit = { |
62986 | 224 |
val visible_lines = text_area.getVisibleLines |
80551 | 225 |
if (visible_lines > 0) { |
80554 | 226 |
val start_line = if (start >= 0) start else 0 |
227 |
val end_line = if (end >= 0) end else visible_lines |
|
228 |
text_area.invalidateScreenLineRange(start_line, end_line) |
|
80551 | 229 |
} |
62986 | 230 |
} |
231 |
||
49409 | 232 |
|
233 |
/* graphics range */ |
|
234 |
||
81340 | 235 |
def font_metric(painter: TextAreaPainter): Font_Metric = |
81412
4794576828df
clarified signature: include standard margin in object equality;
wenzelm
parents:
81401
diff
changeset
|
236 |
new Font_Metric( |
4794576828df
clarified signature: include standard margin in object equality;
wenzelm
parents:
81401
diff
changeset
|
237 |
font = painter.getFont, |
4794576828df
clarified signature: include standard margin in object equality;
wenzelm
parents:
81401
diff
changeset
|
238 |
context = painter.getFontRenderContext, |
4794576828df
clarified signature: include standard margin in object equality;
wenzelm
parents:
81401
diff
changeset
|
239 |
standard_margin = (average_width: Double) => (painter.getWidth.toDouble / average_width).toInt) |
81340 | 240 |
|
60215 | 241 |
case class Gfx_Range(x: Int, y: Int, length: Int) |
49409 | 242 |
|
49843
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
243 |
// NB: jEdit always normalizes \r\n and \r to \n |
49409 | 244 |
// NB: last line lacks \n |
75393 | 245 |
def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] = { |
81340 | 246 |
val metric = font_metric(text_area.getPainter) |
247 |
val char_width = metric.average_width.round.toInt |
|
50849 | 248 |
|
49409 | 249 |
val buffer = text_area.getBuffer |
250 |
||
251 |
val end = buffer.getLength |
|
252 |
val stop = range.stop |
|
51078
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
253 |
|
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
254 |
val (p, q, r) = |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
255 |
try { |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
256 |
val p = text_area.offsetToXY(range.start) |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
257 |
val (q, r) = |
81401 | 258 |
if (get_text(buffer, Text.Range(stop - 1, stop)).contains("\n")) { |
51078
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
259 |
(text_area.offsetToXY(stop - 1), char_width) |
81401 | 260 |
} |
261 |
else if (stop >= end) { |
|
55549
5c40782f68b3
clarified special eol treatment (amending 3d55ef732cd7): allow last line to be empty, which means stop == end for second-last line;
wenzelm
parents:
53786
diff
changeset
|
262 |
(text_area.offsetToXY(end), char_width * (stop - end)) |
81401 | 263 |
} |
51078
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
264 |
else (text_area.offsetToXY(stop), 0) |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
265 |
(p, q, r) |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
266 |
} |
4e1c940b1fb2
more robust JEdit_Lib.pixel_range, which could crash via Rich_Text_Area.tooltip_painter with bad mouse coordinates;
wenzelm
parents:
50849
diff
changeset
|
267 |
catch { case _: ArrayIndexOutOfBoundsException => (null, null, 0) } |
49409 | 268 |
|
81401 | 269 |
if (p != null && q != null && p.x < q.x + r && p.y == q.y) { |
50115 | 270 |
Some(Gfx_Range(p.x, p.y, q.x + r - p.x)) |
81401 | 271 |
} |
49409 | 272 |
else None |
273 |
} |
|
49941
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
274 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
275 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
276 |
/* pixel range */ |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
277 |
|
75393 | 278 |
def pixel_range(text_area: TextArea, x: Int, y: Int): Option[Text.Range] = { |
53183
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
279 |
// coordinates wrt. inner painter component |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
280 |
val painter = text_area.getPainter |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
281 |
if (0 <= x && x < painter.getWidth && 0 <= y && y < painter.getHeight) { |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
282 |
val offset = text_area.xyToOffset(x, y, false) |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
283 |
if (offset >= 0) { |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
284 |
val range = point_range(text_area.getBuffer, offset) |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
285 |
gfx_range(text_area, range) match { |
60215 | 286 |
case Some(g) if g.x <= x && x < g.x + g.length => Some(range) |
53183
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
287 |
case _ => None |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
288 |
} |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
289 |
} |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
290 |
else None |
49941
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
291 |
} |
53183
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
292 |
else None |
49941
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
293 |
} |
51492
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents:
51469
diff
changeset
|
294 |
|
eaa1c4cc1106
more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents:
51469
diff
changeset
|
295 |
|
52873 | 296 |
/* icons */ |
297 |
||
75393 | 298 |
def load_icon(name: String): Icon = { |
52873 | 299 |
val name1 = |
300 |
if (name.startsWith("idea-icons/")) { |
|
75701 | 301 |
val file = File.uri(Path.explode("$ISABELLE_IDEA_ICONS")).toASCIIString |
52873 | 302 |
"jar:" + file + "!/" + name |
303 |
} |
|
304 |
else name |
|
305 |
val icon = GUIUtilities.loadIcon(name1) |
|
306 |
if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name) |
|
307 |
else icon |
|
308 |
} |
|
52874 | 309 |
|
310 |
def load_image_icon(name: String): ImageIcon = |
|
311 |
load_icon(name) match { |
|
312 |
case icon: ImageIcon => icon |
|
313 |
case _ => error("Bad image icon: " + name) |
|
314 |
} |
|
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
315 |
|
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
316 |
|
53231
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
317 |
/* key event handling */ |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
318 |
|
75393 | 319 |
def request_focus_view(alt_view: View = null): Unit = { |
73987
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73652
diff
changeset
|
320 |
val view = if (alt_view != null) alt_view else jEdit.getActiveView() |
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73652
diff
changeset
|
321 |
if (view != null) { |
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73652
diff
changeset
|
322 |
val text_area = view.getTextArea |
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73652
diff
changeset
|
323 |
if (text_area != null) text_area.requestFocus() |
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73652
diff
changeset
|
324 |
} |
fc363a3b690a
build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents:
73652
diff
changeset
|
325 |
} |
66592 | 326 |
|
75393 | 327 |
def propagate_key(view: View, evt: KeyEvent): Unit = { |
53231
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
328 |
if (view != null && !evt.isConsumed) |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
329 |
view.getInputHandler().processKeyEvent(evt, View.ACTION_BAR, false) |
423e29f1f304
avoid complication and event duplication due to KeyEventInterceptor -- NB: popup has focus within root window, it is closed on loss of focus;
wenzelm
parents:
53226
diff
changeset
|
330 |
} |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
331 |
|
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
332 |
def key_listener( |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
333 |
key_typed: KeyEvent => Unit = _ => (), |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
334 |
key_pressed: KeyEvent => Unit = _ => (), |
75393 | 335 |
key_released: KeyEvent => Unit = _ => () |
336 |
): KeyListener = { |
|
337 |
def process_key_event(evt0: KeyEvent, handle: KeyEvent => Unit): Unit = { |
|
53237 | 338 |
val evt = KeyEventWorkaround.processKeyEvent(evt0) |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
339 |
if (evt != null) handle(evt) |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
340 |
} |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
341 |
|
75393 | 342 |
new KeyListener { |
73340 | 343 |
def keyTyped(evt: KeyEvent): Unit = process_key_event(evt, key_typed) |
344 |
def keyPressed(evt: KeyEvent): Unit = process_key_event(evt, key_pressed) |
|
345 |
def keyReleased(evt: KeyEvent): Unit = process_key_event(evt, key_released) |
|
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
346 |
} |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
347 |
} |
53784 | 348 |
|
75393 | 349 |
def special_key(evt: KeyEvent): Boolean = { |
59571 | 350 |
// cf. 5.2.0/jEdit/org/gjt/sp/jedit/gui/KeyEventWorkaround.java |
71500 | 351 |
val mod = evt.getModifiersEx |
352 |
(mod & InputEvent.CTRL_DOWN_MASK) != 0 && (mod & InputEvent.ALT_DOWN_MASK) == 0 || |
|
353 |
(mod & InputEvent.CTRL_DOWN_MASK) == 0 && (mod & InputEvent.ALT_DOWN_MASK) != 0 && |
|
53784 | 354 |
!Debug.ALT_KEY_PRESSED_DISABLED || |
71500 | 355 |
(mod & InputEvent.META_DOWN_MASK) != 0 |
53784 | 356 |
} |
72899 | 357 |
|
358 |
def command_modifier(evt: InputEvent): Boolean = |
|
359 |
(evt.getModifiersEx & Toolkit.getDefaultToolkit.getMenuShortcutKeyMaskEx) != 0 |
|
360 |
||
361 |
def shift_modifier(evt: InputEvent): Boolean = |
|
362 |
(evt.getModifiersEx & InputEvent.SHIFT_DOWN_MASK) != 0 |
|
72927
69f768aff611
clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents:
72899
diff
changeset
|
363 |
|
69f768aff611
clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents:
72899
diff
changeset
|
364 |
def modifier_string(evt: InputEvent): String = |
69f768aff611
clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents:
72899
diff
changeset
|
365 |
KeyEventTranslator.getModifierString(evt) match { |
69f768aff611
clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents:
72899
diff
changeset
|
366 |
case null => "" |
69f768aff611
clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents:
72899
diff
changeset
|
367 |
case s => s |
69f768aff611
clarified caret focus modifier, depending on option "jedit_focus_modifier";
wenzelm
parents:
72899
diff
changeset
|
368 |
} |
49406 | 369 |
} |