Blame docs/tutorial/html/x1873.html

Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>Supplying the selection
Packit 98cdb6
>
Packit 98cdb6
NAME="GENERATOR"
Packit 98cdb6
CONTENT="Modular DocBook HTML Stylesheet Version 1.79">
Packit 98cdb6
REL="HOME"
Packit 98cdb6
TITLE="GTK+ 2.0 Tutorial"
Packit 98cdb6
HREF="book1.html">
Packit 98cdb6
REL="UP"
Packit 98cdb6
TITLE="Managing Selections"
Packit 98cdb6
HREF="c1834.html">
Packit 98cdb6
REL="PREVIOUS"
Packit 98cdb6
TITLE="Retrieving the selection"
Packit 98cdb6
HREF="x1850.html">
Packit 98cdb6
REL="NEXT"
Packit 98cdb6
TITLE="Drag-and-drop (DND)"
Packit 98cdb6
HREF="c1899.html">
Packit 98cdb6
>
Packit 98cdb6
CLASS="SECT1"
Packit 98cdb6
BGCOLOR="#FFFFFF"
Packit 98cdb6
TEXT="#000000"
Packit 98cdb6
LINK="#0000FF"
Packit 98cdb6
VLINK="#840084"
Packit 98cdb6
ALINK="#0000FF"
Packit 98cdb6
>
Packit 98cdb6
CLASS="NAVHEADER"
Packit 98cdb6
>
Packit 98cdb6
SUMMARY="Header navigation table"
Packit 98cdb6
WIDTH="100%"
Packit 98cdb6
BORDER="0"
Packit 98cdb6
CELLPADDING="0"
Packit 98cdb6
CELLSPACING="0"
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
COLSPAN="3"
Packit 98cdb6
ALIGN="center"
Packit 98cdb6
>GTK+ 2.0 Tutorial
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
WIDTH="10%"
Packit 98cdb6
ALIGN="left"
Packit 98cdb6
VALIGN="bottom"
Packit 98cdb6
>
Packit 98cdb6
HREF="x1850.html"
Packit 98cdb6
ACCESSKEY="P"
Packit 98cdb6
><<< Previous
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
WIDTH="80%"
Packit 98cdb6
ALIGN="center"
Packit 98cdb6
VALIGN="bottom"
Packit 98cdb6
>Managing Selections
Packit 98cdb6
>
Packit 98cdb6
WIDTH="10%"
Packit 98cdb6
ALIGN="right"
Packit 98cdb6
VALIGN="bottom"
Packit 98cdb6
>
Packit 98cdb6
HREF="c1899.html"
Packit 98cdb6
ACCESSKEY="N"
Packit 98cdb6
>Next >>>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
ALIGN="LEFT"
Packit 98cdb6
WIDTH="100%">
Packit 98cdb6
>
Packit 98cdb6
CLASS="SECT1"
Packit 98cdb6
>
Packit 98cdb6
CLASS="SECT1"
Packit 98cdb6
>
Packit 98cdb6
NAME="SEC-SUPPLYINGTHESELECTION"
Packit 98cdb6
>Supplying the selection
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>Supplying the selection is a bit more complicated. You must register 
Packit 98cdb6
handlers that will be called when your selection is requested. For
Packit 98cdb6
each selection/target pair you will handle, you make a call to:
Packit 98cdb6
>
Packit 98cdb6
BORDER="0"
Packit 98cdb6
BGCOLOR="#E0E0E0"
Packit 98cdb6
WIDTH="100%"
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
CLASS="PROGRAMLISTING"
Packit 98cdb6
>void gtk_selection_add_target( GtkWidget           *widget, 
Packit 98cdb6
                               GdkAtom              selection,
Packit 98cdb6
                               GdkAtom              target,
Packit 98cdb6
                               guint                info );
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
CLASS="LITERAL"
Packit 98cdb6
>widget
Packit 98cdb6
>, 
Packit 98cdb6
CLASS="LITERAL"
Packit 98cdb6
>selection
Packit 98cdb6
>, and 
Packit 98cdb6
CLASS="LITERAL"
Packit 98cdb6
>target
Packit 98cdb6
> identify the requests
Packit 98cdb6
this handler will manage. When a request for a selection is received,
Packit 98cdb6
the "selection_get" signal will be called. 
Packit 98cdb6
CLASS="LITERAL"
Packit 98cdb6
>info
Packit 98cdb6
> can be used as an
Packit 98cdb6
enumerator to identify the specific target within the callback function.
Packit 98cdb6
>
Packit 98cdb6
>The callback function has the signature:
Packit 98cdb6
>
Packit 98cdb6
BORDER="0"
Packit 98cdb6
BGCOLOR="#E0E0E0"
Packit 98cdb6
WIDTH="100%"
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
CLASS="PROGRAMLISTING"
Packit 98cdb6
>void  "selection_get"( GtkWidget          *widget,
Packit 98cdb6
                       GtkSelectionData   *selection_data,
Packit 98cdb6
                       guint               info,
Packit 98cdb6
                       guint               time );
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>The GtkSelectionData is the same as above, but this time, we're
Packit 98cdb6
responsible for filling in the fields 
Packit 98cdb6
CLASS="LITERAL"
Packit 98cdb6
>type
Packit 98cdb6
>, 
Packit 98cdb6
CLASS="LITERAL"
Packit 98cdb6
>format
Packit 98cdb6
>,
Packit 98cdb6
Packit 98cdb6
CLASS="LITERAL"
Packit 98cdb6
>data
Packit 98cdb6
>, and 
Packit 98cdb6
CLASS="LITERAL"
Packit 98cdb6
>length
Packit 98cdb6
>. (The 
Packit 98cdb6
CLASS="LITERAL"
Packit 98cdb6
>format
Packit 98cdb6
> field is actually
Packit 98cdb6
important here - the X server uses it to figure out whether the data
Packit 98cdb6
needs to be byte-swapped or not. Usually it will be 8 - 
Packit 98cdb6
CLASS="EMPHASIS"
Packit 98cdb6
>i.e.
Packit 98cdb6
> a
Packit 98cdb6
character - or 32 - 
Packit 98cdb6
CLASS="EMPHASIS"
Packit 98cdb6
>i.e.
Packit 98cdb6
> an integer.) This is done by calling the
Packit 98cdb6
function:
Packit 98cdb6
>
Packit 98cdb6
BORDER="0"
Packit 98cdb6
BGCOLOR="#E0E0E0"
Packit 98cdb6
WIDTH="100%"
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
CLASS="PROGRAMLISTING"
Packit 98cdb6
>void gtk_selection_data_set( GtkSelectionData *selection_data,
Packit 98cdb6
                             GdkAtom           type,
Packit 98cdb6
                             gint              format,
Packit 98cdb6
                             guchar           *data,
Packit 98cdb6
                             gint              length );
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>This function takes care of properly making a copy of the data so that
Packit 98cdb6
you don't have to worry about keeping it around. (You should not fill
Packit 98cdb6
in the fields of the GtkSelectionData structure by hand.)
Packit 98cdb6
>
Packit 98cdb6
>When prompted by the user, you claim ownership of the selection by
Packit 98cdb6
calling:
Packit 98cdb6
>
Packit 98cdb6
BORDER="0"
Packit 98cdb6
BGCOLOR="#E0E0E0"
Packit 98cdb6
WIDTH="100%"
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
CLASS="PROGRAMLISTING"
Packit 98cdb6
>gboolean gtk_selection_owner_set( GtkWidget *widget,
Packit 98cdb6
                                  GdkAtom    selection,
Packit 98cdb6
                                  guint32    time );
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>If another application claims ownership of the selection, you will
Packit 98cdb6
receive a "selection_clear_event".
Packit 98cdb6
>
Packit 98cdb6
>As an example of supplying the selection, the following program adds
Packit 98cdb6
selection functionality to a toggle button. When the toggle button is
Packit 98cdb6
depressed, the program claims the primary selection. The only target
Packit 98cdb6
supported (aside from certain targets like "TARGETS" supplied by GTK
Packit 98cdb6
itself), is the "STRING" target. When this target is requested, a
Packit 98cdb6
string representation of the time is returned.
Packit 98cdb6
>
Packit 98cdb6
BORDER="0"
Packit 98cdb6
BGCOLOR="#E0E0E0"
Packit 98cdb6
WIDTH="100%"
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
CLASS="PROGRAMLISTING"
Packit 98cdb6
>
#include <stdlib.h>
Packit 98cdb6
#include <gtk/gtk.h>
Packit 98cdb6
#include <time.h>
Packit 98cdb6
#include <string.h>
Packit 98cdb6
Packit 98cdb6
GtkWidget *selection_button;
Packit 98cdb6
GtkWidget *selection_widget;
Packit 98cdb6
Packit 98cdb6
/* Callback when the user toggles the selection */
Packit 98cdb6
static void selection_toggled( GtkWidget *widget,
Packit 98cdb6
                               gint      *have_selection )
Packit 98cdb6
{
Packit 98cdb6
  if (GTK_TOGGLE_BUTTON (widget)->active)
Packit 98cdb6
    {
Packit 98cdb6
      *have_selection = gtk_selection_owner_set (selection_widget,
Packit 98cdb6
						 GDK_SELECTION_PRIMARY,
Packit 98cdb6
						 GDK_CURRENT_TIME);
Packit 98cdb6
      /* if claiming the selection failed, we return the button to
Packit 98cdb6
	 the out state */
Packit 98cdb6
      if (!*have_selection)
Packit 98cdb6
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), FALSE);
Packit 98cdb6
    }
Packit 98cdb6
  else
Packit 98cdb6
    {
Packit 98cdb6
      if (*have_selection)
Packit 98cdb6
	{
Packit 98cdb6
	  /* Before clearing the selection by setting the owner to NULL,
Packit 98cdb6
	     we check if we are the actual owner */
Packit 98cdb6
	  if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
Packit 98cdb6
	    gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY,
Packit 98cdb6
				     GDK_CURRENT_TIME);
Packit 98cdb6
	  *have_selection = FALSE;
Packit 98cdb6
	}
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/* Called when another application claims the selection */
Packit 98cdb6
static gboolean selection_clear( GtkWidget         *widget,
Packit 98cdb6
                                 GdkEventSelection *event,
Packit 98cdb6
                                 gint              *have_selection )
Packit 98cdb6
{
Packit 98cdb6
  *have_selection = FALSE;
Packit 98cdb6
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (selection_button), FALSE);
Packit 98cdb6
Packit 98cdb6
  return TRUE;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/* Supplies the current time as the selection. */
Packit 98cdb6
static void selection_handle( GtkWidget        *widget, 
Packit 98cdb6
                              GtkSelectionData *selection_data,
Packit 98cdb6
                              guint             info,
Packit 98cdb6
                              guint             time_stamp,
Packit 98cdb6
                              gpointer          data )
Packit 98cdb6
{
Packit 98cdb6
  gchar *timestr;
Packit 98cdb6
  time_t current_time;
Packit 98cdb6
Packit 98cdb6
  current_time = time (NULL);
Packit 98cdb6
  timestr = asctime (localtime (&current_time)); 
Packit 98cdb6
  /* When we return a single string, it should not be null terminated.
Packit 98cdb6
     That will be done for us */
Packit 98cdb6
Packit 98cdb6
  gtk_selection_data_set (selection_data, GDK_SELECTION_TYPE_STRING,
Packit 98cdb6
			  8, timestr, strlen (timestr));
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
int main( int   argc,
Packit 98cdb6
          char *argv[] )
Packit 98cdb6
{
Packit 98cdb6
  GtkWidget *window;
Packit 98cdb6
Packit 98cdb6
  static int have_selection = FALSE;
Packit 98cdb6
  
Packit 98cdb6
  gtk_init (&argc, &argv);
Packit 98cdb6
Packit 98cdb6
  /* Create the toplevel window */
Packit 98cdb6
Packit 98cdb6
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
Packit 98cdb6
  gtk_window_set_title (GTK_WINDOW (window), "Event Box");
Packit 98cdb6
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
Packit 98cdb6
Packit 98cdb6
  g_signal_connect (window, "destroy",
Packit 98cdb6
		    G_CALLBACK (exit), NULL);
Packit 98cdb6
Packit 98cdb6
  /* Create a toggle button to act as the selection */
Packit 98cdb6
Packit 98cdb6
  selection_widget = gtk_invisible_new ();
Packit 98cdb6
  selection_button = gtk_toggle_button_new_with_label ("Claim Selection");
Packit 98cdb6
  gtk_container_add (GTK_CONTAINER (window), selection_button);
Packit 98cdb6
  gtk_widget_show (selection_button);
Packit 98cdb6
Packit 98cdb6
  g_signal_connect (selection_button, "toggled",
Packit 98cdb6
		    G_CALLBACK (selection_toggled), (gpointer) &have_selection);
Packit 98cdb6
  g_signal_connect (selection_widget, "selection_clear_event",
Packit 98cdb6
		    G_CALLBACK (selection_clear), (gpointer) &have_selection);
Packit 98cdb6
Packit 98cdb6
  gtk_selection_add_target (selection_widget,
Packit 98cdb6
			    GDK_SELECTION_PRIMARY,
Packit 98cdb6
			    GDK_SELECTION_TYPE_STRING,
Packit 98cdb6
		            1);
Packit 98cdb6
  g_signal_connect (selection_widget, "selection_get",
Packit 98cdb6
		    G_CALLBACK (selection_handle), (gpointer) &have_selection);
Packit 98cdb6
Packit 98cdb6
  gtk_widget_show (selection_button);
Packit 98cdb6
  gtk_widget_show (window);
Packit 98cdb6
  
Packit 98cdb6
  gtk_main ();
Packit 98cdb6
  
Packit 98cdb6
  return 0;
Packit 98cdb6
}
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
CLASS="NAVFOOTER"
Packit 98cdb6
>
Packit 98cdb6
ALIGN="LEFT"
Packit 98cdb6
WIDTH="100%">
Packit 98cdb6
SUMMARY="Footer navigation table"
Packit 98cdb6
WIDTH="100%"
Packit 98cdb6
BORDER="0"
Packit 98cdb6
CELLPADDING="0"
Packit 98cdb6
CELLSPACING="0"
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
WIDTH="33%"
Packit 98cdb6
ALIGN="left"
Packit 98cdb6
VALIGN="top"
Packit 98cdb6
>
Packit 98cdb6
HREF="x1850.html"
Packit 98cdb6
ACCESSKEY="P"
Packit 98cdb6
><<< Previous
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
WIDTH="34%"
Packit 98cdb6
ALIGN="center"
Packit 98cdb6
VALIGN="top"
Packit 98cdb6
>
Packit 98cdb6
HREF="book1.html"
Packit 98cdb6
ACCESSKEY="H"
Packit 98cdb6
>Home
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
WIDTH="33%"
Packit 98cdb6
ALIGN="right"
Packit 98cdb6
VALIGN="top"
Packit 98cdb6
>
Packit 98cdb6
HREF="c1899.html"
Packit 98cdb6
ACCESSKEY="N"
Packit 98cdb6
>Next >>>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
WIDTH="33%"
Packit 98cdb6
ALIGN="left"
Packit 98cdb6
VALIGN="top"
Packit 98cdb6
>Retrieving the selection
Packit 98cdb6
>
Packit 98cdb6
WIDTH="34%"
Packit 98cdb6
ALIGN="center"
Packit 98cdb6
VALIGN="top"
Packit 98cdb6
>
Packit 98cdb6
HREF="c1834.html"
Packit 98cdb6
ACCESSKEY="U"
Packit 98cdb6
>Up
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
WIDTH="33%"
Packit 98cdb6
ALIGN="right"
Packit 98cdb6
VALIGN="top"
Packit 98cdb6
>Drag-and-drop (DND)
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>
Packit 98cdb6
>