1 /*
   2  * Copyright (c) 2002, 2006, 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 package com.sun.java.swing.plaf.gtk;
  26 
  27 import javax.swing.*;
  28 import javax.swing.plaf.synth.*;
  29 import java.awt.Color;
  30 import java.awt.Graphics;
  31 import java.awt.Rectangle;
  32 
  33 /**
  34  * @author Joshua Outwater
  35  */
  36 class 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             Color orgColor = g.getColor();
  53             g.setColor(context.getStyle().getColor(context,
  54                                                    GTKColorType.WHITE));
  55             x += 1;
  56             y += 1;
  57             super.paintText(context, g, text, x, y, mnemonicIndex);
  58 
  59             g.setColor(orgColor);
  60             x -= 1;
  61             y -= 1;
  62             super.paintText(context, g, text, x, y, mnemonicIndex);
  63         }
  64         else {
  65             String themeName = GTKLookAndFeel.getGtkThemeName();
  66             if (themeName != null && themeName.startsWith("blueprint") &&
  67                 shouldShadowText(context.getRegion(), componentState)) {
  68 
  69                 g.setColor(Color.BLACK);
  70                 super.paintText(context, g, text, x+1, y+1, mnemonicIndex);
  71                 g.setColor(Color.WHITE);
  72             }
  73 
  74             super.paintText(context, g, text, x, y, mnemonicIndex);
  75         }
  76     }
  77 
  78     /**
  79      * Paints text at the specified location. This will not attempt to
  80      * render the text as html nor will it offset by the insets of the
  81      * component.
  82      *
  83      * @param ss SynthContext
  84      * @param g Graphics used to render string in.
  85      * @param text Text to render
  86      * @param bounds Bounds of the text to be drawn.
  87      * @param mnemonicIndex Index to draw string at.
  88      */
  89     public void paintText(SynthContext context, Graphics g, String text,
  90                           Rectangle bounds, int mnemonicIndex) {
  91         if (text == null || text.length() <= 0) {
  92             // We don't need to paint empty strings
  93             return;
  94         }
  95 
  96         Region id = context.getRegion();
  97         if ((id == Region.RADIO_BUTTON ||
  98              id == Region.CHECK_BOX ||
  99              id == Region.TABBED_PANE_TAB) &&
 100             (context.getComponentState() & SynthConstants.FOCUSED) != 0)
 101         {
 102             JComponent source = context.getComponent();
 103             if (!(source instanceof AbstractButton) ||
 104                 ((AbstractButton)source).isFocusPainted()) {
 105 
 106                 // The "bounds" parameter encompasses only the actual text;
 107                 // when drawing the focus, we need to expand that bounding
 108                 // box by "focus-line-width" plus "focus-padding".  Note that
 109                 // the layout process for these components will have already
 110                 // taken these values into account, so there should always
 111                 // be enough space allocated for drawing the focus indicator.
 112                 int synthState = context.getComponentState();
 113                 GTKStyle style = (GTKStyle)context.getStyle();
 114                 int focusSize =
 115                     style.getClassSpecificIntValue(context,
 116                                                    "focus-line-width", 1);
 117                 int focusPad =
 118                     style.getClassSpecificIntValue(context,
 119                                                    "focus-padding", 1);
 120                 int totalFocus = focusSize + focusPad;
 121                 int x = bounds.x - totalFocus;
 122                 int y = bounds.y - totalFocus;
 123                 int w = bounds.width  + (2 * totalFocus);
 124                 int h = bounds.height + (2 * totalFocus);
 125 
 126                 Color color = g.getColor();
 127                 GTKPainter.INSTANCE.paintFocus(context, g, id,
 128                                                synthState, "checkbutton",
 129                                                x, y, w, h);
 130                 g.setColor(color);
 131             }
 132         }
 133         super.paintText(context, g, text, bounds, mnemonicIndex);
 134     }
 135 
 136     private static boolean shouldShadowText(Region id, int state) {
 137         int gtkState = GTKLookAndFeel.synthStateToGTKState(id, state);
 138         return((gtkState == SynthConstants.MOUSE_OVER) &&
 139                (id == Region.MENU ||
 140                 id == Region.MENU_ITEM ||
 141                 id == Region.CHECK_BOX_MENU_ITEM ||
 142                 id == Region.RADIO_BUTTON_MENU_ITEM));
 143     }
 144 }