src/Pure/System/gui.scala
author wenzelm
Sat, 07 Sep 2013 11:36:03 +0200
changeset 53452 8181bc357dc4
parent 51619 95b7da3430d4
child 53712 ea51046be71b
permissions -rw-r--r--
more portable access to icon -- avoid Isabelle_System which is not yet initialized in bootstrap;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/gui.scala
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     3
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     4
Elementary GUI tools.
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     5
*/
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     6
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     7
package isabelle
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     8
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     9
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    10
import java.awt.{Image, Component, Toolkit}
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    11
import javax.swing.{ImageIcon, JOptionPane, UIManager}
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    12
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    13
import scala.swing.{ComboBox, TextArea, ScrollPane}
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    14
import scala.swing.event.SelectionChanged
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    15
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    16
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    17
object GUI
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    18
{
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    19
  /* Swing look-and-feel */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    20
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    21
  def get_laf(): String =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    22
  {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    23
    def laf(name: String): Option[String] =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    24
      UIManager.getInstalledLookAndFeels().find(_.getName == name).map(_.getClassName)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    25
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    26
    if (Platform.is_windows || Platform.is_macos)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    27
      UIManager.getSystemLookAndFeelClassName()
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    28
    else
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    29
      laf("Nimbus") orElse laf("GTK+") getOrElse
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    30
        UIManager.getCrossPlatformLookAndFeelClassName()
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    31
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    32
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    33
  def init_laf(): Unit = UIManager.setLookAndFeel(get_laf())
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    34
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    35
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    36
  /* simple dialogs */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    37
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    38
  def scrollable_text(txt: String, width: Int = 60, editable: Boolean = false): ScrollPane =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    39
  {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    40
    val text = new TextArea(txt)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    41
    if (width > 0) text.columns = width
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    42
    text.editable = editable
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    43
    new ScrollPane(text)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    44
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    45
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    46
  private def simple_dialog(kind: Int, default_title: String,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    47
    parent: Component, title: String, message: Seq[Any])
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    48
  {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    49
    Swing_Thread.now {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    50
      val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    51
      JOptionPane.showMessageDialog(parent,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    52
        java_message.toArray.asInstanceOf[Array[AnyRef]],
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    53
        if (title == null) default_title else title, kind)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    54
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    55
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    56
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    57
  def dialog(parent: Component, title: String, message: Any*) =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    58
    simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    59
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    60
  def warning_dialog(parent: Component, title: String, message: Any*) =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    61
    simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    62
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    63
  def error_dialog(parent: Component, title: String, message: Any*) =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    64
    simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    65
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    66
  def confirm_dialog(parent: Component, title: String, option_type: Int, message: Any*): Int =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    67
    Swing_Thread.now {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    68
      val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    69
      JOptionPane.showConfirmDialog(parent,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    70
        java_message.toArray.asInstanceOf[Array[AnyRef]], title,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    71
          option_type, JOptionPane.QUESTION_MESSAGE)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    72
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    73
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    74
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    75
  /* zoom box */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    76
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    77
  class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String](
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    78
    List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%"))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    79
  {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    80
    val Factor = "([0-9]+)%?".r
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    81
    def parse(text: String): Int =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    82
      text match {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    83
        case Factor(s) =>
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    84
          val i = Integer.parseInt(s)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    85
          if (10 <= i && i <= 1000) i else 100
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    86
        case _ => 100
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    87
      }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    88
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    89
    def print(i: Int): String = i.toString + "%"
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    90
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    91
    def set_item(i: Int) {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    92
      peer.getEditor match {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    93
        case null =>
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    94
        case editor => editor.setItem(print(i))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    95
      }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    96
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    97
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    98
    makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    99
    reactions += {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   100
      case SelectionChanged(_) => apply_factor(parse(selection.item))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   101
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   102
    listenTo(selection)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   103
    selection.index = 3
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   104
    prototypeDisplayValue = Some("00000%")
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   105
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   106
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   107
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   108
  /* screen resolution */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   109
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   110
  def resolution_scale(): Double = Toolkit.getDefaultToolkit.getScreenResolution.toDouble / 72
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   111
  def resolution_scale(i: Int): Int = (i.toDouble * resolution_scale()).round.toInt
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   112
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   113
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   114
  /* icon */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   115
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   116
  def isabelle_icon(): ImageIcon =
53452
8181bc357dc4 more portable access to icon -- avoid Isabelle_System which is not yet initialized in bootstrap;
wenzelm
parents: 51619
diff changeset
   117
    new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle.gif"))
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   118
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   119
  def isabelle_image(): Image = isabelle_icon().getImage
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   120
}
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   121