src/Tools/jEdit/src/document_view.scala
author wenzelm
Tue Jan 08 13:24:12 2013 +0100 (2013-01-08)
changeset 50770 82d48783fd7a
parent 50363 2f8dc9e65401
child 50915 12de8ea66f54
permissions -rw-r--r--
more direct invalidateScreenLineRange after changed assignment;
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@45460
    14
import scala.collection.immutable.SortedMap
wenzelm@34784
    15
import scala.actors.Actor._
wenzelm@34784
    16
wenzelm@43520
    17
import java.lang.System
wenzelm@45744
    18
import java.text.BreakIterator
wenzelm@46572
    19
import java.awt.{Color, Graphics2D, Point}
wenzelm@34784
    20
import javax.swing.event.{CaretListener, CaretEvent}
wenzelm@34734
    21
wenzelm@49410
    22
import org.gjt.sp.jedit.{jEdit, Debug}
wenzelm@34709
    23
import org.gjt.sp.jedit.gui.RolloverButton
wenzelm@39176
    24
import org.gjt.sp.jedit.options.GutterOptionPane
wenzelm@47392
    25
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaExtension, TextAreaPainter}
wenzelm@43369
    26
import org.gjt.sp.jedit.syntax.{SyntaxStyle}
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 apply(text_area: JEditTextArea): Option[Document_View] =
wenzelm@34784
    36
  {
wenzelm@38223
    37
    Swing_Thread.require()
wenzelm@34784
    38
    text_area.getClientProperty(key) match {
wenzelm@34784
    39
      case doc_view: Document_View => Some(doc_view)
wenzelm@34784
    40
      case _ => None
wenzelm@34784
    41
    }
wenzelm@34784
    42
  }
wenzelm@34784
    43
wenzelm@39741
    44
  def exit(text_area: JEditTextArea)
wenzelm@34784
    45
  {
wenzelm@38223
    46
    Swing_Thread.require()
wenzelm@34788
    47
    apply(text_area) match {
wenzelm@39636
    48
      case None =>
wenzelm@34784
    49
      case Some(doc_view) =>
wenzelm@34784
    50
        doc_view.deactivate()
wenzelm@34784
    51
        text_area.putClientProperty(key, null)
wenzelm@34784
    52
    }
wenzelm@34784
    53
  }
wenzelm@43397
    54
wenzelm@43397
    55
  def init(model: Document_Model, text_area: JEditTextArea): Document_View =
wenzelm@43397
    56
  {
wenzelm@43397
    57
    exit(text_area)
wenzelm@43397
    58
    val doc_view = new Document_View(model, text_area)
wenzelm@43397
    59
    text_area.putClientProperty(key, doc_view)
wenzelm@43397
    60
    doc_view.activate()
wenzelm@43397
    61
    doc_view
wenzelm@43397
    62
  }
wenzelm@34784
    63
}
immler@34403
    64
wenzelm@34733
    65
wenzelm@43376
    66
class Document_View(val model: Document_Model, val text_area: JEditTextArea)
immler@34654
    67
{
wenzelm@34784
    68
  private val session = model.session
wenzelm@34784
    69
wenzelm@50205
    70
  def get_rendering(): Rendering = Rendering(model.snapshot(), PIDE.options.value)
wenzelm@49411
    71
wenzelm@50306
    72
  val rich_text_area =
wenzelm@50306
    73
    new Rich_Text_Area(text_area.getView, text_area, get_rendering _,
wenzelm@50306
    74
      caret_visible = true, hovering = false)
wenzelm@49411
    75
immler@34403
    76
wenzelm@44379
    77
  /* perspective */
wenzelm@44379
    78
wenzelm@44379
    79
  def perspective(): Text.Perspective =
wenzelm@44379
    80
  {
wenzelm@44379
    81
    Swing_Thread.require()
wenzelm@50363
    82
    val buffer_range = JEdit_Lib.buffer_range(model.buffer)
wenzelm@44473
    83
    Text.Perspective(
wenzelm@44379
    84
      for {
wenzelm@44379
    85
        i <- 0 until text_area.getVisibleLines
wenzelm@46997
    86
        start = text_area.getScreenLineStartOffset(i)
wenzelm@46997
    87
        stop = text_area.getScreenLineEndOffset(i)
wenzelm@44379
    88
        if start >= 0 && stop >= 0
wenzelm@46997
    89
        range <- buffer_range.try_restrict(Text.Range(start, stop))
wenzelm@44583
    90
        if !range.is_singularity
wenzelm@44379
    91
      }
wenzelm@44583
    92
      yield range)
wenzelm@44379
    93
  }
wenzelm@44379
    94
wenzelm@44437
    95
  private def update_perspective = new TextAreaExtension
wenzelm@44437
    96
  {
wenzelm@44437
    97
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@44437
    98
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@44437
    99
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@44437
   100
    {
wenzelm@49424
   101
      // no robust_body
wenzelm@44437
   102
      model.update_perspective()
wenzelm@44437
   103
    }
wenzelm@44437
   104
  }
wenzelm@44437
   105
wenzelm@44379
   106
wenzelm@49411
   107
  /* gutter */
immler@34513
   108
wenzelm@42825
   109
  private val gutter_painter = new TextAreaExtension
wenzelm@39175
   110
  {
wenzelm@39175
   111
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@39175
   112
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@39175
   113
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@39175
   114
    {
wenzelm@49411
   115
      rich_text_area.robust_body(()) {
wenzelm@46920
   116
        Swing_Thread.assert()
wenzelm@46920
   117
wenzelm@43404
   118
        val gutter = text_area.getGutter
wenzelm@43404
   119
        val width = GutterOptionPane.getSelectionAreaWidth
wenzelm@43404
   120
        val border_width = jEdit.getIntegerProperty("view.gutter.borderWidth", 3)
wenzelm@43404
   121
        val FOLD_MARKER_SIZE = 12
wenzelm@43419
   122
wenzelm@43404
   123
        if (gutter.isSelectionAreaEnabled && !gutter.isExpanded && width >= 12 && line_height >= 12) {
wenzelm@49408
   124
          val buffer = model.buffer
wenzelm@49408
   125
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@49410
   126
            val rendering = get_rendering()
wenzelm@49356
   127
wenzelm@43404
   128
            for (i <- 0 until physical_lines.length) {
wenzelm@43404
   129
              if (physical_lines(i) != -1) {
wenzelm@49843
   130
                val line_range = Text.Range(start(i), end(i))
wenzelm@43419
   131
wenzelm@43404
   132
                // gutter icons
wenzelm@49356
   133
                rendering.gutter_message(line_range) match {
wenzelm@46178
   134
                  case Some(icon) =>
wenzelm@46227
   135
                    val x0 = (FOLD_MARKER_SIZE + width - border_width - icon.getIconWidth) max 10
wenzelm@46227
   136
                    val y0 = y + i * line_height + (((line_height - icon.getIconHeight) / 2) max 0)
wenzelm@46227
   137
                    icon.paintIcon(gutter, gfx, x0, y0)
wenzelm@46178
   138
                  case None =>
wenzelm@43404
   139
                }
wenzelm@39175
   140
              }
wenzelm@39175
   141
            }
wenzelm@39175
   142
          }
wenzelm@39175
   143
        }
wenzelm@39175
   144
      }
wenzelm@39175
   145
    }
wenzelm@39175
   146
  }
wenzelm@39175
   147
wenzelm@39175
   148
wenzelm@37129
   149
  /* caret handling */
wenzelm@34810
   150
wenzelm@46740
   151
  private val delay_caret_update =
wenzelm@50207
   152
    Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) {
wenzelm@44805
   153
      session.caret_focus.event(Session.Caret_Focus)
wenzelm@34810
   154
    }
wenzelm@46740
   155
wenzelm@46740
   156
  private val caret_listener = new CaretListener {
wenzelm@49195
   157
    override def caretUpdate(e: CaretEvent) { delay_caret_update.invoke() }
wenzelm@34810
   158
  }
wenzelm@34810
   159
wenzelm@34810
   160
wenzelm@46572
   161
  /* text status overview left of scrollbar */
wenzelm@34784
   162
wenzelm@47993
   163
  private object overview extends Text_Overview(this)
wenzelm@46572
   164
  {
wenzelm@46571
   165
    val delay_repaint =
wenzelm@50207
   166
      Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { repaint() }
wenzelm@34784
   167
  }
immler@34403
   168
wenzelm@34784
   169
wenzelm@39740
   170
  /* main actor */
wenzelm@39740
   171
wenzelm@39740
   172
  private val main_actor = actor {
wenzelm@39740
   173
    loop {
wenzelm@39740
   174
      react {
wenzelm@49196
   175
        case _: Session.Raw_Edits =>
wenzelm@49196
   176
          Swing_Thread.later {
wenzelm@50207
   177
            overview.delay_repaint.postpone(PIDE.options.seconds("editor_input_delay"))
wenzelm@49196
   178
          }
wenzelm@49196
   179
wenzelm@44608
   180
        case changed: Session.Commands_Changed =>
wenzelm@39740
   181
          val buffer = model.buffer
wenzelm@46918
   182
          Swing_Thread.later {
wenzelm@49406
   183
            JEdit_Lib.buffer_lock(buffer) {
wenzelm@46918
   184
              if (model.buffer == text_area.getBuffer) {
wenzelm@47027
   185
                val snapshot = model.snapshot()
wenzelm@39740
   186
wenzelm@47027
   187
                if (changed.assignment ||
wenzelm@46918
   188
                    (changed.nodes.contains(model.name) &&
wenzelm@46918
   189
                     changed.commands.exists(snapshot.node.commands.contains)))
wenzelm@49196
   190
                  Swing_Thread.later { overview.delay_repaint.invoke() }
wenzelm@39740
   191
wenzelm@50770
   192
                val visible_lines = text_area.getVisibleLines
wenzelm@50770
   193
                if (visible_lines > 0) {
wenzelm@50770
   194
                  if (changed.assignment) text_area.invalidateScreenLineRange(0, visible_lines)
wenzelm@50770
   195
                  else {
wenzelm@50770
   196
                    val visible_range = JEdit_Lib.visible_range(text_area).get
wenzelm@50770
   197
                    val visible_cmds =
wenzelm@50770
   198
                      snapshot.node.command_range(snapshot.revert(visible_range)).map(_._1)
wenzelm@50770
   199
                    if (visible_cmds.exists(changed.commands)) {
wenzelm@50770
   200
                      for {
wenzelm@50770
   201
                        line <- 0 until visible_lines
wenzelm@50770
   202
                        start = text_area.getScreenLineStartOffset(line) if start >= 0
wenzelm@50770
   203
                        end = text_area.getScreenLineEndOffset(line) if end >= 0
wenzelm@50770
   204
                        range = Text.Range(start, end)
wenzelm@50770
   205
                        line_cmds = snapshot.node.command_range(snapshot.revert(range)).map(_._1)
wenzelm@50770
   206
                        if line_cmds.exists(changed.commands)
wenzelm@50770
   207
                      } text_area.invalidateScreenLineRange(line, line)
wenzelm@46918
   208
                    }
wenzelm@50770
   209
                  }
wenzelm@46583
   210
                }
wenzelm@46918
   211
              }
wenzelm@39740
   212
            }
wenzelm@39740
   213
          }
wenzelm@39740
   214
wenzelm@39740
   215
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@39740
   216
      }
wenzelm@39740
   217
    }
wenzelm@39740
   218
  }
wenzelm@39740
   219
wenzelm@39740
   220
wenzelm@34784
   221
  /* activation */
wenzelm@34784
   222
wenzelm@34808
   223
  private def activate()
wenzelm@34784
   224
  {
wenzelm@42825
   225
    val painter = text_area.getPainter
wenzelm@49410
   226
wenzelm@44437
   227
    painter.addExtension(TextAreaPainter.LOWEST_LAYER, update_perspective)
wenzelm@49411
   228
    rich_text_area.activate()
wenzelm@42825
   229
    text_area.getGutter.addExtension(gutter_painter)
wenzelm@34810
   230
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   231
    text_area.addLeftOfScrollBar(overview)
wenzelm@49196
   232
    session.raw_edits += main_actor
wenzelm@39740
   233
    session.commands_changed += main_actor
wenzelm@34784
   234
  }
wenzelm@34784
   235
wenzelm@34808
   236
  private def deactivate()
wenzelm@34784
   237
  {
wenzelm@42825
   238
    val painter = text_area.getPainter
wenzelm@49410
   239
wenzelm@49196
   240
    session.raw_edits -= main_actor
wenzelm@39740
   241
    session.commands_changed -= main_actor
wenzelm@49195
   242
    text_area.removeCaretListener(caret_listener); delay_caret_update.revoke()
wenzelm@49195
   243
    text_area.removeLeftOfScrollBar(overview); overview.delay_repaint.revoke()
wenzelm@42825
   244
    text_area.getGutter.removeExtension(gutter_painter)
wenzelm@49411
   245
    rich_text_area.deactivate()
wenzelm@44437
   246
    painter.removeExtension(update_perspective)
wenzelm@34784
   247
  }
wenzelm@42825
   248
}