2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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.
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.
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/>.
20 #include <gtk/gtkgl.h>
21 #include <gdk/gdkkeysyms.h>
26 #include "aweather-gui.h"
27 #include "aweather-view.h"
33 gboolean on_window_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
35 if (event->keyval == GDK_q)
40 void on_site_changed(GtkComboBox *combo, AWeatherGui *gui)
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);
51 void on_time_changed(GtkTreeView *view, GtkTreePath *path,
52 GtkTreeViewColumn *column, AWeatherGui *gui)
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);
63 static gboolean map(GtkWidget *da, GdkEventConfigure *event, AWeatherGui *gui)
65 //g_message("map:map");
66 AWeatherView *view = aweather_gui_get_view(gui);
67 aweather_view_set_location(view, "IND");
71 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
72 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
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);
82 glDepthFunc(GL_LEQUAL);
83 glEnable(GL_DEPTH_TEST);
85 aweather_gui_gl_end(gui);
89 static gboolean configure(GtkWidget *da, GdkEventConfigure *event, AWeatherGui *gui)
91 aweather_gui_gl_begin(gui);
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
98 glViewport(0, 0, width, height);
101 glMatrixMode(GL_PROJECTION);
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);
107 /* Camera position? */
108 glMatrixMode(GL_MODELVIEW);
110 glTranslatef(0.0, 0.0, -cam);
111 //glRotatef(-45, 1, 0, 0);
113 aweather_gui_gl_end(gui);
117 static gboolean expose_begin(GtkWidget *da, GdkEventExpose *event, AWeatherGui *gui)
119 aweather_gui_gl_begin(gui);
122 static gboolean expose_end(GtkWidget *da, GdkEventExpose *event, AWeatherGui *gui)
124 g_message("aweather:espose_end\n");
125 aweather_gui_gl_end(gui);
126 aweather_gui_gl_flush(gui);
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)
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++) {
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);
150 static void update_location_widget(AWeatherView *view, char *location, AWeatherGui *gui)
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++) {
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++) {
160 gtk_tree_model_iter_nth_child(model, &iter2, &iter1, i);
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);
179 static void combo_sensitive(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
180 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
182 gboolean sensitive = !gtk_tree_model_iter_has_child(tree_model, iter);
183 g_object_set(cell, "sensitive", sensitive, NULL);
186 static void site_setup(AWeatherGui *gui)
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);
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);
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);
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);
215 static void time_setup(AWeatherGui *gui)
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);
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);
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);
231 gboolean opengl_setup(AWeatherGui *gui)
233 GtkDrawingArea *drawing = GTK_DRAWING_AREA(aweather_gui_get_widget(gui, "drawing"));
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);
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");
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);
255 G_DEFINE_TYPE(AWeatherGui, aweather_gui, G_TYPE_OBJECT);
257 /* Constructor / destructors */
258 static void aweather_gui_init(AWeatherGui *gui)
260 //g_message("aweather_gui_init");
263 static GObject *aweather_gui_constructor(GType gtype, guint n_properties,
264 GObjectConstructParam *properties)
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);
271 static void aweather_gui_dispose (GObject *gobject)
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);
283 static void aweather_gui_finalize(GObject *gobject)
285 //g_message("aweather_gui_finalize");
286 G_OBJECT_CLASS(aweather_gui_parent_class)->finalize(gobject);
289 static void aweather_gui_class_init(AWeatherGuiClass *klass)
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;
299 AWeatherGui *aweather_gui_new()
301 //g_message("aweather_gui_new");
302 GError *error = NULL;
304 AWeatherGui *gui = g_object_new(AWEATHER_TYPE_GUI, NULL);
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);
315 /* Load components */
321 AWeatherView *aweather_gui_get_view(AWeatherGui *gui)
323 g_assert(AWEATHER_IS_GUI(gui));
326 GtkBuilder *aweather_gui_get_builder(AWeatherGui *gui)
328 g_assert(AWEATHER_IS_GUI(gui));
331 GtkWidget *aweather_gui_get_widget(AWeatherGui *gui, const gchar *name)
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);
338 void aweather_gui_gl_begin(AWeatherGui *gui)
340 g_assert(AWEATHER_IS_GUI(gui));
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));
346 if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
347 g_assert_not_reached();
349 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
351 void aweather_gui_gl_end(AWeatherGui *gui)
353 g_assert(AWEATHER_IS_GUI(gui));
354 GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
355 gdk_gl_drawable_gl_end(gldrawable);
357 void aweather_gui_gl_flush(AWeatherGui *gui)
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);
365 gdk_gl_drawable_gl_end(gldrawable);
367 //void aweather_gui_register_plugin(AWeatherGui *gui, AWeatherPlugin *plugin);