Search code examples
cxlib

How can I limit the surface in which Xlib graphics primitives draw?


I think that's what clip are used for but I can't find any example to do this. I need to:

  • Limit the region by setting a new clipmask (altering the GC)
  • Draw
  • Set the GC back to its previous state

Solution

  • You can do it by using XSetClipRectangles() referenced here and XSetClipMask() referenced here

    So:

    Display dpy; //This is your display, we'll assume it is a valid Display
    GC gc; //This is your GC, we'll assume it is a valid GC
    XRectangle recs[]; //This is an array containing the clipping regions you want.
    int recs_n; //This is the number of rectangles in the 'recs' array.
    
    XSetClipRectangles(dpy, gc, 0, 0, recs, recs_n, Unsorted); //Enable clipping
    drawMyClippedGraphics(); //Call to whatever you want to use for drawing
    XSetClipMask(dpy, gc, None); //Restore the GC
    

    For further information type man functionName in your terminal.