PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/src/wrappers/gtk/examples/gtk-demo/clipboard.e

http://github.com/tybor/Liberty
Specman e | 349 lines | 21 code | 65 blank | 263 comment | 2 complexity | 9e7fc3e4523b061a180f99100e21be6d MD5 | raw file
  1indexing
  2	description: "."
  3	copyright: "[
  4					Copyright (C) 2006 Paolo Redaelli, GTK+ team
  5					
  6					This library is free software; you can redistribute it and/or
  7					modify it under the terms of the GNU Lesser General Public License
  8					as published by the Free Software Foundation; either version 2.1 of
  9					the License, or (at your option) any later version.
 10					
 11					This library is distributed in the hope that it will be useful, but
 12					WITHOUT ANY WARRANTY; without even the implied warranty of
 13					MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14					Lesser General Public License for more details.
 15
 16					You should have received a copy of the GNU Lesser General Public
 17					License along with this library; if not, write to the Free Software
 18					Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 19					02110-1301 USA
 20			]"
 21
 22class CLIPBOARD
 23
 24creation make
 25
 26feature
 27-- /* Clipboard
 28--  *
 29--  * GtkClipboard is used for clipboard handling. This demo shows how to
 30--  * copy and paste text to and from the clipboard. 
 31--  *
 32--  * It also shows how to transfer images via the clipboard or via 
 33--  * drag-and-drop, and how to make clipboard contents persist after
 34--  * the application exits. Clipboard persistence requires a clipboard
 35--  * manager to run.
 36--  */
 37
 38-- #include <gtk/gtk.h>
 39-- #include <string.h>
 40
 41-- static GtkWidget *window = NULL;
 42
 43-- void
 44-- copy_button_clicked (GtkWidget *button,
 45--                      gpointer   user_data)
 46-- {
 47--   GtkWidget *entry;
 48--   GtkClipboard *clipboard;
 49
 50--   entry = GTK_WIDGET (user_data);
 51  
 52--   /* Get the clipboard object */
 53--   clipboard = gtk_widget_get_clipboard (entry,
 54--                                         GDK_SELECTION_CLIPBOARD);
 55
 56--   /* Set clipboard text */
 57--   gtk_clipboard_set_text (clipboard, gtk_entry_get_text (GTK_ENTRY (entry)), -1);
 58-- }
 59
 60-- void
 61-- paste_received (GtkClipboard *clipboard,
 62--                 const gchar  *text,
 63--                 gpointer      user_data)
 64-- {
 65--   GtkWidget *entry;
 66
 67--   entry = GTK_WIDGET (user_data);
 68  
 69--   /* Set the entry text */
 70--   if(text)
 71--     gtk_entry_set_text (GTK_ENTRY (entry), text);
 72-- }
 73
 74-- void
 75-- paste_button_clicked (GtkWidget *button,
 76--                      gpointer   user_data)
 77-- {
 78--   GtkWidget *entry;
 79--   GtkClipboard *clipboard;
 80
 81--   entry = GTK_WIDGET (user_data);
 82  
 83--   /* Get the clipboard object */
 84--   clipboard = gtk_widget_get_clipboard (entry,
 85--                                         GDK_SELECTION_CLIPBOARD);
 86
 87--   /* Request the contents of the clipboard, contents_received will be
 88--      called when we do get the contents.
 89--    */
 90--   gtk_clipboard_request_text (clipboard,
 91--                               paste_received, entry);
 92-- }
 93
 94-- static GdkPixbuf *
 95-- get_image_pixbuf (GtkImage *image)
 96-- {
 97--   gchar *stock_id;
 98--   GtkIconSize size;
 99
100--   switch (gtk_image_get_storage_type (image))
101--     {
102--     case GTK_IMAGE_PIXBUF:
103--       return g_object_ref (gtk_image_get_pixbuf (image));
104--     case GTK_IMAGE_STOCK:
105--       gtk_image_get_stock (image, &stock_id, &size);
106--       return gtk_widget_render_icon (GTK_WIDGET (image),
107--                                      stock_id, size, NULL);
108--     default:
109--       g_warning ("Image storage type %d not handled", 
110--                  gtk_image_get_storage_type (image));
111--       return NULL;
112--     }
113-- }
114
115-- static void
116-- drag_begin (GtkWidget      *widget,
117--             GdkDragContext *context,
118--             gpointer        data)
119-- {
120--   GdkPixbuf *pixbuf;
121
122--   pixbuf = get_image_pixbuf (GTK_IMAGE (data));
123--   gtk_drag_set_icon_pixbuf (context, pixbuf, -2, -2);
124--   g_object_unref (pixbuf);
125-- }
126
127-- void  
128-- drag_data_get  (GtkWidget        *widget,
129--                 GdkDragContext   *context,
130--                 GtkSelectionData *selection_data,
131--                 guint             info,
132--                 guint             time,
133--                 gpointer          data)
134-- {
135--   GdkPixbuf *pixbuf;
136
137--   pixbuf = get_image_pixbuf (GTK_IMAGE (data));
138--   gtk_selection_data_set_pixbuf (selection_data, pixbuf);
139--   g_object_unref (pixbuf);
140-- }
141
142-- static void
143-- drag_data_received (GtkWidget        *widget,
144--                     GdkDragContext   *context,
145--                     gint              x,
146--                     gint              y,
147--                     GtkSelectionData *selection_data,
148--                     guint             info,
149--                     guint32           time,
150--                     gpointer          data)
151-- {
152--   GdkPixbuf *pixbuf;
153
154--   if (selection_data->length > 0)
155--     {
156--       pixbuf = gtk_selection_data_get_pixbuf (selection_data);
157--       gtk_image_set_from_pixbuf (GTK_IMAGE (data), pixbuf);
158--       g_object_unref (pixbuf);
159--     }
160-- }
161
162-- static void
163-- copy_image (GtkMenuItem *item,
164--             gpointer     data)
165-- {
166--   GtkClipboard *clipboard;
167--   GdkPixbuf *pixbuf;
168
169--   clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
170--   pixbuf = get_image_pixbuf (GTK_IMAGE (data));
171
172--   gtk_clipboard_set_image (clipboard, pixbuf);
173--   g_object_unref (pixbuf);
174-- }
175
176-- static void
177-- paste_image (GtkMenuItem *item,
178--              gpointer     data)
179-- {
180--   GtkClipboard *clipboard;
181--   GdkPixbuf *pixbuf;
182
183--   clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
184--   pixbuf = gtk_clipboard_wait_for_image (clipboard);
185
186--   if (pixbuf)
187--     {
188--       gtk_image_set_from_pixbuf (GTK_IMAGE (data), pixbuf);
189--       g_object_unref (pixbuf);
190--     }
191-- }
192
193-- static gboolean
194-- button_press (GtkWidget      *widget,
195--               GdkEventButton *button,
196--               gpointer        data)
197-- {
198--   GtkWidget *menu;
199--   GtkWidget *item;
200  
201--   if (button->button != 3)
202--     return FALSE;
203
204--   menu = gtk_menu_new ();
205  
206--   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, NULL);
207--   g_signal_connect (item, "activate", G_CALLBACK (copy_image), data);
208--   gtk_widget_show (item);
209--   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
210
211--   item = gtk_image_menu_item_new_from_stock (GTK_STOCK_PASTE, NULL);
212--   g_signal_connect (item, "activate", G_CALLBACK (paste_image), data);
213--   gtk_widget_show (item);
214--   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
215
216--   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 3, button->time);
217--   return TRUE;
218-- }
219
220-- GtkWidget *
221-- do_clipboard (GtkWidget *do_widget)
222-- {
223--   if (!window)
224--     {
225--       GtkWidget *vbox, *hbox;
226--       GtkWidget *label;
227--       GtkWidget *entry, *button;
228--       GtkWidget *ebox, *image;
229--       GtkClipboard *clipboard;
230		
231--       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
232--       g_signal_connect (window, "destroy",
233--                         G_CALLBACK (gtk_widget_destroyed), &window);
234
235--       vbox = gtk_vbox_new (FALSE, 0);
236--       gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
237		
238--       gtk_container_add (GTK_CONTAINER (window), vbox);
239
240--       label = gtk_label_new ("\"Copy\" will copy the text\nin the entry to the clipboard");
241		
242--       gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
243
244--       hbox = gtk_hbox_new (FALSE, 4);
245--       gtk_container_set_border_width (GTK_CONTAINER (hbox), 8);
246--       gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
247
248--       /* Create the first entry */
249--       entry = gtk_entry_new ();
250--       gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
251		
252--       /* Create the button */
253--       button = gtk_button_new_from_stock (GTK_STOCK_COPY);
254--       gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
255--       g_signal_connect (button, "clicked",
256--                         G_CALLBACK (copy_button_clicked), entry);
257
258--       label = gtk_label_new ("\"Paste\" will paste the text from the clipboard to the entry");
259--       gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
260
261--       hbox = gtk_hbox_new (FALSE, 4);
262--       gtk_container_set_border_width (GTK_CONTAINER (hbox), 8);
263--       gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
264
265--       /* Create the second entry */
266--       entry = gtk_entry_new ();
267--       gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
268		
269--       /* Create the button */
270--       button = gtk_button_new_from_stock (GTK_STOCK_PASTE);
271--       gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
272--       g_signal_connect (button, "clicked",
273--                         G_CALLBACK (paste_button_clicked), entry);
274		
275--       label = gtk_label_new ("Images can be transferred via the clipboard, too");
276--       gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
277
278--       hbox = gtk_hbox_new (FALSE, 4);
279--       gtk_container_set_border_width (GTK_CONTAINER (hbox), 8);
280--       gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
281
282--       /* Create the first image */
283--       image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING, 
284--                                         GTK_ICON_SIZE_BUTTON);
285--       ebox = gtk_event_box_new ();
286--       gtk_container_add (GTK_CONTAINER (ebox), image);
287--       gtk_container_add (GTK_CONTAINER (hbox), ebox);
288
289--       /* make ebox a drag source */
290--       gtk_drag_source_set (ebox, GDK_BUTTON1_MASK, NULL, 0, GDK_ACTION_COPY);
291--       gtk_drag_source_add_image_targets (ebox);
292--       g_signal_connect (ebox, "drag_begin", 
293--                         G_CALLBACK (drag_begin), image);
294--       g_signal_connect (ebox, "drag_data_get", 
295--                         G_CALLBACK (drag_data_get), image);
296		
297--       /* accept drops on ebox */
298--       gtk_drag_dest_set (ebox, GTK_DEST_DEFAULT_ALL, 
299--                          NULL, 0, GDK_ACTION_COPY);
300--       gtk_drag_dest_add_image_targets (ebox);
301--       g_signal_connect (ebox, "drag_data_received", 
302--                         G_CALLBACK (drag_data_received), image);
303		
304--       /* context menu on ebox */
305--       g_signal_connect (ebox, "button_press_event", 
306--                         G_CALLBACK (button_press), image);
307
308--       /* Create the second image */
309--       image = gtk_image_new_from_stock (GTK_STOCK_STOP, 
310--                                         GTK_ICON_SIZE_BUTTON);
311--       ebox = gtk_event_box_new ();
312--       gtk_container_add (GTK_CONTAINER (ebox), image);
313--       gtk_container_add (GTK_CONTAINER (hbox), ebox);
314
315--       /* make ebox a drag source */
316--       gtk_drag_source_set (ebox, GDK_BUTTON1_MASK, NULL, 0, GDK_ACTION_COPY);
317--       gtk_drag_source_add_image_targets (ebox);
318--       g_signal_connect (ebox, "drag_begin", 
319--                         G_CALLBACK (drag_begin), image);
320--       g_signal_connect (ebox, "drag_data_get", 
321--                         G_CALLBACK (drag_data_get), image);
322		
323--       /* accept drops on ebox */
324--       gtk_drag_dest_set (ebox, GTK_DEST_DEFAULT_ALL, 
325--                          NULL, 0, GDK_ACTION_COPY);
326--       gtk_drag_dest_add_image_targets (ebox);
327--       g_signal_connect (ebox, "drag_data_received", 
328--                         G_CALLBACK (drag_data_received), image);
329		
330--       /* context menu on ebox */
331--       g_signal_connect (ebox, "button_press_event", 
332--                         G_CALLBACK (button_press), image);
333		
334--       /* tell the clipboard manager to make the data persistent */
335--       clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
336--       gtk_clipboard_set_can_store (clipboard, NULL, 0);
337--     }
338
339--   if (!GTK_WIDGET_VISIBLE (window))
340--     gtk_widget_show_all (window);
341--   else
342--     {
343--       gtk_widget_destroy (window);
344--       window = NULL;
345--     }
346
347--   return window;
348-- }
349end