Statistics
| Revision:

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

History | View | Annotate | Download (3.33 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 RESUME 3
13

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

    
17
void destroy_callback(int arg)
18
{
19
        robots_cleanup();
20
        gtk_main_quit();
21
}
22

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

    
38
        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
39
        {
40
                char *filename;
41
                filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
42
                robot_create(filename);
43
                g_free (filename);
44
        }
45
        gtk_widget_destroy (dialog);
46
        
47
        gdk_threads_leave();
48
        robots_resume();
49
        gdk_threads_enter();
50
}
51

    
52
static gboolean menuhandler(int type)
53
{
54
        switch (type)
55
        {
56
                case NEW_ROBOT:
57
                        make_new_robot();
58
                        break;
59
                case PAUSE:
60
                        gdk_threads_leave();
61
                        robots_pause();
62
                        gdk_threads_enter();
63
                        break;
64
                case RESUME:
65
                        gdk_threads_leave();
66
                        robots_resume();
67
                        gdk_threads_enter();
68
                        break;
69
        }
70

    
71
        return TRUE;
72
}
73

    
74
GtkWidget* make_menu_bar(GtkWidget* window, GtkWidget* other)
75
{
76
        GtkWidget* menu_bar;
77
        GtkWidget* file_item;
78
        GtkWidget* file_menu;
79
        GtkWidget* menu_item;
80
        GtkWidget* vbox;
81

    
82
        file_menu = gtk_menu_new();
83
        gtk_widget_show(file_menu);
84

    
85
        menu_item = gtk_menu_item_new_with_label("New Robot");
86
        gtk_widget_show(menu_item);
87
        gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), menu_item);
88
        g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
89
                G_CALLBACK(menuhandler), (gint*)NEW_ROBOT);
90
        
91
        menu_item = gtk_menu_item_new_with_label("Pause");
92
        gtk_widget_show(menu_item);
93
        gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), menu_item);
94
        g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
95
                G_CALLBACK(menuhandler), (gint*)PAUSE);
96
        
97
        menu_item = gtk_menu_item_new_with_label("Resume");
98
        gtk_widget_show(menu_item);
99
        gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), menu_item);
100
        g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
101
                G_CALLBACK(menuhandler), (gint*)RESUME);
102

    
103
        file_item = gtk_menu_item_new_with_label("File");
104
        gtk_widget_show(file_item);
105
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(file_item), file_menu);
106
        menu_bar = gtk_menu_bar_new();
107

    
108
        vbox = gtk_vbox_new (FALSE, 0);
109
        gtk_container_add (GTK_CONTAINER (window), vbox);
110
        gtk_widget_show (vbox);
111
        gtk_box_pack_start (GTK_BOX (vbox), menu_bar, FALSE, FALSE, 2);
112
        gtk_widget_show(menu_bar);
113

    
114
        gtk_box_pack_end (GTK_BOX (vbox), other, TRUE, TRUE, 2);
115
        gtk_widget_show(other);
116

    
117
        gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), file_item);
118

    
119
        return menu_bar;
120
}
121

    
122
int gtk_gui_run(int argc, char** argv)
123
{
124
        gdk_threads_init();
125
        gdk_threads_enter();
126
        gtk_init(&argc, &argv);
127

    
128
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
129
        gtk_window_set_title(GTK_WINDOW(window), "Colony Simulator");
130
        gtk_window_maximize(GTK_WINDOW(window));
131
        gtk_container_set_border_width(GTK_CONTAINER(window), 0);
132
        g_signal_connect(G_OBJECT(window), "destroy",
133
                        G_CALLBACK(destroy_callback), NULL);
134

    
135
        view = gtk_environment_view_new();
136

    
137
        make_menu_bar(window, view);
138
        gtk_widget_show(window);
139

    
140
        gtk_main();
141
        gdk_threads_leave();
142

    
143
        return 0;
144
}
145

    
146
void gui_refresh()
147
{
148
        if (view == NULL)
149
                return;
150
        gtk_environment_view_refresh(view);
151
}
152