Statistics
| Revision:

root / branches / simulator / projects / simulator / simulator / gui / gtk_gui.c @ 1082

History | View | Annotate | Download (3.49 KB)

1
#include <stdlib.h>
2
#include <gtk/gtk.h>
3
#include <glib.h>
4

    
5
#include "gtk_gui.h"
6
#include "gtk_environment_view.h"
7

    
8
#include "robot.h"
9

    
10
#define NEW_ROBOT        1
11
#define PAUSE                2
12
#define QUIT                3
13

    
14
GtkWidget* window;
15
GtkWidget* view = NULL;
16

    
17
int robots_paused = 0;
18

    
19
void destroy_callback(int arg)
20
{
21
        robots_cleanup();
22
        gtk_main_quit();
23
}
24

    
25
void make_new_robot(void)
26
{
27
        GtkWidget *dialog;
28
        
29
        gdk_threads_leave();
30
        robots_pause();
31
        gdk_threads_enter();
32
        
33
        dialog = gtk_file_chooser_dialog_new ("Open File",
34
                GTK_WINDOW(window),
35
                GTK_FILE_CHOOSER_ACTION_OPEN,
36
                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
37
                GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
38
                NULL);
39

    
40
        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
41
        {
42
                char *filename;
43
                filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
44
                robot_create(filename);
45
                g_free (filename);
46
        }
47
        gtk_widget_destroy (dialog);
48
        
49
        if (!robots_paused)
50
        {
51
                gdk_threads_leave();
52
                robots_resume();
53
                gdk_threads_enter();
54
        }
55
}
56

    
57
static gboolean menuhandler(int type)
58
{
59
        switch (type)
60
        {
61
                case NEW_ROBOT:
62
                        make_new_robot();
63
                        break;
64
                case PAUSE:
65
                        gdk_threads_leave();
66
                        if (robots_paused)
67
                                robots_resume();
68
                        else
69
                                robots_pause();
70
                        robots_paused = !robots_paused;
71
                        gdk_threads_enter();
72
                        break;
73
                case QUIT:
74
                        destroy_callback(0);
75
                        break;
76
        }
77

    
78
        return TRUE;
79
}
80

    
81
GtkWidget* make_menu_bar(GtkWidget* window, GtkWidget* other)
82
{
83
        GtkWidget* menu_bar;
84
        GtkWidget* file_item;
85
        GtkWidget* file_menu;
86
        GtkWidget* menu_item;
87
        GtkWidget* vbox;
88

    
89
        file_menu = gtk_menu_new();
90
        gtk_widget_show(file_menu);
91

    
92
        menu_item = gtk_menu_item_new_with_label("New Robot");
93
        gtk_widget_show(menu_item);
94
        gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), menu_item);
95
        g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
96
                G_CALLBACK(menuhandler), (gint*)NEW_ROBOT);
97
        
98
        menu_item = gtk_check_menu_item_new_with_label("Paused");
99
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), 0);
100
        gtk_widget_show(menu_item);
101
        gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), menu_item);
102
        g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
103
                G_CALLBACK(menuhandler), (gint*)PAUSE);
104
        
105
        menu_item = gtk_menu_item_new_with_label("Quit");
106
        gtk_widget_show(menu_item);
107
        gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), menu_item);
108
        g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
109
                G_CALLBACK(menuhandler), (gint*)QUIT);
110
        
111
        file_item = gtk_menu_item_new_with_label("File");
112
        gtk_widget_show(file_item);
113
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(file_item), file_menu);
114
        menu_bar = gtk_menu_bar_new();
115

    
116
        vbox = gtk_vbox_new (FALSE, 0);
117
        gtk_container_add (GTK_CONTAINER (window), vbox);
118
        gtk_widget_show (vbox);
119
        gtk_box_pack_start (GTK_BOX (vbox), menu_bar, FALSE, FALSE, 2);
120
        gtk_widget_show(menu_bar);
121

    
122
        gtk_box_pack_end (GTK_BOX (vbox), other, TRUE, TRUE, 2);
123
        gtk_widget_show(other);
124

    
125
        gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), file_item);
126

    
127
        return menu_bar;
128
}
129

    
130
int gtk_gui_run(int argc, char** argv)
131
{
132
        gdk_threads_init();
133
        gdk_threads_enter();
134
        gtk_init(&argc, &argv);
135

    
136
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
137
        gtk_window_set_title(GTK_WINDOW(window), "Colony Simulator");
138
        gtk_window_maximize(GTK_WINDOW(window));
139
        gtk_container_set_border_width(GTK_CONTAINER(window), 0);
140
        g_signal_connect(G_OBJECT(window), "destroy",
141
                        G_CALLBACK(destroy_callback), NULL);
142

    
143
        view = gtk_environment_view_new();
144

    
145
        make_menu_bar(window, view);
146
        gtk_widget_show(window);
147

    
148
        gtk_main();
149
        gdk_threads_leave();
150

    
151
        return 0;
152
}
153

    
154
void gui_refresh()
155
{
156
        if (view == NULL)
157
                return;
158
        gtk_environment_view_refresh(view);
159
}
160