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