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