1 /* === S Y N F I G ========================================================= */
2 /*! \file widget_gradient.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "widget_gradient.h"
35 #include <gtkmm/menu.h>
36 #include <synfig/exception.h>
41 /* === U S I N G =========================================================== */
45 using namespace synfig;
46 using namespace studio;
48 /* === M A C R O S ========================================================= */
50 /* === G L O B A L S ======================================================= */
52 /* === P R O C E D U R E S ================================================= */
55 studio::render_gradient_to_window(const Glib::RefPtr<Gdk::Drawable>& window,const Gdk::Rectangle& ca,const synfig::Gradient &gradient)
57 int height = ca.get_height();
58 int width = ca.get_width()-4;
60 float sample_width(1.0f/(float)width);
61 Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
62 const Color bg1(0.25, 0.25, 0.25);
63 const Color bg2(0.5, 0.5, 0.5);
68 const Color c(gradient(float(i)/float(width),sample_width));
69 const Color c1(Color::blend(c,bg1,1.0).clamped());
70 const Color c2(Color::blend(c,bg2,1.0).clamped());
71 gushort r1(256*App::gamma.r_F32_to_U8(c1.get_r()));
72 gushort g1(256*App::gamma.g_F32_to_U8(c1.get_g()));
73 gushort b1(256*App::gamma.b_F32_to_U8(c1.get_b()));
74 gushort r2(256*App::gamma.r_F32_to_U8(c2.get_r()));
75 gushort g2(256*App::gamma.g_F32_to_U8(c2.get_g()));
76 gushort b2(256*App::gamma.b_F32_to_U8(c2.get_b()));
80 gdk_c.set_rgb(r1,g1,b1);
81 gc->set_rgb_fg_color(gdk_c);
82 window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y(), 1, height/2);
84 gdk_c.set_rgb(r2,g2,b2);
85 gc->set_rgb_fg_color(gdk_c);
86 window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y()+height/2, 1, height/2);
90 gdk_c.set_rgb(r2,g2,b2);
91 gc->set_rgb_fg_color(gdk_c);
92 window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y(), 1, height/2);
94 gdk_c.set_rgb(r1,g1,b1);
95 gc->set_rgb_fg_color(gdk_c);
96 window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y()+height/2, 1, height/2);
99 gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
100 window->draw_rectangle(gc, false, ca.get_x()+1, ca.get_y()+1, ca.get_width()-3, height-3);
101 gc->set_rgb_fg_color(Gdk::Color("#000000"));
102 window->draw_rectangle(gc, false, ca.get_x(), ca.get_y(), ca.get_width()-1, height-1);
105 /* === M E T H O D S ======================================================= */
107 Widget_Gradient::Widget_Gradient():
110 set_size_request(-1,64);
111 signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Gradient::redraw));
112 add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
113 add_events(Gdk::BUTTON1_MOTION_MASK);
117 Widget_Gradient::~Widget_Gradient()
121 #define CONTROL_HEIGHT 16
123 Widget_Gradient::redraw(GdkEventExpose */*bleh*/)
125 const int h(get_height());
126 const int w(get_width());
128 Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
129 Gdk::Rectangle area(0,0,w,h);
132 render_gradient_to_window(get_window(),area,gradient_);
136 render_gradient_to_window(get_window(),Gdk::Rectangle(0,0,w,h-CONTROL_HEIGHT),gradient_);
138 gc->set_rgb_fg_color(Gdk::Color("#7f7f7f"));
139 get_window()->draw_rectangle(gc, false, 0, h-CONTROL_HEIGHT, w, CONTROL_HEIGHT);
141 Gradient::iterator iter,selected_iter;
142 bool show_selected(false);
143 for(iter=gradient_.begin();iter!=gradient_.end();iter++)
145 if(*iter!=selected_cpoint)
146 get_style()->paint_arrow(
148 (*iter==selected_cpoint)?Gtk::STATE_SELECTED:Gtk::STATE_ACTIVE,
155 int(iter->pos*w)-CONTROL_HEIGHT/2+1,
167 // we do this so that we can be sure that
168 // the selected marker is shown on top
171 get_style()->paint_arrow(
180 round_to_int(selected_iter->pos*w)-CONTROL_HEIGHT/2+1,
191 Widget_Gradient::insert_cpoint(float x)
193 Gradient::CPoint new_cpoint;
195 new_cpoint.color=gradient_(x);
196 gradient_.push_back(new_cpoint);
199 set_selected_cpoint(new_cpoint);
204 Widget_Gradient::remove_cpoint(float x)
206 gradient_.erase(gradient_.proximity(x));
207 signal_value_changed_();
212 Widget_Gradient::popup_menu(float x)
214 Gtk::Menu* menu(manage(new Gtk::Menu()));
216 menu->items().clear();
218 menu->items().push_back(
219 Gtk::Menu_Helpers::MenuElem(
222 sigc::mem_fun(*this,&studio::Widget_Gradient::insert_cpoint),
228 if(!gradient_.empty())
230 menu->items().push_back(
231 Gtk::Menu_Helpers::MenuElem(
234 sigc::mem_fun(*this,&studio::Widget_Gradient::remove_cpoint),
245 Widget_Gradient::set_value(const synfig::Gradient& x)
249 set_selected_cpoint(*gradient_.proximity(0.0f));
254 Widget_Gradient::set_selected_cpoint(const synfig::Gradient::CPoint &x)
257 signal_cpoint_selected_(selected_cpoint);
262 Widget_Gradient::update_cpoint(const synfig::Gradient::CPoint &x)
266 Gradient::iterator iter(gradient_.find(x));
272 catch(synfig::Exception::NotFound)
279 Widget_Gradient::on_event(GdkEvent *event)
283 const int x(static_cast<int>(event->button.x));
284 const int y(static_cast<int>(event->button.y));
286 float pos((float)x/(float)get_width());
287 if(pos<0.0f)pos=0.0f;
288 if(pos>1.0f)pos=1.0f;
292 case GDK_MOTION_NOTIFY:
293 if(editable_ && y>get_height()-CONTROL_HEIGHT)
295 Gradient::iterator iter(gradient_.find(selected_cpoint));
297 if(event->button.state&GDK_SHIFT_MASK)
299 float begin(-100000000),end(100000000);
300 Gradient::iterator before(iter),after(iter);
302 if(iter!=gradient_.begin())
307 if(after!=gradient_.end())
325 // signal_value_changed_();
331 case GDK_BUTTON_PRESS:
333 if(event->button.button==1)
335 if(editable_ && y>get_height()-CONTROL_HEIGHT)
337 set_selected_cpoint(*gradient_.proximity(pos));
347 else if(editable_ && event->button.button==3)
353 case GDK_BUTTON_RELEASE:
354 if(editable_ && event->button.button==1 && y>get_height()-CONTROL_HEIGHT)
356 set_selected_cpoint(*gradient_.proximity(pos));
357 if(changed_)signal_value_changed_();