Remove all occurences of the old Widget_ColorEdit.
[synfig.git] / synfig-studio / src / gtkmm / dialog_gradient.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dialog_gradient.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "dialog_gradient.h"
33 #include "widget_gradient.h"
34 #include <gtkmm/frame.h>
35 #include <gtkmm/table.h>
36 #include <gtkmm/label.h>
37 #include <synfig/general.h>
38 #include <synfigapp/canvasinterface.h>
39 #include <synfigapp/value_desc.h>
40 #include <synfigapp/main.h>
41 #include "widget_color.h"
42 #include <gtkmm/spinbutton.h>
43 #include "app.h"
44
45 #include "general.h"
46
47 #endif
48
49 /* === U S I N G =========================================================== */
50
51 using namespace std;
52 using namespace etl;
53 using namespace synfig;
54 using namespace studio;
55
56 /* === M A C R O S ========================================================= */
57
58 /* === G L O B A L S ======================================================= */
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 Dialog_Gradient::Dialog_Gradient():
65         Dialog(_("Gradient Editor"),false,true),
66         dialog_settings(this,"gradient"),
67         adjustment_pos(0,0.0,1.0,0.001,0.001,0)
68 {
69         set_type_hint(Gdk::WINDOW_TYPE_HINT_DIALOG);
70         set_keep_above(false);
71
72         set_role("gradient_editor");
73
74         // Setup the buttons
75         grab_button = manage(new class Gtk::Button(Gtk::StockID(_("Set as Default"))));
76         grab_button->show();
77         add_action_widget(*grab_button,2);
78         grab_button->signal_clicked().connect(sigc::mem_fun(*this, &Dialog_Gradient::on_grab_pressed));
79
80         /*
81         Gtk::Button *apply_button(manage(new class Gtk::Button(Gtk::StockID("gtk-apply"))));
82         apply_button->show();
83         add_action_widget(*apply_button,1);
84         apply_button->signal_clicked().connect(sigc::mem_fun(*this, &Dialog_Gradient::on_apply_pressed));
85         */
86
87         Gtk::Button *cancel_button(manage(new class Gtk::Button(Gtk::StockID("gtk-close"))));
88         cancel_button->show();
89         add_action_widget(*cancel_button,0);
90         cancel_button->signal_clicked().connect(sigc::mem_fun(*this, &Dialog_Gradient::hide));
91
92         set_border_width(12);
93         Gtk::Table* table(manage(new Gtk::Table(3,1,false)));
94         table->set_col_spacings(12);
95         table->set_row_spacings(12);
96         get_vbox()->pack_start(*table);
97
98         widget_gradient=manage(new Widget_Gradient());
99         widget_gradient->set_editable();
100         widget_gradient->signal_cpoint_selected().connect(sigc::mem_fun(*this,&studio::Dialog_Gradient::on_cpoint_selected));
101         widget_gradient->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Dialog_Gradient::on_changed));
102         //table->attach(*manage(new Gtk::Label(_("Not yet fully implemented"))), 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
103         table->attach(*widget_gradient, 0, 1, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
104
105         widget_color=manage(new Gtk::ColorSelection());
106         widget_color->set_has_opacity_control(true);
107         widget_color->signal_color_changed().connect(sigc::mem_fun(*this,&studio::Dialog_Gradient::on_values_adjusted));
108         widget_color->signal_color_changed().connect(sigc::mem_fun(*this,&studio::Dialog_Gradient::on_changed));
109         //widget_color->signal_activated().connect(sigc::mem_fun(*this,&studio::Dialog_Gradient::on_values_adjusted));
110         table->attach(*widget_color, 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
111
112
113         spinbutton_pos=manage(new class Gtk::SpinButton(adjustment_pos,0.0001,4));
114         spinbutton_pos->set_update_policy(Gtk::UPDATE_ALWAYS);
115         adjustment_pos.signal_value_changed().connect(sigc::mem_fun(*this,&studio::Dialog_Gradient::on_values_adjusted));
116         adjustment_pos.signal_value_changed().connect(sigc::mem_fun(*this,&studio::Dialog_Gradient::on_changed));
117         table->attach(*spinbutton_pos, 0, 1, 1, 2, Gtk::SHRINK|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
118
119
120         add_accel_group(App::ui_manager()->get_accel_group());
121
122         show_all_children();
123 }
124
125 Dialog_Gradient::~Dialog_Gradient()
126 {
127 }
128
129 void
130 Dialog_Gradient::set_gradient(const synfig::Gradient& x)
131 {
132         widget_gradient->set_value(x);
133 }
134
135 void
136 Dialog_Gradient::reset()
137 {
138         value_changed_connection.disconnect();
139         signal_edited_.clear();
140 }
141
142 void
143 Dialog_Gradient::on_grab_pressed()
144 {
145         synfigapp::Main::set_gradient(get_gradient());
146 //      signal_edited_(get_gradient());
147 //      reset();
148 //      hide();
149 }
150
151 void
152 Dialog_Gradient::on_apply_pressed()
153 {
154         signal_edited_(get_gradient());
155 }
156
157 void
158 Dialog_Gradient::on_changed()
159 {
160         signal_edited_(get_gradient());
161 }
162
163 void
164 Dialog_Gradient::on_cpoint_selected(synfig::Gradient::CPoint x)
165 {
166         widget_color->set_current_color(studio::colorconv_synfig2gdk(x.color));
167         widget_color->set_current_alpha(x.color.get_a() * 65535);
168         adjustment_pos.set_value(x.pos);
169 }
170
171 void
172 Dialog_Gradient::on_values_adjusted()
173 {
174         synfig::Gradient::CPoint x(widget_gradient->get_selected_cpoint());
175         x.color=studio::colorconv_gdk2synfig(widget_color->get_current_color(), widget_color->get_current_alpha() / 65535.0);
176         x.pos=adjustment_pos.get_value();
177         widget_gradient->update_cpoint(x);
178 }
179
180 static void
181 dialog_gradient_value_desc_edit(synfig::Gradient /*g*/,synfigapp::ValueDesc /*x*/,handle<synfigapp::CanvasInterface> /*canvas_interface*/)
182 {
183 //      canvas_interface->connect_value(x,ValueBase(g));
184 }
185
186 void
187 Dialog_Gradient::edit(const synfigapp::ValueDesc &x, etl::handle<synfigapp::CanvasInterface> canvas_interface, synfig::Time time)
188 {
189         reset();
190         if(x.is_const())
191                 set_gradient(x.get_value().get(Gradient()));
192         else if(x.is_value_node())
193                 set_gradient((*x.get_value_node())(time).get(Gradient()));
194
195         signal_edited().connect(
196                 sigc::bind(
197                         sigc::bind(
198                                 sigc::ptr_fun(dialog_gradient_value_desc_edit),
199                                 canvas_interface
200                         ),
201                         x
202                 )
203         );
204
205         present();
206 }