1998-03-31 23:43:49 +00:00
|
|
|
|
2002-02-16 23:52:30 +00:00
|
|
|
#include <stdlib.h>
|
1998-03-31 23:43:49 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <time.h>
|
2002-08-25 11:28:42 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
GtkWidget *selection_button;
|
|
|
|
GtkWidget *selection_widget;
|
1998-03-31 23:43:49 +00:00
|
|
|
|
|
|
|
/* Callback when the user toggles the selection */
|
2005-01-03 19:26:36 +00:00
|
|
|
static void selection_toggled( GtkWidget *widget,
|
|
|
|
gint *have_selection )
|
1998-03-31 23:43:49 +00:00
|
|
|
{
|
2002-02-19 01:25:26 +00:00
|
|
|
if (GTK_TOGGLE_BUTTON (widget)->active)
|
1998-03-31 23:43:49 +00:00
|
|
|
{
|
2002-08-25 11:28:42 +00:00
|
|
|
*have_selection = gtk_selection_owner_set (selection_widget,
|
1998-03-31 23:43:49 +00:00
|
|
|
GDK_SELECTION_PRIMARY,
|
|
|
|
GDK_CURRENT_TIME);
|
|
|
|
/* if claiming the selection failed, we return the button to
|
|
|
|
the out state */
|
|
|
|
if (!*have_selection)
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), FALSE);
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|
|
|
|
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->window)
|
|
|
|
gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY,
|
|
|
|
GDK_CURRENT_TIME);
|
|
|
|
*have_selection = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called when another application claims the selection */
|
2005-01-03 19:26:36 +00:00
|
|
|
static gboolean selection_clear( GtkWidget *widget,
|
|
|
|
GdkEventSelection *event,
|
|
|
|
gint *have_selection )
|
1998-03-31 23:43:49 +00:00
|
|
|
{
|
|
|
|
*have_selection = FALSE;
|
2002-08-25 11:28:42 +00:00
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (selection_button), FALSE);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Supplies the current time as the selection. */
|
2005-01-03 19:26:36 +00:00
|
|
|
static void selection_handle( GtkWidget *widget,
|
|
|
|
GtkSelectionData *selection_data,
|
|
|
|
guint info,
|
|
|
|
guint time_stamp,
|
|
|
|
gpointer data )
|
1998-03-31 23:43:49 +00:00
|
|
|
{
|
|
|
|
gchar *timestr;
|
|
|
|
time_t current_time;
|
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
current_time = time (NULL);
|
|
|
|
timestr = asctime (localtime (¤t_time));
|
1998-03-31 23:43:49 +00:00
|
|
|
/* 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,
|
2002-02-19 01:25:26 +00:00
|
|
|
8, timestr, strlen (timestr));
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
int main( int argc,
|
|
|
|
char *argv[] )
|
1998-03-31 23:43:49 +00:00
|
|
|
{
|
|
|
|
GtkWidget *window;
|
|
|
|
|
|
|
|
static int have_selection = FALSE;
|
|
|
|
|
|
|
|
gtk_init (&argc, &argv);
|
|
|
|
|
|
|
|
/* Create the toplevel window */
|
|
|
|
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Event Box");
|
1999-01-28 10:35:40 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect (G_OBJECT (window), "destroy",
|
|
|
|
G_CALLBACK (exit), NULL);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
|
|
|
/* Create a toggle button to act as the selection */
|
|
|
|
|
2002-08-25 11:28:42 +00:00
|
|
|
selection_widget = gtk_invisible_new ();
|
1998-03-31 23:43:49 +00:00
|
|
|
selection_button = gtk_toggle_button_new_with_label ("Claim Selection");
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), selection_button);
|
|
|
|
gtk_widget_show (selection_button);
|
|
|
|
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect (G_OBJECT (selection_button), "toggled",
|
2002-08-25 11:28:42 +00:00
|
|
|
G_CALLBACK (selection_toggled), (gpointer) &have_selection);
|
|
|
|
g_signal_connect (G_OBJECT (selection_widget), "selection_clear_event",
|
|
|
|
G_CALLBACK (selection_clear), (gpointer) &have_selection);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
2002-08-25 11:28:42 +00:00
|
|
|
gtk_selection_add_target (selection_widget,
|
1999-11-13 23:06:46 +00:00
|
|
|
GDK_SELECTION_PRIMARY,
|
|
|
|
GDK_SELECTION_TYPE_STRING,
|
|
|
|
1);
|
2002-08-25 11:28:42 +00:00
|
|
|
g_signal_connect (G_OBJECT (selection_widget), "selection_get",
|
|
|
|
G_CALLBACK (selection_handle), (gpointer) &have_selection);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
|
|
|
gtk_widget_show (selection_button);
|
|
|
|
gtk_widget_show (window);
|
|
|
|
|
|
|
|
gtk_main ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|