author | wenzelm |
Mon, 09 Dec 2013 22:02:42 +0100 | |
changeset 54709 | 87402674fe2f |
parent 54676 | 6b2ca4850b71 |
child 54962 | 993aab23894c |
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 |
53853
e8430d668f44
more quasi-generic PIDE modules (NB: Swing/JFX needs to be kept separate from non-GUI material);
wenzelm
parents:
53850
diff
changeset
|
2 |
Module: PIDE-GUI |
51619 | 3 |
Author: Makarius |
4 |
||
53712 | 5 |
Basic GUI tools (for AWT/Swing). |
51619 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
53785 | 11 |
import java.awt.{Image, Component, Container, Toolkit, Window, Font} |
12 |
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics} |
|
13 |
import java.awt.geom.AffineTransform |
|
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
|
14 |
import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow} |
51619 | 15 |
|
54709 | 16 |
import scala.collection.convert.WrapAsJava |
51619 | 17 |
import scala.swing.{ComboBox, TextArea, ScrollPane} |
18 |
import scala.swing.event.SelectionChanged |
|
19 |
||
20 |
||
21 |
object GUI |
|
22 |
{ |
|
23 |
/* Swing look-and-feel */ |
|
24 |
||
25 |
def get_laf(): String = |
|
26 |
{ |
|
27 |
if (Platform.is_windows || Platform.is_macos) |
|
28 |
UIManager.getSystemLookAndFeelClassName() |
|
29 |
else |
|
53850
b1bc857f2422
simplified default L&F -- Nimbus should be always available and GTK+ is not fully working yet;
wenzelm
parents:
53849
diff
changeset
|
30 |
UIManager.getInstalledLookAndFeels().find(_.getName == "Nimbus").map(_.getClassName) |
b1bc857f2422
simplified default L&F -- Nimbus should be always available and GTK+ is not fully working yet;
wenzelm
parents:
53849
diff
changeset
|
31 |
.getOrElse(UIManager.getCrossPlatformLookAndFeelClassName()) |
51619 | 32 |
} |
33 |
||
34 |
def init_laf(): Unit = UIManager.setLookAndFeel(get_laf()) |
|
35 |
||
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
|
36 |
def is_macos_laf(): Boolean = |
53849 | 37 |
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
|
38 |
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
|
39 |
|
51619 | 40 |
|
41 |
/* simple dialogs */ |
|
42 |
||
53714 | 43 |
def scrollable_text(txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false) |
44 |
: ScrollPane = |
|
51619 | 45 |
{ |
46 |
val text = new TextArea(txt) |
|
47 |
if (width > 0) text.columns = width |
|
53714 | 48 |
if (height > 0 && split_lines(txt).length > height) text.rows = height |
51619 | 49 |
text.editable = editable |
50 |
new ScrollPane(text) |
|
51 |
} |
|
52 |
||
53 |
private def simple_dialog(kind: Int, default_title: String, |
|
54 |
parent: Component, title: String, message: Seq[Any]) |
|
55 |
{ |
|
56 |
Swing_Thread.now { |
|
57 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
|
58 |
JOptionPane.showMessageDialog(parent, |
|
59 |
java_message.toArray.asInstanceOf[Array[AnyRef]], |
|
60 |
if (title == null) default_title else title, kind) |
|
61 |
} |
|
62 |
} |
|
63 |
||
64 |
def dialog(parent: Component, title: String, message: Any*) = |
|
65 |
simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message) |
|
66 |
||
67 |
def warning_dialog(parent: Component, title: String, message: Any*) = |
|
68 |
simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message) |
|
69 |
||
70 |
def error_dialog(parent: Component, title: String, message: Any*) = |
|
71 |
simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message) |
|
72 |
||
73 |
def confirm_dialog(parent: Component, title: String, option_type: Int, message: Any*): Int = |
|
74 |
Swing_Thread.now { |
|
75 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
|
76 |
JOptionPane.showConfirmDialog(parent, |
|
77 |
java_message.toArray.asInstanceOf[Array[AnyRef]], title, |
|
78 |
option_type, JOptionPane.QUESTION_MESSAGE) |
|
79 |
} |
|
80 |
||
81 |
||
82 |
/* zoom box */ |
|
83 |
||
84 |
class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String]( |
|
85 |
List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")) |
|
86 |
{ |
|
87 |
val Factor = "([0-9]+)%?".r |
|
88 |
def parse(text: String): Int = |
|
89 |
text match { |
|
90 |
case Factor(s) => |
|
91 |
val i = Integer.parseInt(s) |
|
92 |
if (10 <= i && i <= 1000) i else 100 |
|
93 |
case _ => 100 |
|
94 |
} |
|
95 |
||
96 |
def print(i: Int): String = i.toString + "%" |
|
97 |
||
98 |
def set_item(i: Int) { |
|
99 |
peer.getEditor match { |
|
100 |
case null => |
|
101 |
case editor => editor.setItem(print(i)) |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x)) |
|
106 |
reactions += { |
|
107 |
case SelectionChanged(_) => apply_factor(parse(selection.item)) |
|
108 |
} |
|
109 |
listenTo(selection) |
|
110 |
selection.index = 3 |
|
111 |
prototypeDisplayValue = Some("00000%") |
|
112 |
} |
|
113 |
||
114 |
||
53786 | 115 |
/* tooltip with multi-line support */ |
116 |
||
117 |
def tooltip_lines(lines: List[String]): String = |
|
118 |
if (lines.isEmpty) null |
|
119 |
else "<html><pre>" + HTML.encode(cat_lines(lines)) + "</pre></html>" |
|
120 |
||
121 |
||
51619 | 122 |
/* screen resolution */ |
123 |
||
124 |
def resolution_scale(): Double = Toolkit.getDefaultToolkit.getScreenResolution.toDouble / 72 |
|
125 |
def resolution_scale(i: Int): Int = (i.toDouble * resolution_scale()).round.toInt |
|
126 |
||
127 |
||
128 |
/* icon */ |
|
129 |
||
130 |
def isabelle_icon(): ImageIcon = |
|
54676
6b2ca4850b71
uniform use of transparent icons, as for main "apps";
wenzelm
parents:
54659
diff
changeset
|
131 |
new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle_transparent-32.gif")) |
51619 | 132 |
|
54709 | 133 |
def isabelle_icons(): List[ImageIcon] = |
134 |
for (icon <- List("isabelle/isabelle_transparent-32.gif", "isabelle/isabelle_transparent.gif")) |
|
135 |
yield new ImageIcon(getClass.getClassLoader.getResource(icon)) |
|
136 |
||
51619 | 137 |
def isabelle_image(): Image = isabelle_icon().getImage |
53712 | 138 |
|
54709 | 139 |
def isabelle_images(): java.util.List[Image] = |
140 |
WrapAsJava.seqAsJavaList(isabelle_icons.map(_.getImage)) |
|
141 |
||
53712 | 142 |
|
143 |
/* component hierachy */ |
|
144 |
||
145 |
def get_parent(component: Component): Option[Container] = |
|
146 |
component.getParent match { |
|
147 |
case null => None |
|
148 |
case parent => Some(parent) |
|
149 |
} |
|
150 |
||
151 |
def ancestors(component: Component): Iterator[Container] = new Iterator[Container] { |
|
152 |
private var next_elem = get_parent(component) |
|
153 |
def hasNext(): Boolean = next_elem.isDefined |
|
154 |
def next(): Container = |
|
155 |
next_elem match { |
|
156 |
case Some(parent) => |
|
157 |
next_elem = get_parent(parent) |
|
158 |
parent |
|
159 |
case None => Iterator.empty.next() |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
def parent_window(component: Component): Option[Window] = |
|
164 |
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
|
165 |
|
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
|
166 |
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
|
167 |
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
|
168 |
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
|
169 |
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
|
170 |
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
|
171 |
} |
53785 | 172 |
|
173 |
||
174 |
/* font operations */ |
|
175 |
||
176 |
def font_metrics(font: Font): LineMetrics = |
|
177 |
font.getLineMetrics("", new FontRenderContext(null, false, false)) |
|
178 |
||
54368 | 179 |
def imitate_font(family: String, font: Font, scale: Double = 1.0): Font = |
53785 | 180 |
{ |
181 |
val font1 = new Font(family, font.getStyle, font.getSize) |
|
54368 | 182 |
val size = scale * (font_metrics(font).getAscent / font_metrics(font1).getAscent * font.getSize) |
183 |
font1.deriveFont(size.round.toInt) |
|
53785 | 184 |
} |
185 |
||
186 |
def transform_font(font: Font, transform: AffineTransform): Font = |
|
187 |
{ |
|
188 |
import scala.collection.JavaConversions._ |
|
189 |
font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform))) |
|
190 |
} |
|
51619 | 191 |
} |
192 |