1/*
2 * Copyright (c) 2011, 2013, 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
26package com.apple.laf;
27
28import java.awt.*;
29
30import javax.swing.*;
31import javax.swing.border.Border;
32import javax.swing.plaf.*;
33
34import apple.laf.JRSUIConstants.*;
35
36import com.apple.laf.AquaUtilControlSize.*;
37import com.apple.laf.AquaUtils.*;
38
39public abstract class AquaButtonBorder extends AquaBorder implements Border, UIResource {
40    private static final RecyclableSingleton<Dynamic> fDynamic = new RecyclableSingletonFromDefaultConstructor<Dynamic>(Dynamic.class);
41    public static AquaButtonBorder getDynamicButtonBorder() {
42        return fDynamic.get();
43    }
44
45    private static final RecyclableSingleton<Toggle> fToggle = new RecyclableSingletonFromDefaultConstructor<Toggle>(Toggle.class);
46    public static AquaButtonBorder getToggleButtonBorder() {
47        return fToggle.get();
48    }
49
50    private static final RecyclableSingleton<Toolbar> fToolBar = new RecyclableSingletonFromDefaultConstructor<Toolbar>(Toolbar.class);
51    public static Border getToolBarButtonBorder() {
52        return fToolBar.get();
53    }
54
55    private static final RecyclableSingleton<Named> fBevel = new RecyclableSingleton<Named>() {
56        protected Named getInstance() {
57            return new Named(Widget.BUTTON_BEVEL, new SizeDescriptor(new SizeVariant().alterMargins(2, 4, 2, 4)));
58        }
59    };
60    public static AquaButtonBorder getBevelButtonBorder() {
61        return fBevel.get();
62    }
63
64    public AquaButtonBorder(final SizeDescriptor sizeDescriptor) {
65        super(sizeDescriptor);
66    }
67
68    public AquaButtonBorder(final AquaButtonBorder other) {
69        super(other);
70    }
71
72    public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
73        // for now we don't paint a border. We let the button paint it since there
74        // needs to be a strict ordering for aqua components.
75        //paintButton(c, g, x, y, width, height);
76    }
77
78    public void paintButton(final Component c, final Graphics g, int x, int y, int width, int height) {
79        final AbstractButton b = (AbstractButton)c;
80        final ButtonModel model = b.getModel();
81
82        final State state = getButtonState(b, model);
83        painter.state.set(state);
84        painter.state.set((state != State.DISABLED && state != State.INACTIVE) && b.isFocusPainted() && isFocused(b) ? Focused.YES : Focused.NO);
85
86        // Full border size of the component.
87        // g.setColor(new Color(0, 255, 0, 70));
88        // g.drawRect(x, y, width - 1, height - 1);
89
90        final Insets subInsets = sizeVariant.insets;
91        x += subInsets.left;
92        y += subInsets.top;
93        width -= (subInsets.left + subInsets.right);
94        height -= (subInsets.top + subInsets.bottom);
95
96        // Where the native border should start to paint.
97        // g.setColor(new Color(255, 0, 255, 70));
98        // g.drawRect(x, y, width - 1, height - 1);
99
100        doButtonPaint(b, model, g, x, y, width, height);
101    }
102
103    protected void doButtonPaint(final AbstractButton b, final ButtonModel model, final Graphics g, final int x, final int y, final int width, final int height) {
104        painter.paint(g, b, x, y, width, height);
105    }
106
107    protected State getButtonState(final AbstractButton b, final ButtonModel model) {
108        if (!b.isEnabled()) return State.DISABLED;
109
110        // The default button shouldn't draw its color when the window is inactive.
111        // Changed for <rdar://problem/3614421>: Aqua LAF Buttons are incorrectly drawn disabled
112        // all we need to do is make sure we aren't the default button any more and that
113        // we aren't active, but we still are enabled if the button is enabled.
114        // if we set dimmed we would appear disabled despite being enabled and click through
115        // works so this now matches the text drawing and most importantly the HIG
116        if (!AquaFocusHandler.isActive(b)) return State.INACTIVE;
117
118        if (model.isArmed() && model.isPressed()) return State.PRESSED;
119        if (model.isSelected() && isSelectionPressing()) return State.PRESSED;
120        if ((b instanceof JButton) && ((JButton)b).isDefaultButton()) return State.PULSED;
121
122        return State.ACTIVE;
123    }
124
125    protected boolean isSelectionPressing() {
126        return true;
127    }
128
129    public boolean hasSmallerInsets(final JComponent c) {
130        final Insets inset = c.getInsets();
131        final Insets margin = sizeVariant.margins;
132
133        if (margin.equals(inset)) return false;
134
135        return (
136            (inset.top < margin.top) ||
137            (inset.left < margin.left) ||
138            (inset.right < margin.right) ||
139            (inset.bottom < margin.bottom)
140        );
141    }
142
143    /**
144     * Returns the insets of the border.
145     * @param c the component for which this border insets value applies
146     */
147    public Insets getBorderInsets(final Component c) {
148        if (c == null || !(c instanceof AbstractButton)) return new Insets(0, 0, 0, 0);
149
150        Insets margin = ((AbstractButton)c).getMargin();
151        margin = (margin == null) ? new InsetsUIResource(0, 0, 0, 0) : (Insets)margin.clone();
152
153        margin.top += sizeVariant.margins.top;
154        margin.bottom += sizeVariant.margins.bottom;
155        margin.left += sizeVariant.margins.left;
156        margin.right += sizeVariant.margins.right;
157
158        return margin;
159    }
160
161    public Insets getContentInsets(final AbstractButton b, final int w, final int h) {
162        return null;
163    }
164
165    public void alterPreferredSize(final Dimension d) {
166        if (sizeVariant.h > 0 && sizeVariant.h > d.height) d.height = sizeVariant.h;
167        if (sizeVariant.w > 0 && sizeVariant.w > d.width) d.width = sizeVariant.w;
168    }
169
170    /**
171     * Returns whether or not the border is opaque.  If the border
172     * is opaque, it is responsible for filling in it's own
173     * background when painting.
174     */
175    public boolean isBorderOpaque() {
176        return false;
177    }
178
179    static class SizeConstants {
180        protected static final int fNormalButtonHeight = 29;
181        protected static final int fNormalMinButtonWidth = 40;
182        protected static final int fSquareButtonHeightThreshold = 23;
183        protected static final int fSquareButtonWidthThreshold = 16;
184    }
185
186    public static class Dynamic extends AquaButtonBorder {
187        final Insets ALTERNATE_PUSH_INSETS = new Insets(3, 12, 5, 12);
188        final Insets ALTERNATE_BEVEL_INSETS = new Insets(0, 5, 0, 5);
189        final Insets ALTERNATE_SQUARE_INSETS = new Insets(0, 2, 0, 2);
190        public Dynamic() {
191            super(new SizeDescriptor(new SizeVariant(75, 29).alterMargins(3, 20, 5, 20)) {
192                public SizeVariant deriveSmall(final SizeVariant v) {
193                    return super.deriveSmall(v.alterMinSize(0, -2).alterMargins(0, -3, 0, -3).alterInsets(-3, -3, -4, -3));
194                }
195                public SizeVariant deriveMini(final SizeVariant v) {
196                    return super.deriveMini(v.alterMinSize(0, -2).alterMargins(0, -3, 0, -3).alterInsets(-3, -3, -1, -3));
197                }
198            });
199        }
200
201        public Dynamic(final Dynamic other) {
202            super(other);
203        }
204
205        protected State getButtonState(final AbstractButton b, final ButtonModel model) {
206            final State state = super.getButtonState(b, model);
207            painter.state.set(state == State.PULSED ? Animating.YES : Animating.NO);
208            return state;
209        }
210
211        public Insets getContentInsets(final AbstractButton b, final int width, final int height) {
212            final Size size = AquaUtilControlSize.getUserSizeFrom(b);
213            final Widget style = getStyleForSize(b, size, width, height);
214
215            if (style == Widget.BUTTON_PUSH) {
216                return ALTERNATE_PUSH_INSETS;
217            }
218            if (style == Widget.BUTTON_BEVEL_ROUND) {
219                return ALTERNATE_BEVEL_INSETS;
220            }
221            if (style == Widget.BUTTON_BEVEL) {
222                return ALTERNATE_SQUARE_INSETS;
223            }
224
225            return null;
226        }
227
228        protected void doButtonPaint(final AbstractButton b, final ButtonModel model, final Graphics g, int x, int y, int width, int height) {
229            final Size size = AquaUtilControlSize.getUserSizeFrom(b);
230            painter.state.set(size);
231
232            final Widget style = getStyleForSize(b, size, width, height);
233            painter.state.set(style);
234
235            // custom adjusting
236            if (style == Widget.BUTTON_PUSH && y % 2 == 0) {
237                if (size == Size.REGULAR) { y += 1; height -= 1; }
238                if (size == Size.MINI) { height -= 1; x += 4; width -= 8; }
239            }
240
241            super.doButtonPaint(b, model, g, x, y, width, height);
242        }
243
244        protected Widget getStyleForSize(final AbstractButton b, final Size size, final int width, final int height) {
245            if (size != null && size != Size.REGULAR) {
246                return Widget.BUTTON_PUSH;
247            }
248
249            if (height < SizeConstants.fSquareButtonHeightThreshold || width < SizeConstants.fSquareButtonWidthThreshold) {
250                return Widget.BUTTON_BEVEL;
251            }
252
253            if (height <= SizeConstants.fNormalButtonHeight + 3 && width < SizeConstants.fNormalMinButtonWidth) {
254                return Widget.BUTTON_BEVEL;
255            }
256
257            if ((height > SizeConstants.fNormalButtonHeight + 3) || (b.getIcon() != null) || hasSmallerInsets(b)){
258                return Widget.BUTTON_BEVEL_ROUND;
259            }
260
261            return Widget.BUTTON_PUSH;
262        }
263    }
264
265    public static class Toggle extends AquaButtonBorder {
266        public Toggle() {
267            super(new SizeDescriptor(new SizeVariant().alterMargins(6, 6, 6, 6)));
268        }
269
270        public Toggle(final Toggle other) {
271            super(other);
272        }
273
274        protected void doButtonPaint(final AbstractButton b, final ButtonModel model, final Graphics g, final int x, final int y, final int width, final int height) {
275            if (height < SizeConstants.fSquareButtonHeightThreshold || width < SizeConstants.fSquareButtonWidthThreshold) {
276                painter.state.set(Widget.BUTTON_BEVEL);
277                super.doButtonPaint(b, model, g, x, y, width, height);
278                return;
279            }
280
281            painter.state.set(Widget.BUTTON_BEVEL_ROUND);
282            super.doButtonPaint(b, model, g, x, y + 1, width, height - 1);
283        }
284    }
285
286    public static class Named extends AquaButtonBorder {
287        public Named(final Widget widget, final SizeDescriptor sizeDescriptor) {
288            super(sizeDescriptor);
289            painter.state.set(widget);
290        }
291
292        // called by reflection
293        public Named(final Named sizeDescriptor) {
294            super(sizeDescriptor);
295        }
296
297        protected void doButtonPaint(final AbstractButton b, final ButtonModel model, final Graphics g, final int x, final int y, final int width, final int height) {
298            painter.state.set(model.isSelected() ? BooleanValue.YES : BooleanValue.NO);
299            super.doButtonPaint(b, model, g, x, y, width, height);
300        }
301    }
302
303    public static class Toolbar extends AquaButtonBorder {
304        public Toolbar() {
305            super(new SizeDescriptor(new SizeVariant().alterMargins(5, 5, 5, 5)));
306            painter.state.set(Widget.TOOLBAR_ITEM_WELL);
307        }
308
309        public Toolbar(final Toolbar other) {
310            super(other);
311        }
312
313        protected void doButtonPaint(final AbstractButton b, final ButtonModel model, final Graphics g, final int x, final int y, final int w, final int h) {
314            if (!model.isSelected()) return; // only paint when the toolbar button is selected
315            super.doButtonPaint(b, model, g, x, y, w, h);
316        }
317    }
318}
319