[gtk/wip/otte/dnd: 3/9] contentprovider: Add gdk_content_provider_new_typed()
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/wip/otte/dnd: 3/9] contentprovider: Add gdk_content_provider_new_typed()
- Date: Sun, 16 Feb 2020 19:13:35 +0000 (UTC)
commit 8d2ed36a1bdc4f5c5c7a5eab39562764c69139ce
Author: Benjamin Otte <otte redhat com>
Date: Sun Feb 16 14:24:03 2020 +0100
contentprovider: Add gdk_content_provider_new_typed()
Gets around the boilerplate required to create and initialize a GValue
by having this function doing it via G_VALUE_COLLECT().
docs/reference/gdk/gdk4-sections.txt | 1 +
gdk/gdkcontentproviderimpl.c | 40 ++++++++++++++++++++++++++++++++++++
gdk/gdkcontentproviderimpl.h | 3 +++
gtk/gtkcalendar.c | 9 +-------
gtk/gtkpathbar.c | 6 +-----
gtk/gtktext.c | 6 +-----
tests/testclipboard2.c | 5 +----
tests/testdnd.c | 6 +-----
tests/testdnd2.c | 6 +-----
tests/testentryicons.c | 6 +-----
tests/testlist3.c | 10 +--------
11 files changed, 52 insertions(+), 46 deletions(-)
---
diff --git a/docs/reference/gdk/gdk4-sections.txt b/docs/reference/gdk/gdk4-sections.txt
index 48eadea63b..eccaad1dab 100644
--- a/docs/reference/gdk/gdk4-sections.txt
+++ b/docs/reference/gdk/gdk4-sections.txt
@@ -1199,6 +1199,7 @@ gdk_clipboard_get_type
GdkContentProvider
GdkContentProviderClass
gdk_content_provider_new_for_value
+gdk_content_provider_new_typed
gdk_content_provider_new_for_bytes
gdk_content_provider_ref_formats
gdk_content_provider_ref_storable_formats
diff --git a/gdk/gdkcontentproviderimpl.c b/gdk/gdkcontentproviderimpl.c
index e5c86a396c..3be95cbed7 100644
--- a/gdk/gdkcontentproviderimpl.c
+++ b/gdk/gdkcontentproviderimpl.c
@@ -20,6 +20,8 @@
#include "gdkcontentprovider.h"
+#include <gobject/gvaluecollector.h>
+
#include "gdkcontentformats.h"
#include "gdkcontentserializer.h"
#include "gdkintl.h"
@@ -124,6 +126,44 @@ gdk_content_provider_new_for_value (const GValue *value)
return GDK_CONTENT_PROVIDER (content);
}
+/**
+ * gdk_content_provider_new_typed:
+ * @type: Type of value to follow
+ * ...: value
+ *
+ * Create a content provider that provides the value of the given
+ * @type.
+ *
+ * The value is provided using G_VALUE_COLLECT(), so the same rules
+ * apply as when calling g_object_new() or g_object_set().
+ *
+ * Returns: a new #GdkContentProvider
+ **/
+GdkContentProvider *
+gdk_content_provider_new_typed (GType type,
+ ...)
+{
+ GdkContentProviderValue *content;
+ va_list args;
+ char *error;
+
+ content = g_object_new (GDK_TYPE_CONTENT_PROVIDER_VALUE, NULL);
+
+ va_start (args, type);
+ G_VALUE_COLLECT_INIT (&content->value, type, args, 0, &error);
+ if (error)
+ {
+ g_warning ("%s: %s", G_STRLOC, error);
+ g_free (error);
+ /* we purposely leak the value here, it might not be
+ * in a sane state if an error condition occoured
+ */
+ }
+ va_end (args);
+
+ return GDK_CONTENT_PROVIDER (content);
+}
+
#define GDK_TYPE_CONTENT_PROVIDER_BYTES (gdk_content_provider_bytes_get_type ())
#define GDK_CONTENT_PROVIDER_BYTES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj),
GDK_TYPE_CONTENT_PROVIDER_BYTES, GdkContentProviderBytes))
#define GDK_IS_CONTENT_PROVIDER_BYTES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj),
GDK_TYPE_CONTENT_PROVIDER_BYTES))
diff --git a/gdk/gdkcontentproviderimpl.h b/gdk/gdkcontentproviderimpl.h
index cea0b6b667..c10158e5bb 100644
--- a/gdk/gdkcontentproviderimpl.h
+++ b/gdk/gdkcontentproviderimpl.h
@@ -32,6 +32,9 @@ G_BEGIN_DECLS
GDK_AVAILABLE_IN_ALL
GdkContentProvider * gdk_content_provider_new_for_value (const GValue *value);
GDK_AVAILABLE_IN_ALL
+GdkContentProvider * gdk_content_provider_new_typed (GType type,
+ ...);
+GDK_AVAILABLE_IN_ALL
GdkContentProvider * gdk_content_provider_new_for_bytes (const char *mime_type,
GBytes *bytes);
diff --git a/gtk/gtkcalendar.c b/gtk/gtkcalendar.c
index 70b34ff84c..769d85f68d 100644
--- a/gtk/gtkcalendar.c
+++ b/gtk/gtkcalendar.c
@@ -1166,8 +1166,6 @@ get_calendar_content (GtkCalendar *calendar)
GtkCalendarPrivate *priv = gtk_calendar_get_instance_private (calendar);
GDate *date;
gchar str[128];
- GValue value = G_VALUE_INIT;
- GdkContentProvider *content;
date = g_date_new_dmy (g_date_time_get_day_of_month (priv->date),
g_date_time_get_month (priv->date),
@@ -1175,12 +1173,7 @@ get_calendar_content (GtkCalendar *calendar)
g_date_strftime (str, 127, "%x", date);
g_free (date);
- g_value_init (&value, G_TYPE_STRING);
- g_value_set_string (&value, str);
- content = gdk_content_provider_new_for_value (&value);
- g_value_unset (&value);
-
- return content;
+ return gdk_content_provider_new_typed (G_TYPE_STRING, str);
}
static void
diff --git a/gtk/gtkpathbar.c b/gtk/gtkpathbar.c
index 51427ca6ec..152efa625b 100644
--- a/gtk/gtkpathbar.c
+++ b/gtk/gtkpathbar.c
@@ -1196,7 +1196,6 @@ make_directory_button (GtkPathBar *path_bar,
AtkObject *atk_obj;
GtkWidget *child = NULL;
ButtonData *button_data;
- GValue value = G_VALUE_INIT;
GdkContentProvider *content;
GtkDragSource *source;
@@ -1245,14 +1244,11 @@ make_directory_button (GtkPathBar *path_bar,
g_object_weak_ref (G_OBJECT (button_data->button),
(GWeakNotify) button_data_free, button_data);
- g_value_init (&value, G_TYPE_FILE);
- g_value_set_object (&value, button_data->file);
source = gtk_drag_source_new ();
- content = gdk_content_provider_new_for_value (&value);
+ content = gdk_content_provider_new_typed (G_TYPE_FILE, button_data->file);
gtk_drag_source_set_content (source, content);
g_object_unref (content);
gtk_widget_add_controller (button_data->button, GTK_EVENT_CONTROLLER (source));
- g_value_unset (&value);
return button_data;
}
diff --git a/gtk/gtktext.c b/gtk/gtktext.c
index b671c9a39f..6dce1b327b 100644
--- a/gtk/gtktext.c
+++ b/gtk/gtktext.c
@@ -2851,7 +2851,6 @@ gtk_text_drag_gesture_update (GtkGestureDrag *gesture,
GdkDrag *drag;
GdkPaintable *paintable;
GdkContentProvider *content;
- GValue value = G_VALUE_INIT;
text = _gtk_text_get_selected_text (self);
gtk_text_get_pixel_ranges (self, &ranges, &n_ranges);
@@ -2861,10 +2860,7 @@ gtk_text_drag_gesture_update (GtkGestureDrag *gesture,
else
actions = GDK_ACTION_COPY;
- g_value_init (&value, G_TYPE_STRING);
- g_value_set_string (&value, text);
- content = gdk_content_provider_new_for_value (&value);
- g_value_unset (&value);
+ content = gdk_content_provider_new_typed (G_TYPE_STRING, text);
drag = gdk_drag_begin (gdk_event_get_surface ((GdkEvent*) event),
gdk_event_get_device ((GdkEvent*) event),
diff --git a/tests/testclipboard2.c b/tests/testclipboard2.c
index d09c06050f..da812f6782 100644
--- a/tests/testclipboard2.c
+++ b/tests/testclipboard2.c
@@ -344,13 +344,10 @@ get_button_list (GdkClipboard *clipboard,
"GdkPixbuf");
g_value_unset (&value);
- g_value_init (&value, G_TYPE_STRING);
- g_value_set_string (&value, "Hello Clipboard ☺");
add_provider_button (box,
- gdk_content_provider_new_for_value (&value),
+ gdk_content_provider_new_typed (G_TYPE_STRING, "Hello Clipboard ☺"),
clipboard,
"gchararry");
- g_value_unset (&value);
add_provider_button (box,
gdk_content_provider_new_for_bytes ("text/plain;charset=utf-8",
diff --git a/tests/testdnd.c b/tests/testdnd.c
index 41a5109240..40ec72d60c 100644
--- a/tests/testdnd.c
+++ b/tests/testdnd.c
@@ -605,7 +605,6 @@ main (int argc, char **argv)
GdkPixbuf *drag_icon;
GdkTexture *texture;
GdkContentProvider *content;
- GValue value = G_VALUE_INIT;
GtkDragSource *source;
GdkContentFormats *targets;
GtkDropTarget *dest;
@@ -673,10 +672,7 @@ main (int argc, char **argv)
button = gtk_label_new ("Drag Here\n");
source = gtk_drag_source_new ();
- g_value_init (&value, G_TYPE_STRING);
- g_value_set_string (&value, "I'm data!");
- content = gdk_content_provider_new_for_value (&value);
- g_value_unset (&value);
+ content = gdk_content_provider_new_typed (G_TYPE_STRING, "I'm data!");
gtk_drag_source_set_content (source, content);
g_object_unref (content);
gtk_drag_source_set_actions (source, GDK_ACTION_COPY|GDK_ACTION_MOVE);
diff --git a/tests/testdnd2.c b/tests/testdnd2.c
index 608c955b03..cfb0da65c5 100644
--- a/tests/testdnd2.c
+++ b/tests/testdnd2.c
@@ -439,21 +439,17 @@ make_spinner (void)
GtkWidget *spinner;
GtkDragSource *source;
GdkContentProvider *content;
- GValue value = G_VALUE_INIT;
spinner = gtk_spinner_new ();
gtk_spinner_start (GTK_SPINNER (spinner));
- g_value_init (&value, G_TYPE_STRING);
- g_value_set_string (&value, "ACTIVE");
- content = gdk_content_provider_new_for_value (&value);
+ content = gdk_content_provider_new_typed (G_TYPE_STRING, "ACTIVE");
source = gtk_drag_source_new ();
gtk_drag_source_set_content (source, content);
g_signal_connect (source, "drag-begin", G_CALLBACK (spinner_drag_begin), spinner);
gtk_widget_add_controller (spinner, GTK_EVENT_CONTROLLER (source));
g_object_unref (content);
- g_value_unset (&value);
return spinner;
}
diff --git a/tests/testentryicons.c b/tests/testentryicons.c
index 796295a61f..14d3cb6b5d 100644
--- a/tests/testentryicons.c
+++ b/tests/testentryicons.c
@@ -100,7 +100,6 @@ main (int argc, char **argv)
GtkWidget *button4;
GIcon *icon;
GdkContentProvider *content;
- GValue value = G_VALUE_INIT;
gboolean done = FALSE;
gtk_init ();
@@ -165,10 +164,7 @@ main (int argc, char **argv)
GTK_ENTRY_ICON_PRIMARY,
"Save a file");
- g_value_init (&value, G_TYPE_STRING);
- g_value_set_string (&value, "Amazing");
- content = gdk_content_provider_new_for_value (&value);
- g_value_unset (&value);
+ content = gdk_content_provider_new_typed (G_TYPE_STRING, "Amazing");
gtk_entry_set_icon_drag_source (GTK_ENTRY (entry),
GTK_ENTRY_ICON_PRIMARY,
content, GDK_ACTION_COPY);
diff --git a/tests/testlist3.c b/tests/testlist3.c
index 4240d78172..fb5fd1addd 100644
--- a/tests/testlist3.c
+++ b/tests/testlist3.c
@@ -6,15 +6,7 @@ prepare (GtkDragSource *source,
double y,
GtkWidget *row)
{
- GdkContentProvider *content;
- GValue value = G_VALUE_INIT;
-
- g_value_init (&value, GTK_TYPE_LIST_BOX_ROW);
- g_value_set_object (&value, row);
- content = gdk_content_provider_new_for_value (&value);
- g_value_unset (&value);
-
- return content;
+ return gdk_content_provider_new_typed (GTK_TYPE_LIST_BOX_ROW, row);
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]