author | wenzelm |
Fri, 21 May 2010 11:51:03 +0200 | |
changeset 37036 | 49559c4e85f9 |
parent 37034 | 9640f6546179 |
child 37040 | 23b0b9986019 |
permissions | -rw-r--r-- |
36760 | 1 |
/* Title: Tools/jEdit/src/jedit/html_panel.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
HTML panel based on Lobo/Cobra. |
|
5 |
*/ |
|
34765 | 6 |
|
34871
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents:
34823
diff
changeset
|
7 |
package isabelle.jedit |
34765 | 8 |
|
9 |
||
36015 | 10 |
import isabelle._ |
11 |
||
34765 | 12 |
import java.io.StringReader |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
13 |
import java.awt.{BorderLayout, Dimension, GraphicsEnvironment, Toolkit, FontMetrics} |
34775 | 14 |
import java.awt.event.MouseEvent |
15 |
||
34765 | 16 |
import javax.swing.{JButton, JPanel, JScrollPane} |
17 |
import java.util.logging.{Logger, Level} |
|
18 |
||
34775 | 19 |
import org.w3c.dom.html2.HTMLElement |
34765 | 20 |
|
21 |
import org.lobobrowser.html.parser.{DocumentBuilderImpl, InputSourceImpl} |
|
22 |
import org.lobobrowser.html.gui.HtmlPanel |
|
23 |
import org.lobobrowser.html.domimpl.{HTMLDocumentImpl, HTMLStyleElementImpl, NodeImpl} |
|
24 |
import org.lobobrowser.html.test.{SimpleHtmlRendererContext, SimpleUserAgentContext} |
|
25 |
||
26 |
import scala.actors.Actor._ |
|
27 |
||
28 |
||
34775 | 29 |
object HTML_Panel |
30 |
{ |
|
31 |
sealed abstract class Event { val element: HTMLElement; val mouse: MouseEvent } |
|
32 |
case class Context_Menu(val element: HTMLElement, mouse: MouseEvent) extends Event |
|
33 |
case class Mouse_Click(val element: HTMLElement, mouse: MouseEvent) extends Event |
|
34 |
case class Double_Click(val element: HTMLElement, mouse: MouseEvent) extends Event |
|
35 |
case class Mouse_Over(val element: HTMLElement, mouse: MouseEvent) extends Event |
|
36 |
case class Mouse_Out(val element: HTMLElement, mouse: MouseEvent) extends Event |
|
37 |
} |
|
38 |
||
39 |
||
37036 | 40 |
class HTML_Panel(system: Isabelle_System, initial_font_size: Int) extends HtmlPanel |
34765 | 41 |
{ |
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
42 |
/** Lobo setup **/ |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
43 |
|
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
44 |
/* global logging */ |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
45 |
|
34765 | 46 |
Logger.getLogger("org.lobobrowser").setLevel(Level.WARNING) |
47 |
||
48 |
||
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
49 |
/* pixel size -- cf. org.lobobrowser.html.style.HtmlValues.getFontSize */ |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
50 |
|
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
51 |
val screen_resolution = |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
52 |
if (GraphicsEnvironment.isHeadless()) 72 |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
53 |
else Toolkit.getDefaultToolkit().getScreenResolution() |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
54 |
|
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
55 |
def lobo_px(raw_px: Int): Int = raw_px * 96 / screen_resolution |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
56 |
def raw_px(lobo_px: Int): Int = (lobo_px * screen_resolution + 95) / 96 |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
57 |
|
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
58 |
|
37036 | 59 |
/* contexts and event handling */ |
60 |
||
61 |
protected val handler: PartialFunction[HTML_Panel.Event, Unit] = Library.undefined |
|
62 |
||
36993
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
63 |
private val ucontext = new SimpleUserAgentContext |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
64 |
private val rcontext = new SimpleHtmlRendererContext(this, ucontext) |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
65 |
{ |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
66 |
private def handle(event: HTML_Panel.Event): Boolean = |
37036 | 67 |
if (handler.isDefinedAt(event)) { handler(event); true } |
36993
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
68 |
else false |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
69 |
|
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
70 |
override def onContextMenu(elem: HTMLElement, event: MouseEvent): Boolean = |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
71 |
handle(HTML_Panel.Context_Menu(elem, event)) |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
72 |
override def onMouseClick(elem: HTMLElement, event: MouseEvent): Boolean = |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
73 |
handle(HTML_Panel.Mouse_Click(elem, event)) |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
74 |
override def onDoubleClick(elem: HTMLElement, event: MouseEvent): Boolean = |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
75 |
handle(HTML_Panel.Double_Click(elem, event)) |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
76 |
override def onMouseOver(elem: HTMLElement, event: MouseEvent) |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
77 |
{ handle(HTML_Panel.Mouse_Over(elem, event)) } |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
78 |
override def onMouseOut(elem: HTMLElement, event: MouseEvent) |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
79 |
{ handle(HTML_Panel.Mouse_Out(elem, event)) } |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
80 |
} |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
81 |
|
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
82 |
private val builder = new DocumentBuilderImpl(ucontext, rcontext) |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
83 |
|
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
84 |
|
37015 | 85 |
/* document template with style sheets */ |
34765 | 86 |
|
37015 | 87 |
private val template_head = |
34765 | 88 |
"""<?xml version="1.0" encoding="utf-8"?> |
89 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|
90 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
91 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
|
92 |
<head> |
|
34770 | 93 |
<style media="all" type="text/css"> |
94 |
""" + |
|
36992 | 95 |
system.try_read("$ISABELLE_HOME/lib/html/isabelle.css") + "\n" + |
37015 | 96 |
system.try_read("$ISABELLE_HOME_USER/etc/isabelle.css") + "\n" |
97 |
||
98 |
private val template_tail = |
|
34765 | 99 |
""" |
100 |
</style> |
|
101 |
</head> |
|
102 |
<body/> |
|
103 |
</html> |
|
104 |
""" |
|
37015 | 105 |
|
106 |
private def template(font_size: Int): String = |
|
107 |
template_head + |
|
108 |
"body { font-family: " + system.font_family + "; font-size: " + raw_px(font_size) + "px }" + |
|
109 |
template_tail |
|
110 |
||
111 |
||
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
112 |
/** main actor **/ |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
113 |
|
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
114 |
/* internal messages */ |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
115 |
|
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
116 |
private case class Resize(font_size: Int) |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
117 |
private case class Render(body: List[XML.Tree]) |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
118 |
private case object Refresh |
34765 | 119 |
|
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
120 |
private val main_actor = actor { |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
121 |
|
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
122 |
/* internal state */ |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
123 |
|
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
124 |
var current_font_metrics: FontMetrics = null |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
125 |
var current_font_size: Int = 0 |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
126 |
var current_margin: Int = 0 |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
127 |
var current_body: List[XML.Tree] = Nil |
34765 | 128 |
|
36993
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
129 |
def resize(font_size: Int) |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
130 |
{ |
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
131 |
val (font_metrics, margin) = |
36993
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
132 |
Swing_Thread.now { |
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
133 |
val metrics = getFontMetrics(system.get_font(lobo_px(raw_px(font_size)))) |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
134 |
(metrics, (getWidth() / (metrics.charWidth(Symbol.spc) max 1) - 4) max 20) |
36993
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
135 |
} |
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
136 |
if (current_font_metrics == null || |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
137 |
current_font_size != font_size || |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
138 |
current_margin != margin) |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
139 |
{ |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
140 |
current_font_metrics = font_metrics |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
141 |
current_font_size = font_size |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
142 |
current_margin = margin |
37014 | 143 |
refresh() |
36993
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
144 |
} |
36814
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
145 |
} |
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
146 |
|
37014 | 147 |
def refresh() { render(current_body) } |
148 |
||
36993
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
149 |
def render(body: List[XML.Tree]) |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
150 |
{ |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
151 |
current_body = body |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
152 |
val html_body = |
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
153 |
current_body.flatMap(div => |
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
154 |
Pretty.formatted(List(div), current_margin, Pretty.font_metric(current_font_metrics)) |
36993
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
155 |
.map(t => XML.elem(HTML.PRE, HTML.spans(t)))) |
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
156 |
val doc = |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
157 |
builder.parse( |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
158 |
new InputSourceImpl(new StringReader(template(current_font_size)), "http://localhost")) |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
159 |
doc.removeChild(doc.getLastChild()) |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
160 |
doc.appendChild(XML.document_node(doc, XML.elem(HTML.BODY, html_body))) |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
161 |
Swing_Thread.later { setDocument(doc, rcontext) } |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
162 |
} |
34775 | 163 |
|
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
164 |
|
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
165 |
/* main loop */ |
36993
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
166 |
|
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
167 |
resize(initial_font_size) |
34775 | 168 |
|
34765 | 169 |
loop { |
170 |
react { |
|
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
171 |
case Resize(font_size) => resize(font_size) |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
172 |
case Refresh => refresh() |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
173 |
case Render(body) => render(body) |
34769 | 174 |
case bad => System.err.println("main_actor: ignoring bad message " + bad) |
34765 | 175 |
} |
176 |
} |
|
177 |
} |
|
178 |
||
37034
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
179 |
|
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
180 |
/* external methods */ |
9640f6546179
more systematic treatment of internal state, which belongs strictly to the main actor, not the Swing thread;
wenzelm
parents:
37016
diff
changeset
|
181 |
|
36993
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
182 |
def resize(font_size: Int) { main_actor ! Resize(font_size) } |
37014 | 183 |
def refresh() { main_actor ! Refresh } |
36993
b7cce32953f0
more systematic treatment of physical document wrt. font size etc.;
wenzelm
parents:
36992
diff
changeset
|
184 |
def render(body: List[XML.Tree]) { main_actor ! Render(body) } |
34765 | 185 |
} |