⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sec-retrievingtheselection.html

📁 gtk 开发手册和参考文档。 包括gtk glib gdk等
💻 HTML
字号:
<HTML><HEAD><TITLE>Retrieving the selection</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.49"><LINKREL="HOME"TITLE="GTK+ 1.2 Tutorial"HREF="gtk-tut.html"><LINKREL="UP"TITLE="Managing Selections"HREF="ch-managingselections.html"><LINKREL="PREVIOUS"TITLE="Managing Selections"HREF="ch-managingselections.html"><LINKREL="NEXT"TITLE="Supplying the selection"HREF="sec-supplyingtheselection.html"></HEAD></head><body bgcolor="#FFFFFF" marginheight=0 marginwidth=0 width="100%"><table cellspacing=6 border=0 cellpadding=0 width="100%">  <tr>    <td bgcolor="#FFFFFF" valign=top nowrap>    <centeR><A HREF="/"><img src="/images/gtk-logo-rgb.gif" width=107 height=140 border=0></A>      <font face="helvetica,lucidia" color="#000000"><BR><BR><b>GTK+<BR>The GIMP Toolkit</b></center><BR><table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>General</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/">Introduction</A><BR><A HREF="/screenshots/">Screenshots</A><br><A HREF="/download/">Download</A><br><A HREF="/mailinglists.html">Mailing Lists</A><BR><A HREF="/bindings.html">Language Bindings</A><BR><A HREF="http://gtk.themes.org/">Themes</A><BR><A HREF="/bugs.html">Bug Tracker</A><BR></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Documentation</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/faq/">FAQ</A><br><A HREF="/tutorial/">Tutorial</A><BR><A HREF="/api/">API Reference</A><br><A HREF="/books.html">Published Books</A><BR></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Projects</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.pango.org/">Pango</A><BR><A HREF="http://sources.redhat.com/inti/">Inti</A><BR><A HREF="http://www.gnome.org/">GNOME</A><BR><A HREF="http://user.sgic.fi/~tml/gimp/win32/">GTK+ for Win32</A><br><A HREF="http://people.redhat.com/sopwith/gtkfb/">GtkFB (Framebuffer)</A><br><A HREF="http://www.directfb.org/gtk.xml">GTK+ on DirectFB</A><BR><A HREF="/beos/">GTK+ for BeOS</A></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B><B>Applications</B></B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.gimp.org/">GIMP</A><BR><A HREF="http://www.abiword.org/">Abiword</A><BR><A HREF="http://www.lysator.liu.se/~alla/dia/dia.html">Dia</A><BR><A HREF="http://glade.pn.org/">Glade</A><BR><A HREF="http://www.gnucash.org/">GnuCash</A><BR><A HREF="http://www.gnome.org/projects/gnumeric/">Gnumeric</A><BR><BR><A HREF="http://www.gnome.org/applist/">GNOME Software Map</A><br></td></tr></table></td></tr></table><BR>      </td>  <td bgcolor="#ffffff" valign=top width="99%"><font face="lucida,helvetica"><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GTK+ 1.2 Tutorial</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="ch-managingselections.html">&#60;&#60;&#60; Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 20. Managing Selections</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-supplyingtheselection.html">Next &#62;&#62;&#62;</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-RETRIEVINGTHESELECTION">20.2. Retrieving the selection</A></H1><P>Retrieving the selection is an asynchronous process. To start theprocess, you call:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gint gtk_selection_convert( GtkWidget *widget,                             GdkAtom    selection,                             GdkAtom    target,                            guint32    time );</PRE></TD></TR></TABLE><P>This <ICLASS="EMPHASIS">converts</I> the selection into the form specified by<TTCLASS="LITERAL">target</TT>. If at all possible, the time field should be the timefrom the event that triggered the selection. This helps make sure thatevents occur in the order that the user requested them. However, if itis not available (for instance, if the conversion was triggered by a"clicked" signal), then you can use the constant<TTCLASS="LITERAL">GDK_CURRENT_TIME</TT>.</P><P>When the selection owner responds to the request, a"selection_received" signal is sent to your application. The handlerfor this signal receives a pointer to a <TTCLASS="LITERAL">GtkSelectionData</TT>structure, which is defined as:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">struct _GtkSelectionData{  GdkAtom selection;  GdkAtom target;  GdkAtom type;  gint    format;  guchar *data;  gint    length;};</PRE></TD></TR></TABLE><P><TTCLASS="LITERAL">selection</TT> and <TTCLASS="LITERAL">target</TT> are the values you gave in your<TTCLASS="LITERAL">gtk_selection_convert()</TT> call. <TTCLASS="LITERAL">type</TT> is an atom thatidentifies the type of data returned by the selection owner. Somepossible values are "STRING", a string of latin-1 characters, "ATOM",a series of atoms, "INTEGER", an integer, etc. Most targets can onlyreturn one type. <TTCLASS="LITERAL">format</TT> gives the length of the units (forinstance characters) in bits. Usually, you don't care about this whenreceiving data. <TTCLASS="LITERAL">data</TT> is a pointer to the returned data, and<TTCLASS="LITERAL">length</TT> gives the length of the returned data, in bytes. If<TTCLASS="LITERAL">length</TT> is negative, then an error occurred and the selectioncould not be retrieved. This might happen if no application owned theselection, or if you requested a target that the application didn'tsupport. The buffer is actually guaranteed to be one byte longer than<TTCLASS="LITERAL">length</TT>; the extra byte will always be zero, so it isn'tnecessary to make a copy of strings just to null terminate them.</P><P>In the following example, we retrieve the special target "TARGETS",which is a list of all targets into which the selection can beconverted.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* example-start selection gettargets.c */#include &#60;gtk/gtk.h&#62;void selection_received( GtkWidget        *widget,                          GtkSelectionData *selection_data,                          gpointer          data );/* Signal handler invoked when user clicks on the "Get Targets" button */void get_targets( GtkWidget *widget,                  gpointer data ){  static GdkAtom targets_atom = GDK_NONE;  /* Get the atom corresponding to the string "TARGETS" */  if (targets_atom == GDK_NONE)    targets_atom = gdk_atom_intern ("TARGETS", FALSE);  /* And request the "TARGETS" target for the primary selection */  gtk_selection_convert (widget, GDK_SELECTION_PRIMARY, targets_atom,			 GDK_CURRENT_TIME);}/* Signal handler called when the selections owner returns the data */void selection_received( GtkWidget        *widget,                         GtkSelectionData *selection_data, 		         gpointer          data ){  GdkAtom *atoms;  GList *item_list;  int i;  /* **** IMPORTANT **** Check to see if retrieval succeeded  */  if (selection_data-&#62;length &#60; 0)    {      g_print ("Selection retrieval failed\n");      return;    }  /* Make sure we got the data in the expected form */  if (selection_data-&#62;type != GDK_SELECTION_TYPE_ATOM)    {      g_print ("Selection \"TARGETS\" was not returned as atoms!\n");      return;    }    /* Print out the atoms we received */  atoms = (GdkAtom *)selection_data-&#62;data;  item_list = NULL;  for (i=0; i&#60;selection_data-&#62;length/sizeof(GdkAtom); i++)    {      char *name;      name = gdk_atom_name (atoms[i]);      if (name != NULL)	g_print ("%s\n",name);      else	g_print ("(bad atom)\n");    }  return;}int main( int   argc,          char *argv[] ){  GtkWidget *window;  GtkWidget *button;    gtk_init (&#38;argc, &#38;argv);  /* Create the toplevel window */  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);  gtk_window_set_title (GTK_WINDOW (window), "Event Box");  gtk_container_set_border_width (GTK_CONTAINER (window), 10);  gtk_signal_connect (GTK_OBJECT (window), "destroy",		      GTK_SIGNAL_FUNC (gtk_exit), NULL);  /* Create a button the user can click to get targets */  button = gtk_button_new_with_label ("Get Targets");  gtk_container_add (GTK_CONTAINER (window), button);  gtk_signal_connect (GTK_OBJECT(button), "clicked",		      GTK_SIGNAL_FUNC (get_targets), NULL);  gtk_signal_connect (GTK_OBJECT(button), "selection_received",		      GTK_SIGNAL_FUNC (selection_received), NULL);  gtk_widget_show (button);  gtk_widget_show (window);    gtk_main ();    return 0;}/* example-end */</PRE></TD></TR></TABLE></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="ch-managingselections.html">&#60;&#60;&#60; Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sec-supplyingtheselection.html">Next &#62;&#62;&#62;</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Managing Selections</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-managingselections.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Supplying the selection</TD></TR></TABLE></DIV>        </td>    </tr></table>  </td>  </tr></table></body></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -