XDrawString and remapping of my window? - Xwindows

This is a discussion on XDrawString and remapping of my window? - Xwindows ; Hello all, i try to open a x window with a text in the middle of it. And here is my problem: If i draw the text to the window all works fine. If i now unmap the window and ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: XDrawString and remapping of my window?

  1. XDrawString and remapping of my window?

    Hello all,
    i try to open a x window with a text in the middle of it. And here is
    my problem:
    If i draw the text to the window all works fine. If i now unmap the
    window and map it again the text isn't avaiable. Why?
    After this problem i have tried to put the code which draws the text
    into a function to run it again after remapping. In this way i never
    see the text. Whats my fault?

    Here is a part of the sample code that i have found during my search:


    void DrawMyString(Display *display, Window win,GC gc,XFontStruct*
    font_info, unsigned int win_width,unsigned int win_height)
    {
    /* variables used for drawing the text strings. */
    int x, y;
    char* text_string;
    int string_width;
    int font_height;

    /* find the height of the characters drawn using this font.
    */
    font_height = font_info->ascent + font_info->descent;

    /* draw a "hello world" string on the top-left side of our window.
    */
    text_string = "hello world";
    x = 0;
    y = font_height;
    XDrawString(display, win, gc, x, y, text_string,
    strlen(text_string));

    /* draw a "middle of the road" string in the middle of our window.
    */
    text_string = "middle of the road";
    /* find the width, in pixels, of the text that will be drawn using
    */
    /* the given font.
    */
    string_width = XTextWidth(font_info, text_string,
    strlen(text_string));
    x = (win_width - string_width) / 2;
    y = (win_height + font_height) / 2;
    XDrawString(display, win, gc, x, y, text_string,
    strlen(text_string));

    return;
    }

    void
    main(int argc, char* argv[])
    {
    Display* display; /* pointer to X Display structure. */
    int screen_num; /* number of screen to place the window on. */
    Window win; /* pointer to the newly created window. */
    unsigned int display_width,
    display_height; /* height and width of the X
    display. */
    unsigned int win_width,
    win_height; /* height and width for the new window. */
    char *display_name = getenv("DISPLAY"); /* address of the X
    display. */
    GC gc; /* GC (graphics context) used for drawing */
    /* in our window. */
    XFontStruct* font_info; /* Font structure, used for drawing
    text. */
    char* font_name = "*-helvetica-*-12-*"; /* font to use for drawing
    text. */

    /* open connection with the X server. */
    display = XOpenDisplay(display_name);
    if (display == NULL) {
    fprintf(stderr, "%s: cannot connect to X server '%s'\n",
    argv[0], display_name);
    exit(1);
    }

    /* get the geometry of the default screen for our display. */
    screen_num = DefaultScreen(display);
    display_width = DisplayWidth(display, screen_num);
    display_height = DisplayHeight(display, screen_num);

    /* make the new window occupy 1/9 of the screen's size. */
    win_width = (display_width / 3);
    win_height = (display_height / 3);
    printf("window width - '%d'; height - '%d'\n", win_width,
    win_height);

    /* create a simple window, as a direct child of the screen's */
    /* root window. Use the screen's white color as the background */
    /* color of the window. Place the new window's top-left corner */
    /* at the given 'x,y' coordinates. */
    win = create_simple_window(display, win_width, win_height, 0, 0);

    /* allocate a new GC (graphics context) for drawing in the window.
    */
    gc = create_gc(display, win, 0);
    XSync(display, False);

    /* try to load the given font. */
    font_info = XLoadQueryFont(display, font_name);
    if (!font_info) {
    fprintf(stderr, "XLoadQueryFont: failed loading font '%s'\n",
    font_name);
    exit(-1);
    }

    /* assign the given font to our GC. */
    XSetFont(display, gc, font_info->fid);
    #if 0
    {
    /* variables used for drawing the text strings. */
    int x, y;
    char* text_string;
    int string_width;
    int font_height;

    /* find the height of the characters drawn using this font.
    */
    font_height = font_info->ascent + font_info->descent;

    /* draw a "hello world" string on the top-left side of our window.
    */
    text_string = "hello world";
    x = 0;
    y = font_height;
    XDrawString(display, win, gc, x, y, text_string,
    strlen(text_string));

    /* draw a "middle of the road" string in the middle of our window.
    */
    text_string = "middle of the road";
    /* find the width, in pixels, of the text that will be drawn using
    */
    /* the given font.
    */
    string_width = XTextWidth(font_info, text_string,
    strlen(text_string));
    x = (win_width - string_width) / 2;
    y = (win_height + font_height) / 2;
    XDrawString(display, win, gc, x, y, text_string,
    strlen(text_string));
    }
    #endif
    /* why did this procedure didn't work? */
    DrawMyString(display, win, gc, font_info, win_width, win_height);

    /* flush all pending requests to the X server. */
    XFlush(display);

    /* make a delay for a short period. */
    sleep(2);
    #if 0
    XUnmapWindow(display,win);
    XFlush(display);
    sleep(2);
    XMapWindow(display,win);
    XDrawString(display, win, gc, x, y, text_string,
    strlen(text_string));
    XFlush(display);
    sleep(2);
    #endif
    /* close the connection to the X server. */
    XCloseDisplay(display);
    }



    Any suggestions?

    Regards,
    flo


  2. Re: XDrawString and remapping of my window?

    mich1985 writes:

    > Hello all,
    > i try to open a x window with a text in the middle of it. And here is
    > my problem:
    > If i draw the text to the window all works fine. If i now unmap the
    > window and map it again the text isn't avaiable. Why?
    > After this problem i have tried to put the code which draws the text
    > into a function to run it again after remapping. In this way i never
    > see the text. Whats my fault?


    You need to request Expose events and repaint the newly exposed part
    of the window each time you get one.

    --
    Måns Rullgård
    mans@mansr.com

  3. Re: XDrawString and remapping of my window?

    mich1985 writes:
    >> If i draw the text to the window all works fine. If i now unmap the
    >> window and map it again the text isn't avaiable. Why?


    Måns Rullgård correctly replied:
    > You need to request Expose events and repaint the newly exposed part
    > of the window each time you get one.


    Processing Expose events and redrawing is generally best, but there's
    another alternative you could also consider: enabling the window's
    "backing store".

    Cost: Unless you have a small window, the amount of memory the backing
    store will take will likely exceed the amount of memory you would
    have used for Expose event handling + redraw code, but if you have
    plenty of memory and don't care about program size, that may not
    matter to you.

    Benefit: Enabling backing store is usually just a couple short lines of
    code before realizing the widget.
    -WBE

  4. Re: XDrawString and remapping of my window?


    wrote in message
    news:ydmyz2d2qo.fsf@UBEblock.psr.com...
    > mich1985 writes:
    >>> If i draw the text to the window all works fine. If i now unmap the
    >>> window and map it again the text isn't avaiable. Why?

    >
    > Måns Rullgård correctly replied:
    >> You need to request Expose events and repaint the newly exposed part
    >> of the window each time you get one.

    >
    > Processing Expose events and redrawing is generally best, but there's
    > another alternative you could also consider: enabling the window's
    > "backing store".
    >
    > Cost: Unless you have a small window, the amount of memory the backing
    > store will take will likely exceed the amount of memory you would
    > have used for Expose event handling + redraw code, but if you have
    > plenty of memory and don't care about program size, that may not
    > matter to you.
    >
    > Benefit: Enabling backing store is usually just a couple short lines of
    > code before realizing the widget.


    One should never rely on backing store. It is definitely not portable
    (not all screens have backing store available).
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Aero Stability and Controls Computing



+ Reply to Thread