1/*
2 * Copyright (c) 2011, 2012, 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 */
25
26//
27//  AquaFocus.java
28//  Copyright (c) 2009 Apple Inc. All rights reserved.
29//
30
31package com.apple.laf;
32
33import java.awt.*;
34
35import javax.swing.*;
36
37import sun.java2d.*;
38import apple.laf.JRSUIFocus;
39
40import com.apple.laf.AquaUtils.Painter;
41
42public class AquaFocus {
43    interface Drawable {
44        public void draw(final Graphics2D sg2d);
45    }
46
47    static boolean paintFocus(final Graphics g, final Drawable drawable) {
48        // TODO: requires OSXSurfaceData
49        return false;
50        /*if (!(g instanceof SunGraphics2D)) return false;
51        final SunGraphics2D sg2d = (SunGraphics2D)g;
52
53        final SurfaceData surfaceData = sg2d.getSurfaceData();
54        if (!(surfaceData instanceof OSXSurfaceData)) return false;
55
56        try {
57            ((OSXSurfaceData)surfaceData).performCocoaDrawing(sg2d, new OSXSurfaceData.CGContextDrawable() {
58                @Override
59                public void drawIntoCGContext(final long cgContext) {
60                    final JRSUIFocus focus = new JRSUIFocus(cgContext);
61                    focus.beginFocus(JRSUIFocus.RING_BELOW);
62                    drawable.draw(sg2d);
63                    focus.endFocus();
64                }
65            });
66        } finally {
67            sg2d.dispose();
68        }
69        return true;*/
70    }
71
72    public static Icon createFocusedIcon(final Icon tmpIcon, final Component c, final int slack) {
73        return new FocusedIcon(tmpIcon, slack);
74    }
75
76/* -- disabled until we can get the real JRSUI focus painter working
77
78    static class FocusedIcon implements Icon {
79        final Icon icon;
80        final int slack;
81
82        public FocusedIcon(final Icon icon, final int slack) {
83            this.icon = icon;
84            this.slack = slack;
85        }
86
87        @Override
88        public int getIconHeight() {
89            return icon.getIconHeight() + slack + slack;
90        }
91
92        @Override
93        public int getIconWidth() {
94            return icon.getIconWidth() + slack + slack;
95        }
96
97        @Override
98        public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
99            final boolean painted = paintFocus(g, new Drawable() {
100                @Override
101                public void draw(SunGraphics2D sg2d) {
102                    icon.paintIcon(c, sg2d, x + slack, y + slack);
103                }
104            });
105            if (!painted) {
106                icon.paintIcon(c, g, x + slack, y + slack);
107            }
108        }
109    }
110 */
111
112    static class FocusedIcon extends AquaUtils.ShadowBorder implements Icon {
113        final Icon icon;
114        final int slack;
115
116        public FocusedIcon(final Icon icon, final int slack) {
117            super(
118                new Painter() {
119                    public void paint(Graphics g, int x, int y, int w, int h) {
120                        Graphics2D imgG = (Graphics2D)g;
121                        imgG.setComposite(AlphaComposite.Src);
122                        imgG.setColor(UIManager.getColor("Focus.color"));
123                        imgG.fillRect(x, y, w - (slack * 2), h - (slack * 2));
124                        imgG.setComposite(AlphaComposite.DstAtop);
125                        icon.paintIcon(null, imgG, x, y);
126                    }
127                },
128                new Painter() {
129                    public void paint(Graphics g, int x, int y, int w, int h) {
130                        ((Graphics2D)g).setComposite(AlphaComposite.SrcAtop);
131                        icon.paintIcon(null, g, x, y);
132                    }
133                },
134                slack, slack, 0.0f, 1.8f, 7
135            );
136            this.icon = icon;
137            this.slack = slack;
138        }
139
140        @Override
141        public int getIconHeight() {
142            return icon.getIconHeight() + slack + slack;
143        }
144
145        @Override
146        public int getIconWidth() {
147            return icon.getIconWidth() + slack + slack;
148        }
149
150        @Override
151        public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
152            paintBorder(c, g, x, y, getIconWidth(), getIconHeight());
153            icon.paintIcon(c, g, x + slack, y + slack);
154        }
155    }
156}
157