src/Tools/jEdit/src/jedit/document_view.scala
author wenzelm
Tue Sep 28 23:02:05 2010 +0200 (2010-09-28)
changeset 39740 0294948ba962
parent 39737 8c312f064223
child 39741 62b91eb2d39a
permissions -rw-r--r--
basic support for message popups via HTML_Panel;
more systematic CONTROL-mouse management;
tuned;
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@39740
    15
import java.awt.{BorderLayout, Graphics, Color, Dimension, Graphics2D, Point}
wenzelm@39131
    16
import java.awt.event.{MouseAdapter, MouseMotionAdapter, MouseEvent, FocusAdapter, FocusEvent}
wenzelm@39740
    17
import javax.swing.{JPanel, ToolTipManager, Popup, PopupFactory, SwingUtilities}
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@39636
    55
      case None =>
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@39740
   112
  /* HTML popups */
wenzelm@39740
   113
wenzelm@39740
   114
  private var html_popup: Option[Popup] = None
wenzelm@34834
   115
wenzelm@39740
   116
  private def exit_popup() { html_popup.map(_.hide) }
wenzelm@38881
   117
wenzelm@39740
   118
  private val html_panel =
wenzelm@39740
   119
    new HTML_Panel(Isabelle.system, Isabelle.font_family(), scala.math.round(Isabelle.font_size())) {
wenzelm@39740
   120
    }
wenzelm@38884
   121
wenzelm@39740
   122
  private def html_panel_resize()
wenzelm@39740
   123
  {
wenzelm@39740
   124
    Swing_Thread.now {
wenzelm@39740
   125
      html_panel.resize(Isabelle.font_family(), scala.math.round(Isabelle.font_size()))
wenzelm@39740
   126
    }
wenzelm@39740
   127
  }
wenzelm@38884
   128
wenzelm@39740
   129
  private def init_popup(snapshot: Document.Snapshot, x: Int, y: Int)
wenzelm@39740
   130
  {
wenzelm@39740
   131
    exit_popup()
wenzelm@39740
   132
    val offset = text_area.xyToOffset(x, y)
wenzelm@39740
   133
    val p = new Point(x, y); SwingUtilities.convertPointToScreen(p, text_area.getPainter)
wenzelm@34784
   134
wenzelm@39740
   135
    // FIXME snapshot.cumulate
wenzelm@39740
   136
    snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.popup) match {
wenzelm@39740
   137
      case Text.Info(_, Some(msg)) #:: _ =>
wenzelm@39740
   138
        val popup = PopupFactory.getSharedInstance().getPopup(text_area, html_panel, p.x, p.y + 60)
wenzelm@39740
   139
        html_panel.render_sync(List(msg))
wenzelm@39740
   140
        popup.show
wenzelm@39740
   141
        html_popup = Some(popup)
wenzelm@39740
   142
      case _ =>
immler@34403
   143
    }
immler@34678
   144
  }
immler@34403
   145
immler@34403
   146
wenzelm@39131
   147
  /* subexpression highlighting */
wenzelm@39131
   148
wenzelm@39178
   149
  private def subexp_range(snapshot: Document.Snapshot, x: Int, y: Int)
wenzelm@39178
   150
    : Option[(Text.Range, Color)] =
wenzelm@39132
   151
  {
wenzelm@39132
   152
    val offset = text_area.xyToOffset(x, y)
wenzelm@39178
   153
    snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.subexp) match {
wenzelm@39178
   154
      case Text.Info(_, Some((range, color))) #:: _ => Some((snapshot.convert(range), color))
wenzelm@39177
   155
      case _ => None
wenzelm@39177
   156
    }
wenzelm@39132
   157
  }
wenzelm@39132
   158
wenzelm@39178
   159
  private var highlight_range: Option[(Text.Range, Color)] = None
wenzelm@39131
   160
wenzelm@39740
   161
wenzelm@39740
   162
  /* CONTROL-mouse management */
wenzelm@39740
   163
wenzelm@39740
   164
  private def exit_control()
wenzelm@39740
   165
  {
wenzelm@39740
   166
    exit_popup()
wenzelm@39740
   167
    highlight_range = None
wenzelm@39740
   168
  }
wenzelm@39740
   169
wenzelm@39131
   170
  private val focus_listener = new FocusAdapter {
wenzelm@39740
   171
    override def focusLost(e: FocusEvent) { exit_control() }
wenzelm@39131
   172
  }
wenzelm@39131
   173
wenzelm@39131
   174
  private val mouse_motion_listener = new MouseMotionAdapter {
wenzelm@39131
   175
    override def mouseMoved(e: MouseEvent) {
wenzelm@39131
   176
      val control = if (OperatingSystem.isMacOS()) e.isMetaDown else e.isControlDown
wenzelm@39740
   177
      val x = e.getX()
wenzelm@39740
   178
      val y = e.getY()
wenzelm@39740
   179
wenzelm@39740
   180
      if (!model.buffer.isLoaded) exit_control()
wenzelm@39132
   181
      else
wenzelm@39132
   182
        Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@39740
   183
          val snapshot = model.snapshot
wenzelm@39740
   184
wenzelm@39740
   185
          if (control) init_popup(snapshot, x, y)
wenzelm@39740
   186
wenzelm@39178
   187
          highlight_range map { case (range, _) => invalidate_line_range(range) }
wenzelm@39740
   188
          highlight_range = if (control) subexp_range(snapshot, x, y) else None
wenzelm@39178
   189
          highlight_range map { case (range, _) => invalidate_line_range(range) }
wenzelm@39132
   190
        }
wenzelm@39131
   191
    }
wenzelm@39131
   192
  }
wenzelm@39131
   193
wenzelm@39131
   194
wenzelm@34784
   195
  /* text_area_extension */
wenzelm@34784
   196
wenzelm@34784
   197
  private val text_area_extension = new TextAreaExtension
immler@34678
   198
  {
wenzelm@37685
   199
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@37685
   200
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@38886
   201
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@34784
   202
    {
wenzelm@38843
   203
      Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@38843
   204
        val snapshot = model.snapshot()
wenzelm@38843
   205
        val saved_color = gfx.getColor
wenzelm@39044
   206
        val ascent = text_area.getPainter.getFontMetrics.getAscent
wenzelm@39131
   207
wenzelm@39182
   208
        for (i <- 0 until physical_lines.length) {
wenzelm@39182
   209
          if (physical_lines(i) != -1) {
wenzelm@39182
   210
            val line_range = proper_line_range(start(i), end(i))
wenzelm@39044
   211
wenzelm@39182
   212
            // background color: status
wenzelm@39182
   213
            val cmds = snapshot.node.command_range(snapshot.revert(line_range))
wenzelm@39182
   214
            for {
wenzelm@39182
   215
              (command, command_start) <- cmds if !command.is_ignored
wenzelm@39182
   216
              val range = line_range.restrict(snapshot.convert(command.range + command_start))
wenzelm@39182
   217
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@39182
   218
              color <- Isabelle_Markup.status_color(snapshot, command)
wenzelm@39182
   219
            } {
wenzelm@39182
   220
              gfx.setColor(color)
wenzelm@39182
   221
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@39182
   222
            }
wenzelm@39044
   223
wenzelm@39700
   224
            // background color (1): markup
wenzelm@39182
   225
            for {
wenzelm@39182
   226
              Text.Info(range, Some(color)) <-
wenzelm@39700
   227
                snapshot.select_markup(line_range)(Isabelle_Markup.background1).iterator
wenzelm@39182
   228
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@39182
   229
            } {
wenzelm@39182
   230
              gfx.setColor(color)
wenzelm@39182
   231
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
wenzelm@39182
   232
            }
wenzelm@39171
   233
wenzelm@39700
   234
            // background color (2): markup
wenzelm@39700
   235
            for {
wenzelm@39700
   236
              Text.Info(range, Some(color)) <-
wenzelm@39700
   237
                snapshot.select_markup(line_range)(Isabelle_Markup.background2).iterator
wenzelm@39700
   238
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@39700
   239
            } {
wenzelm@39700
   240
              gfx.setColor(color)
wenzelm@39700
   241
              gfx.fillRect(r.x + 2, y + i * line_height + 2, r.length - 4, line_height - 4)
wenzelm@39700
   242
            }
wenzelm@39700
   243
wenzelm@39182
   244
            // sub-expression highlighting -- potentially from other snapshot
wenzelm@39182
   245
            highlight_range match {
wenzelm@39182
   246
              case Some((range, color)) if line_range.overlaps(range) =>
wenzelm@39182
   247
                Isabelle.gfx_range(text_area, line_range.restrict(range)) match {
wenzelm@39182
   248
                  case None =>
wenzelm@39182
   249
                  case Some(r) =>
wenzelm@39182
   250
                    gfx.setColor(color)
wenzelm@39700
   251
                    gfx.drawRect(r.x, y + i * line_height, r.length - 1, line_height - 1)
wenzelm@39182
   252
                }
wenzelm@39182
   253
              case _ =>
wenzelm@39182
   254
            }
wenzelm@39131
   255
wenzelm@39182
   256
            // squiggly underline
wenzelm@39182
   257
            for {
wenzelm@39182
   258
              Text.Info(range, Some(color)) <-
wenzelm@39182
   259
                snapshot.select_markup(line_range)(Isabelle_Markup.message).iterator
wenzelm@39182
   260
              r <- Isabelle.gfx_range(text_area, range)
wenzelm@39182
   261
            } {
wenzelm@39182
   262
              gfx.setColor(color)
wenzelm@39182
   263
              val x0 = (r.x / 2) * 2
wenzelm@39182
   264
              val y0 = r.y + ascent + 1
wenzelm@39182
   265
              for (x1 <- Range(x0, x0 + r.length, 2)) {
wenzelm@39182
   266
                val y1 = if (x1 % 4 < 2) y0 else y0 + 1
wenzelm@39182
   267
                gfx.drawLine(x1, y1, x1 + 1, y1)
wenzelm@38843
   268
              }
wenzelm@38843
   269
            }
wenzelm@38223
   270
          }
wenzelm@38223
   271
        }
wenzelm@38223
   272
      }
wenzelm@34784
   273
    }
wenzelm@34784
   274
wenzelm@34784
   275
    override def getToolTipText(x: Int, y: Int): String =
wenzelm@34784
   276
    {
wenzelm@38845
   277
      Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@38845
   278
        val snapshot = model.snapshot()
wenzelm@38845
   279
        val offset = text_area.xyToOffset(x, y)
wenzelm@39178
   280
        snapshot.select_markup(Text.Range(offset, offset + 1))(Isabelle_Markup.tooltip) match
wenzelm@39178
   281
        {
wenzelm@39177
   282
          case Text.Info(_, Some(text)) #:: _ => Isabelle.tooltip(text)
wenzelm@39177
   283
          case _ => null
wenzelm@39174
   284
        }
wenzelm@34784
   285
      }
wenzelm@34734
   286
    }
immler@34678
   287
  }
immler@34513
   288
wenzelm@34784
   289
wenzelm@39175
   290
  /* gutter_extension */
wenzelm@39175
   291
wenzelm@39175
   292
  private val gutter_extension = new TextAreaExtension
wenzelm@39175
   293
  {
wenzelm@39175
   294
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@39175
   295
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@39175
   296
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@39175
   297
    {
wenzelm@39176
   298
      val gutter = text_area.getGutter
wenzelm@39176
   299
      val width = GutterOptionPane.getSelectionAreaWidth
wenzelm@39176
   300
      val border_width = jEdit.getIntegerProperty("view.gutter.borderWidth", 3)
wenzelm@39176
   301
      val FOLD_MARKER_SIZE = 12
wenzelm@39176
   302
wenzelm@39176
   303
      if (gutter.isSelectionAreaEnabled && !gutter.isExpanded && width >= 12 && line_height >= 12) {
wenzelm@39176
   304
        Isabelle.swing_buffer_lock(model.buffer) {
wenzelm@39176
   305
          val snapshot = model.snapshot()
wenzelm@39175
   306
          for (i <- 0 until physical_lines.length) {
wenzelm@39175
   307
            if (physical_lines(i) != -1) {
wenzelm@39175
   308
              val line_range = proper_line_range(start(i), end(i))
wenzelm@39175
   309
wenzelm@39175
   310
              // gutter icons
wenzelm@39181
   311
              val icons =
wenzelm@39740
   312
                (for (Text.Info(_, Some(icon)) <- // FIXME snapshot.cumulate
wenzelm@39181
   313
                  snapshot.select_markup(line_range)(Isabelle_Markup.gutter_message).iterator)
wenzelm@39181
   314
                yield icon).toList.sortWith(_ >= _)
wenzelm@39181
   315
              icons match {
wenzelm@39181
   316
                case icon :: _ =>
wenzelm@39181
   317
                  val icn = icon.icon
wenzelm@39181
   318
                  val x0 = (FOLD_MARKER_SIZE + width - border_width - icn.getIconWidth) max 10
wenzelm@39181
   319
                  val y0 = y + i * line_height + (((line_height - icn.getIconHeight) / 2) max 0)
wenzelm@39181
   320
                  icn.paintIcon(gutter, gfx, x0, y0)
wenzelm@39181
   321
                case Nil =>
wenzelm@39175
   322
              }
wenzelm@39175
   323
            }
wenzelm@39175
   324
          }
wenzelm@39175
   325
        }
wenzelm@39175
   326
      }
wenzelm@39175
   327
    }
wenzelm@39175
   328
  }
wenzelm@39175
   329
wenzelm@39175
   330
wenzelm@37129
   331
  /* caret handling */
wenzelm@34810
   332
wenzelm@37849
   333
  def selected_command(): Option[Command] =
wenzelm@38223
   334
  {
wenzelm@38223
   335
    Swing_Thread.require()
wenzelm@38151
   336
    model.snapshot().node.proper_command_at(text_area.getCaretPosition)
wenzelm@38223
   337
  }
wenzelm@34810
   338
wenzelm@37849
   339
  private val caret_listener = new CaretListener {
wenzelm@37849
   340
    private val delay = Swing_Thread.delay_last(session.input_delay) {
wenzelm@37849
   341
      session.perspective.event(Session.Perspective)
wenzelm@34810
   342
    }
wenzelm@37849
   343
    override def caretUpdate(e: CaretEvent) { delay() }
wenzelm@34810
   344
  }
wenzelm@34810
   345
wenzelm@34810
   346
wenzelm@34784
   347
  /* overview of command status left of scrollbar */
wenzelm@34784
   348
wenzelm@34834
   349
  private val overview = new JPanel(new BorderLayout)
wenzelm@34784
   350
  {
wenzelm@34784
   351
    private val WIDTH = 10
wenzelm@34784
   352
    private val HEIGHT = 2
wenzelm@34784
   353
wenzelm@34806
   354
    setPreferredSize(new Dimension(WIDTH, 0))
wenzelm@34806
   355
wenzelm@34784
   356
    setRequestFocusEnabled(false)
wenzelm@34784
   357
wenzelm@34784
   358
    addMouseListener(new MouseAdapter {
wenzelm@34784
   359
      override def mousePressed(event: MouseEvent) {
wenzelm@34784
   360
        val line = y_to_line(event.getY)
wenzelm@34784
   361
        if (line >= 0 && line < text_area.getLineCount)
wenzelm@34784
   362
          text_area.setCaretPosition(text_area.getLineStartOffset(line))
wenzelm@34784
   363
      }
wenzelm@34784
   364
    })
wenzelm@34784
   365
wenzelm@34784
   366
    override def addNotify() {
wenzelm@34784
   367
      super.addNotify()
wenzelm@34784
   368
      ToolTipManager.sharedInstance.registerComponent(this)
wenzelm@34784
   369
    }
immler@34403
   370
wenzelm@34784
   371
    override def removeNotify() {
wenzelm@34784
   372
      ToolTipManager.sharedInstance.unregisterComponent(this)
wenzelm@34784
   373
      super.removeNotify
wenzelm@34784
   374
    }
wenzelm@34784
   375
wenzelm@34784
   376
    override def paintComponent(gfx: Graphics)
wenzelm@34784
   377
    {
wenzelm@34784
   378
      super.paintComponent(gfx)
wenzelm@38640
   379
      Swing_Thread.assert()
wenzelm@39737
   380
wenzelm@34784
   381
      val buffer = model.buffer
wenzelm@38843
   382
      Isabelle.buffer_lock(buffer) {
wenzelm@38640
   383
        val snapshot = model.snapshot()
wenzelm@39737
   384
wenzelm@39737
   385
        def line_range(command: Command, start: Text.Offset): Option[(Int, Int)] =
wenzelm@39737
   386
        {
wenzelm@39737
   387
          try {
wenzelm@39737
   388
            val line1 = buffer.getLineOfOffset(snapshot.convert(start))
wenzelm@39737
   389
            val line2 = buffer.getLineOfOffset(snapshot.convert(start + command.length)) + 1
wenzelm@39737
   390
            Some((line1, line2))
wenzelm@39737
   391
          }
wenzelm@39737
   392
          catch { case e: ArrayIndexOutOfBoundsException => None }
wenzelm@39737
   393
        }
wenzelm@39182
   394
        for {
wenzelm@39182
   395
          (command, start) <- snapshot.node.command_starts
wenzelm@39182
   396
          if !command.is_ignored
wenzelm@39737
   397
          (line1, line2) <- line_range(command, start)
wenzelm@39182
   398
          val y = line_to_y(line1)
wenzelm@39182
   399
          val height = HEIGHT * (line2 - line1)
wenzelm@39182
   400
          color <- Isabelle_Markup.overview_color(snapshot, command)
wenzelm@39182
   401
        } {
wenzelm@39182
   402
          gfx.setColor(color)
wenzelm@39182
   403
          gfx.fillRect(0, y, getWidth - 1, height)
wenzelm@37188
   404
        }
wenzelm@34784
   405
      }
wenzelm@34784
   406
    }
wenzelm@34784
   407
wenzelm@34784
   408
    private def line_to_y(line: Int): Int =
wenzelm@34784
   409
      (line * getHeight) / (text_area.getBuffer.getLineCount max text_area.getVisibleLines)
wenzelm@34784
   410
wenzelm@34784
   411
    private def y_to_line(y: Int): Int =
wenzelm@34784
   412
      (y * (text_area.getBuffer.getLineCount max text_area.getVisibleLines)) / getHeight
wenzelm@34784
   413
  }
immler@34403
   414
wenzelm@34784
   415
wenzelm@39740
   416
  /* main actor */
wenzelm@39740
   417
wenzelm@39740
   418
  private val main_actor = actor {
wenzelm@39740
   419
    loop {
wenzelm@39740
   420
      react {
wenzelm@39740
   421
        case Session.Commands_Changed(changed) =>
wenzelm@39740
   422
          val buffer = model.buffer
wenzelm@39740
   423
          Isabelle.swing_buffer_lock(buffer) {
wenzelm@39740
   424
            val snapshot = model.snapshot()
wenzelm@39740
   425
wenzelm@39740
   426
            if (changed.exists(snapshot.node.commands.contains))
wenzelm@39740
   427
              overview.repaint()
wenzelm@39740
   428
wenzelm@39740
   429
            val visible_range = screen_lines_range()
wenzelm@39740
   430
            val visible_cmds = snapshot.node.command_range(snapshot.revert(visible_range)).map(_._1)
wenzelm@39740
   431
            if (visible_cmds.exists(changed)) {
wenzelm@39740
   432
              for {
wenzelm@39740
   433
                line <- 0 until text_area.getVisibleLines
wenzelm@39740
   434
                val start = text_area.getScreenLineStartOffset(line) if start >= 0
wenzelm@39740
   435
                val end = text_area.getScreenLineEndOffset(line) if end >= 0
wenzelm@39740
   436
                val range = proper_line_range(start, end)
wenzelm@39740
   437
                val line_cmds = snapshot.node.command_range(snapshot.revert(range)).map(_._1)
wenzelm@39740
   438
                if line_cmds.exists(changed)
wenzelm@39740
   439
              } text_area.invalidateScreenLineRange(line, line)
wenzelm@39740
   440
wenzelm@39740
   441
              // FIXME danger of deadlock!?
wenzelm@39740
   442
              // FIXME potentially slow!?
wenzelm@39740
   443
              model.buffer.propertiesChanged()
wenzelm@39740
   444
            }
wenzelm@39740
   445
          }
wenzelm@39740
   446
wenzelm@39740
   447
        case Session.Global_Settings => html_panel_resize()
wenzelm@39740
   448
wenzelm@39740
   449
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@39740
   450
      }
wenzelm@39740
   451
    }
wenzelm@39740
   452
  }
wenzelm@39740
   453
wenzelm@39740
   454
wenzelm@34784
   455
  /* activation */
wenzelm@34784
   456
wenzelm@34808
   457
  private def activate()
wenzelm@34784
   458
  {
wenzelm@34784
   459
    text_area.getPainter.
wenzelm@34784
   460
      addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, text_area_extension)
wenzelm@39175
   461
    text_area.getGutter.addExtension(gutter_extension)
wenzelm@39131
   462
    text_area.addFocusListener(focus_listener)
wenzelm@39131
   463
    text_area.getPainter.addMouseMotionListener(mouse_motion_listener)
wenzelm@34810
   464
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   465
    text_area.addLeftOfScrollBar(overview)
wenzelm@39740
   466
    session.commands_changed += main_actor
wenzelm@39740
   467
    session.global_settings += main_actor
wenzelm@34784
   468
  }
wenzelm@34784
   469
wenzelm@34808
   470
  private def deactivate()
wenzelm@34784
   471
  {
wenzelm@39740
   472
    session.commands_changed -= main_actor
wenzelm@39740
   473
    session.global_settings -= main_actor
wenzelm@39131
   474
    text_area.removeFocusListener(focus_listener)
wenzelm@39131
   475
    text_area.getPainter.removeMouseMotionListener(mouse_motion_listener)
wenzelm@39131
   476
    text_area.removeCaretListener(caret_listener)
wenzelm@34810
   477
    text_area.removeLeftOfScrollBar(overview)
wenzelm@39175
   478
    text_area.getGutter.removeExtension(gutter_extension)
wenzelm@34784
   479
    text_area.getPainter.removeExtension(text_area_extension)
wenzelm@39740
   480
    exit_popup()
wenzelm@34784
   481
  }
immler@34403
   482
}