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

📄 sec-supplyingtheselection.html

📁 gtk 开发手册和参考文档。 包括gtk glib gdk等
💻 HTML
字号:
<HTML><HEAD><TITLE>Supplying 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="Retrieving the selection"HREF="sec-retrievingtheselection.html"><LINKREL="NEXT"TITLE="Drag-and-drop (DND)"HREF="ch-dragangdrop.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="sec-retrievingtheselection.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="ch-dragangdrop.html">Next &#62;&#62;&#62;</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-SUPPLYINGTHESELECTION">20.3. Supplying the selection</A></H1><P>Supplying the selection is a bit more complicated. You must register handlers that will be called when your selection is requested. Foreach selection/target pair you will handle, you make a call to:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_selection_add_target (GtkWidget           *widget,                                GdkAtom              selection,                               GdkAtom              target,                               guint                info);</PRE></TD></TR></TABLE><P><TTCLASS="LITERAL">widget</TT>, <TTCLASS="LITERAL">selection</TT>, and <TTCLASS="LITERAL">target</TT> identify the requeststhis handler will manage. When a request for a selection is received,the "selection_get" signal will be called. <TTCLASS="LITERAL">info</TT> can be used as anenumerator to identify the specific target within the callback function.</P><P>The callback function has the signature:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void  "selection_get" (GtkWidget          *widget,                       GtkSelectionData   *selection_data,                       guint               info,                       guint               time);</PRE></TD></TR></TABLE><P>The GtkSelectionData is the same as above, but this time, we'reresponsible for filling in the fields <TTCLASS="LITERAL">type</TT>, <TTCLASS="LITERAL">format</TT>,<TTCLASS="LITERAL">data</TT>, and <TTCLASS="LITERAL">length</TT>. (The <TTCLASS="LITERAL">format</TT> field is actuallyimportant here - the X server uses it to figure out whether the dataneeds to be byte-swapped or not. Usually it will be 8 - <ICLASS="EMPHASIS">i.e.</I> acharacter - or 32 - <ICLASS="EMPHASIS">i.e.</I> a. integer.) This is done by calling thefunction:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_selection_data_set( GtkSelectionData *selection_data,                             GdkAtom           type,                             gint              format,                             guchar           *data,                             gint              length );</PRE></TD></TR></TABLE><P>This function takes care of properly making a copy of the data so thatyou don't have to worry about keeping it around. (You should not fillin the fields of the GtkSelectionData structure by hand.)</P><P>When prompted by the user, you claim ownership of the selection bycalling:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gint gtk_selection_owner_set( GtkWidget *widget,                              GdkAtom    selection,                              guint32    time );</PRE></TD></TR></TABLE><P>If another application claims ownership of the selection, you willreceive a "selection_clear_event".</P><P>As an example of supplying the selection, the following program addsselection functionality to a toggle button. When the toggle button isdepressed, the program claims the primary selection. The only targetsupported (aside from certain targets like "TARGETS" supplied by GTKitself), is the "STRING" target. When this target is requested, astring representation of the time is returned.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* example-start selection setselection.c */#include &#60;gtk/gtk.h&#62;#include &#60;time.h&#62;/* Callback when the user toggles the selection */void selection_toggled( GtkWidget *widget,                        gint      *have_selection ){  if (GTK_TOGGLE_BUTTON(widget)-&#62;active)    {      *have_selection = gtk_selection_owner_set (widget,						 GDK_SELECTION_PRIMARY,						 GDK_CURRENT_TIME);      /* if claiming the selection failed, we return the button to	 the out state */      if (!*have_selection)	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(widget), FALSE);    }  else    {      if (*have_selection)	{	  /* Before clearing the selection by setting the owner to NULL,	     we check if we are the actual owner */	  if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget-&#62;window)	    gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY,				     GDK_CURRENT_TIME);	  *have_selection = FALSE;	}    }}/* Called when another application claims the selection */gint selection_clear( GtkWidget         *widget,                      GdkEventSelection *event,                      gint              *have_selection ){  *have_selection = FALSE;  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(widget), FALSE);  return TRUE;}/* Supplies the current time as the selection. */void selection_handle( GtkWidget        *widget,                        GtkSelectionData *selection_data,                       guint             info,                       guint             time_stamp,                       gpointer          data ){  gchar *timestr;  time_t current_time;  current_time = time(NULL);  timestr = asctime (localtime(&#38;current_time));   /* When we return a single string, it should not be null terminated.     That will be done for us */  gtk_selection_data_set (selection_data, GDK_SELECTION_TYPE_STRING,			  8, timestr, strlen(timestr));}int main( int   argc,          char *argv[] ){  GtkWidget *window;  GtkWidget *selection_button;  static int have_selection = FALSE;    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 toggle button to act as the selection */  selection_button = gtk_toggle_button_new_with_label ("Claim Selection");  gtk_container_add (GTK_CONTAINER (window), selection_button);  gtk_widget_show (selection_button);  gtk_signal_connect (GTK_OBJECT(selection_button), "toggled",		      GTK_SIGNAL_FUNC (selection_toggled), &#38;have_selection);  gtk_signal_connect (GTK_OBJECT(selection_button), "selection_clear_event",		      GTK_SIGNAL_FUNC (selection_clear), &#38;have_selection);  gtk_selection_add_target (selection_button,			    GDK_SELECTION_PRIMARY,			    GDK_SELECTION_TYPE_STRING,		            1);  gtk_signal_connect (GTK_OBJECT(selection_button), "selection_get",		      GTK_SIGNAL_FUNC (selection_handle), &#38;have_selection);  gtk_widget_show (selection_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="sec-retrievingtheselection.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="ch-dragangdrop.html">Next &#62;&#62;&#62;</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Retrieving the selection</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-managingselections.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Drag-and-drop (DND)</TD></TR></TABLE></DIV>        </td>    </tr></table>  </td>  </tr></table></body></BODY></HTML>

⌨️ 快捷键说明

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