* Fixing some memory leaks. A few minor ones in AWeather and a major one in RSL
[grits] / src / aweather-gui.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <config.h>
19 #include <gtk/gtk.h>
20 #include <gtk/gtkgl.h>
21 #include <gdk/gdkkeysyms.h>
22 #include <GL/gl.h>
23 #include <GL/glu.h>
24 #include <math.h>
25
26 #include "aweather-gui.h"
27 #include "aweather-view.h"
28 #include "location.h"
29
30 /*************
31  * callbacks *
32  *************/
33 gboolean on_window_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
34 {
35         if (event->keyval == GDK_q)
36                 gtk_main_quit();
37         return TRUE;
38 }
39
40 void on_site_changed(GtkComboBox *combo, AWeatherGui *gui)
41 {
42         gchar *site;
43         GtkTreeIter iter;
44         GtkTreeModel *model = gtk_combo_box_get_model(combo);
45         gtk_combo_box_get_active_iter(combo, &iter);
46         gtk_tree_model_get(model, &iter, 1, &site, -1);
47         AWeatherView *view = aweather_gui_get_view(gui);
48         aweather_view_set_location(view, site);
49 }
50
51 void on_time_changed(GtkTreeView *view, GtkTreePath *path,
52                 GtkTreeViewColumn *column, AWeatherGui *gui)
53 {
54         gchar *time;
55         GtkTreeIter iter;
56         GtkTreeModel *model = gtk_tree_view_get_model(view);
57         gtk_tree_model_get_iter(model, &iter, path);
58         gtk_tree_model_get(model, &iter, 0, &time, -1);
59         AWeatherView *aview = aweather_gui_get_view(gui);
60         aweather_view_set_time(aview, time);
61 }
62
63 static gboolean map(GtkWidget *da, GdkEventConfigure *event, AWeatherGui *gui)
64 {
65         //g_message("map:map");
66         AWeatherView *view = aweather_gui_get_view(gui);
67         aweather_view_set_location(view, "IND");
68
69         /* Misc */
70         glEnable(GL_BLEND);
71         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
72         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
73
74         /* Tessellation, "finding intersecting triangles" */
75         /* http://research.microsoft.com/pubs/70307/tr-2006-81.pdf */
76         /* http://www.opengl.org/wiki/Alpha_Blending */
77         glAlphaFunc(GL_GREATER,0.1);
78         glEnable(GL_ALPHA_TEST);
79
80         /* Depth test */
81         glClearDepth(1.0);
82         glDepthFunc(GL_LEQUAL);
83         glEnable(GL_DEPTH_TEST);
84
85         aweather_gui_gl_end(gui);
86         return FALSE;
87 }
88
89 static gboolean configure(GtkWidget *da, GdkEventConfigure *event, AWeatherGui *gui)
90 {
91         aweather_gui_gl_begin(gui);
92
93         double width  = da->allocation.width;
94         double height = da->allocation.height;
95         double dist   = 500*1000; // 500 km
96         double cam    = 300*1000; // 500 km
97
98         glViewport(0, 0, width, height);
99
100         /* Perspective */
101         glMatrixMode(GL_PROJECTION);
102         glLoadIdentity();
103         double rad = atan(height/2*1000.0/dist); // 1px = 1000 meters
104         double deg = (rad*180)/M_PI;
105         gluPerspective(deg*2, width/height, cam-20, cam+20);
106
107         /* Camera position? */
108         glMatrixMode(GL_MODELVIEW);
109         glLoadIdentity();
110         glTranslatef(0.0, 0.0, -cam);
111         //glRotatef(-45, 1, 0, 0);
112
113         aweather_gui_gl_end(gui);
114         return FALSE;
115 }
116
117 static gboolean expose_begin(GtkWidget *da, GdkEventExpose *event, AWeatherGui *gui)
118 {
119         aweather_gui_gl_begin(gui);
120         return FALSE;
121 }
122 static gboolean expose_end(GtkWidget *da, GdkEventExpose *event, AWeatherGui *gui)
123 {
124         g_message("aweather:espose_end\n");
125         aweather_gui_gl_end(gui);
126         aweather_gui_gl_flush(gui);
127         return FALSE;
128 }
129
130 /* TODO: replace the code in these with `gtk_tree_model_find' utility */
131 static void update_time_widget(AWeatherView *view, char *time, AWeatherGui *gui)
132 {
133         g_message("updating time widget");
134         GtkTreeView  *tview = GTK_TREE_VIEW(aweather_gui_get_widget(gui, "time"));
135         GtkTreeModel *model = GTK_TREE_MODEL(gtk_tree_view_get_model(tview));
136         for (int i = 0; i < gtk_tree_model_iter_n_children(model, NULL); i++) {
137                 char *text;
138                 GtkTreeIter iter;
139                 gtk_tree_model_iter_nth_child(model, &iter, NULL, i);
140                 gtk_tree_model_get(model, &iter, 0, &text, -1);
141                 if (g_str_equal(text, time)) {
142                         GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
143                         g_signal_handlers_block_by_func(tview, G_CALLBACK(on_site_changed), gui);
144                         gtk_tree_view_set_cursor(tview, path, NULL, FALSE);
145                         g_signal_handlers_unblock_by_func(tview, G_CALLBACK(on_site_changed), gui);
146                         return;
147                 }
148         }
149 }
150 static void update_location_widget(AWeatherView *view, char *location, AWeatherGui *gui)
151 {
152         g_message("updating location widget to %s", location);
153         GtkComboBox  *combo = GTK_COMBO_BOX(aweather_gui_get_widget(gui, "site"));
154         GtkTreeModel *model = GTK_TREE_MODEL(gtk_combo_box_get_model(combo));
155         for (int i = 0; i < gtk_tree_model_iter_n_children(model, NULL); i++) {
156                 GtkTreeIter iter1;
157                 gtk_tree_model_iter_nth_child(model, &iter1, NULL, i);
158                 for (int i = 0; i < gtk_tree_model_iter_n_children(model, &iter1); i++) {
159                         GtkTreeIter iter2;
160                         gtk_tree_model_iter_nth_child(model, &iter2, &iter1, i);
161                         char *text;
162                         gtk_tree_model_get(model, &iter2, 1, &text, -1);
163                         if (g_str_equal(text, location)) {
164                                 GtkTreePath *path = gtk_tree_model_get_path(model, &iter2);
165                                 g_signal_handlers_block_by_func(combo, G_CALLBACK(on_site_changed), gui);
166                                 gtk_combo_box_set_active_iter(combo, &iter2);
167                                 g_signal_handlers_unblock_by_func(combo, G_CALLBACK(on_site_changed), gui);
168                                 g_free(text);
169                                 return;
170                         }
171                         g_free(text);
172                 }
173         }
174 }
175
176 /*****************
177  * Setup helpers *
178  *****************/
179 static void combo_sensitive(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
180                 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
181 {
182         gboolean sensitive = !gtk_tree_model_iter_has_child(tree_model, iter);
183         g_object_set(cell, "sensitive", sensitive, NULL);
184 }
185
186 static void site_setup(AWeatherGui *gui)
187 {
188         GtkTreeIter state, city;
189         GtkTreeStore *store = gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
190         for (int i = 0; cities[i].label; i++) {
191                 if (cities[i].type == LOCATION_STATE) {
192                         gtk_tree_store_append(store, &state, NULL);
193                         gtk_tree_store_set   (store, &state, 0, cities[i].label, 
194                                                              1, cities[i].code,  -1);
195                 } else {
196                         gtk_tree_store_append(store, &city, &state);
197                         gtk_tree_store_set   (store, &city, 0, cities[i].label, 
198                                                             1, cities[i].code,  -1);
199                 }
200         }
201
202         GtkWidget       *combo    = aweather_gui_get_widget(gui, "site");
203         GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
204         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, FALSE);
205         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, "text", 0, NULL);
206         gtk_combo_box_set_model(GTK_COMBO_BOX(combo), GTK_TREE_MODEL(store));
207         gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), renderer,
208                         combo_sensitive, NULL, NULL);
209
210         g_signal_connect(combo, "changed", G_CALLBACK(on_site_changed),  gui);
211         AWeatherView *aview = aweather_gui_get_view(gui);
212         g_signal_connect(aview, "location-changed", G_CALLBACK(update_location_widget), gui);
213 }
214
215 static void time_setup(AWeatherGui *gui)
216 {
217         GtkTreeView  *tview  = GTK_TREE_VIEW(aweather_gui_get_widget(gui, "time"));
218         GtkTreeModel *store  = GTK_TREE_MODEL(gtk_list_store_new(1, G_TYPE_STRING));
219         gtk_tree_view_set_model(tview, store);
220
221         GtkCellRenderer   *rend = gtk_cell_renderer_text_new();
222         GtkTreeViewColumn *col  = gtk_tree_view_column_new_with_attributes(
223                                         "Time", rend, "text", 0, NULL);
224         gtk_tree_view_append_column(tview, col);
225
226         g_signal_connect(tview, "row-activated", G_CALLBACK(on_time_changed), gui);
227         AWeatherView *aview = aweather_gui_get_view(gui);
228         g_signal_connect(aview, "time-changed", G_CALLBACK(update_time_widget), gui);
229 }
230
231 gboolean opengl_setup(AWeatherGui *gui)
232 {
233         GtkDrawingArea *drawing = GTK_DRAWING_AREA(aweather_gui_get_widget(gui, "drawing"));
234
235         GdkGLConfig *glconfig = gdk_gl_config_new_by_mode(
236                         GDK_GL_MODE_RGBA   | GDK_GL_MODE_DEPTH |
237                         GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA);
238         if (!glconfig)
239                 g_error("Failed to create glconfig");
240         if (!gtk_widget_set_gl_capability(GTK_WIDGET(drawing), glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
241                 g_error("GL lacks required capabilities");
242
243         /* Set up OpenGL Stuff */
244         g_signal_connect      (drawing, "map-event",       G_CALLBACK(map),          gui);
245         g_signal_connect      (drawing, "configure-event", G_CALLBACK(configure),    gui);
246         g_signal_connect      (drawing, "expose-event",    G_CALLBACK(expose_begin), gui);
247         g_signal_connect_after(drawing, "expose-event",    G_CALLBACK(expose_end),   gui);
248         return TRUE;
249 }
250
251
252 /****************
253  * GObject code *
254  ****************/
255 G_DEFINE_TYPE(AWeatherGui, aweather_gui, G_TYPE_OBJECT);
256
257 /* Constructor / destructors */
258 static void aweather_gui_init(AWeatherGui *gui)
259 {
260         //g_message("aweather_gui_init");
261 }
262
263 static GObject *aweather_gui_constructor(GType gtype, guint n_properties,
264                 GObjectConstructParam *properties)
265 {
266         //g_message("aweather_gui_constructor");
267         GObjectClass *parent_class = G_OBJECT_CLASS(aweather_gui_parent_class);
268         return  parent_class->constructor(gtype, n_properties, properties);
269 }
270
271 static void aweather_gui_dispose (GObject *gobject)
272 {
273         //g_message("aweather_gui_dispose");
274         AWeatherGui *gui = AWEATHER_GUI(gobject);
275         g_object_unref(gui->view   );
276         g_object_unref(gui->builder);
277         g_object_unref(gui->window );
278         g_object_unref(gui->tabs   );
279         g_object_unref(gui->drawing);
280         G_OBJECT_CLASS(aweather_gui_parent_class)->dispose(gobject);
281 }
282
283 static void aweather_gui_finalize(GObject *gobject)
284 {
285         //g_message("aweather_gui_finalize");
286         G_OBJECT_CLASS(aweather_gui_parent_class)->finalize(gobject);
287 }
288
289 static void aweather_gui_class_init(AWeatherGuiClass *klass)
290 {
291         //g_message("aweather_gui_class_init");
292         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
293         gobject_class->constructor  = aweather_gui_constructor;
294         gobject_class->dispose      = aweather_gui_dispose;
295         gobject_class->finalize     = aweather_gui_finalize;
296 }
297
298 /* Methods */
299 AWeatherGui *aweather_gui_new()
300 {
301         //g_message("aweather_gui_new");
302         GError *error = NULL;
303
304         AWeatherGui *gui = g_object_new(AWEATHER_TYPE_GUI, NULL);
305
306         gui->builder = gtk_builder_new();
307         if (!gtk_builder_add_from_file(gui->builder, DATADIR "/aweather/aweather.xml", &error))
308                 g_error("Failed to create gtk builder: %s", error->message);
309         gui->view    = aweather_view_new();
310         gui->window  = GTK_WINDOW      (gtk_builder_get_object(gui->builder, "window"));
311         gui->tabs    = GTK_NOTEBOOK    (gtk_builder_get_object(gui->builder, "tabs")); 
312         gui->drawing = GTK_DRAWING_AREA(gtk_builder_get_object(gui->builder, "drawing"));
313         gtk_builder_connect_signals(gui->builder, NULL);
314
315         /* Load components */
316         site_setup(gui);
317         time_setup(gui);
318         opengl_setup(gui);
319         return gui;
320 }
321 AWeatherView *aweather_gui_get_view(AWeatherGui *gui)
322 {
323         g_assert(AWEATHER_IS_GUI(gui));
324         return gui->view;
325 }
326 GtkBuilder *aweather_gui_get_builder(AWeatherGui *gui)
327 {
328         g_assert(AWEATHER_IS_GUI(gui));
329         return gui->builder;
330 }
331 GtkWidget *aweather_gui_get_widget(AWeatherGui *gui, const gchar *name)
332 {
333         g_assert(AWEATHER_IS_GUI(gui));
334         GObject *widget = gtk_builder_get_object(gui->builder, name);
335         g_assert(GTK_IS_WIDGET(widget));
336         return GTK_WIDGET(widget);
337 }
338 void aweather_gui_gl_begin(AWeatherGui *gui)
339 {
340         g_assert(AWEATHER_IS_GUI(gui));
341
342         GtkDrawingArea *drawing    = GTK_DRAWING_AREA(aweather_gui_get_widget(gui, "drawing"));
343         GdkGLContext   *glcontext  = gtk_widget_get_gl_context(GTK_WIDGET(drawing));
344         GdkGLDrawable  *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(drawing));
345
346         if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
347                 g_assert_not_reached();
348
349         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
350 }
351 void aweather_gui_gl_end(AWeatherGui *gui)
352 {
353         g_assert(AWEATHER_IS_GUI(gui));
354         GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
355         gdk_gl_drawable_gl_end(gldrawable);
356 }
357 void aweather_gui_gl_flush(AWeatherGui *gui)
358 {
359         g_assert(AWEATHER_IS_GUI(gui));
360         GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
361         if (gdk_gl_drawable_is_double_buffered(gldrawable))
362                 gdk_gl_drawable_swap_buffers(gldrawable);
363         else
364                 glFlush();
365         gdk_gl_drawable_gl_end(gldrawable);
366 }
367 //void aweather_gui_register_plugin(AWeatherGui *gui, AWeatherPlugin *plugin);