jdk/src/share/classes/javax/swing/plaf/synth/SynthGraphicsUtils.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8376 bd543046f656
child 25173 4f093fa54fa1
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8376
diff changeset
     2
 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3981
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3981
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3981
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3981
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3981
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.plaf.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import sun.swing.SwingUtilities2;
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
    28
import sun.swing.MenuItemLayoutHelper;
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.plaf.basic.BasicHTML;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.swing.plaf.synth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Wrapper for primitive graphics calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class SynthGraphicsUtils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // These are used in the text painting code to avoid allocating a bunch of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // garbage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private Rectangle paintIconR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Rectangle paintTextR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private Rectangle paintViewR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private Insets paintInsets = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // These Rectangles/Insets are used in the text size calculation to avoid a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // a bunch of garbage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private Rectangle iconR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Rectangle textR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private Rectangle viewR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private Insets viewSizingInsets = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Creates a <code>SynthGraphicsUtils</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public SynthGraphicsUtils() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Draws a line between the two end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param context Identifies hosting region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param paintKey Identifies the portion of the component being asked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *                 to paint, may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param g Graphics object to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param x1 x origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param y1 y origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param x2 x destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param y2 y destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public void drawLine(SynthContext context, Object paintKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                         Graphics g, int x1, int y1, int x2, int y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        g.drawLine(x1, y1, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Draws a line between the two end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <p>This implementation supports only one line style key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <code>"dashed"</code>. The <code>"dashed"</code> line style is applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * only to vertical and horizontal lines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <p>Specifying <code>null</code> or any key different from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <code>"dashed"</code> will draw solid lines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param context identifies hosting region
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param paintKey identifies the portion of the component being asked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *                 to paint, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param g Graphics object to paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param x1 x origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param y1 y origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param x2 x destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param y2 y destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param styleKey identifies the requested style of the line (e.g. "dashed")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void drawLine(SynthContext context, Object paintKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                         Graphics g, int x1, int y1, int x2, int y2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                         Object styleKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if ("dashed".equals(styleKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // draw vertical line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (x1 == x2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                y1 += (y1 % 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                for (int y = y1; y <= y2; y+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    g.drawLine(x1, y, x2, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // draw horizontal line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            } else if (y1 == y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                x1 += (x1 % 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                for (int x = x1; x <= x2; x+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    g.drawLine(x, y1, x, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            // oblique lines are not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            drawLine(context, paintKey, g, x1, y1, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Lays out text and an icon returning, by reference, the location to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * place the icon and text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param ss SynthContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param fm FontMetrics for the Font to use, this may be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param text Text to layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param icon Icon to layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param hAlign horizontal alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param vAlign vertical alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param hTextPosition horizontal text position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param vTextPosition vertical text position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param viewR Rectangle to layout text and icon in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param iconR Rectangle to place icon bounds in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param textR Rectangle to place text in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param iconTextGap gap between icon and text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public String layoutText(SynthContext ss, FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                         String text, Icon icon, int hAlign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                         int vAlign, int hTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                         int vTextPosition, Rectangle viewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                         Rectangle iconR, Rectangle textR, int iconTextGap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (icon instanceof SynthIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            SynthIconWrapper wrapper = SynthIconWrapper.get((SynthIcon)icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                                            ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            String formattedText = SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                      ss.getComponent(), fm, text, wrapper, vAlign, hAlign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                      vTextPosition, hTextPosition, viewR, iconR, textR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                      iconTextGap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            SynthIconWrapper.release(wrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return formattedText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                      ss.getComponent(), fm, text, icon, vAlign, hAlign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                      vTextPosition, hTextPosition, viewR, iconR, textR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                      iconTextGap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Returns the size of the passed in string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param ss SynthContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param font Font to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param metrics FontMetrics, may be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param text Text to get size of.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public int computeStringWidth(SynthContext ss, Font font,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                  FontMetrics metrics, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return SwingUtilities2.stringWidth(ss.getComponent(), metrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                          text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Returns the minimum size needed to properly render an icon and text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param ss SynthContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param font Font to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param text Text to layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param icon Icon to layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param hAlign horizontal alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param vAlign vertical alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param hTextPosition horizontal text position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param vTextPosition vertical text position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param iconTextGap gap between icon and text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param mnemonicIndex Index into text to render the mnemonic at, -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *        indicates no mnemonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public Dimension getMinimumSize(SynthContext ss, Font font, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                      Icon icon, int hAlign, int vAlign, int hTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                      int vTextPosition, int iconTextGap, int mnemonicIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        JComponent c = ss.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        Dimension size = getPreferredSize(ss, font, text, icon, hAlign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                          vAlign, hTextPosition, vTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                          iconTextGap, mnemonicIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            size.width -= v.getPreferredSpan(View.X_AXIS) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                          v.getMinimumSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Returns the maximum size needed to properly render an icon and text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param ss SynthContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param font Font to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param text Text to layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param icon Icon to layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param hAlign horizontal alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param vAlign vertical alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param hTextPosition horizontal text position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param vTextPosition vertical text position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param iconTextGap gap between icon and text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param mnemonicIndex Index into text to render the mnemonic at, -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *        indicates no mnemonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public Dimension getMaximumSize(SynthContext ss, Font font, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                      Icon icon, int hAlign, int vAlign, int hTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                      int vTextPosition, int iconTextGap, int mnemonicIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        JComponent c = ss.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        Dimension size = getPreferredSize(ss, font, text, icon, hAlign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                          vAlign, hTextPosition, vTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                          iconTextGap, mnemonicIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            size.width += v.getMaximumSpan(View.X_AXIS) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                          v.getPreferredSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Returns the maximum height of the the Font from the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * SynthContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param context SynthContext used to determine font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return maximum height of the characters for the font from the passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *         in context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public int getMaximumCharHeight(SynthContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        FontMetrics fm = context.getComponent().getFontMetrics(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            context.getStyle().getFont(context));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return (fm.getAscent() + fm.getDescent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Returns the preferred size needed to properly render an icon and text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param ss SynthContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param font Font to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param text Text to layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param icon Icon to layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param hAlign horizontal alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param vAlign vertical alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param hTextPosition horizontal text position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param vTextPosition vertical text position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param iconTextGap gap between icon and text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param mnemonicIndex Index into text to render the mnemonic at, -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *        indicates no mnemonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public Dimension getPreferredSize(SynthContext ss, Font font, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                      Icon icon, int hAlign, int vAlign, int hTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                      int vTextPosition, int iconTextGap, int mnemonicIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        JComponent c = ss.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        Insets insets = c.getInsets(viewSizingInsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        int dx = insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int dy = insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (icon == null && (text == null || font == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return new Dimension(dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        else if ((text == null) || ((icon != null) && (font == null))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return new Dimension(SynthIcon.getIconWidth(icon, ss) + dx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                 SynthIcon.getIconHeight(icon, ss) + dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            FontMetrics fm = c.getFontMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            iconR.x = iconR.y = iconR.width = iconR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            textR.x = textR.y = textR.width = textR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            viewR.x = dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            viewR.y = dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            viewR.width = viewR.height = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            layoutText(ss, fm, text, icon, hAlign, vAlign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                   hTextPosition, vTextPosition, viewR, iconR, textR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                   iconTextGap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            int x1 = Math.min(iconR.x, textR.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            int x2 = Math.max(iconR.x + iconR.width, textR.x + textR.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            int y1 = Math.min(iconR.y, textR.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            int y2 = Math.max(iconR.y + iconR.height, textR.y + textR.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            Dimension rv = new Dimension(x2 - x1, y2 - y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            rv.width += dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            rv.height += dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Paints text at the specified location. This will not attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * render the text as html nor will it offset by the insets of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param ss SynthContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param g Graphics used to render string in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param text Text to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param bounds Bounds of the text to be drawn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param mnemonicIndex Index to draw string at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public void paintText(SynthContext ss, Graphics g, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                          Rectangle bounds, int mnemonicIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        paintText(ss, g, text, bounds.x, bounds.y, mnemonicIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Paints text at the specified location. This will not attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * render the text as html nor will it offset by the insets of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param ss SynthContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param g Graphics used to render string in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param text Text to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param x X location to draw text at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param y Upper left corner to draw text at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param mnemonicIndex Index to draw string at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public void paintText(SynthContext ss, Graphics g, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                          int x, int y, int mnemonicIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            JComponent c = ss.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            y += fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            SwingUtilities2.drawStringUnderlineCharAt(c, g, text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                                      mnemonicIndex, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Paints an icon and text. This will render the text as html, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * necessary, and offset the location by the insets of the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param ss SynthContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param g Graphics to render string and icon into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @param text Text to layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param icon Icon to layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param hAlign horizontal alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @param vAlign vertical alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param hTextPosition horizontal text position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param vTextPosition vertical text position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param iconTextGap gap between icon and text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param mnemonicIndex Index into text to render the mnemonic at, -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *        indicates no mnemonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param textOffset Amount to offset the text when painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public void paintText(SynthContext ss, Graphics g, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                      Icon icon, int hAlign, int vAlign, int hTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                      int vTextPosition, int iconTextGap, int mnemonicIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                      int textOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if ((icon == null) && (text == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        JComponent c = ss.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        Insets insets = SynthLookAndFeel.getPaintingInsets(ss, paintInsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        paintViewR.x = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        paintViewR.y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        paintViewR.width = c.getWidth() - (insets.left + insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        paintViewR.height = c.getHeight() - (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        String clippedText =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            layoutText(ss, fm, text, icon, hAlign, vAlign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                   hTextPosition, vTextPosition, paintViewR, paintIconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                   paintTextR, iconTextGap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (icon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            Color color = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (ss.getStyle().getBoolean(ss, "TableHeader.alignSorterArrow", false) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                "TableHeader.renderer".equals(c.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                paintIconR.x = paintViewR.width - paintIconR.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                paintIconR.x += textOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            paintIconR.y += textOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            SynthIcon.paintIcon(icon, ss, g, paintIconR.x, paintIconR.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                                paintIconR.width, paintIconR.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                v.paint(g, paintTextR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                paintTextR.x += textOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                paintTextR.y += textOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                paintText(ss, g, clippedText, paintTextR, mnemonicIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   416
     /**
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   417
      * A quick note about how preferred sizes are calculated... Generally
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   418
      * speaking, SynthPopupMenuUI will run through the list of its children
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   419
      * (from top to bottom) and ask each for its preferred size.  Each menu
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   420
      * item will add up the max width of each element (icons, text,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   421
      * accelerator spacing, accelerator text or arrow icon) encountered thus
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   422
      * far, so by the time all menu items have been calculated, we will
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   423
      * know the maximum (preferred) menu item size for that popup menu.
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   424
      * Later when it comes time to paint each menu item, we can use those
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   425
      * same accumulated max element sizes in order to layout the item.
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   426
      */
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   427
    static Dimension getPreferredMenuItemSize(SynthContext context,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   428
           SynthContext accContext, JComponent c,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   429
           Icon checkIcon, Icon arrowIcon, int defaultTextIconGap,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   430
           String acceleratorDelimiter, boolean useCheckAndArrow,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   431
           String propertyPrefix) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   432
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   433
         JMenuItem mi = (JMenuItem) c;
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   434
         SynthMenuItemLayoutHelper lh = new SynthMenuItemLayoutHelper(
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   435
                 context, accContext, mi, checkIcon, arrowIcon,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   436
                 MenuItemLayoutHelper.createMaxRect(), defaultTextIconGap,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   437
                 acceleratorDelimiter, SynthLookAndFeel.isLeftToRight(mi),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   438
                 useCheckAndArrow, propertyPrefix);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   439
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   440
         Dimension result = new Dimension();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   441
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   442
         // Calculate the result width
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   443
         int gap = lh.getGap();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   444
         result.width = 0;
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   445
         MenuItemLayoutHelper.addMaxWidth(lh.getCheckSize(), gap, result);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   446
         MenuItemLayoutHelper.addMaxWidth(lh.getLabelSize(), gap, result);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   447
         MenuItemLayoutHelper.addWidth(lh.getMaxAccOrArrowWidth(), 5 * gap, result);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   448
         // The last gap is unnecessary
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   449
         result.width -= gap;
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   450
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   451
         // Calculate the result height
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   452
         result.height = MenuItemLayoutHelper.max(lh.getCheckSize().getHeight(),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   453
                 lh.getLabelSize().getHeight(), lh.getAccSize().getHeight(),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   454
                 lh.getArrowSize().getHeight());
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   455
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   456
         // Take into account menu item insets
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   457
         Insets insets = lh.getMenuItem().getInsets();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   458
         if (insets != null) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   459
             result.width += insets.left + insets.right;
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   460
             result.height += insets.top + insets.bottom;
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   461
         }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   462
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   463
         // if the width is even, bump it up one. This is critical
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   464
         // for the focus dash lhne to draw properly
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   465
         if (result.width % 2 == 0) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   466
             result.width++;
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   467
         }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   468
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   469
         // if the height is even, bump it up one. This is critical
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   470
         // for the text to center properly
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   471
         if (result.height % 2 == 0) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   472
             result.height++;
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   473
         }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   474
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   475
         return result;
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   476
     }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   477
3981
a219cd45863d 6857888: closed/javax/swing/JMenuItem/6458123/bug6458123.java fails with InvocationTargetException.
peterz
parents: 1295
diff changeset
   478
    static void applyInsets(Rectangle rect, Insets insets, boolean leftToRight) {
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   479
        if (insets != null) {
3981
a219cd45863d 6857888: closed/javax/swing/JMenuItem/6458123/bug6458123.java fails with InvocationTargetException.
peterz
parents: 1295
diff changeset
   480
            rect.x += (leftToRight ? insets.left : insets.right);
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   481
            rect.y += insets.top;
3981
a219cd45863d 6857888: closed/javax/swing/JMenuItem/6458123/bug6458123.java fails with InvocationTargetException.
peterz
parents: 1295
diff changeset
   482
            rect.width -= (leftToRight ? insets.right : insets.left) + rect.x;
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   483
            rect.height -= (insets.bottom + rect.y);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   484
        }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   485
    }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   486
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   487
    static void paint(SynthContext context, SynthContext accContext, Graphics g,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   488
               Icon checkIcon, Icon arrowIcon, String acceleratorDelimiter,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   489
               int defaultTextIconGap, String propertyPrefix) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   490
        JMenuItem mi = (JMenuItem) context.getComponent();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   491
        SynthStyle style = context.getStyle();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   492
        g.setFont(style.getFont(context));
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   493
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   494
        Rectangle viewRect = new Rectangle(0, 0, mi.getWidth(), mi.getHeight());
3981
a219cd45863d 6857888: closed/javax/swing/JMenuItem/6458123/bug6458123.java fails with InvocationTargetException.
peterz
parents: 1295
diff changeset
   495
        boolean leftToRight = SynthLookAndFeel.isLeftToRight(mi);
a219cd45863d 6857888: closed/javax/swing/JMenuItem/6458123/bug6458123.java fails with InvocationTargetException.
peterz
parents: 1295
diff changeset
   496
        applyInsets(viewRect, mi.getInsets(), leftToRight);
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   497
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   498
        SynthMenuItemLayoutHelper lh = new SynthMenuItemLayoutHelper(
3981
a219cd45863d 6857888: closed/javax/swing/JMenuItem/6458123/bug6458123.java fails with InvocationTargetException.
peterz
parents: 1295
diff changeset
   499
                context, accContext, mi, checkIcon, arrowIcon, viewRect,
a219cd45863d 6857888: closed/javax/swing/JMenuItem/6458123/bug6458123.java fails with InvocationTargetException.
peterz
parents: 1295
diff changeset
   500
                defaultTextIconGap, acceleratorDelimiter, leftToRight,
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   501
                MenuItemLayoutHelper.useCheckAndArrow(mi), propertyPrefix);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   502
        MenuItemLayoutHelper.LayoutResult lr = lh.layoutMenuItem();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   503
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   504
        paintMenuItem(g, lh, lr);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   505
    }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   506
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   507
    static void paintMenuItem(Graphics g, SynthMenuItemLayoutHelper lh,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   508
                              MenuItemLayoutHelper.LayoutResult lr) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   509
        // Save original graphics font and color
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   510
        Font holdf = g.getFont();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   511
        Color holdc = g.getColor();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   512
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   513
        paintCheckIcon(g, lh, lr);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   514
        paintIcon(g, lh, lr);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   515
        paintText(g, lh, lr);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   516
        paintAccText(g, lh, lr);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   517
        paintArrowIcon(g, lh, lr);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   518
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   519
        // Restore original graphics font and color
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   520
        g.setColor(holdc);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   521
        g.setFont(holdf);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   522
    }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   523
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   524
    static void paintBackground(Graphics g, SynthMenuItemLayoutHelper lh) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   525
        paintBackground(lh.getContext(), g, lh.getMenuItem());
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   526
    }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   527
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   528
    static void paintBackground(SynthContext context, Graphics g, JComponent c) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   529
        context.getPainter().paintMenuItemBackground(context, g, 0, 0,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   530
                c.getWidth(), c.getHeight());
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   531
    }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   532
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   533
    static void paintIcon(Graphics g, SynthMenuItemLayoutHelper lh,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   534
                          MenuItemLayoutHelper.LayoutResult lr) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   535
        if (lh.getIcon() != null) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   536
            Icon icon;
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   537
            JMenuItem mi = lh.getMenuItem();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   538
            ButtonModel model = mi.getModel();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   539
            if (!model.isEnabled()) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   540
                icon = mi.getDisabledIcon();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   541
            } else if (model.isPressed() && model.isArmed()) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   542
                icon = mi.getPressedIcon();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   543
                if (icon == null) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   544
                    // Use default icon
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   545
                    icon = mi.getIcon();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   546
                }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   547
            } else {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   548
                icon = mi.getIcon();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   549
            }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   550
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   551
            if (icon != null) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   552
                Rectangle iconRect = lr.getIconRect();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   553
                SynthIcon.paintIcon(icon, lh.getContext(), g, iconRect.x,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   554
                        iconRect.y, iconRect.width, iconRect.height);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   555
            }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   556
        }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   557
    }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   558
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   559
    static void paintCheckIcon(Graphics g, SynthMenuItemLayoutHelper lh,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   560
                               MenuItemLayoutHelper.LayoutResult lr) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   561
        if (lh.getCheckIcon() != null) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   562
            Rectangle checkRect = lr.getCheckRect();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   563
            SynthIcon.paintIcon(lh.getCheckIcon(), lh.getContext(), g,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   564
                    checkRect.x, checkRect.y, checkRect.width, checkRect.height);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   565
        }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   566
    }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   567
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   568
    static void paintAccText(Graphics g, SynthMenuItemLayoutHelper lh,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   569
                             MenuItemLayoutHelper.LayoutResult lr) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   570
        String accText = lh.getAccText();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   571
        if (accText != null && !accText.equals("")) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   572
            g.setColor(lh.getAccStyle().getColor(lh.getAccContext(),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   573
                    ColorType.TEXT_FOREGROUND));
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   574
            g.setFont(lh.getAccStyle().getFont(lh.getAccContext()));
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   575
            lh.getAccGraphicsUtils().paintText(lh.getAccContext(), g, accText,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   576
                    lr.getAccRect().x, lr.getAccRect().y, -1);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   577
        }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   578
    }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   579
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   580
    static void paintText(Graphics g, SynthMenuItemLayoutHelper lh,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   581
                          MenuItemLayoutHelper.LayoutResult lr) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   582
        if (!lh.getText().equals("")) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   583
            if (lh.getHtmlView() != null) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   584
                // Text is HTML
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   585
                lh.getHtmlView().paint(g, lr.getTextRect());
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   586
            } else {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   587
                // Text isn't HTML
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   588
                g.setColor(lh.getStyle().getColor(
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   589
                        lh.getContext(), ColorType.TEXT_FOREGROUND));
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   590
                g.setFont(lh.getStyle().getFont(lh.getContext()));
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   591
                lh.getGraphicsUtils().paintText(lh.getContext(), g, lh.getText(),
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   592
                        lr.getTextRect().x, lr.getTextRect().y,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   593
                        lh.getMenuItem().getDisplayedMnemonicIndex());
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   594
            }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   595
        }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   596
    }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   597
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   598
    static void paintArrowIcon(Graphics g, SynthMenuItemLayoutHelper lh,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   599
                               MenuItemLayoutHelper.LayoutResult lr) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   600
        if (lh.getArrowIcon() != null) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   601
            Rectangle arrowRect = lr.getArrowRect();
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   602
            SynthIcon.paintIcon(lh.getArrowIcon(), lh.getContext(), g,
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   603
                    arrowRect.x, arrowRect.y, arrowRect.width, arrowRect.height);
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   604
        }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   605
    }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 1290
diff changeset
   606
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Wraps a SynthIcon around the Icon interface, forwarding calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * the SynthIcon with a given SynthContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    private static class SynthIconWrapper implements Icon {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   612
        private static final java.util.List<SynthIconWrapper> CACHE = new java.util.ArrayList<SynthIconWrapper>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        private SynthIcon synthIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        private SynthContext context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        static SynthIconWrapper get(SynthIcon icon, SynthContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            synchronized(CACHE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                int size = CACHE.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                if (size > 0) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   621
                    SynthIconWrapper wrapper = CACHE.remove(size - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    wrapper.reset(icon, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    return wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            return new SynthIconWrapper(icon, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        static void release(SynthIconWrapper wrapper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            wrapper.reset(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            synchronized(CACHE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                CACHE.add(wrapper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        SynthIconWrapper(SynthIcon icon, SynthContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            reset(icon, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        void reset(SynthIcon icon, SynthContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            synthIcon = icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        public void paintIcon(Component c, Graphics g, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            // This is a noop as this should only be for sizing calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        public int getIconWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            return synthIcon.getIconWidth(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        public int getIconHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            return synthIcon.getIconHeight(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
}