Professional Documents
Culture Documents
Drawing Graphics
After'ybu create the windows and set up the event handling code, to com-
plete your application, you need to arrange to display text and graphics
in the windows. Although an X display is an array' (or raster) of pixels,
you do not always have to or one pixel at a time. The X1ib library in-
dud ma.tl)'-drawingfunctions; those functions can be grouped into"three
road categories:
.•. I:unctions to draw graphical ob"ects such as lines, rectangles, and
cirdes
.•. Functions to draw text
.•. Functions to display and manipulate images
raphics Contexts
,L / From th~ moment you be~in displaying output in.a window, the graphics context (~C) bec~mes the
::::/ focal POInt of your attention. A C must be s£eClfied as an argument for each Xllb function that
draws in a window. Through the GC ou s eci attributes such as coler, font, atterns and ine
sty es. Wit ot er X resources, you have to create a GC, initialize it, and then use it. Before gett' g
into those topics, consider the process of drawing in X and see where the GC fits into the scheme.
E~I/H/HLH·H~
I
being organized into a number or bit planes, in which each bit plane has a depth of one (see
Figure 9.1). .
NOTE:
. The conce t of GC was introduced in XII. In'XI 0, dra~ing functions proVided all gniphiJ
attributes (such as line width and oreground pixel) in each call. This was inefficient
because the parameters had to be sent to the server for each drawing request, even if you
never ~ the attributes. XII takes the more efficient M'proach of setting up a GC on<;¢
and referring to it by a single resource ID in each drawing request: '
A pointer ro rhe Dis lay
An unsigned -
The 10 of a drawable (a Windowor a Pixmap variable)
long bir mask
GC Attribute Specification
Instead of accepting the default values, you can create a GCwirh some or all of its attributes set to
he address of an XGCValues structure
values of your choice. Yo specifYrhe values of a GC'sattributes rhrough.an XGCValues structure,
You specifYvarious ra2hics attribut~s in the XGCValues s~ructure and use the bit mask ro indicate - 4
which members of the structure have valid"values. The sy'~ax of calling XCreateGC is as follows: which is defined in <X!.:!/~~as follo~ V\~\J /\111 • JA
typedef struct {\ rJ~
Display *p_disp;
Drawable win_or_pix; 1* Operation on pixels *I,t\~
unsigned long mask; 1* Bit planes affected *I ~
XGCValues xgcv; 1* Foreground pixel value *1
GC new_GC; 1* Background pixel value *1
~ 1* Line width (0 or more) *1 tJc,
1* One of: LineSolid,
int cap_style;
LineOnOffDash,
LineDoubleDash
1* One of: CapNotLast,
j
CapButt, CapRound,
CapProjecting
NOTE:
int join_style; 1* One of: JoinMiter,
JoinRound, JoinBevel *1
It is common practice in Xlib to use a structure ~irh a bif mask to indicate the valid fields
int fill_style; 1* One of: FillSolid,
in rhat structure. - FillTiled, FillStippled,
FillOpaqueStippled *1
int fill _rule; 1* One of: EvenOddRule,
WindingRule
If you want ro create a GCwith the default settings (which is described soon), call XCreateGC with
int arc_mode; 1* One of: ArcChord,
a 0 value for the bit mask and the pointer to rhe XGCValues structure: ArcPieSlice *I
~
D~splay *p_disp;
Pixmap tile; 1* Pixmap for tilinQ *1
Window Pixmap stipple; 1* Bitmap for stippling *1
my_window; x and y offset for tile*1
GC int ts_x_origin; 1*
mLGC; 1* or stipple operations *1
int ts_y_origin;
ont font; 1* Default font *I
my_GC XCreateGC(p_disp, my_window, 0, NULL);
int su w~naow_mode; 1* One of: ClipByChildren,
Includelnferiors *1
Once a GCis created rhis way:,lOU can alter specific attributes of 1;.h by usi!l.gthe Xlib function
Bool graphics_exposures; 1* True=generate exposures*1
~------------
XChangeGCor by using convenience functions such as XSetForeground and XSetBackground. int
int
Pixmap
clip_x_origin;
clip_y_origin;
clip_mask;
1* Origin of clip_mask
*1
One GC for Many Drawables int dash_offset; 1* Controls dashed line *1
char dashes; 1* Pattern of dashes *1
Although you specifYthe 10 of a window or a pixmap (a drawable) when creating a GC,rhere is no XGCValues;
requirement that the GCbe used with that specific drawable. What matters is the number of bit The members of rhe XGCValues structure include all graphics attributes necessary ro control rhe
planes in the drawable-its deprh. Thus,.you can use ~ single GCwirh any window or ixma as appearance of text and graphics output in X. Table 9.1 shows rhe default values of rhe members of
long as they borh have the same de rh and are on the same screen. rhe XGCValues structure. The table also lists rhe value of rhe bit mask rhat identifies each member
.t is commo~ ractice to create as many GCsas re uired by rhe distinct drawing styles (determined of rhe XGeValues structure .
by sets of graphi~·attributes) used in an ap licarion. For example, i your application requires sev-
--
Part Three: Drawing in an X Window
GXxor newXORold
GXor new OR old After this is done, if a pixel .value of 0110 ;s to be copied to the raster, only bit 1 of each affected
GXnor NOT (new OR old) pixel will be set to 1.
GXequiv (NOT new) XOR old If you want a graphics operation to affect all bit planes, you can set plane_mask to the constant
GXinvert NOT old AllPlanes, which is a macro that refers to all the bit planes. The default value of the plane mask is
GXorReverse new OR (NOT old)' AllPlanes.
GXcopyInverted NOT new
GXorInverted (NOT new) OR old Foreground and Background Pixels
GXnand NOT (new AND old) The foreground and background ra hics attributes' specifYthe foreground and background col-
GXset 1 ors use or graphics operations. You can use the functions XSetForeground and XSetBackground
to set these attributes. Each attribute is a pixel value, so the colormap associated with the 'window
determines the actual colors displayed. The default values for foreground and background de-
pend on the X server. If you want black and white drawings, you can use the macros BlackPixel
'he default value of function is GXcoPY,which means that the new pixel values overwrite the old
and WhitePixel (they take the display and screen as arguments) as pixel values that map to black
les. Another important display function is GXxor which performs an exclusive-OR (XOR)of the
and white colors on the screen. '
d pixel value with the new. This function is useful in erasing drawings because of the following
:operty of the XOR operation:
hich says that if you exclusive-OR a pixel value x with y you can get back x by repeating the exclu- The background pixel specified in a GCis used differently from the background attribute of
ve-OR with y. Thus, if you use the GXxor function, you can erase a figure by simply redrawing . a window.
rer the old one. You can use this property of GXxor to animate drawings in X by erasing at the old
cation and drawing at the new one. A common use of the GXxor display function is in rubber-
l11ding,where a figure such as a\line or a rectangle is stretched and shrunk as the user moves the
Ouse. (You will see an example of rubber-banding later in this chapter.)
There are four attributes in a GCthat determine the appearance of lines:
~~Gx:nvert function is useful for highlighting an area in a black and white drawing by changing
lIte pIXelsto black and vice versa. •. line_Width
>ucan set the function attribute of a GCby calling XSetFunction. For example, to set the dis- •. line_style
ay function in a GCto GXxor, write the following: •. cap_-style
etFunction(p_disp, this_GC, GXxor); •. join_style