diff options
author | Daniel Narvaez <dwnarvaez@gmail.com> | 2012-09-02 15:11:50 (GMT) |
---|---|---|
committer | Daniel Narvaez <dwnarvaez@gmail.com> | 2012-09-02 15:21:45 (GMT) |
commit | e6c70067f600029c93115e48944de1b37ddb745d (patch) | |
tree | 4e45723313565584bb89534527a2b718b546576d | |
parent | f751ec4761934a4d7193c7b4de1ab48fba8e619a (diff) |
Add a wrapper around gtk_clipboard_set_with_data
-rw-r--r-- | src/sugar3/Makefile.am | 4 | ||||
-rw-r--r-- | src/sugar3/sugar-clipboard.c | 121 | ||||
-rw-r--r-- | src/sugar3/sugar-clipboard.h | 35 |
3 files changed, 160 insertions, 0 deletions
diff --git a/src/sugar3/Makefile.am b/src/sugar3/Makefile.am index 0e46361..5e18ce0 100644 --- a/src/sugar3/Makefile.am +++ b/src/sugar3/Makefile.am @@ -48,6 +48,8 @@ libsugarext_la_SOURCES = \ gsm-session.h \ gsm-xsmp.c \ gsm-xsmp.h \ + sugar-clipboard.c \ + sugar-clipboard.h \ sugar-grid.c \ sugar-grid.h \ sugar-key-grabber.c \ @@ -128,6 +130,8 @@ SugarExt_1_0_gir_FILES = \ gsm-session.h \ gsm-xsmp.c \ gsm-xsmp.h \ + sugar-clipboard.c \ + sugar-clipboard.h \ sugar-key-grabber.c \ sugar-key-grabber.h \ sugar-grid.c \ diff --git a/src/sugar3/sugar-clipboard.c b/src/sugar3/sugar-clipboard.c new file mode 100644 index 0000000..0a8db87 --- /dev/null +++ b/src/sugar3/sugar-clipboard.c @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2012, Daniel Narvaez + * + * 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, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <string.h> + +#include "sugar-clipboard.h" + +typedef struct { + GClosure *get_closure; + GClosure *clear_closure; +} SetWithDataUserData; + +static void clipboard_data_get_func (GtkClipboard *clipboard, + GtkSelectionData *selection_data, + guint info, + gpointer user_data) +{ + SetWithDataUserData *data = user_data; + GValue params[2] = { { 0, }, { 0, } }; + + g_value_init(¶ms[0], GTK_TYPE_SELECTION_DATA); + g_value_take_boxed(¶ms[0], selection_data); + + g_value_init(¶ms[1], G_TYPE_UINT); + g_value_set_uint(¶ms[1], info); + + g_closure_invoke(data->get_closure, NULL, 2, params, NULL); +} + +static void clipboard_data_clear_func (GtkClipboard *clipboard, + gpointer user_data) +{ + SetWithDataUserData *data = user_data; + + if (data->clear_closure) { + g_closure_invoke (data->clear_closure, NULL, 0, NULL, NULL); + g_closure_unref(data->clear_closure); + } + + if (data->get_closure) + g_closure_unref(data->get_closure); + + g_free(data); +} + +/** + * sugar_clipboard_set_with_data: + * @clipboard: a #GtkClipboard + * @targets: (array length=n_targets): array containing information + * about the available forms for the clipboard data + * @n_targets: number of elements in @targets + * @get_closure: #GClosure to call to get the actual clipboard data + * @clear_closure: (allow-none): when the clipboard contents are set again, + * this #GClosure will be called, and @get_func will not be subsequently + * called. + * + * Virtually sets the contents of the specified clipboard by providing + * a list of supported formats for the clipboard data and a closure + * to call to get the actual data when it is requested. + * + * Return value: %TRUE if setting the clipboard data succeeded. + * If setting the clipboard data failed the provided closures + * will be ignored. + **/ +gboolean +sugar_clipboard_set_with_data (GtkClipboard *clipboard, + GtkTargetEntry **targets, + guint n_targets, + GClosure *get_closure, + GClosure *clear_closure) +{ + + GtkTargetEntry *targets_flat; + int i; + + g_return_val_if_fail(clipboard != NULL, FALSE); + g_return_val_if_fail(targets != NULL, FALSE); + g_return_val_if_fail(get_closure != NULL, FALSE); + + targets_flat = g_new0(GtkTargetEntry, n_targets); + + for (i = 0; i < n_targets; i++) { + memcpy(targets_flat + i * sizeof(GtkTargetEntry), + targets[i], + sizeof(GtkTargetEntry)); + } + + SetWithDataUserData *user_data = g_new0(SetWithDataUserData, 1); + + if (get_closure != NULL) { + user_data->get_closure = g_closure_ref(get_closure); + g_closure_sink(get_closure); + } + + if (clear_closure != NULL) { + user_data->clear_closure = g_closure_ref(clear_closure); + g_closure_sink(clear_closure); + } + + return gtk_clipboard_set_with_data(clipboard, targets_flat, n_targets, + clipboard_data_get_func, + clipboard_data_clear_func, + user_data); +} + diff --git a/src/sugar3/sugar-clipboard.h b/src/sugar3/sugar-clipboard.h new file mode 100644 index 0000000..6230bcd --- /dev/null +++ b/src/sugar3/sugar-clipboard.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2012, Daniel Narvaez + * + * 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, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __SUGAR_CLIPBOARD_H__ +#define __SUGAR_CLIPBOARD_H__ + +#include <gtk/gtk.h> + +G_BEGIN_DECLS + +gboolean sugar_clipboard_set_with_data (GtkClipboard *clipboard, + GtkTargetEntry **targets, + guint n_targets, + GClosure *get_closure, + GClosure *clear_closure); + +G_END_DECLS + +#endif /* __SUGAR_CLIPBOARD_H__ */ |