src/Tools/jEdit/src/jedit/html_panel.scala
author wenzelm
Wed Sep 22 16:03:57 2010 +0200 (2010-09-22)
changeset 39590 2258769f112f
parent 38573 d163f0f28e8c
child 39740 0294948ba962
permissions -rw-r--r--
basic support for full document rendering;
wenzelm@36760
     1
/*  Title:      Tools/jEdit/src/jedit/html_panel.scala
wenzelm@36760
     2
    Author:     Makarius
wenzelm@36760
     3
wenzelm@36760
     4
HTML panel based on Lobo/Cobra.
wenzelm@36760
     5
*/
wenzelm@34765
     6
wenzelm@34871
     7
package isabelle.jedit
wenzelm@34765
     8
wenzelm@34765
     9
wenzelm@36015
    10
import isabelle._
wenzelm@36015
    11
wenzelm@34765
    12
import java.io.StringReader
wenzelm@37164
    13
import java.awt.{Font, BorderLayout, Dimension, GraphicsEnvironment, Toolkit, FontMetrics}
wenzelm@34775
    14
import java.awt.event.MouseEvent
wenzelm@34775
    15
wenzelm@34765
    16
import java.util.logging.{Logger, Level}
wenzelm@34765
    17
wenzelm@34775
    18
import org.w3c.dom.html2.HTMLElement
wenzelm@34765
    19
wenzelm@34765
    20
import org.lobobrowser.html.parser.{DocumentBuilderImpl, InputSourceImpl}
wenzelm@34765
    21
import org.lobobrowser.html.gui.HtmlPanel
wenzelm@34765
    22
import org.lobobrowser.html.domimpl.{HTMLDocumentImpl, HTMLStyleElementImpl, NodeImpl}
wenzelm@34765
    23
import org.lobobrowser.html.test.{SimpleHtmlRendererContext, SimpleUserAgentContext}
wenzelm@34765
    24
wenzelm@34765
    25
import scala.actors.Actor._
wenzelm@34765
    26
wenzelm@34765
    27
wenzelm@34775
    28
object HTML_Panel
wenzelm@34775
    29
{
wenzelm@34775
    30
  sealed abstract class Event { val element: HTMLElement; val mouse: MouseEvent }
wenzelm@34775
    31
  case class Context_Menu(val element: HTMLElement, mouse: MouseEvent) extends Event
wenzelm@34775
    32
  case class Mouse_Click(val element: HTMLElement, mouse: MouseEvent) extends Event
wenzelm@34775
    33
  case class Double_Click(val element: HTMLElement, mouse: MouseEvent) extends Event
wenzelm@34775
    34
  case class Mouse_Over(val element: HTMLElement, mouse: MouseEvent) extends Event
wenzelm@34775
    35
  case class Mouse_Out(val element: HTMLElement, mouse: MouseEvent) extends Event
wenzelm@34775
    36
}
wenzelm@34775
    37
wenzelm@34775
    38
wenzelm@37164
    39
class HTML_Panel(
wenzelm@37164
    40
    system: Isabelle_System,
wenzelm@37164
    41
    initial_font_family: String,
wenzelm@37164
    42
    initial_font_size: Int)
wenzelm@37164
    43
  extends HtmlPanel
wenzelm@34765
    44
{
wenzelm@37034
    45
  /** Lobo setup **/
wenzelm@37034
    46
wenzelm@37034
    47
  /* global logging */
wenzelm@37034
    48
wenzelm@34765
    49
  Logger.getLogger("org.lobobrowser").setLevel(Level.WARNING)
wenzelm@34765
    50
wenzelm@34765
    51
wenzelm@37034
    52
  /* pixel size -- cf. org.lobobrowser.html.style.HtmlValues.getFontSize */
wenzelm@36817
    53
wenzelm@36817
    54
  val screen_resolution =
wenzelm@36817
    55
    if (GraphicsEnvironment.isHeadless()) 72
wenzelm@36817
    56
    else Toolkit.getDefaultToolkit().getScreenResolution()
wenzelm@36817
    57
wenzelm@36817
    58
  def lobo_px(raw_px: Int): Int = raw_px * 96 / screen_resolution
wenzelm@36817
    59
  def raw_px(lobo_px: Int): Int = (lobo_px * screen_resolution + 95) / 96
wenzelm@36817
    60
wenzelm@36817
    61
wenzelm@37036
    62
  /* contexts and event handling */
wenzelm@37036
    63
wenzelm@37036
    64
  protected val handler: PartialFunction[HTML_Panel.Event, Unit] = Library.undefined
wenzelm@37036
    65
wenzelm@36993
    66
  private val ucontext = new SimpleUserAgentContext
wenzelm@36993
    67
  private val rcontext = new SimpleHtmlRendererContext(this, ucontext)
wenzelm@36993
    68
  {
wenzelm@36993
    69
    private def handle(event: HTML_Panel.Event): Boolean =
wenzelm@37036
    70
      if (handler.isDefinedAt(event)) { handler(event); true }
wenzelm@36993
    71
      else false
wenzelm@36993
    72
wenzelm@36993
    73
    override def onContextMenu(elem: HTMLElement, event: MouseEvent): Boolean =
wenzelm@36993
    74
      handle(HTML_Panel.Context_Menu(elem, event))
wenzelm@36993
    75
    override def onMouseClick(elem: HTMLElement, event: MouseEvent): Boolean =
wenzelm@36993
    76
      handle(HTML_Panel.Mouse_Click(elem, event))
wenzelm@36993
    77
    override def onDoubleClick(elem: HTMLElement, event: MouseEvent): Boolean =
wenzelm@36993
    78
      handle(HTML_Panel.Double_Click(elem, event))
wenzelm@36993
    79
    override def onMouseOver(elem: HTMLElement, event: MouseEvent)
wenzelm@36993
    80
      { handle(HTML_Panel.Mouse_Over(elem, event)) }
wenzelm@36993
    81
    override def onMouseOut(elem: HTMLElement, event: MouseEvent)
wenzelm@36993
    82
      { handle(HTML_Panel.Mouse_Out(elem, event)) }
wenzelm@36993
    83
  }
wenzelm@36993
    84
wenzelm@36993
    85
  private val builder = new DocumentBuilderImpl(ucontext, rcontext)
wenzelm@36993
    86
wenzelm@36993
    87
wenzelm@37015
    88
  /* document template with style sheets */
wenzelm@34765
    89
wenzelm@37015
    90
  private val template_head =
wenzelm@34765
    91
    """<?xml version="1.0" encoding="utf-8"?>
wenzelm@34765
    92
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
wenzelm@34765
    93
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
wenzelm@34765
    94
<html xmlns="http://www.w3.org/1999/xhtml">
wenzelm@34765
    95
<head>
wenzelm@34770
    96
<style media="all" type="text/css">
wenzelm@34770
    97
""" +
wenzelm@37058
    98
  system.try_read(system.getenv_strict("JEDIT_STYLE_SHEETS").split(":"))
wenzelm@37015
    99
wenzelm@37015
   100
  private val template_tail =
wenzelm@34765
   101
"""
wenzelm@34765
   102
</style>
wenzelm@34765
   103
</head>
wenzelm@34765
   104
<body/>
wenzelm@34765
   105
</html>
wenzelm@34765
   106
"""
wenzelm@37015
   107
wenzelm@37164
   108
  private def template(font_family: String, font_size: Int): String =
wenzelm@37015
   109
    template_head +
wenzelm@37202
   110
    "body { font-family: " + font_family + "; font-size: " + raw_px(font_size) + "px; }" +
wenzelm@37015
   111
    template_tail
wenzelm@37015
   112
wenzelm@37015
   113
wenzelm@37034
   114
  /** main actor **/
wenzelm@37034
   115
wenzelm@37034
   116
  /* internal messages */
wenzelm@37034
   117
wenzelm@37164
   118
  private case class Resize(font_family: String, font_size: Int)
wenzelm@39590
   119
  private case class Render_Document(text: String)
wenzelm@38573
   120
  private case class Render(body: XML.Body)
wenzelm@37034
   121
  private case object Refresh
wenzelm@34765
   122
wenzelm@37034
   123
  private val main_actor = actor {
wenzelm@37034
   124
wenzelm@37034
   125
    /* internal state */
wenzelm@37034
   126
wenzelm@37034
   127
    var current_font_metrics: FontMetrics = null
wenzelm@37164
   128
    var current_font_family = ""
wenzelm@37034
   129
    var current_font_size: Int = 0
wenzelm@37034
   130
    var current_margin: Int = 0
wenzelm@38573
   131
    var current_body: XML.Body = Nil
wenzelm@34765
   132
wenzelm@37164
   133
    def resize(font_family: String, font_size: Int)
wenzelm@36993
   134
    {
wenzelm@37164
   135
      val font = new Font(font_family, Font.PLAIN, lobo_px(raw_px(font_size)))
wenzelm@37034
   136
      val (font_metrics, margin) =
wenzelm@36993
   137
        Swing_Thread.now {
wenzelm@37164
   138
          val metrics = getFontMetrics(font)
wenzelm@37034
   139
          (metrics, (getWidth() / (metrics.charWidth(Symbol.spc) max 1) - 4) max 20)
wenzelm@36993
   140
        }
wenzelm@37034
   141
      if (current_font_metrics == null ||
wenzelm@37164
   142
          current_font_family != font_family ||
wenzelm@37034
   143
          current_font_size != font_size ||
wenzelm@37034
   144
          current_margin != margin)
wenzelm@37034
   145
      {
wenzelm@37034
   146
        current_font_metrics = font_metrics
wenzelm@37164
   147
        current_font_family = font_family
wenzelm@37034
   148
        current_font_size = font_size
wenzelm@37034
   149
        current_margin = margin
wenzelm@37014
   150
        refresh()
wenzelm@36993
   151
      }
wenzelm@36814
   152
    }
wenzelm@36814
   153
wenzelm@37014
   154
    def refresh() { render(current_body) }
wenzelm@37014
   155
wenzelm@39590
   156
    def render_document(text: String)
wenzelm@39590
   157
    {
wenzelm@39590
   158
      val doc = builder.parse(new InputSourceImpl(new StringReader(text), "http://localhost"))
wenzelm@39590
   159
      Swing_Thread.later { setDocument(doc, rcontext) }
wenzelm@39590
   160
    }
wenzelm@39590
   161
wenzelm@38573
   162
    def render(body: XML.Body)
wenzelm@36993
   163
    {
wenzelm@36993
   164
      current_body = body
wenzelm@36993
   165
      val html_body =
wenzelm@36993
   166
        current_body.flatMap(div =>
wenzelm@37034
   167
          Pretty.formatted(List(div), current_margin, Pretty.font_metric(current_font_metrics))
wenzelm@38235
   168
            .map(t =>
wenzelm@38444
   169
              XML.Elem(Markup(HTML.PRE, List((Markup.CLASS, Markup.MESSAGE))),
wenzelm@38444
   170
                HTML.spans(t, true))))
wenzelm@37034
   171
      val doc =
wenzelm@37034
   172
        builder.parse(
wenzelm@37164
   173
          new InputSourceImpl(
wenzelm@37164
   174
            new StringReader(template(current_font_family, current_font_size)), "http://localhost"))
wenzelm@37034
   175
      doc.removeChild(doc.getLastChild())
wenzelm@37034
   176
      doc.appendChild(XML.document_node(doc, XML.elem(HTML.BODY, html_body)))
wenzelm@37034
   177
      Swing_Thread.later { setDocument(doc, rcontext) }
wenzelm@37034
   178
    }
wenzelm@34775
   179
wenzelm@37034
   180
wenzelm@37034
   181
    /* main loop */
wenzelm@36993
   182
wenzelm@37164
   183
    resize(initial_font_family, initial_font_size)
wenzelm@34775
   184
wenzelm@34765
   185
    loop {
wenzelm@34765
   186
      react {
wenzelm@37164
   187
        case Resize(font_family, font_size) => resize(font_family, font_size)
wenzelm@37034
   188
        case Refresh => refresh()
wenzelm@39590
   189
        case Render_Document(text) => render_document(text)
wenzelm@37034
   190
        case Render(body) => render(body)
wenzelm@34769
   191
        case bad => System.err.println("main_actor: ignoring bad message " + bad)
wenzelm@34765
   192
      }
wenzelm@34765
   193
    }
wenzelm@34765
   194
  }
wenzelm@34765
   195
wenzelm@37034
   196
wenzelm@37034
   197
  /* external methods */
wenzelm@37034
   198
wenzelm@37164
   199
  def resize(font_family: String, font_size: Int) { main_actor ! Resize(font_family, font_size) }
wenzelm@37014
   200
  def refresh() { main_actor ! Refresh }
wenzelm@39590
   201
  def render_document(text: String) { main_actor ! Render_Document(text) }
wenzelm@38573
   202
  def render(body: XML.Body) { main_actor ! Render(body) }
wenzelm@34765
   203
}