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