color-entry.cpp revision e1f71774e8765a6a85edc11cf7520a1643ef1025
2N/A/** @file
2N/A * Entry widget for typing color value in css form
2N/A *//*
2N/A * Authors:
2N/A * Tomasz Boczkowski <penginsbacon@gmail.com>
2N/A *
2N/A * Copyright (C) 2014 Authors
2N/A * Released under GNU GPL, read the file 'COPYING' for more information
2N/A */
2N/A
2N/A#ifdef HAVE_CONFIG_H
2N/A# include <config.h>
2N/A#endif
2N/A
2N/A#if GLIBMM_DISABLE_DEPRECATED && HAVE_GLIBMM_THREADS_H
2N/A#include <glibmm/threads.h>
2N/A#endif
2N/A
2N/A#include <iomanip>
2N/A#include <glibmm/i18n.h>
2N/A
2N/A#include "color-entry.h"
2N/A
2N/Anamespace Inkscape {
2N/Anamespace UI {
2N/Anamespace Widget {
2N/A
2N/AColorEntry::ColorEntry(SelectedColor &color)
2N/A : _color(color)
2N/A , _updating(false)
2N/A{
2N/A _color_changed_connection = color.signal_changed.connect(sigc::mem_fun(this, &ColorEntry::_onColorChanged));
2N/A _color_dragged_connection = color.signal_dragged.connect(sigc::mem_fun(this, &ColorEntry::_onColorChanged));
2N/A _onColorChanged();
2N/A
2N/A set_max_length(8);
2N/A set_width_chars(8);
2N/A set_tooltip_text(_("Hexadecimal RGBA value of the color"));
2N/A}
2N/A
2N/AColorEntry::~ColorEntry() {
2N/A _color_changed_connection.disconnect();
2N/A _color_dragged_connection.disconnect();
2N/A}
2N/A
2N/Avoid ColorEntry::on_changed() {
2N/A if (_updating) {
2N/A return;
2N/A }
2N/A
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));
}
if (changed) {
set_text(str);
}
SPColor color(rgba);
_color.setColorAlpha(color, SP_RGBA32_A_F(rgba));
}
g_free(str);
}
void ColorEntry::_onColorChanged() {
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 :