src/Pure/GUI/gui.scala
author wenzelm
Tue, 30 Dec 2014 11:50:34 +0100
changeset 59201 702e0971d617
parent 59183 ec83638b6bfb
child 59230 cae3ef2897f2
permissions -rw-r--r--
added system property isabelle.laf, notably for initial system dialog; let L&F take care of font scaling, e.g. GTK+ for Linux where X11 dpi scaling does not really work;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53783
f5e9d182f645 clarified location of GUI modules (which depend on Swing of JFX);
wenzelm
parents: 53778
diff changeset
     1
/*  Title:      Pure/GUI/gui.scala
53853
e8430d668f44 more quasi-generic PIDE modules (NB: Swing/JFX needs to be kept separate from non-GUI material);
wenzelm
parents: 53850
diff changeset
     2
    Module:     PIDE-GUI
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     4
53712
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
     5
Basic GUI tools (for AWT/Swing).
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     6
*/
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     7
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     8
package isabelle
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     9
54962
993aab23894c access X11 window manager;
wenzelm
parents: 54709
diff changeset
    10
import java.lang.{ClassLoader, ClassNotFoundException, NoSuchMethodException}
57908
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
    11
import java.io.{FileInputStream, BufferedInputStream}
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
    12
import java.awt.{GraphicsEnvironment, Image, Component, Container, Toolkit, Window, Font,
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
    13
  KeyboardFocusManager}
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
    14
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics}
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
    15
import java.awt.geom.AffineTransform
57879
91e188508bc9 proper layered_pane for JDialog, e.g. relevant for floating dockables in jEdit, for completion popup in text field;
wenzelm
parents: 57639
diff changeset
    16
import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow, JDialog,
56874
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
    17
  JButton, JTextField}
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    18
54709
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
    19
import scala.collection.convert.WrapAsJava
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    20
import scala.swing.{ComboBox, TextArea, ScrollPane}
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    21
import scala.swing.event.SelectionChanged
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    22
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    23
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    24
object GUI
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    25
{
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    26
  /* Swing look-and-feel */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    27
59201
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    28
  def find_laf(name: String): Option[String] =
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    29
    UIManager.getInstalledLookAndFeels().
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    30
      find(c => c.getName == name || c.getClassName == name).
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    31
      map(_.getClassName)
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    32
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    33
  def get_laf(): String =
59201
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    34
    find_laf(System.getProperty("isabelle.laf")) getOrElse {
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    35
      if (Platform.is_windows || Platform.is_macos)
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    36
        UIManager.getSystemLookAndFeelClassName()
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    37
      else
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    38
        find_laf("Nimbus") getOrElse
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    39
          UIManager.getCrossPlatformLookAndFeelClassName()
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    40
    }
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    41
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    42
  def init_laf(): Unit = UIManager.setLookAndFeel(get_laf())
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    43
53848
8d7029eb0c31 disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents: 53786
diff changeset
    44
  def is_macos_laf(): Boolean =
53849
a3177973ca5e proper platform-specific test;
wenzelm
parents: 53848
diff changeset
    45
    Platform.is_macos &&
53848
8d7029eb0c31 disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents: 53786
diff changeset
    46
    UIManager.getSystemLookAndFeelClassName() == UIManager.getLookAndFeel.getClass.getName
8d7029eb0c31 disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents: 53786
diff changeset
    47
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    48
56752
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    49
  /* plain focus traversal, notably for text fields */
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    50
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    51
  def plain_focus_traversal(component: Component)
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    52
  {
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    53
    val dummy_button = new JButton
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    54
    def apply(id: Int): Unit =
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    55
      component.setFocusTraversalKeys(id, dummy_button.getFocusTraversalKeys(id))
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    56
    apply(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS)
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    57
    apply(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS)
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    58
  }
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    59
72b4205f4de9 uniform focus traversal via TAB / Shift-TAB for all fields, in contrast to Java defaults, but in accordance to occasional jEdit practice;
wenzelm
parents: 56622
diff changeset
    60
54962
993aab23894c access X11 window manager;
wenzelm
parents: 54709
diff changeset
    61
  /* X11 window manager */
993aab23894c access X11 window manager;
wenzelm
parents: 54709
diff changeset
    62
993aab23894c access X11 window manager;
wenzelm
parents: 54709
diff changeset
    63
  def window_manager(): Option[String] =
993aab23894c access X11 window manager;
wenzelm
parents: 54709
diff changeset
    64
  {
54965
a8af7a9c38d1 more robust;
wenzelm
parents: 54962
diff changeset
    65
    if (Platform.is_windows || Platform.is_macos) None
a8af7a9c38d1 more robust;
wenzelm
parents: 54962
diff changeset
    66
    else
a8af7a9c38d1 more robust;
wenzelm
parents: 54962
diff changeset
    67
      try {
59080
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 57908
diff changeset
    68
        val wm =
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 57908
diff changeset
    69
          Untyped.method(Class.forName("sun.awt.X11.XWM", true, ClassLoader.getSystemClassLoader),
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 57908
diff changeset
    70
            "getWM").invoke(null)
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 57908
diff changeset
    71
        if (wm == null) None
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 57908
diff changeset
    72
        else Some(wm.toString)
54962
993aab23894c access X11 window manager;
wenzelm
parents: 54709
diff changeset
    73
      }
54965
a8af7a9c38d1 more robust;
wenzelm
parents: 54962
diff changeset
    74
      catch {
a8af7a9c38d1 more robust;
wenzelm
parents: 54962
diff changeset
    75
        case _: ClassNotFoundException => None
a8af7a9c38d1 more robust;
wenzelm
parents: 54962
diff changeset
    76
        case _: NoSuchMethodException => None
a8af7a9c38d1 more robust;
wenzelm
parents: 54962
diff changeset
    77
      }
54962
993aab23894c access X11 window manager;
wenzelm
parents: 54709
diff changeset
    78
  }
993aab23894c access X11 window manager;
wenzelm
parents: 54709
diff changeset
    79
993aab23894c access X11 window manager;
wenzelm
parents: 54709
diff changeset
    80
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    81
  /* simple dialogs */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    82
53714
89fb20ae9b73 limit for text height;
wenzelm
parents: 53712
diff changeset
    83
  def scrollable_text(txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false)
89fb20ae9b73 limit for text height;
wenzelm
parents: 53712
diff changeset
    84
    : ScrollPane =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    85
  {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    86
    val text = new TextArea(txt)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    87
    if (width > 0) text.columns = width
53714
89fb20ae9b73 limit for text height;
wenzelm
parents: 53712
diff changeset
    88
    if (height > 0 && split_lines(txt).length > height) text.rows = height
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    89
    text.editable = editable
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    90
    new ScrollPane(text)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    91
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    92
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    93
  private def simple_dialog(kind: Int, default_title: String,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    94
    parent: Component, title: String, message: Seq[Any])
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    95
  {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57044
diff changeset
    96
    GUI_Thread.now {
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    97
      val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    98
      JOptionPane.showMessageDialog(parent,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    99
        java_message.toArray.asInstanceOf[Array[AnyRef]],
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   100
        if (title == null) default_title else title, kind)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   101
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   102
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   103
57639
wenzelm
parents: 57612
diff changeset
   104
  def dialog(parent: Component, title: String, message: Any*): Unit =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   105
    simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   106
57639
wenzelm
parents: 57612
diff changeset
   107
  def warning_dialog(parent: Component, title: String, message: Any*): Unit =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   108
    simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   109
57639
wenzelm
parents: 57612
diff changeset
   110
  def error_dialog(parent: Component, title: String, message: Any*): Unit =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   111
    simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   112
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   113
  def confirm_dialog(parent: Component, title: String, option_type: Int, message: Any*): Int =
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57044
diff changeset
   114
    GUI_Thread.now {
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   115
      val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   116
      JOptionPane.showConfirmDialog(parent,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   117
        java_message.toArray.asInstanceOf[Array[AnyRef]], title,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   118
          option_type, JOptionPane.QUESTION_MESSAGE)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   119
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   120
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   121
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   122
  /* zoom box */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   123
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   124
  private val Zoom_Factor = "([0-9]+)%?".r
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   125
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   126
  abstract class Zoom_Box extends ComboBox[String](
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   127
    List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%"))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   128
  {
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   129
    def changed: Unit
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   130
    def factor: Int = parse(selection.item)
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   131
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   132
    private def parse(text: String): Int =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   133
      text match {
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   134
        case Zoom_Factor(s) =>
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   135
          val i = Integer.parseInt(s)
56874
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   136
          if (10 <= i && i < 1000) i else 100
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   137
        case _ => 100
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   138
      }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   139
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   140
    private def print(i: Int): String = i.toString + "%"
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   141
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   142
    def set_item(i: Int) {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   143
      peer.getEditor match {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   144
        case null =>
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   145
        case editor => editor.setItem(print(i))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   146
      }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   147
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   148
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   149
    makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x))
56874
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   150
    peer.getEditor.getEditorComponent match {
56888
3e8cbb624cc5 tuned GUI for Windows L&F;
wenzelm
parents: 56874
diff changeset
   151
      case text: JTextField => text.setColumns(4)
56874
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   152
      case _ =>
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   153
    }
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   154
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   155
    selection.index = 3
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   156
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   157
    listenTo(selection)
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   158
    reactions += { case SelectionChanged(_) => changed }
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   159
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   160
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   161
53786
wenzelm
parents: 53785
diff changeset
   162
  /* tooltip with multi-line support */
wenzelm
parents: 53785
diff changeset
   163
56622
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 54965
diff changeset
   164
  def tooltip_lines(text: String): String =
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 54965
diff changeset
   165
    if (text == null || text == "") null
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 54965
diff changeset
   166
    else "<html>" + HTML.encode(text) + "</html>"
53786
wenzelm
parents: 53785
diff changeset
   167
wenzelm
parents: 53785
diff changeset
   168
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   169
  /* icon */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   170
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   171
  def isabelle_icon(): ImageIcon =
54676
6b2ca4850b71 uniform use of transparent icons, as for main "apps";
wenzelm
parents: 54659
diff changeset
   172
    new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle_transparent-32.gif"))
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   173
54709
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   174
  def isabelle_icons(): List[ImageIcon] =
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   175
    for (icon <- List("isabelle/isabelle_transparent-32.gif", "isabelle/isabelle_transparent.gif"))
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   176
      yield new ImageIcon(getClass.getClassLoader.getResource(icon))
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   177
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   178
  def isabelle_image(): Image = isabelle_icon().getImage
53712
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   179
54709
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   180
  def isabelle_images(): java.util.List[Image] =
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   181
    WrapAsJava.seqAsJavaList(isabelle_icons.map(_.getImage))
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   182
53712
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   183
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   184
  /* component hierachy */
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   185
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   186
  def get_parent(component: Component): Option[Container] =
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   187
    component.getParent match {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   188
      case null => None
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   189
      case parent => Some(parent)
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   190
    }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   191
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   192
  def ancestors(component: Component): Iterator[Container] = new Iterator[Container] {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   193
    private var next_elem = get_parent(component)
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   194
    def hasNext(): Boolean = next_elem.isDefined
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   195
    def next(): Container =
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   196
      next_elem match {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   197
        case Some(parent) =>
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   198
          next_elem = get_parent(parent)
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   199
          parent
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   200
        case None => Iterator.empty.next()
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   201
      }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   202
  }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   203
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   204
  def parent_window(component: Component): Option[Window] =
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   205
    ancestors(component).collectFirst({ case x: Window => x })
53778
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   206
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   207
  def layered_pane(component: Component): Option[JLayeredPane] =
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   208
    parent_window(component) match {
57879
91e188508bc9 proper layered_pane for JDialog, e.g. relevant for floating dockables in jEdit, for completion popup in text field;
wenzelm
parents: 57639
diff changeset
   209
      case Some(w: JWindow) => Some(w.getLayeredPane)
91e188508bc9 proper layered_pane for JDialog, e.g. relevant for floating dockables in jEdit, for completion popup in text field;
wenzelm
parents: 57639
diff changeset
   210
      case Some(w: JFrame) => Some(w.getLayeredPane)
91e188508bc9 proper layered_pane for JDialog, e.g. relevant for floating dockables in jEdit, for completion popup in text field;
wenzelm
parents: 57639
diff changeset
   211
      case Some(w: JDialog) => Some(w.getLayeredPane)
53778
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   212
      case _ => None
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   213
    }
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   214
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   215
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   216
  /* font operations */
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   217
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   218
  def font_metrics(font: Font): LineMetrics =
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   219
    font.getLineMetrics("", new FontRenderContext(null, false, false))
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   220
54368
36dc6aa4fe87 less tiny font for search criteria;
wenzelm
parents: 53853
diff changeset
   221
  def imitate_font(family: String, font: Font, scale: Double = 1.0): Font =
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   222
  {
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   223
    val font1 = new Font(family, font.getStyle, font.getSize)
59183
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   224
    val size = font_metrics(font).getHeight.toDouble / font_metrics(font1).getHeight * font.getSize
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   225
    font1.deriveFont((scale * size).toInt)
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   226
  }
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   227
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   228
  def imitate_font_css(family: String, font: Font, scale: Double = 1.0): String =
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   229
  {
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   230
    val font1 = new Font(family, font.getStyle, font.getSize)
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   231
    val rel_size = font_metrics(font).getHeight.toDouble / font_metrics(font1).getHeight
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   232
    "font-family: " + family + "; font-size: " + (scale * rel_size * 100).toInt + "%;"
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   233
  }
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   234
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   235
  def transform_font(font: Font, transform: AffineTransform): Font =
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   236
  {
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   237
    import scala.collection.JavaConversions._
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   238
    font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform)))
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   239
  }
57908
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
   240
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
   241
  def font(family: String = "IsabelleText", size: Int = 1, bold: Boolean = false): Font =
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
   242
    new Font(family, if (bold) Font.BOLD else Font.PLAIN, size)
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
   243
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
   244
  def install_fonts()
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
   245
  {
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
   246
    val ge = GraphicsEnvironment.getLocalGraphicsEnvironment()
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
   247
    for (font <- Path.split(Isabelle_System.getenv_strict("ISABELLE_FONTS")))
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
   248
      ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, font.file))
1937603dbdf2 separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents: 57879
diff changeset
   249
  }
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   250
}
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   251