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