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);
52 void on_time_changed(GtkTreeView *view, GtkTreePath *path,
53 GtkTreeViewColumn *column, AWeatherGui *gui)
57 GtkTreeModel *model = gtk_tree_view_get_model(view);
58 gtk_tree_model_get_iter(model, &iter, path);
59 gtk_tree_model_get(model, &iter, 0, &time, -1);
60 AWeatherView *aview = aweather_gui_get_view(gui);
61 aweather_view_set_time(aview, time);
65 static gboolean map(GtkWidget *da, GdkEventConfigure *event, AWeatherGui *gui)
67 //g_message("map:map");
68 AWeatherView *view = aweather_gui_get_view(gui);
69 aweather_view_set_location(view, "IND");
73 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
74 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
76 /* Tessellation, "finding intersecting triangles" */
77 /* http://research.microsoft.com/pubs/70307/tr-2006-81.pdf */
78 /* http://www.opengl.org/wiki/Alpha_Blending */
79 glAlphaFunc(GL_GREATER,0.1);
80 glEnable(GL_ALPHA_TEST);
84 glDepthFunc(GL_LEQUAL);
85 glEnable(GL_DEPTH_TEST);
87 aweather_gui_gl_end(gui);
91 static gboolean configure(GtkWidget *da, GdkEventConfigure *event, AWeatherGui *gui)
93 aweather_gui_gl_begin(gui);
95 double width = da->allocation.width;
96 double height = da->allocation.height;
97 double dist = 500*1000; // 500 km
98 double cam = 300*1000; // 500 km
100 glViewport(0, 0, width, height);
103 glMatrixMode(GL_PROJECTION);
105 double rad = atan(height/2*1000.0/dist); // 1px = 1000 meters
106 double deg = (rad*180)/M_PI;
107 gluPerspective(deg*2, width/height, cam-20, cam+20);
109 /* Camera position? */
110 glMatrixMode(GL_MODELVIEW);
112 glTranslatef(0.0, 0.0, -cam);
113 //glRotatef(-45, 1, 0, 0);
115 aweather_gui_gl_end(gui);
119 static gboolean expose_begin(GtkWidget *da, GdkEventExpose *event, AWeatherGui *gui)
121 aweather_gui_gl_begin(gui);
124 static gboolean expose_end(GtkWidget *da, GdkEventExpose *event, AWeatherGui *gui)
126 g_message("aweather:espose_end\n");
127 aweather_gui_gl_end(gui);
128 aweather_gui_gl_flush(gui);
132 /* TODO: replace the code in these with `gtk_tree_model_find' utility */
133 static void update_time_widget(AWeatherView *view, char *time, AWeatherGui *gui)
135 g_message("updating time widget");
136 GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(gui, "time"));
137 GtkTreeModel *model = GTK_TREE_MODEL(gtk_tree_view_get_model(tview));
138 for (int i = 0; i < gtk_tree_model_iter_n_children(model, NULL); i++) {
141 gtk_tree_model_iter_nth_child(model, &iter, NULL, i);
142 gtk_tree_model_get(model, &iter, 0, &text, -1);
143 if (g_str_equal(text, time)) {
144 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
145 g_signal_handlers_block_by_func(tview,
146 G_CALLBACK(on_site_changed), gui);
147 gtk_tree_view_set_cursor(tview, path, NULL, FALSE);
148 g_signal_handlers_unblock_by_func(tview,
149 G_CALLBACK(on_site_changed), gui);
150 gtk_tree_path_free(path);
157 static void update_location_widget(AWeatherView *view, char *location, AWeatherGui *gui)
159 g_message("updating location widget to %s", location);
160 GtkComboBox *combo = GTK_COMBO_BOX(aweather_gui_get_widget(gui, "site"));
161 GtkTreeModel *model = GTK_TREE_MODEL(gtk_combo_box_get_model(combo));
162 for (int i = 0; i < gtk_tree_model_iter_n_children(model, NULL); i++) {
164 gtk_tree_model_iter_nth_child(model, &iter1, NULL, i);
165 for (int i = 0; i < gtk_tree_model_iter_n_children(model, &iter1); i++) {
167 gtk_tree_model_iter_nth_child(model, &iter2, &iter1, i);
169 gtk_tree_model_get(model, &iter2, 1, &text, -1);
170 if (g_str_equal(text, location)) {
171 g_signal_handlers_block_by_func(combo,
172 G_CALLBACK(on_site_changed), gui);
173 gtk_combo_box_set_active_iter(combo, &iter2);
174 g_signal_handlers_unblock_by_func(combo,
175 G_CALLBACK(on_site_changed), gui);
187 static void combo_sensitive(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
188 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
190 gboolean sensitive = !gtk_tree_model_iter_has_child(tree_model, iter);
191 g_object_set(cell, "sensitive", sensitive, NULL);
194 static void site_setup(AWeatherGui *gui)
196 GtkTreeIter state, city;
197 GtkTreeStore *store = gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
198 for (int i = 0; cities[i].label; i++) {
199 if (cities[i].type == LOCATION_STATE) {
200 gtk_tree_store_append(store, &state, NULL);
201 gtk_tree_store_set (store, &state, 0, cities[i].label,
202 1, cities[i].code, -1);
204 gtk_tree_store_append(store, &city, &state);
205 gtk_tree_store_set (store, &city, 0, cities[i].label,
206 1, cities[i].code, -1);
210 GtkWidget *combo = aweather_gui_get_widget(gui, "site");
211 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
212 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, FALSE);
213 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, "text", 0, NULL);
214 gtk_combo_box_set_model(GTK_COMBO_BOX(combo), GTK_TREE_MODEL(store));
215 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), renderer,
216 combo_sensitive, NULL, NULL);
217 g_object_unref(renderer);
218 g_object_unref(store);
220 g_signal_connect(combo, "changed", G_CALLBACK(on_site_changed), gui);
221 AWeatherView *aview = aweather_gui_get_view(gui);
222 g_signal_connect(aview, "location-changed", G_CALLBACK(update_location_widget), gui);
225 static void time_setup(AWeatherGui *gui)
227 GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(gui, "time"));
228 GtkTreeModel *store = GTK_TREE_MODEL(gtk_list_store_new(1, G_TYPE_STRING));
229 gtk_tree_view_set_model(tview, store);
231 GtkCellRenderer *rend = gtk_cell_renderer_text_new();
232 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes(
233 "Time", rend, "text", 0, NULL);
234 gtk_tree_view_append_column(tview, col);
236 g_signal_connect(tview, "row-activated", G_CALLBACK(on_time_changed), gui);
237 AWeatherView *aview = aweather_gui_get_view(gui);
238 g_signal_connect(aview, "time-changed", G_CALLBACK(update_time_widget), gui);
241 gboolean opengl_setup(AWeatherGui *gui)
243 GtkDrawingArea *drawing = GTK_DRAWING_AREA(aweather_gui_get_widget(gui, "drawing"));
245 GdkGLConfig *glconfig = gdk_gl_config_new_by_mode(
246 GDK_GL_MODE_RGBA | GDK_GL_MODE_DEPTH |
247 GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA);
249 g_error("Failed to create glconfig");
250 if (!gtk_widget_set_gl_capability(GTK_WIDGET(drawing),
251 glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
252 g_error("GL lacks required capabilities");
254 /* Set up OpenGL Stuff */
255 g_signal_connect (drawing, "map-event", G_CALLBACK(map), gui);
256 g_signal_connect (drawing, "configure-event", G_CALLBACK(configure), gui);
257 g_signal_connect (drawing, "expose-event", G_CALLBACK(expose_begin), gui);
258 g_signal_connect_after(drawing, "expose-event", G_CALLBACK(expose_end), gui);
266 G_DEFINE_TYPE(AWeatherGui, aweather_gui, G_TYPE_OBJECT);
268 /* Constructor / destructors */
269 static void aweather_gui_init(AWeatherGui *gui)
271 //g_message("aweather_gui_init");
274 static GObject *aweather_gui_constructor(GType gtype, guint n_properties,
275 GObjectConstructParam *properties)
277 //g_message("aweather_gui_constructor");
278 GObjectClass *parent_class = G_OBJECT_CLASS(aweather_gui_parent_class);
279 return parent_class->constructor(gtype, n_properties, properties);
282 static void aweather_gui_dispose (GObject *gobject)
284 //g_message("aweather_gui_dispose");
285 AWeatherGui *gui = AWEATHER_GUI(gobject);
286 g_object_unref(gui->view );
287 g_object_unref(gui->builder);
288 g_object_unref(gui->window );
289 g_object_unref(gui->tabs );
290 g_object_unref(gui->drawing);
291 G_OBJECT_CLASS(aweather_gui_parent_class)->dispose(gobject);
294 static void aweather_gui_finalize(GObject *gobject)
296 //g_message("aweather_gui_finalize");
297 G_OBJECT_CLASS(aweather_gui_parent_class)->finalize(gobject);
300 static void aweather_gui_class_init(AWeatherGuiClass *klass)
302 //g_message("aweather_gui_class_init");
303 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
304 gobject_class->constructor = aweather_gui_constructor;
305 gobject_class->dispose = aweather_gui_dispose;
306 gobject_class->finalize = aweather_gui_finalize;
310 AWeatherGui *aweather_gui_new()
312 //g_message("aweather_gui_new");
313 GError *error = NULL;
315 AWeatherGui *gui = g_object_new(AWEATHER_TYPE_GUI, NULL);
317 gui->builder = gtk_builder_new();
318 if (!gtk_builder_add_from_file(gui->builder, DATADIR "/aweather/aweather.xml", &error))
319 g_error("Failed to create gtk builder: %s", error->message);
320 gui->view = aweather_view_new();
321 gui->window = GTK_WINDOW (gtk_builder_get_object(gui->builder, "window"));
322 gui->tabs = GTK_NOTEBOOK (gtk_builder_get_object(gui->builder, "tabs"));
323 gui->drawing = GTK_DRAWING_AREA(gtk_builder_get_object(gui->builder, "drawing"));
324 gtk_builder_connect_signals(gui->builder, NULL);
326 /* Load components */
332 AWeatherView *aweather_gui_get_view(AWeatherGui *gui)
334 g_assert(AWEATHER_IS_GUI(gui));
337 GtkBuilder *aweather_gui_get_builder(AWeatherGui *gui)
339 g_assert(AWEATHER_IS_GUI(gui));
342 GtkWidget *aweather_gui_get_widget(AWeatherGui *gui, const gchar *name)
344 g_assert(AWEATHER_IS_GUI(gui));
345 GObject *widget = gtk_builder_get_object(gui->builder, name);
346 g_assert(GTK_IS_WIDGET(widget));
347 return GTK_WIDGET(widget);
349 void aweather_gui_gl_begin(AWeatherGui *gui)
351 g_assert(AWEATHER_IS_GUI(gui));
353 GtkDrawingArea *drawing = GTK_DRAWING_AREA(aweather_gui_get_widget(gui, "drawing"));
354 GdkGLContext *glcontext = gtk_widget_get_gl_context(GTK_WIDGET(drawing));
355 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(drawing));
357 if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
358 g_assert_not_reached();
360 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
362 void aweather_gui_gl_end(AWeatherGui *gui)
364 g_assert(AWEATHER_IS_GUI(gui));
365 GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
366 gdk_gl_drawable_gl_end(gldrawable);
368 void aweather_gui_gl_flush(AWeatherGui *gui)
370 g_assert(AWEATHER_IS_GUI(gui));
371 GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
372 if (gdk_gl_drawable_is_double_buffered(gldrawable))
373 gdk_gl_drawable_swap_buffers(gldrawable);
376 gdk_gl_drawable_gl_end(gldrawable);
378 //void aweather_gui_register_plugin(AWeatherGui *gui, AWeatherPlugin *plugin);