Code

Made style minibar take color drag-n-drop
authorjoncruz <joncruz@users.sourceforge.net>
Wed, 1 Mar 2006 17:21:17 +0000 (17:21 +0000)
committerjoncruz <joncruz@users.sourceforge.net>
Wed, 1 Mar 2006 17:21:17 +0000 (17:21 +0000)
ChangeLog
src/ui/widget/selected-style.cpp
src/ui/widget/selected-style.h

index 69abdd7c87e4ffdb2d8a4726758c0f1ac7412ce5..bcd28a2f8645228088512cdeabb02151dfd090e1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2006-02-28  Jon A. Cruz  <jon@joncruz.org>
+       * src/ui/widget/selected-style.h, src/ui/widget/selected-style.cpp:
+
+         Made the style minibar a target for color drag-n-drop.
+       
 2006-03-01  Michael Wybrow  <mjwybrow@users.sourceforge.net>
 
        * src/conn-avoid-ref.cpp, src/libavoid/connector.h,
index c808289b21b6133f6f494cd555b988990f07d7ba..8a5aa1f46b329ddfb1ced1bfd9ad9ec98ebb4ce2 100644 (file)
@@ -13,6 +13,7 @@
 # include <config.h>
 #endif
 
+#include <gtk/gtkdnd.h>
 
 #include "selected-style.h"
 
@@ -61,6 +62,25 @@ namespace Inkscape {
 namespace UI {
 namespace Widget {
 
+
+typedef struct {
+    SelectedStyle* parent;
+    int item;
+} DropTracker;
+
+/* Drag and Drop */
+typedef enum {
+    APP_X_COLOR
+} ui_drop_target_info;
+
+static GtkTargetEntry ui_drop_target_entries [] = {
+    {"application/x-color", 0, APP_X_COLOR}
+};
+
+#define ENTRIES_SIZE(n) sizeof(n)/sizeof(n[0])
+static guint nui_drop_target_entries = ENTRIES_SIZE(ui_drop_target_entries);
+
+
 SelectedStyle::SelectedStyle(bool layout)
     : _desktop (NULL),
 
@@ -89,7 +109,10 @@ SelectedStyle::SelectedStyle(bool layout)
 
       _sw_unit(NULL),
 
-      _tooltips ()
+      _tooltips (),
+
+      _dropF(0),
+      _dropS(0)
 {
     _fill_label.set_alignment(0.0, 0.5);
     _fill_label.set_padding(0, 0);
@@ -307,6 +330,36 @@ SelectedStyle::SelectedStyle(bool layout)
     sp_set_font_size_smaller (GTK_WIDGET(_stroke_width.gobj()));
     sp_set_font_size_smaller (GTK_WIDGET(_fill_label.gobj()));
     sp_set_font_size_smaller (GTK_WIDGET(_stroke_label.gobj()));
+
+    _dropF = new DropTracker();
+    ((DropTracker*)_dropF)->parent = this;
+    ((DropTracker*)_dropF)->item = SS_FILL;
+
+    _dropS = new DropTracker();
+    ((DropTracker*)_dropS)->parent = this;
+    ((DropTracker*)_dropS)->item = SS_STROKE;
+
+    {
+        gtk_drag_dest_set(GTK_WIDGET(_stroke_place.gobj()),
+                          GTK_DEST_DEFAULT_ALL,
+                          ui_drop_target_entries,
+                          nui_drop_target_entries,
+                          GdkDragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE));
+        g_signal_connect(_stroke_place.gobj(),
+                         "drag_data_received",
+                         G_CALLBACK(dragDataReceived),
+                         _dropS);
+
+        gtk_drag_dest_set(GTK_WIDGET(_fill_place.gobj()),
+                          GTK_DEST_DEFAULT_ALL,
+                          ui_drop_target_entries,
+                          nui_drop_target_entries,
+                          GdkDragAction(GDK_ACTION_COPY | GDK_ACTION_MOVE));
+        g_signal_connect(_fill_place.gobj(),
+                         "drag_data_received",
+                         G_CALLBACK(dragDataReceived),
+                         _dropF);
+    }
 }
 
 SelectedStyle::~SelectedStyle()
@@ -321,6 +374,9 @@ SelectedStyle::~SelectedStyle()
     for (int i = SS_FILL; i <= SS_STROKE; i++) {
         delete _color_preview[i];
     }
+
+    delete (DropTracker*)_dropF;
+    delete (DropTracker*)_dropS;
 }
 
 void
@@ -350,6 +406,43 @@ SelectedStyle::setDesktop(SPDesktop *desktop)
     //_sw_unit = (SPUnit *) SP_DT_NAMEDVIEW(desktop)->doc_units;
 }
 
+void SelectedStyle::dragDataReceived( GtkWidget *widget,
+                                      GdkDragContext *drag_context,
+                                      gint x, gint y,
+                                      GtkSelectionData *data,
+                                      guint info,
+                                      guint event_time,
+                                      gpointer user_data )
+{
+    DropTracker* tracker = (DropTracker*)user_data;
+
+    switch ( (int)tracker->item ) {
+        case SS_FILL:
+        case SS_STROKE:
+        {
+            if ( data->length == 8 ) {
+                gchar c[64];
+                // Careful about endian issues.
+                guint16* dataVals = (guint16*)data->data;
+                sp_svg_write_color( c, 64,
+                                    SP_RGBA32_U_COMPOSE(
+                                        0x0ff & (dataVals[0] >> 8),
+                                        0x0ff & (dataVals[1] >> 8),
+                                        0x0ff & (dataVals[2] >> 8),
+                                        0xff // can't have transparency in the color itself
+                                        //0x0ff & (data->data[3] >> 8),
+                                        ));
+                SPCSSAttr *css = sp_repr_css_attr_new();
+                sp_repr_css_set_property( css, (tracker->item == SS_FILL) ? "fill":"stroke", c );
+                sp_desktop_set_style( tracker->parent->_desktop, css );
+                sp_repr_css_attr_unref( css );
+                sp_document_done( SP_DT_DOCUMENT(tracker->parent->_desktop) );
+            }
+        }
+        break;
+    }
+}
+
 void SelectedStyle::on_fill_remove() {
     SPCSSAttr *css = sp_repr_css_attr_new ();
     sp_repr_css_set_property (css, "fill", "none");
index 8f9c0fbf609ec3f1536fb5933444179b3c095bda..807d4ea949b67c6cb45ef78b39aba0dde40c49ed 100644 (file)
@@ -125,6 +125,14 @@ protected:
     sigc::connection *selection_modified_connection;
     sigc::connection *subselection_changed_connection;
 
+    static void dragDataReceived( GtkWidget *widget,
+                                  GdkDragContext *drag_context,
+                                  gint x, gint y,
+                                  GtkSelectionData *data,
+                                  guint info,
+                                  guint event_time,
+                                  gpointer user_data );
+
     bool on_fill_click(GdkEventButton *event);
     bool on_stroke_click(GdkEventButton *event);
     bool on_opacity_click(GdkEventButton *event);
@@ -191,6 +199,9 @@ protected:
     SPUnit *_sw_unit;
 
     Gtk::Tooltips _tooltips;
+
+    void *_dropF;
+    void *_dropS;
 };