1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jeuclid.swt;
20
21 import net.sourceforge.jeuclid.MutableLayoutContext;
22 import net.sourceforge.jeuclid.context.LayoutContextImpl;
23 import net.sourceforge.jeuclid.elements.generic.DocumentElement;
24
25 import org.eclipse.swt.events.DisposeEvent;
26 import org.eclipse.swt.events.DisposeListener;
27 import org.eclipse.swt.events.PaintEvent;
28 import org.eclipse.swt.events.PaintListener;
29 import org.eclipse.swt.graphics.Color;
30 import org.eclipse.swt.graphics.Device;
31 import org.eclipse.swt.graphics.GC;
32 import org.eclipse.swt.graphics.Image;
33 import org.eclipse.swt.graphics.ImageData;
34 import org.eclipse.swt.widgets.Canvas;
35 import org.eclipse.swt.widgets.Composite;
36 import org.w3c.dom.Node;
37
38
39
40
41
42
43
44
45 public final class MathView extends Canvas {
46
47
48
49
50
51
52 private final MathRenderer mathRenderer = MathRenderer.getInstance();
53
54 private Node document;
55
56 private ImageData renderedFormula;
57
58 private MutableLayoutContext layoutContext = new LayoutContextImpl(
59 LayoutContextImpl.getDefaultLayoutContext());
60
61
62
63
64
65
66
67
68
69 public MathView(final Composite parent, final int style) {
70 super(parent, style);
71 this.setDocument(new DocumentElement());
72 this.addDisposeListener(new DisposeListener() {
73 public void widgetDisposed(final DisposeEvent e) {
74 MathView.this.widgetDisposed(e);
75 }
76 });
77 this.addPaintListener(new PaintListener() {
78 public void paintControl(final PaintEvent e) {
79 MathView.this.paintControl(e);
80 }
81 });
82 }
83
84 private void paintControl(final PaintEvent e) {
85 final GC gc = e.gc;
86 final Device device = gc.getDevice();
87 final Color c = new Color(device, 255, 255, 255);
88 gc.setBackground(c);
89 gc.fillRectangle(e.x, e.y, e.width, e.height);
90 c.dispose();
91 if (this.renderedFormula != null) {
92 final Image im = new Image(device, this.renderedFormula);
93 gc.drawImage(im, 0, 0);
94 im.dispose();
95 }
96 }
97
98 private void widgetDisposed(final DisposeEvent e) {
99 this.document = null;
100 this.renderedFormula = null;
101 }
102
103 private void recreate() {
104 this.renderedFormula = this.mathRenderer.render(this.document,
105 this.layoutContext);
106 }
107
108
109
110
111
112 public void setDocument(final Node doc) {
113 final Node oldValue = this.document;
114 this.document = doc;
115 if (doc != oldValue) {
116 this.recreate();
117 this.redraw();
118 }
119 }
120
121
122
123
124 public Node getDocument() {
125 return this.document;
126 }
127
128 }