Thread: X!!! pixmap cursor!

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    2

    X!!! pixmap cursor!

    This is my first post here so I don't know if this is a ridiculous question or not but....

    On a SUN workstation (Ultra-5) I need to use Xwindows to define a cursor on a window that will be a character. Specifically, I need to XCreatePixmapCursor after doing an XDrawString onto a pixmap created by XCreatePixmapCursor. The reason I need to create a pixmap cursor as opposed to a glyph cursor(which would just take a character from a font and a mask from the same font by supplying the indices) is because a) the mask is in a different font from the source and b) I need to define the hotspot at the center of the character/pixmap instead of at (0,0) as defined by the bounding box of the character which would be the top left corner(XCreatePixmapCursor takes x,y coords for a hotspot) and c) I may have to expand this to take TWO characters as the cursor (a glyph cursor only takes one character from a font whereas a pixmap can have a 2 character string in it).

    If anybody has had any experience with this sort of thing (probably not likely) and has any ideas or info I'd greatly appreciate it.

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    2
    In case anybody was at all curious about this, I found my solution. It turns out that I was just drawing the character onto the pixmap at the wrong coordinates so the character was actually above the pixmap and not visible. Here's the code in case you guys are interested (sorry for the lack of comments but you know how it is ):

    #include <stdio.h>
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    #include <X11/Xatom.h>

    #define ConvertTo64K(c) ((65535 * (long) (c))/256)

    static Display *dpy;
    static Window w;

    char fontname[256];

    XFontStruct *font_info;



    static void StartConnectionToServer(argc, argv)
    int argc;
    char *argv[];
    {

    char *display;

    display = NULL;

    /* expect -d "display" as parameters */
    ++argv;
    if ((argc < 2) || (((*argv)[0] != '-') && ((*argv)[1] != 'd')))
    {
    printf ("Parameter error ! \n");
    exit();
    }

    display=argv[1];

    printf("Display = %s\n", display);
    if (!(dpy = XOpenDisplay(display)))
    {
    printf("Couldn't open display\n");
    exit(0);
    }
    XSynchronize(dpy, True);
    }


    main (argc, argv)
    int argc;
    char **argv;
    {

    XEvent event;
    XSetWindowAttributes xswa;
    unsigned long mask;
    XCharStruct overall_return;
    int direction_return, ascent_return, descent_return;
    int screen, width, height, keep_peeking;
    Colormap cmap;
    XColor xcolor1, xcolor2;
    unsigned long pixel;
    Pixmap curspix, cur_mask;
    Cursor cursor;
    XGCValues xgc;
    GC gc, gc2;

    StartConnectionToServer(argc, argv);

    screen = DefaultScreen(dpy);
    xswa.event_mask = ExposureMask;
    xswa.background_pixel = WhitePixel(dpy, DefaultScreen(dpy));
    mask = CWEventMask | CWBackPixel;
    w = XCreateWindow(dpy, RootWindow(dpy, DefaultScreen(dpy)),
    100, 100, 600, 800, 0, CopyFromParent, CopyFromParent,
    CopyFromParent, mask, &xswa);

    XMapWindow(dpy, w);

    cmap = DefaultColormap(dpy, screen);
    if (XAllocColorCells(dpy, cmap, False, NULL, 0, &pixel, 1)==0)
    {
    printf("Unable to alloc colorcell\n");
    }

    /* This color doesn't seem to be used for anything .... */
    xcolor1.pixel = 0;
    xcolor1.red = ConvertTo64K(255);
    xcolor1.green = ConvertTo64K(0);
    xcolor1.blue = ConvertTo64K(0);
    xcolor1.flags = DoRed|DoGreen|DoBlue;
    XStoreColor(dpy, cmap, &xcolor1);

    /* This color ends up being the color of the mask bits */
    xcolor2.pixel = 0;
    xcolor2.red = ConvertTo64K(0);
    xcolor2.green = ConvertTo64K(0);
    xcolor2.blue = ConvertTo64K(0);
    xcolor2.flags = DoRed|DoGreen|DoBlue;
    XStoreColor(dpy, cmap, &xcolor2);

    if ((font_info = XLoadQueryFont(dpy, "tvsys128")) == NULL)
    {
    printf("Can't load font!!!\n");
    }
    XTextExtents(font_info, "A", 1, &direction_return,
    &ascent_return, &descent_return, &overall_return);
    height = overall_return.ascent + abs(overall_return.descent);
    width = overall_return.rbearing + overall_return.lbearing;

    printf("Source width = %d\n", width);
    curspix = XCreatePixmap(dpy, w, 2*width, height, 1);
    gc = XCreateGC(dpy, curspix, 0, 0);

    XSetBackground(dpy, gc, WhitePixel(dpy,DefaultScreen(dpy)));
    XSetForeground(dpy, gc, WhitePixel(dpy,DefaultScreen(dpy)));
    XFillRectangle(dpy, curspix, gc, 0, 0, 2*width, height);
    XSetForeground(dpy, gc, BlackPixel(dpy,DefaultScreen(dpy)));
    XSetFont(dpy, gc, font_info->fid);
    XDrawImageString(dpy, curspix, gc, 0,height, "2D", 2);

    if ((font_info = XLoadQueryFont(dpy, "tvsys128b")) == NULL)
    {
    printf("Can't load font!!!!\n");
    }

    XTextExtents(font_info, "A", 1, &direction_return,
    &ascent_return, &descent_return, &overall_return);
    height = overall_return.ascent + abs(overall_return.descent);
    width = overall_return.lbearing + overall_return.rbearing;

    printf("Mask width = %d\n", width);
    cur_mask = XCreatePixmap(dpy, w, 2*width, height, 1);
    gc2 = XCreateGC(dpy, cur_mask, 0, 0);

    XSetBackground(dpy, gc2, WhitePixel(dpy, DefaultScreen(dpy)));
    XSetForeground(dpy, gc2, WhitePixel(dpy, DefaultScreen(dpy)));
    XFillRectangle(dpy, cur_mask, gc2, 0, 0,2*width, height);
    XSetForeground(dpy, gc2, BlackPixel(dpy,DefaultScreen(dpy)));
    XSetFont(dpy, gc2, font_info->fid);
    XDrawImageString(dpy, cur_mask, gc2, 0,height, "2D", 2);
    #if 1
    cursor = XCreatePixmapCursor(dpy, curspix, cur_mask, &xcolor1, &xcolor2,
    width, height/2);
    #else
    cursor = XCreatePixmapCursor(dpy, curspix, None, &xcolor1, &xcolor2,
    width/2, height/2);
    #endif
    XFreePixmap(dpy, curspix);
    XFreePixmap(dpy, cur_mask);
    XDefineCursor(dpy, w, cursor);

    while(1)
    {
    XNextEvent(dpy, &event);
    switch(event.type)
    {
    case Expose:
    keep_peeking =1;
    while (XEventsQueued(dpy, QueuedAlready) && keep_peeking)
    {
    XPeekEvent(dpy, &event);
    if (event.type == Expose)
    {
    XNextEvent(dpy, &event);
    }
    else
    {
    keep_peeking = 0;
    }
    }
    break;
    }
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get visible mouse cursor height
    By eXistenZ in forum Windows Programming
    Replies: 10
    Last Post: 09-05-2008, 09:46 PM
  2. Custom Animated Cursor
    By willc0de4food in forum Windows Programming
    Replies: 3
    Last Post: 05-13-2005, 10:05 PM
  3. cursor question
    By GodLike in forum Windows Programming
    Replies: 3
    Last Post: 05-09-2002, 06:14 PM
  4. cursor remains in openGL fullscreen
    By Ken Fitlike in forum Game Programming
    Replies: 5
    Last Post: 03-14-2002, 08:52 PM
  5. Mouse in 800x600 24Bit Mode?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 11-11-2001, 01:38 AM