src/Tools/jEdit/src/jedit_lib.scala
author wenzelm
Tue Aug 27 12:35:32 2013 +0200 (2013-08-27)
changeset 53226 9cf8e2263ca7
parent 53183 018d71bee930
child 53231 423e29f1f304
permissions -rw-r--r--
more systematic JEdit_Lib.key_listener with optional KeyEventWorkaround;
wenzelm@49406
     1
/*  Title:      Tools/jEdit/src/jedit_lib.scala
wenzelm@49406
     2
    Author:     Makarius
wenzelm@49406
     3
wenzelm@49406
     4
Misc library functions for jEdit.
wenzelm@49406
     5
*/
wenzelm@49406
     6
wenzelm@49406
     7
package isabelle.jedit
wenzelm@49406
     8
wenzelm@49406
     9
wenzelm@49406
    10
import isabelle._
wenzelm@49406
    11
wenzelm@50658
    12
import java.awt.{Component, Container, Window, GraphicsEnvironment, Point, Rectangle}
wenzelm@53226
    13
import java.awt.event.{KeyEvent, KeyListener}
wenzelm@53019
    14
import javax.swing.{Icon, ImageIcon, JWindow}
wenzelm@49710
    15
wenzelm@49710
    16
import scala.annotation.tailrec
wenzelm@49406
    17
wenzelm@52873
    18
import org.gjt.sp.jedit.{jEdit, Buffer, View, GUIUtilities}
wenzelm@53226
    19
import org.gjt.sp.jedit.gui.KeyEventWorkaround
wenzelm@49406
    20
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@51492
    21
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, TextAreaPainter}
wenzelm@49406
    22
wenzelm@49406
    23
wenzelm@49406
    24
object JEdit_Lib
wenzelm@49406
    25
{
wenzelm@50554
    26
  /* bounds within multi-screen environment */
wenzelm@50554
    27
wenzelm@50554
    28
  def screen_bounds(screen_point: Point): Rectangle =
wenzelm@50554
    29
  {
wenzelm@50554
    30
    val ge = GraphicsEnvironment.getLocalGraphicsEnvironment
wenzelm@50554
    31
    (for {
wenzelm@50554
    32
      device <- ge.getScreenDevices.iterator
wenzelm@50554
    33
      config <- device.getConfigurations.iterator
wenzelm@50554
    34
      bounds = config.getBounds
wenzelm@50554
    35
    } yield bounds).find(_.contains(screen_point)) getOrElse ge.getMaximumWindowBounds
wenzelm@50554
    36
  }
wenzelm@50554
    37
wenzelm@50554
    38
wenzelm@53019
    39
  /* window geometry measurement */
wenzelm@53019
    40
wenzelm@53019
    41
  private lazy val dummy_window = new JWindow
wenzelm@53019
    42
wenzelm@53019
    43
  final case class Window_Geometry(width: Int, height: Int, inner_width: Int, inner_height: Int)
wenzelm@53019
    44
  {
wenzelm@53019
    45
    def deco_width: Int = width - inner_width
wenzelm@53019
    46
    def deco_height: Int = height - inner_height
wenzelm@53019
    47
  }
wenzelm@53019
    48
wenzelm@53019
    49
  def window_geometry(outer: Container, inner: Component): Window_Geometry =
wenzelm@53019
    50
  {
wenzelm@53019
    51
    Swing_Thread.require()
wenzelm@53019
    52
wenzelm@53019
    53
    val old_content = dummy_window.getContentPane
wenzelm@53019
    54
wenzelm@53019
    55
    dummy_window.setContentPane(outer)
wenzelm@53019
    56
    dummy_window.pack
wenzelm@53019
    57
    dummy_window.revalidate
wenzelm@53019
    58
wenzelm@53019
    59
    val geometry =
wenzelm@53019
    60
      Window_Geometry(
wenzelm@53019
    61
        dummy_window.getWidth, dummy_window.getHeight, inner.getWidth, inner.getHeight)
wenzelm@53019
    62
wenzelm@53019
    63
    dummy_window.setContentPane(old_content)
wenzelm@53019
    64
wenzelm@53019
    65
    geometry
wenzelm@53019
    66
  }
wenzelm@53019
    67
wenzelm@53019
    68
wenzelm@49712
    69
  /* GUI components */
wenzelm@49712
    70
wenzelm@49712
    71
  def get_parent(component: Component): Option[Container] =
wenzelm@49712
    72
    component.getParent match {
wenzelm@49712
    73
      case null => None
wenzelm@49712
    74
      case parent => Some(parent)
wenzelm@49712
    75
    }
wenzelm@49712
    76
wenzelm@49712
    77
  def ancestors(component: Component): Iterator[Container] = new Iterator[Container] {
wenzelm@49712
    78
    private var next_elem = get_parent(component)
wenzelm@49712
    79
    def hasNext(): Boolean = next_elem.isDefined
wenzelm@49712
    80
    def next(): Container =
wenzelm@49712
    81
      next_elem match {
wenzelm@49712
    82
        case Some(parent) =>
wenzelm@49712
    83
          next_elem = get_parent(parent)
wenzelm@49712
    84
          parent
wenzelm@49712
    85
        case None => Iterator.empty.next()
wenzelm@49712
    86
      }
wenzelm@49712
    87
  }
wenzelm@49712
    88
wenzelm@49712
    89
  def parent_window(component: Component): Option[Window] =
wenzelm@52478
    90
    ancestors(component).collectFirst({ case x: Window => x })
wenzelm@49710
    91
wenzelm@49710
    92
wenzelm@50186
    93
  /* basic tooltips, with multi-line support */
wenzelm@50186
    94
wenzelm@50186
    95
  def wrap_tooltip(text: String): String =
wenzelm@50186
    96
    if (text == "") null
wenzelm@50186
    97
    else "<html><pre>" + HTML.encode(text) + "</pre></html>"
wenzelm@50186
    98
wenzelm@50186
    99
wenzelm@49406
   100
  /* buffers */
wenzelm@49406
   101
wenzelm@49406
   102
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@49406
   103
    Swing_Thread.now { buffer_lock(buffer) { body } }
wenzelm@49406
   104
wenzelm@49406
   105
  def buffer_text(buffer: JEditBuffer): String =
wenzelm@49406
   106
    buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
wenzelm@49406
   107
wenzelm@49406
   108
  def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath
wenzelm@49406
   109
wenzelm@49406
   110
wenzelm@53003
   111
  /* focus of main window */
wenzelm@53003
   112
wenzelm@53003
   113
  def request_focus_view: Unit =
wenzelm@53003
   114
  {
wenzelm@53003
   115
    jEdit.getActiveView() match {
wenzelm@53003
   116
      case null =>
wenzelm@53003
   117
      case view =>
wenzelm@53003
   118
        view.getTextArea match {
wenzelm@53003
   119
          case null =>
wenzelm@53003
   120
          case text_area => text_area.requestFocus
wenzelm@53003
   121
        }
wenzelm@53003
   122
    }
wenzelm@53003
   123
  }
wenzelm@53003
   124
wenzelm@53003
   125
wenzelm@49406
   126
  /* main jEdit components */
wenzelm@49406
   127
wenzelm@49406
   128
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
wenzelm@49406
   129
wenzelm@49406
   130
  def jedit_buffer(name: String): Option[Buffer] =
wenzelm@49406
   131
    jedit_buffers().find(buffer => buffer_name(buffer) == name)
wenzelm@49406
   132
wenzelm@49406
   133
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
wenzelm@49406
   134
wenzelm@49406
   135
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
wenzelm@49406
   136
    view.getEditPanes().iterator.map(_.getTextArea)
wenzelm@49406
   137
wenzelm@49406
   138
  def jedit_text_areas(): Iterator[JEditTextArea] =
wenzelm@49406
   139
    jedit_views().flatMap(jedit_text_areas(_))
wenzelm@49406
   140
wenzelm@49406
   141
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
wenzelm@49406
   142
    jedit_text_areas().filter(_.getBuffer == buffer)
wenzelm@49406
   143
wenzelm@49406
   144
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@49406
   145
  {
wenzelm@49406
   146
    try { buffer.readLock(); body }
wenzelm@49406
   147
    finally { buffer.readUnlock() }
wenzelm@49406
   148
  }
wenzelm@49407
   149
wenzelm@50195
   150
  def buffer_edit[A](buffer: JEditBuffer)(body: => A): A =
wenzelm@50195
   151
  {
wenzelm@50195
   152
    try { buffer.beginCompoundEdit(); body }
wenzelm@50195
   153
    finally { buffer.endCompoundEdit() }
wenzelm@50195
   154
  }
wenzelm@50195
   155
wenzelm@49407
   156
wenzelm@50215
   157
  /* get text */
wenzelm@50215
   158
wenzelm@50215
   159
  def try_get_text(buffer: JEditBuffer, range: Text.Range): Option[String] =
wenzelm@50215
   160
    try { Some(buffer.getText(range.start, range.length)) }
wenzelm@50215
   161
    catch { case _: ArrayIndexOutOfBoundsException => None }
wenzelm@50215
   162
wenzelm@50215
   163
wenzelm@50363
   164
  /* buffer range */
wenzelm@50363
   165
wenzelm@50363
   166
  def buffer_range(buffer: JEditBuffer): Text.Range =
wenzelm@50363
   167
    Text.Range(0, (buffer.getLength - 1) max 0)
wenzelm@50363
   168
wenzelm@50363
   169
wenzelm@49407
   170
  /* point range */
wenzelm@49407
   171
wenzelm@49407
   172
  def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range =
wenzelm@49407
   173
    buffer_lock(buffer) {
wenzelm@49407
   174
      def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0)
wenzelm@49407
   175
      try {
wenzelm@49407
   176
        val c = text(offset)
wenzelm@49407
   177
        if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1)))
wenzelm@49407
   178
          Text.Range(offset, offset + 2)
wenzelm@49407
   179
        else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1)))
wenzelm@49407
   180
          Text.Range(offset - 1, offset + 1)
wenzelm@49407
   181
        else Text.Range(offset, offset + 1)
wenzelm@49407
   182
      }
wenzelm@49407
   183
      catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) }
wenzelm@49407
   184
    }
wenzelm@49408
   185
wenzelm@49408
   186
wenzelm@49408
   187
  /* visible text range */
wenzelm@49408
   188
wenzelm@49408
   189
  def visible_range(text_area: TextArea): Option[Text.Range] =
wenzelm@49408
   190
  {
wenzelm@49408
   191
    val buffer = text_area.getBuffer
wenzelm@49408
   192
    val n = text_area.getVisibleLines
wenzelm@49408
   193
    if (n > 0) {
wenzelm@49408
   194
      val start = text_area.getScreenLineStartOffset(0)
wenzelm@49408
   195
      val raw_end = text_area.getScreenLineEndOffset(n - 1)
wenzelm@49843
   196
      val end = if (raw_end >= 0) raw_end min buffer.getLength else buffer.getLength
wenzelm@49843
   197
      Some(Text.Range(start, end))
wenzelm@49408
   198
    }
wenzelm@49408
   199
    else None
wenzelm@49408
   200
  }
wenzelm@49408
   201
wenzelm@49408
   202
  def invalidate_range(text_area: TextArea, range: Text.Range)
wenzelm@49408
   203
  {
wenzelm@49408
   204
    val buffer = text_area.getBuffer
wenzelm@49408
   205
    text_area.invalidateLineRange(
wenzelm@49408
   206
      buffer.getLineOfOffset(range.start),
wenzelm@49408
   207
      buffer.getLineOfOffset(range.stop))
wenzelm@49408
   208
  }
wenzelm@49409
   209
wenzelm@49409
   210
wenzelm@49409
   211
  /* graphics range */
wenzelm@49409
   212
wenzelm@50115
   213
  case class Gfx_Range(val x: Int, val y: Int, val length: Int)
wenzelm@49409
   214
wenzelm@49843
   215
  // NB: jEdit always normalizes \r\n and \r to \n
wenzelm@49409
   216
  // NB: last line lacks \n
wenzelm@49409
   217
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
wenzelm@49409
   218
  {
wenzelm@51507
   219
    val metric = pretty_metric(text_area.getPainter)
wenzelm@51492
   220
    val char_width = (metric.unit * metric.average).round.toInt
wenzelm@50849
   221
wenzelm@49409
   222
    val buffer = text_area.getBuffer
wenzelm@49409
   223
wenzelm@49409
   224
    val end = buffer.getLength
wenzelm@49409
   225
    val stop = range.stop
wenzelm@51078
   226
wenzelm@51078
   227
    val (p, q, r) =
wenzelm@51078
   228
      try {
wenzelm@51078
   229
        val p = text_area.offsetToXY(range.start)
wenzelm@51078
   230
        val (q, r) =
wenzelm@51078
   231
          if (stop >= end) (text_area.offsetToXY(end), char_width * (stop - end))
wenzelm@51078
   232
          else if (stop > 0 && buffer.getText(stop - 1, 1) == "\n")
wenzelm@51078
   233
            (text_area.offsetToXY(stop - 1), char_width)
wenzelm@51078
   234
          else (text_area.offsetToXY(stop), 0)
wenzelm@51078
   235
        (p, q, r)
wenzelm@51078
   236
      }
wenzelm@51078
   237
      catch { case _: ArrayIndexOutOfBoundsException => (null, null, 0) }
wenzelm@49409
   238
wenzelm@49409
   239
    if (p != null && q != null && p.x < q.x + r && p.y == q.y)
wenzelm@50115
   240
      Some(Gfx_Range(p.x, p.y, q.x + r - p.x))
wenzelm@49409
   241
    else None
wenzelm@49409
   242
  }
wenzelm@49941
   243
wenzelm@49941
   244
wenzelm@49941
   245
  /* pixel range */
wenzelm@49941
   246
wenzelm@49941
   247
  def pixel_range(text_area: TextArea, x: Int, y: Int): Option[Text.Range] =
wenzelm@49941
   248
  {
wenzelm@53183
   249
    // coordinates wrt. inner painter component
wenzelm@53183
   250
    val painter = text_area.getPainter
wenzelm@53183
   251
    if (0 <= x && x < painter.getWidth && 0 <= y && y < painter.getHeight) {
wenzelm@53183
   252
      val offset = text_area.xyToOffset(x, y, false)
wenzelm@53183
   253
      if (offset >= 0) {
wenzelm@53183
   254
        val range = point_range(text_area.getBuffer, offset)
wenzelm@53183
   255
        gfx_range(text_area, range) match {
wenzelm@53183
   256
          case Some(g) if (g.x <= x && x < g.x + g.length) => Some(range)
wenzelm@53183
   257
          case _ => None
wenzelm@53183
   258
        }
wenzelm@53183
   259
      }
wenzelm@53183
   260
      else None
wenzelm@49941
   261
    }
wenzelm@53183
   262
    else None
wenzelm@49941
   263
  }
wenzelm@51492
   264
wenzelm@51492
   265
wenzelm@51492
   266
  /* pretty text metric */
wenzelm@51492
   267
wenzelm@51507
   268
  abstract class Pretty_Metric extends Pretty.Metric
wenzelm@51507
   269
  {
wenzelm@51507
   270
    def average: Double
wenzelm@51507
   271
  }
wenzelm@51492
   272
wenzelm@51507
   273
  def pretty_metric(painter: TextAreaPainter): Pretty_Metric =
wenzelm@51507
   274
    new Pretty_Metric {
wenzelm@51507
   275
      def string_width(s: String): Double =
wenzelm@51507
   276
        painter.getFont.getStringBounds(s, painter.getFontRenderContext).getWidth
wenzelm@51507
   277
wenzelm@51507
   278
      val unit = string_width(Pretty.space)
wenzelm@51507
   279
      val average = string_width("mix") / (3 * unit)
wenzelm@51507
   280
      def apply(s: String): Double = if (s == "\n") 1.0 else string_width(s) / unit
wenzelm@51492
   281
    }
wenzelm@52873
   282
wenzelm@52873
   283
wenzelm@52873
   284
  /* icons */
wenzelm@52873
   285
wenzelm@52873
   286
  def load_icon(name: String): Icon =
wenzelm@52873
   287
  {
wenzelm@52873
   288
    val name1 =
wenzelm@52873
   289
      if (name.startsWith("idea-icons/")) {
wenzelm@52873
   290
        val file =
wenzelm@52873
   291
          Isabelle_System.platform_file_url(Path.explode("$JEDIT_HOME/dist/jars/idea-icons.jar"))
wenzelm@52873
   292
        "jar:" + file + "!/" + name
wenzelm@52873
   293
      }
wenzelm@52873
   294
      else name
wenzelm@52873
   295
    val icon = GUIUtilities.loadIcon(name1)
wenzelm@52873
   296
    if (icon.getIconWidth < 0 || icon.getIconHeight < 0) error("Bad icon: " + name)
wenzelm@52873
   297
    else icon
wenzelm@52873
   298
  }
wenzelm@52874
   299
wenzelm@52874
   300
  def load_image_icon(name: String): ImageIcon =
wenzelm@52874
   301
    load_icon(name) match {
wenzelm@52874
   302
      case icon: ImageIcon => icon
wenzelm@52874
   303
      case _ => error("Bad image icon: " + name)
wenzelm@52874
   304
    }
wenzelm@53226
   305
wenzelm@53226
   306
wenzelm@53226
   307
  /* key listener */
wenzelm@53226
   308
wenzelm@53226
   309
  def key_listener(
wenzelm@53226
   310
    workaround: Boolean = true,
wenzelm@53226
   311
    key_typed: KeyEvent => Unit = _ => (),
wenzelm@53226
   312
    key_pressed: KeyEvent => Unit = _ => (),
wenzelm@53226
   313
    key_released: KeyEvent => Unit = _ => ()): KeyListener =
wenzelm@53226
   314
  {
wenzelm@53226
   315
    def process_key_event(evt0: KeyEvent, handle: KeyEvent => Unit)
wenzelm@53226
   316
    {
wenzelm@53226
   317
      val evt = if (workaround) KeyEventWorkaround.processKeyEvent(evt0) else evt0
wenzelm@53226
   318
      if (evt != null) handle(evt)
wenzelm@53226
   319
    }
wenzelm@53226
   320
wenzelm@53226
   321
    new KeyListener
wenzelm@53226
   322
    {
wenzelm@53226
   323
      def keyTyped(evt: KeyEvent) { process_key_event(evt, key_typed) }
wenzelm@53226
   324
      def keyPressed(evt: KeyEvent) { process_key_event(evt, key_pressed) }
wenzelm@53226
   325
      def keyReleased(evt: KeyEvent) { process_key_event(evt, key_released) }
wenzelm@53226
   326
    }
wenzelm@53226
   327
  }
wenzelm@49406
   328
}
wenzelm@49406
   329