src/Tools/jEdit/src/text_area_painter.scala
author wenzelm
Wed Jun 15 21:11:53 2011 +0200 (2011-06-15)
changeset 43404 c8369f3d88a1
parent 43398 c3e2a361b418
child 43414 f0770743b7ec
permissions -rw-r--r--
uniform use of Document_View.robust_body;
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@43393
    12
import java.awt.{Graphics2D, Shape}
wenzelm@43392
    13
import java.awt.font.TextAttribute
wenzelm@43392
    14
import java.text.AttributedString
wenzelm@43369
    15
import java.util.ArrayList
wenzelm@43369
    16
wenzelm@43369
    17
import org.gjt.sp.jedit.Debug
wenzelm@43369
    18
import org.gjt.sp.jedit.syntax.{DisplayTokenHandler, Chunk}
wenzelm@43392
    19
import org.gjt.sp.jedit.textarea.{TextAreaExtension, TextAreaPainter, JEditTextArea}
wenzelm@43369
    20
wenzelm@43369
    21
wenzelm@43381
    22
class Text_Area_Painter(doc_view: Document_View)
wenzelm@43369
    23
{
wenzelm@43381
    24
  private val model = doc_view.model
wenzelm@43382
    25
  private val buffer = model.buffer
wenzelm@43376
    26
  private val text_area = doc_view.text_area
wenzelm@43376
    27
wenzelm@43392
    28
wenzelm@43392
    29
  /* original painters */
wenzelm@43392
    30
wenzelm@43392
    31
  private def pick_extension(name: String): TextAreaExtension =
wenzelm@43369
    32
  {
wenzelm@43369
    33
    text_area.getPainter.getExtensions.iterator.filter(x => x.getClass.getName == name).toList
wenzelm@43369
    34
    match {
wenzelm@43369
    35
      case List(x) => x
wenzelm@43369
    36
      case _ => error("Expected exactly one " + name)
wenzelm@43369
    37
    }
wenzelm@43369
    38
  }
wenzelm@43369
    39
wenzelm@43392
    40
  private val orig_text_painter =
wenzelm@43392
    41
    pick_extension("org.gjt.sp.jedit.textarea.TextAreaPainter$PaintText")
wenzelm@43392
    42
wenzelm@43392
    43
wenzelm@43393
    44
  /* common painter state */
wenzelm@43381
    45
wenzelm@43393
    46
  @volatile private var painter_snapshot: Document.Snapshot = null
wenzelm@43393
    47
  @volatile private var painter_clip: Shape = null
wenzelm@43381
    48
wenzelm@43393
    49
  private val set_state = new TextAreaExtension
wenzelm@43381
    50
  {
wenzelm@43381
    51
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
    52
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
    53
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
    54
    {
wenzelm@43404
    55
      doc_view.robust_body(()) {
wenzelm@43404
    56
        painter_snapshot = model.snapshot()
wenzelm@43404
    57
        painter_clip = gfx.getClip
wenzelm@43404
    58
      }
wenzelm@43381
    59
    }
wenzelm@43381
    60
  }
wenzelm@43381
    61
wenzelm@43393
    62
  private val reset_state = new TextAreaExtension
wenzelm@43369
    63
  {
wenzelm@43381
    64
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
    65
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
    66
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
    67
    {
wenzelm@43404
    68
      doc_view.robust_body(()) {
wenzelm@43404
    69
        painter_snapshot = null
wenzelm@43404
    70
        painter_clip = null
wenzelm@43404
    71
      }
wenzelm@43381
    72
    }
wenzelm@43381
    73
  }
wenzelm@43381
    74
wenzelm@43381
    75
wenzelm@43381
    76
  /* text background */
wenzelm@43376
    77
wenzelm@43381
    78
  private val background_painter = new TextAreaExtension
wenzelm@43381
    79
  {
wenzelm@43381
    80
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
    81
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
    82
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
    83
    {
wenzelm@43404
    84
      doc_view.robust_body(()) {
wenzelm@43393
    85
        val snapshot = painter_snapshot
wenzelm@43381
    86
        val ascent = text_area.getPainter.getFontMetrics.getAscent
wenzelm@43376
    87
wenzelm@43381
    88
        for (i <- 0 until physical_lines.length) {
wenzelm@43381
    89
          if (physical_lines(i) != -1) {
wenzelm@43381
    90
            val line_range = doc_view.proper_line_range(start(i), end(i))
wenzelm@43376
    91
wenzelm@43381
    92
            // background color: status
wenzelm@43381
    93
            val cmds = snapshot.node.command_range(snapshot.revert(line_range))
wenzelm@43381
    94
            for {
wenzelm@43381
    95
              (command, command_start) <- cmds if !command.is_ignored
wenzelm@43381
    96
              val range = line_range.restrict(snapshot.convert(command.range + command_start))
wenzelm@43381
    97
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@43381
    98
              color <- Isabelle_Markup.status_color(snapshot, command)
wenzelm@43381
    99
            } {
wenzelm@43381
   100
              gfx.setColor(color)
wenzelm@43381
   101
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@43381
   102
            }
wenzelm@43376
   103
wenzelm@43381
   104
            // background color (1): markup
wenzelm@43381
   105
            for {
wenzelm@43381
   106
              Text.Info(range, Some(color)) <-
wenzelm@43381
   107
                snapshot.select_markup(line_range)(Isabelle_Markup.background1).iterator
wenzelm@43381
   108
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@43381
   109
            } {
wenzelm@43381
   110
              gfx.setColor(color)
wenzelm@43381
   111
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@43381
   112
            }
wenzelm@43376
   113
wenzelm@43381
   114
            // background color (2): markup
wenzelm@43381
   115
            for {
wenzelm@43381
   116
              Text.Info(range, Some(color)) <-
wenzelm@43381
   117
                snapshot.select_markup(line_range)(Isabelle_Markup.background2).iterator
wenzelm@43381
   118
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@43381
   119
            } {
wenzelm@43381
   120
              gfx.setColor(color)
wenzelm@43381
   121
              gfx.fillRect(r.x + 2, y + i * line_height + 2, r.length - 4, line_height - 4)
wenzelm@43376
   122
            }
wenzelm@43381
   123
wenzelm@43381
   124
            // sub-expression highlighting -- potentially from other snapshot
wenzelm@43381
   125
            doc_view.highlight_range match {
wenzelm@43381
   126
              case Some((range, color)) if line_range.overlaps(range) =>
wenzelm@43381
   127
                Isabelle.gfx_range(text_area, line_range.restrict(range)) match {
wenzelm@43381
   128
                  case None =>
wenzelm@43381
   129
                  case Some(r) =>
wenzelm@43381
   130
                    gfx.setColor(color)
wenzelm@43381
   131
                    gfx.drawRect(r.x, y + i * line_height, r.length - 1, line_height - 1)
wenzelm@43381
   132
                }
wenzelm@43381
   133
              case _ =>
wenzelm@43381
   134
            }
wenzelm@43381
   135
wenzelm@43381
   136
            // squiggly underline
wenzelm@43381
   137
            for {
wenzelm@43381
   138
              Text.Info(range, Some(color)) <-
wenzelm@43381
   139
                snapshot.select_markup(line_range)(Isabelle_Markup.message).iterator
wenzelm@43381
   140
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@43381
   141
            } {
wenzelm@43381
   142
              gfx.setColor(color)
wenzelm@43381
   143
              val x0 = (r.x / 2) * 2
wenzelm@43381
   144
              val y0 = r.y + ascent + 1
wenzelm@43381
   145
              for (x1 <- Range(x0, x0 + r.length, 2)) {
wenzelm@43381
   146
                val y1 = if (x1 % 4 < 2) y0 else y0 + 1
wenzelm@43381
   147
                gfx.drawLine(x1, y1, x1 + 1, y1)
wenzelm@43376
   148
              }
wenzelm@43376
   149
            }
wenzelm@43376
   150
          }
wenzelm@43376
   151
        }
wenzelm@43376
   152
      }
wenzelm@43381
   153
    }
wenzelm@43381
   154
  }
wenzelm@43381
   155
wenzelm@43381
   156
wenzelm@43381
   157
  /* text */
wenzelm@43369
   158
wenzelm@43393
   159
  def char_width(): Int =
wenzelm@43393
   160
  {
wenzelm@43393
   161
    val painter = text_area.getPainter
wenzelm@43393
   162
    val font = painter.getFont
wenzelm@43393
   163
    val font_context = painter.getFontRenderContext
wenzelm@43393
   164
    font.getStringBounds(" ", font_context).getWidth.round.toInt
wenzelm@43393
   165
  }
wenzelm@43393
   166
wenzelm@43382
   167
  private def line_infos(physical_lines: Iterator[Int]): Map[Text.Offset, Chunk] =
wenzelm@43382
   168
  {
wenzelm@43382
   169
    val painter = text_area.getPainter
wenzelm@43382
   170
    val font = painter.getFont
wenzelm@43382
   171
    val font_context = painter.getFontRenderContext
wenzelm@43382
   172
wenzelm@43382
   173
    // see org.gjt.sp.jedit.textarea.TextArea.propertiesChanged
wenzelm@43382
   174
    // see org.gjt.sp.jedit.textarea.TextArea.setMaxLineLength
wenzelm@43382
   175
    val margin =
wenzelm@43382
   176
      if (buffer.getStringProperty("wrap") != "soft") 0.0f
wenzelm@43382
   177
      else {
wenzelm@43382
   178
        val max = buffer.getIntegerProperty("maxLineLen", 0)
wenzelm@43382
   179
        if (max > 0) font.getStringBounds(" " * max, font_context).getWidth.toInt
wenzelm@43393
   180
        else painter.getWidth - char_width() * 3
wenzelm@43382
   181
      }.toFloat
wenzelm@43382
   182
wenzelm@43382
   183
    val out = new ArrayList[Chunk]
wenzelm@43382
   184
    val handler = new DisplayTokenHandler
wenzelm@43382
   185
wenzelm@43382
   186
    var result = Map[Text.Offset, Chunk]()
wenzelm@43382
   187
    for (line <- physical_lines) {
wenzelm@43382
   188
      out.clear
wenzelm@43382
   189
      handler.init(painter.getStyles, font_context, painter, out, margin)
wenzelm@43382
   190
      buffer.markTokens(line, handler)
wenzelm@43382
   191
wenzelm@43382
   192
      val line_start = buffer.getLineStartOffset(line)
wenzelm@43382
   193
      for (i <- 0 until out.size) {
wenzelm@43382
   194
        val chunk = out.get(i)
wenzelm@43382
   195
        result += (line_start + chunk.offset -> chunk)
wenzelm@43382
   196
      }
wenzelm@43382
   197
    }
wenzelm@43382
   198
    result
wenzelm@43382
   199
  }
wenzelm@43382
   200
wenzelm@43382
   201
  private def paint_chunk_list(gfx: Graphics2D,
wenzelm@43392
   202
    offset: Text.Offset, caret_offset: Text.Offset, head: Chunk, x: Float, y: Float): Float =
wenzelm@43382
   203
  {
wenzelm@43382
   204
    val clip_rect = gfx.getClipBounds
wenzelm@43392
   205
    val painter = text_area.getPainter
wenzelm@43392
   206
    val font_context = painter.getFontRenderContext
wenzelm@43382
   207
wenzelm@43382
   208
    var w = 0.0f
wenzelm@43382
   209
    var chunk_offset = offset
wenzelm@43382
   210
    var chunk = head
wenzelm@43382
   211
    while (chunk != null) {
wenzelm@43382
   212
      val chunk_length = if (chunk.str == null) 0 else chunk.str.length
wenzelm@43382
   213
wenzelm@43382
   214
      if (x + w + chunk.width > clip_rect.x &&
wenzelm@43382
   215
          x + w < clip_rect.x + clip_rect.width &&
wenzelm@43382
   216
          chunk.accessable && chunk.visible)
wenzelm@43382
   217
      {
wenzelm@43382
   218
        val chunk_range = Text.Range(chunk_offset, chunk_offset + chunk_length)
wenzelm@43382
   219
        val chunk_font = chunk.style.getFont
wenzelm@43382
   220
        val chunk_color = chunk.style.getForegroundColor
wenzelm@43382
   221
wenzelm@43382
   222
        val markup = painter_snapshot.select_markup(chunk_range)(Isabelle_Markup.foreground)
wenzelm@43382
   223
wenzelm@43382
   224
        gfx.setFont(chunk_font)
wenzelm@43382
   225
        if (!Debug.DISABLE_GLYPH_VECTOR && chunk.gv != null &&
wenzelm@43392
   226
            markup.forall(info => info.info.isEmpty) &&
wenzelm@43392
   227
            !chunk_range.contains(caret_offset)) {
wenzelm@43382
   228
          gfx.setColor(chunk_color)
wenzelm@43382
   229
          gfx.drawGlyphVector(chunk.gv, x + w, y)
wenzelm@43382
   230
        }
wenzelm@43382
   231
        else {
wenzelm@43383
   232
          var x1 = x + w
wenzelm@43382
   233
          for (Text.Info(range, info) <- markup) {
wenzelm@43382
   234
            val str = chunk.str.substring(range.start - chunk_offset, range.stop - chunk_offset)
wenzelm@43382
   235
            gfx.setColor(info.getOrElse(chunk_color))
wenzelm@43392
   236
            if (range.contains(caret_offset)) {
wenzelm@43392
   237
              val astr = new AttributedString(str)
wenzelm@43392
   238
              val i = caret_offset - range.start
wenzelm@43392
   239
              astr.addAttribute(TextAttribute.FONT, chunk_font)
wenzelm@43392
   240
              astr.addAttribute(TextAttribute.FOREGROUND, painter.getCaretColor, i, i + 1)
wenzelm@43392
   241
              astr.addAttribute(TextAttribute.SWAP_COLORS, TextAttribute.SWAP_COLORS_ON, i, i + 1)
wenzelm@43392
   242
              gfx.drawString(astr.getIterator, x1.toInt, y.toInt)
wenzelm@43392
   243
            }
wenzelm@43392
   244
            else {
wenzelm@43392
   245
              gfx.drawString(str, x1.toInt, y.toInt)
wenzelm@43392
   246
            }
wenzelm@43383
   247
            x1 += chunk_font.getStringBounds(str, font_context).getWidth.toFloat
wenzelm@43382
   248
          }
wenzelm@43382
   249
        }
wenzelm@43382
   250
      }
wenzelm@43382
   251
      w += chunk.width
wenzelm@43382
   252
      chunk_offset += chunk_length
wenzelm@43382
   253
      chunk = chunk.next.asInstanceOf[Chunk]
wenzelm@43382
   254
    }
wenzelm@43382
   255
    w
wenzelm@43382
   256
  }
wenzelm@43382
   257
wenzelm@43381
   258
  private val text_painter = new TextAreaExtension
wenzelm@43381
   259
  {
wenzelm@43381
   260
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
   261
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43382
   262
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
   263
    {
wenzelm@43404
   264
      doc_view.robust_body(()) {
wenzelm@43381
   265
        val clip = gfx.getClip
wenzelm@43381
   266
        val x0 = text_area.getHorizontalOffset
wenzelm@43382
   267
        val fm = text_area.getPainter.getFontMetrics
wenzelm@43382
   268
        var y0 = y + fm.getHeight - (fm.getLeading + 1) - fm.getDescent
wenzelm@43372
   269
wenzelm@43392
   270
        val caret_offset =
wenzelm@43392
   271
          if (text_area.hasFocus) text_area.getCaretPosition
wenzelm@43392
   272
          else -1
wenzelm@43392
   273
wenzelm@43382
   274
        val infos = line_infos(physical_lines.iterator.filter(i => i != -1))
wenzelm@43381
   275
        for (i <- 0 until physical_lines.length) {
wenzelm@43381
   276
          if (physical_lines(i) != -1) {
wenzelm@43392
   277
            val x1 =
wenzelm@43392
   278
              infos.get(start(i)) match {
wenzelm@43392
   279
                case None => x0
wenzelm@43392
   280
                case Some(chunk) =>
wenzelm@43392
   281
                  gfx.clipRect(x0, y + line_height * i, Integer.MAX_VALUE, line_height)
wenzelm@43392
   282
                  val w = paint_chunk_list(gfx, start(i), caret_offset, chunk, x0, y0).toInt
wenzelm@43392
   283
                  x0 + w.toInt
wenzelm@43392
   284
              }
wenzelm@43392
   285
            gfx.clipRect(x1, 0, Integer.MAX_VALUE, Integer.MAX_VALUE)
wenzelm@43392
   286
            orig_text_painter.paintValidLine(gfx,
wenzelm@43392
   287
              first_line + i, physical_lines(i), start(i), end(i), y + line_height * i)
wenzelm@43392
   288
            gfx.setClip(clip)
wenzelm@43369
   289
          }
wenzelm@43381
   290
          y0 += line_height
wenzelm@43369
   291
        }
wenzelm@43369
   292
      }
wenzelm@43369
   293
    }
wenzelm@43369
   294
  }
wenzelm@43369
   295
wenzelm@43369
   296
wenzelm@43393
   297
  /* caret -- outside of text range */
wenzelm@43393
   298
wenzelm@43393
   299
  private class Caret_Painter(before: Boolean) extends TextAreaExtension
wenzelm@43393
   300
  {
wenzelm@43393
   301
    override def paintValidLine(gfx: Graphics2D,
wenzelm@43393
   302
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
wenzelm@43393
   303
    {
wenzelm@43404
   304
      doc_view.robust_body(()) {
wenzelm@43404
   305
        if (before) gfx.clipRect(0, 0, 0, 0)
wenzelm@43404
   306
        else gfx.setClip(painter_clip)
wenzelm@43404
   307
      }
wenzelm@43393
   308
    }
wenzelm@43393
   309
  }
wenzelm@43393
   310
wenzelm@43393
   311
  private val before_caret_painter1 = new Caret_Painter(true)
wenzelm@43393
   312
  private val after_caret_painter1 = new Caret_Painter(false)
wenzelm@43393
   313
  private val before_caret_painter2 = new Caret_Painter(true)
wenzelm@43393
   314
  private val after_caret_painter2 = new Caret_Painter(false)
wenzelm@43393
   315
wenzelm@43393
   316
  private val caret_painter = new TextAreaExtension
wenzelm@43393
   317
  {
wenzelm@43393
   318
    override def paintValidLine(gfx: Graphics2D,
wenzelm@43393
   319
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
wenzelm@43393
   320
    {
wenzelm@43404
   321
      doc_view.robust_body(()) {
wenzelm@43398
   322
        if (text_area.hasFocus) {
wenzelm@43398
   323
          val caret = text_area.getCaretPosition
wenzelm@43398
   324
          if (start <= caret && caret == end - 1) {
wenzelm@43398
   325
            val painter = text_area.getPainter
wenzelm@43398
   326
            val fm = painter.getFontMetrics
wenzelm@43393
   327
wenzelm@43398
   328
            val offset = caret - text_area.getLineStartOffset(physical_line)
wenzelm@43398
   329
            val x = text_area.offsetToXY(physical_line, offset).x
wenzelm@43398
   330
            gfx.setColor(painter.getCaretColor)
wenzelm@43398
   331
            gfx.drawRect(x, y, char_width() - 1, fm.getHeight - 1)
wenzelm@43398
   332
          }
wenzelm@43393
   333
        }
wenzelm@43393
   334
      }
wenzelm@43393
   335
    }
wenzelm@43393
   336
  }
wenzelm@43393
   337
wenzelm@43393
   338
wenzelm@43369
   339
  /* activation */
wenzelm@43369
   340
wenzelm@43369
   341
  def activate()
wenzelm@43369
   342
  {
wenzelm@43369
   343
    val painter = text_area.getPainter
wenzelm@43393
   344
    painter.addExtension(TextAreaPainter.LOWEST_LAYER, set_state)
wenzelm@43381
   345
    painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, background_painter)
wenzelm@43381
   346
    painter.addExtension(TextAreaPainter.TEXT_LAYER, text_painter)
wenzelm@43393
   347
    painter.addExtension(TextAreaPainter.CARET_LAYER - 1, before_caret_painter1)
wenzelm@43393
   348
    painter.addExtension(TextAreaPainter.CARET_LAYER + 1, after_caret_painter1)
wenzelm@43393
   349
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER - 1, before_caret_painter2)
wenzelm@43393
   350
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 1, after_caret_painter2)
wenzelm@43393
   351
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 2, caret_painter)
wenzelm@43393
   352
    painter.addExtension(TextAreaPainter.HIGHEST_LAYER, reset_state)
wenzelm@43369
   353
    painter.removeExtension(orig_text_painter)
wenzelm@43369
   354
  }
wenzelm@43369
   355
wenzelm@43369
   356
  def deactivate()
wenzelm@43369
   357
  {
wenzelm@43369
   358
    val painter = text_area.getPainter
wenzelm@43396
   359
    painter.addExtension(TextAreaPainter.TEXT_LAYER, orig_text_painter)
wenzelm@43393
   360
    painter.removeExtension(reset_state)
wenzelm@43393
   361
    painter.removeExtension(caret_painter)
wenzelm@43393
   362
    painter.removeExtension(after_caret_painter2)
wenzelm@43393
   363
    painter.removeExtension(before_caret_painter2)
wenzelm@43393
   364
    painter.removeExtension(after_caret_painter1)
wenzelm@43393
   365
    painter.removeExtension(before_caret_painter1)
wenzelm@43381
   366
    painter.removeExtension(text_painter)
wenzelm@43381
   367
    painter.removeExtension(background_painter)
wenzelm@43393
   368
    painter.removeExtension(set_state)
wenzelm@43369
   369
  }
wenzelm@43369
   370
}
wenzelm@43369
   371