9d984cf9e0ac635ba75d0f59abeea7be09ffc308
[synfig.git] / synfig-studio / src / gtkmm / dialog_color.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dialog_color.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_color.h"
33 #include "widget_color.h"
34 #include <synfig/general.h>
35 #include <synfigapp/main.h>
36 #include <gtkmm/button.h>
37 #include "app.h"
38
39 #include "general.h"
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48 using namespace studio;
49
50 /* === M A C R O S ========================================================= */
51
52 /* === G L O B A L S ======================================================= */
53
54 /* === P R O C E D U R E S ================================================= */
55
56 /* === M E T H O D S ======================================================= */
57
58 Dialog_Color::Dialog_Color():
59         Dialog(_("Colors"), false, true),
60         dialog_settings(this, "color"),
61         busy_(false)
62 {
63         set_type_hint(Gdk::WINDOW_TYPE_HINT_UTILITY);
64
65         create_color_edit_widget();
66         create_set_color_button("synfig-set_outline_color", _("Set as Outline"), 0,
67                         sigc::mem_fun(*this, &Dialog_Color::on_set_oc_pressed));
68         create_set_color_button("synfig-set_fill_color", _("Set as Fill"), 1,
69                         sigc::mem_fun(*this, &Dialog_Color::on_set_fc_pressed));
70         create_close_button();
71
72         add_accel_group(App::ui_manager()->get_accel_group());
73         show_all_children();
74 }
75
76 Dialog_Color::~Dialog_Color()
77 {
78 }
79
80 void
81 Dialog_Color::create_color_edit_widget()
82 {
83         color_edit_widget = manage(new Gtk::ColorSelection());
84         color_edit_widget->set_has_opacity_control(true);
85         color_edit_widget->signal_color_changed().connect(sigc::mem_fun(*this,
86                         &studio::Dialog_Color::on_color_changed));
87         get_vbox()->pack_start(*color_edit_widget);
88 }
89
90 void
91 Dialog_Color::create_set_color_button(const char *stock_id,
92                 const Glib::ustring& tip_text, int index,
93                 const sigc::slot0<void>& callback)
94 {
95         Gtk::Button *set_color_button = manage(new Gtk::Button());
96         Gtk::Image *set_color_icon = manage(new Gtk::Image(Gtk::StockID(stock_id),
97                         Gtk::IconSize::IconSize(Gtk::ICON_SIZE_BUTTON)));
98         set_color_button->add(*set_color_icon);
99         set_color_icon->show();
100         tooltips.set_tip(*set_color_button, tip_text);
101         set_color_button->show();
102         add_action_widget(*set_color_button, index);
103         set_color_button->signal_clicked().connect(callback);
104 }
105
106 void
107 Dialog_Color::create_close_button()
108 {
109         Gtk::Button *close_button(manage(new Gtk::Button(Gtk::StockID("gtk-close"))));
110         close_button->show();
111         add_action_widget(*close_button, 2);
112         close_button->signal_clicked().connect(sigc::hide_return(sigc::mem_fun(*this,
113                         &Dialog_Color::on_close_pressed)));
114         signal_delete_event().connect(sigc::hide(sigc::mem_fun(*this,
115                         &Dialog_Color::on_close_pressed)));
116 }
117
118 void
119 Dialog_Color::on_color_changed()
120 {
121         busy_ = true;
122         signal_edited_(get_color());
123         busy_ = false;
124 }
125
126 void
127 Dialog_Color::on_set_oc_pressed()
128 {
129         busy_ = true;
130         synfigapp::Main::set_outline_color(get_color());
131         busy_ = false;
132 }
133
134 void
135 Dialog_Color::on_set_fc_pressed()
136 {
137         busy_ = true;
138         synfigapp::Main::set_fill_color(get_color());
139         busy_ = false;
140 }
141
142 bool
143 Dialog_Color::on_close_pressed()
144 {
145         busy_ = false;
146         grab_focus();
147         reset();
148         hide();
149         return true;
150 }
151
152 void
153 Dialog_Color::set_color(const synfig::Color& color)
154 {
155                 color_edit_widget->set_current_color(studio::colorconv_synfig2gdk(color));
156                 color_edit_widget->set_current_alpha(color.get_a());
157 }
158
159 synfig::Color
160 Dialog_Color::get_color() const
161 {
162                 return studio::colorconv_gdk2synfig(color_edit_widget->get_current_color(), color_edit_widget->get_current_alpha() / 65535.0);
163 }
164
165 void
166 Dialog_Color::reset()
167 {
168         signal_edited_.clear();
169 }