]> Pileus Git - grits/blob - src/tile-test.c
Fix 64-bit warnings
[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 "objects/grits-tile.h"
24
25 struct CacheState {
26         GtkWidget *image;
27         GtkWidget *status;
28         GtkWidget *progress;
29 };
30
31
32 void chunk_callback(gsize cur, gsize total, gpointer _state)
33 {
34         struct CacheState *state = _state;
35         g_message("chunk_callback: %ld/%ld", cur, total);
36
37         if (state->progress == NULL) {
38                 state->progress = gtk_progress_bar_new();
39                 gtk_box_pack_end(GTK_BOX(state->status), state->progress, FALSE, FALSE, 0);
40                 gtk_widget_show(state->progress);
41         }
42
43         if (cur == total)
44                 gtk_widget_destroy(state->progress);
45         else
46                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(state->progress), (gdouble)cur/total);
47 }
48
49 gpointer do_bmng_cache(gpointer _image)
50 {
51         GtkImage *image = _image;
52         g_message("Creating bmng tile");
53         GritsTile *tile = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
54         tile->children[0][1] = grits_tile_new(tile, NORTH, 0, 0, WEST);
55         tile = tile->children[0][1];
56
57         g_message("Fetching bmng image");
58         GritsWms *bmng_wms = grits_wms_new(
59                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
60                 "bmng_test/", "jpg", 512, 256);
61         const char *path = grits_wms_fetch(bmng_wms, tile, GRITS_ONCE, NULL, NULL);
62
63         g_message("Loading bmng image: [%s]", path);
64         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
65         gdk_threads_enter();
66         gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);
67         gdk_threads_leave();
68
69         g_message("Cleaning bmng up");
70         grits_wms_free(bmng_wms);
71         grits_tile_free(tile, NULL, NULL);
72         return NULL;
73 }
74
75 gpointer do_osm_cache(gpointer _image)
76 {
77         GtkImage *image = _image;
78         g_message("Creating osm tile");
79         GritsTile *tile = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
80         tile->children[0][1] = grits_tile_new(tile, NORTH, 0, 0, WEST);
81         tile = tile->children[0][1];
82
83         g_message("Fetching osm image");
84         GritsWms *osm_wms = grits_wms_new(
85                 "http://labs.metacarta.com/wms/vmap0", "basic", "image/png",
86                 "osm_test/", "png", 512, 256);
87         const char *path = grits_wms_fetch(osm_wms, tile, GRITS_ONCE, NULL, NULL);
88
89         g_message("Loading osm image: [%s]", path);
90         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
91         gdk_threads_enter();
92         gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);
93         gdk_threads_leave();
94
95         g_message("Cleaning osm up");
96         grits_wms_free(osm_wms);
97         grits_tile_free(tile, NULL, NULL);
98         return NULL;
99 }
100
101 gboolean key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
102 {
103         if (event->keyval == GDK_q)
104                 gtk_main_quit();
105         return TRUE;
106 }
107
108 int main(int argc, char **argv)
109 {
110         g_thread_init(NULL);
111         gdk_threads_init();
112         gtk_init(&argc, &argv);
113
114         GtkWidget *win        = gtk_window_new(GTK_WINDOW_TOPLEVEL);
115         GtkWidget *vbox1      = gtk_vbox_new(FALSE, 0);
116         GtkWidget *vbox2      = gtk_vbox_new(FALSE, 0);
117         GtkWidget *status     = gtk_statusbar_new();
118         GtkWidget *scroll     = gtk_scrolled_window_new(NULL, NULL);
119         GtkWidget *bmng_image = gtk_image_new();
120         GtkWidget *srtm_image = gtk_image_new();
121         GtkWidget *osm_image  = gtk_image_new();
122         gtk_container_add(GTK_CONTAINER(win), vbox1);
123         gtk_box_pack_start(GTK_BOX(vbox1), scroll, TRUE, TRUE, 0);
124         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), vbox2);
125         gtk_box_pack_start(GTK_BOX(vbox2), bmng_image, TRUE, TRUE, 0);
126         gtk_box_pack_start(GTK_BOX(vbox2), srtm_image, TRUE, TRUE, 0);
127         gtk_box_pack_start(GTK_BOX(vbox2), osm_image,  TRUE, TRUE, 0);
128         gtk_box_pack_start(GTK_BOX(vbox1), status, FALSE, FALSE, 0);
129         g_signal_connect(win, "key-press-event", G_CALLBACK(key_press_cb), NULL);
130         g_signal_connect(win, "destroy", gtk_main_quit, NULL);
131         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
132                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
133
134         g_thread_create(do_bmng_cache, bmng_image, FALSE, NULL);
135         g_thread_create(do_osm_cache,  osm_image,  FALSE, NULL);
136
137         gtk_widget_show_all(win);
138         gtk_main();
139
140         return 0;
141 }