5.13 Display Depth and DPI
SWT runs on many different display devices. These devices can be different sizes and support different numbers of colors. Application programmers can query the display in order to use the best possible colors for the device.
The bit depth of a graphics device is the number of bits it takes to represent each color on the device. It is also called bits per pixel. The bit depth of a device is usually one of 1, 8, 15, 16, 24, or 32. Application programmers can query the depth of a device using getDepth().
The physical size of a device is represented in dots per inch (DPI). The method getDPI() returns the number of dots, or pixels, per inch in both the horizontal and vertical direction. This can be used, for example, to allow the application to draw graphics the same size, regardless of the device.
Both getDepth() and getDPI() are covered further in the Graphics part of the book.
Some platforms restrict the number of colors that can be used for an icon separately from the number of colors available on the device. You can query the number of colors that are supported for icons using the getIconDepth() method.
getIconDepth()
Returns the maximum number of bits per pixel allowed for icons that are created on this display.
The primary use for this method is to choose the best icon for the display. For example, an application program could use the result of getIconDepth() to select one of several different icons, each representing the same picture but using different numbers of colors.
5.13.1 System Information
Display also provides system information and stock graphics objects or system objects. System objects are resources such as fonts and colors that are allocated by the operating system. They are available for use by every application but owned by the system and released on shutdown.
|
Do not attempt to dispose of system objects by calling dispose(). You did not allocate them, so you must not free them. If you do, depending on the platform and the object, the results will be unpredictable. For example, some operating systems (such as Microsoft Windows) guard against disposing of system objects, whereas others (such as Motif) do not. |
5.13.2 System Colors
On every platform, no matter how many colors are available, the window system reserves a small number of colors. These are nominal enough to display the widgets on the desktop. If more colors are available, the desktop may make use of them, but the minimal set is always present and preallocated. These are called system colors.
In SWT, system colors are specified using constants with the prefix COLOR_. The following code fragment gets the color red from the system.
Color red = display.getSystemColor(SWT.COLOR_RED);
It is also possible to query standard system colors for things such as shadows, the background color of a text widget, and the gradient that should be used when drawing a title. These days, most platforms have theme managers that allow arbitrary code to be used to draw widgets. As long as your application uses the native widgets that SWT provides, it will take on the appearance generated by the theme manager. For custom widgets that you create, using the system colors will help the widget fit in with the appearance of the platform, but it is not sufficient to make them match exactly.
The SWT team is investigating the possibility of providing a "skinning" API that allows nonnative widgets to make direct use of the platform theme manager, but as of the writing of this book, this has not been implemented.
For a complete description of system colors, see the section System Colors in the Colors chapter.
5.13.3 System Font
The system font is the default font used by the window system on platforms that have this concept. The following code fragment queries the system font and sets it into a control.
Font font = display.getSystemFont();
control.setFont(font);
Note that it is best to avoid code like this. When a widget is created, the platform theme manager automatically assigns the appropriate font for the widget. Setting the font for a widget to null, rather than to the system font, restores the correct font for that widget.
The system font is covered further in the Fonts chapter.
|