testselection.c 12.2 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16 17
 */

18
/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

25

26
#include "config.h"
27
#include <stdio.h>
28
#include <string.h>
Owen Taylor's avatar
Owen Taylor committed
29
#include "gtk/gtk.h"
Elliot Lee's avatar
Elliot Lee committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

typedef enum {
  SEL_TYPE_NONE,
  APPLE_PICT,
  ATOM,
  ATOM_PAIR,
  BITMAP,
  C_STRING,
  COLORMAP,
  COMPOUND_TEXT,
  DRAWABLE,
  INTEGER,
  PIXEL,
  PIXMAP,
  SPAN,
  STRING,
  TEXT,
  WINDOW,
48
  LAST_SEL_TYPE
Elliot Lee's avatar
Elliot Lee committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
} SelType;

GdkAtom seltypes[LAST_SEL_TYPE];

typedef struct _Target {
  gchar *target_name;
  SelType type;
  GdkAtom target;
  gint format;
} Target;

/* The following is a list of all the selection targets defined
   in the ICCCM */

static Target targets[] = {
Matthias Clasen's avatar
Matthias Clasen committed
64 65 66 67 68 69 70 71 72 73
  { "ADOBE_PORTABLE_DOCUMENT_FORMAT",	    STRING, 	   NULL, 8 },
  { "APPLE_PICT", 			    APPLE_PICT,    NULL, 8 },
  { "BACKGROUND",			    PIXEL,         NULL, 32 },
  { "BITMAP", 				    BITMAP,        NULL, 32 },
  { "CHARACTER_POSITION",                   SPAN, 	   NULL, 32 },
  { "CLASS", 				    TEXT, 	   NULL, 8 },
  { "CLIENT_WINDOW", 			    WINDOW, 	   NULL, 32 },
  { "COLORMAP", 			    COLORMAP,      NULL, 32 },
  { "COLUMN_NUMBER", 			    SPAN, 	   NULL, 32 },
  { "COMPOUND_TEXT", 			    COMPOUND_TEXT, NULL, 8 },
74
  /*  { "DELETE", "NULL", 0, ? }, */
Matthias Clasen's avatar
Matthias Clasen committed
75 76 77 78 79 80
  { "DRAWABLE", 			    DRAWABLE,      NULL, 32 },
  { "ENCAPSULATED_POSTSCRIPT", 		    STRING, 	   NULL, 8 },
  { "ENCAPSULATED_POSTSCRIPT_INTERCHANGE",  STRING, 	   NULL, 8 },
  { "FILE_NAME", 			    TEXT, 	   NULL, 8 },
  { "FOREGROUND", 			    PIXEL, 	   NULL, 32 },
  { "HOST_NAME", 			    TEXT, 	   NULL, 8 },
Elliot Lee's avatar
Elliot Lee committed
81 82
  /*  { "INSERT_PROPERTY", "NULL", 0, ? NULL }, */
  /*  { "INSERT_SELECTION", "NULL", 0, ? NULL }, */
Matthias Clasen's avatar
Matthias Clasen committed
83 84 85 86
  { "LENGTH", 				    INTEGER, 	   NULL, 32 },
  { "LINE_NUMBER", 			    SPAN, 	   NULL, 32 },
  { "LIST_LENGTH", 			    INTEGER,       NULL, 32 },
  { "MODULE", 				    TEXT, 	   NULL, 8 },
87
  /*  { "MULTIPLE", "ATOM_PAIR", 0, 32 }, */
Matthias Clasen's avatar
Matthias Clasen committed
88 89 90 91 92 93 94 95 96 97 98 99 100
  { "NAME", 				    TEXT, 	   NULL, 8 },
  { "ODIF", 				    TEXT,          NULL, 8 },
  { "OWNER_OS", 			    TEXT, 	   NULL, 8 },
  { "PIXMAP", 				    PIXMAP,        NULL, 32 },
  { "POSTSCRIPT", 			    STRING,        NULL, 8 },
  { "PROCEDURE", 			    TEXT,          NULL, 8 },
  { "PROCESS",				    INTEGER,       NULL, 32 },
  { "STRING", 				    STRING,        NULL, 8 },
  { "TARGETS", 				    ATOM, 	   NULL, 32 },
  { "TASK", 				    INTEGER,       NULL, 32 },
  { "TEXT", 				    TEXT,          NULL, 8  },
  { "TIMESTAMP", 			    INTEGER,       NULL, 32 },
  { "USER", 				    TEXT, 	   NULL, 8 },
Elliot Lee's avatar
Elliot Lee committed
101 102 103 104 105 106
};

static int num_targets = sizeof(targets)/sizeof(Target);

static int have_selection = FALSE;

107
GtkWidget *selection_widget;
Elliot Lee's avatar
Elliot Lee committed
108 109 110 111 112
GtkWidget *selection_text;
GtkWidget *selection_button;
GString *selection_string = NULL;

static void
113
init_atoms (void)
Elliot Lee's avatar
Elliot Lee committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
  int i;

  seltypes[SEL_TYPE_NONE] = GDK_NONE;
  seltypes[APPLE_PICT] = gdk_atom_intern ("APPLE_PICT",FALSE);
  seltypes[ATOM]       = gdk_atom_intern ("ATOM",FALSE);
  seltypes[ATOM_PAIR]  = gdk_atom_intern ("ATOM_PAIR",FALSE);
  seltypes[BITMAP]     = gdk_atom_intern ("BITMAP",FALSE);
  seltypes[C_STRING]   = gdk_atom_intern ("C_STRING",FALSE);
  seltypes[COLORMAP]   = gdk_atom_intern ("COLORMAP",FALSE);
  seltypes[COMPOUND_TEXT] = gdk_atom_intern ("COMPOUND_TEXT",FALSE);
  seltypes[DRAWABLE]   = gdk_atom_intern ("DRAWABLE",FALSE);
  seltypes[INTEGER]    = gdk_atom_intern ("INTEGER",FALSE);
  seltypes[PIXEL]      = gdk_atom_intern ("PIXEL",FALSE);
  seltypes[PIXMAP]     = gdk_atom_intern ("PIXMAP",FALSE);
  seltypes[SPAN]       = gdk_atom_intern ("SPAN",FALSE);
  seltypes[STRING]     = gdk_atom_intern ("STRING",FALSE);
  seltypes[TEXT]       = gdk_atom_intern ("TEXT",FALSE);
  seltypes[WINDOW]     = gdk_atom_intern ("WINDOW",FALSE);

  for (i=0; i<num_targets; i++)
    targets[i].target = gdk_atom_intern (targets[i].target_name, FALSE);
}

void
selection_toggled (GtkWidget *widget)
{
141
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
Elliot Lee's avatar
Elliot Lee committed
142
    {
143
      have_selection = gtk_selection_owner_set (selection_widget,
Elliot Lee's avatar
Elliot Lee committed
144 145 146
						GDK_SELECTION_PRIMARY,
						GDK_CURRENT_TIME);
      if (!have_selection)
147
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(widget), FALSE);
Elliot Lee's avatar
Elliot Lee committed
148 149 150 151 152
    }
  else
    {
      if (have_selection)
	{
153
          if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == gtk_widget_get_window (widget))
Elliot Lee's avatar
Elliot Lee committed
154 155 156 157 158 159 160 161
	    gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY,
				     GDK_CURRENT_TIME);
	  have_selection = FALSE;
	}
    }
}

void
162 163 164 165 166
selection_get (GtkWidget *widget, 
	       GtkSelectionData *selection_data,
	       guint      info,
	       guint      time,
	       gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
167 168 169
{
  guchar *buffer;
  gint len;
170
  GdkAtom type = GDK_NONE;
Elliot Lee's avatar
Elliot Lee committed
171 172 173 174 175 176 177 178

  if (!selection_string)
    {
      buffer = NULL;
      len = 0;
    }      
  else
    {
179
      buffer = (guchar *)selection_string->str;
Elliot Lee's avatar
Elliot Lee committed
180 181
      len = selection_string->len;
    }
182 183 184 185 186 187 188 189 190

  switch (info)
    {
    case COMPOUND_TEXT:
    case TEXT:
      type = seltypes[COMPOUND_TEXT];
    case STRING:
      type = seltypes[STRING];
    }
Elliot Lee's avatar
Elliot Lee committed
191
  
192
  gtk_selection_data_set (selection_data, type, 8, buffer, len);
Elliot Lee's avatar
Elliot Lee committed
193 194 195 196 197 198
}

gint
selection_clear (GtkWidget *widget, GdkEventSelection *event)
{
  have_selection = FALSE;
199
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(selection_button), FALSE);
Elliot Lee's avatar
Elliot Lee committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

  return TRUE;
}

gchar *
stringify_atom (guchar *data, gint *position)
{
  gchar *str = gdk_atom_name (*(GdkAtom *)(data+*position));
  *position += sizeof(GdkAtom);
    
  return str;
}

gchar *
stringify_text (guchar *data, gint *position)
{
  gchar *str = g_strdup ((gchar *)(data+*position));
  *position += strlen (str) + 1;
    
  return str;
}

gchar *
stringify_xid (guchar *data, gint *position)
{
  gchar buffer[20];
  gchar *str;

  sprintf(buffer,"0x%x",*(guint32 *)(data+*position));
  str = g_strdup (buffer);

  *position += sizeof(guint32);
    
  return str;
}

gchar *
stringify_integer (guchar *data, gint *position)
{
  gchar buffer[20];
  gchar *str;

  sprintf(buffer,"%d",*(int *)(data+*position));
  str = g_strdup (buffer);

  *position += sizeof(int);
    
  return str;
}

gchar *
stringify_span (guchar *data, gint *position)
{
  gchar buffer[42];
  gchar *str;

  sprintf(buffer,"%d - %d",((int *)(data+*position))[0],
	  ((int *)(data+*position))[1]);
  str = g_strdup (buffer);

  *position += 2*sizeof(int);
    
  return str;
}

void
266
selection_received (GtkWidget *widget, GtkSelectionData *selection_data)
Elliot Lee's avatar
Elliot Lee committed
267 268 269 270 271
{
  int position;
  int i;
  SelType seltype;
  char *str;
272
  guchar *data;
273
  GtkTextBuffer *buffer;
274 275 276
  GdkAtom type;

  if (gtk_selection_data_get_length (selection_data) < 0)
Elliot Lee's avatar
Elliot Lee committed
277 278 279 280 281
    {
      g_print("Error retrieving selection\n");
      return;
    }

282 283
  type = gtk_selection_data_get_data_type (selection_data);

Elliot Lee's avatar
Elliot Lee committed
284 285 286
  seltype = SEL_TYPE_NONE;
  for (i=0; i<LAST_SEL_TYPE; i++)
    {
287
      if (seltypes[i] == type)
Elliot Lee's avatar
Elliot Lee committed
288 289 290 291 292 293 294 295
	{
	  seltype = i;
	  break;
	}
    }

  if (seltype == SEL_TYPE_NONE)
    {
296
      char *name = gdk_atom_name (type);
297 298
      g_print("Don't know how to handle type: %s\n",
	      name?name:"<unknown>");
Elliot Lee's avatar
Elliot Lee committed
299 300 301 302 303 304 305 306
      return;
    }

  if (selection_string != NULL)
    g_string_free (selection_string, TRUE);

  selection_string = g_string_new (NULL);

307 308
  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (selection_text));
  gtk_text_buffer_set_text (buffer, "", -1);
Elliot Lee's avatar
Elliot Lee committed
309 310

  position = 0;
311
  while (position < gtk_selection_data_get_length (selection_data))
Elliot Lee's avatar
Elliot Lee committed
312
    {
313
      data = (guchar *) gtk_selection_data_get_data (selection_data);
Elliot Lee's avatar
Elliot Lee committed
314 315 316
      switch (seltype)
	{
	case ATOM:
317
	  str = stringify_atom (data, &position);
Elliot Lee's avatar
Elliot Lee committed
318 319 320 321
	  break;
	case COMPOUND_TEXT:
	case STRING:
	case TEXT:
322
	  str = stringify_text (data, &position);
Elliot Lee's avatar
Elliot Lee committed
323 324 325 326 327 328
	  break;
	case BITMAP:
	case DRAWABLE:
	case PIXMAP:
	case WINDOW:
	case COLORMAP:
329
	  str = stringify_xid (data, &position);
Elliot Lee's avatar
Elliot Lee committed
330 331 332
	  break;
	case INTEGER:
	case PIXEL:
333
	  str = stringify_integer (data, &position);
Elliot Lee's avatar
Elliot Lee committed
334 335
	  break;
	case SPAN:
336
	  str = stringify_span (data, &position);
Elliot Lee's avatar
Elliot Lee committed
337 338 339
	  break;
	default:
	  {
340
	    char *name = gdk_atom_name (gtk_selection_data_get_data_type (selection_data));
341 342
	    g_print("Can't convert type %s to string\n",
		    name?name:"<unknown>");
343
	    position = gtk_selection_data_get_length (selection_data);
344
	    continue;
Elliot Lee's avatar
Elliot Lee committed
345 346
	  }
	}
347 348
      gtk_text_buffer_insert_at_cursor (buffer, str, -1);
      gtk_text_buffer_insert_at_cursor (buffer, "\n", -1);
Elliot Lee's avatar
Elliot Lee committed
349 350 351 352 353 354
      g_string_append (selection_string, str);
      g_free (str);
    }
}

void
355
paste (GtkWidget *dialog, gint response, GtkWidget *entry)
Elliot Lee's avatar
Elliot Lee committed
356
{
357
  const char *name;
Elliot Lee's avatar
Elliot Lee committed
358 359
  GdkAtom atom;

360 361 362 363 364 365
  if (response != GTK_RESPONSE_APPLY)
    {
      gtk_widget_destroy (dialog);
      return;
    }

Elliot Lee's avatar
Elliot Lee committed
366 367 368 369 370 371 372 373 374
  name = gtk_entry_get_text (GTK_ENTRY(entry));
  atom = gdk_atom_intern (name, FALSE);

  if (atom == GDK_NONE)
    {
      g_print("Could not create atom: \"%s\"\n",name);
      return;
    }

375
  gtk_selection_convert (selection_widget, GDK_SELECTION_PRIMARY, atom, 
Elliot Lee's avatar
Elliot Lee committed
376 377 378 379
			 GDK_CURRENT_TIME);
}

void
380
quit (void)
Elliot Lee's avatar
Elliot Lee committed
381
{
382
  gtk_main_quit ();
Elliot Lee's avatar
Elliot Lee committed
383 384 385 386 387
}

int
main (int argc, char *argv[])
{
388
  GtkWidget *content_area;
Elliot Lee's avatar
Elliot Lee committed
389
  GtkWidget *dialog;
390
  GtkWidget *vbox;
Elliot Lee's avatar
Elliot Lee committed
391 392 393
  GtkWidget *label;
  GtkWidget *entry;
  GtkWidget *hbox;
394
  GtkWidget *scrolled;
395 396

  static GtkTargetEntry targetlist[] = {
397 398 399
    { "STRING",        0, STRING },
    { "TEXT",          0, TEXT },
    { "COMPOUND_TEXT", 0, COMPOUND_TEXT }
400 401
  };
  static gint ntargets = sizeof(targetlist) / sizeof(targetlist[0]);
Elliot Lee's avatar
Elliot Lee committed
402 403 404 405 406
  
  gtk_init (&argc, &argv);

  init_atoms();

407 408
  selection_widget = gtk_invisible_new ();

Elliot Lee's avatar
Elliot Lee committed
409 410
  dialog = gtk_dialog_new ();
  gtk_widget_set_name (dialog, "Test Input");
411
  gtk_container_set_border_width (GTK_CONTAINER(dialog), 0);
Elliot Lee's avatar
Elliot Lee committed
412

Manish Singh's avatar
Manish Singh committed
413 414
  g_signal_connect (dialog, "destroy",
		    G_CALLBACK (quit), NULL);
Elliot Lee's avatar
Elliot Lee committed
415

416 417
  content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));

418 419
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
Elliot Lee's avatar
Elliot Lee committed
420

421 422
  gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
  gtk_widget_show (vbox);
Elliot Lee's avatar
Elliot Lee committed
423 424
  
  selection_button = gtk_toggle_button_new_with_label ("Claim Selection");
425
  gtk_container_add (GTK_CONTAINER (vbox), selection_button);
Elliot Lee's avatar
Elliot Lee committed
426 427
  gtk_widget_show (selection_button);

Manish Singh's avatar
Manish Singh committed
428 429 430 431 432 433
  g_signal_connect (selection_button, "toggled",
		    G_CALLBACK (selection_toggled), NULL);
  g_signal_connect (selection_widget, "selection_clear_event",
		    G_CALLBACK (selection_clear), NULL);
  g_signal_connect (selection_widget, "selection_received",
		    G_CALLBACK (selection_received), NULL);
Elliot Lee's avatar
Elliot Lee committed
434

435
  gtk_selection_add_targets (selection_widget, GDK_SELECTION_PRIMARY,
436
			     targetlist, ntargets);
Elliot Lee's avatar
Elliot Lee committed
437

Manish Singh's avatar
Manish Singh committed
438 439
  g_signal_connect (selection_widget, "selection_get",
		    G_CALLBACK (selection_get), NULL);
Elliot Lee's avatar
Elliot Lee committed
440

441 442 443
  selection_text = gtk_text_view_new ();
  scrolled = gtk_scrolled_window_new (NULL, NULL);
  gtk_container_add (GTK_CONTAINER (scrolled), selection_text);
444
  gtk_container_add (GTK_CONTAINER (vbox), scrolled);
Elliot Lee's avatar
Elliot Lee committed
445 446
  gtk_widget_show (selection_text);

447
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
448
  gtk_container_add (GTK_CONTAINER (vbox), hbox);
Elliot Lee's avatar
Elliot Lee committed
449 450 451 452 453 454 455 456 457 458 459
  gtk_widget_show (hbox);

  label = gtk_label_new ("Target:");
  gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX(hbox), entry, TRUE, TRUE, 0);
  gtk_widget_show (entry);

  /* .. And create some buttons */
460 461 462 463 464 465 466 467
  gtk_dialog_add_button (GTK_DIALOG (dialog),
                         "Paste",
                         GTK_RESPONSE_APPLY);
  gtk_dialog_add_button (GTK_DIALOG (dialog),
                         "Quit",
                         GTK_RESPONSE_CLOSE);

  g_signal_connect (dialog, "response",
Manish Singh's avatar
Manish Singh committed
468
		    G_CALLBACK (paste), entry);
Elliot Lee's avatar
Elliot Lee committed
469 470 471 472 473 474 475

  gtk_widget_show (dialog);

  gtk_main ();

  return 0;
}