author | wenzelm |
Tue, 11 May 2010 23:36:06 +0200 | |
changeset 36817 | ed97e877ff2d |
parent 36814 | dc85664dbf6d |
child 36819 | fc8a6b5f9b0b |
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.io.Source |
|
27 |
import scala.actors.Actor._ |
|
28 |
||
29 |
||
34775 | 30 |
object HTML_Panel |
31 |
{ |
|
32 |
sealed abstract class Event { val element: HTMLElement; val mouse: MouseEvent } |
|
33 |
case class Context_Menu(val element: HTMLElement, mouse: MouseEvent) extends Event |
|
34 |
case class Mouse_Click(val element: HTMLElement, mouse: MouseEvent) extends Event |
|
35 |
case class Double_Click(val element: HTMLElement, mouse: MouseEvent) extends Event |
|
36 |
case class Mouse_Over(val element: HTMLElement, mouse: MouseEvent) extends Event |
|
37 |
case class Mouse_Out(val element: HTMLElement, mouse: MouseEvent) extends Event |
|
38 |
} |
|
39 |
||
40 |
||
41 |
class HTML_Panel( |
|
42 |
sys: Isabelle_System, |
|
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
43 |
initial_font_size: Int, |
34775 | 44 |
handler: PartialFunction[HTML_Panel.Event, Unit]) extends HtmlPanel |
34765 | 45 |
{ |
46 |
// global logging |
|
47 |
Logger.getLogger("org.lobobrowser").setLevel(Level.WARNING) |
|
48 |
||
49 |
||
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
50 |
/* pixel size -- cf. org.lobobrowser.html.style.HtmlValues.getFontSize */ |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
51 |
|
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
52 |
val screen_resolution = |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
53 |
if (GraphicsEnvironment.isHeadless()) 72 |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
54 |
else Toolkit.getDefaultToolkit().getScreenResolution() |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
55 |
|
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
56 |
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
|
57 |
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
|
58 |
|
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
59 |
|
34765 | 60 |
/* document template */ |
61 |
||
62 |
private def try_file(name: String): String = |
|
63 |
{ |
|
64 |
val file = sys.platform_file(name) |
|
34776 | 65 |
if (file.isFile) Source.fromFile(file).mkString else "" |
34765 | 66 |
} |
67 |
||
68 |
private def template(font_size: Int): String = |
|
36790
c8deab866174
font size re-adjustment according to Lobo internals;
wenzelm
parents:
36760
diff
changeset
|
69 |
{ |
34765 | 70 |
"""<?xml version="1.0" encoding="utf-8"?> |
71 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|
72 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
73 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
|
74 |
<head> |
|
34770 | 75 |
<style media="all" type="text/css"> |
76 |
""" + |
|
34765 | 77 |
try_file("$ISABELLE_HOME/lib/html/isabelle.css") + "\n" + |
78 |
try_file("$ISABELLE_HOME_USER/etc/isabelle.css") + "\n" + |
|
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
79 |
"body { font-family: " + sys.font_family + "; font-size: " + raw_px(font_size) + "px }" + |
34765 | 80 |
""" |
81 |
</style> |
|
82 |
</head> |
|
83 |
<body/> |
|
84 |
</html> |
|
85 |
""" |
|
36790
c8deab866174
font size re-adjustment according to Lobo internals;
wenzelm
parents:
36760
diff
changeset
|
86 |
} |
34765 | 87 |
|
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
88 |
def font_metrics(font_size: Int): FontMetrics = |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
89 |
Swing_Thread.now { getFontMetrics(sys.get_font(font_size)) } |
34765 | 90 |
|
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
91 |
def panel_width(font_size: Int): Int = |
36814
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
92 |
Swing_Thread.now { |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
93 |
(getWidth() / (font_metrics(font_size).charWidth(Symbol.spc) max 1) - 4) max 20 |
36814
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
94 |
} |
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
95 |
|
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
96 |
|
34765 | 97 |
/* actor with local state */ |
98 |
||
99 |
private val ucontext = new SimpleUserAgentContext |
|
34775 | 100 |
|
34765 | 101 |
private val rcontext = new SimpleHtmlRendererContext(this, ucontext) |
34775 | 102 |
{ |
103 |
private def handle(event: HTML_Panel.Event): Boolean = |
|
104 |
if (handler != null && handler.isDefinedAt(event)) { handler(event); true } |
|
105 |
else false |
|
106 |
||
107 |
override def onContextMenu(elem: HTMLElement, event: MouseEvent): Boolean = |
|
108 |
handle(HTML_Panel.Context_Menu(elem, event)) |
|
109 |
override def onMouseClick(elem: HTMLElement, event: MouseEvent): Boolean = |
|
110 |
handle(HTML_Panel.Mouse_Click(elem, event)) |
|
111 |
override def onDoubleClick(elem: HTMLElement, event: MouseEvent): Boolean = |
|
112 |
handle(HTML_Panel.Double_Click(elem, event)) |
|
113 |
override def onMouseOver(elem: HTMLElement, event: MouseEvent) |
|
114 |
{ handle(HTML_Panel.Mouse_Over(elem, event)) } |
|
115 |
override def onMouseOut(elem: HTMLElement, event: MouseEvent) |
|
116 |
{ handle(HTML_Panel.Mouse_Out(elem, event)) } |
|
117 |
} |
|
118 |
||
34765 | 119 |
private val builder = new DocumentBuilderImpl(ucontext, rcontext) |
120 |
||
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
121 |
private case class Init(font_size: Int) |
34765 | 122 |
private case class Render(body: List[XML.Tree]) |
123 |
||
124 |
private val main_actor = actor { |
|
34791 | 125 |
// crude double buffering |
34823 | 126 |
var doc1: org.w3c.dom.Document = null |
127 |
var doc2: org.w3c.dom.Document = null |
|
34765 | 128 |
|
36814
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
129 |
var current_font_size = 16 |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
130 |
var current_font_metrics: FontMetrics = null |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
131 |
|
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
132 |
def metric(s: String): Double = |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
133 |
if (current_font_metrics == null) s.length.toDouble |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
134 |
else current_font_metrics.stringWidth(s).toDouble / current_font_metrics.charWidth(Symbol.spc) |
36814
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
135 |
|
34765 | 136 |
loop { |
137 |
react { |
|
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
138 |
case Init(font_size) => |
36814
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
139 |
current_font_size = font_size |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
140 |
current_font_metrics = font_metrics(lobo_px(raw_px(font_size))) |
36814
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
141 |
|
34765 | 142 |
val src = template(font_size) |
34766 | 143 |
def parse() = |
144 |
builder.parse(new InputSourceImpl(new StringReader(src), "http://localhost")) |
|
34765 | 145 |
doc1 = parse() |
146 |
doc2 = parse() |
|
147 |
Swing_Thread.now { setDocument(doc1, rcontext) } |
|
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
148 |
|
34765 | 149 |
case Render(body) => |
150 |
val doc = doc2 |
|
36814
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
151 |
val html_body = |
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
152 |
Pretty.formatted(body, panel_width(current_font_size), metric) |
36814
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
153 |
.map(t => XML.elem(HTML.PRE, HTML.spans(t))) |
36735
42b7f881f5fc
output symbolic pretty printing markup and format in the front end;
wenzelm
parents:
36015
diff
changeset
|
154 |
val node = XML.document_node(doc, XML.elem(HTML.BODY, html_body)) |
34765 | 155 |
doc.removeChild(doc.getLastChild()) |
156 |
doc.appendChild(node) |
|
157 |
doc2 = doc1 |
|
158 |
doc1 = doc |
|
159 |
Swing_Thread.now { setDocument(doc1, rcontext) } |
|
160 |
||
34769 | 161 |
case bad => System.err.println("main_actor: ignoring bad message " + bad) |
34765 | 162 |
} |
163 |
} |
|
164 |
} |
|
165 |
||
166 |
||
167 |
/* main method wrappers */ |
|
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
168 |
|
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
169 |
def init(font_size: Int) { main_actor ! Init(font_size) } |
34765 | 170 |
def render(body: List[XML.Tree]) { main_actor ! Render(body) } |
36814
dc85664dbf6d
support Isabelle plugin properties with defaults;
wenzelm
parents:
36790
diff
changeset
|
171 |
|
36817
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36814
diff
changeset
|
172 |
init(initial_font_size) |
34765 | 173 |
} |