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.8f, 0.8f, 1.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
97 glViewport(0, 0, width, height);
100 glMatrixMode(GL_PROJECTION);
102 double rad = atan(height/2*1000.0/dist); // 1px = 1000 meters
103 double deg = (rad*180)/M_PI;
104 gluPerspective(deg*2, width/height, dist-20, dist+20);
106 /* Camera position? */
107 glMatrixMode(GL_MODELVIEW);
109 glTranslatef(0.0, 0.0, -dist);
110 //glRotatef(-45, 1, 0, 0);
112 aweather_gui_gl_end(gui);
116 static gboolean expose_begin(GtkWidget *da, GdkEventExpose *event, AWeatherGui *gui)
118 aweather_gui_gl_begin(gui);
121 static gboolean expose_end(GtkWidget *da, GdkEventExpose *event, AWeatherGui *gui)
123 g_message("aweather:espose_end\n");
124 aweather_gui_gl_end(gui);
125 aweather_gui_gl_flush(gui);
129 /* TODO: replace the code in these with `gtk_tree_model_find' utility */
130 static void update_time_widget(AWeatherView *view, char *time, AWeatherGui *gui)
132 g_message("updating time widget");
133 GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(gui, "time"));
134 GtkTreeModel *model = GTK_TREE_MODEL(gtk_tree_view_get_model(tview));
135 for (int i = 0; i < gtk_tree_model_iter_n_children(model, NULL); i++) {
138 gtk_tree_model_iter_nth_child(model, &iter, NULL, i);
139 gtk_tree_model_get(model, &iter, 0, &text, -1);
140 if (g_str_equal(text, time)) {
141 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
142 g_signal_handlers_block_by_func(tview, G_CALLBACK(on_site_changed), gui);
143 gtk_tree_view_set_cursor(tview, path, NULL, FALSE);
144 g_signal_handlers_unblock_by_func(tview, G_CALLBACK(on_site_changed), gui);
149 static void update_location_widget(AWeatherView *view, char *location, AWeatherGui *gui)
151 g_message("updating location widget to %s", location);
152 GtkComboBox *combo = GTK_COMBO_BOX(aweather_gui_get_widget(gui, "site"));
153 GtkTreeModel *model = GTK_TREE_MODEL(gtk_combo_box_get_model(combo));
154 for (int i = 0; i < gtk_tree_model_iter_n_children(model, NULL); i++) {
156 gtk_tree_model_iter_nth_child(model, &iter1, NULL, i);
157 for (int i = 0; i < gtk_tree_model_iter_n_children(model, &iter1); i++) {
159 gtk_tree_model_iter_nth_child(model, &iter2, &iter1, i);
161 gtk_tree_model_get(model, &iter2, 1, &text, -1);
162 if (g_str_equal(text, location)) {
163 GtkTreePath *path = gtk_tree_model_get_path(model, &iter2);
164 g_signal_handlers_block_by_func(combo, G_CALLBACK(on_site_changed), gui);
165 gtk_combo_box_set_active_iter(combo, &iter2);
166 g_signal_handlers_unblock_by_func(combo, G_CALLBACK(on_site_changed), gui);
176 static void combo_sensitive(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
177 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
179 gboolean sensitive = !gtk_tree_model_iter_has_child(tree_model, iter);
180 g_object_set(cell, "sensitive", sensitive, NULL);
183 static void site_setup(AWeatherGui *gui)
185 GtkTreeIter state, city;
186 GtkTreeStore *store = gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
187 for (int i = 0; cities[i].label; i++) {
188 if (cities[i].type == LOCATION_STATE) {
189 gtk_tree_store_append(store, &state, NULL);
190 gtk_tree_store_set (store, &state, 0, cities[i].label,
191 1, cities[i].code, -1);
193 gtk_tree_store_append(store, &city, &state);
194 gtk_tree_store_set (store, &city, 0, cities[i].label,
195 1, cities[i].code, -1);
199 GtkWidget *combo = aweather_gui_get_widget(gui, "site");
200 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
201 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, FALSE);
202 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, "text", 0, NULL);
203 gtk_combo_box_set_model(GTK_COMBO_BOX(combo), GTK_TREE_MODEL(store));
204 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(combo), renderer,
205 combo_sensitive, NULL, NULL);
207 g_signal_connect(combo, "changed", G_CALLBACK(on_site_changed), gui);
208 AWeatherView *aview = aweather_gui_get_view(gui);
209 g_signal_connect(aview, "location-changed", G_CALLBACK(update_location_widget), gui);
212 static void time_setup(AWeatherGui *gui)
214 GtkTreeView *tview = GTK_TREE_VIEW(aweather_gui_get_widget(gui, "time"));
215 GtkTreeModel *store = GTK_TREE_MODEL(gtk_list_store_new(1, G_TYPE_STRING));
216 gtk_tree_view_set_model(tview, store);
218 GtkCellRenderer *rend = gtk_cell_renderer_text_new();
219 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes(
220 "Time", rend, "text", 0, NULL);
221 gtk_tree_view_append_column(tview, col);
223 g_signal_connect(tview, "row-activated", G_CALLBACK(on_time_changed), gui);
224 AWeatherView *aview = aweather_gui_get_view(gui);
225 g_signal_connect(aview, "time-changed", G_CALLBACK(update_time_widget), gui);
228 gboolean opengl_setup(AWeatherGui *gui)
230 GtkDrawingArea *drawing = GTK_DRAWING_AREA(aweather_gui_get_widget(gui, "drawing"));
232 GdkGLConfig *glconfig = gdk_gl_config_new_by_mode(
233 GDK_GL_MODE_RGBA | GDK_GL_MODE_DEPTH |
234 GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA);
236 g_error("Failed to create glconfig");
237 if (!gtk_widget_set_gl_capability(GTK_WIDGET(drawing), glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
238 g_error("GL lacks required capabilities");
240 /* Set up OpenGL Stuff */
241 g_signal_connect (drawing, "map-event", G_CALLBACK(map), gui);
242 g_signal_connect (drawing, "configure-event", G_CALLBACK(configure), gui);
243 g_signal_connect (drawing, "expose-event", G_CALLBACK(expose_begin), gui);
244 g_signal_connect_after(drawing, "expose-event", G_CALLBACK(expose_end), gui);
252 G_DEFINE_TYPE(AWeatherGui, aweather_gui, G_TYPE_OBJECT);
254 /* Constructor / destructors */
255 static void aweather_gui_init(AWeatherGui *gui)
257 //g_message("aweather_gui_init");
260 static GObject *aweather_gui_constructor(GType gtype, guint n_properties,
261 GObjectConstructParam *properties)
263 //g_message("aweather_gui_constructor");
264 GObjectClass *parent_class = G_OBJECT_CLASS(aweather_gui_parent_class);
265 return parent_class->constructor(gtype, n_properties, properties);
268 static void aweather_gui_dispose (GObject *gobject)
270 //g_message("aweather_gui_dispose");
271 AWeatherGui *gui = AWEATHER_GUI(gobject);
272 g_object_unref(gui->view );
273 g_object_unref(gui->builder);
274 g_object_unref(gui->window );
275 g_object_unref(gui->tabs );
276 g_object_unref(gui->drawing);
277 G_OBJECT_CLASS(aweather_gui_parent_class)->dispose(gobject);
280 static void aweather_gui_finalize(GObject *gobject)
282 //g_message("aweather_gui_finalize");
283 G_OBJECT_CLASS(aweather_gui_parent_class)->finalize(gobject);
286 static void aweather_gui_class_init(AWeatherGuiClass *klass)
288 //g_message("aweather_gui_class_init");
289 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
290 gobject_class->constructor = aweather_gui_constructor;
291 gobject_class->dispose = aweather_gui_dispose;
292 gobject_class->finalize = aweather_gui_finalize;
296 AWeatherGui *aweather_gui_new()
298 //g_message("aweather_gui_new");
299 GError *error = NULL;
301 AWeatherGui *gui = g_object_new(AWEATHER_TYPE_GUI, NULL);
303 gui->builder = gtk_builder_new();
304 if (!gtk_builder_add_from_file(gui->builder, DATADIR "/aweather/aweather.xml", &error))
305 g_error("Failed to create gtk builder: %s", error->message);
306 gui->view = aweather_view_new();
307 gui->window = GTK_WINDOW (gtk_builder_get_object(gui->builder, "window"));
308 gui->tabs = GTK_NOTEBOOK (gtk_builder_get_object(gui->builder, "tabs"));
309 gui->drawing = GTK_DRAWING_AREA(gtk_builder_get_object(gui->builder, "drawing"));
310 gtk_builder_connect_signals(gui->builder, NULL);
312 /* Load components */
318 AWeatherView *aweather_gui_get_view(AWeatherGui *gui)
320 g_assert(AWEATHER_IS_GUI(gui));
323 GtkBuilder *aweather_gui_get_builder(AWeatherGui *gui)
325 g_assert(AWEATHER_IS_GUI(gui));
328 GtkWidget *aweather_gui_get_widget(AWeatherGui *gui, const gchar *name)
330 g_assert(AWEATHER_IS_GUI(gui));
331 GObject *widget = gtk_builder_get_object(gui->builder, name);
332 g_assert(GTK_IS_WIDGET(widget));
333 return GTK_WIDGET(widget);
335 void aweather_gui_gl_begin(AWeatherGui *gui)
337 g_assert(AWEATHER_IS_GUI(gui));
339 GtkDrawingArea *drawing = GTK_DRAWING_AREA(aweather_gui_get_widget(gui, "drawing"));
340 GdkGLContext *glcontext = gtk_widget_get_gl_context(GTK_WIDGET(drawing));
341 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(drawing));
343 if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
344 g_assert_not_reached();
346 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
348 void aweather_gui_gl_end(AWeatherGui *gui)
350 g_assert(AWEATHER_IS_GUI(gui));
351 GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
352 gdk_gl_drawable_gl_end(gldrawable);
354 void aweather_gui_gl_flush(AWeatherGui *gui)
356 g_assert(AWEATHER_IS_GUI(gui));
357 GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current();
358 if (gdk_gl_drawable_is_double_buffered(gldrawable))
359 gdk_gl_drawable_swap_buffers(gldrawable);
362 gdk_gl_drawable_gl_end(gldrawable);
364 //void aweather_gui_register_plugin(AWeatherGui *gui, AWeatherPlugin *plugin);