author | wenzelm |
Sat, 26 Apr 2014 15:33:13 +0200 | |
changeset 56752 | 72b4205f4de9 |
parent 56622 | 891d1b8b64fb |
child 56874 | 5d78bce4f5a4 |
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} |
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
|
12 |
import java.awt.{Image, Component, Container, Toolkit, Window, Font, KeyboardFocusManager} |
53785 | 13 |
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics} |
14 |
import java.awt.geom.AffineTransform |
|
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
|
15 |
import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow, JButton} |
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 |
|
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
|
42 |
/* 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
|
43 |
|
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 |
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
|
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 |
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
|
47 |
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
|
48 |
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
|
49 |
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
|
50 |
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
|
51 |
} |
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 |
|
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 |
|
54962 | 54 |
/* X11 window manager */ |
55 |
||
56 |
def window_manager(): Option[String] = |
|
57 |
{ |
|
54965 | 58 |
if (Platform.is_windows || Platform.is_macos) None |
59 |
else |
|
60 |
try { |
|
61 |
val XWM = Class.forName("sun.awt.X11.XWM", true, ClassLoader.getSystemClassLoader) |
|
62 |
val getWM = XWM.getDeclaredMethod("getWM") |
|
63 |
getWM.setAccessible(true) |
|
64 |
getWM.invoke(null) match { |
|
65 |
case null => None |
|
66 |
case wm => Some(wm.toString) |
|
67 |
} |
|
54962 | 68 |
} |
54965 | 69 |
catch { |
70 |
case _: ClassNotFoundException => None |
|
71 |
case _: NoSuchMethodException => None |
|
72 |
} |
|
54962 | 73 |
} |
74 |
||
75 |
||
51619 | 76 |
/* simple dialogs */ |
77 |
||
53714 | 78 |
def scrollable_text(txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false) |
79 |
: ScrollPane = |
|
51619 | 80 |
{ |
81 |
val text = new TextArea(txt) |
|
82 |
if (width > 0) text.columns = width |
|
53714 | 83 |
if (height > 0 && split_lines(txt).length > height) text.rows = height |
51619 | 84 |
text.editable = editable |
85 |
new ScrollPane(text) |
|
86 |
} |
|
87 |
||
88 |
private def simple_dialog(kind: Int, default_title: String, |
|
89 |
parent: Component, title: String, message: Seq[Any]) |
|
90 |
{ |
|
91 |
Swing_Thread.now { |
|
92 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
|
93 |
JOptionPane.showMessageDialog(parent, |
|
94 |
java_message.toArray.asInstanceOf[Array[AnyRef]], |
|
95 |
if (title == null) default_title else title, kind) |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
def dialog(parent: Component, title: String, message: Any*) = |
|
100 |
simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message) |
|
101 |
||
102 |
def warning_dialog(parent: Component, title: String, message: Any*) = |
|
103 |
simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message) |
|
104 |
||
105 |
def error_dialog(parent: Component, title: String, message: Any*) = |
|
106 |
simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message) |
|
107 |
||
108 |
def confirm_dialog(parent: Component, title: String, option_type: Int, message: Any*): Int = |
|
109 |
Swing_Thread.now { |
|
110 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
|
111 |
JOptionPane.showConfirmDialog(parent, |
|
112 |
java_message.toArray.asInstanceOf[Array[AnyRef]], title, |
|
113 |
option_type, JOptionPane.QUESTION_MESSAGE) |
|
114 |
} |
|
115 |
||
116 |
||
117 |
/* zoom box */ |
|
118 |
||
119 |
class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String]( |
|
120 |
List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")) |
|
121 |
{ |
|
122 |
val Factor = "([0-9]+)%?".r |
|
123 |
def parse(text: String): Int = |
|
124 |
text match { |
|
125 |
case Factor(s) => |
|
126 |
val i = Integer.parseInt(s) |
|
127 |
if (10 <= i && i <= 1000) i else 100 |
|
128 |
case _ => 100 |
|
129 |
} |
|
130 |
||
131 |
def print(i: Int): String = i.toString + "%" |
|
132 |
||
133 |
def set_item(i: Int) { |
|
134 |
peer.getEditor match { |
|
135 |
case null => |
|
136 |
case editor => editor.setItem(print(i)) |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x)) |
|
141 |
reactions += { |
|
142 |
case SelectionChanged(_) => apply_factor(parse(selection.item)) |
|
143 |
} |
|
144 |
listenTo(selection) |
|
145 |
selection.index = 3 |
|
146 |
prototypeDisplayValue = Some("00000%") |
|
147 |
} |
|
148 |
||
149 |
||
53786 | 150 |
/* tooltip with multi-line support */ |
151 |
||
56622
891d1b8b64fb
clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents:
54965
diff
changeset
|
152 |
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
|
153 |
if (text == null || text == "") null |
891d1b8b64fb
clarified tooltip_lines: HTML.encode already takes care of newline (but not space);
wenzelm
parents:
54965
diff
changeset
|
154 |
else "<html>" + HTML.encode(text) + "</html>" |
53786 | 155 |
|
156 |
||
51619 | 157 |
/* screen resolution */ |
158 |
||
159 |
def resolution_scale(): Double = Toolkit.getDefaultToolkit.getScreenResolution.toDouble / 72 |
|
160 |
def resolution_scale(i: Int): Int = (i.toDouble * resolution_scale()).round.toInt |
|
161 |
||
162 |
||
163 |
/* icon */ |
|
164 |
||
165 |
def isabelle_icon(): ImageIcon = |
|
54676
6b2ca4850b71
uniform use of transparent icons, as for main "apps";
wenzelm
parents:
54659
diff
changeset
|
166 |
new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle_transparent-32.gif")) |
51619 | 167 |
|
54709 | 168 |
def isabelle_icons(): List[ImageIcon] = |
169 |
for (icon <- List("isabelle/isabelle_transparent-32.gif", "isabelle/isabelle_transparent.gif")) |
|
170 |
yield new ImageIcon(getClass.getClassLoader.getResource(icon)) |
|
171 |
||
51619 | 172 |
def isabelle_image(): Image = isabelle_icon().getImage |
53712 | 173 |
|
54709 | 174 |
def isabelle_images(): java.util.List[Image] = |
175 |
WrapAsJava.seqAsJavaList(isabelle_icons.map(_.getImage)) |
|
176 |
||
53712 | 177 |
|
178 |
/* component hierachy */ |
|
179 |
||
180 |
def get_parent(component: Component): Option[Container] = |
|
181 |
component.getParent match { |
|
182 |
case null => None |
|
183 |
case parent => Some(parent) |
|
184 |
} |
|
185 |
||
186 |
def ancestors(component: Component): Iterator[Container] = new Iterator[Container] { |
|
187 |
private var next_elem = get_parent(component) |
|
188 |
def hasNext(): Boolean = next_elem.isDefined |
|
189 |
def next(): Container = |
|
190 |
next_elem match { |
|
191 |
case Some(parent) => |
|
192 |
next_elem = get_parent(parent) |
|
193 |
parent |
|
194 |
case None => Iterator.empty.next() |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
def parent_window(component: Component): Option[Window] = |
|
199 |
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
|
200 |
|
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
|
201 |
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
|
202 |
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
|
203 |
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
|
204 |
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
|
205 |
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
|
206 |
} |
53785 | 207 |
|
208 |
||
209 |
/* font operations */ |
|
210 |
||
211 |
def font_metrics(font: Font): LineMetrics = |
|
212 |
font.getLineMetrics("", new FontRenderContext(null, false, false)) |
|
213 |
||
54368 | 214 |
def imitate_font(family: String, font: Font, scale: Double = 1.0): Font = |
53785 | 215 |
{ |
216 |
val font1 = new Font(family, font.getStyle, font.getSize) |
|
54368 | 217 |
val size = scale * (font_metrics(font).getAscent / font_metrics(font1).getAscent * font.getSize) |
218 |
font1.deriveFont(size.round.toInt) |
|
53785 | 219 |
} |
220 |
||
221 |
def transform_font(font: Font, transform: AffineTransform): Font = |
|
222 |
{ |
|
223 |
import scala.collection.JavaConversions._ |
|
224 |
font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform))) |
|
225 |
} |
|
51619 | 226 |
} |
227 |