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