#include <stdlib.h>

#include <X11/Xatom.h>
#include <gtk/gtk.h>
#include <gtk/gtkx.h>

static Display *dpy;
static long embed;

static void
destroywin(GtkWidget* w)
{
	gtk_main_quit();
}

static GtkWidget *
newwin(void)
{
	GtkWidget *w;

	if (embed)
		w = gtk_plug_new(embed);
	else
		w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	g_signal_connect(G_OBJECT(w), "destroy",
	                 G_CALLBACK(destroywin), NULL);
	gtk_window_set_default_size(GTK_WINDOW(w), 800, 600);

	return w;
}

static void
show(GtkWidget *w, GtkWidget *v)
{
	gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(v));
	gtk_widget_grab_focus(GTK_WIDGET(v));
	gtk_widget_show_all(w);
}

static void
setup(int *argc, char **argv[])
{
	GdkDisplay *gdpy;

	gtk_init(argc, argv);

	if (*argc > 1)
		embed = strtol((*argv)[1], NULL, 0);

	gdpy = gdk_display_get_default();
	dpy = GDK_DISPLAY_XDISPLAY(gdpy);

	XInternAtom(dpy, "_FOO", False);
	XInternAtom(dpy, "_BAR", False);
	XInternAtom(dpy, "_BAZ", False);
}

int
main(int argc, char *argv[])
{
	GtkWidget *win, *view;

	setup(&argc, &argv);

	view = gtk_text_view_new();
	win = newwin();
	show(win, view);

	gtk_main();
}
