[dia/dia-next: 53/59] Reimplement selection box



commit a173670e1cf5b6d01e13b44986bfb6b5fc563ff6
Author: Zander Brown <zbrown gnome org>
Date:   Mon Jan 7 21:39:26 2019 +0000

    Reimplement selection box

 app/modify_tool.c                   | 99 +++++++++++++++----------------------
 lib/diainteractiverenderer.c        | 19 ++++++-
 lib/diarenderer.h                   | 22 +++++++--
 lib/renderer/diacairo-interactive.c | 54 +++++++++++++++++++-
 4 files changed, 126 insertions(+), 68 deletions(-)
---
diff --git a/app/modify_tool.c b/app/modify_tool.c
index c2e95ddd..e3c49a2f 100644
--- a/app/modify_tool.c
+++ b/app/modify_tool.c
@@ -292,21 +292,14 @@ modify_button_press(ModifyTool *tool, GdkEventButton *event,
     tool->x1 = tool->x2 = (int) event->x;
     tool->y1 = tool->y2 = (int) event->y;
 
-
-      /* TODO: Selection Box
-      tool->gc = gdk_gc_new(gtk_widget_get_window(ddisp->canvas));
-      gdk_gc_set_line_attributes(tool->gc, 1, GDK_LINE_ON_OFF_DASH, 
-                                GDK_CAP_BUTT, GDK_JOIN_MITER);
-      gdk_gc_set_foreground(tool->gc, &color_white);
-      gdk_gc_set_function(tool->gc, GDK_XOR);
-      */
-
-    /* TODO: Selection Box
-    gdk_draw_rectangle (gtk_widget_get_window (ddisp->canvas), tool->gc, FALSE,
-                       tool->x1, tool->y1,
-                       tool->x2 - tool->x1, tool->y2 - tool->y1);
-    */
-
+    dia_interactive_renderer_set_selection (ddisp->renderer,
+                                            TRUE,
+                                            tool->x1,
+                                            tool->y1,
+                                            tool->x2 - tool->x1,
+                                            tool->y2 - tool->y1);
+    ddisplay_flush (ddisp);
+    
     gdk_pointer_grab (gtk_widget_get_window (ddisp->canvas), FALSE,
                       GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
                       NULL, NULL, event->time);
@@ -430,8 +423,9 @@ modify_move_already(ModifyTool *tool, DDisplay *ddisp, Point *to)
 }
 
 static void
-modify_motion(ModifyTool *tool, GdkEventMotion *event,
-             DDisplay *ddisp)
+modify_motion (ModifyTool     *tool,
+               GdkEventMotion *event,
+               DDisplay       *ddisp)
 {
   Point to;
   Point now, delta, full_delta;
@@ -472,9 +466,9 @@ modify_motion(ModifyTool *tool, GdkEventMotion *event,
       tool->orig_pos = g_new(Point, g_list_length(list));
       i=0;
       while (list != NULL) {
-       obj = (DiaObject *)  list->data;
-       tool->orig_pos[i] = obj->position;
-       list = g_list_next(list); i++;
+        obj = (DiaObject *)  list->data;
+        tool->orig_pos[i] = obj->position;
+        list = g_list_next(list); i++;
       }
       g_list_free (pla);
     }
@@ -552,18 +546,19 @@ modify_motion(ModifyTool *tool, GdkEventMotion *event,
     highlight_reset_all(ddisp->diagram);
     if ((tool->handle->connect_type != HANDLE_NONCONNECTABLE)) {
       /* Move to ConnectionPoint if near: */
-      connectionpoint =
-       object_find_connectpoint_display(ddisp, &to, tool->object, TRUE);
+      connectionpoint = object_find_connectpoint_display (ddisp,
+                                                          &to,
+                                                          tool->object, TRUE);
       if (connectionpoint != NULL) {
         DiaHighlightType type;
-       to = connectionpoint->pos;
-       if (connectionpoint->flags & CP_FLAGS_MAIN) {
+        to = connectionpoint->pos;
+        if (connectionpoint->flags & CP_FLAGS_MAIN) {
           type = DIA_HIGHLIGHT_CONNECTIONPOINT_MAIN;
-       } else {
+        } else {
           type = DIA_HIGHLIGHT_CONNECTIONPOINT;
-       }
-       highlight_object(connectionpoint->object, type, ddisp->diagram);
-       ddisplay_set_all_cursor(get_cursor(CURSOR_CONNECT));
+        }
+        highlight_object(connectionpoint->object, type, ddisp->diagram);
+        ddisplay_set_all_cursor(get_cursor(CURSOR_CONNECT));
       }
     }
     if (connectionpoint == NULL) {
@@ -575,9 +570,10 @@ modify_motion(ModifyTool *tool, GdkEventMotion *event,
     if (tool->break_connections) {
       /* break connections to the handle currently selected. */
       if (tool->handle->connected_to!=NULL) {
-       Change *change = undo_unconnect(ddisp->diagram, tool->object,
-                                       tool->handle);
-                                       
+        Change *change = undo_unconnect (ddisp->diagram,
+                                         tool->object,
+                                         tool->handle);
+
         (change->apply)(change, ddisp->diagram);
       }
     }
@@ -603,13 +599,13 @@ modify_motion(ModifyTool *tool, GdkEventMotion *event,
       guint context_id = gtk_statusbar_get_context_id (statusbar, "ObjectPos");
     
       if (tool->object) { /* play safe */
-       real w = tool->object->bounding_box.right - tool->object->bounding_box.left;
-       real h = tool->object->bounding_box.bottom - tool->object->bounding_box.top;
+        real w = tool->object->bounding_box.right - tool->object->bounding_box.left;
+        real h = tool->object->bounding_box.bottom - tool->object->bounding_box.top;
         postext = g_strdup_printf("%.3f, %.3f (%.3fx%.3f)", to.x, to.y, w, h);
       } else {
         postext = g_strdup_printf("%.3f, %.3f", to.x, to.y);
       }
-                              
+
       gtk_statusbar_pop (statusbar, context_id); 
       gtk_statusbar_push (statusbar, context_id, postext);
 
@@ -632,17 +628,6 @@ modify_motion(ModifyTool *tool, GdkEventMotion *event,
     diagram_flush(ddisp->diagram);
     break;
   case STATE_BOX_SELECT:
-
-    if (!auto_scroll && !tool->auto_scrolled)
-    {
-          /* TODO: Selection Box
-
-      gdk_draw_rectangle (gtk_widget_get_window (ddisp->canvas), tool->gc, FALSE,
-                         tool->x1, tool->y1,
-                         tool->x2 - tool->x1, tool->y2 - tool->y1);
-        */
-    }
-
     tool->end_box = to;
 
     ddisplay_transform_coords(ddisp,
@@ -654,16 +639,16 @@ modify_motion(ModifyTool *tool, GdkEventMotion *event,
                              MAX(tool->start_box.y, tool->end_box.y),
                              &tool->x2, &tool->y2);
 
-                /* TODO: Selection Box
-
+    dia_interactive_renderer_set_selection (ddisp->renderer,
+                                            TRUE,
+                                            tool->x1,
+                                            tool->y1,
+                                            tool->x2 - tool->x1,
+                                            tool->y2 - tool->y1);
+    ddisplay_flush (ddisp);
 
-    gdk_draw_rectangle (gtk_widget_get_window (ddisp->canvas), tool->gc, FALSE,
-                       tool->x1, tool->y1,
-                       tool->x2 - tool->x1, tool->y2 - tool->y1);
-      */
     break;
   case STATE_NONE:
-    
     break;
   default:
     message_error("Internal error: Strange state in modify_tool\n");
@@ -795,14 +780,8 @@ modify_button_release(ModifyTool *tool, GdkEventButton *event,
     
     gdk_pointer_ungrab (event->time);
     /* Remove last box: */
-    if (!tool->auto_scrolled) {
-          /* TODO: Selection Box
-
-      gdk_draw_rectangle (gtk_widget_get_window (ddisp->canvas), tool->gc, FALSE,
-                       tool->x1, tool->y1,
-                       tool->x2 - tool->x1, tool->y2 - tool->y1);
-      */
-    }
+    dia_interactive_renderer_set_selection (ddisp->renderer,
+                                            FALSE, 0, 0, 0, 0);
 
     {
       GList *list, *list_to_free;
diff --git a/lib/diainteractiverenderer.c b/lib/diainteractiverenderer.c
index 6d0976de..64e4d784 100644
--- a/lib/diainteractiverenderer.c
+++ b/lib/diainteractiverenderer.c
@@ -91,4 +91,21 @@ dia_interactive_renderer_paint (DiaRenderer *renderer,
   g_return_if_fail (irenderer->paint != NULL);
 
   irenderer->paint (renderer, ctx, width, height);
-}
\ No newline at end of file
+}
+
+void
+dia_interactive_renderer_set_selection (DiaRenderer *renderer,
+                                        gboolean     has_selection,
+                                        double       x,
+                                        double       y,
+                                        double       width,
+                                        double       height)
+{
+  DiaInteractiveRendererInterface *irenderer =
+    DIA_GET_INTERACTIVE_RENDERER_INTERFACE (renderer);
+  
+  g_return_if_fail (irenderer != NULL);
+  g_return_if_fail (irenderer->set_selection != NULL);
+
+  irenderer->set_selection (renderer, has_selection, x, y, width, height);
+}
diff --git a/lib/diarenderer.h b/lib/diarenderer.h
index 1f94a434..92be0d4e 100644
--- a/lib/diarenderer.h
+++ b/lib/diarenderer.h
@@ -313,15 +313,27 @@ struct _DiaInteractiveRendererInterface
   void (*draw_object_highlighted) (DiaRenderer *renderer,
                                   DiaObject *object,
                                   DiaHighlightType type);
+  /* Draw a selection box */
+  void (*set_selection)       (DiaRenderer *renderer,
+                               gboolean     has_selection,
+                               double       x,
+                               double       y,
+                               double       width,
+                               double       height);
 };
 
 GType dia_interactive_renderer_interface_get_type (void) G_GNUC_CONST;
 
-void dia_interactive_renderer_paint (DiaRenderer *renderer,
-                                     cairo_t     *ctx, 
-                                     int          width,
-                                     int          height);
-
+void dia_interactive_renderer_paint              (DiaRenderer *renderer,
+                                                  cairo_t     *ctx, 
+                                                  int          width,
+                                                  int          height);
+void dia_interactive_renderer_set_selection      (DiaRenderer *renderer,
+                                                  gboolean     has_selection,
+                                                  double       x,
+                                                  double       y,
+                                                  double       width,
+                                                  double       height);
 /*!
  * \brief Size adjustment to the given window
  * \memberof DiaInteractiveRendererInterface
diff --git a/lib/renderer/diacairo-interactive.c b/lib/renderer/diacairo-interactive.c
index 26a24165..6fa472b4 100644
--- a/lib/renderer/diacairo-interactive.c
+++ b/lib/renderer/diacairo-interactive.c
@@ -53,6 +53,13 @@ struct _DiaCairoInteractiveRenderer
   guint32 height;                 /* The height of the surface in pixels */
   cairo_region_t *clip_region;
 
+  /* Selection box */
+  gboolean has_selection;
+  double selection_x;
+  double selection_y;
+  double selection_width;
+  double selection_height;
+
   /** If non-NULL, this rendering is a highlighting with the given color. */
   GdkRGBA *highlight_color;
 };
@@ -262,6 +269,24 @@ draw_object_highlighted (DiaRenderer     *self,
   /* always reset when done with this object */
   interactive->highlight_color = NULL;
 }
+
+static void
+set_selection (DiaRenderer *renderer,
+               gboolean     has_selection,
+               double       x,
+               double       y,
+               double       width,
+               double       height)
+{
+  DiaCairoInteractiveRenderer *self = DIA_CAIRO_INTERACTIVE_RENDERER (renderer);
+
+  self->has_selection = has_selection;
+  self->selection_x = x;
+  self->selection_y = y;
+  self->selection_width = width;
+  self->selection_height = height;
+}
+
 static void 
 dia_cairo_interactive_renderer_iface_init (DiaInteractiveRendererInterface* iface)
 {
@@ -273,6 +298,7 @@ dia_cairo_interactive_renderer_iface_init (DiaInteractiveRendererInterface* ifac
   iface->paint = paint;
   iface->set_size = set_size;
   iface->draw_object_highlighted = draw_object_highlighted;
+  iface->set_selection = set_selection;
 }
 
 GType
@@ -467,12 +493,36 @@ paint (DiaRenderer *object,
        int          height)
 {
   DiaCairoInteractiveRenderer *renderer = DIA_CAIRO_INTERACTIVE_RENDERER (object);
-
+  double dashes[1] = {3};
   cairo_save (ctx);
+
   cairo_set_source_surface (ctx, renderer->surface, 0.0, 0.0);
   cairo_rectangle (ctx, 0, 0, width > 0 ? width : -width, height > 0 ? height : -height);
   cairo_clip (ctx);
   cairo_paint (ctx);
+
+  /* If there should be a selection rectange */
+  if (renderer->has_selection) {
+    /* Use a dark gray */
+    cairo_set_source_rgba (ctx, 0.1, 0.1, 0.1, 0.8);
+    cairo_set_line_cap (ctx, CAIRO_LINE_CAP_BUTT);
+    cairo_set_line_join (ctx, CAIRO_LINE_JOIN_MITER);
+    cairo_set_line_width (ctx, 1);
+    cairo_set_dash (ctx, dashes, 1, 0);
+
+    /* Set the selected area */
+    cairo_rectangle (ctx,
+                    renderer->selection_x,
+                    renderer->selection_y,
+                    renderer->selection_width,
+                    renderer->selection_height);
+    /* Add a dashed gray outline */
+    cairo_stroke_preserve (ctx);
+    /* Very light blue tint fill */
+    cairo_set_source_rgba (ctx, 0, 0, 0.85, 0.05);
+    cairo_fill (ctx);
+  }
+
   cairo_restore (ctx);
 }
 
@@ -582,4 +632,4 @@ DiaRenderer *
 dia_cairo_interactive_renderer_new ()
 {
   return g_object_new (DIA_TYPE_CAIRO_INTERACTIVE_RENDERER, NULL);
-}
\ No newline at end of file
+}


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