[sysprof/wip/visualizers] visualizer-selection: initial visualizer selection support



commit 70aeeb66bcf851589e20d19350639396f145e8b6
Author: Christian Hergert <chergert redhat com>
Date:   Sat Oct 8 13:40:12 2016 -0700

    visualizer-selection: initial visualizer selection support
    
    This gets started on allowing the user to select a region.
    Nothing is yet responding to the changes in selection, but
    that can come later.
    
    Ideally, we will truncate (or mark rows invisible/insensitive)
    if they do not fall within the selected time region.

 lib/Makefile.am                        |    2 +
 lib/resources/css/shared.css           |   10 +
 lib/resources/libsysprof.gresource.xml |    2 +
 lib/resources/ui/sp-visualizer-view.ui |    3 +
 lib/sp-visualizer-row.c                |    2 +
 lib/sp-visualizer-selection.c          |  214 +++++++++++++++++
 lib/sp-visualizer-selection.h          |   49 ++++
 lib/sp-visualizer-view.c               |  396 ++++++++++++++++++++++++++++---
 lib/sp-visualizer-view.h               |   16 +-
 lib/sysprof-ui.h                       |    1 +
 10 files changed, 649 insertions(+), 46 deletions(-)
---
diff --git a/lib/Makefile.am b/lib/Makefile.am
index af2081e..42803c2 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -151,6 +151,7 @@ uiheaders_DATA = \
        sp-profiler-menu-button.h \
        sp-recording-state-view.h \
        sp-visualizer-row.h \
+       sp-visualizer-selection.h \
        sp-visualizer-view.h \
        sp-zoom-manager.h \
        sysprof-ui.h \
@@ -178,6 +179,7 @@ libsysprof_ui_@API_VERSION@_la_SOURCES = \
        sp-visualizer-list.c \
        sp-visualizer-list.h \
        sp-visualizer-row.c \
+       sp-visualizer-selection.c \
        sp-visualizer-ticks.c \
        sp-visualizer-ticks.h \
        sp-visualizer-ticks-private.h \
diff --git a/lib/resources/css/shared.css b/lib/resources/css/shared.css
new file mode 100644
index 0000000..2546f89
--- /dev/null
+++ b/lib/resources/css/shared.css
@@ -0,0 +1,10 @@
+visualizers.selection {
+  background: none;
+  background-color: alpha(@theme_selected_bg_color, 0.35);
+  border: 1px solid @theme_selected_bg_color;
+}
+visualizers.selection:backdrop {
+  background: none;
+  background-color: alpha(@theme_selected_bg_color, 0.15);
+  border: none;
+}
diff --git a/lib/resources/libsysprof.gresource.xml b/lib/resources/libsysprof.gresource.xml
index 5854eaf..a6f73c4 100644
--- a/lib/resources/libsysprof.gresource.xml
+++ b/lib/resources/libsysprof.gresource.xml
@@ -1,6 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <gresources>
   <gresource prefix="/org/gnome/sysprof">
+    <file compressed="true">css/shared.css</file>
+
     <file compressed="true">ui/sp-callgraph-view.ui</file>
     <file compressed="true">ui/sp-empty-state-view.ui</file>
     <file compressed="true">ui/sp-failed-state-view.ui</file>
diff --git a/lib/resources/ui/sp-visualizer-view.ui b/lib/resources/ui/sp-visualizer-view.ui
index 742f9a1..9254397 100644
--- a/lib/resources/ui/sp-visualizer-view.ui
+++ b/lib/resources/ui/sp-visualizer-view.ui
@@ -10,6 +10,9 @@
             <property name="hexpand">true</property>
             <property name="visible">true</property>
           </object>
+          <packing>
+            <property name="pass-through">true</property>
+          </packing>
         </child>
         <child>
           <object class="GtkScrolledWindow" id="scroller">
diff --git a/lib/sp-visualizer-row.c b/lib/sp-visualizer-row.c
index 0bf420b..3272ccb 100644
--- a/lib/sp-visualizer-row.c
+++ b/lib/sp-visualizer-row.c
@@ -159,6 +159,8 @@ sp_visualizer_row_class_init (SpVisualizerRowClass *klass)
 static void
 sp_visualizer_row_init (SpVisualizerRow *self)
 {
+  gtk_list_box_row_set_activatable (GTK_LIST_BOX_ROW (self), FALSE);
+  gtk_list_box_row_set_selectable (GTK_LIST_BOX_ROW (self), FALSE);
 }
 
 static void
diff --git a/lib/sp-visualizer-selection.c b/lib/sp-visualizer-selection.c
new file mode 100644
index 0000000..062d480
--- /dev/null
+++ b/lib/sp-visualizer-selection.c
@@ -0,0 +1,214 @@
+/* sp-visualizer-selection.c
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "sp-visualizer-selection"
+
+#include "sp-visualizer-selection.h"
+
+struct _SpVisualizerSelection
+{
+  GObject  parent_instance;
+  GArray  *ranges;
+};
+
+typedef struct
+{
+  gint64 begin;
+  gint64 end;
+} Range;
+
+G_DEFINE_TYPE (SpVisualizerSelection, sp_visualizer_selection, G_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_HAS_SELECTION,
+  N_PROPS
+};
+
+enum {
+  CHANGED,
+  N_SIGNALS
+};
+
+static GParamSpec *properties [N_PROPS];
+static guint signals [N_SIGNALS];
+
+static inline void
+int64_swap (gint64 *a,
+            gint64 *b)
+{
+  if (*a > *b)
+    {
+      gint64 tmp = *a;
+      *a = *b;
+      *b = tmp;
+    }
+}
+
+static void
+sp_visualizer_selection_finalize (GObject *object)
+{
+  SpVisualizerSelection *self = (SpVisualizerSelection *)object;
+
+  g_clear_pointer (&self->ranges, g_array_unref);
+
+  G_OBJECT_CLASS (sp_visualizer_selection_parent_class)->finalize (object);
+}
+
+static void
+sp_visualizer_selection_get_property (GObject    *object,
+                                      guint       prop_id,
+                                      GValue     *value,
+                                      GParamSpec *pspec)
+{
+  SpVisualizerSelection *self = SP_VISUALIZER_SELECTION (object);
+
+  switch (prop_id)
+    {
+    case PROP_HAS_SELECTION:
+      g_value_set_boolean (value, sp_visualizer_selection_get_has_selection (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+sp_visualizer_selection_class_init (SpVisualizerSelectionClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = sp_visualizer_selection_finalize;
+  object_class->get_property = sp_visualizer_selection_get_property;
+
+  properties [PROP_HAS_SELECTION] =
+    g_param_spec_boolean ("has-selection",
+                          "Has Selection",
+                          "Has Selection",
+                          FALSE,
+                          (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+
+  /**
+   * SpVisualizerSelection::changed:
+   *
+   * This signal is emitted when the selection has changed.
+   */
+  signals [CHANGED] =
+    g_signal_new ("changed",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  0, NULL, NULL, NULL, G_TYPE_NONE, 0);
+}
+
+static void
+sp_visualizer_selection_init (SpVisualizerSelection *self)
+{
+  self->ranges = g_array_new (FALSE, FALSE, sizeof (Range));
+}
+
+gboolean
+sp_visualizer_selection_get_has_selection (SpVisualizerSelection *self)
+{
+  g_return_val_if_fail (SP_IS_VISUALIZER_SELECTION (self), FALSE);
+
+  return self->ranges->len > 0;
+}
+
+/**
+ * sp_visualizer_selection_foreach:
+ * @self: A #SpVisualizerSelection
+ * @foreach_func: (scope call): a callback for each range
+ * @user_data: user data for @foreach_func
+ *
+ * Calls @foreach_func for every selected range.
+ */
+void
+sp_visualizer_selection_foreach (SpVisualizerSelection            *self,
+                                 SpVisualizerSelectionForeachFunc  foreach_func,
+                                 gpointer                          user_data)
+{
+  g_return_if_fail (SP_IS_VISUALIZER_SELECTION (self));
+  g_return_if_fail (foreach_func != NULL);
+
+  for (guint i = 0; i < self->ranges->len; i++)
+    {
+      const Range *range = &g_array_index (self->ranges, Range, i);
+      foreach_func (self, range->begin, range->end, user_data);
+    }
+}
+
+void
+sp_visualizer_selection_select_range (SpVisualizerSelection *self,
+                                      gint64                 begin_time,
+                                      gint64                 end_time)
+{
+  Range range = { 0 };
+
+  g_return_if_fail (SP_IS_VISUALIZER_SELECTION (self));
+
+  int64_swap (&begin_time, &end_time);
+
+  range.begin = begin_time;
+  range.end = end_time;
+
+  g_array_append_val (self->ranges, range);
+
+  if (self->ranges->len == 1)
+    g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_HAS_SELECTION]);
+  g_signal_emit (self, signals [CHANGED], 0);
+}
+
+void
+sp_visualizer_selection_unselect_range (SpVisualizerSelection *self,
+                                        gint64                 begin,
+                                        gint64                 end)
+{
+  g_return_if_fail (SP_IS_VISUALIZER_SELECTION (self));
+
+  int64_swap (&begin, &end);
+
+  for (guint i = 0; i < self->ranges->len; i++)
+    {
+      const Range *range = &g_array_index (self->ranges, Range, i);
+
+      if (range->begin == begin && range->end == end)
+        {
+          g_array_remove_index (self->ranges, i);
+          if (self->ranges->len == 0)
+            g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_HAS_SELECTION]);
+          g_signal_emit (self, signals [CHANGED], 0);
+          break;
+        }
+    }
+}
+
+void
+sp_visualizer_selection_unselect_all (SpVisualizerSelection *self)
+{
+  g_return_if_fail (SP_IS_VISUALIZER_SELECTION (self));
+
+  if (self->ranges->len > 0)
+    {
+      g_array_remove_range (self->ranges, 0, self->ranges->len);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_HAS_SELECTION]);
+      g_signal_emit (self, signals [CHANGED], 0);
+    }
+}
diff --git a/lib/sp-visualizer-selection.h b/lib/sp-visualizer-selection.h
new file mode 100644
index 0000000..76a7867
--- /dev/null
+++ b/lib/sp-visualizer-selection.h
@@ -0,0 +1,49 @@
+/* sp-visualizer-selection.h
+ *
+ * Copyright (C) 2016 Christian Hergert <chergert redhat com>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SP_VISUALIZER_SELECTION_H
+#define SP_VISUALIZER_SELECTION_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define SP_TYPE_VISUALIZER_SELECTION (sp_visualizer_selection_get_type())
+
+G_DECLARE_FINAL_TYPE (SpVisualizerSelection, sp_visualizer_selection, SP, VISUALIZER_SELECTION, 
GtkDrawingArea)
+
+typedef void (*SpVisualizerSelectionForeachFunc) (SpVisualizerSelection *self,
+                                                  gint64                 begin_time,
+                                                  gint64                 end_time,
+                                                  gpointer               user_data);
+
+gboolean sp_visualizer_selection_get_has_selection (SpVisualizerSelection            *self);
+void     sp_visualizer_selection_select_range      (SpVisualizerSelection            *self,
+                                                    gint64                            begin_time,
+                                                    gint64                            end_time);
+void     sp_visualizer_selection_unselect_range    (SpVisualizerSelection            *self,
+                                                    gint64                            begin,
+                                                    gint64                            end);
+void     sp_visualizer_selection_unselect_all      (SpVisualizerSelection            *self);
+void     sp_visualizer_selection_foreach           (SpVisualizerSelection            *self,
+                                                    SpVisualizerSelectionForeachFunc  foreach_func,
+                                                    gpointer                          user_data);
+
+G_END_DECLS
+
+#endif /* SP_VISUALIZER_SELECTION_H */
diff --git a/lib/sp-visualizer-view.c b/lib/sp-visualizer-view.c
index e4bd557..e73192f 100644
--- a/lib/sp-visualizer-view.c
+++ b/lib/sp-visualizer-view.c
@@ -23,6 +23,7 @@
 #include "sp-visualizer-list.h"
 #include "sp-visualizer-row.h"
 #include "sp-visualizer-row-private.h"
+#include "sp-visualizer-selection.h"
 #include "sp-visualizer-ticks.h"
 #include "sp-visualizer-view.h"
 
@@ -31,14 +32,28 @@
 
 typedef struct
 {
-  SpCaptureReader   *reader;
-  SpZoomManager     *zoom_manager;
+  SpCaptureReader       *reader;
+  SpZoomManager         *zoom_manager;
+  SpVisualizerSelection *selection;
 
-  SpVisualizerList  *list;
-  GtkScrolledWindow *scroller;
-  SpVisualizerTicks *ticks;
+  SpVisualizerList      *list;
+  GtkScrolledWindow     *scroller;
+  SpVisualizerTicks     *ticks;
+
+  gint64                 drag_begin_at;
+  gint64                 drag_selection_at;
+
+  guint                  button_pressed : 1;
 } SpVisualizerViewPrivate;
 
+typedef struct
+{
+  SpVisualizerView *self;
+  GtkStyleContext  *style_context;
+  cairo_t          *cr;
+  GtkAllocation     alloc;
+} SelectionDraw;
+
 enum {
   PROP_0,
   PROP_READER,
@@ -61,32 +76,7 @@ G_DEFINE_TYPE_EXTENDED (SpVisualizerView, sp_visualizer_view, GTK_TYPE_BIN, 0,
 static GParamSpec *properties [N_PROPS];
 static guint signals [N_SIGNALS];
 static GtkBuildableIface *parent_buildable;
-
-static void
-sp_visualizer_view_row_added (SpVisualizerView *self,
-                              GtkWidget        *widget,
-                              SpVisualizerList *list)
-{
-  g_assert (SP_IS_VISUALIZER_VIEW (self));
-  g_assert (GTK_IS_WIDGET (widget));
-  g_assert (SP_IS_VISUALIZER_LIST (list));
-
-  if (SP_IS_VISUALIZER_ROW (widget))
-    g_signal_emit (self, signals [VISUALIZER_ADDED], 0, widget);
-}
-
-static void
-sp_visualizer_view_row_removed (SpVisualizerView *self,
-                                GtkWidget        *widget,
-                                SpVisualizerList *list)
-{
-  g_assert (SP_IS_VISUALIZER_VIEW (self));
-  g_assert (GTK_IS_WIDGET (widget));
-  g_assert (SP_IS_VISUALIZER_LIST (list));
-
-  if (SP_IS_VISUALIZER_ROW (widget))
-    g_signal_emit (self, signals [VISUALIZER_REMOVED], 0, widget);
-}
+static GtkCssProvider *css_provider;
 
 static void
 find_row1 (GtkWidget *widget,
@@ -98,8 +88,10 @@ find_row1 (GtkWidget *widget,
     *row1 = widget;
 }
 
-static void
-sp_visualizer_view_update_ticks (SpVisualizerView *self)
+static gint64
+get_time_from_coordinates (SpVisualizerView *self,
+                           gint              x,
+                           gint              y)
 {
   SpVisualizerViewPrivate *priv = sp_visualizer_view_get_instance_private (self);
   SpVisualizerRow *row1 = NULL;
@@ -114,26 +106,113 @@ sp_visualizer_view_update_ticks (SpVisualizerView *self)
   g_assert (SP_IS_VISUALIZER_VIEW (self));
 
   if (priv->reader == NULL)
-    return;
+    return 0;
+
+  gtk_widget_get_allocation (GTK_WIDGET (self), &alloc);
+
+  x -= alloc.x;
+  y -= alloc.y;
+
+  /*
+   * Find the first row so we can get an idea of how wide the graph is
+   * (ignoring spacing caused by the widget being wider than the data points.
+   */
+  gtk_container_foreach (GTK_CONTAINER (priv->list), find_row1, &row1);
+  if (!SP_IS_VISUALIZER_ROW (row1))
+    return 0;
+
+  hadjustment = gtk_scrolled_window_get_hadjustment (priv->scroller);
+  value = gtk_adjustment_get_value (hadjustment);
 
   begin_time = sp_capture_reader_get_start_time (priv->reader);
   end_time = sp_capture_reader_get_end_time (priv->reader);
 
-  gtk_container_foreach (GTK_CONTAINER (priv->list), find_row1, &row1);
+  graph_width = _sp_visualizer_row_get_graph_width (row1);
+  nsec_per_pixel = (end_time - begin_time) / (gdouble)graph_width;
+  begin_time += value * nsec_per_pixel;
+  end_time = begin_time + (alloc.width * nsec_per_pixel);
+
+  return begin_time + ((end_time - begin_time) / (gdouble)alloc.width * x);
+}
+
+static gint
+get_x_for_time_at (SpVisualizerView    *self,
+                   const GtkAllocation *alloc,
+                   gint64               time_at)
+{
+  SpVisualizerViewPrivate *priv = sp_visualizer_view_get_instance_private (self);
+  SpVisualizerRow *row1 = NULL;
+  GtkAdjustment *hadjustment;
+  gdouble nsec_per_pixel;
+  gdouble value;
+  gint64 begin_time;
+  gint64 end_time;
+  gint graph_width;
 
+  g_assert (SP_IS_VISUALIZER_VIEW (self));
+  g_assert (alloc != NULL);
+
+  /*
+   * Find the first row so we can get an idea of how wide the graph is
+   * (ignoring spacing caused by the widget being wider than the data points.
+   */
+  gtk_container_foreach (GTK_CONTAINER (priv->list), find_row1, &row1);
   if (!SP_IS_VISUALIZER_ROW (row1))
-    return;
+    return 0;
 
   hadjustment = gtk_scrolled_window_get_hadjustment (priv->scroller);
   value = gtk_adjustment_get_value (hadjustment);
 
-  gtk_widget_get_allocation (GTK_WIDGET (priv->ticks), &alloc);
+  begin_time = sp_capture_reader_get_start_time (priv->reader);
+  end_time = sp_capture_reader_get_end_time (priv->reader);
 
-  /* In practice, the adjustment is 1.0 per pixel. */
   graph_width = _sp_visualizer_row_get_graph_width (row1);
   nsec_per_pixel = (end_time - begin_time) / (gdouble)graph_width;
   begin_time += value * nsec_per_pixel;
-  end_time = begin_time + (alloc.width * nsec_per_pixel);
+
+  return ((time_at - begin_time) / nsec_per_pixel);
+}
+
+static void
+sp_visualizer_view_row_added (SpVisualizerView *self,
+                              GtkWidget        *widget,
+                              SpVisualizerList *list)
+{
+  g_assert (SP_IS_VISUALIZER_VIEW (self));
+  g_assert (GTK_IS_WIDGET (widget));
+  g_assert (SP_IS_VISUALIZER_LIST (list));
+
+  if (SP_IS_VISUALIZER_ROW (widget))
+    g_signal_emit (self, signals [VISUALIZER_ADDED], 0, widget);
+}
+
+static void
+sp_visualizer_view_row_removed (SpVisualizerView *self,
+                                GtkWidget        *widget,
+                                SpVisualizerList *list)
+{
+  g_assert (SP_IS_VISUALIZER_VIEW (self));
+  g_assert (GTK_IS_WIDGET (widget));
+  g_assert (SP_IS_VISUALIZER_LIST (list));
+
+  if (SP_IS_VISUALIZER_ROW (widget))
+    g_signal_emit (self, signals [VISUALIZER_REMOVED], 0, widget);
+}
+
+static void
+sp_visualizer_view_update_ticks (SpVisualizerView *self)
+{
+  SpVisualizerViewPrivate *priv = sp_visualizer_view_get_instance_private (self);
+  GtkAllocation alloc;
+  gint64 begin_time;
+  gint64 end_time;
+
+  g_assert (SP_IS_VISUALIZER_VIEW (self));
+
+  gtk_widget_get_allocation (GTK_WIDGET (self), &alloc);
+
+  begin_time = get_time_from_coordinates (self, alloc.x, alloc.y);
+  end_time = get_time_from_coordinates (self, alloc.x + alloc.width, alloc.y);
 
   sp_visualizer_ticks_set_time_range (priv->ticks, begin_time, end_time);
 }
@@ -163,6 +242,182 @@ sp_visualizer_view_size_allocate (GtkWidget     *widget,
 }
 
 static void
+draw_selection_cb (SpVisualizerSelection *selection,
+                   gint64                 range_begin,
+                   gint64                 range_end,
+                   gpointer               user_data)
+{
+  SelectionDraw *draw = user_data;
+  GdkRectangle area;
+
+  g_assert (SP_IS_VISUALIZER_SELECTION (selection));
+  g_assert (draw != NULL);
+  g_assert (draw->cr != NULL);
+  g_assert (SP_IS_VISUALIZER_VIEW (draw->self));
+
+  area.x = get_x_for_time_at (draw->self, &draw->alloc, range_begin);
+  area.width = get_x_for_time_at (draw->self, &draw->alloc, range_end) - area.x;
+  area.y = 0;
+  area.height = draw->alloc.height;
+
+  if (area.width < 0)
+    {
+      area.width = ABS (area.width);
+      area.x -= area.width;
+    }
+
+  gtk_render_background (draw->style_context, draw->cr, area.x, area.y, area.width, area.height);
+}
+
+static gboolean
+sp_visualizer_view_draw (GtkWidget *widget,
+                         cairo_t   *cr)
+{
+  SpVisualizerView *self = (SpVisualizerView *)widget;
+  SpVisualizerViewPrivate *priv = sp_visualizer_view_get_instance_private (self);
+  SelectionDraw draw = { 0 };
+  gboolean ret;
+
+  g_assert (GTK_IS_WIDGET (widget));
+  g_assert (cr != NULL);
+
+  draw.style_context = gtk_widget_get_style_context (widget);
+  draw.self = self;
+  draw.cr = cr;
+
+  gtk_widget_get_allocation (widget, &draw.alloc);
+
+  ret = GTK_WIDGET_CLASS (sp_visualizer_view_parent_class)->draw (widget, cr);
+
+  if (sp_visualizer_selection_get_has_selection (priv->selection) || priv->button_pressed)
+    {
+      gtk_style_context_add_class (draw.style_context, "selection");
+      sp_visualizer_selection_foreach (priv->selection, draw_selection_cb, &draw);
+      if (priv->button_pressed)
+        draw_selection_cb (priv->selection, priv->drag_begin_at, priv->drag_selection_at, &draw);
+      gtk_style_context_remove_class (draw.style_context, "selection");
+    }
+
+  return ret;
+}
+
+static gboolean
+sp_visualizer_view_list_button_press_event (SpVisualizerView *self,
+                                            GdkEventButton   *ev,
+                                            SpVisualizerList *list)
+{
+  SpVisualizerViewPrivate *priv = sp_visualizer_view_get_instance_private (self);
+
+  g_assert (SP_IS_VISUALIZER_VIEW (self));
+  g_assert (ev != NULL);
+  g_assert (SP_IS_VISUALIZER_LIST (list));
+
+  if (priv->reader == NULL)
+    return GDK_EVENT_PROPAGATE;
+
+  if (ev->button != GDK_BUTTON_PRIMARY)
+    {
+      if (sp_visualizer_selection_get_has_selection (priv->selection))
+        {
+          sp_visualizer_selection_unselect_all (priv->selection);
+          return GDK_EVENT_STOP;
+        }
+      return GDK_EVENT_PROPAGATE;
+    }
+
+  if ((ev->state & GDK_SHIFT_MASK) == 0)
+    sp_visualizer_selection_unselect_all (priv->selection);
+
+  priv->button_pressed = TRUE;
+
+  priv->drag_begin_at = get_time_from_coordinates (self, ev->x, ev->y);
+  priv->drag_selection_at = priv->drag_begin_at;
+
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+
+  return GDK_EVENT_PROPAGATE;
+}
+
+static gboolean
+sp_visualizer_view_list_button_release_event (SpVisualizerView *self,
+                                              GdkEventButton   *ev,
+                                              SpVisualizerList *list)
+{
+  SpVisualizerViewPrivate *priv = sp_visualizer_view_get_instance_private (self);
+
+  g_assert (SP_IS_VISUALIZER_VIEW (self));
+  g_assert (ev != NULL);
+  g_assert (SP_IS_VISUALIZER_LIST (list));
+
+  if (!priv->button_pressed || ev->button != GDK_BUTTON_PRIMARY)
+    return GDK_EVENT_PROPAGATE;
+
+  priv->button_pressed = FALSE;
+
+  if (priv->drag_begin_at != priv->drag_selection_at)
+    {
+      sp_visualizer_selection_select_range (priv->selection,
+                                            priv->drag_begin_at,
+                                            priv->drag_selection_at);
+      priv->drag_begin_at = -1;
+      priv->drag_selection_at = -1;
+    }
+
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+
+  return GDK_EVENT_STOP;
+}
+
+static gboolean
+sp_visualizer_view_list_motion_notify_event (SpVisualizerView *self,
+                                             GdkEventMotion   *ev,
+                                             SpVisualizerList *list)
+{
+  SpVisualizerViewPrivate *priv = sp_visualizer_view_get_instance_private (self);
+
+  g_assert (SP_IS_VISUALIZER_VIEW (self));
+  g_assert (ev != NULL);
+  g_assert (SP_IS_VISUALIZER_LIST (list));
+
+  if (!priv->button_pressed)
+    return GDK_EVENT_PROPAGATE;
+
+  priv->drag_selection_at = get_time_from_coordinates (self, ev->x, ev->y);
+
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+
+  return GDK_EVENT_PROPAGATE;
+}
+
+static void
+sp_visualizer_view_list_realize_after (SpVisualizerView *self,
+                                       SpVisualizerList *list)
+{
+  GdkDisplay *display;
+  GdkWindow *window;
+  GdkCursor *cursor;
+
+  g_assert (SP_IS_VISUALIZER_VIEW (self));
+  g_assert (SP_IS_VISUALIZER_LIST (list));
+
+  window = gtk_widget_get_window (GTK_WIDGET (list));
+  display = gdk_window_get_display (window);
+  cursor = gdk_cursor_new_from_name (display, "text");
+  gdk_window_set_cursor (window, cursor);
+  g_clear_object (&cursor);
+}
+
+static void
+sp_visualizer_view_selection_changed (SpVisualizerView      *self,
+                                      SpVisualizerSelection *selection)
+{
+  g_assert (SP_IS_VISUALIZER_VIEW (self));
+  g_assert (SP_IS_VISUALIZER_SELECTION (selection));
+
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
+static void
 sp_visualizer_view_finalize (GObject *object)
 {
   SpVisualizerView *self = (SpVisualizerView *)object;
@@ -170,6 +425,7 @@ sp_visualizer_view_finalize (GObject *object)
 
   g_clear_pointer (&priv->reader, sp_capture_reader_unref);
   g_clear_object (&priv->zoom_manager);
+  g_clear_object (&priv->selection);
 
   G_OBJECT_CLASS (sp_visualizer_view_parent_class)->finalize (object);
 }
@@ -230,6 +486,7 @@ sp_visualizer_view_class_init (SpVisualizerViewClass *klass)
   object_class->get_property = sp_visualizer_view_get_property;
   object_class->set_property = sp_visualizer_view_set_property;
 
+  widget_class->draw = sp_visualizer_view_draw;
   widget_class->size_allocate = sp_visualizer_view_size_allocate;
 
   properties [PROP_READER] =
@@ -270,6 +527,12 @@ sp_visualizer_view_class_init (SpVisualizerViewClass *klass)
   gtk_widget_class_bind_template_child_private (widget_class, SpVisualizerView, ticks);
 
   gtk_widget_class_set_css_name (widget_class, "visualizers");
+
+  css_provider = gtk_css_provider_new ();
+  gtk_css_provider_load_from_resource (css_provider, "/org/gnome/sysprof/css/shared.css");
+  gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
+                                             GTK_STYLE_PROVIDER (css_provider),
+                                             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION-1);
 }
 
 static void
@@ -278,8 +541,43 @@ sp_visualizer_view_init (SpVisualizerView *self)
   SpVisualizerViewPrivate *priv = sp_visualizer_view_get_instance_private (self);
   GtkAdjustment *hadjustment;
 
+  priv->drag_begin_at = -1;
+  priv->drag_selection_at = -1;
+
   gtk_widget_init_template (GTK_WIDGET (self));
 
+  priv->selection = g_object_new (SP_TYPE_VISUALIZER_SELECTION, NULL);
+
+  g_signal_connect_object (priv->selection,
+                           "changed",
+                           G_CALLBACK (sp_visualizer_view_selection_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (priv->list,
+                           "button-press-event",
+                           G_CALLBACK (sp_visualizer_view_list_button_press_event),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (priv->list,
+                           "button-release-event",
+                           G_CALLBACK (sp_visualizer_view_list_button_release_event),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (priv->list,
+                           "motion-notify-event",
+                           G_CALLBACK (sp_visualizer_view_list_motion_notify_event),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (priv->list,
+                           "realize",
+                           G_CALLBACK (sp_visualizer_view_list_realize_after),
+                           self,
+                           G_CONNECT_SWAPPED | G_CONNECT_AFTER);
+
   g_signal_connect_object (priv->list,
                            "add",
                            G_CALLBACK (sp_visualizer_view_row_added),
@@ -338,6 +636,8 @@ sp_visualizer_view_set_reader (SpVisualizerView *self,
 
           sp_visualizer_ticks_set_epoch (priv->ticks, begin_time);
           sp_visualizer_ticks_set_time_range (priv->ticks, begin_time, begin_time);
+
+          sp_visualizer_selection_unselect_all (priv->selection);
         }
 
       sp_visualizer_list_set_reader (priv->list, reader);
@@ -437,3 +737,21 @@ sp_visualizer_view_set_zoom_manager (SpVisualizerView *self,
       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ZOOM_MANAGER]);
     }
 }
+
+/**
+ * sp_visualizer_view_get_selection:
+ *
+ * Gets the #SpVisualizerSelection instance for the visualizer view.
+ * This can be used to alter the selection or selections of the visualizers.
+ *
+ * Returns: (transfer none): An #SpVisualizerSelection.
+ */
+SpVisualizerSelection *
+sp_visualizer_view_get_selection (SpVisualizerView *self)
+{
+  SpVisualizerViewPrivate *priv = sp_visualizer_view_get_instance_private (self);
+
+  g_return_val_if_fail (SP_IS_VISUALIZER_VIEW (self), NULL);
+
+  return priv->selection;
+}
diff --git a/lib/sp-visualizer-view.h b/lib/sp-visualizer-view.h
index ea49cb9..7d85633 100644
--- a/lib/sp-visualizer-view.h
+++ b/lib/sp-visualizer-view.h
@@ -22,6 +22,7 @@
 #include <gtk/gtk.h>
 
 #include "sp-visualizer-row.h"
+#include "sp-visualizer-selection.h"
 #include "sp-zoom-manager.h"
 
 G_BEGIN_DECLS
@@ -57,13 +58,14 @@ struct _SpVisualizerViewClass
   gpointer _reserved16;
 };
 
-GtkWidget       *sp_visualizer_view_new              (void);
-SpCaptureReader *sp_visualizer_view_get_reader       (SpVisualizerView *self);
-void             sp_visualizer_view_set_reader       (SpVisualizerView *self,
-                                                      SpCaptureReader  *reader);
-SpZoomManager   *sp_visualizer_view_get_zoom_manager (SpVisualizerView *self);
-void             sp_visualizer_view_set_zoom_manager (SpVisualizerView *self,
-                                                      SpZoomManager    *zoom_manager);
+GtkWidget             *sp_visualizer_view_new              (void);
+SpCaptureReader       *sp_visualizer_view_get_reader       (SpVisualizerView *self);
+void                   sp_visualizer_view_set_reader       (SpVisualizerView *self,
+                                                            SpCaptureReader  *reader);
+SpZoomManager         *sp_visualizer_view_get_zoom_manager (SpVisualizerView *self);
+void                   sp_visualizer_view_set_zoom_manager (SpVisualizerView *self,
+                                                            SpZoomManager    *zoom_manager);
+SpVisualizerSelection *sp_visualizer_view_get_selection    (SpVisualizerView *self);
 
 G_END_DECLS
 
diff --git a/lib/sysprof-ui.h b/lib/sysprof-ui.h
index e3c3bf9..21c1609 100644
--- a/lib/sysprof-ui.h
+++ b/lib/sysprof-ui.h
@@ -36,6 +36,7 @@ G_BEGIN_DECLS
 # include "sp-process-model-row.h"
 # include "sp-profiler-menu-button.h"
 # include "sp-visualizer-row.h"
+# include "sp-visualizer-selection.h"
 # include "sp-visualizer-view.h"
 # include "sp-zoom-manager.h"
 #undef SYSPROF_INSIDE


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