author | wenzelm |
Sun, 22 Sep 2013 14:30:34 +0200 | |
changeset 53783 | f5e9d182f645 |
parent 53778 | src/Pure/System/gui.scala@29eaacff4078 |
child 53785 | e64edcc2f8bf |
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 |
||
9 |
||
53712 | 10 |
import java.awt.{Image, Component, Container, Toolkit, Window} |
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
|
11 |
import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow} |
51619 | 12 |
|
13 |
import scala.swing.{ComboBox, TextArea, ScrollPane} |
|
14 |
import scala.swing.event.SelectionChanged |
|
15 |
||
16 |
||
17 |
object GUI |
|
18 |
{ |
|
19 |
/* Swing look-and-feel */ |
|
20 |
||
21 |
def get_laf(): String = |
|
22 |
{ |
|
23 |
def laf(name: String): Option[String] = |
|
24 |
UIManager.getInstalledLookAndFeels().find(_.getName == name).map(_.getClassName) |
|
25 |
||
26 |
if (Platform.is_windows || Platform.is_macos) |
|
27 |
UIManager.getSystemLookAndFeelClassName() |
|
28 |
else |
|
29 |
laf("Nimbus") orElse laf("GTK+") getOrElse |
|
30 |
UIManager.getCrossPlatformLookAndFeelClassName() |
|
31 |
} |
|
32 |
||
33 |
def init_laf(): Unit = UIManager.setLookAndFeel(get_laf()) |
|
34 |
||
35 |
||
36 |
/* simple dialogs */ |
|
37 |
||
53714 | 38 |
def scrollable_text(txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false) |
39 |
: ScrollPane = |
|
51619 | 40 |
{ |
41 |
val text = new TextArea(txt) |
|
42 |
if (width > 0) text.columns = width |
|
53714 | 43 |
if (height > 0 && split_lines(txt).length > height) text.rows = height |
51619 | 44 |
text.editable = editable |
45 |
new ScrollPane(text) |
|
46 |
} |
|
47 |
||
48 |
private def simple_dialog(kind: Int, default_title: String, |
|
49 |
parent: Component, title: String, message: Seq[Any]) |
|
50 |
{ |
|
51 |
Swing_Thread.now { |
|
52 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
|
53 |
JOptionPane.showMessageDialog(parent, |
|
54 |
java_message.toArray.asInstanceOf[Array[AnyRef]], |
|
55 |
if (title == null) default_title else title, kind) |
|
56 |
} |
|
57 |
} |
|
58 |
||
59 |
def dialog(parent: Component, title: String, message: Any*) = |
|
60 |
simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message) |
|
61 |
||
62 |
def warning_dialog(parent: Component, title: String, message: Any*) = |
|
63 |
simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message) |
|
64 |
||
65 |
def error_dialog(parent: Component, title: String, message: Any*) = |
|
66 |
simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message) |
|
67 |
||
68 |
def confirm_dialog(parent: Component, title: String, option_type: Int, message: Any*): Int = |
|
69 |
Swing_Thread.now { |
|
70 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
|
71 |
JOptionPane.showConfirmDialog(parent, |
|
72 |
java_message.toArray.asInstanceOf[Array[AnyRef]], title, |
|
73 |
option_type, JOptionPane.QUESTION_MESSAGE) |
|
74 |
} |
|
75 |
||
76 |
||
77 |
/* zoom box */ |
|
78 |
||
79 |
class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String]( |
|
80 |
List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")) |
|
81 |
{ |
|
82 |
val Factor = "([0-9]+)%?".r |
|
83 |
def parse(text: String): Int = |
|
84 |
text match { |
|
85 |
case Factor(s) => |
|
86 |
val i = Integer.parseInt(s) |
|
87 |
if (10 <= i && i <= 1000) i else 100 |
|
88 |
case _ => 100 |
|
89 |
} |
|
90 |
||
91 |
def print(i: Int): String = i.toString + "%" |
|
92 |
||
93 |
def set_item(i: Int) { |
|
94 |
peer.getEditor match { |
|
95 |
case null => |
|
96 |
case editor => editor.setItem(print(i)) |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x)) |
|
101 |
reactions += { |
|
102 |
case SelectionChanged(_) => apply_factor(parse(selection.item)) |
|
103 |
} |
|
104 |
listenTo(selection) |
|
105 |
selection.index = 3 |
|
106 |
prototypeDisplayValue = Some("00000%") |
|
107 |
} |
|
108 |
||
109 |
||
110 |
/* screen resolution */ |
|
111 |
||
112 |
def resolution_scale(): Double = Toolkit.getDefaultToolkit.getScreenResolution.toDouble / 72 |
|
113 |
def resolution_scale(i: Int): Int = (i.toDouble * resolution_scale()).round.toInt |
|
114 |
||
115 |
||
116 |
/* icon */ |
|
117 |
||
118 |
def isabelle_icon(): ImageIcon = |
|
53452
8181bc357dc4
more portable access to icon -- avoid Isabelle_System which is not yet initialized in bootstrap;
wenzelm
parents:
51619
diff
changeset
|
119 |
new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle.gif")) |
51619 | 120 |
|
121 |
def isabelle_image(): Image = isabelle_icon().getImage |
|
53712 | 122 |
|
123 |
||
124 |
/* component hierachy */ |
|
125 |
||
126 |
def get_parent(component: Component): Option[Container] = |
|
127 |
component.getParent match { |
|
128 |
case null => None |
|
129 |
case parent => Some(parent) |
|
130 |
} |
|
131 |
||
132 |
def ancestors(component: Component): Iterator[Container] = new Iterator[Container] { |
|
133 |
private var next_elem = get_parent(component) |
|
134 |
def hasNext(): Boolean = next_elem.isDefined |
|
135 |
def next(): Container = |
|
136 |
next_elem match { |
|
137 |
case Some(parent) => |
|
138 |
next_elem = get_parent(parent) |
|
139 |
parent |
|
140 |
case None => Iterator.empty.next() |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
def parent_window(component: Component): Option[Window] = |
|
145 |
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
|
146 |
|
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
|
147 |
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
|
148 |
parent_window(component) match { |
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
|
149 |
case Some(window: JWindow) => Some(window.getLayeredPane) |
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
|
150 |
case Some(frame: JFrame) => Some(frame.getLayeredPane) |
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
|
151 |
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
|
152 |
} |
51619 | 153 |
} |
154 |