From e94b9b9a62571e3fde676b4a4cba8fbaf2219609 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 28 May 2014 23:55:53 -0400 Subject: [PATCH] gdk: A GdkClipboard API draft This commit adds a GdkClipboard object which is intended to replace GtkClipboard, eventually. --- gdk/gdk.h | 1 + gdk/gdkclipboard.c | 235 ++++++++++++++++++++++++++++++++++++++ gdk/gdkclipboard.h | 48 ++++++++ gdk/gdkclipboardprivate.h | 51 +++++++++ gdk/gdkdisplay.c | 41 +++++++ gdk/gdkdisplay.h | 4 + gdk/gdkdisplayprivate.h | 3 + gdk/gdktypes.h | 1 + gdk/meson.build | 2 + 9 files changed, 386 insertions(+) create mode 100644 gdk/gdkclipboard.c create mode 100644 gdk/gdkclipboard.h create mode 100644 gdk/gdkclipboardprivate.h diff --git a/gdk/gdk.h b/gdk/gdk.h index 4b9a5ada1a..64a06e2677 100644 --- a/gdk/gdk.h +++ b/gdk/gdk.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include diff --git a/gdk/gdkclipboard.c b/gdk/gdkclipboard.c new file mode 100644 index 0000000000..caa73540fb --- /dev/null +++ b/gdk/gdkclipboard.c @@ -0,0 +1,235 @@ +/* GDK - The GIMP Drawing Kit + * + * Copyright (C) 2017 Benjamin Otte + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#include "config.h" + +#include "gdkclipboardprivate.h" + +#include "gdkcontentformats.h" +#include "gdkdisplay.h" + +typedef struct _GdkClipboardPrivate GdkClipboardPrivate; + +struct _GdkClipboardPrivate +{ + GdkDisplay *display; + GdkContentFormats *formats; + + guint local : 1; +}; + +enum { + PROP_0, + PROP_DISPLAY, + PROP_FORMATS, + PROP_LOCAL, + N_PROPERTIES +}; + +enum { + CHANGED, + N_SIGNALS +}; + +static GParamSpec *properties[N_PROPERTIES] = { NULL, }; +static guint signals[N_SIGNALS] = { 0 }; + +G_DEFINE_TYPE_WITH_PRIVATE (GdkClipboard, gdk_clipboard, G_TYPE_OBJECT) + +static void +gdk_clipboard_set_property (GObject *gobject, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + GdkClipboard *clipboard = GDK_CLIPBOARD (gobject); + GdkClipboardPrivate *priv = gdk_clipboard_get_instance_private (clipboard); + + switch (prop_id) + { + case PROP_DISPLAY: + priv->display = g_value_get_object (value); + g_assert (priv->display != NULL); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec); + break; + } +} + +static void +gdk_clipboard_get_property (GObject *gobject, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + GdkClipboard *clipboard = GDK_CLIPBOARD (gobject); + GdkClipboardPrivate *priv = gdk_clipboard_get_instance_private (clipboard); + + switch (prop_id) + { + case PROP_DISPLAY: + g_value_set_object (value, priv->display); + break; + + case PROP_FORMATS: + g_value_set_boxed (value, priv->formats); + break; + + case PROP_LOCAL: + g_value_set_boolean (value, priv->local); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec); + break; + } +} + +static void +gdk_clipboard_finalize (GObject *object) +{ + GdkClipboard *clipboard = GDK_CLIPBOARD (object); + GdkClipboardPrivate *priv = gdk_clipboard_get_instance_private (clipboard); + + g_clear_pointer (&priv->formats, gdk_content_formats_unref); + + G_OBJECT_CLASS (gdk_clipboard_parent_class)->finalize (object); +} + +static void +gdk_clipboard_class_init (GdkClipboardClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->get_property = gdk_clipboard_get_property; + object_class->set_property = gdk_clipboard_set_property; + object_class->finalize = gdk_clipboard_finalize; + + /** + * GdkClipboard:display: + * + * The #GdkDisplay that the clipboard belongs to. + * + * Since: 3.94 + */ + properties[PROP_DISPLAY] = + g_param_spec_object ("display", + "Display", + "Display owning this clipboard", + GDK_TYPE_DISPLAY, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_STRINGS | + G_PARAM_EXPLICIT_NOTIFY); + + /** + * GdkClipboard:formats: + * + * The possible formats that the clipboard can provide its data in. + * + * Since: 3.94 + */ + properties[PROP_FORMATS] = + g_param_spec_boxed ("formats", + "Formats", + "The possible formats for data", + GDK_TYPE_CONTENT_FORMATS, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS | + G_PARAM_EXPLICIT_NOTIFY); + + /** + * GdkClipboard:local: + * + * %TRUE if the contents of the clipboard are owned by this process. + * + * Since: 3.94 + */ + properties[PROP_LOCAL] = + g_param_spec_boolean ("local", + "Local", + "If the contents are owned by this process", + TRUE, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS | + G_PARAM_EXPLICIT_NOTIFY); + + signals[CHANGED] = + g_signal_new ("changed", + G_TYPE_FROM_CLASS (class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GdkClipboardClass, changed), + NULL, NULL, NULL, + G_TYPE_NONE, 0); + + g_object_class_install_properties (object_class, N_PROPERTIES, properties); +} + +static void +gdk_clipboard_init (GdkClipboard *clipboard) +{ + GdkClipboardPrivate *priv = gdk_clipboard_get_instance_private (clipboard); + + priv->formats = gdk_content_formats_new (NULL, 0); +} + +/** + * gdk_clipboard_get_display: + * @clipboard: a #GdkClipboard + * + * Gets the #GdkDisplay that the clipboard was created for. + * + * Returns: (transfer none): a #GdkDisplay + **/ +GdkDisplay * +gdk_clipboard_get_display (GdkClipboard *clipboard) +{ + GdkClipboardPrivate *priv = gdk_clipboard_get_instance_private (clipboard); + + g_return_val_if_fail (GDK_IS_CLIPBOARD (clipboard), NULL); + + return priv->display; +} + +/** + * gdk_clipboard_get_formats: + * @clipboard: a #GdkClipboard + * + * Gets the formats that the clipboard can provide its current contents in. + * + * Returns: (transfer none): The formats of the clipboard + **/ +GdkContentFormats * +gdk_clipboard_get_formats (GdkClipboard *clipboard) +{ + GdkClipboardPrivate *priv = gdk_clipboard_get_instance_private (clipboard); + + g_return_val_if_fail (GDK_IS_CLIPBOARD (clipboard), NULL); + + return priv->formats; +} + +GdkClipboard * +gdk_clipboard_new (GdkDisplay *display) +{ + return g_object_new (GDK_TYPE_CLIPBOARD, + "display", display, + NULL); +} diff --git a/gdk/gdkclipboard.h b/gdk/gdkclipboard.h new file mode 100644 index 0000000000..08943e55e6 --- /dev/null +++ b/gdk/gdkclipboard.h @@ -0,0 +1,48 @@ +/* GDK - The GIMP Drawing Kit + * + * Copyright (C) 2017 Benjamin Otte + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#ifndef __GDK_CLIPBOARD_H__ +#define __GDK_CLIPBOARD_H__ + +#if !defined (__GDK_H_INSIDE__) && !defined (GDK_COMPILATION) +#error "Only can be included directly." +#endif + +#include +#include +#include + + +G_BEGIN_DECLS + +#define GDK_TYPE_CLIPBOARD (gdk_clipboard_get_type ()) +#define GDK_CLIPBOARD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GDK_TYPE_CLIPBOARD, GdkClipboard)) +#define GDK_IS_CLIPBOARD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GDK_TYPE_CLIPBOARD)) + +GDK_AVAILABLE_IN_3_94 +GType gdk_clipboard_get_type (void) G_GNUC_CONST; + +GDK_AVAILABLE_IN_3_94 +GdkDisplay * gdk_clipboard_get_display (GdkClipboard *clipboard); +GDK_AVAILABLE_IN_3_94 +GdkContentFormats * gdk_clipboard_get_formats (GdkClipboard *clipboard); + + +G_END_DECLS + +#endif /* __GDK_CLIPBOARD_H__ */ diff --git a/gdk/gdkclipboardprivate.h b/gdk/gdkclipboardprivate.h new file mode 100644 index 0000000000..ac3f0b88c4 --- /dev/null +++ b/gdk/gdkclipboardprivate.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2017 Benjamin Otte + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#ifndef __GDK_CLIPBOARD_PRIVATE_H__ +#define __GDK_CLIPBOARD_PRIVATE_H__ + +#include + +G_BEGIN_DECLS + +#define GDK_CLIPBOARD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_CLIPBOARD, GdkClipboardClass)) +#define GDK_IS_CLIPBOARD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_CLIPBOARD)) +#define GDK_CLIPBOARD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_CLIPBOARD, GdkClipboardClass)) + +typedef struct _GdkClipboardClass GdkClipboardClass; + +struct _GdkClipboard +{ + GObject parent; +}; + +struct _GdkClipboardClass +{ + GObjectClass parent_class; + + /* signals */ + void (* changed) (GdkClipboard *clipboard); + + /* vfuncs */ +}; + +GdkClipboard * gdk_clipboard_new (GdkDisplay *display); + + +G_END_DECLS + +#endif /* __GDK_CLIPBOARD_PRIVATE_H__ */ diff --git a/gdk/gdkdisplay.c b/gdk/gdkdisplay.c index fe9af51898..5b198fe2e1 100644 --- a/gdk/gdkdisplay.c +++ b/gdk/gdkdisplay.c @@ -27,6 +27,7 @@ #include "gdkintl.h" #include "gdk-private.h" +#include "gdkclipboardprivate.h" #include "gdkdeviceprivate.h" #include "gdkdisplaymanagerprivate.h" #include "gdkevents.h" @@ -1187,6 +1188,46 @@ gdk_display_request_selection_notification (GdkDisplay *display, return GDK_DISPLAY_GET_CLASS (display)->request_selection_notification (display, selection); } +/** + * gdk_display_get_clipboard: + * @display: a #GdkDisplay + * + * Gets the clipboard used for copy/paste operations. + * + * Returns: the display's clipboard + **/ +GdkClipboard * +gdk_display_get_clipboard (GdkDisplay *display) +{ + g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL); + + if (display->clipboard == NULL) + display->clipboard = gdk_clipboard_new (display); + + return display->clipboard; +} + +/** + * gdk_display_get_primary_clipboard: + * @display: a #GdkDisplay + * + * Gets the clipboard used for the primary selection. On backends where the + * primary clipboard is not supported natively, GDK emulates this clipboard + * locally. + * + * Returns: the primary clipboard + **/ +GdkClipboard * +gdk_display_get_primary_clipboard (GdkDisplay *display) +{ + g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL); + + if (display->primary_clipboard == NULL) + display->primary_clipboard = gdk_clipboard_new (display); + + return display->primary_clipboard; +} + /** * gdk_display_supports_clipboard_persistence: * @display: a #GdkDisplay diff --git a/gdk/gdkdisplay.h b/gdk/gdkdisplay.h index e81d5b7067..1c9f3ca842 100644 --- a/gdk/gdkdisplay.h +++ b/gdk/gdkdisplay.h @@ -99,6 +99,10 @@ GDK_AVAILABLE_IN_ALL gboolean gdk_display_request_selection_notification (GdkDisplay *display, GdkAtom selection); +GDK_AVAILABLE_IN_3_94 +GdkClipboard * gdk_display_get_clipboard (GdkDisplay *display); +GDK_AVAILABLE_IN_3_94 +GdkClipboard * gdk_display_get_primary_clipboard (GdkDisplay *display); GDK_AVAILABLE_IN_ALL gboolean gdk_display_supports_clipboard_persistence (GdkDisplay *display); GDK_AVAILABLE_IN_ALL diff --git a/gdk/gdkdisplayprivate.h b/gdk/gdkdisplayprivate.h index 4e7742dd02..a05a721721 100644 --- a/gdk/gdkdisplayprivate.h +++ b/gdk/gdkdisplayprivate.h @@ -83,6 +83,9 @@ struct _GdkDisplay GHashTable *device_grabs; + GdkClipboard *clipboard; + GdkClipboard *primary_clipboard; + GHashTable *pointers_info; /* GdkPointerWindowInfo for each device */ guint32 last_event_time; /* Last reported event time from server */ diff --git a/gdk/gdktypes.h b/gdk/gdktypes.h index f7cb9f8b86..32dba8794c 100644 --- a/gdk/gdktypes.h +++ b/gdk/gdktypes.h @@ -126,6 +126,7 @@ typedef struct _GdkTexture GdkTexture; typedef struct _GdkDevice GdkDevice; typedef struct _GdkDragContext GdkDragContext; +typedef struct _GdkClipboard GdkClipboard; typedef struct _GdkDisplayManager GdkDisplayManager; typedef struct _GdkDisplay GdkDisplay; typedef struct _GdkWindow GdkWindow; diff --git a/gdk/meson.build b/gdk/meson.build index 8792f34138..d1e75ce5b2 100644 --- a/gdk/meson.build +++ b/gdk/meson.build @@ -2,6 +2,7 @@ gdk_public_sources = files([ 'gdk.c', 'gdkapplaunchcontext.c', 'gdkcairo.c', + 'gdkclipboard.c', 'gdkcontentformats.c', 'gdkcursor.c', 'gdkdevice.c', @@ -41,6 +42,7 @@ gdk_public_headers = files([ 'gdk.h', 'gdkapplaunchcontext.h', 'gdkcairo.h', + 'gdkclipboard.h', 'gdkcontentformats.h', 'gdkcursor.h', 'gdkdevice.h',