]> Pileus Git - grits/blob - src/tile-test.c
Add support for GTK 3
[grits] / src / tile-test.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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 <gtk/gtk.h>
19 #include <gdk/gdkkeysyms.h>
20
21 #include "grits-util.h"
22 #include "data/grits-wms.h"
23 #include "data/grits-tms.h"
24 #include "objects/grits-tile.h"
25
26 #include "compat.h"
27
28 struct CacheState {
29         GtkWidget *image;
30         GtkWidget *status;
31         GtkWidget *progress;
32 };
33
34 struct LoadData {
35         GtkImage  *image;
36         GdkPixbuf *pixbuf;
37 };
38
39 void chunk_callback(gsize cur, gsize total, gpointer _state)
40 {
41         struct CacheState *state = _state;
42         g_message("chunk_callback: %ld/%ld", (glong)cur, (glong)total);
43
44         if (state->progress == NULL) {
45                 state->progress = gtk_progress_bar_new();
46                 gtk_box_pack_end(GTK_BOX(state->status), state->progress, FALSE, FALSE, 0);
47                 gtk_widget_show(state->progress);
48         }
49
50         if (cur == total)
51                 gtk_widget_destroy(state->progress);
52         else
53                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(state->progress), (gdouble)cur/total);
54 }
55
56 gboolean load_callback(gpointer _data)
57 {
58         struct LoadData *data = _data;
59         gtk_image_set_from_pixbuf(GTK_IMAGE(data->image), data->pixbuf);
60         g_free(data);
61         return FALSE;
62 }
63
64 void load_image(GtkImage *image, GdkPixbuf *pixbuf)
65 {
66         struct LoadData *data = g_new0(struct LoadData, 1);
67         data->image  = image;
68         data->pixbuf = pixbuf;
69         g_idle_add(load_callback, data);
70 }
71
72 gpointer do_bmng_cache(gpointer _image)
73 {
74         GtkImage *image = _image;
75         g_message("Creating bmng tile");
76         GritsTile *tile = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
77         tile->children[0][1] = grits_tile_new(tile, NORTH, 0, 0, WEST);
78         tile = tile->children[0][1];
79
80         g_message("Fetching bmng image");
81         GritsWms *bmng_wms = grits_wms_new(
82                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
83                 "bmng_test/", "jpg", 512, 256);
84         const char *path = grits_wms_fetch(bmng_wms, tile, GRITS_ONCE, NULL, NULL);
85
86         g_message("Loading bmng image: [%s]", path);
87         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
88         load_image(GTK_IMAGE(image), pixbuf);
89
90         g_message("Cleaning bmng up");
91         grits_wms_free(bmng_wms);
92         grits_tile_free(tile, NULL, NULL);
93         return NULL;
94 }
95
96 gpointer do_osm_cache(gpointer _image)
97 {
98         GtkImage *image = _image;
99         g_message("Creating osm tile");
100         GritsTile *tile = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
101         tile->children[0][1] = grits_tile_new(tile, NORTH, 0, 0, WEST);
102         tile = tile->children[0][1];
103
104         g_message("Fetching osm image");
105         GritsWms *osm_wms = grits_wms_new(
106                 "http://labs.metacarta.com/wms/vmap0", "basic", "image/png",
107                 "osm_test/", "png", 512, 256);
108         const char *path = grits_wms_fetch(osm_wms, tile, GRITS_ONCE, NULL, NULL);
109
110         g_message("Loading osm image: [%s]", path);
111         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
112         load_image(GTK_IMAGE(image), pixbuf);
113
114         g_message("Cleaning osm up");
115         grits_wms_free(osm_wms);
116         grits_tile_free(tile, NULL, NULL);
117         return NULL;
118 }
119
120 gpointer do_osm2_cache(gpointer _image)
121 {
122         GtkImage *image = _image;
123         g_message("Creating osm2 tile");
124         GritsTile *tile = grits_tile_new(NULL, 85.0511, -85.0511, EAST, WEST);
125         tile->children[0][1] = grits_tile_new(tile, 85.0511, 0, 0, WEST);
126         tile = tile->children[0][1];
127
128         g_message("Fetching osm2 image");
129         GritsTms *osm2_tms = grits_tms_new("http://tile.openstreetmap.org",
130                         "tms_test/", "png");
131         const char *path = grits_tms_fetch(osm2_tms, tile, GRITS_ONCE, NULL, NULL);
132
133         g_message("Loading osm2 image: [%s]", path);
134         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
135         load_image(GTK_IMAGE(image), pixbuf);
136
137         g_message("Cleaning osm2 up");
138         grits_tms_free(osm2_tms);
139         grits_tile_free(tile, NULL, NULL);
140         return NULL;
141 }
142
143
144 gboolean key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
145 {
146         if (event->keyval == GDK_KEY_q)
147                 gtk_main_quit();
148         return TRUE;
149 }
150
151 int main(int argc, char **argv)
152 {
153         gtk_init(&argc, &argv);
154
155         GtkWidget *win        = gtk_window_new(GTK_WINDOW_TOPLEVEL);
156         GtkWidget *vbox1      = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
157         GtkWidget *vbox2      = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
158         GtkWidget *status     = gtk_statusbar_new();
159         GtkWidget *scroll     = gtk_scrolled_window_new(NULL, NULL);
160         GtkWidget *bmng_image = gtk_image_new();
161         GtkWidget *srtm_image = gtk_image_new();
162         GtkWidget *osm_image  = gtk_image_new();
163         GtkWidget *osm2_image = gtk_image_new();
164         gtk_container_add(GTK_CONTAINER(win), vbox1);
165         gtk_box_pack_start(GTK_BOX(vbox1), scroll, TRUE, TRUE, 0);
166         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), vbox2);
167         gtk_box_pack_start(GTK_BOX(vbox2), bmng_image, TRUE, TRUE, 0);
168         gtk_box_pack_start(GTK_BOX(vbox2), srtm_image, TRUE, TRUE, 0);
169         gtk_box_pack_start(GTK_BOX(vbox2), osm_image,  TRUE, TRUE, 0);
170         gtk_box_pack_start(GTK_BOX(vbox2), osm2_image, TRUE, TRUE, 0);
171         gtk_box_pack_start(GTK_BOX(vbox1), status, FALSE, FALSE, 0);
172         g_signal_connect(win, "key-press-event", G_CALLBACK(key_press_cb), NULL);
173         g_signal_connect(win, "destroy", gtk_main_quit, NULL);
174         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
175                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
176
177         g_thread_new("bmng-thread", do_bmng_cache, bmng_image);
178         g_thread_new("osm-thread",  do_osm_cache,  osm_image);
179         g_thread_new("osm2-thread", do_osm2_cache, osm2_image);
180
181         gtk_widget_show_all(win);
182         gtk_main();
183
184         return 0;
185 }