author | wenzelm |
Fri, 05 Oct 2012 14:32:56 +0200 | |
changeset 49712 | a1bd8fe5131b |
parent 49710 | 21d88a631fcc |
child 49725 | f8eeff667076 |
permissions | -rw-r--r-- |
49702 | 1 |
/* Title: Tools/jEdit/src/pretty_tooltip.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Enhanced tooltip window based on Pretty_Text_Area. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.jedit |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
49712 | 12 |
import java.awt.{Toolkit, Color, Point, BorderLayout, Window} |
49705 | 13 |
import java.awt.event.{ActionListener, ActionEvent, KeyEvent, WindowEvent, WindowAdapter} |
14 |
import javax.swing.{SwingUtilities, JWindow, JPanel, JComponent, KeyStroke} |
|
49702 | 15 |
import javax.swing.border.LineBorder |
16 |
||
17 |
import org.gjt.sp.jedit.View |
|
18 |
import org.gjt.sp.jedit.textarea.TextArea |
|
19 |
||
20 |
||
21 |
class Pretty_Tooltip( |
|
22 |
view: View, |
|
23 |
text_area: TextArea, |
|
24 |
rendering: Isabelle_Rendering, |
|
49710
21d88a631fcc
refer to parent frame -- relevant for floating dockables in particular;
wenzelm
parents:
49709
diff
changeset
|
25 |
mouse_x: Int, mouse_y: Int, body: XML.Body) |
49712 | 26 |
extends JWindow(JEdit_Lib.parent_window(text_area) getOrElse view) |
49702 | 27 |
{ |
49705 | 28 |
window => |
29 |
||
30 |
window.addWindowFocusListener(new WindowAdapter { |
|
49712 | 31 |
override def windowLostFocus(e: WindowEvent) { |
32 |
if (!Window.getWindows.exists(w => |
|
33 |
w.isDisplayable && JEdit_Lib.ancestors(w).exists(_ == window))) |
|
34 |
window.dispose() |
|
35 |
} |
|
49702 | 36 |
}) |
49705 | 37 |
|
38 |
window.setContentPane(new JPanel(new BorderLayout) { |
|
39 |
private val action_listener = new ActionListener { |
|
40 |
def actionPerformed(e: ActionEvent) { |
|
41 |
e.getActionCommand match { |
|
49712 | 42 |
case "close" => |
43 |
window.dispose() |
|
44 |
JEdit_Lib.ancestors(window) foreach { |
|
45 |
case c: Pretty_Tooltip => c.dispose |
|
46 |
case _ => |
|
47 |
} |
|
49705 | 48 |
case _ => |
49 |
} |
|
50 |
} |
|
51 |
} |
|
52 |
registerKeyboardAction(action_listener, "close", |
|
53 |
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED) |
|
54 |
||
49702 | 55 |
override def getFocusTraversalKeysEnabled(): Boolean = false |
56 |
}) |
|
49705 | 57 |
window.getRootPane.setBorder(new LineBorder(Color.BLACK)) |
49702 | 58 |
|
49703
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
59 |
val pretty_text_area = new Pretty_Text_Area(view) |
49706 | 60 |
pretty_text_area.getPainter.setBackground(rendering.tooltip_color) |
49703
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
61 |
pretty_text_area.resize( |
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
62 |
Isabelle.font_family(), Isabelle.font_size("jedit_tooltip_font_scale").round) |
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
63 |
pretty_text_area.update(rendering.snapshot, body) |
c89fffb11769
some re-ordering of initialization to ensure proper formatting;
wenzelm
parents:
49702
diff
changeset
|
64 |
|
49706 | 65 |
window.add(pretty_text_area) |
49708 | 66 |
|
67 |
{ |
|
49707 | 68 |
val font_metrics = pretty_text_area.getPainter.getFontMetrics |
69 |
val margin = Isabelle.options.int("jedit_tooltip_margin") // FIXME via rendering?! |
|
70 |
val lines = // FIXME avoid redundant formatting |
|
71 |
XML.traverse_text(Pretty.formatted(body, margin, Pretty.font_metric(font_metrics)))(0)( |
|
72 |
(n: Int, s: String) => n + s.iterator.filter(_ == '\n').length) |
|
73 |
||
74 |
val screen = Toolkit.getDefaultToolkit.getScreenSize |
|
75 |
val w = (font_metrics.charWidth(Pretty.spc) * margin) min (screen.width / 2) |
|
76 |
val h = (font_metrics.getHeight * (lines + 2)) min (screen.height / 2) |
|
77 |
window.setSize(w, h) |
|
78 |
} |
|
49706 | 79 |
|
49709 | 80 |
{ |
81 |
val container = text_area.getPainter |
|
82 |
val font_metrics = container.getFontMetrics |
|
83 |
val point = new Point(mouse_x, mouse_y + font_metrics.getHeight / 2) |
|
84 |
SwingUtilities.convertPointToScreen(point, container) |
|
85 |
||
86 |
val screen = Toolkit.getDefaultToolkit.getScreenSize |
|
87 |
val x = point.x min (screen.width - window.getWidth) |
|
88 |
val y = point.y min (screen.height - window.getHeight) |
|
89 |
window.setLocation(x, y) |
|
90 |
} |
|
91 |
||
49705 | 92 |
window.setVisible(true) |
49707 | 93 |
pretty_text_area.refresh() // FIXME avoid redundant formatting |
49702 | 94 |
} |
95 |