SystemColor.java 13.9 KB
Newer Older
Tom Tromey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
/* SystemColor.java -- access dynamic system color values
   Copyright (C) 1999, 2002, 2004, 2005  Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.awt;

import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;
import java.io.Serializable;

/**
 * This class contains the various "system colors" in use by the native
 * windowing system. The <code>getRGB()</code> method is dynamic on systems
 * which support dynamic system color changes, and most methods in the
 * superclass are written to use this dynamic value when reporting colors.
 * However, the <code>equals()</code> method is not dynamic, and does not
 * track the actual color of instances in this class. This means that equals
 * may give surprising results; you are better off relying on getRGB.
 *
 * @author Aaron M. Renn (arenn@urbanophile.com)
 * @author Eric Blake (ebb9@email.byu.edu)
 * @since 1.1
 * @status updated to 1.4
 */
public final class SystemColor extends Color implements Serializable
{
  // Implementation note: To be serial compatible with JDK, this class must
  // violate the semantic meaning of super.value to be one of the
  // NUM_COLORS constants instead of the actual RGB value. Hence there are
  // a lot of ugly workarounds in Color and in this class. I would have
  // designed it MUCH differently, making a separate id field in this class.

  /**
   * Compatible with JDK 1.1+.
   */
  private static final long serialVersionUID = 4503142729533789064L;

  /**
   * Array index of the desktop color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #desktop
   */
  public static final int DESKTOP = 0;

  /**
   * Array index of the active caption color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #activeCaption
   */
  public static final int ACTIVE_CAPTION = 1;

  /**
   * Array index of the active caption text color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #activeCaptionText
   */
  public static final int ACTIVE_CAPTION_TEXT = 2;

  /**
   * Array index of the active caption border color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #activeCaptionBorder
   */
  public static final int ACTIVE_CAPTION_BORDER = 3;

  /**
   * Array index of the inactive caption color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #inactiveCaption
   */
  public static final int INACTIVE_CAPTION = 4;

  /**
   * Array index of the inactive caption text color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #inactiveCaptionText
   */
  public static final int INACTIVE_CAPTION_TEXT = 5;

  /**
   * Array index of the inactive caption border color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #inactiveCaptionBorder
   */
  public static final int INACTIVE_CAPTION_BORDER = 6;

  /**
   * Array index of the window background color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #window
   */
  public static final int WINDOW = 7;

  /**
   * Array index of the window border color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #windowBorder
   */
  public static final int WINDOW_BORDER = 8;

  /**
   * Array index of the window text color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #windowText
   */
  public static final int WINDOW_TEXT = 9;

  /**
   * Array index of the menu background color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #menu
   */
  public static final int MENU = 10;

  /**
   * Array index of the menu text color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #menuText
   */
  public static final int MENU_TEXT = 11;

  /**
   * Array index of the text background color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #text
   */
  public static final int TEXT = 12;

  /**
   * Array index of the text foreground color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #textText
  */
  public static final int TEXT_TEXT = 13;

  /**
   * Array index of the highlighted text background color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #textHighlight
   */
  public static final int TEXT_HIGHLIGHT = 14;

  /**
   * Array index of the highlighted text foreground color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #textHighlightText
   */
  public static final int TEXT_HIGHLIGHT_TEXT = 15;

  /**
   * Array index of the inactive text foreground color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #textInactiveText
   */
  public static final int TEXT_INACTIVE_TEXT = 16;

  /**
   * Array index of the control background color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #control
   */
  public static final int CONTROL = 17;

  /**
   * Array index of the control text color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #controlText
   */
  public static final int CONTROL_TEXT = 18;

  /**
   * Array index of the highlighted control background color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #controlHighlight
   */
  public static final int CONTROL_HIGHLIGHT = 19;

  /**
   * Array index of the lightly highlighted control background color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #controlLtHighlight
   */
  public static final int CONTROL_LT_HIGHLIGHT = 20;

  /**
   * Array index of the shadowed control background color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #controlShadow
   */
  public static final int CONTROL_SHADOW = 21;

  /**
   * Array index of the darkly shadowed control background color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #controlDkShadow
   */
  public static final int CONTROL_DK_SHADOW = 22;

  /**
   * Array index of the scrollbar background color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #scrollbar
   */
  public static final int SCROLLBAR = 23;

  /**
   * Array index of the info background color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #info
   */
  public static final int INFO = 24;

  /**
   * Array index of the info text color.  Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   *
   * @see #infoText
   */
  public static final int INFO_TEXT = 25;

  /**
   * The number of system colors. Used by
   * {@link Toolkit#loadSystemColors(int[])}.
   */
  public static final int NUM_COLORS = 26;

  /**
   * The internal array used to dynamically update <code>getRGB()</code>.
   */
  private static final int[] colors = new int[NUM_COLORS];

  /** The desktop color. */
  public static final SystemColor desktop
    = new SystemColor(DESKTOP);

  /** The active caption background color. */
  public static final SystemColor activeCaption
    = new SystemColor(ACTIVE_CAPTION);

  /** The active caption text color. */
  public static final SystemColor activeCaptionText
    = new SystemColor(ACTIVE_CAPTION_TEXT);

  /** The active caption border color. */
  public static final SystemColor activeCaptionBorder
    = new SystemColor(ACTIVE_CAPTION_BORDER);

  /** The inactive caption background color. */
  public static final SystemColor inactiveCaption
    = new SystemColor(INACTIVE_CAPTION);

  /** The inactive caption text color. */
  public static final SystemColor inactiveCaptionText
    = new SystemColor(INACTIVE_CAPTION_TEXT);

  /** The inactive caption border color. */
  public static final SystemColor inactiveCaptionBorder
    = new SystemColor(INACTIVE_CAPTION_BORDER);

  /** The window background color. */
  public static final SystemColor window
    = new SystemColor(WINDOW);

  /** The window border color. */
  public static final SystemColor windowBorder
    = new SystemColor(WINDOW_BORDER);

  /** The window text color. */
  public static final SystemColor windowText
    = new SystemColor(WINDOW_TEXT);

  /** The menu background color. */
  public static final SystemColor menu
    = new SystemColor(MENU);

  /** The menu text color. */
  public static final SystemColor menuText
    = new SystemColor(MENU_TEXT);

  /** The text background color. */
  public static final SystemColor text
    = new SystemColor(TEXT);

  /** The text foreground color. */
  public static final SystemColor textText
    = new SystemColor(TEXT_TEXT);

  /** The highlighted text background color. */
  public static final SystemColor textHighlight
    = new SystemColor(TEXT_HIGHLIGHT);

  /** The highlighted text foreground color. */
  public static final SystemColor textHighlightText
    = new SystemColor(TEXT_HIGHLIGHT_TEXT);

  /** The inactive text color. */
  public static final SystemColor textInactiveText
    = new SystemColor(TEXT_INACTIVE_TEXT);

  /** The control background color. */
  public static final SystemColor control
    = new SystemColor(CONTROL);

  /** The control text color. */
  public static final SystemColor controlText
    = new SystemColor(CONTROL_TEXT);

  /** The control highlight color. */
  public static final SystemColor controlHighlight
    = new SystemColor(CONTROL_HIGHLIGHT);

  /** The control light highlight color. */
  public static final SystemColor controlLtHighlight
    = new SystemColor(CONTROL_LT_HIGHLIGHT);

  /** The control shadow color. */
  public static final SystemColor controlShadow
    = new SystemColor(CONTROL_SHADOW);

  /** The control dark shadow color. */
  public static final SystemColor controlDkShadow
    = new SystemColor(CONTROL_DK_SHADOW);

  /** The scrollbar color. */
  public static final SystemColor scrollbar
    = new SystemColor(SCROLLBAR);

  /** The info text background color. */
  public static final SystemColor info
    = new SystemColor(INFO);

  /** The info text foreground color. */
  public static final SystemColor infoText
    = new SystemColor(INFO_TEXT);

  /**
   * Construct a system color which is dynamically updated.
   *
   * @param id the color id
   */
  private SystemColor(int id)
  {
    // Note: See Color#Color(int, boolean) to explain why we use this
    // particular constructor.
    super(id, true);
  }

  /**
   * Returns the RGB value for this color, in the sRGB color space. The blue
   * value will be in bits 0-7, green in 8-15, red in 6-23, and the alpha
   * value (bits 24-31) is 0xff. This is dynamically updated, so it may not
   * match the results of <code>getRed()</code>, <code>getGreen()</code>, or
   * <code>getBlue()</code>.
   *
   * @return the current RGB value
   */
  public int getRGB()
  {
    Toolkit.getDefaultToolkit().loadSystemColors(colors);
    return colors[value] | ALPHA_MASK;
  }

  /**
   * Returns a paint context, used for filling areas of a raster scan with
   * the current value of this system color. Since the system colors may be
   * dynamically updated, the returned value may not always be the same; but
   * as the system color is solid, the context does not need any of the
   * passed parameters to do its job.
   *
   * @param cm the requested color model
   * @param deviceBounds the bounding box in device coordinates, ignored
   * @param userBounds the bounding box in user coordinates, ignored
   * @param xform the bounds transformation, ignored
   * @param hints any rendering hints, ignored
   * @return a context for painting this solid color
   */
  public PaintContext createContext(ColorModel cm, Rectangle deviceBounds,
                                    Rectangle2D userBounds,
                                    AffineTransform xform,
                                    RenderingHints hints)
  {
    Toolkit.getDefaultToolkit().loadSystemColors(colors);
    int color = colors[value] | ALPHA_MASK;
    if (context == null || color != context.color || !context.getColorModel().equals(cm))
      context = new ColorPaintContext(cm,color);
    return context;
  }    

  /**
   * Returns a string describing this color. This is in the format
   * "java.awt.SystemColor[i=" + index + ']', where index is one of the
   * integer constants of this class. Unfortunately, this description
   * does not describe the current value of the color; for that you should
   * use <code>new Color(syscolor.getRGB()).toString()</code>.
   *
   * @return a string describing this color
   */
  public String toString()
  {
    return "java.awt.SystemColor[i=" + value + ']';
  }
} // class SystemColor