001/* SystemColor.java -- access dynamic system color values
002   Copyright (C) 1999, 2002, 2004, 2005  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.awt;
040
041import java.awt.geom.AffineTransform;
042import java.awt.geom.Rectangle2D;
043import java.awt.image.ColorModel;
044import java.io.Serializable;
045
046/**
047 * This class contains the various "system colors" in use by the native
048 * windowing system. The <code>getRGB()</code> method is dynamic on systems
049 * which support dynamic system color changes, and most methods in the
050 * superclass are written to use this dynamic value when reporting colors.
051 * However, the <code>equals()</code> method is not dynamic, and does not
052 * track the actual color of instances in this class. This means that equals
053 * may give surprising results; you are better off relying on getRGB.
054 *
055 * @author Aaron M. Renn (arenn@urbanophile.com)
056 * @author Eric Blake (ebb9@email.byu.edu)
057 * @since 1.1
058 * @status updated to 1.4
059 */
060public final class SystemColor extends Color implements Serializable
061{
062  // Implementation note: To be serial compatible with JDK, this class must
063  // violate the semantic meaning of super.value to be one of the
064  // NUM_COLORS constants instead of the actual RGB value. Hence there are
065  // a lot of ugly workarounds in Color and in this class. I would have
066  // designed it MUCH differently, making a separate id field in this class.
067
068  /**
069   * Compatible with JDK 1.1+.
070   */
071  private static final long serialVersionUID = 4503142729533789064L;
072
073  /**
074   * Array index of the desktop color.  Used by
075   * {@link Toolkit#loadSystemColors(int[])}.
076   *
077   * @see #desktop
078   */
079  public static final int DESKTOP = 0;
080
081  /**
082   * Array index of the active caption color.  Used by
083   * {@link Toolkit#loadSystemColors(int[])}.
084   *
085   * @see #activeCaption
086   */
087  public static final int ACTIVE_CAPTION = 1;
088
089  /**
090   * Array index of the active caption text color.  Used by
091   * {@link Toolkit#loadSystemColors(int[])}.
092   *
093   * @see #activeCaptionText
094   */
095  public static final int ACTIVE_CAPTION_TEXT = 2;
096
097  /**
098   * Array index of the active caption border color.  Used by
099   * {@link Toolkit#loadSystemColors(int[])}.
100   *
101   * @see #activeCaptionBorder
102   */
103  public static final int ACTIVE_CAPTION_BORDER = 3;
104
105  /**
106   * Array index of the inactive caption color.  Used by
107   * {@link Toolkit#loadSystemColors(int[])}.
108   *
109   * @see #inactiveCaption
110   */
111  public static final int INACTIVE_CAPTION = 4;
112
113  /**
114   * Array index of the inactive caption text color.  Used by
115   * {@link Toolkit#loadSystemColors(int[])}.
116   *
117   * @see #inactiveCaptionText
118   */
119  public static final int INACTIVE_CAPTION_TEXT = 5;
120
121  /**
122   * Array index of the inactive caption border color.  Used by
123   * {@link Toolkit#loadSystemColors(int[])}.
124   *
125   * @see #inactiveCaptionBorder
126   */
127  public static final int INACTIVE_CAPTION_BORDER = 6;
128
129  /**
130   * Array index of the window background color.  Used by
131   * {@link Toolkit#loadSystemColors(int[])}.
132   *
133   * @see #window
134   */
135  public static final int WINDOW = 7;
136
137  /**
138   * Array index of the window border color.  Used by
139   * {@link Toolkit#loadSystemColors(int[])}.
140   *
141   * @see #windowBorder
142   */
143  public static final int WINDOW_BORDER = 8;
144
145  /**
146   * Array index of the window text color.  Used by
147   * {@link Toolkit#loadSystemColors(int[])}.
148   *
149   * @see #windowText
150   */
151  public static final int WINDOW_TEXT = 9;
152
153  /**
154   * Array index of the menu background color.  Used by
155   * {@link Toolkit#loadSystemColors(int[])}.
156   *
157   * @see #menu
158   */
159  public static final int MENU = 10;
160
161  /**
162   * Array index of the menu text color.  Used by
163   * {@link Toolkit#loadSystemColors(int[])}.
164   *
165   * @see #menuText
166   */
167  public static final int MENU_TEXT = 11;
168
169  /**
170   * Array index of the text background color.  Used by
171   * {@link Toolkit#loadSystemColors(int[])}.
172   *
173   * @see #text
174   */
175  public static final int TEXT = 12;
176
177  /**
178   * Array index of the text foreground color.  Used by
179   * {@link Toolkit#loadSystemColors(int[])}.
180   *
181   * @see #textText
182  */
183  public static final int TEXT_TEXT = 13;
184
185  /**
186   * Array index of the highlighted text background color.  Used by
187   * {@link Toolkit#loadSystemColors(int[])}.
188   *
189   * @see #textHighlight
190   */
191  public static final int TEXT_HIGHLIGHT = 14;
192
193  /**
194   * Array index of the highlighted text foreground color.  Used by
195   * {@link Toolkit#loadSystemColors(int[])}.
196   *
197   * @see #textHighlightText
198   */
199  public static final int TEXT_HIGHLIGHT_TEXT = 15;
200
201  /**
202   * Array index of the inactive text foreground color.  Used by
203   * {@link Toolkit#loadSystemColors(int[])}.
204   *
205   * @see #textInactiveText
206   */
207  public static final int TEXT_INACTIVE_TEXT = 16;
208
209  /**
210   * Array index of the control background color.  Used by
211   * {@link Toolkit#loadSystemColors(int[])}.
212   *
213   * @see #control
214   */
215  public static final int CONTROL = 17;
216
217  /**
218   * Array index of the control text color.  Used by
219   * {@link Toolkit#loadSystemColors(int[])}.
220   *
221   * @see #controlText
222   */
223  public static final int CONTROL_TEXT = 18;
224
225  /**
226   * Array index of the highlighted control background color.  Used by
227   * {@link Toolkit#loadSystemColors(int[])}.
228   *
229   * @see #controlHighlight
230   */
231  public static final int CONTROL_HIGHLIGHT = 19;
232
233  /**
234   * Array index of the lightly highlighted control background color.  Used by
235   * {@link Toolkit#loadSystemColors(int[])}.
236   *
237   * @see #controlLtHighlight
238   */
239  public static final int CONTROL_LT_HIGHLIGHT = 20;
240
241  /**
242   * Array index of the shadowed control background color.  Used by
243   * {@link Toolkit#loadSystemColors(int[])}.
244   *
245   * @see #controlShadow
246   */
247  public static final int CONTROL_SHADOW = 21;
248
249  /**
250   * Array index of the darkly shadowed control background color.  Used by
251   * {@link Toolkit#loadSystemColors(int[])}.
252   *
253   * @see #controlDkShadow
254   */
255  public static final int CONTROL_DK_SHADOW = 22;
256
257  /**
258   * Array index of the scrollbar background color.  Used by
259   * {@link Toolkit#loadSystemColors(int[])}.
260   *
261   * @see #scrollbar
262   */
263  public static final int SCROLLBAR = 23;
264
265  /**
266   * Array index of the info background color.  Used by
267   * {@link Toolkit#loadSystemColors(int[])}.
268   *
269   * @see #info
270   */
271  public static final int INFO = 24;
272
273  /**
274   * Array index of the info text color.  Used by
275   * {@link Toolkit#loadSystemColors(int[])}.
276   *
277   * @see #infoText
278   */
279  public static final int INFO_TEXT = 25;
280
281  /**
282   * The number of system colors. Used by
283   * {@link Toolkit#loadSystemColors(int[])}.
284   */
285  public static final int NUM_COLORS = 26;
286
287  /**
288   * The internal array used to dynamically update <code>getRGB()</code>.
289   */
290  private static final int[] colors = new int[NUM_COLORS];
291
292  /** The desktop color. */
293  public static final SystemColor desktop
294    = new SystemColor(DESKTOP);
295
296  /** The active caption background color. */
297  public static final SystemColor activeCaption
298    = new SystemColor(ACTIVE_CAPTION);
299
300  /** The active caption text color. */
301  public static final SystemColor activeCaptionText
302    = new SystemColor(ACTIVE_CAPTION_TEXT);
303
304  /** The active caption border color. */
305  public static final SystemColor activeCaptionBorder
306    = new SystemColor(ACTIVE_CAPTION_BORDER);
307
308  /** The inactive caption background color. */
309  public static final SystemColor inactiveCaption
310    = new SystemColor(INACTIVE_CAPTION);
311
312  /** The inactive caption text color. */
313  public static final SystemColor inactiveCaptionText
314    = new SystemColor(INACTIVE_CAPTION_TEXT);
315
316  /** The inactive caption border color. */
317  public static final SystemColor inactiveCaptionBorder
318    = new SystemColor(INACTIVE_CAPTION_BORDER);
319
320  /** The window background color. */
321  public static final SystemColor window
322    = new SystemColor(WINDOW);
323
324  /** The window border color. */
325  public static final SystemColor windowBorder
326    = new SystemColor(WINDOW_BORDER);
327
328  /** The window text color. */
329  public static final SystemColor windowText
330    = new SystemColor(WINDOW_TEXT);
331
332  /** The menu background color. */
333  public static final SystemColor menu
334    = new SystemColor(MENU);
335
336  /** The menu text color. */
337  public static final SystemColor menuText
338    = new SystemColor(MENU_TEXT);
339
340  /** The text background color. */
341  public static final SystemColor text
342    = new SystemColor(TEXT);
343
344  /** The text foreground color. */
345  public static final SystemColor textText
346    = new SystemColor(TEXT_TEXT);
347
348  /** The highlighted text background color. */
349  public static final SystemColor textHighlight
350    = new SystemColor(TEXT_HIGHLIGHT);
351
352  /** The highlighted text foreground color. */
353  public static final SystemColor textHighlightText
354    = new SystemColor(TEXT_HIGHLIGHT_TEXT);
355
356  /** The inactive text color. */
357  public static final SystemColor textInactiveText
358    = new SystemColor(TEXT_INACTIVE_TEXT);
359
360  /** The control background color. */
361  public static final SystemColor control
362    = new SystemColor(CONTROL);
363
364  /** The control text color. */
365  public static final SystemColor controlText
366    = new SystemColor(CONTROL_TEXT);
367
368  /** The control highlight color. */
369  public static final SystemColor controlHighlight
370    = new SystemColor(CONTROL_HIGHLIGHT);
371
372  /** The control light highlight color. */
373  public static final SystemColor controlLtHighlight
374    = new SystemColor(CONTROL_LT_HIGHLIGHT);
375
376  /** The control shadow color. */
377  public static final SystemColor controlShadow
378    = new SystemColor(CONTROL_SHADOW);
379
380  /** The control dark shadow color. */
381  public static final SystemColor controlDkShadow
382    = new SystemColor(CONTROL_DK_SHADOW);
383
384  /** The scrollbar color. */
385  public static final SystemColor scrollbar
386    = new SystemColor(SCROLLBAR);
387
388  /** The info text background color. */
389  public static final SystemColor info
390    = new SystemColor(INFO);
391
392  /** The info text foreground color. */
393  public static final SystemColor infoText
394    = new SystemColor(INFO_TEXT);
395
396  /**
397   * Construct a system color which is dynamically updated.
398   *
399   * @param id the color id
400   */
401  private SystemColor(int id)
402  {
403    // Note: See Color#Color(int, boolean) to explain why we use this
404    // particular constructor.
405    super(id, true);
406  }
407
408  /**
409   * Returns the RGB value for this color, in the sRGB color space. The blue
410   * value will be in bits 0-7, green in 8-15, red in 6-23, and the alpha
411   * value (bits 24-31) is 0xff. This is dynamically updated, so it may not
412   * match the results of <code>getRed()</code>, <code>getGreen()</code>, or
413   * <code>getBlue()</code>.
414   *
415   * @return the current RGB value
416   */
417  public int getRGB()
418  {
419    Toolkit.getDefaultToolkit().loadSystemColors(colors);
420    return colors[value] | ALPHA_MASK;
421  }
422
423  /**
424   * Returns a paint context, used for filling areas of a raster scan with
425   * the current value of this system color. Since the system colors may be
426   * dynamically updated, the returned value may not always be the same; but
427   * as the system color is solid, the context does not need any of the
428   * passed parameters to do its job.
429   *
430   * @param cm the requested color model
431   * @param deviceBounds the bounding box in device coordinates, ignored
432   * @param userBounds the bounding box in user coordinates, ignored
433   * @param xform the bounds transformation, ignored
434   * @param hints any rendering hints, ignored
435   * @return a context for painting this solid color
436   */
437  public PaintContext createContext(ColorModel cm, Rectangle deviceBounds,
438                                    Rectangle2D userBounds,
439                                    AffineTransform xform,
440                                    RenderingHints hints)
441  {
442    Toolkit.getDefaultToolkit().loadSystemColors(colors);
443    int color = colors[value] | ALPHA_MASK;
444    if (context == null || color != context.color || !context.getColorModel().equals(cm))
445      context = new ColorPaintContext(cm,color);
446    return context;
447  }    
448
449  /**
450   * Returns a string describing this color. This is in the format
451   * "java.awt.SystemColor[i=" + index + ']', where index is one of the
452   * integer constants of this class. Unfortunately, this description
453   * does not describe the current value of the color; for that you should
454   * use <code>new Color(syscolor.getRGB()).toString()</code>.
455   *
456   * @return a string describing this color
457   */
458  public String toString()
459  {
460    return "java.awt.SystemColor[i=" + value + ']';
461  }
462} // class SystemColor