src/Tools/jEdit/src/document_view.scala
author wenzelm
Mon Aug 22 16:12:23 2011 +0200 (2011-08-22)
changeset 44379 1079ab6b342b
parent 44378 81b4af4cfa5a
child 44436 546adfa8a6fc
permissions -rw-r--r--
added official Text.Range.Ordering;
some support for text perspective;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/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@44379
    13
import scala.collection.mutable
wenzelm@34784
    14
import scala.actors.Actor._
wenzelm@34784
    15
wenzelm@43520
    16
import java.lang.System
wenzelm@39740
    17
import java.awt.{BorderLayout, Graphics, Color, Dimension, Graphics2D, Point}
wenzelm@39741
    18
import java.awt.event.{MouseAdapter, MouseMotionAdapter, MouseEvent,
wenzelm@39741
    19
  FocusAdapter, FocusEvent, WindowEvent, WindowAdapter}
wenzelm@39741
    20
import javax.swing.{JPanel, ToolTipManager, Popup, PopupFactory, SwingUtilities, BorderFactory}
wenzelm@34784
    21
import javax.swing.event.{CaretListener, CaretEvent}
wenzelm@34734
    22
wenzelm@43404
    23
import org.gjt.sp.util.Log
wenzelm@43404
    24
wenzelm@42839
    25
import org.gjt.sp.jedit.{jEdit, OperatingSystem, Debug}
wenzelm@34709
    26
import org.gjt.sp.jedit.gui.RolloverButton
wenzelm@39176
    27
import org.gjt.sp.jedit.options.GutterOptionPane
wenzelm@34784
    28
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaExtension, TextAreaPainter}
wenzelm@43369
    29
import org.gjt.sp.jedit.syntax.{SyntaxStyle}
wenzelm@34784
    30
wenzelm@34784
    31
wenzelm@34784
    32
object Document_View
wenzelm@34784
    33
{
wenzelm@34784
    34
  /* document view of text area */
wenzelm@34784
    35
wenzelm@34784
    36
  private val key = new Object
wenzelm@34784
    37
wenzelm@39741
    38
  def apply(text_area: JEditTextArea): Option[Document_View] =
wenzelm@34784
    39
  {
wenzelm@38223
    40
    Swing_Thread.require()
wenzelm@34784
    41
    text_area.getClientProperty(key) match {
wenzelm@34784
    42
      case doc_view: Document_View => Some(doc_view)
wenzelm@34784
    43
      case _ => None
wenzelm@34784
    44
    }
wenzelm@34784
    45
  }
wenzelm@34784
    46
wenzelm@39741
    47
  def exit(text_area: JEditTextArea)
wenzelm@34784
    48
  {
wenzelm@38223
    49
    Swing_Thread.require()
wenzelm@34788
    50
    apply(text_area) match {
wenzelm@39636
    51
      case None =>
wenzelm@34784
    52
      case Some(doc_view) =>
wenzelm@34784
    53
        doc_view.deactivate()
wenzelm@34784
    54
        text_area.putClientProperty(key, null)
wenzelm@34784
    55
    }
wenzelm@34784
    56
  }
wenzelm@43397
    57
wenzelm@43397
    58
  def init(model: Document_Model, text_area: JEditTextArea): Document_View =
wenzelm@43397
    59
  {
wenzelm@43397
    60
    exit(text_area)
wenzelm@43397
    61
    val doc_view = new Document_View(model, text_area)
wenzelm@43397
    62
    text_area.putClientProperty(key, doc_view)
wenzelm@43397
    63
    doc_view.activate()
wenzelm@43397
    64
    doc_view
wenzelm@43397
    65
  }
wenzelm@34784
    66
}
immler@34403
    67
wenzelm@34733
    68
wenzelm@43376
    69
class Document_View(val model: Document_Model, val text_area: JEditTextArea)
immler@34654
    70
{
wenzelm@34784
    71
  private val session = model.session
wenzelm@34784
    72
immler@34403
    73
wenzelm@43419
    74
  /* robust extension body */
wenzelm@43404
    75
wenzelm@43404
    76
  def robust_body[A](default: A)(body: => A): A =
wenzelm@43417
    77
  {
wenzelm@43417
    78
    try {
wenzelm@43417
    79
      Swing_Thread.require()
wenzelm@43417
    80
      if (model.buffer == text_area.getBuffer) body
wenzelm@43417
    81
      else {
wenzelm@43650
    82
        Log.log(Log.ERROR, this, ERROR("Inconsistent document model"))
wenzelm@43417
    83
        default
wenzelm@43404
    84
      }
wenzelm@43404
    85
    }
wenzelm@43417
    86
    catch { case t: Throwable => Log.log(Log.ERROR, this, t); default }
wenzelm@43417
    87
  }
wenzelm@43404
    88
wenzelm@43404
    89
wenzelm@44378
    90
  /* visible text range */
wenzelm@38881
    91
wenzelm@38883
    92
  // simplify slightly odd result of TextArea.getScreenLineEndOffset etc.
wenzelm@38881
    93
  // NB: jEdit already normalizes \r\n and \r to \n
wenzelm@38881
    94
  def proper_line_range(start: Text.Offset, end: Text.Offset): Text.Range =
wenzelm@38881
    95
  {
wenzelm@38883
    96
    val stop = if (start < end) end - 1 else end min model.buffer.getLength
wenzelm@38881
    97
    Text.Range(start, stop)
wenzelm@38881
    98
  }
wenzelm@38881
    99
wenzelm@44378
   100
  def visible_range(): Text.Range =
wenzelm@38881
   101
  {
wenzelm@38881
   102
    val start = text_area.getScreenLineStartOffset(0)
wenzelm@38881
   103
    val raw_end = text_area.getScreenLineEndOffset(text_area.getVisibleLines - 1 max 0)
wenzelm@38881
   104
    proper_line_range(start, if (raw_end >= 0) raw_end else model.buffer.getLength)
wenzelm@38881
   105
  }
wenzelm@38881
   106
wenzelm@44378
   107
  def invalidate_range(range: Text.Range)
wenzelm@39132
   108
  {
wenzelm@39132
   109
    text_area.invalidateLineRange(
wenzelm@39132
   110
      model.buffer.getLineOfOffset(range.start),
wenzelm@39132
   111
      model.buffer.getLineOfOffset(range.stop))
wenzelm@39132
   112
  }
wenzelm@39132
   113
wenzelm@38881
   114
wenzelm@44379
   115
  /* perspective */
wenzelm@44379
   116
wenzelm@44379
   117
  def perspective(): Text.Perspective =
wenzelm@44379
   118
  {
wenzelm@44379
   119
    Swing_Thread.require()
wenzelm@44379
   120
    Text.perspective(
wenzelm@44379
   121
      for {
wenzelm@44379
   122
        i <- 0 until text_area.getVisibleLines
wenzelm@44379
   123
        val start = text_area.getScreenLineStartOffset(i)
wenzelm@44379
   124
        val stop = text_area.getScreenLineEndOffset(i)
wenzelm@44379
   125
        if start >= 0 && stop >= 0
wenzelm@44379
   126
      }
wenzelm@44379
   127
      yield Text.Range(start, stop))
wenzelm@44379
   128
  }
wenzelm@44379
   129
wenzelm@44379
   130
wenzelm@43419
   131
  /* snapshot */
wenzelm@43419
   132
wenzelm@43419
   133
  // owned by Swing thread
wenzelm@43419
   134
  @volatile private var was_outdated = false
wenzelm@43419
   135
  @volatile private var was_updated = false
wenzelm@43419
   136
wenzelm@43419
   137
  def update_snapshot(): Document.Snapshot =
wenzelm@43419
   138
  {
wenzelm@43419
   139
    Swing_Thread.require()
wenzelm@43419
   140
    val snapshot = model.snapshot()
wenzelm@43419
   141
    was_updated = was_outdated && !snapshot.is_outdated
wenzelm@43419
   142
    was_outdated = was_outdated || snapshot.is_outdated
wenzelm@43419
   143
    snapshot
wenzelm@43419
   144
  }
wenzelm@43419
   145
wenzelm@43419
   146
  def flush_snapshot(): (Boolean, Document.Snapshot) =
wenzelm@43419
   147
  {
wenzelm@43419
   148
    Swing_Thread.require()
wenzelm@43419
   149
    val snapshot = update_snapshot()
wenzelm@43419
   150
    val updated = was_updated
wenzelm@43419
   151
    if (updated) { was_outdated = false; was_updated = false }
wenzelm@43419
   152
    (updated, snapshot)
wenzelm@43419
   153
  }
wenzelm@43419
   154
wenzelm@43419
   155
wenzelm@39740
   156
  /* HTML popups */
wenzelm@39740
   157
wenzelm@39740
   158
  private var html_popup: Option[Popup] = None
wenzelm@34834
   159
wenzelm@39740
   160
  private def exit_popup() { html_popup.map(_.hide) }
wenzelm@38881
   161
wenzelm@39740
   162
  private val html_panel =
wenzelm@43661
   163
    new HTML_Panel(Isabelle.font_family(), scala.math.round(Isabelle.font_size()))
wenzelm@39741
   164
  html_panel.setBorder(BorderFactory.createLineBorder(Color.black))
wenzelm@38884
   165
wenzelm@39740
   166
  private def html_panel_resize()
wenzelm@39740
   167
  {
wenzelm@39740
   168
    Swing_Thread.now {
wenzelm@39740
   169
      html_panel.resize(Isabelle.font_family(), scala.math.round(Isabelle.font_size()))
wenzelm@39740
   170
    }
wenzelm@39740
   171
  }
wenzelm@38884
   172
wenzelm@39740
   173
  private def init_popup(snapshot: Document.Snapshot, x: Int, y: Int)
wenzelm@39740
   174
  {
wenzelm@39740
   175
    exit_popup()
wenzelm@40154
   176
/* FIXME broken
wenzelm@39740
   177
    val offset = text_area.xyToOffset(x, y)
wenzelm@39740
   178
    val p = new Point(x, y); SwingUtilities.convertPointToScreen(p, text_area.getPainter)
wenzelm@34784
   179
wenzelm@39740
   180
    // FIXME snapshot.cumulate
wenzelm@39740
   181
    snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.popup) match {
wenzelm@39740
   182
      case Text.Info(_, Some(msg)) #:: _ =>
wenzelm@39740
   183
        val popup = PopupFactory.getSharedInstance().getPopup(text_area, html_panel, p.x, p.y + 60)
wenzelm@39740
   184
        html_panel.render_sync(List(msg))
wenzelm@39742
   185
        Thread.sleep(10)  // FIXME !?
wenzelm@39740
   186
        popup.show
wenzelm@39740
   187
        html_popup = Some(popup)
wenzelm@39740
   188
      case _ =>
immler@34403
   189
    }
wenzelm@40154
   190
*/
immler@34678
   191
  }
immler@34403
   192
immler@34403
   193
wenzelm@39131
   194
  /* subexpression highlighting */
wenzelm@39131
   195
wenzelm@39178
   196
  private def subexp_range(snapshot: Document.Snapshot, x: Int, y: Int)
wenzelm@39178
   197
    : Option[(Text.Range, Color)] =
wenzelm@39132
   198
  {
wenzelm@39132
   199
    val offset = text_area.xyToOffset(x, y)
wenzelm@39178
   200
    snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.subexp) match {
wenzelm@39178
   201
      case Text.Info(_, Some((range, color))) #:: _ => Some((snapshot.convert(range), color))
wenzelm@39177
   202
      case _ => None
wenzelm@39177
   203
    }
wenzelm@39132
   204
  }
wenzelm@39132
   205
wenzelm@43381
   206
  @volatile private var _highlight_range: Option[(Text.Range, Color)] = None
wenzelm@43381
   207
  def highlight_range(): Option[(Text.Range, Color)] = _highlight_range
wenzelm@39131
   208
wenzelm@39740
   209
wenzelm@39740
   210
  /* CONTROL-mouse management */
wenzelm@39740
   211
wenzelm@40338
   212
  private var control: Boolean = false
wenzelm@40338
   213
wenzelm@39740
   214
  private def exit_control()
wenzelm@39740
   215
  {
wenzelm@39740
   216
    exit_popup()
wenzelm@43381
   217
    _highlight_range = None
wenzelm@39740
   218
  }
wenzelm@39740
   219
wenzelm@39131
   220
  private val focus_listener = new FocusAdapter {
wenzelm@39742
   221
    override def focusLost(e: FocusEvent) {
wenzelm@43381
   222
      _highlight_range = None // FIXME exit_control !?
wenzelm@39742
   223
    }
wenzelm@39131
   224
  }
wenzelm@39131
   225
wenzelm@39741
   226
  private val window_listener = new WindowAdapter {
wenzelm@39741
   227
    override def windowIconified(e: WindowEvent) { exit_control() }
wenzelm@39742
   228
    override def windowDeactivated(e: WindowEvent) { exit_control() }
wenzelm@39741
   229
  }
wenzelm@39741
   230
wenzelm@39131
   231
  private val mouse_motion_listener = new MouseMotionAdapter {
wenzelm@39131
   232
    override def mouseMoved(e: MouseEvent) {
wenzelm@40338
   233
      control = if (OperatingSystem.isMacOS()) e.isMetaDown else e.isControlDown
wenzelm@39740
   234
      val x = e.getX()
wenzelm@39740
   235
      val y = e.getY()
wenzelm@39740
   236
wenzelm@39740
   237
      if (!model.buffer.isLoaded) exit_control()
wenzelm@39132
   238
      else
wenzelm@39132
   239
        Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@43419
   240
          val snapshot = update_snapshot()
wenzelm@39740
   241
wenzelm@39740
   242
          if (control) init_popup(snapshot, x, y)
wenzelm@39740
   243
wenzelm@44378
   244
          _highlight_range map { case (range, _) => invalidate_range(range) }
wenzelm@43381
   245
          _highlight_range = if (control) subexp_range(snapshot, x, y) else None
wenzelm@44378
   246
          _highlight_range map { case (range, _) => invalidate_range(range) }
wenzelm@39132
   247
        }
wenzelm@39131
   248
    }
wenzelm@39131
   249
  }
wenzelm@39131
   250
wenzelm@39131
   251
wenzelm@43381
   252
  /* text area painting */
wenzelm@39044
   253
wenzelm@43381
   254
  private val text_area_painter = new Text_Area_Painter(this)
wenzelm@39700
   255
wenzelm@43381
   256
  private val tooltip_painter = new TextAreaExtension
wenzelm@43381
   257
  {
wenzelm@34784
   258
    override def getToolTipText(x: Int, y: Int): String =
wenzelm@34784
   259
    {
wenzelm@43404
   260
      robust_body(null: String) {
wenzelm@43419
   261
        val snapshot = update_snapshot()
wenzelm@38845
   262
        val offset = text_area.xyToOffset(x, y)
wenzelm@40338
   263
        if (control) {
wenzelm@40338
   264
          snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.tooltip) match
wenzelm@40338
   265
          {
wenzelm@40338
   266
            case Text.Info(_, Some(text)) #:: _ => Isabelle.tooltip(text)
wenzelm@40338
   267
            case _ => null
wenzelm@40338
   268
          }
wenzelm@40338
   269
        }
wenzelm@40338
   270
        else {
wenzelm@40338
   271
          // FIXME snapshot.cumulate
wenzelm@40338
   272
          snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.popup) match
wenzelm@40338
   273
          {
wenzelm@40338
   274
            case Text.Info(_, Some(text)) #:: _ => Isabelle.tooltip(text)
wenzelm@40338
   275
            case _ => null
wenzelm@40338
   276
          }
wenzelm@39174
   277
        }
wenzelm@34784
   278
      }
wenzelm@34734
   279
    }
immler@34678
   280
  }
immler@34513
   281
wenzelm@42825
   282
  private val gutter_painter = new TextAreaExtension
wenzelm@39175
   283
  {
wenzelm@39175
   284
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@39175
   285
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@39175
   286
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@39175
   287
    {
wenzelm@43404
   288
      robust_body(()) {
wenzelm@43404
   289
        val gutter = text_area.getGutter
wenzelm@43404
   290
        val width = GutterOptionPane.getSelectionAreaWidth
wenzelm@43404
   291
        val border_width = jEdit.getIntegerProperty("view.gutter.borderWidth", 3)
wenzelm@43404
   292
        val FOLD_MARKER_SIZE = 12
wenzelm@43419
   293
wenzelm@43404
   294
        if (gutter.isSelectionAreaEnabled && !gutter.isExpanded && width >= 12 && line_height >= 12) {
wenzelm@43404
   295
          Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@43419
   296
            val snapshot = update_snapshot()
wenzelm@43404
   297
            for (i <- 0 until physical_lines.length) {
wenzelm@43404
   298
              if (physical_lines(i) != -1) {
wenzelm@43404
   299
                val line_range = proper_line_range(start(i), end(i))
wenzelm@43419
   300
wenzelm@43404
   301
                // gutter icons
wenzelm@43404
   302
                val icons =
wenzelm@43404
   303
                  (for (Text.Info(_, Some(icon)) <- // FIXME snapshot.cumulate
wenzelm@43404
   304
                    snapshot.select_markup(line_range)(Isabelle_Markup.gutter_message).iterator)
wenzelm@43404
   305
                  yield icon).toList.sortWith(_ >= _)
wenzelm@43404
   306
                icons match {
wenzelm@43404
   307
                  case icon :: _ =>
wenzelm@43404
   308
                    val icn = icon.icon
wenzelm@43404
   309
                    val x0 = (FOLD_MARKER_SIZE + width - border_width - icn.getIconWidth) max 10
wenzelm@43404
   310
                    val y0 = y + i * line_height + (((line_height - icn.getIconHeight) / 2) max 0)
wenzelm@43404
   311
                    icn.paintIcon(gutter, gfx, x0, y0)
wenzelm@43404
   312
                  case Nil =>
wenzelm@43404
   313
                }
wenzelm@39175
   314
              }
wenzelm@39175
   315
            }
wenzelm@39175
   316
          }
wenzelm@39175
   317
        }
wenzelm@39175
   318
      }
wenzelm@39175
   319
    }
wenzelm@39175
   320
  }
wenzelm@39175
   321
wenzelm@39175
   322
wenzelm@43448
   323
  /* caret range */
wenzelm@43448
   324
wenzelm@43448
   325
  def caret_range(): Text.Range =
wenzelm@43448
   326
    Isabelle.buffer_lock(model.buffer) {
wenzelm@43448
   327
      def text(i: Text.Offset): Char = model.buffer.getText(i, 1).charAt(0)
wenzelm@43448
   328
      val caret = text_area.getCaretPosition
wenzelm@43448
   329
      try {
wenzelm@43448
   330
        val c = text(caret)
wenzelm@43448
   331
        if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(caret + 1)))
wenzelm@43448
   332
          Text.Range(caret, caret + 2)
wenzelm@43448
   333
        else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(caret - 1)))
wenzelm@43448
   334
          Text.Range(caret - 1, caret + 1)
wenzelm@43448
   335
        else Text.Range(caret, caret + 1)
wenzelm@43448
   336
      }
wenzelm@43448
   337
      catch { case _: ArrayIndexOutOfBoundsException => Text.Range(caret, caret + 1) }
wenzelm@43448
   338
    }
wenzelm@43448
   339
wenzelm@43448
   340
wenzelm@37129
   341
  /* caret handling */
wenzelm@34810
   342
wenzelm@37849
   343
  def selected_command(): Option[Command] =
wenzelm@38223
   344
  {
wenzelm@38223
   345
    Swing_Thread.require()
wenzelm@43419
   346
    update_snapshot().node.proper_command_at(text_area.getCaretPosition)
wenzelm@38223
   347
  }
wenzelm@34810
   348
wenzelm@37849
   349
  private val caret_listener = new CaretListener {
wenzelm@37849
   350
    private val delay = Swing_Thread.delay_last(session.input_delay) {
wenzelm@37849
   351
      session.perspective.event(Session.Perspective)
wenzelm@34810
   352
    }
wenzelm@37849
   353
    override def caretUpdate(e: CaretEvent) { delay() }
wenzelm@34810
   354
  }
wenzelm@34810
   355
wenzelm@34810
   356
wenzelm@34784
   357
  /* overview of command status left of scrollbar */
wenzelm@34784
   358
wenzelm@34834
   359
  private val overview = new JPanel(new BorderLayout)
wenzelm@34784
   360
  {
wenzelm@34784
   361
    private val WIDTH = 10
wenzelm@34784
   362
    private val HEIGHT = 2
wenzelm@34784
   363
wenzelm@34806
   364
    setPreferredSize(new Dimension(WIDTH, 0))
wenzelm@34806
   365
wenzelm@34784
   366
    setRequestFocusEnabled(false)
wenzelm@34784
   367
wenzelm@34784
   368
    addMouseListener(new MouseAdapter {
wenzelm@34784
   369
      override def mousePressed(event: MouseEvent) {
wenzelm@34784
   370
        val line = y_to_line(event.getY)
wenzelm@34784
   371
        if (line >= 0 && line < text_area.getLineCount)
wenzelm@34784
   372
          text_area.setCaretPosition(text_area.getLineStartOffset(line))
wenzelm@34784
   373
      }
wenzelm@34784
   374
    })
wenzelm@34784
   375
wenzelm@34784
   376
    override def addNotify() {
wenzelm@34784
   377
      super.addNotify()
wenzelm@34784
   378
      ToolTipManager.sharedInstance.registerComponent(this)
wenzelm@34784
   379
    }
immler@34403
   380
wenzelm@34784
   381
    override def removeNotify() {
wenzelm@34784
   382
      ToolTipManager.sharedInstance.unregisterComponent(this)
wenzelm@34784
   383
      super.removeNotify
wenzelm@34784
   384
    }
wenzelm@34784
   385
wenzelm@34784
   386
    override def paintComponent(gfx: Graphics)
wenzelm@34784
   387
    {
wenzelm@34784
   388
      super.paintComponent(gfx)
wenzelm@38640
   389
      Swing_Thread.assert()
wenzelm@39737
   390
wenzelm@34784
   391
      val buffer = model.buffer
wenzelm@38843
   392
      Isabelle.buffer_lock(buffer) {
wenzelm@43419
   393
        val snapshot = update_snapshot()
wenzelm@39737
   394
wenzelm@39737
   395
        def line_range(command: Command, start: Text.Offset): Option[(Int, Int)] =
wenzelm@39737
   396
        {
wenzelm@39737
   397
          try {
wenzelm@39737
   398
            val line1 = buffer.getLineOfOffset(snapshot.convert(start))
wenzelm@39737
   399
            val line2 = buffer.getLineOfOffset(snapshot.convert(start + command.length)) + 1
wenzelm@39737
   400
            Some((line1, line2))
wenzelm@39737
   401
          }
wenzelm@39737
   402
          catch { case e: ArrayIndexOutOfBoundsException => None }
wenzelm@39737
   403
        }
wenzelm@39182
   404
        for {
wenzelm@39182
   405
          (command, start) <- snapshot.node.command_starts
wenzelm@39182
   406
          if !command.is_ignored
wenzelm@39737
   407
          (line1, line2) <- line_range(command, start)
wenzelm@39182
   408
          val y = line_to_y(line1)
wenzelm@39182
   409
          val height = HEIGHT * (line2 - line1)
wenzelm@39182
   410
          color <- Isabelle_Markup.overview_color(snapshot, command)
wenzelm@39182
   411
        } {
wenzelm@39182
   412
          gfx.setColor(color)
wenzelm@39182
   413
          gfx.fillRect(0, y, getWidth - 1, height)
wenzelm@37188
   414
        }
wenzelm@34784
   415
      }
wenzelm@34784
   416
    }
wenzelm@34784
   417
wenzelm@34784
   418
    private def line_to_y(line: Int): Int =
wenzelm@34784
   419
      (line * getHeight) / (text_area.getBuffer.getLineCount max text_area.getVisibleLines)
wenzelm@34784
   420
wenzelm@34784
   421
    private def y_to_line(y: Int): Int =
wenzelm@34784
   422
      (y * (text_area.getBuffer.getLineCount max text_area.getVisibleLines)) / getHeight
wenzelm@34784
   423
  }
immler@34403
   424
wenzelm@34784
   425
wenzelm@39740
   426
  /* main actor */
wenzelm@39740
   427
wenzelm@39740
   428
  private val main_actor = actor {
wenzelm@39740
   429
    loop {
wenzelm@39740
   430
      react {
wenzelm@39740
   431
        case Session.Commands_Changed(changed) =>
wenzelm@39740
   432
          val buffer = model.buffer
wenzelm@39740
   433
          Isabelle.swing_buffer_lock(buffer) {
wenzelm@43419
   434
            val (updated, snapshot) = flush_snapshot()
wenzelm@44378
   435
            val visible = visible_range()
wenzelm@39740
   436
wenzelm@43419
   437
            if (updated || changed.exists(snapshot.node.commands.contains))
wenzelm@39740
   438
              overview.repaint()
wenzelm@39740
   439
wenzelm@44378
   440
            if (updated) invalidate_range(visible)
wenzelm@43419
   441
            else {
wenzelm@43419
   442
              val visible_cmds =
wenzelm@44378
   443
                snapshot.node.command_range(snapshot.revert(visible)).map(_._1)
wenzelm@43419
   444
              if (visible_cmds.exists(changed)) {
wenzelm@43419
   445
                for {
wenzelm@43419
   446
                  line <- 0 until text_area.getVisibleLines
wenzelm@43419
   447
                  val start = text_area.getScreenLineStartOffset(line) if start >= 0
wenzelm@43419
   448
                  val end = text_area.getScreenLineEndOffset(line) if end >= 0
wenzelm@43419
   449
                  val range = proper_line_range(start, end)
wenzelm@43419
   450
                  val line_cmds = snapshot.node.command_range(snapshot.revert(range)).map(_._1)
wenzelm@43419
   451
                  if line_cmds.exists(changed)
wenzelm@43419
   452
                } text_area.invalidateScreenLineRange(line, line)
wenzelm@43419
   453
              }
wenzelm@39740
   454
            }
wenzelm@39740
   455
          }
wenzelm@39740
   456
wenzelm@39740
   457
        case Session.Global_Settings => html_panel_resize()
wenzelm@39740
   458
wenzelm@39740
   459
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@39740
   460
      }
wenzelm@39740
   461
    }
wenzelm@39740
   462
  }
wenzelm@39740
   463
wenzelm@39740
   464
wenzelm@34784
   465
  /* activation */
wenzelm@34784
   466
wenzelm@34808
   467
  private def activate()
wenzelm@34784
   468
  {
wenzelm@42825
   469
    val painter = text_area.getPainter
wenzelm@43387
   470
    painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, tooltip_painter)
wenzelm@43381
   471
    text_area_painter.activate()
wenzelm@42825
   472
    text_area.getGutter.addExtension(gutter_painter)
wenzelm@39131
   473
    text_area.addFocusListener(focus_listener)
wenzelm@39741
   474
    text_area.getView.addWindowListener(window_listener)
wenzelm@42825
   475
    painter.addMouseMotionListener(mouse_motion_listener)
wenzelm@34810
   476
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   477
    text_area.addLeftOfScrollBar(overview)
wenzelm@39740
   478
    session.commands_changed += main_actor
wenzelm@39740
   479
    session.global_settings += main_actor
wenzelm@34784
   480
  }
wenzelm@34784
   481
wenzelm@34808
   482
  private def deactivate()
wenzelm@34784
   483
  {
wenzelm@42825
   484
    val painter = text_area.getPainter
wenzelm@39740
   485
    session.commands_changed -= main_actor
wenzelm@39740
   486
    session.global_settings -= main_actor
wenzelm@39131
   487
    text_area.removeFocusListener(focus_listener)
wenzelm@39741
   488
    text_area.getView.removeWindowListener(window_listener)
wenzelm@42825
   489
    painter.removeMouseMotionListener(mouse_motion_listener)
wenzelm@39131
   490
    text_area.removeCaretListener(caret_listener)
wenzelm@34810
   491
    text_area.removeLeftOfScrollBar(overview)
wenzelm@42825
   492
    text_area.getGutter.removeExtension(gutter_painter)
wenzelm@43381
   493
    text_area_painter.deactivate()
wenzelm@43381
   494
    painter.removeExtension(tooltip_painter)
wenzelm@39740
   495
    exit_popup()
wenzelm@34784
   496
  }
wenzelm@42825
   497
}