src/Pure/GUI/gui.scala
author wenzelm
Sun, 29 Nov 2020 23:23:32 +0100
changeset 73023 21ff9c1a4644
parent 71570 21a41356d78f
child 73214 3afd293347cc
permissions -rw-r--r--
more Isabelle fonts, notably for PanelWindowContainer buttons with Windows L&F;
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
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     3
53712
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
     4
Basic GUI tools (for AWT/Swing).
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     5
*/
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     6
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     7
package isabelle
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     8
54962
993aab23894c access X11 window manager;
wenzelm
parents: 54709
diff changeset
     9
import java.lang.{ClassLoader, ClassNotFoundException, NoSuchMethodException}
69378
6f360600eabc clarified signature: fonts are not dependent on GUI;
wenzelm
parents: 69370
diff changeset
    10
import java.awt.{Image, Component, Container, Toolkit, Window, Font, KeyboardFocusManager}
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
    11
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics}
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
    12
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
    13
import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow, JDialog,
69387
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
    14
  JButton, JTextField, JLabel}
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    15
71566
7f2cd237ee4f tuned -- more direct java.util.Map.of;
wenzelm
parents: 69394
diff changeset
    16
7f2cd237ee4f tuned -- more direct java.util.Map.of;
wenzelm
parents: 69394
diff changeset
    17
import scala.collection.JavaConverters
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    18
import scala.swing.{ComboBox, TextArea, ScrollPane}
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    19
import scala.swing.event.SelectionChanged
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    20
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    21
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    22
object GUI
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    23
{
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    24
  /* Swing look-and-feel */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    25
59201
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    26
  def find_laf(name: String): Option[String] =
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    27
    UIManager.getInstalledLookAndFeels().
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    28
      find(c => c.getName == name || c.getClassName == name).
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    29
      map(_.getClassName)
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    30
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    31
  def get_laf(): String =
59201
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    32
    find_laf(System.getProperty("isabelle.laf")) getOrElse {
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    33
      if (Platform.is_windows || Platform.is_macos)
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    34
        UIManager.getSystemLookAndFeelClassName()
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    35
      else
61530
82fc5a6231a2 back to traditional Metal as default, and thus evade current problems with Nimbus scrollbar slider;
wenzelm
parents: 61523
diff changeset
    36
        UIManager.getCrossPlatformLookAndFeelClassName()
59201
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    37
    }
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    38
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    39
  def init_laf(): Unit = UIManager.setLookAndFeel(get_laf())
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    40
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
    41
  def is_macos_laf(): Boolean =
53849
a3177973ca5e proper platform-specific test;
wenzelm
parents: 53848
diff changeset
    42
    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
    43
    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
    44
60999
42cebb02b5ae more scalable GUI;
wenzelm
parents: 60040
diff changeset
    45
  def is_windows_laf(): Boolean =
42cebb02b5ae more scalable GUI;
wenzelm
parents: 60040
diff changeset
    46
    Platform.is_windows &&
42cebb02b5ae more scalable GUI;
wenzelm
parents: 60040
diff changeset
    47
    UIManager.getSystemLookAndFeelClassName() == UIManager.getLookAndFeel.getClass.getName
42cebb02b5ae more scalable GUI;
wenzelm
parents: 60040
diff changeset
    48
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    49
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
    50
  /* 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
    51
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
  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
    53
  {
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
    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
    55
    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
    56
      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
    57
    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
    58
    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
    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
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
    61
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    62
  /* simple dialogs */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    63
59671
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 59286
diff changeset
    64
  def scrollable_text(raw_txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false)
53714
89fb20ae9b73 limit for text height;
wenzelm
parents: 53712
diff changeset
    65
    : ScrollPane =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    66
  {
59671
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 59286
diff changeset
    67
    val txt = Output.clean_yxml(raw_txt)
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    68
    val text = new TextArea(txt)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    69
    if (width > 0) text.columns = width
53714
89fb20ae9b73 limit for text height;
wenzelm
parents: 53712
diff changeset
    70
    if (height > 0 && split_lines(txt).length > height) text.rows = height
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    71
    text.editable = editable
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    72
    new ScrollPane(text)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    73
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    74
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    75
  private def simple_dialog(kind: Int, default_title: String,
65373
wenzelm
parents: 65083
diff changeset
    76
    parent: Component, title: String, message: Iterable[Any])
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    77
  {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57044
diff changeset
    78
    GUI_Thread.now {
65373
wenzelm
parents: 65083
diff changeset
    79
      val java_message =
wenzelm
parents: 65083
diff changeset
    80
        message.iterator.map({ case x: scala.swing.Component => x.peer case x => x }).
wenzelm
parents: 65083
diff changeset
    81
          toArray.asInstanceOf[Array[AnyRef]]
wenzelm
parents: 65083
diff changeset
    82
      JOptionPane.showMessageDialog(parent, java_message,
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    83
        if (title == null) default_title else title, kind)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    84
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    85
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    86
57639
wenzelm
parents: 57612
diff changeset
    87
  def dialog(parent: Component, title: String, message: Any*): Unit =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    88
    simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    89
57639
wenzelm
parents: 57612
diff changeset
    90
  def warning_dialog(parent: Component, title: String, message: Any*): Unit =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    91
    simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    92
57639
wenzelm
parents: 57612
diff changeset
    93
  def error_dialog(parent: Component, title: String, message: Any*): Unit =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    94
    simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    95
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    96
  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
    97
    GUI_Thread.now {
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    98
      val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    99
      JOptionPane.showConfirmDialog(parent,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   100
        java_message.toArray.asInstanceOf[Array[AnyRef]], title,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   101
          option_type, JOptionPane.QUESTION_MESSAGE)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   102
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   103
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   104
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   105
  /* zoom box */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   106
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   107
  private val Zoom_Factor = "([0-9]+)%?".r
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   108
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   109
  abstract class Zoom_Box extends ComboBox[String](
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   110
    List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%"))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   111
  {
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   112
    def changed: Unit
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   113
    def factor: Int = parse(selection.item)
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   114
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   115
    private def parse(text: String): Int =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   116
      text match {
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   117
        case Zoom_Factor(s) =>
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   118
          val i = Integer.parseInt(s)
56874
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   119
          if (10 <= i && i < 1000) i else 100
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   120
        case _ => 100
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   121
      }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   122
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   123
    private def print(i: Int): String = i.toString + "%"
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   124
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   125
    def set_item(i: Int) {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   126
      peer.getEditor match {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   127
        case null =>
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   128
        case editor => editor.setItem(print(i))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   129
      }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   130
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   131
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   132
    makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x))
56874
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   133
    peer.getEditor.getEditorComponent match {
56888
3e8cbb624cc5 tuned GUI for Windows L&F;
wenzelm
parents: 56874
diff changeset
   134
      case text: JTextField => text.setColumns(4)
56874
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   135
      case _ =>
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   136
    }
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   137
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   138
    selection.index = 3
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   139
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   140
    listenTo(selection)
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   141
    reactions += { case SelectionChanged(_) => changed }
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   142
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   143
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   144
53786
wenzelm
parents: 53785
diff changeset
   145
  /* tooltip with multi-line support */
wenzelm
parents: 53785
diff changeset
   146
56622
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 54965
diff changeset
   147
  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
   148
    if (text == null || text == "") null
62113
16de2a9b5b3d tuned -- according to ML version;
wenzelm
parents: 61742
diff changeset
   149
    else "<html>" + HTML.output(text) + "</html>"
53786
wenzelm
parents: 53785
diff changeset
   150
wenzelm
parents: 53785
diff changeset
   151
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   152
  /* icon */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   153
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   154
  def isabelle_icon(): ImageIcon =
54676
6b2ca4850b71 uniform use of transparent icons, as for main "apps";
wenzelm
parents: 54659
diff changeset
   155
    new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle_transparent-32.gif"))
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   156
54709
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   157
  def isabelle_icons(): List[ImageIcon] =
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   158
    for (icon <- List("isabelle/isabelle_transparent-32.gif", "isabelle/isabelle_transparent.gif"))
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   159
      yield new ImageIcon(getClass.getClassLoader.getResource(icon))
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   160
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   161
  def isabelle_image(): Image = isabelle_icon().getImage
53712
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   162
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   163
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   164
  /* component hierachy */
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   165
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   166
  def get_parent(component: Component): Option[Container] =
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   167
    component.getParent match {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   168
      case null => None
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   169
      case parent => Some(parent)
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   170
    }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   171
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   172
  def ancestors(component: Component): Iterator[Container] = new Iterator[Container] {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   173
    private var next_elem = get_parent(component)
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   174
    def hasNext(): Boolean = next_elem.isDefined
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   175
    def next(): Container =
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   176
      next_elem match {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   177
        case Some(parent) =>
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   178
          next_elem = get_parent(parent)
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   179
          parent
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   180
        case None => Iterator.empty.next()
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   181
      }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   182
  }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   183
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   184
  def parent_window(component: Component): Option[Window] =
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   185
    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
   186
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
   187
  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
   188
    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
   189
      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
   190
      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
   191
      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
   192
      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
   193
    }
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   194
63874
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   195
  def traverse_components(component: Component, apply: Component => Unit)
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   196
  {
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   197
    def traverse(comp: Component)
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   198
    {
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   199
      apply(comp)
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   200
      comp match {
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   201
        case cont: Container =>
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   202
          for (i <- 0 until cont.getComponentCount)
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   203
            traverse(cont.getComponent(i))
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   204
        case _ =>
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   205
      }
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   206
    }
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   207
    traverse(component)
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   208
  }
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   209
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   210
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   211
  /* font operations */
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   212
61742
fd3b214b0979 clarified font: GUI defaults might change dynamically;
wenzelm
parents: 61530
diff changeset
   213
  def copy_font(font: Font): Font =
fd3b214b0979 clarified font: GUI defaults might change dynamically;
wenzelm
parents: 61530
diff changeset
   214
    if (font == null) null
fd3b214b0979 clarified font: GUI defaults might change dynamically;
wenzelm
parents: 61530
diff changeset
   215
    else new Font(font.getFamily, font.getStyle, font.getSize)
fd3b214b0979 clarified font: GUI defaults might change dynamically;
wenzelm
parents: 61530
diff changeset
   216
59230
cae3ef2897f2 tuned signature;
wenzelm
parents: 59201
diff changeset
   217
  def line_metrics(font: Font): LineMetrics =
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   218
    font.getLineMetrics("", new FontRenderContext(null, false, false))
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   219
69386
wenzelm
parents: 69378
diff changeset
   220
  def transform_font(font: Font, transform: AffineTransform): Font =
71566
7f2cd237ee4f tuned -- more direct java.util.Map.of;
wenzelm
parents: 69394
diff changeset
   221
    font.deriveFont(java.util.Map.of(TextAttribute.TRANSFORM, new TransformAttribute(transform)))
69386
wenzelm
parents: 69378
diff changeset
   222
wenzelm
parents: 69378
diff changeset
   223
  def font(family: String = Isabelle_Fonts.sans, size: Int = 1, bold: Boolean = false): Font =
wenzelm
parents: 69378
diff changeset
   224
    new Font(family, if (bold) Font.BOLD else Font.PLAIN, size)
wenzelm
parents: 69378
diff changeset
   225
69387
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   226
  def label_font(): Font = (new JLabel).getFont
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   227
69386
wenzelm
parents: 69378
diff changeset
   228
wenzelm
parents: 69378
diff changeset
   229
  /* Isabelle fonts */
wenzelm
parents: 69378
diff changeset
   230
69368
71ef6e6da3dc prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents: 69367
diff changeset
   231
  def imitate_font(font: Font,
71ef6e6da3dc prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents: 69367
diff changeset
   232
    family: String = Isabelle_Fonts.sans,
71ef6e6da3dc prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents: 69367
diff changeset
   233
    scale: Double = 1.0): Font =
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   234
  {
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   235
    val font1 = new Font(family, font.getStyle, font.getSize)
59286
ac74eedb910a GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents: 59230
diff changeset
   236
    val rel_size = line_metrics(font).getHeight.toDouble / line_metrics(font1).getHeight
ac74eedb910a GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents: 59230
diff changeset
   237
    new Font(family, font.getStyle, (scale * rel_size * font.getSize).toInt)
59183
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   238
  }
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   239
69368
71ef6e6da3dc prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents: 69367
diff changeset
   240
  def imitate_font_css(font: Font,
71ef6e6da3dc prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents: 69367
diff changeset
   241
    family: String = Isabelle_Fonts.sans,
71ef6e6da3dc prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents: 69367
diff changeset
   242
    scale: Double = 1.0): String =
59183
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   243
  {
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   244
    val font1 = new Font(family, font.getStyle, font.getSize)
59230
cae3ef2897f2 tuned signature;
wenzelm
parents: 59201
diff changeset
   245
    val rel_size = line_metrics(font).getHeight.toDouble / line_metrics(font1).getHeight
59183
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   246
    "font-family: " + family + "; font-size: " + (scale * rel_size * 100).toInt + "%;"
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   247
  }
69387
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   248
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   249
  def use_isabelle_fonts()
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   250
  {
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   251
    val default_font = label_font()
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   252
    val ui = UIManager.getDefaults
69394
0c7d8b1b6594 more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents: 69388
diff changeset
   253
    for (prop <- List(
73023
21ff9c1a4644 more Isabelle fonts, notably for PanelWindowContainer buttons with Windows L&F;
wenzelm
parents: 71570
diff changeset
   254
      "ToggleButton.font",
69394
0c7d8b1b6594 more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents: 69388
diff changeset
   255
      "CheckBoxMenuItem.font",
0c7d8b1b6594 more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents: 69388
diff changeset
   256
      "Label.font",
0c7d8b1b6594 more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents: 69388
diff changeset
   257
      "Menu.font",
0c7d8b1b6594 more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents: 69388
diff changeset
   258
      "MenuItem.font",
0c7d8b1b6594 more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents: 69388
diff changeset
   259
      "PopupMenu.font",
71569
fcf5ee85743d more Isabelle fonts, notably for File Browser title in GTK L&F;
wenzelm
parents: 71566
diff changeset
   260
      "Table.font",
fcf5ee85743d more Isabelle fonts, notably for File Browser title in GTK L&F;
wenzelm
parents: 71566
diff changeset
   261
      "TableHeader.font",
69394
0c7d8b1b6594 more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents: 69388
diff changeset
   262
      "TextArea.font",
0c7d8b1b6594 more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents: 69388
diff changeset
   263
      "TextField.font",
0c7d8b1b6594 more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents: 69388
diff changeset
   264
      "TextPane.font",
71570
21a41356d78f proper name;
wenzelm
parents: 71569
diff changeset
   265
      "ToolTip.font",
69394
0c7d8b1b6594 more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents: 69388
diff changeset
   266
      "Tree.font"))
69387
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   267
    {
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   268
      val font = ui.get(prop) match { case font: Font => font case _ => default_font }
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   269
      ui.put(prop, GUI.imitate_font(font))
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   270
    }
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69386
diff changeset
   271
  }
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   272
}