📄 gtkselection.c
字号:
/* GTK - The GIMP Toolkit * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library 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. *//* This file implements most of the work of the ICCM selection protocol. * The code was written after an intensive study of the equivalent part * of John Ousterhout's Tk toolkit, and does many things in much the * same way. * * The one thing in the ICCM that isn't fully supported here (or in Tk) * is side effects targets. For these to be handled properly, MULTIPLE * targets need to be done in the order specified. This cannot be * guaranteed with the way we do things, since if we are doing INCR * transfers, the order will depend on the timing of the requestor. * * By Owen Taylor <owt1@cornell.edu> 8/16/97 *//* Terminology note: when not otherwise specified, the term "incr" below * refers to the _sending_ part of the INCR protocol. The receiving * portion is referred to just as "retrieval". (Terminology borrowed * from Tk, because there is no good opposite to "retrieval" in English. * "send" can't be made into a noun gracefully and we're already using * "emission" for something else ....) *//* The MOTIF entry widget seems to ask for the TARGETS target, then (regardless of the reply) ask for the TEXT target. It's slightly possible though that it somehow thinks we are responding negatively to the TARGETS request, though I don't really think so ... *//* * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS * file for a list of people on the GTK+ Team. See the ChangeLog * files for a list of changes. These files are distributed with * GTK+ at ftp://ftp.gtk.org/pub/gtk/. */#include <stdarg.h>#include <string.h>#include <gdk/gdkx.h>/* we need this for gdk_window_lookup() */#include "gtkmain.h"#include "gtkselection.h"#include "gtksignal.h"/* #define DEBUG_SELECTION *//* Maximum size of a sent chunk, in bytes. Also the default size of our buffers */#define GTK_SELECTION_MAX_SIZE 4000enum { INCR, MULTIPLE, TARGETS, TIMESTAMP, LAST_ATOM};typedef struct _GtkSelectionInfo GtkSelectionInfo;typedef struct _GtkIncrConversion GtkIncrConversion;typedef struct _GtkIncrInfo GtkIncrInfo;typedef struct _GtkRetrievalInfo GtkRetrievalInfo;struct _GtkSelectionInfo{ GdkAtom selection; GtkWidget *widget; /* widget that owns selection */ guint32 time; /* time used to acquire selection */};struct _GtkIncrConversion { GdkAtom target; /* Requested target */ GdkAtom property; /* Property to store in */ GtkSelectionData data; /* The data being supplied */ gint offset; /* Current offset in sent selection. * -1 => All done * -2 => Only the final (empty) portion * left to send */};struct _GtkIncrInfo{ GtkWidget *widget; /* Selection owner */ GdkWindow *requestor; /* Requestor window - we create a GdkWindow so we can receive events */ GdkAtom selection; /* Selection we're sending */ GtkIncrConversion *conversions; /* Information about requested conversions - * With MULTIPLE requests (benighted 1980's * hardware idea), there can be more than * one */ gint num_conversions; gint num_incrs; /* number of remaining INCR style transactions */ guint32 idle_time;};struct _GtkRetrievalInfo{ GtkWidget *widget; GdkAtom selection; /* Selection being retrieved. */ GdkAtom target; /* Form of selection that we requested */ guint32 idle_time; /* Number of seconds since we last heard from selection owner */ guchar *buffer; /* Buffer in which to accumulate results */ gint offset; /* Current offset in buffer, -1 indicates not yet started */ guint32 notify_time; /* Timestamp from SelectionNotify */};/* Local Functions */static void gtk_selection_init (void);static gint gtk_selection_incr_timeout (GtkIncrInfo *info);static gint gtk_selection_retrieval_timeout (GtkRetrievalInfo *info);static void gtk_selection_retrieval_report (GtkRetrievalInfo *info, GdkAtom type, gint format, guchar *buffer, gint length, guint32 time);static void gtk_selection_invoke_handler (GtkWidget *widget, GtkSelectionData *data, guint time);static void gtk_selection_default_handler (GtkWidget *widget, GtkSelectionData *data);/* Local Data */static gint initialize = TRUE;static GList *current_retrievals = NULL;static GList *current_incrs = NULL;static GList *current_selections = NULL;static GdkAtom gtk_selection_atoms[LAST_ATOM];static const char *gtk_selection_handler_key = "gtk-selection-handlers";/**************** * Target Lists * ****************//* * Target lists */GtkTargetList *gtk_target_list_new (const GtkTargetEntry *targets, guint ntargets){ GtkTargetList *result = g_new (GtkTargetList, 1); result->list = NULL; result->ref_count = 1; if (targets) gtk_target_list_add_table (result, targets, ntargets); return result;}void gtk_target_list_ref (GtkTargetList *list){ g_return_if_fail (list != NULL); list->ref_count++;}void gtk_target_list_unref (GtkTargetList *list){ g_return_if_fail (list != NULL); g_return_if_fail (list->ref_count > 0); list->ref_count--; if (list->ref_count == 0) { GList *tmp_list = list->list; while (tmp_list) { GtkTargetPair *pair = tmp_list->data; g_free (pair); tmp_list = tmp_list->next; } g_list_free (list->list); g_free (list); }}void gtk_target_list_add (GtkTargetList *list, GdkAtom target, guint flags, guint info){ GtkTargetPair *pair; g_return_if_fail (list != NULL); pair = g_new (GtkTargetPair, 1); pair->target = target; pair->flags = flags; pair->info = info; list->list = g_list_append (list->list, pair);}void gtk_target_list_add_table (GtkTargetList *list, const GtkTargetEntry *targets, guint ntargets){ gint i; for (i=ntargets-1; i >= 0; i--) { GtkTargetPair *pair = g_new (GtkTargetPair, 1); pair->target = gdk_atom_intern (targets[i].target, FALSE); pair->flags = targets[i].flags; pair->info = targets[i].info; list->list = g_list_prepend (list->list, pair); }}void gtk_target_list_remove (GtkTargetList *list, GdkAtom target){ GList *tmp_list; g_return_if_fail (list != NULL); tmp_list = list->list; while (tmp_list) { GtkTargetPair *pair = tmp_list->data; if (pair->target == target) { g_free (pair); list->list = g_list_remove (list->list, tmp_list); g_list_free_1 (tmp_list); return; } tmp_list = tmp_list->next; }}gbooleangtk_target_list_find (GtkTargetList *list, GdkAtom target, guint *info){ GList *tmp_list = list->list; while (tmp_list) { GtkTargetPair *pair = tmp_list->data; if (pair->target == target) { *info = pair->info; return TRUE; } tmp_list = tmp_list->next; } return FALSE;}/************************************************************* * gtk_selection_owner_set: * Claim ownership of a selection. * arguments: * widget: new selection owner * selection: which selection * time: time (use GDK_CURRENT_TIME only if necessary) * * results: *************************************************************/gintgtk_selection_owner_set (GtkWidget *widget, GdkAtom selection, guint32 time){ GList *tmp_list; GtkWidget *old_owner; GtkSelectionInfo *selection_info = NULL; GdkWindow *window; if (widget == NULL) window = NULL; else { if (!GTK_WIDGET_REALIZED (widget)) gtk_widget_realize (widget); window = widget->window; } tmp_list = current_selections; while (tmp_list) { selection_info = (GtkSelectionInfo *)tmp_list->data; if (selection_info->selection == selection) break; tmp_list = tmp_list->next; } if (tmp_list == NULL) selection_info = NULL; else if (selection_info->widget == widget) return TRUE; if (gdk_selection_owner_set (window, selection, time, TRUE)) { old_owner = NULL; if (widget == NULL) { if (selection_info) { old_owner = selection_info->widget; current_selections = g_list_remove_link (current_selections, tmp_list); g_list_free (tmp_list); g_free (selection_info); } } else { if (selection_info == NULL) { selection_info = g_new (GtkSelectionInfo, 1); selection_info->selection = selection; selection_info->widget = widget; selection_info->time = time; current_selections = g_list_append (current_selections, selection_info); } else { old_owner = selection_info->widget; selection_info->widget = widget; selection_info->time = time; } } /* If another widget in the application lost the selection, * send it a GDK_SELECTION_CLEAR event, unless we're setting * the owner to None, in which case an event will be sent */ if (old_owner && (widget != NULL)) { GdkEventSelection event; event.type = GDK_SELECTION_CLEAR; event.window = old_owner->window; event.selection = selection; event.time = time; gtk_widget_event (old_owner, (GdkEvent *) &event); } return TRUE; } else return FALSE;}/************************************************************* * gtk_selection_add_target * Add specified target to list of supported targets * * arguments: * widget: The widget for which this target applies * selection: * target: * info: guint to pass to to the selection_get signal * * results: *************************************************************/typedef struct _GtkSelectionTargetList GtkSelectionTargetList;struct _GtkSelectionTargetList { GdkAtom selection; GtkTargetList *list;};static GtkTargetList *gtk_selection_target_list_get (GtkWidget *widget, GdkAtom selection){ GtkSelectionTargetList *sellist; GList *tmp_list; GList *lists; lists = gtk_object_get_data (GTK_OBJECT (widget), gtk_selection_handler_key); tmp_list = lists; while (tmp_list) { sellist = tmp_list->data; if (sellist->selection == selection) return sellist->list; tmp_list = tmp_list->next; } sellist = g_new (GtkSelectionTargetList, 1); sellist->selection = selection; sellist->list = gtk_target_list_new (NULL, 0); lists = g_list_prepend (lists, sellist); gtk_object_set_data (GTK_OBJECT (widget), gtk_selection_handler_key, lists); return sellist->list;}static voidgtk_selection_target_list_remove (GtkWidget *widget){ GtkSelectionTargetList *sellist; GList *tmp_list; GList *lists; lists = gtk_object_get_data (GTK_OBJECT (widget), gtk_selection_handler_key); tmp_list = lists; while (tmp_list) { sellist = tmp_list->data; gtk_target_list_unref (sellist->list); g_free (sellist); tmp_list = tmp_list->next; } g_list_free (lists); gtk_object_set_data (GTK_OBJECT (widget), gtk_selection_handler_key, NULL);}void gtk_selection_add_target (GtkWidget *widget, GdkAtom selection, GdkAtom target, guint info){ GtkTargetList *list; g_return_if_fail (widget != NULL); list = gtk_selection_target_list_get (widget, selection); gtk_target_list_add (list, target, 0, info);}void gtk_selection_add_targets (GtkWidget *widget, GdkAtom selection, const GtkTargetEntry *targets, guint ntargets){ GtkTargetList *list; g_return_if_fail (widget != NULL); g_return_if_fail (targets != NULL); list = gtk_selection_target_list_get (widget, selection); gtk_target_list_add_table (list, targets, ntargets);}/************************************************************* * gtk_selection_remove_all: * Removes all handlers and unsets ownership of all * selections for a widget. Called when widget is being * destroyed * * arguments: * widget: The widget * results: *************************************************************/voidgtk_selection_remove_all (GtkWidget *widget){ GList *tmp_list; GList *next; GtkSelectionInfo *selection_info; /* Remove pending requests/incrs for this widget */ tmp_list = current_incrs; while (tmp_list) { next = tmp_list->next; if (((GtkIncrInfo *)tmp_list->data)->widget == widget) { current_incrs = g_list_remove_link (current_incrs, tmp_list); /* structure will be freed in timeout */ g_list_free (tmp_list); } tmp_list = next; } tmp_list = current_retrievals; while (tmp_list)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -