author | wenzelm |
Tue, 27 Oct 2015 23:18:32 +0100 | |
changeset 61523 | 9ad1fccbba96 |
parent 60998 | 42cebb02b5ae |
child 61529 | 82fc5a6231a2 |
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} |
57908
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
10 |
import java.io.{FileInputStream, BufferedInputStream} |
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
11 |
import java.awt.{GraphicsEnvironment, Image, Component, Container, Toolkit, Window, Font, |
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
12 |
KeyboardFocusManager} |
53785 | 13 |
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics} |
14 |
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
|
15 |
import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow, JDialog, |
56874 | 16 |
JButton, JTextField} |
51619 | 17 |
|
54709 | 18 |
import scala.collection.convert.WrapAsJava |
51619 | 19 |
import scala.swing.{ComboBox, TextArea, ScrollPane} |
20 |
import scala.swing.event.SelectionChanged |
|
21 |
||
22 |
||
23 |
object GUI |
|
24 |
{ |
|
25 |
/* Swing look-and-feel */ |
|
26 |
||
59201
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
27 |
def find_laf(name: String): Option[String] = |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
28 |
UIManager.getInstalledLookAndFeels(). |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
29 |
find(c => c.getName == name || c.getClassName == name). |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
30 |
map(_.getClassName) |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
31 |
|
51619 | 32 |
def get_laf(): String = |
59201
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
33 |
find_laf(System.getProperty("isabelle.laf")) getOrElse { |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
34 |
if (Platform.is_windows || Platform.is_macos) |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
35 |
UIManager.getSystemLookAndFeelClassName() |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
36 |
else |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
37 |
find_laf("Nimbus") getOrElse |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
38 |
UIManager.getCrossPlatformLookAndFeelClassName() |
702e0971d617
added system property isabelle.laf, notably for initial system dialog;
wenzelm
parents:
59183
diff
changeset
|
39 |
} |
51619 | 40 |
|
41 |
def init_laf(): Unit = UIManager.setLookAndFeel(get_laf()) |
|
42 |
||
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
|
43 |
def is_macos_laf(): Boolean = |
53849 | 44 |
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
|
45 |
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
|
46 |
|
60998 | 47 |
def is_windows_laf(): Boolean = |
48 |
Platform.is_windows && |
|
49 |
UIManager.getSystemLookAndFeelClassName() == UIManager.getLookAndFeel.getClass.getName |
|
50 |
||
51619 | 51 |
|
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
|
52 |
/* 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
|
53 |
|
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 |
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
|
55 |
{ |
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 |
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
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
} |
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 |
|
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 |
|
54962 | 64 |
/* X11 window manager */ |
65 |
||
66 |
def window_manager(): Option[String] = |
|
67 |
{ |
|
54965 | 68 |
if (Platform.is_windows || Platform.is_macos) None |
69 |
else |
|
70 |
try { |
|
59080
611914621edb
added Untyped.method convenience (for *this* class only);
wenzelm
parents:
57908
diff
changeset
|
71 |
val wm = |
611914621edb
added Untyped.method convenience (for *this* class only);
wenzelm
parents:
57908
diff
changeset
|
72 |
Untyped.method(Class.forName("sun.awt.X11.XWM", true, ClassLoader.getSystemClassLoader), |
611914621edb
added Untyped.method convenience (for *this* class only);
wenzelm
parents:
57908
diff
changeset
|
73 |
"getWM").invoke(null) |
611914621edb
added Untyped.method convenience (for *this* class only);
wenzelm
parents:
57908
diff
changeset
|
74 |
if (wm == null) None |
611914621edb
added Untyped.method convenience (for *this* class only);
wenzelm
parents:
57908
diff
changeset
|
75 |
else Some(wm.toString) |
54962 | 76 |
} |
54965 | 77 |
catch { |
78 |
case _: ClassNotFoundException => None |
|
79 |
case _: NoSuchMethodException => None |
|
80 |
} |
|
54962 | 81 |
} |
82 |
||
83 |
||
51619 | 84 |
/* simple dialogs */ |
85 |
||
59671
9715eb8e9408
more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents:
59286
diff
changeset
|
86 |
def scrollable_text(raw_txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false) |
53714 | 87 |
: ScrollPane = |
51619 | 88 |
{ |
59671
9715eb8e9408
more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents:
59286
diff
changeset
|
89 |
val txt = Output.clean_yxml(raw_txt) |
51619 | 90 |
val text = new TextArea(txt) |
91 |
if (width > 0) text.columns = width |
|
53714 | 92 |
if (height > 0 && split_lines(txt).length > height) text.rows = height |
51619 | 93 |
text.editable = editable |
94 |
new ScrollPane(text) |
|
95 |
} |
|
96 |
||
97 |
private def simple_dialog(kind: Int, default_title: String, |
|
98 |
parent: Component, title: String, message: Seq[Any]) |
|
99 |
{ |
|
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57044
diff
changeset
|
100 |
GUI_Thread.now { |
51619 | 101 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
102 |
JOptionPane.showMessageDialog(parent, |
|
103 |
java_message.toArray.asInstanceOf[Array[AnyRef]], |
|
104 |
if (title == null) default_title else title, kind) |
|
105 |
} |
|
106 |
} |
|
107 |
||
57639 | 108 |
def dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 109 |
simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message) |
110 |
||
57639 | 111 |
def warning_dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 112 |
simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message) |
113 |
||
57639 | 114 |
def error_dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 115 |
simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message) |
116 |
||
117 |
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
|
118 |
GUI_Thread.now { |
51619 | 119 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
120 |
JOptionPane.showConfirmDialog(parent, |
|
121 |
java_message.toArray.asInstanceOf[Array[AnyRef]], title, |
|
122 |
option_type, JOptionPane.QUESTION_MESSAGE) |
|
123 |
} |
|
124 |
||
125 |
||
126 |
/* zoom box */ |
|
127 |
||
57044 | 128 |
private val Zoom_Factor = "([0-9]+)%?".r |
129 |
||
130 |
abstract class Zoom_Box extends ComboBox[String]( |
|
51619 | 131 |
List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")) |
132 |
{ |
|
57044 | 133 |
def changed: Unit |
134 |
def factor: Int = parse(selection.item) |
|
135 |
||
136 |
private def parse(text: String): Int = |
|
51619 | 137 |
text match { |
57044 | 138 |
case Zoom_Factor(s) => |
51619 | 139 |
val i = Integer.parseInt(s) |
56874 | 140 |
if (10 <= i && i < 1000) i else 100 |
51619 | 141 |
case _ => 100 |
142 |
} |
|
143 |
||
57044 | 144 |
private def print(i: Int): String = i.toString + "%" |
51619 | 145 |
|
146 |
def set_item(i: Int) { |
|
147 |
peer.getEditor match { |
|
148 |
case null => |
|
149 |
case editor => editor.setItem(print(i)) |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x)) |
|
56874 | 154 |
peer.getEditor.getEditorComponent match { |
56888 | 155 |
case text: JTextField => text.setColumns(4) |
56874 | 156 |
case _ => |
157 |
} |
|
158 |
||
57044 | 159 |
selection.index = 3 |
160 |
||
51619 | 161 |
listenTo(selection) |
57044 | 162 |
reactions += { case SelectionChanged(_) => changed } |
51619 | 163 |
} |
164 |
||
165 |
||
53786 | 166 |
/* tooltip with multi-line support */ |
167 |
||
56622
891d1b8b64fb
clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents:
54965
diff
changeset
|
168 |
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
|
169 |
if (text == null || text == "") null |
891d1b8b64fb
clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents:
54965
diff
changeset
|
170 |
else "<html>" + HTML.encode(text) + "</html>" |
53786 | 171 |
|
172 |
||
51619 | 173 |
/* icon */ |
174 |
||
175 |
def isabelle_icon(): ImageIcon = |
|
54676
6b2ca4850b71
uniform use of transparent icons, as for main "apps";
wenzelm
parents:
54659
diff
changeset
|
176 |
new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle_transparent-32.gif")) |
51619 | 177 |
|
54709 | 178 |
def isabelle_icons(): List[ImageIcon] = |
179 |
for (icon <- List("isabelle/isabelle_transparent-32.gif", "isabelle/isabelle_transparent.gif")) |
|
180 |
yield new ImageIcon(getClass.getClassLoader.getResource(icon)) |
|
181 |
||
51619 | 182 |
def isabelle_image(): Image = isabelle_icon().getImage |
53712 | 183 |
|
184 |
||
185 |
/* component hierachy */ |
|
186 |
||
187 |
def get_parent(component: Component): Option[Container] = |
|
188 |
component.getParent match { |
|
189 |
case null => None |
|
190 |
case parent => Some(parent) |
|
191 |
} |
|
192 |
||
193 |
def ancestors(component: Component): Iterator[Container] = new Iterator[Container] { |
|
194 |
private var next_elem = get_parent(component) |
|
195 |
def hasNext(): Boolean = next_elem.isDefined |
|
196 |
def next(): Container = |
|
197 |
next_elem match { |
|
198 |
case Some(parent) => |
|
199 |
next_elem = get_parent(parent) |
|
200 |
parent |
|
201 |
case None => Iterator.empty.next() |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
def parent_window(component: Component): Option[Window] = |
|
206 |
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
|
207 |
|
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
|
208 |
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
|
209 |
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
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
} |
53785 | 215 |
|
216 |
||
217 |
/* font operations */ |
|
218 |
||
59230 | 219 |
def line_metrics(font: Font): LineMetrics = |
53785 | 220 |
font.getLineMetrics("", new FontRenderContext(null, false, false)) |
221 |
||
59286
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59230
diff
changeset
|
222 |
def imitate_font(font: Font, family: String, scale: Double = 1.0): Font = |
53785 | 223 |
{ |
224 |
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
|
225 |
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
|
226 |
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
|
227 |
} |
ec83638b6bfb
imitate font more carefully: err on smaller size;
wenzelm
parents:
59080
diff
changeset
|
228 |
|
59286
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59230
diff
changeset
|
229 |
def imitate_font_css(font: Font, family: String, scale: Double = 1.0): String = |
59183
ec83638b6bfb
imitate font more carefully: err on smaller size;
wenzelm
parents:
59080
diff
changeset
|
230 |
{ |
ec83638b6bfb
imitate font more carefully: err on smaller size;
wenzelm
parents:
59080
diff
changeset
|
231 |
val font1 = new Font(family, font.getStyle, font.getSize) |
59230 | 232 |
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
|
233 |
"font-family: " + family + "; font-size: " + (scale * rel_size * 100).toInt + "%;" |
53785 | 234 |
} |
235 |
||
236 |
def transform_font(font: Font, transform: AffineTransform): Font = |
|
237 |
{ |
|
238 |
import scala.collection.JavaConversions._ |
|
239 |
font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform))) |
|
240 |
} |
|
57908
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
241 |
|
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
242 |
def font(family: String = "IsabelleText", size: Int = 1, bold: Boolean = false): Font = |
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
243 |
new Font(family, if (bold) Font.BOLD else Font.PLAIN, size) |
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
244 |
|
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
245 |
def install_fonts() |
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
246 |
{ |
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
247 |
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment() |
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
248 |
for (font <- Path.split(Isabelle_System.getenv_strict("ISABELLE_FONTS"))) |
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
249 |
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, font.file)) |
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
250 |
} |
51619 | 251 |
} |
252 |