src/Pure/GUI/gui.scala
author wenzelm
Sun Sep 22 18:42:18 2013 +0200 (2013-09-22)
changeset 53786 064cb0458071
parent 53785 e64edcc2f8bf
child 53848 8d7029eb0c31
permissions -rw-r--r--
tuned;
     1 /*  Title:      Pure/GUI/gui.scala
     2     Author:     Makarius
     3 
     4 Basic GUI tools (for AWT/Swing).
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import java.awt.{Image, Component, Container, Toolkit, Window, Font}
    11 import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics}
    12 import java.awt.geom.AffineTransform
    13 import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow}
    14 
    15 import scala.swing.{ComboBox, TextArea, ScrollPane}
    16 import scala.swing.event.SelectionChanged
    17 
    18 
    19 object GUI
    20 {
    21   /* Swing look-and-feel */
    22 
    23   def get_laf(): String =
    24   {
    25     def laf(name: String): Option[String] =
    26       UIManager.getInstalledLookAndFeels().find(_.getName == name).map(_.getClassName)
    27 
    28     if (Platform.is_windows || Platform.is_macos)
    29       UIManager.getSystemLookAndFeelClassName()
    30     else
    31       laf("Nimbus") orElse laf("GTK+") getOrElse
    32         UIManager.getCrossPlatformLookAndFeelClassName()
    33   }
    34 
    35   def init_laf(): Unit = UIManager.setLookAndFeel(get_laf())
    36 
    37 
    38   /* simple dialogs */
    39 
    40   def scrollable_text(txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false)
    41     : ScrollPane =
    42   {
    43     val text = new TextArea(txt)
    44     if (width > 0) text.columns = width
    45     if (height > 0 && split_lines(txt).length > height) text.rows = height
    46     text.editable = editable
    47     new ScrollPane(text)
    48   }
    49 
    50   private def simple_dialog(kind: Int, default_title: String,
    51     parent: Component, title: String, message: Seq[Any])
    52   {
    53     Swing_Thread.now {
    54       val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
    55       JOptionPane.showMessageDialog(parent,
    56         java_message.toArray.asInstanceOf[Array[AnyRef]],
    57         if (title == null) default_title else title, kind)
    58     }
    59   }
    60 
    61   def dialog(parent: Component, title: String, message: Any*) =
    62     simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message)
    63 
    64   def warning_dialog(parent: Component, title: String, message: Any*) =
    65     simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message)
    66 
    67   def error_dialog(parent: Component, title: String, message: Any*) =
    68     simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message)
    69 
    70   def confirm_dialog(parent: Component, title: String, option_type: Int, message: Any*): Int =
    71     Swing_Thread.now {
    72       val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
    73       JOptionPane.showConfirmDialog(parent,
    74         java_message.toArray.asInstanceOf[Array[AnyRef]], title,
    75           option_type, JOptionPane.QUESTION_MESSAGE)
    76     }
    77 
    78 
    79   /* zoom box */
    80 
    81   class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String](
    82     List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%"))
    83   {
    84     val Factor = "([0-9]+)%?".r
    85     def parse(text: String): Int =
    86       text match {
    87         case Factor(s) =>
    88           val i = Integer.parseInt(s)
    89           if (10 <= i && i <= 1000) i else 100
    90         case _ => 100
    91       }
    92 
    93     def print(i: Int): String = i.toString + "%"
    94 
    95     def set_item(i: Int) {
    96       peer.getEditor match {
    97         case null =>
    98         case editor => editor.setItem(print(i))
    99       }
   100     }
   101 
   102     makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x))
   103     reactions += {
   104       case SelectionChanged(_) => apply_factor(parse(selection.item))
   105     }
   106     listenTo(selection)
   107     selection.index = 3
   108     prototypeDisplayValue = Some("00000%")
   109   }
   110 
   111 
   112   /* tooltip with multi-line support */
   113 
   114   def tooltip_lines(lines: List[String]): String =
   115     if (lines.isEmpty) null
   116     else "<html><pre>" + HTML.encode(cat_lines(lines)) + "</pre></html>"
   117 
   118 
   119   /* screen resolution */
   120 
   121   def resolution_scale(): Double = Toolkit.getDefaultToolkit.getScreenResolution.toDouble / 72
   122   def resolution_scale(i: Int): Int = (i.toDouble * resolution_scale()).round.toInt
   123 
   124 
   125   /* icon */
   126 
   127   def isabelle_icon(): ImageIcon =
   128     new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle.gif"))
   129 
   130   def isabelle_image(): Image = isabelle_icon().getImage
   131 
   132 
   133   /* component hierachy */
   134 
   135   def get_parent(component: Component): Option[Container] =
   136     component.getParent match {
   137       case null => None
   138       case parent => Some(parent)
   139     }
   140 
   141   def ancestors(component: Component): Iterator[Container] = new Iterator[Container] {
   142     private var next_elem = get_parent(component)
   143     def hasNext(): Boolean = next_elem.isDefined
   144     def next(): Container =
   145       next_elem match {
   146         case Some(parent) =>
   147           next_elem = get_parent(parent)
   148           parent
   149         case None => Iterator.empty.next()
   150       }
   151   }
   152 
   153   def parent_window(component: Component): Option[Window] =
   154     ancestors(component).collectFirst({ case x: Window => x })
   155 
   156   def layered_pane(component: Component): Option[JLayeredPane] =
   157     parent_window(component) match {
   158       case Some(window: JWindow) => Some(window.getLayeredPane)
   159       case Some(frame: JFrame) => Some(frame.getLayeredPane)
   160       case _ => None
   161     }
   162 
   163 
   164   /* font operations */
   165 
   166   def font_metrics(font: Font): LineMetrics =
   167     font.getLineMetrics("", new FontRenderContext(null, false, false))
   168 
   169   def imitate_font(family: String, font: Font): Font =
   170   {
   171     val font1 = new Font(family, font.getStyle, font.getSize)
   172     font1.deriveFont(font_metrics(font).getAscent / font_metrics(font1).getAscent * font.getSize)
   173   }
   174 
   175   def transform_font(font: Font, transform: AffineTransform): Font =
   176   {
   177     import scala.collection.JavaConversions._
   178     font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform)))
   179   }
   180 }
   181