Add support for copying hex data to the clipboard. Submitted by Thomas
authorRichard Sharpe <sharpe@ns.aus.com>
Thu, 2 Sep 2004 16:58:43 +0000 (16:58 -0000)
committerRichard Sharpe <sharpe@ns.aus.com>
Thu, 2 Sep 2004 16:58:43 +0000 (16:58 -0000)
Palmer.

What we really should do is add this to the Edit menu as well.

svn path=/trunk/; revision=11883

AUTHORS
gtk/menu.c
gtk/proto_draw.c
gtk/proto_draw.h
gtk/ui_util.c
gtk/ui_util.h

diff --git a/AUTHORS b/AUTHORS
index 88f1d12a71a356a24ba9b0edf8add352f9493c76..65193985cc76b97f333e532a9933de5537468019 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -2268,6 +2268,7 @@ Ming Zhang                <mingz [AT] ele.uri.edu>
 Neil Piercy            <Neil.Piercy [AT] ipaccess.com>
 RĂ©mi Denis-Courmont    <courmisch [AT] via.ecp.fr>
 Francisco Alcoba       <francisco.alcoba [AT] ericsson.com>
+Thomas Palmer          <tpalmer [AT] elmore.rr.com>
 
 Alain Magloire <alainm[AT]rcsm.ece.mcgill.ca> was kind enough to
 give his permission to use his version of snprintf.c.
index b9a3f39c1447449762af5303e37d7551b1c1a6f5..7ceb475741e09f375de2f2321ca2000e4bb3104f 100644 (file)
@@ -473,6 +473,8 @@ static GtkItemFactoryEntry hexdump_menu_items[] =
     ITEM_FACTORY_ENTRY("/Display Filters...", NULL, dfilter_dialog_cb,
                        0, NULL, NULL),
     ITEM_FACTORY_ENTRY("/Export Selected Packet Bytes...", NULL, savehex_cb,
+                       0, NULL, NULL),
+    ITEM_FACTORY_ENTRY("/Copy Packet Bytes into Clipboard", NULL, copy_hex_cb,
                        0, NULL, NULL)
 };
 
index a2eb20fbea626a427ba09e751e35a7ff7f940236..f824483c8317b3e9a6baa98891a52c600f8e0210 100644 (file)
@@ -801,6 +801,47 @@ savehex_dlg_destroy_cb(void)
         savehex_dlg = NULL;
 }
 
+extern void
+copy_hex_cb(GtkWidget * w _U_, gpointer data _U_)  
+{
+       GtkWidget *bv;
+       int len;
+       int i=0;
+       const guint8 *data_p = NULL;
+       GString *ASCII_representation = g_string_new("");
+       GString *byte_str = g_string_new("");
+       
+       bv = get_notebook_bv_ptr(byte_nb_ptr);
+       if (bv == NULL) {
+               /* shouldn't happen */ 
+               simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not find the corresponding text window!");
+               return;
+       }
+       
+       data_p = get_byte_view_data_and_length(GTK_WIDGET(bv), &len);
+       
+       g_string_append_printf(byte_str,"%04x  ",i); /* Offset 0000 */
+       for (i=0; i<len; i++){
+         g_string_append_printf(ASCII_representation,"%c",isprint(*data_p) ? *data_p : '.');
+         g_string_append_printf(byte_str," %02x",*data_p++);
+         if ((i+1)%16==0 && i!=0){
+           g_string_append_printf(byte_str,"  %s\n%04x  ",ASCII_representation->str,i+1);
+           g_string_assign (ASCII_representation,"");
+         }
+       }
+       
+       if(ASCII_representation->len){
+         for (i=ASCII_representation->len; i<16; i++){
+           g_string_append_printf(byte_str,"   ");
+         }
+         g_string_append_printf(byte_str,"  %s\n",ASCII_representation->str);
+       }
+       /* Now that we have the byte data, copy it into the default clipboard */
+        copy_to_clipboard(byte_str);
+       g_string_free(byte_str, TRUE);                       /* Free the memory */
+       g_string_free(ASCII_representation, TRUE);           /* Free the memory */
+}
+
 /* save the current highlighted hex data */
 static void
 savehex_save_clicked_cb(GtkWidget * w _U_, gpointer data _U_)
index 6f9a95f3d02e4a1017af87e497c939cdf899416a..740161df5853b060b51590f0e13fbf04fcd86ed7 100644 (file)
@@ -96,6 +96,13 @@ extern gboolean byte_view_select(GtkWidget *widget, GdkEventButton *event);
  */
 extern void savehex_cb(GtkWidget * w, gpointer data);
 
+/** Callback for "Copy packet bytes to clipboard" operation.
+ *
+ * @param w unused
+ * @param data unused
+ */
+extern void copy_hex_cb(GtkWidget * w, gpointer data);
+
 #if GTK_MAJOR_VERSION < 2
 /** Redraw a given byte view window.
  *
index 8401f7042ca44d0a1a32bf738ad6222d6484a587..2e50b767ac745646e922d225cb193e51682158dd 100644 (file)
@@ -988,3 +988,24 @@ simple_list_new(gint cols, gchar **titles) {
     return plugins_list;
 }
 
+extern void
+copy_to_clipboard(GString *str)  
+{
+#if (GTK_MAJOR_VERSION >= 2)
+        GtkClipboard    *cb;
+
+       cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);     /* Get the default clipboard */
+       gtk_clipboard_set_text(cb, str->str, -1);            /* Copy the byte data into the clipboard */
+#else
+        GtkWidget *window;
+        GtkWidget *text;
+
+        window = window_new (GTK_WINDOW_TOPLEVEL,"");
+        text = gtk_text_new (NULL, NULL);                 /* Create the GtkText widget */
+        gtk_container_add (GTK_CONTAINER (window), text); /* Avoid a GTK assertion */
+        gtk_widget_realize (text);   /* Realizing a widget creates a window for it, ready for us to insert some text */
+        gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str->str, -1);
+        gtk_editable_select_region((GtkEditable *)text, 0, -1); /* Select ALL text */
+        gtk_editable_copy_clipboard((GtkEditable *)text); /* Copy the byte data into the clipboard */
+#endif
+}
index 9ac69a2e896acd05c89843031bee474d02769651..ca7f3c024cca9381a360a91a92479bb88c02c626 100644 (file)
@@ -291,4 +291,10 @@ extern GtkWidget *xpm_to_widget(const char ** xpm);
  */
 extern GtkWidget *xpm_to_widget_from_parent(GtkWidget *parent, const char ** xpm);
 
+/** Copy a GString to the clipboard.
+ *
+ * @param str GString that is to be copied to the clipboard.
+ */
+extern void copy_to_clipboard(GString *str);  
+
 #endif /* __GTKGUIUI_UTIL_H__ */