#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>

#include <gtk/gtk.h>


typedef struct {
	GtkWidget *mainwin;
	GtkDrawingArea *darea;
	GdkPixmap *pixmap;
} oswald_ui;



void set_pixel(oswald_ui *ui, gint x, gint y, gboolean state)
{
	GdkRectangle update_rect;
	GdkGC *gc;
	gint ix, iy;

	ix = x*2;
	iy = y*2;

	update_rect.x = ix - 5;
	update_rect.y = iy - 5;
	update_rect.width = 10;
	update_rect.height = 10;

	gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->mainwin->style->black_gc : ui->mainwin->style->white_gc, ix, iy);
	gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->mainwin->style->black_gc : ui->mainwin->style->white_gc, ix+1, iy);
	gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->mainwin->style->black_gc : ui->mainwin->style->white_gc, ix, iy+1);
	gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->mainwin->style->black_gc : ui->mainwin->style->white_gc, ix+1, iy+1);

	gtk_widget_draw (GTK_WIDGET(ui->darea), &update_rect);
}

static gint
configure_event (GtkWidget *widget, GdkEventConfigure *event, gpointer user_data)
{
	oswald_ui *ui = (oswald_ui *)user_data;

	if (ui->pixmap)
		gdk_pixmap_unref(ui->pixmap);

	ui->pixmap = gdk_pixmap_new(widget->window,
		widget->allocation.width,
		widget->allocation.height,
		-1);
	gdk_draw_rectangle (ui->pixmap,
		widget->style->white_gc,
		TRUE,
		0, 0,
		widget->allocation.width,
		widget->allocation.height);

	return TRUE;
}

static gint
expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
{
	oswald_ui *ui = (oswald_ui *)user_data;

	gdk_draw_pixmap(widget->window,
		widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
		ui->pixmap,
		event->area.x, event->area.y,
		event->area.x, event->area.y,
		event->area.width, event->area.height);

	return FALSE;
}

static void create_mainwin(oswald_ui *ui)
{
	GtkWidget *hb, *vb, *btn;

	ui->pixmap = NULL;

	ui->mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	// gtk_window_set_default_size (GTK_WINDOW (ui->mainwin), 440, 240);
	g_signal_connect(G_OBJECT(ui->mainwin), "destroy", gtk_main_quit, NULL);

	hb = gtk_hbox_new(FALSE, 5);
	gtk_container_add(GTK_CONTAINER(ui->mainwin), hb);

	vb = gtk_vbox_new(FALSE, 5);
	gtk_box_pack_start (GTK_BOX(hb), vb, FALSE, FALSE, 5);

	btn = gtk_button_new_with_label("D");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	btn = gtk_button_new_with_label("E");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	btn = gtk_button_new_with_label("F");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	ui->darea = (GtkDrawingArea *)gtk_drawing_area_new ();
	gtk_box_pack_start (GTK_BOX(hb), GTK_WIDGET(ui->darea), FALSE, FALSE, 5);
	gtk_drawing_area_size (ui->darea, 192, 192);

	gtk_signal_connect (GTK_OBJECT (ui->darea), "expose_event", (GtkSignalFunc) expose_event, ui);
	gtk_signal_connect (GTK_OBJECT (ui->darea), "configure_event", (GtkSignalFunc) configure_event, ui);
	// gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event", (GtkSignalFunc) motion_notify_event, ui);
	// gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event", (GtkSignalFunc) button_press_event, ui);

	gtk_widget_set_events (GTK_WIDGET(ui->darea), GDK_EXPOSURE_MASK
		| GDK_LEAVE_NOTIFY_MASK
		| GDK_BUTTON_PRESS_MASK
		| GDK_POINTER_MOTION_MASK
		| GDK_POINTER_MOTION_HINT_MASK);

	vb = gtk_vbox_new(FALSE, 5);
	gtk_box_pack_start (GTK_BOX(hb), vb, FALSE, FALSE, 5);

	btn = gtk_button_new_with_label("A");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	btn = gtk_button_new_with_label("B");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	btn = gtk_button_new_with_label("C");
	gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);

	gtk_widget_show_all(ui->mainwin);
}

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

	gtk_init (&argc, &argv);

	create_mainwin(&ui);

	set_pixel(&ui, 48, 48, TRUE);

	gtk_main ();
	return 0;
}