diff --git a/src/Pure/GUI/gui.scala b/src/Pure/GUI/gui.scala --- a/src/Pure/GUI/gui.scala +++ b/src/Pure/GUI/gui.scala @@ -1,270 +1,269 @@ /* Title: Pure/GUI/gui.scala Author: Makarius Basic GUI tools (for AWT/Swing). */ package isabelle import java.lang.{ClassLoader, ClassNotFoundException, NoSuchMethodException} import java.awt.{Image, Component, Container, Toolkit, Window, Font, KeyboardFocusManager} import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics} import java.awt.geom.AffineTransform import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow, JDialog, JButton, JTextField, JLabel} + +import scala.collection.JavaConverters import scala.swing.{ComboBox, TextArea, ScrollPane} import scala.swing.event.SelectionChanged object GUI { /* Swing look-and-feel */ def find_laf(name: String): Option[String] = UIManager.getInstalledLookAndFeels(). find(c => c.getName == name || c.getClassName == name). map(_.getClassName) def get_laf(): String = find_laf(System.getProperty("isabelle.laf")) getOrElse { if (Platform.is_windows || Platform.is_macos) UIManager.getSystemLookAndFeelClassName() else UIManager.getCrossPlatformLookAndFeelClassName() } def init_laf(): Unit = UIManager.setLookAndFeel(get_laf()) def is_macos_laf(): Boolean = Platform.is_macos && UIManager.getSystemLookAndFeelClassName() == UIManager.getLookAndFeel.getClass.getName def is_windows_laf(): Boolean = Platform.is_windows && UIManager.getSystemLookAndFeelClassName() == UIManager.getLookAndFeel.getClass.getName /* plain focus traversal, notably for text fields */ def plain_focus_traversal(component: Component) { val dummy_button = new JButton def apply(id: Int): Unit = component.setFocusTraversalKeys(id, dummy_button.getFocusTraversalKeys(id)) apply(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS) apply(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS) } /* simple dialogs */ def scrollable_text(raw_txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false) : ScrollPane = { val txt = Output.clean_yxml(raw_txt) val text = new TextArea(txt) if (width > 0) text.columns = width if (height > 0 && split_lines(txt).length > height) text.rows = height text.editable = editable new ScrollPane(text) } private def simple_dialog(kind: Int, default_title: String, parent: Component, title: String, message: Iterable[Any]) { GUI_Thread.now { val java_message = message.iterator.map({ case x: scala.swing.Component => x.peer case x => x }). toArray.asInstanceOf[Array[AnyRef]] JOptionPane.showMessageDialog(parent, java_message, if (title == null) default_title else title, kind) } } def dialog(parent: Component, title: String, message: Any*): Unit = simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message) def warning_dialog(parent: Component, title: String, message: Any*): Unit = simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message) def error_dialog(parent: Component, title: String, message: Any*): Unit = simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message) def confirm_dialog(parent: Component, title: String, option_type: Int, message: Any*): Int = GUI_Thread.now { val java_message = message map { case x: scala.swing.Component => x.peer case x => x } JOptionPane.showConfirmDialog(parent, java_message.toArray.asInstanceOf[Array[AnyRef]], title, option_type, JOptionPane.QUESTION_MESSAGE) } /* zoom box */ private val Zoom_Factor = "([0-9]+)%?".r abstract class Zoom_Box extends ComboBox[String]( List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")) { def changed: Unit def factor: Int = parse(selection.item) private def parse(text: String): Int = text match { case Zoom_Factor(s) => val i = Integer.parseInt(s) if (10 <= i && i < 1000) i else 100 case _ => 100 } private def print(i: Int): String = i.toString + "%" def set_item(i: Int) { peer.getEditor match { case null => case editor => editor.setItem(print(i)) } } makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x)) peer.getEditor.getEditorComponent match { case text: JTextField => text.setColumns(4) case _ => } selection.index = 3 listenTo(selection) reactions += { case SelectionChanged(_) => changed } } /* tooltip with multi-line support */ def tooltip_lines(text: String): String = if (text == null || text == "") null else "" + HTML.output(text) + "" /* icon */ def isabelle_icon(): ImageIcon = new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle_transparent-32.gif")) def isabelle_icons(): List[ImageIcon] = for (icon <- List("isabelle/isabelle_transparent-32.gif", "isabelle/isabelle_transparent.gif")) yield new ImageIcon(getClass.getClassLoader.getResource(icon)) def isabelle_image(): Image = isabelle_icon().getImage /* component hierachy */ def get_parent(component: Component): Option[Container] = component.getParent match { case null => None case parent => Some(parent) } def ancestors(component: Component): Iterator[Container] = new Iterator[Container] { private var next_elem = get_parent(component) def hasNext(): Boolean = next_elem.isDefined def next(): Container = next_elem match { case Some(parent) => next_elem = get_parent(parent) parent case None => Iterator.empty.next() } } def parent_window(component: Component): Option[Window] = ancestors(component).collectFirst({ case x: Window => x }) def layered_pane(component: Component): Option[JLayeredPane] = parent_window(component) match { case Some(w: JWindow) => Some(w.getLayeredPane) case Some(w: JFrame) => Some(w.getLayeredPane) case Some(w: JDialog) => Some(w.getLayeredPane) case _ => None } def traverse_components(component: Component, apply: Component => Unit) { def traverse(comp: Component) { apply(comp) comp match { case cont: Container => for (i <- 0 until cont.getComponentCount) traverse(cont.getComponent(i)) case _ => } } traverse(component) } /* font operations */ def copy_font(font: Font): Font = if (font == null) null else new Font(font.getFamily, font.getStyle, font.getSize) def line_metrics(font: Font): LineMetrics = font.getLineMetrics("", new FontRenderContext(null, false, false)) def transform_font(font: Font, transform: AffineTransform): Font = - { - import scala.collection.JavaConversions._ - font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform))) - } + font.deriveFont(java.util.Map.of(TextAttribute.TRANSFORM, new TransformAttribute(transform))) def font(family: String = Isabelle_Fonts.sans, size: Int = 1, bold: Boolean = false): Font = new Font(family, if (bold) Font.BOLD else Font.PLAIN, size) def label_font(): Font = (new JLabel).getFont /* Isabelle fonts */ def imitate_font(font: Font, family: String = Isabelle_Fonts.sans, scale: Double = 1.0): Font = { val font1 = new Font(family, font.getStyle, font.getSize) val rel_size = line_metrics(font).getHeight.toDouble / line_metrics(font1).getHeight new Font(family, font.getStyle, (scale * rel_size * font.getSize).toInt) } def imitate_font_css(font: Font, family: String = Isabelle_Fonts.sans, scale: Double = 1.0): String = { val font1 = new Font(family, font.getStyle, font.getSize) val rel_size = line_metrics(font).getHeight.toDouble / line_metrics(font1).getHeight "font-family: " + family + "; font-size: " + (scale * rel_size * 100).toInt + "%;" } def use_isabelle_fonts() { val default_font = label_font() val ui = UIManager.getDefaults for (prop <- List( "CheckBoxMenuItem.font", "Label.font", "Menu.font", "MenuItem.font", "PopupMenu.font", "TextArea.font", "TextField.font", "TextPane.font", "Tooltip.font", "Tree.font")) { val font = ui.get(prop) match { case font: Font => font case _ => default_font } ui.put(prop, GUI.imitate_font(font)) } } } diff --git a/src/Tools/jEdit/src/syntax_style.scala b/src/Tools/jEdit/src/syntax_style.scala --- a/src/Tools/jEdit/src/syntax_style.scala +++ b/src/Tools/jEdit/src/syntax_style.scala @@ -1,188 +1,188 @@ /* Title: Tools/jEdit/src/syntax_style.scala Author: Makarius Support for extended syntax styles: subscript, superscript, bold, user fonts. */ package isabelle.jedit import isabelle._ import java.awt.{Font, Color} import java.awt.font.TextAttribute import java.awt.geom.AffineTransform import org.gjt.sp.util.SyntaxUtilities import org.gjt.sp.jedit.syntax.{Token => JEditToken, SyntaxStyle} import org.gjt.sp.jedit.textarea.TextArea object Syntax_Style { /* extended syntax styles */ private val plain_range: Int = JEditToken.ID_COUNT private def check_range(i: Int) { require(0 <= i && i < plain_range) } def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte } def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte } def bold(i: Byte): Byte = { check_range(i); (i + 3 * plain_range).toByte } def user_font(idx: Int, i: Byte): Byte = { check_range(i); (i + (4 + idx) * plain_range).toByte } val hidden: Byte = (6 * plain_range).toByte val control: Byte = (hidden + JEditToken.DIGIT).toByte private def font_style(style: SyntaxStyle, f: Font => Font): SyntaxStyle = new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, f(style.getFont)) private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle = { font_style(style, font0 => { - import scala.collection.JavaConversions._ - val font1 = font0.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i))) + val font1 = + font0.deriveFont(java.util.Map.of(TextAttribute.SUPERSCRIPT, new java.lang.Integer(i))) def shift(y: Float): Font = GUI.transform_font(font1, AffineTransform.getTranslateInstance(0.0, y.toDouble)) val m0 = GUI.line_metrics(font0) val m1 = GUI.line_metrics(font1) val a = m1.getAscent - m0.getAscent val b = (m1.getDescent + m1.getLeading) - (m0.getDescent + m0.getLeading) if (a > 0.0f) shift(a) else if (b > 0.0f) shift(- b) else font1 }) } private def bold_style(style: SyntaxStyle): SyntaxStyle = font_style(style, font => font.deriveFont(if (font.isBold) Font.PLAIN else Font.BOLD)) val hidden_color: Color = new Color(255, 255, 255, 0) object Extender extends SyntaxUtilities.StyleExtender { val max_user_fonts = 2 if (Symbol.font_names.length > max_user_fonts) error("Too many user symbol fonts (" + max_user_fonts + " permitted): " + Symbol.font_names.mkString(", ")) override def extendStyles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] = { val style0 = styles(0) val font0 = style0.getFont val new_styles = Array.fill[SyntaxStyle](java.lang.Byte.MAX_VALUE)(styles(0)) for (i <- 0 until plain_range) { val style = styles(i) new_styles(i) = style new_styles(subscript(i.toByte)) = script_style(style, -1) new_styles(superscript(i.toByte)) = script_style(style, 1) new_styles(bold(i.toByte)) = bold_style(style) for (idx <- 0 until max_user_fonts) new_styles(user_font(idx, i.toByte)) = style for ((family, idx) <- Symbol.font_index) new_styles(user_font(idx, i.toByte)) = font_style(style, GUI.imitate_font(_, family)) } new_styles(hidden) = new SyntaxStyle(hidden_color, null, GUI.transform_font(new Font(font0.getFamily, 0, 1), AffineTransform.getScaleInstance(2.0, font0.getSize.toDouble))) new_styles(control) = new SyntaxStyle(style0.getForegroundColor, style0.getBackgroundColor, { val font_style = (if (font0.isItalic) 0 else Font.ITALIC) | (if (font0.isBold) 0 else Font.BOLD) new Font(font0.getFamily, font_style, font0.getSize) }) new_styles } } private def control_style(sym: String): Option[Byte => Byte] = if (sym == Symbol.sub_decoded) Some(subscript(_)) else if (sym == Symbol.sup_decoded) Some(superscript(_)) else if (sym == Symbol.bold_decoded) Some(bold(_)) else None def extended(text: CharSequence): Map[Text.Offset, Byte => Byte] = { var result = Map[Text.Offset, Byte => Byte]() def mark(start: Text.Offset, stop: Text.Offset, style: Byte => Byte) { for (i <- start until stop) result += (i -> style) } var offset = 0 var control_sym = "" for (sym <- Symbol.iterator(text)) { val end_offset = offset + sym.length if (control_style(sym).isDefined) control_sym = sym else if (control_sym != "") { if (Symbol.is_controllable(sym) && !Symbol.fonts.isDefinedAt(sym)) { mark(offset - control_sym.length, offset, _ => hidden) mark(offset, end_offset, control_style(control_sym).get) } control_sym = "" } if (Symbol.is_control_encoded(sym)) { val a = offset + Symbol.control_prefix.length val b = end_offset - Symbol.control_suffix.length mark(offset, a, _ => hidden) mark(a, b, _ => control) mark(b, end_offset, _ => hidden) } Symbol.lookup_font(sym) match { case Some(idx) => mark(offset, end_offset, user_font(idx, _)) case _ => } offset = end_offset } result } /* editing support for control symbols */ def edit_control_style(text_area: TextArea, control_sym: String) { GUI_Thread.assert {} val buffer = text_area.getBuffer val control_decoded = Isabelle_Encoding.perhaps_decode(buffer, control_sym) def update_style(text: String): String = { val result = new StringBuilder for (sym <- Symbol.iterator(text) if !HTML.is_control(sym)) { if (Symbol.is_controllable(sym)) result ++= control_decoded result ++= sym } result.toString } text_area.getSelection.foreach(sel => { val before = JEdit_Lib.point_range(buffer, sel.getStart - 1) JEdit_Lib.get_text(buffer, before) match { case Some(s) if HTML.is_control(s) => text_area.extendSelection(before.start, before.stop) case _ => } }) text_area.getSelection.toList match { case Nil => text_area.setSelectedText(control_decoded) case sels => JEdit_Lib.buffer_edit(buffer) { sels.foreach(sel => text_area.setSelectedText(sel, update_style(text_area.getSelectedText(sel)))) } } } }