[gtk+/wip/otte/clipboard: 33/64] gdkcontentformats: Change the matching API



commit 8a49a8050a12b7106807f77d477a94795b5d2bc3
Author: Benjamin Otte <otte redhat com>
Date:   Fri Nov 24 11:34:19 2017 +0100

    gdkcontentformats: Change the matching API
    
    Instead of having just one function that has the gtype and mime type as
    out arguments, have 3 functions: 1 that finds any match, 1 that finds a
    GType match and one for a mime type match.
    
    This makes the API way more convenient to use.

 docs/reference/gdk/gdk4-sections.txt |    2 +
 gdk/gdkclipboard.c                   |    7 ++-
 gdk/gdkcontentformats.c              |   78 ++++++++++++++++++++++------------
 gdk/gdkcontentformats.h              |   10 +++-
 gtk/gtkdragdest.c                    |   12 +----
 gtk/gtkdragdest.h                    |    2 +-
 gtk/gtktextview.c                    |    2 +-
 tests/testdnd.c                      |    4 +-
 8 files changed, 71 insertions(+), 46 deletions(-)
---
diff --git a/docs/reference/gdk/gdk4-sections.txt b/docs/reference/gdk/gdk4-sections.txt
index b199ea9..ec05622 100644
--- a/docs/reference/gdk/gdk4-sections.txt
+++ b/docs/reference/gdk/gdk4-sections.txt
@@ -377,6 +377,8 @@ gdk_content_formats_get_gtypes
 gdk_content_formats_get_mime_types
 gdk_content_formats_union
 gdk_content_formats_match
+gdk_content_formats_match_gtype
+gdk_content_formats_match_mime_types
 gdk_content_formats_contain_gtype
 gdk_content_formats_contain_mime_type
 
diff --git a/gdk/gdkclipboard.c b/gdk/gdkclipboard.c
index b5dc5db..38df1c5 100644
--- a/gdk/gdkclipboard.c
+++ b/gdk/gdkclipboard.c
@@ -166,9 +166,9 @@ gdk_clipboard_read_local_async (GdkClipboard        *clipboard,
 
   content_formats = gdk_content_provider_ref_formats (priv->content);
   content_formats = gdk_content_formats_union_serialize_mime_types (content_formats);
+  mime_type = gdk_content_formats_match_mime_type (content_formats, formats);
 
-  if (gdk_content_formats_match (content_formats, formats, NULL, &mime_type)
-      && mime_type != NULL)
+  if (mime_type != NULL)
     {
       GOutputStream *output_stream;
       GIOStream *stream;
@@ -892,7 +892,8 @@ gdk_clipboard_write_async (GdkClipboard        *clipboard,
 
   mime_formats = gdk_content_formats_new ((const gchar *[2]) { mime_type, NULL }, 1);
   mime_formats = gdk_content_formats_union_serialize_gtypes (mime_formats);
-  if (gdk_content_formats_match (mime_formats, formats, &gtype, NULL))
+  gtype = gdk_content_formats_match_gtype (formats, mime_formats);
+  if (gtype != G_TYPE_INVALID)
     {
       GValue value = G_VALUE_INIT;
       GError *error = NULL;
diff --git a/gdk/gdkcontentformats.c b/gdk/gdkcontentformats.c
index 2663a3b..7b4a101 100644
--- a/gdk/gdkcontentformats.c
+++ b/gdk/gdkcontentformats.c
@@ -276,55 +276,78 @@ gdk_content_formats_contain_interned_mime_type (const GdkContentFormats *formats
  * gdk_content_formats_match:
  * @first: the primary #GdkContentFormats to intersect
  * @second: the #GdkContentFormats to intersect with
- * @out_gtype: (out) (allow-none): pointer to take the 
- *     matching #GType or %G_TYPE_INVALID if @out_mime_type was set.
- * @out_mime_type: (out) (allow-none) (transfer none): The matching
- *    mime type or %NULL if @out_gtype is set
  *
- * Finds the first element from @first that is also contained
- * in @second. If no matching format is found, %FALSE is returned
- * and @out_gtype and @out_mime_type are set to %G_TYPE_INVALID and
- * %NULL respectively.
+ * Checks if @first and @second have any matching formats.
  *
  * Returns: %TRUE if a matching format was found.
  */
 gboolean
 gdk_content_formats_match (const GdkContentFormats *first,
-                           const GdkContentFormats *second,
-                           GType                   *out_gtype,
-                           const char             **out_mime_type)
+                           const GdkContentFormats *second)
+{
+  g_return_val_if_fail (first != NULL, FALSE);
+  g_return_val_if_fail (second != NULL, FALSE);
+
+  return gdk_content_formats_match_gtype (first, second) != G_TYPE_INVALID
+      || gdk_content_formats_match_mime_type (first, second) != NULL;
+}
+
+/**
+ * gdk_content_formats_match_gtype:
+ * @first: the primary #GdkContentFormats to intersect
+ * @second: the #GdkContentFormats to intersect with
+ *
+ * Finds the first #GType from @first that is also contained
+ * in @second. If no matching #GType is found, %G_TYPE_INVALID
+ * is returned.
+ *
+ * Returns: The first common #GType or %G_TYPE_INVALID if none.
+ **/
+GType
+gdk_content_formats_match_gtype (const GdkContentFormats *first,
+                                 const GdkContentFormats *second)
 {
   gsize i;
 
   g_return_val_if_fail (first != NULL, FALSE);
   g_return_val_if_fail (second != NULL, FALSE);
 
-  if (out_gtype)
-    *out_gtype = G_TYPE_INVALID;
-  if (out_mime_type)
-    *out_mime_type = NULL;
-
   for (i = 0; i < first->n_gtypes; i++)
     {
       if (gdk_content_formats_contain_gtype (second, first->gtypes[i]))
-        {
-          if (out_gtype)
-            *out_gtype = first->gtypes[i];
-          return TRUE;
-        }
+        return first->gtypes[i];
     }
 
+  return G_TYPE_INVALID;
+}
+
+/**
+ * gdk_content_formats_match_mime_type:
+ * @first: the primary #GdkContentFormats to intersect
+ * @second: the #GdkContentFormats to intersect with
+ *
+ * Finds the first mime type from @first that is also contained
+ * in @second. If no matching mime type is found, %NULL is
+ * returned.
+ *
+ * Returns: The first common mime type or %NULL if none.
+ **/
+const char *
+gdk_content_formats_match_mime_type (const GdkContentFormats *first,
+                                     const GdkContentFormats *second)
+{
+  gsize i;
+
+  g_return_val_if_fail (first != NULL, FALSE);
+  g_return_val_if_fail (second != NULL, FALSE);
+
   for (i = 0; i < first->n_mime_types; i++)
     {
       if (gdk_content_formats_contain_interned_mime_type (second, first->mime_types[i]))
-        {
-          if (out_mime_type)
-            *out_mime_type = first->mime_types[i];
-          return TRUE;
-        }
+        return first->mime_types[i];
     }
 
-  return FALSE;
+  return NULL;
 }
 
 /**
@@ -534,6 +557,7 @@ gdk_content_formats_builder_add_gtype (GdkContentFormatsBuilder *builder,
                                        GType                     type)
 {
   g_return_if_fail (builder != NULL);
+  g_return_if_fail (type != G_TYPE_INVALID);
 
   if (g_slist_find (builder->gtypes, GSIZE_TO_POINTER (type)))
     return;
diff --git a/gdk/gdkcontentformats.h b/gdk/gdkcontentformats.h
index 7ba1496..1c67d00 100644
--- a/gdk/gdkcontentformats.h
+++ b/gdk/gdkcontentformats.h
@@ -58,9 +58,13 @@ GdkContentFormats *     gdk_content_formats_union               (GdkContentForma
                                                                  const GdkContentFormats        *second) 
G_GNUC_WARN_UNUSED_RESULT;
 GDK_AVAILABLE_IN_3_94
 gboolean                gdk_content_formats_match               (const GdkContentFormats        *first,
-                                                                 const GdkContentFormats        *second,
-                                                                 GType                          *out_gtype,
-                                                                 const char                    
**out_mime_type);
+                                                                 const GdkContentFormats        *second);
+GDK_AVAILABLE_IN_3_94
+GType                   gdk_content_formats_match_gtype         (const GdkContentFormats        *first,
+                                                                 const GdkContentFormats        *second);
+GDK_AVAILABLE_IN_3_94
+const char *            gdk_content_formats_match_mime_type     (const GdkContentFormats        *first,
+                                                                 const GdkContentFormats        *second);
 GDK_AVAILABLE_IN_3_94
 gboolean                gdk_content_formats_contain_gtype       (const GdkContentFormats        *formats,
                                                                  GType                           type);
diff --git a/gtk/gtkdragdest.c b/gtk/gtkdragdest.c
index 8b9fbff..5c5c09a 100644
--- a/gtk/gtkdragdest.c
+++ b/gtk/gtkdragdest.c
@@ -406,13 +406,11 @@ gtk_drag_dest_get_track_motion (GtkWidget *widget)
  * Returns: (transfer none) (nullable): first target that the source offers
  *     and the dest can accept, or %NULL
  */
-GdkAtom
+const char *
 gtk_drag_dest_find_target (GtkWidget         *widget,
                            GdkDragContext    *context,
                            GdkContentFormats *target_list)
 {
-  GdkAtom result;
-
   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
 
@@ -422,11 +420,7 @@ gtk_drag_dest_find_target (GtkWidget         *widget,
   if (target_list == NULL)
     return NULL;
 
-  gdk_content_formats_match (target_list,
-                             gdk_drag_context_get_formats (context),
-                             NULL,
-                             &result);
-
-  return result;
+  return gdk_content_formats_match_mime_type (target_list,
+                                              gdk_drag_context_get_formats (context));
 }
 
diff --git a/gtk/gtkdragdest.h b/gtk/gtkdragdest.h
index d69bd06..9fc1d3e 100644
--- a/gtk/gtkdragdest.h
+++ b/gtk/gtkdragdest.h
@@ -76,7 +76,7 @@ GDK_AVAILABLE_IN_ALL
 void gtk_drag_dest_unset (GtkWidget          *widget);
 
 GDK_AVAILABLE_IN_ALL
-GdkAtom                 gtk_drag_dest_find_target       (GtkWidget              *widget,
+const char *            gtk_drag_dest_find_target       (GtkWidget              *widget,
                                                          GdkDragContext         *context,
                                                          GdkContentFormats      *target_list);
 GDK_AVAILABLE_IN_ALL
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index ec423d1..2db9bd3 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -8189,7 +8189,7 @@ gtk_text_view_drag_data_received (GtkWidget        *widget,
           buffer_formats = gdk_content_formats_new (atoms, n_atoms);
           dnd_formats = gdk_drag_context_get_formats (context);
 
-          gdk_content_formats_match (dnd_formats, buffer_formats, NULL, &target);
+          target = gdk_content_formats_match_mime_type (dnd_formats, buffer_formats);
 
           gdk_content_formats_unref (buffer_formats);
           g_free (atoms);
diff --git a/tests/testdnd.c b/tests/testdnd.c
index 3fbc0f6..3954aab 100644
--- a/tests/testdnd.c
+++ b/tests/testdnd.c
@@ -344,7 +344,7 @@ target_drag_drop       (GtkWidget          *widget,
                            guint               time)
 {
   GdkContentFormats *formats;
-  GdkAtom format;
+  const char *format;
 
   g_print("drop\n");
   have_drag = FALSE;
@@ -352,7 +352,7 @@ target_drag_drop       (GtkWidget          *widget,
   gtk_image_set_from_pixbuf (GTK_IMAGE (widget), trashcan_closed);
 
   formats = gdk_drag_context_get_formats (context);
-  gdk_content_formats_match (formats, formats, NULL, &format);
+  format = gdk_content_formats_match_mime_type (formats, formats);
   if (format)
     {
       gtk_drag_get_data (widget, context,


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