Paint a image in Cairo surface X11
Alex Vazquez <[email protected]>
| Newsgroups | gmane.comp.lib.cairo |
|---|---|
| Message-ID | <CAOTEMURG3o+TaJBewnP-T0q8GMwASNH8_=tQbOL460qy0z2N3A@mail.gmail.com> |
I have a problem when I paint a image using Cairo and X11. The test is simple I try paint a image in a X11 Windows using Cairo. If the size of surface is less that size of image, X11 don't show the image but If the size of surface is bigger that the size of image X11 show fine the image. Why is happen? How can i fix this problem? I add a test where you can test this problem. -- cairo mailing list [email protected] http://lists.cairographics.org/mailman/listinfo/cairo
Makefile
(application/octet-stream, 397 B) - not displayed
cairo_xlib_simple.c
(text/x-csrc, 2.3 KB)
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void cairo_close_x11_surface();
static Display *dsp;
cairo_surface_t *sfc;
int cairo_check_event(cairo_surface_t *sfc, int block)
{
XEvent e;
for (;;)
{
///if (block || XPending(cairo_xlib_surface_get_display(sfc)))
XNextEvent(cairo_xlib_surface_get_display(sfc), &e);
//else
// return 0;
switch (e.type)
{
case ButtonPress:
cairo_close_x11_surface();
break;
default:
break;
}
}
}
/*! Open an X11 window and create a cairo surface base on that window.
* @param x Width of window.
* @param y Height of window.
* @return Returns a pointer to a valid Xlib cairo surface. The function does
* not return on error (exit(3)).
*/
cairo_surface_t *cairo_create_x11_surface0(int x, int y)
{
//Display *dsp;
Drawable da;
int screen;
if ((dsp = XOpenDisplay(NULL)) == NULL)
exit(1);
screen = DefaultScreen(dsp);
da = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp), 0, 0, x, y, 0, 0, 0);
XSelectInput(dsp, da, ButtonPressMask | ButtonReleaseMask);
XMapWindow(dsp, da);
XFlush(dsp);
XSync(dsp, screen);
sfc = cairo_xlib_surface_create(dsp, da, DefaultVisual(dsp, screen), x, y);
cairo_xlib_surface_set_size(sfc, x, y);
return sfc;
}
/*! Destroy cairo Xlib surface and close X connection.
*/
void cairo_close_x11_surface()
{
Display *dsp = cairo_xlib_surface_get_display(sfc);
cairo_surface_destroy(sfc);
XCloseDisplay(dsp);
}
int main(int argc, char **argv)
{
cairo_surface_t *sfc;
cairo_t *ctx;
cairo_surface_t *image;
int w, h;
int iwidth,iheight;
if (argc < 3)
{
printf("./cairo_xlib_simple [Width] [Height]\n");
return 1;
}
iwidth = atoi(argv[1]);
iheight = atoi(argv[2]);
sfc = cairo_create_x11_surface0(iwidth, iheight);
ctx = cairo_create(sfc);
image = cairo_image_surface_create_from_png ("photo.png");
cairo_set_source_surface (ctx, image, 0, 0);
cairo_paint (ctx);
w = cairo_image_surface_get_width (image);
h = cairo_image_surface_get_height (image);
printf("Size: %dx%d\n",w,h );
XFlush(dsp);
cairo_destroy(ctx);
cairo_check_event(sfc, 1);
cairo_close_x11_surface(sfc);
return 0;
}