1. /*
  2. * @(#)WindowsGraphicsUtils.java 1.13 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.windows;
  8. import com.sun.java.swing.SwingUtilities2;
  9. import java.awt.*;
  10. import javax.swing.*;
  11. /**
  12. * A collection of static utility methods used for rendering the Windows look
  13. * and feel.
  14. *
  15. * @version 1.13 12/19/03
  16. * @author Mark Davidson
  17. * @since 1.4
  18. */
  19. public class WindowsGraphicsUtils {
  20. /**
  21. * Renders a text String in Windows without the mnemonic.
  22. * This is here because the WindowsUI hiearchy doesn't match the Component heirarchy. All
  23. * the overriden paintText methods of the ButtonUI delegates will call this static method.
  24. * <p>
  25. * @param g Graphics context
  26. * @param b Current button to render
  27. * @param textRect Bounding rectangle to render the text.
  28. * @param text String to render
  29. */
  30. public static void paintText(Graphics g, AbstractButton b,
  31. Rectangle textRect, String text,
  32. int textShiftOffset) {
  33. ButtonModel model = b.getModel();
  34. FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
  35. int mnemIndex = b.getDisplayedMnemonicIndex();
  36. // W2K Feature: Check to see if the Underscore should be rendered.
  37. if (WindowsLookAndFeel.isMnemonicHidden() == true) {
  38. mnemIndex = -1;
  39. }
  40. /* Draw the Text */
  41. Color color = b.getForeground();
  42. if(model.isEnabled()) {
  43. /*** paint the text normally */
  44. if(!(b instanceof JMenuItem && model.isArmed())
  45. && !(b instanceof JMenu && (model.isSelected() || model.isRollover()))) {
  46. /* We shall not set foreground color for selected menu or
  47. * armed menuitem. Foreground must be set in appropriate
  48. * Windows* class because these colors passes from
  49. * BasicMenuItemUI as protected fields and we can't
  50. * reach them from this class */
  51. g.setColor(b.getForeground());
  52. }
  53. SwingUtilities2.drawStringUnderlineCharAt(b, g,text, mnemIndex,
  54. textRect.x + textShiftOffset,
  55. textRect.y + fm.getAscent() + textShiftOffset);
  56. } else { /*** paint the text disabled ***/
  57. color = UIManager.getColor("Button.disabledForeground");
  58. Color shadow = UIManager.getColor("Button.disabledShadow");
  59. XPStyle xp = XPStyle.getXP();
  60. if (xp != null) {
  61. color = xp.getColor("button.pushbutton(disabled).textcolor", color);
  62. } else {
  63. // Paint shadow only if not XP
  64. if (shadow == null) {
  65. shadow = b.getBackground().darker();
  66. }
  67. g.setColor(shadow);
  68. SwingUtilities2.drawStringUnderlineCharAt(b, g,text,
  69. mnemIndex,
  70. textRect.x,
  71. textRect.y + fm.getAscent());
  72. }
  73. if (color == null) {
  74. color = b.getBackground().brighter();
  75. }
  76. g.setColor(color);
  77. SwingUtilities2.drawStringUnderlineCharAt(b, g,text,
  78. mnemIndex,
  79. textRect.x - 1,
  80. textRect.y + fm.getAscent() - 1);
  81. }
  82. }
  83. static boolean isLeftToRight(Component c) {
  84. return c.getComponentOrientation().isLeftToRight();
  85. }
  86. }