src/Tools/jEdit/src/jedit/document_view.scala
author wenzelm
Tue Sep 07 23:06:52 2010 +0200 (2010-09-07)
changeset 39178 83e9f3ccea9f
parent 39177 0468964aec11
child 39181 2257eded8323
permissions -rw-r--r--
concentrate Isabelle specific physical rendering markup selection in isabelle_markup.scala;
wenzelm@36760
     1
/*  Title:      Tools/jEdit/src/jedit/document_view.scala
wenzelm@36760
     2
    Author:     Fabian Immler, TU Munich
wenzelm@36760
     3
    Author:     Makarius
wenzelm@36760
     4
wenzelm@36760
     5
Document view connected to jEdit text area.
wenzelm@36760
     6
*/
wenzelm@34408
     7
immler@34403
     8
package isabelle.jedit
immler@34403
     9
wenzelm@34760
    10
wenzelm@36015
    11
import isabelle._
wenzelm@36015
    12
wenzelm@34784
    13
import scala.actors.Actor._
wenzelm@34784
    14
wenzelm@39131
    15
import java.awt.event.{MouseAdapter, MouseMotionAdapter, MouseEvent, FocusAdapter, FocusEvent}
wenzelm@39178
    16
import java.awt.{BorderLayout, Graphics, Color, Dimension, Graphics2D}
wenzelm@34734
    17
import javax.swing.{JPanel, ToolTipManager}
wenzelm@34784
    18
import javax.swing.event.{CaretListener, CaretEvent}
wenzelm@34734
    19
wenzelm@39178
    20
import org.gjt.sp.jedit.{jEdit, OperatingSystem}
wenzelm@34709
    21
import org.gjt.sp.jedit.gui.RolloverButton
wenzelm@39176
    22
import org.gjt.sp.jedit.options.GutterOptionPane
wenzelm@34784
    23
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaExtension, TextAreaPainter}
wenzelm@37241
    24
import org.gjt.sp.jedit.syntax.SyntaxStyle
wenzelm@34784
    25
wenzelm@34784
    26
wenzelm@34784
    27
object Document_View
wenzelm@34784
    28
{
wenzelm@34784
    29
  /* document view of text area */
wenzelm@34784
    30
wenzelm@34784
    31
  private val key = new Object
wenzelm@34784
    32
wenzelm@34784
    33
  def init(model: Document_Model, text_area: TextArea): Document_View =
wenzelm@34784
    34
  {
wenzelm@38223
    35
    Swing_Thread.require()
wenzelm@34784
    36
    val doc_view = new Document_View(model, text_area)
wenzelm@34784
    37
    text_area.putClientProperty(key, doc_view)
wenzelm@34784
    38
    doc_view.activate()
wenzelm@34784
    39
    doc_view
wenzelm@34784
    40
  }
wenzelm@34784
    41
wenzelm@34788
    42
  def apply(text_area: TextArea): Option[Document_View] =
wenzelm@34784
    43
  {
wenzelm@38223
    44
    Swing_Thread.require()
wenzelm@34784
    45
    text_area.getClientProperty(key) match {
wenzelm@34784
    46
      case doc_view: Document_View => Some(doc_view)
wenzelm@34784
    47
      case _ => None
wenzelm@34784
    48
    }
wenzelm@34784
    49
  }
wenzelm@34784
    50
wenzelm@34784
    51
  def exit(text_area: TextArea)
wenzelm@34784
    52
  {
wenzelm@38223
    53
    Swing_Thread.require()
wenzelm@34788
    54
    apply(text_area) match {
wenzelm@34784
    55
      case None => error("No document view for text area: " + text_area)
wenzelm@34784
    56
      case Some(doc_view) =>
wenzelm@34784
    57
        doc_view.deactivate()
wenzelm@34784
    58
        text_area.putClientProperty(key, null)
wenzelm@34784
    59
    }
wenzelm@34784
    60
  }
wenzelm@34784
    61
}
immler@34403
    62
wenzelm@34733
    63
wenzelm@37129
    64
class Document_View(val model: Document_Model, text_area: TextArea)
immler@34654
    65
{
wenzelm@34784
    66
  private val session = model.session
wenzelm@34784
    67
immler@34403
    68
wenzelm@37241
    69
  /* extended token styles */
wenzelm@37241
    70
wenzelm@37241
    71
  private var styles: Array[SyntaxStyle] = null  // owned by Swing thread
wenzelm@37241
    72
wenzelm@37241
    73
  def extend_styles()
wenzelm@37241
    74
  {
wenzelm@38223
    75
    Swing_Thread.require()
wenzelm@38158
    76
    styles = Document_Model.Token_Markup.extend_styles(text_area.getPainter.getStyles)
wenzelm@37241
    77
  }
wenzelm@37241
    78
  extend_styles()
wenzelm@37241
    79
wenzelm@37241
    80
  def set_styles()
wenzelm@37241
    81
  {
wenzelm@38223
    82
    Swing_Thread.require()
wenzelm@37241
    83
    text_area.getPainter.setStyles(styles)
wenzelm@37241
    84
  }
wenzelm@37241
    85
wenzelm@37241
    86
wenzelm@38881
    87
  /* visible line ranges */
wenzelm@38881
    88
wenzelm@38883
    89
  // simplify slightly odd result of TextArea.getScreenLineEndOffset etc.
wenzelm@38881
    90
  // NB: jEdit already normalizes \r\n and \r to \n
wenzelm@38881
    91
  def proper_line_range(start: Text.Offset, end: Text.Offset): Text.Range =
wenzelm@38881
    92
  {
wenzelm@38883
    93
    val stop = if (start < end) end - 1 else end min model.buffer.getLength
wenzelm@38881
    94
    Text.Range(start, stop)
wenzelm@38881
    95
  }
wenzelm@38881
    96
wenzelm@38881
    97
  def screen_lines_range(): Text.Range =
wenzelm@38881
    98
  {
wenzelm@38881
    99
    val start = text_area.getScreenLineStartOffset(0)
wenzelm@38881
   100
    val raw_end = text_area.getScreenLineEndOffset(text_area.getVisibleLines - 1 max 0)
wenzelm@38881
   101
    proper_line_range(start, if (raw_end >= 0) raw_end else model.buffer.getLength)
wenzelm@38881
   102
  }
wenzelm@38881
   103
wenzelm@39132
   104
  def invalidate_line_range(range: Text.Range)
wenzelm@39132
   105
  {
wenzelm@39132
   106
    text_area.invalidateLineRange(
wenzelm@39132
   107
      model.buffer.getLineOfOffset(range.start),
wenzelm@39132
   108
      model.buffer.getLineOfOffset(range.stop))
wenzelm@39132
   109
  }
wenzelm@39132
   110
wenzelm@38881
   111
wenzelm@37129
   112
  /* commands_changed_actor */
wenzelm@34834
   113
wenzelm@37129
   114
  private val commands_changed_actor = actor {
wenzelm@34784
   115
    loop {
wenzelm@34784
   116
      react {
wenzelm@38360
   117
        case Session.Commands_Changed(changed) =>
wenzelm@38843
   118
          val buffer = model.buffer
wenzelm@38843
   119
          Isabelle.swing_buffer_lock(buffer) {
wenzelm@38151
   120
            val snapshot = model.snapshot()
wenzelm@38881
   121
wenzelm@38884
   122
            if (changed.exists(snapshot.node.commands.contains))
wenzelm@38884
   123
              overview.repaint()
wenzelm@38884
   124
wenzelm@38881
   125
            val visible_range = screen_lines_range()
wenzelm@38881
   126
            val visible_cmds = snapshot.node.command_range(snapshot.revert(visible_range)).map(_._1)
wenzelm@38881
   127
            if (visible_cmds.exists(changed)) {
wenzelm@38881
   128
              for {
wenzelm@38881
   129
                line <- 0 until text_area.getVisibleLines
wenzelm@38881
   130
                val start = text_area.getScreenLineStartOffset(line) if start >= 0
wenzelm@38881
   131
                val end = text_area.getScreenLineEndOffset(line) if end >= 0
wenzelm@38881
   132
                val range = proper_line_range(start, end)
wenzelm@38881
   133
                val line_cmds = snapshot.node.command_range(snapshot.revert(range)).map(_._1)
wenzelm@38881
   134
                if line_cmds.exists(changed)
wenzelm@38881
   135
              } text_area.invalidateScreenLineRange(line, line)
wenzelm@38884
   136
wenzelm@38843
   137
              // FIXME danger of deadlock!?
wenzelm@38881
   138
              // FIXME potentially slow!?
wenzelm@38881
   139
              model.buffer.propertiesChanged()
wenzelm@38640
   140
            }
wenzelm@34834
   141
          }
wenzelm@34784
   142
wenzelm@34784
   143
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@34784
   144
      }
immler@34403
   145
    }
immler@34678
   146
  }
immler@34403
   147
immler@34403
   148
wenzelm@39131
   149
  /* subexpression highlighting */
wenzelm@39131
   150
wenzelm@39178
   151
  private def subexp_range(snapshot: Document.Snapshot, x: Int, y: Int)
wenzelm@39178
   152
    : Option[(Text.Range, Color)] =
wenzelm@39132
   153
  {
wenzelm@39132
   154
    val offset = text_area.xyToOffset(x, y)
wenzelm@39178
   155
    snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.subexp) match {
wenzelm@39178
   156
      case Text.Info(_, Some((range, color))) #:: _ => Some((snapshot.convert(range), color))
wenzelm@39177
   157
      case _ => None
wenzelm@39177
   158
    }
wenzelm@39132
   159
  }
wenzelm@39132
   160
wenzelm@39178
   161
  private var highlight_range: Option[(Text.Range, Color)] = None
wenzelm@39131
   162
wenzelm@39131
   163
  private val focus_listener = new FocusAdapter {
wenzelm@39132
   164
    override def focusLost(e: FocusEvent) { highlight_range = None }
wenzelm@39131
   165
  }
wenzelm@39131
   166
wenzelm@39131
   167
  private val mouse_motion_listener = new MouseMotionAdapter {
wenzelm@39131
   168
    override def mouseMoved(e: MouseEvent) {
wenzelm@39131
   169
      val control = if (OperatingSystem.isMacOS()) e.isMetaDown else e.isControlDown
wenzelm@39132
   170
      if (!model.buffer.isLoaded) highlight_range = None
wenzelm@39132
   171
      else
wenzelm@39132
   172
        Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@39178
   173
          highlight_range map { case (range, _) => invalidate_line_range(range) }
wenzelm@39132
   174
          highlight_range =
wenzelm@39132
   175
            if (control) subexp_range(model.snapshot(), e.getX(), e.getY()) else None
wenzelm@39178
   176
          highlight_range map { case (range, _) => invalidate_line_range(range) }
wenzelm@39132
   177
        }
wenzelm@39131
   178
    }
wenzelm@39131
   179
  }
wenzelm@39131
   180
wenzelm@39131
   181
wenzelm@34784
   182
  /* text_area_extension */
wenzelm@34784
   183
wenzelm@34784
   184
  private val text_area_extension = new TextAreaExtension
immler@34678
   185
  {
wenzelm@37685
   186
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@37685
   187
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@38886
   188
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@34784
   189
    {
wenzelm@38843
   190
      Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@38843
   191
        val snapshot = model.snapshot()
wenzelm@38843
   192
        val saved_color = gfx.getColor
wenzelm@39044
   193
        val ascent = text_area.getPainter.getFontMetrics.getAscent
wenzelm@39131
   194
wenzelm@38843
   195
        try {
wenzelm@38843
   196
          for (i <- 0 until physical_lines.length) {
wenzelm@38843
   197
            if (physical_lines(i) != -1) {
wenzelm@38881
   198
              val line_range = proper_line_range(start(i), end(i))
wenzelm@39044
   199
wenzelm@39171
   200
              // background color: status
wenzelm@38880
   201
              val cmds = snapshot.node.command_range(snapshot.revert(line_range))
wenzelm@39044
   202
              for {
wenzelm@39044
   203
                (command, command_start) <- cmds if !command.is_ignored
wenzelm@38880
   204
                val range = line_range.restrict(snapshot.convert(command.range + command_start))
wenzelm@39044
   205
                r <- Isabelle.gfx_range(text_area, range)
wenzelm@39178
   206
                color <- Isabelle_Markup.status_color(snapshot, command)
wenzelm@39044
   207
              } {
wenzelm@39169
   208
                gfx.setColor(color)
wenzelm@39044
   209
                gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@39044
   210
              }
wenzelm@39044
   211
wenzelm@39171
   212
              // background color: markup
wenzelm@39171
   213
              for {
wenzelm@39177
   214
                Text.Info(range, Some(color)) <-
wenzelm@39178
   215
                  snapshot.select_markup(line_range)(Isabelle_Markup.background).iterator
wenzelm@39171
   216
                r <- Isabelle.gfx_range(text_area, range)
wenzelm@39171
   217
              } {
wenzelm@39171
   218
                gfx.setColor(color)
wenzelm@39171
   219
                gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@39171
   220
              }
wenzelm@39171
   221
wenzelm@39178
   222
              // sub-expression highlighting -- potentially from other snapshot
wenzelm@39178
   223
              highlight_range match {
wenzelm@39178
   224
                case Some((range, color)) if line_range.overlaps(range) =>
wenzelm@39178
   225
                  Isabelle.gfx_range(text_area, line_range.restrict(range)) match {
wenzelm@39131
   226
                    case None =>
wenzelm@39131
   227
                    case Some(r) =>
wenzelm@39178
   228
                      gfx.setColor(color)
wenzelm@39131
   229
                      gfx.drawRect(r.x, y + i * line_height, r.length, line_height - 1)
wenzelm@39131
   230
                  }
wenzelm@39178
   231
                case _ =>
wenzelm@39131
   232
              }
wenzelm@39131
   233
wenzelm@39168
   234
              // boxed text
wenzelm@39168
   235
              for {
wenzelm@39177
   236
                Text.Info(range, Some(color)) <-
wenzelm@39178
   237
                  snapshot.select_markup(line_range)(Isabelle_Markup.box).iterator
wenzelm@39168
   238
                r <- Isabelle.gfx_range(text_area, range)
wenzelm@39168
   239
              } {
wenzelm@39168
   240
                gfx.setColor(color)
wenzelm@39168
   241
                gfx.drawRect(r.x + 1, y + i * line_height + 1, r.length - 2, line_height - 3)
wenzelm@39168
   242
              }
wenzelm@39168
   243
wenzelm@39044
   244
              // squiggly underline
wenzelm@39044
   245
              for {
wenzelm@39177
   246
                Text.Info(range, Some(color)) <-
wenzelm@39178
   247
                  snapshot.select_markup(line_range)(Isabelle_Markup.message).iterator
wenzelm@39044
   248
                if color != null
wenzelm@39044
   249
                r <- Isabelle.gfx_range(text_area, range)
wenzelm@39044
   250
              } {
wenzelm@39044
   251
                gfx.setColor(color)
wenzelm@39044
   252
                val x0 = (r.x / 2) * 2
wenzelm@39044
   253
                val y0 = r.y + ascent + 1
wenzelm@39044
   254
                for (x1 <- Range(x0, x0 + r.length, 2)) {
wenzelm@39044
   255
                  val y1 = if (x1 % 4 < 2) y0 else y0 + 1
wenzelm@39044
   256
                  gfx.drawLine(x1, y1, x1 + 1, y1)
wenzelm@38883
   257
                }
wenzelm@38843
   258
              }
wenzelm@38843
   259
            }
wenzelm@38223
   260
          }
wenzelm@38223
   261
        }
wenzelm@38843
   262
        finally { gfx.setColor(saved_color) }
wenzelm@38223
   263
      }
wenzelm@34784
   264
    }
wenzelm@34784
   265
wenzelm@34784
   266
    override def getToolTipText(x: Int, y: Int): String =
wenzelm@34784
   267
    {
wenzelm@38845
   268
      Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@38845
   269
        val snapshot = model.snapshot()
wenzelm@38845
   270
        val offset = text_area.xyToOffset(x, y)
wenzelm@39178
   271
        snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.tooltip) match
wenzelm@39178
   272
        {
wenzelm@39177
   273
          case Text.Info(_, Some(text)) #:: _ => Isabelle.tooltip(text)
wenzelm@39177
   274
          case _ => null
wenzelm@39174
   275
        }
wenzelm@34784
   276
      }
wenzelm@34734
   277
    }
immler@34678
   278
  }
immler@34513
   279
wenzelm@34784
   280
wenzelm@39175
   281
  /* gutter_extension */
wenzelm@39175
   282
wenzelm@39175
   283
  private val gutter_extension = new TextAreaExtension
wenzelm@39175
   284
  {
wenzelm@39175
   285
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@39175
   286
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@39175
   287
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@39175
   288
    {
wenzelm@39176
   289
      val gutter = text_area.getGutter
wenzelm@39176
   290
      val width = GutterOptionPane.getSelectionAreaWidth
wenzelm@39176
   291
      val border_width = jEdit.getIntegerProperty("view.gutter.borderWidth", 3)
wenzelm@39176
   292
      val FOLD_MARKER_SIZE = 12
wenzelm@39176
   293
wenzelm@39176
   294
      if (gutter.isSelectionAreaEnabled && !gutter.isExpanded && width >= 12 && line_height >= 12) {
wenzelm@39176
   295
        Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@39176
   296
          val snapshot = model.snapshot()
wenzelm@39175
   297
          for (i <- 0 until physical_lines.length) {
wenzelm@39175
   298
            if (physical_lines(i) != -1) {
wenzelm@39175
   299
              val line_range = proper_line_range(start(i), end(i))
wenzelm@39175
   300
wenzelm@39175
   301
              // gutter icons
wenzelm@39176
   302
              val cmds = snapshot.node.command_range(snapshot.revert(line_range))
wenzelm@39176
   303
              val states = cmds.map(p => snapshot.state(p._1)).toStream
wenzelm@39175
   304
              val opt_icon =
wenzelm@39175
   305
                if (states.exists(_.results.exists(p => Isar_Document.is_error(p._2))))
wenzelm@39178
   306
                  Some(Isabelle_Markup.error_icon)
wenzelm@39175
   307
                else if (states.exists(_.results.exists(p => Isar_Document.is_warning(p._2))))
wenzelm@39178
   308
                  Some(Isabelle_Markup.warning_icon)
wenzelm@39175
   309
                else None
wenzelm@39175
   310
              opt_icon match {
wenzelm@39175
   311
                case Some(icon) if icon.getIconWidth > 0 && icon.getIconHeight > 0 =>
wenzelm@39176
   312
                  val x0 = (FOLD_MARKER_SIZE + width - border_width - icon.getIconWidth) max 10
wenzelm@39175
   313
                  val y0 = y + i * line_height + (((line_height - icon.getIconHeight) / 2) max 0)
wenzelm@39176
   314
                  icon.paintIcon(gutter, gfx, x0, y0)
wenzelm@39175
   315
                case _ =>
wenzelm@39175
   316
              }
wenzelm@39175
   317
            }
wenzelm@39175
   318
          }
wenzelm@39175
   319
        }
wenzelm@39175
   320
      }
wenzelm@39175
   321
    }
wenzelm@39175
   322
  }
wenzelm@39175
   323
wenzelm@39175
   324
wenzelm@37129
   325
  /* caret handling */
wenzelm@34810
   326
wenzelm@37849
   327
  def selected_command(): Option[Command] =
wenzelm@38223
   328
  {
wenzelm@38223
   329
    Swing_Thread.require()
wenzelm@38151
   330
    model.snapshot().node.proper_command_at(text_area.getCaretPosition)
wenzelm@38223
   331
  }
wenzelm@34810
   332
wenzelm@37849
   333
  private val caret_listener = new CaretListener {
wenzelm@37849
   334
    private val delay = Swing_Thread.delay_last(session.input_delay) {
wenzelm@37849
   335
      session.perspective.event(Session.Perspective)
wenzelm@34810
   336
    }
wenzelm@37849
   337
    override def caretUpdate(e: CaretEvent) { delay() }
wenzelm@34810
   338
  }
wenzelm@34810
   339
wenzelm@34810
   340
wenzelm@34784
   341
  /* overview of command status left of scrollbar */
wenzelm@34784
   342
wenzelm@34834
   343
  private val overview = new JPanel(new BorderLayout)
wenzelm@34784
   344
  {
wenzelm@34784
   345
    private val WIDTH = 10
wenzelm@34784
   346
    private val HEIGHT = 2
wenzelm@34784
   347
wenzelm@34806
   348
    setPreferredSize(new Dimension(WIDTH, 0))
wenzelm@34806
   349
wenzelm@34784
   350
    setRequestFocusEnabled(false)
wenzelm@34784
   351
wenzelm@34784
   352
    addMouseListener(new MouseAdapter {
wenzelm@34784
   353
      override def mousePressed(event: MouseEvent) {
wenzelm@34784
   354
        val line = y_to_line(event.getY)
wenzelm@34784
   355
        if (line >= 0 && line < text_area.getLineCount)
wenzelm@34784
   356
          text_area.setCaretPosition(text_area.getLineStartOffset(line))
wenzelm@34784
   357
      }
wenzelm@34784
   358
    })
wenzelm@34784
   359
wenzelm@34784
   360
    override def addNotify() {
wenzelm@34784
   361
      super.addNotify()
wenzelm@34784
   362
      ToolTipManager.sharedInstance.registerComponent(this)
wenzelm@34784
   363
    }
immler@34403
   364
wenzelm@34784
   365
    override def removeNotify() {
wenzelm@34784
   366
      ToolTipManager.sharedInstance.unregisterComponent(this)
wenzelm@34784
   367
      super.removeNotify
wenzelm@34784
   368
    }
wenzelm@34784
   369
wenzelm@34784
   370
    override def getToolTipText(event: MouseEvent): String =
wenzelm@34784
   371
    {
wenzelm@34784
   372
      val line = y_to_line(event.getY())
wenzelm@34784
   373
      if (line >= 0 && line < text_area.getLineCount) "<html><b>TODO:</b><br>Tooltip</html>"
wenzelm@34784
   374
      else ""
wenzelm@34784
   375
    }
wenzelm@34784
   376
wenzelm@34784
   377
    override def paintComponent(gfx: Graphics)
wenzelm@34784
   378
    {
wenzelm@34784
   379
      super.paintComponent(gfx)
wenzelm@38640
   380
      Swing_Thread.assert()
wenzelm@34784
   381
      val buffer = model.buffer
wenzelm@38843
   382
      Isabelle.buffer_lock(buffer) {
wenzelm@38640
   383
        val snapshot = model.snapshot()
wenzelm@38640
   384
        val saved_color = gfx.getColor  // FIXME needed!?
wenzelm@38640
   385
        try {
wenzelm@39169
   386
          for {
wenzelm@39169
   387
            (command, start) <- snapshot.node.command_starts
wenzelm@39169
   388
            if !command.is_ignored
wenzelm@38640
   389
            val line1 = buffer.getLineOfOffset(snapshot.convert(start))
wenzelm@38640
   390
            val line2 = buffer.getLineOfOffset(snapshot.convert(start + command.length)) + 1
wenzelm@38640
   391
            val y = line_to_y(line1)
wenzelm@38640
   392
            val height = HEIGHT * (line2 - line1)
wenzelm@39178
   393
            color <- Isabelle_Markup.overview_color(snapshot, command)
wenzelm@39169
   394
          } {
wenzelm@39169
   395
            gfx.setColor(color)
wenzelm@38640
   396
            gfx.fillRect(0, y, getWidth - 1, height)
wenzelm@38640
   397
          }
wenzelm@37188
   398
        }
wenzelm@38640
   399
        finally { gfx.setColor(saved_color) }
wenzelm@34784
   400
      }
wenzelm@34784
   401
    }
wenzelm@34784
   402
wenzelm@34784
   403
    private def line_to_y(line: Int): Int =
wenzelm@34784
   404
      (line * getHeight) / (text_area.getBuffer.getLineCount max text_area.getVisibleLines)
wenzelm@34784
   405
wenzelm@34784
   406
    private def y_to_line(y: Int): Int =
wenzelm@34784
   407
      (y * (text_area.getBuffer.getLineCount max text_area.getVisibleLines)) / getHeight
wenzelm@34784
   408
  }
immler@34403
   409
wenzelm@34784
   410
wenzelm@34784
   411
  /* activation */
wenzelm@34784
   412
wenzelm@34808
   413
  private def activate()
wenzelm@34784
   414
  {
wenzelm@34784
   415
    text_area.getPainter.
wenzelm@34784
   416
      addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, text_area_extension)
wenzelm@39175
   417
    text_area.getGutter.addExtension(gutter_extension)
wenzelm@39131
   418
    text_area.addFocusListener(focus_listener)
wenzelm@39131
   419
    text_area.getPainter.addMouseMotionListener(mouse_motion_listener)
wenzelm@34810
   420
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   421
    text_area.addLeftOfScrollBar(overview)
wenzelm@37129
   422
    session.commands_changed += commands_changed_actor
wenzelm@34784
   423
  }
wenzelm@34784
   424
wenzelm@34808
   425
  private def deactivate()
wenzelm@34784
   426
  {
wenzelm@37129
   427
    session.commands_changed -= commands_changed_actor
wenzelm@39131
   428
    text_area.removeFocusListener(focus_listener)
wenzelm@39131
   429
    text_area.getPainter.removeMouseMotionListener(mouse_motion_listener)
wenzelm@39131
   430
    text_area.removeCaretListener(caret_listener)
wenzelm@34810
   431
    text_area.removeLeftOfScrollBar(overview)
wenzelm@39175
   432
    text_area.getGutter.removeExtension(gutter_extension)
wenzelm@34784
   433
    text_area.getPainter.removeExtension(text_area_extension)
wenzelm@34784
   434
  }
immler@34403
   435
}