From 80e905c00e4d3f67008876e2ba7b159ceac19f01 Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Mon, 5 May 2008 10:23:36 +0300 Subject: [PATCH] Some more map widget work. Does not work yet. --- src/gtkmap.c | 201 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 132 insertions(+), 69 deletions(-) diff --git a/src/gtkmap.c b/src/gtkmap.c index e6b58d0..09a5f58 100644 --- a/src/gtkmap.c +++ b/src/gtkmap.c @@ -224,7 +224,7 @@ static gboolean gtk_map_configure(GtkWidget *widget, GdkEventConfigure *event); static void gtk_map_scale_draw(GtkWidget *widget, GdkEventExpose *event); static void gtk_map_mark_draw(GtkWidget *widget, GdkEventExpose *event); -static gboolean gtk_map_update_buffer_size(GtkMap *map, gint new_width, gint new_height); +static gboolean gtk_map_update_buffer_size(GtkWidget *widget, gint new_width, gint new_height); static void gtk_map_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); static void gtk_map_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); @@ -251,7 +251,7 @@ enum { static guint gtk_map_signals[LAST_SIGNAL] = { 0 }; static void -gtk_map_class_init (GtkMapClass *class) +gtk_map_class_init(GtkMapClass *class) { GObjectClass *object_class; GtkWidgetClass *widget_class; @@ -265,7 +265,7 @@ object_class->get_property = gtk_map_get_property; widget_class->size_request = gtk_map_size_request; widget_class->expose_event = gtk_map_expose; -widget_class->configure_event = gtk_map_configure; +/* widget_class->configure_event = gtk_map_configure; */ widget_class->realize = gtk_map_realize; widget_class->size_allocate = gtk_map_size_allocate; @@ -332,9 +332,8 @@ static void gtk_map_init(GtkMap *map) { GtkMapPriv *priv; -GdkColor color; -g_debug("GTKMAP: Init"); +g_debug("GTKMAP: %s", __PRETTY_FUNCTION__); priv=GTK_MAP_GET_PRIVATE(map); #ifdef WITH_CAIRO @@ -352,32 +351,6 @@ priv->speed_gc=priv->speed_gc1; priv->icache=image_cache_new(64); -priv->scale_context=gtk_widget_get_pango_context(GTK_WIDGET(map)); -priv->scale_layout=pango_layout_new(priv->scale_context); -priv->scale_font=pango_font_description_new(); -pango_font_description_set_size(priv->scale_font, 12 * PANGO_SCALE); -pango_layout_set_font_description(priv->scale_layout, priv->scale_font); - -/* Speed limit, over limit color */ -priv->speed_gc1=gdk_gc_new(GTK_WIDGET(map)->window); -color.red=0xffff; -color.green=0; -color.blue=0; -gdk_gc_set_rgb_fg_color(priv->speed_gc1, &color); - -/* Speed limit, under limit color */ -priv->speed_gc2=gdk_gc_new(GTK_WIDGET(map)->window); -color.red=0; -color.green=0x1000; -color.blue=0; -gdk_gc_set_rgb_fg_color(priv->speed_gc2, &color); - -priv->speed_context=gtk_widget_get_pango_context(GTK_WIDGET(map)); -priv->speed_layout=pango_layout_new(priv->speed_context); -priv->speed_font=pango_font_description_new(); -pango_font_description_set_size(priv->speed_font, 48 * PANGO_SCALE); -pango_layout_set_font_description(priv->speed_layout, priv->speed_font); -pango_layout_set_alignment(priv->speed_layout, PANGO_ALIGN_LEFT); priv->gl=FALSE; @@ -408,7 +381,6 @@ g_signal_connect(G_OBJECT(map), "button_press_event", G_CALLBACK(gtk_map_cb_butt static gboolean gtk_map_configure(GtkWidget *widget, GdkEventConfigure *event) { -guint tw, th; GtkMap *map; GtkMapPriv *priv; @@ -416,33 +388,7 @@ g_return_val_if_fail(GTK_IS_MAP(widget), TRUE); map=GTK_MAP(widget); priv=GTK_MAP_GET_PRIVATE(map); -tw=GTK_MAP_TILE_SIZE_PIXELS*((widget->allocation.width/GTK_MAP_TILE_SIZE_PIXELS)+2); -th=GTK_MAP_TILE_SIZE_PIXELS*((widget->allocation.height/GTK_MAP_TILE_SIZE_PIXELS)+2); - -gtk_map_update_buffer_size(GTK_MAP(widget), tw, th); -g_assert(priv->buffer); - -priv->buf_width_pixels=tw; -priv->buf_height_pixels=th; -priv->buf_width_tiles=priv->buf_width_pixels/GTK_MAP_TILE_SIZE_PIXELS; -priv->buf_height_tiles=priv->buf_height_pixels/GTK_MAP_TILE_SIZE_PIXELS; - -priv->screen_width_pixels = widget->allocation.width; -priv->screen_height_pixels = widget->allocation.height; -priv->screen_grids_halfwidth = pixel2grid(priv->screen_width_pixels) / 2; -priv->screen_grids_halfheight = pixel2grid(priv->screen_height_pixels) / 2; - -/* Set scale_rect. */ -priv->scale_rect.x = (priv->screen_width_pixels - SCALE_WIDTH) / 2; -priv->scale_rect.width = SCALE_WIDTH; - -gtk_map_recalc_focus_base(priv); -gtk_map_recalc_focus_size(priv); - -priv->min_center.unitx = pixel2unit(grid2pixel(priv->screen_grids_halfwidth)); -priv->min_center.unity = pixel2unit(grid2pixel(priv->screen_grids_halfheight)); -priv->max_center.unitx = GTK_MAP_WORLD_SIZE_UNITS - grid2unit(priv->screen_grids_halfwidth) - 1; -priv->max_center.unity = GTK_MAP_WORLD_SIZE_UNITS - grid2unit(priv->screen_grids_halfheight) - 1; +g_debug("GTKMAP: %s", __PRETTY_FUNCTION__); return TRUE; } @@ -477,6 +423,7 @@ switch (prop_id) { GtkWidget* gtk_map_new(void) { +g_debug("GTKMAP: %s", __PRETTY_FUNCTION__); return g_object_new(GTK_MAP_TYPE, NULL); } @@ -488,6 +435,8 @@ GtkMap *map; g_return_if_fail(GTK_IS_MAP(object)); map=GTK_MAP(object); +g_debug("GTKMAP: %s", __PRETTY_FUNCTION__); + if (GTK_WIDGET(object)->parent && GTK_WIDGET_MAPPED(object)) { gtk_widget_unmap(GTK_WIDGET(object)); } @@ -499,9 +448,48 @@ static void gtk_map_size_allocate(GtkWidget *widget, GtkAllocation *allocation) { GtkMap *map; - +GtkMapPriv *priv; +guint tw, th; + g_return_if_fail(GTK_IS_MAP(widget)); map=GTK_MAP(widget); +priv=GTK_MAP_GET_PRIVATE(map); +widget->allocation = *allocation; + +g_debug("GTKMAP: Size allocate (%d, %d)", widget->allocation.width, widget->allocation.height); + +if (GTK_WIDGET_REALIZED(widget)) + gdk_window_move_resize(widget->window, allocation->x, allocation->y, allocation->width, allocation->height); + +tw=GTK_MAP_TILE_SIZE_PIXELS*((widget->allocation.width/GTK_MAP_TILE_SIZE_PIXELS)+2); +th=GTK_MAP_TILE_SIZE_PIXELS*((widget->allocation.height/GTK_MAP_TILE_SIZE_PIXELS)+2); + +gtk_map_update_buffer_size(GTK_MAP(widget), tw, th); +if (!priv->buffer) { + return; +} + +priv->buf_width_pixels=tw; +priv->buf_height_pixels=th; +priv->buf_width_tiles=priv->buf_width_pixels/GTK_MAP_TILE_SIZE_PIXELS; +priv->buf_height_tiles=priv->buf_height_pixels/GTK_MAP_TILE_SIZE_PIXELS; + +priv->screen_width_pixels = widget->allocation.width; +priv->screen_height_pixels = widget->allocation.height; +priv->screen_grids_halfwidth = pixel2grid(priv->screen_width_pixels) / 2; +priv->screen_grids_halfheight = pixel2grid(priv->screen_height_pixels) / 2; + +/* Set scale_rect. */ +priv->scale_rect.x = (priv->screen_width_pixels - SCALE_WIDTH) / 2; +priv->scale_rect.width = SCALE_WIDTH; + +gtk_map_recalc_focus_base(priv); +gtk_map_recalc_focus_size(priv); + +priv->min_center.unitx = pixel2unit(grid2pixel(priv->screen_grids_halfwidth)); +priv->min_center.unity = pixel2unit(grid2pixel(priv->screen_grids_halfheight)); +priv->max_center.unitx = GTK_MAP_WORLD_SIZE_UNITS - grid2unit(priv->screen_grids_halfwidth) - 1; +priv->max_center.unity = GTK_MAP_WORLD_SIZE_UNITS - grid2unit(priv->screen_grids_halfheight) - 1; } static void @@ -513,27 +501,34 @@ g_return_if_fail(GTK_IS_MAP(widget)); g_return_if_fail(requisition != NULL); map=GTK_MAP(widget); + +g_debug("GTKMAP: Size request"); -requisition->width=512; -requisition->height=256; -map->width=512; -map->height=256; +requisition->width=GTK_MAP_TILE_SIZE_PIXELS/2; +requisition->height=GTK_MAP_TILE_SIZE_PIXELS/2; } static gboolean -gtk_map_update_buffer_size(GtkMap *map, gint new_width, gint new_height) +gtk_map_update_buffer_size(GtkWidget *widget, gint new_width, gint new_height) { +GtkMap *map; GtkMapPriv *priv; +g_return_val_if_fail(GTK_IS_MAP(widget), FALSE); +g_return_val_if_fail(widget->window, FALSE); + +map=GTK_MAP(widget); priv=GTK_MAP_GET_PRIVATE(map); +g_debug("GTKMAP: %s (%d, %d)", __PRETTY_FUNCTION__, new_width, new_height); + if (priv->buffer==NULL) { - priv->buffer=gdk_pixmap_new(GTK_WIDGET(map)->window, new_width, new_height, -1); + priv->buffer=gdk_pixmap_new(widget->window, new_width, new_height, -1); return TRUE; } else if (new_width>priv->buf_width_pixels || new_height>priv->buf_height_pixels || new_widthbuf_width_pixels-(GTK_MAP_TILE_SIZE_PIXELS*2) || new_heightbuf_height_pixels-(GTK_MAP_TILE_SIZE_PIXELS*2) ) { g_object_unref(priv->buffer); - priv->buffer=gdk_pixmap_new(GTK_WIDGET(map)->window, new_width, new_height, -1); + priv->buffer=gdk_pixmap_new(widget->window, new_width, new_height, -1); return TRUE; } return FALSE; @@ -543,9 +538,62 @@ static void gtk_map_realize(GtkWidget *widget) { GtkMap *map; - +GtkMapPriv *priv; +GdkColor color; +GdkWindowAttr attributes; +guint attributes_mask; + g_return_if_fail(GTK_IS_MAP(widget)); map=GTK_MAP(widget); +priv=GTK_MAP_GET_PRIVATE(map); + +g_debug("GTKMAP: Realize"); + +GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED); +attributes.window_type=GDK_WINDOW_CHILD; +attributes.x=widget->allocation.x; +attributes.y=widget->allocation.y; +attributes.width=512; +attributes.height=512; + +attributes.wclass=GDK_INPUT_OUTPUT; +attributes.event_mask=gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK; +attributes_mask=GDK_WA_X | GDK_WA_Y; + +widget->window=gdk_window_new(gtk_widget_get_parent_window(widget), &attributes, attributes_mask); + +gdk_window_set_user_data(widget->window, widget); + +widget->style=gtk_style_attach(widget->style, widget->window); +gtk_style_set_background(widget->style, widget->window, GTK_STATE_NORMAL); + +priv->scale_context=gtk_widget_get_pango_context(GTK_WIDGET(map)); +priv->scale_layout=pango_layout_new(priv->scale_context); +priv->scale_font=pango_font_description_new(); +pango_font_description_set_size(priv->scale_font, 12 * PANGO_SCALE); +pango_layout_set_font_description(priv->scale_layout, priv->scale_font); + +/* Speed limit, over limit color */ +priv->speed_gc1=gdk_gc_new(GTK_WIDGET(map)->window); +color.red=0xffff; +color.green=0; +color.blue=0; +gdk_gc_set_rgb_fg_color(priv->speed_gc1, &color); + +/* Speed limit, under limit color */ +priv->speed_gc2=gdk_gc_new(GTK_WIDGET(map)->window); +color.red=0; +color.green=0x1000; +color.blue=0; +gdk_gc_set_rgb_fg_color(priv->speed_gc2, &color); + +priv->speed_context=gtk_widget_get_pango_context(GTK_WIDGET(map)); +priv->speed_layout=pango_layout_new(priv->speed_context); +priv->speed_font=pango_font_description_new(); +pango_font_description_set_size(priv->speed_font, 48 * PANGO_SCALE); +pango_layout_set_font_description(priv->speed_layout, priv->speed_font); +pango_layout_set_alignment(priv->speed_layout, PANGO_ALIGN_LEFT); + } static gboolean @@ -563,9 +611,11 @@ priv=GTK_MAP_GET_PRIVATE(map); g_return_val_if_fail(priv->buffer, FALSE); +g_debug("GTKMAP: expose (%d, %d)-(%d, %d)", event->area.x, event->area.y, event->area.width, event->area.height); + style=widget->style; -gdk_draw_drawable(GDK_DRAWABLE(map), +gdk_draw_drawable(widget->window, style->fg_gc[GTK_STATE_NORMAL], priv->buffer, event->area.x + priv->offsetx, @@ -843,6 +893,19 @@ do { while ((dest_dim >>= ratio_p2) > 1); } +static GdkPixbuf * +gtk_map_tile_load(GtkWidget *widget, guint tilex, guint tiley, gint zoff, gboolean fast_fail) +{ +GtkMap *map; +GtkMapPriv *priv; + +g_return_val_if_fail(GTK_IS_MAP(widget), FALSE); +map=GTK_MAP(widget); +priv=GTK_MAP_GET_PRIVATE(map); + +return NULL; +} + static gboolean gtk_map_render_tile(GtkWidget *widget, guint tilex, guint tiley, guint destx, guint desty, gboolean fast_fail) { -- 2.39.5