Code

* Add connection-pool.h: Inkscape::ConnectionPool, an auxilliary class to manage...
authormderezynski <mderezynski@users.sourceforge.net>
Fri, 19 May 2006 03:28:16 +0000 (03:28 +0000)
committermderezynski <mderezynski@users.sourceforge.net>
Fri, 19 May 2006 03:28:16 +0000 (03:28 +0000)
* Finish listening to selections, make family selector work properly

src/Makefile_insert
src/connection-pool.h [new file with mode: 0644]
src/widgets/toolbox.cpp

index aaaca15266c1580e418f872df1e3edb682a39188..ec26df0ed2a8a4caa504ecccea3949861a18f054 100644 (file)
@@ -48,6 +48,7 @@ libinkpre_a_SOURCES = \
        conditions.cpp conditions.h\
        conn-avoid-ref.cpp conn-avoid-ref.h     \
        connector-context.cpp connector-context.h       \
+       connection-pool.h \
        context-fns.cpp context-fns.h \
        desktop-affine.cpp desktop-affine.h     \
        desktop-events.cpp desktop-events.h     \
diff --git a/src/connection-pool.h b/src/connection-pool.h
new file mode 100644 (file)
index 0000000..7a14f57
--- /dev/null
@@ -0,0 +1,83 @@
+#ifndef CONNECTION_POOL_H
+#define CONNECTION_POOL_H
+
+#include <glib-object.h>
+#include <gtkmm.h>
+#include <sigc++/sigc++.h>
+
+namespace Inkscape
+{
+class ConnectionPool
+{
+  public:
+
+    enum Exception
+    {
+      NAME_EXISTS,
+      NAME_DOES_NOT_EXIST
+    };
+
+    typedef std::map<std::string, sigc::connection*> ConnectionMap;
+
+    ~ConnectionPool ()
+    {
+      for (ConnectionMap::iterator iter = map.begin (), end = map.end (); iter != end; ++iter)
+      {
+       sigc::connection* connection = (*iter).second;
+       connection->disconnect ();
+       delete connection;
+      }
+    }
+
+    void
+    add_connection (std::string name, sigc::connection* connection)
+    {
+      if (map.find (name) != map.end ()) throw NAME_EXISTS;
+      map.insert (std::make_pair (name, connection)); 
+    }
+
+    void
+    del_connection (std::string name)
+    {
+      ConnectionMap::iterator iter = map.find (name); 
+      if (iter == map.end ()) throw NAME_DOES_NOT_EXIST;
+      sigc::connection* connection = (*iter).second;
+      connection->disconnect ();
+      delete connection;
+    }
+
+
+    static Inkscape::ConnectionPool*
+    new_connection_pool (std::string name)
+    {
+       return new ConnectionPool (name); 
+    }
+
+    static void
+    del_connection_pool (Inkscape::ConnectionPool* pool)
+    {
+      delete pool;
+    }
+
+    static void
+    connect_destroy (GObject *obj, Inkscape::ConnectionPool *pool)
+    {
+      g_object_connect (obj, "swapped-signal::destroy", G_CALLBACK (del_connection_pool), pool, NULL);
+    }
+
+    operator std::string ()
+    {
+      return name;
+    }
+
+  private:
+
+    ConnectionPool (std::string name) : name (name)
+    {}
+
+    ConnectionMap map;
+    std::string   name;
+};
+}
+
+#endif
index de7b27c579e6210ba1e209117ffa2589c2a9141d..eb70ea509f0db1775a30b00f2934f602f54fa527 100644 (file)
@@ -69,6 +69,7 @@
 #include "document-private.h"
 #include "desktop-style.h"
 #include "../libnrtype/font-lister.h"
+#include "../connection-pool.h"
 
 #include "mod360.h"
 
@@ -89,7 +90,8 @@ static GtkWidget *sp_calligraphy_toolbox_new(SPDesktop *desktop);
 static GtkWidget *sp_dropper_toolbox_new(SPDesktop *desktop);
 static GtkWidget *sp_empty_toolbox_new(SPDesktop *desktop);
 static GtkWidget *sp_connector_toolbox_new(SPDesktop *desktop);
-static GtkWidget *sp_text_toolbox_new (SPDesktop *desktop);
+
+namespace { GtkWidget *sp_text_toolbox_new (SPDesktop *desktop); }
 
 
 static struct {
@@ -2783,60 +2785,115 @@ sp_text_letter_rotation_changed(GtkAdjustment *adj, GtkWidget *tbl)
     //Call back for letter rotation spinbutton
 }*/
 
-static void
-sp_text_toolbox_selection_changed (Inkscape::Selection *selection, GObject *tbl)
-{
-    GtkWidget *cbox = GTK_WIDGET(g_object_get_data (G_OBJECT(tbl), "combo-box-family"));
-
-    // create temporary style
-    SPStyle *query = sp_style_new ();
-    // query style from desktop into it. This returns a result flag and fills query with the style of subselection, if any, or selection
-    int result_family = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_FONTFAMILY); 
-    int result_style = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_FONTSTYLE); 
-    int result_numbers = sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_FONTNUMBERS); 
+namespace {
 
-    // If querying returned nothing, read the style from the text tool prefs (default style for new texts)
-    if (result_family == QUERY_STYLE_NOTHING || result_style == QUERY_STYLE_NOTHING || result_numbers == QUERY_STYLE_NOTHING)
+    void
+    sp_text_toolbox_selection_changed (Inkscape::Selection *selection, GObject *tbl)
     {
-        return;
+        GtkComboBox *cbox = 0;
+    
+        SPStyle *query =
+            sp_style_new ();
+
+        int result_family =
+            sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_FONTFAMILY); 
+
+        int result_style =
+            sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_FONTSTYLE); 
+
+        int result_numbers =
+            sp_desktop_query_style (SP_ACTIVE_DESKTOP, query, QUERY_STYLE_PROPERTY_FONTNUMBERS); 
+
+        // If querying returned nothing, read the style from the text tool prefs (default style for new texts)
+        if (result_family == QUERY_STYLE_NOTHING || result_style == QUERY_STYLE_NOTHING || result_numbers == QUERY_STYLE_NOTHING)
+        {
+            return;
+        }
+
+        if (result_numbers == QUERY_STYLE_MULTIPLE_DIFFERENT)
+        {
+            static char* cboxes[] = { "combo-box-family", "combo-box-style" };
+        
+            for (unsigned n = 0 ; n < G_N_ELEMENTS(cboxes); ++n)
+            {
+                cbox = GTK_COMBO_BOX(g_object_get_data (G_OBJECT(tbl), cboxes[n]));
+                g_object_set_data (G_OBJECT (cbox), "block", GINT_TO_POINTER(1));
+                gtk_combo_box_set_active (GTK_COMBO_BOX (cbox), -1); 
+                g_object_set_data (G_OBJECT (cbox), "block", GINT_TO_POINTER(0));
+            }
+            return;
+        }
+
+        if (query->text)
+        {
+            if (query->text->font_family.value) 
+            {
+                Gtk::TreePath path;
+                try {
+                    path = Inkscape::FontLister::get_instance()->get_row_for_font (query->text->font_family.value);
+                } catch (...) {
+                    return;
+                }
+
+                cbox = GTK_COMBO_BOX(g_object_get_data (G_OBJECT(tbl), "combo-box-family"));
+                g_object_set_data (G_OBJECT (cbox), "block", GINT_TO_POINTER(1));
+                gtk_combo_box_set_active (GTK_COMBO_BOX (cbox), gtk_tree_path_get_indices (path.gobj())[0]);
+                g_object_set_data (G_OBJECT (cbox), "block", GINT_TO_POINTER(0));
+            }
+
+            //Style
+            cbox = GTK_COMBO_BOX(g_object_get_data (G_OBJECT(tbl), "combo-box-style"));
+            g_object_set_data (G_OBJECT (cbox), "block", GINT_TO_POINTER(1));
+            gtk_combo_box_set_active (GTK_COMBO_BOX (cbox), gint(query->font_style.value));
+            g_object_set_data (G_OBJECT (cbox), "block", GINT_TO_POINTER(0));
+        }
     }
 
-    if (result_numbers == QUERY_STYLE_MULTIPLE_DIFFERENT)
+    void
+    sp_text_toolbox_selection_modified (Inkscape::Selection *selection, guint flags, GObject *tbl) 
     {
-        g_object_set_data (G_OBJECT (cbox), "block", GINT_TO_POINTER(1));
-        gtk_combo_box_set_active (GTK_COMBO_BOX (cbox), -1); 
-        g_object_set_data (G_OBJECT (cbox), "block", GINT_TO_POINTER(0));
-        return;
+        sp_text_toolbox_selection_changed (selection, tbl); 
     }
 
-    if (query->text && query->text->font_family.value)
+    void
+    sp_text_toolbox_subselection_changed (gpointer dragger, GObject *tbl)
     {
-        Gtk::TreePath path;
-        try {
-            path = Inkscape::FontLister::get_instance()->get_row_for_font (query->text->font_family.value);
-        } catch (...) {
-            return;
-        }
-        g_object_set_data (G_OBJECT (cbox), "block", GINT_TO_POINTER(1));
-        gtk_combo_box_set_active (GTK_COMBO_BOX (cbox), gtk_tree_path_get_indices (path.gobj())[0]);
-        g_object_set_data (G_OBJECT (cbox), "block", GINT_TO_POINTER(0));
+        sp_text_toolbox_selection_changed (NULL, tbl); 
     }
-}
 
-static void
-sp_text_toolbox_family_changed (GtkComboBox *cbox,
+    void
+    sp_text_toolbox_family_changed (GtkComboBox *cbox,
                                 GtkWidget   *tbl) 
-{
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+    {
+        SPDesktop *desktop = SP_ACTIVE_DESKTOP;
 
-    if (GPOINTER_TO_INT(g_object_get_data (G_OBJECT (cbox), "block")) != 0) return;
+        if (GPOINTER_TO_INT(g_object_get_data (G_OBJECT (cbox), "block")) != 0) return;
 
-    SPCSSAttr *css = sp_repr_css_attr_new (); 
-    sp_repr_css_set_property (css, "font-family", gtk_combo_box_get_active_text (cbox));
-    sp_desktop_set_style (desktop, css, true, true);
-    sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP));
-    sp_repr_css_attr_unref (css);
-}
+        SPCSSAttr *css = sp_repr_css_attr_new (); 
+        sp_repr_css_set_property (css, "font-family", gtk_combo_box_get_active_text (cbox));
+        sp_desktop_set_style (desktop, css, true, true);
+        sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP));
+        sp_repr_css_attr_unref (css);
+    }
+
+    void
+    sp_text_toolbox_style_changed (GtkComboBox *cbox,
+                                   GtkWidget   *tbl) 
+    {
+        SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+
+        if (GPOINTER_TO_INT(g_object_get_data (G_OBJECT (cbox), "block")) != 0) return;
+    
+        static char* styles[] = { "normal", "italic" , "oblique" };
+
+        SPCSSAttr *css = sp_repr_css_attr_new (); 
+        sp_repr_css_set_property (css, "font-style", styles[gtk_combo_box_get_active (cbox)]);
+        sp_desktop_set_style (desktop, css, true, true);
+        sp_document_done (sp_desktop_document (SP_ACTIVE_DESKTOP));
+        sp_repr_css_attr_unref (css);
+    }
+
+}//<unnamed> namespace
 
 #if 0
 static void  cell_data_func  (GtkCellLayout     *cell_layout,
@@ -2851,10 +2908,12 @@ static void  cell_data_func  (GtkCellLayout     *cell_layout,
 }
 #endif
 
-static GtkWidget*
-sp_text_toolbox_new (SPDesktop *desktop)
+namespace
 {
-    GtkWidget   *tbl = gtk_hbox_new (FALSE, 0);
+    GtkWidget*
+    sp_text_toolbox_new (SPDesktop *desktop)
+    {
+        GtkWidget   *tbl = gtk_hbox_new (FALSE, 0);
 
 #if 0
     GtkWidget   *us = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(tbl), "units"));
@@ -2862,16 +2921,16 @@ sp_text_toolbox_new (SPDesktop *desktop)
     GtkWidget   *group;
 #endif
 
-    //Font Family
-    GtkWidget *cbox = gtk_combo_box_entry_new_text ();
-    Glib::RefPtr<Gtk::ListStore> store = Inkscape::FontLister::get_instance()->get_font_list();
-    gtk_cell_layout_clear (GTK_CELL_LAYOUT (cbox));
-    GtkCellRenderer *cell = gtk_cell_renderer_text_new ();
-    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cbox), cell, FALSE);
-    gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (cbox), cell, "text", 0, NULL);
+        //Font Family
+        GtkWidget *cbox = gtk_combo_box_entry_new_text ();
+        Glib::RefPtr<Gtk::ListStore> store = Inkscape::FontLister::get_instance()->get_font_list();
+        gtk_cell_layout_clear (GTK_CELL_LAYOUT (cbox));
+        GtkCellRenderer *cell = gtk_cell_renderer_text_new ();
+        gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cbox), cell, FALSE);
+        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (cbox), cell, "text", 0, NULL);
 
 #if 0
-    gtk_cell_layout_set_cell_data_func
+        gtk_cell_layout_set_cell_data_func
             (GTK_CELL_LAYOUT (cbox),
              cell, 
              GtkCellLayoutDataFunc (cell_data_func),
@@ -2879,26 +2938,42 @@ sp_text_toolbox_new (SPDesktop *desktop)
              NULL); 
 #endif
 
-    gtk_combo_box_set_model (GTK_COMBO_BOX (cbox), GTK_TREE_MODEL (Glib::unwrap(store)));
-
-    gtk_widget_set_size_request (cbox, 250, -1);
-    aux_toolbox_space (tbl, 1);
-    gtk_box_pack_start (GTK_BOX (tbl), cbox, FALSE, FALSE, 0);
-    g_object_set_data (G_OBJECT (tbl), "combo-box-family", cbox);
-    g_signal_connect (G_OBJECT (cbox), "changed", G_CALLBACK (sp_text_toolbox_family_changed), tbl);
-
-#if 0
-    //Font Style
-    GtkWidget *cbox = gtk_combo_box_new_text ();
-    gtk_widget_set_size_request (cbox, 144, -1);
-    aux_toolbox_space (tbl, 1);
-    gtk_box_pack_start (GTK_BOX (tbl), cbox, FALSE, FALSE, 0);
-    g_object_set_data (G_OBJECT (tbl), "combo-box-style", cbox);
-#endif
-
-    sigc::connection *connection =
-    new sigc::connection( sp_desktop_selection (desktop)->connectChanged (sigc::bind (sigc::ptr_fun (sp_text_toolbox_selection_changed), (GObject*)tbl)));
-    g_signal_connect(G_OBJECT(tbl), "destroy", G_CALLBACK(delete_connection), connection);
+        gtk_combo_box_set_model (GTK_COMBO_BOX (cbox), GTK_TREE_MODEL (Glib::unwrap(store)));
+        gtk_widget_set_size_request (cbox, 250, -1);
+        aux_toolbox_space (tbl, 1);
+        gtk_box_pack_start (GTK_BOX (tbl), cbox, FALSE, FALSE, 0);
+        g_object_set_data (G_OBJECT (tbl), "combo-box-family", cbox);
+        g_signal_connect (G_OBJECT (cbox), "changed", G_CALLBACK (sp_text_toolbox_family_changed), tbl);
+
+        //Font Style
+        cbox = gtk_combo_box_new_text ();
+        gtk_combo_box_append_text (GTK_COMBO_BOX (cbox), _("Normal"));
+        gtk_combo_box_append_text (GTK_COMBO_BOX (cbox), _("Italic"));
+        gtk_combo_box_append_text (GTK_COMBO_BOX (cbox), _("Oblique"));
+        gtk_widget_set_size_request (cbox, 144, -1);
+        aux_toolbox_space (tbl, 1);
+        gtk_box_pack_start (GTK_BOX (tbl), cbox, FALSE, FALSE, 0);
+        g_object_set_data (G_OBJECT (tbl), "combo-box-style", cbox);
+        g_signal_connect (G_OBJECT (cbox), "changed", G_CALLBACK (sp_text_toolbox_style_changed), tbl);
+
+        Inkscape::ConnectionPool* pool = Inkscape::ConnectionPool::new_connection_pool ("ISTextToolbox");
+
+        sigc::connection *c_selection_changed =
+            new sigc::connection (sp_desktop_selection (desktop)->connectChanged 
+                                (sigc::bind (sigc::ptr_fun (sp_text_toolbox_selection_changed), (GObject*)tbl)));
+        pool->add_connection ("selection-changed", c_selection_changed);
+
+        sigc::connection *c_selection_modified =
+            new sigc::connection (sp_desktop_selection (desktop)->connectModified 
+                                (sigc::bind (sigc::ptr_fun (sp_text_toolbox_selection_modified), (GObject*)tbl)));
+        pool->add_connection ("selection-modified", c_selection_modified);
+
+        sigc::connection *c_subselection_changed =
+            new sigc::connection (desktop->connectToolSubselectionChanged
+                                (sigc::bind (sigc::ptr_fun (sp_text_toolbox_subselection_changed), (GObject*)tbl)));
+        pool->add_connection ("tool-subselection-changed", c_subselection_changed);
+
+        Inkscape::ConnectionPool::connect_destroy (G_OBJECT (tbl), pool);
 
 #if 0
     //Font Size
@@ -3160,15 +3235,17 @@ sp_text_toolbox_new (SPDesktop *desktop)
         }
 #endif
 
-    Inkscape::UI::Widget::StyleSwatch *swatch = new Inkscape::UI::Widget::StyleSwatch(NULL);
-    swatch->setWatchedTool ("tools.text", true);
-    GtkWidget *swatch_ = GTK_WIDGET(swatch->gobj());
-    gtk_box_pack_end (GTK_BOX(tbl), swatch_, FALSE, FALSE, 0);
-    gtk_widget_show_all (tbl);
+        Inkscape::UI::Widget::StyleSwatch *swatch = new Inkscape::UI::Widget::StyleSwatch(NULL);
+        swatch->setWatchedTool ("tools.text", true);
+        GtkWidget *swatch_ = GTK_WIDGET(swatch->gobj());
+        gtk_box_pack_end (GTK_BOX(tbl), swatch_, FALSE, FALSE, 0);
+        gtk_widget_show_all (tbl);
 
-    return tbl;
+        return tbl;
+
+    } // end of sp_text_toolbox_new()
 
-} // end of sp_text_toolbox_new()
+}//<unnamed> namespace
 
 
 //#########################