简体   繁体   中英

Make writing and lines appear on a GTK Window … XLIB and GDK used

What my code (below) does:

  1. Creates a XLIB window with a background color
  2. Draws a string on the window
  3. Draws a line on the window
  4. Creates a GTK+ window
  5. Makes the GTK+ window realise the XLIB window exsists via a GDK window
  6. Display the output of the XLIB window inside the GTK+ window

It works and creates a window of the correct colour but it doesn't draw the string or the line on the screen. Any ideas of how to make it appear or where im going wrong?

The reason I am not using the GTK+ drawing functions is because this a test program in reality all the drawing needs to come from the xlib window.

#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void destroy(GtkWidget *widget, gpointer data ) {
    gtk_main_quit ();
}

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

    GtkWidget *xwindow;

    //Open the display
    Display *display = XOpenDisplay(NULL);
    int screen = DefaultScreen(display);

    //Create the window
    Window w = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0,
    200, 100, 20, black, 10201020);
    XSelectInput(display, w, StructureNotifyMask);
    XMapWindow(display, w);
    GC gc = XCreateGC(display, w, 0, NULL);
    for(;;) {
          XEvent e;
          XNextEvent(display, &e);
          if (e.type == MapNotify)
                break;
    } 
    XDrawString(display, w, gc, 10, 10, "HelloWorld!", 12);
    XDrawLine(display, w, gc, 10, 60, 180, 20);
    XFlush(display);

    //SET UP
    gtk_init (&argc, &argv);

    //xwindow
    xwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect (xwindow, "destroy",
          G_CALLBACK (destroy), NULL);
    g_signal_connect (xwindow, "destroy",
          G_CALLBACK (print), NULL);
    gtk_widget_realize(xwindow);
    xwindow->window = gdk_window_foreign_new((guint32)w);

    //SET UP
    gtk_widget_show(xwindow);

    gtk_main ();

    return 0;
}

I believe this is simply due to a misunderstanding of what "drawing" really means, here.

The Xlib drawing model isn't "state-ful", it won't remember that your particular window has had some text drawn in a particular location, and then a line, and so on. The drawing happens immediately when you request it, and is then considered "done", ie forgotten about at the protocol level.

When you wrap the X window in a GTK+ widget, it will likely cause the X window system to attempt to refresh the contents of the X window, but that doesn't do anything, which is why your initial graphics are lost.

In short, you need to be able to respond to requests to redraw the window as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM