Fix for bug 2630. Add copy to clipboard button
authorQball Cow <qball@sarine.nl>
Sun, 1 Nov 2009 20:52:42 +0000 (21:52 +0100)
committerQball Cow <qball@sarine.nl>
Sun, 1 Nov 2009 20:52:42 +0000 (21:52 +0100)
This patch adds a copy to clipboard button to the messages dialog.
This copies the selected rows (or all if no row selected) to the default clipboard.

glade/playlist-message-window.ui
src/playlist3-messages.c

index f0f491e..7be57d8 100644 (file)
@@ -1,21 +1,23 @@
 <?xml version="1.0"?>
-<!--*- mode: xml -*-->
 <interface>
+  <!-- interface-requires gtk+ 2.12 -->
+  <!-- interface-naming-policy toplevel-contextual -->
   <object class="GtkWindow" id="message_window">
     <property name="title" translatable="yes">Messages</property>
     <property name="default_width">650</property>
     <property name="default_height">150</property>
-    <signal handler="message_window_destroy" name="delete_event"/>
+    <signal name="delete_event" handler="message_window_destroy"/>
     <child>
-      <object class="GtkVBox" id="vbox8">
+      <object class="GtkVBox" id="main_hbox">
         <property name="visible">True</property>
+        <property name="orientation">vertical</property>
         <child>
           <object class="GtkScrolledWindow" id="scrolledwindow3">
             <property name="visible">True</property>
             <property name="can_focus">True</property>
-            <property name="hscrollbar_policy">GTK_POLICY_AUTOMATIC</property>
-            <property name="vscrollbar_policy">GTK_POLICY_AUTOMATIC</property>
-            <property name="shadow_type">GTK_SHADOW_ETCHED_IN</property>
+            <property name="hscrollbar_policy">automatic</property>
+            <property name="vscrollbar_policy">automatic</property>
+            <property name="shadow_type">etched-in</property>
             <child>
               <object class="GtkTreeView" id="message_tree">
                 <property name="visible">True</property>
               </object>
             </child>
           </object>
+          <packing>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkHBox" id="hbox1">
+            <property name="visible">True</property>
+            <child>
+              <object class="GtkButton" id="button1">
+                <property name="label" translatable="yes">_Copy to clipboard</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="image">copy_image</property>
+                <property name="use_underline">True</property>
+                <signal name="clicked" handler="copy_to_clipboard"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="pack_type">end</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="position">1</property>
+          </packing>
         </child>
       </object>
     </child>
   </object>
+  <object class="GtkImage" id="copy_image">
+    <property name="visible">True</property>
+    <property name="stock">gtk-copy</property>
+  </object>
 </interface>
index 9ece08e..9f41b74 100644 (file)
@@ -235,6 +235,51 @@ void message_window_destroy(GtkWidget *win,GdkEvent *event, GtkBuilder *message_
        g_object_unref(message_xml);
        message_xml = NULL;
 }
+void copy_to_clipboard(GtkButton *button, GtkBuilder *xml)
+{
+       GtkWidget *tree= (GtkWidget *) gtk_builder_get_object(xml, "message_tree");
+       GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(tree));
+       GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
+       GtkClipboard *gcb = NULL;
+       GList *list = NULL, *liter = NULL;
+       GString *str = g_string_new("");
+       printf("Copy to clipboard\n");
+       if(gtk_tree_selection_count_selected_rows(selection) == 0)
+       {
+               GtkTreeIter iter;
+               if(gtk_tree_model_get_iter_first(model, &iter)) {
+                       do{
+                               GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
+                               list = g_list_prepend(list, path);
+                       }while(gtk_tree_model_iter_next(model, &iter));
+               }       
+               list = g_list_reverse(list);
+       }else{
+                       list = gtk_tree_selection_get_selected_rows(selection, NULL);
+       }
+       for(liter = g_list_first(list); liter; liter = g_list_next(liter))
+       {
+               gchar *message = NULL;
+               GtkTreeIter iter;
+               if(gtk_tree_model_get_iter(model, &iter, liter->data))
+               {
+                       gtk_tree_model_get(model,&iter,2, &message, -1);
+                       str = g_string_append(str, message);
+                       str = g_string_append(str, "\n");
+                       g_free(message);
+               }
+       }
+
+       gcb = gtk_widget_get_clipboard(button, GDK_SELECTION_CLIPBOARD);
+       printf("Set clipboard: %s\n", str->str);
+       gtk_clipboard_set_text(gcb, str->str, str->len);
+       
+
+       g_list_foreach (list, gtk_tree_path_free, NULL);
+       g_list_free (list);
+       g_string_free(str, TRUE);
+}
+
 static void playlist3_message_window_open(Playlist3MessagePlugin *self)
 {
        GtkBuilder *message_xml = NULL;