src/Tools/jEdit/src/rich_text_area.scala
author wenzelm
Wed Jul 23 11:19:24 2014 +0200 (2014-07-23)
changeset 57612 990ffb84489b
parent 56884 7de69b35bdaf
child 57857 4d86378e635f
permissions -rw-r--r--
clarified module name: facilitate alternative GUI frameworks;
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@56322
    13
import java.awt.{Graphics2D, Shape, Color, Point, Toolkit, Cursor, MouseInfo}
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@56322
    17
import javax.swing.SwingUtilities
wenzelm@43392
    18
import java.text.AttributedString
wenzelm@43369
    19
import java.util.ArrayList
wenzelm@43369
    20
wenzelm@56883
    21
import scala.util.matching.Regex
wenzelm@56883
    22
wenzelm@49410
    23
import org.gjt.sp.util.Log
wenzelm@49410
    24
import org.gjt.sp.jedit.{OperatingSystem, Debug, View}
wenzelm@43369
    25
import org.gjt.sp.jedit.syntax.{DisplayTokenHandler, Chunk}
wenzelm@49410
    26
import org.gjt.sp.jedit.textarea.{TextAreaExtension, TextAreaPainter, TextArea}
wenzelm@49410
    27
wenzelm@49410
    28
wenzelm@49492
    29
class Rich_Text_Area(
wenzelm@49492
    30
  view: View,
wenzelm@49492
    31
  text_area: TextArea,
wenzelm@50199
    32
  get_rendering: () => Rendering,
wenzelm@50915
    33
  close_action: () => Unit,
wenzelm@56883
    34
  get_search_pattern: () => Option[Regex],
wenzelm@50306
    35
  caret_visible: Boolean,
wenzelm@53179
    36
  enable_hovering: Boolean)
wenzelm@49410
    37
{
wenzelm@49410
    38
  private val buffer = text_area.getBuffer
wenzelm@43369
    39
wenzelm@43369
    40
wenzelm@49410
    41
  /* robust extension body */
wenzelm@49410
    42
wenzelm@49410
    43
  def robust_body[A](default: A)(body: => A): A =
wenzelm@49410
    44
  {
wenzelm@49410
    45
    try {
wenzelm@57612
    46
      GUI_Thread.require {}
wenzelm@49410
    47
      if (buffer == text_area.getBuffer) body
wenzelm@49410
    48
      else {
wenzelm@49410
    49
        Log.log(Log.ERROR, this, ERROR("Implicit change of text area buffer"))
wenzelm@49410
    50
        default
wenzelm@49410
    51
      }
wenzelm@49410
    52
    }
wenzelm@50641
    53
    catch { case exn: Throwable => Log.log(Log.ERROR, this, exn); default }
wenzelm@49410
    54
  }
wenzelm@43376
    55
wenzelm@43392
    56
wenzelm@43392
    57
  /* original painters */
wenzelm@43392
    58
wenzelm@43392
    59
  private def pick_extension(name: String): TextAreaExtension =
wenzelm@43369
    60
  {
wenzelm@43369
    61
    text_area.getPainter.getExtensions.iterator.filter(x => x.getClass.getName == name).toList
wenzelm@43369
    62
    match {
wenzelm@43369
    63
      case List(x) => x
wenzelm@43369
    64
      case _ => error("Expected exactly one " + name)
wenzelm@43369
    65
    }
wenzelm@43369
    66
  }
wenzelm@43369
    67
wenzelm@43392
    68
  private val orig_text_painter =
wenzelm@43392
    69
    pick_extension("org.gjt.sp.jedit.textarea.TextAreaPainter$PaintText")
wenzelm@43392
    70
wenzelm@43392
    71
wenzelm@43393
    72
  /* common painter state */
wenzelm@43381
    73
wenzelm@50199
    74
  @volatile private var painter_rendering: Rendering = null
wenzelm@43393
    75
  @volatile private var painter_clip: Shape = null
wenzelm@43381
    76
wenzelm@43393
    77
  private val set_state = new TextAreaExtension
wenzelm@43381
    78
  {
wenzelm@43381
    79
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
    80
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
    81
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
    82
    {
wenzelm@49410
    83
      painter_rendering = get_rendering()
wenzelm@46220
    84
      painter_clip = gfx.getClip
wenzelm@43381
    85
    }
wenzelm@43381
    86
  }
wenzelm@43381
    87
wenzelm@43393
    88
  private val reset_state = new TextAreaExtension
wenzelm@43369
    89
  {
wenzelm@43381
    90
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
    91
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
    92
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
    93
    {
wenzelm@49356
    94
      painter_rendering = null
wenzelm@46220
    95
      painter_clip = null
wenzelm@43381
    96
    }
wenzelm@43381
    97
  }
wenzelm@43381
    98
wenzelm@50199
    99
  def robust_rendering(body: Rendering => Unit)
wenzelm@46220
   100
  {
wenzelm@49410
   101
    robust_body(()) { body(painter_rendering) }
wenzelm@49410
   102
  }
wenzelm@49410
   103
wenzelm@49410
   104
wenzelm@49410
   105
  /* active areas within the text */
wenzelm@49410
   106
wenzelm@56317
   107
  private class Active_Area[A](
wenzelm@56317
   108
    rendering: Rendering => Text.Range => Option[Text.Info[A]],
wenzelm@56317
   109
    cursor: Option[Int])
wenzelm@49410
   110
  {
wenzelm@49493
   111
    private var the_text_info: Option[(String, Text.Info[A])] = None
wenzelm@49410
   112
wenzelm@55688
   113
    def is_active: Boolean = the_text_info.isDefined
wenzelm@49493
   114
    def text_info: Option[(String, Text.Info[A])] = the_text_info
wenzelm@49493
   115
    def info: Option[Text.Info[A]] = the_text_info.map(_._2)
wenzelm@49410
   116
wenzelm@49410
   117
    def update(new_info: Option[Text.Info[A]])
wenzelm@49410
   118
    {
wenzelm@49493
   119
      val old_text_info = the_text_info
wenzelm@49493
   120
      val new_text_info =
wenzelm@49493
   121
        new_info.map(info => (text_area.getText(info.range.start, info.range.length), info))
wenzelm@49493
   122
wenzelm@49493
   123
      if (new_text_info != old_text_info) {
wenzelm@56317
   124
        if (cursor.isDefined) {
wenzelm@56317
   125
          if (new_text_info.isDefined)
wenzelm@56317
   126
            text_area.getPainter.setCursor(Cursor.getPredefinedCursor(cursor.get))
wenzelm@56317
   127
          else
wenzelm@56406
   128
            text_area.getPainter.resetCursor()
wenzelm@56317
   129
        }
wenzelm@49493
   130
        for {
wenzelm@49493
   131
          r0 <- JEdit_Lib.visible_range(text_area)
wenzelm@49493
   132
          opt <- List(old_text_info, new_text_info)
wenzelm@49493
   133
          (_, Text.Info(r1, _)) <- opt
wenzelm@49493
   134
          r2 <- r1.try_restrict(r0)  // FIXME more precise?!
wenzelm@49493
   135
        } JEdit_Lib.invalidate_range(text_area, r2)
wenzelm@49493
   136
        the_text_info = new_text_info
wenzelm@49410
   137
      }
wenzelm@49410
   138
    }
wenzelm@49410
   139
wenzelm@50199
   140
    def update_rendering(r: Rendering, range: Text.Range)
wenzelm@49410
   141
    { update(rendering(r)(range)) }
wenzelm@49410
   142
wenzelm@49410
   143
    def reset { update(None) }
wenzelm@49410
   144
  }
wenzelm@49410
   145
wenzelm@57612
   146
  // owned by GUI thread
wenzelm@49410
   147
wenzelm@56317
   148
  private val highlight_area =
wenzelm@56317
   149
    new Active_Area[Color]((r: Rendering) => r.highlight _, None)
wenzelm@52980
   150
  private val hyperlink_area =
wenzelm@56317
   151
    new Active_Area[PIDE.editor.Hyperlink](
wenzelm@56317
   152
      (r: Rendering) => r.hyperlink _, Some(Cursor.HAND_CURSOR))
wenzelm@56317
   153
  private val active_area =
wenzelm@56317
   154
    new Active_Area[XML.Elem]((r: Rendering) => r.active _, Some(Cursor.DEFAULT_CURSOR))
wenzelm@49492
   155
wenzelm@49492
   156
  private val active_areas =
wenzelm@50450
   157
    List((highlight_area, true), (hyperlink_area, true), (active_area, false))
wenzelm@50216
   158
  def active_reset(): Unit = active_areas.foreach(_._1.reset)
wenzelm@49410
   159
wenzelm@49410
   160
  private val focus_listener = new FocusAdapter {
wenzelm@49424
   161
    override def focusLost(e: FocusEvent) { robust_body(()) { active_reset() } }
wenzelm@49410
   162
  }
wenzelm@49410
   163
wenzelm@49410
   164
  private val window_listener = new WindowAdapter {
wenzelm@49424
   165
    override def windowIconified(e: WindowEvent) { robust_body(()) { active_reset() } }
wenzelm@49424
   166
    override def windowDeactivated(e: WindowEvent) { robust_body(()) { active_reset() } }
wenzelm@49410
   167
  }
wenzelm@49410
   168
wenzelm@49410
   169
  private val mouse_listener = new MouseAdapter {
wenzelm@49410
   170
    override def mouseClicked(e: MouseEvent) {
wenzelm@49424
   171
      robust_body(()) {
wenzelm@49424
   172
        hyperlink_area.info match {
wenzelm@56433
   173
          case Some(Text.Info(range, link)) =>
wenzelm@56494
   174
            if (!link.external) {
wenzelm@56494
   175
              try { text_area.moveCaretPosition(range.start) }
wenzelm@56494
   176
              catch {
wenzelm@56494
   177
                case _: ArrayIndexOutOfBoundsException =>
wenzelm@56494
   178
                case _: IllegalArgumentException =>
wenzelm@56494
   179
              }
wenzelm@56494
   180
              text_area.requestFocus
wenzelm@56433
   181
            }
wenzelm@52482
   182
            link.follow(view)
wenzelm@49492
   183
          case None =>
wenzelm@49492
   184
        }
wenzelm@50450
   185
        active_area.text_info match {
wenzelm@50915
   186
          case Some((text, Text.Info(_, markup))) =>
wenzelm@50915
   187
            Active.action(view, text, markup)
wenzelm@50915
   188
            close_action()
wenzelm@49424
   189
          case None =>
wenzelm@49424
   190
        }
wenzelm@49410
   191
      }
wenzelm@49410
   192
    }
wenzelm@49410
   193
  }
wenzelm@49410
   194
wenzelm@56322
   195
  private def mouse_inside_painter(): Boolean =
wenzelm@56322
   196
    MouseInfo.getPointerInfo match {
wenzelm@56322
   197
      case null => false
wenzelm@56322
   198
      case info =>
wenzelm@56322
   199
        val point = info.getLocation
wenzelm@56322
   200
        val painter = text_area.getPainter
wenzelm@56322
   201
        SwingUtilities.convertPointFromScreen(point, painter)
wenzelm@56322
   202
        painter.contains(point)
wenzelm@56322
   203
    }
wenzelm@56322
   204
wenzelm@49410
   205
  private val mouse_motion_listener = new MouseMotionAdapter {
wenzelm@56321
   206
    override def mouseDragged(evt: MouseEvent) {
wenzelm@56321
   207
      robust_body(()) {
wenzelm@56497
   208
        Completion_Popup.Text_Area.dismissed(text_area)
wenzelm@56497
   209
        Pretty_Tooltip.dismiss_descendant(text_area.getPainter)
wenzelm@56321
   210
      }
wenzelm@56321
   211
    }
wenzelm@56321
   212
wenzelm@53182
   213
    override def mouseMoved(evt: MouseEvent) {
wenzelm@49424
   214
      robust_body(()) {
wenzelm@53182
   215
        val x = evt.getX
wenzelm@53182
   216
        val y = evt.getY
wenzelm@53182
   217
        val control = (evt.getModifiers & Toolkit.getDefaultToolkit.getMenuShortcutKeyMask) != 0
wenzelm@49492
   218
wenzelm@53179
   219
        if ((control || enable_hovering) && !buffer.isLoading) {
wenzelm@49424
   220
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@53182
   221
            JEdit_Lib.pixel_range(text_area, x, y) match {
wenzelm@50211
   222
              case None => active_reset()
wenzelm@49941
   223
              case Some(range) =>
wenzelm@49941
   224
                val rendering = get_rendering()
wenzelm@49941
   225
                for ((area, require_control) <- active_areas)
wenzelm@49941
   226
                {
wenzelm@50165
   227
                  if (control == require_control && !rendering.snapshot.is_outdated)
wenzelm@49941
   228
                    area.update_rendering(rendering, range)
wenzelm@49941
   229
                  else area.reset
wenzelm@49941
   230
                }
wenzelm@49492
   231
            }
wenzelm@49424
   232
          }
wenzelm@49410
   233
        }
wenzelm@49424
   234
        else active_reset()
wenzelm@51441
   235
wenzelm@53182
   236
        if (evt.getSource == text_area.getPainter) {
wenzelm@52494
   237
          Pretty_Tooltip.invoke(() =>
wenzelm@52495
   238
            robust_body(()) {
wenzelm@56322
   239
              if (mouse_inside_painter()) {
wenzelm@56322
   240
                val rendering = get_rendering()
wenzelm@56322
   241
                val snapshot = rendering.snapshot
wenzelm@56322
   242
                if (!snapshot.is_outdated) {
wenzelm@56322
   243
                  JEdit_Lib.pixel_range(text_area, x, y) match {
wenzelm@56322
   244
                    case None =>
wenzelm@56322
   245
                    case Some(range) =>
wenzelm@56322
   246
                      val result =
wenzelm@56322
   247
                        if (control) rendering.tooltip(range)
wenzelm@56322
   248
                        else rendering.tooltip_message(range)
wenzelm@56322
   249
                      result match {
wenzelm@56322
   250
                        case None =>
wenzelm@56322
   251
                        case Some(tip) =>
wenzelm@56322
   252
                          val painter = text_area.getPainter
wenzelm@56322
   253
                          val loc = new Point(x, y + painter.getFontMetrics.getHeight / 2)
wenzelm@56496
   254
                          val results = rendering.command_results(tip.range)
wenzelm@56322
   255
                          Pretty_Tooltip(view, painter, loc, rendering, results, tip)
wenzelm@56322
   256
                      }
wenzelm@56322
   257
                  }
wenzelm@52494
   258
                }
wenzelm@52494
   259
              }
wenzelm@52495
   260
          })
wenzelm@51452
   261
        }
wenzelm@49410
   262
      }
wenzelm@49410
   263
    }
wenzelm@49410
   264
  }
wenzelm@49410
   265
wenzelm@49410
   266
wenzelm@55713
   267
  /* text background */
wenzelm@55713
   268
wenzelm@43381
   269
  private val background_painter = new TextAreaExtension
wenzelm@43381
   270
  {
wenzelm@43381
   271
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
   272
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43381
   273
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
   274
    {
wenzelm@49356
   275
      robust_rendering { rendering =>
wenzelm@51581
   276
        val fm = text_area.getPainter.getFontMetrics
wenzelm@43376
   277
wenzelm@43381
   278
        for (i <- 0 until physical_lines.length) {
wenzelm@43381
   279
          if (physical_lines(i) != -1) {
wenzelm@56884
   280
            val line_range = Text.Range(start(i), end(i) min buffer.getLength)
wenzelm@43376
   281
wenzelm@49473
   282
            // line background color
wenzelm@49473
   283
            for { (color, separator) <- rendering.line_background(line_range) }
wenzelm@49473
   284
            {
wenzelm@49473
   285
              gfx.setColor(color)
wenzelm@49475
   286
              val sep = if (separator) (2 min (line_height / 2)) else 0
wenzelm@49475
   287
              gfx.fillRect(0, y + i * line_height, text_area.getWidth, line_height - sep)
wenzelm@49473
   288
            }
wenzelm@49473
   289
wenzelm@55790
   290
            // background color
wenzelm@43381
   291
            for {
wenzelm@55790
   292
              Text.Info(range, color) <- rendering.background(line_range)
wenzelm@49409
   293
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@43381
   294
            } {
wenzelm@43381
   295
              gfx.setColor(color)
wenzelm@43381
   296
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@43381
   297
            }
wenzelm@43376
   298
wenzelm@50450
   299
            // active area -- potentially from other snapshot
wenzelm@49492
   300
            for {
wenzelm@50450
   301
              info <- active_area.info
wenzelm@49492
   302
              Text.Info(range, _) <- info.try_restrict(line_range)
wenzelm@49492
   303
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@49492
   304
            } {
wenzelm@50450
   305
              gfx.setColor(rendering.active_hover_color)
wenzelm@49492
   306
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@49492
   307
            }
wenzelm@49492
   308
wenzelm@43381
   309
            // squiggly underline
wenzelm@43381
   310
            for {
wenzelm@49356
   311
              Text.Info(range, color) <- rendering.squiggly_underline(line_range)
wenzelm@49409
   312
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@43381
   313
            } {
wenzelm@43381
   314
              gfx.setColor(color)
wenzelm@43381
   315
              val x0 = (r.x / 2) * 2
wenzelm@51574
   316
              val y0 = r.y + fm.getAscent + 1
wenzelm@43381
   317
              for (x1 <- Range(x0, x0 + r.length, 2)) {
wenzelm@43381
   318
                val y1 = if (x1 % 4 < 2) y0 else y0 + 1
wenzelm@43381
   319
                gfx.drawLine(x1, y1, x1 + 1, y1)
wenzelm@43376
   320
              }
wenzelm@43376
   321
            }
wenzelm@56550
   322
wenzelm@56560
   323
            // spell checker
wenzelm@56550
   324
            for {
wenzelm@56558
   325
              spell_checker <- PIDE.spell_checker.get
wenzelm@56565
   326
              spell_range <- rendering.spell_checker_ranges(line_range)
wenzelm@56565
   327
              text <- JEdit_Lib.try_get_text(buffer, spell_range)
wenzelm@56565
   328
              info <- spell_checker.marked_words(spell_range.start, text)
wenzelm@56565
   329
              r <- JEdit_Lib.gfx_range(text_area, info.range)
wenzelm@56550
   330
            } {
wenzelm@56550
   331
              gfx.setColor(rendering.spell_checker_color)
wenzelm@56560
   332
              val y0 = r.y + ((fm.getAscent + 4) min (line_height - 2))
wenzelm@56550
   333
              gfx.drawLine(r.x, y0, r.x + r.length, y0)
wenzelm@56550
   334
            }
wenzelm@43376
   335
          }
wenzelm@43376
   336
        }
wenzelm@43376
   337
      }
wenzelm@43381
   338
    }
wenzelm@43381
   339
  }
wenzelm@43381
   340
wenzelm@43381
   341
wenzelm@43381
   342
  /* text */
wenzelm@43369
   343
wenzelm@55766
   344
  private def caret_enabled: Boolean =
wenzelm@55766
   345
    caret_visible && (!text_area.hasFocus || text_area.isCaretVisible)
wenzelm@55766
   346
wenzelm@55747
   347
  private def caret_color(rendering: Rendering): Color =
wenzelm@55747
   348
  {
wenzelm@55747
   349
    if (text_area.isCaretVisible)
wenzelm@55747
   350
      text_area.getPainter.getCaretColor
wenzelm@55747
   351
    else rendering.caret_invisible_color
wenzelm@55747
   352
  }
wenzelm@55747
   353
wenzelm@50199
   354
  private def paint_chunk_list(rendering: Rendering,
wenzelm@43505
   355
    gfx: Graphics2D, line_start: Text.Offset, head: Chunk, x: Float, y: Float): Float =
wenzelm@43382
   356
  {
wenzelm@43382
   357
    val clip_rect = gfx.getClipBounds
wenzelm@43392
   358
    val painter = text_area.getPainter
wenzelm@43392
   359
    val font_context = painter.getFontRenderContext
wenzelm@55747
   360
wenzelm@55747
   361
    val caret_range =
wenzelm@56592
   362
      if (caret_enabled) JEdit_Lib.caret_range(text_area)
wenzelm@56172
   363
      else Text.Range.offside
wenzelm@43382
   364
wenzelm@43382
   365
    var w = 0.0f
wenzelm@43382
   366
    var chunk = head
wenzelm@43382
   367
    while (chunk != null) {
wenzelm@43505
   368
      val chunk_offset = line_start + chunk.offset
wenzelm@43382
   369
      if (x + w + chunk.width > clip_rect.x &&
wenzelm@50306
   370
          x + w < clip_rect.x + clip_rect.width && chunk.length > 0)
wenzelm@43382
   371
      {
wenzelm@43505
   372
        val chunk_range = Text.Range(chunk_offset, chunk_offset + chunk.length)
wenzelm@44662
   373
        val chunk_str = if (chunk.str == null) " " * chunk.length else chunk.str
wenzelm@43382
   374
        val chunk_font = chunk.style.getFont
wenzelm@43382
   375
        val chunk_color = chunk.style.getForegroundColor
wenzelm@43382
   376
wenzelm@44056
   377
        def string_width(s: String): Float =
wenzelm@44056
   378
          if (s.isEmpty) 0.0f
wenzelm@44056
   379
          else chunk_font.getStringBounds(s, font_context).getWidth.toFloat
wenzelm@44056
   380
wenzelm@43426
   381
        val markup =
wenzelm@43428
   382
          for {
wenzelm@49356
   383
            r1 <- rendering.text_color(chunk_range, chunk_color)
wenzelm@43450
   384
            r2 <- r1.try_restrict(chunk_range)
wenzelm@43450
   385
          } yield r2
wenzelm@43382
   386
wenzelm@56373
   387
        val padded_markup_iterator =
wenzelm@43759
   388
          if (markup.isEmpty)
wenzelm@46197
   389
            Iterator(Text.Info(chunk_range, chunk_color))
wenzelm@43759
   390
          else
wenzelm@46197
   391
            Iterator(
wenzelm@46197
   392
              Text.Info(Text.Range(chunk_range.start, markup.head.range.start), chunk_color)) ++
wenzelm@43759
   393
            markup.iterator ++
wenzelm@46197
   394
            Iterator(Text.Info(Text.Range(markup.last.range.stop, chunk_range.stop), chunk_color))
wenzelm@43759
   395
wenzelm@43450
   396
        var x1 = x + w
wenzelm@43382
   397
        gfx.setFont(chunk_font)
wenzelm@56373
   398
        for (Text.Info(range, color) <- padded_markup_iterator if !range.is_singularity) {
wenzelm@44662
   399
          val str = chunk_str.substring(range.start - chunk_offset, range.stop - chunk_offset)
wenzelm@46197
   400
          gfx.setColor(color)
wenzelm@43448
   401
wenzelm@43759
   402
          range.try_restrict(caret_range) match {
wenzelm@43759
   403
            case Some(r) if !r.is_singularity =>
wenzelm@43759
   404
              val i = r.start - range.start
wenzelm@43759
   405
              val j = r.stop - range.start
wenzelm@44056
   406
              val s1 = str.substring(0, i)
wenzelm@44056
   407
              val s2 = str.substring(i, j)
wenzelm@44056
   408
              val s3 = str.substring(j)
wenzelm@44056
   409
wenzelm@44056
   410
              if (!s1.isEmpty) gfx.drawString(s1, x1, y)
wenzelm@44056
   411
wenzelm@44056
   412
              val astr = new AttributedString(s2)
wenzelm@43759
   413
              astr.addAttribute(TextAttribute.FONT, chunk_font)
wenzelm@55747
   414
              astr.addAttribute(TextAttribute.FOREGROUND, caret_color(rendering))
wenzelm@44056
   415
              astr.addAttribute(TextAttribute.SWAP_COLORS, TextAttribute.SWAP_COLORS_ON)
wenzelm@44056
   416
              gfx.drawString(astr.getIterator, x1 + string_width(s1), y)
wenzelm@44056
   417
wenzelm@44056
   418
              if (!s3.isEmpty)
wenzelm@44056
   419
                gfx.drawString(s3, x1 + string_width(str.substring(0, j)), y)
wenzelm@44056
   420
wenzelm@43759
   421
            case _ =>
wenzelm@43759
   422
              gfx.drawString(str, x1, y)
wenzelm@43382
   423
          }
wenzelm@44056
   424
          x1 += string_width(str)
wenzelm@43382
   425
        }
wenzelm@43382
   426
      }
wenzelm@43382
   427
      w += chunk.width
wenzelm@43382
   428
      chunk = chunk.next.asInstanceOf[Chunk]
wenzelm@43382
   429
    }
wenzelm@43382
   430
    w
wenzelm@43382
   431
  }
wenzelm@43382
   432
wenzelm@43381
   433
  private val text_painter = new TextAreaExtension
wenzelm@43381
   434
  {
wenzelm@43381
   435
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43381
   436
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43382
   437
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43381
   438
    {
wenzelm@49356
   439
      robust_rendering { rendering =>
wenzelm@51581
   440
        val painter = text_area.getPainter
wenzelm@51581
   441
        val fm = painter.getFontMetrics
wenzelm@51581
   442
        val lm = painter.getFont.getLineMetrics(" ", painter.getFontRenderContext)
wenzelm@51581
   443
wenzelm@43381
   444
        val clip = gfx.getClip
wenzelm@43381
   445
        val x0 = text_area.getHorizontalOffset
wenzelm@43382
   446
        var y0 = y + fm.getHeight - (fm.getLeading + 1) - fm.getDescent
wenzelm@43372
   447
wenzelm@51581
   448
        val (bullet_x, bullet_y, bullet_w, bullet_h) =
wenzelm@51581
   449
        {
wenzelm@51581
   450
          val w = fm.charWidth(' ')
wenzelm@51581
   451
          val b = (w / 2) max 1
wenzelm@51581
   452
          val c = (lm.getAscent + lm.getStrikethroughOffset).round.toInt
wenzelm@51581
   453
          ((w - b + 1) / 2, c - b / 2, w - b, line_height - b)
wenzelm@51581
   454
        }
wenzelm@51581
   455
wenzelm@43381
   456
        for (i <- 0 until physical_lines.length) {
wenzelm@49097
   457
          val line = physical_lines(i)
wenzelm@49097
   458
          if (line != -1) {
wenzelm@56884
   459
            val line_range = Text.Range(start(i), end(i) min buffer.getLength)
wenzelm@51581
   460
wenzelm@51581
   461
            // bullet bar
wenzelm@51581
   462
            for {
wenzelm@51581
   463
              Text.Info(range, color) <- rendering.bullet(line_range)
wenzelm@51581
   464
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@51581
   465
            } {
wenzelm@51581
   466
              gfx.setColor(color)
wenzelm@51581
   467
              gfx.fillRect(r.x + bullet_x, y + i * line_height + bullet_y,
wenzelm@51581
   468
                r.length - bullet_w, line_height - bullet_h)
wenzelm@51581
   469
            }
wenzelm@51581
   470
wenzelm@51581
   471
            // text chunks
wenzelm@49097
   472
            val screen_line = first_line + i
wenzelm@49097
   473
            val chunks = text_area.getChunksOfScreenLine(screen_line)
wenzelm@49097
   474
            if (chunks != null) {
wenzelm@56358
   475
              try {
wenzelm@56358
   476
                val line_start = buffer.getLineStartOffset(line)
wenzelm@56358
   477
                gfx.clipRect(x0, y + line_height * i, Integer.MAX_VALUE, line_height)
wenzelm@56358
   478
                val w = paint_chunk_list(rendering, gfx, line_start, chunks, x0, y0).toInt
wenzelm@56358
   479
                gfx.clipRect(x0 + w.toInt, 0, Integer.MAX_VALUE, Integer.MAX_VALUE)
wenzelm@56358
   480
                orig_text_painter.paintValidLine(gfx,
wenzelm@56358
   481
                  screen_line, line, start(i), end(i), y + line_height * i)
wenzelm@56358
   482
              } finally { gfx.setClip(clip) }
wenzelm@49097
   483
            }
wenzelm@43369
   484
          }
wenzelm@43381
   485
          y0 += line_height
wenzelm@43369
   486
        }
wenzelm@43369
   487
      }
wenzelm@43369
   488
    }
wenzelm@43369
   489
  }
wenzelm@43369
   490
wenzelm@43369
   491
wenzelm@43435
   492
  /* foreground */
wenzelm@43435
   493
wenzelm@43435
   494
  private val foreground_painter = new TextAreaExtension
wenzelm@43435
   495
  {
wenzelm@43435
   496
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@43435
   497
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@43435
   498
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@43435
   499
    {
wenzelm@49356
   500
      robust_rendering { rendering =>
wenzelm@56883
   501
        val search_pattern = get_search_pattern()
wenzelm@43435
   502
        for (i <- 0 until physical_lines.length) {
wenzelm@43435
   503
          if (physical_lines(i) != -1) {
wenzelm@56884
   504
            val line_range = Text.Range(start(i), end(i) min buffer.getLength)
wenzelm@43435
   505
wenzelm@44545
   506
            // foreground color
wenzelm@44545
   507
            for {
wenzelm@49356
   508
              Text.Info(range, color) <- rendering.foreground(line_range)
wenzelm@49409
   509
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@44545
   510
            } {
wenzelm@44545
   511
              gfx.setColor(color)
wenzelm@44545
   512
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@44545
   513
            }
wenzelm@44545
   514
wenzelm@56883
   515
            // search pattern
wenzelm@56883
   516
            for {
wenzelm@56883
   517
              regex <- search_pattern
wenzelm@56883
   518
              text <- JEdit_Lib.try_get_text(buffer, line_range)
wenzelm@56883
   519
              m <- regex.findAllMatchIn(text)
wenzelm@56883
   520
              range = Text.Range(m.start, m.end) + line_range.start
wenzelm@56883
   521
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@56883
   522
            } {
wenzelm@56883
   523
              gfx.setColor(rendering.search_color)
wenzelm@56883
   524
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@56883
   525
            }
wenzelm@56883
   526
wenzelm@49357
   527
            // highlight range -- potentially from other snapshot
wenzelm@46205
   528
            for {
wenzelm@49410
   529
              info <- highlight_area.info
wenzelm@46205
   530
              Text.Info(range, color) <- info.try_restrict(line_range)
wenzelm@49409
   531
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@46205
   532
            } {
wenzelm@46205
   533
              gfx.setColor(color)
wenzelm@46205
   534
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@43435
   535
            }
wenzelm@48921
   536
wenzelm@48921
   537
            // hyperlink range -- potentially from other snapshot
wenzelm@48921
   538
            for {
wenzelm@49410
   539
              info <- hyperlink_area.info
wenzelm@48921
   540
              Text.Info(range, _) <- info.try_restrict(line_range)
wenzelm@49409
   541
              r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@48921
   542
            } {
wenzelm@49356
   543
              gfx.setColor(rendering.hyperlink_color)
wenzelm@48921
   544
              gfx.drawRect(r.x, y + i * line_height, r.length - 1, line_height - 1)
wenzelm@48921
   545
            }
wenzelm@55688
   546
wenzelm@55688
   547
            // completion range
wenzelm@55747
   548
            if (!hyperlink_area.is_active && caret_visible) {
wenzelm@55747
   549
              for {
wenzelm@55747
   550
                completion <- Completion_Popup.Text_Area(text_area)
wenzelm@55747
   551
                Text.Info(range, color) <- completion.rendering(rendering, line_range)
wenzelm@55747
   552
                r <- JEdit_Lib.gfx_range(text_area, range)
wenzelm@55747
   553
              } {
wenzelm@55747
   554
                gfx.setColor(color)
wenzelm@55747
   555
                gfx.drawRect(r.x, y + i * line_height, r.length - 1, line_height - 1)
wenzelm@55711
   556
              }
wenzelm@55688
   557
            }
wenzelm@43435
   558
          }
wenzelm@43435
   559
        }
wenzelm@43435
   560
      }
wenzelm@43435
   561
    }
wenzelm@43435
   562
  }
wenzelm@43435
   563
wenzelm@43435
   564
wenzelm@43393
   565
  /* caret -- outside of text range */
wenzelm@43393
   566
wenzelm@43393
   567
  private class Caret_Painter(before: Boolean) extends TextAreaExtension
wenzelm@43393
   568
  {
wenzelm@43393
   569
    override def paintValidLine(gfx: Graphics2D,
wenzelm@43393
   570
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
wenzelm@43393
   571
    {
wenzelm@49356
   572
      robust_rendering { _ =>
wenzelm@43404
   573
        if (before) gfx.clipRect(0, 0, 0, 0)
wenzelm@43404
   574
        else gfx.setClip(painter_clip)
wenzelm@43404
   575
      }
wenzelm@43393
   576
    }
wenzelm@43393
   577
  }
wenzelm@43393
   578
wenzelm@43393
   579
  private val before_caret_painter1 = new Caret_Painter(true)
wenzelm@43393
   580
  private val after_caret_painter1 = new Caret_Painter(false)
wenzelm@43393
   581
  private val before_caret_painter2 = new Caret_Painter(true)
wenzelm@43393
   582
  private val after_caret_painter2 = new Caret_Painter(false)
wenzelm@43393
   583
wenzelm@43393
   584
  private val caret_painter = new TextAreaExtension
wenzelm@43393
   585
  {
wenzelm@43393
   586
    override def paintValidLine(gfx: Graphics2D,
wenzelm@43393
   587
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
wenzelm@43393
   588
    {
wenzelm@55713
   589
      robust_rendering { rendering =>
wenzelm@55713
   590
        if (caret_visible) {
wenzelm@43398
   591
          val caret = text_area.getCaretPosition
wenzelm@55766
   592
          if (caret_enabled && start <= caret && caret == end - 1) {
wenzelm@43398
   593
            val painter = text_area.getPainter
wenzelm@43398
   594
            val fm = painter.getFontMetrics
wenzelm@43393
   595
wenzelm@43398
   596
            val offset = caret - text_area.getLineStartOffset(physical_line)
wenzelm@43398
   597
            val x = text_area.offsetToXY(physical_line, offset).x
wenzelm@56358
   598
            val y1 = y + fm.getHeight - (fm.getLeading + 1) - fm.getDescent
wenzelm@56358
   599
wenzelm@56358
   600
            val astr = new AttributedString(" ")
wenzelm@56358
   601
            astr.addAttribute(TextAttribute.FONT, painter.getFont)
wenzelm@56358
   602
            astr.addAttribute(TextAttribute.FOREGROUND, caret_color(rendering))
wenzelm@56358
   603
            astr.addAttribute(TextAttribute.SWAP_COLORS, TextAttribute.SWAP_COLORS_ON)
wenzelm@56358
   604
wenzelm@56358
   605
            val clip = gfx.getClip
wenzelm@56358
   606
            try {
wenzelm@56358
   607
              gfx.clipRect(x, y, Integer.MAX_VALUE, painter.getLineHeight)
wenzelm@56358
   608
              gfx.drawString(astr.getIterator, x, y1)
wenzelm@56358
   609
            }
wenzelm@56358
   610
            finally { gfx.setClip(clip) }
wenzelm@43398
   611
          }
wenzelm@43393
   612
        }
wenzelm@43393
   613
      }
wenzelm@43393
   614
    }
wenzelm@43393
   615
  }
wenzelm@43393
   616
wenzelm@43393
   617
wenzelm@43369
   618
  /* activation */
wenzelm@43369
   619
wenzelm@43369
   620
  def activate()
wenzelm@43369
   621
  {
wenzelm@43369
   622
    val painter = text_area.getPainter
wenzelm@43393
   623
    painter.addExtension(TextAreaPainter.LOWEST_LAYER, set_state)
wenzelm@43381
   624
    painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, background_painter)
wenzelm@43381
   625
    painter.addExtension(TextAreaPainter.TEXT_LAYER, text_painter)
wenzelm@43393
   626
    painter.addExtension(TextAreaPainter.CARET_LAYER - 1, before_caret_painter1)
wenzelm@43393
   627
    painter.addExtension(TextAreaPainter.CARET_LAYER + 1, after_caret_painter1)
wenzelm@43393
   628
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER - 1, before_caret_painter2)
wenzelm@43393
   629
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 1, after_caret_painter2)
wenzelm@43393
   630
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 2, caret_painter)
wenzelm@43435
   631
    painter.addExtension(500, foreground_painter)
wenzelm@43393
   632
    painter.addExtension(TextAreaPainter.HIGHEST_LAYER, reset_state)
wenzelm@43369
   633
    painter.removeExtension(orig_text_painter)
wenzelm@49410
   634
    painter.addMouseListener(mouse_listener)
wenzelm@49410
   635
    painter.addMouseMotionListener(mouse_motion_listener)
wenzelm@49410
   636
    text_area.addFocusListener(focus_listener)
wenzelm@49410
   637
    view.addWindowListener(window_listener)
wenzelm@43369
   638
  }
wenzelm@43369
   639
wenzelm@43369
   640
  def deactivate()
wenzelm@43369
   641
  {
wenzelm@56340
   642
    active_reset()
wenzelm@43369
   643
    val painter = text_area.getPainter
wenzelm@49410
   644
    view.removeWindowListener(window_listener)
wenzelm@49410
   645
    text_area.removeFocusListener(focus_listener)
wenzelm@49410
   646
    painter.removeMouseMotionListener(mouse_motion_listener)
wenzelm@49410
   647
    painter.removeMouseListener(mouse_listener)
wenzelm@43396
   648
    painter.addExtension(TextAreaPainter.TEXT_LAYER, orig_text_painter)
wenzelm@43393
   649
    painter.removeExtension(reset_state)
wenzelm@43435
   650
    painter.removeExtension(foreground_painter)
wenzelm@43393
   651
    painter.removeExtension(caret_painter)
wenzelm@43393
   652
    painter.removeExtension(after_caret_painter2)
wenzelm@43393
   653
    painter.removeExtension(before_caret_painter2)
wenzelm@43393
   654
    painter.removeExtension(after_caret_painter1)
wenzelm@43393
   655
    painter.removeExtension(before_caret_painter1)
wenzelm@43381
   656
    painter.removeExtension(text_painter)
wenzelm@43381
   657
    painter.removeExtension(background_painter)
wenzelm@43393
   658
    painter.removeExtension(set_state)
wenzelm@43369
   659
  }
wenzelm@43369
   660
}
wenzelm@43369
   661