comparison dwtx/draw2d/BufferedGraphicsSource.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.draw2d.BufferedGraphicsSource;
14
15 import dwt.dwthelper.utils;
16
17
18
19 import dwt.DWT;
20 import dwt.DWTError;
21 import dwt.graphics.GC;
22 import dwt.graphics.Image;
23 static import dwt.graphics.Point;
24 import dwt.widgets.Canvas;
25 import dwt.widgets.Caret;
26 import dwt.widgets.Control;
27 import dwtx.draw2d.geometry.Rectangle;
28 import dwtx.draw2d.GraphicsSource;
29 import dwtx.draw2d.Graphics;
30 import dwtx.draw2d.SWTGraphics;
31
32 class BufferedGraphicsSource : GraphicsSource {
33
34 private Image imageBuffer;
35 private GC imageGC;
36 private GC controlGC;
37 private Control control;
38 private Rectangle inUse;
39
40 /**
41 * Constructs a new buffered graphics source using the given control.
42 * @since 2.1
43 * @param c the control
44 */
45 public this(Control c) {
46 control = c;
47 }
48
49 /**
50 * @see dwtx.draw2d.GraphicsSource#flushGraphics(dwtx.draw2d.geometry.Rectangle)
51 */
52 public void flushGraphics(Rectangle region) {
53 if (inUse.isEmpty())
54 return;
55
56 bool restoreCaret = false;
57 Canvas canvas = null;
58 if (auto canvas = cast(Canvas)control ) {
59 Caret caret = canvas.getCaret();
60 if (caret !is null)
61 restoreCaret = caret.isVisible();
62 if (restoreCaret)
63 caret.setVisible(false);
64 }
65 /*
66 * The imageBuffer may be null if double-buffering was not successful.
67 */
68 if (imageBuffer !is null) {
69 imageGC.dispose();
70 controlGC.drawImage(getImage(),
71 0, 0, inUse.width, inUse.height,
72 inUse.x, inUse.y, inUse.width, inUse.height);
73 imageBuffer.dispose();
74 imageBuffer = null;
75 imageGC = null;
76 }
77 controlGC.dispose();
78 controlGC = null;
79
80 if (restoreCaret)
81 canvas.getCaret().setVisible(true);
82 }
83
84 /**
85 * @see dwtx.draw2d.GraphicsSource#getGraphics(dwtx.draw2d.geometry.Rectangle)
86 */
87 public Graphics getGraphics(Rectangle region) {
88 if (control is null || control.isDisposed())
89 return null;
90
91 dwt.graphics.Point.Point ptSWT = control.getSize();
92 inUse = new Rectangle(0, 0, ptSWT.x, ptSWT.y);
93 inUse.intersect(region);
94 if (inUse.isEmpty())
95 return null;
96
97 /*
98 * Bugzilla 53632 - Attempts to create large images on some platforms will fail.
99 * When this happens, do not use double-buffering for painting.
100 */
101 try {
102 imageBuffer = new Image(null, inUse.width, inUse.height);
103 } catch (DWTError noMoreHandles) {
104 imageBuffer = null;
105 } catch (IllegalArgumentException tooBig) {
106 imageBuffer = null;
107 }
108
109 controlGC = new GC(control,
110 control.getStyle() & (DWT.RIGHT_TO_LEFT | DWT.LEFT_TO_RIGHT));
111 Graphics graphics;
112 if (imageBuffer !is null) {
113 imageGC = new GC(imageBuffer,
114 control.getStyle() & (DWT.RIGHT_TO_LEFT | DWT.LEFT_TO_RIGHT));
115 imageGC.setBackground(controlGC.getBackground());
116 imageGC.setForeground(controlGC.getForeground());
117 imageGC.setFont(controlGC.getFont());
118 imageGC.setLineStyle(controlGC.getLineStyle());
119 imageGC.setLineWidth(controlGC.getLineWidth());
120 imageGC.setXORMode(controlGC.getXORMode());
121 graphics = new SWTGraphics(imageGC);
122 graphics.translate(inUse.getLocation().negate());
123 } else {
124 graphics = new SWTGraphics(controlGC);
125 }
126
127 graphics.setClip(inUse);
128 return graphics;
129 }
130
131 /**
132 * Returns the current image buffer or <code>null</code>.
133 * @since 2.1
134 * @return the current image buffer
135 */
136 protected Image getImage() {
137 return imageBuffer;
138 }
139
140 /**
141 * Returns the current GC used on the buffer or <code>null</code>.
142 * @since 2.1
143 * @return the GC for the image buffer
144 */
145 protected GC getImageGC() {
146 return imageGC;
147 }
148
149 }