Professional Documents
Culture Documents
Amongthe attributes that you can set in the XSet~indowAttributes structure, there is a long in-
tegerfield named event_mask. The value of this mask determines which types of events occurring
in the window are reported to your application. Recall from Chapter 6, "The Xlib Programming
Mood," that you can use the XSelectlnput function to select the events to be reported for a win-
dow.For the main window, ini tapp selects the Expose event:
1* Select Exposure events for the main window * I
Youcan select multiple events by using as the last argument of XSelectlnput the bitwise-OR of
definedevent masks from the header file <x 11 / X. h>. For example, to enable buttonpresses as well
as Expose events, you can use the following call:
xselectlnput(theDisplay, theMain,
ExposureMask : ButtonPressMask);
Insteadof calling XSelectlnput, you can set the event_mask field in an XSetWindowAttributes
structureand select the events with a call to the XChangeWindowAttributes function. For example,
toselectthe Expose and ButtonPress events for-the main window, you can use the following code:
xswa.colormap = DefaultColormap(theDisplay,
DefaultScreen(theDisplay));
xswa.bit_9ravity = NorthWestGravity;
xswa.event_mask = ExposureMask : ButtonPressMask;
XChangeWindowAttributes(theDisplay, theMain,
(CWColormap : CWBitGravity : CWEventMask), &xswa);
Sometimesyou may want to select a new event or drop an event without disturbing the other input I
selections.If you have to change the event selectio;1s, use the following procedure:
A good approach is to develop two utility functions, AddNewEvent and IgnoreEvent, that respec-
tively enable or disable an event specified by an event mask. Listing 7.3 shows such a pair of func-
tions. These functions can be very handy. For example, if you want to stop receiving ButtonPress
events in a section of your code, you can use these functions to achieve your objective:
IgnoreEvent(theMain, ButtonPressMask)j
/* No more buttonpresses reported ... */
*/
void AddNewEvent(Window xid, long event_mask)
{
XWindowAttributes xwa;
XSetWindowAttributes xswaj
XChangeWindowAttributes(theDisplay, xid,
CWEventMask, &xswa)j
}
/*_ ... _ ..... _ .. _ .. _ ..• _ .. _ ... _. _. - -- _. - _. - - _.. - -- - -- -- - - - - - - - -*/
/* I 9 nor e Eve n t
*/
void IgnoreEvent(Window xid, long event_mask)
XWindowAttributes xwaj
XSetWindowAttributes xswaj
XCloseDisplay(theDisplay);
If you want to destroy only the subwindows of a window, but not the window itself, you can use.
exit(0); the function XDestroySubwi.ndows, which is called in the same manner as XDestroyWindow.
Usually, you do nor have to destroy windows explicitly. When you close the connection to a display Display *p_disp;
Window DialogBox;
(with XCloseElispl..ay), the X server automatically destroys all resources associated with your appli-
cation. However, if you have an occasion in which you want to destroy a window and all its
subwindows, you can use XDestroyWindow to do the job: XUnmapWindow{p_disp, DialogBox);
Windows are the reason programmers use the X Window System, the mechanism that enables sev-
eral applications to share the limited space in a physical display screen.
Chapter 8
andling Events
} Even though the display pointer appears as an argument to XSaveContext, storing data this way
if(theEvent.xany.window QuitButton) has nothing to with the X server, and everything is handled locally by Xlib. (An arbitrary collection
{
of data stored in a window at the server is known as that window's property, properties are described
f* Process events occurring in the QuitButton window *f
in Chapter 16, "Advanced Topics in X.")
Context is used to identifY the type of data. You can use the same context to store the same type of
data in many different windows. You do not have to have a separate context for each window. The
} ,context help~ you distinguish between different data types stored at a window.
f* Check for other windows ... and process events *f
Finally, the last argument to XSaveContext is of type caddr _ t, which denotes the address of any
type of data Of function in C (caddr _t is usually defined in the header file <sys/types. h». The
exact meaning of the data is irrelevant to the context management routines. How you interpret this
The problem with this processing loop is that as the number of windows grows, the event handling data is up to you.
loop gets out of hand. There are too many if statements testing for the window 10 and processing
the events. Because there are many windows in any reasonable user interface, you need to organize You can retrieve the stored data using XFindContext:
the program in a way that helps you manage the complexity of event handling. This is where the caddr_t data;
context management routines ofXlib can help you. A side benefit of using these routines is that you
if (XFindContext (theDisplay , w, context1,
can begin to develop a structure that enables you to create and manage multiple copies of a single (caddr_t *) &data) == 0)
type of window (for example, several buttons for menus). {
f* Data retrieved without error. Use data in 'this block *f
1. Call XUniqueContext to get a unique context identifier for this type of data:
---
windows and handle "the events in a convenient manner.
if(theEvent.type
{