Root/solidify/solidify.c

Source at commit f6ed3bf762b63b950cf801c9282101a818c0ff8e created 9 years 2 months ago.
By Werner Almesberger, solidify now stores the context of sessions in project description files.
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 <unistd.h>
17#include <locale.h>
18#include <gtk/gtk.h>
19
20#include "face.h"
21#include "solid.h"
22#include "project.h"
23#include "style.h"
24#include "level.h"
25#include "overlap.h"
26
27
28static struct project *prj;
29static const struct face *active; /* NULL if overlapping */
30static GtkWidget *canvas;
31
32
33static void clicked(GtkButton *button, gpointer user_data)
34{
35    struct face *face = user_data;
36
37    if (active == face)
38        return;
39
40    gtk_widget_destroy(gtk_bin_get_child(GTK_BIN(canvas)));
41
42    if (face)
43        level(canvas, face);
44    else
45        overlap(canvas, &prj->s);
46    active = face;
47
48    gtk_widget_show_all(canvas);
49}
50
51
52static GtkWidget *gui_buttons(void)
53{
54    GtkWidget *vbox, *but;
55
56    vbox = gtk_vbox_new(FALSE, 0);
57
58    but = gtk_button_new_with_label("A");
59    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
60    g_signal_connect(G_OBJECT(but), "clicked",
61        G_CALLBACK(clicked), prj->s.a);
62
63    but = gtk_button_new_with_label("B");
64    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
65    g_signal_connect(G_OBJECT(but), "clicked",
66        G_CALLBACK(clicked), prj->s.b);
67
68    but = gtk_button_new_with_label("A+B");
69    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
70    g_signal_connect(G_OBJECT(but), "clicked",
71        G_CALLBACK(clicked), NULL);
72
73    return vbox;
74}
75
76
77static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event,
78    gpointer data)
79{
80    if (event->keyval == 'q')
81        gtk_main_quit();
82    return TRUE;
83}
84
85
86static void gui(void)
87{
88    GtkWidget *root, *hbox, *buttons;
89
90    root = gtk_window_new(GTK_WINDOW_TOPLEVEL);
91    gtk_window_set_position(GTK_WINDOW(root), GTK_WIN_POS_CENTER);
92
93    hbox = gtk_hbox_new(FALSE, 0);
94    gtk_container_add(GTK_CONTAINER(root), hbox);
95
96    canvas = gtk_event_box_new();
97    gtk_box_pack_start(GTK_BOX(hbox), canvas, FALSE, FALSE, 0);
98
99    /* initialize root->window */
100    gtk_widget_show_all(root);
101
102    buttons = gui_buttons();
103    gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
104
105    level(canvas, prj->s.a);
106    active = prj->s.a;
107
108    init_style(root->window);
109
110    gtk_widget_show_all(root);
111
112    g_signal_connect(G_OBJECT(root), "key-press-event",
113        G_CALLBACK(key_press_event), NULL);
114    g_signal_connect(G_OBJECT(root), "destroy",
115        G_CALLBACK(gtk_main_quit), NULL);
116
117    gtk_main();
118}
119
120
121static void usage(const char *name)
122{
123    fprintf(stderr, "usage: %s project [top bottom dist]\n", name);
124    exit(1);
125}
126
127
128int main(int argc, char **argv)
129{
130    double dist;
131
132    gtk_init(&argc, &argv);
133    setlocale(LC_ALL, "C"); /* damage control */
134
135    switch (argc) {
136    case 2:
137        prj = load_project(argv[1]);
138        break;
139    case 5:
140        dist = atof(argv[4]);
141        prj = new_project(argv[1], argv[2], argv[3], dist);
142        break;
143    default:
144        usage(*argv);
145    }
146
147    gui();
148
149    save_project(prj);
150
151    if (!isatty(1))
152        povray(&prj->s);
153
154    return 0;
155}
156

Archive Download this file

Branches:
master



interactive