author | wenzelm |
Tue, 27 Aug 2013 12:35:32 +0200 | |
changeset 53226 | 9cf8e2263ca7 |
parent 53183 | 018d71bee930 |
child 53231 | 423e29f1f304 |
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 |
||
50658 | 12 |
import java.awt.{Component, Container, Window, GraphicsEnvironment, Point, Rectangle} |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
13 |
import java.awt.event.{KeyEvent, KeyListener} |
53019 | 14 |
import javax.swing.{Icon, ImageIcon, JWindow} |
49710
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
15 |
|
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
16 |
import scala.annotation.tailrec |
49406 | 17 |
|
52873 | 18 |
import org.gjt.sp.jedit.{jEdit, Buffer, View, GUIUtilities} |
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
19 |
import org.gjt.sp.jedit.gui.KeyEventWorkaround |
49406 | 20 |
import org.gjt.sp.jedit.buffer.JEditBuffer |
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
|
21 |
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaPainter} |
49406 | 22 |
|
23 |
||
24 |
object JEdit_Lib |
|
25 |
{ |
|
50554
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
26 |
/* bounds within multi-screen environment */ |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
27 |
|
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
28 |
def screen_bounds(screen_point: Point): Rectangle = |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
29 |
{ |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
30 |
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
31 |
(for { |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
32 |
device <- ge.getScreenDevices.iterator |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
33 |
config <- device.getConfigurations.iterator |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
34 |
bounds = config.getBounds |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
35 |
} yield bounds).find(_.contains(screen_point)) getOrElse ge.getMaximumWindowBounds |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
36 |
} |
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
37 |
|
0493efcc97e9
more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents:
50363
diff
changeset
|
38 |
|
53019 | 39 |
/* window geometry measurement */ |
40 |
||
41 |
private lazy val dummy_window = new JWindow |
|
42 |
||
43 |
final case class Window_Geometry(width: Int, height: Int, inner_width: Int, inner_height: Int) |
|
44 |
{ |
|
45 |
def deco_width: Int = width - inner_width |
|
46 |
def deco_height: Int = height - inner_height |
|
47 |
} |
|
48 |
||
49 |
def window_geometry(outer: Container, inner: Component): Window_Geometry = |
|
50 |
{ |
|
51 |
Swing_Thread.require() |
|
52 |
||
53 |
val old_content = dummy_window.getContentPane |
|
54 |
||
55 |
dummy_window.setContentPane(outer) |
|
56 |
dummy_window.pack |
|
57 |
dummy_window.revalidate |
|
58 |
||
59 |
val geometry = |
|
60 |
Window_Geometry( |
|
61 |
dummy_window.getWidth, dummy_window.getHeight, inner.getWidth, inner.getHeight) |
|
62 |
||
63 |
dummy_window.setContentPane(old_content) |
|
64 |
||
65 |
geometry |
|
66 |
} |
|
67 |
||
68 |
||
49712 | 69 |
/* GUI components */ |
70 |
||
71 |
def get_parent(component: Component): Option[Container] = |
|
72 |
component.getParent match { |
|
73 |
case null => None |
|
74 |
case parent => Some(parent) |
|
75 |
} |
|
76 |
||
77 |
def ancestors(component: Component): Iterator[Container] = new Iterator[Container] { |
|
78 |
private var next_elem = get_parent(component) |
|
79 |
def hasNext(): Boolean = next_elem.isDefined |
|
80 |
def next(): Container = |
|
81 |
next_elem match { |
|
82 |
case Some(parent) => |
|
83 |
next_elem = get_parent(parent) |
|
84 |
parent |
|
85 |
case None => Iterator.empty.next() |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
def parent_window(component: Component): Option[Window] = |
|
52478
0a1db0d02628
manage popup windows via PopupFactory, which prefers light-weight JComponent, but might fall back on JWindow (despite JPanel of 0f88591478e6);
wenzelm
parents:
51507
diff
changeset
|
90 |
ancestors(component).collectFirst({ case x: Window => x }) |
49710
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
91 |
|
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49409
diff
changeset
|
92 |
|
50186
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
93 |
/* basic tooltips, with multi-line support */ |
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
94 |
|
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
95 |
def wrap_tooltip(text: String): String = |
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
96 |
if (text == "") null |
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
97 |
else "<html><pre>" + HTML.encode(text) + "</pre></html>" |
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
98 |
|
f83cab68c788
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents:
50116
diff
changeset
|
99 |
|
49406 | 100 |
/* buffers */ |
101 |
||
102 |
def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A = |
|
103 |
Swing_Thread.now { buffer_lock(buffer) { body } } |
|
104 |
||
105 |
def buffer_text(buffer: JEditBuffer): String = |
|
106 |
buffer_lock(buffer) { buffer.getText(0, buffer.getLength) } |
|
107 |
||
108 |
def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath |
|
109 |
||
110 |
||
53003
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
111 |
/* focus of main window */ |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
112 |
|
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
113 |
def request_focus_view: Unit = |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
114 |
{ |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
115 |
jEdit.getActiveView() match { |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
116 |
case null => |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
117 |
case view => |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
118 |
view.getTextArea match { |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
119 |
case null => |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
120 |
case text_area => text_area.requestFocus |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
121 |
} |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
122 |
} |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
123 |
} |
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
124 |
|
39da27fc6101
attempt to transfer focus back to main window after closing popups, which is potentially relevant for heavy-weight windows (cf. workaround in org/gjt/sp/jedit/gui/CompletionPopup.java);
wenzelm
parents:
52874
diff
changeset
|
125 |
|
49406 | 126 |
/* main jEdit components */ |
127 |
||
128 |
def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator |
|
129 |
||
130 |
def jedit_buffer(name: String): Option[Buffer] = |
|
131 |
jedit_buffers().find(buffer => buffer_name(buffer) == name) |
|
132 |
||
133 |
def jedit_views(): Iterator[View] = jEdit.getViews().iterator |
|
134 |
||
135 |
def jedit_text_areas(view: View): Iterator[JEditTextArea] = |
|
136 |
view.getEditPanes().iterator.map(_.getTextArea) |
|
137 |
||
138 |
def jedit_text_areas(): Iterator[JEditTextArea] = |
|
139 |
jedit_views().flatMap(jedit_text_areas(_)) |
|
140 |
||
141 |
def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] = |
|
142 |
jedit_text_areas().filter(_.getBuffer == buffer) |
|
143 |
||
144 |
def buffer_lock[A](buffer: JEditBuffer)(body: => A): A = |
|
145 |
{ |
|
146 |
try { buffer.readLock(); body } |
|
147 |
finally { buffer.readUnlock() } |
|
148 |
} |
|
49407 | 149 |
|
50195
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
150 |
def buffer_edit[A](buffer: JEditBuffer)(body: => A): A = |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
151 |
{ |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
152 |
try { buffer.beginCompoundEdit(); body } |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
153 |
finally { buffer.endCompoundEdit() } |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
154 |
} |
863b1dfc396c
prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents:
50186
diff
changeset
|
155 |
|
49407 | 156 |
|
50215 | 157 |
/* get text */ |
158 |
||
159 |
def try_get_text(buffer: JEditBuffer, range: Text.Range): Option[String] = |
|
160 |
try { Some(buffer.getText(range.start, range.length)) } |
|
161 |
catch { case _: ArrayIndexOutOfBoundsException => None } |
|
162 |
||
163 |
||
50363
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
164 |
/* buffer range */ |
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
165 |
|
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
166 |
def buffer_range(buffer: JEditBuffer): Text.Range = |
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
167 |
Text.Range(0, (buffer.getLength - 1) max 0) |
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
168 |
|
2f8dc9e65401
tuned signature in accordance to document operations;
wenzelm
parents:
50215
diff
changeset
|
169 |
|
49407 | 170 |
/* point range */ |
171 |
||
172 |
def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range = |
|
173 |
buffer_lock(buffer) { |
|
174 |
def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0) |
|
175 |
try { |
|
176 |
val c = text(offset) |
|
177 |
if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1))) |
|
178 |
Text.Range(offset, offset + 2) |
|
179 |
else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1))) |
|
180 |
Text.Range(offset - 1, offset + 1) |
|
181 |
else Text.Range(offset, offset + 1) |
|
182 |
} |
|
183 |
catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) } |
|
184 |
} |
|
49408 | 185 |
|
186 |
||
187 |
/* visible text range */ |
|
188 |
||
189 |
def visible_range(text_area: TextArea): Option[Text.Range] = |
|
190 |
{ |
|
191 |
val buffer = text_area.getBuffer |
|
192 |
val n = text_area.getVisibleLines |
|
193 |
if (n > 0) { |
|
194 |
val start = text_area.getScreenLineStartOffset(0) |
|
195 |
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
|
196 |
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
|
197 |
Some(Text.Range(start, end)) |
49408 | 198 |
} |
199 |
else None |
|
200 |
} |
|
201 |
||
202 |
def invalidate_range(text_area: TextArea, range: Text.Range) |
|
203 |
{ |
|
204 |
val buffer = text_area.getBuffer |
|
205 |
text_area.invalidateLineRange( |
|
206 |
buffer.getLineOfOffset(range.start), |
|
207 |
buffer.getLineOfOffset(range.stop)) |
|
208 |
} |
|
49409 | 209 |
|
210 |
||
211 |
/* graphics range */ |
|
212 |
||
50115 | 213 |
case class Gfx_Range(val x: Int, val y: Int, val length: Int) |
49409 | 214 |
|
49843
afddf4e26fac
further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents:
49712
diff
changeset
|
215 |
// NB: jEdit always normalizes \r\n and \r to \n |
49409 | 216 |
// NB: last line lacks \n |
217 |
def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] = |
|
218 |
{ |
|
51507 | 219 |
val metric = pretty_metric(text_area.getPainter) |
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
|
220 |
val char_width = (metric.unit * metric.average).round.toInt |
50849 | 221 |
|
49409 | 222 |
val buffer = text_area.getBuffer |
223 |
||
224 |
val end = buffer.getLength |
|
225 |
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
|
226 |
|
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
|
227 |
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
|
228 |
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
|
229 |
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
|
230 |
val (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
|
231 |
if (stop >= end) (text_area.offsetToXY(end), char_width * (stop - end)) |
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
|
232 |
else if (stop > 0 && buffer.getText(stop - 1, 1) == "\n") |
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
|
233 |
(text_area.offsetToXY(stop - 1), char_width) |
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
|
234 |
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
|
235 |
(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
|
236 |
} |
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
|
237 |
catch { case _: ArrayIndexOutOfBoundsException => (null, null, 0) } |
49409 | 238 |
|
239 |
if (p != null && q != null && p.x < q.x + r && p.y == q.y) |
|
50115 | 240 |
Some(Gfx_Range(p.x, p.y, q.x + r - p.x)) |
49409 | 241 |
else None |
242 |
} |
|
49941
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
243 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
244 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
245 |
/* pixel range */ |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
246 |
|
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
247 |
def pixel_range(text_area: TextArea, x: Int, y: Int): Option[Text.Range] = |
f2db0596bd61
more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents:
49843
diff
changeset
|
248 |
{ |
53183
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
249 |
// coordinates wrt. inner painter component |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
250 |
val painter = text_area.getPainter |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
251 |
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
|
252 |
val offset = text_area.xyToOffset(x, y, false) |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
253 |
if (offset >= 0) { |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
254 |
val range = point_range(text_area.getBuffer, offset) |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
255 |
gfx_range(text_area, range) match { |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
256 |
case Some(g) if (g.x <= x && x < g.x + g.length) => Some(range) |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
257 |
case _ => None |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
258 |
} |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
259 |
} |
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
260 |
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
|
261 |
} |
53183
018d71bee930
strict checking of coordinates wrt. inner painter component;
wenzelm
parents:
53019
diff
changeset
|
262 |
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
|
263 |
} |
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
|
264 |
|
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
|
265 |
|
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
|
266 |
/* pretty text metric */ |
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
|
267 |
|
51507 | 268 |
abstract class Pretty_Metric extends Pretty.Metric |
269 |
{ |
|
270 |
def average: Double |
|
271 |
} |
|
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
|
272 |
|
51507 | 273 |
def pretty_metric(painter: TextAreaPainter): Pretty_Metric = |
274 |
new Pretty_Metric { |
|
275 |
def string_width(s: String): Double = |
|
276 |
painter.getFont.getStringBounds(s, painter.getFontRenderContext).getWidth |
|
277 |
||
278 |
val unit = string_width(Pretty.space) |
|
279 |
val average = string_width("mix") / (3 * unit) |
|
280 |
def apply(s: String): Double = if (s == "\n") 1.0 else string_width(s) / unit |
|
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
|
281 |
} |
52873 | 282 |
|
283 |
||
284 |
/* icons */ |
|
285 |
||
286 |
def load_icon(name: String): Icon = |
|
287 |
{ |
|
288 |
val name1 = |
|
289 |
if (name.startsWith("idea-icons/")) { |
|
290 |
val file = |
|
291 |
Isabelle_System.platform_file_url(Path.explode("$JEDIT_HOME/dist/jars/idea-icons.jar")) |
|
292 |
"jar:" + file + "!/" + name |
|
293 |
} |
|
294 |
else name |
|
295 |
val icon = GUIUtilities.loadIcon(name1) |
|
296 |
if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name) |
|
297 |
else icon |
|
298 |
} |
|
52874 | 299 |
|
300 |
def load_image_icon(name: String): ImageIcon = |
|
301 |
load_icon(name) match { |
|
302 |
case icon: ImageIcon => icon |
|
303 |
case _ => error("Bad image icon: " + name) |
|
304 |
} |
|
53226
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
305 |
|
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
306 |
|
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
307 |
/* key listener */ |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
308 |
|
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
309 |
def key_listener( |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
310 |
workaround: Boolean = true, |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
311 |
key_typed: KeyEvent => Unit = _ => (), |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
312 |
key_pressed: KeyEvent => Unit = _ => (), |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
313 |
key_released: KeyEvent => Unit = _ => ()): KeyListener = |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
314 |
{ |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
315 |
def process_key_event(evt0: KeyEvent, handle: KeyEvent => Unit) |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
316 |
{ |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
317 |
val evt = if (workaround) KeyEventWorkaround.processKeyEvent(evt0) else evt0 |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
318 |
if (evt != null) handle(evt) |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
319 |
} |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
320 |
|
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
321 |
new KeyListener |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
322 |
{ |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
323 |
def keyTyped(evt: KeyEvent) { process_key_event(evt, key_typed) } |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
324 |
def keyPressed(evt: KeyEvent) { process_key_event(evt, key_pressed) } |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
325 |
def keyReleased(evt: KeyEvent) { process_key_event(evt, key_released) } |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
326 |
} |
9cf8e2263ca7
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm
parents:
53183
diff
changeset
|
327 |
} |
49406 | 328 |
} |
329 |