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().
This commit is contained in:
Benjamin Otte 2020-02-16 14:24:03 +01:00
parent 762c4602fb
commit 8d2ed36a1b
11 changed files with 52 additions and 46 deletions

View File

@ -1199,6 +1199,7 @@ gdk_clipboard_get_type
GdkContentProvider GdkContentProvider
GdkContentProviderClass GdkContentProviderClass
gdk_content_provider_new_for_value gdk_content_provider_new_for_value
gdk_content_provider_new_typed
gdk_content_provider_new_for_bytes gdk_content_provider_new_for_bytes
gdk_content_provider_ref_formats gdk_content_provider_ref_formats
gdk_content_provider_ref_storable_formats gdk_content_provider_ref_storable_formats

View File

@ -20,6 +20,8 @@
#include "gdkcontentprovider.h" #include "gdkcontentprovider.h"
#include <gobject/gvaluecollector.h>
#include "gdkcontentformats.h" #include "gdkcontentformats.h"
#include "gdkcontentserializer.h" #include "gdkcontentserializer.h"
#include "gdkintl.h" #include "gdkintl.h"
@ -124,6 +126,44 @@ gdk_content_provider_new_for_value (const GValue *value)
return GDK_CONTENT_PROVIDER (content); 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_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_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)) #define GDK_IS_CONTENT_PROVIDER_BYTES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GDK_TYPE_CONTENT_PROVIDER_BYTES))

View File

@ -32,6 +32,9 @@ G_BEGIN_DECLS
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
GdkContentProvider * gdk_content_provider_new_for_value (const GValue *value); GdkContentProvider * gdk_content_provider_new_for_value (const GValue *value);
GDK_AVAILABLE_IN_ALL 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, GdkContentProvider * gdk_content_provider_new_for_bytes (const char *mime_type,
GBytes *bytes); GBytes *bytes);

View File

@ -1166,8 +1166,6 @@ get_calendar_content (GtkCalendar *calendar)
GtkCalendarPrivate *priv = gtk_calendar_get_instance_private (calendar); GtkCalendarPrivate *priv = gtk_calendar_get_instance_private (calendar);
GDate *date; GDate *date;
gchar str[128]; gchar str[128];
GValue value = G_VALUE_INIT;
GdkContentProvider *content;
date = g_date_new_dmy (g_date_time_get_day_of_month (priv->date), date = g_date_new_dmy (g_date_time_get_day_of_month (priv->date),
g_date_time_get_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_date_strftime (str, 127, "%x", date);
g_free (date); g_free (date);
g_value_init (&value, G_TYPE_STRING); return gdk_content_provider_new_typed (G_TYPE_STRING, str);
g_value_set_string (&value, str);
content = gdk_content_provider_new_for_value (&value);
g_value_unset (&value);
return content;
} }
static void static void

View File

@ -1196,7 +1196,6 @@ make_directory_button (GtkPathBar *path_bar,
AtkObject *atk_obj; AtkObject *atk_obj;
GtkWidget *child = NULL; GtkWidget *child = NULL;
ButtonData *button_data; ButtonData *button_data;
GValue value = G_VALUE_INIT;
GdkContentProvider *content; GdkContentProvider *content;
GtkDragSource *source; GtkDragSource *source;
@ -1245,14 +1244,11 @@ make_directory_button (GtkPathBar *path_bar,
g_object_weak_ref (G_OBJECT (button_data->button), g_object_weak_ref (G_OBJECT (button_data->button),
(GWeakNotify) button_data_free, button_data); (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 (); 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); gtk_drag_source_set_content (source, content);
g_object_unref (content); g_object_unref (content);
gtk_widget_add_controller (button_data->button, GTK_EVENT_CONTROLLER (source)); gtk_widget_add_controller (button_data->button, GTK_EVENT_CONTROLLER (source));
g_value_unset (&value);
return button_data; return button_data;
} }

View File

@ -2851,7 +2851,6 @@ gtk_text_drag_gesture_update (GtkGestureDrag *gesture,
GdkDrag *drag; GdkDrag *drag;
GdkPaintable *paintable; GdkPaintable *paintable;
GdkContentProvider *content; GdkContentProvider *content;
GValue value = G_VALUE_INIT;
text = _gtk_text_get_selected_text (self); text = _gtk_text_get_selected_text (self);
gtk_text_get_pixel_ranges (self, &ranges, &n_ranges); gtk_text_get_pixel_ranges (self, &ranges, &n_ranges);
@ -2861,10 +2860,7 @@ gtk_text_drag_gesture_update (GtkGestureDrag *gesture,
else else
actions = GDK_ACTION_COPY; actions = GDK_ACTION_COPY;
g_value_init (&value, G_TYPE_STRING); content = gdk_content_provider_new_typed (G_TYPE_STRING, text);
g_value_set_string (&value, text);
content = gdk_content_provider_new_for_value (&value);
g_value_unset (&value);
drag = gdk_drag_begin (gdk_event_get_surface ((GdkEvent*) event), drag = gdk_drag_begin (gdk_event_get_surface ((GdkEvent*) event),
gdk_event_get_device ((GdkEvent*) event), gdk_event_get_device ((GdkEvent*) event),

View File

@ -344,13 +344,10 @@ get_button_list (GdkClipboard *clipboard,
"GdkPixbuf"); "GdkPixbuf");
g_value_unset (&value); g_value_unset (&value);
g_value_init (&value, G_TYPE_STRING);
g_value_set_string (&value, "Hello Clipboard ☺");
add_provider_button (box, add_provider_button (box,
gdk_content_provider_new_for_value (&value), gdk_content_provider_new_typed (G_TYPE_STRING, "Hello Clipboard ☺"),
clipboard, clipboard,
"gchararry"); "gchararry");
g_value_unset (&value);
add_provider_button (box, add_provider_button (box,
gdk_content_provider_new_for_bytes ("text/plain;charset=utf-8", gdk_content_provider_new_for_bytes ("text/plain;charset=utf-8",

View File

@ -605,7 +605,6 @@ main (int argc, char **argv)
GdkPixbuf *drag_icon; GdkPixbuf *drag_icon;
GdkTexture *texture; GdkTexture *texture;
GdkContentProvider *content; GdkContentProvider *content;
GValue value = G_VALUE_INIT;
GtkDragSource *source; GtkDragSource *source;
GdkContentFormats *targets; GdkContentFormats *targets;
GtkDropTarget *dest; GtkDropTarget *dest;
@ -673,10 +672,7 @@ main (int argc, char **argv)
button = gtk_label_new ("Drag Here\n"); button = gtk_label_new ("Drag Here\n");
source = gtk_drag_source_new (); source = gtk_drag_source_new ();
g_value_init (&value, G_TYPE_STRING); content = gdk_content_provider_new_typed (G_TYPE_STRING, "I'm data!");
g_value_set_string (&value, "I'm data!");
content = gdk_content_provider_new_for_value (&value);
g_value_unset (&value);
gtk_drag_source_set_content (source, content); gtk_drag_source_set_content (source, content);
g_object_unref (content); g_object_unref (content);
gtk_drag_source_set_actions (source, GDK_ACTION_COPY|GDK_ACTION_MOVE); gtk_drag_source_set_actions (source, GDK_ACTION_COPY|GDK_ACTION_MOVE);

View File

@ -439,21 +439,17 @@ make_spinner (void)
GtkWidget *spinner; GtkWidget *spinner;
GtkDragSource *source; GtkDragSource *source;
GdkContentProvider *content; GdkContentProvider *content;
GValue value = G_VALUE_INIT;
spinner = gtk_spinner_new (); spinner = gtk_spinner_new ();
gtk_spinner_start (GTK_SPINNER (spinner)); gtk_spinner_start (GTK_SPINNER (spinner));
g_value_init (&value, G_TYPE_STRING); content = gdk_content_provider_new_typed (G_TYPE_STRING, "ACTIVE");
g_value_set_string (&value, "ACTIVE");
content = gdk_content_provider_new_for_value (&value);
source = gtk_drag_source_new (); source = gtk_drag_source_new ();
gtk_drag_source_set_content (source, content); gtk_drag_source_set_content (source, content);
g_signal_connect (source, "drag-begin", G_CALLBACK (spinner_drag_begin), spinner); g_signal_connect (source, "drag-begin", G_CALLBACK (spinner_drag_begin), spinner);
gtk_widget_add_controller (spinner, GTK_EVENT_CONTROLLER (source)); gtk_widget_add_controller (spinner, GTK_EVENT_CONTROLLER (source));
g_object_unref (content); g_object_unref (content);
g_value_unset (&value);
return spinner; return spinner;
} }

View File

@ -100,7 +100,6 @@ main (int argc, char **argv)
GtkWidget *button4; GtkWidget *button4;
GIcon *icon; GIcon *icon;
GdkContentProvider *content; GdkContentProvider *content;
GValue value = G_VALUE_INIT;
gboolean done = FALSE; gboolean done = FALSE;
gtk_init (); gtk_init ();
@ -165,10 +164,7 @@ main (int argc, char **argv)
GTK_ENTRY_ICON_PRIMARY, GTK_ENTRY_ICON_PRIMARY,
"Save a file"); "Save a file");
g_value_init (&value, G_TYPE_STRING); content = gdk_content_provider_new_typed (G_TYPE_STRING, "Amazing");
g_value_set_string (&value, "Amazing");
content = gdk_content_provider_new_for_value (&value);
g_value_unset (&value);
gtk_entry_set_icon_drag_source (GTK_ENTRY (entry), gtk_entry_set_icon_drag_source (GTK_ENTRY (entry),
GTK_ENTRY_ICON_PRIMARY, GTK_ENTRY_ICON_PRIMARY,
content, GDK_ACTION_COPY); content, GDK_ACTION_COPY);

View File

@ -6,15 +6,7 @@ prepare (GtkDragSource *source,
double y, double y,
GtkWidget *row) GtkWidget *row)
{ {
GdkContentProvider *content; return gdk_content_provider_new_typed (GTK_TYPE_LIST_BOX_ROW, row);
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;
} }
static void static void