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