summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/color-picker.cpp
blob: 009ec8ea3695407711ab9821b67c6b4997b56dcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *   Lauris Kaplinski <lauris@kaplinski.com>
 *   bulia byak <buliabyak@users.sf.net>
 *   Ralf Stephan <ralf@ark.in-berlin.de>
 *   Abhishek Sharma
 *
 * Copyright (C) Authors 2000-2005
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "color-picker.h"

#include "inkscape.h"
#include "desktop.h"
#include "document.h"
#include "document-undo.h"

#include "ui/dialog-events.h"
#include "ui/widget/color-notebook.h"


static bool _in_use = false;

namespace Inkscape {
namespace UI {
namespace Widget {

ColorPicker::ColorPicker (const Glib::ustring& title, const Glib::ustring& tip,
                          guint32 rgba, bool undo, Gtk::Button* external_button)
    : _preview(new ColorPreview(rgba))
    , _title(title)
    , _rgba(rgba)
    , _undo(undo)
    , _colorSelectorDialog("dialogs.colorpickerwindow")
{
    Gtk::Button* button = external_button ? external_button : this;
    _color_selector = nullptr;
    setupDialog(title);
    _preview->show();
    button->add(*Gtk::manage(_preview));
    // set tooltip if given, otherwise leave original tooltip in place (from external button)
    if (!tip.empty()) {
        button->set_tooltip_text(tip);
    }
    _selected_color.signal_changed.connect(sigc::mem_fun(this, &ColorPicker::_onSelectedColorChanged));
    _selected_color.signal_dragged.connect(sigc::mem_fun(this, &ColorPicker::_onSelectedColorChanged));
    _selected_color.signal_released.connect(sigc::mem_fun(this, &ColorPicker::_onSelectedColorChanged));

    if (external_button) {
        external_button->signal_clicked().connect([=](){ on_clicked(); });
    }
}

ColorPicker::~ColorPicker()
{
    closeWindow();
}

void ColorPicker::setupDialog(const Glib::ustring &title)
{
    GtkWidget *dlg = GTK_WIDGET(_colorSelectorDialog.gobj());
    sp_transientize(dlg);

    _colorSelectorDialog.hide();
    _colorSelectorDialog.set_title (title);
    _colorSelectorDialog.set_border_width (4);
}

void ColorPicker::setSensitive(bool sensitive) { set_sensitive(sensitive); }

void ColorPicker::setRgba32 (guint32 rgba)
{
    if (_in_use) return;

    set_preview(rgba);
    _rgba = rgba;
    if (_color_selector)
    {
        _updating = true;
        _selected_color.setValue(rgba);
        _updating = false;
    }
}

void ColorPicker::closeWindow()
{
    _colorSelectorDialog.hide();
}

void ColorPicker::open() {
    on_clicked();
}

void ColorPicker::on_clicked()
{
    if (!_color_selector) {
        auto selector = Gtk::manage(new ColorNotebook(_selected_color));
        selector->set_label(_title);
        _color_selector = selector;
        _colorSelectorDialog.get_content_area()->pack_start(*_color_selector, true, true, 0);
        _color_selector->show();
    }

    _updating = true;
    _selected_color.setValue(_rgba);
    _updating = false;

    _colorSelectorDialog.show();
    Glib::RefPtr<Gdk::Window> window = _colorSelectorDialog.get_parent_window();
    if (window) {
        window->focus(1);
    }
}

void ColorPicker::on_changed (guint32)
{
}

void ColorPicker::_onSelectedColorChanged() {
    if (_updating) {
        return;
    }

    if (_in_use) {
        return;
    } else {
        _in_use = true;
    }

    guint32 rgba = _selected_color.value();
    set_preview(rgba);

    if (_undo && SP_ACTIVE_DESKTOP) {
        DocumentUndo::done(SP_ACTIVE_DESKTOP->getDocument(), /* TODO: annotate */ "color-picker.cpp:129", "");
    }

    on_changed(rgba);
    _in_use = false;
    _changed_signal.emit(rgba);
    _rgba = rgba;
}

void ColorPicker::set_preview(guint32 rgba) {
    _preview->setRgba32(_ignore_transparency ? rgba | 0xff : rgba);
}

void ColorPicker::use_transparency(bool enable) {
    _ignore_transparency = !enable;
    set_preview(_rgba);
}

}//namespace Widget
}//namespace UI
}//namespace Inkscape


/*
  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 :