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