src/Pure/GUI/gui.scala
author wenzelm
Thu, 05 Dec 2013 19:59:43 +0100
changeset 54676 6b2ca4850b71
parent 54659 b636dab842f3
child 54709 87402674fe2f
permissions -rw-r--r--
uniform use of transparent icons, as for main "apps";
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53783
f5e9d182f645 clarified location of GUI modules (which depend on Swing of JFX);
wenzelm
parents: 53778
diff changeset
     1
/*  Title:      Pure/GUI/gui.scala
53853
e8430d668f44 more quasi-generic PIDE modules (NB: Swing/JFX needs to be kept separate from non-GUI material);
wenzelm
parents: 53850
diff changeset
     2
    Module:     PIDE-GUI
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     4
53712
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
     5
Basic GUI tools (for AWT/Swing).
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     6
*/
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     7
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     8
package isabelle
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
     9
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    10
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
    11
import java.awt.{Image, Component, Container, Toolkit, Window, Font}
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
    12
import java.awt.font.{TextAttribute, TransformAttribute, FontRenderContext, LineMetrics}
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
    13
import java.awt.geom.AffineTransform
53778
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
    14
import javax.swing.{ImageIcon, JOptionPane, UIManager, JLayeredPane, JFrame, JWindow}
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    15
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    16
import scala.swing.{ComboBox, TextArea, ScrollPane}
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    17
import scala.swing.event.SelectionChanged
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    18
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    19
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    20
object GUI
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    21
{
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    22
  /* Swing look-and-feel */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    23
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    24
  def get_laf(): String =
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
53850
b1bc857f2422 simplified default L&F -- Nimbus should be always available and GTK+ is not fully working yet;
wenzelm
parents: 53849
diff changeset
    29
      UIManager.getInstalledLookAndFeels().find(_.getName == "Nimbus").map(_.getClassName)
b1bc857f2422 simplified default L&F -- Nimbus should be always available and GTK+ is not fully working yet;
wenzelm
parents: 53849
diff changeset
    30
        .getOrElse(UIManager.getCrossPlatformLookAndFeelClassName())
51619
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
53848
8d7029eb0c31 disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents: 53786
diff changeset
    35
  def is_macos_laf(): Boolean =
53849
a3177973ca5e proper platform-specific test;
wenzelm
parents: 53848
diff changeset
    36
    Platform.is_macos &&
53848
8d7029eb0c31 disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents: 53786
diff changeset
    37
    UIManager.getSystemLookAndFeelClassName() == UIManager.getLookAndFeel.getClass.getName
8d7029eb0c31 disable standard behaviour of Mac OS X text field (i.e. select-all after focus gain) in order to make completion work more smoothly;
wenzelm
parents: 53786
diff changeset
    38
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    39
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    40
  /* simple dialogs */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    41
53714
89fb20ae9b73 limit for text height;
wenzelm
parents: 53712
diff changeset
    42
  def scrollable_text(txt: String, width: Int = 60, height: Int = 20, editable: Boolean = false)
89fb20ae9b73 limit for text height;
wenzelm
parents: 53712
diff changeset
    43
    : ScrollPane =
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    44
  {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    45
    val text = new TextArea(txt)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    46
    if (width > 0) text.columns = width
53714
89fb20ae9b73 limit for text height;
wenzelm
parents: 53712
diff changeset
    47
    if (height > 0 && split_lines(txt).length > height) text.rows = height
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    48
    text.editable = editable
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    49
    new ScrollPane(text)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    50
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    51
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    52
  private def simple_dialog(kind: Int, default_title: String,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    53
    parent: Component, title: String, message: Seq[Any])
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    54
  {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    55
    Swing_Thread.now {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    56
      val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    57
      JOptionPane.showMessageDialog(parent,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    58
        java_message.toArray.asInstanceOf[Array[AnyRef]],
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    59
        if (title == null) default_title else title, kind)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    60
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    61
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    62
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    63
  def dialog(parent: Component, title: String, message: Any*) =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    64
    simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    65
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    66
  def warning_dialog(parent: Component, title: String, message: Any*) =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    67
    simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    68
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    69
  def error_dialog(parent: Component, title: String, message: Any*) =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    70
    simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    71
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    72
  def confirm_dialog(parent: Component, title: String, option_type: Int, message: Any*): Int =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    73
    Swing_Thread.now {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    74
      val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    75
      JOptionPane.showConfirmDialog(parent,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    76
        java_message.toArray.asInstanceOf[Array[AnyRef]], title,
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    77
          option_type, JOptionPane.QUESTION_MESSAGE)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    78
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    79
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    80
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    81
  /* zoom box */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    82
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    83
  class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String](
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    84
    List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%"))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    85
  {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    86
    val Factor = "([0-9]+)%?".r
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    87
    def parse(text: String): Int =
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    88
      text match {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    89
        case Factor(s) =>
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    90
          val i = Integer.parseInt(s)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    91
          if (10 <= i && i <= 1000) i else 100
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    92
        case _ => 100
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    93
      }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    94
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    95
    def print(i: Int): String = i.toString + "%"
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    96
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    97
    def set_item(i: Int) {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    98
      peer.getEditor match {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
    99
        case null =>
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   100
        case editor => editor.setItem(print(i))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   101
      }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   102
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   103
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   104
    makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   105
    reactions += {
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   106
      case SelectionChanged(_) => apply_factor(parse(selection.item))
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   107
    }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   108
    listenTo(selection)
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   109
    selection.index = 3
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   110
    prototypeDisplayValue = Some("00000%")
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   111
  }
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   112
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   113
53786
wenzelm
parents: 53785
diff changeset
   114
  /* tooltip with multi-line support */
wenzelm
parents: 53785
diff changeset
   115
wenzelm
parents: 53785
diff changeset
   116
  def tooltip_lines(lines: List[String]): String =
wenzelm
parents: 53785
diff changeset
   117
    if (lines.isEmpty) null
wenzelm
parents: 53785
diff changeset
   118
    else "<html><pre>" + HTML.encode(cat_lines(lines)) + "</pre></html>"
wenzelm
parents: 53785
diff changeset
   119
wenzelm
parents: 53785
diff changeset
   120
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   121
  /* screen resolution */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   122
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   123
  def resolution_scale(): Double = Toolkit.getDefaultToolkit.getScreenResolution.toDouble / 72
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   124
  def resolution_scale(i: Int): Int = (i.toDouble * resolution_scale()).round.toInt
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   125
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   126
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   127
  /* icon */
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   128
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   129
  def isabelle_icon(): ImageIcon =
54676
6b2ca4850b71 uniform use of transparent icons, as for main "apps";
wenzelm
parents: 54659
diff changeset
   130
    new ImageIcon(getClass.getClassLoader.getResource("isabelle/isabelle_transparent-32.gif"))
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   131
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   132
  def isabelle_image(): Image = isabelle_icon().getImage
53712
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   133
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   134
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   135
  /* component hierachy */
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   136
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   137
  def get_parent(component: Component): Option[Container] =
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   138
    component.getParent match {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   139
      case null => None
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   140
      case parent => Some(parent)
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   141
    }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   142
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   143
  def ancestors(component: Component): Iterator[Container] = new Iterator[Container] {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   144
    private var next_elem = get_parent(component)
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   145
    def hasNext(): Boolean = next_elem.isDefined
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   146
    def next(): Container =
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   147
      next_elem match {
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   148
        case Some(parent) =>
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   149
          next_elem = get_parent(parent)
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   150
          parent
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   151
        case None => Iterator.empty.next()
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   152
      }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   153
  }
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   154
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   155
  def parent_window(component: Component): Option[Window] =
ea51046be71b tuned signature;
wenzelm
parents: 53452
diff changeset
   156
    ancestors(component).collectFirst({ case x: Window => x })
53778
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   157
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   158
  def layered_pane(component: Component): Option[JLayeredPane] =
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   159
    parent_window(component) match {
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   160
      case Some(window: JWindow) => Some(window.getLayeredPane)
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   161
      case Some(frame: JFrame) => Some(frame.getLayeredPane)
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   162
      case _ => None
29eaacff4078 proper layered pane at root of parent component, not global view (e.g. relevant for tooltips for detached info windows);
wenzelm
parents: 53714
diff changeset
   163
    }
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   164
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   165
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   166
  /* font operations */
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   167
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   168
  def font_metrics(font: Font): LineMetrics =
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   169
    font.getLineMetrics("", new FontRenderContext(null, false, false))
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   170
54368
36dc6aa4fe87 less tiny font for search criteria;
wenzelm
parents: 53853
diff changeset
   171
  def imitate_font(family: String, font: Font, scale: Double = 1.0): Font =
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   172
  {
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   173
    val font1 = new Font(family, font.getStyle, font.getSize)
54368
36dc6aa4fe87 less tiny font for search criteria;
wenzelm
parents: 53853
diff changeset
   174
    val size = scale * (font_metrics(font).getAscent / font_metrics(font1).getAscent * font.getSize)
36dc6aa4fe87 less tiny font for search criteria;
wenzelm
parents: 53853
diff changeset
   175
    font1.deriveFont(size.round.toInt)
53785
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   176
  }
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   177
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   178
  def transform_font(font: Font, transform: AffineTransform): Font =
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   179
  {
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   180
    import scala.collection.JavaConversions._
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   181
    font.deriveFont(Map(TextAttribute.TRANSFORM -> new TransformAttribute(transform)))
e64edcc2f8bf tuned signature;
wenzelm
parents: 53783
diff changeset
   182
  }
51619
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   183
}
95b7da3430d4 added missing file;
wenzelm
parents:
diff changeset
   184