src/Tools/jEdit/src/html_panel.scala
author wenzelm
Mon Jul 04 22:11:32 2011 +0200 (2011-07-04)
changeset 43661 39fdbd814c7f
parent 43606 e1a09c2a6248
child 43669 9d34288e9351
permissions -rw-r--r--
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
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@43661
    95
  Isabelle_System.try_read(
wenzelm@43661
    96
    Isabelle_System.getenv_strict("JEDIT_STYLE_SHEETS").split(":").map(Path.explode))
wenzelm@37015
    97
wenzelm@37015
    98
  private val template_tail =
wenzelm@34765
    99
"""
wenzelm@34765
   100
</style>
wenzelm@34765
   101
</head>
wenzelm@34765
   102
<body/>
wenzelm@34765
   103
</html>
wenzelm@34765
   104
"""
wenzelm@37015
   105
wenzelm@37164
   106
  private def template(font_family: String, font_size: Int): String =
wenzelm@37015
   107
    template_head +
wenzelm@37202
   108
    "body { font-family: " + font_family + "; font-size: " + raw_px(font_size) + "px; }" +
wenzelm@37015
   109
    template_tail
wenzelm@37015
   110
wenzelm@37015
   111
wenzelm@37034
   112
  /** main actor **/
wenzelm@37034
   113
wenzelm@37034
   114
  /* internal messages */
wenzelm@37034
   115
wenzelm@37164
   116
  private case class Resize(font_family: String, font_size: Int)
wenzelm@39590
   117
  private case class Render_Document(text: String)
wenzelm@38573
   118
  private case class Render(body: XML.Body)
wenzelm@39740
   119
  private case class Render_Sync(body: XML.Body)
wenzelm@37034
   120
  private case object Refresh
wenzelm@34765
   121
wenzelm@37034
   122
  private val main_actor = actor {
wenzelm@37034
   123
wenzelm@37034
   124
    /* internal state */
wenzelm@37034
   125
wenzelm@37034
   126
    var current_font_metrics: FontMetrics = null
wenzelm@37164
   127
    var current_font_family = ""
wenzelm@37034
   128
    var current_font_size: Int = 0
wenzelm@37034
   129
    var current_margin: Int = 0
wenzelm@38573
   130
    var current_body: XML.Body = Nil
wenzelm@34765
   131
wenzelm@37164
   132
    def resize(font_family: String, font_size: Int)
wenzelm@36993
   133
    {
wenzelm@37164
   134
      val font = new Font(font_family, Font.PLAIN, lobo_px(raw_px(font_size)))
wenzelm@37034
   135
      val (font_metrics, margin) =
wenzelm@36993
   136
        Swing_Thread.now {
wenzelm@37164
   137
          val metrics = getFontMetrics(font)
wenzelm@37034
   138
          (metrics, (getWidth() / (metrics.charWidth(Symbol.spc) max 1) - 4) max 20)
wenzelm@36993
   139
        }
wenzelm@37034
   140
      if (current_font_metrics == null ||
wenzelm@37164
   141
          current_font_family != font_family ||
wenzelm@37034
   142
          current_font_size != font_size ||
wenzelm@37034
   143
          current_margin != margin)
wenzelm@37034
   144
      {
wenzelm@37034
   145
        current_font_metrics = font_metrics
wenzelm@37164
   146
        current_font_family = font_family
wenzelm@37034
   147
        current_font_size = font_size
wenzelm@37034
   148
        current_margin = margin
wenzelm@37014
   149
        refresh()
wenzelm@36993
   150
      }
wenzelm@36814
   151
    }
wenzelm@36814
   152
wenzelm@37014
   153
    def refresh() { render(current_body) }
wenzelm@37014
   154
wenzelm@39590
   155
    def render_document(text: String)
wenzelm@39590
   156
    {
wenzelm@39590
   157
      val doc = builder.parse(new InputSourceImpl(new StringReader(text), "http://localhost"))
wenzelm@39590
   158
      Swing_Thread.later { setDocument(doc, rcontext) }
wenzelm@39590
   159
    }
wenzelm@39590
   160
wenzelm@38573
   161
    def render(body: XML.Body)
wenzelm@36993
   162
    {
wenzelm@36993
   163
      current_body = body
wenzelm@36993
   164
      val html_body =
wenzelm@36993
   165
        current_body.flatMap(div =>
wenzelm@37034
   166
          Pretty.formatted(List(div), current_margin, Pretty.font_metric(current_font_metrics))
wenzelm@38235
   167
            .map(t =>
wenzelm@43661
   168
              XML.Elem(Markup(HTML.PRE, List((HTML.CLASS, Markup.MESSAGE))), HTML.spans(t, true))))
wenzelm@37034
   169
      val doc =
wenzelm@37034
   170
        builder.parse(
wenzelm@37164
   171
          new InputSourceImpl(
wenzelm@37164
   172
            new StringReader(template(current_font_family, current_font_size)), "http://localhost"))
wenzelm@37034
   173
      doc.removeChild(doc.getLastChild())
wenzelm@37034
   174
      doc.appendChild(XML.document_node(doc, XML.elem(HTML.BODY, html_body)))
wenzelm@37034
   175
      Swing_Thread.later { setDocument(doc, rcontext) }
wenzelm@37034
   176
    }
wenzelm@34775
   177
wenzelm@37034
   178
wenzelm@37034
   179
    /* main loop */
wenzelm@36993
   180
wenzelm@37164
   181
    resize(initial_font_family, initial_font_size)
wenzelm@34775
   182
wenzelm@34765
   183
    loop {
wenzelm@34765
   184
      react {
wenzelm@37164
   185
        case Resize(font_family, font_size) => resize(font_family, font_size)
wenzelm@37034
   186
        case Refresh => refresh()
wenzelm@39590
   187
        case Render_Document(text) => render_document(text)
wenzelm@37034
   188
        case Render(body) => render(body)
wenzelm@39740
   189
        case Render_Sync(body) => render(body); reply(())
wenzelm@34769
   190
        case bad => System.err.println("main_actor: ignoring bad message " + bad)
wenzelm@34765
   191
      }
wenzelm@34765
   192
    }
wenzelm@34765
   193
  }
wenzelm@34765
   194
wenzelm@37034
   195
wenzelm@37034
   196
  /* external methods */
wenzelm@37034
   197
wenzelm@37164
   198
  def resize(font_family: String, font_size: Int) { main_actor ! Resize(font_family, font_size) }
wenzelm@37014
   199
  def refresh() { main_actor ! Refresh }
wenzelm@39590
   200
  def render_document(text: String) { main_actor ! Render_Document(text) }
wenzelm@38573
   201
  def render(body: XML.Body) { main_actor ! Render(body) }
wenzelm@39740
   202
  def render_sync(body: XML.Body) { main_actor !? Render_Sync(body) }
wenzelm@34765
   203
}