Example 21-1. The color_logo.c program (continued)
xlogo64_bits, xlogo64_width, xlogo64_height);
/* setup gc for rendering logos to screen */
gcvalues.graphics_exposures = False;
gcvalues.background = pixel_table[WHITE];
gc = XCreateGC(xv_get(frame, XV_DISPLAY), xv_get(frame, XV_XID),
GCBackground | GCGraphicsExposures, &gcvalues);
xv_main_loop(frame);
}
/* Draws onto the canvas using Xlib drawing functions.
* Draw the X logo into the window in three colors. In each case,
* change the GCs foreground color to the pixel value specified.
*/
void
canvas_repaint_proc(canvas, pw, display, win, xrects)
Canvas canvas; /* unused */
Xv_Window pw; /* unused */
Display *display;
Window win;
Xv_xrectlist *xrects; /* unused */
{
/* Use XCopyPlane because the logo is a 1-bit deep pixmap. */
XSetForeground(display, gc, pixel_table[RED]);
XCopyPlane(display, xlogo, win, gc, 0, 0,
xlogo64_width, xlogo64_height, 64, 64, 1);
XSetForeground(display, gc, pixel_table[GREEN]);
XCopyPlane(display, xlogo, win, gc, 0, 0,
xlogo64_width, xlogo64_height, 192, 64, 1);
XSetForeground(display, gc, pixel_table[BLUE]);
XCopyPlane(display, xlogo, win, gc, 0, 0,
xlogo64_width, xlogo64_height, 320, 64, 1);
}
Example 21-1 uses Xlib routines to draw into the canvas’s paint window. Therefore, the GC’s
foreground color is set to an index from the colormap being used by that paint window. In
order to get the correct color from the colormap, we need to get the color table for the win-
dow using the attribute CMS_COLOR_INDEX. The repaint routine draws the X logo in the
specified colors.
Color
Color 523
21.5 The Control Colormap Segment
The management of colormap segments is a little different for control objects (panels,
notices, menus, etc.) than it is for other XView objects. In order for XView to provide the
same control colors for control objects, these objects must use a control colormap segment.
This is just like a normal cms except that parts of it are reserved for the predefined colors.
Since OPEN LOOK suggests that the background of control objects in an application appear in
a consistent color, XView sets that color to be that specified by the resource OpenWin-
dows.WindowColor from the user’s environment. This color is used as the background
color, and along with a few others, they are set aside in the first few indices of the control
colormap segment.
These control colors are used to provide a 3D look for the control objects. Thus, the control
colormap segment must be used for all control objects.
Aside from the added colors, there is little difference between a control cms and a normal
cms. The cms may still request many colors and there is no limit to the choice of colors used
in the new cms. However, these extra colors cannot be used as the background for control
objects.
A control colormap segment is created by setting the boolean attribute CMS_CONTROL_CMS to
TRUE in the call to xv_create(). The macro CMS_CONTROL_COLORS (defined in
<xview/cms.h>) indicates how many predefined control colors there are, so the first
CMS_CONTROL_COLORS indices in the cms are initialized by the XView library. If the appli-
cation requires n other colors in this cms, it must explicitly ask that the segment be created
with a size of:
n + CMS_CONTROL_COLORS
In such a case, the application must refer to its own n colors using the index range:
CMS_CONTROL_COLORS to CMS_CONTROL_COLORS + n-1
An application-defined colormap segment set on a control object (such as a panel) must be a
control colormap segment so that the object can be pointed with the 3D look.
#define WHITE 0
#define RED 1
#define GREEN 2
#define BLUE 3
#define NUM_COLORS 4
control_cms = xv_create(NULL, CMS,
CMS_SIZE, CMS_CONTROL_COLORS + NUM_COLORS,
CMS_CONTROL_CMS, TRUE,
CMS_NAMED_COLORS, "white", "red", "green", "blue", NULL,
NULL);
524 XView Programming Manual
We set the boolean attribute CMS_CONTROL_CMS to TRUE to indicate that we are creating a
control cms. Notice that the size of the colormap segment is the number of colors we speci-
fied plus the number of control colors. This colormap segment contains both control colors
and our colors; XView automatically allocates our colors after the control colors.
When we create the panel, we specify the new colormap segment as the panel’s cms using
the common window attribute, WIN_CMS:
panel = (Panel)xv_create(frame, PANEL,
WIN_CMS, control_cms,
NULL);
When we reference our own specified colors, we must offset those color values by CMS_
CONTROL_COLORS
:
/* assume a 1-bit deep 16x16 square pixmap */
extern Server_image chip;
xv_create(panel, PANEL_CHOICE,
PANEL_LABEL_STRING, "Colors",
PANEL_CHOICE_IMAGES, chip, chip, chip, chip, NULL,
PANEL_CHOICE_COLOR, 0, WHITE + CMS_CONTROL_COLORS,
PANEL_CHOICE_COLOR, 1, RED + CMS_CONTROL_COLORS,
PANEL_CHOICE_COLOR, 2, GREEN + CMS_CONTROL_COLORS,
PANEL_CHOICE_COLOR, 3, BLUE + CMS_CONTROL_COLORS,
NULL);
Here, we create a choice item whose choices are colored “chips” in solid colors correspond-
ing to the color names offset by the control colormap segment.
21.5.1 Coloring Panel Items
You can specify colors for panel items using
PANEL_ITEM_COLOR. This attribute takes as a
value an index into a colormap segment. The value -1 is reserved for the panel’s foreground
color, whatever that may be. It is also the default color of panel items unless you have speci-
fied otherwise with
PANEL_ITEM_COLOR. The scrollbar on a PANEL_LIST will always take
on the window color that is specified in the Workspace Properties sheet.
PANEL_ITEM_COLOR on a PANEL_LIST will only affect the scrolling list’s label, title, and
rows. Remember, if you’re going to be using the 3D interface, then you must create the cms
as a control cms. Example 21-2 briefly demonstrates how to create colored panel items.
Example 21-2. The color_panel.c program
/* color_panel.c --
* This program demonstrates how to set panel items to different
* colors using the XView API for color.
*/
#include <xview/xview.h>
#include <xview/panel.h>
#include <xview/cms.h>
/* Color indices */
#define WHITE 0
#define RED 1
Color
Color 525
Example 21-2. The color_panel.c program (continued)
#define GREEN 2
#define BLUE 3
#define NUM_COLORS 4
/* Create a frame, panel, and a colormap segment and assign the
* cms to the panel.
*/
main(argc,argv)
int argc;
char *argv[ ];
{
Frame frame;
Panel panel;
Cms cms;
extern void exit(), pressed();
static Xv_singlecolor colors[ ] = {
{ 255, 255, 255 }, /* white */
{ 255, 0, 0 }, /* red */
{ 0, 255, 0 }, /* green */
{ 0, 0, 255 }, /* blue */
};
xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);
cms = (Cms) xv_create(NULL, CMS,
CMS_CONTROL_CMS, TRUE,
CMS_SIZE, CMS_CONTROL_COLORS + 4,
CMS_COLORS, colors,
NULL);
frame = (Frame)xv_create(XV_NULL, FRAME,
FRAME_LABEL, argv[0],
FRAME_SHOW_FOOTER, TRUE,
NULL);
panel = xv_create(frame, PANEL,
WIN_CMS, cms,
NULL);
xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Red",
PANEL_ITEM_COLOR, CMS_CONTROL_COLORS + RED,
PANEL_NOTIFY_PROC, pressed,
NULL);
xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Green",
PANEL_ITEM_COLOR, CMS_CONTROL_COLORS + GREEN,
PANEL_NOTIFY_PROC, pressed,
NULL);
xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Blue",
PANEL_ITEM_COLOR, CMS_CONTROL_COLORS + BLUE,
PANEL_NOTIFY_PROC, pressed,
NULL);
xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Quit",
526 XView Programming Manual
Example 21-2. The color_panel.c program (continued)
PANEL_ITEM_COLOR, CMS_CONTROL_COLORS + WHITE,
PANEL_NOTIFY_PROC, exit,
NULL);
window_fit(panel);
window_fit(frame);
xv_main_loop(frame);
}
void
pressed(item, event)
Panel_item item;
Event *event;
{
char *name = (char *)xv_get(item, PANEL_LABEL_STRING);
Frame frame = xv_get(xv_get(item, PANEL_PARENT_PANEL), XV_OWNER);
xv_set(frame, FRAME_LEFT_FOOTER, name, NULL);
}
Notice how the color index for each panel item is offset by the number of colors in the con-
trol color item. If all the references to the CMS_CONTROL_COLORS and CMS_CONTROL_CMS
attributes were removed, the result would be a 2D panel whose panel items are the same
colors as their names.
21.6 Using xv_find() with Colormap Segments
xv_find() can be used to find a previously created colormap segment. Currently, the attri-
bute
CMS_NAME is the only attribute used by xv_find() to find a match.
cms = (Cms)xv_find(screen, CMS,
CMS_NAME, "palette",
XV_AUTO_CREATE, FALSE,
NULL);
This example returns a handle to a colormap segment whose name is “palette.”
XV_AUTO_CREATE is set to FALSE to prevent a new colormap segment from being created. If
the colormap segment with that name is not found, then NULL is returned.
Color
Color 527

Get Volume 7A: XView Programming Manual now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.