[gtk/wip/otte/listview: 83/138] listview: Add gtk_list_view_get_position_at_y()



commit ce72883b3811341c5c910610d07246d445a5a30b
Author: Benjamin Otte <otte redhat com>
Date:   Wed Oct 16 21:31:00 2019 +0200

    listview: Add gtk_list_view_get_position_at_y()

 gtk/gtklistview.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)
---
diff --git a/gtk/gtklistview.c b/gtk/gtklistview.c
index 4f2ad0dbda..49bc462972 100644
--- a/gtk/gtklistview.c
+++ b/gtk/gtklistview.c
@@ -204,6 +204,37 @@ gtk_list_view_get_row_at_y (GtkListView *self,
   return row;
 }
 
+static int
+gtk_list_view_get_position_at_y (GtkListView *self,
+                                 int          y,
+                                 int         *offset,
+                                 int         *height)
+{
+  ListRow *row;
+  guint pos;
+  int remaining;
+
+  row = gtk_list_view_get_row_at_y (self, y, &remaining);
+  if (row == NULL)
+    {
+      pos = GTK_INVALID_LIST_POSITION;
+      if (offset)
+        *offset = 0;
+      if (height)
+        *height = 0;
+      return GTK_INVALID_LIST_POSITION;
+    }
+  pos = gtk_list_item_manager_get_item_position (self->item_manager, row);
+  g_assert (remaining < row->height * row->parent.n_items);
+  pos += remaining / row->height;
+  if (offset)
+    *offset = remaining % row->height;
+  if (height)
+    *height = row->height;
+
+  return pos;
+}
+
 static int
 list_row_get_y (GtkListView *self,
                 ListRow     *row)
@@ -279,18 +310,11 @@ gtk_list_view_adjustment_value_changed_cb (GtkAdjustment *adjustment,
 {
   if (adjustment == self->adjustment[self->orientation])
     {
-      ListRow *row;
       guint pos;
       int dy;
 
-      row = gtk_list_view_get_row_at_y (self, gtk_adjustment_get_value (adjustment), &dy);
-      if (row)
-        {
-          pos = gtk_list_item_manager_get_item_position (self->item_manager, row) + dy / row->height;
-        }
-      else
-        pos = 0;
-
+      pos = gtk_list_view_get_position_at_y (self, gtk_adjustment_get_value (adjustment), &dy, NULL);
+      g_return_if_fail (pos != GTK_INVALID_LIST_POSITION);
       gtk_list_view_set_anchor (self, pos, 0);
     }
   


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