From 12afe3ee55b19869b97637a7e12c2220306b1f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jard=C3=B3n?= Date: Mon, 12 Sep 2011 19:17:22 +0100 Subject: [PATCH 6/6] util/show-polygon: Use accessor functions instead direct access --- util/show-polygon.c | 73 ++++++++++++++++++++++++++++++-------------------- 1 files changed, 44 insertions(+), 29 deletions(-) diff --git a/util/show-polygon.c b/util/show-polygon.c index 3ef09dc..131679e 100644 --- a/util/show-polygon.c +++ b/util/show-polygon.c @@ -94,10 +94,14 @@ static void draw_polygon (cairo_t *cr, polygon_t *p, gdouble sf) static cairo_surface_t * pixmap_create (PolygonView *self, cairo_surface_t *target) { - cairo_surface_t *surface = - cairo_surface_create_similar (target, CAIRO_CONTENT_COLOR, - self->widget.allocation.width, - self->widget.allocation.height); + GtkAllocation allocation; + cairo_surface_t *surface; + + gtk_widget_get_allocation (&(self->widget), &allocation); + + surface = cairo_surface_create_similar (target, CAIRO_CONTENT_COLOR, + allocation.width, + allocation.height); cairo_t *cr = cairo_create (surface); polygon_t *polygon; gdouble sf_x, sf_y, sf; @@ -117,11 +121,11 @@ pixmap_create (PolygonView *self, cairo_surface_t *target) mid = (extents.p2.x + extents.p1.x) / 2.; dim = (extents.p2.x - extents.p1.x) / 2. * 1.25; - sf_x = self->widget.allocation.width / dim / 2; + sf_x = allocation.width / dim / 2; mid = (extents.p2.y + extents.p1.y) / 2.; dim = (extents.p2.y - extents.p1.y) / 2. * 1.25; - sf_y = self->widget.allocation.height / dim / 2; + sf_y = allocation.height / dim / 2; sf = MIN (sf_x, sf_y); @@ -163,6 +167,7 @@ static void polygon_view_draw (PolygonView *self, cairo_t *cr) { polygon_t *polygon; + GtkAllocation allocation; gdouble sf_x, sf_y, sf; gdouble mid, dim; gdouble x0, y0; @@ -170,13 +175,15 @@ polygon_view_draw (PolygonView *self, cairo_t *cr) extents = self->extents; + gtk_widget_get_allocation (&(self->widget), &allocation); + mid = (extents.p2.x + extents.p1.x) / 2.; dim = (extents.p2.x - extents.p1.x) / 2. * 1.25; - sf_x = self->widget.allocation.width / dim / 2; + sf_x = allocation.width / dim / 2; mid = (extents.p2.y + extents.p1.y) / 2.; dim = (extents.p2.y - extents.p1.y) / 2. * 1.25; - sf_y = self->widget.allocation.height / dim / 2; + sf_y = allocation.height / dim / 2; sf = MIN (sf_x, sf_y); @@ -187,13 +194,13 @@ polygon_view_draw (PolygonView *self, cairo_t *cr) dim = sf_y / sf * (extents.p2.y - extents.p1.y) / 2. * 1.25; y0 = mid - dim; - if (self->pixmap_width != self->widget.allocation.width || - self->pixmap_height != self->widget.allocation.height) + if (self->pixmap_width != allocation.width || + self->pixmap_height != allocation.height) { cairo_surface_destroy (self->pixmap); self->pixmap = pixmap_create (self, cairo_get_target (cr)); - self->pixmap_width = self->widget.allocation.width; - self->pixmap_height = self->widget.allocation.height; + self->pixmap_width = allocation.width; + self->pixmap_height = allocation.height; } cairo_set_source_surface (cr, self->pixmap, 0, 0); @@ -210,15 +217,15 @@ polygon_view_draw (PolygonView *self, cairo_t *cr) int mag_y = self->mag_y; if (1) { - if (self->px + size < self->widget.allocation.width/2) + if (self->px + size < allocation.width/2) mag_x = self->px + size/4; else mag_x = self->px - size/4 - size; mag_y = self->py - size/2; if (mag_y < 0) mag_y = 0; - if (mag_y + size > self->widget.allocation.height) - mag_y = self->widget.allocation.height - size; + if (mag_y + size > allocation.height) + mag_y = allocation.height - size; } cairo_save (cr); { @@ -294,7 +301,7 @@ polygon_view_expose (GtkWidget *w, GdkEventExpose *ev) PolygonView *self = (PolygonView *) w; cairo_t *cr; - cr = gdk_cairo_create (w->window); + cr = gdk_cairo_create (gtk_widget_get_window (w)); gdk_cairo_region (cr, ev->region); cairo_clip (cr); @@ -395,15 +402,19 @@ polygon_view_motion (GtkWidget *w, GdkEventMotion *ev) static void polygon_view_realize (GtkWidget *widget) { + GtkAllocation allocation; GdkWindowAttr attributes; + GdkWindow *gdk_window; gtk_widget_set_realized (widget, TRUE); + gtk_widget_get_allocation (widget, &allocation); + attributes.window_type = GDK_WINDOW_CHILD; - attributes.x = widget->allocation.x; - attributes.y = widget->allocation.y; - attributes.width = widget->allocation.width; - attributes.height = widget->allocation.height; + attributes.x = allocation.x; + attributes.y = allocation.y; + attributes.width = allocation.width; + attributes.height = allocation.height; attributes.wclass = GDK_INPUT_OUTPUT; attributes.visual = gtk_widget_get_visual (widget); attributes.colormap = gtk_widget_get_colormap (widget); @@ -416,25 +427,29 @@ polygon_view_realize (GtkWidget *widget) GDK_BUTTON_MOTION_MASK | GDK_EXPOSURE_MASK; - widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), - &attributes, - GDK_WA_X | GDK_WA_Y | - GDK_WA_VISUAL | GDK_WA_COLORMAP); - gdk_window_set_user_data (widget->window, widget); + gdk_window = gdk_window_new (gtk_widget_get_parent_window (widget), + &attributes, + GDK_WA_X | GDK_WA_Y | + GDK_WA_VISUAL | GDK_WA_COLORMAP); + gtk_widget_set_window (widget, gdk_window); + gdk_window_set_user_data (gdk_window, widget); - widget->style = gtk_style_attach (widget->style, widget->window); - gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL); + gtk_widget_style_attach (widget); + gtk_style_set_background (gtk_widget_get_style (widget), gdk_window, GTK_STATE_NORMAL); } static void polygon_view_size_allocate (GtkWidget *w, GdkRectangle *r) { PolygonView *self = (PolygonView *) w; + GtkAllocation allocation; GTK_WIDGET_CLASS (polygon_view_parent_class)->size_allocate (w, r); - self->mag_x = w->allocation.width - self->mag_size - 10; - self->mag_y = w->allocation.height - self->mag_size - 10; + gtk_widget_get_allocation (w, &allocation); + + self->mag_x = allocation.width - self->mag_size - 10; + self->mag_y = allocation.height - self->mag_size - 10; } static void -- 1.7.5.4