Mercurial > projects > dwt-addons
comparison dwtx/draw2d/ScalableLayeredPane.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.ScalableLayeredPane; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import dwtx.draw2d.geometry.Dimension; | |
18 import dwtx.draw2d.geometry.Rectangle; | |
19 import dwtx.draw2d.geometry.Translatable; | |
20 import dwtx.draw2d.LayeredPane; | |
21 import dwtx.draw2d.ScalableFigure; | |
22 import dwtx.draw2d.Graphics; | |
23 import dwtx.draw2d.ScaledGraphics; | |
24 | |
25 /** | |
26 * A non-freeform, scalable layered pane. | |
27 * @author Eric Bordeau | |
28 * @since 2.1.1 | |
29 */ | |
30 public class ScalableLayeredPane | |
31 : LayeredPane | |
32 , ScalableFigure | |
33 { | |
34 | |
35 private double scale = 1.0; | |
36 | |
37 /** | |
38 * @see IFigure#getClientArea(Rectangle) | |
39 */ | |
40 public Rectangle getClientArea(Rectangle rect) { | |
41 super.getClientArea(rect); | |
42 rect.width /= scale; | |
43 rect.height /= scale; | |
44 rect.x /= scale; | |
45 rect.y /= scale; | |
46 return rect; | |
47 } | |
48 | |
49 /** | |
50 * @see Figure#getPreferredSize(int, int) | |
51 */ | |
52 public Dimension getMinimumSize(int wHint, int hHint) { | |
53 Dimension d = super.getMinimumSize(cast(int) (wHint / getScale()), cast(int)(hHint / getScale())); | |
54 int w = getInsets().getWidth(); | |
55 int h = getInsets().getHeight(); | |
56 return d.getExpanded(-w, -h) | |
57 .scale(scale) | |
58 .expand(w, h); | |
59 } | |
60 | |
61 /** | |
62 * @see Figure#getPreferredSize(int, int) | |
63 */ | |
64 public Dimension getPreferredSize(int wHint, int hHint) { | |
65 Dimension d = super.getPreferredSize(cast(int) (wHint / getScale()), cast(int)(hHint / getScale())); | |
66 int w = getInsets().getWidth(); | |
67 int h = getInsets().getHeight(); | |
68 return d.getExpanded(-w, -h) | |
69 .scale(scale) | |
70 .expand(w, h); | |
71 } | |
72 | |
73 /** | |
74 * Returns the scale level, default is 1.0. | |
75 * @return the scale level | |
76 */ | |
77 public double getScale() { | |
78 return scale; | |
79 } | |
80 | |
81 /** | |
82 * @see dwtx.draw2d.IFigure#isCoordinateSystem() | |
83 */ | |
84 public bool isCoordinateSystem() { | |
85 return true; | |
86 } | |
87 | |
88 /** | |
89 * @see dwtx.draw2d.Figure#paintClientArea(Graphics) | |
90 */ | |
91 protected void paintClientArea(Graphics graphics) { | |
92 if (getChildren().isEmpty()) | |
93 return; | |
94 if (scale is 1.0) { | |
95 super.paintClientArea(graphics); | |
96 } else { | |
97 ScaledGraphics g = new ScaledGraphics(graphics); | |
98 bool optimizeClip = getBorder() is null || getBorder().isOpaque(); | |
99 if (!optimizeClip) | |
100 g.clipRect(getBounds().getCropped(getInsets())); | |
101 g.scale(scale); | |
102 g.pushState(); | |
103 paintChildren(g); | |
104 g.dispose(); | |
105 graphics.restoreState(); | |
106 } | |
107 } | |
108 | |
109 /** | |
110 * Sets the zoom level | |
111 * @param newZoom The new zoom level | |
112 */ | |
113 public void setScale(double newZoom) { | |
114 if (scale is newZoom) | |
115 return; | |
116 scale = newZoom; | |
117 fireMoved(); //for AncestorListener compatibility | |
118 revalidate(); | |
119 repaint(); | |
120 } | |
121 | |
122 /** | |
123 * @see dwtx.draw2d.Figure#translateFromParent(Translatable) | |
124 */ | |
125 public void translateFromParent(Translatable t) { | |
126 t.performScale(1 / scale); | |
127 } | |
128 | |
129 /** | |
130 * @see dwtx.draw2d.Figure#translateToParent(Translatable) | |
131 */ | |
132 public void translateToParent(Translatable t) { | |
133 t.performScale(scale); | |
134 } | |
135 | |
136 } |