jdk/src/solaris/classes/sun/awt/X11/XIconWindow.java
author yan
Mon, 06 Oct 2008 16:45:00 +0400
changeset 1966 12a51fb0db0d
parent 439 3488710b02f8
child 3938 ef327bd847c0
child 4191 ca8d3ef845c8
permissions -rw-r--r--
5100701: Toolkit.getLockingKeyState() does not work on XToolkit, but works on Motif Summary: Does not work on Motif but works on XToolkit now; implemented using XQueryPointer. Reviewed-by: anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
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
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.awt.X11GraphicsConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.awt.image.ToolkitImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.awt.image.ImageRepresentation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.logging.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class XIconWindow extends XBaseWindow {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private final static Logger log = Logger.getLogger("sun.awt.X11.XIconWindow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    XDecoratedPeer parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    Dimension size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    long iconPixmap = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    long iconMask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    int iconWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    int iconHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    XIconWindow(XDecoratedPeer parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        super(new XCreateWindowParams(new Object[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            PARENT, parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            DELAYED, Boolean.TRUE}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    void instantPreInit(XCreateWindowParams params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        super.instantPreInit(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        this.parent = (XDecoratedPeer)params.get(PARENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @return array of XIconsSize structures, caller must free this array after use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private XIconSize[] getIconSizes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            AwtGraphicsConfigData adata = parent.getGraphicsConfigurationData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            final long screen = adata.get_awt_visInfo().get_screen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            final long display = XToolkit.getDisplay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (log.isLoggable(Level.FINEST)) log.finest(adata.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            long status =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                XlibWrapper.XGetIconSizes(display, XToolkit.getDefaultRootWindow(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                          XlibWrapper.larg1, XlibWrapper.iarg1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (status == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            int count = Native.getInt(XlibWrapper.iarg1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            long sizes_ptr = Native.getLong(XlibWrapper.larg1); // XIconSize*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            log.log(Level.FINEST, "count = {1}, sizes_ptr = {0}", new Object[] {Long.valueOf(sizes_ptr), Integer.valueOf(count)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            XIconSize[] res = new XIconSize[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            for (int i = 0; i < count; i++, sizes_ptr += XIconSize.getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                res[i] = new XIconSize(sizes_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                log.log(Level.FINEST, "sizes_ptr[{1}] = {0}", new Object[] {res[i], Integer.valueOf(i)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private Dimension calcIconSize(int widthHint, int heightHint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (XWM.getWMID() == XWM.ICE_WM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            // ICE_WM has a bug - it only displays icons of the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // 16x16, while reporting 32x32 in its size list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            log.log(Level.FINEST, "Returning ICE_WM icon size: 16x16");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return new Dimension(16, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        XIconSize[] sizeList = getIconSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        log.log(Level.FINEST, "Icon sizes: {0}", new Object[] {sizeList});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (sizeList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            // No icon sizes so we simply fall back to 16x16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return new Dimension(16, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int dist = 0xffffffff, newDist, diff = 0, closestHeight, closestWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int saveWidth = 0, saveHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        for (int i = 0; i < sizeList.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (widthHint >= sizeList[i].get_min_width() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                widthHint <= sizeList[i].get_max_width() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                heightHint >= sizeList[i].get_min_height() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                heightHint <= sizeList[i].get_max_height()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if ((((widthHint-sizeList[i].get_min_width())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                      % sizeList[i].get_width_inc()) == 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    (((heightHint-sizeList[i].get_min_height())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                      % sizeList[i].get_height_inc()) ==0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    /* Found an exact match */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    saveWidth = widthHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    saveHeight = heightHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    dist = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                diff = widthHint - sizeList[i].get_min_width();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                if (diff == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    closestWidth = widthHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    diff = diff%sizeList[i].get_width_inc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    closestWidth = widthHint - diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                diff = heightHint - sizeList[i].get_min_height();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (diff == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    closestHeight = heightHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    diff = diff%sizeList[i].get_height_inc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    closestHeight = heightHint - diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                newDist = closestWidth*closestWidth +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    closestHeight*closestHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if (dist > newDist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    saveWidth = closestWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    saveHeight = closestHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    dist = newDist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (log.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            log.finest("found=" + found);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (log.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                log.finest("widthHint=" + widthHint + ", heightHint=" + heightHint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                           + ", saveWidth=" + saveWidth + ", saveHeight=" + saveHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                           + ", max_width=" + sizeList[0].get_max_width()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                           + ", max_height=" + sizeList[0].get_max_height()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                           + ", min_width=" + sizeList[0].get_min_width()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                           + ", min_height=" + sizeList[0].get_min_height());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (widthHint  > sizeList[0].get_max_width() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                heightHint > sizeList[0].get_max_height())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                // Icon image too big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                /* determine which way to scale */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                int wdiff = widthHint - sizeList[0].get_max_width();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                int hdiff = heightHint - sizeList[0].get_max_height();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if (log.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    log.finest("wdiff=" + wdiff + ", hdiff=" + hdiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                if (wdiff >= hdiff) { /* need to scale width more  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    saveWidth = sizeList[0].get_max_width();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    saveHeight =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        (int)(((double)sizeList[0].get_max_width()/widthHint) * heightHint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    saveWidth =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        (int)(((double)sizeList[0].get_max_height()/heightHint) * widthHint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    saveHeight = sizeList[0].get_max_height();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } else if (widthHint  < sizeList[0].get_min_width() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                       heightHint < sizeList[0].get_min_height())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                // Icon image too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                saveWidth = (sizeList[0].get_min_width()+sizeList[0].get_max_width())/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                saveHeight = (sizeList[0].get_min_height()+sizeList[0].get_max_height())/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                // Icon image fits within right size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                saveWidth = widthHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                saveHeight = widthHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            XlibWrapper.XFree(sizeList[0].pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (log.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            log.finest("return " + saveWidth + "x" + saveHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return new Dimension(saveWidth, saveHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return preffered icon size calculated from specific icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    Dimension getIconSize(int widthHint, int heightHint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (size == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            size = calcIconSize(widthHint, heightHint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    * This function replaces iconPixmap handle with new image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    * It does not replace window's hints, so it should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    * called only from setIconImage()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
   void replaceImage(Image img)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        //Prepare image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        //create new buffered image of desired size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        //in current window's color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        BufferedImage bi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (img != null && iconWidth != 0 && iconHeight != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            GraphicsConfiguration defaultGC = parent.getGraphicsConfiguration().getDevice().getDefaultConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            ColorModel model = defaultGC.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            WritableRaster raster = model.createCompatibleWritableRaster(iconWidth, iconHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            bi = new BufferedImage(model, raster, model.isAlphaPremultiplied(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            Graphics g = bi.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                //We need to draw image on SystemColors.window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                //for using as iconWindow's background
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                g.setColor(SystemColor.window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                g.fillRect(0, 0, iconWidth, iconHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                if (g instanceof Graphics2D) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    ((Graphics2D)g).setComposite(AlphaComposite.Src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                g.drawImage(img, 0, 0, iconWidth, iconHeight, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        //create pixmap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (iconPixmap != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                XlibWrapper.XFreePixmap(XToolkit.getDisplay(), iconPixmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                iconPixmap = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                log.finest("Freed previous pixmap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (bi == null || iconWidth == 0 || iconHeight == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                return;  //The iconPixmap is 0 now, we have done everything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            AwtGraphicsConfigData adata = parent.getGraphicsConfigurationData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            awtImageData awtImage = adata.get_awtImage(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            XVisualInfo visInfo = adata.get_awt_visInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            iconPixmap = XlibWrapper.XCreatePixmap(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                                   XlibWrapper.RootWindow(XToolkit.getDisplay(), visInfo.get_screen()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                                   iconWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                                   iconHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                                   awtImage.get_Depth()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                                   );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (iconPixmap == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                log.finest("Can't create new pixmap for icon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                return; //Can't do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            //Transform image data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            long bytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            DataBuffer srcBuf = bi.getData().getDataBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (srcBuf instanceof DataBufferByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                byte[] buf = ((DataBufferByte)srcBuf).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                ColorData cdata = adata.get_color_data(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                int num_colors = cdata.get_awt_numICMcolors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                for (int i = 0; i < buf.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    buf[i] = (buf[i] >= num_colors) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        0 : cdata.get_awt_icmLUT2Colors(buf[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                bytes = Native.toData(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } else if (srcBuf instanceof DataBufferInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                bytes = Native.toData(((DataBufferInt)srcBuf).getData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            } else if (srcBuf instanceof DataBufferUShort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                bytes = Native.toData(((DataBufferUShort)srcBuf).getData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                throw new IllegalArgumentException("Unknown data buffer: " + srcBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            int bpp = awtImage.get_wsImageFormat().get_bits_per_pixel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            int slp =awtImage.get_wsImageFormat().get_scanline_pad();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            int bpsl = paddedwidth(iconWidth*bpp, slp) >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (((bpsl << 3) / bpp) < iconWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                log.finest("Image format doesn't fit to icon width");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            long dst = XlibWrapper.XCreateImage(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                                visInfo.get_visual(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                                (int)awtImage.get_Depth(),
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   296
                                                (int)XConstants.ZPixmap,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                                0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                                bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                                iconWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                                iconHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                                32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                                bpsl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (dst == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                log.finest("Can't create XImage for icon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                XlibWrapper.XFreePixmap(XToolkit.getDisplay(), iconPixmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                iconPixmap = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                log.finest("Created XImage for icon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            long gc = XlibWrapper.XCreateGC(XToolkit.getDisplay(), iconPixmap, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (gc == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                log.finest("Can't create GC for pixmap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                XlibWrapper.XFreePixmap(XToolkit.getDisplay(), iconPixmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                iconPixmap = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                log.finest("Created GC for pixmap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                XlibWrapper.XPutImage(XToolkit.getDisplay(), iconPixmap, gc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                      dst, 0, 0, 0, 0, iconWidth, iconHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                XlibWrapper.XFreeGC(XToolkit.getDisplay(), gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    * This function replaces iconPixmap handle with new image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    * It does not replace window's hints, so it should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    * called only from setIconImage()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    void replaceMask(Image img) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        //Prepare image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        BufferedImage bi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (img != null && iconWidth != 0 && iconHeight != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            bi = new BufferedImage(iconWidth, iconHeight, BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            Graphics g = bi.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                g.drawImage(img, 0, 0, iconWidth, iconHeight, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        //create mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (iconMask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                XlibWrapper.XFreePixmap(XToolkit.getDisplay(), iconMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                iconMask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                log.finest("Freed previous mask");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (bi == null || iconWidth == 0 || iconHeight == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                return;  //The iconMask is 0 now, we have done everything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            AwtGraphicsConfigData adata = parent.getGraphicsConfigurationData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            awtImageData awtImage = adata.get_awtImage(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            XVisualInfo visInfo = adata.get_awt_visInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            ColorModel cm = bi.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            DataBuffer srcBuf = bi.getRaster().getDataBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            int sidx = 0;//index of source element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            int bpl = (iconWidth + 7) >> 3;//bytes per line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            byte[] destBuf = new byte[bpl * iconHeight];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            int didx = 0;//index of destination element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            for (int i = 0; i < iconHeight; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                int dbit = 0;//index of current bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                int cv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                for (int j = 0; j < iconWidth; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    if (cm.getAlpha(srcBuf.getElem(sidx)) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        cv = cv + (1 << dbit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    dbit++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    if (dbit == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        destBuf[didx] = (byte)cv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        cv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        dbit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        didx++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    sidx++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            iconMask = XlibWrapper.XCreateBitmapFromData(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                XlibWrapper.RootWindow(XToolkit.getDisplay(), visInfo.get_screen()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                Native.toData(destBuf),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                iconWidth, iconHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Sets icon image by selecting one of the images from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * The selected image is the one having the best matching size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    void setIconImages(java.util.List<XIconInfo> icons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (icons == null || icons.size() == 0) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        int minDiff = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        Image min = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        for (XIconInfo iconInfo : icons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (iconInfo.isValid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                Image image = iconInfo.getImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                Dimension dim = calcIconSize(image.getWidth(null), image.getHeight(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                int widthDiff = Math.abs(dim.width - image.getWidth(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                int heightDiff = Math.abs(image.getHeight(null) - dim.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                // "=" below allows to select the best matching icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                if (minDiff >= (widthDiff + heightDiff)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    minDiff = (widthDiff + heightDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    min = image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (min != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            log.log(Level.FINER, "Icon: {0}x{1}", new Object[] { min.getWidth(null), min.getHeight(null)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            setIconImage(min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    void setIconImage(Image img) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (img == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            //if image is null, reset to default image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            replaceImage(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            replaceMask(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            //get image size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if (img instanceof ToolkitImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                ImageRepresentation ir = ((ToolkitImage)img).getImageRep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                ir.reconstruct(ImageObserver.ALLBITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                width = ir.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                height = ir.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                width = img.getWidth(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                height = img.getHeight(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            Dimension iconSize = getIconSize(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            if (iconSize != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                log.log(Level.FINEST, "Icon size: {0}", iconSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                iconWidth = iconSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                iconHeight = iconSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                log.finest("Error calculating image size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                iconWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                iconHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            replaceImage(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            replaceMask(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        //create icon window and set XWMHints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            AwtGraphicsConfigData adata = parent.getGraphicsConfigurationData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            awtImageData awtImage = adata.get_awtImage(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            XVisualInfo visInfo = adata.get_awt_visInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            XWMHints hints = parent.getWMHints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            window = hints.get_icon_window();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            if (window == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                log.finest("Icon window wasn't set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                XCreateWindowParams params = getDelayedParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                params.add(BORDER_PIXEL, Long.valueOf(XToolkit.getAwtDefaultFg()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                params.add(BACKGROUND_PIXMAP, iconPixmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                params.add(COLORMAP, adata.get_awt_cmap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                params.add(DEPTH, awtImage.get_Depth());
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   473
                params.add(VISUAL_CLASS, (int)XConstants.InputOutput);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                params.add(VISUAL, visInfo.get_visual());
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   475
                params.add(VALUE_MASK, XConstants.CWBorderPixel | XConstants.CWColormap | XConstants.CWBackPixmap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                params.add(PARENT_WINDOW, XlibWrapper.RootWindow(XToolkit.getDisplay(), visInfo.get_screen()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                params.add(BOUNDS, new Rectangle(0, 0, iconWidth, iconHeight));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                params.remove(DELAYED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                init(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                if (getWindow() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    log.finest("Can't create new icon window");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    log.finest("Created new icon window");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if (getWindow() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                XlibWrapper.XSetWindowBackgroundPixmap(XToolkit.getDisplay(), getWindow(), iconPixmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                XlibWrapper.XClearWindow(XToolkit.getDisplay(), getWindow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            // Provide both pixmap and window, WM or Taskbar will use the one they find more appropriate
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   491
            long newFlags = hints.get_flags() | XUtilConstants.IconPixmapHint | XUtilConstants.IconMaskHint;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (getWindow()  != 0) {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   493
                newFlags |= XUtilConstants.IconWindowHint;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            hints.set_flags(newFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            hints.set_icon_pixmap(iconPixmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            hints.set_icon_mask(iconMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            hints.set_icon_window(getWindow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            XlibWrapper.XSetWMHints(XToolkit.getDisplay(), parent.getShell(), hints.pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            log.finest("Set icon window hint");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    static int paddedwidth(int number, int boundary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        return (((number) + ((boundary) - 1)) & (~((boundary) - 1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
}