Gamma – Helm - Johnson – Vlissides
_imp->DeviceRect(x0, y0, x1, y1);
}
where _imp is a member variable of Window that stores the WindowImp with which the Window is configured.
The window implementation is defined by the instance of the WindowImp subclass that _imp points to. For an
XWindowImp (that is, a WindowImp subclass for the X Window System), the DeviceRect's implementation
might look like
void XWindowImp::DeviceRect (
Coord x0, Coord y0, Coord x1, Coord y1
) {
int x = round(min(x0, x1));
int y = round(min(y0, y1));
int w = round(abs(x0 - x1));
int h = round(abs(y0 - y1));
XDrawRectangle(_dpy, _winid, _gc, x, y, w, h);
}
DeviceRect is defined like this because XDrawRectangle (the X interface for drawing a rectangle) defines a
rectangle in terms of its lower left corner, its width, and its height. DeviceRect must compute these values from
those supplied. First it ascertains the lower left corner (since (x0, y0) might be any one of the rectangle's four
corners) and then calculates the width and height.
PMWindowImp (a subclass of WindowImp for Presentation Manager) would define DeviceRect differently:
void PMWindowImp::DeviceRect (
Coord x0, Coord y0, Coord x1, Coord y1
) {
Coord left = min(x0, x1);
Coord right = max(x0, x1);
Coord bottom = min(y0, y1);
Coord top = max(y0, y1);
PPOINTL point[4];
point[0].x = left; point[0].y = top;
point[1].x = right; point[1].y = top;
point[2].x = right; point[2].y = bottom;
point[3].x = left; point[3].y = bottom;
if (
(GpiBeginPath(_hps, 1L) == false) ||
(GpiSetCurrentPosition(_hps, &point[3]) == false) ||
(GpiPolyLine(_hps, 4L, point) == GPI_ERROR) ||
(GpiEndPath(_hps) == false)
) {
// report error
} else {
GpiStrokePath(_hps, 1L, 0L);
}
}
Why is this so different from the X version? Well, PM doesn't have an operation for drawing rectangles
explicitly as X does. Instead, PM has a more general interface for specifying vertices of multisegment shapes
(called a path) and for outlining or filling the area they enclose.
PM's implementation of DeviceRect is obviously quite different from X's, but that doesn't matter. WindowImp
hides variations in window system interfaces behind a potentially large but stable interface. That lets Window
Página 56