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