src/Pure/GUI/gui.scala
author wenzelm
Thu, 26 Dec 2024 15:38:57 +0100
changeset 81659 a904fcbbbdbc
parent 81657 4210fd10e776
child 81661 98ecc0ed6af1
permissions -rw-r--r--
clarified signature;
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
73909
1d0d9772fff0 tuned imports;
wenzelm
parents: 73878
diff changeset
     9
import java.util.{Map => JMap}
73037
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
    10
import java.awt.{Component, Container, Font, Image, Insets, KeyboardFocusManager, Window, Point,
73117
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    11
  Rectangle, Dimension, GraphicsEnvironment, MouseInfo, Toolkit}
76494
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
    12
import java.awt.event.{KeyAdapter, KeyEvent, ItemListener, ItemEvent}
72974
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
    13
import java.awt.font.{FontRenderContext, LineMetrics, TextAttribute, TransformAttribute}
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
    14
import java.awt.geom.AffineTransform
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents: 81322
diff changeset
    15
import javax.swing.{ImageIcon, JButton, JDialog, JFrame, JLabel, JLayeredPane, JOptionPane,
80553
wenzelm
parents: 76789
diff changeset
    16
  RootPaneContainer, JTextField, JWindow, JComboBox, LookAndFeel, UIManager, SwingUtilities}
76492
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
    17
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
    18
import scala.swing.{CheckBox, ComboBox, ScrollPane, TextArea, ListView, Label, Separator,
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
    19
  Orientation}
75852
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
    20
import scala.swing.event.{ButtonClicked, SelectionChanged}
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    21
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    22
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    23
object GUI {
73116
b84887a67cc6 clarified default L&F of Isabelle/Scala (not Isabelle/jEdit);
wenzelm
parents: 73111
diff changeset
    24
  /* Swing look-and-feel */
59201
702e0971d617 added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents: 59183
diff changeset
    25
73878
291597140695 avoid deprecated operation;
wenzelm
parents: 73367
diff changeset
    26
  def init_laf(): Unit = com.formdev.flatlaf.FlatLightLaf.setup()
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    27
81335
fe32eaea366c clarified signature;
wenzelm
parents: 81333
diff changeset
    28
  def current_laf(): String = UIManager.getLookAndFeel.getClass.getName()
72975
315f9b4f9e7a tuned signature;
wenzelm
parents: 72974
diff changeset
    29
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
    30
  def is_macos_laf: Boolean =
81335
fe32eaea366c clarified signature;
wenzelm
parents: 81333
diff changeset
    31
    Platform.is_macos && UIManager.getSystemLookAndFeelClassName() == current_laf()
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
    32
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    33
  class Look_And_Feel(laf: LookAndFeel) extends Isabelle_System.Service {
73117
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    34
    def info: UIManager.LookAndFeelInfo =
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    35
      new UIManager.LookAndFeelInfo(laf.getName, laf.getClass.getName)
73111
01f4965fd09b support for additional look-and-feels;
wenzelm
parents: 73095
diff changeset
    36
  }
01f4965fd09b support for additional look-and-feels;
wenzelm
parents: 73095
diff changeset
    37
01f4965fd09b support for additional look-and-feels;
wenzelm
parents: 73095
diff changeset
    38
  lazy val look_and_feels: List[Look_And_Feel] =
01f4965fd09b support for additional look-and-feels;
wenzelm
parents: 73095
diff changeset
    39
    Isabelle_System.make_services(classOf[Look_And_Feel])
01f4965fd09b support for additional look-and-feels;
wenzelm
parents: 73095
diff changeset
    40
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    41
  def init_lafs(): Unit = {
73117
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    42
    val old_lafs =
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    43
      Set(
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    44
        "com.sun.java.swing.plaf.motif.MotifLookAndFeel",
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    45
        "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel")
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    46
    val lafs =
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    47
      UIManager.getInstalledLookAndFeels().toList
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    48
        .filterNot(info => old_lafs(info.getClassName))
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    49
    val more_lafs = look_and_feels.map(_.info)
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    50
    UIManager.setInstalledLookAndFeels((more_lafs ::: lafs).toArray)
81322
0521e65af41e tuned GUI;
wenzelm
parents: 81320
diff changeset
    51
0521e65af41e tuned GUI;
wenzelm
parents: 81320
diff changeset
    52
    // see https://www.formdev.com/flatlaf/customizing
0521e65af41e tuned GUI;
wenzelm
parents: 81320
diff changeset
    53
    UIManager.put("Component.arrowType", "triangle")
73117
6a6e987552c7 clarified list of presented look-and-feels;
wenzelm
parents: 73116
diff changeset
    54
  }
73111
01f4965fd09b support for additional look-and-feels;
wenzelm
parents: 73095
diff changeset
    55
01f4965fd09b support for additional look-and-feels;
wenzelm
parents: 73095
diff changeset
    56
73116
b84887a67cc6 clarified default L&F of Isabelle/Scala (not Isabelle/jEdit);
wenzelm
parents: 73111
diff changeset
    57
  /* additional look-and-feels */
b84887a67cc6 clarified default L&F of Isabelle/Scala (not Isabelle/jEdit);
wenzelm
parents: 73111
diff changeset
    58
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
    59
  /* 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
    60
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    61
  def plain_focus_traversal(component: Component): Unit = {
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
    62
    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
    63
    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
    64
      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
    65
    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
    66
    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
    67
  }
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
    68
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
    69
81655
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    70
  /* style */
81648
c98cfdcb2df0 clarified signature;
wenzelm
parents: 81382
diff changeset
    71
81655
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    72
  class Style {
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    73
    def enclose(body: String): String = body
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    74
    def make_text(str: String): String = str
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    75
    def make_bold(str: String): String = str
81657
4210fd10e776 clarified signature;
wenzelm
parents: 81655
diff changeset
    76
    def enclose_text(str: String): String = enclose(make_text(str))
4210fd10e776 clarified signature;
wenzelm
parents: 81655
diff changeset
    77
    def enclose_bold(str: String): String = enclose(make_bold(str))
81659
a904fcbbbdbc clarified signature;
wenzelm
parents: 81657
diff changeset
    78
    def spaces(n: Int): String = Symbol.spaces(n)
81655
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    79
  }
81649
904b2144e9c5 more GUI styles;
wenzelm
parents: 81648
diff changeset
    80
81655
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    81
  class Style_HTML extends Style {
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    82
    override def enclose(body: String): String = "<html>" + body + "</html>"
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    83
    override def make_text(str: String): String = HTML.output(str)
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    84
    override def make_bold(str: String): String = "<b>" + make_text(str) + "</b>"
81659
a904fcbbbdbc clarified signature;
wenzelm
parents: 81657
diff changeset
    85
    override def spaces(n: Int): String = HTML.spaces(n)
81655
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    86
  }
81654
f13d04506126 more robust: proper HTML.output;
wenzelm
parents: 81650
diff changeset
    87
81655
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    88
  abstract class Style_Symbol extends Style {
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    89
    def bold: String
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    90
    override def make_bold(str: String): String =
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    91
      Symbol.iterator(str)
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    92
        .flatMap(s => if (Symbol.is_controllable(s)) List(bold, s) else List(s))
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    93
        .mkString
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    94
  }
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    95
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    96
  object Style_Plain extends Style { override def toString: String = "plain" }
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    97
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    98
  object Style_HTML extends Style_HTML { override def toString: String = "html" }
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
    99
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   100
  object Style_Symbol_Encoded extends Style_Symbol {
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   101
    override def toString: String = "symbol_encoded"
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   102
    override def bold: String = Symbol.bold
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   103
  }
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   104
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   105
  object Style_Symbol_Decoded extends Style_Symbol {
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   106
    override def toString: String = "symbol_decoded"
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   107
    override def bold: String = Symbol.bold_decoded
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   108
  }
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   109
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   110
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   111
  /* named items */
81649
904b2144e9c5 more GUI styles;
wenzelm
parents: 81648
diff changeset
   112
904b2144e9c5 more GUI styles;
wenzelm
parents: 81648
diff changeset
   113
  sealed case class Name(
904b2144e9c5 more GUI styles;
wenzelm
parents: 81648
diff changeset
   114
    name: String,
904b2144e9c5 more GUI styles;
wenzelm
parents: 81648
diff changeset
   115
    kind: String = "",
904b2144e9c5 more GUI styles;
wenzelm
parents: 81648
diff changeset
   116
    prefix: String = "",
81655
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   117
    style: Style = Style_Plain
81649
904b2144e9c5 more GUI styles;
wenzelm
parents: 81648
diff changeset
   118
  ) {
81648
c98cfdcb2df0 clarified signature;
wenzelm
parents: 81382
diff changeset
   119
    override def toString: String = {
c98cfdcb2df0 clarified signature;
wenzelm
parents: 81382
diff changeset
   120
      val a = kind.nonEmpty
c98cfdcb2df0 clarified signature;
wenzelm
parents: 81382
diff changeset
   121
      val b = name.nonEmpty
81655
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   122
      style.make_text(prefix) +
81654
f13d04506126 more robust: proper HTML.output;
wenzelm
parents: 81650
diff changeset
   123
        if_proper(a || b,
81655
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   124
          if_proper(prefix, ": ") + if_proper(kind, style.make_bold(kind)) +
775514416939 clarified signature;
wenzelm
parents: 81654
diff changeset
   125
          if_proper(a && b, " ") + if_proper(b, style.make_text(quote(name))))
81648
c98cfdcb2df0 clarified signature;
wenzelm
parents: 81382
diff changeset
   126
    }
c98cfdcb2df0 clarified signature;
wenzelm
parents: 81382
diff changeset
   127
  }
c98cfdcb2df0 clarified signature;
wenzelm
parents: 81382
diff changeset
   128
c98cfdcb2df0 clarified signature;
wenzelm
parents: 81382
diff changeset
   129
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   130
  /* simple dialogs */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   131
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   132
  def scrollable_text(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   133
    raw_txt: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   134
    width: Int = 60,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   135
    height: Int = 20,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   136
    editable: Boolean = false
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   137
  ) : ScrollPane = {
80817
e31ebb2be437 clarified signature;
wenzelm
parents: 80553
diff changeset
   138
    val txt = Protocol_Message.clean_output(raw_txt)
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   139
    val text = new TextArea(txt)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   140
    if (width > 0) text.columns = width
53714
89fb20ae9b73 limit for text height;
wenzelm
parents: 53712
diff changeset
   141
    if (height > 0 && split_lines(txt).length > height) text.rows = height
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   142
    text.editable = editable
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   143
    new ScrollPane(text)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   144
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   145
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   146
  private def simple_dialog(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   147
    kind: Int,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   148
    default_title: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   149
    parent: Component,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   150
    title: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   151
    message: Iterable[Any]
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   152
  ): Unit = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57044
diff changeset
   153
    GUI_Thread.now {
65370
wenzelm
parents: 65083
diff changeset
   154
      val java_message =
wenzelm
parents: 65083
diff changeset
   155
        message.iterator.map({ case x: scala.swing.Component => x.peer case x => x }).
wenzelm
parents: 65083
diff changeset
   156
          toArray.asInstanceOf[Array[AnyRef]]
wenzelm
parents: 65083
diff changeset
   157
      JOptionPane.showMessageDialog(parent, java_message,
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   158
        if (title == null) default_title else title, kind)
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
57639
wenzelm
parents: 57612
diff changeset
   162
  def dialog(parent: Component, title: String, message: Any*): Unit =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   163
    simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   164
57639
wenzelm
parents: 57612
diff changeset
   165
  def warning_dialog(parent: Component, title: String, message: Any*): Unit =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   166
    simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   167
57639
wenzelm
parents: 57612
diff changeset
   168
  def error_dialog(parent: Component, title: String, message: Any*): Unit =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   169
    simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   170
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   171
  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
   172
    GUI_Thread.now {
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   173
      val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   174
      JOptionPane.showConfirmDialog(parent,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   175
        java_message.toArray.asInstanceOf[Array[AnyRef]], title,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   176
          option_type, JOptionPane.QUESTION_MESSAGE)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   177
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   178
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   179
75852
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   180
  /* basic GUI components */
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   181
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   182
  class Button(label: String) extends scala.swing.Button(label) {
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   183
    def clicked(): Unit = {}
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   184
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   185
    reactions += { case ButtonClicked(_) => clicked() }
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   186
  }
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   187
75854
2163772eeaf2 tuned signature;
wenzelm
parents: 75853
diff changeset
   188
  class Check(label: String, init: Boolean = false) extends CheckBox(label) {
75852
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   189
    def clicked(state: Boolean): Unit = {}
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   190
    def clicked(): Unit = {}
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   191
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   192
    selected = init
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   193
    reactions += { case ButtonClicked(_) => clicked(selected); clicked() }
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   194
  }
75839
29441f2bfe81 clarified signature: more explicit types;
wenzelm
parents: 75809
diff changeset
   195
76492
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   196
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   197
  /* list selector */
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   198
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   199
  object Selector {
76505
e0d797283638 tuned signature;
wenzelm
parents: 76504
diff changeset
   200
    sealed abstract class Entry[A] { def get_value: Option[A] = Value.unapply(this) }
76504
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   201
    object Value {
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   202
      def unapply[A](entry: Entry[A]): Option[A] =
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   203
        entry match {
76502
08b950ca0313 tuned signature;
wenzelm
parents: 76494
diff changeset
   204
          case item: Item[_] => Some(item.value)
76492
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   205
          case _ => None
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   206
        }
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   207
    }
76505
e0d797283638 tuned signature;
wenzelm
parents: 76504
diff changeset
   208
    def item[A](value: A): Entry[A] = Item(value, "", 0)
e0d797283638 tuned signature;
wenzelm
parents: 76504
diff changeset
   209
    def item_description[A](value: A, description: String): Entry[A] = Item(value, description, 0)
e0d797283638 tuned signature;
wenzelm
parents: 76504
diff changeset
   210
76504
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   211
    private case class Item[A](value: A, description: String, batch: Int) extends Entry[A] {
76502
08b950ca0313 tuned signature;
wenzelm
parents: 76494
diff changeset
   212
      override def toString: String = proper_string(description) getOrElse value.toString
76492
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   213
    }
76504
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   214
    private case class Separator[A](batch: Int) extends Entry[A] {
76503
5944f9e70d98 clarified signature: only support nameless separator;
wenzelm
parents: 76502
diff changeset
   215
      override def toString: String = "---"
76492
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   216
    }
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   217
76505
e0d797283638 tuned signature;
wenzelm
parents: 76504
diff changeset
   218
    private def make_entries[A](batches: List[List[Entry[A]]]): List[Entry[A]] = {
76789
wenzelm
parents: 76505
diff changeset
   219
      val item_batches = batches.map(_.flatMap(Library.as_subclass(classOf[Item[A]])))
76504
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   220
      val sep_entries: List[Entry[A]] =
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   221
        item_batches.filter(_.nonEmpty).zipWithIndex.flatMap({ case (batch, i) =>
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   222
          Separator[A](i) :: batch.map(_.copy(batch = i))
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   223
        })
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   224
      sep_entries.tail
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   225
    }
76492
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   226
  }
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   227
76504
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   228
  class Selector[A](batches: List[Selector.Entry[A]]*)
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   229
  extends ComboBox[Selector.Entry[A]](Selector.make_entries(batches.toList)) {
75839
29441f2bfe81 clarified signature: more explicit types;
wenzelm
parents: 75809
diff changeset
   230
    def changed(): Unit = {}
29441f2bfe81 clarified signature: more explicit types;
wenzelm
parents: 75809
diff changeset
   231
76504
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   232
    lazy val entries: List[Selector.Entry[A]] = Selector.make_entries(batches.toList)
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   233
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   234
    def find_value(pred: A => Boolean): Option[Selector.Entry[A]] =
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   235
      entries.find({ case item: Selector.Item[A] => pred(item.value) case _ => false })
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   236
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   237
    def selection_value: Option[A] = selection.item.get_value
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   238
76494
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   239
    override lazy val peer: JComboBox[Selector.Entry[A]] =
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   240
      new JComboBox[Selector.Entry[A]](ComboBox.newConstantModel(entries)) with SuperMixin {
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   241
        private var key_released = false
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   242
        private var sep_selected = false
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   243
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   244
        addKeyListener(new KeyAdapter {
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   245
          override def keyPressed(e: KeyEvent): Unit = { key_released = false }
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   246
          override def keyReleased(e: KeyEvent): Unit = { key_released = true }
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   247
        })
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   248
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   249
        override def setSelectedIndex(i: Int): Unit = {
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   250
          getItemAt(i) match {
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   251
            case _: Selector.Separator[_] =>
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   252
              if (key_released) { sep_selected = true }
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   253
              else {
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   254
                val k = getSelectedIndex()
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   255
                val j = if (i > k) i + 1 else i - 1
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   256
                if (0 <= j && j < dataModel.getSize()) super.setSelectedIndex(j)
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   257
              }
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   258
            case _ => super.setSelectedIndex(i)
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   259
          }
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   260
        }
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   261
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   262
        override def setPopupVisible(visible: Boolean): Unit = {
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   263
          if (sep_selected) { sep_selected = false}
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   264
          else super.setPopupVisible(visible)
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   265
        }
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   266
      }
9686049ce988 more robust selection: avoid duplicates via "batch" number;
wenzelm
parents: 76492
diff changeset
   267
76492
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   268
    private val default_renderer = renderer
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   269
    private val render_separator = new Separator
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   270
    renderer =
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   271
      (list: ListView[_ <: Selector.Entry[A]], selected: Boolean, focus: Boolean, entry: Selector.Entry[A], i: Int) => {
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   272
        entry match {
76503
5944f9e70d98 clarified signature: only support nameless separator;
wenzelm
parents: 76502
diff changeset
   273
          case _: Selector.Separator[_] => render_separator
76492
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   274
          case _ => default_renderer.componentFor(list, selected, focus, entry, i)
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   275
        }
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   276
      }
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   277
75839
29441f2bfe81 clarified signature: more explicit types;
wenzelm
parents: 75809
diff changeset
   278
    listenTo(selection)
29441f2bfe81 clarified signature: more explicit types;
wenzelm
parents: 75809
diff changeset
   279
    reactions += { case SelectionChanged(_) => changed() }
29441f2bfe81 clarified signature: more explicit types;
wenzelm
parents: 75809
diff changeset
   280
  }
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   281
75852
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   282
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   283
  /* zoom factor */
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   284
81382
5e8287d34295 clarified signature;
wenzelm
parents: 81335
diff changeset
   285
  private val Percent = "([0-9]+)%?".r
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   286
75839
29441f2bfe81 clarified signature: more explicit types;
wenzelm
parents: 75809
diff changeset
   287
  class Zoom extends Selector[String](
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   288
    List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")
76504
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   289
      .map(GUI.Selector.item)
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   290
  ) {
81382
5e8287d34295 clarified signature;
wenzelm
parents: 81335
diff changeset
   291
    def percent: Int = parse(selection.item.toString)
5e8287d34295 clarified signature;
wenzelm
parents: 81335
diff changeset
   292
    def scale: Double = 0.01 * percent
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   293
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   294
    private def parse(text: String): Int =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   295
      text match {
81382
5e8287d34295 clarified signature;
wenzelm
parents: 81335
diff changeset
   296
        case Percent(s) =>
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   297
          val i = Integer.parseInt(s)
56874
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   298
          if (10 <= i && i < 1000) i else 100
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   299
        case _ => 100
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   300
      }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   301
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   302
    private def print(i: Int): String = i.toString + "%"
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   303
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   304
    def set_item(i: Int): Unit = {
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   305
      peer.getEditor match {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   306
        case null =>
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   307
        case editor => editor.setItem(print(i))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   308
      }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   309
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   310
76492
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75854
diff changeset
   311
    makeEditable()(c =>
76504
15b058bb2416 clarified signature: ensure that entries are well-formed --- no consecutive separators, no separators at start/end;
wenzelm
parents: 76503
diff changeset
   312
      new ComboBox.BuiltInEditor(c)(text => Selector.item(print(parse(text))), _.toString))
56874
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   313
    peer.getEditor.getEditorComponent match {
56888
3e8cbb624cc5 tuned GUI for Windows L&F;
wenzelm
parents: 56874
diff changeset
   314
      case text: JTextField => text.setColumns(4)
56874
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   315
      case _ =>
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   316
    }
5d78bce4f5a4 tuned GUI layout;
wenzelm
parents: 56752
diff changeset
   317
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56888
diff changeset
   318
    selection.index = 3
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   319
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   320
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   321
53786
wenzelm
parents: 53785
diff changeset
   322
  /* tooltip with multi-line support */
wenzelm
parents: 53785
diff changeset
   323
56622
891d1b8b64fb clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents: 54965
diff changeset
   324
  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
   325
    if (text == null || text == "") null
81657
4210fd10e776 clarified signature;
wenzelm
parents: 81655
diff changeset
   326
    else Style_HTML.enclose_text(text)
53786
wenzelm
parents: 53785
diff changeset
   327
wenzelm
parents: 53785
diff changeset
   328
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   329
  /* icon */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   330
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   331
  def isabelle_icon(): ImageIcon =
54676
6b2ca4850b71 uniform use of transparent icons, as for main "apps";
wenzelm
parents: 54659
diff changeset
   332
    new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle_transparent-32.gif"))
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   333
54709
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   334
  def isabelle_icons(): List[ImageIcon] =
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   335
    for (icon <- List("isabelle/isabelle_transparent-32.gif", "isabelle/isabelle_transparent.gif"))
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   336
      yield new ImageIcon(getClass.getClassLoader.getResource(icon))
87402674fe2f alternative hires icon;
wenzelm
parents: 54676
diff changeset
   337
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   338
  def isabelle_image(): Image = isabelle_icon().getImage
53712
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   339
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   340
72974
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   341
  /* location within multi-screen environment */
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   342
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   343
  final case class Screen_Location(point: Point, bounds: Rectangle) {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   344
    def relative(parent: Component, size: Dimension): Point = {
72974
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   345
      val w = size.width
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   346
      val h = size.height
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   347
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   348
      val x0 = parent.getLocationOnScreen.x
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   349
      val y0 = parent.getLocationOnScreen.y
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   350
      val x1 = x0 + parent.getWidth - w
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   351
      val y1 = y0 + parent.getHeight - h
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   352
      val x2 = point.x min (bounds.x + bounds.width - w)
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   353
      val y2 = point.y min (bounds.y + bounds.height - h)
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   354
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   355
      val location = new Point((x2 min x1) max x0, (y2 min y1) max y0)
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   356
      SwingUtilities.convertPointFromScreen(location, parent)
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   357
      location
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   358
    }
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   359
  }
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   360
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   361
  def screen_location(component: Component, point: Point): Screen_Location = {
72974
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   362
    val screen_point = new Point(point.x, point.y)
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   363
    if (component != null) SwingUtilities.convertPointToScreen(screen_point, component)
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   364
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   365
    val ge = GraphicsEnvironment.getLocalGraphicsEnvironment
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   366
    val screen_bounds =
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   367
      (for {
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   368
        device <- ge.getScreenDevices.iterator
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   369
        config <- device.getConfigurations.iterator
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   370
        bounds = config.getBounds
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   371
      } yield bounds).find(_.contains(screen_point)) getOrElse ge.getMaximumWindowBounds
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   372
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   373
    Screen_Location(screen_point, screen_bounds)
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   374
  }
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   375
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   376
  def mouse_location(): Screen_Location =
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   377
    screen_location(null, MouseInfo.getPointerInfo.getLocation)
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   378
3afd293347cc clarified modules;
wenzelm
parents: 72786
diff changeset
   379
73037
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
   380
  /* screen size */
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
   381
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   382
  sealed case class Screen_Size(bounds: Rectangle, insets: Insets) {
73038
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   383
    def full_screen_bounds: Rectangle =
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   384
      if (Platform.is_linux) {
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   385
        // avoid menu bar and docking areas
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   386
        new Rectangle(
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   387
          bounds.x + insets.left,
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   388
          bounds.y + insets.top,
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   389
          bounds.width - insets.left - insets.right,
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   390
          bounds.height - insets.top - insets.bottom)
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   391
      }
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   392
      else if (Platform.is_macos) {
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   393
        // avoid menu bar, but ignore docking areas
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   394
        new Rectangle(
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   395
          bounds.x,
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   396
          bounds.y + insets.top,
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   397
          bounds.width,
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   398
          bounds.height - insets.top)
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   399
      }
3b14f7315dd2 some attempts at multi-platform full-screen mode;
wenzelm
parents: 73037
diff changeset
   400
      else bounds
73037
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
   401
  }
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
   402
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   403
  def screen_size(component: Component): Screen_Size = {
73037
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
   404
    val config = component.getGraphicsConfiguration
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
   405
    val bounds = config.getBounds
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
   406
    val insets = Toolkit.getDefaultToolkit.getScreenInsets(config)
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
   407
    Screen_Size(bounds, insets)
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
   408
  }
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
   409
473509b160d9 more operations;
wenzelm
parents: 72975
diff changeset
   410
53712
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   411
  /* component hierachy */
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   412
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   413
  def get_parent(component: Component): Option[Container] =
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   414
    component.getParent match {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   415
      case null => None
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   416
      case parent => Some(parent)
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   417
    }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   418
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   419
  def ancestors(component: Component): Iterator[Container] = new Iterator[Container] {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   420
    private var next_elem = get_parent(component)
73337
0af9e7e4476f tuned --- fewer warnings;
wenzelm
parents: 73117
diff changeset
   421
    def hasNext: Boolean = next_elem.isDefined
53712
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   422
    def next(): Container =
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   423
      next_elem match {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   424
        case Some(parent) =>
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   425
          next_elem = get_parent(parent)
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   426
          parent
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   427
        case None => Iterator.empty.next()
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   428
      }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   429
  }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   430
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   431
  def parent_window(component: Component): Option[Window] =
80553
wenzelm
parents: 76789
diff changeset
   432
    ancestors(component).collectFirst({ case c: Window => c })
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
   433
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
   434
  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
   435
    parent_window(component) match {
80553
wenzelm
parents: 76789
diff changeset
   436
      case Some(c: RootPaneContainer) => Some(c.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
   437
      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
   438
    }
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   439
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   440
  def traverse_components(component: Component, apply: Component => Unit): Unit = {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   441
    def traverse(comp: Component): Unit = {
63874
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   442
      apply(comp)
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   443
      comp match {
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   444
        case cont: Container =>
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   445
          for (i <- 0 until cont.getComponentCount)
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   446
            traverse(cont.getComponent(i))
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   447
        case _ =>
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   448
      }
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   449
    }
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   450
    traverse(component)
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   451
  }
e2393cfde472 handle font-size events;
wenzelm
parents: 63420
diff changeset
   452
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   453
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   454
  /* font operations */
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   455
61742
fd3b214b0979 clarified font: GUI defaults might change dynamically;
wenzelm
parents: 61529
diff changeset
   456
  def copy_font(font: Font): Font =
fd3b214b0979 clarified font: GUI defaults might change dynamically;
wenzelm
parents: 61529
diff changeset
   457
    if (font == null) null
fd3b214b0979 clarified font: GUI defaults might change dynamically;
wenzelm
parents: 61529
diff changeset
   458
    else new Font(font.getFamily, font.getStyle, font.getSize)
fd3b214b0979 clarified font: GUI defaults might change dynamically;
wenzelm
parents: 61529
diff changeset
   459
59230
cae3ef2897f2 tuned signature;
wenzelm
parents: 59201
diff changeset
   460
  def line_metrics(font: Font): LineMetrics =
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   461
    font.getLineMetrics("", new FontRenderContext(null, false, false))
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   462
69376
wenzelm
parents: 69368
diff changeset
   463
  def transform_font(font: Font, transform: AffineTransform): Font =
73909
1d0d9772fff0 tuned imports;
wenzelm
parents: 73878
diff changeset
   464
    font.deriveFont(JMap.of(TextAttribute.TRANSFORM, new TransformAttribute(transform)))
69376
wenzelm
parents: 69368
diff changeset
   465
wenzelm
parents: 69368
diff changeset
   466
  def font(family: String = Isabelle_Fonts.sans, size: Int = 1, bold: Boolean = false): Font =
wenzelm
parents: 69368
diff changeset
   467
    new Font(family, if (bold) Font.BOLD else Font.PLAIN, size)
wenzelm
parents: 69368
diff changeset
   468
69377
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69376
diff changeset
   469
  def label_font(): Font = (new JLabel).getFont
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69376
diff changeset
   470
69376
wenzelm
parents: 69368
diff changeset
   471
wenzelm
parents: 69368
diff changeset
   472
  /* Isabelle fonts */
wenzelm
parents: 69368
diff changeset
   473
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   474
  def imitate_font(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   475
    font: Font,
69358
71ef6e6da3dc prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents: 69357
diff changeset
   476
    family: String = Isabelle_Fonts.sans,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   477
    scale: Double = 1.0
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   478
  ): Font = {
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   479
    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
   480
    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
   481
    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
   482
  }
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   483
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   484
  def imitate_font_css(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   485
    font: Font,
69358
71ef6e6da3dc prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents: 69357
diff changeset
   486
    family: String = Isabelle_Fonts.sans,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   487
    scale: Double = 1.0
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   488
  ): String = {
59183
ec83638b6bfb imitate font more carefully: err on smaller size;
wenzelm
parents: 59080
diff changeset
   489
    val font1 = new Font(family, font.getStyle, font.getSize)
59230
cae3ef2897f2 tuned signature;
wenzelm
parents: 59201
diff changeset
   490
    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
   491
    "font-family: " + family + "; font-size: " + (scale * rel_size * 100).toInt + "%;"
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   492
  }
69377
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69376
diff changeset
   493
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   494
  def use_isabelle_fonts(): Unit = {
69377
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69376
diff changeset
   495
    val default_font = label_font()
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69376
diff changeset
   496
    val ui = UIManager.getDefaults
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   497
    for (prop <-
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   498
      List(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   499
        "ToggleButton.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   500
        "CheckBoxMenuItem.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   501
        "Label.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   502
        "Menu.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   503
        "MenuItem.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   504
        "PopupMenu.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   505
        "Table.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   506
        "TableHeader.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   507
        "TextArea.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   508
        "TextField.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   509
        "TextPane.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   510
        "ToolTip.font",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
   511
        "Tree.font")) {
69377
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69376
diff changeset
   512
      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: 69376
diff changeset
   513
      ui.put(prop, GUI.imitate_font(font))
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69376
diff changeset
   514
    }
81ae5893c556 use Isabelle fonts for all GUI look-and-feels;
wenzelm
parents: 69376
diff changeset
   515
  }
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   516
}
73111
01f4965fd09b support for additional look-and-feels;
wenzelm
parents: 73095
diff changeset
   517
01f4965fd09b support for additional look-and-feels;
wenzelm
parents: 73095
diff changeset
   518
class FlatLightLaf extends GUI.Look_And_Feel(new com.formdev.flatlaf.FlatLightLaf)
01f4965fd09b support for additional look-and-feels;
wenzelm
parents: 73095
diff changeset
   519
class FlatDarkLaf extends GUI.Look_And_Feel(new com.formdev.flatlaf.FlatDarkLaf)