author | wenzelm |
Sat, 01 Dec 2018 16:57:46 +0100 | |
changeset 69384 | 0c7d8b1b6594 |
parent 69378 | 429426640596 |
child 71357 | 7f2cd237ee4f |
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 |
||
54962 | 9 |
import java.lang.{ClassLoader, ClassNotFoundException, NoSuchMethodException} |
69368
6f360600eabc
clarified signature: fonts are not dependent on GUI;
wenzelm
parents:
69360
diff
changeset
|
10 |
import java.awt.{Image, Component, Container, Toolkit, Window, Font, KeyboardFocusManager} |
53785 | 11 |
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics} |
12 |
import java.awt.geom.AffineTransform |
|
57879
91e188508bc9
proper layered_pane for JDialog, e.g. relevant for floating dockables in jEdit, for completion popup in text field;
wenzelm
parents:
57639
diff
changeset
|
13 |
import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow, JDialog, |
69377 | 14 |
JButton, JTextField, JLabel} |
51619 | 15 |
|
16 |
import scala.swing.{ComboBox, TextArea, ScrollPane} |
|
17 |
import scala.swing.event.SelectionChanged |
|
18 |
||
19 |
||
20 |
object GUI |
|
21 |
{ |
|
22 |
/* Swing look-and-feel */ |
|
23 |
||
59201
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
24 |
def find_laf(name: String): Option[String] = |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
25 |
UIManager.getInstalledLookAndFeels(). |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
26 |
find(c => c.getName == name || c.getClassName == name). |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
27 |
map(_.getClassName) |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
28 |
|
51619 | 29 |
def get_laf(): String = |
59201
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
30 |
find_laf(System.getProperty("isabelle.laf")) getOrElse { |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
31 |
if (Platform.is_windows || Platform.is_macos) |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
32 |
UIManager.getSystemLookAndFeelClassName() |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
33 |
else |
61529
82fc5a6231a2
back to traditional Metal as default, and thus evade current problems with Nimbus scrollbar slider;
wenzelm
parents:
61523
diff
changeset
|
34 |
UIManager.getCrossPlatformLookAndFeelClassName() |
59201
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
35 |
} |
51619 | 36 |
|
37 |
def init_laf(): Unit = UIManager.setLookAndFeel(get_laf()) |
|
38 |
||
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
|
39 |
def is_macos_laf(): Boolean = |
53849 | 40 |
Platform.is_macos && |
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
|
41 |
UIManager.getSystemLookAndFeelClassName() == UIManager.getLookAndFeel.getClass.getName |
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
|
42 |
|
60998 | 43 |
def is_windows_laf(): Boolean = |
44 |
Platform.is_windows && |
|
45 |
UIManager.getSystemLookAndFeelClassName() == UIManager.getLookAndFeel.getClass.getName |
|
46 |
||
51619 | 47 |
|
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
|
48 |
/* 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
|
49 |
|
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
|
50 |
def plain_focus_traversal(component: Component) |
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
|
51 |
{ |
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
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
} |
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 |
|
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 |
|
51619 | 60 |
/* simple dialogs */ |
61 |
||
59671
9715eb8e9408
more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents:
59286
diff
changeset
|
62 |
def scrollable_text(raw_txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false) |
53714 | 63 |
: ScrollPane = |
51619 | 64 |
{ |
59671
9715eb8e9408
more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents:
59286
diff
changeset
|
65 |
val txt = Output.clean_yxml(raw_txt) |
51619 | 66 |
val text = new TextArea(txt) |
67 |
if (width > 0) text.columns = width |
|
53714 | 68 |
if (height > 0 && split_lines(txt).length > height) text.rows = height |
51619 | 69 |
text.editable = editable |
70 |
new ScrollPane(text) |
|
71 |
} |
|
72 |
||
73 |
private def simple_dialog(kind: Int, default_title: String, |
|
65370 | 74 |
parent: Component, title: String, message: Iterable[Any]) |
51619 | 75 |
{ |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57044
diff
changeset
|
76 |
GUI_Thread.now { |
65370 | 77 |
val java_message = |
78 |
message.iterator.map({ case x: scala.swing.Component => x.peer case x => x }). |
|
79 |
toArray.asInstanceOf[Array[AnyRef]] |
|
80 |
JOptionPane.showMessageDialog(parent, java_message, |
|
51619 | 81 |
if (title == null) default_title else title, kind) |
82 |
} |
|
83 |
} |
|
84 |
||
57639 | 85 |
def dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 86 |
simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message) |
87 |
||
57639 | 88 |
def warning_dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 89 |
simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message) |
90 |
||
57639 | 91 |
def error_dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 92 |
simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message) |
93 |
||
94 |
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
|
95 |
GUI_Thread.now { |
51619 | 96 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
97 |
JOptionPane.showConfirmDialog(parent, |
|
98 |
java_message.toArray.asInstanceOf[Array[AnyRef]], title, |
|
99 |
option_type, JOptionPane.QUESTION_MESSAGE) |
|
100 |
} |
|
101 |
||
102 |
||
103 |
/* zoom box */ |
|
104 |
||
57044 | 105 |
private val Zoom_Factor = "([0-9]+)%?".r |
106 |
||
107 |
abstract class Zoom_Box extends ComboBox[String]( |
|
51619 | 108 |
List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")) |
109 |
{ |
|
57044 | 110 |
def changed: Unit |
111 |
def factor: Int = parse(selection.item) |
|
112 |
||
113 |
private def parse(text: String): Int = |
|
51619 | 114 |
text match { |
57044 | 115 |
case Zoom_Factor(s) => |
51619 | 116 |
val i = Integer.parseInt(s) |
56874 | 117 |
if (10 <= i && i < 1000) i else 100 |
51619 | 118 |
case _ => 100 |
119 |
} |
|
120 |
||
57044 | 121 |
private def print(i: Int): String = i.toString + "%" |
51619 | 122 |
|
123 |
def set_item(i: Int) { |
|
124 |
peer.getEditor match { |
|
125 |
case null => |
|
126 |
case editor => editor.setItem(print(i)) |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x)) |
|
56874 | 131 |
peer.getEditor.getEditorComponent match { |
56888 | 132 |
case text: JTextField => text.setColumns(4) |
56874 | 133 |
case _ => |
134 |
} |
|
135 |
||
57044 | 136 |
selection.index = 3 |
137 |
||
51619 | 138 |
listenTo(selection) |
57044 | 139 |
reactions += { case SelectionChanged(_) => changed } |
51619 | 140 |
} |
141 |
||
142 |
||
53786 | 143 |
/* tooltip with multi-line support */ |
144 |
||
56622
891d1b8b64fb
clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents:
54965
diff
changeset
|
145 |
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
|
146 |
if (text == null || text == "") null |
62113 | 147 |
else "<html>" + HTML.output(text) + "</html>" |
53786 | 148 |
|
149 |
||
51619 | 150 |
/* icon */ |
151 |
||
152 |
def isabelle_icon(): ImageIcon = |
|
54676
6b2ca4850b71
uniform use of transparent icons, as for main "apps";
wenzelm
parents:
54659
diff
changeset
|
153 |
new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle_transparent-32.gif")) |
51619 | 154 |
|
54709 | 155 |
def isabelle_icons(): List[ImageIcon] = |
156 |
for (icon <- List("isabelle/isabelle_transparent-32.gif", "isabelle/isabelle_transparent.gif")) |
|
157 |
yield new ImageIcon(getClass.getClassLoader.getResource(icon)) |
|
158 |
||
51619 | 159 |
def isabelle_image(): Image = isabelle_icon().getImage |
53712 | 160 |
|
161 |
||
162 |
/* component hierachy */ |
|
163 |
||
164 |
def get_parent(component: Component): Option[Container] = |
|
165 |
component.getParent match { |
|
166 |
case null => None |
|
167 |
case parent => Some(parent) |
|
168 |
} |
|
169 |
||
170 |
def ancestors(component: Component): Iterator[Container] = new Iterator[Container] { |
|
171 |
private var next_elem = get_parent(component) |
|
172 |
def hasNext(): Boolean = next_elem.isDefined |
|
173 |
def next(): Container = |
|
174 |
next_elem match { |
|
175 |
case Some(parent) => |
|
176 |
next_elem = get_parent(parent) |
|
177 |
parent |
|
178 |
case None => Iterator.empty.next() |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
def parent_window(component: Component): Option[Window] = |
|
183 |
ancestors(component).collectFirst({ case x: Window => x }) |
|
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
|
184 |
|
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
|
185 |
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
|
186 |
parent_window(component) match { |
57879
91e188508bc9
proper layered_pane for JDialog, e.g. relevant for floating dockables in jEdit, for completion popup in text field;
wenzelm
parents:
57639
diff
changeset
|
187 |
case Some(w: JWindow) => Some(w.getLayeredPane) |
91e188508bc9
proper layered_pane for JDialog, e.g. relevant for floating dockables in jEdit, for completion popup in text field;
wenzelm
parents:
57639
diff
changeset
|
188 |
case Some(w: JFrame) => Some(w.getLayeredPane) |
91e188508bc9
proper layered_pane for JDialog, e.g. relevant for floating dockables in jEdit, for completion popup in text field;
wenzelm
parents:
57639
diff
changeset
|
189 |
case Some(w: JDialog) => Some(w.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
|
190 |
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
|
191 |
} |
53785 | 192 |
|
63874 | 193 |
def traverse_components(component: Component, apply: Component => Unit) |
194 |
{ |
|
195 |
def traverse(comp: Component) |
|
196 |
{ |
|
197 |
apply(comp) |
|
198 |
comp match { |
|
199 |
case cont: Container => |
|
200 |
for (i <- 0 until cont.getComponentCount) |
|
201 |
traverse(cont.getComponent(i)) |
|
202 |
case _ => |
|
203 |
} |
|
204 |
} |
|
205 |
traverse(component) |
|
206 |
} |
|
207 |
||
53785 | 208 |
|
209 |
/* font operations */ |
|
210 |
||
61742
fd3b214b0979
clarified font: GUI defaults might change dynamically;
wenzelm
parents:
61529
diff
changeset
|
211 |
def copy_font(font: Font): Font = |
fd3b214b0979
clarified font: GUI defaults might change dynamically;
wenzelm
parents:
61529
diff
changeset
|
212 |
if (font == null) null |
fd3b214b0979
clarified font: GUI defaults might change dynamically;
wenzelm
parents:
61529
diff
changeset
|
213 |
else new Font(font.getFamily, font.getStyle, font.getSize) |
fd3b214b0979
clarified font: GUI defaults might change dynamically;
wenzelm
parents:
61529
diff
changeset
|
214 |
|
59230 | 215 |
def line_metrics(font: Font): LineMetrics = |
53785 | 216 |
font.getLineMetrics("", new FontRenderContext(null, false, false)) |
217 |
||
69376 | 218 |
def transform_font(font: Font, transform: AffineTransform): Font = |
219 |
{ |
|
220 |
import scala.collection.JavaConversions._ |
|
221 |
font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform))) |
|
222 |
} |
|
223 |
||
224 |
def font(family: String = Isabelle_Fonts.sans, size: Int = 1, bold: Boolean = false): Font = |
|
225 |
new Font(family, if (bold) Font.BOLD else Font.PLAIN, size) |
|
226 |
||
69377 | 227 |
def label_font(): Font = (new JLabel).getFont |
228 |
||
69376 | 229 |
|
230 |
/* Isabelle fonts */ |
|
231 |
||
69358
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
69357
diff
changeset
|
232 |
def imitate_font(font: Font, |
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
69357
diff
changeset
|
233 |
family: String = Isabelle_Fonts.sans, |
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
69357
diff
changeset
|
234 |
scale: Double = 1.0): Font = |
53785 | 235 |
{ |
236 |
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
|
237 |
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
|
238 |
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
|
239 |
} |
ec83638b6bfb
imitate font more carefully: err on smaller size;
wenzelm
parents:
59080
diff
changeset
|
240 |
|
69358
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
69357
diff
changeset
|
241 |
def imitate_font_css(font: Font, |
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
69357
diff
changeset
|
242 |
family: String = Isabelle_Fonts.sans, |
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
69357
diff
changeset
|
243 |
scale: Double = 1.0): String = |
59183
ec83638b6bfb
imitate font more carefully: err on smaller size;
wenzelm
parents:
59080
diff
changeset
|
244 |
{ |
ec83638b6bfb
imitate font more carefully: err on smaller size;
wenzelm
parents:
59080
diff
changeset
|
245 |
val font1 = new Font(family, font.getStyle, font.getSize) |
59230 | 246 |
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
|
247 |
"font-family: " + family + "; font-size: " + (scale * rel_size * 100).toInt + "%;" |
53785 | 248 |
} |
69377 | 249 |
|
250 |
def use_isabelle_fonts() |
|
251 |
{ |
|
252 |
val default_font = label_font() |
|
253 |
val ui = UIManager.getDefaults |
|
69384
0c7d8b1b6594
more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents:
69378
diff
changeset
|
254 |
for (prop <- List( |
0c7d8b1b6594
more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents:
69378
diff
changeset
|
255 |
"CheckBoxMenuItem.font", |
0c7d8b1b6594
more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents:
69378
diff
changeset
|
256 |
"Label.font", |
0c7d8b1b6594
more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents:
69378
diff
changeset
|
257 |
"Menu.font", |
0c7d8b1b6594
more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents:
69378
diff
changeset
|
258 |
"MenuItem.font", |
0c7d8b1b6594
more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents:
69378
diff
changeset
|
259 |
"PopupMenu.font", |
0c7d8b1b6594
more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents:
69378
diff
changeset
|
260 |
"TextArea.font", |
0c7d8b1b6594
more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents:
69378
diff
changeset
|
261 |
"TextField.font", |
0c7d8b1b6594
more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents:
69378
diff
changeset
|
262 |
"TextPane.font", |
0c7d8b1b6594
more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents:
69378
diff
changeset
|
263 |
"Tooltip.font", |
0c7d8b1b6594
more Isabelle fonts: CheckBoxMenuItem.font notably for Windows L&F;
wenzelm
parents:
69378
diff
changeset
|
264 |
"Tree.font")) |
69377 | 265 |
{ |
266 |
val font = ui.get(prop) match { case font: Font => font case _ => default_font } |
|
267 |
ui.put(prop, GUI.imitate_font(font)) |
|
268 |
} |
|
269 |
} |
|
51619 | 270 |
} |