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