[gtk+/gtk-3-14] Editable cells demo: Add new row at cursor



commit c68490ee17941c6ca356a4aabb103053497df40b
Author: Phillip Wood <phillip wood dunelm org uk>
Date:   Sun Dec 22 18:17:32 2013 +0000

    Editable cells demo: Add new row at cursor
    
    Adding rows to the bottom of the list is confusing as you cannot see
    them if the window is small so it is not apparent that anything has
    happened. Fix this by adding the new row immediately below the current
    row and set the cursor on the new row so it is ready to be edited.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=721939

 demos/gtk-demo/editable_cells.c |   25 +++++++++++++++++++++----
 1 files changed, 21 insertions(+), 4 deletions(-)
---
diff --git a/demos/gtk-demo/editable_cells.c b/demos/gtk-demo/editable_cells.c
index 917a84e..18ff477 100644
--- a/demos/gtk-demo/editable_cells.c
+++ b/demos/gtk-demo/editable_cells.c
@@ -142,8 +142,11 @@ static void
 add_item (GtkWidget *button, gpointer data)
 {
   Item foo;
-  GtkTreeIter iter;
-  GtkTreeModel *model = (GtkTreeModel *)data;
+  GtkTreeIter current, iter;
+  GtkTreePath *path;
+  GtkTreeModel *model;
+  GtkTreeViewColumn *column;
+  GtkTreeView *treeview = (GtkTreeView *)data;
 
   g_return_if_fail (articles != NULL);
 
@@ -152,12 +155,26 @@ add_item (GtkWidget *button, gpointer data)
   foo.yummy = 50;
   g_array_append_vals (articles, &foo, 1);
 
-  gtk_list_store_append (GTK_LIST_STORE (model), &iter);
+  /* Insert a new row below the current one */
+  gtk_tree_view_get_cursor (treeview, &path, NULL);
+  model = gtk_tree_view_get_model (treeview);
+  gtk_tree_model_get_iter (model, &current, path);
+  gtk_tree_path_free (path);
+
+  /* Set the data for the new row */
+  gtk_list_store_insert_after (GTK_LIST_STORE (model), &iter, &current);
   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
                       COLUMN_ITEM_NUMBER, foo.number,
                       COLUMN_ITEM_PRODUCT, foo.product,
                       COLUMN_ITEM_YUMMY, foo.yummy,
                       -1);
+
+  /* Move focus to the new row */
+  path = gtk_tree_model_get_path (model, &iter);
+  column = gtk_tree_view_get_column (treeview, 0);
+  gtk_tree_view_set_cursor (treeview, path, column, FALSE);
+
+  gtk_tree_path_free (path);
 }
 
 static void
@@ -368,7 +385,7 @@ do_editable_cells (GtkWidget *do_widget)
 
       button = gtk_button_new_with_label ("Add item");
       g_signal_connect (button, "clicked",
-                        G_CALLBACK (add_item), items_model);
+                        G_CALLBACK (add_item), treeview);
       gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
 
       button = gtk_button_new_with_label ("Remove item");


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