src/Tools/jEdit/src/rich_text_area.scala
author wenzelm
Mon, 18 Mar 2013 11:29:50 +0100
changeset 51452 14e6d761ba1c
parent 51449 8d6e478934dc
child 51469 18120e26f818
permissions -rw-r--r--
extra tooltip_delay after window.dismiss operation, to avoid flickering of quick reactivation;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49411
1da54e9bda68 renamed Text_Area_Painter to Rich_Text_Area;
wenzelm
parents: 49410
diff changeset
     1
/*  Title:      Tools/jEdit/src/rich_text_area.scala
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
     3
49411
1da54e9bda68 renamed Text_Area_Painter to Rich_Text_Area;
wenzelm
parents: 49410
diff changeset
     4
Enhanced version of jEdit text area, with rich text rendering,
1da54e9bda68 renamed Text_Area_Painter to Rich_Text_Area;
wenzelm
parents: 49410
diff changeset
     5
tooltips, hyperlinks etc.
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
     6
*/
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
     7
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
     8
package isabelle.jedit
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
     9
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    10
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    11
import isabelle._
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    12
50657
57abb2a814ab tuned imports;
wenzelm
parents: 50641
diff changeset
    13
import java.awt.{Graphics2D, Shape, Color, Point, Toolkit}
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    14
import java.awt.event.{MouseMotionAdapter, MouseAdapter, MouseEvent,
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    15
  FocusAdapter, FocusEvent, WindowEvent, WindowAdapter}
43392
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    16
import java.awt.font.TextAttribute
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    17
import java.text.AttributedString
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    18
import java.util.ArrayList
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    19
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    20
import org.gjt.sp.util.Log
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    21
import org.gjt.sp.jedit.{OperatingSystem, Debug, View}
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    22
import org.gjt.sp.jedit.syntax.{DisplayTokenHandler, Chunk}
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    23
import org.gjt.sp.jedit.textarea.{TextAreaExtension, TextAreaPainter, TextArea}
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    24
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    25
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
    26
class Rich_Text_Area(
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
    27
  view: View,
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
    28
  text_area: TextArea,
50199
6d04e2422769 quasi-abstract module Rendering, with Isabelle-specific implementation;
wenzelm
parents: 50165
diff changeset
    29
  get_rendering: () => Rendering,
50915
12de8ea66f54 close tooltip after Active.action, to make it look more interactive (notably due to lack of dynamic update);
wenzelm
parents: 50849
diff changeset
    30
  close_action: () => Unit,
50306
b655d2d0406d updated to jedit-5.0.0;
wenzelm
parents: 50216
diff changeset
    31
  caret_visible: Boolean,
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
    32
  hovering: Boolean)
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    33
{
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    34
  private val buffer = text_area.getBuffer
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    35
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    36
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    37
  /* robust extension body */
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    38
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    39
  def robust_body[A](default: A)(body: => A): A =
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    40
  {
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    41
    try {
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    42
      Swing_Thread.require()
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    43
      if (buffer == text_area.getBuffer) body
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    44
      else {
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    45
        Log.log(Log.ERROR, this, ERROR("Implicit change of text area buffer"))
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    46
        default
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    47
      }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    48
    }
50641
wenzelm
parents: 50553
diff changeset
    49
    catch { case exn: Throwable => Log.log(Log.ERROR, this, exn); default }
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    50
  }
43376
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
    51
43392
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    52
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    53
  /* original painters */
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    54
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    55
  private def pick_extension(name: String): TextAreaExtension =
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    56
  {
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    57
    text_area.getPainter.getExtensions.iterator.filter(x => x.getClass.getName == name).toList
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    58
    match {
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    59
      case List(x) => x
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    60
      case _ => error("Expected exactly one " + name)
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    61
    }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    62
  }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    63
43392
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    64
  private val orig_text_painter =
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    65
    pick_extension("org.gjt.sp.jedit.textarea.TextAreaPainter$PaintText")
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    66
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
    67
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
    68
  /* common painter state */
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    69
50199
6d04e2422769 quasi-abstract module Rendering, with Isabelle-specific implementation;
wenzelm
parents: 50165
diff changeset
    70
  @volatile private var painter_rendering: Rendering = null
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
    71
  @volatile private var painter_clip: Shape = null
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    72
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
    73
  private val set_state = new TextAreaExtension
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    74
  {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    75
    override def paintScreenLineRange(gfx: Graphics2D,
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    76
      first_line: Int, last_line: Int, physical_lines: Array[Int],
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    77
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    78
    {
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    79
      painter_rendering = get_rendering()
46220
663251a395c2 more explicit/robust treatment of common snapshot;
wenzelm
parents: 46205
diff changeset
    80
      painter_clip = gfx.getClip
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    81
    }
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    82
  }
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    83
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
    84
  private val reset_state = new TextAreaExtension
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
    85
  {
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    86
    override def paintScreenLineRange(gfx: Graphics2D,
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    87
      first_line: Int, last_line: Int, physical_lines: Array[Int],
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    88
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    89
    {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
    90
      painter_rendering = null
46220
663251a395c2 more explicit/robust treatment of common snapshot;
wenzelm
parents: 46205
diff changeset
    91
      painter_clip = null
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    92
    }
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    93
  }
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
    94
50199
6d04e2422769 quasi-abstract module Rendering, with Isabelle-specific implementation;
wenzelm
parents: 50165
diff changeset
    95
  def robust_rendering(body: Rendering => Unit)
46220
663251a395c2 more explicit/robust treatment of common snapshot;
wenzelm
parents: 46205
diff changeset
    96
  {
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    97
    robust_body(()) { body(painter_rendering) }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    98
  }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
    99
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   100
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   101
  /* active areas within the text */
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   102
50199
6d04e2422769 quasi-abstract module Rendering, with Isabelle-specific implementation;
wenzelm
parents: 50165
diff changeset
   103
  private class Active_Area[A](rendering: Rendering => Text.Range => Option[Text.Info[A]])
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   104
  {
49493
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   105
    private var the_text_info: Option[(String, Text.Info[A])] = None
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   106
49493
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   107
    def text_info: Option[(String, Text.Info[A])] = the_text_info
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   108
    def info: Option[Text.Info[A]] = the_text_info.map(_._2)
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   109
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   110
    def update(new_info: Option[Text.Info[A]])
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   111
    {
49493
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   112
      val old_text_info = the_text_info
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   113
      val new_text_info =
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   114
        new_info.map(info => (text_area.getText(info.range.start, info.range.length), info))
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   115
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   116
      if (new_text_info != old_text_info) {
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   117
        for {
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   118
          r0 <- JEdit_Lib.visible_range(text_area)
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   119
          opt <- List(old_text_info, new_text_info)
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   120
          (_, Text.Info(r1, _)) <- opt
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   121
          r2 <- r1.try_restrict(r0)  // FIXME more precise?!
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   122
        } JEdit_Lib.invalidate_range(text_area, r2)
db58490a68ac more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents: 49492
diff changeset
   123
        the_text_info = new_text_info
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   124
      }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   125
    }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   126
50199
6d04e2422769 quasi-abstract module Rendering, with Isabelle-specific implementation;
wenzelm
parents: 50165
diff changeset
   127
    def update_rendering(r: Rendering, range: Text.Range)
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   128
    { update(rendering(r)(range)) }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   129
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   130
    def reset { update(None) }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   131
  }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   132
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   133
  // owned by Swing thread
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   134
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   135
  private var control: Boolean = false
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   136
50199
6d04e2422769 quasi-abstract module Rendering, with Isabelle-specific implementation;
wenzelm
parents: 50165
diff changeset
   137
  private val highlight_area = new Active_Area[Color]((r: Rendering) => r.highlight _)
6d04e2422769 quasi-abstract module Rendering, with Isabelle-specific implementation;
wenzelm
parents: 50165
diff changeset
   138
  private val hyperlink_area = new Active_Area[Hyperlink]((r: Rendering) => r.hyperlink _)
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50306
diff changeset
   139
  private val active_area = new Active_Area[XML.Elem]((r: Rendering) => r.active _)
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   140
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   141
  private val active_areas =
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50306
diff changeset
   142
    List((highlight_area, true), (hyperlink_area, true), (active_area, false))
50216
de77cde57376 reset active areas on content update;
wenzelm
parents: 50215
diff changeset
   143
  def active_reset(): Unit = active_areas.foreach(_._1.reset)
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   144
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   145
  private val focus_listener = new FocusAdapter {
49424
491363c6feb4 more robust GUI component handlers;
wenzelm
parents: 49411
diff changeset
   146
    override def focusLost(e: FocusEvent) { robust_body(()) { active_reset() } }
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   147
  }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   148
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   149
  private val window_listener = new WindowAdapter {
49424
491363c6feb4 more robust GUI component handlers;
wenzelm
parents: 49411
diff changeset
   150
    override def windowIconified(e: WindowEvent) { robust_body(()) { active_reset() } }
491363c6feb4 more robust GUI component handlers;
wenzelm
parents: 49411
diff changeset
   151
    override def windowDeactivated(e: WindowEvent) { robust_body(()) { active_reset() } }
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   152
  }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   153
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   154
  private val mouse_listener = new MouseAdapter {
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   155
    override def mouseClicked(e: MouseEvent) {
49424
491363c6feb4 more robust GUI component handlers;
wenzelm
parents: 49411
diff changeset
   156
      robust_body(()) {
491363c6feb4 more robust GUI component handlers;
wenzelm
parents: 49411
diff changeset
   157
        hyperlink_area.info match {
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   158
          case Some(Text.Info(_, link)) => link.follow(view)
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   159
          case None =>
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   160
        }
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50306
diff changeset
   161
        active_area.text_info match {
50915
12de8ea66f54 close tooltip after Active.action, to make it look more interactive (notably due to lack of dynamic update);
wenzelm
parents: 50849
diff changeset
   162
          case Some((text, Text.Info(_, markup))) =>
12de8ea66f54 close tooltip after Active.action, to make it look more interactive (notably due to lack of dynamic update);
wenzelm
parents: 50849
diff changeset
   163
            Active.action(view, text, markup)
12de8ea66f54 close tooltip after Active.action, to make it look more interactive (notably due to lack of dynamic update);
wenzelm
parents: 50849
diff changeset
   164
            close_action()
49424
491363c6feb4 more robust GUI component handlers;
wenzelm
parents: 49411
diff changeset
   165
          case None =>
491363c6feb4 more robust GUI component handlers;
wenzelm
parents: 49411
diff changeset
   166
        }
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   167
      }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   168
    }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   169
  }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   170
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   171
  private val mouse_motion_listener = new MouseMotionAdapter {
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   172
    override def mouseMoved(e: MouseEvent) {
49424
491363c6feb4 more robust GUI component handlers;
wenzelm
parents: 49411
diff changeset
   173
      robust_body(()) {
50553
ce0398b766ce prefer more official getMenuShortcutKeyMask, in deviation to traditional jEdit technique;
wenzelm
parents: 50502
diff changeset
   174
        control = (e.getModifiers & Toolkit.getDefaultToolkit.getMenuShortcutKeyMask) != 0
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   175
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   176
        if ((control || hovering) && !buffer.isLoading) {
49424
491363c6feb4 more robust GUI component handlers;
wenzelm
parents: 49411
diff changeset
   177
          JEdit_Lib.buffer_lock(buffer) {
51441
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   178
            JEdit_Lib.pixel_range(text_area, e.getX, e.getY) match {
50211
2a3d6d760629 always reset active areas;
wenzelm
parents: 50199
diff changeset
   179
              case None => active_reset()
49941
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   180
              case Some(range) =>
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   181
                val rendering = get_rendering()
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   182
                for ((area, require_control) <- active_areas)
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   183
                {
50165
24d47733975f reset active area for outdated snapshot (again?);
wenzelm
parents: 50164
diff changeset
   184
                  if (control == require_control && !rendering.snapshot.is_outdated)
49941
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   185
                    area.update_rendering(rendering, range)
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   186
                  else area.reset
f2db0596bd61 more precise pixel_range: avoid popup when pointing into empty space after actual end-of-line;
wenzelm
parents: 49843
diff changeset
   187
                }
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   188
            }
49424
491363c6feb4 more robust GUI component handlers;
wenzelm
parents: 49411
diff changeset
   189
          }
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   190
        }
49424
491363c6feb4 more robust GUI component handlers;
wenzelm
parents: 49411
diff changeset
   191
        else active_reset()
51441
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   192
51452
14e6d761ba1c extra tooltip_delay after window.dismiss operation, to avoid flickering of quick reactivation;
wenzelm
parents: 51449
diff changeset
   193
        if (Pretty_Tooltip.is_active) {
14e6d761ba1c extra tooltip_delay after window.dismiss operation, to avoid flickering of quick reactivation;
wenzelm
parents: 51449
diff changeset
   194
          tooltip_event = Some(e)
14e6d761ba1c extra tooltip_delay after window.dismiss operation, to avoid flickering of quick reactivation;
wenzelm
parents: 51449
diff changeset
   195
          tooltip_delay.invoke()
14e6d761ba1c extra tooltip_delay after window.dismiss operation, to avoid flickering of quick reactivation;
wenzelm
parents: 51449
diff changeset
   196
        }
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   197
      }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   198
    }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   199
  }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   200
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   201
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   202
  /* tooltips */
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   203
51441
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   204
  private var tooltip_event: Option[MouseEvent] = None
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   205
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   206
  private val tooltip_delay =
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   207
    Swing_Thread.delay_last(PIDE.options.seconds("jedit_tooltip_delay")) {
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   208
      tooltip_event match {
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   209
        case Some(e) if e.getSource == text_area.getPainter =>
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   210
          val rendering = get_rendering()
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   211
          val snapshot = rendering.snapshot
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   212
          if (!snapshot.is_outdated) {
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   213
            val x = e.getX
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   214
            val y = e.getY
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   215
            JEdit_Lib.pixel_range(text_area, x, y) match {
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   216
              case None =>
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   217
              case Some(range) =>
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   218
                val tip =
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   219
                  if (control) rendering.tooltip(range)
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   220
                  else rendering.tooltip_message(range)
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   221
                if (!tip.isEmpty) {
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   222
                  val painter = text_area.getPainter
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   223
                  val y1 = y + painter.getFontMetrics.getHeight / 2
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   224
                  val results = rendering.command_results(range)
51449
8d6e478934dc explicit handling of tooltip window stack -- avoid memory leak due to not-so-weak references to disposed windows (via event handlers and other aux. components);
wenzelm
parents: 51441
diff changeset
   225
                  Pretty_Tooltip(view, painter, rendering, x, y1, results, tip)
51441
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   226
                }
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   227
            }
49730
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49702
diff changeset
   228
          }
51441
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   229
          tooltip_event = None
37f699750430 more elementary tooltips via mouse events (imitating parts of javax.swing.ToolTipManager) -- avoid abuse of getToolTipText to produce window as side-effect;
wenzelm
parents: 50915
diff changeset
   230
        case _ =>
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   231
      }
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   232
    }
46220
663251a395c2 more explicit/robust treatment of common snapshot;
wenzelm
parents: 46205
diff changeset
   233
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   234
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   235
  /* text background */
43376
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   236
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   237
  private val background_painter = new TextAreaExtension
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   238
  {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   239
    override def paintScreenLineRange(gfx: Graphics2D,
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   240
      first_line: Int, last_line: Int, physical_lines: Array[Int],
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   241
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   242
    {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   243
      robust_rendering { rendering =>
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   244
        val ascent = text_area.getPainter.getFontMetrics.getAscent
43376
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   245
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   246
        for (i <- 0 until physical_lines.length) {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   247
          if (physical_lines(i) != -1) {
49843
afddf4e26fac further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents: 49730
diff changeset
   248
            val line_range = Text.Range(start(i), end(i))
43376
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   249
49473
ca7e2c21b104 tuned rendering;
wenzelm
parents: 49424
diff changeset
   250
            // line background color
ca7e2c21b104 tuned rendering;
wenzelm
parents: 49424
diff changeset
   251
            for { (color, separator) <- rendering.line_background(line_range) }
ca7e2c21b104 tuned rendering;
wenzelm
parents: 49424
diff changeset
   252
            {
ca7e2c21b104 tuned rendering;
wenzelm
parents: 49424
diff changeset
   253
              gfx.setColor(color)
49475
8f3a3adadd5a tuned painter;
wenzelm
parents: 49473
diff changeset
   254
              val sep = if (separator) (2 min (line_height / 2)) else 0
8f3a3adadd5a tuned painter;
wenzelm
parents: 49473
diff changeset
   255
              gfx.fillRect(0, y + i * line_height, text_area.getWidth, line_height - sep)
49473
ca7e2c21b104 tuned rendering;
wenzelm
parents: 49424
diff changeset
   256
            }
ca7e2c21b104 tuned rendering;
wenzelm
parents: 49424
diff changeset
   257
46166
4beb2f41ed93 command status color via regular markup;
wenzelm
parents: 45665
diff changeset
   258
            // background color (1)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   259
            for {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   260
              Text.Info(range, color) <- rendering.background1(line_range)
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   261
              r <- JEdit_Lib.gfx_range(text_area, range)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   262
            } {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   263
              gfx.setColor(color)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   264
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   265
            }
43376
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   266
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50306
diff changeset
   267
            // active area -- potentially from other snapshot
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   268
            for {
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50306
diff changeset
   269
              info <- active_area.info
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   270
              Text.Info(range, _) <- info.try_restrict(line_range)
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   271
              r <- JEdit_Lib.gfx_range(text_area, range)
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   272
            } {
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50306
diff changeset
   273
              gfx.setColor(rendering.active_hover_color)
49492
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   274
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   275
            }
2e3e7ea5ce8e some support for hovering and sendback area;
wenzelm
parents: 49475
diff changeset
   276
46166
4beb2f41ed93 command status color via regular markup;
wenzelm
parents: 45665
diff changeset
   277
            // background color (2)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   278
            for {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   279
              Text.Info(range, color) <- rendering.background2(line_range)
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   280
              r <- JEdit_Lib.gfx_range(text_area, range)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   281
            } {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   282
              gfx.setColor(color)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   283
              gfx.fillRect(r.x + 2, y + i * line_height + 2, r.length - 4, line_height - 4)
43376
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   284
            }
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   285
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   286
            // squiggly underline
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   287
            for {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   288
              Text.Info(range, color) <- rendering.squiggly_underline(line_range)
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   289
              r <- JEdit_Lib.gfx_range(text_area, range)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   290
            } {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   291
              gfx.setColor(color)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   292
              val x0 = (r.x / 2) * 2
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   293
              val y0 = r.y + ascent + 1
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   294
              for (x1 <- Range(x0, x0 + r.length, 2)) {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   295
                val y1 = if (x1 % 4 < 2) y0 else y0 + 1
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   296
                gfx.drawLine(x1, y1, x1 + 1, y1)
43376
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   297
              }
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   298
            }
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   299
          }
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   300
        }
0f6880c1c759 some direct text foreground painting, instead of token marking;
wenzelm
parents: 43375
diff changeset
   301
      }
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   302
    }
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   303
  }
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   304
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   305
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   306
  /* text */
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   307
50199
6d04e2422769 quasi-abstract module Rendering, with Isabelle-specific implementation;
wenzelm
parents: 50165
diff changeset
   308
  private def paint_chunk_list(rendering: Rendering,
43505
0aca4edbfa99 clarified chunk.offset, chunk.length;
wenzelm
parents: 43451
diff changeset
   309
    gfx: Graphics2D, line_start: Text.Offset, head: Chunk, x: Float, y: Float): Float =
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   310
  {
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   311
    val clip_rect = gfx.getClipBounds
43392
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   312
    val painter = text_area.getPainter
fe4b8c52b1cc paint caret according to precise font metrics;
wenzelm
parents: 43383
diff changeset
   313
    val font_context = painter.getFontRenderContext
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   314
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   315
    var w = 0.0f
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   316
    var chunk = head
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   317
    while (chunk != null) {
43505
0aca4edbfa99 clarified chunk.offset, chunk.length;
wenzelm
parents: 43451
diff changeset
   318
      val chunk_offset = line_start + chunk.offset
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   319
      if (x + w + chunk.width > clip_rect.x &&
50306
b655d2d0406d updated to jedit-5.0.0;
wenzelm
parents: 50216
diff changeset
   320
          x + w < clip_rect.x + clip_rect.width && chunk.length > 0)
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   321
      {
43505
0aca4edbfa99 clarified chunk.offset, chunk.length;
wenzelm
parents: 43451
diff changeset
   322
        val chunk_range = Text.Range(chunk_offset, chunk_offset + chunk.length)
44662
c8f1d943bfc5 more robust chunk painting wrt. hard tabs, when chunk.str == null;
wenzelm
parents: 44545
diff changeset
   323
        val chunk_str = if (chunk.str == null) " " * chunk.length else chunk.str
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   324
        val chunk_font = chunk.style.getFont
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   325
        val chunk_color = chunk.style.getForegroundColor
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   326
44056
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   327
        def string_width(s: String): Float =
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   328
          if (s.isEmpty) 0.0f
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   329
          else chunk_font.getStringBounds(s, font_context).getWidth.toFloat
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   330
43448
90aec5043461 more robust caret painting wrt. surrogate characters;
wenzelm
parents: 43435
diff changeset
   331
        val caret_range =
50306
b655d2d0406d updated to jedit-5.0.0;
wenzelm
parents: 50216
diff changeset
   332
          if (caret_visible && text_area.isCaretVisible)
49407
215ba6884bdf tuned signature;
wenzelm
parents: 49357
diff changeset
   333
            JEdit_Lib.point_range(buffer, text_area.getCaretPosition)
43448
90aec5043461 more robust caret painting wrt. surrogate characters;
wenzelm
parents: 43435
diff changeset
   334
          else Text.Range(-1)
90aec5043461 more robust caret painting wrt. surrogate characters;
wenzelm
parents: 43435
diff changeset
   335
43426
24e2e2f0032b more explicit treatment of ranges after revert/convert, which may well distort the overall start/end positions;
wenzelm
parents: 43419
diff changeset
   336
        val markup =
43428
b41dea5772c6 more robust treatment of partial range restriction;
wenzelm
parents: 43426
diff changeset
   337
          for {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   338
            r1 <- rendering.text_color(chunk_range, chunk_color)
43450
b6b09fc8d671 proper x1;
wenzelm
parents: 43448
diff changeset
   339
            r2 <- r1.try_restrict(chunk_range)
b6b09fc8d671 proper x1;
wenzelm
parents: 43448
diff changeset
   340
          } yield r2
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   341
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   342
        val padded_markup =
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   343
          if (markup.isEmpty)
46197
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   344
            Iterator(Text.Info(chunk_range, chunk_color))
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   345
          else
46197
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   346
            Iterator(
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   347
              Text.Info(Text.Range(chunk_range.start, markup.head.range.start), chunk_color)) ++
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   348
            markup.iterator ++
46197
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   349
            Iterator(Text.Info(Text.Range(markup.last.range.stop, chunk_range.stop), chunk_color))
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   350
43450
b6b09fc8d671 proper x1;
wenzelm
parents: 43448
diff changeset
   351
        var x1 = x + w
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   352
        gfx.setFont(chunk_font)
46197
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   353
        for (Text.Info(range, color) <- padded_markup if !range.is_singularity) {
44662
c8f1d943bfc5 more robust chunk painting wrt. hard tabs, when chunk.str == null;
wenzelm
parents: 44545
diff changeset
   354
          val str = chunk_str.substring(range.start - chunk_offset, range.stop - chunk_offset)
46197
e4da482283ef tuned text_color: cumulate with explicit default color;
wenzelm
parents: 46178
diff changeset
   355
          gfx.setColor(color)
43448
90aec5043461 more robust caret painting wrt. surrogate characters;
wenzelm
parents: 43435
diff changeset
   356
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   357
          range.try_restrict(caret_range) match {
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   358
            case Some(r) if !r.is_singularity =>
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   359
              val i = r.start - range.start
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   360
              val j = r.stop - range.start
44056
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   361
              val s1 = str.substring(0, i)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   362
              val s2 = str.substring(i, j)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   363
              val s3 = str.substring(j)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   364
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   365
              if (!s1.isEmpty) gfx.drawString(s1, x1, y)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   366
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   367
              val astr = new AttributedString(s2)
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   368
              astr.addAttribute(TextAttribute.FONT, chunk_font)
44056
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   369
              astr.addAttribute(TextAttribute.FOREGROUND, painter.getCaretColor)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   370
              astr.addAttribute(TextAttribute.SWAP_COLORS, TextAttribute.SWAP_COLORS_ON)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   371
              gfx.drawString(astr.getIterator, x1 + string_width(s1), y)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   372
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   373
              if (!s3.isEmpty)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   374
                gfx.drawString(s3, x1 + string_width(str.substring(0, j)), y)
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   375
43759
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   376
            case _ =>
d93a69672362 more uniform padded_markup, which is important for caret visibility despite absence of markup;
wenzelm
parents: 43506
diff changeset
   377
              gfx.drawString(str, x1, y)
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   378
          }
44056
be825a69fc67 less ambitious use of AttributedString, for proper caret painting within \<^sup>\<foobar>;
wenzelm
parents: 43759
diff changeset
   379
          x1 += string_width(str)
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   380
        }
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   381
      }
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   382
      w += chunk.width
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   383
      chunk = chunk.next.asInstanceOf[Chunk]
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   384
    }
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   385
    w
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   386
  }
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   387
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   388
  private val text_painter = new TextAreaExtension
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   389
  {
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   390
    override def paintScreenLineRange(gfx: Graphics2D,
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   391
      first_line: Int, last_line: Int, physical_lines: Array[Int],
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   392
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   393
    {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   394
      robust_rendering { rendering =>
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   395
        val clip = gfx.getClip
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   396
        val x0 = text_area.getHorizontalOffset
43382
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   397
        val fm = text_area.getPainter.getFontMetrics
5d9d34bdec25 misc tuning and simplification;
wenzelm
parents: 43381
diff changeset
   398
        var y0 = y + fm.getHeight - (fm.getLeading + 1) - fm.getDescent
43372
2df2144b0910 use orig_text_painter for extras outside main text (also required to update internal line infos);
wenzelm
parents: 43371
diff changeset
   399
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   400
        for (i <- 0 until physical_lines.length) {
49097
4e5e48c589ea more direct access to all-important chunks for text painting;
wenzelm
parents: 48921
diff changeset
   401
          val line = physical_lines(i)
4e5e48c589ea more direct access to all-important chunks for text painting;
wenzelm
parents: 48921
diff changeset
   402
          if (line != -1) {
4e5e48c589ea more direct access to all-important chunks for text painting;
wenzelm
parents: 48921
diff changeset
   403
            val screen_line = first_line + i
4e5e48c589ea more direct access to all-important chunks for text painting;
wenzelm
parents: 48921
diff changeset
   404
            val chunks = text_area.getChunksOfScreenLine(screen_line)
4e5e48c589ea more direct access to all-important chunks for text painting;
wenzelm
parents: 48921
diff changeset
   405
            if (chunks != null) {
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   406
              val line_start = buffer.getLineStartOffset(line)
49097
4e5e48c589ea more direct access to all-important chunks for text painting;
wenzelm
parents: 48921
diff changeset
   407
              gfx.clipRect(x0, y + line_height * i, Integer.MAX_VALUE, line_height)
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   408
              val w = paint_chunk_list(rendering, gfx, line_start, chunks, x0, y0).toInt
49097
4e5e48c589ea more direct access to all-important chunks for text painting;
wenzelm
parents: 48921
diff changeset
   409
              gfx.clipRect(x0 + w.toInt, 0, Integer.MAX_VALUE, Integer.MAX_VALUE)
4e5e48c589ea more direct access to all-important chunks for text painting;
wenzelm
parents: 48921
diff changeset
   410
              orig_text_painter.paintValidLine(gfx,
4e5e48c589ea more direct access to all-important chunks for text painting;
wenzelm
parents: 48921
diff changeset
   411
                screen_line, line, start(i), end(i), y + line_height * i)
4e5e48c589ea more direct access to all-important chunks for text painting;
wenzelm
parents: 48921
diff changeset
   412
              gfx.setClip(clip)
4e5e48c589ea more direct access to all-important chunks for text painting;
wenzelm
parents: 48921
diff changeset
   413
            }
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   414
          }
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   415
          y0 += line_height
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   416
        }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   417
      }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   418
    }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   419
  }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   420
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   421
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   422
  /* foreground */
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   423
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   424
  private val foreground_painter = new TextAreaExtension
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   425
  {
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   426
    override def paintScreenLineRange(gfx: Graphics2D,
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   427
      first_line: Int, last_line: Int, physical_lines: Array[Int],
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   428
      start: Array[Int], end: Array[Int], y: Int, line_height: Int)
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   429
    {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   430
      robust_rendering { rendering =>
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   431
        for (i <- 0 until physical_lines.length) {
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   432
          if (physical_lines(i) != -1) {
49843
afddf4e26fac further refinement of jEdit line range, avoiding lack of final \n;
wenzelm
parents: 49730
diff changeset
   433
            val line_range = Text.Range(start(i), end(i))
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   434
44545
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   435
            // foreground color
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   436
            for {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   437
              Text.Info(range, color) <- rendering.foreground(line_range)
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   438
              r <- JEdit_Lib.gfx_range(text_area, range)
44545
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   439
            } {
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   440
              gfx.setColor(color)
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   441
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   442
            }
3c40007aa031 transparent foreground color for quoted entities;
wenzelm
parents: 44056
diff changeset
   443
49357
34ac36642a31 more general Document_Model.point_range;
wenzelm
parents: 49356
diff changeset
   444
            // highlight range -- potentially from other snapshot
46205
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   445
            for {
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   446
              info <- highlight_area.info
46205
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   447
              Text.Info(range, color) <- info.try_restrict(line_range)
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   448
              r <- JEdit_Lib.gfx_range(text_area, range)
46205
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   449
            } {
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   450
              gfx.setColor(color)
07e334ad2e2a ignore empty gfx_range;
wenzelm
parents: 46204
diff changeset
   451
              gfx.fillRect(r.x, y + i * line_height, r.length, line_height)
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   452
            }
48921
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents: 47471
diff changeset
   453
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents: 47471
diff changeset
   454
            // hyperlink range -- potentially from other snapshot
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents: 47471
diff changeset
   455
            for {
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   456
              info <- hyperlink_area.info
48921
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents: 47471
diff changeset
   457
              Text.Info(range, _) <- info.try_restrict(line_range)
49409
7140a738aa49 tuned signature;
wenzelm
parents: 49408
diff changeset
   458
              r <- JEdit_Lib.gfx_range(text_area, range)
48921
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents: 47471
diff changeset
   459
            } {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   460
              gfx.setColor(rendering.hyperlink_color)
48921
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents: 47471
diff changeset
   461
              gfx.drawRect(r.x, y + i * line_height, r.length - 1, line_height - 1)
5d8d409b897e support for direct hyperlinks, without the Hyperlinks plugin;
wenzelm
parents: 47471
diff changeset
   462
            }
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   463
          }
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   464
        }
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   465
      }
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   466
    }
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   467
  }
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   468
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   469
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   470
  /* caret -- outside of text range */
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   471
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   472
  private class Caret_Painter(before: Boolean) extends TextAreaExtension
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   473
  {
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   474
    override def paintValidLine(gfx: Graphics2D,
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   475
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   476
    {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   477
      robust_rendering { _ =>
43404
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
   478
        if (before) gfx.clipRect(0, 0, 0, 0)
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
   479
        else gfx.setClip(painter_clip)
c8369f3d88a1 uniform use of Document_View.robust_body;
wenzelm
parents: 43398
diff changeset
   480
      }
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   481
    }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   482
  }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   483
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   484
  private val before_caret_painter1 = new Caret_Painter(true)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   485
  private val after_caret_painter1 = new Caret_Painter(false)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   486
  private val before_caret_painter2 = new Caret_Painter(true)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   487
  private val after_caret_painter2 = new Caret_Painter(false)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   488
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   489
  private val caret_painter = new TextAreaExtension
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   490
  {
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   491
    override def paintValidLine(gfx: Graphics2D,
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   492
      screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   493
    {
49356
6e0c0ffb6ec7 more static handling of rendering options;
wenzelm
parents: 49355
diff changeset
   494
      robust_rendering { _ =>
50306
b655d2d0406d updated to jedit-5.0.0;
wenzelm
parents: 50216
diff changeset
   495
        if (caret_visible && text_area.isCaretVisible) {
43398
c3e2a361b418 more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents: 43396
diff changeset
   496
          val caret = text_area.getCaretPosition
c3e2a361b418 more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents: 43396
diff changeset
   497
          if (start <= caret && caret == end - 1) {
c3e2a361b418 more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents: 43396
diff changeset
   498
            val painter = text_area.getPainter
c3e2a361b418 more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents: 43396
diff changeset
   499
            val fm = painter.getFontMetrics
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   500
43398
c3e2a361b418 more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents: 43396
diff changeset
   501
            val offset = caret - text_area.getLineStartOffset(physical_line)
c3e2a361b418 more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents: 43396
diff changeset
   502
            val x = text_area.offsetToXY(physical_line, offset).x
c3e2a361b418 more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents: 43396
diff changeset
   503
            gfx.setColor(painter.getCaretColor)
50849
70f7483df9cb more uniform Pretty.char_width;
wenzelm
parents: 50657
diff changeset
   504
            gfx.drawRect(x, y, Pretty.char_width_int(fm) - 1, fm.getHeight - 1)
43398
c3e2a361b418 more robust painter_body wrt. EBP races and spurious exceptions (which causes jEdit to remove the extension);
wenzelm
parents: 43396
diff changeset
   505
          }
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   506
        }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   507
      }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   508
    }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   509
  }
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   510
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   511
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   512
  /* activation */
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   513
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   514
  def activate()
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   515
  {
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   516
    val painter = text_area.getPainter
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   517
    painter.addExtension(TextAreaPainter.LOWEST_LAYER, set_state)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   518
    painter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, background_painter)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   519
    painter.addExtension(TextAreaPainter.TEXT_LAYER, text_painter)
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   520
    painter.addExtension(TextAreaPainter.CARET_LAYER - 1, before_caret_painter1)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   521
    painter.addExtension(TextAreaPainter.CARET_LAYER + 1, after_caret_painter1)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   522
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER - 1, before_caret_painter2)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   523
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 1, after_caret_painter2)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   524
    painter.addExtension(TextAreaPainter.BLOCK_CARET_LAYER + 2, caret_painter)
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   525
    painter.addExtension(500, foreground_painter)
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   526
    painter.addExtension(TextAreaPainter.HIGHEST_LAYER, reset_state)
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   527
    painter.removeExtension(orig_text_painter)
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   528
    painter.addMouseListener(mouse_listener)
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   529
    painter.addMouseMotionListener(mouse_motion_listener)
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   530
    text_area.addFocusListener(focus_listener)
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   531
    view.addWindowListener(window_listener)
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   532
  }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   533
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   534
  def deactivate()
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   535
  {
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   536
    val painter = text_area.getPainter
49410
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   537
    view.removeWindowListener(window_listener)
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   538
    text_area.removeFocusListener(focus_listener)
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   539
    painter.removeMouseMotionListener(mouse_motion_listener)
34acbcc33adf tuned signature -- more general Text_Area_Painter operations;
wenzelm
parents: 49409
diff changeset
   540
    painter.removeMouseListener(mouse_listener)
43396
548a68eafaea recovered orig_text_painter from f4141da52e92;
wenzelm
parents: 43393
diff changeset
   541
    painter.addExtension(TextAreaPainter.TEXT_LAYER, orig_text_painter)
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   542
    painter.removeExtension(reset_state)
43435
ae6b0c3e58a8 highlight via foreground painter, using alpha channel;
wenzelm
parents: 43434
diff changeset
   543
    painter.removeExtension(foreground_painter)
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   544
    painter.removeExtension(caret_painter)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   545
    painter.removeExtension(after_caret_painter2)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   546
    painter.removeExtension(before_caret_painter2)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   547
    painter.removeExtension(after_caret_painter1)
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   548
    painter.removeExtension(before_caret_painter1)
43381
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   549
    painter.removeExtension(text_painter)
806878ae2219 separate module for text area painting;
wenzelm
parents: 43376
diff changeset
   550
    painter.removeExtension(background_painter)
43393
f4141da52e92 more precise caret painting, working around existing painter (which is reinstalled by jEdit occasionally);
wenzelm
parents: 43392
diff changeset
   551
    painter.removeExtension(set_state)
43369
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   552
  }
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   553
}
4c86b3405010 separate isabelle.jedit.Text_Painter, which actually replaces the original TextAreaPainter$PaintText instance;
wenzelm
parents:
diff changeset
   554