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