src/Tools/jEdit/src/text_area_painter.scala
author wenzelm
Tue Jun 14 13:18:36 2011 +0200 (2011-06-14)
changeset 43383 63fc6b5ef8ac
parent 43382 5d9d34bdec25
child 43392 fe4b8c52b1cc
permissions -rw-r--r--
tuned;
wenzelm@43381
     1
/*  Title:      Tools/jEdit/src/text_area_painter.scala
wenzelm@43369
     2
    Author:     Makarius
wenzelm@43369
     3
wenzelm@43381
     4
Painter setup for main jEdit text area.
wenzelm@43369
     5
*/
wenzelm@43369
     6
wenzelm@43369
     7
package isabelle.jedit
wenzelm@43369
     8
wenzelm@43369
     9
wenzelm@43369
    10
import isabelle._
wenzelm@43369
    11
wenzelm@43383
    12
import java.awt.Graphics2D
wenzelm@43369
    13
import java.util.ArrayList
wenzelm@43369
    14
wenzelm@43369
    15
import org.gjt.sp.jedit.Debug
wenzelm@43369
    16
import org.gjt.sp.jedit.syntax.{DisplayTokenHandler, Chunk}
wenzelm@43383
    17
import org.gjt.sp.jedit.textarea.{TextAreaExtension, TextAreaPainter}
wenzelm@43369
    18
wenzelm@43369
    19
wenzelm@43381
    20
class Text_Area_Painter(doc_view: Document_View)
wenzelm@43369
    21
{
wenzelm@43381
    22
  private val model = doc_view.model
wenzelm@43382
    23
  private val buffer = model.buffer
wenzelm@43376
    24
  private val text_area = doc_view.text_area
wenzelm@43376
    25
wenzelm@43369
    26
  private val orig_text_painter: TextAreaExtension =
wenzelm@43369
    27
  {
wenzelm@43369
    28
    val name = "org.gjt.sp.jedit.textarea.TextAreaPainter$PaintText"
wenzelm@43369
    29
    text_area.getPainter.getExtensions.iterator.filter(x => x.getClass.getName == name).toList
wenzelm@43369
    30
    match {
wenzelm@43369
    31
      case List(x) => x
wenzelm@43369
    32
      case _ => error("Expected exactly one " + name)
wenzelm@43369
    33
    }
wenzelm@43369
    34
  }
wenzelm@43369
    35
wenzelm@43381
    36
wenzelm@43381
    37
  /* painter snapshot */
wenzelm@43381
    38
wenzelm@43381
    39
  @volatile private var _painter_snapshot: Option[Document.Snapshot] = None
wenzelm@43381
    40
wenzelm@43381
    41
  private def painter_snapshot(): Document.Snapshot =
wenzelm@43381
    42
    _painter_snapshot match {
wenzelm@43381
    43
      case Some(snapshot) => snapshot
wenzelm@43381
    44
      case None => error("Missing document snapshot for text area painter")
wenzelm@43381
    45
    }
wenzelm@43381
    46
wenzelm@43381
    47
  private val set_snapshot = new TextAreaExtension
wenzelm@43381
    48
  {
wenzelm@43381
    49
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
    50
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
    51
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
    52
    {
wenzelm@43381
    53
      _painter_snapshot = Some(model.snapshot())
wenzelm@43381
    54
    }
wenzelm@43381
    55
  }
wenzelm@43381
    56
wenzelm@43381
    57
  private val reset_snapshot = new TextAreaExtension
wenzelm@43369
    58
  {
wenzelm@43381
    59
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
    60
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
    61
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
    62
    {
wenzelm@43381
    63
      _painter_snapshot = None
wenzelm@43381
    64
    }
wenzelm@43381
    65
  }
wenzelm@43381
    66
wenzelm@43381
    67
wenzelm@43381
    68
  /* text background */
wenzelm@43376
    69
wenzelm@43381
    70
  private val background_painter = new TextAreaExtension
wenzelm@43381
    71
  {
wenzelm@43381
    72
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
    73
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
    74
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
    75
    {
wenzelm@43382
    76
      Isabelle.swing_buffer_lock(buffer) {
wenzelm@43381
    77
        val snapshot = painter_snapshot()
wenzelm@43381
    78
        val ascent = text_area.getPainter.getFontMetrics.getAscent
wenzelm@43376
    79
wenzelm@43381
    80
        for (i <- 0 until physical_lines.length) {
wenzelm@43381
    81
          if (physical_lines(i) != -1) {
wenzelm@43381
    82
            val line_range = doc_view.proper_line_range(start(i), end(i))
wenzelm@43376
    83
wenzelm@43381
    84
            // background color: status
wenzelm@43381
    85
            val cmds = snapshot.node.command_range(snapshot.revert(line_range))
wenzelm@43381
    86
            for {
wenzelm@43381
    87
              (command, command_start) <- cmds if !command.is_ignored
wenzelm@43381
    88
              val range = line_range.restrict(snapshot.convert(command.range + command_start))
wenzelm@43381
    89
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@43381
    90
              color <- Isabelle_Markup.status_color(snapshot, command)
wenzelm@43381
    91
            } {
wenzelm@43381
    92
              gfx.setColor(color)
wenzelm@43381
    93
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@43381
    94
            }
wenzelm@43376
    95
wenzelm@43381
    96
            // background color (1): markup
wenzelm@43381
    97
            for {
wenzelm@43381
    98
              Text.Info(range, Some(color)) <-
wenzelm@43381
    99
                snapshot.select_markup(line_range)(Isabelle_Markup.background1).iterator
wenzelm@43381
   100
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@43381
   101
            } {
wenzelm@43381
   102
              gfx.setColor(color)
wenzelm@43381
   103
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@43381
   104
            }
wenzelm@43376
   105
wenzelm@43381
   106
            // background color (2): markup
wenzelm@43381
   107
            for {
wenzelm@43381
   108
              Text.Info(range, Some(color)) <-
wenzelm@43381
   109
                snapshot.select_markup(line_range)(Isabelle_Markup.background2).iterator
wenzelm@43381
   110
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@43381
   111
            } {
wenzelm@43381
   112
              gfx.setColor(color)
wenzelm@43381
   113
              gfx.fillRect(r.x + 2, y + i * line_height + 2, r.length - 4, line_height - 4)
wenzelm@43376
   114
            }
wenzelm@43381
   115
wenzelm@43381
   116
            // sub-expression highlighting -- potentially from other snapshot
wenzelm@43381
   117
            doc_view.highlight_range match {
wenzelm@43381
   118
              case Some((range, color)) if line_range.overlaps(range) =>
wenzelm@43381
   119
                Isabelle.gfx_range(text_area, line_range.restrict(range)) match {
wenzelm@43381
   120
                  case None =>
wenzelm@43381
   121
                  case Some(r) =>
wenzelm@43381
   122
                    gfx.setColor(color)
wenzelm@43381
   123
                    gfx.drawRect(r.x, y + i * line_height, r.length - 1, line_height - 1)
wenzelm@43381
   124
                }
wenzelm@43381
   125
              case _ =>
wenzelm@43381
   126
            }
wenzelm@43381
   127
wenzelm@43381
   128
            // squiggly underline
wenzelm@43381
   129
            for {
wenzelm@43381
   130
              Text.Info(range, Some(color)) <-
wenzelm@43381
   131
                snapshot.select_markup(line_range)(Isabelle_Markup.message).iterator
wenzelm@43381
   132
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@43381
   133
            } {
wenzelm@43381
   134
              gfx.setColor(color)
wenzelm@43381
   135
              val x0 = (r.x / 2) * 2
wenzelm@43381
   136
              val y0 = r.y + ascent + 1
wenzelm@43381
   137
              for (x1 <- Range(x0, x0 + r.length, 2)) {
wenzelm@43381
   138
                val y1 = if (x1 % 4 < 2) y0 else y0 + 1
wenzelm@43381
   139
                gfx.drawLine(x1, y1, x1 + 1, y1)
wenzelm@43376
   140
              }
wenzelm@43376
   141
            }
wenzelm@43376
   142
          }
wenzelm@43376
   143
        }
wenzelm@43376
   144
      }
wenzelm@43381
   145
    }
wenzelm@43381
   146
  }
wenzelm@43381
   147
wenzelm@43381
   148
wenzelm@43381
   149
  /* text */
wenzelm@43369
   150
wenzelm@43382
   151
  private def line_infos(physical_lines: Iterator[Int]): Map[Text.Offset, Chunk] =
wenzelm@43382
   152
  {
wenzelm@43382
   153
    val painter = text_area.getPainter
wenzelm@43382
   154
    val font = painter.getFont
wenzelm@43382
   155
    val font_context = painter.getFontRenderContext
wenzelm@43382
   156
wenzelm@43382
   157
    // see org.gjt.sp.jedit.textarea.TextArea.propertiesChanged
wenzelm@43382
   158
    // see org.gjt.sp.jedit.textarea.TextArea.setMaxLineLength
wenzelm@43382
   159
    val margin =
wenzelm@43382
   160
      if (buffer.getStringProperty("wrap") != "soft") 0.0f
wenzelm@43382
   161
      else {
wenzelm@43382
   162
        val max = buffer.getIntegerProperty("maxLineLen", 0)
wenzelm@43382
   163
        if (max > 0) font.getStringBounds(" " * max, font_context).getWidth.toInt
wenzelm@43382
   164
        else painter.getWidth - font.getStringBounds(" ", font_context).getWidth.round.toInt * 3
wenzelm@43382
   165
      }.toFloat
wenzelm@43382
   166
wenzelm@43382
   167
    val out = new ArrayList[Chunk]
wenzelm@43382
   168
    val handler = new DisplayTokenHandler
wenzelm@43382
   169
wenzelm@43382
   170
    var result = Map[Text.Offset, Chunk]()
wenzelm@43382
   171
    for (line <- physical_lines) {
wenzelm@43382
   172
      out.clear
wenzelm@43382
   173
      handler.init(painter.getStyles, font_context, painter, out, margin)
wenzelm@43382
   174
      buffer.markTokens(line, handler)
wenzelm@43382
   175
wenzelm@43382
   176
      val line_start = buffer.getLineStartOffset(line)
wenzelm@43382
   177
      for (i <- 0 until out.size) {
wenzelm@43382
   178
        val chunk = out.get(i)
wenzelm@43382
   179
        result += (line_start + chunk.offset -> chunk)
wenzelm@43382
   180
      }
wenzelm@43382
   181
    }
wenzelm@43382
   182
    result
wenzelm@43382
   183
  }
wenzelm@43382
   184
wenzelm@43382
   185
  private def paint_chunk_list(gfx: Graphics2D,
wenzelm@43382
   186
    offset: Text.Offset, head: Chunk, x: Float, y: Float): Float =
wenzelm@43382
   187
  {
wenzelm@43382
   188
    val clip_rect = gfx.getClipBounds
wenzelm@43382
   189
    val font_context = text_area.getPainter.getFontRenderContext
wenzelm@43382
   190
wenzelm@43382
   191
    var w = 0.0f
wenzelm@43382
   192
    var chunk_offset = offset
wenzelm@43382
   193
    var chunk = head
wenzelm@43382
   194
    while (chunk != null) {
wenzelm@43382
   195
      val chunk_length = if (chunk.str == null) 0 else chunk.str.length
wenzelm@43382
   196
wenzelm@43382
   197
      if (x + w + chunk.width > clip_rect.x &&
wenzelm@43382
   198
          x + w < clip_rect.x + clip_rect.width &&
wenzelm@43382
   199
          chunk.accessable && chunk.visible)
wenzelm@43382
   200
      {
wenzelm@43382
   201
        val chunk_range = Text.Range(chunk_offset, chunk_offset + chunk_length)
wenzelm@43382
   202
        val chunk_font = chunk.style.getFont
wenzelm@43382
   203
        val chunk_color = chunk.style.getForegroundColor
wenzelm@43382
   204
wenzelm@43382
   205
        val markup = painter_snapshot.select_markup(chunk_range)(Isabelle_Markup.foreground)
wenzelm@43382
   206
wenzelm@43382
   207
        gfx.setFont(chunk_font)
wenzelm@43382
   208
        if (!Debug.DISABLE_GLYPH_VECTOR && chunk.gv != null &&
wenzelm@43382
   209
            markup.forall(info => info.info.isEmpty)) {
wenzelm@43382
   210
          gfx.setColor(chunk_color)
wenzelm@43382
   211
          gfx.drawGlyphVector(chunk.gv, x + w, y)
wenzelm@43382
   212
        }
wenzelm@43382
   213
        else {
wenzelm@43383
   214
          var x1 = x + w
wenzelm@43382
   215
          for (Text.Info(range, info) <- markup) {
wenzelm@43382
   216
            val str = chunk.str.substring(range.start - chunk_offset, range.stop - chunk_offset)
wenzelm@43382
   217
            gfx.setColor(info.getOrElse(chunk_color))
wenzelm@43383
   218
            gfx.drawString(str, x1.toInt, y.toInt)
wenzelm@43383
   219
            x1 += chunk_font.getStringBounds(str, font_context).getWidth.toFloat
wenzelm@43382
   220
          }
wenzelm@43382
   221
        }
wenzelm@43382
   222
      }
wenzelm@43382
   223
      w += chunk.width
wenzelm@43382
   224
      chunk_offset += chunk_length
wenzelm@43382
   225
      chunk = chunk.next.asInstanceOf[Chunk]
wenzelm@43382
   226
    }
wenzelm@43382
   227
    w
wenzelm@43382
   228
  }
wenzelm@43382
   229
wenzelm@43381
   230
  private val text_painter = new TextAreaExtension
wenzelm@43381
   231
  {
wenzelm@43381
   232
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
   233
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43382
   234
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
   235
    {
wenzelm@43381
   236
      Isabelle.swing_buffer_lock(buffer) {
wenzelm@43381
   237
        val clip = gfx.getClip
wenzelm@43381
   238
        val x0 = text_area.getHorizontalOffset
wenzelm@43382
   239
        val fm = text_area.getPainter.getFontMetrics
wenzelm@43382
   240
        var y0 = y + fm.getHeight - (fm.getLeading + 1) - fm.getDescent
wenzelm@43372
   241
wenzelm@43382
   242
        val infos = line_infos(physical_lines.iterator.filter(i => i != -1))
wenzelm@43381
   243
        for (i <- 0 until physical_lines.length) {
wenzelm@43381
   244
          if (physical_lines(i) != -1) {
wenzelm@43382
   245
            infos.get(start(i)) match {
wenzelm@43381
   246
              case Some(chunk) =>
wenzelm@43382
   247
                val w = paint_chunk_list(gfx, start(i), chunk, x0, y0).toInt
wenzelm@43381
   248
                gfx.clipRect(x0 + w, 0, Integer.MAX_VALUE, Integer.MAX_VALUE)
wenzelm@43381
   249
                orig_text_painter.paintValidLine(gfx,
wenzelm@43382
   250
                  first_line + i, physical_lines(i), start(i), end(i), y + line_height * i)
wenzelm@43381
   251
                gfx.setClip(clip)
wenzelm@43381
   252
              case None =>
wenzelm@43381
   253
            }
wenzelm@43369
   254
          }
wenzelm@43381
   255
          y0 += line_height
wenzelm@43369
   256
        }
wenzelm@43369
   257
      }
wenzelm@43369
   258
    }
wenzelm@43369
   259
  }
wenzelm@43369
   260
wenzelm@43369
   261
wenzelm@43369
   262
  /* activation */
wenzelm@43369
   263
wenzelm@43369
   264
  def activate()
wenzelm@43369
   265
  {
wenzelm@43369
   266
    val painter = text_area.getPainter
wenzelm@43381
   267
    painter.addExtension(TextAreaPainter.LOWEST_LAYER, set_snapshot)
wenzelm@43381
   268
    painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, background_painter)
wenzelm@43381
   269
    painter.addExtension(TextAreaPainter.TEXT_LAYER, text_painter)
wenzelm@43381
   270
    painter.addExtension(TextAreaPainter.HIGHEST_LAYER, reset_snapshot)
wenzelm@43369
   271
    painter.removeExtension(orig_text_painter)
wenzelm@43369
   272
  }
wenzelm@43369
   273
wenzelm@43369
   274
  def deactivate()
wenzelm@43369
   275
  {
wenzelm@43369
   276
    val painter = text_area.getPainter
wenzelm@43369
   277
    painter.addExtension(TextAreaPainter.TEXT_LAYER, orig_text_painter)
wenzelm@43381
   278
    painter.removeExtension(reset_snapshot)
wenzelm@43381
   279
    painter.removeExtension(text_painter)
wenzelm@43381
   280
    painter.removeExtension(background_painter)
wenzelm@43381
   281
    painter.removeExtension(set_snapshot)
wenzelm@43369
   282
  }
wenzelm@43369
   283
}
wenzelm@43369
   284