1/*
2 * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package com.sun.java.swing.plaf.gtk;
26
27import javax.swing.*;
28import javax.swing.plaf.synth.*;
29import java.awt.Color;
30import java.awt.Graphics;
31import java.awt.Rectangle;
32
33/**
34 * @author Joshua Outwater
35 */
36class GTKGraphicsUtils extends SynthGraphicsUtils {
37    public void paintText(SynthContext context, Graphics g, String text,
38                          int x, int y, int mnemonicIndex) {
39        if (text == null || text.length() <= 0) {
40            // We don't need to paint empty strings
41            return;
42        }
43
44        if (context.getRegion() == Region.INTERNAL_FRAME_TITLE_PANE) {
45            // Metacity handles painting of text on internal frame title,
46            // ignore this.
47            return;
48        }
49        int componentState = context.getComponentState();
50        if ((componentState & SynthConstants.DISABLED) ==
51                              SynthConstants.DISABLED){
52            if (!GTKLookAndFeel.is3()) {
53                Color orgColor = g.getColor();
54                g.setColor(context.getStyle().getColor(context,
55                        GTKColorType.WHITE));
56                x += 1;
57                y += 1;
58                super.paintText(context, g, text, x, y, mnemonicIndex);
59
60                g.setColor(orgColor);
61                x -= 1;
62                y -= 1;
63            }
64            super.paintText(context, g, text, x, y, mnemonicIndex);
65        }
66        else {
67            String themeName = GTKLookAndFeel.getGtkThemeName();
68            if (themeName != null && themeName.startsWith("blueprint") &&
69                shouldShadowText(context.getRegion(), componentState)) {
70
71                g.setColor(Color.BLACK);
72                super.paintText(context, g, text, x+1, y+1, mnemonicIndex);
73                g.setColor(Color.WHITE);
74            }
75
76            super.paintText(context, g, text, x, y, mnemonicIndex);
77        }
78    }
79
80    /**
81     * Paints text at the specified location. This will not attempt to
82     * render the text as html nor will it offset by the insets of the
83     * component.
84     *
85     * @param context SynthContext
86     * @param g Graphics used to render string in.
87     * @param text Text to render
88     * @param bounds Bounds of the text to be drawn.
89     * @param mnemonicIndex Index to draw string at.
90     */
91    public void paintText(SynthContext context, Graphics g, String text,
92                          Rectangle bounds, int mnemonicIndex) {
93        if (text == null || text.length() <= 0) {
94            // We don't need to paint empty strings
95            return;
96        }
97
98        Region id = context.getRegion();
99        if ((id == Region.RADIO_BUTTON ||
100             id == Region.CHECK_BOX ||
101             id == Region.TABBED_PANE_TAB) &&
102            (context.getComponentState() & SynthConstants.FOCUSED) != 0)
103        {
104            JComponent source = context.getComponent();
105            if (!(source instanceof AbstractButton) ||
106                ((AbstractButton)source).isFocusPainted()) {
107
108                // The "bounds" parameter encompasses only the actual text;
109                // when drawing the focus, we need to expand that bounding
110                // box by "focus-line-width" plus "focus-padding".  Note that
111                // the layout process for these components will have already
112                // taken these values into account, so there should always
113                // be enough space allocated for drawing the focus indicator.
114                int synthState = context.getComponentState();
115                GTKStyle style = (GTKStyle)context.getStyle();
116                int focusSize =
117                    style.getClassSpecificIntValue(context,
118                                                   "focus-line-width", 1);
119                int focusPad =
120                    style.getClassSpecificIntValue(context,
121                                                   "focus-padding", 1);
122                int totalFocus = focusSize + focusPad;
123                int x = bounds.x - totalFocus;
124                int y = bounds.y - totalFocus;
125                int w = bounds.width  + (2 * totalFocus);
126                int h = bounds.height + (2 * totalFocus);
127
128                Color color = g.getColor();
129                GTKPainter.INSTANCE.paintFocus(context, g, id,
130                                               synthState, "checkbutton",
131                                               x, y, w, h);
132                g.setColor(color);
133            }
134        }
135        super.paintText(context, g, text, bounds, mnemonicIndex);
136    }
137
138    private static boolean shouldShadowText(Region id, int state) {
139        int gtkState = GTKLookAndFeel.synthStateToGTKState(id, state);
140        return((gtkState == SynthConstants.MOUSE_OVER) &&
141               (id == Region.MENU ||
142                id == Region.MENU_ITEM ||
143                id == Region.CHECK_BOX_MENU_ITEM ||
144                id == Region.RADIO_BUTTON_MENU_ITEM));
145    }
146}
147