Please help: How to embed a X window in a GTK window?



Dear All,
I am working on a project whose code was written in Xlib and now I need to develop a GUI for it. I want to use GTK so I wonder if I could embed the former xlib window into a GTK top-level window. I wrote a small test program as below but it doesn't work. Does anyone have any idea about how this is gonna work?

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

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



int main(int argc, char* argv[])
{
    Display* display = XOpenDisplay(NULL);
    int screen = DefaultScreen(display);
    int width = DisplayWidth(display, screen)/6;
    int height = DisplayHeight(display, screen)/6;
    GtkWidget *gtkWin;
    XEvent e;
    pid_t pid;
    Bool child = FALSE;
    int timer = 0;

    gtk_init (&argc, &argv);

    gtkWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);

gtk_signal_connect(GTK_OBJECT(gtkWin), "destroy",GTK_SIGNAL_FUNC(destroy),NULL);

    gtk_widget_realize(gtkWin);

Window win = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, width, height, 3, BlackPixel(display, screen), WhitePixel(display, screen));
    XStoreName(display, win, "hello");
    GC gc = XCreateGC(display, win, 0, NULL);

    XMapWindow(display, win);

    XSelectInput(display, win, ExposureMask|ButtonPressMask);

    if((pid = fork()) <0 )
    {
        perror("fork");
        exit(1);
    }
    else if(pid > 0)
    {
        printf("parent\n");

XReparentWindow(display,win,GDK_WINDOW_XID(gtkWin->window), 50,50);
        XMapWindow(display,GDK_WINDOW_XID(gtkWin->window));
        gtk_widget_show(gtkWin);


        gtk_main();

    }
    else
    {
        printf("child\n");
        
        while(1)
        {
            XNextEvent(display, &e);
            if(e.type==Expose && e.xexpose.count<1)
            {
XDrawString(display, win, gc, 10, 10, "Hello World!", 12);

            }
            else if(e.type==ButtonPress)
            {
                XCloseDisplay(display);
                break;
            }
            else
            {
            }
        }
    }


    return 0;
}


Thanks & Regards,
Kay



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]