src/Tools/jEdit/src/document_view.scala
author wenzelm
Sun Nov 25 21:23:20 2012 +0100 (2012-11-25)
changeset 50207 54be125d8cdc
parent 50205 788c8263e634
child 50306 b655d2d0406d
permissions -rw-r--r--
tuned signature;
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@49492
    72
  val rich_text_area = new Rich_Text_Area(text_area.getView, text_area, get_rendering _, false)
wenzelm@49411
    73
immler@34403
    74
wenzelm@44379
    75
  /* perspective */
wenzelm@44379
    76
wenzelm@44379
    77
  def perspective(): Text.Perspective =
wenzelm@44379
    78
  {
wenzelm@44379
    79
    Swing_Thread.require()
wenzelm@44776
    80
    val buffer_range = model.buffer_range()
wenzelm@44473
    81
    Text.Perspective(
wenzelm@44379
    82
      for {
wenzelm@44379
    83
        i <- 0 until text_area.getVisibleLines
wenzelm@46997
    84
        start = text_area.getScreenLineStartOffset(i)
wenzelm@46997
    85
        stop = text_area.getScreenLineEndOffset(i)
wenzelm@44379
    86
        if start >= 0 && stop >= 0
wenzelm@46997
    87
        range <- buffer_range.try_restrict(Text.Range(start, stop))
wenzelm@44583
    88
        if !range.is_singularity
wenzelm@44379
    89
      }
wenzelm@44583
    90
      yield range)
wenzelm@44379
    91
  }
wenzelm@44379
    92
wenzelm@44437
    93
  private def update_perspective = new TextAreaExtension
wenzelm@44437
    94
  {
wenzelm@44437
    95
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@44437
    96
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@44437
    97
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@44437
    98
    {
wenzelm@49424
    99
      // no robust_body
wenzelm@44437
   100
      model.update_perspective()
wenzelm@44437
   101
    }
wenzelm@44437
   102
  }
wenzelm@44437
   103
wenzelm@44379
   104
wenzelm@49411
   105
  /* gutter */
immler@34513
   106
wenzelm@42825
   107
  private val gutter_painter = new TextAreaExtension
wenzelm@39175
   108
  {
wenzelm@39175
   109
    override def paintScreenLineRange(gfx: Graphics2D,
wenzelm@39175
   110
      first_line: Int, last_line: Int, physical_lines: Array[Int],
wenzelm@39175
   111
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
wenzelm@39175
   112
    {
wenzelm@49411
   113
      rich_text_area.robust_body(()) {
wenzelm@46920
   114
        Swing_Thread.assert()
wenzelm@46920
   115
wenzelm@43404
   116
        val gutter = text_area.getGutter
wenzelm@43404
   117
        val width = GutterOptionPane.getSelectionAreaWidth
wenzelm@43404
   118
        val border_width = jEdit.getIntegerProperty("view.gutter.borderWidth", 3)
wenzelm@43404
   119
        val FOLD_MARKER_SIZE = 12
wenzelm@43419
   120
wenzelm@43404
   121
        if (gutter.isSelectionAreaEnabled && !gutter.isExpanded && width >= 12 && line_height >= 12) {
wenzelm@49408
   122
          val buffer = model.buffer
wenzelm@49408
   123
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@49410
   124
            val rendering = get_rendering()
wenzelm@49356
   125
wenzelm@43404
   126
            for (i <- 0 until physical_lines.length) {
wenzelm@43404
   127
              if (physical_lines(i) != -1) {
wenzelm@49843
   128
                val line_range = Text.Range(start(i), end(i))
wenzelm@43419
   129
wenzelm@43404
   130
                // gutter icons
wenzelm@49356
   131
                rendering.gutter_message(line_range) match {
wenzelm@46178
   132
                  case Some(icon) =>
wenzelm@46227
   133
                    val x0 = (FOLD_MARKER_SIZE + width - border_width - icon.getIconWidth) max 10
wenzelm@46227
   134
                    val y0 = y + i * line_height + (((line_height - icon.getIconHeight) / 2) max 0)
wenzelm@46227
   135
                    icon.paintIcon(gutter, gfx, x0, y0)
wenzelm@46178
   136
                  case None =>
wenzelm@43404
   137
                }
wenzelm@39175
   138
              }
wenzelm@39175
   139
            }
wenzelm@39175
   140
          }
wenzelm@39175
   141
        }
wenzelm@39175
   142
      }
wenzelm@39175
   143
    }
wenzelm@39175
   144
  }
wenzelm@39175
   145
wenzelm@39175
   146
wenzelm@37129
   147
  /* caret handling */
wenzelm@34810
   148
wenzelm@46740
   149
  private val delay_caret_update =
wenzelm@50207
   150
    Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) {
wenzelm@44805
   151
      session.caret_focus.event(Session.Caret_Focus)
wenzelm@34810
   152
    }
wenzelm@46740
   153
wenzelm@46740
   154
  private val caret_listener = new CaretListener {
wenzelm@49195
   155
    override def caretUpdate(e: CaretEvent) { delay_caret_update.invoke() }
wenzelm@34810
   156
  }
wenzelm@34810
   157
wenzelm@34810
   158
wenzelm@46572
   159
  /* text status overview left of scrollbar */
wenzelm@34784
   160
wenzelm@47993
   161
  private object overview extends Text_Overview(this)
wenzelm@46572
   162
  {
wenzelm@46571
   163
    val delay_repaint =
wenzelm@50207
   164
      Swing_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { repaint() }
wenzelm@34784
   165
  }
immler@34403
   166
wenzelm@34784
   167
wenzelm@39740
   168
  /* main actor */
wenzelm@39740
   169
wenzelm@39740
   170
  private val main_actor = actor {
wenzelm@39740
   171
    loop {
wenzelm@39740
   172
      react {
wenzelm@49196
   173
        case _: Session.Raw_Edits =>
wenzelm@49196
   174
          Swing_Thread.later {
wenzelm@50207
   175
            overview.delay_repaint.postpone(PIDE.options.seconds("editor_input_delay"))
wenzelm@49196
   176
          }
wenzelm@49196
   177
wenzelm@44608
   178
        case changed: Session.Commands_Changed =>
wenzelm@39740
   179
          val buffer = model.buffer
wenzelm@46918
   180
          Swing_Thread.later {
wenzelm@49406
   181
            JEdit_Lib.buffer_lock(buffer) {
wenzelm@46918
   182
              if (model.buffer == text_area.getBuffer) {
wenzelm@47027
   183
                val snapshot = model.snapshot()
wenzelm@39740
   184
wenzelm@47027
   185
                if (changed.assignment ||
wenzelm@46918
   186
                    (changed.nodes.contains(model.name) &&
wenzelm@46918
   187
                     changed.commands.exists(snapshot.node.commands.contains)))
wenzelm@49196
   188
                  Swing_Thread.later { overview.delay_repaint.invoke() }
wenzelm@39740
   189
wenzelm@49408
   190
                JEdit_Lib.visible_range(text_area) match {
wenzelm@46918
   191
                  case Some(visible) =>
wenzelm@49408
   192
                    if (changed.assignment) JEdit_Lib.invalidate_range(text_area, visible)
wenzelm@46918
   193
                    else {
wenzelm@46918
   194
                      val visible_cmds =
wenzelm@46918
   195
                        snapshot.node.command_range(snapshot.revert(visible)).map(_._1)
wenzelm@46918
   196
                      if (visible_cmds.exists(changed.commands)) {
wenzelm@46918
   197
                        for {
wenzelm@46918
   198
                          line <- 0 until text_area.getVisibleLines
wenzelm@46997
   199
                          start = text_area.getScreenLineStartOffset(line) if start >= 0
wenzelm@46997
   200
                          end = text_area.getScreenLineEndOffset(line) if end >= 0
wenzelm@49843
   201
                          range = Text.Range(start, end)
wenzelm@46997
   202
                          line_cmds = snapshot.node.command_range(snapshot.revert(range)).map(_._1)
wenzelm@46918
   203
                          if line_cmds.exists(changed.commands)
wenzelm@46918
   204
                        } text_area.invalidateScreenLineRange(line, line)
wenzelm@46918
   205
                      }
wenzelm@46918
   206
                    }
wenzelm@46918
   207
                  case None =>
wenzelm@46583
   208
                }
wenzelm@46918
   209
              }
wenzelm@39740
   210
            }
wenzelm@39740
   211
          }
wenzelm@39740
   212
wenzelm@39740
   213
        case bad => System.err.println("command_change_actor: ignoring bad message " + bad)
wenzelm@39740
   214
      }
wenzelm@39740
   215
    }
wenzelm@39740
   216
  }
wenzelm@39740
   217
wenzelm@39740
   218
wenzelm@34784
   219
  /* activation */
wenzelm@34784
   220
wenzelm@34808
   221
  private def activate()
wenzelm@34784
   222
  {
wenzelm@42825
   223
    val painter = text_area.getPainter
wenzelm@49410
   224
wenzelm@44437
   225
    painter.addExtension(TextAreaPainter.LOWEST_LAYER, update_perspective)
wenzelm@49411
   226
    rich_text_area.activate()
wenzelm@42825
   227
    text_area.getGutter.addExtension(gutter_painter)
wenzelm@34810
   228
    text_area.addCaretListener(caret_listener)
wenzelm@34810
   229
    text_area.addLeftOfScrollBar(overview)
wenzelm@49196
   230
    session.raw_edits += main_actor
wenzelm@39740
   231
    session.commands_changed += main_actor
wenzelm@34784
   232
  }
wenzelm@34784
   233
wenzelm@34808
   234
  private def deactivate()
wenzelm@34784
   235
  {
wenzelm@42825
   236
    val painter = text_area.getPainter
wenzelm@49410
   237
wenzelm@49196
   238
    session.raw_edits -= main_actor
wenzelm@39740
   239
    session.commands_changed -= main_actor
wenzelm@49195
   240
    text_area.removeCaretListener(caret_listener); delay_caret_update.revoke()
wenzelm@49195
   241
    text_area.removeLeftOfScrollBar(overview); overview.delay_repaint.revoke()
wenzelm@42825
   242
    text_area.getGutter.removeExtension(gutter_painter)
wenzelm@49411
   243
    rich_text_area.deactivate()
wenzelm@44437
   244
    painter.removeExtension(update_perspective)
wenzelm@34784
   245
  }
wenzelm@42825
   246
}