/* * Copyright (C) 2021 Nicole Faerber * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #define DEFAULT_WIDTH 800 #define DEFAULT_HEIGHT 600 // UI variables static GtkWidget *window = NULL; static GtkWidget *image_darea = NULL; // we paint everything in here and then into the drawing area widget static cairo_surface_t *psurface; static unsigned int zoomlevel=70000; static double direction=0.0; static double center_lat=50.8838492; static double center_lon=8.0209591; static gboolean need_redraw=TRUE; static gboolean draw_event (GtkWidget *widget, cairo_t *wcr, gpointer data) { static cairo_surface_t *surface=NULL; if (need_redraw) { if (surface != NULL) cairo_surface_destroy(surface); surface=cairo_image_surface_create(CAIRO_FORMAT_RGB24, DEFAULT_WIDTH, DEFAULT_HEIGHT); DrawMapCairo(surface, center_lat, center_lon, zoomlevel, direction); need_redraw=FALSE; } cairo_set_source_surface (wcr, surface, 0, 0); cairo_paint (wcr); return FALSE; } void move_clicked(GtkWidget *button, gpointer user_data) { gchar *dir=(gchar *)user_data; switch (dir[0]) { case 'l': center_lon-=(100. / zoomlevel); break; case 'r': center_lon+=(100. / zoomlevel); break; case 'u': center_lat+=(100. / zoomlevel); break; case 'd': center_lat-=(100. / zoomlevel); break; case '+': zoomlevel+=(zoomlevel/10); break; case '-': zoomlevel-=(zoomlevel/10); break; case '>': direction-=.1; break; case '<': direction+=.1; break; } fprintf(stderr, "%f %f @ %f %d\n", center_lat, center_lon, direction, zoomlevel); need_redraw=TRUE; gtk_widget_queue_draw(image_darea); } void create_main_window (void) { GtkWidget *box; GtkWidget *hbox; GtkWidget *w, *i; GtkWidget *grid; window = gtk_window_new(GTK_WINDOW_TOPLEVEL); psurface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 800, 600); box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4); gtk_container_add (GTK_CONTAINER (window), box); image_darea = gtk_drawing_area_new (); gtk_widget_set_size_request (image_darea, 800, 600); gtk_container_add (GTK_CONTAINER (box), image_darea); g_signal_connect (image_darea, "draw", G_CALLBACK (draw_event), NULL); grid = gtk_grid_new(); w=gtk_button_new_with_label("left"); gtk_grid_attach(GTK_GRID(grid), w, 0, 1, 1, 1); g_signal_connect (w, "clicked", G_CALLBACK (move_clicked), "l"); w=gtk_button_new_with_label("right"); gtk_grid_attach(GTK_GRID(grid), w, 2, 1, 1, 1); g_signal_connect (w, "clicked", G_CALLBACK (move_clicked), "r"); w=gtk_button_new_with_label("up"); gtk_grid_attach(GTK_GRID(grid), w, 1, 0, 1, 1); g_signal_connect (w, "clicked", G_CALLBACK (move_clicked), "u"); w=gtk_button_new_with_label("down"); g_signal_connect (w, "clicked", G_CALLBACK (move_clicked), "d"); gtk_grid_attach(GTK_GRID(grid), w, 1, 2, 1, 1); w=gtk_button_new_with_label("+"); g_signal_connect (w, "clicked", G_CALLBACK (move_clicked), "+"); gtk_grid_attach(GTK_GRID(grid), w, 4, 0, 1, 1); w=gtk_button_new_with_label("-"); g_signal_connect (w, "clicked", G_CALLBACK (move_clicked), "-"); gtk_grid_attach(GTK_GRID(grid), w, 4, 2, 1, 1); w=gtk_button_new_with_label("<"); g_signal_connect (w, "clicked", G_CALLBACK (move_clicked), "<"); gtk_grid_attach(GTK_GRID(grid), w, 3, 1, 1, 1); w=gtk_button_new_with_label(">"); g_signal_connect (w, "clicked", G_CALLBACK (move_clicked), ">"); gtk_grid_attach(GTK_GRID(grid), w, 5, 1, 1, 1); gtk_container_add (GTK_CONTAINER (box), grid); } int main(int argc, char **argv) { cairo_surface_t *surface; if (DrawMapCairoInit(argc, argv) != 0) { fprintf(stderr, "DrawMapCairoInit() failed\n"); return 1; } gtk_init(&argc, &argv); create_main_window(); gtk_widget_show_all(window); gtk_main(); return 0; }