📄 gdkselection-x11.c
字号:
/* GDK - The GIMP Drawing Kit * 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 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. *//* * Modified by the GTK+ Team and others 1997-2000. 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 <config.h>#include <X11/Xlib.h>#include <X11/Xatom.h>#include <string.h>#include "gdkx.h"#include "gdkproperty.h"#include "gdkselection.h"#include "gdkprivate.h"#include "gdkprivate-x11.h"#include "gdkdisplay-x11.h"#include "gdkalias.h"typedef struct _OwnerInfo OwnerInfo;struct _OwnerInfo{ GdkAtom selection; GdkWindow *owner; gulong serial;};static GSList *owner_list;/* When a window is destroyed we check if it is the owner * of any selections. This is somewhat inefficient, but * owner_list is typically short, and it is a low memory, * low code solution */void_gdk_selection_window_destroyed (GdkWindow *window){ GSList *tmp_list = owner_list; while (tmp_list) { OwnerInfo *info = tmp_list->data; tmp_list = tmp_list->next; if (info->owner == window) { owner_list = g_slist_remove (owner_list, info); g_free (info); } }}/* We only pass through those SelectionClear events that actually * reflect changes to the selection owner that we didn't make ourself. */gboolean_gdk_selection_filter_clear_event (XSelectionClearEvent *event){ GSList *tmp_list = owner_list; GdkDisplay *display = gdk_x11_lookup_xdisplay (event->display); while (tmp_list) { OwnerInfo *info = tmp_list->data; if (gdk_drawable_get_display (info->owner) == display && info->selection == gdk_x11_xatom_to_atom_for_display (display, event->selection)) { if ((GDK_DRAWABLE_XID (info->owner) == event->window && event->serial >= info->serial)) { owner_list = g_slist_remove (owner_list, info); g_free (info); return TRUE; } else return FALSE; } tmp_list = tmp_list->next; } return FALSE;}/** * gdk_selection_owner_set_for_display: * @display: the #GdkDisplay. * @owner: a #GdkWindow or %NULL to indicate that the owner for * the given should be unset. * @selection: an atom identifying a selection. * @time_: timestamp to use when setting the selection. * If this is older than the timestamp given last time the owner was * set for the given selection, the request will be ignored. * @send_event: if %TRUE, and the new owner is different from the current * owner, the current owner will be sent a SelectionClear event. * * Sets the #GdkWindow @owner as the current owner of the selection @selection. * * Returns: %TRUE if the selection owner was successfully changed to owner, * otherwise %FALSE. * * Since: 2.2 */gbooleangdk_selection_owner_set_for_display (GdkDisplay *display, GdkWindow *owner, GdkAtom selection, guint32 time, gboolean send_event){ Display *xdisplay; Window xwindow; Atom xselection; GSList *tmp_list; OwnerInfo *info; g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE); g_return_val_if_fail (selection != GDK_NONE, FALSE); if (display->closed) return FALSE; if (owner) { if (GDK_WINDOW_DESTROYED (owner)) return FALSE; xdisplay = GDK_WINDOW_XDISPLAY (owner); xwindow = GDK_WINDOW_XID (owner); } else { xdisplay = GDK_DISPLAY_XDISPLAY (display); xwindow = None; } xselection = gdk_x11_atom_to_xatom_for_display (display, selection); tmp_list = owner_list; while (tmp_list) { info = tmp_list->data; if (info->selection == selection) { owner_list = g_slist_remove (owner_list, info); g_free (info); break; } tmp_list = tmp_list->next; } if (owner) { info = g_new (OwnerInfo, 1); info->owner = owner; info->serial = NextRequest (GDK_WINDOW_XDISPLAY (owner)); info->selection = selection; owner_list = g_slist_prepend (owner_list, info); } XSetSelectionOwner (xdisplay, xselection, xwindow, time); return (XGetSelectionOwner (xdisplay, xselection) == xwindow);}/** * gdk_selection_owner_get_for_display: * @display: a #GdkDisplay. * @selection: an atom indentifying a selection. * * Determine the owner of the given selection. * * Note that the return value may be owned by a different * process if a foreign window was previously created for that * window, but a new foreign window will never be created by this call. * * Returns: if there is a selection owner for this window, and it is a * window known to the current process, the #GdkWindow that owns the * selection, otherwise %NULL. * * Since: 2.2 */ GdkWindow *gdk_selection_owner_get_for_display (GdkDisplay *display, GdkAtom selection){ Window xwindow; g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL); g_return_val_if_fail (selection != GDK_NONE, NULL); if (display->closed) return NULL; xwindow = XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), gdk_x11_atom_to_xatom_for_display (display, selection)); if (xwindow == None) return NULL; return gdk_window_lookup_for_display (display, xwindow);}voidgdk_selection_convert (GdkWindow *requestor, GdkAtom selection, GdkAtom target, guint32 time){ GdkDisplay *display; g_return_if_fail (selection != GDK_NONE); if (GDK_WINDOW_DESTROYED (requestor)) return; display = GDK_WINDOW_DISPLAY (requestor); XConvertSelection (GDK_WINDOW_XDISPLAY (requestor), gdk_x11_atom_to_xatom_for_display (display, selection), gdk_x11_atom_to_xatom_for_display (display, target), gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property), GDK_WINDOW_XID (requestor), time);}/** * gdk_selection_property_get: * @requestor: the window on which the data is stored * @data: location to store a pointer to the retrieved data. If the retrieval failed, %NULL we be stored here, otherwise, it will be non-%NULL and the returned data should be freed with g_free() when you are finished using it. The length of the allocated memory is one more than the length of the returned data, and the final byte will always be zero, to ensure nul-termination of strings. * @prop_type: location to store the type of the property. * @prop_format: location to store the format of the property. * * Retrieves selection data that was stored by the selection * data in response to a call to gdk_selection_convert(). This function * will not be used by applications, who should use the #GtkClipboard * API instead. * * Return value: the length of the retrieved data. **/gintgdk_selection_property_get (GdkWindow *requestor, guchar **data, GdkAtom *ret_type, gint *ret_format){ gulong nitems; gulong nbytes; gulong length = 0; /* Quiet GCC */ Atom prop_type; gint prop_format; guchar *t = NULL; GdkDisplay *display; g_return_val_if_fail (requestor != NULL, 0); g_return_val_if_fail (GDK_IS_WINDOW (requestor), 0); display = GDK_WINDOW_DISPLAY (requestor); if (GDK_WINDOW_DESTROYED (requestor)) goto err; t = NULL; /* We can't delete the selection here, because it might be the INCR protocol, in which case the client has to make sure they'll be notified of PropertyChange events _before_ the property is deleted. Otherwise there's no guarantee we'll win the race ... */ if (XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (requestor), GDK_DRAWABLE_XID (requestor), gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property), 0, 0x1FFFFFFF /* MAXINT32 / 4 */, False, AnyPropertyType, &prop_type, &prop_format, &nitems, &nbytes, &t) != Success) goto err; if (prop_type != None) { if (ret_type) *ret_type = gdk_x11_xatom_to_atom_for_display (display, prop_type); if (ret_format) *ret_format = prop_format; if (prop_type == XA_ATOM || prop_type == gdk_x11_get_xatom_by_name_for_display (display, "ATOM_PAIR")) { Atom* atoms = (Atom*) t; GdkAtom* atoms_dest; gint num_atom, i; if (prop_format != 32) goto err; num_atom = nitems; length = sizeof (GdkAtom) * num_atom + 1; if (data) { *data = g_malloc (length); (*data)[length - 1] = '\0'; atoms_dest = (GdkAtom *)(*data); for (i=0; i < num_atom; i++) atoms_dest[i] = gdk_x11_xatom_to_atom_for_display (display, atoms[i]); } } else { switch (prop_format) { case 8: length = nitems; break; case 16: length = sizeof(short) * nitems; break; case 32: length = sizeof(long) * nitems; break; default: g_assert_not_reached (); break; } /* Add on an extra byte to handle null termination. X guarantees that t will be 1 longer than nitems and null terminated */ length += 1; if (data) *data = g_memdup (t, length); } if (t) XFree (t); return length - 1; } err: if (ret_type) *ret_type = GDK_NONE; if (ret_format) *ret_format = 0; if (data) *data = NULL; return 0;}/** * gdk_selection_send_notify_for_display: * @display: the #GdkDisplay where @requestor is realized * @requestor: window to which to deliver response. * @selection: selection that was requested. * @target: target that was selected. * @property: property in which the selection owner stored the data, * or %GDK_NONE to indicate that the request was rejected. * @time_: timestamp. * * Send a response to SelectionRequest event. * * Since: 2.2 **/voidgdk_selection_send_notify_for_display (GdkDisplay *display, guint32 requestor, GdkAtom selection, GdkAtom target, GdkAtom property, guint32 time){ XSelectionEvent xevent; g_return_if_fail (GDK_IS_DISPLAY (display)); xevent.type = SelectionNotify; xevent.serial = 0; xevent.send_event = True; xevent.requestor = requestor; xevent.selection = gdk_x11_atom_to_xatom_for_display (display, selection); xevent.target = gdk_x11_atom_to_xatom_for_display (display, target); xevent.property = gdk_x11_atom_to_xatom_for_display (display, property); xevent.time = time; _gdk_send_xevent (display, requestor, False, NoEventMask, (XEvent*) & xevent);}/** * gdk_text_property_to_text_list_for_display: * @display: The #GdkDisplay where the encoding is defined. * @encoding: an atom representing the encoding. The most * common values for this are STRING, or COMPOUND_TEXT. * This is value used as the type for the property. * @format: the format of the property. * @text: The text data. * @length: The number of items to transform. * @list: location to store a terminated array of strings in * the encoding of the current locale. This array should be * freed using gdk_free_text_list(). * * Convert a text string from the encoding as it is stored * in a property into an array of strings in the encoding of * the current locale. (The elements of the array represent the * nul-separated elements of the original text string.) * * Returns: the number of strings stored in list, or 0, * if the conversion failed. * * Since: 2.2 */gintgdk_text_property_to_text_list_for_display (GdkDisplay *display, GdkAtom encoding, gint format, const guchar *text, gint length, gchar ***list){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -