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
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

View File

@ -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))

View File

@ -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);

View File

@ -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

View File

@ -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;
}

View File

@ -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),

View File

@ -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",

View File

@ -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);

View File

@ -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;
}

View File

@ -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);

View File

@ -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