1/*
2 * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package sun.awt.X11;
26
27import java.awt.*;
28
29class WindowDimensions {
30    private Point loc;
31    private Dimension size;
32    private Insets insets;
33    private boolean isClientSizeSet;
34
35    /**
36     * If isClient is true, the bounds represent the client window area.
37     * Otherwise, they represent the entire window area, with the insets included
38     */
39    public WindowDimensions(int x, int y, int width, int height, boolean isClient) {
40        this(new Rectangle(x, y, width, height), null, isClient);
41    }
42
43    /**
44     * If isClient is true, the bounds represent the client window area.
45     * Otherwise, they represent the entire window area, with the insets included
46     */
47    public WindowDimensions(Rectangle rec, Insets ins, boolean isClient) {
48        if (rec == null) {
49            throw new IllegalArgumentException("Client bounds can't be null");
50        }
51        isClientSizeSet = isClient;
52        this.loc = rec.getLocation();
53        this.size = rec.getSize();
54        setInsets(ins);
55    }
56
57    /**
58     * If isClient is true, the bounds represent the client window area.
59     * Otherwise, they represent the entire window area, with the insets included
60     */
61    public WindowDimensions(Point loc, Dimension size, Insets in, boolean isClient) {
62        this(new Rectangle(loc, size), in, isClient);
63    }
64
65    /**
66     * If isClient is true, the bounds represent the client window area.
67     * Otherwise, they represent the entire window area, with the insets included
68     */
69    public WindowDimensions(Rectangle bounds, boolean isClient) {
70        this(bounds, null, isClient);
71    }
72
73    public WindowDimensions(final WindowDimensions dims) {
74        this.loc = new Point(dims.loc);
75        this.size = new Dimension(dims.size);
76        this.insets = (dims.insets != null)?((Insets)dims.insets.clone()):new Insets(0, 0, 0, 0);
77        this.isClientSizeSet = dims.isClientSizeSet;
78    }
79
80    public Rectangle getClientRect() {
81        if (isClientSizeSet) {
82            return new Rectangle(loc, size);
83        } else {
84            // Calculate client bounds
85            if (insets != null) {
86                return new Rectangle(loc.x, loc.y,
87                                     size.width-(insets.left+insets.right),
88                                     size.height-(insets.top+insets.bottom));
89            } else {
90                return new Rectangle(loc, size);
91            }
92        }
93    }
94
95    public void setClientSize(Dimension size) {
96        this.size = new Dimension(size);
97        isClientSizeSet = true;
98    }
99
100    public void setClientSize(int width, int height) {
101        size = new Dimension(width, height);
102        isClientSizeSet = true;
103    }
104
105    public Dimension getClientSize() {
106        return getClientRect().getSize();
107    }
108
109    public void setSize(int width, int height) {
110        size = new Dimension(width, height);
111        isClientSizeSet = false;
112    }
113
114    public Dimension getSize() {
115        return getBounds().getSize();
116    }
117
118    public Insets getInsets() {
119        return (Insets)insets.clone();
120    }
121    public Rectangle getBounds() {
122        if (isClientSizeSet) {
123            Rectangle res = new Rectangle(loc, size);
124            res.width += (insets.left + insets.right);
125            res.height += (insets.top + insets.bottom);
126            return res;
127        } else {
128            return new Rectangle(loc, size);
129        }
130    }
131
132    public Point getLocation() {
133        return new Point(loc);
134    }
135    public void setLocation(int x, int y) {
136        loc = new Point(x, y);
137    }
138
139    public Rectangle getScreenBounds() {
140        Dimension size = getClientSize();
141        Point location = getLocation(); // this is Java location
142        location.x += insets.left;
143        location.y += insets.top;
144        return new Rectangle(location, size);
145    }
146
147    public void setInsets(Insets in) {
148        insets = (in != null)?((Insets)in.clone()):new Insets(0, 0, 0, 0);
149        if (!isClientSizeSet) {
150            if (size.width < (insets.left+insets.right)) {
151                size.width = (insets.left+insets.right);
152            }
153            if (size.height < (insets.top+insets.bottom)) {
154                size.height = (insets.top+insets.bottom);
155            }
156        }
157    }
158
159    public boolean isClientSizeSet() {
160        return isClientSizeSet;
161    }
162
163    public String toString() {
164        return "[" + loc + ", " + size + "(" +(isClientSizeSet?"client":"bounds") + ")+" + insets + "]";
165    }
166
167    public boolean equals(Object o) {
168        if (!(o instanceof WindowDimensions)) {
169            return false;
170        }
171        WindowDimensions dims = (WindowDimensions)o;
172        return ((dims.insets.equals(insets)))
173            && (getClientRect().equals(dims.getClientRect()))
174            && (getBounds().equals(dims.getBounds()));
175    }
176
177    public int hashCode() {
178        return ((insets == null)? (0):(insets.hashCode())) ^ getClientRect().hashCode() ^ getBounds().hashCode();
179    }
180}
181