From 59b2c04f52a4732e9305c9411726bf31e40afb57 Mon Sep 17 00:00:00 2001 From: joncruz Date: Tue, 17 Mar 2009 08:32:02 +0000 Subject: [PATCH] Initial read-only generation of per-document palette --- src/ui/dialog/swatches.cpp | 164 +++++++++++++++++++++++++++------ src/ui/dialog/swatches.h | 12 +++ src/widgets/desktop-widget.cpp | 13 +-- src/widgets/desktop-widget.h | 4 +- 4 files changed, 159 insertions(+), 34 deletions(-) diff --git a/src/ui/dialog/swatches.cpp b/src/ui/dialog/swatches.cpp index f1d6c6816..c3b5dd678 100644 --- a/src/ui/dialog/swatches.cpp +++ b/src/ui/dialog/swatches.cpp @@ -35,6 +35,9 @@ #include "preferences.h" #include "sp-item.h" #include "svg/svg-color.h" +#include "sp-gradient-fns.h" +#include "sp-gradient.h" +#include "sp-gradient-vector.h" #include "swatches.h" #include "widgets/eek-preview.h" @@ -42,6 +45,7 @@ namespace Inkscape { namespace UI { namespace Dialogs { + ColorItem::ColorItem(eek::ColorDef::ColorType type) : def(type), _isLive(false), @@ -284,10 +288,10 @@ static void dieDieDie( GtkObject *obj, gpointer user_data ) void ColorItem::_dropDataIn( GtkWidget */*widget*/, GdkDragContext */*drag_context*/, gint /*x*/, gint /*y*/, - GtkSelectionData *data, - guint info, + GtkSelectionData */*data*/, + guint /*info*/, guint /*event_time*/, - gpointer user_data) + gpointer /*user_data*/) { } @@ -979,15 +983,30 @@ SwatchesPanel& SwatchesPanel::getInstance() */ SwatchesPanel::SwatchesPanel(gchar const* prefsPath) : Inkscape::UI::Widget::Panel("", prefsPath, SP_VERB_DIALOG_SWATCHES, "", true), - _holder(0) + _holder(0), + _clear(0), + _remove(0), + _currentIndex(0), + _currentDesktop(0), + _currentDocument(0), + _ptr(0) { Gtk::RadioMenuItem* hotItem = 0; _holder = new PreviewHolder(); _clear = new ColorItem( eek::ColorDef::CLEAR ); _remove = new ColorItem( eek::ColorDef::NONE ); + { + JustForNow *docPalette = new JustForNow(); + + docPalette->_name = "Auto"; + possible.push_back(docPalette); + + _ptr = docPalette; + } loadEmUp(); if ( !possible.empty() ) { JustForNow* first = 0; + int index = 0; Glib::ustring targetName; if ( !_prefs_path.empty() ) { Inkscape::Preferences *prefs = Inkscape::Preferences::get(); @@ -998,24 +1017,19 @@ SwatchesPanel::SwatchesPanel(gchar const* prefsPath) : first = *iter; break; } + index++; } } } if ( !first ) { first = possible.front(); + _currentIndex = 0; + } else { + _currentIndex = index; } - if ( first->_prefWidth > 0 ) { - _holder->setColumnPref( first->_prefWidth ); - } - _holder->freezeUpdates(); - // TODO restore once 'clear' works _holder->addPreview(_clear); - _holder->addPreview(_remove); - for ( std::vector::iterator it = first->_colors.begin(); it != first->_colors.end(); it++ ) { - _holder->addPreview(*it); - } - _holder->thawUpdates(); + _rebuild(); Gtk::RadioMenuItem::Group groupOne; @@ -1045,6 +1059,9 @@ SwatchesPanel::SwatchesPanel(gchar const* prefsPath) : SwatchesPanel::~SwatchesPanel() { + _documentConnection.disconnect(); + _resourceConnection.disconnect(); + if ( _clear ) { delete _clear; } @@ -1067,36 +1084,129 @@ void SwatchesPanel::setOrientation( Gtk::AnchorType how ) } } +void SwatchesPanel::setDesktop( SPDesktop* desktop ) +{ + if ( desktop != _currentDesktop ) { + if ( _currentDesktop ) { + _documentConnection.disconnect(); + } + + _currentDesktop = desktop; + + if ( desktop ) { + sigc::bound_mem_functor1 first = sigc::mem_fun(*this, &SwatchesPanel::_setDocument); + + sigc::slot base2 = first; + + sigc::slot slot2 = sigc::hide<0>( base2 ); + _documentConnection = desktop->connectDocumentReplaced( slot2 ); + + _setDocument( desktop->doc() ); + } else { + _setDocument(0); + } + } +} + +void SwatchesPanel::_setDocument( SPDocument *document ) +{ + if ( document != _currentDocument ) { + if ( _currentDocument ) { + _resourceConnection.disconnect(); + } + _currentDocument = document; + if ( _currentDocument ) { + _resourceConnection = sp_document_resources_changed_connect(document, + "gradient", + sigc::mem_fun(*this, &SwatchesPanel::_handleGradientsChange)); + } + + _handleGradientsChange(); + } +} + +void SwatchesPanel::_handleGradientsChange() +{ + std::vector newList; + + const GSList *gradients = sp_document_get_resource_list(_currentDocument, "gradient"); + for (const GSList *item = gradients; item; item = item->next) { + SPGradient* grad = SP_GRADIENT(item->data); + if ( grad->has_stops ) { + newList.push_back(SP_GRADIENT(item->data)); + } + } + + if ( _ptr ) { + JustForNow *docPalette = reinterpret_cast(_ptr); + // TODO delete pointed to objects + docPalette->_colors.clear(); + if ( !newList.empty() ) { + for ( std::vector::iterator it = newList.begin(); it != newList.end(); ++it ) + { + if ( (*it)->vector.stops.size() == 2 ) { + SPGradientStop first = (*it)->vector.stops[0]; + SPGradientStop second = (*it)->vector.stops[1]; + if ((first.offset <0.0001) && (static_cast(second.offset * 100.0f) == 100)) { + SPColor color = first.color; + guint32 together = color.toRGBA32(0); + Glib::ustring name((*it)->id); + unsigned int r = SP_RGBA32_R_U(together); + unsigned int g = SP_RGBA32_G_U(together); + unsigned int b = SP_RGBA32_B_U(together); + ColorItem* item = new ColorItem( r, g, b, name ); + docPalette->_colors.push_back(item); + } + } + } + } + JustForNow* curr = possible[_currentIndex]; + if (curr == docPalette) { + _rebuild(); + } + } +} + void SwatchesPanel::_handleAction( int setId, int itemId ) { switch( setId ) { case 3: { if ( itemId >= 0 && itemId < static_cast(possible.size()) ) { - _holder->clear(); - JustForNow* curr = possible[itemId]; + _currentIndex = itemId; if ( !_prefs_path.empty() ) { Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - prefs->setString(_prefs_path + "/palette", curr->_name); + prefs->setString(_prefs_path + "/palette", possible[_currentIndex]->_name); } - if ( curr->_prefWidth > 0 ) { - _holder->setColumnPref( curr->_prefWidth ); - } - _holder->freezeUpdates(); - // TODO restore once 'clear' works _holder->addPreview(_clear); - _holder->addPreview(_remove); - for ( std::vector::iterator it = curr->_colors.begin(); it != curr->_colors.end(); it++ ) { - _holder->addPreview(*it); - } - _holder->thawUpdates(); + _rebuild(); } } break; } } +void SwatchesPanel::_rebuild() +{ + JustForNow* curr = possible[_currentIndex]; + _holder->clear(); + + if ( curr->_prefWidth > 0 ) { + _holder->setColumnPref( curr->_prefWidth ); + } + _holder->freezeUpdates(); + // TODO restore once 'clear' works _holder->addPreview(_clear); + _holder->addPreview(_remove); + for ( std::vector::iterator it = curr->_colors.begin(); it != curr->_colors.end(); it++ ) { + _holder->addPreview(*it); + } + _holder->thawUpdates(); +} + + + + } //namespace Dialogs } //namespace UI } //namespace Inkscape diff --git a/src/ui/dialog/swatches.h b/src/ui/dialog/swatches.h index 560719ed2..ed311242e 100644 --- a/src/ui/dialog/swatches.h +++ b/src/ui/dialog/swatches.h @@ -91,8 +91,13 @@ public: static SwatchesPanel& getInstance(); virtual void setOrientation( Gtk::AnchorType how ); + virtual void setDesktop( SPDesktop* desktop ); + protected: virtual void _handleAction( int setId, int itemId ); + virtual void _handleGradientsChange(); + virtual void _setDocument( SPDocument *document ); + virtual void _rebuild(); private: SwatchesPanel(SwatchesPanel const &); // no copy @@ -103,6 +108,13 @@ private: PreviewHolder* _holder; ColorItem* _clear; ColorItem* _remove; + int _currentIndex; + SPDesktop* _currentDesktop; + SPDocument* _currentDocument; + void* _ptr; + + sigc::connection _documentConnection; + sigc::connection _resourceConnection; }; } //namespace Dialogs diff --git a/src/widgets/desktop-widget.cpp b/src/widgets/desktop-widget.cpp index 6550ca0cd..5904ccc79 100644 --- a/src/widgets/desktop-widget.cpp +++ b/src/widgets/desktop-widget.cpp @@ -314,10 +314,9 @@ sp_desktop_widget_init (SPDesktopWidget *dtw) { using Inkscape::UI::Dialogs::SwatchesPanel; - SwatchesPanel* swatches = new SwatchesPanel("/embedded/swatches"); - swatches->setOrientation( Gtk::ANCHOR_SOUTH ); - dtw->panels = GTK_WIDGET(swatches->gobj()); - gtk_box_pack_end( GTK_BOX( dtw->vbox ), dtw->panels, FALSE, TRUE, 0 ); + dtw->panels = new SwatchesPanel("/embedded/swatches"); + dtw->panels->setOrientation( Gtk::ANCHOR_SOUTH ); + gtk_box_pack_end( GTK_BOX( dtw->vbox ), GTK_WIDGET(dtw->panels->gobj()), FALSE, TRUE, 0 ); } hbox = gtk_hbox_new (FALSE, 0); @@ -1244,9 +1243,9 @@ sp_desktop_widget_layout (SPDesktopWidget *dtw) } if (!prefs->getBool(pref_root + "panels/state", true)) { - gtk_widget_hide_all( dtw->panels ); + gtk_widget_hide_all( GTK_WIDGET(dtw->panels->gobj()) ); } else { - gtk_widget_show_all( dtw->panels ); + gtk_widget_show_all( GTK_WIDGET(dtw->panels->gobj()) ); } if (!prefs->getBool(pref_root + "scrollbars/state", true)) { @@ -1362,6 +1361,8 @@ sp_desktop_widget_new (SPNamedView *namedview) sp_commands_toolbox_set_desktop (dtw->commands_toolbox, dtw->desktop); sp_snap_toolbox_set_desktop (dtw->snap_toolbox, dtw->desktop); + dtw->panels->setDesktop( dtw->desktop ); + return SP_VIEW_WIDGET (dtw); } diff --git a/src/widgets/desktop-widget.h b/src/widgets/desktop-widget.h index 3c6434efa..04146cac6 100644 --- a/src/widgets/desktop-widget.h +++ b/src/widgets/desktop-widget.h @@ -61,6 +61,8 @@ namespace Inkscape { namespace Widgets { class LayerSelector; } } namespace Inkscape { namespace UI { namespace Widget { class SelectedStyle; } } } +namespace Inkscape { namespace UI { namespace Dialogs { class SwatchesPanel; } } } + /// A GtkEventBox on an SPDesktop. struct SPDesktopWidget { SPViewWidget viewwidget; @@ -80,7 +82,7 @@ struct SPDesktopWidget { GtkWidget *menubar, *statusbar; - GtkWidget *panels; + Inkscape::UI::Dialogs::SwatchesPanel *panels; GtkWidget *hscrollbar, *vscrollbar, *vscrollbar_box; -- 2.30.2