diff options
| author | Tomasz Boczkowski <penginsbacon@gmail.com> | 2014-05-30 20:53:40 +0000 |
|---|---|---|
| committer | Tomasz Boczkowski <penginsbacon@gmail.com> | 2014-05-30 20:53:40 +0000 |
| commit | b0e9181bacdad19055532a2620f5390815b91727 (patch) | |
| tree | bd858d525a12a5cd3dffd3d365f6f2882514d874 /src | |
| parent | changed coding style of SelectedColor (diff) | |
| download | inkscape-b0e9181bacdad19055532a2620f5390815b91727.tar.gz inkscape-b0e9181bacdad19055532a2620f5390815b91727.zip | |
added ColorEntry class, refactored SPColorNotebook to use it
(bzr r13341.6.31)
Diffstat (limited to 'src')
| -rw-r--r-- | src/ui/widget/Makefile_insert | 2 | ||||
| -rw-r--r-- | src/ui/widget/color-entry.cpp | 115 | ||||
| -rw-r--r-- | src/ui/widget/color-entry.h | 59 | ||||
| -rw-r--r-- | src/widgets/sp-color-notebook.cpp | 104 | ||||
| -rw-r--r-- | src/widgets/sp-color-notebook.h | 6 |
5 files changed, 206 insertions, 80 deletions
diff --git a/src/ui/widget/Makefile_insert b/src/ui/widget/Makefile_insert index 6deaf6671..bad3342b1 100644 --- a/src/ui/widget/Makefile_insert +++ b/src/ui/widget/Makefile_insert @@ -6,6 +6,8 @@ ink_common_sources += \ ui/widget/attr-widget.h \ ui/widget/button.h \ ui/widget/button.cpp \ + ui/widget/color-entry.cpp \ + ui/widget/color-entry.h \ ui/widget/color-picker.cpp \ ui/widget/color-picker.h \ ui/widget/color-preview.cpp \ diff --git a/src/ui/widget/color-entry.cpp b/src/ui/widget/color-entry.cpp new file mode 100644 index 000000000..223c86fd3 --- /dev/null +++ b/src/ui/widget/color-entry.cpp @@ -0,0 +1,115 @@ +/** @file + * Entry widget for typing color value in css form + *//* + * Authors: + * Tomasz Boczkowski <penginsbacon@gmail.com> + * + * Copyright (C) 2014 Authors + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#if GLIBMM_DISABLE_DEPRECATED && HAVE_GLIBMM_THREADS_H +#include <glibmm/threads.h> +#endif + +#include <iomanip> +#include <glibmm/i18n.h> + +#include "color-entry.h" + +namespace Inkscape { +namespace UI { +namespace Widget { + +ColorEntry::ColorEntry(SelectedColor &color) + : _color(color) + , _updating(false) +{ + _color_changed_connection = color.signal_changed.connect(sigc::mem_fun(this, &ColorEntry::_onColorChanged)); + _onColorChanged(); + + set_max_length(8); + set_width_chars(8); + set_tooltip_text(_("Hexadecimal RGBA value of the color")); +} + +ColorEntry::~ColorEntry() { + _color_changed_connection.disconnect(); +} + +void ColorEntry::on_changed() { + if (_updating) { + return; + } + + Glib::ustring text = get_text(); + bool changed = false; + + //Coerce the value format to eight hex digits + if (!text.empty() && text[0] == '#') { + changed = true; + text.erase(0, 1); + if (text.size() == 6) { + // it was a standard RGB hex + unsigned int alph = SP_COLOR_F_TO_U(_color.alpha()); + Glib::ustring tmp = Glib::ustring::format(std::hex, std::setw(2), std::setfill(L'0'), alph); + text += tmp; + } + } + + gchar* str = g_strdup(text.c_str()); + gchar* end = 0; + guint64 rgba = g_ascii_strtoull(str, &end, 16); + if (end != str) { + ptrdiff_t len = end - str; + if (len < 8) { + rgba = rgba << (4 * (8 - len)); + } + _updating = true; + if (changed) { + set_text(str); + } + SPColor color(rgba); + _color.setColorAlpha(color, SP_RGBA32_A_F(rgba), true); + _updating = false; + } + g_free(str); +} + + +void ColorEntry::_onColorChanged() { + if (_updating) { + return; + } + + SPColor color = _color.color(); + gdouble alpha = _color.alpha(); + + guint32 rgba = color.toRGBA32( alpha ); + Glib::ustring text = Glib::ustring::format(std::hex, std::setw(8), std::setfill(L'0'), rgba); + + Glib::ustring old_text = get_text(); + if (old_text != text) { + _updating = true; + set_text(text); + _updating = false; + } +} + +} +} +} +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/ui/widget/color-entry.h b/src/ui/widget/color-entry.h new file mode 100644 index 000000000..742324337 --- /dev/null +++ b/src/ui/widget/color-entry.h @@ -0,0 +1,59 @@ +/** @file + * Entry widget for typing color value in css form + *//* + * Authors: + * Tomasz Boczkowski <penginsbacon@gmail.com> + * + * Copyright (C) 2014 Authors + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#ifndef SEEN_COLOR_ENTRY_H +#define SEEN_COLOR_ENTRY_H_ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#if GLIBMM_DISABLE_DEPRECATED && HAVE_GLIBMM_THREADS_H +#include <glibmm/threads.h> +#endif + +#include <gtkmm/entry.h> +#include "ui/selected-color.h" + +namespace Inkscape { +namespace UI { +namespace Widget { + +class ColorEntry: public Gtk::Entry { +public: + ColorEntry(SelectedColor &color); + virtual ~ColorEntry(); + +protected: + void on_changed(); + +private: + void _onColorChanged(); + + SelectedColor &_color; + sigc::connection _color_changed_connection; + bool _updating; +}; + +} +} +} + +#endif +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/widgets/sp-color-notebook.cpp b/src/widgets/sp-color-notebook.cpp index 364e81ae5..ac110b86c 100644 --- a/src/widgets/sp-color-notebook.cpp +++ b/src/widgets/sp-color-notebook.cpp @@ -41,9 +41,12 @@ #include "cms-system.h" #include "tools-switch.h" #include "ui/tools/tool-base.h" +#include "ui/widget/color-entry.h" using Inkscape::CMSSystem; +using namespace Inkscape::UI::Widget; + struct SPColorNotebookTracker { const gchar* name; const gchar* className; @@ -156,8 +159,7 @@ void ColorNotebook::init() { guint row = 0; - _updating = FALSE; - _updatingrgba = FALSE; + _updating = false; _book = gtk_notebook_new (); gtk_widget_show (_book); @@ -324,13 +326,10 @@ void ColorNotebook::init() gtk_misc_set_alignment (GTK_MISC (_rgbal), 1.0, 0.5); gtk_box_pack_start(GTK_BOX(rgbabox), _rgbal, TRUE, TRUE, 2); - _rgbae = gtk_entry_new (); - sp_dialog_defocus_on_enter (_rgbae); - gtk_entry_set_max_length (GTK_ENTRY (_rgbae), 8); - gtk_entry_set_width_chars (GTK_ENTRY (_rgbae), 8); - gtk_widget_set_tooltip_text (_rgbae, _("Hexadecimal RGBA value of the color")); - gtk_box_pack_start(GTK_BOX(rgbabox), _rgbae, FALSE, FALSE, 0); - gtk_label_set_mnemonic_widget (GTK_LABEL(_rgbal), _rgbae); + ColorEntry* rgba_entry = Gtk::manage(new ColorEntry(_selected_color)); + sp_dialog_defocus_on_enter (GTK_WIDGET(rgba_entry->gobj())); + gtk_box_pack_start(GTK_BOX(rgbabox), GTK_WIDGET(rgba_entry->gobj()), FALSE, FALSE, 0); + gtk_label_set_mnemonic_widget (GTK_LABEL(_rgbal), GTK_WIDGET(rgba_entry->gobj())); sp_set_font_size_smaller (rgbabox); gtk_widget_show_all (rgbabox); @@ -359,7 +358,7 @@ void ColorNotebook::init() _switchId = g_signal_connect(G_OBJECT (_book), "switch-page", G_CALLBACK (sp_color_notebook_switch_page), SP_COLOR_NOTEBOOK(_csel)); - _entryId = g_signal_connect (G_OBJECT (_rgbae), "changed", G_CALLBACK (ColorNotebook::_rgbaEntryChangedHook), _csel); + _selected_color.signal_changed.connect(sigc::mem_fun(this, &ColorNotebook::_onSelectedColorChanged)); } static void sp_color_notebook_dispose(GObject *object) @@ -449,13 +448,13 @@ ColorNotebook::Page::Page(Inkscape::UI::ColorSelectorFactory *selector_factory, void ColorNotebook::_colorChanged() { + _selected_color.setColorAlpha(_color, _alpha, true); + SPColorSelector* cselPage = getCurrentSelector(); if ( cselPage ) { cselPage->base->setColorAlpha( _color, _alpha ); } - - _updateRgbaEntry( _color, _alpha ); } void ColorNotebook::_picker_clicked(GtkWidget * /*widget*/, SPColorNotebook * /*colorbook*/) @@ -466,52 +465,6 @@ void ColorNotebook::_picker_clicked(GtkWidget * /*widget*/, SPColorNotebook * /* Inkscape::UI::Tools::sp_toggle_dropper(SP_ACTIVE_DESKTOP); } -void ColorNotebook::_rgbaEntryChangedHook(GtkEntry *entry, SPColorNotebook *colorbook) -{ - (dynamic_cast<ColorNotebook*>(SP_COLOR_SELECTOR(colorbook)->base))->_rgbaEntryChanged( entry ); -} - -void ColorNotebook::_rgbaEntryChanged(GtkEntry* entry) -{ - if (_updating) return; - if (_updatingrgba) return; - - const gchar *t = gtk_entry_get_text( entry ); - - if (t) { - Glib::ustring text = t; - bool changed = false; - if (!text.empty() && text[0] == '#') { - changed = true; - text.erase(0,1); - if (text.size() == 6) { - // it was a standard RGB hex - unsigned int alph = SP_COLOR_F_TO_U(_alpha); - gchar* tmp = g_strdup_printf("%02x", alph); - text += tmp; - g_free(tmp); - } - } - gchar* str = g_strdup(text.c_str()); - gchar* end = 0; - guint64 rgba = g_ascii_strtoull( str, &end, 16 ); - if ( end != str ) { - ptrdiff_t len = end - str; - if ( len < 8 ) { - rgba = rgba << ( 4 * ( 8 - len ) ); - } - _updatingrgba = TRUE; - if ( changed ) { - gtk_entry_set_text( entry, str ); - } - SPColor color( rgba ); - setColorAlpha( color, SP_RGBA32_A_F(rgba), true ); - _updatingrgba = FALSE; - } - g_free(str); - } -} - // TODO pass in param so as to avoid the need for SP_ACTIVE_DOCUMENT void ColorNotebook::_updateRgbaEntry( const SPColor& color, gfloat alpha ) { @@ -550,24 +503,6 @@ void ColorNotebook::_updateRgbaEntry( const SPColor& color, gfloat alpha ) } } #endif //defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) - - if ( !_updatingrgba ) - { - gchar s[32]; - guint32 rgba; - - /* Update RGBA entry */ - rgba = color.toRGBA32( alpha ); - - g_snprintf (s, 32, "%08x", rgba); - const gchar* oldText = gtk_entry_get_text( GTK_ENTRY( _rgbae ) ); - if ( strcmp( oldText, s ) != 0 ) - { - g_signal_handler_block( _rgbae, _entryId ); - gtk_entry_set_text( GTK_ENTRY(_rgbae), s ); - g_signal_handler_unblock( _rgbae, _entryId ); - } - } } void ColorNotebook::_setCurrentPage(int i) @@ -644,10 +579,25 @@ void ColorNotebook::_entryModified (SPColorSelector *csel, SPColorNotebook *colo gfloat alpha = 1.0; csel->base->getColorAlpha( color, alpha ); - nb->_updateRgbaEntry( color, alpha ); + + nb->_updating = true; + nb->_selected_color.setColorAlpha(color, alpha, true); + nb->_updating = false; nb->_updateInternals( color, alpha, nb->_dragging ); } +void ColorNotebook::_onSelectedColorChanged() { + if (_updating) { + return; + } + + SPColor color; + gfloat alpha = 1.0; + + _selected_color.colorAlpha(color, alpha); + _updateInternals(color, alpha, _dragging); +} + GtkWidget* ColorNotebook::_addPage(Page& page) { Gtk::Widget *selector_widget; diff --git a/src/widgets/sp-color-notebook.h b/src/widgets/sp-color-notebook.h index f688d9210..9ab98f1e9 100644 --- a/src/widgets/sp-color-notebook.h +++ b/src/widgets/sp-color-notebook.h @@ -50,7 +50,6 @@ protected: bool enabled_full; }; - static void _rgbaEntryChangedHook( GtkEntry* entry, SPColorNotebook *colorbook ); static void _entryGrabbed( SPColorSelector *csel, SPColorNotebook *colorbook ); static void _entryDragged( SPColorSelector *csel, SPColorNotebook *colorbook ); static void _entryReleased( SPColorSelector *csel, SPColorNotebook *colorbook ); @@ -61,7 +60,8 @@ protected: virtual void _colorChanged(); - void _rgbaEntryChanged( GtkEntry* entry ); + virtual void _onSelectedColorChanged(); + void _updateRgbaEntry( const SPColor& color, gfloat alpha ); void _setCurrentPage(int i); @@ -76,7 +76,7 @@ protected: GtkWidget *_book; GtkWidget *_buttonbox; GtkWidget **_buttons; - GtkWidget *_rgbal, *_rgbae; /* RGBA entry */ + GtkWidget *_rgbal; /* RGBA entry */ #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) GtkWidget *_box_outofgamut, *_box_colormanaged, *_box_toomuchink; #endif //defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) |
