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