author | wenzelm |
Tue, 12 Aug 2014 12:06:22 +0200 | |
changeset 57908 | 1937603dbdf2 |
parent 57879 | 91e188508bc9 |
child 59080 | 611914621edb |
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 |
||
54962 | 10 |
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
|
11 |
import java.io.{FileInputStream, BufferedInputStream} |
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
12 |
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
|
13 |
KeyboardFocusManager} |
53785 | 14 |
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics} |
15 |
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
|
16 |
import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow, JDialog, |
56874 | 17 |
JButton, JTextField} |
51619 | 18 |
|
54709 | 19 |
import scala.collection.convert.WrapAsJava |
51619 | 20 |
import scala.swing.{ComboBox, TextArea, ScrollPane} |
21 |
import scala.swing.event.SelectionChanged |
|
22 |
||
23 |
||
24 |
object GUI |
|
25 |
{ |
|
26 |
/* Swing look-and-feel */ |
|
27 |
||
28 |
def get_laf(): String = |
|
29 |
{ |
|
30 |
if (Platform.is_windows || Platform.is_macos) |
|
31 |
UIManager.getSystemLookAndFeelClassName() |
|
32 |
else |
|
53850
b1bc857f2422
simplified default L&F -- Nimbus should be always available and GTK+ is not fully working yet;
wenzelm
parents:
53849
diff
changeset
|
33 |
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
|
34 |
.getOrElse(UIManager.getCrossPlatformLookAndFeelClassName()) |
51619 | 35 |
} |
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 |
|
51619 | 43 |
|
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
|
44 |
/* 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
|
45 |
|
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
|
46 |
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
|
47 |
{ |
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 |
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
|
49 |
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
|
50 |
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
|
51 |
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
|
52 |
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
|
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 |
|
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 |
|
54962 | 56 |
/* X11 window manager */ |
57 |
||
58 |
def window_manager(): Option[String] = |
|
59 |
{ |
|
54965 | 60 |
if (Platform.is_windows || Platform.is_macos) None |
61 |
else |
|
62 |
try { |
|
63 |
val XWM = Class.forName("sun.awt.X11.XWM", true, ClassLoader.getSystemClassLoader) |
|
64 |
val getWM = XWM.getDeclaredMethod("getWM") |
|
65 |
getWM.setAccessible(true) |
|
66 |
getWM.invoke(null) match { |
|
67 |
case null => None |
|
68 |
case wm => Some(wm.toString) |
|
69 |
} |
|
54962 | 70 |
} |
54965 | 71 |
catch { |
72 |
case _: ClassNotFoundException => None |
|
73 |
case _: NoSuchMethodException => None |
|
74 |
} |
|
54962 | 75 |
} |
76 |
||
77 |
||
51619 | 78 |
/* simple dialogs */ |
79 |
||
53714 | 80 |
def scrollable_text(txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false) |
81 |
: ScrollPane = |
|
51619 | 82 |
{ |
83 |
val text = new TextArea(txt) |
|
84 |
if (width > 0) text.columns = width |
|
53714 | 85 |
if (height > 0 && split_lines(txt).length > height) text.rows = height |
51619 | 86 |
text.editable = editable |
87 |
new ScrollPane(text) |
|
88 |
} |
|
89 |
||
90 |
private def simple_dialog(kind: Int, default_title: String, |
|
91 |
parent: Component, title: String, message: Seq[Any]) |
|
92 |
{ |
|
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
57044
diff
changeset
|
93 |
GUI_Thread.now { |
51619 | 94 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
95 |
JOptionPane.showMessageDialog(parent, |
|
96 |
java_message.toArray.asInstanceOf[Array[AnyRef]], |
|
97 |
if (title == null) default_title else title, kind) |
|
98 |
} |
|
99 |
} |
|
100 |
||
57639 | 101 |
def dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 102 |
simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message) |
103 |
||
57639 | 104 |
def warning_dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 105 |
simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message) |
106 |
||
57639 | 107 |
def error_dialog(parent: Component, title: String, message: Any*): Unit = |
51619 | 108 |
simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message) |
109 |
||
110 |
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
|
111 |
GUI_Thread.now { |
51619 | 112 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
113 |
JOptionPane.showConfirmDialog(parent, |
|
114 |
java_message.toArray.asInstanceOf[Array[AnyRef]], title, |
|
115 |
option_type, JOptionPane.QUESTION_MESSAGE) |
|
116 |
} |
|
117 |
||
118 |
||
119 |
/* zoom box */ |
|
120 |
||
57044 | 121 |
private val Zoom_Factor = "([0-9]+)%?".r |
122 |
||
123 |
abstract class Zoom_Box extends ComboBox[String]( |
|
51619 | 124 |
List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")) |
125 |
{ |
|
57044 | 126 |
def changed: Unit |
127 |
def factor: Int = parse(selection.item) |
|
128 |
||
129 |
private def parse(text: String): Int = |
|
51619 | 130 |
text match { |
57044 | 131 |
case Zoom_Factor(s) => |
51619 | 132 |
val i = Integer.parseInt(s) |
56874 | 133 |
if (10 <= i && i < 1000) i else 100 |
51619 | 134 |
case _ => 100 |
135 |
} |
|
136 |
||
57044 | 137 |
private def print(i: Int): String = i.toString + "%" |
51619 | 138 |
|
139 |
def set_item(i: Int) { |
|
140 |
peer.getEditor match { |
|
141 |
case null => |
|
142 |
case editor => editor.setItem(print(i)) |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x)) |
|
56874 | 147 |
peer.getEditor.getEditorComponent match { |
56888 | 148 |
case text: JTextField => text.setColumns(4) |
56874 | 149 |
case _ => |
150 |
} |
|
151 |
||
57044 | 152 |
selection.index = 3 |
153 |
||
51619 | 154 |
listenTo(selection) |
57044 | 155 |
reactions += { case SelectionChanged(_) => changed } |
51619 | 156 |
} |
157 |
||
158 |
||
53786 | 159 |
/* tooltip with multi-line support */ |
160 |
||
56622
891d1b8b64fb
clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents:
54965
diff
changeset
|
161 |
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
|
162 |
if (text == null || text == "") null |
891d1b8b64fb
clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents:
54965
diff
changeset
|
163 |
else "<html>" + HTML.encode(text) + "</html>" |
53786 | 164 |
|
165 |
||
51619 | 166 |
/* screen resolution */ |
167 |
||
168 |
def resolution_scale(): Double = Toolkit.getDefaultToolkit.getScreenResolution.toDouble / 72 |
|
169 |
def resolution_scale(i: Int): Int = (i.toDouble * resolution_scale()).round.toInt |
|
170 |
||
171 |
||
172 |
/* icon */ |
|
173 |
||
174 |
def isabelle_icon(): ImageIcon = |
|
54676
6b2ca4850b71
uniform use of transparent icons, as for main "apps";
wenzelm
parents:
54659
diff
changeset
|
175 |
new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle_transparent-32.gif")) |
51619 | 176 |
|
54709 | 177 |
def isabelle_icons(): List[ImageIcon] = |
178 |
for (icon <- List("isabelle/isabelle_transparent-32.gif", "isabelle/isabelle_transparent.gif")) |
|
179 |
yield new ImageIcon(getClass.getClassLoader.getResource(icon)) |
|
180 |
||
51619 | 181 |
def isabelle_image(): Image = isabelle_icon().getImage |
53712 | 182 |
|
54709 | 183 |
def isabelle_images(): java.util.List[Image] = |
184 |
WrapAsJava.seqAsJavaList(isabelle_icons.map(_.getImage)) |
|
185 |
||
53712 | 186 |
|
187 |
/* component hierachy */ |
|
188 |
||
189 |
def get_parent(component: Component): Option[Container] = |
|
190 |
component.getParent match { |
|
191 |
case null => None |
|
192 |
case parent => Some(parent) |
|
193 |
} |
|
194 |
||
195 |
def ancestors(component: Component): Iterator[Container] = new Iterator[Container] { |
|
196 |
private var next_elem = get_parent(component) |
|
197 |
def hasNext(): Boolean = next_elem.isDefined |
|
198 |
def next(): Container = |
|
199 |
next_elem match { |
|
200 |
case Some(parent) => |
|
201 |
next_elem = get_parent(parent) |
|
202 |
parent |
|
203 |
case None => Iterator.empty.next() |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
def parent_window(component: Component): Option[Window] = |
|
208 |
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
|
209 |
|
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
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
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
|
215 |
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
|
216 |
} |
53785 | 217 |
|
218 |
||
219 |
/* font operations */ |
|
220 |
||
221 |
def font_metrics(font: Font): LineMetrics = |
|
222 |
font.getLineMetrics("", new FontRenderContext(null, false, false)) |
|
223 |
||
54368 | 224 |
def imitate_font(family: String, font: Font, scale: Double = 1.0): Font = |
53785 | 225 |
{ |
226 |
val font1 = new Font(family, font.getStyle, font.getSize) |
|
54368 | 227 |
val size = scale * (font_metrics(font).getAscent / font_metrics(font1).getAscent * font.getSize) |
228 |
font1.deriveFont(size.round.toInt) |
|
53785 | 229 |
} |
230 |
||
231 |
def transform_font(font: Font, transform: AffineTransform): Font = |
|
232 |
{ |
|
233 |
import scala.collection.JavaConversions._ |
|
234 |
font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform))) |
|
235 |
} |
|
57908
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
236 |
|
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
237 |
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
|
238 |
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
|
239 |
|
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
240 |
def install_fonts() |
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 |
val ge = GraphicsEnvironment.getLocalGraphicsEnvironment() |
1937603dbdf2
separate Java FX modules -- no need to include jfxrt.jar by default;
wenzelm
parents:
57879
diff
changeset
|
243 |
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
|
244 |
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
|
245 |
} |
51619 | 246 |
} |
247 |