Statistics
| Revision:

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

History | View | Annotate | Download (3.27 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
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
        gdk_threads_leave();
50
        robots_resume();
51
        gdk_threads_enter();
52
}
53

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

    
77
        return TRUE;
78
}
79

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

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

    
91
        menu_item = gtk_menu_item_new_with_label("New Robot");
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*)NEW_ROBOT);
96
        
97
        menu_item = gtk_check_menu_item_new_with_label("Paused");
98
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), 0);
99
        gtk_widget_show(menu_item);
100
        gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), menu_item);
101
        g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
102
                G_CALLBACK(menuhandler), (gint*)PAUSE);
103
        
104
        file_item = gtk_menu_item_new_with_label("File");
105
        gtk_widget_show(file_item);
106
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(file_item), file_menu);
107
        menu_bar = gtk_menu_bar_new();
108

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

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

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

    
120
        return menu_bar;
121
}
122

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

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

    
136
        view = gtk_environment_view_new();
137

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

    
141
        gtk_main();
142
        gdk_threads_leave();
143

    
144
        return 0;
145
}
146

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