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