comparison dwtx/draw2d/FreeformHelper.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.FreeformHelper;
14
15 import dwt.dwthelper.utils;
16 import dwtx.dwtxhelper.Collection;
17
18 import dwtx.draw2d.geometry.Insets;
19 import dwtx.draw2d.geometry.Rectangle;
20 import dwtx.draw2d.FreeformListener;
21 import dwtx.draw2d.FreeformFigure;
22 import dwtx.draw2d.IFigure;
23 import dwtx.draw2d.FigureListener;
24
25 class FreeformHelper
26 : FreeformListener
27 {
28
29 class ChildTracker : FigureListener {
30 public void figureMoved(IFigure source) {
31 invalidate();
32 }
33 }
34
35 private FreeformFigure host;
36 private Rectangle freeformExtent;
37 private FigureListener figureListener;
38
39 private void instanceInit(){
40 figureListener = new ChildTracker();
41 }
42 this(FreeformFigure host) {
43 instanceInit();
44 this.host = host;
45 }
46
47 public Rectangle getFreeformExtent() {
48 if (freeformExtent !is null)
49 return freeformExtent;
50 Rectangle r;
51 List children = host.getChildren();
52 for (int i = 0; i < children.size(); i++) {
53 IFigure child = cast(IFigure)children.get(i);
54 if (null !is cast(FreeformFigure) child )
55 r = (cast(FreeformFigure) child).getFreeformExtent();
56 else
57 r = child.getBounds();
58 if (freeformExtent is null)
59 freeformExtent = r.getCopy();
60 else
61 freeformExtent.union_(r);
62 }
63 Insets insets = host.getInsets();
64 if (freeformExtent is null)
65 freeformExtent = new Rectangle(0, 0, insets.getWidth(), insets.getHeight());
66 else {
67 host.translateToParent(freeformExtent);
68 freeformExtent.expand(insets);
69 }
70 // System.out.println("New extent calculated for " + host + " = " + freeformExtent);
71 return freeformExtent;
72 }
73
74 public void hookChild(IFigure child) {
75 invalidate();
76 if (null !is cast(FreeformFigure)child )
77 (cast(FreeformFigure)child).addFreeformListener(this);
78 else
79 child.addFigureListener(figureListener);
80 }
81
82 void invalidate() {
83 freeformExtent = null;
84 host.fireExtentChanged();
85 if (host.getParent() !is null)
86 host.getParent().revalidate();
87 else
88 host.revalidate();
89 }
90
91 public void notifyFreeformExtentChanged() {
92 //A childs freeform extent has changed, therefore this extent must be recalculated
93 invalidate();
94 }
95
96 public void setFreeformBounds(Rectangle bounds) {
97 host.setBounds(bounds);
98 bounds = bounds.getCopy();
99 host.translateFromParent(bounds);
100 List children = host.getChildren();
101 for (int i = 0; i < children.size(); i++) {
102 IFigure child = cast(IFigure)children.get(i);
103 if (null !is cast(FreeformFigure)child )
104 (cast(FreeformFigure) child).setFreeformBounds(bounds);
105 }
106 }
107
108 public void unhookChild(IFigure child) {
109 invalidate();
110 if (null !is cast(FreeformFigure)child )
111 (cast(FreeformFigure)child).removeFreeformListener(this);
112 else
113 child.removeFigureListener(figureListener);
114 }
115
116 }