src/Pure/GUI/gui.scala
author wenzelm
Tue, 06 May 2014 23:35:24 +0200
changeset 58230 3e8cbb624cc5
parent 58216 5d78bce4f5a4
child 58386 042d6e58cb40
permissions -rw-r--r--
tuned GUI for Windows L&F;
     1 /*  Title:      Pure/GUI/gui.scala
     2     Module:     PIDE-GUI
     3     Author:     Makarius
     4 
     5 Basic GUI tools (for AWT/Swing).
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import java.lang.{ClassLoader, ClassNotFoundException, NoSuchMethodException}
    12 import java.awt.{Image, Component, Container, Toolkit, Window, Font, KeyboardFocusManager}
    13 import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics}
    14 import java.awt.geom.AffineTransform
    15 import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow,
    16   JButton, JTextField}
    17 
    18 import scala.collection.convert.WrapAsJava
    19 import scala.swing.{ComboBox, TextArea, ScrollPane}
    20 import scala.swing.event.SelectionChanged
    21 
    22 
    23 object GUI
    24 {
    25   /* Swing look-and-feel */
    26 
    27   def get_laf(): String =
    28   {
    29     if (Platform.is_windows || Platform.is_macos)
    30       UIManager.getSystemLookAndFeelClassName()
    31     else
    32       UIManager.getInstalledLookAndFeels().find(_.getName == "Nimbus").map(_.getClassName)
    33         .getOrElse(UIManager.getCrossPlatformLookAndFeelClassName())
    34   }
    35 
    36   def init_laf(): Unit = UIManager.setLookAndFeel(get_laf())
    37 
    38   def is_macos_laf(): Boolean =
    39     Platform.is_macos &&
    40     UIManager.getSystemLookAndFeelClassName() == UIManager.getLookAndFeel.getClass.getName
    41 
    42 
    43   /* plain focus traversal, notably for text fields */
    44 
    45   def plain_focus_traversal(component: Component)
    46   {
    47     val dummy_button = new JButton
    48     def apply(id: Int): Unit =
    49       component.setFocusTraversalKeys(id, dummy_button.getFocusTraversalKeys(id))
    50     apply(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS)
    51     apply(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS)
    52   }
    53 
    54 
    55   /* X11 window manager */
    56 
    57   def window_manager(): Option[String] =
    58   {
    59     if (Platform.is_windows || Platform.is_macos) None
    60     else
    61       try {
    62         val XWM = Class.forName("sun.awt.X11.XWM", true, ClassLoader.getSystemClassLoader)
    63         val getWM = XWM.getDeclaredMethod("getWM")
    64         getWM.setAccessible(true)
    65         getWM.invoke(null) match {
    66           case null => None
    67           case wm => Some(wm.toString)
    68         }
    69       }
    70       catch {
    71         case _: ClassNotFoundException => None
    72         case _: NoSuchMethodException => None
    73       }
    74   }
    75 
    76 
    77   /* simple dialogs */
    78 
    79   def scrollable_text(txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false)
    80     : ScrollPane =
    81   {
    82     val text = new TextArea(txt)
    83     if (width > 0) text.columns = width
    84     if (height > 0 && split_lines(txt).length > height) text.rows = height
    85     text.editable = editable
    86     new ScrollPane(text)
    87   }
    88 
    89   private def simple_dialog(kind: Int, default_title: String,
    90     parent: Component, title: String, message: Seq[Any])
    91   {
    92     Swing_Thread.now {
    93       val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
    94       JOptionPane.showMessageDialog(parent,
    95         java_message.toArray.asInstanceOf[Array[AnyRef]],
    96         if (title == null) default_title else title, kind)
    97     }
    98   }
    99 
   100   def dialog(parent: Component, title: String, message: Any*) =
   101     simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message)
   102 
   103   def warning_dialog(parent: Component, title: String, message: Any*) =
   104     simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message)
   105 
   106   def error_dialog(parent: Component, title: String, message: Any*) =
   107     simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message)
   108 
   109   def confirm_dialog(parent: Component, title: String, option_type: Int, message: Any*): Int =
   110     Swing_Thread.now {
   111       val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
   112       JOptionPane.showConfirmDialog(parent,
   113         java_message.toArray.asInstanceOf[Array[AnyRef]], title,
   114           option_type, JOptionPane.QUESTION_MESSAGE)
   115     }
   116 
   117 
   118   /* zoom box */
   119 
   120   class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String](
   121     List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%"))
   122   {
   123     val Factor = "([0-9]+)%?".r
   124     def parse(text: String): Int =
   125       text match {
   126         case Factor(s) =>
   127           val i = Integer.parseInt(s)
   128           if (10 <= i && i < 1000) i else 100
   129         case _ => 100
   130       }
   131 
   132     def print(i: Int): String = i.toString + "%"
   133 
   134     def set_item(i: Int) {
   135       peer.getEditor match {
   136         case null =>
   137         case editor => editor.setItem(print(i))
   138       }
   139     }
   140 
   141     makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x))
   142     peer.getEditor.getEditorComponent match {
   143       case text: JTextField => text.setColumns(4)
   144       case _ =>
   145     }
   146 
   147     reactions += {
   148       case SelectionChanged(_) => apply_factor(parse(selection.item))
   149     }
   150     listenTo(selection)
   151     selection.index = 3
   152   }
   153 
   154 
   155   /* tooltip with multi-line support */
   156 
   157   def tooltip_lines(text: String): String =
   158     if (text == null || text == "") null
   159     else "<html>" + HTML.encode(text) + "</html>"
   160 
   161 
   162   /* screen resolution */
   163 
   164   def resolution_scale(): Double = Toolkit.getDefaultToolkit.getScreenResolution.toDouble / 72
   165   def resolution_scale(i: Int): Int = (i.toDouble * resolution_scale()).round.toInt
   166 
   167 
   168   /* icon */
   169 
   170   def isabelle_icon(): ImageIcon =
   171     new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle_transparent-32.gif"))
   172 
   173   def isabelle_icons(): List[ImageIcon] =
   174     for (icon <- List("isabelle/isabelle_transparent-32.gif", "isabelle/isabelle_transparent.gif"))
   175       yield new ImageIcon(getClass.getClassLoader.getResource(icon))
   176 
   177   def isabelle_image(): Image = isabelle_icon().getImage
   178 
   179   def isabelle_images(): java.util.List[Image] =
   180     WrapAsJava.seqAsJavaList(isabelle_icons.map(_.getImage))
   181 
   182 
   183   /* component hierachy */
   184 
   185   def get_parent(component: Component): Option[Container] =
   186     component.getParent match {
   187       case null => None
   188       case parent => Some(parent)
   189     }
   190 
   191   def ancestors(component: Component): Iterator[Container] = new Iterator[Container] {
   192     private var next_elem = get_parent(component)
   193     def hasNext(): Boolean = next_elem.isDefined
   194     def next(): Container =
   195       next_elem match {
   196         case Some(parent) =>
   197           next_elem = get_parent(parent)
   198           parent
   199         case None => Iterator.empty.next()
   200       }
   201   }
   202 
   203   def parent_window(component: Component): Option[Window] =
   204     ancestors(component).collectFirst({ case x: Window => x })
   205 
   206   def layered_pane(component: Component): Option[JLayeredPane] =
   207     parent_window(component) match {
   208       case Some(window: JWindow) => Some(window.getLayeredPane)
   209       case Some(frame: JFrame) => Some(frame.getLayeredPane)
   210       case _ => None
   211     }
   212 
   213 
   214   /* font operations */
   215 
   216   def font_metrics(font: Font): LineMetrics =
   217     font.getLineMetrics("", new FontRenderContext(null, false, false))
   218 
   219   def imitate_font(family: String, font: Font, scale: Double = 1.0): Font =
   220   {
   221     val font1 = new Font(family, font.getStyle, font.getSize)
   222     val size = scale * (font_metrics(font).getAscent / font_metrics(font1).getAscent * font.getSize)
   223     font1.deriveFont(size.round.toInt)
   224   }
   225 
   226   def transform_font(font: Font, transform: AffineTransform): Font =
   227   {
   228     import scala.collection.JavaConversions._
   229     font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform)))
   230   }
   231 }
   232