src/Tools/jEdit/src/jedit_lib.scala
author wenzelm
Mon, 31 Dec 2012 21:01:00 +0100
changeset 50658 0464c2007a25
parent 50566 b43c4f660320
child 50849 70f7483df9cb
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/jedit_lib.scala
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     3
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     4
Misc library functions for jEdit.
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     5
*/
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     6
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     8
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
     9
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    10
import isabelle._
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    11
50658
0464c2007a25 tuned signature;
wenzelm
parents: 50566
diff changeset
    12
import java.awt.{Component, Container, Window, GraphicsEnvironment, Point, Rectangle}
49710
21d88a631fcc refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents: 49409
diff changeset
    13
21d88a631fcc refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents: 49409
diff changeset
    14
import scala.annotation.tailrec
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    15
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    16
import org.gjt.sp.jedit.{jEdit, Buffer, View}
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    17
import org.gjt.sp.jedit.buffer.JEditBuffer
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    18
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    19
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    20
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    21
object JEdit_Lib
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    22
{
50554
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    23
  /* bounds within multi-screen environment */
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    24
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    25
  def screen_bounds(screen_point: Point): Rectangle =
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    26
  {
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    27
    val ge = GraphicsEnvironment.getLocalGraphicsEnvironment
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    28
    (for {
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    29
      device <- ge.getScreenDevices.iterator
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    30
      config <- device.getConfigurations.iterator
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    31
      bounds = config.getBounds
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    32
    } yield bounds).find(_.contains(screen_point)) getOrElse ge.getMaximumWindowBounds
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    33
  }
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    34
0493efcc97e9 more general handling of graphics configurations, to increase chance of proper positioning of tooltips in multi-screen environment;
wenzelm
parents: 50363
diff changeset
    35
49712
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    36
  /* GUI components */
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    37
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    38
  def get_parent(component: Component): Option[Container] =
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    39
    component.getParent match {
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    40
      case null => None
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    41
      case parent => Some(parent)
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    42
    }
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    43
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    44
  def ancestors(component: Component): Iterator[Container] = new Iterator[Container] {
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    45
    private var next_elem = get_parent(component)
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    46
    def hasNext(): Boolean = next_elem.isDefined
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    47
    def next(): Container =
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    48
      next_elem match {
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    49
        case Some(parent) =>
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    50
          next_elem = get_parent(parent)
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    51
          parent
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    52
        case None => Iterator.empty.next()
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    53
      }
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    54
  }
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    55
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    56
  def parent_window(component: Component): Option[Window] =
a1bd8fe5131b further support for nested tooltips;
wenzelm
parents: 49710
diff changeset
    57
    ancestors(component).find(_.isInstanceOf[Window]).map(_.asInstanceOf[Window])
49710
21d88a631fcc refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents: 49409
diff changeset
    58
21d88a631fcc refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents: 49409
diff changeset
    59
50186
f83cab68c788 recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents: 50116
diff changeset
    60
  /* basic tooltips, with multi-line support */
f83cab68c788 recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents: 50116
diff changeset
    61
f83cab68c788 recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents: 50116
diff changeset
    62
  def wrap_tooltip(text: String): String =
f83cab68c788 recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents: 50116
diff changeset
    63
    if (text == "") null
f83cab68c788 recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents: 50116
diff changeset
    64
    else "<html><pre>" + HTML.encode(text) + "</pre></html>"
f83cab68c788 recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents: 50116
diff changeset
    65
f83cab68c788 recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
wenzelm
parents: 50116
diff changeset
    66
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    67
  /* buffers */
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    68
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    69
  def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    70
    Swing_Thread.now { buffer_lock(buffer) { body } }
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    71
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    72
  def buffer_text(buffer: JEditBuffer): String =
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    73
    buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    74
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    75
  def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    76
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    77
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    78
  /* main jEdit components */
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    79
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    80
  def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    81
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    82
  def jedit_buffer(name: String): Option[Buffer] =
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    83
    jedit_buffers().find(buffer => buffer_name(buffer) == name)
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    84
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    85
  def jedit_views(): Iterator[View] = jEdit.getViews().iterator
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    86
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    87
  def jedit_text_areas(view: View): Iterator[JEditTextArea] =
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    88
    view.getEditPanes().iterator.map(_.getTextArea)
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    89
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    90
  def jedit_text_areas(): Iterator[JEditTextArea] =
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    91
    jedit_views().flatMap(jedit_text_areas(_))
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    92
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    93
  def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    94
    jedit_text_areas().filter(_.getBuffer == buffer)
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    95
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    96
  def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    97
  {
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    98
    try { buffer.readLock(); body }
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
    99
    finally { buffer.readUnlock() }
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   100
  }
49407
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   101
50195
863b1dfc396c prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents: 50186
diff changeset
   102
  def buffer_edit[A](buffer: JEditBuffer)(body: => A): A =
863b1dfc396c prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents: 50186
diff changeset
   103
  {
863b1dfc396c prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents: 50186
diff changeset
   104
    try { buffer.beginCompoundEdit(); body }
863b1dfc396c prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents: 50186
diff changeset
   105
    finally { buffer.endCompoundEdit() }
863b1dfc396c prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents: 50186
diff changeset
   106
  }
863b1dfc396c prefer buffer_edit combinator over Java-style boilerplate;
wenzelm
parents: 50186
diff changeset
   107
49407
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   108
50215
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   109
  /* get text */
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   110
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   111
  def try_get_text(buffer: JEditBuffer, range: Text.Range): Option[String] =
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   112
    try { Some(buffer.getText(range.start, range.length)) }
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   113
    catch { case _: ArrayIndexOutOfBoundsException => None }
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   114
97959912840a more general sendback properties;
wenzelm
parents: 50195
diff changeset
   115
50363
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50215
diff changeset
   116
  /* buffer range */
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50215
diff changeset
   117
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50215
diff changeset
   118
  def buffer_range(buffer: JEditBuffer): Text.Range =
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50215
diff changeset
   119
    Text.Range(0, (buffer.getLength - 1) max 0)
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50215
diff changeset
   120
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50215
diff changeset
   121
49407
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   122
  /* point range */
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   123
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   124
  def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range =
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   125
    buffer_lock(buffer) {
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   126
      def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0)
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   127
      try {
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   128
        val c = text(offset)
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   129
        if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1)))
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   130
          Text.Range(offset, offset + 2)
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   131
        else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1)))
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   132
          Text.Range(offset - 1, offset + 1)
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   133
        else Text.Range(offset, offset + 1)
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   134
      }
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   135
      catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) }
215ba6884bdf tuned signature;
wenzelm
parents: 49406
diff changeset
   136
    }
49408
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   137
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   138
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   139
  /* visible text range */
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   140
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   141
  def visible_range(text_area: TextArea): Option[Text.Range] =
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   142
  {
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   143
    val buffer = text_area.getBuffer
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   144
    val n = text_area.getVisibleLines
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   145
    if (n > 0) {
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   146
      val start = text_area.getScreenLineStartOffset(0)
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   147
      val raw_end = text_area.getScreenLineEndOffset(n - 1)
49843
afddf4e26fac further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents: 49712
diff changeset
   148
      val end = if (raw_end >= 0) raw_end min buffer.getLength else buffer.getLength
afddf4e26fac further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents: 49712
diff changeset
   149
      Some(Text.Range(start, end))
49408
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   150
    }
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   151
    else None
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   152
  }
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   153
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   154
  def invalidate_range(text_area: TextArea, range: Text.Range)
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   155
  {
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   156
    val buffer = text_area.getBuffer
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   157
    text_area.invalidateLineRange(
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   158
      buffer.getLineOfOffset(range.start),
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   159
      buffer.getLineOfOffset(range.stop))
3cfc114acd05 tuned signature;
wenzelm
parents: 49407
diff changeset
   160
  }
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   161
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   162
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   163
  /* char width */
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   164
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   165
  def char_width(text_area: TextArea): Int =
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   166
  {
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   167
    val painter = text_area.getPainter
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   168
    val font = painter.getFont
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   169
    val font_context = painter.getFontRenderContext
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   170
    font.getStringBounds(" ", font_context).getWidth.round.toInt
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   171
  }
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   172
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   173
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   174
  /* graphics range */
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   175
50115
8cde6f1a0106 tuned signature;
wenzelm
parents: 49941
diff changeset
   176
  case class Gfx_Range(val x: Int, val y: Int, val length: Int)
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   177
49843
afddf4e26fac further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents: 49712
diff changeset
   178
  // NB: jEdit always normalizes \r\n and \r to \n
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   179
  // NB: last line lacks \n
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   180
  def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   181
  {
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   182
    val buffer = text_area.getBuffer
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   183
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   184
    val p = text_area.offsetToXY(range.start)
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   185
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   186
    val end = buffer.getLength
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   187
    val stop = range.stop
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   188
    val (q, r) =
49843
afddf4e26fac further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents: 49712
diff changeset
   189
      if (stop >= end) (text_area.offsetToXY(end), char_width(text_area) * (stop - end))
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   190
      else if (stop > 0 && buffer.getText(stop - 1, 1) == "\n")
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   191
        (text_area.offsetToXY(stop - 1), char_width(text_area))
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   192
      else (text_area.offsetToXY(stop), 0)
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   193
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   194
    if (p != null && q != null && p.x < q.x + r && p.y == q.y)
50115
8cde6f1a0106 tuned signature;
wenzelm
parents: 49941
diff changeset
   195
      Some(Gfx_Range(p.x, p.y, q.x + r - p.x))
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   196
    else None
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   197
  }
49941
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   198
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   199
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   200
  /* pixel range */
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   201
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   202
  def pixel_range(text_area: TextArea, x: Int, y: Int): Option[Text.Range] =
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   203
  {
50116
88b971fca902 more accurate pixel_range -- do not round offset here;
wenzelm
parents: 50115
diff changeset
   204
    val range = point_range(text_area.getBuffer, text_area.xyToOffset(x, y, false))
49941
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   205
    gfx_range(text_area, range) match {
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   206
      case Some(g) if (g.x <= x && x < g.x + g.length) => Some(range)
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   207
      case _ => None
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   208
    }
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   209
  }
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   210
}
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents:
diff changeset
   211