chiark / gitweb /
Make queue rearrangement debug output more readable.
[disorder] / disobedience / multidrag.c
index 6014e07d347e9e4f20493908126d18f7449c7b7b..8b28c94e53784f1c58a33869e2fe30bb07b53cd2 100644 (file)
@@ -1,19 +1,27 @@
 /*
- * This file is part of DisOrder
  * Copyright (C) 2009 Richard Kettlewell
  *
- * 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.
+ * Note that this license ONLY applies to multidrag.c and multidrag.h, not to
+ * the rest of DisOrder.
  *
- * 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/>.
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+ * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 /** @file disobedience/multidrag.c
  * @brief Drag multiple rows of a GtkTreeView
@@ -68,11 +76,11 @@ static void block_selection(GtkWidget *w, gboolean block,
   int *where = g_object_get_data(G_OBJECT(w), "multidrag-where");
   if(!where) {
     where = g_malloc(2 * sizeof (int));
-    g_object_set_data(G_OBJECT(w), "multidrag-where", where);
+    g_object_set_data_full(G_OBJECT(w), "multidrag-where", where,
+                           g_free);
   }
   where[0] = x;
   where[1] = y;
-  // TODO release 'where' when object is destroyed
 }
 
 static gboolean multidrag_button_press_event(GtkWidget *w,
@@ -88,21 +96,22 @@ static gboolean multidrag_button_press_event(GtkWidget *w,
   if(event->state & GDK_MODIFIER_MASK)
     return FALSE;
   /* We are only interested if a well-defined path is clicked */
-  GtkTreePath *path;
+  GtkTreePath *path = NULL;
   if(!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(w),
                                    event->x, event->y,
                                    &path,
                                    NULL,
                                    NULL, NULL))
     return FALSE;
-  //gtk_widget_grab_focus(w);    // TODO why??
   /* We are only interested if a selected row is clicked */
   GtkTreeSelection *s = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
-  if(!gtk_tree_selection_path_is_selected(s, path))
-    return FALSE;
-  /* We block subsequent selection changes and remember where the
-   * click was */
-  block_selection(w, FALSE, event->x, event->y);
+  if(gtk_tree_selection_path_is_selected(s, path)) {
+    /* We block subsequent selection changes and remember where the
+     * click was */
+    block_selection(w, FALSE, event->x, event->y);
+  }
+  if(path)
+    gtk_tree_path_free(path);
   return FALSE;                        /* propagate */
 }
 
@@ -121,7 +130,7 @@ static gboolean multidrag_button_release_event(GtkWidget *w,
     if(x == event->x && y == event->y) {
       // If the up-click is at the same location as the down-click,
       // it's not a drag.
-      GtkTreePath *path;
+      GtkTreePath *path = NULL;
       GtkTreeViewColumn *col;
       if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(w),
                                       event->x, event->y,
@@ -130,6 +139,8 @@ static gboolean multidrag_button_release_event(GtkWidget *w,
                                       NULL, NULL)) {
        gtk_tree_view_set_cursor(GTK_TREE_VIEW(w), path, col, FALSE);
       }
+      if(path)
+        gtk_tree_path_free(path);
     }
   }
   return FALSE;                        /* propagate */
@@ -138,6 +149,7 @@ static gboolean multidrag_button_release_event(GtkWidget *w,
 /** @brief State for multidrag_begin() and its callbacks */
 struct multidrag_begin_state {
   GtkTreeView *view;
+  multidrag_row_predicate *predicate;
   int rows;
   int index;
   GdkPixmap **pixmaps;
@@ -146,22 +158,24 @@ struct multidrag_begin_state {
 /** @brief Callback to construct a row pixmap */
 static void multidrag_make_row_pixmaps(GtkTreeModel attribute((unused)) *model,
                                       GtkTreePath *path,
-                                      GtkTreeIter attribute((unused)) *iter,
+                                      GtkTreeIter *iter,
                                       gpointer data) {
   struct multidrag_begin_state *qdbs = data;
 
-  qdbs->pixmaps[qdbs->index++]
-    = gtk_tree_view_create_row_drag_icon(qdbs->view, path);
+  if(qdbs->predicate(path, iter)) {
+    qdbs->pixmaps[qdbs->index++]
+      = gtk_tree_view_create_row_drag_icon(qdbs->view, path);
+  }
 }
 
 /** @brief Called when a drag operation starts
  * @param w Source widget (the tree view)
  * @param dc Drag context
- * @param user_data Not used
+ * @param user_data Row predicate
  */
 static void multidrag_drag_begin(GtkWidget *w,
                                 GdkDragContext attribute((unused)) *dc,
-                                gpointer attribute((unused)) user_data) {
+                                gpointer user_data) {
   struct multidrag_begin_state qdbs[1];
   GdkPixmap *icon;
   GtkTreeSelection *sel;
@@ -169,6 +183,7 @@ static void multidrag_drag_begin(GtkWidget *w,
   //fprintf(stderr, "drag-begin\n");
   memset(qdbs, 0, sizeof *qdbs);
   qdbs->view = GTK_TREE_VIEW(w);
+  qdbs->predicate = (multidrag_row_predicate *)user_data;
   sel = gtk_tree_view_get_selection(qdbs->view);
   /* Find out how many rows there are */
   if(!(qdbs->rows = gtk_tree_selection_count_selected_rows(sel)))
@@ -178,6 +193,8 @@ static void multidrag_drag_begin(GtkWidget *w,
   gtk_tree_selection_selected_foreach(sel,
                                       multidrag_make_row_pixmaps,
                                       qdbs);
+  /* Might not have used all rows */
+  qdbs->rows = qdbs->index;
   /* Determine the size of the final icon */
   int height = 0, width = 0;
   for(int n = 0; n < qdbs->rows; ++n) {
@@ -217,18 +234,32 @@ static void multidrag_drag_begin(GtkWidget *w,
                            NULL);
 }
 
+static gboolean multidrag_default_predicate(GtkTreePath attribute((unused)) *path,
+                                           GtkTreeIter attribute((unused)) *iter) {
+  return TRUE;
+}
+
 /** @brief Allow multi-row drag for @p w
  * @param w A GtkTreeView widget
+ * @param predicate Function called to test rows for draggability, or NULL
+ *
+ * Suppresses the restriction of selections when a drag is started, and
+ * intercepts drag-begin to construct an icon.
  *
- * Suppresses the restriction of selections when a drag is started.
+ * @p predicate should return TRUE for draggable rows and FALSE otherwise, to
+ * control what goes in the icon.  If NULL, equivalent to a function that
+ * always returns TRUE.
  */
-void make_treeview_multidrag(GtkWidget *w) {
+void make_treeview_multidrag(GtkWidget *w,
+                            multidrag_row_predicate *predicate) {
+  if(!predicate)
+    predicate = multidrag_default_predicate;
   g_signal_connect(w, "button-press-event",
                   G_CALLBACK(multidrag_button_press_event), NULL);
   g_signal_connect(w, "button-release-event",
                   G_CALLBACK(multidrag_button_release_event), NULL);
   g_signal_connect(w, "drag-begin",
-                   G_CALLBACK(multidrag_drag_begin), NULL);
+                   G_CALLBACK(multidrag_drag_begin), predicate);
 }
 
 /*