[dia/cairo-port: 40/53] Port the navigator



commit ea21bcab9d8740cbf7feef81359525128734cea6
Author: Zander Brown <zbrown gnome org>
Date:   Fri Dec 7 03:21:43 2018 +0000

    Port the navigator

 app/navigation.c                    | 130 ++++++++++++------------------------
 lib/renderer/diacairo-interactive.c |  87 +-----------------------
 2 files changed, 43 insertions(+), 174 deletions(-)
---
diff --git a/app/navigation.c b/app/navigation.c
index 704bb8b8..c9413a9e 100644
--- a/app/navigation.c
+++ b/app/navigation.c
@@ -26,7 +26,7 @@
 
 #include "diagram.h"
 #include "display.h"
-#include "diagdkrenderer.h"
+#include "renderer/diacairo.h"
 
 #include "navigation.h"
 
@@ -48,7 +48,6 @@ struct _NavigationWindow
   /*miniframe*/
   int frame_w;
   int frame_h;
-  GdkGC * gc;
   GdkCursor * cursor;
 
   /*factors to translate thumbnail coordinates to adjustement values*/
@@ -56,7 +55,7 @@ struct _NavigationWindow
   gdouble vadj_coef;
 
   /*diagram thumbnail's buffer*/
-  GdkPixmap * buffer;
+  cairo_surface_t *surface;
 
   /*display to navigate*/
   DDisplay * ddisp;
@@ -150,7 +149,7 @@ on_button_navigation_popup_pressed (GtkButton * button, gpointer _ddisp)
   Rectangle rect;/*diagram's extents*/
   real zoom;/*zoom factor for thumbnail rendering*/
 
-  GtkStyle *style;
+  DiaCairoRenderer *renderer;
 
   memset (nav, 0, sizeof(NavigationWindow));
   /*--Retrieve the diagram's data*/
@@ -234,12 +233,6 @@ on_button_navigation_popup_pressed (GtkButton * button, gpointer _ddisp)
   gtk_widget_show (frame);
   gtk_widget_show (popup_window);
 
-  /*miniframe style*/
-  nav->gc = gdk_gc_new (gtk_widget_get_window (drawing_area));
-  gdk_gc_set_line_attributes (nav->gc,
-                              FRAME_THICKNESS,
-                              GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
-
   /*cursor*/
   if (MIN(nav->frame_h, nav->frame_w) > STD_CURSOR_MIN) {
     nav->cursor = gdk_cursor_new (GDK_FLEUR);
@@ -261,34 +254,18 @@ on_button_navigation_popup_pressed (GtkButton * button, gpointer _ddisp)
                     nav->cursor,
                     GDK_CURRENT_TIME);
 
-  /*buffer to draw the thumbnail on*/
-  nav->buffer = gdk_pixmap_new (gtk_widget_get_window (drawing_area),
-                                nav->width, nav->height, -1);
-  style = gtk_widget_get_style (drawing_area);
-  gdk_draw_rectangle (nav->buffer,
-                      style->black_gc, TRUE,
-                      0, 0, nav->width, nav->height);
-
-  {/*--Render the thumbnail*/
-    DiaGdkRenderer *renderer;
-    GdkColor color;
-
-    renderer = g_object_new (DIA_TYPE_GDK_RENDERER, NULL);
-    renderer->transform = dia_transform_new (&rect, &zoom);
-    renderer->pixmap = nav->buffer;/*render on the thumbnail buffer*/
-    renderer->gc = gdk_gc_new (nav->buffer);
-
-    /*Background color*/
-    color_convert (&data->bg_color, &color);
-    gdk_gc_set_foreground (renderer->gc, &color);
-    gdk_draw_rectangle (renderer->pixmap, renderer->gc, 1, 0, 0, nav->width, nav->height);
-
-    /*render the data*/
-    data_render (data, DIA_RENDERER (renderer), NULL, NULL, NULL);
-
-    g_object_ref (renderer->pixmap);
-    g_object_unref (renderer);  
-  }
+  /* surface to draw the thumbnail on */
+  nav->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+                                            nav->width, nav->height);
+
+  renderer = g_object_new (g_type_from_name ("DiaCairoRenderer"), NULL);
+  renderer->scale = zoom;
+  renderer->surface = cairo_surface_reference (nav->surface);
+
+  /*render the data*/
+  data_render (data, DIA_RENDERER (renderer), NULL, NULL, NULL);
+
+  g_object_unref (renderer);
 
   nav->is_first_expose = TRUE;/*set to request to draw the miniframe*/
 }
@@ -313,38 +290,35 @@ reset_sc_adj (GtkAdjustment * adj, gdouble lower, gdouble upper, gdouble page)
 static gboolean
 on_da_expose_event (GtkWidget * widget, GdkEventExpose * event, gpointer unused)
 {
-  GtkStyle *style = gtk_widget_get_style (widget);
-  /*refresh the part outdated by the event*/
-  gdk_draw_drawable (gtk_widget_get_window (widget),
-#if GTK_CHECK_VERSION(2,18,0)
-                     style->fg_gc[gtk_widget_get_state (widget)],
-#else
-                     style->fg_gc[GTK_WIDGET_STATE (widget)],
-#endif
-                     GDK_PIXMAP(nav->buffer),
-                     event->area.x, event->area.y,
-                     event->area.x, event->area.y,
-                     event->area.width, event->area.height);
-
-  /*the first time, display the current display's state*/
-  if(nav->is_first_expose){
+  GtkAdjustment * adj;
+  int x, y;
+  cairo_t *ctx;
 
-    GtkAdjustment * adj;
-    int x, y;
+  ctx = gdk_cairo_create (gtk_widget_get_window (widget));
+  cairo_set_line_width (ctx, FRAME_THICKNESS);
+  cairo_set_line_cap (ctx, CAIRO_LINE_CAP_BUTT);
+  cairo_set_line_join (ctx, CAIRO_LINE_JOIN_MITER);
 
-    adj = nav->ddisp->hsbdata;
-    x = (adj->value - adj->lower) / (adj->upper - adj->lower) * (nav->width) +1;
+  /*refresh the part outdated by the event*/
+  cairo_set_source_surface (ctx, nav->surface,
+                            event->area.x, event->area.y);
+  cairo_rectangle (ctx, event->area.x, event->area.y,
+                        event->area.width, event->area.height);
+  cairo_fill (ctx);
 
-    adj = nav->ddisp->vsbdata;
-    y = (adj->value - adj->lower) / (adj->upper - adj->lower) * (nav->height) +1;
+  adj = nav->ddisp->hsbdata;
+  x = (adj->value - adj->lower) / (adj->upper - adj->lower) * (nav->width) +1;
 
-    /*draw directly on the window, do not buffer the miniframe*/
-    gdk_draw_rectangle (gtk_widget_get_window (widget),
-                        nav->gc, FALSE,
-                        x, y, nav->frame_w, nav->frame_h);
+  adj = nav->ddisp->vsbdata;
+  y = (adj->value - adj->lower) / (adj->upper - adj->lower) * (nav->height) +1;
 
-    nav->is_first_expose = FALSE;
-  }
+  /*draw directly on the window, do not buffer the miniframe*/
+  cairo_set_source_rgb (ctx, 0, 0, 0);
+  cairo_rectangle (ctx, x, y, nav->frame_w, nav->frame_h);
+  cairo_stroke (ctx);
+
+  nav->is_first_expose = FALSE;
+  
   return FALSE;
 }
 
@@ -354,7 +328,6 @@ on_da_motion_notify_event (GtkWidget * drawing_area, GdkEventMotion * event, gpo
 {
   GtkAdjustment * adj;
   gboolean value_changed;
-  GtkStyle *style;
 
   int w = nav->frame_w;
   int h = nav->frame_h;
@@ -399,22 +372,9 @@ on_da_motion_notify_event (GtkWidget * drawing_area, GdkEventMotion * event, gpo
   }
   if (value_changed) gtk_adjustment_value_changed(adj);
 
+  /* Trigger redraw */
+  gdk_window_invalidate_rect (gtk_widget_get_window (drawing_area), NULL, TRUE);
 
-/*--Draw the miniframe*/
-/*refresh from the buffer*/
-  style = gtk_widget_get_style (drawing_area);
-  gdk_draw_drawable (gtk_widget_get_window (drawing_area),
-#if GTK_CHECK_VERSION(2,18,0)
-                     style->fg_gc[gtk_widget_get_state (drawing_area)],
-#else
-                     style->fg_gc[GTK_WIDGET_STATE (drawing_area)],
-#endif
-                     GDK_PIXMAP(nav->buffer),
-                     0, 0, 0, 0, nav->width, nav->height);
-/*draw directly on the window, do not buffer the miniframe*/
-  gdk_draw_rectangle (gtk_widget_get_window (drawing_area),
-                      nav->gc, FALSE,
-                      x, y, w, h);
   return FALSE;
 }
 
@@ -425,14 +385,6 @@ on_da_button_release_event (GtkWidget * widget, GdkEventButton * event, gpointer
   /* Apparently there are circumstances where this is run twice for one popup 
    * Protected calls to avoid crashing on second pass.
    */
-  if (nav->buffer)
-    g_object_unref (nav->buffer);
-  nav->buffer = NULL;
-
-  if (nav->gc)
-    g_object_unref (nav->gc);
-  nav->gc = NULL;
-
   if (nav->cursor)
     gdk_cursor_unref (nav->cursor);
   nav->cursor = NULL;
diff --git a/lib/renderer/diacairo-interactive.c b/lib/renderer/diacairo-interactive.c
index c586de37..80334d7e 100644
--- a/lib/renderer/diacairo-interactive.c
+++ b/lib/renderer/diacairo-interactive.c
@@ -31,15 +31,6 @@
 #include "object.h"
 #include "textline.h"
 
-/* There is a variant prepared for GTK+3 
- * but it seems to be sligly slower than the original version.
- */
-#if GTK_CHECK_VERSION(3,0,0)
-#define DIA_CAIRO_WITH_PIXMAP 0
-#else
-#define DIA_CAIRO_WITH_PIXMAP 1
-#endif
-
 #define DIA_TYPE_CAIRO_INTERACTIVE_RENDERER           (dia_cairo_interactive_renderer_get_type ())
 #define DIA_CAIRO_INTERACTIVE_RENDERER(obj)           (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
DIA_TYPE_CAIRO_INTERACTIVE_RENDERER, DiaCairoInteractiveRenderer))
 #define DIA_CAIRO_INTERACTIVE_RENDERER_CLASS(klass)   (G_TYPE_CHECK_CLASS_CAST ((klass), 
DIA_TYPE_CAIRO_INTERACTIVE_RENDERER, DiaCairoInteractiveRendererClass))
@@ -59,12 +50,7 @@ struct _DiaCairoInteractiveRenderer
   Rectangle *visible;
   real *zoom_factor;
 
-#if DIA_CAIRO_WITH_PIXMAP
-  GdkPixmap *pixmap;              /* The pixmap shown in this display  */
-  GdkGC *gc;
-#else
   cairo_surface_t *pixmap;        /* The pixmap shown in this display  */
-#endif
   guint32 width;                  /* The width of the pixmap in pixels */
   guint32 height;                 /* The height of the pixmap in pixels */
   GdkRegion *clip_region;
@@ -153,18 +139,14 @@ cairo_interactive_renderer_init (DiaCairoInteractiveRenderer *object, void *p)
 static void
 cairo_interactive_renderer_finalize (GObject *object)
 {
-#if !DIA_CAIRO_WITH_PIXMAP
   DiaCairoInteractiveRenderer *renderer = DIA_CAIRO_INTERACTIVE_RENDERER (object);
-#endif
   DiaCairoRenderer *base_renderer = DIA_CAIRO_RENDERER (object);
 
   if (base_renderer->cr)
     cairo_destroy (base_renderer->cr);
   base_renderer->cr = NULL;
-#if !DIA_CAIRO_WITH_PIXMAP
   if (renderer->pixmap)
     cairo_surface_destroy (renderer->pixmap);
-#endif
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
@@ -342,11 +324,7 @@ begin_render(DiaRenderer *self, const Rectangle *update)
   DiaCairoRenderer *base_renderer = DIA_CAIRO_RENDERER (self);
 
   g_return_if_fail (base_renderer->cr == NULL);
-#if DIA_CAIRO_WITH_PIXMAP
-  base_renderer->cr = gdk_cairo_create(renderer->pixmap);
-#else
   base_renderer->cr = cairo_create(renderer->pixmap);
-#endif
 
   /* Setup clipping for this sequence of render operations */
   /* Must be done before the scaling because the clip is in pixel coords */
@@ -367,14 +345,12 @@ begin_render(DiaRenderer *self, const Rectangle *update)
 
   cairo_set_fill_rule (base_renderer->cr, CAIRO_FILL_RULE_EVEN_ODD);
 
-#if !DIA_CAIRO_WITH_PIXMAP
   /* should we set the background color? Or do nothing at all? */
   /* if this is drawn you can see 'clipping in action', outside of the clip it gets yellow ;) */
   cairo_set_source_rgba (base_renderer->cr, 1.0, 1.0, .8, 1.0);
   cairo_set_operator (base_renderer->cr, CAIRO_OPERATOR_OVER);
   cairo_rectangle (base_renderer->cr, 0, 0, renderer->width, renderer->height);
   cairo_fill (base_renderer->cr);
-#endif
 }
 
 static void
@@ -477,38 +453,12 @@ set_size(DiaRenderer *object, gpointer window,
 
   renderer->width = width;
   renderer->height = height;
-#if DIA_CAIRO_WITH_PIXMAP
-  if (renderer->pixmap != NULL)
-    g_object_unref(renderer->pixmap);
-
-  /* TODO: we can probably get rid of this extra pixmap and just draw directly
-   * to what gdk_cairo_create() gives us for the window
-   */
-  renderer->pixmap = gdk_pixmap_new(GDK_WINDOW(window),  width, height, -1);
-#else
-# if GTK_CHECK_VERSION(2,22,0)
   renderer->pixmap = gdk_window_create_similar_surface (GDK_WINDOW (window),
-                                                       CAIRO_CONTENT_COLOR,
-                                                       width, height);
-# else
-  {
-    cairo_rectangle_t extents;
-
-    extents.x = 0;
-    extents.y = 0;
-    extents.width = width;
-    extents.height = height;
-    renderer->pixmap = cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA, &extents);
-  }
-# endif
-#endif
+                                                        CAIRO_CONTENT_COLOR,
+                                                        width, height);
 
   if (base_renderer->surface != NULL)
     cairo_surface_destroy(base_renderer->surface);
-#if DIA_CAIRO_WITH_PIXMAP
-  if (renderer->gc == NULL)
-    renderer->gc = gdk_gc_new(renderer->pixmap);
-#endif
 }
 
 static void
@@ -516,19 +466,6 @@ copy_to_window (DiaRenderer *object, gpointer window,
                 int x, int y, int width, int height)
 {
   DiaCairoInteractiveRenderer *renderer = DIA_CAIRO_INTERACTIVE_RENDERER (object);
-#if DIA_CAIRO_WITH_PIXMAP
-  static GdkGC *copy_gc = NULL;
-  
-  if (!copy_gc)
-    copy_gc = gdk_gc_new(window);
-
-  gdk_draw_drawable (GDK_WINDOW(window),
-                     copy_gc,
-                     renderer->pixmap,
-                     x, y,
-                     x, y,
-                     width > 0 ? width : -width, height > 0 ? height : -height);
-#else
   cairo_t *cr;
 
   cr = gdk_cairo_create (GDK_WINDOW(window));
@@ -537,7 +474,6 @@ copy_to_window (DiaRenderer *object, gpointer window,
   cairo_clip (cr);
   cairo_paint (cr);
   cairo_destroy (cr);
-#endif
 }
 
 static void
@@ -549,9 +485,6 @@ clip_region_clear(DiaRenderer *object)
     gdk_region_destroy(renderer->clip_region);
 
   renderer->clip_region =  gdk_region_new();
-#if DIA_CAIRO_WITH_PIXMAP
-  gdk_gc_set_clip_region(renderer->gc, renderer->clip_region);
-#endif
 }
 
 static void
@@ -576,9 +509,6 @@ clip_region_add_rect(DiaRenderer *object,
   clip_rect.height = y2 - y1 + 1;
 
   gdk_region_union_with_rect(renderer->clip_region, &clip_rect);
-#if DIA_CAIRO_WITH_PIXMAP
-  gdk_gc_set_clip_region(renderer->gc, renderer->clip_region);
-#endif
 }
 
 static void
@@ -632,18 +562,6 @@ fill_pixel_rect(DiaRenderer *object,
                int width, int height,
                Color *color)
 {
-#if DIA_CAIRO_WITH_PIXMAP
-  /* if we do it with cairo there is something wrong with the clipping? */
-  DiaCairoInteractiveRenderer *renderer = DIA_CAIRO_INTERACTIVE_RENDERER (object);
-  GdkGC *gc = renderer->gc;
-  GdkColor gdkcolor;
-    
-  color_convert(color, &gdkcolor);
-  gdk_gc_set_foreground(gc, &gdkcolor);
-
-  gdk_draw_rectangle (renderer->pixmap, gc, TRUE,
-                     x, y,  width, height);
-#else
   DiaCairoRenderer *renderer = DIA_CAIRO_RENDERER (object);
   double x1u = x + .5, y1u = y + .5, x2u = x + width + .5, y2u = y + height + .5;
   double lw[2];
@@ -658,7 +576,6 @@ fill_pixel_rect(DiaRenderer *object,
   cairo_set_source_rgba (renderer->cr, color->red, color->green, color->blue, color->alpha);
   cairo_rectangle (renderer->cr, x1u, y1u, x2u - x1u, y2u - y1u);
   cairo_fill (renderer->cr);
-#endif
 }
 
 DiaRenderer *


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]