src/Tools/jEdit/src/text_area_painter.scala
author wenzelm
Sat, 14 Jan 2012 13:11:32 +0100
changeset 46205 07e334ad2e2a
parent 46204 df1369a42393
child 46220 663251a395c2
permissions -rw-r--r--
ignore empty gfx_range; tuned;
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
46204
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    29
  /* text area ranges */
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    30
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    31
  private class Gfx_Range(val x: Int, val y: Int, val length: Int)
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    32
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    33
  private def gfx_range(range: Text.Range): Option[Gfx_Range] =
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    34
  {
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    35
    val p = text_area.offsetToXY(range.start)
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    36
    val q = text_area.offsetToXY(range.stop)
46205
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
    37
    if (p != null && q != null && p.x < q.x && p.y == q.y)
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
    38
      Some(new Gfx_Range(p.x, p.y, q.x - p.x))
46204
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    39
    else None
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    40
  }
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    41
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
    42
43392
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    43
  /* original painters */
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    44
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    45
  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
    46
  {
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    47
    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
    48
    match {
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    49
      case List(x) => x
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    50
      case _ => error("Expected exactly one " + name)
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    51
    }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    52
  }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    53
43392
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    54
  private val orig_text_painter =
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    55
    pick_extension("org.gjt.sp.jedit.textarea.TextAreaPainter$PaintText")
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    56
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    57
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
    58
  /* common painter state */
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    59
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
    60
  @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
    61
  @volatile private var painter_clip: Shape = null
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    62
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
    63
  private val set_state = new TextAreaExtension
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    64
  {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    65
    override def paintScreenLineRange(gfx: Graphics2D,
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    66
      first_line: Int, last_line: Int, physical_lines: Array[Int],
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    67
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    68
    {
43404
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
    69
      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
    70
        painter_snapshot = doc_view.update_snapshot()
43404
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
    71
        painter_clip = gfx.getClip
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
    72
      }
43381
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
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
    76
  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
    77
  {
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    78
    override def paintScreenLineRange(gfx: Graphics2D,
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    79
      first_line: Int, last_line: Int, physical_lines: Array[Int],
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    80
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    81
    {
43404
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
    82
      doc_view.robust_body(()) {
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
    83
        painter_snapshot = null
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
    84
        painter_clip = null
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
    85
      }
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    86
    }
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    87
  }
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    88
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    89
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    90
  /* text background */
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
  private val background_painter = new TextAreaExtension
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    93
  {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    94
    override def paintScreenLineRange(gfx: Graphics2D,
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    95
      first_line: Int, last_line: Int, physical_lines: Array[Int],
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    96
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    97
    {
43404
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
    98
      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
    99
        val snapshot = painter_snapshot
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   100
        val ascent = text_area.getPainter.getFontMetrics.getAscent
43376
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   101
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   102
        for (i <- 0 until physical_lines.length) {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   103
          if (physical_lines(i) != -1) {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   104
            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
   105
46166
4beb2f41ed93 command status color via regular markup;
wenzelm
parents: 45665
diff changeset
   106
            // background color (1)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   107
            for {
46178
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46166
diff changeset
   108
              Text.Info(range, color) <- Isabelle_Rendering.background1(snapshot, line_range)
46204
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
   109
              r <- gfx_range(range)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   110
            } {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   111
              gfx.setColor(color)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   112
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   113
            }
43376
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   114
46166
4beb2f41ed93 command status color via regular markup;
wenzelm
parents: 45665
diff changeset
   115
            // background color (2)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   116
            for {
46178
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46166
diff changeset
   117
              Text.Info(range, color) <- Isabelle_Rendering.background2(snapshot, line_range)
46204
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
   118
              r <- gfx_range(range)
43381
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
            // squiggly underline
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   125
            for {
46178
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46166
diff changeset
   126
              Text.Info(range, color) <- Isabelle_Rendering.message_color(snapshot, line_range)
46204
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
   127
              r <- gfx_range(range)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   128
            } {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   129
              gfx.setColor(color)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   130
              val x0 = (r.x / 2) * 2
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   131
              val y0 = r.y + ascent + 1
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   132
              for (x1 <- Range(x0, x0 + r.length, 2)) {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   133
                val y1 = if (x1 % 4 < 2) y0 else y0 + 1
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   134
                gfx.drawLine(x1, y1, x1 + 1, y1)
43376
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   135
              }
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   136
            }
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   137
          }
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   138
        }
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   139
      }
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   140
    }
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
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   143
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   144
  /* text */
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   145
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   146
  def char_width(): Int =
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   147
  {
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   148
    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
   149
    val font = painter.getFont
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   150
    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
   151
    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
   152
  }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   153
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   154
  private def line_infos(physical_lines: Iterator[Int]): Map[Text.Offset, Chunk] =
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   155
  {
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   156
    val painter = text_area.getPainter
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   157
    val font = painter.getFont
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   158
    val font_context = painter.getFontRenderContext
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   159
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   160
    // see org.gjt.sp.jedit.textarea.TextArea.propertiesChanged
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   161
    // see org.gjt.sp.jedit.textarea.TextArea.setMaxLineLength
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   162
    val margin =
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   163
      if (buffer.getStringProperty("wrap") != "soft") 0.0f
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   164
      else {
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   165
        val max = buffer.getIntegerProperty("maxLineLen", 0)
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   166
        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
   167
        else painter.getWidth - char_width() * 3
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   168
      }.toFloat
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   169
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   170
    val out = new ArrayList[Chunk]
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   171
    val handler = new DisplayTokenHandler
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   172
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   173
    var result = Map[Text.Offset, Chunk]()
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   174
    for (line <- physical_lines) {
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   175
      out.clear
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   176
      handler.init(painter.getStyles, font_context, painter, out, margin)
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   177
      buffer.markTokens(line, handler)
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   178
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   179
      val line_start = buffer.getLineStartOffset(line)
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   180
      for (i <- 0 until out.size) {
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   181
        val chunk = out.get(i)
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   182
        result += (line_start + chunk.offset -> chunk)
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   183
      }
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   184
    }
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   185
    result
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   186
  }
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   187
43448
90aec5043461 more robust caret painting wrt. surrogate characters;
wenzelm
parents: 43435
diff changeset
   188
  private def paint_chunk_list(
43505
0aca4edbfa99 clarified chunk.offset, chunk.length;
wenzelm
parents: 43451
diff changeset
   189
    gfx: Graphics2D, line_start: Text.Offset, head: Chunk, x: Float, y: Float): Float =
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   190
  {
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   191
    val clip_rect = gfx.getClipBounds
43392
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   192
    val painter = text_area.getPainter
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   193
    val font_context = painter.getFontRenderContext
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   194
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   195
    var w = 0.0f
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   196
    var chunk = head
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   197
    while (chunk != null) {
43505
0aca4edbfa99 clarified chunk.offset, chunk.length;
wenzelm
parents: 43451
diff changeset
   198
      val chunk_offset = line_start + chunk.offset
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   199
      if (x + w + chunk.width > clip_rect.x &&
43506
bf7400573617 updated to jedit-4.4.1 and jedit_build-20110622;
wenzelm
parents: 43505
diff changeset
   200
          x + w < clip_rect.x + clip_rect.width && chunk.accessable)
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   201
      {
43505
0aca4edbfa99 clarified chunk.offset, chunk.length;
wenzelm
parents: 43451
diff changeset
   202
        val chunk_range = Text.Range(chunk_offset, chunk_offset + chunk.length)
44662
c8f1d943bfc5 more robust chunk painting wrt. hard tabs, when chunk.str == null;
wenzelm
parents: 44545
diff changeset
   203
        val chunk_str = if (chunk.str == null) " " * chunk.length else chunk.str
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   204
        val chunk_font = chunk.style.getFont
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   205
        val chunk_color = chunk.style.getForegroundColor
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   206
44056
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   207
        def string_width(s: String): Float =
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   208
          if (s.isEmpty) 0.0f
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   209
          else chunk_font.getStringBounds(s, font_context).getWidth.toFloat
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   210
43448
90aec5043461 more robust caret painting wrt. surrogate characters;
wenzelm
parents: 43435
diff changeset
   211
        val caret_range =
90aec5043461 more robust caret painting wrt. surrogate characters;
wenzelm
parents: 43435
diff changeset
   212
          if (text_area.hasFocus) doc_view.caret_range()
90aec5043461 more robust caret painting wrt. surrogate characters;
wenzelm
parents: 43435
diff changeset
   213
          else Text.Range(-1)
90aec5043461 more robust caret painting wrt. surrogate characters;
wenzelm
parents: 43435
diff changeset
   214
43426
24e2e2f0032b more explicit treatment of ranges after revert/convert, which may well distort the overall start/end positions;
wenzelm
parents: 43419
diff changeset
   215
        val markup =
43428
b41dea5772c6 more robust treatment of partial range restriction;
wenzelm
parents: 43426
diff changeset
   216
          for {
46197
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   217
            r1 <- Isabelle_Rendering.text_color(painter_snapshot, chunk_range, chunk_color)
43450
b6b09fc8d671 proper x1;
wenzelm
parents: 43448
diff changeset
   218
            r2 <- r1.try_restrict(chunk_range)
b6b09fc8d671 proper x1;
wenzelm
parents: 43448
diff changeset
   219
          } yield r2
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   220
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   221
        val padded_markup =
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   222
          if (markup.isEmpty)
46197
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   223
            Iterator(Text.Info(chunk_range, chunk_color))
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   224
          else
46197
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   225
            Iterator(
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   226
              Text.Info(Text.Range(chunk_range.start, markup.head.range.start), chunk_color)) ++
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   227
            markup.iterator ++
46197
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   228
            Iterator(Text.Info(Text.Range(markup.last.range.stop, chunk_range.stop), chunk_color))
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   229
43450
b6b09fc8d671 proper x1;
wenzelm
parents: 43448
diff changeset
   230
        var x1 = x + w
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   231
        gfx.setFont(chunk_font)
46197
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   232
        for (Text.Info(range, color) <- padded_markup if !range.is_singularity) {
44662
c8f1d943bfc5 more robust chunk painting wrt. hard tabs, when chunk.str == null;
wenzelm
parents: 44545
diff changeset
   233
          val str = chunk_str.substring(range.start - chunk_offset, range.stop - chunk_offset)
46197
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   234
          gfx.setColor(color)
43448
90aec5043461 more robust caret painting wrt. surrogate characters;
wenzelm
parents: 43435
diff changeset
   235
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   236
          range.try_restrict(caret_range) match {
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   237
            case Some(r) if !r.is_singularity =>
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   238
              val i = r.start - range.start
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   239
              val j = r.stop - range.start
44056
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   240
              val s1 = str.substring(0, i)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   241
              val s2 = str.substring(i, j)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   242
              val s3 = str.substring(j)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   243
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   244
              if (!s1.isEmpty) gfx.drawString(s1, x1, y)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   245
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   246
              val astr = new AttributedString(s2)
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   247
              astr.addAttribute(TextAttribute.FONT, chunk_font)
44056
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   248
              astr.addAttribute(TextAttribute.FOREGROUND, painter.getCaretColor)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   249
              astr.addAttribute(TextAttribute.SWAP_COLORS, TextAttribute.SWAP_COLORS_ON)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   250
              gfx.drawString(astr.getIterator, x1 + string_width(s1), y)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   251
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   252
              if (!s3.isEmpty)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   253
                gfx.drawString(s3, x1 + string_width(str.substring(0, j)), y)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   254
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   255
            case _ =>
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   256
              gfx.drawString(str, x1, y)
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   257
          }
44056
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   258
          x1 += string_width(str)
43382
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 = chunk.next.asInstanceOf[Chunk]
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   263
    }
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   264
    w
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   265
  }
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   266
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   267
  private val text_painter = new TextAreaExtension
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   268
  {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   269
    override def paintScreenLineRange(gfx: Graphics2D,
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   270
      first_line: Int, last_line: Int, physical_lines: Array[Int],
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   271
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   272
    {
43404
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
   273
      doc_view.robust_body(()) {
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   274
        val clip = gfx.getClip
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   275
        val x0 = text_area.getHorizontalOffset
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   276
        val fm = text_area.getPainter.getFontMetrics
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   277
        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
   278
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   279
        val infos = line_infos(physical_lines.iterator.filter(i => i != -1))
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   280
        for (i <- 0 until physical_lines.length) {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   281
          if (physical_lines(i) != -1) {
43392
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   282
            val x1 =
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   283
              infos.get(start(i)) match {
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   284
                case None => x0
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   285
                case Some(chunk) =>
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   286
                  gfx.clipRect(x0, y + line_height * i, Integer.MAX_VALUE, line_height)
43448
90aec5043461 more robust caret painting wrt. surrogate characters;
wenzelm
parents: 43435
diff changeset
   287
                  val w = paint_chunk_list(gfx, start(i), chunk, x0, y0).toInt
43392
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   288
                  x0 + w.toInt
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   289
              }
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   290
            gfx.clipRect(x1, 0, Integer.MAX_VALUE, Integer.MAX_VALUE)
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   291
            orig_text_painter.paintValidLine(gfx,
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   292
              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
   293
            gfx.setClip(clip)
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   294
          }
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   295
          y0 += line_height
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   296
        }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   297
      }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   298
    }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   299
  }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   300
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   301
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   302
  /* foreground */
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   303
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   304
  private val foreground_painter = new TextAreaExtension
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   305
  {
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   306
    override def paintScreenLineRange(gfx: Graphics2D,
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   307
      first_line: Int, last_line: Int, physical_lines: Array[Int],
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   308
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   309
    {
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   310
      doc_view.robust_body(()) {
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   311
        val snapshot = painter_snapshot
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   312
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   313
        for (i <- 0 until physical_lines.length) {
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   314
          if (physical_lines(i) != -1) {
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   315
            val line_range = doc_view.proper_line_range(start(i), end(i))
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   316
44545
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   317
            // foreground color
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   318
            for {
46178
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46166
diff changeset
   319
              Text.Info(range, color) <- Isabelle_Rendering.foreground(snapshot, line_range)
46204
df1369a42393 tuned signature;
wenzelm
parents: 46197
diff changeset
   320
              r <- gfx_range(range)
44545
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   321
            } {
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   322
              gfx.setColor(color)
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   323
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   324
            }
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   325
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   326
            // highlighted range -- potentially from other snapshot
46205
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   327
            for {
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   328
              info <- doc_view.highlight_range()
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   329
              Text.Info(range, color) <- info.try_restrict(line_range)
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   330
              r <- gfx_range(range)
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   331
            } {
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   332
              gfx.setColor(color)
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   333
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   334
            }
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   335
          }
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   336
        }
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   337
      }
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   338
    }
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   339
  }
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   340
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   341
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   342
  /* 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
   343
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   344
  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
   345
  {
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   346
    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
   347
      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
   348
    {
43404
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
   349
      doc_view.robust_body(()) {
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
   350
        if (before) gfx.clipRect(0, 0, 0, 0)
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
   351
        else gfx.setClip(painter_clip)
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
   352
      }
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   353
    }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   354
  }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   355
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   356
  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
   357
  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
   358
  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
   359
  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
   360
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   361
  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
   362
  {
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   363
    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
   364
      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
   365
    {
43404
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
   366
      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
   367
        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
   368
          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
   369
          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
   370
            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
   371
            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
   372
43398
c3e2a361b418 more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents: 43396
diff changeset
   373
            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
   374
            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
   375
            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
   376
            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
   377
          }
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   378
        }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   379
      }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   380
    }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   381
  }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   382
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   383
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   384
  /* activation */
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   385
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   386
  def activate()
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   387
  {
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   388
    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
   389
    painter.addExtension(TextAreaPainter.LOWEST_LAYER, set_state)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   390
    painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, background_painter)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   391
    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
   392
    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
   393
    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
   394
    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
   395
    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
   396
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 2, caret_painter)
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   397
    painter.addExtension(500, foreground_painter)
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   398
    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
   399
    painter.removeExtension(orig_text_painter)
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   400
  }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   401
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   402
  def deactivate()
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   403
  {
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   404
    val painter = text_area.getPainter
43396
548a68eafaea recovered orig_text_painter from f4141da52e92;
wenzelm
parents: 43393
diff changeset
   405
    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
   406
    painter.removeExtension(reset_state)
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   407
    painter.removeExtension(foreground_painter)
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   408
    painter.removeExtension(caret_painter)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   409
    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
   410
    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
   411
    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
   412
    painter.removeExtension(before_caret_painter1)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   413
    painter.removeExtension(text_painter)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   414
    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
   415
    painter.removeExtension(set_state)
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   416
  }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   417
}
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   418