Statistics
| Revision:

root / branches / simulator / projects / simulator / simulator / gui / gtk_environment_view.c @ 976

History | View | Annotate | Download (9.11 KB)

1
#include <math.h>
2
#include <stdlib.h>
3
#include <stdio.h>
4
#include <sys/time.h>
5
#include <string.h>
6

    
7
#include <gtk/gtkmain.h>
8
#include <gtk/gtksignal.h>
9

    
10
#include "gtk_environment_view.h"
11

    
12
#define ENVIRONMENT_VIEW_MIN_WIDTH                100
13
#define ENVIRONMENT_VIEW_MIN_HEIGHT                50
14

    
15
static GtkWidgetClass* parent_class = NULL;
16

    
17
static void gtk_environment_view_class_init(GtkEnvironmentViewClass* environmentClass);
18
static void gtk_environment_view_init(GtkEnvironmentView* view);
19
static void gtk_environment_view_destroy(GtkObject* object);
20

    
21
static void gtk_environment_view_size_request(GtkWidget* widget,
22
                GtkRequisition* req);
23
static void gtk_environment_view_realize(GtkWidget* widget);
24
static void gtk_environment_view_size_allocate(GtkWidget* widget, GtkAllocation* a);
25
static gboolean gtk_environment_view_expose(GtkWidget* widget,
26
                GdkEventExpose* event);
27

    
28
static gboolean gtk_environment_view_mouse_down(GtkWidget* widget,
29
                                GdkEventButton* event);
30
static gboolean gtk_environment_view_mouse_release(GtkWidget* widget,
31
                                GdkEventButton* event);
32
static gboolean gtk_environment_view_mouse_move(GtkWidget* widget,
33
                                GdkEventMotion* event);
34

    
35
GtkType gtk_environment_view_get_type(void)
36
{
37
        static GtkType environment_view_type = 0;
38
        if (!environment_view_type)
39
        {
40
                static const GtkTypeInfo environment_view_info =
41
                {
42
                        "GtkEnvironmentView",
43
                        sizeof(GtkEnvironmentView),
44
                        sizeof(GtkEnvironmentViewClass),
45
                        (GtkClassInitFunc)gtk_environment_view_class_init,
46
                        (GtkObjectInitFunc)gtk_environment_view_init,
47
                        NULL, NULL, (GtkClassInitFunc)NULL
48
                };
49

    
50
                environment_view_type = gtk_type_unique(GTK_TYPE_WIDGET,
51
                        &environment_view_info);
52
        }
53

    
54
        return environment_view_type;
55
}
56

    
57
static void gtk_environment_view_class_init(GtkEnvironmentViewClass* environmentClass)
58
{
59
        GtkObjectClass* object_class;
60
        GtkWidgetClass* widget_class;
61

    
62
        object_class = (GtkObjectClass*)environmentClass;
63
        widget_class = (GtkWidgetClass*)environmentClass;
64

    
65
        parent_class = gtk_type_class(gtk_widget_get_type());
66

    
67
        object_class->destroy = gtk_environment_view_destroy;
68

    
69
        widget_class->realize = gtk_environment_view_realize;
70
        widget_class->expose_event = gtk_environment_view_expose;
71
        widget_class->size_request = gtk_environment_view_size_request;
72
        widget_class->size_allocate = gtk_environment_view_size_allocate;
73
        widget_class->button_press_event = gtk_environment_view_mouse_down;
74
        widget_class->button_release_event = gtk_environment_view_mouse_release;
75
        widget_class->motion_notify_event = gtk_environment_view_mouse_move;
76
}
77

    
78
GtkWidget* gtk_environment_view_new(void)
79
{
80
        GtkEnvironmentView* view;
81
        view = gtk_type_new(gtk_environment_view_get_type());
82

    
83
        return GTK_WIDGET(view);
84
}
85

    
86
static void gtk_environment_view_init(GtkEnvironmentView* view)
87
{
88
        view->topLeftX = -2.0;
89
        view->topLeftY = -1.3;
90
        view->scale = 2.0 / 800.0;
91
        view->width = -1;
92
        view->height = -1;
93

    
94
        GTK_WIDGET_SET_FLAGS(&(view->widget), GTK_DOUBLE_BUFFERED);
95

    
96
        view->mouseDown = 0;
97
}
98

    
99
static void gtk_environment_view_destroy(GtkObject* object)
100
{
101
        GtkEnvironmentView* view;
102
        g_return_if_fail(object != NULL);
103
        g_return_if_fail(GTK_IS_ENVIRONMENT_VIEW(object));
104

    
105
        view = GTK_ENVIRONMENT_VIEW(object);
106

    
107
        if (GTK_OBJECT_CLASS(parent_class)->destroy)
108
                (* GTK_OBJECT_CLASS(parent_class)->destroy) (object);
109
}
110

    
111
static void gtk_environment_view_realize(GtkWidget* widget)
112
{
113
        GtkEnvironmentView* view;
114
        GdkWindowAttr attributes;
115
        gint attributes_mask;
116

    
117
        g_return_if_fail( widget != NULL);
118
        g_return_if_fail( GTK_IS_ENVIRONMENT_VIEW(widget));
119

    
120
        GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
121
        view = GTK_ENVIRONMENT_VIEW(widget);
122

    
123
        attributes.x = widget->allocation.x;
124
        attributes.y = widget->allocation.y;
125
        attributes.width = widget->allocation.width;
126
        attributes.height = widget->allocation.height;
127
        attributes.wclass = GDK_INPUT_OUTPUT;
128
        attributes.window_type = GDK_WINDOW_CHILD;
129
        attributes.event_mask = gtk_widget_get_events(widget) |
130
                GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK |
131
                GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK |
132
                GDK_POINTER_MOTION_HINT_MASK;
133
        attributes.visual = gtk_widget_get_visual(widget);
134
        attributes.colormap = gtk_widget_get_colormap(widget);
135

    
136
        attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
137
        widget->window = gdk_window_new(widget->parent->window, &attributes,
138
                        attributes_mask);
139
        gdk_window_set_user_data(widget->window, widget);
140
        widget->style = gtk_style_attach(widget->style, widget->window);
141
        gtk_style_set_background(widget->style, widget->window,
142
                        GTK_STATE_NORMAL);
143
}
144

    
145
static void gtk_environment_view_size_request(GtkWidget* widget, 
146
                GtkRequisition* req)
147
{
148
        if (req->width < ENVIRONMENT_VIEW_MIN_WIDTH)
149
                req->width = ENVIRONMENT_VIEW_MIN_WIDTH;
150
        if (req->height < ENVIRONMENT_VIEW_MIN_HEIGHT)
151
                req->height = ENVIRONMENT_VIEW_MIN_HEIGHT;
152
}
153

    
154
static void gtk_environment_view_size_allocate(GtkWidget* widget, GtkAllocation* a)
155
{
156
        GtkEnvironmentView* view;
157

    
158
        g_return_if_fail(widget != NULL);
159
        g_return_if_fail(GTK_IS_ENVIRONMENT_VIEW(widget));
160
        g_return_if_fail(a != NULL);
161

    
162
        view = GTK_ENVIRONMENT_VIEW(widget);
163
        widget->allocation = *a;
164

    
165
        view->width = a->width;
166
        view->height = a->height;
167

    
168
        if (GTK_WIDGET_REALIZED(widget))
169
        {
170
                gdk_window_move_resize(widget->window, a->x, a->y, a->width, a->height);
171
        }
172
}
173

    
174
static gboolean gtk_environment_view_expose(GtkWidget* widget,
175
                GdkEventExpose* event)
176
{
177
        GtkEnvironmentView* view;
178

    
179
        if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL)
180
                return FALSE;
181
        if (event->count > 0)
182
                return FALSE;
183
        
184
        view = GTK_ENVIRONMENT_VIEW(widget);
185
        
186
        gdk_draw_rectangle(widget->window, widget->style->fg_gc[
187
                        GTK_WIDGET_STATE(widget)], 0, 100, 100, 100, 100);
188

    
189
        gdk_draw_arc(widget->window, 
190
                widget->style->fg_gc[GTK_WIDGET_STATE(widget)], FALSE, 0, 0,
191
                100, 100, 0, 360*64);
192

    
193
        return FALSE;
194
}
195

    
196
static gboolean gtk_environment_view_mouse_down(GtkWidget* widget,
197
                                GdkEventButton* event)
198
{
199
        GtkEnvironmentView* view;
200

    
201
        if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL)
202
                return FALSE;
203

    
204
        view = GTK_ENVIRONMENT_VIEW(widget);
205

    
206
        if (event->button != 1)
207
                return FALSE;
208

    
209
        view->mouseDownX = event->x;
210
        view->mouseDownY = event->y;
211
        view->mouseX = event->x;
212
        view->mouseY = event->y;
213
        view->mouseDown = 1;
214

    
215
        return FALSE;
216
}
217

    
218
static gboolean gtk_environment_view_mouse_release(GtkWidget* widget,
219
                                GdkEventButton* event)
220
{
221
        GtkEnvironmentView* view;
222
        double x1, x2, y1, y2;
223
        int w, h;
224

    
225
        if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL)
226
                return FALSE;
227

    
228
        view = GTK_ENVIRONMENT_VIEW(widget);
229

    
230
        if (event->button != 1 && event->button != 3)
231
                return FALSE;
232
        
233
        if (event->button == 1)
234
                view->mouseDown = 0;
235
        view->mouseX = event->x;
236
        view->mouseY = event->y;
237
        w = abs(view->mouseX - view->mouseDownX);
238
        h = abs(view->mouseY - view->mouseDownY);
239
        
240
        // if the box is big, zoom to the box
241
        if (event->button == 1 && (w >= 10 || h >= 10))
242
        {
243
                // we need to make sure the box is to scale
244
                // scale to height
245
                if (fabs((double)w / h) <= fabs((double)view->width / view->height))
246
                {
247
                        w = (int)((double)view->width / view->height * h);
248
                        if (view->mouseX < view->mouseDownX)
249
                                view->mouseX = view->mouseDownX - w;
250
                        else
251
                                view->mouseX = view->mouseDownX + w;
252
                }
253
                // scale to width
254
                else
255
                {
256
                        h = (int)((double)view->height / view->width * w);
257
                        if (view->mouseY < view->mouseDownY)
258
                                view->mouseY = view->mouseDownY - h;
259
                        else
260
                                view->mouseY = view->mouseDownY + h;
261
                }
262
        
263
                x1 = view->mouseX * view->scale + view->topLeftX;
264
                y1 = view->mouseY * view->scale + view->topLeftY;
265
                x2 = view->mouseDownX * view->scale + view->topLeftX;
266
                y2 = view->mouseDownY * view->scale + view->topLeftY;
267
                if (x2 < x1)
268
                        x1 = x2;
269
                if (y2 < y1)
270
                        y1 = y2;
271

    
272
                view->scale = (w * view->scale) / view->width;
273
                view->topLeftX = x1;
274
                view->topLeftY = y1;
275
        }
276
        else
277
        {
278
                x1 = view->mouseX * view->scale + view->topLeftX;
279
                y1 = view->mouseY * view->scale + view->topLeftY;
280

    
281
                // zoom in on left click, out on right click
282
                if (event->button == 1)
283
                        view->scale /= 2.0;
284
                else
285
                        view->scale *= 2.0;
286
                view->topLeftX = x1 - view->width / 2 * view->scale;
287
                view->topLeftY = y1 - view->height / 2 * view->scale;
288
        }
289

    
290
        return FALSE;
291
}
292

    
293
static gboolean gtk_environment_view_mouse_move(GtkWidget* widget,
294
                                GdkEventMotion* event)
295
{
296
        GtkEnvironmentView* view;
297

    
298
        if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL)
299
                return FALSE;
300

    
301
        view = GTK_ENVIRONMENT_VIEW(widget);
302

    
303
        view->mouseX = event->x;
304
        view->mouseY = event->y;
305

    
306
        if (!view->mouseDown)
307
                return FALSE;
308

    
309
        // we need to make sure the new view is to scale
310
        int w = abs(view->mouseX - view->mouseDownX);
311
        int h = abs(view->mouseY - view->mouseDownY);
312
        // scale to height
313
        if (fabs((double)w / h) <= fabs((double)view->width / view->height))
314
        {
315
                w = (int)((double)view->width / view->height * h);
316
                if (view->mouseX < view->mouseDownX)
317
                        view->mouseX = view->mouseDownX - w;
318
                else
319
                        view->mouseX = view->mouseDownX + w;
320
        }
321
        // scale to width
322
        else
323
        {
324
                h = (int)((double)view->height / view->width * w);
325
                if (view->mouseY < view->mouseDownY)
326
                        view->mouseY = view->mouseDownY - h;
327
                else
328
                        view->mouseY = view->mouseDownY + h;
329
        }
330

    
331
        gtk_widget_queue_draw_area(widget, 0, 0, view->width, view->height);
332

    
333
        return FALSE;
334
}
335

    
336
        /*gdk_threads_enter();
337
        gtk_widget_queue_draw_area(widget, 0, row, view->width, 1);
338
        gdk_threads_leave();*/
339