src/Tools/jEdit/src/rich_text_area.scala
author wenzelm
Sat Mar 23 19:39:31 2013 +0100 (2013-03-23 ago)
changeset 51496 cb677987b7e3
parent 51492 eaa1c4cc1106
child 51574 2b58d7b139d6
permissions -rw-r--r--
retain original tooltip range, to avoid repeated window popup when the mouse is moved over the same content;
wenzelm@49411
     1
/*  Title:      Tools/jEdit/src/rich_text_area.scala
wenzelm@43369
     2
    Author:     Makarius
wenzelm@43369
     3
wenzelm@49411
     4
Enhanced version of jEdit text area, with rich text rendering,
wenzelm@49411
     5
tooltips, hyperlinks etc.
wenzelm@43369
     6
*/
wenzelm@43369
     7
wenzelm@43369
     8
package isabelle.jedit
wenzelm@43369
     9
wenzelm@43369
    10
wenzelm@43369
    11
import isabelle._
wenzelm@43369
    12
wenzelm@50657
    13
import java.awt.{Graphics2D, Shape, Color, Point, Toolkit}
wenzelm@49410
    14
import java.awt.event.{MouseMotionAdapter, MouseAdapter, MouseEvent,
wenzelm@49410
    15
  FocusAdapter, FocusEvent, WindowEvent, WindowAdapter}
wenzelm@43392
    16
import java.awt.font.TextAttribute
wenzelm@43392
    17
import java.text.AttributedString
wenzelm@43369
    18
import java.util.ArrayList
wenzelm@43369
    19
wenzelm@49410
    20
import org.gjt.sp.util.Log
wenzelm@49410
    21
import org.gjt.sp.jedit.{OperatingSystem, Debug, View}
wenzelm@43369
    22
import org.gjt.sp.jedit.syntax.{DisplayTokenHandler, Chunk}
wenzelm@49410
    23
import org.gjt.sp.jedit.textarea.{TextAreaExtension, TextAreaPainter, TextArea}
wenzelm@49410
    24
wenzelm@49410
    25
wenzelm@49492
    26
class Rich_Text_Area(
wenzelm@49492
    27
  view: View,
wenzelm@49492
    28
  text_area: TextArea,
wenzelm@50199
    29
  get_rendering: () => Rendering,
wenzelm@50915
    30
  close_action: () => Unit,
wenzelm@50306
    31
  caret_visible: Boolean,
wenzelm@49492
    32
  hovering: Boolean)
wenzelm@49410
    33
{
wenzelm@49410
    34
  private val buffer = text_area.getBuffer
wenzelm@43369
    35
wenzelm@43369
    36
wenzelm@49410
    37
  /* robust extension body */
wenzelm@49410
    38
wenzelm@49410
    39
  def robust_body[A](default: A)(body: => A): A =
wenzelm@49410
    40
  {
wenzelm@49410
    41
    try {
wenzelm@49410
    42
      Swing_Thread.require()
wenzelm@49410
    43
      if (buffer == text_area.getBuffer) body
wenzelm@49410
    44
      else {
wenzelm@49410
    45
        Log.log(Log.ERROR, this, ERROR("Implicit change of text area buffer"))
wenzelm@49410
    46
        default
wenzelm@49410
    47
      }
wenzelm@49410
    48
    }
wenzelm@50641
    49
    catch { case exn: Throwable => Log.log(Log.ERROR, this, exn); default }
wenzelm@49410
    50
  }
wenzelm@43376
    51
wenzelm@43392
    52
wenzelm@43392
    53
  /* original painters */
wenzelm@43392
    54
wenzelm@43392
    55
  private def pick_extension(name: String): TextAreaExtension =
wenzelm@43369
    56
  {
wenzelm@43369
    57
    text_area.getPainter.getExtensions.iterator.filter(x => x.getClass.getName == name).toList
wenzelm@43369
    58
    match {
wenzelm@43369
    59
      case List(x) => x
wenzelm@43369
    60
      case _ => error("Expected exactly one " + name)
wenzelm@43369
    61
    }
wenzelm@43369
    62
  }
wenzelm@43369
    63
wenzelm@43392
    64
  private val orig_text_painter =
wenzelm@43392
    65
    pick_extension("org.gjt.sp.jedit.textarea.TextAreaPainter$PaintText")
wenzelm@43392
    66
wenzelm@43392
    67
wenzelm@43393
    68
  /* common painter state */
wenzelm@43381
    69
wenzelm@50199
    70
  @volatile private var painter_rendering: Rendering = null
wenzelm@43393
    71
  @volatile private var painter_clip: Shape = null
wenzelm@43381
    72
wenzelm@43393
    73
  private val set_state = new TextAreaExtension
wenzelm@43381
    74
  {
wenzelm@43381
    75
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
    76
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
    77
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
    78
    {
wenzelm@49410
    79
      painter_rendering = get_rendering()
wenzelm@46220
    80
      painter_clip = gfx.getClip
wenzelm@43381
    81
    }
wenzelm@43381
    82
  }
wenzelm@43381
    83
wenzelm@43393
    84
  private val reset_state = new TextAreaExtension
wenzelm@43369
    85
  {
wenzelm@43381
    86
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
    87
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
    88
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
    89
    {
wenzelm@49356
    90
      painter_rendering = null
wenzelm@46220
    91
      painter_clip = null
wenzelm@43381
    92
    }
wenzelm@43381
    93
  }
wenzelm@43381
    94
wenzelm@50199
    95
  def robust_rendering(body: Rendering => Unit)
wenzelm@46220
    96
  {
wenzelm@49410
    97
    robust_body(()) { body(painter_rendering) }
wenzelm@49410
    98
  }
wenzelm@49410
    99
wenzelm@49410
   100
wenzelm@49410
   101
  /* active areas within the text */
wenzelm@49410
   102
wenzelm@50199
   103
  private class Active_Area[A](rendering: Rendering => Text.Range => Option[Text.Info[A]])
wenzelm@49410
   104
  {
wenzelm@49493
   105
    private var the_text_info: Option[(String, Text.Info[A])] = None
wenzelm@49410
   106
wenzelm@49493
   107
    def text_info: Option[(String, Text.Info[A])] = the_text_info
wenzelm@49493
   108
    def info: Option[Text.Info[A]] = the_text_info.map(_._2)
wenzelm@49410
   109
wenzelm@49410
   110
    def update(new_info: Option[Text.Info[A]])
wenzelm@49410
   111
    {
wenzelm@49493
   112
      val old_text_info = the_text_info
wenzelm@49493
   113
      val new_text_info =
wenzelm@49493
   114
        new_info.map(info => (text_area.getText(info.range.start, info.range.length), info))
wenzelm@49493
   115
wenzelm@49493
   116
      if (new_text_info != old_text_info) {
wenzelm@49493
   117
        for {
wenzelm@49493
   118
          r0 <- JEdit_Lib.visible_range(text_area)
wenzelm@49493
   119
          opt <- List(old_text_info, new_text_info)
wenzelm@49493
   120
          (_, Text.Info(r1, _)) <- opt
wenzelm@49493
   121
          r2 <- r1.try_restrict(r0)  // FIXME more precise?!
wenzelm@49493
   122
        } JEdit_Lib.invalidate_range(text_area, r2)
wenzelm@49493
   123
        the_text_info = new_text_info
wenzelm@49410
   124
      }
wenzelm@49410
   125
    }
wenzelm@49410
   126
wenzelm@50199
   127
    def update_rendering(r: Rendering, range: Text.Range)
wenzelm@49410
   128
    { update(rendering(r)(range)) }
wenzelm@49410
   129
wenzelm@49410
   130
    def reset { update(None) }
wenzelm@49410
   131
  }
wenzelm@49410
   132
wenzelm@49410
   133
  // owned by Swing thread
wenzelm@49410
   134
wenzelm@49410
   135
  private var control: Boolean = false
wenzelm@49410
   136
wenzelm@50199
   137
  private val highlight_area = new Active_Area[Color]((r: Rendering) => r.highlight _)
wenzelm@50199
   138
  private val hyperlink_area = new Active_Area[Hyperlink]((r: Rendering) => r.hyperlink _)
wenzelm@50450
   139
  private val active_area = new Active_Area[XML.Elem]((r: Rendering) => r.active _)
wenzelm@49492
   140
wenzelm@49492
   141
  private val active_areas =
wenzelm@50450
   142
    List((highlight_area, true), (hyperlink_area, true), (active_area, false))
wenzelm@50216
   143
  def active_reset(): Unit = active_areas.foreach(_._1.reset)
wenzelm@49410
   144
wenzelm@49410
   145
  private val focus_listener = new FocusAdapter {
wenzelm@49424
   146
    override def focusLost(e: FocusEvent) { robust_body(()) { active_reset() } }
wenzelm@49410
   147
  }
wenzelm@49410
   148
wenzelm@49410
   149
  private val window_listener = new WindowAdapter {
wenzelm@49424
   150
    override def windowIconified(e: WindowEvent) { robust_body(()) { active_reset() } }
wenzelm@49424
   151
    override def windowDeactivated(e: WindowEvent) { robust_body(()) { active_reset() } }
wenzelm@49410
   152
  }
wenzelm@49410
   153
wenzelm@49410
   154
  private val mouse_listener = new MouseAdapter {
wenzelm@49410
   155
    override def mouseClicked(e: MouseEvent) {
wenzelm@49424
   156
      robust_body(()) {
wenzelm@49424
   157
        hyperlink_area.info match {
wenzelm@49492
   158
          case Some(Text.Info(_, link)) => link.follow(view)
wenzelm@49492
   159
          case None =>
wenzelm@49492
   160
        }
wenzelm@50450
   161
        active_area.text_info match {
wenzelm@50915
   162
          case Some((text, Text.Info(_, markup))) =>
wenzelm@50915
   163
            Active.action(view, text, markup)
wenzelm@50915
   164
            close_action()
wenzelm@49424
   165
          case None =>
wenzelm@49424
   166
        }
wenzelm@49410
   167
      }
wenzelm@49410
   168
    }
wenzelm@49410
   169
  }
wenzelm@49410
   170
wenzelm@49410
   171
  private val mouse_motion_listener = new MouseMotionAdapter {
wenzelm@49410
   172
    override def mouseMoved(e: MouseEvent) {
wenzelm@49424
   173
      robust_body(()) {
wenzelm@50553
   174
        control = (e.getModifiers & Toolkit.getDefaultToolkit.getMenuShortcutKeyMask) != 0
wenzelm@49492
   175
wenzelm@49492
   176
        if ((control || hovering) && !buffer.isLoading) {
wenzelm@49424
   177
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@51441
   178
            JEdit_Lib.pixel_range(text_area, e.getX, e.getY) match {
wenzelm@50211
   179
              case None => active_reset()
wenzelm@49941
   180
              case Some(range) =>
wenzelm@49941
   181
                val rendering = get_rendering()
wenzelm@49941
   182
                for ((area, require_control) <- active_areas)
wenzelm@49941
   183
                {
wenzelm@50165
   184
                  if (control == require_control && !rendering.snapshot.is_outdated)
wenzelm@49941
   185
                    area.update_rendering(rendering, range)
wenzelm@49941
   186
                  else area.reset
wenzelm@49941
   187
                }
wenzelm@49492
   188
            }
wenzelm@49424
   189
          }
wenzelm@49410
   190
        }
wenzelm@49424
   191
        else active_reset()
wenzelm@51441
   192
wenzelm@51452
   193
        if (Pretty_Tooltip.is_active) {
wenzelm@51452
   194
          tooltip_event = Some(e)
wenzelm@51452
   195
          tooltip_delay.invoke()
wenzelm@51452
   196
        }
wenzelm@49410
   197
      }
wenzelm@49410
   198
    }
wenzelm@49410
   199
  }
wenzelm@49410
   200
wenzelm@49410
   201
wenzelm@49410
   202
  /* tooltips */
wenzelm@49410
   203
wenzelm@51441
   204
  private var tooltip_event: Option[MouseEvent] = None
wenzelm@51441
   205
wenzelm@51441
   206
  private val tooltip_delay =
wenzelm@51441
   207
    Swing_Thread.delay_last(PIDE.options.seconds("jedit_tooltip_delay")) {
wenzelm@51441
   208
      tooltip_event match {
wenzelm@51441
   209
        case Some(e) if e.getSource == text_area.getPainter =>
wenzelm@51441
   210
          val rendering = get_rendering()
wenzelm@51441
   211
          val snapshot = rendering.snapshot
wenzelm@51441
   212
          if (!snapshot.is_outdated) {
wenzelm@51441
   213
            val x = e.getX
wenzelm@51441
   214
            val y = e.getY
wenzelm@51441
   215
            JEdit_Lib.pixel_range(text_area, x, y) match {
wenzelm@51441
   216
              case None =>
wenzelm@51441
   217
              case Some(range) =>
wenzelm@51496
   218
                val result =
wenzelm@51441
   219
                  if (control) rendering.tooltip(range)
wenzelm@51441
   220
                  else rendering.tooltip_message(range)
wenzelm@51496
   221
                result match {
wenzelm@51496
   222
                  case None =>
wenzelm@51496
   223
                  case Some(tip) =>
wenzelm@51496
   224
                    val painter = text_area.getPainter
wenzelm@51496
   225
                    val y1 = y + painter.getFontMetrics.getHeight / 2
wenzelm@51496
   226
                    val results = rendering.command_results(range)
wenzelm@51496
   227
                    Pretty_Tooltip(view, painter, rendering, x, y1, results, tip.range, tip.info)
wenzelm@51441
   228
                }
wenzelm@51441
   229
            }
wenzelm@49730
   230
          }
wenzelm@51441
   231
          tooltip_event = None
wenzelm@51441
   232
        case _ =>
wenzelm@49410
   233
      }
wenzelm@49410
   234
    }
wenzelm@46220
   235
wenzelm@43381
   236
wenzelm@43381
   237
  /* text background */
wenzelm@43376
   238
wenzelm@43381
   239
  private val background_painter = new TextAreaExtension
wenzelm@43381
   240
  {
wenzelm@43381
   241
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
   242
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
   243
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
   244
    {
wenzelm@49356
   245
      robust_rendering { rendering =>
wenzelm@43381
   246
        val ascent = text_area.getPainter.getFontMetrics.getAscent
wenzelm@43376
   247
wenzelm@43381
   248
        for (i <- 0 until physical_lines.length) {
wenzelm@43381
   249
          if (physical_lines(i) != -1) {
wenzelm@49843
   250
            val line_range = Text.Range(start(i), end(i))
wenzelm@43376
   251
wenzelm@49473
   252
            // line background color
wenzelm@49473
   253
            for { (color, separator) <- rendering.line_background(line_range) }
wenzelm@49473
   254
            {
wenzelm@49473
   255
              gfx.setColor(color)
wenzelm@49475
   256
              val sep = if (separator) (2 min (line_height / 2)) else 0
wenzelm@49475
   257
              gfx.fillRect(0, y + i * line_height, text_area.getWidth, line_height - sep)
wenzelm@49473
   258
            }
wenzelm@49473
   259
wenzelm@46166
   260
            // background color (1)
wenzelm@43381
   261
            for {
wenzelm@49356
   262
              Text.Info(range, color) <- rendering.background1(line_range)
wenzelm@49409
   263
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@43381
   264
            } {
wenzelm@43381
   265
              gfx.setColor(color)
wenzelm@43381
   266
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@43381
   267
            }
wenzelm@43376
   268
wenzelm@50450
   269
            // active area -- potentially from other snapshot
wenzelm@49492
   270
            for {
wenzelm@50450
   271
              info <- active_area.info
wenzelm@49492
   272
              Text.Info(range, _) <- info.try_restrict(line_range)
wenzelm@49492
   273
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@49492
   274
            } {
wenzelm@50450
   275
              gfx.setColor(rendering.active_hover_color)
wenzelm@49492
   276
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@49492
   277
            }
wenzelm@49492
   278
wenzelm@46166
   279
            // background color (2)
wenzelm@43381
   280
            for {
wenzelm@49356
   281
              Text.Info(range, color) <- rendering.background2(line_range)
wenzelm@49409
   282
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@43381
   283
            } {
wenzelm@43381
   284
              gfx.setColor(color)
wenzelm@43381
   285
              gfx.fillRect(r.x + 2, y + i * line_height + 2, r.length - 4, line_height - 4)
wenzelm@43376
   286
            }
wenzelm@43381
   287
wenzelm@43381
   288
            // squiggly underline
wenzelm@43381
   289
            for {
wenzelm@49356
   290
              Text.Info(range, color) <- rendering.squiggly_underline(line_range)
wenzelm@49409
   291
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@43381
   292
            } {
wenzelm@43381
   293
              gfx.setColor(color)
wenzelm@43381
   294
              val x0 = (r.x / 2) * 2
wenzelm@43381
   295
              val y0 = r.y + ascent + 1
wenzelm@43381
   296
              for (x1 <- Range(x0, x0 + r.length, 2)) {
wenzelm@43381
   297
                val y1 = if (x1 % 4 < 2) y0 else y0 + 1
wenzelm@43381
   298
                gfx.drawLine(x1, y1, x1 + 1, y1)
wenzelm@43376
   299
              }
wenzelm@43376
   300
            }
wenzelm@43376
   301
          }
wenzelm@43376
   302
        }
wenzelm@43376
   303
      }
wenzelm@43381
   304
    }
wenzelm@43381
   305
  }
wenzelm@43381
   306
wenzelm@43381
   307
wenzelm@43381
   308
  /* text */
wenzelm@43369
   309
wenzelm@50199
   310
  private def paint_chunk_list(rendering: Rendering,
wenzelm@43505
   311
    gfx: Graphics2D, line_start: Text.Offset, head: Chunk, x: Float, y: Float): Float =
wenzelm@43382
   312
  {
wenzelm@43382
   313
    val clip_rect = gfx.getClipBounds
wenzelm@43392
   314
    val painter = text_area.getPainter
wenzelm@43392
   315
    val font_context = painter.getFontRenderContext
wenzelm@43382
   316
wenzelm@43382
   317
    var w = 0.0f
wenzelm@43382
   318
    var chunk = head
wenzelm@43382
   319
    while (chunk != null) {
wenzelm@43505
   320
      val chunk_offset = line_start + chunk.offset
wenzelm@43382
   321
      if (x + w + chunk.width > clip_rect.x &&
wenzelm@50306
   322
          x + w < clip_rect.x + clip_rect.width && chunk.length > 0)
wenzelm@43382
   323
      {
wenzelm@43505
   324
        val chunk_range = Text.Range(chunk_offset, chunk_offset + chunk.length)
wenzelm@44662
   325
        val chunk_str = if (chunk.str == null) " " * chunk.length else chunk.str
wenzelm@43382
   326
        val chunk_font = chunk.style.getFont
wenzelm@43382
   327
        val chunk_color = chunk.style.getForegroundColor
wenzelm@43382
   328
wenzelm@44056
   329
        def string_width(s: String): Float =
wenzelm@44056
   330
          if (s.isEmpty) 0.0f
wenzelm@44056
   331
          else chunk_font.getStringBounds(s, font_context).getWidth.toFloat
wenzelm@44056
   332
wenzelm@43448
   333
        val caret_range =
wenzelm@50306
   334
          if (caret_visible && text_area.isCaretVisible)
wenzelm@49407
   335
            JEdit_Lib.point_range(buffer, text_area.getCaretPosition)
wenzelm@43448
   336
          else Text.Range(-1)
wenzelm@43448
   337
wenzelm@43426
   338
        val markup =
wenzelm@43428
   339
          for {
wenzelm@49356
   340
            r1 <- rendering.text_color(chunk_range, chunk_color)
wenzelm@43450
   341
            r2 <- r1.try_restrict(chunk_range)
wenzelm@43450
   342
          } yield r2
wenzelm@43382
   343
wenzelm@43759
   344
        val padded_markup =
wenzelm@43759
   345
          if (markup.isEmpty)
wenzelm@46197
   346
            Iterator(Text.Info(chunk_range, chunk_color))
wenzelm@43759
   347
          else
wenzelm@46197
   348
            Iterator(
wenzelm@46197
   349
              Text.Info(Text.Range(chunk_range.start, markup.head.range.start), chunk_color)) ++
wenzelm@43759
   350
            markup.iterator ++
wenzelm@46197
   351
            Iterator(Text.Info(Text.Range(markup.last.range.stop, chunk_range.stop), chunk_color))
wenzelm@43759
   352
wenzelm@43450
   353
        var x1 = x + w
wenzelm@43382
   354
        gfx.setFont(chunk_font)
wenzelm@46197
   355
        for (Text.Info(range, color) <- padded_markup if !range.is_singularity) {
wenzelm@44662
   356
          val str = chunk_str.substring(range.start - chunk_offset, range.stop - chunk_offset)
wenzelm@46197
   357
          gfx.setColor(color)
wenzelm@43448
   358
wenzelm@43759
   359
          range.try_restrict(caret_range) match {
wenzelm@43759
   360
            case Some(r) if !r.is_singularity =>
wenzelm@43759
   361
              val i = r.start - range.start
wenzelm@43759
   362
              val j = r.stop - range.start
wenzelm@44056
   363
              val s1 = str.substring(0, i)
wenzelm@44056
   364
              val s2 = str.substring(i, j)
wenzelm@44056
   365
              val s3 = str.substring(j)
wenzelm@44056
   366
wenzelm@44056
   367
              if (!s1.isEmpty) gfx.drawString(s1, x1, y)
wenzelm@44056
   368
wenzelm@44056
   369
              val astr = new AttributedString(s2)
wenzelm@43759
   370
              astr.addAttribute(TextAttribute.FONT, chunk_font)
wenzelm@44056
   371
              astr.addAttribute(TextAttribute.FOREGROUND, painter.getCaretColor)
wenzelm@44056
   372
              astr.addAttribute(TextAttribute.SWAP_COLORS, TextAttribute.SWAP_COLORS_ON)
wenzelm@44056
   373
              gfx.drawString(astr.getIterator, x1 + string_width(s1), y)
wenzelm@44056
   374
wenzelm@44056
   375
              if (!s3.isEmpty)
wenzelm@44056
   376
                gfx.drawString(s3, x1 + string_width(str.substring(0, j)), y)
wenzelm@44056
   377
wenzelm@43759
   378
            case _ =>
wenzelm@43759
   379
              gfx.drawString(str, x1, y)
wenzelm@43382
   380
          }
wenzelm@44056
   381
          x1 += string_width(str)
wenzelm@43382
   382
        }
wenzelm@43382
   383
      }
wenzelm@43382
   384
      w += chunk.width
wenzelm@43382
   385
      chunk = chunk.next.asInstanceOf[Chunk]
wenzelm@43382
   386
    }
wenzelm@43382
   387
    w
wenzelm@43382
   388
  }
wenzelm@43382
   389
wenzelm@43381
   390
  private val text_painter = new TextAreaExtension
wenzelm@43381
   391
  {
wenzelm@43381
   392
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
   393
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43382
   394
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
   395
    {
wenzelm@49356
   396
      robust_rendering { rendering =>
wenzelm@43381
   397
        val clip = gfx.getClip
wenzelm@43381
   398
        val x0 = text_area.getHorizontalOffset
wenzelm@43382
   399
        val fm = text_area.getPainter.getFontMetrics
wenzelm@43382
   400
        var y0 = y + fm.getHeight - (fm.getLeading + 1) - fm.getDescent
wenzelm@43372
   401
wenzelm@43381
   402
        for (i <- 0 until physical_lines.length) {
wenzelm@49097
   403
          val line = physical_lines(i)
wenzelm@49097
   404
          if (line != -1) {
wenzelm@49097
   405
            val screen_line = first_line + i
wenzelm@49097
   406
            val chunks = text_area.getChunksOfScreenLine(screen_line)
wenzelm@49097
   407
            if (chunks != null) {
wenzelm@49410
   408
              val line_start = buffer.getLineStartOffset(line)
wenzelm@49097
   409
              gfx.clipRect(x0, y + line_height * i, Integer.MAX_VALUE, line_height)
wenzelm@49356
   410
              val w = paint_chunk_list(rendering, gfx, line_start, chunks, x0, y0).toInt
wenzelm@49097
   411
              gfx.clipRect(x0 + w.toInt, 0, Integer.MAX_VALUE, Integer.MAX_VALUE)
wenzelm@49097
   412
              orig_text_painter.paintValidLine(gfx,
wenzelm@49097
   413
                screen_line, line, start(i), end(i), y + line_height * i)
wenzelm@49097
   414
              gfx.setClip(clip)
wenzelm@49097
   415
            }
wenzelm@43369
   416
          }
wenzelm@43381
   417
          y0 += line_height
wenzelm@43369
   418
        }
wenzelm@43369
   419
      }
wenzelm@43369
   420
    }
wenzelm@43369
   421
  }
wenzelm@43369
   422
wenzelm@43369
   423
wenzelm@43435
   424
  /* foreground */
wenzelm@43435
   425
wenzelm@43435
   426
  private val foreground_painter = new TextAreaExtension
wenzelm@43435
   427
  {
wenzelm@43435
   428
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43435
   429
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43435
   430
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43435
   431
    {
wenzelm@49356
   432
      robust_rendering { rendering =>
wenzelm@43435
   433
        for (i <- 0 until physical_lines.length) {
wenzelm@43435
   434
          if (physical_lines(i) != -1) {
wenzelm@49843
   435
            val line_range = Text.Range(start(i), end(i))
wenzelm@43435
   436
wenzelm@44545
   437
            // foreground color
wenzelm@44545
   438
            for {
wenzelm@49356
   439
              Text.Info(range, color) <- rendering.foreground(line_range)
wenzelm@49409
   440
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@44545
   441
            } {
wenzelm@44545
   442
              gfx.setColor(color)
wenzelm@44545
   443
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@44545
   444
            }
wenzelm@44545
   445
wenzelm@49357
   446
            // highlight range -- potentially from other snapshot
wenzelm@46205
   447
            for {
wenzelm@49410
   448
              info <- highlight_area.info
wenzelm@46205
   449
              Text.Info(range, color) <- info.try_restrict(line_range)
wenzelm@49409
   450
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@46205
   451
            } {
wenzelm@46205
   452
              gfx.setColor(color)
wenzelm@46205
   453
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@43435
   454
            }
wenzelm@48921
   455
wenzelm@48921
   456
            // hyperlink range -- potentially from other snapshot
wenzelm@48921
   457
            for {
wenzelm@49410
   458
              info <- hyperlink_area.info
wenzelm@48921
   459
              Text.Info(range, _) <- info.try_restrict(line_range)
wenzelm@49409
   460
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@48921
   461
            } {
wenzelm@49356
   462
              gfx.setColor(rendering.hyperlink_color)
wenzelm@48921
   463
              gfx.drawRect(r.x, y + i * line_height, r.length - 1, line_height - 1)
wenzelm@48921
   464
            }
wenzelm@43435
   465
          }
wenzelm@43435
   466
        }
wenzelm@43435
   467
      }
wenzelm@43435
   468
    }
wenzelm@43435
   469
  }
wenzelm@43435
   470
wenzelm@43435
   471
wenzelm@43393
   472
  /* caret -- outside of text range */
wenzelm@43393
   473
wenzelm@43393
   474
  private class Caret_Painter(before: Boolean) extends TextAreaExtension
wenzelm@43393
   475
  {
wenzelm@43393
   476
    override def paintValidLine(gfx: Graphics2D,
wenzelm@43393
   477
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
wenzelm@43393
   478
    {
wenzelm@49356
   479
      robust_rendering { _ =>
wenzelm@43404
   480
        if (before) gfx.clipRect(0, 0, 0, 0)
wenzelm@43404
   481
        else gfx.setClip(painter_clip)
wenzelm@43404
   482
      }
wenzelm@43393
   483
    }
wenzelm@43393
   484
  }
wenzelm@43393
   485
wenzelm@43393
   486
  private val before_caret_painter1 = new Caret_Painter(true)
wenzelm@43393
   487
  private val after_caret_painter1 = new Caret_Painter(false)
wenzelm@43393
   488
  private val before_caret_painter2 = new Caret_Painter(true)
wenzelm@43393
   489
  private val after_caret_painter2 = new Caret_Painter(false)
wenzelm@43393
   490
wenzelm@43393
   491
  private val caret_painter = new TextAreaExtension
wenzelm@43393
   492
  {
wenzelm@43393
   493
    override def paintValidLine(gfx: Graphics2D,
wenzelm@43393
   494
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
wenzelm@43393
   495
    {
wenzelm@49356
   496
      robust_rendering { _ =>
wenzelm@50306
   497
        if (caret_visible && text_area.isCaretVisible) {
wenzelm@43398
   498
          val caret = text_area.getCaretPosition
wenzelm@43398
   499
          if (start <= caret && caret == end - 1) {
wenzelm@43398
   500
            val painter = text_area.getPainter
wenzelm@43398
   501
            val fm = painter.getFontMetrics
wenzelm@51492
   502
            val metric = JEdit_Lib.pretty_metric(painter)
wenzelm@43393
   503
wenzelm@43398
   504
            val offset = caret - text_area.getLineStartOffset(physical_line)
wenzelm@43398
   505
            val x = text_area.offsetToXY(physical_line, offset).x
wenzelm@43398
   506
            gfx.setColor(painter.getCaretColor)
wenzelm@51492
   507
            gfx.drawRect(x, y, (metric.unit * metric.average).round.toInt - 1, fm.getHeight - 1)
wenzelm@43398
   508
          }
wenzelm@43393
   509
        }
wenzelm@43393
   510
      }
wenzelm@43393
   511
    }
wenzelm@43393
   512
  }
wenzelm@43393
   513
wenzelm@43393
   514
wenzelm@43369
   515
  /* activation */
wenzelm@43369
   516
wenzelm@43369
   517
  def activate()
wenzelm@43369
   518
  {
wenzelm@43369
   519
    val painter = text_area.getPainter
wenzelm@43393
   520
    painter.addExtension(TextAreaPainter.LOWEST_LAYER, set_state)
wenzelm@43381
   521
    painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, background_painter)
wenzelm@43381
   522
    painter.addExtension(TextAreaPainter.TEXT_LAYER, text_painter)
wenzelm@43393
   523
    painter.addExtension(TextAreaPainter.CARET_LAYER - 1, before_caret_painter1)
wenzelm@43393
   524
    painter.addExtension(TextAreaPainter.CARET_LAYER + 1, after_caret_painter1)
wenzelm@43393
   525
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER - 1, before_caret_painter2)
wenzelm@43393
   526
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 1, after_caret_painter2)
wenzelm@43393
   527
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 2, caret_painter)
wenzelm@43435
   528
    painter.addExtension(500, foreground_painter)
wenzelm@43393
   529
    painter.addExtension(TextAreaPainter.HIGHEST_LAYER, reset_state)
wenzelm@43369
   530
    painter.removeExtension(orig_text_painter)
wenzelm@49410
   531
    painter.addMouseListener(mouse_listener)
wenzelm@49410
   532
    painter.addMouseMotionListener(mouse_motion_listener)
wenzelm@49410
   533
    text_area.addFocusListener(focus_listener)
wenzelm@49410
   534
    view.addWindowListener(window_listener)
wenzelm@43369
   535
  }
wenzelm@43369
   536
wenzelm@43369
   537
  def deactivate()
wenzelm@43369
   538
  {
wenzelm@43369
   539
    val painter = text_area.getPainter
wenzelm@49410
   540
    view.removeWindowListener(window_listener)
wenzelm@49410
   541
    text_area.removeFocusListener(focus_listener)
wenzelm@49410
   542
    painter.removeMouseMotionListener(mouse_motion_listener)
wenzelm@49410
   543
    painter.removeMouseListener(mouse_listener)
wenzelm@43396
   544
    painter.addExtension(TextAreaPainter.TEXT_LAYER, orig_text_painter)
wenzelm@43393
   545
    painter.removeExtension(reset_state)
wenzelm@43435
   546
    painter.removeExtension(foreground_painter)
wenzelm@43393
   547
    painter.removeExtension(caret_painter)
wenzelm@43393
   548
    painter.removeExtension(after_caret_painter2)
wenzelm@43393
   549
    painter.removeExtension(before_caret_painter2)
wenzelm@43393
   550
    painter.removeExtension(after_caret_painter1)
wenzelm@43393
   551
    painter.removeExtension(before_caret_painter1)
wenzelm@43381
   552
    painter.removeExtension(text_painter)
wenzelm@43381
   553
    painter.removeExtension(background_painter)
wenzelm@43393
   554
    painter.removeExtension(set_state)
wenzelm@43369
   555
  }
wenzelm@43369
   556
}
wenzelm@43369
   557