Root/solidify/solidify.c

Source at commit 1fa23c574e532cd0a398661828a03912e6a2039c created 8 years 11 months ago.
By Werner Almesberger, Introduce a solid data type and use it, mainly in overlap()
1/*
2 * solidify.c - Merge two opposing faces of a part into a solid
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <locale.h>
17#include <gtk/gtk.h>
18
19#include "face.h"
20#include "solid.h"
21#include "level.h"
22#include "overlap.h"
23
24
25static struct solid solid;
26static const struct face *active;
27static GtkWidget *canvas;
28
29
30static void clicked(GtkButton *button, gpointer user_data)
31{
32    struct face *face = user_data;
33
34    if (active == face)
35        return;
36
37    gtk_widget_destroy(gtk_bin_get_child(GTK_BIN(canvas)));
38
39    if (face)
40        level(canvas, face);
41    else
42        overlap(canvas, &solid);
43    active = face;
44
45    gtk_widget_show_all(canvas);
46}
47
48
49static GtkWidget *gui_buttons(void)
50{
51    GtkWidget *vbox, *but;
52
53    vbox = gtk_vbox_new(FALSE, 0);
54
55    but = gtk_button_new_with_label("A");
56    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
57    g_signal_connect(G_OBJECT(but), "clicked",
58        G_CALLBACK(clicked), solid.a);
59
60    but = gtk_button_new_with_label("B");
61    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
62    g_signal_connect(G_OBJECT(but), "clicked",
63        G_CALLBACK(clicked), solid.b);
64
65    but = gtk_button_new_with_label("A+B");
66    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
67    g_signal_connect(G_OBJECT(but), "clicked",
68        G_CALLBACK(clicked), NULL);
69
70    return vbox;
71}
72
73
74static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event,
75    gpointer data)
76{
77    if (event->keyval == 'q')
78        gtk_main_quit();
79    return TRUE;
80}
81
82
83static void gui(void)
84{
85    GtkWidget *root, *hbox, *buttons;
86
87    root = gtk_window_new(GTK_WINDOW_TOPLEVEL);
88    gtk_window_set_position(GTK_WINDOW(root), GTK_WIN_POS_CENTER);
89
90    hbox = gtk_hbox_new(FALSE, 0);
91    gtk_container_add(GTK_CONTAINER(root), hbox);
92
93    canvas = gtk_event_box_new();
94    gtk_box_pack_start(GTK_BOX(hbox), canvas, FALSE, FALSE, 0);
95
96    /* initialize root->window */
97    gtk_widget_show_all(root);
98
99    buttons = gui_buttons();
100    gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
101
102    level(canvas, solid.a);
103    active = solid.a;
104
105    gtk_widget_show_all(root);
106
107    g_signal_connect(G_OBJECT(root), "key-press-event",
108        G_CALLBACK(key_press_event), NULL);
109    g_signal_connect(G_OBJECT(root), "destroy",
110        G_CALLBACK(gtk_main_quit), NULL);
111
112    gtk_main();
113}
114
115
116static void usage(const char *name)
117{
118    fprintf(stderr, "usage: %s top XXXbottomXXX\n", name);
119    exit(1);
120}
121
122
123int main(int argc, char **argv)
124{
125    gtk_init(&argc, &argv);
126    switch (argc) {
127    case 2:
128        break;
129    default:
130        usage(*argv);
131    }
132    setlocale(LC_ALL, "C"); /* damage control */
133    solid.a = read_face(argv[1]);
134    solid.b = solid.a;
135    gui();
136
137    return 0;
138}
139

Archive Download this file

Branches:
master



interactive