diff --git a/solidify/level.c b/solidify/level.c index ee541b0..5fe314c 100644 --- a/solidify/level.c +++ b/solidify/level.c @@ -131,45 +131,20 @@ static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, } -static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event, - gpointer data) -{ - if (event->keyval == 'q') - gtk_main_quit(); - return TRUE; -} - - -static void make_screen(GtkWidget *root, struct face *f) +void level(GtkWidget *canvas, struct face *f) { GtkWidget *evbox, *da; + evbox = gtk_event_box_new(); da = gtk_drawing_area_new(); gtk_widget_set_size_request(da, f->sx, f->sy); - evbox = gtk_event_box_new(); + gtk_container_add(GTK_CONTAINER(canvas), evbox); gtk_container_add(GTK_CONTAINER(evbox), da); - gtk_container_add(GTK_CONTAINER(root), evbox); - gtk_widget_show_all(root); + draw_image(da, f); g_signal_connect(G_OBJECT(evbox), "scroll-event", G_CALLBACK(scroll_event), f); g_signal_connect(G_OBJECT(da), "expose-event", G_CALLBACK(expose_event), f); - g_signal_connect(G_OBJECT(root), "key-press-event", - G_CALLBACK(key_press_event), NULL); -} - - -void level(struct face *f) -{ - GtkWidget *root; - - root = gtk_window_new(GTK_WINDOW_TOPLEVEL); - gtk_window_set_position(GTK_WINDOW(root), GTK_WIN_POS_CENTER); - - g_signal_connect(G_OBJECT(root), "destroy", - G_CALLBACK(gtk_main_quit), NULL); - make_screen(root, f); - gtk_main(); } diff --git a/solidify/level.h b/solidify/level.h index 654d08f..e30ae76 100644 --- a/solidify/level.h +++ b/solidify/level.h @@ -13,9 +13,11 @@ #ifndef LEVEL_H #define LEVEL_H +#include + #include "face.h" -void level(struct face *f); +void level(GtkWidget *canvas, struct face *f); #endif /* LEVEL_H */ diff --git a/solidify/overlap.c b/solidify/overlap.c index a260398..c0b92e2 100644 --- a/solidify/overlap.c +++ b/solidify/overlap.c @@ -156,33 +156,22 @@ static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, -static void make_screen(GtkWidget *root, struct face *f) +void overlap(GtkWidget *canvas, struct face *f) { GtkWidget *evbox, *da; + evbox = gtk_event_box_new(); da = gtk_drawing_area_new(); gtk_widget_set_size_request(da, f->sx, f->sy); - evbox = gtk_event_box_new(); + gtk_container_add(GTK_CONTAINER(canvas), evbox); gtk_container_add(GTK_CONTAINER(evbox), da); - gtk_container_add(GTK_CONTAINER(root), evbox); - gtk_widget_show_all(root); + draw_image(da, f); g_signal_connect(G_OBJECT(evbox), "scroll-event", G_CALLBACK(scroll_event), f); g_signal_connect(G_OBJECT(da), "expose-event", G_CALLBACK(expose_event), f); -} - -void overlap(struct face *face) -{ - GtkWidget *root; - - root = gtk_window_new(GTK_WINDOW_TOPLEVEL); - gtk_window_set_position(GTK_WINDOW(root), GTK_WIN_POS_CENTER); - - g_signal_connect(G_OBJECT(root), "destroy", - G_CALLBACK(gtk_main_quit), NULL); - make_screen(root, face); - gtk_main(); + + return evbox; } diff --git a/solidify/overlap.h b/solidify/overlap.h index d86be11..b24b3e5 100644 --- a/solidify/overlap.h +++ b/solidify/overlap.h @@ -10,12 +10,12 @@ * (at your option) any later version. */ - #ifndef OVERLAP_H #define OVERLAP_H +#include -void overlap(struct face *face); +void overlap(GtkWidget *canvas, struct face *face); #endif /* !OVERLAP_H */ diff --git a/solidify/solidify.c b/solidify/solidify.c index 78dce2e..c17b22f 100644 --- a/solidify/solidify.c +++ b/solidify/solidify.c @@ -18,6 +18,98 @@ #include "face.h" #include "level.h" +#include "overlap.h" + + +static struct face *face_a, *face_b; +static const struct face *active; +static GtkWidget *canvas; + + +static void clicked(GtkButton *button, gpointer user_data) +{ + struct face *face = user_data; + + if (active == face) + return; + + gtk_widget_destroy(gtk_bin_get_child(GTK_BIN(canvas))); + + if (face) + level(canvas, face); + else + overlap(canvas, face_a); + active = face; + + gtk_widget_show_all(canvas); +} + + +static GtkWidget *gui_buttons(void) +{ + GtkWidget *vbox, *but; + + vbox = gtk_vbox_new(FALSE, 0); + + but = gtk_button_new_with_label("A"); + gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0); + g_signal_connect(G_OBJECT(but), "clicked", + G_CALLBACK(clicked), face_a); + + but = gtk_button_new_with_label("B"); + gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0); + g_signal_connect(G_OBJECT(but), "clicked", + G_CALLBACK(clicked), face_b); + + but = gtk_button_new_with_label("A+B"); + gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0); + g_signal_connect(G_OBJECT(but), "clicked", + G_CALLBACK(clicked), NULL); + + return vbox; +} + + +static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event, + gpointer data) +{ + if (event->keyval == 'q') + gtk_main_quit(); + return TRUE; +} + + +static void gui(void) +{ + GtkWidget *root, *hbox, *buttons; + + root = gtk_window_new(GTK_WINDOW_TOPLEVEL); + gtk_window_set_position(GTK_WINDOW(root), GTK_WIN_POS_CENTER); + + hbox = gtk_hbox_new(FALSE, 0); + gtk_container_add(GTK_CONTAINER(root), hbox); + + canvas = gtk_event_box_new(); + gtk_box_pack_start(GTK_BOX(hbox), canvas, FALSE, FALSE, 0); + + /* initialize root->window */ + gtk_widget_show_all(root); + + buttons = gui_buttons(); + gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0); + + level(canvas, face_a); + active = face_a; + + gtk_widget_show_all(root); + + g_signal_connect(G_OBJECT(root), "key-press-event", + G_CALLBACK(key_press_event), NULL); + g_signal_connect(G_OBJECT(root), "destroy", + G_CALLBACK(gtk_main_quit), NULL); + + gtk_main(); +} static void usage(const char *name) @@ -29,8 +121,6 @@ static void usage(const char *name) int main(int argc, char **argv) { - struct face *top; - gtk_init(&argc, &argv); switch (argc) { case 2: @@ -39,8 +129,9 @@ int main(int argc, char **argv) usage(*argv); } setlocale(LC_ALL, "C"); /* damage control */ - top = read_face(argv[1]); - level(top); + face_a = read_face(argv[1]); + face_b = face_a; + gui(); return 0; }