Blame examples/selection/setselection.c

Packit Service fb6fa5
Packit Service fb6fa5
#include <stdlib.h>
Packit Service fb6fa5
#include <gtk/gtk.h>
Packit Service fb6fa5
#include <time.h>
Packit Service fb6fa5
#include <string.h>
Packit Service fb6fa5
Packit Service fb6fa5
GtkWidget *selection_button;
Packit Service fb6fa5
GtkWidget *selection_widget;
Packit Service fb6fa5
Packit Service fb6fa5
/* Callback when the user toggles the selection */
Packit Service fb6fa5
static void selection_toggled( GtkWidget *widget,
Packit Service fb6fa5
                               gint      *have_selection )
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (GTK_TOGGLE_BUTTON (widget)->active)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      *have_selection = gtk_selection_owner_set (selection_widget,
Packit Service fb6fa5
						 GDK_SELECTION_PRIMARY,
Packit Service fb6fa5
						 GDK_CURRENT_TIME);
Packit Service fb6fa5
      /* if claiming the selection failed, we return the button to
Packit Service fb6fa5
	 the out state */
Packit Service fb6fa5
      if (!*have_selection)
Packit Service fb6fa5
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), FALSE);
Packit Service fb6fa5
    }
Packit Service fb6fa5
  else
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (*have_selection)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  /* Before clearing the selection by setting the owner to NULL,
Packit Service fb6fa5
	     we check if we are the actual owner */
Packit Service fb6fa5
	  if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
Packit Service fb6fa5
	    gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY,
Packit Service fb6fa5
				     GDK_CURRENT_TIME);
Packit Service fb6fa5
	  *have_selection = FALSE;
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Called when another application claims the selection */
Packit Service fb6fa5
static gboolean selection_clear( GtkWidget         *widget,
Packit Service fb6fa5
                                 GdkEventSelection *event,
Packit Service fb6fa5
                                 gint              *have_selection )
Packit Service fb6fa5
{
Packit Service fb6fa5
  *have_selection = FALSE;
Packit Service fb6fa5
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (selection_button), FALSE);
Packit Service fb6fa5
Packit Service fb6fa5
  return TRUE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Supplies the current time as the selection. */
Packit Service fb6fa5
static void selection_handle( GtkWidget        *widget,
Packit Service fb6fa5
                              GtkSelectionData *selection_data,
Packit Service fb6fa5
                              guint             info,
Packit Service fb6fa5
                              guint             time_stamp,
Packit Service fb6fa5
                              gpointer          data )
Packit Service fb6fa5
{
Packit Service fb6fa5
  gchar *timestr;
Packit Service fb6fa5
  time_t current_time;
Packit Service fb6fa5
Packit Service fb6fa5
  current_time = time (NULL);
Packit Service fb6fa5
  timestr = asctime (localtime (&current_time));
Packit Service fb6fa5
  /* When we return a single string, it should not be null terminated.
Packit Service fb6fa5
     That will be done for us */
Packit Service fb6fa5
Packit Service fb6fa5
  gtk_selection_data_set (selection_data, GDK_SELECTION_TYPE_STRING,
Packit Service fb6fa5
			  8, timestr, strlen (timestr));
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
int main( int   argc,
Packit Service fb6fa5
          char *argv[] )
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkWidget *window;
Packit Service fb6fa5
Packit Service fb6fa5
  static int have_selection = FALSE;
Packit Service fb6fa5
Packit Service fb6fa5
  gtk_init (&argc, &argv);
Packit Service fb6fa5
Packit Service fb6fa5
  /* Create the toplevel window */
Packit Service fb6fa5
Packit Service fb6fa5
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
Packit Service fb6fa5
  gtk_window_set_title (GTK_WINDOW (window), "Event Box");
Packit Service fb6fa5
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
Packit Service fb6fa5
Packit Service fb6fa5
  g_signal_connect (window, "destroy",
Packit Service fb6fa5
		    G_CALLBACK (exit), NULL);
Packit Service fb6fa5
Packit Service fb6fa5
  /* Create a toggle button to act as the selection */
Packit Service fb6fa5
Packit Service fb6fa5
  selection_widget = gtk_invisible_new ();
Packit Service fb6fa5
  selection_button = gtk_toggle_button_new_with_label ("Claim Selection");
Packit Service fb6fa5
  gtk_container_add (GTK_CONTAINER (window), selection_button);
Packit Service fb6fa5
  gtk_widget_show (selection_button);
Packit Service fb6fa5
Packit Service fb6fa5
  g_signal_connect (selection_button, "toggled",
Packit Service fb6fa5
		    G_CALLBACK (selection_toggled), &have_selection);
Packit Service fb6fa5
  g_signal_connect (selection_widget, "selection-clear-event",
Packit Service fb6fa5
		    G_CALLBACK (selection_clear), &have_selection);
Packit Service fb6fa5
Packit Service fb6fa5
  gtk_selection_add_target (selection_widget,
Packit Service fb6fa5
			    GDK_SELECTION_PRIMARY,
Packit Service fb6fa5
			    GDK_SELECTION_TYPE_STRING,
Packit Service fb6fa5
		            1);
Packit Service fb6fa5
  g_signal_connect (selection_widget, "selection-get",
Packit Service fb6fa5
		    G_CALLBACK (selection_handle), &have_selection);
Packit Service fb6fa5
Packit Service fb6fa5
  gtk_widget_show (selection_button);
Packit Service fb6fa5
  gtk_widget_show (window);
Packit Service fb6fa5
Packit Service fb6fa5
  gtk_main ();
Packit Service fb6fa5
Packit Service fb6fa5
  return 0;
Packit Service fb6fa5
}