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