author | wenzelm |
Thu, 26 Dec 2024 15:38:57 +0100 | |
changeset 81659 | a904fcbbbdbc |
parent 81657 | 4210fd10e776 |
child 81661 | 98ecc0ed6af1 |
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 |
||
98 |
object Style_HTML extends Style_HTML { override def toString: String = "html" } |
|
99 |
||
100 |
object Style_Symbol_Encoded extends Style_Symbol { |
|
101 |
override def toString: String = "symbol_encoded" |
|
102 |
override def bold: String = Symbol.bold |
|
103 |
} |
|
104 |
||
105 |
object Style_Symbol_Decoded extends Style_Symbol { |
|
106 |
override def toString: String = "symbol_decoded" |
|
107 |
override def bold: String = Symbol.bold_decoded |
|
108 |
} |
|
109 |
||
110 |
||
111 |
/* named items */ |
|
81649 | 112 |
|
113 |
sealed case class Name( |
|
114 |
name: String, |
|
115 |
kind: String = "", |
|
116 |
prefix: String = "", |
|
81655 | 117 |
style: Style = Style_Plain |
81649 | 118 |
) { |
81648 | 119 |
override def toString: String = { |
120 |
val a = kind.nonEmpty |
|
121 |
val b = name.nonEmpty |
|
81655 | 122 |
style.make_text(prefix) + |
81654 | 123 |
if_proper(a || b, |
81655 | 124 |
if_proper(prefix, ": ") + if_proper(kind, style.make_bold(kind)) + |
125 |
if_proper(a && b, " ") + if_proper(b, style.make_text(quote(name)))) |
|
81648 | 126 |
} |
127 |
} |
|
128 |
||
129 |
||
51619 | 130 |
/* simple dialogs */ |
131 |
||
75393 | 132 |
def scrollable_text( |
133 |
raw_txt: String, |
|
134 |
width: Int = 60, |
|
135 |
height: Int = 20, |
|
136 |
editable: Boolean = false |
|
137 |
) : ScrollPane = { |
|
80817 | 138 |
val txt = Protocol_Message.clean_output(raw_txt) |
51619 | 139 |
val text = new TextArea(txt) |
140 |
if (width > 0) text.columns = width |
|
53714 | 141 |
if (height > 0 && split_lines(txt).length > height) text.rows = height |
51619 | 142 |
text.editable = editable |
143 |
new ScrollPane(text) |
|
144 |
} |
|
145 |
||
75393 | 146 |
private def simple_dialog( |
147 |
kind: Int, |
|
148 |
default_title: String, |
|
149 |
parent: Component, |
|
150 |
title: String, |
|
151 |
message: Iterable[Any] |
|
152 |
): Unit = { |
|
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57044
diff
changeset
|
153 |
GUI_Thread.now { |
65370 | 154 |
val java_message = |
155 |
message.iterator.map({ case x: scala.swing.Component => x.peer case x => x }). |
|
156 |
toArray.asInstanceOf[Array[AnyRef]] |
|
157 |
JOptionPane.showMessageDialog(parent, java_message, |
|
51619 | 158 |
if (title == null) default_title else title, kind) |
159 |
} |
|
160 |
} |
|
161 |
||
57639 | 162 |
def dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 163 |
simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message) |
164 |
||
57639 | 165 |
def warning_dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 166 |
simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message) |
167 |
||
57639 | 168 |
def error_dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 169 |
simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message) |
170 |
||
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 | 173 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
174 |
JOptionPane.showConfirmDialog(parent, |
|
175 |
java_message.toArray.asInstanceOf[Array[AnyRef]], title, |
|
176 |
option_type, JOptionPane.QUESTION_MESSAGE) |
|
177 |
} |
|
178 |
||
179 |
||
75852 | 180 |
/* basic GUI components */ |
181 |
||
75853 | 182 |
class Button(label: String) extends scala.swing.Button(label) { |
183 |
def clicked(): Unit = {} |
|
184 |
||
185 |
reactions += { case ButtonClicked(_) => clicked() } |
|
186 |
} |
|
187 |
||
75854 | 188 |
class Check(label: String, init: Boolean = false) extends CheckBox(label) { |
75852 | 189 |
def clicked(state: Boolean): Unit = {} |
190 |
def clicked(): Unit = {} |
|
191 |
||
192 |
selected = init |
|
193 |
reactions += { case ButtonClicked(_) => clicked(selected); clicked() } |
|
194 |
} |
|
75839 | 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 | 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 | 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 | 208 |
def item[A](value: A): Entry[A] = Item(value, "", 0) |
209 |
def item_description[A](value: A, description: String): Entry[A] = Item(value, description, 0) |
|
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 | 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 | 218 |
private def make_entries[A](batches: List[List[Entry[A]]]): List[Entry[A]] = { |
76789 | 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 | 230 |
def changed(): Unit = {} |
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 | 278 |
listenTo(selection) |
279 |
reactions += { case SelectionChanged(_) => changed() } |
|
280 |
} |
|
51619 | 281 |
|
75852 | 282 |
|
283 |
/* zoom factor */ |
|
284 |
||
81382 | 285 |
private val Percent = "([0-9]+)%?".r |
57044 | 286 |
|
75839 | 287 |
class Zoom extends Selector[String]( |
75393 | 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 | 290 |
) { |
81382 | 291 |
def percent: Int = parse(selection.item.toString) |
292 |
def scale: Double = 0.01 * percent |
|
57044 | 293 |
|
294 |
private def parse(text: String): Int = |
|
51619 | 295 |
text match { |
81382 | 296 |
case Percent(s) => |
51619 | 297 |
val i = Integer.parseInt(s) |
56874 | 298 |
if (10 <= i && i < 1000) i else 100 |
51619 | 299 |
case _ => 100 |
300 |
} |
|
301 |
||
57044 | 302 |
private def print(i: Int): String = i.toString + "%" |
51619 | 303 |
|
75393 | 304 |
def set_item(i: Int): Unit = { |
51619 | 305 |
peer.getEditor match { |
306 |
case null => |
|
307 |
case editor => editor.setItem(print(i)) |
|
308 |
} |
|
309 |
} |
|
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 | 313 |
peer.getEditor.getEditorComponent match { |
56888 | 314 |
case text: JTextField => text.setColumns(4) |
56874 | 315 |
case _ => |
316 |
} |
|
317 |
||
57044 | 318 |
selection.index = 3 |
51619 | 319 |
} |
320 |
||
321 |
||
53786 | 322 |
/* tooltip with multi-line support */ |
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 | 326 |
else Style_HTML.enclose_text(text) |
53786 | 327 |
|
328 |
||
51619 | 329 |
/* icon */ |
330 |
||
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 | 333 |
|
54709 | 334 |
def isabelle_icons(): List[ImageIcon] = |
335 |
for (icon <- List("isabelle/isabelle_transparent-32.gif", "isabelle/isabelle_transparent.gif")) |
|
336 |
yield new ImageIcon(getClass.getClassLoader.getResource(icon)) |
|
337 |
||
51619 | 338 |
def isabelle_image(): Image = isabelle_icon().getImage |
53712 | 339 |
|
340 |
||
72974 | 341 |
/* location within multi-screen environment */ |
342 |
||
75393 | 343 |
final case class Screen_Location(point: Point, bounds: Rectangle) { |
344 |
def relative(parent: Component, size: Dimension): Point = { |
|
72974 | 345 |
val w = size.width |
346 |
val h = size.height |
|
347 |
||
348 |
val x0 = parent.getLocationOnScreen.x |
|
349 |
val y0 = parent.getLocationOnScreen.y |
|
350 |
val x1 = x0 + parent.getWidth - w |
|
351 |
val y1 = y0 + parent.getHeight - h |
|
352 |
val x2 = point.x min (bounds.x + bounds.width - w) |
|
353 |
val y2 = point.y min (bounds.y + bounds.height - h) |
|
354 |
||
355 |
val location = new Point((x2 min x1) max x0, (y2 min y1) max y0) |
|
356 |
SwingUtilities.convertPointFromScreen(location, parent) |
|
357 |
location |
|
358 |
} |
|
359 |
} |
|
360 |
||
75393 | 361 |
def screen_location(component: Component, point: Point): Screen_Location = { |
72974 | 362 |
val screen_point = new Point(point.x, point.y) |
363 |
if (component != null) SwingUtilities.convertPointToScreen(screen_point, component) |
|
364 |
||
365 |
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment |
|
366 |
val screen_bounds = |
|
367 |
(for { |
|
368 |
device <- ge.getScreenDevices.iterator |
|
369 |
config <- device.getConfigurations.iterator |
|
370 |
bounds = config.getBounds |
|
371 |
} yield bounds).find(_.contains(screen_point)) getOrElse ge.getMaximumWindowBounds |
|
372 |
||
373 |
Screen_Location(screen_point, screen_bounds) |
|
374 |
} |
|
375 |
||
376 |
def mouse_location(): Screen_Location = |
|
377 |
screen_location(null, MouseInfo.getPointerInfo.getLocation) |
|
378 |
||
379 |
||
73037 | 380 |
/* screen size */ |
381 |
||
75393 | 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 | 401 |
} |
402 |
||
75393 | 403 |
def screen_size(component: Component): Screen_Size = { |
73037 | 404 |
val config = component.getGraphicsConfiguration |
405 |
val bounds = config.getBounds |
|
406 |
val insets = Toolkit.getDefaultToolkit.getScreenInsets(config) |
|
407 |
Screen_Size(bounds, insets) |
|
408 |
} |
|
409 |
||
410 |
||
53712 | 411 |
/* component hierachy */ |
412 |
||
413 |
def get_parent(component: Component): Option[Container] = |
|
414 |
component.getParent match { |
|
415 |
case null => None |
|
416 |
case parent => Some(parent) |
|
417 |
} |
|
418 |
||
419 |
def ancestors(component: Component): Iterator[Container] = new Iterator[Container] { |
|
420 |
private var next_elem = get_parent(component) |
|
73337 | 421 |
def hasNext: Boolean = next_elem.isDefined |
53712 | 422 |
def next(): Container = |
423 |
next_elem match { |
|
424 |
case Some(parent) => |
|
425 |
next_elem = get_parent(parent) |
|
426 |
parent |
|
427 |
case None => Iterator.empty.next() |
|
428 |
} |
|
429 |
} |
|
430 |
||
431 |
def parent_window(component: Component): Option[Window] = |
|
80553 | 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 | 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 | 439 |
|
75393 | 440 |
def traverse_components(component: Component, apply: Component => Unit): Unit = { |
441 |
def traverse(comp: Component): Unit = { |
|
63874 | 442 |
apply(comp) |
443 |
comp match { |
|
444 |
case cont: Container => |
|
445 |
for (i <- 0 until cont.getComponentCount) |
|
446 |
traverse(cont.getComponent(i)) |
|
447 |
case _ => |
|
448 |
} |
|
449 |
} |
|
450 |
traverse(component) |
|
451 |
} |
|
452 |
||
53785 | 453 |
|
454 |
/* font operations */ |
|
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 | 460 |
def line_metrics(font: Font): LineMetrics = |
53785 | 461 |
font.getLineMetrics("", new FontRenderContext(null, false, false)) |
462 |
||
69376 | 463 |
def transform_font(font: Font, transform: AffineTransform): Font = |
73909 | 464 |
font.deriveFont(JMap.of(TextAttribute.TRANSFORM, new TransformAttribute(transform))) |
69376 | 465 |
|
466 |
def font(family: String = Isabelle_Fonts.sans, size: Int = 1, bold: Boolean = false): Font = |
|
467 |
new Font(family, if (bold) Font.BOLD else Font.PLAIN, size) |
|
468 |
||
69377 | 469 |
def label_font(): Font = (new JLabel).getFont |
470 |
||
69376 | 471 |
|
472 |
/* Isabelle fonts */ |
|
473 |
||
75393 | 474 |
def imitate_font( |
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 | 477 |
scale: Double = 1.0 |
478 |
): Font = { |
|
53785 | 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 | 484 |
def imitate_font_css( |
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 | 487 |
scale: Double = 1.0 |
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 | 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 | 492 |
} |
69377 | 493 |
|
75393 | 494 |
def use_isabelle_fonts(): Unit = { |
69377 | 495 |
val default_font = label_font() |
496 |
val ui = UIManager.getDefaults |
|
75393 | 497 |
for (prop <- |
498 |
List( |
|
499 |
"ToggleButton.font", |
|
500 |
"CheckBoxMenuItem.font", |
|
501 |
"Label.font", |
|
502 |
"Menu.font", |
|
503 |
"MenuItem.font", |
|
504 |
"PopupMenu.font", |
|
505 |
"Table.font", |
|
506 |
"TableHeader.font", |
|
507 |
"TextArea.font", |
|
508 |
"TextField.font", |
|
509 |
"TextPane.font", |
|
510 |
"ToolTip.font", |
|
511 |
"Tree.font")) { |
|
69377 | 512 |
val font = ui.get(prop) match { case font: Font => font case _ => default_font } |
513 |
ui.put(prop, GUI.imitate_font(font)) |
|
514 |
} |
|
515 |
} |
|
51619 | 516 |
} |
73111 | 517 |
|
518 |
class FlatLightLaf extends GUI.Look_And_Feel(new com.formdev.flatlaf.FlatLightLaf) |
|
519 |
class FlatDarkLaf extends GUI.Look_And_Feel(new com.formdev.flatlaf.FlatDarkLaf) |