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