Example #1
0
/* \brief Set the contents of the system clipboard.
 * \par Function Description
 * Set the system clipboard to contain the gschem objects listed in \a
 * object_list.
 *
 * \param [in,out] w_current   The current GSCHEM_TOPLEVEL.
 * \param [in]     object_list The objects to put in the clipboard.
 *
 * \return TRUE if the clipboard is successfully set.
 */
gboolean
x_clipboard_set (GSCHEM_TOPLEVEL *w_current, const GList *object_list)
{
  GtkClipboard *cb = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
  GtkTargetEntry target = { MIME_TYPE_SCHEMATIC, 0,
                            CLIP_TYPE_SCHEMATIC };
  TOPLEVEL *toplevel = w_current->toplevel;
  gboolean result;

  /* Clear the clipboard buffer */
  if (w_current->clipboard_buffer)
    gtk_clipboard_clear (cb);

  /* Copy the objects to the clipboard buffer */
  w_current->clipboard_buffer =
    o_glist_copy_all (toplevel, object_list, w_current->clipboard_buffer);

  /* Advertise that the data is available */
  result = gtk_clipboard_set_with_data (cb, &target, 1,
                                        clip_get, clip_clear, w_current);

  /* Hint that the data can be stored to be accessed after the program
   * has quit. */
  gtk_clipboard_set_can_store (cb, NULL, 0);

  return result;
}
Example #2
0
static void test_pasting_markup(CopyAndPasteFixture* fixture, gconstpointer data)
{
    fixture->info = (TestInfo*)data;
    currentFixture = fixture;

    GtkTargetList* targetList = gtk_target_list_new(0, 0);
    gtk_target_list_add(targetList, gdk_atom_intern("text/html", FALSE), 0, 0);

    int numberOfTargets = 1;
    GtkTargetEntry* targetTable = gtk_target_table_new_from_list(targetList, &numberOfTargets);
    gtk_clipboard_set_with_data(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
                                targetTable, numberOfTargets,
                                pasting_test_get_data_callback,
                                pasting_test_clear_data_callback,
                                g_strdup(fixture->info->expectedContent));
    gtk_target_list_unref(targetList);
    gtk_target_table_free(targetTable, numberOfTargets);

    g_signal_connect(fixture->window, "map-event",
                     G_CALLBACK(map_event_cb), fixture);
    g_signal_connect(fixture->webView, "window-object-cleared",
                     G_CALLBACK(window_object_cleared_callback), fixture);

    gtk_widget_show(fixture->window);
    gtk_widget_show(GTK_WIDGET(fixture->webView));
    gtk_window_present(GTK_WINDOW(fixture->window));

    g_main_loop_run(fixture->loop);
}
Example #3
0
/*
 * 	main entry point for cut and copy operations
 * 	cut : cut = TRUE;
 * 	copy: cut = FALSE;
 *
 * 	NOTE: every we cut or copy, we need to override
 * 	      previous data in clipboard.so we acquire
 * 	      clipboard every time we cut or copy.
 * 	      _clipboard_owner_change_cb is used to clear
 * 	      clipboard
 * 	      1. this is the only way we can set clipboard_info.
 * 	      2. this is the only way we can set clipboard_info_prev.
 *
 */
void
init_fileops_clipboard (GFile* file_list[], guint num, gboolean cut)
{
    g_debug ("init_fileops_clipboard:begin");

    //set prev clipboard_info
    __clear_clipboard_info (&clipboard_info_prev);
    __copy_clipboard_info (&clipboard_info, &clipboard_info_prev);

    int j=0;
    for (j = 0; j < clipboard_info_prev.num; j++)
    {
	g_debug ("init_fileops prev: file_list[%d] = %s", j, g_file_get_uri (clipboard_info_prev.file_list[j]));
    }
    //we're the clipboard owner, cleanup clipboard_info
    __clear_clipboard_info (&clipboard_info);

    clipboard_info.file_list = (GFile**)g_malloc (num * sizeof (GFile*));
    int i;
    for (i = 0; i < num; i++)
    {
	clipboard_info.file_list[i] = g_object_ref (file_list[i]);
	g_debug ("init_fileops %s: file_list[%d] = %s", cut? "cut": "paste", i, g_file_get_uri (file_list[i]));
    }
    clipboard_info.num = num;
    clipboard_info.cut = cut;

    GtkTargetList*  target_list;
    GtkTargetEntry* targets;
    gint	    n_targets;

    if (copied_files_atom == GDK_NONE)
	copied_files_atom = gdk_atom_intern ("x-special/gnome-copied-files", FALSE);

    //TODO: request clipboard data before take ownership
    //      so we can interoperate with nautilus.
    if (fileops_clipboard == NULL)
    {
	fileops_clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
	g_signal_connect (fileops_clipboard, "owner-change",
		          G_CALLBACK (_clipboard_owner_change_cb), NULL);
    }

    target_list = gtk_target_list_new (NULL, 0);
    gtk_target_list_add (target_list, copied_files_atom, 0, 0);
    gtk_target_list_add_uri_targets (target_list, 0);
    gtk_target_list_add_text_targets (target_list, 0);

    targets = gtk_target_table_new_from_list (target_list, &n_targets);

    gtk_clipboard_set_with_data (fileops_clipboard,
				 targets, n_targets,
				 _get_clipboard_callback, _clear_clipboard_callback,
				 NULL);

    gtk_target_list_unref (target_list);
    gtk_target_table_free (targets, n_targets);

    g_debug ("init_fileops_clipboard:end");
}
void PasteboardHelperGtk::writeClipboardContents(GtkClipboard* clipboard, gpointer data)
{
    DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
    GtkTargetList* list = targetListForDataObject(dataObject);

    int numberOfTargets;
    GtkTargetEntry* table = gtk_target_table_new_from_list(list, &numberOfTargets);

    if (numberOfTargets > 0 && table) {
        settingClipboardDataObject = dataObject;
        settingClipboardData = data;

        // Protect the web view from being destroyed before one of the clipboard callbacks
        // is called. Balanced in both getClipboardContentsCallback and
        // clearClipboardContentsCallback.
        WebKitWebView* webView = static_cast<WebKitWebView*>(data);
        g_object_ref(webView);

        gboolean succeeded = gtk_clipboard_set_with_data(clipboard, table, numberOfTargets,
                                                         getClipboardContentsCallback,
                                                         clearClipboardContentsCallback, data);
        if (!succeeded)
            g_object_unref(webView);

        settingClipboardDataObject = 0;
        settingClipboardData = 0;
    } else
        gtk_clipboard_clear(clipboard);

    if (table)
        gtk_target_table_free(table, numberOfTargets);

    gtk_target_list_unref(list);
}
Example #5
0
void PasteboardHelper::writeClipboardContents(GtkClipboard* clipboard, SmartPasteInclusion includeSmartPaste, GClosure* callback)
{
    DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
    GtkTargetList* list = targetListForDataObject(dataObject, includeSmartPaste);

    int numberOfTargets;
    GtkTargetEntry* table = gtk_target_table_new_from_list(list, &numberOfTargets);

    if (numberOfTargets > 0 && table) {
        settingClipboardDataObject = dataObject;

        if (gtk_clipboard_set_with_data(clipboard, table, numberOfTargets, getClipboardContentsCallback, clearClipboardContentsCallback, callback ? g_closure_ref(callback) : nullptr))
            gtk_clipboard_set_can_store(clipboard, nullptr, 0);
        else {
            // When gtk_clipboard_set_with_data fails the callbacks are ignored, so we need to release the reference we were passing to clearClipboardContentsCallback.
            if (callback)
                g_closure_unref(callback);
        }

        settingClipboardDataObject = nullptr;

    } else
        gtk_clipboard_clear(clipboard);

    if (table)
        gtk_target_table_free(table, numberOfTargets);
    gtk_target_list_unref(list);
}
Example #6
0
/*
 * detect_get_clipboard - try and get the CLIPBOARD_NAME clipboard
 *
 * Returns TRUE if successfully retrieved and FALSE otherwise.
 */
gboolean detect_get_clipboard()
{
    static const GtkTargetEntry targets[] = { {CLIPBOARD_NAME, 0, 0} };
    gboolean retval = FALSE;
    GtkClipboard *clipboard;
    Atom atom;

    atom = gdk_x11_get_xatom_by_name(CLIPBOARD_NAME);

    XGrabServer(GDK_DISPLAY());

    if (XGetSelectionOwner(GDK_DISPLAY(), atom) != None)
        goto out;

    clipboard = gtk_clipboard_get(gdk_atom_intern(CLIPBOARD_NAME, FALSE));

    if (gtk_clipboard_set_with_data(
                clipboard, targets,
                G_N_ELEMENTS (targets),
                clipboard_get_func,
                clipboard_clear_func, NULL))
        retval = TRUE;

out:
    XUngrabServer (GDK_DISPLAY ());
    gdk_flush ();

    return retval;
}
Example #7
0
gboolean fm_clipboard_cut_or_copy_files(GtkWidget* src_widget, FmPathList* files, gboolean _is_cut)
{
	GdkDisplay* dpy = src_widget ? gtk_widget_get_display(src_widget) : gdk_display_get_default();
	GtkClipboard* clip = gtk_clipboard_get_for_display(dpy, GDK_SELECTION_CLIPBOARD);
    gboolean ret = gtk_clipboard_set_with_data(clip, targets, G_N_ELEMENTS(targets),
                                               get_data, clear_data, fm_list_ref(files));
    is_cut = _is_cut;
	return ret;
}
Example #8
0
static gboolean
clipboard_cut_or_copy_files (GtkWidget* src_widget, clipboard_data_context_t *ctx)
{
    GdkDisplay *display = src_widget ? gtk_widget_get_display (src_widget) : gdk_display_get_default();
    GtkClipboard *clipboard = gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
    gboolean ret;
    ret = gtk_clipboard_set_with_data (clipboard, targets, G_N_ELEMENTS(targets),
                                      clipboard_get_clipboard_data, clipboard_free, ctx);
    return ret;
}
Example #9
0
void clipboard_copy( ClipboardCopyFunc copy ){
	GtkClipboard* clipboard = gtk_clipboard_get( GDK_SELECTION_CLIPBOARD );

	BufferOutputStream ostream;
	copy( ostream );
	std::size_t length = ostream.size();
	char* data = new char[length + sizeof( std::size_t )];
	*reinterpret_cast<std::size_t*>( data ) = length;
	memcpy( data + sizeof( std::size_t ), ostream.data(), length );

	gtk_clipboard_set_with_data( clipboard, clipboard_targets, 1, clipboard_get, clipboard_clear, data );
}
/*
 * Class:     com_sun_glass_ui_gtk_GtkSystemClipboard
 * Method:    pushToSystem
 * Signature: (Ljava/util/HashMap;I)V
 */
JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkSystemClipboard_pushToSystem
  (JNIEnv * env, jobject obj, jobject data, jint supported)
{
    GtkTargetEntry* targets = NULL;
    gint ntargets;
    data = env->NewGlobalRef(data);
    init_atoms();
    data_to_targets(env, data, &targets, &ntargets);
    CHECK_JNI_EXCEPTION(env)
    if (targets) {
        gtk_clipboard_set_with_data(get_clipboard(), targets, ntargets, set_data_func, clear_data_func, data);
        gtk_target_table_free(targets, ntargets);
    } else { 
        // targets == NULL means that we want to clear clipboard.
        // Passing NULL as targets parameter to gtk_clipboard_set_with_data will produce Gtk-CRITICAL assertion
        // but passing 0 as n_targets parameter allows to set empty list of available mime types
        GtkTargetEntry dummy_targets = {(gchar*) "MIME_DUMMY_TARGET", 0, 0};
        gtk_clipboard_set_with_data(get_clipboard(), &dummy_targets, 0, set_data_func, clear_data_func, data);
    }

    is_clipboard_updated_by_glass = TRUE;
}
Example #11
0
void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
{
    GtkClipboard* clipboard = m_helper->getClipboard(frame);
    gchar* text = g_strdup(frame->selectedText().utf8().data());
    gchar* markup = g_strdup(createMarkup(selectedRange, 0, AnnotateForInterchange).utf8().data());
    PasteboardSelectionData* data = new PasteboardSelectionData(text, markup);

    gint n_targets;
    GtkTargetEntry* targets = gtk_target_table_new_from_list(m_helper->getCopyTargetList(frame), &n_targets);
    gtk_clipboard_set_with_data(clipboard, targets, n_targets,
                                clipboard_get_contents_cb, clipboard_clear_contents_cb, data);
    gtk_target_table_free(targets, n_targets);
}
Example #12
0
void a_clipboard_copy( VikClipboardDataType type, guint16 layer_type, gint subtype, guint len, const gchar* text, guint8 * data)
{
  vik_clipboard_t * vc = g_malloc(sizeof(*vc) + len);
  GtkClipboard *c = gtk_clipboard_get ( GDK_SELECTION_CLIPBOARD );

  vc->type = type;
  vc->layer_type = layer_type;
  vc->subtype = subtype;
  vc->len = len;
  vc->text = g_strdup (text);
  memcpy(vc->data, data, len);
  g_free(data);
  vc->pid = getpid();
  gtk_clipboard_set_with_data ( c, target_table, G_N_ELEMENTS(target_table), clip_get, clip_clear, vc );
}
/**
 * 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_func: (closure user_data) (scope notified): function to call to get the
 *     actual clipboard data
 * @clear_func: (closure user_data) (scope async): when the clipboard
 *     contents are set again, this function will be called, and @get_func
 *     will not be subsequently called.
 * @user_data: user data to pass to @get_func and @clear_func.
 *
 * Virtually sets the contents of the specified clipboard by providing
 * a list of supported formats for the clipboard data and a function
 * 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 callback
 *    functions will be ignored.
 **/
gboolean
sugar_clipboard_set_with_data (GtkClipboard *clipboard,
                              const GtkTargetEntry *targets,
                              guint n_targets,
                              GtkClipboardGetFunc get_func,
                              GtkClipboardClearFunc clear_func,
                              gpointer user_data)
{
    return gtk_clipboard_set_with_data (clipboard,
                                        targets,
                                        n_targets,
                                        get_func,
                                        clear_func,
                                        user_data);
}
Example #14
0
void ptk_clipboard_cut_or_copy_files( const char* working_dir,
                                      GList* files,
                                      gboolean copy )
{
    GtkClipboard * clip = gtk_clipboard_get( GDK_SELECTION_CLIPBOARD );
    GtkTargetList* target_list = gtk_target_list_new( NULL, 0 );
    GList* target;
    gint i, n_targets;
    GtkTargetEntry* targets;
    GtkTargetPair* pair;
    GList *l;
    VFSFileInfo* file;
    char* file_path;
    GList* file_list = NULL;

    gtk_target_list_add_text_targets( target_list, 0 );
    n_targets = g_list_length( target_list->list ) + 2;

    targets = g_new0( GtkTargetEntry, n_targets );
    target = target_list->list;
    for ( i = 0; target; ++i, target = g_list_next( target ) )
    {
        pair = ( GtkTargetPair* ) target->data;
        targets[ i ].target = gdk_atom_name ( pair->target );
    }
    targets[ i ].target = "x-special/gnome-copied-files";
    targets[ i + 1 ].target = "text/uri-list";

    gtk_target_list_unref ( target_list );

    for ( l = g_list_last( files ); l; l = l->prev )  //sfm was reverse order
    {
        file = ( VFSFileInfo* ) l->data;
        file_path = g_build_filename( working_dir,
                                      vfs_file_info_get_name( file ), NULL );
        file_list = g_list_prepend( file_list, file_path );
    }

    gtk_clipboard_set_with_data ( clip, targets, n_targets,
                                  clipboard_get_data,
                                  clipboard_clean_data,
                                  NULL );

    g_free( targets );

    clipboard_file_list = file_list;
    clipboard_action = copy ? GDK_ACTION_COPY : GDK_ACTION_MOVE;
}
Example #15
0
/* As icon_set_selected(), but doesn't automatically unselect incompatible
 * icons.
 */
static void icon_set_selected_int(Icon *icon, gboolean selected)
{
	static GtkClipboard *primary;

	g_return_if_fail(icon != NULL);

	if (icon->selected == selected)
		return;

	if (!primary)
		primary = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));

	if (selected)
	{
		icon_selection = g_list_prepend(icon_selection, icon);
		if (!have_primary)
		{
			GtkTargetEntry target_table[] =
			{
				{"text/uri-list", 0, TARGET_URI_LIST},
				{"UTF8", 0, TARGET_STRING},
				{"COMPOUND_TEXT", 0, TARGET_STRING},
				{"STRING", 0, TARGET_STRING},
			};

			/* Grab selection */
			have_primary = gtk_clipboard_set_with_data(primary,
				target_table,
				sizeof(target_table) / sizeof(*target_table),
				selection_get, lose_selection, NULL);
		}
	}
	else
	{
		icon_selection = g_list_remove(icon_selection, icon);
		if (have_primary && !icon_selection)
		{
			have_primary = FALSE;
			gtk_clipboard_clear(primary);
		}
	}

	icon->selected = selected;
	g_signal_emit_by_name(icon, "redraw");
}
Example #16
0
static VALUE
clipboard_set_body(VALUE value)
{
    struct clipboard_set_args *args = (struct clipboard_set_args *)value;
    GtkClipboard *clipboard = _SELF(args->self);
    long n;
    GtkTargetEntry *targets = RVAL2GTKTARGETENTRIES(args->targets, &n);
    gboolean result = gtk_clipboard_set_with_data(clipboard,
                                                  targets,
                                                  n,
                                                  (GtkClipboardGetFunc)clipboard_get_func,
                                                  (GtkClipboardClearFunc)NULL,
                                                  (gpointer)args->func);

    g_free(targets);

    return CBOOL2RVAL(result);
}
Example #17
0
static void
_gth_browser_clipboard_copy_or_cut (GthBrowser *browser,
				    GList      *file_list,
				    gboolean    cut)
{
	ClipboardData  *data;
	GtkTargetList  *target_list;
	GtkTargetEntry *targets;
	int             n_targets;
	GList          *scan;
	int             i;

	data = g_new0 (ClipboardData, 1);
	data->cut = cut;
	data->n_uris = g_list_length (file_list);
	data->uris = g_new (char *, data->n_uris + 1);
	for (scan = file_list, i = 0; scan; scan = scan->next, i++) {
		GthFileData *file_data = scan->data;
		data->uris[i] = g_file_get_uri (file_data->file);
	}
	data->uris[data->n_uris] = NULL;

	target_list = gtk_target_list_new (NULL, 0);
	gtk_target_list_add (target_list, GNOME_COPIED_FILES, 0, 0);
	gtk_target_list_add_uri_targets (target_list, 0);
	gtk_target_list_add_text_targets (target_list, 0);
	targets = gtk_target_table_new_from_list (target_list, &n_targets);
	gtk_clipboard_set_with_data (gtk_clipboard_get_for_display (gtk_widget_get_display (GTK_WIDGET (browser)), GDK_SELECTION_CLIPBOARD),
				     targets,
				     n_targets,
				     clipboard_get_cb,
				     clipboard_clear_cb,
				     data);

	gtk_target_list_unref (target_list);
	gtk_target_table_free (targets, n_targets);
}
    void SetupClipboardContentTypes()
    {
        GtkTargetList* list = gtk_target_list_new(NULL, 0);

        // TODO: Support for images
        if (!this->text && !this->uris)
            return;

        if (this->text)
            gtk_target_list_add_text_targets(list, ClipboardPrivate::TEXT_DATA);

        if (this->uris)
            gtk_target_list_add_uri_targets(list, ClipboardPrivate::URI_LIST_DATA);

        int size = 0;
        GtkTargetEntry* table = gtk_target_table_new_from_list(list, &size);
        if (table)
        {
            // gtk_clipboard_set_with_data may try to clear our clipboard when we
            // call it, so we turn on a flag here which prevents our clipboard data
            // from being freed during this call.
            this->preserve = true;
            GtkClipboard* clipboard = GetClipboard();
            if (gtk_clipboard_set_with_data(clipboard, table, size, GetClipboardData,
                ClearClipboardData, NULL))
            {
                this->ownClipboard = true;
                gtk_clipboard_set_can_store(clipboard, NULL, 0);
            }
            this->preserve = false;

            gtk_target_table_free(table, size);
        }

        gtk_target_list_unref(list);
    }
Example #19
0
void PasteboardHelper::writeClipboardContents(GtkClipboard* clipboard, GClosure* callback)
{
    DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
    GtkTargetList* list = targetListForDataObject(dataObject);

    int numberOfTargets;
    GtkTargetEntry* table = gtk_target_table_new_from_list(list, &numberOfTargets);

    if (numberOfTargets > 0 && table) {
        settingClipboardDataObject = dataObject;

        gtk_clipboard_set_with_data(clipboard, table, numberOfTargets,
            getClipboardContentsCallback, clearClipboardContentsCallback, callback);
        gtk_clipboard_set_can_store(clipboard, 0, 0);

        settingClipboardDataObject = 0;

    } else
        gtk_clipboard_clear(clipboard);

    if (table)
        gtk_target_table_free(table, numberOfTargets);
    gtk_target_list_unref(list);
}
NS_IMETHODIMP
nsClipboard::SetData(nsITransferable *aTransferable,
                     nsIClipboardOwner *aOwner, PRInt32 aWhichClipboard)
{
    // See if we can short cut
    if ((aWhichClipboard == kGlobalClipboard &&
         aTransferable == mGlobalTransferable.get() &&
         aOwner == mGlobalOwner.get()) ||
        (aWhichClipboard == kSelectionClipboard &&
         aTransferable == mSelectionTransferable.get() &&
         aOwner == mSelectionOwner.get())) {
        return NS_OK;
    }

    nsresult rv;
    if (!mPrivacyHandler) {
        rv = NS_NewClipboardPrivacyHandler(getter_AddRefs(mPrivacyHandler));
        NS_ENSURE_SUCCESS(rv, rv);
    }
    rv = mPrivacyHandler->PrepareDataForClipboard(aTransferable);
    NS_ENSURE_SUCCESS(rv, rv);

    // Clear out the clipboard in order to set the new data
    EmptyClipboard(aWhichClipboard);

    // List of suported targets
    GtkTargetList *list = gtk_target_list_new(NULL, 0);

    // Get the types of supported flavors
    nsCOMPtr<nsISupportsArray> flavors;

    rv = aTransferable->FlavorsTransferableCanExport(getter_AddRefs(flavors));
    if (!flavors || NS_FAILED(rv))
        return NS_ERROR_FAILURE;

    // Add all the flavors to this widget's supported type.
    PRBool imagesAdded = PR_FALSE;
    PRUint32 count;
    flavors->Count(&count);
    for (PRUint32 i=0; i < count; i++) {
        nsCOMPtr<nsISupports> tastesLike;
        flavors->GetElementAt(i, getter_AddRefs(tastesLike));
        nsCOMPtr<nsISupportsCString> flavor = do_QueryInterface(tastesLike);

        if (flavor) {
            nsXPIDLCString flavorStr;
            flavor->ToString(getter_Copies(flavorStr));

            // special case text/unicode since we can handle all of
            // the string types
            if (!strcmp(flavorStr, kUnicodeMime)) {
                gtk_target_list_add(list, gdk_atom_intern("UTF8_STRING", FALSE), 0, 0);
                gtk_target_list_add(list, gdk_atom_intern("COMPOUND_TEXT", FALSE), 0, 0);
                gtk_target_list_add(list, gdk_atom_intern("TEXT", FALSE), 0, 0);
                gtk_target_list_add(list, GDK_SELECTION_TYPE_STRING, 0, 0);
                continue;
            }

            if (flavorStr.EqualsLiteral(kNativeImageMime) ||
                flavorStr.EqualsLiteral(kPNGImageMime) ||
                flavorStr.EqualsLiteral(kJPEGImageMime) ||
                flavorStr.EqualsLiteral(kGIFImageMime)) {
                // don't bother adding image targets twice
                if (!imagesAdded) {
                    // accept any writable image type
                    gtk_target_list_add_image_targets(list, 0, TRUE);
                    imagesAdded = PR_TRUE;
                }
                continue;
            }

            // Add this to our list of valid targets
            GdkAtom atom = gdk_atom_intern(flavorStr, FALSE);
            gtk_target_list_add(list, atom, 0, 0);
        }
    }
    
    // Get GTK clipboard (CLIPBOARD or PRIMARY)
    GtkClipboard *gtkClipboard = gtk_clipboard_get(GetSelectionAtom(aWhichClipboard));
  
    gint numTargets;
    GtkTargetEntry *gtkTargets = gtk_target_table_new_from_list(list, &numTargets);
          
    // Set getcallback and request to store data after an application exit
    if (gtk_clipboard_set_with_data(gtkClipboard, gtkTargets, numTargets, 
                                    clipboard_get_cb, clipboard_clear_cb, this))
    {
        // We managed to set-up the clipboard so update internal state
        // We have to set it now because gtk_clipboard_set_with_data() calls clipboard_clear_cb()
        // which reset our internal state 
        if (aWhichClipboard == kSelectionClipboard) {
            mSelectionOwner = aOwner;
            mSelectionTransferable = aTransferable;
        }
        else {
            mGlobalOwner = aOwner;
            mGlobalTransferable = aTransferable;
            gtk_clipboard_set_can_store(gtkClipboard, gtkTargets, numTargets);
        }

        rv = NS_OK;
    }
    else {  
        rv = NS_ERROR_FAILURE;
    }

    gtk_target_table_free(gtkTargets, numTargets);
    gtk_target_list_unref(list);
  
    return rv;
}
Example #21
0
void selection_to_clip(void)
{
  struct XojSelectionData *sel;
  int bufsz, nitems, val;
  char *p;
  GList *list;
  struct Item *item;
  GtkTargetList *targetlist;
  GtkTargetEntry *targets;
  int n_targets;
  
  if (ui.selection == NULL) return;
  bufsz = 2*sizeof(int) // bufsz, nitems
        + sizeof(struct BBox); // bbox
  nitems = 0;
  for (list = ui.selection->items; list != NULL; list = list->next) {
    item = (struct Item *)list->data;
    nitems++;
    if (item->type == ITEM_STROKE) {
      bufsz+= sizeof(int) // type
            + sizeof(struct Brush) // brush
            + sizeof(int) // num_points
            + 2*item->path->num_points*sizeof(double); // the points
      if (item->brush.variable_width)
        bufsz += (item->path->num_points-1)*sizeof(double); // the widths
    }
    else if (item->type == ITEM_TEXT) {
      bufsz+= sizeof(int) // type
            + sizeof(struct Brush) // brush
            + 2*sizeof(double) // bbox upper-left
            + sizeof(int) // text len
            + strlen(item->text)+1 // text
            + sizeof(int) // font_name len
            + strlen(item->font_name)+1 // font_name
            + sizeof(double); // font_size
    }
    else if (item->type == ITEM_IMAGE) {
      if (item->image_png == NULL) {
        set_cursor_busy(TRUE);
        if (!gdk_pixbuf_save_to_buffer(item->image, &item->image_png, &item->image_png_len, "png", NULL, NULL))
          item->image_png_len = 0;       // failed for some reason, so forget it
        set_cursor_busy(FALSE);
      }
      bufsz+= sizeof(int) // type
        + sizeof(struct BBox)
        + sizeof(gsize) // png_buflen
        + item->image_png_len;
    }
    else bufsz+= sizeof(int); // type
  }

  // allocate selection data structure and buffer
  sel = g_malloc(sizeof(struct XojSelectionData));
  sel->xo_data_len = bufsz;
  sel->xo_data = g_malloc(bufsz);
  sel->image_data = NULL;
  sel->text_data = NULL;

  // fill in the data
  p = sel->xo_data;
  g_memmove(p, &bufsz, sizeof(int)); p+= sizeof(int);
  g_memmove(p, &nitems, sizeof(int)); p+= sizeof(int);
  g_memmove(p, &ui.selection->bbox, sizeof(struct BBox)); p+= sizeof(struct BBox);
  for (list = ui.selection->items; list != NULL; list = list->next) {
    item = (struct Item *)list->data;
    g_memmove(p, &item->type, sizeof(int)); p+= sizeof(int);
    if (item->type == ITEM_STROKE) {
      g_memmove(p, &item->brush, sizeof(struct Brush)); p+= sizeof(struct Brush);
      g_memmove(p, &item->path->num_points, sizeof(int)); p+= sizeof(int);
      g_memmove(p, item->path->coords, 2*item->path->num_points*sizeof(double));
      p+= 2*item->path->num_points*sizeof(double);
      if (item->brush.variable_width) {
        g_memmove(p, item->widths, (item->path->num_points-1)*sizeof(double));
        p+= (item->path->num_points-1)*sizeof(double);
      }
    }
    if (item->type == ITEM_TEXT) {
      g_memmove(p, &item->brush, sizeof(struct Brush)); p+= sizeof(struct Brush);
      g_memmove(p, &item->bbox.left, sizeof(double)); p+= sizeof(double);
      g_memmove(p, &item->bbox.top, sizeof(double)); p+= sizeof(double);
      val = strlen(item->text);
      g_memmove(p, &val, sizeof(int)); p+= sizeof(int);
      g_memmove(p, item->text, val+1); p+= val+1;
      val = strlen(item->font_name);
      g_memmove(p, &val, sizeof(int)); p+= sizeof(int);
      g_memmove(p, item->font_name, val+1); p+= val+1;
      g_memmove(p, &item->font_size, sizeof(double)); p+= sizeof(double);
      if (nitems==1) sel->text_data = g_strdup(item->text); // single text item
    }
    if (item->type == ITEM_IMAGE) {
      g_memmove(p, &item->bbox, sizeof(struct BBox)); p+= sizeof(struct BBox);
      g_memmove(p, &item->image_png_len, sizeof(gsize)); p+= sizeof(gsize);
      if (item->image_png_len > 0) {
        g_memmove(p, item->image_png, item->image_png_len); p+= item->image_png_len;
      }
      if (nitems==1) sel->image_data = gdk_pixbuf_copy(item->image); // single image
    }
  }
  
  /* build list of valid targets */
  targetlist = gtk_target_list_new(NULL, 0);
  gtk_target_list_add(targetlist, 
    gdk_atom_intern(XOURNAL_TARGET_ATOM, FALSE), 0, TARGET_XOURNAL);
  if (sel->image_data!=NULL)
    gtk_target_list_add_image_targets(targetlist, TARGET_PIXBUF, TRUE);
  if (sel->text_data!=NULL) 
    gtk_target_list_add_text_targets(targetlist, TARGET_TEXT);
  targets = gtk_target_table_new_from_list(targetlist, &n_targets);
  gtk_target_list_unref(targetlist);
  
  gtk_clipboard_set_with_data(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), 
       targets, n_targets,
       callback_clipboard_get, callback_clipboard_clear, sel);
  gtk_target_table_free(targets, n_targets);
}