Date:2010-09-24 04:18:46 (8 years 10 months ago)
Author:Werner Almesberger
Commit:b8bba1d3abe4db362c457a9eeb849fdaaaad1cc5
Message:The GUI can now switch among operations.

- solidify/level.c (key_press_event, make_screen), solidify/solidify.c
(key_press_event): moved "q" handler to solidify.c
- solidify/level.h (level), solidify/level.c (make_screen, level): removed
root window initialization and merged remainder into "level"
- solidify/overlap.h (overlap), solidify/overlap.c (make_screen, overlap):
removed root window initialization and merged remainder into "overlap"
- solidify/solidify.c (gui): initialize the root window
- solidify/solidify.c (gui, gui_buttons, clicked): added buttons to select
operation
Files: solidify/level.c (1 diff)
solidify/level.h (1 diff)
solidify/overlap.c (1 diff)
solidify/overlap.h (1 diff)
solidify/solidify.c (3 diffs)

Change Details

solidify/level.c
131131}
132132
133133
134static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event,
135    gpointer data)
136{
137    if (event->keyval == 'q')
138        gtk_main_quit();
139    return TRUE;
140}
141
142
143static void make_screen(GtkWidget *root, struct face *f)
134void level(GtkWidget *canvas, struct face *f)
144135{
145136    GtkWidget *evbox, *da;
146137
138    evbox = gtk_event_box_new();
147139    da = gtk_drawing_area_new();
148140    gtk_widget_set_size_request(da, f->sx, f->sy);
149    evbox = gtk_event_box_new();
141    gtk_container_add(GTK_CONTAINER(canvas), evbox);
150142    gtk_container_add(GTK_CONTAINER(evbox), da);
151    gtk_container_add(GTK_CONTAINER(root), evbox);
152    gtk_widget_show_all(root);
143
153144    draw_image(da, f);
154145
155146    g_signal_connect(G_OBJECT(evbox), "scroll-event",
156147        G_CALLBACK(scroll_event), f);
157148    g_signal_connect(G_OBJECT(da), "expose-event",
158149        G_CALLBACK(expose_event), f);
159    g_signal_connect(G_OBJECT(root), "key-press-event",
160        G_CALLBACK(key_press_event), NULL);
161}
162
163
164void level(struct face *f)
165{
166    GtkWidget *root;
167
168    root = gtk_window_new(GTK_WINDOW_TOPLEVEL);
169    gtk_window_set_position(GTK_WINDOW(root), GTK_WIN_POS_CENTER);
170
171    g_signal_connect(G_OBJECT(root), "destroy",
172        G_CALLBACK(gtk_main_quit), NULL);
173    make_screen(root, f);
174    gtk_main();
175150}
solidify/level.h
1313#ifndef LEVEL_H
1414#define LEVEL_H
1515
16#include <gtk/gtk.h>
17
1618#include "face.h"
1719
1820
19void level(struct face *f);
21void level(GtkWidget *canvas, struct face *f);
2022
2123#endif /* LEVEL_H */
solidify/overlap.c
156156
157157
158158
159static void make_screen(GtkWidget *root, struct face *f)
159void overlap(GtkWidget *canvas, struct face *f)
160160{
161161    GtkWidget *evbox, *da;
162162
163    evbox = gtk_event_box_new();
163164    da = gtk_drawing_area_new();
164165    gtk_widget_set_size_request(da, f->sx, f->sy);
165    evbox = gtk_event_box_new();
166    gtk_container_add(GTK_CONTAINER(canvas), evbox);
166167    gtk_container_add(GTK_CONTAINER(evbox), da);
167    gtk_container_add(GTK_CONTAINER(root), evbox);
168    gtk_widget_show_all(root);
168
169169    draw_image(da, f);
170170
171171    g_signal_connect(G_OBJECT(evbox), "scroll-event",
172172        G_CALLBACK(scroll_event), f);
173173    g_signal_connect(G_OBJECT(da), "expose-event",
174174        G_CALLBACK(expose_event), f);
175}
176
177void overlap(struct face *face)
178{
179    GtkWidget *root;
180
181    root = gtk_window_new(GTK_WINDOW_TOPLEVEL);
182    gtk_window_set_position(GTK_WINDOW(root), GTK_WIN_POS_CENTER);
183175
184    g_signal_connect(G_OBJECT(root), "destroy",
185        G_CALLBACK(gtk_main_quit), NULL);
186    make_screen(root, face);
187    gtk_main();
176    return evbox;
188177}
solidify/overlap.h
1010 * (at your option) any later version.
1111 */
1212
13
1413#ifndef OVERLAP_H
1514#define OVERLAP_H
1615
16#include <gtk/gtk.h>
1717
18void overlap(struct face *face);
1918
19void overlap(GtkWidget *canvas, struct face *face);
2020
2121#endif /* !OVERLAP_H */
solidify/solidify.c
1818
1919#include "face.h"
2020#include "level.h"
21#include "overlap.h"
22
23
24static struct face *face_a, *face_b;
25static const struct face *active;
26static GtkWidget *canvas;
27
28
29static void clicked(GtkButton *button, gpointer user_data)
30{
31    struct face *face = user_data;
32
33    if (active == face)
34        return;
35
36    gtk_widget_destroy(gtk_bin_get_child(GTK_BIN(canvas)));
37
38    if (face)
39        level(canvas, face);
40    else
41        overlap(canvas, face_a);
42    active = face;
43
44    gtk_widget_show_all(canvas);
45}
46
47
48static GtkWidget *gui_buttons(void)
49{
50    GtkWidget *vbox, *but;
51
52    vbox = gtk_vbox_new(FALSE, 0);
53
54    but = gtk_button_new_with_label("A");
55    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
56    g_signal_connect(G_OBJECT(but), "clicked",
57        G_CALLBACK(clicked), face_a);
58
59    but = gtk_button_new_with_label("B");
60    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
61    g_signal_connect(G_OBJECT(but), "clicked",
62        G_CALLBACK(clicked), face_b);
63
64    but = gtk_button_new_with_label("A+B");
65    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
66    g_signal_connect(G_OBJECT(but), "clicked",
67        G_CALLBACK(clicked), NULL);
68
69    return vbox;
70}
71
72
73static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event,
74    gpointer data)
75{
76    if (event->keyval == 'q')
77        gtk_main_quit();
78    return TRUE;
79}
80
81
82static void gui(void)
83{
84    GtkWidget *root, *hbox, *buttons;
85
86    root = gtk_window_new(GTK_WINDOW_TOPLEVEL);
87    gtk_window_set_position(GTK_WINDOW(root), GTK_WIN_POS_CENTER);
88
89    hbox = gtk_hbox_new(FALSE, 0);
90    gtk_container_add(GTK_CONTAINER(root), hbox);
91
92    canvas = gtk_event_box_new();
93    gtk_box_pack_start(GTK_BOX(hbox), canvas, FALSE, FALSE, 0);
94
95    /* initialize root->window */
96    gtk_widget_show_all(root);
97
98    buttons = gui_buttons();
99    gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
100
101    level(canvas, face_a);
102    active = face_a;
103
104    gtk_widget_show_all(root);
105
106    g_signal_connect(G_OBJECT(root), "key-press-event",
107        G_CALLBACK(key_press_event), NULL);
108    g_signal_connect(G_OBJECT(root), "destroy",
109        G_CALLBACK(gtk_main_quit), NULL);
110
111    gtk_main();
112}
21113
22114
23115static void usage(const char *name)
...... 
29121
30122int main(int argc, char **argv)
31123{
32    struct face *top;
33
34124    gtk_init(&argc, &argv);
35125    switch (argc) {
36126    case 2:
...... 
39129        usage(*argv);
40130    }
41131    setlocale(LC_ALL, "C"); /* damage control */
42    top = read_face(argv[1]);
43    level(top);
132    face_a = read_face(argv[1]);
133    face_b = face_a;
134    gui();
44135
45136    return 0;
46137}

Archive Download the corresponding diff file

Branches:
master



interactive