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